]> code.delx.au - gnu-emacs/blob - src/xdisp.c
Merged in changes from CVS trunk.
[gnu-emacs] / src / xdisp.c
1 /* Display generation from window structure and buffer text.
2 Copyright (C) 1985,86,87,88,93,94,95,97,98,99,2000,01,02,03,04
3 Free Software Foundation, Inc.
4
5 This file is part of GNU Emacs.
6
7 GNU Emacs is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
21
22 /* New redisplay written by Gerd Moellmann <gerd@gnu.org>.
23
24 Redisplay.
25
26 Emacs separates the task of updating the display from code
27 modifying global state, e.g. buffer text. This way functions
28 operating on buffers don't also have to be concerned with updating
29 the display.
30
31 Updating the display is triggered by the Lisp interpreter when it
32 decides it's time to do it. This is done either automatically for
33 you as part of the interpreter's command loop or as the result of
34 calling Lisp functions like `sit-for'. The C function `redisplay'
35 in xdisp.c is the only entry into the inner redisplay code. (Or,
36 let's say almost---see the description of direct update
37 operations, below.)
38
39 The following diagram shows how redisplay code is invoked. As you
40 can see, Lisp calls redisplay and vice versa. Under window systems
41 like X, some portions of the redisplay code are also called
42 asynchronously during mouse movement or expose events. It is very
43 important that these code parts do NOT use the C library (malloc,
44 free) because many C libraries under Unix are not reentrant. They
45 may also NOT call functions of the Lisp interpreter which could
46 change the interpreter's state. If you don't follow these rules,
47 you will encounter bugs which are very hard to explain.
48
49 (Direct functions, see below)
50 direct_output_for_insert,
51 direct_forward_char (dispnew.c)
52 +---------------------------------+
53 | |
54 | V
55 +--------------+ redisplay +----------------+
56 | Lisp machine |---------------->| Redisplay code |<--+
57 +--------------+ (xdisp.c) +----------------+ |
58 ^ | |
59 +----------------------------------+ |
60 Don't use this path when called |
61 asynchronously! |
62 |
63 expose_window (asynchronous) |
64 |
65 X expose events -----+
66
67 What does redisplay do? Obviously, it has to figure out somehow what
68 has been changed since the last time the display has been updated,
69 and to make these changes visible. Preferably it would do that in
70 a moderately intelligent way, i.e. fast.
71
72 Changes in buffer text can be deduced from window and buffer
73 structures, and from some global variables like `beg_unchanged' and
74 `end_unchanged'. The contents of the display are additionally
75 recorded in a `glyph matrix', a two-dimensional matrix of glyph
76 structures. Each row in such a matrix corresponds to a line on the
77 display, and each glyph in a row corresponds to a column displaying
78 a character, an image, or what else. This matrix is called the
79 `current glyph matrix' or `current matrix' in redisplay
80 terminology.
81
82 For buffer parts that have been changed since the last update, a
83 second glyph matrix is constructed, the so called `desired glyph
84 matrix' or short `desired matrix'. Current and desired matrix are
85 then compared to find a cheap way to update the display, e.g. by
86 reusing part of the display by scrolling lines.
87
88
89 Direct operations.
90
91 You will find a lot of redisplay optimizations when you start
92 looking at the innards of redisplay. The overall goal of all these
93 optimizations is to make redisplay fast because it is done
94 frequently.
95
96 Two optimizations are not found in xdisp.c. These are the direct
97 operations mentioned above. As the name suggests they follow a
98 different principle than the rest of redisplay. Instead of
99 building a desired matrix and then comparing it with the current
100 display, they perform their actions directly on the display and on
101 the current matrix.
102
103 One direct operation updates the display after one character has
104 been entered. The other one moves the cursor by one position
105 forward or backward. You find these functions under the names
106 `direct_output_for_insert' and `direct_output_forward_char' in
107 dispnew.c.
108
109
110 Desired matrices.
111
112 Desired matrices are always built per Emacs window. The function
113 `display_line' is the central function to look at if you are
114 interested. It constructs one row in a desired matrix given an
115 iterator structure containing both a buffer position and a
116 description of the environment in which the text is to be
117 displayed. But this is too early, read on.
118
119 Characters and pixmaps displayed for a range of buffer text depend
120 on various settings of buffers and windows, on overlays and text
121 properties, on display tables, on selective display. The good news
122 is that all this hairy stuff is hidden behind a small set of
123 interface functions taking an iterator structure (struct it)
124 argument.
125
126 Iteration over things to be displayed is then simple. It is
127 started by initializing an iterator with a call to init_iterator.
128 Calls to get_next_display_element fill the iterator structure with
129 relevant information about the next thing to display. Calls to
130 set_iterator_to_next move the iterator to the next thing.
131
132 Besides this, an iterator also contains information about the
133 display environment in which glyphs for display elements are to be
134 produced. It has fields for the width and height of the display,
135 the information whether long lines are truncated or continued, a
136 current X and Y position, and lots of other stuff you can better
137 see in dispextern.h.
138
139 Glyphs in a desired matrix are normally constructed in a loop
140 calling get_next_display_element and then produce_glyphs. The call
141 to produce_glyphs will fill the iterator structure with pixel
142 information about the element being displayed and at the same time
143 produce glyphs for it. If the display element fits on the line
144 being displayed, set_iterator_to_next is called next, otherwise the
145 glyphs produced are discarded.
146
147
148 Frame matrices.
149
150 That just couldn't be all, could it? What about terminal types not
151 supporting operations on sub-windows of the screen? To update the
152 display on such a terminal, window-based glyph matrices are not
153 well suited. To be able to reuse part of the display (scrolling
154 lines up and down), we must instead have a view of the whole
155 screen. This is what `frame matrices' are for. They are a trick.
156
157 Frames on terminals like above have a glyph pool. Windows on such
158 a frame sub-allocate their glyph memory from their frame's glyph
159 pool. The frame itself is given its own glyph matrices. By
160 coincidence---or maybe something else---rows in window glyph
161 matrices are slices of corresponding rows in frame matrices. Thus
162 writing to window matrices implicitly updates a frame matrix which
163 provides us with the view of the whole screen that we originally
164 wanted to have without having to move many bytes around. To be
165 honest, there is a little bit more done, but not much more. If you
166 plan to extend that code, take a look at dispnew.c. The function
167 build_frame_matrix is a good starting point. */
168
169 #include <config.h>
170 #include <stdio.h>
171
172 #include "lisp.h"
173 #include "keyboard.h"
174 #include "frame.h"
175 #include "window.h"
176 #include "termchar.h"
177 #include "dispextern.h"
178 #include "buffer.h"
179 #include "charset.h"
180 #include "indent.h"
181 #include "commands.h"
182 #include "keymap.h"
183 #include "macros.h"
184 #include "disptab.h"
185 #include "termhooks.h"
186 #include "intervals.h"
187 #include "coding.h"
188 #include "process.h"
189 #include "region-cache.h"
190 #include "fontset.h"
191 #include "blockinput.h"
192
193 #ifdef HAVE_X_WINDOWS
194 #include "xterm.h"
195 #endif
196 #ifdef WINDOWSNT
197 #include "w32term.h"
198 #endif
199 #ifdef MAC_OS
200 #include "macterm.h"
201 #endif
202
203 #ifndef FRAME_X_OUTPUT
204 #define FRAME_X_OUTPUT(f) ((f)->output_data.x)
205 #endif
206
207 #define INFINITY 10000000
208
209 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
210 || defined (USE_GTK)
211 extern void set_frame_menubar P_ ((struct frame *f, int, int));
212 extern int pending_menu_activation;
213 #endif
214
215 extern int interrupt_input;
216 extern int command_loop_level;
217
218 extern 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 /* The variable `resize-mini-windows'. If nil, don't resize
674 mini-windows. If t, always resize them to fit the text they
675 display. If `grow-only', let mini-windows grow only until they
676 become empty. */
677
678 Lisp_Object Vresize_mini_windows;
679
680 /* Buffer being redisplayed -- for redisplay_window_error. */
681
682 struct buffer *displayed_buffer;
683
684 /* Value returned from text property handlers (see below). */
685
686 enum prop_handled
687 {
688 HANDLED_NORMALLY,
689 HANDLED_RECOMPUTE_PROPS,
690 HANDLED_OVERLAY_STRING_CONSUMED,
691 HANDLED_RETURN
692 };
693
694 /* A description of text properties that redisplay is interested
695 in. */
696
697 struct props
698 {
699 /* The name of the property. */
700 Lisp_Object *name;
701
702 /* A unique index for the property. */
703 enum prop_idx idx;
704
705 /* A handler function called to set up iterator IT from the property
706 at IT's current position. Value is used to steer handle_stop. */
707 enum prop_handled (*handler) P_ ((struct it *it));
708 };
709
710 static enum prop_handled handle_face_prop P_ ((struct it *));
711 static enum prop_handled handle_invisible_prop P_ ((struct it *));
712 static enum prop_handled handle_display_prop P_ ((struct it *));
713 static enum prop_handled handle_composition_prop P_ ((struct it *));
714 static enum prop_handled handle_overlay_change P_ ((struct it *));
715 static enum prop_handled handle_fontified_prop P_ ((struct it *));
716
717 /* Properties handled by iterators. */
718
719 static struct props it_props[] =
720 {
721 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
722 /* Handle `face' before `display' because some sub-properties of
723 `display' need to know the face. */
724 {&Qface, FACE_PROP_IDX, handle_face_prop},
725 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
726 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
727 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
728 {NULL, 0, NULL}
729 };
730
731 /* Value is the position described by X. If X is a marker, value is
732 the marker_position of X. Otherwise, value is X. */
733
734 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
735
736 /* Enumeration returned by some move_it_.* functions internally. */
737
738 enum move_it_result
739 {
740 /* Not used. Undefined value. */
741 MOVE_UNDEFINED,
742
743 /* Move ended at the requested buffer position or ZV. */
744 MOVE_POS_MATCH_OR_ZV,
745
746 /* Move ended at the requested X pixel position. */
747 MOVE_X_REACHED,
748
749 /* Move within a line ended at the end of a line that must be
750 continued. */
751 MOVE_LINE_CONTINUED,
752
753 /* Move within a line ended at the end of a line that would
754 be displayed truncated. */
755 MOVE_LINE_TRUNCATED,
756
757 /* Move within a line ended at a line end. */
758 MOVE_NEWLINE_OR_CR
759 };
760
761 /* This counter is used to clear the face cache every once in a while
762 in redisplay_internal. It is incremented for each redisplay.
763 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
764 cleared. */
765
766 #define CLEAR_FACE_CACHE_COUNT 500
767 static int clear_face_cache_count;
768
769 /* Non-zero while redisplay_internal is in progress. */
770
771 int redisplaying_p;
772
773 /* Non-zero means don't free realized faces. Bound while freeing
774 realized faces is dangerous because glyph matrices might still
775 reference them. */
776
777 int inhibit_free_realized_faces;
778 Lisp_Object Qinhibit_free_realized_faces;
779
780 /* If a string, XTread_socket generates an event to display that string.
781 (The display is done in read_char.) */
782
783 Lisp_Object help_echo_string;
784 Lisp_Object help_echo_window;
785 Lisp_Object help_echo_object;
786 int help_echo_pos;
787
788 /* Temporary variable for XTread_socket. */
789
790 Lisp_Object previous_help_echo_string;
791
792 /* Null glyph slice */
793
794 static struct glyph_slice null_glyph_slice = { 0, 0, 0, 0 };
795
796 \f
797 /* Function prototypes. */
798
799 static void setup_for_ellipsis P_ ((struct it *));
800 static void mark_window_display_accurate_1 P_ ((struct window *, int));
801 static int single_display_prop_string_p P_ ((Lisp_Object, Lisp_Object));
802 static int display_prop_string_p P_ ((Lisp_Object, Lisp_Object));
803 static int cursor_row_p P_ ((struct window *, struct glyph_row *));
804 static int redisplay_mode_lines P_ ((Lisp_Object, int));
805 static char *decode_mode_spec_coding P_ ((Lisp_Object, char *, int));
806
807 #if 0
808 static int invisible_text_between_p P_ ((struct it *, int, int));
809 #endif
810
811 static int next_element_from_ellipsis P_ ((struct it *));
812 static void pint2str P_ ((char *, int, int));
813 static void pint2hrstr P_ ((char *, int, int));
814 static struct text_pos run_window_scroll_functions P_ ((Lisp_Object,
815 struct text_pos));
816 static void reconsider_clip_changes P_ ((struct window *, struct buffer *));
817 static int text_outside_line_unchanged_p P_ ((struct window *, int, int));
818 static void store_frame_title_char P_ ((char));
819 static int store_frame_title P_ ((const unsigned char *, int, int));
820 static void x_consider_frame_title P_ ((Lisp_Object));
821 static void handle_stop P_ ((struct it *));
822 static int tool_bar_lines_needed P_ ((struct frame *));
823 static int single_display_prop_intangible_p P_ ((Lisp_Object));
824 static void ensure_echo_area_buffers P_ ((void));
825 static Lisp_Object unwind_with_echo_area_buffer P_ ((Lisp_Object));
826 static Lisp_Object with_echo_area_buffer_unwind_data P_ ((struct window *));
827 static int with_echo_area_buffer P_ ((struct window *, int,
828 int (*) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
829 EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
830 static void clear_garbaged_frames P_ ((void));
831 static int current_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
832 static int truncate_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
833 static int set_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
834 static int display_echo_area P_ ((struct window *));
835 static int display_echo_area_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
836 static int resize_mini_window_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
837 static Lisp_Object unwind_redisplay P_ ((Lisp_Object));
838 static int string_char_and_length P_ ((const unsigned char *, int, int *));
839 static struct text_pos display_prop_end P_ ((struct it *, Lisp_Object,
840 struct text_pos));
841 static int compute_window_start_on_continuation_line P_ ((struct window *));
842 static Lisp_Object safe_eval_handler P_ ((Lisp_Object));
843 static void insert_left_trunc_glyphs P_ ((struct it *));
844 static struct glyph_row *get_overlay_arrow_glyph_row P_ ((struct window *,
845 Lisp_Object));
846 static void extend_face_to_end_of_line P_ ((struct it *));
847 static int append_space_for_newline P_ ((struct it *, int));
848 static int make_cursor_line_fully_visible P_ ((struct window *, int));
849 static int try_scrolling P_ ((Lisp_Object, int, EMACS_INT, EMACS_INT, int, int));
850 static int try_cursor_movement P_ ((Lisp_Object, struct text_pos, int *));
851 static int trailing_whitespace_p P_ ((int));
852 static int message_log_check_duplicate P_ ((int, int, int, int));
853 static void push_it P_ ((struct it *));
854 static void pop_it P_ ((struct it *));
855 static void sync_frame_with_window_matrix_rows P_ ((struct window *));
856 static void select_frame_for_redisplay P_ ((Lisp_Object));
857 static void redisplay_internal P_ ((int));
858 static int echo_area_display P_ ((int));
859 static void redisplay_windows P_ ((Lisp_Object));
860 static void redisplay_window P_ ((Lisp_Object, int));
861 static Lisp_Object redisplay_window_error ();
862 static Lisp_Object redisplay_window_0 P_ ((Lisp_Object));
863 static Lisp_Object redisplay_window_1 P_ ((Lisp_Object));
864 static void update_menu_bar P_ ((struct frame *, int));
865 static int try_window_reusing_current_matrix P_ ((struct window *));
866 static int try_window_id P_ ((struct window *));
867 static int display_line P_ ((struct it *));
868 static int display_mode_lines P_ ((struct window *));
869 static int display_mode_line P_ ((struct window *, enum face_id, Lisp_Object));
870 static int display_mode_element P_ ((struct it *, int, int, int, Lisp_Object, Lisp_Object, int));
871 static int store_mode_line_string P_ ((char *, Lisp_Object, int, int, int, Lisp_Object));
872 static char *decode_mode_spec P_ ((struct window *, int, int, int, int *));
873 static void display_menu_bar P_ ((struct window *));
874 static int display_count_lines P_ ((int, int, int, int, int *));
875 static int display_string P_ ((unsigned char *, Lisp_Object, Lisp_Object,
876 int, int, struct it *, int, int, int, int));
877 static void compute_line_metrics P_ ((struct it *));
878 static void run_redisplay_end_trigger_hook P_ ((struct it *));
879 static int get_overlay_strings P_ ((struct it *, int));
880 static void next_overlay_string P_ ((struct it *));
881 static void reseat P_ ((struct it *, struct text_pos, int));
882 static void reseat_1 P_ ((struct it *, struct text_pos, int));
883 static void back_to_previous_visible_line_start P_ ((struct it *));
884 static void reseat_at_previous_visible_line_start P_ ((struct it *));
885 static void reseat_at_next_visible_line_start P_ ((struct it *, int));
886 static int next_element_from_display_vector P_ ((struct it *));
887 static int next_element_from_string P_ ((struct it *));
888 static int next_element_from_c_string P_ ((struct it *));
889 static int next_element_from_buffer P_ ((struct it *));
890 static int next_element_from_composition P_ ((struct it *));
891 static int next_element_from_image P_ ((struct it *));
892 static int next_element_from_stretch P_ ((struct it *));
893 static void load_overlay_strings P_ ((struct it *, int));
894 static int init_from_display_pos P_ ((struct it *, struct window *,
895 struct display_pos *));
896 static void reseat_to_string P_ ((struct it *, unsigned char *,
897 Lisp_Object, int, int, int, int));
898 static enum move_it_result move_it_in_display_line_to P_ ((struct it *,
899 int, int, int));
900 void move_it_vertically_backward P_ ((struct it *, int));
901 static void init_to_row_start P_ ((struct it *, struct window *,
902 struct glyph_row *));
903 static int init_to_row_end P_ ((struct it *, struct window *,
904 struct glyph_row *));
905 static void back_to_previous_line_start P_ ((struct it *));
906 static int forward_to_next_line_start P_ ((struct it *, int *));
907 static struct text_pos string_pos_nchars_ahead P_ ((struct text_pos,
908 Lisp_Object, int));
909 static struct text_pos string_pos P_ ((int, Lisp_Object));
910 static struct text_pos c_string_pos P_ ((int, unsigned char *, int));
911 static int number_of_chars P_ ((unsigned char *, int));
912 static void compute_stop_pos P_ ((struct it *));
913 static void compute_string_pos P_ ((struct text_pos *, struct text_pos,
914 Lisp_Object));
915 static int face_before_or_after_it_pos P_ ((struct it *, int));
916 static int next_overlay_change P_ ((int));
917 static int handle_single_display_prop P_ ((struct it *, Lisp_Object,
918 Lisp_Object, struct text_pos *,
919 int));
920 static int underlying_face_id P_ ((struct it *));
921 static int in_ellipses_for_invisible_text_p P_ ((struct display_pos *,
922 struct window *));
923
924 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
925 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
926
927 #ifdef HAVE_WINDOW_SYSTEM
928
929 static void update_tool_bar P_ ((struct frame *, int));
930 static void build_desired_tool_bar_string P_ ((struct frame *f));
931 static int redisplay_tool_bar P_ ((struct frame *));
932 static void display_tool_bar_line P_ ((struct it *));
933 static void notice_overwritten_cursor P_ ((struct window *,
934 enum glyph_row_area,
935 int, int, int, int));
936
937
938
939 #endif /* HAVE_WINDOW_SYSTEM */
940
941 \f
942 /***********************************************************************
943 Window display dimensions
944 ***********************************************************************/
945
946 /* Return the bottom boundary y-position for text lines in window W.
947 This is the first y position at which a line cannot start.
948 It is relative to the top of the window.
949
950 This is the height of W minus the height of a mode line, if any. */
951
952 INLINE int
953 window_text_bottom_y (w)
954 struct window *w;
955 {
956 int height = WINDOW_TOTAL_HEIGHT (w);
957
958 if (WINDOW_WANTS_MODELINE_P (w))
959 height -= CURRENT_MODE_LINE_HEIGHT (w);
960 return height;
961 }
962
963 /* Return the pixel width of display area AREA of window W. AREA < 0
964 means return the total width of W, not including fringes to
965 the left and right of the window. */
966
967 INLINE int
968 window_box_width (w, area)
969 struct window *w;
970 int area;
971 {
972 int cols = XFASTINT (w->total_cols);
973 int pixels = 0;
974
975 if (!w->pseudo_window_p)
976 {
977 cols -= WINDOW_SCROLL_BAR_COLS (w);
978
979 if (area == TEXT_AREA)
980 {
981 if (INTEGERP (w->left_margin_cols))
982 cols -= XFASTINT (w->left_margin_cols);
983 if (INTEGERP (w->right_margin_cols))
984 cols -= XFASTINT (w->right_margin_cols);
985 pixels = -WINDOW_TOTAL_FRINGE_WIDTH (w);
986 }
987 else if (area == LEFT_MARGIN_AREA)
988 {
989 cols = (INTEGERP (w->left_margin_cols)
990 ? XFASTINT (w->left_margin_cols) : 0);
991 pixels = 0;
992 }
993 else if (area == RIGHT_MARGIN_AREA)
994 {
995 cols = (INTEGERP (w->right_margin_cols)
996 ? XFASTINT (w->right_margin_cols) : 0);
997 pixels = 0;
998 }
999 }
1000
1001 return cols * WINDOW_FRAME_COLUMN_WIDTH (w) + pixels;
1002 }
1003
1004
1005 /* Return the pixel height of the display area of window W, not
1006 including mode lines of W, if any. */
1007
1008 INLINE int
1009 window_box_height (w)
1010 struct window *w;
1011 {
1012 struct frame *f = XFRAME (w->frame);
1013 int height = WINDOW_TOTAL_HEIGHT (w);
1014
1015 xassert (height >= 0);
1016
1017 /* Note: the code below that determines the mode-line/header-line
1018 height is essentially the same as that contained in the macro
1019 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
1020 the appropriate glyph row has its `mode_line_p' flag set,
1021 and if it doesn't, uses estimate_mode_line_height instead. */
1022
1023 if (WINDOW_WANTS_MODELINE_P (w))
1024 {
1025 struct glyph_row *ml_row
1026 = (w->current_matrix && w->current_matrix->rows
1027 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
1028 : 0);
1029 if (ml_row && ml_row->mode_line_p)
1030 height -= ml_row->height;
1031 else
1032 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
1033 }
1034
1035 if (WINDOW_WANTS_HEADER_LINE_P (w))
1036 {
1037 struct glyph_row *hl_row
1038 = (w->current_matrix && w->current_matrix->rows
1039 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1040 : 0);
1041 if (hl_row && hl_row->mode_line_p)
1042 height -= hl_row->height;
1043 else
1044 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1045 }
1046
1047 /* With a very small font and a mode-line that's taller than
1048 default, we might end up with a negative height. */
1049 return max (0, height);
1050 }
1051
1052 /* Return the window-relative coordinate of the left edge of display
1053 area AREA of window W. AREA < 0 means return the left edge of the
1054 whole window, to the right of the left fringe of W. */
1055
1056 INLINE int
1057 window_box_left_offset (w, area)
1058 struct window *w;
1059 int area;
1060 {
1061 int x;
1062
1063 if (w->pseudo_window_p)
1064 return 0;
1065
1066 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1067
1068 if (area == TEXT_AREA)
1069 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1070 + window_box_width (w, LEFT_MARGIN_AREA));
1071 else if (area == RIGHT_MARGIN_AREA)
1072 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1073 + window_box_width (w, LEFT_MARGIN_AREA)
1074 + window_box_width (w, TEXT_AREA)
1075 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1076 ? 0
1077 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1078 else if (area == LEFT_MARGIN_AREA
1079 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1080 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1081
1082 return x;
1083 }
1084
1085
1086 /* Return the window-relative coordinate of the right edge of display
1087 area AREA of window W. AREA < 0 means return the left edge of the
1088 whole window, to the left of the right fringe of W. */
1089
1090 INLINE int
1091 window_box_right_offset (w, area)
1092 struct window *w;
1093 int area;
1094 {
1095 return window_box_left_offset (w, area) + window_box_width (w, area);
1096 }
1097
1098 /* Return the frame-relative coordinate of the left edge of display
1099 area AREA of window W. AREA < 0 means return the left edge of the
1100 whole window, to the right of the left fringe of W. */
1101
1102 INLINE int
1103 window_box_left (w, area)
1104 struct window *w;
1105 int area;
1106 {
1107 struct frame *f = XFRAME (w->frame);
1108 int x;
1109
1110 if (w->pseudo_window_p)
1111 return FRAME_INTERNAL_BORDER_WIDTH (f);
1112
1113 x = (WINDOW_LEFT_EDGE_X (w)
1114 + window_box_left_offset (w, area));
1115
1116 return x;
1117 }
1118
1119
1120 /* Return the frame-relative coordinate of the right edge of display
1121 area AREA of window W. AREA < 0 means return the left edge of the
1122 whole window, to the left of the right fringe of W. */
1123
1124 INLINE int
1125 window_box_right (w, area)
1126 struct window *w;
1127 int area;
1128 {
1129 return window_box_left (w, area) + window_box_width (w, area);
1130 }
1131
1132 /* Get the bounding box of the display area AREA of window W, without
1133 mode lines, in frame-relative coordinates. AREA < 0 means the
1134 whole window, not including the left and right fringes of
1135 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1136 coordinates of the upper-left corner of the box. Return in
1137 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1138
1139 INLINE void
1140 window_box (w, area, box_x, box_y, box_width, box_height)
1141 struct window *w;
1142 int area;
1143 int *box_x, *box_y, *box_width, *box_height;
1144 {
1145 if (box_width)
1146 *box_width = window_box_width (w, area);
1147 if (box_height)
1148 *box_height = window_box_height (w);
1149 if (box_x)
1150 *box_x = window_box_left (w, area);
1151 if (box_y)
1152 {
1153 *box_y = WINDOW_TOP_EDGE_Y (w);
1154 if (WINDOW_WANTS_HEADER_LINE_P (w))
1155 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1156 }
1157 }
1158
1159
1160 /* Get the bounding box of the display area AREA of window W, without
1161 mode lines. AREA < 0 means the whole window, not including the
1162 left and right fringe of the window. Return in *TOP_LEFT_X
1163 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1164 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1165 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1166 box. */
1167
1168 INLINE void
1169 window_box_edges (w, area, top_left_x, top_left_y,
1170 bottom_right_x, bottom_right_y)
1171 struct window *w;
1172 int area;
1173 int *top_left_x, *top_left_y, *bottom_right_x, *bottom_right_y;
1174 {
1175 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
1176 bottom_right_y);
1177 *bottom_right_x += *top_left_x;
1178 *bottom_right_y += *top_left_y;
1179 }
1180
1181
1182 \f
1183 /***********************************************************************
1184 Utilities
1185 ***********************************************************************/
1186
1187 /* Return the bottom y-position of the line the iterator IT is in.
1188 This can modify IT's settings. */
1189
1190 int
1191 line_bottom_y (it)
1192 struct it *it;
1193 {
1194 int line_height = it->max_ascent + it->max_descent;
1195 int line_top_y = it->current_y;
1196
1197 if (line_height == 0)
1198 {
1199 if (last_height)
1200 line_height = last_height;
1201 else if (IT_CHARPOS (*it) < ZV)
1202 {
1203 move_it_by_lines (it, 1, 1);
1204 line_height = (it->max_ascent || it->max_descent
1205 ? it->max_ascent + it->max_descent
1206 : last_height);
1207 }
1208 else
1209 {
1210 struct glyph_row *row = it->glyph_row;
1211
1212 /* Use the default character height. */
1213 it->glyph_row = NULL;
1214 it->what = IT_CHARACTER;
1215 it->c = ' ';
1216 it->len = 1;
1217 PRODUCE_GLYPHS (it);
1218 line_height = it->ascent + it->descent;
1219 it->glyph_row = row;
1220 }
1221 }
1222
1223 return line_top_y + line_height;
1224 }
1225
1226
1227 /* Return 1 if position CHARPOS is visible in window W. Set *FULLY to
1228 1 if POS is visible and the line containing POS is fully visible.
1229 EXACT_MODE_LINE_HEIGHTS_P non-zero means compute exact mode-line
1230 and header-lines heights. */
1231
1232 int
1233 pos_visible_p (w, charpos, fully, x, y, exact_mode_line_heights_p)
1234 struct window *w;
1235 int charpos, *fully, *x, *y, exact_mode_line_heights_p;
1236 {
1237 struct it it;
1238 struct text_pos top;
1239 int visible_p;
1240 struct buffer *old_buffer = NULL;
1241
1242 if (XBUFFER (w->buffer) != current_buffer)
1243 {
1244 old_buffer = current_buffer;
1245 set_buffer_internal_1 (XBUFFER (w->buffer));
1246 }
1247
1248 *fully = visible_p = 0;
1249 SET_TEXT_POS_FROM_MARKER (top, w->start);
1250
1251 /* Compute exact mode line heights, if requested. */
1252 if (exact_mode_line_heights_p)
1253 {
1254 if (WINDOW_WANTS_MODELINE_P (w))
1255 current_mode_line_height
1256 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1257 current_buffer->mode_line_format);
1258
1259 if (WINDOW_WANTS_HEADER_LINE_P (w))
1260 current_header_line_height
1261 = display_mode_line (w, HEADER_LINE_FACE_ID,
1262 current_buffer->header_line_format);
1263 }
1264
1265 start_display (&it, w, top);
1266 move_it_to (&it, charpos, 0, it.last_visible_y, -1,
1267 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
1268
1269 /* Note that we may overshoot because of invisible text. */
1270 if (IT_CHARPOS (it) >= charpos)
1271 {
1272 int top_y = it.current_y;
1273 int bottom_y = line_bottom_y (&it);
1274 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1275
1276 if (top_y < window_top_y)
1277 visible_p = bottom_y > window_top_y;
1278 else if (top_y < it.last_visible_y)
1279 {
1280 visible_p = 1;
1281 *fully = bottom_y <= it.last_visible_y;
1282 }
1283 if (visible_p && x)
1284 {
1285 *x = it.current_x;
1286 *y = max (top_y + it.max_ascent - it.ascent, window_top_y);
1287 }
1288 }
1289 else if (it.current_y + it.max_ascent + it.max_descent > it.last_visible_y)
1290 {
1291 struct it it2;
1292
1293 it2 = it;
1294 move_it_by_lines (&it, 1, 0);
1295 if (charpos < IT_CHARPOS (it))
1296 {
1297 visible_p = 1;
1298 if (x)
1299 {
1300 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1301 *x = it2.current_x;
1302 *y = it2.current_y + it2.max_ascent - it2.ascent;
1303 }
1304 }
1305 }
1306
1307 if (old_buffer)
1308 set_buffer_internal_1 (old_buffer);
1309
1310 current_header_line_height = current_mode_line_height = -1;
1311
1312 return visible_p;
1313 }
1314
1315
1316 /* Return the next character from STR which is MAXLEN bytes long.
1317 Return in *LEN the length of the character. This is like
1318 STRING_CHAR_AND_LENGTH but never returns an invalid character. If
1319 we find one, we return a `?', but with the length of the invalid
1320 character. */
1321
1322 static INLINE int
1323 string_char_and_length (str, maxlen, len)
1324 const unsigned char *str;
1325 int maxlen, *len;
1326 {
1327 int c;
1328
1329 c = STRING_CHAR_AND_LENGTH (str, maxlen, *len);
1330 if (!CHAR_VALID_P (c, 1))
1331 /* We may not change the length here because other places in Emacs
1332 don't use this function, i.e. they silently accept invalid
1333 characters. */
1334 c = '?';
1335
1336 return c;
1337 }
1338
1339
1340
1341 /* Given a position POS containing a valid character and byte position
1342 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1343
1344 static struct text_pos
1345 string_pos_nchars_ahead (pos, string, nchars)
1346 struct text_pos pos;
1347 Lisp_Object string;
1348 int nchars;
1349 {
1350 xassert (STRINGP (string) && nchars >= 0);
1351
1352 if (STRING_MULTIBYTE (string))
1353 {
1354 int rest = SBYTES (string) - BYTEPOS (pos);
1355 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1356 int len;
1357
1358 while (nchars--)
1359 {
1360 string_char_and_length (p, rest, &len);
1361 p += len, rest -= len;
1362 xassert (rest >= 0);
1363 CHARPOS (pos) += 1;
1364 BYTEPOS (pos) += len;
1365 }
1366 }
1367 else
1368 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1369
1370 return pos;
1371 }
1372
1373
1374 /* Value is the text position, i.e. character and byte position,
1375 for character position CHARPOS in STRING. */
1376
1377 static INLINE struct text_pos
1378 string_pos (charpos, string)
1379 int charpos;
1380 Lisp_Object string;
1381 {
1382 struct text_pos pos;
1383 xassert (STRINGP (string));
1384 xassert (charpos >= 0);
1385 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1386 return pos;
1387 }
1388
1389
1390 /* Value is a text position, i.e. character and byte position, for
1391 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1392 means recognize multibyte characters. */
1393
1394 static struct text_pos
1395 c_string_pos (charpos, s, multibyte_p)
1396 int charpos;
1397 unsigned char *s;
1398 int multibyte_p;
1399 {
1400 struct text_pos pos;
1401
1402 xassert (s != NULL);
1403 xassert (charpos >= 0);
1404
1405 if (multibyte_p)
1406 {
1407 int rest = strlen (s), len;
1408
1409 SET_TEXT_POS (pos, 0, 0);
1410 while (charpos--)
1411 {
1412 string_char_and_length (s, rest, &len);
1413 s += len, rest -= len;
1414 xassert (rest >= 0);
1415 CHARPOS (pos) += 1;
1416 BYTEPOS (pos) += len;
1417 }
1418 }
1419 else
1420 SET_TEXT_POS (pos, charpos, charpos);
1421
1422 return pos;
1423 }
1424
1425
1426 /* Value is the number of characters in C string S. MULTIBYTE_P
1427 non-zero means recognize multibyte characters. */
1428
1429 static int
1430 number_of_chars (s, multibyte_p)
1431 unsigned char *s;
1432 int multibyte_p;
1433 {
1434 int nchars;
1435
1436 if (multibyte_p)
1437 {
1438 int rest = strlen (s), len;
1439 unsigned char *p = (unsigned char *) s;
1440
1441 for (nchars = 0; rest > 0; ++nchars)
1442 {
1443 string_char_and_length (p, rest, &len);
1444 rest -= len, p += len;
1445 }
1446 }
1447 else
1448 nchars = strlen (s);
1449
1450 return nchars;
1451 }
1452
1453
1454 /* Compute byte position NEWPOS->bytepos corresponding to
1455 NEWPOS->charpos. POS is a known position in string STRING.
1456 NEWPOS->charpos must be >= POS.charpos. */
1457
1458 static void
1459 compute_string_pos (newpos, pos, string)
1460 struct text_pos *newpos, pos;
1461 Lisp_Object string;
1462 {
1463 xassert (STRINGP (string));
1464 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1465
1466 if (STRING_MULTIBYTE (string))
1467 *newpos = string_pos_nchars_ahead (pos, string,
1468 CHARPOS (*newpos) - CHARPOS (pos));
1469 else
1470 BYTEPOS (*newpos) = CHARPOS (*newpos);
1471 }
1472
1473 /* EXPORT:
1474 Return an estimation of the pixel height of mode or top lines on
1475 frame F. FACE_ID specifies what line's height to estimate. */
1476
1477 int
1478 estimate_mode_line_height (f, face_id)
1479 struct frame *f;
1480 enum face_id face_id;
1481 {
1482 #ifdef HAVE_WINDOW_SYSTEM
1483 if (FRAME_WINDOW_P (f))
1484 {
1485 int height = FONT_HEIGHT (FRAME_FONT (f));
1486
1487 /* This function is called so early when Emacs starts that the face
1488 cache and mode line face are not yet initialized. */
1489 if (FRAME_FACE_CACHE (f))
1490 {
1491 struct face *face = FACE_FROM_ID (f, face_id);
1492 if (face)
1493 {
1494 if (face->font)
1495 height = FONT_HEIGHT (face->font);
1496 if (face->box_line_width > 0)
1497 height += 2 * face->box_line_width;
1498 }
1499 }
1500
1501 return height;
1502 }
1503 #endif
1504
1505 return 1;
1506 }
1507
1508 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1509 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1510 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1511 not force the value into range. */
1512
1513 void
1514 pixel_to_glyph_coords (f, pix_x, pix_y, x, y, bounds, noclip)
1515 FRAME_PTR f;
1516 register int pix_x, pix_y;
1517 int *x, *y;
1518 NativeRectangle *bounds;
1519 int noclip;
1520 {
1521
1522 #ifdef HAVE_WINDOW_SYSTEM
1523 if (FRAME_WINDOW_P (f))
1524 {
1525 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1526 even for negative values. */
1527 if (pix_x < 0)
1528 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1529 if (pix_y < 0)
1530 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1531
1532 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1533 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1534
1535 if (bounds)
1536 STORE_NATIVE_RECT (*bounds,
1537 FRAME_COL_TO_PIXEL_X (f, pix_x),
1538 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1539 FRAME_COLUMN_WIDTH (f) - 1,
1540 FRAME_LINE_HEIGHT (f) - 1);
1541
1542 if (!noclip)
1543 {
1544 if (pix_x < 0)
1545 pix_x = 0;
1546 else if (pix_x > FRAME_TOTAL_COLS (f))
1547 pix_x = FRAME_TOTAL_COLS (f);
1548
1549 if (pix_y < 0)
1550 pix_y = 0;
1551 else if (pix_y > FRAME_LINES (f))
1552 pix_y = FRAME_LINES (f);
1553 }
1554 }
1555 #endif
1556
1557 *x = pix_x;
1558 *y = pix_y;
1559 }
1560
1561
1562 /* Given HPOS/VPOS in the current matrix of W, return corresponding
1563 frame-relative pixel positions in *FRAME_X and *FRAME_Y. If we
1564 can't tell the positions because W's display is not up to date,
1565 return 0. */
1566
1567 int
1568 glyph_to_pixel_coords (w, hpos, vpos, frame_x, frame_y)
1569 struct window *w;
1570 int hpos, vpos;
1571 int *frame_x, *frame_y;
1572 {
1573 #ifdef HAVE_WINDOW_SYSTEM
1574 if (FRAME_WINDOW_P (XFRAME (WINDOW_FRAME (w))))
1575 {
1576 int success_p;
1577
1578 xassert (hpos >= 0 && hpos < w->current_matrix->matrix_w);
1579 xassert (vpos >= 0 && vpos < w->current_matrix->matrix_h);
1580
1581 if (display_completed)
1582 {
1583 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
1584 struct glyph *glyph = row->glyphs[TEXT_AREA];
1585 struct glyph *end = glyph + min (hpos, row->used[TEXT_AREA]);
1586
1587 hpos = row->x;
1588 vpos = row->y;
1589 while (glyph < end)
1590 {
1591 hpos += glyph->pixel_width;
1592 ++glyph;
1593 }
1594
1595 /* If first glyph is partially visible, its first visible position is still 0. */
1596 if (hpos < 0)
1597 hpos = 0;
1598
1599 success_p = 1;
1600 }
1601 else
1602 {
1603 hpos = vpos = 0;
1604 success_p = 0;
1605 }
1606
1607 *frame_x = WINDOW_TO_FRAME_PIXEL_X (w, hpos);
1608 *frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, vpos);
1609 return success_p;
1610 }
1611 #endif
1612
1613 *frame_x = hpos;
1614 *frame_y = vpos;
1615 return 1;
1616 }
1617
1618
1619 #ifdef HAVE_WINDOW_SYSTEM
1620
1621 /* Find the glyph under window-relative coordinates X/Y in window W.
1622 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1623 strings. Return in *HPOS and *VPOS the row and column number of
1624 the glyph found. Return in *AREA the glyph area containing X.
1625 Value is a pointer to the glyph found or null if X/Y is not on
1626 text, or we can't tell because W's current matrix is not up to
1627 date. */
1628
1629 static struct glyph *
1630 x_y_to_hpos_vpos (w, x, y, hpos, vpos, dx, dy, area)
1631 struct window *w;
1632 int x, y;
1633 int *hpos, *vpos, *dx, *dy, *area;
1634 {
1635 struct glyph *glyph, *end;
1636 struct glyph_row *row = NULL;
1637 int x0, i;
1638
1639 /* Find row containing Y. Give up if some row is not enabled. */
1640 for (i = 0; i < w->current_matrix->nrows; ++i)
1641 {
1642 row = MATRIX_ROW (w->current_matrix, i);
1643 if (!row->enabled_p)
1644 return NULL;
1645 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1646 break;
1647 }
1648
1649 *vpos = i;
1650 *hpos = 0;
1651
1652 /* Give up if Y is not in the window. */
1653 if (i == w->current_matrix->nrows)
1654 return NULL;
1655
1656 /* Get the glyph area containing X. */
1657 if (w->pseudo_window_p)
1658 {
1659 *area = TEXT_AREA;
1660 x0 = 0;
1661 }
1662 else
1663 {
1664 if (x < window_box_left_offset (w, TEXT_AREA))
1665 {
1666 *area = LEFT_MARGIN_AREA;
1667 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1668 }
1669 else if (x < window_box_right_offset (w, TEXT_AREA))
1670 {
1671 *area = TEXT_AREA;
1672 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1673 }
1674 else
1675 {
1676 *area = RIGHT_MARGIN_AREA;
1677 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1678 }
1679 }
1680
1681 /* Find glyph containing X. */
1682 glyph = row->glyphs[*area];
1683 end = glyph + row->used[*area];
1684 x -= x0;
1685 while (glyph < end && x >= glyph->pixel_width)
1686 {
1687 x -= glyph->pixel_width;
1688 ++glyph;
1689 }
1690
1691 if (glyph == end)
1692 return NULL;
1693
1694 if (dx)
1695 {
1696 *dx = x;
1697 *dy = y - (row->y + row->ascent - glyph->ascent);
1698 }
1699
1700 *hpos = glyph - row->glyphs[*area];
1701 return glyph;
1702 }
1703
1704
1705 /* EXPORT:
1706 Convert frame-relative x/y to coordinates relative to window W.
1707 Takes pseudo-windows into account. */
1708
1709 void
1710 frame_to_window_pixel_xy (w, x, y)
1711 struct window *w;
1712 int *x, *y;
1713 {
1714 if (w->pseudo_window_p)
1715 {
1716 /* A pseudo-window is always full-width, and starts at the
1717 left edge of the frame, plus a frame border. */
1718 struct frame *f = XFRAME (w->frame);
1719 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1720 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1721 }
1722 else
1723 {
1724 *x -= WINDOW_LEFT_EDGE_X (w);
1725 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1726 }
1727 }
1728
1729 /* EXPORT:
1730 Return in *R the clipping rectangle for glyph string S. */
1731
1732 void
1733 get_glyph_string_clip_rect (s, nr)
1734 struct glyph_string *s;
1735 NativeRectangle *nr;
1736 {
1737 XRectangle r;
1738
1739 if (s->row->full_width_p)
1740 {
1741 /* Draw full-width. X coordinates are relative to S->w->left_col. */
1742 r.x = WINDOW_LEFT_EDGE_X (s->w);
1743 r.width = WINDOW_TOTAL_WIDTH (s->w);
1744
1745 /* Unless displaying a mode or menu bar line, which are always
1746 fully visible, clip to the visible part of the row. */
1747 if (s->w->pseudo_window_p)
1748 r.height = s->row->visible_height;
1749 else
1750 r.height = s->height;
1751 }
1752 else
1753 {
1754 /* This is a text line that may be partially visible. */
1755 r.x = window_box_left (s->w, s->area);
1756 r.width = window_box_width (s->w, s->area);
1757 r.height = s->row->visible_height;
1758 }
1759
1760 /* If S draws overlapping rows, it's sufficient to use the top and
1761 bottom of the window for clipping because this glyph string
1762 intentionally draws over other lines. */
1763 if (s->for_overlaps_p)
1764 {
1765 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1766 r.height = window_text_bottom_y (s->w) - r.y;
1767 }
1768 else
1769 {
1770 /* Don't use S->y for clipping because it doesn't take partially
1771 visible lines into account. For example, it can be negative for
1772 partially visible lines at the top of a window. */
1773 if (!s->row->full_width_p
1774 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
1775 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1776 else
1777 r.y = max (0, s->row->y);
1778
1779 /* If drawing a tool-bar window, draw it over the internal border
1780 at the top of the window. */
1781 if (s->w == XWINDOW (s->f->tool_bar_window))
1782 r.y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
1783 }
1784
1785 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
1786
1787 /* If drawing the cursor, don't let glyph draw outside its
1788 advertised boundaries. Cleartype does this under some circumstances. */
1789 if (s->hl == DRAW_CURSOR)
1790 {
1791 struct glyph *glyph = s->first_glyph;
1792 int height;
1793
1794 if (s->x > r.x)
1795 {
1796 r.width -= s->x - r.x;
1797 r.x = s->x;
1798 }
1799 r.width = min (r.width, glyph->pixel_width);
1800
1801 /* Don't draw cursor glyph taller than our actual glyph. */
1802 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
1803 if (height < r.height)
1804 {
1805 int max_y = r.y + r.height;
1806 r.y = min (max_y, s->ybase + glyph->descent - height);
1807 r.height = min (max_y - r.y, height);
1808 }
1809 }
1810
1811 #ifdef CONVERT_FROM_XRECT
1812 CONVERT_FROM_XRECT (r, *nr);
1813 #else
1814 *nr = r;
1815 #endif
1816 }
1817
1818 #endif /* HAVE_WINDOW_SYSTEM */
1819
1820 \f
1821 /***********************************************************************
1822 Lisp form evaluation
1823 ***********************************************************************/
1824
1825 /* Error handler for safe_eval and safe_call. */
1826
1827 static Lisp_Object
1828 safe_eval_handler (arg)
1829 Lisp_Object arg;
1830 {
1831 add_to_log ("Error during redisplay: %s", arg, Qnil);
1832 return Qnil;
1833 }
1834
1835
1836 /* Evaluate SEXPR and return the result, or nil if something went
1837 wrong. Prevent redisplay during the evaluation. */
1838
1839 Lisp_Object
1840 safe_eval (sexpr)
1841 Lisp_Object sexpr;
1842 {
1843 Lisp_Object val;
1844
1845 if (inhibit_eval_during_redisplay)
1846 val = Qnil;
1847 else
1848 {
1849 int count = SPECPDL_INDEX ();
1850 struct gcpro gcpro1;
1851
1852 GCPRO1 (sexpr);
1853 specbind (Qinhibit_redisplay, Qt);
1854 /* Use Qt to ensure debugger does not run,
1855 so there is no possibility of wanting to redisplay. */
1856 val = internal_condition_case_1 (Feval, sexpr, Qt,
1857 safe_eval_handler);
1858 UNGCPRO;
1859 val = unbind_to (count, val);
1860 }
1861
1862 return val;
1863 }
1864
1865
1866 /* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
1867 Return the result, or nil if something went wrong. Prevent
1868 redisplay during the evaluation. */
1869
1870 Lisp_Object
1871 safe_call (nargs, args)
1872 int nargs;
1873 Lisp_Object *args;
1874 {
1875 Lisp_Object val;
1876
1877 if (inhibit_eval_during_redisplay)
1878 val = Qnil;
1879 else
1880 {
1881 int count = SPECPDL_INDEX ();
1882 struct gcpro gcpro1;
1883
1884 GCPRO1 (args[0]);
1885 gcpro1.nvars = nargs;
1886 specbind (Qinhibit_redisplay, Qt);
1887 /* Use Qt to ensure debugger does not run,
1888 so there is no possibility of wanting to redisplay. */
1889 val = internal_condition_case_2 (Ffuncall, nargs, args, Qt,
1890 safe_eval_handler);
1891 UNGCPRO;
1892 val = unbind_to (count, val);
1893 }
1894
1895 return val;
1896 }
1897
1898
1899 /* Call function FN with one argument ARG.
1900 Return the result, or nil if something went wrong. */
1901
1902 Lisp_Object
1903 safe_call1 (fn, arg)
1904 Lisp_Object fn, arg;
1905 {
1906 Lisp_Object args[2];
1907 args[0] = fn;
1908 args[1] = arg;
1909 return safe_call (2, args);
1910 }
1911
1912
1913 \f
1914 /***********************************************************************
1915 Debugging
1916 ***********************************************************************/
1917
1918 #if 0
1919
1920 /* Define CHECK_IT to perform sanity checks on iterators.
1921 This is for debugging. It is too slow to do unconditionally. */
1922
1923 static void
1924 check_it (it)
1925 struct it *it;
1926 {
1927 if (it->method == next_element_from_string)
1928 {
1929 xassert (STRINGP (it->string));
1930 xassert (IT_STRING_CHARPOS (*it) >= 0);
1931 }
1932 else
1933 {
1934 xassert (IT_STRING_CHARPOS (*it) < 0);
1935 if (it->method == next_element_from_buffer)
1936 {
1937 /* Check that character and byte positions agree. */
1938 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
1939 }
1940 }
1941
1942 if (it->dpvec)
1943 xassert (it->current.dpvec_index >= 0);
1944 else
1945 xassert (it->current.dpvec_index < 0);
1946 }
1947
1948 #define CHECK_IT(IT) check_it ((IT))
1949
1950 #else /* not 0 */
1951
1952 #define CHECK_IT(IT) (void) 0
1953
1954 #endif /* not 0 */
1955
1956
1957 #if GLYPH_DEBUG
1958
1959 /* Check that the window end of window W is what we expect it
1960 to be---the last row in the current matrix displaying text. */
1961
1962 static void
1963 check_window_end (w)
1964 struct window *w;
1965 {
1966 if (!MINI_WINDOW_P (w)
1967 && !NILP (w->window_end_valid))
1968 {
1969 struct glyph_row *row;
1970 xassert ((row = MATRIX_ROW (w->current_matrix,
1971 XFASTINT (w->window_end_vpos)),
1972 !row->enabled_p
1973 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
1974 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
1975 }
1976 }
1977
1978 #define CHECK_WINDOW_END(W) check_window_end ((W))
1979
1980 #else /* not GLYPH_DEBUG */
1981
1982 #define CHECK_WINDOW_END(W) (void) 0
1983
1984 #endif /* not GLYPH_DEBUG */
1985
1986
1987 \f
1988 /***********************************************************************
1989 Iterator initialization
1990 ***********************************************************************/
1991
1992 /* Initialize IT for displaying current_buffer in window W, starting
1993 at character position CHARPOS. CHARPOS < 0 means that no buffer
1994 position is specified which is useful when the iterator is assigned
1995 a position later. BYTEPOS is the byte position corresponding to
1996 CHARPOS. BYTEPOS < 0 means compute it from CHARPOS.
1997
1998 If ROW is not null, calls to produce_glyphs with IT as parameter
1999 will produce glyphs in that row.
2000
2001 BASE_FACE_ID is the id of a base face to use. It must be one of
2002 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2003 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2004 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2005
2006 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2007 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2008 will be initialized to use the corresponding mode line glyph row of
2009 the desired matrix of W. */
2010
2011 void
2012 init_iterator (it, w, charpos, bytepos, row, base_face_id)
2013 struct it *it;
2014 struct window *w;
2015 int charpos, bytepos;
2016 struct glyph_row *row;
2017 enum face_id base_face_id;
2018 {
2019 int highlight_region_p;
2020
2021 /* Some precondition checks. */
2022 xassert (w != NULL && it != NULL);
2023 xassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2024 && charpos <= ZV));
2025
2026 /* If face attributes have been changed since the last redisplay,
2027 free realized faces now because they depend on face definitions
2028 that might have changed. Don't free faces while there might be
2029 desired matrices pending which reference these faces. */
2030 if (face_change_count && !inhibit_free_realized_faces)
2031 {
2032 face_change_count = 0;
2033 free_all_realized_faces (Qnil);
2034 }
2035
2036 /* Use one of the mode line rows of W's desired matrix if
2037 appropriate. */
2038 if (row == NULL)
2039 {
2040 if (base_face_id == MODE_LINE_FACE_ID
2041 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2042 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2043 else if (base_face_id == HEADER_LINE_FACE_ID)
2044 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2045 }
2046
2047 /* Clear IT. */
2048 bzero (it, sizeof *it);
2049 it->current.overlay_string_index = -1;
2050 it->current.dpvec_index = -1;
2051 it->base_face_id = base_face_id;
2052 it->string = Qnil;
2053 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2054
2055 /* The window in which we iterate over current_buffer: */
2056 XSETWINDOW (it->window, w);
2057 it->w = w;
2058 it->f = XFRAME (w->frame);
2059
2060 /* Extra space between lines (on window systems only). */
2061 if (base_face_id == DEFAULT_FACE_ID
2062 && FRAME_WINDOW_P (it->f))
2063 {
2064 if (NATNUMP (current_buffer->extra_line_spacing))
2065 it->extra_line_spacing = XFASTINT (current_buffer->extra_line_spacing);
2066 else if (FLOATP (current_buffer->extra_line_spacing))
2067 it->extra_line_spacing = (XFLOAT_DATA (current_buffer->extra_line_spacing)
2068 * FRAME_LINE_HEIGHT (it->f));
2069 else if (it->f->extra_line_spacing > 0)
2070 it->extra_line_spacing = it->f->extra_line_spacing;
2071 }
2072
2073 /* If realized faces have been removed, e.g. because of face
2074 attribute changes of named faces, recompute them. When running
2075 in batch mode, the face cache of the initial frame is null. If
2076 we happen to get called, make a dummy face cache. */
2077 if (FRAME_FACE_CACHE (it->f) == NULL)
2078 init_frame_faces (it->f);
2079 if (FRAME_FACE_CACHE (it->f)->used == 0)
2080 recompute_basic_faces (it->f);
2081
2082 /* Current value of the `slice', `space-width', and 'height' properties. */
2083 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2084 it->space_width = Qnil;
2085 it->font_height = Qnil;
2086 it->override_ascent = -1;
2087
2088 /* Are control characters displayed as `^C'? */
2089 it->ctl_arrow_p = !NILP (current_buffer->ctl_arrow);
2090
2091 /* -1 means everything between a CR and the following line end
2092 is invisible. >0 means lines indented more than this value are
2093 invisible. */
2094 it->selective = (INTEGERP (current_buffer->selective_display)
2095 ? XFASTINT (current_buffer->selective_display)
2096 : (!NILP (current_buffer->selective_display)
2097 ? -1 : 0));
2098 it->selective_display_ellipsis_p
2099 = !NILP (current_buffer->selective_display_ellipses);
2100
2101 /* Display table to use. */
2102 it->dp = window_display_table (w);
2103
2104 /* Are multibyte characters enabled in current_buffer? */
2105 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
2106
2107 /* Non-zero if we should highlight the region. */
2108 highlight_region_p
2109 = (!NILP (Vtransient_mark_mode)
2110 && !NILP (current_buffer->mark_active)
2111 && XMARKER (current_buffer->mark)->buffer != 0);
2112
2113 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
2114 start and end of a visible region in window IT->w. Set both to
2115 -1 to indicate no region. */
2116 if (highlight_region_p
2117 /* Maybe highlight only in selected window. */
2118 && (/* Either show region everywhere. */
2119 highlight_nonselected_windows
2120 /* Or show region in the selected window. */
2121 || w == XWINDOW (selected_window)
2122 /* Or show the region if we are in the mini-buffer and W is
2123 the window the mini-buffer refers to. */
2124 || (MINI_WINDOW_P (XWINDOW (selected_window))
2125 && WINDOWP (minibuf_selected_window)
2126 && w == XWINDOW (minibuf_selected_window))))
2127 {
2128 int charpos = marker_position (current_buffer->mark);
2129 it->region_beg_charpos = min (PT, charpos);
2130 it->region_end_charpos = max (PT, charpos);
2131 }
2132 else
2133 it->region_beg_charpos = it->region_end_charpos = -1;
2134
2135 /* Get the position at which the redisplay_end_trigger hook should
2136 be run, if it is to be run at all. */
2137 if (MARKERP (w->redisplay_end_trigger)
2138 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2139 it->redisplay_end_trigger_charpos
2140 = marker_position (w->redisplay_end_trigger);
2141 else if (INTEGERP (w->redisplay_end_trigger))
2142 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
2143
2144 /* Correct bogus values of tab_width. */
2145 it->tab_width = XINT (current_buffer->tab_width);
2146 if (it->tab_width <= 0 || it->tab_width > 1000)
2147 it->tab_width = 8;
2148
2149 /* Are lines in the display truncated? */
2150 it->truncate_lines_p
2151 = (base_face_id != DEFAULT_FACE_ID
2152 || XINT (it->w->hscroll)
2153 || (truncate_partial_width_windows
2154 && !WINDOW_FULL_WIDTH_P (it->w))
2155 || !NILP (current_buffer->truncate_lines));
2156
2157 /* Get dimensions of truncation and continuation glyphs. These are
2158 displayed as fringe bitmaps under X, so we don't need them for such
2159 frames. */
2160 if (!FRAME_WINDOW_P (it->f))
2161 {
2162 if (it->truncate_lines_p)
2163 {
2164 /* We will need the truncation glyph. */
2165 xassert (it->glyph_row == NULL);
2166 produce_special_glyphs (it, IT_TRUNCATION);
2167 it->truncation_pixel_width = it->pixel_width;
2168 }
2169 else
2170 {
2171 /* We will need the continuation glyph. */
2172 xassert (it->glyph_row == NULL);
2173 produce_special_glyphs (it, IT_CONTINUATION);
2174 it->continuation_pixel_width = it->pixel_width;
2175 }
2176
2177 /* Reset these values to zero because the produce_special_glyphs
2178 above has changed them. */
2179 it->pixel_width = it->ascent = it->descent = 0;
2180 it->phys_ascent = it->phys_descent = 0;
2181 }
2182
2183 /* Set this after getting the dimensions of truncation and
2184 continuation glyphs, so that we don't produce glyphs when calling
2185 produce_special_glyphs, above. */
2186 it->glyph_row = row;
2187 it->area = TEXT_AREA;
2188
2189 /* Get the dimensions of the display area. The display area
2190 consists of the visible window area plus a horizontally scrolled
2191 part to the left of the window. All x-values are relative to the
2192 start of this total display area. */
2193 if (base_face_id != DEFAULT_FACE_ID)
2194 {
2195 /* Mode lines, menu bar in terminal frames. */
2196 it->first_visible_x = 0;
2197 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
2198 }
2199 else
2200 {
2201 it->first_visible_x
2202 = XFASTINT (it->w->hscroll) * FRAME_COLUMN_WIDTH (it->f);
2203 it->last_visible_x = (it->first_visible_x
2204 + window_box_width (w, TEXT_AREA));
2205
2206 /* If we truncate lines, leave room for the truncator glyph(s) at
2207 the right margin. Otherwise, leave room for the continuation
2208 glyph(s). Truncation and continuation glyphs are not inserted
2209 for window-based redisplay. */
2210 if (!FRAME_WINDOW_P (it->f))
2211 {
2212 if (it->truncate_lines_p)
2213 it->last_visible_x -= it->truncation_pixel_width;
2214 else
2215 it->last_visible_x -= it->continuation_pixel_width;
2216 }
2217
2218 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2219 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2220 }
2221
2222 /* Leave room for a border glyph. */
2223 if (!FRAME_WINDOW_P (it->f)
2224 && !WINDOW_RIGHTMOST_P (it->w))
2225 it->last_visible_x -= 1;
2226
2227 it->last_visible_y = window_text_bottom_y (w);
2228
2229 /* For mode lines and alike, arrange for the first glyph having a
2230 left box line if the face specifies a box. */
2231 if (base_face_id != DEFAULT_FACE_ID)
2232 {
2233 struct face *face;
2234
2235 it->face_id = base_face_id;
2236
2237 /* If we have a boxed mode line, make the first character appear
2238 with a left box line. */
2239 face = FACE_FROM_ID (it->f, base_face_id);
2240 if (face->box != FACE_NO_BOX)
2241 it->start_of_box_run_p = 1;
2242 }
2243
2244 /* If a buffer position was specified, set the iterator there,
2245 getting overlays and face properties from that position. */
2246 if (charpos >= BUF_BEG (current_buffer))
2247 {
2248 it->end_charpos = ZV;
2249 it->face_id = -1;
2250 IT_CHARPOS (*it) = charpos;
2251
2252 /* Compute byte position if not specified. */
2253 if (bytepos < charpos)
2254 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
2255 else
2256 IT_BYTEPOS (*it) = bytepos;
2257
2258 it->start = it->current;
2259
2260 /* Compute faces etc. */
2261 reseat (it, it->current.pos, 1);
2262 }
2263
2264 CHECK_IT (it);
2265 }
2266
2267
2268 /* Initialize IT for the display of window W with window start POS. */
2269
2270 void
2271 start_display (it, w, pos)
2272 struct it *it;
2273 struct window *w;
2274 struct text_pos pos;
2275 {
2276 struct glyph_row *row;
2277 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2278
2279 row = w->desired_matrix->rows + first_vpos;
2280 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2281 it->first_vpos = first_vpos;
2282
2283 if (!it->truncate_lines_p)
2284 {
2285 int start_at_line_beg_p;
2286 int first_y = it->current_y;
2287
2288 /* If window start is not at a line start, skip forward to POS to
2289 get the correct continuation lines width. */
2290 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2291 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2292 if (!start_at_line_beg_p)
2293 {
2294 int new_x;
2295
2296 reseat_at_previous_visible_line_start (it);
2297 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
2298
2299 new_x = it->current_x + it->pixel_width;
2300
2301 /* If lines are continued, this line may end in the middle
2302 of a multi-glyph character (e.g. a control character
2303 displayed as \003, or in the middle of an overlay
2304 string). In this case move_it_to above will not have
2305 taken us to the start of the continuation line but to the
2306 end of the continued line. */
2307 if (it->current_x > 0
2308 && !it->truncate_lines_p /* Lines are continued. */
2309 && (/* And glyph doesn't fit on the line. */
2310 new_x > it->last_visible_x
2311 /* Or it fits exactly and we're on a window
2312 system frame. */
2313 || (new_x == it->last_visible_x
2314 && FRAME_WINDOW_P (it->f))))
2315 {
2316 if (it->current.dpvec_index >= 0
2317 || it->current.overlay_string_index >= 0)
2318 {
2319 set_iterator_to_next (it, 1);
2320 move_it_in_display_line_to (it, -1, -1, 0);
2321 }
2322
2323 it->continuation_lines_width += it->current_x;
2324 }
2325
2326 /* We're starting a new display line, not affected by the
2327 height of the continued line, so clear the appropriate
2328 fields in the iterator structure. */
2329 it->max_ascent = it->max_descent = 0;
2330 it->max_phys_ascent = it->max_phys_descent = 0;
2331
2332 it->current_y = first_y;
2333 it->vpos = 0;
2334 it->current_x = it->hpos = 0;
2335 }
2336 }
2337
2338 #if 0 /* Don't assert the following because start_display is sometimes
2339 called intentionally with a window start that is not at a
2340 line start. Please leave this code in as a comment. */
2341
2342 /* Window start should be on a line start, now. */
2343 xassert (it->continuation_lines_width
2344 || IT_CHARPOS (it) == BEGV
2345 || FETCH_BYTE (IT_BYTEPOS (it) - 1) == '\n');
2346 #endif /* 0 */
2347 }
2348
2349
2350 /* Return 1 if POS is a position in ellipses displayed for invisible
2351 text. W is the window we display, for text property lookup. */
2352
2353 static int
2354 in_ellipses_for_invisible_text_p (pos, w)
2355 struct display_pos *pos;
2356 struct window *w;
2357 {
2358 Lisp_Object prop, window;
2359 int ellipses_p = 0;
2360 int charpos = CHARPOS (pos->pos);
2361
2362 /* If POS specifies a position in a display vector, this might
2363 be for an ellipsis displayed for invisible text. We won't
2364 get the iterator set up for delivering that ellipsis unless
2365 we make sure that it gets aware of the invisible text. */
2366 if (pos->dpvec_index >= 0
2367 && pos->overlay_string_index < 0
2368 && CHARPOS (pos->string_pos) < 0
2369 && charpos > BEGV
2370 && (XSETWINDOW (window, w),
2371 prop = Fget_char_property (make_number (charpos),
2372 Qinvisible, window),
2373 !TEXT_PROP_MEANS_INVISIBLE (prop)))
2374 {
2375 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
2376 window);
2377 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
2378 }
2379
2380 return ellipses_p;
2381 }
2382
2383
2384 /* Initialize IT for stepping through current_buffer in window W,
2385 starting at position POS that includes overlay string and display
2386 vector/ control character translation position information. Value
2387 is zero if there are overlay strings with newlines at POS. */
2388
2389 static int
2390 init_from_display_pos (it, w, pos)
2391 struct it *it;
2392 struct window *w;
2393 struct display_pos *pos;
2394 {
2395 int charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
2396 int i, overlay_strings_with_newlines = 0;
2397
2398 /* If POS specifies a position in a display vector, this might
2399 be for an ellipsis displayed for invisible text. We won't
2400 get the iterator set up for delivering that ellipsis unless
2401 we make sure that it gets aware of the invisible text. */
2402 if (in_ellipses_for_invisible_text_p (pos, w))
2403 {
2404 --charpos;
2405 bytepos = 0;
2406 }
2407
2408 /* Keep in mind: the call to reseat in init_iterator skips invisible
2409 text, so we might end up at a position different from POS. This
2410 is only a problem when POS is a row start after a newline and an
2411 overlay starts there with an after-string, and the overlay has an
2412 invisible property. Since we don't skip invisible text in
2413 display_line and elsewhere immediately after consuming the
2414 newline before the row start, such a POS will not be in a string,
2415 but the call to init_iterator below will move us to the
2416 after-string. */
2417 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
2418
2419 for (i = 0; i < it->n_overlay_strings; ++i)
2420 {
2421 const char *s = SDATA (it->overlay_strings[i]);
2422 const char *e = s + SBYTES (it->overlay_strings[i]);
2423
2424 while (s < e && *s != '\n')
2425 ++s;
2426
2427 if (s < e)
2428 {
2429 overlay_strings_with_newlines = 1;
2430 break;
2431 }
2432 }
2433
2434 /* If position is within an overlay string, set up IT to the right
2435 overlay string. */
2436 if (pos->overlay_string_index >= 0)
2437 {
2438 int relative_index;
2439
2440 /* If the first overlay string happens to have a `display'
2441 property for an image, the iterator will be set up for that
2442 image, and we have to undo that setup first before we can
2443 correct the overlay string index. */
2444 if (it->method == next_element_from_image)
2445 pop_it (it);
2446
2447 /* We already have the first chunk of overlay strings in
2448 IT->overlay_strings. Load more until the one for
2449 pos->overlay_string_index is in IT->overlay_strings. */
2450 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
2451 {
2452 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
2453 it->current.overlay_string_index = 0;
2454 while (n--)
2455 {
2456 load_overlay_strings (it, 0);
2457 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
2458 }
2459 }
2460
2461 it->current.overlay_string_index = pos->overlay_string_index;
2462 relative_index = (it->current.overlay_string_index
2463 % OVERLAY_STRING_CHUNK_SIZE);
2464 it->string = it->overlay_strings[relative_index];
2465 xassert (STRINGP (it->string));
2466 it->current.string_pos = pos->string_pos;
2467 it->method = next_element_from_string;
2468 }
2469
2470 #if 0 /* This is bogus because POS not having an overlay string
2471 position does not mean it's after the string. Example: A
2472 line starting with a before-string and initialization of IT
2473 to the previous row's end position. */
2474 else if (it->current.overlay_string_index >= 0)
2475 {
2476 /* If POS says we're already after an overlay string ending at
2477 POS, make sure to pop the iterator because it will be in
2478 front of that overlay string. When POS is ZV, we've thereby
2479 also ``processed'' overlay strings at ZV. */
2480 while (it->sp)
2481 pop_it (it);
2482 it->current.overlay_string_index = -1;
2483 it->method = next_element_from_buffer;
2484 if (CHARPOS (pos->pos) == ZV)
2485 it->overlay_strings_at_end_processed_p = 1;
2486 }
2487 #endif /* 0 */
2488
2489 if (CHARPOS (pos->string_pos) >= 0)
2490 {
2491 /* Recorded position is not in an overlay string, but in another
2492 string. This can only be a string from a `display' property.
2493 IT should already be filled with that string. */
2494 it->current.string_pos = pos->string_pos;
2495 xassert (STRINGP (it->string));
2496 }
2497
2498 /* Restore position in display vector translations, control
2499 character translations or ellipses. */
2500 if (pos->dpvec_index >= 0)
2501 {
2502 if (it->dpvec == NULL)
2503 get_next_display_element (it);
2504 xassert (it->dpvec && it->current.dpvec_index == 0);
2505 it->current.dpvec_index = pos->dpvec_index;
2506 }
2507
2508 CHECK_IT (it);
2509 return !overlay_strings_with_newlines;
2510 }
2511
2512
2513 /* Initialize IT for stepping through current_buffer in window W
2514 starting at ROW->start. */
2515
2516 static void
2517 init_to_row_start (it, w, row)
2518 struct it *it;
2519 struct window *w;
2520 struct glyph_row *row;
2521 {
2522 init_from_display_pos (it, w, &row->start);
2523 it->start = row->start;
2524 it->continuation_lines_width = row->continuation_lines_width;
2525 CHECK_IT (it);
2526 }
2527
2528
2529 /* Initialize IT for stepping through current_buffer in window W
2530 starting in the line following ROW, i.e. starting at ROW->end.
2531 Value is zero if there are overlay strings with newlines at ROW's
2532 end position. */
2533
2534 static int
2535 init_to_row_end (it, w, row)
2536 struct it *it;
2537 struct window *w;
2538 struct glyph_row *row;
2539 {
2540 int success = 0;
2541
2542 if (init_from_display_pos (it, w, &row->end))
2543 {
2544 if (row->continued_p)
2545 it->continuation_lines_width
2546 = row->continuation_lines_width + row->pixel_width;
2547 CHECK_IT (it);
2548 success = 1;
2549 }
2550
2551 return success;
2552 }
2553
2554
2555
2556 \f
2557 /***********************************************************************
2558 Text properties
2559 ***********************************************************************/
2560
2561 /* Called when IT reaches IT->stop_charpos. Handle text property and
2562 overlay changes. Set IT->stop_charpos to the next position where
2563 to stop. */
2564
2565 static void
2566 handle_stop (it)
2567 struct it *it;
2568 {
2569 enum prop_handled handled;
2570 int handle_overlay_change_p = 1;
2571 struct props *p;
2572
2573 it->dpvec = NULL;
2574 it->current.dpvec_index = -1;
2575
2576 do
2577 {
2578 handled = HANDLED_NORMALLY;
2579
2580 /* Call text property handlers. */
2581 for (p = it_props; p->handler; ++p)
2582 {
2583 handled = p->handler (it);
2584
2585 if (handled == HANDLED_RECOMPUTE_PROPS)
2586 break;
2587 else if (handled == HANDLED_RETURN)
2588 return;
2589 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
2590 handle_overlay_change_p = 0;
2591 }
2592
2593 if (handled != HANDLED_RECOMPUTE_PROPS)
2594 {
2595 /* Don't check for overlay strings below when set to deliver
2596 characters from a display vector. */
2597 if (it->method == next_element_from_display_vector)
2598 handle_overlay_change_p = 0;
2599
2600 /* Handle overlay changes. */
2601 if (handle_overlay_change_p)
2602 handled = handle_overlay_change (it);
2603
2604 /* Determine where to stop next. */
2605 if (handled == HANDLED_NORMALLY)
2606 compute_stop_pos (it);
2607 }
2608 }
2609 while (handled == HANDLED_RECOMPUTE_PROPS);
2610 }
2611
2612
2613 /* Compute IT->stop_charpos from text property and overlay change
2614 information for IT's current position. */
2615
2616 static void
2617 compute_stop_pos (it)
2618 struct it *it;
2619 {
2620 register INTERVAL iv, next_iv;
2621 Lisp_Object object, limit, position;
2622
2623 /* If nowhere else, stop at the end. */
2624 it->stop_charpos = it->end_charpos;
2625
2626 if (STRINGP (it->string))
2627 {
2628 /* Strings are usually short, so don't limit the search for
2629 properties. */
2630 object = it->string;
2631 limit = Qnil;
2632 position = make_number (IT_STRING_CHARPOS (*it));
2633 }
2634 else
2635 {
2636 int charpos;
2637
2638 /* If next overlay change is in front of the current stop pos
2639 (which is IT->end_charpos), stop there. Note: value of
2640 next_overlay_change is point-max if no overlay change
2641 follows. */
2642 charpos = next_overlay_change (IT_CHARPOS (*it));
2643 if (charpos < it->stop_charpos)
2644 it->stop_charpos = charpos;
2645
2646 /* If showing the region, we have to stop at the region
2647 start or end because the face might change there. */
2648 if (it->region_beg_charpos > 0)
2649 {
2650 if (IT_CHARPOS (*it) < it->region_beg_charpos)
2651 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
2652 else if (IT_CHARPOS (*it) < it->region_end_charpos)
2653 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
2654 }
2655
2656 /* Set up variables for computing the stop position from text
2657 property changes. */
2658 XSETBUFFER (object, current_buffer);
2659 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
2660 position = make_number (IT_CHARPOS (*it));
2661
2662 }
2663
2664 /* Get the interval containing IT's position. Value is a null
2665 interval if there isn't such an interval. */
2666 iv = validate_interval_range (object, &position, &position, 0);
2667 if (!NULL_INTERVAL_P (iv))
2668 {
2669 Lisp_Object values_here[LAST_PROP_IDX];
2670 struct props *p;
2671
2672 /* Get properties here. */
2673 for (p = it_props; p->handler; ++p)
2674 values_here[p->idx] = textget (iv->plist, *p->name);
2675
2676 /* Look for an interval following iv that has different
2677 properties. */
2678 for (next_iv = next_interval (iv);
2679 (!NULL_INTERVAL_P (next_iv)
2680 && (NILP (limit)
2681 || XFASTINT (limit) > next_iv->position));
2682 next_iv = next_interval (next_iv))
2683 {
2684 for (p = it_props; p->handler; ++p)
2685 {
2686 Lisp_Object new_value;
2687
2688 new_value = textget (next_iv->plist, *p->name);
2689 if (!EQ (values_here[p->idx], new_value))
2690 break;
2691 }
2692
2693 if (p->handler)
2694 break;
2695 }
2696
2697 if (!NULL_INTERVAL_P (next_iv))
2698 {
2699 if (INTEGERP (limit)
2700 && next_iv->position >= XFASTINT (limit))
2701 /* No text property change up to limit. */
2702 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
2703 else
2704 /* Text properties change in next_iv. */
2705 it->stop_charpos = min (it->stop_charpos, next_iv->position);
2706 }
2707 }
2708
2709 xassert (STRINGP (it->string)
2710 || (it->stop_charpos >= BEGV
2711 && it->stop_charpos >= IT_CHARPOS (*it)));
2712 }
2713
2714
2715 /* Return the position of the next overlay change after POS in
2716 current_buffer. Value is point-max if no overlay change
2717 follows. This is like `next-overlay-change' but doesn't use
2718 xmalloc. */
2719
2720 static int
2721 next_overlay_change (pos)
2722 int pos;
2723 {
2724 int noverlays;
2725 int endpos;
2726 Lisp_Object *overlays;
2727 int i;
2728
2729 /* Get all overlays at the given position. */
2730 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
2731
2732 /* If any of these overlays ends before endpos,
2733 use its ending point instead. */
2734 for (i = 0; i < noverlays; ++i)
2735 {
2736 Lisp_Object oend;
2737 int oendpos;
2738
2739 oend = OVERLAY_END (overlays[i]);
2740 oendpos = OVERLAY_POSITION (oend);
2741 endpos = min (endpos, oendpos);
2742 }
2743
2744 return endpos;
2745 }
2746
2747
2748 \f
2749 /***********************************************************************
2750 Fontification
2751 ***********************************************************************/
2752
2753 /* Handle changes in the `fontified' property of the current buffer by
2754 calling hook functions from Qfontification_functions to fontify
2755 regions of text. */
2756
2757 static enum prop_handled
2758 handle_fontified_prop (it)
2759 struct it *it;
2760 {
2761 Lisp_Object prop, pos;
2762 enum prop_handled handled = HANDLED_NORMALLY;
2763
2764 /* Get the value of the `fontified' property at IT's current buffer
2765 position. (The `fontified' property doesn't have a special
2766 meaning in strings.) If the value is nil, call functions from
2767 Qfontification_functions. */
2768 if (!STRINGP (it->string)
2769 && it->s == NULL
2770 && !NILP (Vfontification_functions)
2771 && !NILP (Vrun_hooks)
2772 && (pos = make_number (IT_CHARPOS (*it)),
2773 prop = Fget_char_property (pos, Qfontified, Qnil),
2774 NILP (prop)))
2775 {
2776 int count = SPECPDL_INDEX ();
2777 Lisp_Object val;
2778
2779 val = Vfontification_functions;
2780 specbind (Qfontification_functions, Qnil);
2781
2782 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
2783 safe_call1 (val, pos);
2784 else
2785 {
2786 Lisp_Object globals, fn;
2787 struct gcpro gcpro1, gcpro2;
2788
2789 globals = Qnil;
2790 GCPRO2 (val, globals);
2791
2792 for (; CONSP (val); val = XCDR (val))
2793 {
2794 fn = XCAR (val);
2795
2796 if (EQ (fn, Qt))
2797 {
2798 /* A value of t indicates this hook has a local
2799 binding; it means to run the global binding too.
2800 In a global value, t should not occur. If it
2801 does, we must ignore it to avoid an endless
2802 loop. */
2803 for (globals = Fdefault_value (Qfontification_functions);
2804 CONSP (globals);
2805 globals = XCDR (globals))
2806 {
2807 fn = XCAR (globals);
2808 if (!EQ (fn, Qt))
2809 safe_call1 (fn, pos);
2810 }
2811 }
2812 else
2813 safe_call1 (fn, pos);
2814 }
2815
2816 UNGCPRO;
2817 }
2818
2819 unbind_to (count, Qnil);
2820
2821 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
2822 something. This avoids an endless loop if they failed to
2823 fontify the text for which reason ever. */
2824 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
2825 handled = HANDLED_RECOMPUTE_PROPS;
2826 }
2827
2828 return handled;
2829 }
2830
2831
2832 \f
2833 /***********************************************************************
2834 Faces
2835 ***********************************************************************/
2836
2837 /* Set up iterator IT from face properties at its current position.
2838 Called from handle_stop. */
2839
2840 static enum prop_handled
2841 handle_face_prop (it)
2842 struct it *it;
2843 {
2844 int new_face_id, next_stop;
2845
2846 if (!STRINGP (it->string))
2847 {
2848 new_face_id
2849 = face_at_buffer_position (it->w,
2850 IT_CHARPOS (*it),
2851 it->region_beg_charpos,
2852 it->region_end_charpos,
2853 &next_stop,
2854 (IT_CHARPOS (*it)
2855 + TEXT_PROP_DISTANCE_LIMIT),
2856 0);
2857
2858 /* Is this a start of a run of characters with box face?
2859 Caveat: this can be called for a freshly initialized
2860 iterator; face_id is -1 in this case. We know that the new
2861 face will not change until limit, i.e. if the new face has a
2862 box, all characters up to limit will have one. But, as
2863 usual, we don't know whether limit is really the end. */
2864 if (new_face_id != it->face_id)
2865 {
2866 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
2867
2868 /* If new face has a box but old face has not, this is
2869 the start of a run of characters with box, i.e. it has
2870 a shadow on the left side. The value of face_id of the
2871 iterator will be -1 if this is the initial call that gets
2872 the face. In this case, we have to look in front of IT's
2873 position and see whether there is a face != new_face_id. */
2874 it->start_of_box_run_p
2875 = (new_face->box != FACE_NO_BOX
2876 && (it->face_id >= 0
2877 || IT_CHARPOS (*it) == BEG
2878 || new_face_id != face_before_it_pos (it)));
2879 it->face_box_p = new_face->box != FACE_NO_BOX;
2880 }
2881 }
2882 else
2883 {
2884 int base_face_id, bufpos;
2885
2886 if (it->current.overlay_string_index >= 0)
2887 bufpos = IT_CHARPOS (*it);
2888 else
2889 bufpos = 0;
2890
2891 /* For strings from a buffer, i.e. overlay strings or strings
2892 from a `display' property, use the face at IT's current
2893 buffer position as the base face to merge with, so that
2894 overlay strings appear in the same face as surrounding
2895 text, unless they specify their own faces. */
2896 base_face_id = underlying_face_id (it);
2897
2898 new_face_id = face_at_string_position (it->w,
2899 it->string,
2900 IT_STRING_CHARPOS (*it),
2901 bufpos,
2902 it->region_beg_charpos,
2903 it->region_end_charpos,
2904 &next_stop,
2905 base_face_id, 0);
2906
2907 #if 0 /* This shouldn't be neccessary. Let's check it. */
2908 /* If IT is used to display a mode line we would really like to
2909 use the mode line face instead of the frame's default face. */
2910 if (it->glyph_row == MATRIX_MODE_LINE_ROW (it->w->desired_matrix)
2911 && new_face_id == DEFAULT_FACE_ID)
2912 new_face_id = CURRENT_MODE_LINE_FACE_ID (it->w);
2913 #endif
2914
2915 /* Is this a start of a run of characters with box? Caveat:
2916 this can be called for a freshly allocated iterator; face_id
2917 is -1 is this case. We know that the new face will not
2918 change until the next check pos, i.e. if the new face has a
2919 box, all characters up to that position will have a
2920 box. But, as usual, we don't know whether that position
2921 is really the end. */
2922 if (new_face_id != it->face_id)
2923 {
2924 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
2925 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
2926
2927 /* If new face has a box but old face hasn't, this is the
2928 start of a run of characters with box, i.e. it has a
2929 shadow on the left side. */
2930 it->start_of_box_run_p
2931 = new_face->box && (old_face == NULL || !old_face->box);
2932 it->face_box_p = new_face->box != FACE_NO_BOX;
2933 }
2934 }
2935
2936 it->face_id = new_face_id;
2937 return HANDLED_NORMALLY;
2938 }
2939
2940
2941 /* Return the ID of the face ``underlying'' IT's current position,
2942 which is in a string. If the iterator is associated with a
2943 buffer, return the face at IT's current buffer position.
2944 Otherwise, use the iterator's base_face_id. */
2945
2946 static int
2947 underlying_face_id (it)
2948 struct it *it;
2949 {
2950 int face_id = it->base_face_id, i;
2951
2952 xassert (STRINGP (it->string));
2953
2954 for (i = it->sp - 1; i >= 0; --i)
2955 if (NILP (it->stack[i].string))
2956 face_id = it->stack[i].face_id;
2957
2958 return face_id;
2959 }
2960
2961
2962 /* Compute the face one character before or after the current position
2963 of IT. BEFORE_P non-zero means get the face in front of IT's
2964 position. Value is the id of the face. */
2965
2966 static int
2967 face_before_or_after_it_pos (it, before_p)
2968 struct it *it;
2969 int before_p;
2970 {
2971 int face_id, limit;
2972 int next_check_charpos;
2973 struct text_pos pos;
2974
2975 xassert (it->s == NULL);
2976
2977 if (STRINGP (it->string))
2978 {
2979 int bufpos, base_face_id;
2980
2981 /* No face change past the end of the string (for the case
2982 we are padding with spaces). No face change before the
2983 string start. */
2984 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
2985 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
2986 return it->face_id;
2987
2988 /* Set pos to the position before or after IT's current position. */
2989 if (before_p)
2990 pos = string_pos (IT_STRING_CHARPOS (*it) - 1, it->string);
2991 else
2992 /* For composition, we must check the character after the
2993 composition. */
2994 pos = (it->what == IT_COMPOSITION
2995 ? string_pos (IT_STRING_CHARPOS (*it) + it->cmp_len, it->string)
2996 : string_pos (IT_STRING_CHARPOS (*it) + 1, it->string));
2997
2998 if (it->current.overlay_string_index >= 0)
2999 bufpos = IT_CHARPOS (*it);
3000 else
3001 bufpos = 0;
3002
3003 base_face_id = underlying_face_id (it);
3004
3005 /* Get the face for ASCII, or unibyte. */
3006 face_id = face_at_string_position (it->w,
3007 it->string,
3008 CHARPOS (pos),
3009 bufpos,
3010 it->region_beg_charpos,
3011 it->region_end_charpos,
3012 &next_check_charpos,
3013 base_face_id, 0);
3014
3015 /* Correct the face for charsets different from ASCII. Do it
3016 for the multibyte case only. The face returned above is
3017 suitable for unibyte text if IT->string is unibyte. */
3018 if (STRING_MULTIBYTE (it->string))
3019 {
3020 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos);
3021 int rest = SBYTES (it->string) - BYTEPOS (pos);
3022 int c, len;
3023 struct face *face = FACE_FROM_ID (it->f, face_id);
3024
3025 c = string_char_and_length (p, rest, &len);
3026 face_id = FACE_FOR_CHAR (it->f, face, c);
3027 }
3028 }
3029 else
3030 {
3031 if ((IT_CHARPOS (*it) >= ZV && !before_p)
3032 || (IT_CHARPOS (*it) <= BEGV && before_p))
3033 return it->face_id;
3034
3035 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
3036 pos = it->current.pos;
3037
3038 if (before_p)
3039 DEC_TEXT_POS (pos, it->multibyte_p);
3040 else
3041 {
3042 if (it->what == IT_COMPOSITION)
3043 /* For composition, we must check the position after the
3044 composition. */
3045 pos.charpos += it->cmp_len, pos.bytepos += it->len;
3046 else
3047 INC_TEXT_POS (pos, it->multibyte_p);
3048 }
3049
3050 /* Determine face for CHARSET_ASCII, or unibyte. */
3051 face_id = face_at_buffer_position (it->w,
3052 CHARPOS (pos),
3053 it->region_beg_charpos,
3054 it->region_end_charpos,
3055 &next_check_charpos,
3056 limit, 0);
3057
3058 /* Correct the face for charsets different from ASCII. Do it
3059 for the multibyte case only. The face returned above is
3060 suitable for unibyte text if current_buffer is unibyte. */
3061 if (it->multibyte_p)
3062 {
3063 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
3064 struct face *face = FACE_FROM_ID (it->f, face_id);
3065 face_id = FACE_FOR_CHAR (it->f, face, c);
3066 }
3067 }
3068
3069 return face_id;
3070 }
3071
3072
3073 \f
3074 /***********************************************************************
3075 Invisible text
3076 ***********************************************************************/
3077
3078 /* Set up iterator IT from invisible properties at its current
3079 position. Called from handle_stop. */
3080
3081 static enum prop_handled
3082 handle_invisible_prop (it)
3083 struct it *it;
3084 {
3085 enum prop_handled handled = HANDLED_NORMALLY;
3086
3087 if (STRINGP (it->string))
3088 {
3089 extern Lisp_Object Qinvisible;
3090 Lisp_Object prop, end_charpos, limit, charpos;
3091
3092 /* Get the value of the invisible text property at the
3093 current position. Value will be nil if there is no such
3094 property. */
3095 charpos = make_number (IT_STRING_CHARPOS (*it));
3096 prop = Fget_text_property (charpos, Qinvisible, it->string);
3097
3098 if (!NILP (prop)
3099 && IT_STRING_CHARPOS (*it) < it->end_charpos)
3100 {
3101 handled = HANDLED_RECOMPUTE_PROPS;
3102
3103 /* Get the position at which the next change of the
3104 invisible text property can be found in IT->string.
3105 Value will be nil if the property value is the same for
3106 all the rest of IT->string. */
3107 XSETINT (limit, SCHARS (it->string));
3108 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
3109 it->string, limit);
3110
3111 /* Text at current position is invisible. The next
3112 change in the property is at position end_charpos.
3113 Move IT's current position to that position. */
3114 if (INTEGERP (end_charpos)
3115 && XFASTINT (end_charpos) < XFASTINT (limit))
3116 {
3117 struct text_pos old;
3118 old = it->current.string_pos;
3119 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
3120 compute_string_pos (&it->current.string_pos, old, it->string);
3121 }
3122 else
3123 {
3124 /* The rest of the string is invisible. If this is an
3125 overlay string, proceed with the next overlay string
3126 or whatever comes and return a character from there. */
3127 if (it->current.overlay_string_index >= 0)
3128 {
3129 next_overlay_string (it);
3130 /* Don't check for overlay strings when we just
3131 finished processing them. */
3132 handled = HANDLED_OVERLAY_STRING_CONSUMED;
3133 }
3134 else
3135 {
3136 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
3137 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
3138 }
3139 }
3140 }
3141 }
3142 else
3143 {
3144 int invis_p, newpos, next_stop, start_charpos;
3145 Lisp_Object pos, prop, overlay;
3146
3147 /* First of all, is there invisible text at this position? */
3148 start_charpos = IT_CHARPOS (*it);
3149 pos = make_number (IT_CHARPOS (*it));
3150 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
3151 &overlay);
3152 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3153
3154 /* If we are on invisible text, skip over it. */
3155 if (invis_p && IT_CHARPOS (*it) < it->end_charpos)
3156 {
3157 /* Record whether we have to display an ellipsis for the
3158 invisible text. */
3159 int display_ellipsis_p = invis_p == 2;
3160
3161 handled = HANDLED_RECOMPUTE_PROPS;
3162
3163 /* Loop skipping over invisible text. The loop is left at
3164 ZV or with IT on the first char being visible again. */
3165 do
3166 {
3167 /* Try to skip some invisible text. Return value is the
3168 position reached which can be equal to IT's position
3169 if there is nothing invisible here. This skips both
3170 over invisible text properties and overlays with
3171 invisible property. */
3172 newpos = skip_invisible (IT_CHARPOS (*it),
3173 &next_stop, ZV, it->window);
3174
3175 /* If we skipped nothing at all we weren't at invisible
3176 text in the first place. If everything to the end of
3177 the buffer was skipped, end the loop. */
3178 if (newpos == IT_CHARPOS (*it) || newpos >= ZV)
3179 invis_p = 0;
3180 else
3181 {
3182 /* We skipped some characters but not necessarily
3183 all there are. Check if we ended up on visible
3184 text. Fget_char_property returns the property of
3185 the char before the given position, i.e. if we
3186 get invis_p = 0, this means that the char at
3187 newpos is visible. */
3188 pos = make_number (newpos);
3189 prop = Fget_char_property (pos, Qinvisible, it->window);
3190 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3191 }
3192
3193 /* If we ended up on invisible text, proceed to
3194 skip starting with next_stop. */
3195 if (invis_p)
3196 IT_CHARPOS (*it) = next_stop;
3197 }
3198 while (invis_p);
3199
3200 /* The position newpos is now either ZV or on visible text. */
3201 IT_CHARPOS (*it) = newpos;
3202 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
3203
3204 /* If there are before-strings at the start of invisible
3205 text, and the text is invisible because of a text
3206 property, arrange to show before-strings because 20.x did
3207 it that way. (If the text is invisible because of an
3208 overlay property instead of a text property, this is
3209 already handled in the overlay code.) */
3210 if (NILP (overlay)
3211 && get_overlay_strings (it, start_charpos))
3212 {
3213 handled = HANDLED_RECOMPUTE_PROPS;
3214 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
3215 }
3216 else if (display_ellipsis_p)
3217 setup_for_ellipsis (it);
3218 }
3219 }
3220
3221 return handled;
3222 }
3223
3224
3225 /* Make iterator IT return `...' next. */
3226
3227 static void
3228 setup_for_ellipsis (it)
3229 struct it *it;
3230 {
3231 if (it->dp
3232 && VECTORP (DISP_INVIS_VECTOR (it->dp)))
3233 {
3234 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
3235 it->dpvec = v->contents;
3236 it->dpend = v->contents + v->size;
3237 }
3238 else
3239 {
3240 /* Default `...'. */
3241 it->dpvec = default_invis_vector;
3242 it->dpend = default_invis_vector + 3;
3243 }
3244
3245 /* The ellipsis display does not replace the display of the
3246 character at the new position. Indicate this by setting
3247 IT->dpvec_char_len to zero. */
3248 it->dpvec_char_len = 0;
3249
3250 it->current.dpvec_index = 0;
3251 it->method = next_element_from_display_vector;
3252 }
3253
3254
3255 \f
3256 /***********************************************************************
3257 'display' property
3258 ***********************************************************************/
3259
3260 /* Set up iterator IT from `display' property at its current position.
3261 Called from handle_stop. */
3262
3263 static enum prop_handled
3264 handle_display_prop (it)
3265 struct it *it;
3266 {
3267 Lisp_Object prop, object;
3268 struct text_pos *position;
3269 int display_replaced_p = 0;
3270
3271 if (STRINGP (it->string))
3272 {
3273 object = it->string;
3274 position = &it->current.string_pos;
3275 }
3276 else
3277 {
3278 object = it->w->buffer;
3279 position = &it->current.pos;
3280 }
3281
3282 /* Reset those iterator values set from display property values. */
3283 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
3284 it->space_width = Qnil;
3285 it->font_height = Qnil;
3286 it->voffset = 0;
3287
3288 /* We don't support recursive `display' properties, i.e. string
3289 values that have a string `display' property, that have a string
3290 `display' property etc. */
3291 if (!it->string_from_display_prop_p)
3292 it->area = TEXT_AREA;
3293
3294 prop = Fget_char_property (make_number (position->charpos),
3295 Qdisplay, object);
3296 if (NILP (prop))
3297 return HANDLED_NORMALLY;
3298
3299 if (CONSP (prop)
3300 /* Simple properties. */
3301 && !EQ (XCAR (prop), Qimage)
3302 && !EQ (XCAR (prop), Qspace)
3303 && !EQ (XCAR (prop), Qwhen)
3304 && !EQ (XCAR (prop), Qslice)
3305 && !EQ (XCAR (prop), Qspace_width)
3306 && !EQ (XCAR (prop), Qheight)
3307 && !EQ (XCAR (prop), Qraise)
3308 /* Marginal area specifications. */
3309 && !(CONSP (XCAR (prop)) && EQ (XCAR (XCAR (prop)), Qmargin))
3310 && !EQ (XCAR (prop), Qleft_fringe)
3311 && !EQ (XCAR (prop), Qright_fringe)
3312 && !NILP (XCAR (prop)))
3313 {
3314 for (; CONSP (prop); prop = XCDR (prop))
3315 {
3316 if (handle_single_display_prop (it, XCAR (prop), object,
3317 position, display_replaced_p))
3318 display_replaced_p = 1;
3319 }
3320 }
3321 else if (VECTORP (prop))
3322 {
3323 int i;
3324 for (i = 0; i < ASIZE (prop); ++i)
3325 if (handle_single_display_prop (it, AREF (prop, i), object,
3326 position, display_replaced_p))
3327 display_replaced_p = 1;
3328 }
3329 else
3330 {
3331 if (handle_single_display_prop (it, prop, object, position, 0))
3332 display_replaced_p = 1;
3333 }
3334
3335 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
3336 }
3337
3338
3339 /* Value is the position of the end of the `display' property starting
3340 at START_POS in OBJECT. */
3341
3342 static struct text_pos
3343 display_prop_end (it, object, start_pos)
3344 struct it *it;
3345 Lisp_Object object;
3346 struct text_pos start_pos;
3347 {
3348 Lisp_Object end;
3349 struct text_pos end_pos;
3350
3351 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
3352 Qdisplay, object, Qnil);
3353 CHARPOS (end_pos) = XFASTINT (end);
3354 if (STRINGP (object))
3355 compute_string_pos (&end_pos, start_pos, it->string);
3356 else
3357 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
3358
3359 return end_pos;
3360 }
3361
3362
3363 /* Set up IT from a single `display' sub-property value PROP. OBJECT
3364 is the object in which the `display' property was found. *POSITION
3365 is the position at which it was found. DISPLAY_REPLACED_P non-zero
3366 means that we previously saw a display sub-property which already
3367 replaced text display with something else, for example an image;
3368 ignore such properties after the first one has been processed.
3369
3370 If PROP is a `space' or `image' sub-property, set *POSITION to the
3371 end position of the `display' property.
3372
3373 Value is non-zero if something was found which replaces the display
3374 of buffer or string text. */
3375
3376 static int
3377 handle_single_display_prop (it, prop, object, position,
3378 display_replaced_before_p)
3379 struct it *it;
3380 Lisp_Object prop;
3381 Lisp_Object object;
3382 struct text_pos *position;
3383 int display_replaced_before_p;
3384 {
3385 Lisp_Object value;
3386 int replaces_text_display_p = 0;
3387 Lisp_Object form;
3388
3389 /* If PROP is a list of the form `(when FORM . VALUE)', FORM is
3390 evaluated. If the result is nil, VALUE is ignored. */
3391 form = Qt;
3392 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
3393 {
3394 prop = XCDR (prop);
3395 if (!CONSP (prop))
3396 return 0;
3397 form = XCAR (prop);
3398 prop = XCDR (prop);
3399 }
3400
3401 if (!NILP (form) && !EQ (form, Qt))
3402 {
3403 int count = SPECPDL_INDEX ();
3404 struct gcpro gcpro1;
3405
3406 /* Bind `object' to the object having the `display' property, a
3407 buffer or string. Bind `position' to the position in the
3408 object where the property was found, and `buffer-position'
3409 to the current position in the buffer. */
3410 specbind (Qobject, object);
3411 specbind (Qposition, make_number (CHARPOS (*position)));
3412 specbind (Qbuffer_position,
3413 make_number (STRINGP (object)
3414 ? IT_CHARPOS (*it) : CHARPOS (*position)));
3415 GCPRO1 (form);
3416 form = safe_eval (form);
3417 UNGCPRO;
3418 unbind_to (count, Qnil);
3419 }
3420
3421 if (NILP (form))
3422 return 0;
3423
3424 if (CONSP (prop)
3425 && EQ (XCAR (prop), Qheight)
3426 && CONSP (XCDR (prop)))
3427 {
3428 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3429 return 0;
3430
3431 /* `(height HEIGHT)'. */
3432 it->font_height = XCAR (XCDR (prop));
3433 if (!NILP (it->font_height))
3434 {
3435 struct face *face = FACE_FROM_ID (it->f, it->face_id);
3436 int new_height = -1;
3437
3438 if (CONSP (it->font_height)
3439 && (EQ (XCAR (it->font_height), Qplus)
3440 || EQ (XCAR (it->font_height), Qminus))
3441 && CONSP (XCDR (it->font_height))
3442 && INTEGERP (XCAR (XCDR (it->font_height))))
3443 {
3444 /* `(+ N)' or `(- N)' where N is an integer. */
3445 int steps = XINT (XCAR (XCDR (it->font_height)));
3446 if (EQ (XCAR (it->font_height), Qplus))
3447 steps = - steps;
3448 it->face_id = smaller_face (it->f, it->face_id, steps);
3449 }
3450 else if (FUNCTIONP (it->font_height))
3451 {
3452 /* Call function with current height as argument.
3453 Value is the new height. */
3454 Lisp_Object height;
3455 height = safe_call1 (it->font_height,
3456 face->lface[LFACE_HEIGHT_INDEX]);
3457 if (NUMBERP (height))
3458 new_height = XFLOATINT (height);
3459 }
3460 else if (NUMBERP (it->font_height))
3461 {
3462 /* Value is a multiple of the canonical char height. */
3463 struct face *face;
3464
3465 face = FACE_FROM_ID (it->f, DEFAULT_FACE_ID);
3466 new_height = (XFLOATINT (it->font_height)
3467 * XINT (face->lface[LFACE_HEIGHT_INDEX]));
3468 }
3469 else
3470 {
3471 /* Evaluate IT->font_height with `height' bound to the
3472 current specified height to get the new height. */
3473 Lisp_Object value;
3474 int count = SPECPDL_INDEX ();
3475
3476 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
3477 value = safe_eval (it->font_height);
3478 unbind_to (count, Qnil);
3479
3480 if (NUMBERP (value))
3481 new_height = XFLOATINT (value);
3482 }
3483
3484 if (new_height > 0)
3485 it->face_id = face_with_height (it->f, it->face_id, new_height);
3486 }
3487 }
3488 else if (CONSP (prop)
3489 && EQ (XCAR (prop), Qspace_width)
3490 && CONSP (XCDR (prop)))
3491 {
3492 /* `(space_width WIDTH)'. */
3493 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3494 return 0;
3495
3496 value = XCAR (XCDR (prop));
3497 if (NUMBERP (value) && XFLOATINT (value) > 0)
3498 it->space_width = value;
3499 }
3500 else if (CONSP (prop)
3501 && EQ (XCAR (prop), Qslice))
3502 {
3503 /* `(slice X Y WIDTH HEIGHT)'. */
3504 Lisp_Object tem;
3505
3506 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3507 return 0;
3508
3509 if (tem = XCDR (prop), CONSP (tem))
3510 {
3511 it->slice.x = XCAR (tem);
3512 if (tem = XCDR (tem), CONSP (tem))
3513 {
3514 it->slice.y = XCAR (tem);
3515 if (tem = XCDR (tem), CONSP (tem))
3516 {
3517 it->slice.width = XCAR (tem);
3518 if (tem = XCDR (tem), CONSP (tem))
3519 it->slice.height = XCAR (tem);
3520 }
3521 }
3522 }
3523 }
3524 else if (CONSP (prop)
3525 && EQ (XCAR (prop), Qraise)
3526 && CONSP (XCDR (prop)))
3527 {
3528 /* `(raise FACTOR)'. */
3529 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3530 return 0;
3531
3532 #ifdef HAVE_WINDOW_SYSTEM
3533 value = XCAR (XCDR (prop));
3534 if (NUMBERP (value))
3535 {
3536 struct face *face = FACE_FROM_ID (it->f, it->face_id);
3537 it->voffset = - (XFLOATINT (value)
3538 * (FONT_HEIGHT (face->font)));
3539 }
3540 #endif /* HAVE_WINDOW_SYSTEM */
3541 }
3542 else if (!it->string_from_display_prop_p)
3543 {
3544 /* `((margin left-margin) VALUE)' or `((margin right-margin)
3545 VALUE) or `((margin nil) VALUE)' or VALUE. */
3546 Lisp_Object location, value;
3547 struct text_pos start_pos;
3548 int valid_p;
3549
3550 /* Characters having this form of property are not displayed, so
3551 we have to find the end of the property. */
3552 start_pos = *position;
3553 *position = display_prop_end (it, object, start_pos);
3554 value = Qnil;
3555
3556 /* Let's stop at the new position and assume that all
3557 text properties change there. */
3558 it->stop_charpos = position->charpos;
3559
3560 if (CONSP (prop)
3561 && (EQ (XCAR (prop), Qleft_fringe)
3562 || EQ (XCAR (prop), Qright_fringe))
3563 && CONSP (XCDR (prop)))
3564 {
3565 unsigned face_id = DEFAULT_FACE_ID;
3566
3567 /* Save current settings of IT so that we can restore them
3568 when we are finished with the glyph property value. */
3569
3570 /* `(left-fringe BITMAP FACE)'. */
3571 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3572 return 0;
3573
3574 #ifdef HAVE_WINDOW_SYSTEM
3575 value = XCAR (XCDR (prop));
3576 if (!NUMBERP (value)
3577 || !valid_fringe_bitmap_id_p (XINT (value)))
3578 return 0;
3579
3580 if (CONSP (XCDR (XCDR (prop))))
3581 {
3582 Lisp_Object face_name = XCAR (XCDR (XCDR (prop)));
3583
3584 face_id = lookup_named_face (it->f, face_name, 'A');
3585 if (face_id < 0)
3586 return 0;
3587 }
3588
3589 push_it (it);
3590
3591 it->area = TEXT_AREA;
3592 it->what = IT_IMAGE;
3593 it->image_id = -1; /* no image */
3594 it->position = start_pos;
3595 it->object = NILP (object) ? it->w->buffer : object;
3596 it->method = next_element_from_image;
3597 it->face_id = face_id;
3598
3599 /* Say that we haven't consumed the characters with
3600 `display' property yet. The call to pop_it in
3601 set_iterator_to_next will clean this up. */
3602 *position = start_pos;
3603
3604 if (EQ (XCAR (prop), Qleft_fringe))
3605 {
3606 it->left_user_fringe_bitmap = XINT (value);
3607 it->left_user_fringe_face_id = face_id;
3608 }
3609 else
3610 {
3611 it->right_user_fringe_bitmap = XINT (value);
3612 it->right_user_fringe_face_id = face_id;
3613 }
3614 #endif /* HAVE_WINDOW_SYSTEM */
3615 return 1;
3616 }
3617
3618 location = Qunbound;
3619 if (CONSP (prop) && CONSP (XCAR (prop)))
3620 {
3621 Lisp_Object tem;
3622
3623 value = XCDR (prop);
3624 if (CONSP (value))
3625 value = XCAR (value);
3626
3627 tem = XCAR (prop);
3628 if (EQ (XCAR (tem), Qmargin)
3629 && (tem = XCDR (tem),
3630 tem = CONSP (tem) ? XCAR (tem) : Qnil,
3631 (NILP (tem)
3632 || EQ (tem, Qleft_margin)
3633 || EQ (tem, Qright_margin))))
3634 location = tem;
3635 }
3636
3637 if (EQ (location, Qunbound))
3638 {
3639 location = Qnil;
3640 value = prop;
3641 }
3642
3643 valid_p = (STRINGP (value)
3644 #ifdef HAVE_WINDOW_SYSTEM
3645 || (!FRAME_TERMCAP_P (it->f) && valid_image_p (value))
3646 #endif /* not HAVE_WINDOW_SYSTEM */
3647 || (CONSP (value) && EQ (XCAR (value), Qspace)));
3648
3649 if ((EQ (location, Qleft_margin)
3650 || EQ (location, Qright_margin)
3651 || NILP (location))
3652 && valid_p
3653 && !display_replaced_before_p)
3654 {
3655 replaces_text_display_p = 1;
3656
3657 /* Save current settings of IT so that we can restore them
3658 when we are finished with the glyph property value. */
3659 push_it (it);
3660
3661 if (NILP (location))
3662 it->area = TEXT_AREA;
3663 else if (EQ (location, Qleft_margin))
3664 it->area = LEFT_MARGIN_AREA;
3665 else
3666 it->area = RIGHT_MARGIN_AREA;
3667
3668 if (STRINGP (value))
3669 {
3670 it->string = value;
3671 it->multibyte_p = STRING_MULTIBYTE (it->string);
3672 it->current.overlay_string_index = -1;
3673 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
3674 it->end_charpos = it->string_nchars = SCHARS (it->string);
3675 it->method = next_element_from_string;
3676 it->stop_charpos = 0;
3677 it->string_from_display_prop_p = 1;
3678 /* Say that we haven't consumed the characters with
3679 `display' property yet. The call to pop_it in
3680 set_iterator_to_next will clean this up. */
3681 *position = start_pos;
3682 }
3683 else if (CONSP (value) && EQ (XCAR (value), Qspace))
3684 {
3685 it->method = next_element_from_stretch;
3686 it->object = value;
3687 it->current.pos = it->position = start_pos;
3688 }
3689 #ifdef HAVE_WINDOW_SYSTEM
3690 else
3691 {
3692 it->what = IT_IMAGE;
3693 it->image_id = lookup_image (it->f, value);
3694 it->position = start_pos;
3695 it->object = NILP (object) ? it->w->buffer : object;
3696 it->method = next_element_from_image;
3697
3698 /* Say that we haven't consumed the characters with
3699 `display' property yet. The call to pop_it in
3700 set_iterator_to_next will clean this up. */
3701 *position = start_pos;
3702 }
3703 #endif /* HAVE_WINDOW_SYSTEM */
3704 }
3705 else
3706 /* Invalid property or property not supported. Restore
3707 the position to what it was before. */
3708 *position = start_pos;
3709 }
3710
3711 return replaces_text_display_p;
3712 }
3713
3714
3715 /* Check if PROP is a display sub-property value whose text should be
3716 treated as intangible. */
3717
3718 static int
3719 single_display_prop_intangible_p (prop)
3720 Lisp_Object prop;
3721 {
3722 /* Skip over `when FORM'. */
3723 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
3724 {
3725 prop = XCDR (prop);
3726 if (!CONSP (prop))
3727 return 0;
3728 prop = XCDR (prop);
3729 }
3730
3731 if (STRINGP (prop))
3732 return 1;
3733
3734 if (!CONSP (prop))
3735 return 0;
3736
3737 /* Skip over `margin LOCATION'. If LOCATION is in the margins,
3738 we don't need to treat text as intangible. */
3739 if (EQ (XCAR (prop), Qmargin))
3740 {
3741 prop = XCDR (prop);
3742 if (!CONSP (prop))
3743 return 0;
3744
3745 prop = XCDR (prop);
3746 if (!CONSP (prop)
3747 || EQ (XCAR (prop), Qleft_margin)
3748 || EQ (XCAR (prop), Qright_margin))
3749 return 0;
3750 }
3751
3752 return (CONSP (prop)
3753 && (EQ (XCAR (prop), Qimage)
3754 || EQ (XCAR (prop), Qspace)));
3755 }
3756
3757
3758 /* Check if PROP is a display property value whose text should be
3759 treated as intangible. */
3760
3761 int
3762 display_prop_intangible_p (prop)
3763 Lisp_Object prop;
3764 {
3765 if (CONSP (prop)
3766 && CONSP (XCAR (prop))
3767 && !EQ (Qmargin, XCAR (XCAR (prop))))
3768 {
3769 /* A list of sub-properties. */
3770 while (CONSP (prop))
3771 {
3772 if (single_display_prop_intangible_p (XCAR (prop)))
3773 return 1;
3774 prop = XCDR (prop);
3775 }
3776 }
3777 else if (VECTORP (prop))
3778 {
3779 /* A vector of sub-properties. */
3780 int i;
3781 for (i = 0; i < ASIZE (prop); ++i)
3782 if (single_display_prop_intangible_p (AREF (prop, i)))
3783 return 1;
3784 }
3785 else
3786 return single_display_prop_intangible_p (prop);
3787
3788 return 0;
3789 }
3790
3791
3792 /* Return 1 if PROP is a display sub-property value containing STRING. */
3793
3794 static int
3795 single_display_prop_string_p (prop, string)
3796 Lisp_Object prop, string;
3797 {
3798 if (EQ (string, prop))
3799 return 1;
3800
3801 /* Skip over `when FORM'. */
3802 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
3803 {
3804 prop = XCDR (prop);
3805 if (!CONSP (prop))
3806 return 0;
3807 prop = XCDR (prop);
3808 }
3809
3810 if (CONSP (prop))
3811 /* Skip over `margin LOCATION'. */
3812 if (EQ (XCAR (prop), Qmargin))
3813 {
3814 prop = XCDR (prop);
3815 if (!CONSP (prop))
3816 return 0;
3817
3818 prop = XCDR (prop);
3819 if (!CONSP (prop))
3820 return 0;
3821 }
3822
3823 return CONSP (prop) && EQ (XCAR (prop), string);
3824 }
3825
3826
3827 /* Return 1 if STRING appears in the `display' property PROP. */
3828
3829 static int
3830 display_prop_string_p (prop, string)
3831 Lisp_Object prop, string;
3832 {
3833 if (CONSP (prop)
3834 && CONSP (XCAR (prop))
3835 && !EQ (Qmargin, XCAR (XCAR (prop))))
3836 {
3837 /* A list of sub-properties. */
3838 while (CONSP (prop))
3839 {
3840 if (single_display_prop_string_p (XCAR (prop), string))
3841 return 1;
3842 prop = XCDR (prop);
3843 }
3844 }
3845 else if (VECTORP (prop))
3846 {
3847 /* A vector of sub-properties. */
3848 int i;
3849 for (i = 0; i < ASIZE (prop); ++i)
3850 if (single_display_prop_string_p (AREF (prop, i), string))
3851 return 1;
3852 }
3853 else
3854 return single_display_prop_string_p (prop, string);
3855
3856 return 0;
3857 }
3858
3859
3860 /* Determine from which buffer position in W's buffer STRING comes
3861 from. AROUND_CHARPOS is an approximate position where it could
3862 be from. Value is the buffer position or 0 if it couldn't be
3863 determined.
3864
3865 W's buffer must be current.
3866
3867 This function is necessary because we don't record buffer positions
3868 in glyphs generated from strings (to keep struct glyph small).
3869 This function may only use code that doesn't eval because it is
3870 called asynchronously from note_mouse_highlight. */
3871
3872 int
3873 string_buffer_position (w, string, around_charpos)
3874 struct window *w;
3875 Lisp_Object string;
3876 int around_charpos;
3877 {
3878 Lisp_Object limit, prop, pos;
3879 const int MAX_DISTANCE = 1000;
3880 int found = 0;
3881
3882 pos = make_number (around_charpos);
3883 limit = make_number (min (XINT (pos) + MAX_DISTANCE, ZV));
3884 while (!found && !EQ (pos, limit))
3885 {
3886 prop = Fget_char_property (pos, Qdisplay, Qnil);
3887 if (!NILP (prop) && display_prop_string_p (prop, string))
3888 found = 1;
3889 else
3890 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil, limit);
3891 }
3892
3893 if (!found)
3894 {
3895 pos = make_number (around_charpos);
3896 limit = make_number (max (XINT (pos) - MAX_DISTANCE, BEGV));
3897 while (!found && !EQ (pos, limit))
3898 {
3899 prop = Fget_char_property (pos, Qdisplay, Qnil);
3900 if (!NILP (prop) && display_prop_string_p (prop, string))
3901 found = 1;
3902 else
3903 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
3904 limit);
3905 }
3906 }
3907
3908 return found ? XINT (pos) : 0;
3909 }
3910
3911
3912 \f
3913 /***********************************************************************
3914 `composition' property
3915 ***********************************************************************/
3916
3917 /* Set up iterator IT from `composition' property at its current
3918 position. Called from handle_stop. */
3919
3920 static enum prop_handled
3921 handle_composition_prop (it)
3922 struct it *it;
3923 {
3924 Lisp_Object prop, string;
3925 int pos, pos_byte, end;
3926 enum prop_handled handled = HANDLED_NORMALLY;
3927
3928 if (STRINGP (it->string))
3929 {
3930 pos = IT_STRING_CHARPOS (*it);
3931 pos_byte = IT_STRING_BYTEPOS (*it);
3932 string = it->string;
3933 }
3934 else
3935 {
3936 pos = IT_CHARPOS (*it);
3937 pos_byte = IT_BYTEPOS (*it);
3938 string = Qnil;
3939 }
3940
3941 /* If there's a valid composition and point is not inside of the
3942 composition (in the case that the composition is from the current
3943 buffer), draw a glyph composed from the composition components. */
3944 if (find_composition (pos, -1, &pos, &end, &prop, string)
3945 && COMPOSITION_VALID_P (pos, end, prop)
3946 && (STRINGP (it->string) || (PT <= pos || PT >= end)))
3947 {
3948 int id = get_composition_id (pos, pos_byte, end - pos, prop, string);
3949
3950 if (id >= 0)
3951 {
3952 it->method = next_element_from_composition;
3953 it->cmp_id = id;
3954 it->cmp_len = COMPOSITION_LENGTH (prop);
3955 /* For a terminal, draw only the first character of the
3956 components. */
3957 it->c = COMPOSITION_GLYPH (composition_table[id], 0);
3958 it->len = (STRINGP (it->string)
3959 ? string_char_to_byte (it->string, end)
3960 : CHAR_TO_BYTE (end)) - pos_byte;
3961 it->stop_charpos = end;
3962 handled = HANDLED_RETURN;
3963 }
3964 }
3965
3966 return handled;
3967 }
3968
3969
3970 \f
3971 /***********************************************************************
3972 Overlay strings
3973 ***********************************************************************/
3974
3975 /* The following structure is used to record overlay strings for
3976 later sorting in load_overlay_strings. */
3977
3978 struct overlay_entry
3979 {
3980 Lisp_Object overlay;
3981 Lisp_Object string;
3982 int priority;
3983 int after_string_p;
3984 };
3985
3986
3987 /* Set up iterator IT from overlay strings at its current position.
3988 Called from handle_stop. */
3989
3990 static enum prop_handled
3991 handle_overlay_change (it)
3992 struct it *it;
3993 {
3994 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
3995 return HANDLED_RECOMPUTE_PROPS;
3996 else
3997 return HANDLED_NORMALLY;
3998 }
3999
4000
4001 /* Set up the next overlay string for delivery by IT, if there is an
4002 overlay string to deliver. Called by set_iterator_to_next when the
4003 end of the current overlay string is reached. If there are more
4004 overlay strings to display, IT->string and
4005 IT->current.overlay_string_index are set appropriately here.
4006 Otherwise IT->string is set to nil. */
4007
4008 static void
4009 next_overlay_string (it)
4010 struct it *it;
4011 {
4012 ++it->current.overlay_string_index;
4013 if (it->current.overlay_string_index == it->n_overlay_strings)
4014 {
4015 /* No more overlay strings. Restore IT's settings to what
4016 they were before overlay strings were processed, and
4017 continue to deliver from current_buffer. */
4018 int display_ellipsis_p = it->stack[it->sp - 1].display_ellipsis_p;
4019
4020 pop_it (it);
4021 xassert (it->stop_charpos >= BEGV
4022 && it->stop_charpos <= it->end_charpos);
4023 it->string = Qnil;
4024 it->current.overlay_string_index = -1;
4025 SET_TEXT_POS (it->current.string_pos, -1, -1);
4026 it->n_overlay_strings = 0;
4027 it->method = next_element_from_buffer;
4028
4029 /* If we're at the end of the buffer, record that we have
4030 processed the overlay strings there already, so that
4031 next_element_from_buffer doesn't try it again. */
4032 if (IT_CHARPOS (*it) >= it->end_charpos)
4033 it->overlay_strings_at_end_processed_p = 1;
4034
4035 /* If we have to display `...' for invisible text, set
4036 the iterator up for that. */
4037 if (display_ellipsis_p)
4038 setup_for_ellipsis (it);
4039 }
4040 else
4041 {
4042 /* There are more overlay strings to process. If
4043 IT->current.overlay_string_index has advanced to a position
4044 where we must load IT->overlay_strings with more strings, do
4045 it. */
4046 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
4047
4048 if (it->current.overlay_string_index && i == 0)
4049 load_overlay_strings (it, 0);
4050
4051 /* Initialize IT to deliver display elements from the overlay
4052 string. */
4053 it->string = it->overlay_strings[i];
4054 it->multibyte_p = STRING_MULTIBYTE (it->string);
4055 SET_TEXT_POS (it->current.string_pos, 0, 0);
4056 it->method = next_element_from_string;
4057 it->stop_charpos = 0;
4058 }
4059
4060 CHECK_IT (it);
4061 }
4062
4063
4064 /* Compare two overlay_entry structures E1 and E2. Used as a
4065 comparison function for qsort in load_overlay_strings. Overlay
4066 strings for the same position are sorted so that
4067
4068 1. All after-strings come in front of before-strings, except
4069 when they come from the same overlay.
4070
4071 2. Within after-strings, strings are sorted so that overlay strings
4072 from overlays with higher priorities come first.
4073
4074 2. Within before-strings, strings are sorted so that overlay
4075 strings from overlays with higher priorities come last.
4076
4077 Value is analogous to strcmp. */
4078
4079
4080 static int
4081 compare_overlay_entries (e1, e2)
4082 void *e1, *e2;
4083 {
4084 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
4085 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
4086 int result;
4087
4088 if (entry1->after_string_p != entry2->after_string_p)
4089 {
4090 /* Let after-strings appear in front of before-strings if
4091 they come from different overlays. */
4092 if (EQ (entry1->overlay, entry2->overlay))
4093 result = entry1->after_string_p ? 1 : -1;
4094 else
4095 result = entry1->after_string_p ? -1 : 1;
4096 }
4097 else if (entry1->after_string_p)
4098 /* After-strings sorted in order of decreasing priority. */
4099 result = entry2->priority - entry1->priority;
4100 else
4101 /* Before-strings sorted in order of increasing priority. */
4102 result = entry1->priority - entry2->priority;
4103
4104 return result;
4105 }
4106
4107
4108 /* Load the vector IT->overlay_strings with overlay strings from IT's
4109 current buffer position, or from CHARPOS if that is > 0. Set
4110 IT->n_overlays to the total number of overlay strings found.
4111
4112 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
4113 a time. On entry into load_overlay_strings,
4114 IT->current.overlay_string_index gives the number of overlay
4115 strings that have already been loaded by previous calls to this
4116 function.
4117
4118 IT->add_overlay_start contains an additional overlay start
4119 position to consider for taking overlay strings from, if non-zero.
4120 This position comes into play when the overlay has an `invisible'
4121 property, and both before and after-strings. When we've skipped to
4122 the end of the overlay, because of its `invisible' property, we
4123 nevertheless want its before-string to appear.
4124 IT->add_overlay_start will contain the overlay start position
4125 in this case.
4126
4127 Overlay strings are sorted so that after-string strings come in
4128 front of before-string strings. Within before and after-strings,
4129 strings are sorted by overlay priority. See also function
4130 compare_overlay_entries. */
4131
4132 static void
4133 load_overlay_strings (it, charpos)
4134 struct it *it;
4135 int charpos;
4136 {
4137 extern Lisp_Object Qafter_string, Qbefore_string, Qwindow, Qpriority;
4138 Lisp_Object overlay, window, str, invisible;
4139 struct Lisp_Overlay *ov;
4140 int start, end;
4141 int size = 20;
4142 int n = 0, i, j, invis_p;
4143 struct overlay_entry *entries
4144 = (struct overlay_entry *) alloca (size * sizeof *entries);
4145
4146 if (charpos <= 0)
4147 charpos = IT_CHARPOS (*it);
4148
4149 /* Append the overlay string STRING of overlay OVERLAY to vector
4150 `entries' which has size `size' and currently contains `n'
4151 elements. AFTER_P non-zero means STRING is an after-string of
4152 OVERLAY. */
4153 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
4154 do \
4155 { \
4156 Lisp_Object priority; \
4157 \
4158 if (n == size) \
4159 { \
4160 int new_size = 2 * size; \
4161 struct overlay_entry *old = entries; \
4162 entries = \
4163 (struct overlay_entry *) alloca (new_size \
4164 * sizeof *entries); \
4165 bcopy (old, entries, size * sizeof *entries); \
4166 size = new_size; \
4167 } \
4168 \
4169 entries[n].string = (STRING); \
4170 entries[n].overlay = (OVERLAY); \
4171 priority = Foverlay_get ((OVERLAY), Qpriority); \
4172 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
4173 entries[n].after_string_p = (AFTER_P); \
4174 ++n; \
4175 } \
4176 while (0)
4177
4178 /* Process overlay before the overlay center. */
4179 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
4180 {
4181 XSETMISC (overlay, ov);
4182 xassert (OVERLAYP (overlay));
4183 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4184 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4185
4186 if (end < charpos)
4187 break;
4188
4189 /* Skip this overlay if it doesn't start or end at IT's current
4190 position. */
4191 if (end != charpos && start != charpos)
4192 continue;
4193
4194 /* Skip this overlay if it doesn't apply to IT->w. */
4195 window = Foverlay_get (overlay, Qwindow);
4196 if (WINDOWP (window) && XWINDOW (window) != it->w)
4197 continue;
4198
4199 /* If the text ``under'' the overlay is invisible, both before-
4200 and after-strings from this overlay are visible; start and
4201 end position are indistinguishable. */
4202 invisible = Foverlay_get (overlay, Qinvisible);
4203 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4204
4205 /* If overlay has a non-empty before-string, record it. */
4206 if ((start == charpos || (end == charpos && invis_p))
4207 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4208 && SCHARS (str))
4209 RECORD_OVERLAY_STRING (overlay, str, 0);
4210
4211 /* If overlay has a non-empty after-string, record it. */
4212 if ((end == charpos || (start == charpos && invis_p))
4213 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4214 && SCHARS (str))
4215 RECORD_OVERLAY_STRING (overlay, str, 1);
4216 }
4217
4218 /* Process overlays after the overlay center. */
4219 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
4220 {
4221 XSETMISC (overlay, ov);
4222 xassert (OVERLAYP (overlay));
4223 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4224 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4225
4226 if (start > charpos)
4227 break;
4228
4229 /* Skip this overlay if it doesn't start or end at IT's current
4230 position. */
4231 if (end != charpos && start != charpos)
4232 continue;
4233
4234 /* Skip this overlay if it doesn't apply to IT->w. */
4235 window = Foverlay_get (overlay, Qwindow);
4236 if (WINDOWP (window) && XWINDOW (window) != it->w)
4237 continue;
4238
4239 /* If the text ``under'' the overlay is invisible, it has a zero
4240 dimension, and both before- and after-strings apply. */
4241 invisible = Foverlay_get (overlay, Qinvisible);
4242 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4243
4244 /* If overlay has a non-empty before-string, record it. */
4245 if ((start == charpos || (end == charpos && invis_p))
4246 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4247 && SCHARS (str))
4248 RECORD_OVERLAY_STRING (overlay, str, 0);
4249
4250 /* If overlay has a non-empty after-string, record it. */
4251 if ((end == charpos || (start == charpos && invis_p))
4252 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4253 && SCHARS (str))
4254 RECORD_OVERLAY_STRING (overlay, str, 1);
4255 }
4256
4257 #undef RECORD_OVERLAY_STRING
4258
4259 /* Sort entries. */
4260 if (n > 1)
4261 qsort (entries, n, sizeof *entries, compare_overlay_entries);
4262
4263 /* Record the total number of strings to process. */
4264 it->n_overlay_strings = n;
4265
4266 /* IT->current.overlay_string_index is the number of overlay strings
4267 that have already been consumed by IT. Copy some of the
4268 remaining overlay strings to IT->overlay_strings. */
4269 i = 0;
4270 j = it->current.overlay_string_index;
4271 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
4272 it->overlay_strings[i++] = entries[j++].string;
4273
4274 CHECK_IT (it);
4275 }
4276
4277
4278 /* Get the first chunk of overlay strings at IT's current buffer
4279 position, or at CHARPOS if that is > 0. Value is non-zero if at
4280 least one overlay string was found. */
4281
4282 static int
4283 get_overlay_strings (it, charpos)
4284 struct it *it;
4285 int charpos;
4286 {
4287 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
4288 process. This fills IT->overlay_strings with strings, and sets
4289 IT->n_overlay_strings to the total number of strings to process.
4290 IT->pos.overlay_string_index has to be set temporarily to zero
4291 because load_overlay_strings needs this; it must be set to -1
4292 when no overlay strings are found because a zero value would
4293 indicate a position in the first overlay string. */
4294 it->current.overlay_string_index = 0;
4295 load_overlay_strings (it, charpos);
4296
4297 /* If we found overlay strings, set up IT to deliver display
4298 elements from the first one. Otherwise set up IT to deliver
4299 from current_buffer. */
4300 if (it->n_overlay_strings)
4301 {
4302 /* Make sure we know settings in current_buffer, so that we can
4303 restore meaningful values when we're done with the overlay
4304 strings. */
4305 compute_stop_pos (it);
4306 xassert (it->face_id >= 0);
4307
4308 /* Save IT's settings. They are restored after all overlay
4309 strings have been processed. */
4310 xassert (it->sp == 0);
4311 push_it (it);
4312
4313 /* Set up IT to deliver display elements from the first overlay
4314 string. */
4315 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4316 it->string = it->overlay_strings[0];
4317 it->stop_charpos = 0;
4318 xassert (STRINGP (it->string));
4319 it->end_charpos = SCHARS (it->string);
4320 it->multibyte_p = STRING_MULTIBYTE (it->string);
4321 it->method = next_element_from_string;
4322 }
4323 else
4324 {
4325 it->string = Qnil;
4326 it->current.overlay_string_index = -1;
4327 it->method = next_element_from_buffer;
4328 }
4329
4330 CHECK_IT (it);
4331
4332 /* Value is non-zero if we found at least one overlay string. */
4333 return STRINGP (it->string);
4334 }
4335
4336
4337 \f
4338 /***********************************************************************
4339 Saving and restoring state
4340 ***********************************************************************/
4341
4342 /* Save current settings of IT on IT->stack. Called, for example,
4343 before setting up IT for an overlay string, to be able to restore
4344 IT's settings to what they were after the overlay string has been
4345 processed. */
4346
4347 static void
4348 push_it (it)
4349 struct it *it;
4350 {
4351 struct iterator_stack_entry *p;
4352
4353 xassert (it->sp < 2);
4354 p = it->stack + it->sp;
4355
4356 p->stop_charpos = it->stop_charpos;
4357 xassert (it->face_id >= 0);
4358 p->face_id = it->face_id;
4359 p->string = it->string;
4360 p->pos = it->current;
4361 p->end_charpos = it->end_charpos;
4362 p->string_nchars = it->string_nchars;
4363 p->area = it->area;
4364 p->multibyte_p = it->multibyte_p;
4365 p->slice = it->slice;
4366 p->space_width = it->space_width;
4367 p->font_height = it->font_height;
4368 p->voffset = it->voffset;
4369 p->string_from_display_prop_p = it->string_from_display_prop_p;
4370 p->display_ellipsis_p = 0;
4371 ++it->sp;
4372 }
4373
4374
4375 /* Restore IT's settings from IT->stack. Called, for example, when no
4376 more overlay strings must be processed, and we return to delivering
4377 display elements from a buffer, or when the end of a string from a
4378 `display' property is reached and we return to delivering display
4379 elements from an overlay string, or from a buffer. */
4380
4381 static void
4382 pop_it (it)
4383 struct it *it;
4384 {
4385 struct iterator_stack_entry *p;
4386
4387 xassert (it->sp > 0);
4388 --it->sp;
4389 p = it->stack + it->sp;
4390 it->stop_charpos = p->stop_charpos;
4391 it->face_id = p->face_id;
4392 it->string = p->string;
4393 it->current = p->pos;
4394 it->end_charpos = p->end_charpos;
4395 it->string_nchars = p->string_nchars;
4396 it->area = p->area;
4397 it->multibyte_p = p->multibyte_p;
4398 it->slice = p->slice;
4399 it->space_width = p->space_width;
4400 it->font_height = p->font_height;
4401 it->voffset = p->voffset;
4402 it->string_from_display_prop_p = p->string_from_display_prop_p;
4403 }
4404
4405
4406 \f
4407 /***********************************************************************
4408 Moving over lines
4409 ***********************************************************************/
4410
4411 /* Set IT's current position to the previous line start. */
4412
4413 static void
4414 back_to_previous_line_start (it)
4415 struct it *it;
4416 {
4417 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
4418 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
4419 }
4420
4421
4422 /* Move IT to the next line start.
4423
4424 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
4425 we skipped over part of the text (as opposed to moving the iterator
4426 continuously over the text). Otherwise, don't change the value
4427 of *SKIPPED_P.
4428
4429 Newlines may come from buffer text, overlay strings, or strings
4430 displayed via the `display' property. That's the reason we can't
4431 simply use find_next_newline_no_quit.
4432
4433 Note that this function may not skip over invisible text that is so
4434 because of text properties and immediately follows a newline. If
4435 it would, function reseat_at_next_visible_line_start, when called
4436 from set_iterator_to_next, would effectively make invisible
4437 characters following a newline part of the wrong glyph row, which
4438 leads to wrong cursor motion. */
4439
4440 static int
4441 forward_to_next_line_start (it, skipped_p)
4442 struct it *it;
4443 int *skipped_p;
4444 {
4445 int old_selective, newline_found_p, n;
4446 const int MAX_NEWLINE_DISTANCE = 500;
4447
4448 /* If already on a newline, just consume it to avoid unintended
4449 skipping over invisible text below. */
4450 if (it->what == IT_CHARACTER
4451 && it->c == '\n'
4452 && CHARPOS (it->position) == IT_CHARPOS (*it))
4453 {
4454 set_iterator_to_next (it, 0);
4455 it->c = 0;
4456 return 1;
4457 }
4458
4459 /* Don't handle selective display in the following. It's (a)
4460 unnecessary because it's done by the caller, and (b) leads to an
4461 infinite recursion because next_element_from_ellipsis indirectly
4462 calls this function. */
4463 old_selective = it->selective;
4464 it->selective = 0;
4465
4466 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
4467 from buffer text. */
4468 for (n = newline_found_p = 0;
4469 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
4470 n += STRINGP (it->string) ? 0 : 1)
4471 {
4472 if (!get_next_display_element (it))
4473 return 0;
4474 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
4475 set_iterator_to_next (it, 0);
4476 }
4477
4478 /* If we didn't find a newline near enough, see if we can use a
4479 short-cut. */
4480 if (!newline_found_p)
4481 {
4482 int start = IT_CHARPOS (*it);
4483 int limit = find_next_newline_no_quit (start, 1);
4484 Lisp_Object pos;
4485
4486 xassert (!STRINGP (it->string));
4487
4488 /* If there isn't any `display' property in sight, and no
4489 overlays, we can just use the position of the newline in
4490 buffer text. */
4491 if (it->stop_charpos >= limit
4492 || ((pos = Fnext_single_property_change (make_number (start),
4493 Qdisplay,
4494 Qnil, make_number (limit)),
4495 NILP (pos))
4496 && next_overlay_change (start) == ZV))
4497 {
4498 IT_CHARPOS (*it) = limit;
4499 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
4500 *skipped_p = newline_found_p = 1;
4501 }
4502 else
4503 {
4504 while (get_next_display_element (it)
4505 && !newline_found_p)
4506 {
4507 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
4508 set_iterator_to_next (it, 0);
4509 }
4510 }
4511 }
4512
4513 it->selective = old_selective;
4514 return newline_found_p;
4515 }
4516
4517
4518 /* Set IT's current position to the previous visible line start. Skip
4519 invisible text that is so either due to text properties or due to
4520 selective display. Caution: this does not change IT->current_x and
4521 IT->hpos. */
4522
4523 static void
4524 back_to_previous_visible_line_start (it)
4525 struct it *it;
4526 {
4527 int visible_p = 0;
4528
4529 /* Go back one newline if not on BEGV already. */
4530 if (IT_CHARPOS (*it) > BEGV)
4531 back_to_previous_line_start (it);
4532
4533 /* Move over lines that are invisible because of selective display
4534 or text properties. */
4535 while (IT_CHARPOS (*it) > BEGV
4536 && !visible_p)
4537 {
4538 visible_p = 1;
4539
4540 /* If selective > 0, then lines indented more than that values
4541 are invisible. */
4542 if (it->selective > 0
4543 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
4544 (double) it->selective)) /* iftc */
4545 visible_p = 0;
4546 else
4547 {
4548 Lisp_Object prop;
4549
4550 prop = Fget_char_property (make_number (IT_CHARPOS (*it)),
4551 Qinvisible, it->window);
4552 if (TEXT_PROP_MEANS_INVISIBLE (prop))
4553 visible_p = 0;
4554 }
4555
4556 if (visible_p)
4557 {
4558 struct it it2 = *it;
4559
4560 if (handle_display_prop (&it2) == HANDLED_RETURN)
4561 visible_p = 0;
4562 }
4563
4564 /* Back one more newline if the current one is invisible. */
4565 if (!visible_p)
4566 back_to_previous_line_start (it);
4567 }
4568
4569 xassert (IT_CHARPOS (*it) >= BEGV);
4570 xassert (IT_CHARPOS (*it) == BEGV
4571 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
4572 CHECK_IT (it);
4573 }
4574
4575
4576 /* Reseat iterator IT at the previous visible line start. Skip
4577 invisible text that is so either due to text properties or due to
4578 selective display. At the end, update IT's overlay information,
4579 face information etc. */
4580
4581 static void
4582 reseat_at_previous_visible_line_start (it)
4583 struct it *it;
4584 {
4585 back_to_previous_visible_line_start (it);
4586 reseat (it, it->current.pos, 1);
4587 CHECK_IT (it);
4588 }
4589
4590
4591 /* Reseat iterator IT on the next visible line start in the current
4592 buffer. ON_NEWLINE_P non-zero means position IT on the newline
4593 preceding the line start. Skip over invisible text that is so
4594 because of selective display. Compute faces, overlays etc at the
4595 new position. Note that this function does not skip over text that
4596 is invisible because of text properties. */
4597
4598 static void
4599 reseat_at_next_visible_line_start (it, on_newline_p)
4600 struct it *it;
4601 int on_newline_p;
4602 {
4603 int newline_found_p, skipped_p = 0;
4604
4605 newline_found_p = forward_to_next_line_start (it, &skipped_p);
4606
4607 /* Skip over lines that are invisible because they are indented
4608 more than the value of IT->selective. */
4609 if (it->selective > 0)
4610 while (IT_CHARPOS (*it) < ZV
4611 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
4612 (double) it->selective)) /* iftc */
4613 {
4614 xassert (FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
4615 newline_found_p = forward_to_next_line_start (it, &skipped_p);
4616 }
4617
4618 /* Position on the newline if that's what's requested. */
4619 if (on_newline_p && newline_found_p)
4620 {
4621 if (STRINGP (it->string))
4622 {
4623 if (IT_STRING_CHARPOS (*it) > 0)
4624 {
4625 --IT_STRING_CHARPOS (*it);
4626 --IT_STRING_BYTEPOS (*it);
4627 }
4628 }
4629 else if (IT_CHARPOS (*it) > BEGV)
4630 {
4631 --IT_CHARPOS (*it);
4632 --IT_BYTEPOS (*it);
4633 reseat (it, it->current.pos, 0);
4634 }
4635 }
4636 else if (skipped_p)
4637 reseat (it, it->current.pos, 0);
4638
4639 CHECK_IT (it);
4640 }
4641
4642
4643 \f
4644 /***********************************************************************
4645 Changing an iterator's position
4646 ***********************************************************************/
4647
4648 /* Change IT's current position to POS in current_buffer. If FORCE_P
4649 is non-zero, always check for text properties at the new position.
4650 Otherwise, text properties are only looked up if POS >=
4651 IT->check_charpos of a property. */
4652
4653 static void
4654 reseat (it, pos, force_p)
4655 struct it *it;
4656 struct text_pos pos;
4657 int force_p;
4658 {
4659 int original_pos = IT_CHARPOS (*it);
4660
4661 reseat_1 (it, pos, 0);
4662
4663 /* Determine where to check text properties. Avoid doing it
4664 where possible because text property lookup is very expensive. */
4665 if (force_p
4666 || CHARPOS (pos) > it->stop_charpos
4667 || CHARPOS (pos) < original_pos)
4668 handle_stop (it);
4669
4670 CHECK_IT (it);
4671 }
4672
4673
4674 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
4675 IT->stop_pos to POS, also. */
4676
4677 static void
4678 reseat_1 (it, pos, set_stop_p)
4679 struct it *it;
4680 struct text_pos pos;
4681 int set_stop_p;
4682 {
4683 /* Don't call this function when scanning a C string. */
4684 xassert (it->s == NULL);
4685
4686 /* POS must be a reasonable value. */
4687 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
4688
4689 it->current.pos = it->position = pos;
4690 XSETBUFFER (it->object, current_buffer);
4691 it->end_charpos = ZV;
4692 it->dpvec = NULL;
4693 it->current.dpvec_index = -1;
4694 it->current.overlay_string_index = -1;
4695 IT_STRING_CHARPOS (*it) = -1;
4696 IT_STRING_BYTEPOS (*it) = -1;
4697 it->string = Qnil;
4698 it->method = next_element_from_buffer;
4699 /* RMS: I added this to fix a bug in move_it_vertically_backward
4700 where it->area continued to relate to the starting point
4701 for the backward motion. Bug report from
4702 Nick Roberts <nick@nick.uklinux.net> on 19 May 2003.
4703 However, I am not sure whether reseat still does the right thing
4704 in general after this change. */
4705 it->area = TEXT_AREA;
4706 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
4707 it->sp = 0;
4708 it->face_before_selective_p = 0;
4709
4710 if (set_stop_p)
4711 it->stop_charpos = CHARPOS (pos);
4712 }
4713
4714
4715 /* Set up IT for displaying a string, starting at CHARPOS in window W.
4716 If S is non-null, it is a C string to iterate over. Otherwise,
4717 STRING gives a Lisp string to iterate over.
4718
4719 If PRECISION > 0, don't return more then PRECISION number of
4720 characters from the string.
4721
4722 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
4723 characters have been returned. FIELD_WIDTH < 0 means an infinite
4724 field width.
4725
4726 MULTIBYTE = 0 means disable processing of multibyte characters,
4727 MULTIBYTE > 0 means enable it,
4728 MULTIBYTE < 0 means use IT->multibyte_p.
4729
4730 IT must be initialized via a prior call to init_iterator before
4731 calling this function. */
4732
4733 static void
4734 reseat_to_string (it, s, string, charpos, precision, field_width, multibyte)
4735 struct it *it;
4736 unsigned char *s;
4737 Lisp_Object string;
4738 int charpos;
4739 int precision, field_width, multibyte;
4740 {
4741 /* No region in strings. */
4742 it->region_beg_charpos = it->region_end_charpos = -1;
4743
4744 /* No text property checks performed by default, but see below. */
4745 it->stop_charpos = -1;
4746
4747 /* Set iterator position and end position. */
4748 bzero (&it->current, sizeof it->current);
4749 it->current.overlay_string_index = -1;
4750 it->current.dpvec_index = -1;
4751 xassert (charpos >= 0);
4752
4753 /* If STRING is specified, use its multibyteness, otherwise use the
4754 setting of MULTIBYTE, if specified. */
4755 if (multibyte >= 0)
4756 it->multibyte_p = multibyte > 0;
4757
4758 if (s == NULL)
4759 {
4760 xassert (STRINGP (string));
4761 it->string = string;
4762 it->s = NULL;
4763 it->end_charpos = it->string_nchars = SCHARS (string);
4764 it->method = next_element_from_string;
4765 it->current.string_pos = string_pos (charpos, string);
4766 }
4767 else
4768 {
4769 it->s = s;
4770 it->string = Qnil;
4771
4772 /* Note that we use IT->current.pos, not it->current.string_pos,
4773 for displaying C strings. */
4774 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
4775 if (it->multibyte_p)
4776 {
4777 it->current.pos = c_string_pos (charpos, s, 1);
4778 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
4779 }
4780 else
4781 {
4782 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
4783 it->end_charpos = it->string_nchars = strlen (s);
4784 }
4785
4786 it->method = next_element_from_c_string;
4787 }
4788
4789 /* PRECISION > 0 means don't return more than PRECISION characters
4790 from the string. */
4791 if (precision > 0 && it->end_charpos - charpos > precision)
4792 it->end_charpos = it->string_nchars = charpos + precision;
4793
4794 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
4795 characters have been returned. FIELD_WIDTH == 0 means don't pad,
4796 FIELD_WIDTH < 0 means infinite field width. This is useful for
4797 padding with `-' at the end of a mode line. */
4798 if (field_width < 0)
4799 field_width = INFINITY;
4800 if (field_width > it->end_charpos - charpos)
4801 it->end_charpos = charpos + field_width;
4802
4803 /* Use the standard display table for displaying strings. */
4804 if (DISP_TABLE_P (Vstandard_display_table))
4805 it->dp = XCHAR_TABLE (Vstandard_display_table);
4806
4807 it->stop_charpos = charpos;
4808 CHECK_IT (it);
4809 }
4810
4811
4812 \f
4813 /***********************************************************************
4814 Iteration
4815 ***********************************************************************/
4816
4817 /* Load IT's display element fields with information about the next
4818 display element from the current position of IT. Value is zero if
4819 end of buffer (or C string) is reached. */
4820
4821 int
4822 get_next_display_element (it)
4823 struct it *it;
4824 {
4825 /* Non-zero means that we found a display element. Zero means that
4826 we hit the end of what we iterate over. Performance note: the
4827 function pointer `method' used here turns out to be faster than
4828 using a sequence of if-statements. */
4829 int success_p = (*it->method) (it);
4830
4831 if (it->what == IT_CHARACTER)
4832 {
4833 /* Map via display table or translate control characters.
4834 IT->c, IT->len etc. have been set to the next character by
4835 the function call above. If we have a display table, and it
4836 contains an entry for IT->c, translate it. Don't do this if
4837 IT->c itself comes from a display table, otherwise we could
4838 end up in an infinite recursion. (An alternative could be to
4839 count the recursion depth of this function and signal an
4840 error when a certain maximum depth is reached.) Is it worth
4841 it? */
4842 if (success_p && it->dpvec == NULL)
4843 {
4844 Lisp_Object dv;
4845
4846 if (it->dp
4847 && (dv = DISP_CHAR_VECTOR (it->dp, it->c),
4848 VECTORP (dv)))
4849 {
4850 struct Lisp_Vector *v = XVECTOR (dv);
4851
4852 /* Return the first character from the display table
4853 entry, if not empty. If empty, don't display the
4854 current character. */
4855 if (v->size)
4856 {
4857 it->dpvec_char_len = it->len;
4858 it->dpvec = v->contents;
4859 it->dpend = v->contents + v->size;
4860 it->current.dpvec_index = 0;
4861 it->method = next_element_from_display_vector;
4862 success_p = get_next_display_element (it);
4863 }
4864 else
4865 {
4866 set_iterator_to_next (it, 0);
4867 success_p = get_next_display_element (it);
4868 }
4869 }
4870
4871 /* Translate control characters into `\003' or `^C' form.
4872 Control characters coming from a display table entry are
4873 currently not translated because we use IT->dpvec to hold
4874 the translation. This could easily be changed but I
4875 don't believe that it is worth doing.
4876
4877 If it->multibyte_p is nonzero, eight-bit characters and
4878 non-printable multibyte characters are also translated to
4879 octal form.
4880
4881 If it->multibyte_p is zero, eight-bit characters that
4882 don't have corresponding multibyte char code are also
4883 translated to octal form. */
4884 else if ((it->c < ' '
4885 && (it->area != TEXT_AREA
4886 || (it->c != '\n' && it->c != '\t')))
4887 || (it->multibyte_p
4888 ? ((it->c >= 127
4889 && it->len == 1)
4890 || !CHAR_PRINTABLE_P (it->c))
4891 : (it->c >= 127
4892 && it->c == unibyte_char_to_multibyte (it->c))))
4893 {
4894 /* IT->c is a control character which must be displayed
4895 either as '\003' or as `^C' where the '\\' and '^'
4896 can be defined in the display table. Fill
4897 IT->ctl_chars with glyphs for what we have to
4898 display. Then, set IT->dpvec to these glyphs. */
4899 GLYPH g;
4900
4901 if (it->c < 128 && it->ctl_arrow_p)
4902 {
4903 /* Set IT->ctl_chars[0] to the glyph for `^'. */
4904 if (it->dp
4905 && INTEGERP (DISP_CTRL_GLYPH (it->dp))
4906 && GLYPH_CHAR_VALID_P (XINT (DISP_CTRL_GLYPH (it->dp))))
4907 g = XINT (DISP_CTRL_GLYPH (it->dp));
4908 else
4909 g = FAST_MAKE_GLYPH ('^', 0);
4910 XSETINT (it->ctl_chars[0], g);
4911
4912 g = FAST_MAKE_GLYPH (it->c ^ 0100, 0);
4913 XSETINT (it->ctl_chars[1], g);
4914
4915 /* Set up IT->dpvec and return first character from it. */
4916 it->dpvec_char_len = it->len;
4917 it->dpvec = it->ctl_chars;
4918 it->dpend = it->dpvec + 2;
4919 it->current.dpvec_index = 0;
4920 it->method = next_element_from_display_vector;
4921 get_next_display_element (it);
4922 }
4923 else
4924 {
4925 unsigned char str[MAX_MULTIBYTE_LENGTH];
4926 int len;
4927 int i;
4928 GLYPH escape_glyph;
4929
4930 /* Set IT->ctl_chars[0] to the glyph for `\\'. */
4931 if (it->dp
4932 && INTEGERP (DISP_ESCAPE_GLYPH (it->dp))
4933 && GLYPH_CHAR_VALID_P (XFASTINT (DISP_ESCAPE_GLYPH (it->dp))))
4934 escape_glyph = XFASTINT (DISP_ESCAPE_GLYPH (it->dp));
4935 else
4936 escape_glyph = FAST_MAKE_GLYPH ('\\', 0);
4937
4938 if (SINGLE_BYTE_CHAR_P (it->c))
4939 str[0] = it->c, len = 1;
4940 else
4941 {
4942 len = CHAR_STRING_NO_SIGNAL (it->c, str);
4943 if (len < 0)
4944 {
4945 /* It's an invalid character, which
4946 shouldn't happen actually, but due to
4947 bugs it may happen. Let's print the char
4948 as is, there's not much meaningful we can
4949 do with it. */
4950 str[0] = it->c;
4951 str[1] = it->c >> 8;
4952 str[2] = it->c >> 16;
4953 str[3] = it->c >> 24;
4954 len = 4;
4955 }
4956 }
4957
4958 for (i = 0; i < len; i++)
4959 {
4960 XSETINT (it->ctl_chars[i * 4], escape_glyph);
4961 /* Insert three more glyphs into IT->ctl_chars for
4962 the octal display of the character. */
4963 g = FAST_MAKE_GLYPH (((str[i] >> 6) & 7) + '0', 0);
4964 XSETINT (it->ctl_chars[i * 4 + 1], g);
4965 g = FAST_MAKE_GLYPH (((str[i] >> 3) & 7) + '0', 0);
4966 XSETINT (it->ctl_chars[i * 4 + 2], g);
4967 g = FAST_MAKE_GLYPH ((str[i] & 7) + '0', 0);
4968 XSETINT (it->ctl_chars[i * 4 + 3], g);
4969 }
4970
4971 /* Set up IT->dpvec and return the first character
4972 from it. */
4973 it->dpvec_char_len = it->len;
4974 it->dpvec = it->ctl_chars;
4975 it->dpend = it->dpvec + len * 4;
4976 it->current.dpvec_index = 0;
4977 it->method = next_element_from_display_vector;
4978 get_next_display_element (it);
4979 }
4980 }
4981 }
4982
4983 /* Adjust face id for a multibyte character. There are no
4984 multibyte character in unibyte text. */
4985 if (it->multibyte_p
4986 && success_p
4987 && FRAME_WINDOW_P (it->f))
4988 {
4989 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4990 it->face_id = FACE_FOR_CHAR (it->f, face, it->c);
4991 }
4992 }
4993
4994 /* Is this character the last one of a run of characters with
4995 box? If yes, set IT->end_of_box_run_p to 1. */
4996 if (it->face_box_p
4997 && it->s == NULL)
4998 {
4999 int face_id;
5000 struct face *face;
5001
5002 it->end_of_box_run_p
5003 = ((face_id = face_after_it_pos (it),
5004 face_id != it->face_id)
5005 && (face = FACE_FROM_ID (it->f, face_id),
5006 face->box == FACE_NO_BOX));
5007 }
5008
5009 /* Value is 0 if end of buffer or string reached. */
5010 return success_p;
5011 }
5012
5013
5014 /* Move IT to the next display element.
5015
5016 RESEAT_P non-zero means if called on a newline in buffer text,
5017 skip to the next visible line start.
5018
5019 Functions get_next_display_element and set_iterator_to_next are
5020 separate because I find this arrangement easier to handle than a
5021 get_next_display_element function that also increments IT's
5022 position. The way it is we can first look at an iterator's current
5023 display element, decide whether it fits on a line, and if it does,
5024 increment the iterator position. The other way around we probably
5025 would either need a flag indicating whether the iterator has to be
5026 incremented the next time, or we would have to implement a
5027 decrement position function which would not be easy to write. */
5028
5029 void
5030 set_iterator_to_next (it, reseat_p)
5031 struct it *it;
5032 int reseat_p;
5033 {
5034 /* Reset flags indicating start and end of a sequence of characters
5035 with box. Reset them at the start of this function because
5036 moving the iterator to a new position might set them. */
5037 it->start_of_box_run_p = it->end_of_box_run_p = 0;
5038
5039 if (it->method == next_element_from_buffer)
5040 {
5041 /* The current display element of IT is a character from
5042 current_buffer. Advance in the buffer, and maybe skip over
5043 invisible lines that are so because of selective display. */
5044 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
5045 reseat_at_next_visible_line_start (it, 0);
5046 else
5047 {
5048 xassert (it->len != 0);
5049 IT_BYTEPOS (*it) += it->len;
5050 IT_CHARPOS (*it) += 1;
5051 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
5052 }
5053 }
5054 else if (it->method == next_element_from_composition)
5055 {
5056 xassert (it->cmp_id >= 0 && it ->cmp_id < n_compositions);
5057 if (STRINGP (it->string))
5058 {
5059 IT_STRING_BYTEPOS (*it) += it->len;
5060 IT_STRING_CHARPOS (*it) += it->cmp_len;
5061 it->method = next_element_from_string;
5062 goto consider_string_end;
5063 }
5064 else
5065 {
5066 IT_BYTEPOS (*it) += it->len;
5067 IT_CHARPOS (*it) += it->cmp_len;
5068 it->method = next_element_from_buffer;
5069 }
5070 }
5071 else if (it->method == next_element_from_c_string)
5072 {
5073 /* Current display element of IT is from a C string. */
5074 IT_BYTEPOS (*it) += it->len;
5075 IT_CHARPOS (*it) += 1;
5076 }
5077 else if (it->method == next_element_from_display_vector)
5078 {
5079 /* Current display element of IT is from a display table entry.
5080 Advance in the display table definition. Reset it to null if
5081 end reached, and continue with characters from buffers/
5082 strings. */
5083 ++it->current.dpvec_index;
5084
5085 /* Restore face of the iterator to what they were before the
5086 display vector entry (these entries may contain faces). */
5087 it->face_id = it->saved_face_id;
5088
5089 if (it->dpvec + it->current.dpvec_index == it->dpend)
5090 {
5091 if (it->s)
5092 it->method = next_element_from_c_string;
5093 else if (STRINGP (it->string))
5094 it->method = next_element_from_string;
5095 else
5096 it->method = next_element_from_buffer;
5097
5098 it->dpvec = NULL;
5099 it->current.dpvec_index = -1;
5100
5101 /* Skip over characters which were displayed via IT->dpvec. */
5102 if (it->dpvec_char_len < 0)
5103 reseat_at_next_visible_line_start (it, 1);
5104 else if (it->dpvec_char_len > 0)
5105 {
5106 it->len = it->dpvec_char_len;
5107 set_iterator_to_next (it, reseat_p);
5108 }
5109 }
5110 }
5111 else if (it->method == next_element_from_string)
5112 {
5113 /* Current display element is a character from a Lisp string. */
5114 xassert (it->s == NULL && STRINGP (it->string));
5115 IT_STRING_BYTEPOS (*it) += it->len;
5116 IT_STRING_CHARPOS (*it) += 1;
5117
5118 consider_string_end:
5119
5120 if (it->current.overlay_string_index >= 0)
5121 {
5122 /* IT->string is an overlay string. Advance to the
5123 next, if there is one. */
5124 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
5125 next_overlay_string (it);
5126 }
5127 else
5128 {
5129 /* IT->string is not an overlay string. If we reached
5130 its end, and there is something on IT->stack, proceed
5131 with what is on the stack. This can be either another
5132 string, this time an overlay string, or a buffer. */
5133 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
5134 && it->sp > 0)
5135 {
5136 pop_it (it);
5137 if (!STRINGP (it->string))
5138 it->method = next_element_from_buffer;
5139 else
5140 goto consider_string_end;
5141 }
5142 }
5143 }
5144 else if (it->method == next_element_from_image
5145 || it->method == next_element_from_stretch)
5146 {
5147 /* The position etc with which we have to proceed are on
5148 the stack. The position may be at the end of a string,
5149 if the `display' property takes up the whole string. */
5150 pop_it (it);
5151 it->image_id = 0;
5152 if (STRINGP (it->string))
5153 {
5154 it->method = next_element_from_string;
5155 goto consider_string_end;
5156 }
5157 else
5158 it->method = next_element_from_buffer;
5159 }
5160 else
5161 /* There are no other methods defined, so this should be a bug. */
5162 abort ();
5163
5164 xassert (it->method != next_element_from_string
5165 || (STRINGP (it->string)
5166 && IT_STRING_CHARPOS (*it) >= 0));
5167 }
5168
5169
5170 /* Load IT's display element fields with information about the next
5171 display element which comes from a display table entry or from the
5172 result of translating a control character to one of the forms `^C'
5173 or `\003'. IT->dpvec holds the glyphs to return as characters. */
5174
5175 static int
5176 next_element_from_display_vector (it)
5177 struct it *it;
5178 {
5179 /* Precondition. */
5180 xassert (it->dpvec && it->current.dpvec_index >= 0);
5181
5182 /* Remember the current face id in case glyphs specify faces.
5183 IT's face is restored in set_iterator_to_next. */
5184 it->saved_face_id = it->face_id;
5185
5186 if (INTEGERP (*it->dpvec)
5187 && GLYPH_CHAR_VALID_P (XFASTINT (*it->dpvec)))
5188 {
5189 int lface_id;
5190 GLYPH g;
5191
5192 g = XFASTINT (it->dpvec[it->current.dpvec_index]);
5193 it->c = FAST_GLYPH_CHAR (g);
5194 it->len = CHAR_BYTES (it->c);
5195
5196 /* The entry may contain a face id to use. Such a face id is
5197 the id of a Lisp face, not a realized face. A face id of
5198 zero means no face is specified. */
5199 lface_id = FAST_GLYPH_FACE (g);
5200 if (lface_id)
5201 {
5202 /* The function returns -1 if lface_id is invalid. */
5203 int face_id = ascii_face_of_lisp_face (it->f, lface_id);
5204 if (face_id >= 0)
5205 it->face_id = face_id;
5206 }
5207 }
5208 else
5209 /* Display table entry is invalid. Return a space. */
5210 it->c = ' ', it->len = 1;
5211
5212 /* Don't change position and object of the iterator here. They are
5213 still the values of the character that had this display table
5214 entry or was translated, and that's what we want. */
5215 it->what = IT_CHARACTER;
5216 return 1;
5217 }
5218
5219
5220 /* Load IT with the next display element from Lisp string IT->string.
5221 IT->current.string_pos is the current position within the string.
5222 If IT->current.overlay_string_index >= 0, the Lisp string is an
5223 overlay string. */
5224
5225 static int
5226 next_element_from_string (it)
5227 struct it *it;
5228 {
5229 struct text_pos position;
5230
5231 xassert (STRINGP (it->string));
5232 xassert (IT_STRING_CHARPOS (*it) >= 0);
5233 position = it->current.string_pos;
5234
5235 /* Time to check for invisible text? */
5236 if (IT_STRING_CHARPOS (*it) < it->end_charpos
5237 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
5238 {
5239 handle_stop (it);
5240
5241 /* Since a handler may have changed IT->method, we must
5242 recurse here. */
5243 return get_next_display_element (it);
5244 }
5245
5246 if (it->current.overlay_string_index >= 0)
5247 {
5248 /* Get the next character from an overlay string. In overlay
5249 strings, There is no field width or padding with spaces to
5250 do. */
5251 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
5252 {
5253 it->what = IT_EOB;
5254 return 0;
5255 }
5256 else if (STRING_MULTIBYTE (it->string))
5257 {
5258 int remaining = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
5259 const unsigned char *s = (SDATA (it->string)
5260 + IT_STRING_BYTEPOS (*it));
5261 it->c = string_char_and_length (s, remaining, &it->len);
5262 }
5263 else
5264 {
5265 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
5266 it->len = 1;
5267 }
5268 }
5269 else
5270 {
5271 /* Get the next character from a Lisp string that is not an
5272 overlay string. Such strings come from the mode line, for
5273 example. We may have to pad with spaces, or truncate the
5274 string. See also next_element_from_c_string. */
5275 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
5276 {
5277 it->what = IT_EOB;
5278 return 0;
5279 }
5280 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
5281 {
5282 /* Pad with spaces. */
5283 it->c = ' ', it->len = 1;
5284 CHARPOS (position) = BYTEPOS (position) = -1;
5285 }
5286 else if (STRING_MULTIBYTE (it->string))
5287 {
5288 int maxlen = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
5289 const unsigned char *s = (SDATA (it->string)
5290 + IT_STRING_BYTEPOS (*it));
5291 it->c = string_char_and_length (s, maxlen, &it->len);
5292 }
5293 else
5294 {
5295 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
5296 it->len = 1;
5297 }
5298 }
5299
5300 /* Record what we have and where it came from. Note that we store a
5301 buffer position in IT->position although it could arguably be a
5302 string position. */
5303 it->what = IT_CHARACTER;
5304 it->object = it->string;
5305 it->position = position;
5306 return 1;
5307 }
5308
5309
5310 /* Load IT with next display element from C string IT->s.
5311 IT->string_nchars is the maximum number of characters to return
5312 from the string. IT->end_charpos may be greater than
5313 IT->string_nchars when this function is called, in which case we
5314 may have to return padding spaces. Value is zero if end of string
5315 reached, including padding spaces. */
5316
5317 static int
5318 next_element_from_c_string (it)
5319 struct it *it;
5320 {
5321 int success_p = 1;
5322
5323 xassert (it->s);
5324 it->what = IT_CHARACTER;
5325 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
5326 it->object = Qnil;
5327
5328 /* IT's position can be greater IT->string_nchars in case a field
5329 width or precision has been specified when the iterator was
5330 initialized. */
5331 if (IT_CHARPOS (*it) >= it->end_charpos)
5332 {
5333 /* End of the game. */
5334 it->what = IT_EOB;
5335 success_p = 0;
5336 }
5337 else if (IT_CHARPOS (*it) >= it->string_nchars)
5338 {
5339 /* Pad with spaces. */
5340 it->c = ' ', it->len = 1;
5341 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
5342 }
5343 else if (it->multibyte_p)
5344 {
5345 /* Implementation note: The calls to strlen apparently aren't a
5346 performance problem because there is no noticeable performance
5347 difference between Emacs running in unibyte or multibyte mode. */
5348 int maxlen = strlen (it->s) - IT_BYTEPOS (*it);
5349 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it),
5350 maxlen, &it->len);
5351 }
5352 else
5353 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
5354
5355 return success_p;
5356 }
5357
5358
5359 /* Set up IT to return characters from an ellipsis, if appropriate.
5360 The definition of the ellipsis glyphs may come from a display table
5361 entry. This function Fills IT with the first glyph from the
5362 ellipsis if an ellipsis is to be displayed. */
5363
5364 static int
5365 next_element_from_ellipsis (it)
5366 struct it *it;
5367 {
5368 if (it->selective_display_ellipsis_p)
5369 {
5370 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
5371 {
5372 /* Use the display table definition for `...'. Invalid glyphs
5373 will be handled by the method returning elements from dpvec. */
5374 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
5375 it->dpvec_char_len = it->len;
5376 it->dpvec = v->contents;
5377 it->dpend = v->contents + v->size;
5378 it->current.dpvec_index = 0;
5379 it->method = next_element_from_display_vector;
5380 }
5381 else
5382 {
5383 /* Use default `...' which is stored in default_invis_vector. */
5384 it->dpvec_char_len = it->len;
5385 it->dpvec = default_invis_vector;
5386 it->dpend = default_invis_vector + 3;
5387 it->current.dpvec_index = 0;
5388 it->method = next_element_from_display_vector;
5389 }
5390 }
5391 else
5392 {
5393 /* The face at the current position may be different from the
5394 face we find after the invisible text. Remember what it
5395 was in IT->saved_face_id, and signal that it's there by
5396 setting face_before_selective_p. */
5397 it->saved_face_id = it->face_id;
5398 it->method = next_element_from_buffer;
5399 reseat_at_next_visible_line_start (it, 1);
5400 it->face_before_selective_p = 1;
5401 }
5402
5403 return get_next_display_element (it);
5404 }
5405
5406
5407 /* Deliver an image display element. The iterator IT is already
5408 filled with image information (done in handle_display_prop). Value
5409 is always 1. */
5410
5411
5412 static int
5413 next_element_from_image (it)
5414 struct it *it;
5415 {
5416 it->what = IT_IMAGE;
5417 return 1;
5418 }
5419
5420
5421 /* Fill iterator IT with next display element from a stretch glyph
5422 property. IT->object is the value of the text property. Value is
5423 always 1. */
5424
5425 static int
5426 next_element_from_stretch (it)
5427 struct it *it;
5428 {
5429 it->what = IT_STRETCH;
5430 return 1;
5431 }
5432
5433
5434 /* Load IT with the next display element from current_buffer. Value
5435 is zero if end of buffer reached. IT->stop_charpos is the next
5436 position at which to stop and check for text properties or buffer
5437 end. */
5438
5439 static int
5440 next_element_from_buffer (it)
5441 struct it *it;
5442 {
5443 int success_p = 1;
5444
5445 /* Check this assumption, otherwise, we would never enter the
5446 if-statement, below. */
5447 xassert (IT_CHARPOS (*it) >= BEGV
5448 && IT_CHARPOS (*it) <= it->stop_charpos);
5449
5450 if (IT_CHARPOS (*it) >= it->stop_charpos)
5451 {
5452 if (IT_CHARPOS (*it) >= it->end_charpos)
5453 {
5454 int overlay_strings_follow_p;
5455
5456 /* End of the game, except when overlay strings follow that
5457 haven't been returned yet. */
5458 if (it->overlay_strings_at_end_processed_p)
5459 overlay_strings_follow_p = 0;
5460 else
5461 {
5462 it->overlay_strings_at_end_processed_p = 1;
5463 overlay_strings_follow_p = get_overlay_strings (it, 0);
5464 }
5465
5466 if (overlay_strings_follow_p)
5467 success_p = get_next_display_element (it);
5468 else
5469 {
5470 it->what = IT_EOB;
5471 it->position = it->current.pos;
5472 success_p = 0;
5473 }
5474 }
5475 else
5476 {
5477 handle_stop (it);
5478 return get_next_display_element (it);
5479 }
5480 }
5481 else
5482 {
5483 /* No face changes, overlays etc. in sight, so just return a
5484 character from current_buffer. */
5485 unsigned char *p;
5486
5487 /* Maybe run the redisplay end trigger hook. Performance note:
5488 This doesn't seem to cost measurable time. */
5489 if (it->redisplay_end_trigger_charpos
5490 && it->glyph_row
5491 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
5492 run_redisplay_end_trigger_hook (it);
5493
5494 /* Get the next character, maybe multibyte. */
5495 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
5496 if (it->multibyte_p && !ASCII_BYTE_P (*p))
5497 {
5498 int maxlen = ((IT_BYTEPOS (*it) >= GPT_BYTE ? ZV_BYTE : GPT_BYTE)
5499 - IT_BYTEPOS (*it));
5500 it->c = string_char_and_length (p, maxlen, &it->len);
5501 }
5502 else
5503 it->c = *p, it->len = 1;
5504
5505 /* Record what we have and where it came from. */
5506 it->what = IT_CHARACTER;;
5507 it->object = it->w->buffer;
5508 it->position = it->current.pos;
5509
5510 /* Normally we return the character found above, except when we
5511 really want to return an ellipsis for selective display. */
5512 if (it->selective)
5513 {
5514 if (it->c == '\n')
5515 {
5516 /* A value of selective > 0 means hide lines indented more
5517 than that number of columns. */
5518 if (it->selective > 0
5519 && IT_CHARPOS (*it) + 1 < ZV
5520 && indented_beyond_p (IT_CHARPOS (*it) + 1,
5521 IT_BYTEPOS (*it) + 1,
5522 (double) it->selective)) /* iftc */
5523 {
5524 success_p = next_element_from_ellipsis (it);
5525 it->dpvec_char_len = -1;
5526 }
5527 }
5528 else if (it->c == '\r' && it->selective == -1)
5529 {
5530 /* A value of selective == -1 means that everything from the
5531 CR to the end of the line is invisible, with maybe an
5532 ellipsis displayed for it. */
5533 success_p = next_element_from_ellipsis (it);
5534 it->dpvec_char_len = -1;
5535 }
5536 }
5537 }
5538
5539 /* Value is zero if end of buffer reached. */
5540 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
5541 return success_p;
5542 }
5543
5544
5545 /* Run the redisplay end trigger hook for IT. */
5546
5547 static void
5548 run_redisplay_end_trigger_hook (it)
5549 struct it *it;
5550 {
5551 Lisp_Object args[3];
5552
5553 /* IT->glyph_row should be non-null, i.e. we should be actually
5554 displaying something, or otherwise we should not run the hook. */
5555 xassert (it->glyph_row);
5556
5557 /* Set up hook arguments. */
5558 args[0] = Qredisplay_end_trigger_functions;
5559 args[1] = it->window;
5560 XSETINT (args[2], it->redisplay_end_trigger_charpos);
5561 it->redisplay_end_trigger_charpos = 0;
5562
5563 /* Since we are *trying* to run these functions, don't try to run
5564 them again, even if they get an error. */
5565 it->w->redisplay_end_trigger = Qnil;
5566 Frun_hook_with_args (3, args);
5567
5568 /* Notice if it changed the face of the character we are on. */
5569 handle_face_prop (it);
5570 }
5571
5572
5573 /* Deliver a composition display element. The iterator IT is already
5574 filled with composition information (done in
5575 handle_composition_prop). Value is always 1. */
5576
5577 static int
5578 next_element_from_composition (it)
5579 struct it *it;
5580 {
5581 it->what = IT_COMPOSITION;
5582 it->position = (STRINGP (it->string)
5583 ? it->current.string_pos
5584 : it->current.pos);
5585 return 1;
5586 }
5587
5588
5589 \f
5590 /***********************************************************************
5591 Moving an iterator without producing glyphs
5592 ***********************************************************************/
5593
5594 /* Move iterator IT to a specified buffer or X position within one
5595 line on the display without producing glyphs.
5596
5597 OP should be a bit mask including some or all of these bits:
5598 MOVE_TO_X: Stop on reaching x-position TO_X.
5599 MOVE_TO_POS: Stop on reaching buffer or string position TO_CHARPOS.
5600 Regardless of OP's value, stop in reaching the end of the display line.
5601
5602 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
5603 This means, in particular, that TO_X includes window's horizontal
5604 scroll amount.
5605
5606 The return value has several possible values that
5607 say what condition caused the scan to stop:
5608
5609 MOVE_POS_MATCH_OR_ZV
5610 - when TO_POS or ZV was reached.
5611
5612 MOVE_X_REACHED
5613 -when TO_X was reached before TO_POS or ZV were reached.
5614
5615 MOVE_LINE_CONTINUED
5616 - when we reached the end of the display area and the line must
5617 be continued.
5618
5619 MOVE_LINE_TRUNCATED
5620 - when we reached the end of the display area and the line is
5621 truncated.
5622
5623 MOVE_NEWLINE_OR_CR
5624 - when we stopped at a line end, i.e. a newline or a CR and selective
5625 display is on. */
5626
5627 static enum move_it_result
5628 move_it_in_display_line_to (it, to_charpos, to_x, op)
5629 struct it *it;
5630 int to_charpos, to_x, op;
5631 {
5632 enum move_it_result result = MOVE_UNDEFINED;
5633 struct glyph_row *saved_glyph_row;
5634
5635 /* Don't produce glyphs in produce_glyphs. */
5636 saved_glyph_row = it->glyph_row;
5637 it->glyph_row = NULL;
5638
5639 #define BUFFER_POS_REACHED_P() \
5640 ((op & MOVE_TO_POS) != 0 \
5641 && BUFFERP (it->object) \
5642 && IT_CHARPOS (*it) >= to_charpos)
5643
5644 while (1)
5645 {
5646 int x, i, ascent = 0, descent = 0;
5647
5648 /* Stop when ZV or TO_CHARPOS reached. */
5649 if (!get_next_display_element (it)
5650 || BUFFER_POS_REACHED_P ())
5651 {
5652 result = MOVE_POS_MATCH_OR_ZV;
5653 break;
5654 }
5655
5656 /* The call to produce_glyphs will get the metrics of the
5657 display element IT is loaded with. We record in x the
5658 x-position before this display element in case it does not
5659 fit on the line. */
5660 x = it->current_x;
5661
5662 /* Remember the line height so far in case the next element doesn't
5663 fit on the line. */
5664 if (!it->truncate_lines_p)
5665 {
5666 ascent = it->max_ascent;
5667 descent = it->max_descent;
5668 }
5669
5670 PRODUCE_GLYPHS (it);
5671
5672 if (it->area != TEXT_AREA)
5673 {
5674 set_iterator_to_next (it, 1);
5675 continue;
5676 }
5677
5678 /* The number of glyphs we get back in IT->nglyphs will normally
5679 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
5680 character on a terminal frame, or (iii) a line end. For the
5681 second case, IT->nglyphs - 1 padding glyphs will be present
5682 (on X frames, there is only one glyph produced for a
5683 composite character.
5684
5685 The behavior implemented below means, for continuation lines,
5686 that as many spaces of a TAB as fit on the current line are
5687 displayed there. For terminal frames, as many glyphs of a
5688 multi-glyph character are displayed in the current line, too.
5689 This is what the old redisplay code did, and we keep it that
5690 way. Under X, the whole shape of a complex character must
5691 fit on the line or it will be completely displayed in the
5692 next line.
5693
5694 Note that both for tabs and padding glyphs, all glyphs have
5695 the same width. */
5696 if (it->nglyphs)
5697 {
5698 /* More than one glyph or glyph doesn't fit on line. All
5699 glyphs have the same width. */
5700 int single_glyph_width = it->pixel_width / it->nglyphs;
5701 int new_x;
5702
5703 for (i = 0; i < it->nglyphs; ++i, x = new_x)
5704 {
5705 new_x = x + single_glyph_width;
5706
5707 /* We want to leave anything reaching TO_X to the caller. */
5708 if ((op & MOVE_TO_X) && new_x > to_x)
5709 {
5710 it->current_x = x;
5711 result = MOVE_X_REACHED;
5712 break;
5713 }
5714 else if (/* Lines are continued. */
5715 !it->truncate_lines_p
5716 && (/* And glyph doesn't fit on the line. */
5717 new_x > it->last_visible_x
5718 /* Or it fits exactly and we're on a window
5719 system frame. */
5720 || (new_x == it->last_visible_x
5721 && FRAME_WINDOW_P (it->f))))
5722 {
5723 if (/* IT->hpos == 0 means the very first glyph
5724 doesn't fit on the line, e.g. a wide image. */
5725 it->hpos == 0
5726 || (new_x == it->last_visible_x
5727 && FRAME_WINDOW_P (it->f)))
5728 {
5729 ++it->hpos;
5730 it->current_x = new_x;
5731 if (i == it->nglyphs - 1)
5732 {
5733 set_iterator_to_next (it, 1);
5734 #ifdef HAVE_WINDOW_SYSTEM
5735 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
5736 {
5737 if (!get_next_display_element (it)
5738 || BUFFER_POS_REACHED_P ())
5739 {
5740 result = MOVE_POS_MATCH_OR_ZV;
5741 break;
5742 }
5743 if (ITERATOR_AT_END_OF_LINE_P (it))
5744 {
5745 result = MOVE_NEWLINE_OR_CR;
5746 break;
5747 }
5748 }
5749 #endif /* HAVE_WINDOW_SYSTEM */
5750 }
5751 }
5752 else
5753 {
5754 it->current_x = x;
5755 it->max_ascent = ascent;
5756 it->max_descent = descent;
5757 }
5758
5759 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
5760 IT_CHARPOS (*it)));
5761 result = MOVE_LINE_CONTINUED;
5762 break;
5763 }
5764 else if (new_x > it->first_visible_x)
5765 {
5766 /* Glyph is visible. Increment number of glyphs that
5767 would be displayed. */
5768 ++it->hpos;
5769 }
5770 else
5771 {
5772 /* Glyph is completely off the left margin of the display
5773 area. Nothing to do. */
5774 }
5775 }
5776
5777 if (result != MOVE_UNDEFINED)
5778 break;
5779 }
5780 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
5781 {
5782 /* Stop when TO_X specified and reached. This check is
5783 necessary here because of lines consisting of a line end,
5784 only. The line end will not produce any glyphs and we
5785 would never get MOVE_X_REACHED. */
5786 xassert (it->nglyphs == 0);
5787 result = MOVE_X_REACHED;
5788 break;
5789 }
5790
5791 /* Is this a line end? If yes, we're done. */
5792 if (ITERATOR_AT_END_OF_LINE_P (it))
5793 {
5794 result = MOVE_NEWLINE_OR_CR;
5795 break;
5796 }
5797
5798 /* The current display element has been consumed. Advance
5799 to the next. */
5800 set_iterator_to_next (it, 1);
5801
5802 /* Stop if lines are truncated and IT's current x-position is
5803 past the right edge of the window now. */
5804 if (it->truncate_lines_p
5805 && it->current_x >= it->last_visible_x)
5806 {
5807 #ifdef HAVE_WINDOW_SYSTEM
5808 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
5809 {
5810 if (!get_next_display_element (it)
5811 || BUFFER_POS_REACHED_P ())
5812 {
5813 result = MOVE_POS_MATCH_OR_ZV;
5814 break;
5815 }
5816 if (ITERATOR_AT_END_OF_LINE_P (it))
5817 {
5818 result = MOVE_NEWLINE_OR_CR;
5819 break;
5820 }
5821 }
5822 #endif /* HAVE_WINDOW_SYSTEM */
5823 result = MOVE_LINE_TRUNCATED;
5824 break;
5825 }
5826 }
5827
5828 #undef BUFFER_POS_REACHED_P
5829
5830 /* Restore the iterator settings altered at the beginning of this
5831 function. */
5832 it->glyph_row = saved_glyph_row;
5833 return result;
5834 }
5835
5836
5837 /* Move IT forward until it satisfies one or more of the criteria in
5838 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
5839
5840 OP is a bit-mask that specifies where to stop, and in particular,
5841 which of those four position arguments makes a difference. See the
5842 description of enum move_operation_enum.
5843
5844 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
5845 screen line, this function will set IT to the next position >
5846 TO_CHARPOS. */
5847
5848 void
5849 move_it_to (it, to_charpos, to_x, to_y, to_vpos, op)
5850 struct it *it;
5851 int to_charpos, to_x, to_y, to_vpos;
5852 int op;
5853 {
5854 enum move_it_result skip, skip2 = MOVE_X_REACHED;
5855 int line_height;
5856 int reached = 0;
5857
5858 for (;;)
5859 {
5860 if (op & MOVE_TO_VPOS)
5861 {
5862 /* If no TO_CHARPOS and no TO_X specified, stop at the
5863 start of the line TO_VPOS. */
5864 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
5865 {
5866 if (it->vpos == to_vpos)
5867 {
5868 reached = 1;
5869 break;
5870 }
5871 else
5872 skip = move_it_in_display_line_to (it, -1, -1, 0);
5873 }
5874 else
5875 {
5876 /* TO_VPOS >= 0 means stop at TO_X in the line at
5877 TO_VPOS, or at TO_POS, whichever comes first. */
5878 if (it->vpos == to_vpos)
5879 {
5880 reached = 2;
5881 break;
5882 }
5883
5884 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
5885
5886 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
5887 {
5888 reached = 3;
5889 break;
5890 }
5891 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
5892 {
5893 /* We have reached TO_X but not in the line we want. */
5894 skip = move_it_in_display_line_to (it, to_charpos,
5895 -1, MOVE_TO_POS);
5896 if (skip == MOVE_POS_MATCH_OR_ZV)
5897 {
5898 reached = 4;
5899 break;
5900 }
5901 }
5902 }
5903 }
5904 else if (op & MOVE_TO_Y)
5905 {
5906 struct it it_backup;
5907
5908 /* TO_Y specified means stop at TO_X in the line containing
5909 TO_Y---or at TO_CHARPOS if this is reached first. The
5910 problem is that we can't really tell whether the line
5911 contains TO_Y before we have completely scanned it, and
5912 this may skip past TO_X. What we do is to first scan to
5913 TO_X.
5914
5915 If TO_X is not specified, use a TO_X of zero. The reason
5916 is to make the outcome of this function more predictable.
5917 If we didn't use TO_X == 0, we would stop at the end of
5918 the line which is probably not what a caller would expect
5919 to happen. */
5920 skip = move_it_in_display_line_to (it, to_charpos,
5921 ((op & MOVE_TO_X)
5922 ? to_x : 0),
5923 (MOVE_TO_X
5924 | (op & MOVE_TO_POS)));
5925
5926 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
5927 if (skip == MOVE_POS_MATCH_OR_ZV)
5928 {
5929 reached = 5;
5930 break;
5931 }
5932
5933 /* If TO_X was reached, we would like to know whether TO_Y
5934 is in the line. This can only be said if we know the
5935 total line height which requires us to scan the rest of
5936 the line. */
5937 if (skip == MOVE_X_REACHED)
5938 {
5939 it_backup = *it;
5940 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
5941 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
5942 op & MOVE_TO_POS);
5943 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
5944 }
5945
5946 /* Now, decide whether TO_Y is in this line. */
5947 line_height = it->max_ascent + it->max_descent;
5948 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
5949
5950 if (to_y >= it->current_y
5951 && to_y < it->current_y + line_height)
5952 {
5953 if (skip == MOVE_X_REACHED)
5954 /* If TO_Y is in this line and TO_X was reached above,
5955 we scanned too far. We have to restore IT's settings
5956 to the ones before skipping. */
5957 *it = it_backup;
5958 reached = 6;
5959 }
5960 else if (skip == MOVE_X_REACHED)
5961 {
5962 skip = skip2;
5963 if (skip == MOVE_POS_MATCH_OR_ZV)
5964 reached = 7;
5965 }
5966
5967 if (reached)
5968 break;
5969 }
5970 else
5971 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
5972
5973 switch (skip)
5974 {
5975 case MOVE_POS_MATCH_OR_ZV:
5976 reached = 8;
5977 goto out;
5978
5979 case MOVE_NEWLINE_OR_CR:
5980 set_iterator_to_next (it, 1);
5981 it->continuation_lines_width = 0;
5982 break;
5983
5984 case MOVE_LINE_TRUNCATED:
5985 it->continuation_lines_width = 0;
5986 reseat_at_next_visible_line_start (it, 0);
5987 if ((op & MOVE_TO_POS) != 0
5988 && IT_CHARPOS (*it) > to_charpos)
5989 {
5990 reached = 9;
5991 goto out;
5992 }
5993 break;
5994
5995 case MOVE_LINE_CONTINUED:
5996 it->continuation_lines_width += it->current_x;
5997 break;
5998
5999 default:
6000 abort ();
6001 }
6002
6003 /* Reset/increment for the next run. */
6004 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
6005 it->current_x = it->hpos = 0;
6006 it->current_y += it->max_ascent + it->max_descent;
6007 ++it->vpos;
6008 last_height = it->max_ascent + it->max_descent;
6009 last_max_ascent = it->max_ascent;
6010 it->max_ascent = it->max_descent = 0;
6011 }
6012
6013 out:
6014
6015 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
6016 }
6017
6018
6019 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
6020
6021 If DY > 0, move IT backward at least that many pixels. DY = 0
6022 means move IT backward to the preceding line start or BEGV. This
6023 function may move over more than DY pixels if IT->current_y - DY
6024 ends up in the middle of a line; in this case IT->current_y will be
6025 set to the top of the line moved to. */
6026
6027 void
6028 move_it_vertically_backward (it, dy)
6029 struct it *it;
6030 int dy;
6031 {
6032 int nlines, h;
6033 struct it it2, it3;
6034 int start_pos = IT_CHARPOS (*it);
6035
6036 xassert (dy >= 0);
6037
6038 /* Estimate how many newlines we must move back. */
6039 nlines = max (1, dy / FRAME_LINE_HEIGHT (it->f));
6040
6041 /* Set the iterator's position that many lines back. */
6042 while (nlines-- && IT_CHARPOS (*it) > BEGV)
6043 back_to_previous_visible_line_start (it);
6044
6045 /* Reseat the iterator here. When moving backward, we don't want
6046 reseat to skip forward over invisible text, set up the iterator
6047 to deliver from overlay strings at the new position etc. So,
6048 use reseat_1 here. */
6049 reseat_1 (it, it->current.pos, 1);
6050
6051 /* We are now surely at a line start. */
6052 it->current_x = it->hpos = 0;
6053 it->continuation_lines_width = 0;
6054
6055 /* Move forward and see what y-distance we moved. First move to the
6056 start of the next line so that we get its height. We need this
6057 height to be able to tell whether we reached the specified
6058 y-distance. */
6059 it2 = *it;
6060 it2.max_ascent = it2.max_descent = 0;
6061 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
6062 MOVE_TO_POS | MOVE_TO_VPOS);
6063 xassert (IT_CHARPOS (*it) >= BEGV);
6064 it3 = it2;
6065
6066 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
6067 xassert (IT_CHARPOS (*it) >= BEGV);
6068 /* H is the actual vertical distance from the position in *IT
6069 and the starting position. */
6070 h = it2.current_y - it->current_y;
6071 /* NLINES is the distance in number of lines. */
6072 nlines = it2.vpos - it->vpos;
6073
6074 /* Correct IT's y and vpos position
6075 so that they are relative to the starting point. */
6076 it->vpos -= nlines;
6077 it->current_y -= h;
6078
6079 if (dy == 0)
6080 {
6081 /* DY == 0 means move to the start of the screen line. The
6082 value of nlines is > 0 if continuation lines were involved. */
6083 if (nlines > 0)
6084 move_it_by_lines (it, nlines, 1);
6085 xassert (IT_CHARPOS (*it) <= start_pos);
6086 }
6087 else
6088 {
6089 /* The y-position we try to reach, relative to *IT.
6090 Note that H has been subtracted in front of the if-statement. */
6091 int target_y = it->current_y + h - dy;
6092 int y0 = it3.current_y;
6093 int y1 = line_bottom_y (&it3);
6094 int line_height = y1 - y0;
6095
6096 /* If we did not reach target_y, try to move further backward if
6097 we can. If we moved too far backward, try to move forward. */
6098 if (target_y < it->current_y
6099 /* This is heuristic. In a window that's 3 lines high, with
6100 a line height of 13 pixels each, recentering with point
6101 on the bottom line will try to move -39/2 = 19 pixels
6102 backward. Try to avoid moving into the first line. */
6103 && it->current_y - target_y > line_height / 3 * 2
6104 && IT_CHARPOS (*it) > BEGV)
6105 {
6106 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
6107 target_y - it->current_y));
6108 move_it_vertically (it, target_y - it->current_y);
6109 xassert (IT_CHARPOS (*it) >= BEGV);
6110 }
6111 else if (target_y >= it->current_y + line_height
6112 && IT_CHARPOS (*it) < ZV)
6113 {
6114 /* Should move forward by at least one line, maybe more.
6115
6116 Note: Calling move_it_by_lines can be expensive on
6117 terminal frames, where compute_motion is used (via
6118 vmotion) to do the job, when there are very long lines
6119 and truncate-lines is nil. That's the reason for
6120 treating terminal frames specially here. */
6121
6122 if (!FRAME_WINDOW_P (it->f))
6123 move_it_vertically (it, target_y - (it->current_y + line_height));
6124 else
6125 {
6126 do
6127 {
6128 move_it_by_lines (it, 1, 1);
6129 }
6130 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
6131 }
6132
6133 xassert (IT_CHARPOS (*it) >= BEGV);
6134 }
6135 }
6136 }
6137
6138
6139 /* Move IT by a specified amount of pixel lines DY. DY negative means
6140 move backwards. DY = 0 means move to start of screen line. At the
6141 end, IT will be on the start of a screen line. */
6142
6143 void
6144 move_it_vertically (it, dy)
6145 struct it *it;
6146 int dy;
6147 {
6148 if (dy <= 0)
6149 move_it_vertically_backward (it, -dy);
6150 else if (dy > 0)
6151 {
6152 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
6153 move_it_to (it, ZV, -1, it->current_y + dy, -1,
6154 MOVE_TO_POS | MOVE_TO_Y);
6155 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
6156
6157 /* If buffer ends in ZV without a newline, move to the start of
6158 the line to satisfy the post-condition. */
6159 if (IT_CHARPOS (*it) == ZV
6160 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
6161 move_it_by_lines (it, 0, 0);
6162 }
6163 }
6164
6165
6166 /* Move iterator IT past the end of the text line it is in. */
6167
6168 void
6169 move_it_past_eol (it)
6170 struct it *it;
6171 {
6172 enum move_it_result rc;
6173
6174 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
6175 if (rc == MOVE_NEWLINE_OR_CR)
6176 set_iterator_to_next (it, 0);
6177 }
6178
6179
6180 #if 0 /* Currently not used. */
6181
6182 /* Return non-zero if some text between buffer positions START_CHARPOS
6183 and END_CHARPOS is invisible. IT->window is the window for text
6184 property lookup. */
6185
6186 static int
6187 invisible_text_between_p (it, start_charpos, end_charpos)
6188 struct it *it;
6189 int start_charpos, end_charpos;
6190 {
6191 Lisp_Object prop, limit;
6192 int invisible_found_p;
6193
6194 xassert (it != NULL && start_charpos <= end_charpos);
6195
6196 /* Is text at START invisible? */
6197 prop = Fget_char_property (make_number (start_charpos), Qinvisible,
6198 it->window);
6199 if (TEXT_PROP_MEANS_INVISIBLE (prop))
6200 invisible_found_p = 1;
6201 else
6202 {
6203 limit = Fnext_single_char_property_change (make_number (start_charpos),
6204 Qinvisible, Qnil,
6205 make_number (end_charpos));
6206 invisible_found_p = XFASTINT (limit) < end_charpos;
6207 }
6208
6209 return invisible_found_p;
6210 }
6211
6212 #endif /* 0 */
6213
6214
6215 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
6216 negative means move up. DVPOS == 0 means move to the start of the
6217 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
6218 NEED_Y_P is zero, IT->current_y will be left unchanged.
6219
6220 Further optimization ideas: If we would know that IT->f doesn't use
6221 a face with proportional font, we could be faster for
6222 truncate-lines nil. */
6223
6224 void
6225 move_it_by_lines (it, dvpos, need_y_p)
6226 struct it *it;
6227 int dvpos, need_y_p;
6228 {
6229 struct position pos;
6230
6231 if (!FRAME_WINDOW_P (it->f))
6232 {
6233 struct text_pos textpos;
6234
6235 /* We can use vmotion on frames without proportional fonts. */
6236 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
6237 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
6238 reseat (it, textpos, 1);
6239 it->vpos += pos.vpos;
6240 it->current_y += pos.vpos;
6241 }
6242 else if (dvpos == 0)
6243 {
6244 /* DVPOS == 0 means move to the start of the screen line. */
6245 move_it_vertically_backward (it, 0);
6246 xassert (it->current_x == 0 && it->hpos == 0);
6247 }
6248 else if (dvpos > 0)
6249 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
6250 else
6251 {
6252 struct it it2;
6253 int start_charpos, i;
6254
6255 /* Start at the beginning of the screen line containing IT's
6256 position. */
6257 move_it_vertically_backward (it, 0);
6258
6259 /* Go back -DVPOS visible lines and reseat the iterator there. */
6260 start_charpos = IT_CHARPOS (*it);
6261 for (i = -dvpos; i && IT_CHARPOS (*it) > BEGV; --i)
6262 back_to_previous_visible_line_start (it);
6263 reseat (it, it->current.pos, 1);
6264 it->current_x = it->hpos = 0;
6265
6266 /* Above call may have moved too far if continuation lines
6267 are involved. Scan forward and see if it did. */
6268 it2 = *it;
6269 it2.vpos = it2.current_y = 0;
6270 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
6271 it->vpos -= it2.vpos;
6272 it->current_y -= it2.current_y;
6273 it->current_x = it->hpos = 0;
6274
6275 /* If we moved too far, move IT some lines forward. */
6276 if (it2.vpos > -dvpos)
6277 {
6278 int delta = it2.vpos + dvpos;
6279 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
6280 }
6281 }
6282 }
6283
6284 /* Return 1 if IT points into the middle of a display vector. */
6285
6286 int
6287 in_display_vector_p (it)
6288 struct it *it;
6289 {
6290 return (it->method == next_element_from_display_vector
6291 && it->current.dpvec_index > 0
6292 && it->dpvec + it->current.dpvec_index != it->dpend);
6293 }
6294
6295 \f
6296 /***********************************************************************
6297 Messages
6298 ***********************************************************************/
6299
6300
6301 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
6302 to *Messages*. */
6303
6304 void
6305 add_to_log (format, arg1, arg2)
6306 char *format;
6307 Lisp_Object arg1, arg2;
6308 {
6309 Lisp_Object args[3];
6310 Lisp_Object msg, fmt;
6311 char *buffer;
6312 int len;
6313 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
6314 USE_SAFE_ALLOCA;
6315
6316 /* Do nothing if called asynchronously. Inserting text into
6317 a buffer may call after-change-functions and alike and
6318 that would means running Lisp asynchronously. */
6319 if (handling_signal)
6320 return;
6321
6322 fmt = msg = Qnil;
6323 GCPRO4 (fmt, msg, arg1, arg2);
6324
6325 args[0] = fmt = build_string (format);
6326 args[1] = arg1;
6327 args[2] = arg2;
6328 msg = Fformat (3, args);
6329
6330 len = SBYTES (msg) + 1;
6331 SAFE_ALLOCA (buffer, char *, len);
6332 bcopy (SDATA (msg), buffer, len);
6333
6334 message_dolog (buffer, len - 1, 1, 0);
6335 SAFE_FREE (len);
6336
6337 UNGCPRO;
6338 }
6339
6340
6341 /* Output a newline in the *Messages* buffer if "needs" one. */
6342
6343 void
6344 message_log_maybe_newline ()
6345 {
6346 if (message_log_need_newline)
6347 message_dolog ("", 0, 1, 0);
6348 }
6349
6350
6351 /* Add a string M of length NBYTES to the message log, optionally
6352 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
6353 nonzero, means interpret the contents of M as multibyte. This
6354 function calls low-level routines in order to bypass text property
6355 hooks, etc. which might not be safe to run. */
6356
6357 void
6358 message_dolog (m, nbytes, nlflag, multibyte)
6359 const char *m;
6360 int nbytes, nlflag, multibyte;
6361 {
6362 if (!NILP (Vmemory_full))
6363 return;
6364
6365 if (!NILP (Vmessage_log_max))
6366 {
6367 struct buffer *oldbuf;
6368 Lisp_Object oldpoint, oldbegv, oldzv;
6369 int old_windows_or_buffers_changed = windows_or_buffers_changed;
6370 int point_at_end = 0;
6371 int zv_at_end = 0;
6372 Lisp_Object old_deactivate_mark, tem;
6373 struct gcpro gcpro1;
6374
6375 old_deactivate_mark = Vdeactivate_mark;
6376 oldbuf = current_buffer;
6377 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
6378 current_buffer->undo_list = Qt;
6379
6380 oldpoint = message_dolog_marker1;
6381 set_marker_restricted (oldpoint, make_number (PT), Qnil);
6382 oldbegv = message_dolog_marker2;
6383 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
6384 oldzv = message_dolog_marker3;
6385 set_marker_restricted (oldzv, make_number (ZV), Qnil);
6386 GCPRO1 (old_deactivate_mark);
6387
6388 if (PT == Z)
6389 point_at_end = 1;
6390 if (ZV == Z)
6391 zv_at_end = 1;
6392
6393 BEGV = BEG;
6394 BEGV_BYTE = BEG_BYTE;
6395 ZV = Z;
6396 ZV_BYTE = Z_BYTE;
6397 TEMP_SET_PT_BOTH (Z, Z_BYTE);
6398
6399 /* Insert the string--maybe converting multibyte to single byte
6400 or vice versa, so that all the text fits the buffer. */
6401 if (multibyte
6402 && NILP (current_buffer->enable_multibyte_characters))
6403 {
6404 int i, c, char_bytes;
6405 unsigned char work[1];
6406
6407 /* Convert a multibyte string to single-byte
6408 for the *Message* buffer. */
6409 for (i = 0; i < nbytes; i += char_bytes)
6410 {
6411 c = string_char_and_length (m + i, nbytes - i, &char_bytes);
6412 work[0] = (SINGLE_BYTE_CHAR_P (c)
6413 ? c
6414 : multibyte_char_to_unibyte (c, Qnil));
6415 insert_1_both (work, 1, 1, 1, 0, 0);
6416 }
6417 }
6418 else if (! multibyte
6419 && ! NILP (current_buffer->enable_multibyte_characters))
6420 {
6421 int i, c, char_bytes;
6422 unsigned char *msg = (unsigned char *) m;
6423 unsigned char str[MAX_MULTIBYTE_LENGTH];
6424 /* Convert a single-byte string to multibyte
6425 for the *Message* buffer. */
6426 for (i = 0; i < nbytes; i++)
6427 {
6428 c = unibyte_char_to_multibyte (msg[i]);
6429 char_bytes = CHAR_STRING (c, str);
6430 insert_1_both (str, 1, char_bytes, 1, 0, 0);
6431 }
6432 }
6433 else if (nbytes)
6434 insert_1 (m, nbytes, 1, 0, 0);
6435
6436 if (nlflag)
6437 {
6438 int this_bol, this_bol_byte, prev_bol, prev_bol_byte, dup;
6439 insert_1 ("\n", 1, 1, 0, 0);
6440
6441 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
6442 this_bol = PT;
6443 this_bol_byte = PT_BYTE;
6444
6445 /* See if this line duplicates the previous one.
6446 If so, combine duplicates. */
6447 if (this_bol > BEG)
6448 {
6449 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
6450 prev_bol = PT;
6451 prev_bol_byte = PT_BYTE;
6452
6453 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
6454 this_bol, this_bol_byte);
6455 if (dup)
6456 {
6457 del_range_both (prev_bol, prev_bol_byte,
6458 this_bol, this_bol_byte, 0);
6459 if (dup > 1)
6460 {
6461 char dupstr[40];
6462 int duplen;
6463
6464 /* If you change this format, don't forget to also
6465 change message_log_check_duplicate. */
6466 sprintf (dupstr, " [%d times]", dup);
6467 duplen = strlen (dupstr);
6468 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
6469 insert_1 (dupstr, duplen, 1, 0, 1);
6470 }
6471 }
6472 }
6473
6474 /* If we have more than the desired maximum number of lines
6475 in the *Messages* buffer now, delete the oldest ones.
6476 This is safe because we don't have undo in this buffer. */
6477
6478 if (NATNUMP (Vmessage_log_max))
6479 {
6480 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
6481 -XFASTINT (Vmessage_log_max) - 1, 0);
6482 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
6483 }
6484 }
6485 BEGV = XMARKER (oldbegv)->charpos;
6486 BEGV_BYTE = marker_byte_position (oldbegv);
6487
6488 if (zv_at_end)
6489 {
6490 ZV = Z;
6491 ZV_BYTE = Z_BYTE;
6492 }
6493 else
6494 {
6495 ZV = XMARKER (oldzv)->charpos;
6496 ZV_BYTE = marker_byte_position (oldzv);
6497 }
6498
6499 if (point_at_end)
6500 TEMP_SET_PT_BOTH (Z, Z_BYTE);
6501 else
6502 /* We can't do Fgoto_char (oldpoint) because it will run some
6503 Lisp code. */
6504 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
6505 XMARKER (oldpoint)->bytepos);
6506
6507 UNGCPRO;
6508 unchain_marker (XMARKER (oldpoint));
6509 unchain_marker (XMARKER (oldbegv));
6510 unchain_marker (XMARKER (oldzv));
6511
6512 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
6513 set_buffer_internal (oldbuf);
6514 if (NILP (tem))
6515 windows_or_buffers_changed = old_windows_or_buffers_changed;
6516 message_log_need_newline = !nlflag;
6517 Vdeactivate_mark = old_deactivate_mark;
6518 }
6519 }
6520
6521
6522 /* We are at the end of the buffer after just having inserted a newline.
6523 (Note: We depend on the fact we won't be crossing the gap.)
6524 Check to see if the most recent message looks a lot like the previous one.
6525 Return 0 if different, 1 if the new one should just replace it, or a
6526 value N > 1 if we should also append " [N times]". */
6527
6528 static int
6529 message_log_check_duplicate (prev_bol, prev_bol_byte, this_bol, this_bol_byte)
6530 int prev_bol, this_bol;
6531 int prev_bol_byte, this_bol_byte;
6532 {
6533 int i;
6534 int len = Z_BYTE - 1 - this_bol_byte;
6535 int seen_dots = 0;
6536 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
6537 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
6538
6539 for (i = 0; i < len; i++)
6540 {
6541 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
6542 seen_dots = 1;
6543 if (p1[i] != p2[i])
6544 return seen_dots;
6545 }
6546 p1 += len;
6547 if (*p1 == '\n')
6548 return 2;
6549 if (*p1++ == ' ' && *p1++ == '[')
6550 {
6551 int n = 0;
6552 while (*p1 >= '0' && *p1 <= '9')
6553 n = n * 10 + *p1++ - '0';
6554 if (strncmp (p1, " times]\n", 8) == 0)
6555 return n+1;
6556 }
6557 return 0;
6558 }
6559
6560
6561 /* Display an echo area message M with a specified length of NBYTES
6562 bytes. The string may include null characters. If M is 0, clear
6563 out any existing message, and let the mini-buffer text show
6564 through.
6565
6566 The buffer M must continue to exist until after the echo area gets
6567 cleared or some other message gets displayed there. This means do
6568 not pass text that is stored in a Lisp string; do not pass text in
6569 a buffer that was alloca'd. */
6570
6571 void
6572 message2 (m, nbytes, multibyte)
6573 const char *m;
6574 int nbytes;
6575 int multibyte;
6576 {
6577 /* First flush out any partial line written with print. */
6578 message_log_maybe_newline ();
6579 if (m)
6580 message_dolog (m, nbytes, 1, multibyte);
6581 message2_nolog (m, nbytes, multibyte);
6582 }
6583
6584
6585 /* The non-logging counterpart of message2. */
6586
6587 void
6588 message2_nolog (m, nbytes, multibyte)
6589 const char *m;
6590 int nbytes, multibyte;
6591 {
6592 struct frame *sf = SELECTED_FRAME ();
6593 message_enable_multibyte = multibyte;
6594
6595 if (noninteractive)
6596 {
6597 if (noninteractive_need_newline)
6598 putc ('\n', stderr);
6599 noninteractive_need_newline = 0;
6600 if (m)
6601 fwrite (m, nbytes, 1, stderr);
6602 if (cursor_in_echo_area == 0)
6603 fprintf (stderr, "\n");
6604 fflush (stderr);
6605 }
6606 /* A null message buffer means that the frame hasn't really been
6607 initialized yet. Error messages get reported properly by
6608 cmd_error, so this must be just an informative message; toss it. */
6609 else if (INTERACTIVE
6610 && sf->glyphs_initialized_p
6611 && FRAME_MESSAGE_BUF (sf))
6612 {
6613 Lisp_Object mini_window;
6614 struct frame *f;
6615
6616 /* Get the frame containing the mini-buffer
6617 that the selected frame is using. */
6618 mini_window = FRAME_MINIBUF_WINDOW (sf);
6619 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
6620
6621 FRAME_SAMPLE_VISIBILITY (f);
6622 if (FRAME_VISIBLE_P (sf)
6623 && ! FRAME_VISIBLE_P (f))
6624 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
6625
6626 if (m)
6627 {
6628 set_message (m, Qnil, nbytes, multibyte);
6629 if (minibuffer_auto_raise)
6630 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
6631 }
6632 else
6633 clear_message (1, 1);
6634
6635 do_pending_window_change (0);
6636 echo_area_display (1);
6637 do_pending_window_change (0);
6638 if (FRAME_DISPLAY (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
6639 (*FRAME_DISPLAY (f)->frame_up_to_date_hook) (f);
6640 }
6641 }
6642
6643
6644 /* Display an echo area message M with a specified length of NBYTES
6645 bytes. The string may include null characters. If M is not a
6646 string, clear out any existing message, and let the mini-buffer
6647 text show through. */
6648
6649 void
6650 message3 (m, nbytes, multibyte)
6651 Lisp_Object m;
6652 int nbytes;
6653 int multibyte;
6654 {
6655 struct gcpro gcpro1;
6656
6657 GCPRO1 (m);
6658
6659 /* First flush out any partial line written with print. */
6660 message_log_maybe_newline ();
6661 if (STRINGP (m))
6662 message_dolog (SDATA (m), nbytes, 1, multibyte);
6663 message3_nolog (m, nbytes, multibyte);
6664
6665 UNGCPRO;
6666 }
6667
6668
6669 /* The non-logging version of message3. */
6670
6671 void
6672 message3_nolog (m, nbytes, multibyte)
6673 Lisp_Object m;
6674 int nbytes, multibyte;
6675 {
6676 struct frame *sf = SELECTED_FRAME ();
6677 message_enable_multibyte = multibyte;
6678
6679 if (noninteractive)
6680 {
6681 if (noninteractive_need_newline)
6682 putc ('\n', stderr);
6683 noninteractive_need_newline = 0;
6684 if (STRINGP (m))
6685 fwrite (SDATA (m), nbytes, 1, stderr);
6686 if (cursor_in_echo_area == 0)
6687 fprintf (stderr, "\n");
6688 fflush (stderr);
6689 }
6690 /* A null message buffer means that the frame hasn't really been
6691 initialized yet. Error messages get reported properly by
6692 cmd_error, so this must be just an informative message; toss it. */
6693 else if (INTERACTIVE
6694 && sf->glyphs_initialized_p
6695 && FRAME_MESSAGE_BUF (sf))
6696 {
6697 Lisp_Object mini_window;
6698 Lisp_Object frame;
6699 struct frame *f;
6700
6701 /* Get the frame containing the mini-buffer
6702 that the selected frame is using. */
6703 mini_window = FRAME_MINIBUF_WINDOW (sf);
6704 frame = XWINDOW (mini_window)->frame;
6705 f = XFRAME (frame);
6706
6707 FRAME_SAMPLE_VISIBILITY (f);
6708 if (FRAME_VISIBLE_P (sf)
6709 && !FRAME_VISIBLE_P (f))
6710 Fmake_frame_visible (frame);
6711
6712 if (STRINGP (m) && SCHARS (m) > 0)
6713 {
6714 set_message (NULL, m, nbytes, multibyte);
6715 if (minibuffer_auto_raise)
6716 Fraise_frame (frame);
6717 }
6718 else
6719 clear_message (1, 1);
6720
6721 do_pending_window_change (0);
6722 echo_area_display (1);
6723 do_pending_window_change (0);
6724 if (FRAME_DISPLAY (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
6725 (*FRAME_DISPLAY (f)->frame_up_to_date_hook) (f);
6726 }
6727 }
6728
6729
6730 /* Display a null-terminated echo area message M. If M is 0, clear
6731 out any existing message, and let the mini-buffer text show through.
6732
6733 The buffer M must continue to exist until after the echo area gets
6734 cleared or some other message gets displayed there. Do not pass
6735 text that is stored in a Lisp string. Do not pass text in a buffer
6736 that was alloca'd. */
6737
6738 void
6739 message1 (m)
6740 char *m;
6741 {
6742 message2 (m, (m ? strlen (m) : 0), 0);
6743 }
6744
6745
6746 /* The non-logging counterpart of message1. */
6747
6748 void
6749 message1_nolog (m)
6750 char *m;
6751 {
6752 message2_nolog (m, (m ? strlen (m) : 0), 0);
6753 }
6754
6755 /* Display a message M which contains a single %s
6756 which gets replaced with STRING. */
6757
6758 void
6759 message_with_string (m, string, log)
6760 char *m;
6761 Lisp_Object string;
6762 int log;
6763 {
6764 CHECK_STRING (string);
6765
6766 if (noninteractive)
6767 {
6768 if (m)
6769 {
6770 if (noninteractive_need_newline)
6771 putc ('\n', stderr);
6772 noninteractive_need_newline = 0;
6773 fprintf (stderr, m, SDATA (string));
6774 if (cursor_in_echo_area == 0)
6775 fprintf (stderr, "\n");
6776 fflush (stderr);
6777 }
6778 }
6779 else if (INTERACTIVE)
6780 {
6781 /* The frame whose minibuffer we're going to display the message on.
6782 It may be larger than the selected frame, so we need
6783 to use its buffer, not the selected frame's buffer. */
6784 Lisp_Object mini_window;
6785 struct frame *f, *sf = SELECTED_FRAME ();
6786
6787 /* Get the frame containing the minibuffer
6788 that the selected frame is using. */
6789 mini_window = FRAME_MINIBUF_WINDOW (sf);
6790 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
6791
6792 /* A null message buffer means that the frame hasn't really been
6793 initialized yet. Error messages get reported properly by
6794 cmd_error, so this must be just an informative message; toss it. */
6795 if (FRAME_MESSAGE_BUF (f))
6796 {
6797 Lisp_Object args[2], message;
6798 struct gcpro gcpro1, gcpro2;
6799
6800 args[0] = build_string (m);
6801 args[1] = message = string;
6802 GCPRO2 (args[0], message);
6803 gcpro1.nvars = 2;
6804
6805 message = Fformat (2, args);
6806
6807 if (log)
6808 message3 (message, SBYTES (message), STRING_MULTIBYTE (message));
6809 else
6810 message3_nolog (message, SBYTES (message), STRING_MULTIBYTE (message));
6811
6812 UNGCPRO;
6813
6814 /* Print should start at the beginning of the message
6815 buffer next time. */
6816 message_buf_print = 0;
6817 }
6818 }
6819 }
6820
6821
6822 /* Dump an informative message to the minibuf. If M is 0, clear out
6823 any existing message, and let the mini-buffer text show through. */
6824
6825 /* VARARGS 1 */
6826 void
6827 message (m, a1, a2, a3)
6828 char *m;
6829 EMACS_INT a1, a2, a3;
6830 {
6831 if (noninteractive)
6832 {
6833 if (m)
6834 {
6835 if (noninteractive_need_newline)
6836 putc ('\n', stderr);
6837 noninteractive_need_newline = 0;
6838 fprintf (stderr, m, a1, a2, a3);
6839 if (cursor_in_echo_area == 0)
6840 fprintf (stderr, "\n");
6841 fflush (stderr);
6842 }
6843 }
6844 else if (INTERACTIVE)
6845 {
6846 /* The frame whose mini-buffer we're going to display the message
6847 on. It may be larger than the selected frame, so we need to
6848 use its buffer, not the selected frame's buffer. */
6849 Lisp_Object mini_window;
6850 struct frame *f, *sf = SELECTED_FRAME ();
6851
6852 /* Get the frame containing the mini-buffer
6853 that the selected frame is using. */
6854 mini_window = FRAME_MINIBUF_WINDOW (sf);
6855 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
6856
6857 /* A null message buffer means that the frame hasn't really been
6858 initialized yet. Error messages get reported properly by
6859 cmd_error, so this must be just an informative message; toss
6860 it. */
6861 if (FRAME_MESSAGE_BUF (f))
6862 {
6863 if (m)
6864 {
6865 int len;
6866 #ifdef NO_ARG_ARRAY
6867 char *a[3];
6868 a[0] = (char *) a1;
6869 a[1] = (char *) a2;
6870 a[2] = (char *) a3;
6871
6872 len = doprnt (FRAME_MESSAGE_BUF (f),
6873 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
6874 #else
6875 len = doprnt (FRAME_MESSAGE_BUF (f),
6876 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3,
6877 (char **) &a1);
6878 #endif /* NO_ARG_ARRAY */
6879
6880 message2 (FRAME_MESSAGE_BUF (f), len, 0);
6881 }
6882 else
6883 message1 (0);
6884
6885 /* Print should start at the beginning of the message
6886 buffer next time. */
6887 message_buf_print = 0;
6888 }
6889 }
6890 }
6891
6892
6893 /* The non-logging version of message. */
6894
6895 void
6896 message_nolog (m, a1, a2, a3)
6897 char *m;
6898 EMACS_INT a1, a2, a3;
6899 {
6900 Lisp_Object old_log_max;
6901 old_log_max = Vmessage_log_max;
6902 Vmessage_log_max = Qnil;
6903 message (m, a1, a2, a3);
6904 Vmessage_log_max = old_log_max;
6905 }
6906
6907
6908 /* Display the current message in the current mini-buffer. This is
6909 only called from error handlers in process.c, and is not time
6910 critical. */
6911
6912 void
6913 update_echo_area ()
6914 {
6915 if (!NILP (echo_area_buffer[0]))
6916 {
6917 Lisp_Object string;
6918 string = Fcurrent_message ();
6919 message3 (string, SBYTES (string),
6920 !NILP (current_buffer->enable_multibyte_characters));
6921 }
6922 }
6923
6924
6925 /* Make sure echo area buffers in `echo_buffers' are live.
6926 If they aren't, make new ones. */
6927
6928 static void
6929 ensure_echo_area_buffers ()
6930 {
6931 int i;
6932
6933 for (i = 0; i < 2; ++i)
6934 if (!BUFFERP (echo_buffer[i])
6935 || NILP (XBUFFER (echo_buffer[i])->name))
6936 {
6937 char name[30];
6938 Lisp_Object old_buffer;
6939 int j;
6940
6941 old_buffer = echo_buffer[i];
6942 sprintf (name, " *Echo Area %d*", i);
6943 echo_buffer[i] = Fget_buffer_create (build_string (name));
6944 XBUFFER (echo_buffer[i])->truncate_lines = Qnil;
6945
6946 for (j = 0; j < 2; ++j)
6947 if (EQ (old_buffer, echo_area_buffer[j]))
6948 echo_area_buffer[j] = echo_buffer[i];
6949 }
6950 }
6951
6952
6953 /* Call FN with args A1..A4 with either the current or last displayed
6954 echo_area_buffer as current buffer.
6955
6956 WHICH zero means use the current message buffer
6957 echo_area_buffer[0]. If that is nil, choose a suitable buffer
6958 from echo_buffer[] and clear it.
6959
6960 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
6961 suitable buffer from echo_buffer[] and clear it.
6962
6963 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
6964 that the current message becomes the last displayed one, make
6965 choose a suitable buffer for echo_area_buffer[0], and clear it.
6966
6967 Value is what FN returns. */
6968
6969 static int
6970 with_echo_area_buffer (w, which, fn, a1, a2, a3, a4)
6971 struct window *w;
6972 int which;
6973 int (*fn) P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
6974 EMACS_INT a1;
6975 Lisp_Object a2;
6976 EMACS_INT a3, a4;
6977 {
6978 Lisp_Object buffer;
6979 int this_one, the_other, clear_buffer_p, rc;
6980 int count = SPECPDL_INDEX ();
6981
6982 /* If buffers aren't live, make new ones. */
6983 ensure_echo_area_buffers ();
6984
6985 clear_buffer_p = 0;
6986
6987 if (which == 0)
6988 this_one = 0, the_other = 1;
6989 else if (which > 0)
6990 this_one = 1, the_other = 0;
6991 else
6992 {
6993 this_one = 0, the_other = 1;
6994 clear_buffer_p = 1;
6995
6996 /* We need a fresh one in case the current echo buffer equals
6997 the one containing the last displayed echo area message. */
6998 if (!NILP (echo_area_buffer[this_one])
6999 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
7000 echo_area_buffer[this_one] = Qnil;
7001 }
7002
7003 /* Choose a suitable buffer from echo_buffer[] is we don't
7004 have one. */
7005 if (NILP (echo_area_buffer[this_one]))
7006 {
7007 echo_area_buffer[this_one]
7008 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
7009 ? echo_buffer[the_other]
7010 : echo_buffer[this_one]);
7011 clear_buffer_p = 1;
7012 }
7013
7014 buffer = echo_area_buffer[this_one];
7015
7016 /* Don't get confused by reusing the buffer used for echoing
7017 for a different purpose. */
7018 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
7019 cancel_echoing ();
7020
7021 record_unwind_protect (unwind_with_echo_area_buffer,
7022 with_echo_area_buffer_unwind_data (w));
7023
7024 /* Make the echo area buffer current. Note that for display
7025 purposes, it is not necessary that the displayed window's buffer
7026 == current_buffer, except for text property lookup. So, let's
7027 only set that buffer temporarily here without doing a full
7028 Fset_window_buffer. We must also change w->pointm, though,
7029 because otherwise an assertions in unshow_buffer fails, and Emacs
7030 aborts. */
7031 set_buffer_internal_1 (XBUFFER (buffer));
7032 if (w)
7033 {
7034 w->buffer = buffer;
7035 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
7036 }
7037
7038 current_buffer->undo_list = Qt;
7039 current_buffer->read_only = Qnil;
7040 specbind (Qinhibit_read_only, Qt);
7041 specbind (Qinhibit_modification_hooks, Qt);
7042
7043 if (clear_buffer_p && Z > BEG)
7044 del_range (BEG, Z);
7045
7046 xassert (BEGV >= BEG);
7047 xassert (ZV <= Z && ZV >= BEGV);
7048
7049 rc = fn (a1, a2, a3, a4);
7050
7051 xassert (BEGV >= BEG);
7052 xassert (ZV <= Z && ZV >= BEGV);
7053
7054 unbind_to (count, Qnil);
7055 return rc;
7056 }
7057
7058
7059 /* Save state that should be preserved around the call to the function
7060 FN called in with_echo_area_buffer. */
7061
7062 static Lisp_Object
7063 with_echo_area_buffer_unwind_data (w)
7064 struct window *w;
7065 {
7066 int i = 0;
7067 Lisp_Object vector;
7068
7069 /* Reduce consing by keeping one vector in
7070 Vwith_echo_area_save_vector. */
7071 vector = Vwith_echo_area_save_vector;
7072 Vwith_echo_area_save_vector = Qnil;
7073
7074 if (NILP (vector))
7075 vector = Fmake_vector (make_number (7), Qnil);
7076
7077 XSETBUFFER (AREF (vector, i), current_buffer); ++i;
7078 AREF (vector, i) = Vdeactivate_mark, ++i;
7079 AREF (vector, i) = make_number (windows_or_buffers_changed), ++i;
7080
7081 if (w)
7082 {
7083 XSETWINDOW (AREF (vector, i), w); ++i;
7084 AREF (vector, i) = w->buffer; ++i;
7085 AREF (vector, i) = make_number (XMARKER (w->pointm)->charpos); ++i;
7086 AREF (vector, i) = make_number (XMARKER (w->pointm)->bytepos); ++i;
7087 }
7088 else
7089 {
7090 int end = i + 4;
7091 for (; i < end; ++i)
7092 AREF (vector, i) = Qnil;
7093 }
7094
7095 xassert (i == ASIZE (vector));
7096 return vector;
7097 }
7098
7099
7100 /* Restore global state from VECTOR which was created by
7101 with_echo_area_buffer_unwind_data. */
7102
7103 static Lisp_Object
7104 unwind_with_echo_area_buffer (vector)
7105 Lisp_Object vector;
7106 {
7107 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
7108 Vdeactivate_mark = AREF (vector, 1);
7109 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
7110
7111 if (WINDOWP (AREF (vector, 3)))
7112 {
7113 struct window *w;
7114 Lisp_Object buffer, charpos, bytepos;
7115
7116 w = XWINDOW (AREF (vector, 3));
7117 buffer = AREF (vector, 4);
7118 charpos = AREF (vector, 5);
7119 bytepos = AREF (vector, 6);
7120
7121 w->buffer = buffer;
7122 set_marker_both (w->pointm, buffer,
7123 XFASTINT (charpos), XFASTINT (bytepos));
7124 }
7125
7126 Vwith_echo_area_save_vector = vector;
7127 return Qnil;
7128 }
7129
7130
7131 /* Set up the echo area for use by print functions. MULTIBYTE_P
7132 non-zero means we will print multibyte. */
7133
7134 void
7135 setup_echo_area_for_printing (multibyte_p)
7136 int multibyte_p;
7137 {
7138 /* If we can't find an echo area any more, exit. */
7139 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
7140 Fkill_emacs (Qnil);
7141
7142 ensure_echo_area_buffers ();
7143
7144 if (!message_buf_print)
7145 {
7146 /* A message has been output since the last time we printed.
7147 Choose a fresh echo area buffer. */
7148 if (EQ (echo_area_buffer[1], echo_buffer[0]))
7149 echo_area_buffer[0] = echo_buffer[1];
7150 else
7151 echo_area_buffer[0] = echo_buffer[0];
7152
7153 /* Switch to that buffer and clear it. */
7154 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
7155 current_buffer->truncate_lines = Qnil;
7156
7157 if (Z > BEG)
7158 {
7159 int count = SPECPDL_INDEX ();
7160 specbind (Qinhibit_read_only, Qt);
7161 /* Note that undo recording is always disabled. */
7162 del_range (BEG, Z);
7163 unbind_to (count, Qnil);
7164 }
7165 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
7166
7167 /* Set up the buffer for the multibyteness we need. */
7168 if (multibyte_p
7169 != !NILP (current_buffer->enable_multibyte_characters))
7170 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
7171
7172 /* Raise the frame containing the echo area. */
7173 if (minibuffer_auto_raise)
7174 {
7175 struct frame *sf = SELECTED_FRAME ();
7176 Lisp_Object mini_window;
7177 mini_window = FRAME_MINIBUF_WINDOW (sf);
7178 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
7179 }
7180
7181 message_log_maybe_newline ();
7182 message_buf_print = 1;
7183 }
7184 else
7185 {
7186 if (NILP (echo_area_buffer[0]))
7187 {
7188 if (EQ (echo_area_buffer[1], echo_buffer[0]))
7189 echo_area_buffer[0] = echo_buffer[1];
7190 else
7191 echo_area_buffer[0] = echo_buffer[0];
7192 }
7193
7194 if (current_buffer != XBUFFER (echo_area_buffer[0]))
7195 {
7196 /* Someone switched buffers between print requests. */
7197 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
7198 current_buffer->truncate_lines = Qnil;
7199 }
7200 }
7201 }
7202
7203
7204 /* Display an echo area message in window W. Value is non-zero if W's
7205 height is changed. If display_last_displayed_message_p is
7206 non-zero, display the message that was last displayed, otherwise
7207 display the current message. */
7208
7209 static int
7210 display_echo_area (w)
7211 struct window *w;
7212 {
7213 int i, no_message_p, window_height_changed_p, count;
7214
7215 /* Temporarily disable garbage collections while displaying the echo
7216 area. This is done because a GC can print a message itself.
7217 That message would modify the echo area buffer's contents while a
7218 redisplay of the buffer is going on, and seriously confuse
7219 redisplay. */
7220 count = inhibit_garbage_collection ();
7221
7222 /* If there is no message, we must call display_echo_area_1
7223 nevertheless because it resizes the window. But we will have to
7224 reset the echo_area_buffer in question to nil at the end because
7225 with_echo_area_buffer will sets it to an empty buffer. */
7226 i = display_last_displayed_message_p ? 1 : 0;
7227 no_message_p = NILP (echo_area_buffer[i]);
7228
7229 window_height_changed_p
7230 = with_echo_area_buffer (w, display_last_displayed_message_p,
7231 display_echo_area_1,
7232 (EMACS_INT) w, Qnil, 0, 0);
7233
7234 if (no_message_p)
7235 echo_area_buffer[i] = Qnil;
7236
7237 unbind_to (count, Qnil);
7238 return window_height_changed_p;
7239 }
7240
7241
7242 /* Helper for display_echo_area. Display the current buffer which
7243 contains the current echo area message in window W, a mini-window,
7244 a pointer to which is passed in A1. A2..A4 are currently not used.
7245 Change the height of W so that all of the message is displayed.
7246 Value is non-zero if height of W was changed. */
7247
7248 static int
7249 display_echo_area_1 (a1, a2, a3, a4)
7250 EMACS_INT a1;
7251 Lisp_Object a2;
7252 EMACS_INT a3, a4;
7253 {
7254 struct window *w = (struct window *) a1;
7255 Lisp_Object window;
7256 struct text_pos start;
7257 int window_height_changed_p = 0;
7258
7259 /* Do this before displaying, so that we have a large enough glyph
7260 matrix for the display. */
7261 window_height_changed_p = resize_mini_window (w, 0);
7262
7263 /* Display. */
7264 clear_glyph_matrix (w->desired_matrix);
7265 XSETWINDOW (window, w);
7266 SET_TEXT_POS (start, BEG, BEG_BYTE);
7267 try_window (window, start);
7268
7269 return window_height_changed_p;
7270 }
7271
7272
7273 /* Resize the echo area window to exactly the size needed for the
7274 currently displayed message, if there is one. If a mini-buffer
7275 is active, don't shrink it. */
7276
7277 void
7278 resize_echo_area_exactly ()
7279 {
7280 if (BUFFERP (echo_area_buffer[0])
7281 && WINDOWP (echo_area_window))
7282 {
7283 struct window *w = XWINDOW (echo_area_window);
7284 int resized_p;
7285 Lisp_Object resize_exactly;
7286
7287 if (minibuf_level == 0)
7288 resize_exactly = Qt;
7289 else
7290 resize_exactly = Qnil;
7291
7292 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
7293 (EMACS_INT) w, resize_exactly, 0, 0);
7294 if (resized_p)
7295 {
7296 ++windows_or_buffers_changed;
7297 ++update_mode_lines;
7298 redisplay_internal (0);
7299 }
7300 }
7301 }
7302
7303
7304 /* Callback function for with_echo_area_buffer, when used from
7305 resize_echo_area_exactly. A1 contains a pointer to the window to
7306 resize, EXACTLY non-nil means resize the mini-window exactly to the
7307 size of the text displayed. A3 and A4 are not used. Value is what
7308 resize_mini_window returns. */
7309
7310 static int
7311 resize_mini_window_1 (a1, exactly, a3, a4)
7312 EMACS_INT a1;
7313 Lisp_Object exactly;
7314 EMACS_INT a3, a4;
7315 {
7316 return resize_mini_window ((struct window *) a1, !NILP (exactly));
7317 }
7318
7319
7320 /* Resize mini-window W to fit the size of its contents. EXACT:P
7321 means size the window exactly to the size needed. Otherwise, it's
7322 only enlarged until W's buffer is empty. Value is non-zero if
7323 the window height has been changed. */
7324
7325 int
7326 resize_mini_window (w, exact_p)
7327 struct window *w;
7328 int exact_p;
7329 {
7330 struct frame *f = XFRAME (w->frame);
7331 int window_height_changed_p = 0;
7332
7333 xassert (MINI_WINDOW_P (w));
7334
7335 /* Don't resize windows while redisplaying a window; it would
7336 confuse redisplay functions when the size of the window they are
7337 displaying changes from under them. Such a resizing can happen,
7338 for instance, when which-func prints a long message while
7339 we are running fontification-functions. We're running these
7340 functions with safe_call which binds inhibit-redisplay to t. */
7341 if (!NILP (Vinhibit_redisplay))
7342 return 0;
7343
7344 /* Nil means don't try to resize. */
7345 if (NILP (Vresize_mini_windows)
7346 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
7347 return 0;
7348
7349 if (!FRAME_MINIBUF_ONLY_P (f))
7350 {
7351 struct it it;
7352 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
7353 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
7354 int height, max_height;
7355 int unit = FRAME_LINE_HEIGHT (f);
7356 struct text_pos start;
7357 struct buffer *old_current_buffer = NULL;
7358
7359 if (current_buffer != XBUFFER (w->buffer))
7360 {
7361 old_current_buffer = current_buffer;
7362 set_buffer_internal (XBUFFER (w->buffer));
7363 }
7364
7365 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
7366
7367 /* Compute the max. number of lines specified by the user. */
7368 if (FLOATP (Vmax_mini_window_height))
7369 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
7370 else if (INTEGERP (Vmax_mini_window_height))
7371 max_height = XINT (Vmax_mini_window_height);
7372 else
7373 max_height = total_height / 4;
7374
7375 /* Correct that max. height if it's bogus. */
7376 max_height = max (1, max_height);
7377 max_height = min (total_height, max_height);
7378
7379 /* Find out the height of the text in the window. */
7380 if (it.truncate_lines_p)
7381 height = 1;
7382 else
7383 {
7384 last_height = 0;
7385 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
7386 if (it.max_ascent == 0 && it.max_descent == 0)
7387 height = it.current_y + last_height;
7388 else
7389 height = it.current_y + it.max_ascent + it.max_descent;
7390 height -= it.extra_line_spacing;
7391 height = (height + unit - 1) / unit;
7392 }
7393
7394 /* Compute a suitable window start. */
7395 if (height > max_height)
7396 {
7397 height = max_height;
7398 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
7399 move_it_vertically_backward (&it, (height - 1) * unit);
7400 start = it.current.pos;
7401 }
7402 else
7403 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
7404 SET_MARKER_FROM_TEXT_POS (w->start, start);
7405
7406 if (EQ (Vresize_mini_windows, Qgrow_only))
7407 {
7408 /* Let it grow only, until we display an empty message, in which
7409 case the window shrinks again. */
7410 if (height > WINDOW_TOTAL_LINES (w))
7411 {
7412 int old_height = WINDOW_TOTAL_LINES (w);
7413 freeze_window_starts (f, 1);
7414 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
7415 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
7416 }
7417 else if (height < WINDOW_TOTAL_LINES (w)
7418 && (exact_p || BEGV == ZV))
7419 {
7420 int old_height = WINDOW_TOTAL_LINES (w);
7421 freeze_window_starts (f, 0);
7422 shrink_mini_window (w);
7423 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
7424 }
7425 }
7426 else
7427 {
7428 /* Always resize to exact size needed. */
7429 if (height > WINDOW_TOTAL_LINES (w))
7430 {
7431 int old_height = WINDOW_TOTAL_LINES (w);
7432 freeze_window_starts (f, 1);
7433 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
7434 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
7435 }
7436 else if (height < WINDOW_TOTAL_LINES (w))
7437 {
7438 int old_height = WINDOW_TOTAL_LINES (w);
7439 freeze_window_starts (f, 0);
7440 shrink_mini_window (w);
7441
7442 if (height)
7443 {
7444 freeze_window_starts (f, 1);
7445 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
7446 }
7447
7448 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
7449 }
7450 }
7451
7452 if (old_current_buffer)
7453 set_buffer_internal (old_current_buffer);
7454 }
7455
7456 return window_height_changed_p;
7457 }
7458
7459
7460 /* Value is the current message, a string, or nil if there is no
7461 current message. */
7462
7463 Lisp_Object
7464 current_message ()
7465 {
7466 Lisp_Object msg;
7467
7468 if (NILP (echo_area_buffer[0]))
7469 msg = Qnil;
7470 else
7471 {
7472 with_echo_area_buffer (0, 0, current_message_1,
7473 (EMACS_INT) &msg, Qnil, 0, 0);
7474 if (NILP (msg))
7475 echo_area_buffer[0] = Qnil;
7476 }
7477
7478 return msg;
7479 }
7480
7481
7482 static int
7483 current_message_1 (a1, a2, a3, a4)
7484 EMACS_INT a1;
7485 Lisp_Object a2;
7486 EMACS_INT a3, a4;
7487 {
7488 Lisp_Object *msg = (Lisp_Object *) a1;
7489
7490 if (Z > BEG)
7491 *msg = make_buffer_string (BEG, Z, 1);
7492 else
7493 *msg = Qnil;
7494 return 0;
7495 }
7496
7497
7498 /* Push the current message on Vmessage_stack for later restauration
7499 by restore_message. Value is non-zero if the current message isn't
7500 empty. This is a relatively infrequent operation, so it's not
7501 worth optimizing. */
7502
7503 int
7504 push_message ()
7505 {
7506 Lisp_Object msg;
7507 msg = current_message ();
7508 Vmessage_stack = Fcons (msg, Vmessage_stack);
7509 return STRINGP (msg);
7510 }
7511
7512
7513 /* Restore message display from the top of Vmessage_stack. */
7514
7515 void
7516 restore_message ()
7517 {
7518 Lisp_Object msg;
7519
7520 xassert (CONSP (Vmessage_stack));
7521 msg = XCAR (Vmessage_stack);
7522 if (STRINGP (msg))
7523 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
7524 else
7525 message3_nolog (msg, 0, 0);
7526 }
7527
7528
7529 /* Handler for record_unwind_protect calling pop_message. */
7530
7531 Lisp_Object
7532 pop_message_unwind (dummy)
7533 Lisp_Object dummy;
7534 {
7535 pop_message ();
7536 return Qnil;
7537 }
7538
7539 /* Pop the top-most entry off Vmessage_stack. */
7540
7541 void
7542 pop_message ()
7543 {
7544 xassert (CONSP (Vmessage_stack));
7545 Vmessage_stack = XCDR (Vmessage_stack);
7546 }
7547
7548
7549 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
7550 exits. If the stack is not empty, we have a missing pop_message
7551 somewhere. */
7552
7553 void
7554 check_message_stack ()
7555 {
7556 if (!NILP (Vmessage_stack))
7557 abort ();
7558 }
7559
7560
7561 /* Truncate to NCHARS what will be displayed in the echo area the next
7562 time we display it---but don't redisplay it now. */
7563
7564 void
7565 truncate_echo_area (nchars)
7566 int nchars;
7567 {
7568 if (nchars == 0)
7569 echo_area_buffer[0] = Qnil;
7570 /* A null message buffer means that the frame hasn't really been
7571 initialized yet. Error messages get reported properly by
7572 cmd_error, so this must be just an informative message; toss it. */
7573 else if (!noninteractive
7574 && INTERACTIVE
7575 && !NILP (echo_area_buffer[0]))
7576 {
7577 struct frame *sf = SELECTED_FRAME ();
7578 if (FRAME_MESSAGE_BUF (sf))
7579 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
7580 }
7581 }
7582
7583
7584 /* Helper function for truncate_echo_area. Truncate the current
7585 message to at most NCHARS characters. */
7586
7587 static int
7588 truncate_message_1 (nchars, a2, a3, a4)
7589 EMACS_INT nchars;
7590 Lisp_Object a2;
7591 EMACS_INT a3, a4;
7592 {
7593 if (BEG + nchars < Z)
7594 del_range (BEG + nchars, Z);
7595 if (Z == BEG)
7596 echo_area_buffer[0] = Qnil;
7597 return 0;
7598 }
7599
7600
7601 /* Set the current message to a substring of S or STRING.
7602
7603 If STRING is a Lisp string, set the message to the first NBYTES
7604 bytes from STRING. NBYTES zero means use the whole string. If
7605 STRING is multibyte, the message will be displayed multibyte.
7606
7607 If S is not null, set the message to the first LEN bytes of S. LEN
7608 zero means use the whole string. MULTIBYTE_P non-zero means S is
7609 multibyte. Display the message multibyte in that case. */
7610
7611 void
7612 set_message (s, string, nbytes, multibyte_p)
7613 const char *s;
7614 Lisp_Object string;
7615 int nbytes, multibyte_p;
7616 {
7617 message_enable_multibyte
7618 = ((s && multibyte_p)
7619 || (STRINGP (string) && STRING_MULTIBYTE (string)));
7620
7621 with_echo_area_buffer (0, -1, set_message_1,
7622 (EMACS_INT) s, string, nbytes, multibyte_p);
7623 message_buf_print = 0;
7624 help_echo_showing_p = 0;
7625 }
7626
7627
7628 /* Helper function for set_message. Arguments have the same meaning
7629 as there, with A1 corresponding to S and A2 corresponding to STRING
7630 This function is called with the echo area buffer being
7631 current. */
7632
7633 static int
7634 set_message_1 (a1, a2, nbytes, multibyte_p)
7635 EMACS_INT a1;
7636 Lisp_Object a2;
7637 EMACS_INT nbytes, multibyte_p;
7638 {
7639 const char *s = (const char *) a1;
7640 Lisp_Object string = a2;
7641
7642 xassert (BEG == Z);
7643
7644 /* Change multibyteness of the echo buffer appropriately. */
7645 if (message_enable_multibyte
7646 != !NILP (current_buffer->enable_multibyte_characters))
7647 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
7648
7649 current_buffer->truncate_lines = message_truncate_lines ? Qt : Qnil;
7650
7651 /* Insert new message at BEG. */
7652 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
7653
7654 if (STRINGP (string))
7655 {
7656 int nchars;
7657
7658 if (nbytes == 0)
7659 nbytes = SBYTES (string);
7660 nchars = string_byte_to_char (string, nbytes);
7661
7662 /* This function takes care of single/multibyte conversion. We
7663 just have to ensure that the echo area buffer has the right
7664 setting of enable_multibyte_characters. */
7665 insert_from_string (string, 0, 0, nchars, nbytes, 1);
7666 }
7667 else if (s)
7668 {
7669 if (nbytes == 0)
7670 nbytes = strlen (s);
7671
7672 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
7673 {
7674 /* Convert from multi-byte to single-byte. */
7675 int i, c, n;
7676 unsigned char work[1];
7677
7678 /* Convert a multibyte string to single-byte. */
7679 for (i = 0; i < nbytes; i += n)
7680 {
7681 c = string_char_and_length (s + i, nbytes - i, &n);
7682 work[0] = (SINGLE_BYTE_CHAR_P (c)
7683 ? c
7684 : multibyte_char_to_unibyte (c, Qnil));
7685 insert_1_both (work, 1, 1, 1, 0, 0);
7686 }
7687 }
7688 else if (!multibyte_p
7689 && !NILP (current_buffer->enable_multibyte_characters))
7690 {
7691 /* Convert from single-byte to multi-byte. */
7692 int i, c, n;
7693 const unsigned char *msg = (const unsigned char *) s;
7694 unsigned char str[MAX_MULTIBYTE_LENGTH];
7695
7696 /* Convert a single-byte string to multibyte. */
7697 for (i = 0; i < nbytes; i++)
7698 {
7699 c = unibyte_char_to_multibyte (msg[i]);
7700 n = CHAR_STRING (c, str);
7701 insert_1_both (str, 1, n, 1, 0, 0);
7702 }
7703 }
7704 else
7705 insert_1 (s, nbytes, 1, 0, 0);
7706 }
7707
7708 return 0;
7709 }
7710
7711
7712 /* Clear messages. CURRENT_P non-zero means clear the current
7713 message. LAST_DISPLAYED_P non-zero means clear the message
7714 last displayed. */
7715
7716 void
7717 clear_message (current_p, last_displayed_p)
7718 int current_p, last_displayed_p;
7719 {
7720 if (current_p)
7721 {
7722 echo_area_buffer[0] = Qnil;
7723 message_cleared_p = 1;
7724 }
7725
7726 if (last_displayed_p)
7727 echo_area_buffer[1] = Qnil;
7728
7729 message_buf_print = 0;
7730 }
7731
7732 /* Clear garbaged frames.
7733
7734 This function is used where the old redisplay called
7735 redraw_garbaged_frames which in turn called redraw_frame which in
7736 turn called clear_frame. The call to clear_frame was a source of
7737 flickering. I believe a clear_frame is not necessary. It should
7738 suffice in the new redisplay to invalidate all current matrices,
7739 and ensure a complete redisplay of all windows. */
7740
7741 static void
7742 clear_garbaged_frames ()
7743 {
7744 if (frame_garbaged)
7745 {
7746 Lisp_Object tail, frame;
7747 int changed_count = 0;
7748
7749 FOR_EACH_FRAME (tail, frame)
7750 {
7751 struct frame *f = XFRAME (frame);
7752
7753 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
7754 {
7755 if (f->resized_p)
7756 {
7757 Fredraw_frame (frame);
7758 f->force_flush_display_p = 1;
7759 }
7760 clear_current_matrices (f);
7761 changed_count++;
7762 f->garbaged = 0;
7763 f->resized_p = 0;
7764 }
7765 }
7766
7767 frame_garbaged = 0;
7768 if (changed_count)
7769 ++windows_or_buffers_changed;
7770 }
7771 }
7772
7773
7774 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
7775 is non-zero update selected_frame. Value is non-zero if the
7776 mini-windows height has been changed. */
7777
7778 static int
7779 echo_area_display (update_frame_p)
7780 int update_frame_p;
7781 {
7782 Lisp_Object mini_window;
7783 struct window *w;
7784 struct frame *f;
7785 int window_height_changed_p = 0;
7786 struct frame *sf = SELECTED_FRAME ();
7787
7788 mini_window = FRAME_MINIBUF_WINDOW (sf);
7789 w = XWINDOW (mini_window);
7790 f = XFRAME (WINDOW_FRAME (w));
7791
7792 /* Don't display if frame is invisible or not yet initialized. */
7793 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
7794 return 0;
7795
7796 /* The terminal frame is used as the first Emacs frame on the Mac OS. */
7797 #ifndef MAC_OS8
7798 #ifdef HAVE_WINDOW_SYSTEM
7799 /* When Emacs starts, selected_frame may be the initial terminal
7800 frame. If we let this through, a message would be displayed on
7801 the terminal. */
7802 if (FRAME_TERMCAP_P (XFRAME (selected_frame))
7803 && FRAME_TTY (XFRAME (selected_frame))->type == NULL)
7804 return 0;
7805 #endif /* HAVE_WINDOW_SYSTEM */
7806 #endif
7807
7808 /* Redraw garbaged frames. */
7809 if (frame_garbaged)
7810 clear_garbaged_frames ();
7811
7812 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
7813 {
7814 echo_area_window = mini_window;
7815 window_height_changed_p = display_echo_area (w);
7816 w->must_be_updated_p = 1;
7817
7818 /* Update the display, unless called from redisplay_internal.
7819 Also don't update the screen during redisplay itself. The
7820 update will happen at the end of redisplay, and an update
7821 here could cause confusion. */
7822 if (update_frame_p && !redisplaying_p)
7823 {
7824 int n = 0;
7825
7826 /* If the display update has been interrupted by pending
7827 input, update mode lines in the frame. Due to the
7828 pending input, it might have been that redisplay hasn't
7829 been called, so that mode lines above the echo area are
7830 garbaged. This looks odd, so we prevent it here. */
7831 if (!display_completed)
7832 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
7833
7834 if (window_height_changed_p
7835 /* Don't do this if Emacs is shutting down. Redisplay
7836 needs to run hooks. */
7837 && !NILP (Vrun_hooks))
7838 {
7839 /* Must update other windows. Likewise as in other
7840 cases, don't let this update be interrupted by
7841 pending input. */
7842 int count = SPECPDL_INDEX ();
7843 specbind (Qredisplay_dont_pause, Qt);
7844 windows_or_buffers_changed = 1;
7845 redisplay_internal (0);
7846 unbind_to (count, Qnil);
7847 }
7848 else if (FRAME_WINDOW_P (f) && n == 0)
7849 {
7850 /* Window configuration is the same as before.
7851 Can do with a display update of the echo area,
7852 unless we displayed some mode lines. */
7853 update_single_window (w, 1);
7854 FRAME_RIF (f)->flush_display (f);
7855 }
7856 else
7857 update_frame (f, 1, 1);
7858
7859 /* If cursor is in the echo area, make sure that the next
7860 redisplay displays the minibuffer, so that the cursor will
7861 be replaced with what the minibuffer wants. */
7862 if (cursor_in_echo_area)
7863 ++windows_or_buffers_changed;
7864 }
7865 }
7866 else if (!EQ (mini_window, selected_window))
7867 windows_or_buffers_changed++;
7868
7869 /* Last displayed message is now the current message. */
7870 echo_area_buffer[1] = echo_area_buffer[0];
7871
7872 /* Prevent redisplay optimization in redisplay_internal by resetting
7873 this_line_start_pos. This is done because the mini-buffer now
7874 displays the message instead of its buffer text. */
7875 if (EQ (mini_window, selected_window))
7876 CHARPOS (this_line_start_pos) = 0;
7877
7878 return window_height_changed_p;
7879 }
7880
7881
7882 \f
7883 /***********************************************************************
7884 Frame Titles
7885 ***********************************************************************/
7886
7887
7888 /* The frame title buffering code is also used by Fformat_mode_line.
7889 So it is not conditioned by HAVE_WINDOW_SYSTEM. */
7890
7891 /* A buffer for constructing frame titles in it; allocated from the
7892 heap in init_xdisp and resized as needed in store_frame_title_char. */
7893
7894 static char *frame_title_buf;
7895
7896 /* The buffer's end, and a current output position in it. */
7897
7898 static char *frame_title_buf_end;
7899 static char *frame_title_ptr;
7900
7901
7902 /* Store a single character C for the frame title in frame_title_buf.
7903 Re-allocate frame_title_buf if necessary. */
7904
7905 static void
7906 #ifdef PROTOTYPES
7907 store_frame_title_char (char c)
7908 #else
7909 store_frame_title_char (c)
7910 char c;
7911 #endif
7912 {
7913 /* If output position has reached the end of the allocated buffer,
7914 double the buffer's size. */
7915 if (frame_title_ptr == frame_title_buf_end)
7916 {
7917 int len = frame_title_ptr - frame_title_buf;
7918 int new_size = 2 * len * sizeof *frame_title_buf;
7919 frame_title_buf = (char *) xrealloc (frame_title_buf, new_size);
7920 frame_title_buf_end = frame_title_buf + new_size;
7921 frame_title_ptr = frame_title_buf + len;
7922 }
7923
7924 *frame_title_ptr++ = c;
7925 }
7926
7927
7928 /* Store part of a frame title in frame_title_buf, beginning at
7929 frame_title_ptr. STR is the string to store. Do not copy
7930 characters that yield more columns than PRECISION; PRECISION <= 0
7931 means copy the whole string. Pad with spaces until FIELD_WIDTH
7932 number of characters have been copied; FIELD_WIDTH <= 0 means don't
7933 pad. Called from display_mode_element when it is used to build a
7934 frame title. */
7935
7936 static int
7937 store_frame_title (str, field_width, precision)
7938 const unsigned char *str;
7939 int field_width, precision;
7940 {
7941 int n = 0;
7942 int dummy, nbytes;
7943
7944 /* Copy at most PRECISION chars from STR. */
7945 nbytes = strlen (str);
7946 n+= c_string_width (str, nbytes, precision, &dummy, &nbytes);
7947 while (nbytes--)
7948 store_frame_title_char (*str++);
7949
7950 /* Fill up with spaces until FIELD_WIDTH reached. */
7951 while (field_width > 0
7952 && n < field_width)
7953 {
7954 store_frame_title_char (' ');
7955 ++n;
7956 }
7957
7958 return n;
7959 }
7960
7961 #ifdef HAVE_WINDOW_SYSTEM
7962
7963 /* Set the title of FRAME, if it has changed. The title format is
7964 Vicon_title_format if FRAME is iconified, otherwise it is
7965 frame_title_format. */
7966
7967 static void
7968 x_consider_frame_title (frame)
7969 Lisp_Object frame;
7970 {
7971 struct frame *f = XFRAME (frame);
7972
7973 if (FRAME_WINDOW_P (f)
7974 || FRAME_MINIBUF_ONLY_P (f)
7975 || f->explicit_name)
7976 {
7977 /* Do we have more than one visible frame on this X display? */
7978 Lisp_Object tail;
7979 Lisp_Object fmt;
7980 struct buffer *obuf;
7981 int len;
7982 struct it it;
7983
7984 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
7985 {
7986 Lisp_Object other_frame = XCAR (tail);
7987 struct frame *tf = XFRAME (other_frame);
7988
7989 if (tf != f
7990 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
7991 && !FRAME_MINIBUF_ONLY_P (tf)
7992 && !EQ (other_frame, tip_frame)
7993 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
7994 break;
7995 }
7996
7997 /* Set global variable indicating that multiple frames exist. */
7998 multiple_frames = CONSP (tail);
7999
8000 /* Switch to the buffer of selected window of the frame. Set up
8001 frame_title_ptr so that display_mode_element will output into it;
8002 then display the title. */
8003 obuf = current_buffer;
8004 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
8005 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
8006 frame_title_ptr = frame_title_buf;
8007 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
8008 NULL, DEFAULT_FACE_ID);
8009 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
8010 len = frame_title_ptr - frame_title_buf;
8011 frame_title_ptr = NULL;
8012 set_buffer_internal_1 (obuf);
8013
8014 /* Set the title only if it's changed. This avoids consing in
8015 the common case where it hasn't. (If it turns out that we've
8016 already wasted too much time by walking through the list with
8017 display_mode_element, then we might need to optimize at a
8018 higher level than this.) */
8019 if (! STRINGP (f->name)
8020 || SBYTES (f->name) != len
8021 || bcmp (frame_title_buf, SDATA (f->name), len) != 0)
8022 x_implicitly_set_name (f, make_string (frame_title_buf, len), Qnil);
8023 }
8024 }
8025
8026 #endif /* not HAVE_WINDOW_SYSTEM */
8027
8028
8029
8030 \f
8031 /***********************************************************************
8032 Menu Bars
8033 ***********************************************************************/
8034
8035
8036 /* Prepare for redisplay by updating menu-bar item lists when
8037 appropriate. This can call eval. */
8038
8039 void
8040 prepare_menu_bars ()
8041 {
8042 int all_windows;
8043 struct gcpro gcpro1, gcpro2;
8044 struct frame *f;
8045 Lisp_Object tooltip_frame;
8046
8047 #ifdef HAVE_WINDOW_SYSTEM
8048 tooltip_frame = tip_frame;
8049 #else
8050 tooltip_frame = Qnil;
8051 #endif
8052
8053 /* Update all frame titles based on their buffer names, etc. We do
8054 this before the menu bars so that the buffer-menu will show the
8055 up-to-date frame titles. */
8056 #ifdef HAVE_WINDOW_SYSTEM
8057 if (windows_or_buffers_changed || update_mode_lines)
8058 {
8059 Lisp_Object tail, frame;
8060
8061 FOR_EACH_FRAME (tail, frame)
8062 {
8063 f = XFRAME (frame);
8064 if (!EQ (frame, tooltip_frame)
8065 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
8066 x_consider_frame_title (frame);
8067 }
8068 }
8069 #endif /* HAVE_WINDOW_SYSTEM */
8070
8071 /* Update the menu bar item lists, if appropriate. This has to be
8072 done before any actual redisplay or generation of display lines. */
8073 all_windows = (update_mode_lines
8074 || buffer_shared > 1
8075 || windows_or_buffers_changed);
8076 if (all_windows)
8077 {
8078 Lisp_Object tail, frame;
8079 int count = SPECPDL_INDEX ();
8080
8081 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
8082
8083 FOR_EACH_FRAME (tail, frame)
8084 {
8085 f = XFRAME (frame);
8086
8087 /* Ignore tooltip frame. */
8088 if (EQ (frame, tooltip_frame))
8089 continue;
8090
8091 /* If a window on this frame changed size, report that to
8092 the user and clear the size-change flag. */
8093 if (FRAME_WINDOW_SIZES_CHANGED (f))
8094 {
8095 Lisp_Object functions;
8096
8097 /* Clear flag first in case we get an error below. */
8098 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
8099 functions = Vwindow_size_change_functions;
8100 GCPRO2 (tail, functions);
8101
8102 while (CONSP (functions))
8103 {
8104 call1 (XCAR (functions), frame);
8105 functions = XCDR (functions);
8106 }
8107 UNGCPRO;
8108 }
8109
8110 GCPRO1 (tail);
8111 update_menu_bar (f, 0);
8112 #ifdef HAVE_WINDOW_SYSTEM
8113 update_tool_bar (f, 0);
8114 #endif
8115 UNGCPRO;
8116 }
8117
8118 unbind_to (count, Qnil);
8119 }
8120 else
8121 {
8122 struct frame *sf = SELECTED_FRAME ();
8123 update_menu_bar (sf, 1);
8124 #ifdef HAVE_WINDOW_SYSTEM
8125 update_tool_bar (sf, 1);
8126 #endif
8127 }
8128
8129 /* Motif needs this. See comment in xmenu.c. Turn it off when
8130 pending_menu_activation is not defined. */
8131 #ifdef USE_X_TOOLKIT
8132 pending_menu_activation = 0;
8133 #endif
8134 }
8135
8136
8137 /* Update the menu bar item list for frame F. This has to be done
8138 before we start to fill in any display lines, because it can call
8139 eval.
8140
8141 If SAVE_MATCH_DATA is non-zero, we must save and restore it here. */
8142
8143 static void
8144 update_menu_bar (f, save_match_data)
8145 struct frame *f;
8146 int save_match_data;
8147 {
8148 Lisp_Object window;
8149 register struct window *w;
8150
8151 /* If called recursively during a menu update, do nothing. This can
8152 happen when, for instance, an activate-menubar-hook causes a
8153 redisplay. */
8154 if (inhibit_menubar_update)
8155 return;
8156
8157 window = FRAME_SELECTED_WINDOW (f);
8158 w = XWINDOW (window);
8159
8160 #if 0 /* The if statement below this if statement used to include the
8161 condition !NILP (w->update_mode_line), rather than using
8162 update_mode_lines directly, and this if statement may have
8163 been added to make that condition work. Now the if
8164 statement below matches its comment, this isn't needed. */
8165 if (update_mode_lines)
8166 w->update_mode_line = Qt;
8167 #endif
8168
8169 if (FRAME_WINDOW_P (f)
8170 ?
8171 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
8172 || defined (USE_GTK)
8173 FRAME_EXTERNAL_MENU_BAR (f)
8174 #else
8175 FRAME_MENU_BAR_LINES (f) > 0
8176 #endif
8177 : FRAME_MENU_BAR_LINES (f) > 0)
8178 {
8179 /* If the user has switched buffers or windows, we need to
8180 recompute to reflect the new bindings. But we'll
8181 recompute when update_mode_lines is set too; that means
8182 that people can use force-mode-line-update to request
8183 that the menu bar be recomputed. The adverse effect on
8184 the rest of the redisplay algorithm is about the same as
8185 windows_or_buffers_changed anyway. */
8186 if (windows_or_buffers_changed
8187 /* This used to test w->update_mode_line, but we believe
8188 there is no need to recompute the menu in that case. */
8189 || update_mode_lines
8190 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
8191 < BUF_MODIFF (XBUFFER (w->buffer)))
8192 != !NILP (w->last_had_star))
8193 || ((!NILP (Vtransient_mark_mode)
8194 && !NILP (XBUFFER (w->buffer)->mark_active))
8195 != !NILP (w->region_showing)))
8196 {
8197 struct buffer *prev = current_buffer;
8198 int count = SPECPDL_INDEX ();
8199
8200 specbind (Qinhibit_menubar_update, Qt);
8201
8202 set_buffer_internal_1 (XBUFFER (w->buffer));
8203 if (save_match_data)
8204 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
8205 if (NILP (Voverriding_local_map_menu_flag))
8206 {
8207 specbind (Qoverriding_terminal_local_map, Qnil);
8208 specbind (Qoverriding_local_map, Qnil);
8209 }
8210
8211 /* Run the Lucid hook. */
8212 safe_run_hooks (Qactivate_menubar_hook);
8213
8214 /* If it has changed current-menubar from previous value,
8215 really recompute the menu-bar from the value. */
8216 if (! NILP (Vlucid_menu_bar_dirty_flag))
8217 call0 (Qrecompute_lucid_menubar);
8218
8219 safe_run_hooks (Qmenu_bar_update_hook);
8220 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
8221
8222 /* Redisplay the menu bar in case we changed it. */
8223 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
8224 || defined (USE_GTK)
8225 if (FRAME_WINDOW_P (f)
8226 #if defined (MAC_OS)
8227 /* All frames on Mac OS share the same menubar. So only the
8228 selected frame should be allowed to set it. */
8229 && f == SELECTED_FRAME ()
8230 #endif
8231 )
8232 set_frame_menubar (f, 0, 0);
8233 else
8234 /* On a terminal screen, the menu bar is an ordinary screen
8235 line, and this makes it get updated. */
8236 w->update_mode_line = Qt;
8237 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
8238 /* In the non-toolkit version, the menu bar is an ordinary screen
8239 line, and this makes it get updated. */
8240 w->update_mode_line = Qt;
8241 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
8242
8243 unbind_to (count, Qnil);
8244 set_buffer_internal_1 (prev);
8245 }
8246 }
8247 }
8248
8249
8250 \f
8251 /***********************************************************************
8252 Output Cursor
8253 ***********************************************************************/
8254
8255 #ifdef HAVE_WINDOW_SYSTEM
8256
8257 /* EXPORT:
8258 Nominal cursor position -- where to draw output.
8259 HPOS and VPOS are window relative glyph matrix coordinates.
8260 X and Y are window relative pixel coordinates. */
8261
8262 struct cursor_pos output_cursor;
8263
8264
8265 /* EXPORT:
8266 Set the global variable output_cursor to CURSOR. All cursor
8267 positions are relative to updated_window. */
8268
8269 void
8270 set_output_cursor (cursor)
8271 struct cursor_pos *cursor;
8272 {
8273 output_cursor.hpos = cursor->hpos;
8274 output_cursor.vpos = cursor->vpos;
8275 output_cursor.x = cursor->x;
8276 output_cursor.y = cursor->y;
8277 }
8278
8279
8280 /* EXPORT for RIF:
8281 Set a nominal cursor position.
8282
8283 HPOS and VPOS are column/row positions in a window glyph matrix. X
8284 and Y are window text area relative pixel positions.
8285
8286 If this is done during an update, updated_window will contain the
8287 window that is being updated and the position is the future output
8288 cursor position for that window. If updated_window is null, use
8289 selected_window and display the cursor at the given position. */
8290
8291 void
8292 x_cursor_to (vpos, hpos, y, x)
8293 int vpos, hpos, y, x;
8294 {
8295 struct window *w;
8296
8297 /* If updated_window is not set, work on selected_window. */
8298 if (updated_window)
8299 w = updated_window;
8300 else
8301 w = XWINDOW (selected_window);
8302
8303 /* Set the output cursor. */
8304 output_cursor.hpos = hpos;
8305 output_cursor.vpos = vpos;
8306 output_cursor.x = x;
8307 output_cursor.y = y;
8308
8309 /* If not called as part of an update, really display the cursor.
8310 This will also set the cursor position of W. */
8311 if (updated_window == NULL)
8312 {
8313 BLOCK_INPUT;
8314 display_and_set_cursor (w, 1, hpos, vpos, x, y);
8315 if (FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
8316 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (SELECTED_FRAME ());
8317 UNBLOCK_INPUT;
8318 }
8319 }
8320
8321 #endif /* HAVE_WINDOW_SYSTEM */
8322
8323 \f
8324 /***********************************************************************
8325 Tool-bars
8326 ***********************************************************************/
8327
8328 #ifdef HAVE_WINDOW_SYSTEM
8329
8330 /* Where the mouse was last time we reported a mouse event. */
8331
8332 FRAME_PTR last_mouse_frame;
8333
8334 /* Tool-bar item index of the item on which a mouse button was pressed
8335 or -1. */
8336
8337 int last_tool_bar_item;
8338
8339
8340 /* Update the tool-bar item list for frame F. This has to be done
8341 before we start to fill in any display lines. Called from
8342 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
8343 and restore it here. */
8344
8345 static void
8346 update_tool_bar (f, save_match_data)
8347 struct frame *f;
8348 int save_match_data;
8349 {
8350 #ifdef USE_GTK
8351 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
8352 #else
8353 int do_update = WINDOWP (f->tool_bar_window)
8354 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
8355 #endif
8356
8357 if (do_update)
8358 {
8359 Lisp_Object window;
8360 struct window *w;
8361
8362 window = FRAME_SELECTED_WINDOW (f);
8363 w = XWINDOW (window);
8364
8365 /* If the user has switched buffers or windows, we need to
8366 recompute to reflect the new bindings. But we'll
8367 recompute when update_mode_lines is set too; that means
8368 that people can use force-mode-line-update to request
8369 that the menu bar be recomputed. The adverse effect on
8370 the rest of the redisplay algorithm is about the same as
8371 windows_or_buffers_changed anyway. */
8372 if (windows_or_buffers_changed
8373 || !NILP (w->update_mode_line)
8374 || update_mode_lines
8375 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
8376 < BUF_MODIFF (XBUFFER (w->buffer)))
8377 != !NILP (w->last_had_star))
8378 || ((!NILP (Vtransient_mark_mode)
8379 && !NILP (XBUFFER (w->buffer)->mark_active))
8380 != !NILP (w->region_showing)))
8381 {
8382 struct buffer *prev = current_buffer;
8383 int count = SPECPDL_INDEX ();
8384 Lisp_Object old_tool_bar;
8385 struct gcpro gcpro1;
8386
8387 /* Set current_buffer to the buffer of the selected
8388 window of the frame, so that we get the right local
8389 keymaps. */
8390 set_buffer_internal_1 (XBUFFER (w->buffer));
8391
8392 /* Save match data, if we must. */
8393 if (save_match_data)
8394 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
8395
8396 /* Make sure that we don't accidentally use bogus keymaps. */
8397 if (NILP (Voverriding_local_map_menu_flag))
8398 {
8399 specbind (Qoverriding_terminal_local_map, Qnil);
8400 specbind (Qoverriding_local_map, Qnil);
8401 }
8402
8403 old_tool_bar = f->tool_bar_items;
8404 GCPRO1 (old_tool_bar);
8405
8406 /* Build desired tool-bar items from keymaps. */
8407 BLOCK_INPUT;
8408 f->tool_bar_items
8409 = tool_bar_items (f->tool_bar_items, &f->n_tool_bar_items);
8410 UNBLOCK_INPUT;
8411
8412 /* Redisplay the tool-bar if we changed it. */
8413 if (! NILP (Fequal (old_tool_bar, f->tool_bar_items)))
8414 w->update_mode_line = Qt;
8415
8416 UNGCPRO;
8417
8418 unbind_to (count, Qnil);
8419 set_buffer_internal_1 (prev);
8420 }
8421 }
8422 }
8423
8424
8425 /* Set F->desired_tool_bar_string to a Lisp string representing frame
8426 F's desired tool-bar contents. F->tool_bar_items must have
8427 been set up previously by calling prepare_menu_bars. */
8428
8429 static void
8430 build_desired_tool_bar_string (f)
8431 struct frame *f;
8432 {
8433 int i, size, size_needed;
8434 struct gcpro gcpro1, gcpro2, gcpro3;
8435 Lisp_Object image, plist, props;
8436
8437 image = plist = props = Qnil;
8438 GCPRO3 (image, plist, props);
8439
8440 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
8441 Otherwise, make a new string. */
8442
8443 /* The size of the string we might be able to reuse. */
8444 size = (STRINGP (f->desired_tool_bar_string)
8445 ? SCHARS (f->desired_tool_bar_string)
8446 : 0);
8447
8448 /* We need one space in the string for each image. */
8449 size_needed = f->n_tool_bar_items;
8450
8451 /* Reuse f->desired_tool_bar_string, if possible. */
8452 if (size < size_needed || NILP (f->desired_tool_bar_string))
8453 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
8454 make_number (' '));
8455 else
8456 {
8457 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
8458 Fremove_text_properties (make_number (0), make_number (size),
8459 props, f->desired_tool_bar_string);
8460 }
8461
8462 /* Put a `display' property on the string for the images to display,
8463 put a `menu_item' property on tool-bar items with a value that
8464 is the index of the item in F's tool-bar item vector. */
8465 for (i = 0; i < f->n_tool_bar_items; ++i)
8466 {
8467 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
8468
8469 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
8470 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
8471 int hmargin, vmargin, relief, idx, end;
8472 extern Lisp_Object QCrelief, QCmargin, QCconversion;
8473
8474 /* If image is a vector, choose the image according to the
8475 button state. */
8476 image = PROP (TOOL_BAR_ITEM_IMAGES);
8477 if (VECTORP (image))
8478 {
8479 if (enabled_p)
8480 idx = (selected_p
8481 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
8482 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
8483 else
8484 idx = (selected_p
8485 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
8486 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
8487
8488 xassert (ASIZE (image) >= idx);
8489 image = AREF (image, idx);
8490 }
8491 else
8492 idx = -1;
8493
8494 /* Ignore invalid image specifications. */
8495 if (!valid_image_p (image))
8496 continue;
8497
8498 /* Display the tool-bar button pressed, or depressed. */
8499 plist = Fcopy_sequence (XCDR (image));
8500
8501 /* Compute margin and relief to draw. */
8502 relief = (tool_bar_button_relief >= 0
8503 ? tool_bar_button_relief
8504 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
8505 hmargin = vmargin = relief;
8506
8507 if (INTEGERP (Vtool_bar_button_margin)
8508 && XINT (Vtool_bar_button_margin) > 0)
8509 {
8510 hmargin += XFASTINT (Vtool_bar_button_margin);
8511 vmargin += XFASTINT (Vtool_bar_button_margin);
8512 }
8513 else if (CONSP (Vtool_bar_button_margin))
8514 {
8515 if (INTEGERP (XCAR (Vtool_bar_button_margin))
8516 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
8517 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
8518
8519 if (INTEGERP (XCDR (Vtool_bar_button_margin))
8520 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
8521 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
8522 }
8523
8524 if (auto_raise_tool_bar_buttons_p)
8525 {
8526 /* Add a `:relief' property to the image spec if the item is
8527 selected. */
8528 if (selected_p)
8529 {
8530 plist = Fplist_put (plist, QCrelief, make_number (-relief));
8531 hmargin -= relief;
8532 vmargin -= relief;
8533 }
8534 }
8535 else
8536 {
8537 /* If image is selected, display it pressed, i.e. with a
8538 negative relief. If it's not selected, display it with a
8539 raised relief. */
8540 plist = Fplist_put (plist, QCrelief,
8541 (selected_p
8542 ? make_number (-relief)
8543 : make_number (relief)));
8544 hmargin -= relief;
8545 vmargin -= relief;
8546 }
8547
8548 /* Put a margin around the image. */
8549 if (hmargin || vmargin)
8550 {
8551 if (hmargin == vmargin)
8552 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
8553 else
8554 plist = Fplist_put (plist, QCmargin,
8555 Fcons (make_number (hmargin),
8556 make_number (vmargin)));
8557 }
8558
8559 /* If button is not enabled, and we don't have special images
8560 for the disabled state, make the image appear disabled by
8561 applying an appropriate algorithm to it. */
8562 if (!enabled_p && idx < 0)
8563 plist = Fplist_put (plist, QCconversion, Qdisabled);
8564
8565 /* Put a `display' text property on the string for the image to
8566 display. Put a `menu-item' property on the string that gives
8567 the start of this item's properties in the tool-bar items
8568 vector. */
8569 image = Fcons (Qimage, plist);
8570 props = list4 (Qdisplay, image,
8571 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
8572
8573 /* Let the last image hide all remaining spaces in the tool bar
8574 string. The string can be longer than needed when we reuse a
8575 previous string. */
8576 if (i + 1 == f->n_tool_bar_items)
8577 end = SCHARS (f->desired_tool_bar_string);
8578 else
8579 end = i + 1;
8580 Fadd_text_properties (make_number (i), make_number (end),
8581 props, f->desired_tool_bar_string);
8582 #undef PROP
8583 }
8584
8585 UNGCPRO;
8586 }
8587
8588
8589 /* Display one line of the tool-bar of frame IT->f. */
8590
8591 static void
8592 display_tool_bar_line (it)
8593 struct it *it;
8594 {
8595 struct glyph_row *row = it->glyph_row;
8596 int max_x = it->last_visible_x;
8597 struct glyph *last;
8598
8599 prepare_desired_row (row);
8600 row->y = it->current_y;
8601
8602 /* Note that this isn't made use of if the face hasn't a box,
8603 so there's no need to check the face here. */
8604 it->start_of_box_run_p = 1;
8605
8606 while (it->current_x < max_x)
8607 {
8608 int x_before, x, n_glyphs_before, i, nglyphs;
8609
8610 /* Get the next display element. */
8611 if (!get_next_display_element (it))
8612 break;
8613
8614 /* Produce glyphs. */
8615 x_before = it->current_x;
8616 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
8617 PRODUCE_GLYPHS (it);
8618
8619 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
8620 i = 0;
8621 x = x_before;
8622 while (i < nglyphs)
8623 {
8624 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
8625
8626 if (x + glyph->pixel_width > max_x)
8627 {
8628 /* Glyph doesn't fit on line. */
8629 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
8630 it->current_x = x;
8631 goto out;
8632 }
8633
8634 ++it->hpos;
8635 x += glyph->pixel_width;
8636 ++i;
8637 }
8638
8639 /* Stop at line ends. */
8640 if (ITERATOR_AT_END_OF_LINE_P (it))
8641 break;
8642
8643 set_iterator_to_next (it, 1);
8644 }
8645
8646 out:;
8647
8648 row->displays_text_p = row->used[TEXT_AREA] != 0;
8649 extend_face_to_end_of_line (it);
8650 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
8651 last->right_box_line_p = 1;
8652 if (last == row->glyphs[TEXT_AREA])
8653 last->left_box_line_p = 1;
8654 compute_line_metrics (it);
8655
8656 /* If line is empty, make it occupy the rest of the tool-bar. */
8657 if (!row->displays_text_p)
8658 {
8659 row->height = row->phys_height = it->last_visible_y - row->y;
8660 row->ascent = row->phys_ascent = 0;
8661 }
8662
8663 row->full_width_p = 1;
8664 row->continued_p = 0;
8665 row->truncated_on_left_p = 0;
8666 row->truncated_on_right_p = 0;
8667
8668 it->current_x = it->hpos = 0;
8669 it->current_y += row->height;
8670 ++it->vpos;
8671 ++it->glyph_row;
8672 }
8673
8674
8675 /* Value is the number of screen lines needed to make all tool-bar
8676 items of frame F visible. */
8677
8678 static int
8679 tool_bar_lines_needed (f)
8680 struct frame *f;
8681 {
8682 struct window *w = XWINDOW (f->tool_bar_window);
8683 struct it it;
8684
8685 /* Initialize an iterator for iteration over
8686 F->desired_tool_bar_string in the tool-bar window of frame F. */
8687 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
8688 it.first_visible_x = 0;
8689 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
8690 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
8691
8692 while (!ITERATOR_AT_END_P (&it))
8693 {
8694 it.glyph_row = w->desired_matrix->rows;
8695 clear_glyph_row (it.glyph_row);
8696 display_tool_bar_line (&it);
8697 }
8698
8699 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
8700 }
8701
8702
8703 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
8704 0, 1, 0,
8705 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
8706 (frame)
8707 Lisp_Object frame;
8708 {
8709 struct frame *f;
8710 struct window *w;
8711 int nlines = 0;
8712
8713 if (NILP (frame))
8714 frame = selected_frame;
8715 else
8716 CHECK_FRAME (frame);
8717 f = XFRAME (frame);
8718
8719 if (WINDOWP (f->tool_bar_window)
8720 || (w = XWINDOW (f->tool_bar_window),
8721 WINDOW_TOTAL_LINES (w) > 0))
8722 {
8723 update_tool_bar (f, 1);
8724 if (f->n_tool_bar_items)
8725 {
8726 build_desired_tool_bar_string (f);
8727 nlines = tool_bar_lines_needed (f);
8728 }
8729 }
8730
8731 return make_number (nlines);
8732 }
8733
8734
8735 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
8736 height should be changed. */
8737
8738 static int
8739 redisplay_tool_bar (f)
8740 struct frame *f;
8741 {
8742 struct window *w;
8743 struct it it;
8744 struct glyph_row *row;
8745 int change_height_p = 0;
8746
8747 #ifdef USE_GTK
8748 if (FRAME_EXTERNAL_TOOL_BAR (f))
8749 update_frame_tool_bar (f);
8750 return 0;
8751 #endif
8752
8753 /* If frame hasn't a tool-bar window or if it is zero-height, don't
8754 do anything. This means you must start with tool-bar-lines
8755 non-zero to get the auto-sizing effect. Or in other words, you
8756 can turn off tool-bars by specifying tool-bar-lines zero. */
8757 if (!WINDOWP (f->tool_bar_window)
8758 || (w = XWINDOW (f->tool_bar_window),
8759 WINDOW_TOTAL_LINES (w) == 0))
8760 return 0;
8761
8762 /* Set up an iterator for the tool-bar window. */
8763 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
8764 it.first_visible_x = 0;
8765 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
8766 row = it.glyph_row;
8767
8768 /* Build a string that represents the contents of the tool-bar. */
8769 build_desired_tool_bar_string (f);
8770 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
8771
8772 /* Display as many lines as needed to display all tool-bar items. */
8773 while (it.current_y < it.last_visible_y)
8774 display_tool_bar_line (&it);
8775
8776 /* It doesn't make much sense to try scrolling in the tool-bar
8777 window, so don't do it. */
8778 w->desired_matrix->no_scrolling_p = 1;
8779 w->must_be_updated_p = 1;
8780
8781 if (auto_resize_tool_bars_p)
8782 {
8783 int nlines;
8784
8785 /* If we couldn't display everything, change the tool-bar's
8786 height. */
8787 if (IT_STRING_CHARPOS (it) < it.end_charpos)
8788 change_height_p = 1;
8789
8790 /* If there are blank lines at the end, except for a partially
8791 visible blank line at the end that is smaller than
8792 FRAME_LINE_HEIGHT, change the tool-bar's height. */
8793 row = it.glyph_row - 1;
8794 if (!row->displays_text_p
8795 && row->height >= FRAME_LINE_HEIGHT (f))
8796 change_height_p = 1;
8797
8798 /* If row displays tool-bar items, but is partially visible,
8799 change the tool-bar's height. */
8800 if (row->displays_text_p
8801 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y)
8802 change_height_p = 1;
8803
8804 /* Resize windows as needed by changing the `tool-bar-lines'
8805 frame parameter. */
8806 if (change_height_p
8807 && (nlines = tool_bar_lines_needed (f),
8808 nlines != WINDOW_TOTAL_LINES (w)))
8809 {
8810 extern Lisp_Object Qtool_bar_lines;
8811 Lisp_Object frame;
8812 int old_height = WINDOW_TOTAL_LINES (w);
8813
8814 XSETFRAME (frame, f);
8815 clear_glyph_matrix (w->desired_matrix);
8816 Fmodify_frame_parameters (frame,
8817 Fcons (Fcons (Qtool_bar_lines,
8818 make_number (nlines)),
8819 Qnil));
8820 if (WINDOW_TOTAL_LINES (w) != old_height)
8821 fonts_changed_p = 1;
8822 }
8823 }
8824
8825 return change_height_p;
8826 }
8827
8828
8829 /* Get information about the tool-bar item which is displayed in GLYPH
8830 on frame F. Return in *PROP_IDX the index where tool-bar item
8831 properties start in F->tool_bar_items. Value is zero if
8832 GLYPH doesn't display a tool-bar item. */
8833
8834 static int
8835 tool_bar_item_info (f, glyph, prop_idx)
8836 struct frame *f;
8837 struct glyph *glyph;
8838 int *prop_idx;
8839 {
8840 Lisp_Object prop;
8841 int success_p;
8842 int charpos;
8843
8844 /* This function can be called asynchronously, which means we must
8845 exclude any possibility that Fget_text_property signals an
8846 error. */
8847 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
8848 charpos = max (0, charpos);
8849
8850 /* Get the text property `menu-item' at pos. The value of that
8851 property is the start index of this item's properties in
8852 F->tool_bar_items. */
8853 prop = Fget_text_property (make_number (charpos),
8854 Qmenu_item, f->current_tool_bar_string);
8855 if (INTEGERP (prop))
8856 {
8857 *prop_idx = XINT (prop);
8858 success_p = 1;
8859 }
8860 else
8861 success_p = 0;
8862
8863 return success_p;
8864 }
8865
8866 \f
8867 /* Get information about the tool-bar item at position X/Y on frame F.
8868 Return in *GLYPH a pointer to the glyph of the tool-bar item in
8869 the current matrix of the tool-bar window of F, or NULL if not
8870 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
8871 item in F->tool_bar_items. Value is
8872
8873 -1 if X/Y is not on a tool-bar item
8874 0 if X/Y is on the same item that was highlighted before.
8875 1 otherwise. */
8876
8877 static int
8878 get_tool_bar_item (f, x, y, glyph, hpos, vpos, prop_idx)
8879 struct frame *f;
8880 int x, y;
8881 struct glyph **glyph;
8882 int *hpos, *vpos, *prop_idx;
8883 {
8884 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
8885 struct window *w = XWINDOW (f->tool_bar_window);
8886 int area;
8887
8888 /* Find the glyph under X/Y. */
8889 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
8890 if (*glyph == NULL)
8891 return -1;
8892
8893 /* Get the start of this tool-bar item's properties in
8894 f->tool_bar_items. */
8895 if (!tool_bar_item_info (f, *glyph, prop_idx))
8896 return -1;
8897
8898 /* Is mouse on the highlighted item? */
8899 if (EQ (f->tool_bar_window, dpyinfo->mouse_face_window)
8900 && *vpos >= dpyinfo->mouse_face_beg_row
8901 && *vpos <= dpyinfo->mouse_face_end_row
8902 && (*vpos > dpyinfo->mouse_face_beg_row
8903 || *hpos >= dpyinfo->mouse_face_beg_col)
8904 && (*vpos < dpyinfo->mouse_face_end_row
8905 || *hpos < dpyinfo->mouse_face_end_col
8906 || dpyinfo->mouse_face_past_end))
8907 return 0;
8908
8909 return 1;
8910 }
8911
8912
8913 /* EXPORT:
8914 Handle mouse button event on the tool-bar of frame F, at
8915 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
8916 0 for button release. MODIFIERS is event modifiers for button
8917 release. */
8918
8919 void
8920 handle_tool_bar_click (f, x, y, down_p, modifiers)
8921 struct frame *f;
8922 int x, y, down_p;
8923 unsigned int modifiers;
8924 {
8925 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
8926 struct window *w = XWINDOW (f->tool_bar_window);
8927 int hpos, vpos, prop_idx;
8928 struct glyph *glyph;
8929 Lisp_Object enabled_p;
8930
8931 /* If not on the highlighted tool-bar item, return. */
8932 frame_to_window_pixel_xy (w, &x, &y);
8933 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
8934 return;
8935
8936 /* If item is disabled, do nothing. */
8937 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
8938 if (NILP (enabled_p))
8939 return;
8940
8941 if (down_p)
8942 {
8943 /* Show item in pressed state. */
8944 show_mouse_face (dpyinfo, DRAW_IMAGE_SUNKEN);
8945 dpyinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
8946 last_tool_bar_item = prop_idx;
8947 }
8948 else
8949 {
8950 Lisp_Object key, frame;
8951 struct input_event event;
8952 EVENT_INIT (event);
8953
8954 /* Show item in released state. */
8955 show_mouse_face (dpyinfo, DRAW_IMAGE_RAISED);
8956 dpyinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
8957
8958 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
8959
8960 XSETFRAME (frame, f);
8961 event.kind = TOOL_BAR_EVENT;
8962 event.frame_or_window = frame;
8963 event.arg = frame;
8964 kbd_buffer_store_event (&event);
8965
8966 event.kind = TOOL_BAR_EVENT;
8967 event.frame_or_window = frame;
8968 event.arg = key;
8969 event.modifiers = modifiers;
8970 kbd_buffer_store_event (&event);
8971 last_tool_bar_item = -1;
8972 }
8973 }
8974
8975
8976 /* Possibly highlight a tool-bar item on frame F when mouse moves to
8977 tool-bar window-relative coordinates X/Y. Called from
8978 note_mouse_highlight. */
8979
8980 static void
8981 note_tool_bar_highlight (f, x, y)
8982 struct frame *f;
8983 int x, y;
8984 {
8985 Lisp_Object window = f->tool_bar_window;
8986 struct window *w = XWINDOW (window);
8987 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
8988 int hpos, vpos;
8989 struct glyph *glyph;
8990 struct glyph_row *row;
8991 int i;
8992 Lisp_Object enabled_p;
8993 int prop_idx;
8994 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
8995 int mouse_down_p, rc;
8996
8997 /* Function note_mouse_highlight is called with negative x(y
8998 values when mouse moves outside of the frame. */
8999 if (x <= 0 || y <= 0)
9000 {
9001 clear_mouse_face (dpyinfo);
9002 return;
9003 }
9004
9005 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
9006 if (rc < 0)
9007 {
9008 /* Not on tool-bar item. */
9009 clear_mouse_face (dpyinfo);
9010 return;
9011 }
9012 else if (rc == 0)
9013 /* On same tool-bar item as before. */
9014 goto set_help_echo;
9015
9016 clear_mouse_face (dpyinfo);
9017
9018 /* Mouse is down, but on different tool-bar item? */
9019 mouse_down_p = (dpyinfo->grabbed
9020 && f == last_mouse_frame
9021 && FRAME_LIVE_P (f));
9022 if (mouse_down_p
9023 && last_tool_bar_item != prop_idx)
9024 return;
9025
9026 dpyinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
9027 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
9028
9029 /* If tool-bar item is not enabled, don't highlight it. */
9030 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
9031 if (!NILP (enabled_p))
9032 {
9033 /* Compute the x-position of the glyph. In front and past the
9034 image is a space. We include this in the highlighted area. */
9035 row = MATRIX_ROW (w->current_matrix, vpos);
9036 for (i = x = 0; i < hpos; ++i)
9037 x += row->glyphs[TEXT_AREA][i].pixel_width;
9038
9039 /* Record this as the current active region. */
9040 dpyinfo->mouse_face_beg_col = hpos;
9041 dpyinfo->mouse_face_beg_row = vpos;
9042 dpyinfo->mouse_face_beg_x = x;
9043 dpyinfo->mouse_face_beg_y = row->y;
9044 dpyinfo->mouse_face_past_end = 0;
9045
9046 dpyinfo->mouse_face_end_col = hpos + 1;
9047 dpyinfo->mouse_face_end_row = vpos;
9048 dpyinfo->mouse_face_end_x = x + glyph->pixel_width;
9049 dpyinfo->mouse_face_end_y = row->y;
9050 dpyinfo->mouse_face_window = window;
9051 dpyinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
9052
9053 /* Display it as active. */
9054 show_mouse_face (dpyinfo, draw);
9055 dpyinfo->mouse_face_image_state = draw;
9056 }
9057
9058 set_help_echo:
9059
9060 /* Set help_echo_string to a help string to display for this tool-bar item.
9061 XTread_socket does the rest. */
9062 help_echo_object = help_echo_window = Qnil;
9063 help_echo_pos = -1;
9064 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
9065 if (NILP (help_echo_string))
9066 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
9067 }
9068
9069 #endif /* HAVE_WINDOW_SYSTEM */
9070
9071
9072 \f
9073 /************************************************************************
9074 Horizontal scrolling
9075 ************************************************************************/
9076
9077 static int hscroll_window_tree P_ ((Lisp_Object));
9078 static int hscroll_windows P_ ((Lisp_Object));
9079
9080 /* For all leaf windows in the window tree rooted at WINDOW, set their
9081 hscroll value so that PT is (i) visible in the window, and (ii) so
9082 that it is not within a certain margin at the window's left and
9083 right border. Value is non-zero if any window's hscroll has been
9084 changed. */
9085
9086 static int
9087 hscroll_window_tree (window)
9088 Lisp_Object window;
9089 {
9090 int hscrolled_p = 0;
9091 int hscroll_relative_p = FLOATP (Vhscroll_step);
9092 int hscroll_step_abs = 0;
9093 double hscroll_step_rel = 0;
9094
9095 if (hscroll_relative_p)
9096 {
9097 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
9098 if (hscroll_step_rel < 0)
9099 {
9100 hscroll_relative_p = 0;
9101 hscroll_step_abs = 0;
9102 }
9103 }
9104 else if (INTEGERP (Vhscroll_step))
9105 {
9106 hscroll_step_abs = XINT (Vhscroll_step);
9107 if (hscroll_step_abs < 0)
9108 hscroll_step_abs = 0;
9109 }
9110 else
9111 hscroll_step_abs = 0;
9112
9113 while (WINDOWP (window))
9114 {
9115 struct window *w = XWINDOW (window);
9116
9117 if (WINDOWP (w->hchild))
9118 hscrolled_p |= hscroll_window_tree (w->hchild);
9119 else if (WINDOWP (w->vchild))
9120 hscrolled_p |= hscroll_window_tree (w->vchild);
9121 else if (w->cursor.vpos >= 0)
9122 {
9123 int h_margin;
9124 int text_area_width;
9125 struct glyph_row *current_cursor_row
9126 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
9127 struct glyph_row *desired_cursor_row
9128 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
9129 struct glyph_row *cursor_row
9130 = (desired_cursor_row->enabled_p
9131 ? desired_cursor_row
9132 : current_cursor_row);
9133
9134 text_area_width = window_box_width (w, TEXT_AREA);
9135
9136 /* Scroll when cursor is inside this scroll margin. */
9137 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
9138
9139 if ((XFASTINT (w->hscroll)
9140 && w->cursor.x <= h_margin)
9141 || (cursor_row->enabled_p
9142 && cursor_row->truncated_on_right_p
9143 && (w->cursor.x >= text_area_width - h_margin)))
9144 {
9145 struct it it;
9146 int hscroll;
9147 struct buffer *saved_current_buffer;
9148 int pt;
9149 int wanted_x;
9150
9151 /* Find point in a display of infinite width. */
9152 saved_current_buffer = current_buffer;
9153 current_buffer = XBUFFER (w->buffer);
9154
9155 if (w == XWINDOW (selected_window))
9156 pt = BUF_PT (current_buffer);
9157 else
9158 {
9159 pt = marker_position (w->pointm);
9160 pt = max (BEGV, pt);
9161 pt = min (ZV, pt);
9162 }
9163
9164 /* Move iterator to pt starting at cursor_row->start in
9165 a line with infinite width. */
9166 init_to_row_start (&it, w, cursor_row);
9167 it.last_visible_x = INFINITY;
9168 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
9169 current_buffer = saved_current_buffer;
9170
9171 /* Position cursor in window. */
9172 if (!hscroll_relative_p && hscroll_step_abs == 0)
9173 hscroll = max (0, (it.current_x
9174 - (ITERATOR_AT_END_OF_LINE_P (&it)
9175 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
9176 : (text_area_width / 2))))
9177 / FRAME_COLUMN_WIDTH (it.f);
9178 else if (w->cursor.x >= text_area_width - h_margin)
9179 {
9180 if (hscroll_relative_p)
9181 wanted_x = text_area_width * (1 - hscroll_step_rel)
9182 - h_margin;
9183 else
9184 wanted_x = text_area_width
9185 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
9186 - h_margin;
9187 hscroll
9188 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
9189 }
9190 else
9191 {
9192 if (hscroll_relative_p)
9193 wanted_x = text_area_width * hscroll_step_rel
9194 + h_margin;
9195 else
9196 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
9197 + h_margin;
9198 hscroll
9199 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
9200 }
9201 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
9202
9203 /* Don't call Fset_window_hscroll if value hasn't
9204 changed because it will prevent redisplay
9205 optimizations. */
9206 if (XFASTINT (w->hscroll) != hscroll)
9207 {
9208 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
9209 w->hscroll = make_number (hscroll);
9210 hscrolled_p = 1;
9211 }
9212 }
9213 }
9214
9215 window = w->next;
9216 }
9217
9218 /* Value is non-zero if hscroll of any leaf window has been changed. */
9219 return hscrolled_p;
9220 }
9221
9222
9223 /* Set hscroll so that cursor is visible and not inside horizontal
9224 scroll margins for all windows in the tree rooted at WINDOW. See
9225 also hscroll_window_tree above. Value is non-zero if any window's
9226 hscroll has been changed. If it has, desired matrices on the frame
9227 of WINDOW are cleared. */
9228
9229 static int
9230 hscroll_windows (window)
9231 Lisp_Object window;
9232 {
9233 int hscrolled_p;
9234
9235 if (automatic_hscrolling_p)
9236 {
9237 hscrolled_p = hscroll_window_tree (window);
9238 if (hscrolled_p)
9239 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
9240 }
9241 else
9242 hscrolled_p = 0;
9243 return hscrolled_p;
9244 }
9245
9246
9247 \f
9248 /************************************************************************
9249 Redisplay
9250 ************************************************************************/
9251
9252 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
9253 to a non-zero value. This is sometimes handy to have in a debugger
9254 session. */
9255
9256 #if GLYPH_DEBUG
9257
9258 /* First and last unchanged row for try_window_id. */
9259
9260 int debug_first_unchanged_at_end_vpos;
9261 int debug_last_unchanged_at_beg_vpos;
9262
9263 /* Delta vpos and y. */
9264
9265 int debug_dvpos, debug_dy;
9266
9267 /* Delta in characters and bytes for try_window_id. */
9268
9269 int debug_delta, debug_delta_bytes;
9270
9271 /* Values of window_end_pos and window_end_vpos at the end of
9272 try_window_id. */
9273
9274 EMACS_INT debug_end_pos, debug_end_vpos;
9275
9276 /* Append a string to W->desired_matrix->method. FMT is a printf
9277 format string. A1...A9 are a supplement for a variable-length
9278 argument list. If trace_redisplay_p is non-zero also printf the
9279 resulting string to stderr. */
9280
9281 static void
9282 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
9283 struct window *w;
9284 char *fmt;
9285 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
9286 {
9287 char buffer[512];
9288 char *method = w->desired_matrix->method;
9289 int len = strlen (method);
9290 int size = sizeof w->desired_matrix->method;
9291 int remaining = size - len - 1;
9292
9293 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
9294 if (len && remaining)
9295 {
9296 method[len] = '|';
9297 --remaining, ++len;
9298 }
9299
9300 strncpy (method + len, buffer, remaining);
9301
9302 if (trace_redisplay_p)
9303 fprintf (stderr, "%p (%s): %s\n",
9304 w,
9305 ((BUFFERP (w->buffer)
9306 && STRINGP (XBUFFER (w->buffer)->name))
9307 ? (char *) SDATA (XBUFFER (w->buffer)->name)
9308 : "no buffer"),
9309 buffer);
9310 }
9311
9312 #endif /* GLYPH_DEBUG */
9313
9314
9315 /* Value is non-zero if all changes in window W, which displays
9316 current_buffer, are in the text between START and END. START is a
9317 buffer position, END is given as a distance from Z. Used in
9318 redisplay_internal for display optimization. */
9319
9320 static INLINE int
9321 text_outside_line_unchanged_p (w, start, end)
9322 struct window *w;
9323 int start, end;
9324 {
9325 int unchanged_p = 1;
9326
9327 /* If text or overlays have changed, see where. */
9328 if (XFASTINT (w->last_modified) < MODIFF
9329 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
9330 {
9331 /* Gap in the line? */
9332 if (GPT < start || Z - GPT < end)
9333 unchanged_p = 0;
9334
9335 /* Changes start in front of the line, or end after it? */
9336 if (unchanged_p
9337 && (BEG_UNCHANGED < start - 1
9338 || END_UNCHANGED < end))
9339 unchanged_p = 0;
9340
9341 /* If selective display, can't optimize if changes start at the
9342 beginning of the line. */
9343 if (unchanged_p
9344 && INTEGERP (current_buffer->selective_display)
9345 && XINT (current_buffer->selective_display) > 0
9346 && (BEG_UNCHANGED < start || GPT <= start))
9347 unchanged_p = 0;
9348
9349 /* If there are overlays at the start or end of the line, these
9350 may have overlay strings with newlines in them. A change at
9351 START, for instance, may actually concern the display of such
9352 overlay strings as well, and they are displayed on different
9353 lines. So, quickly rule out this case. (For the future, it
9354 might be desirable to implement something more telling than
9355 just BEG/END_UNCHANGED.) */
9356 if (unchanged_p)
9357 {
9358 if (BEG + BEG_UNCHANGED == start
9359 && overlay_touches_p (start))
9360 unchanged_p = 0;
9361 if (END_UNCHANGED == end
9362 && overlay_touches_p (Z - end))
9363 unchanged_p = 0;
9364 }
9365 }
9366
9367 return unchanged_p;
9368 }
9369
9370
9371 /* Do a frame update, taking possible shortcuts into account. This is
9372 the main external entry point for redisplay.
9373
9374 If the last redisplay displayed an echo area message and that message
9375 is no longer requested, we clear the echo area or bring back the
9376 mini-buffer if that is in use. */
9377
9378 void
9379 redisplay ()
9380 {
9381 redisplay_internal (0);
9382 }
9383
9384
9385 static Lisp_Object
9386 overlay_arrow_string_or_property (var, pbitmap)
9387 Lisp_Object var;
9388 int *pbitmap;
9389 {
9390 Lisp_Object pstr = Fget (var, Qoverlay_arrow_string);
9391 Lisp_Object bitmap;
9392
9393 if (pbitmap)
9394 {
9395 *pbitmap = 0;
9396 if (bitmap = Fget (var, Qoverlay_arrow_bitmap), INTEGERP (bitmap))
9397 *pbitmap = XINT (bitmap);
9398 }
9399
9400 if (!NILP (pstr))
9401 return pstr;
9402 return Voverlay_arrow_string;
9403 }
9404
9405 /* Return 1 if there are any overlay-arrows in current_buffer. */
9406 static int
9407 overlay_arrow_in_current_buffer_p ()
9408 {
9409 Lisp_Object vlist;
9410
9411 for (vlist = Voverlay_arrow_variable_list;
9412 CONSP (vlist);
9413 vlist = XCDR (vlist))
9414 {
9415 Lisp_Object var = XCAR (vlist);
9416 Lisp_Object val;
9417
9418 if (!SYMBOLP (var))
9419 continue;
9420 val = find_symbol_value (var);
9421 if (MARKERP (val)
9422 && current_buffer == XMARKER (val)->buffer)
9423 return 1;
9424 }
9425 return 0;
9426 }
9427
9428
9429 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
9430 has changed. */
9431
9432 static int
9433 overlay_arrows_changed_p ()
9434 {
9435 Lisp_Object vlist;
9436
9437 for (vlist = Voverlay_arrow_variable_list;
9438 CONSP (vlist);
9439 vlist = XCDR (vlist))
9440 {
9441 Lisp_Object var = XCAR (vlist);
9442 Lisp_Object val, pstr;
9443
9444 if (!SYMBOLP (var))
9445 continue;
9446 val = find_symbol_value (var);
9447 if (!MARKERP (val))
9448 continue;
9449 if (! EQ (COERCE_MARKER (val),
9450 Fget (var, Qlast_arrow_position))
9451 || ! (pstr = overlay_arrow_string_or_property (var, 0),
9452 EQ (pstr, Fget (var, Qlast_arrow_string))))
9453 return 1;
9454 }
9455 return 0;
9456 }
9457
9458 /* Mark overlay arrows to be updated on next redisplay. */
9459
9460 static void
9461 update_overlay_arrows (up_to_date)
9462 int up_to_date;
9463 {
9464 Lisp_Object vlist;
9465
9466 for (vlist = Voverlay_arrow_variable_list;
9467 CONSP (vlist);
9468 vlist = XCDR (vlist))
9469 {
9470 Lisp_Object var = XCAR (vlist);
9471
9472 if (!SYMBOLP (var))
9473 continue;
9474
9475 if (up_to_date > 0)
9476 {
9477 Lisp_Object val = find_symbol_value (var);
9478 Fput (var, Qlast_arrow_position,
9479 COERCE_MARKER (val));
9480 Fput (var, Qlast_arrow_string,
9481 overlay_arrow_string_or_property (var, 0));
9482 }
9483 else if (up_to_date < 0
9484 || !NILP (Fget (var, Qlast_arrow_position)))
9485 {
9486 Fput (var, Qlast_arrow_position, Qt);
9487 Fput (var, Qlast_arrow_string, Qt);
9488 }
9489 }
9490 }
9491
9492
9493 /* Return overlay arrow string at row, or nil. */
9494
9495 static Lisp_Object
9496 overlay_arrow_at_row (f, row, pbitmap)
9497 struct frame *f;
9498 struct glyph_row *row;
9499 int *pbitmap;
9500 {
9501 Lisp_Object vlist;
9502
9503 for (vlist = Voverlay_arrow_variable_list;
9504 CONSP (vlist);
9505 vlist = XCDR (vlist))
9506 {
9507 Lisp_Object var = XCAR (vlist);
9508 Lisp_Object val;
9509
9510 if (!SYMBOLP (var))
9511 continue;
9512
9513 val = find_symbol_value (var);
9514
9515 if (MARKERP (val)
9516 && current_buffer == XMARKER (val)->buffer
9517 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
9518 {
9519 val = overlay_arrow_string_or_property (var, pbitmap);
9520 if (FRAME_WINDOW_P (f))
9521 return Qt;
9522 else if (STRINGP (val))
9523 return val;
9524 break;
9525 }
9526 }
9527
9528 *pbitmap = 0;
9529 return Qnil;
9530 }
9531
9532 /* Return 1 if point moved out of or into a composition. Otherwise
9533 return 0. PREV_BUF and PREV_PT are the last point buffer and
9534 position. BUF and PT are the current point buffer and position. */
9535
9536 int
9537 check_point_in_composition (prev_buf, prev_pt, buf, pt)
9538 struct buffer *prev_buf, *buf;
9539 int prev_pt, pt;
9540 {
9541 int start, end;
9542 Lisp_Object prop;
9543 Lisp_Object buffer;
9544
9545 XSETBUFFER (buffer, buf);
9546 /* Check a composition at the last point if point moved within the
9547 same buffer. */
9548 if (prev_buf == buf)
9549 {
9550 if (prev_pt == pt)
9551 /* Point didn't move. */
9552 return 0;
9553
9554 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
9555 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
9556 && COMPOSITION_VALID_P (start, end, prop)
9557 && start < prev_pt && end > prev_pt)
9558 /* The last point was within the composition. Return 1 iff
9559 point moved out of the composition. */
9560 return (pt <= start || pt >= end);
9561 }
9562
9563 /* Check a composition at the current point. */
9564 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
9565 && find_composition (pt, -1, &start, &end, &prop, buffer)
9566 && COMPOSITION_VALID_P (start, end, prop)
9567 && start < pt && end > pt);
9568 }
9569
9570
9571 /* Reconsider the setting of B->clip_changed which is displayed
9572 in window W. */
9573
9574 static INLINE void
9575 reconsider_clip_changes (w, b)
9576 struct window *w;
9577 struct buffer *b;
9578 {
9579 if (b->clip_changed
9580 && !NILP (w->window_end_valid)
9581 && w->current_matrix->buffer == b
9582 && w->current_matrix->zv == BUF_ZV (b)
9583 && w->current_matrix->begv == BUF_BEGV (b))
9584 b->clip_changed = 0;
9585
9586 /* If display wasn't paused, and W is not a tool bar window, see if
9587 point has been moved into or out of a composition. In that case,
9588 we set b->clip_changed to 1 to force updating the screen. If
9589 b->clip_changed has already been set to 1, we can skip this
9590 check. */
9591 if (!b->clip_changed
9592 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
9593 {
9594 int pt;
9595
9596 if (w == XWINDOW (selected_window))
9597 pt = BUF_PT (current_buffer);
9598 else
9599 pt = marker_position (w->pointm);
9600
9601 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
9602 || pt != XINT (w->last_point))
9603 && check_point_in_composition (w->current_matrix->buffer,
9604 XINT (w->last_point),
9605 XBUFFER (w->buffer), pt))
9606 b->clip_changed = 1;
9607 }
9608 }
9609 \f
9610
9611 /* Select FRAME to forward the values of frame-local variables into C
9612 variables so that the redisplay routines can access those values
9613 directly. */
9614
9615 static void
9616 select_frame_for_redisplay (frame)
9617 Lisp_Object frame;
9618 {
9619 Lisp_Object tail, sym, val;
9620 Lisp_Object old = selected_frame;
9621
9622 selected_frame = frame;
9623
9624 for (tail = XFRAME (frame)->param_alist; CONSP (tail); tail = XCDR (tail))
9625 if (CONSP (XCAR (tail))
9626 && (sym = XCAR (XCAR (tail)),
9627 SYMBOLP (sym))
9628 && (sym = indirect_variable (sym),
9629 val = SYMBOL_VALUE (sym),
9630 (BUFFER_LOCAL_VALUEP (val)
9631 || SOME_BUFFER_LOCAL_VALUEP (val)))
9632 && XBUFFER_LOCAL_VALUE (val)->check_frame)
9633 Fsymbol_value (sym);
9634
9635 for (tail = XFRAME (old)->param_alist; CONSP (tail); tail = XCDR (tail))
9636 if (CONSP (XCAR (tail))
9637 && (sym = XCAR (XCAR (tail)),
9638 SYMBOLP (sym))
9639 && (sym = indirect_variable (sym),
9640 val = SYMBOL_VALUE (sym),
9641 (BUFFER_LOCAL_VALUEP (val)
9642 || SOME_BUFFER_LOCAL_VALUEP (val)))
9643 && XBUFFER_LOCAL_VALUE (val)->check_frame)
9644 Fsymbol_value (sym);
9645 }
9646
9647
9648 #define STOP_POLLING \
9649 do { if (! polling_stopped_here) stop_polling (); \
9650 polling_stopped_here = 1; } while (0)
9651
9652 #define RESUME_POLLING \
9653 do { if (polling_stopped_here) start_polling (); \
9654 polling_stopped_here = 0; } while (0)
9655
9656
9657 /* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
9658 response to any user action; therefore, we should preserve the echo
9659 area. (Actually, our caller does that job.) Perhaps in the future
9660 avoid recentering windows if it is not necessary; currently that
9661 causes some problems. */
9662
9663 static void
9664 redisplay_internal (preserve_echo_area)
9665 int preserve_echo_area;
9666 {
9667 struct window *w = XWINDOW (selected_window);
9668 struct frame *f = XFRAME (w->frame);
9669 int pause;
9670 int must_finish = 0;
9671 struct text_pos tlbufpos, tlendpos;
9672 int number_of_visible_frames;
9673 int count;
9674 struct frame *sf = SELECTED_FRAME ();
9675 int polling_stopped_here = 0;
9676
9677 /* Non-zero means redisplay has to consider all windows on all
9678 frames. Zero means, only selected_window is considered. */
9679 int consider_all_windows_p;
9680
9681 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
9682
9683 /* No redisplay if running in batch mode or frame is not yet fully
9684 initialized, or redisplay is explicitly turned off by setting
9685 Vinhibit_redisplay. */
9686 if (noninteractive
9687 || !NILP (Vinhibit_redisplay)
9688 || !f->glyphs_initialized_p)
9689 return;
9690
9691 /* The flag redisplay_performed_directly_p is set by
9692 direct_output_for_insert when it already did the whole screen
9693 update necessary. */
9694 if (redisplay_performed_directly_p)
9695 {
9696 redisplay_performed_directly_p = 0;
9697 if (!hscroll_windows (selected_window))
9698 return;
9699 }
9700
9701 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
9702 if (popup_activated ())
9703 return;
9704 #endif
9705
9706 /* I don't think this happens but let's be paranoid. */
9707 if (redisplaying_p)
9708 return;
9709
9710 /* Record a function that resets redisplaying_p to its old value
9711 when we leave this function. */
9712 count = SPECPDL_INDEX ();
9713 record_unwind_protect (unwind_redisplay,
9714 Fcons (make_number (redisplaying_p), selected_frame));
9715 ++redisplaying_p;
9716 specbind (Qinhibit_free_realized_faces, Qnil);
9717
9718 retry:
9719 pause = 0;
9720 reconsider_clip_changes (w, current_buffer);
9721
9722 /* If new fonts have been loaded that make a glyph matrix adjustment
9723 necessary, do it. */
9724 if (fonts_changed_p)
9725 {
9726 adjust_glyphs (NULL);
9727 ++windows_or_buffers_changed;
9728 fonts_changed_p = 0;
9729 }
9730
9731 /* If face_change_count is non-zero, init_iterator will free all
9732 realized faces, which includes the faces referenced from current
9733 matrices. So, we can't reuse current matrices in this case. */
9734 if (face_change_count)
9735 ++windows_or_buffers_changed;
9736
9737 if (FRAME_TERMCAP_P (sf)
9738 && FRAME_TTY (sf)->previous_terminal_frame != sf)
9739 {
9740 /* Since frames on a single ASCII terminal share the same
9741 display area, displaying a different frame means redisplay
9742 the whole thing. */
9743 windows_or_buffers_changed++;
9744 SET_FRAME_GARBAGED (sf);
9745 FRAME_TTY (sf)->previous_terminal_frame = sf;
9746 }
9747
9748 /* Set the visible flags for all frames. Do this before checking
9749 for resized or garbaged frames; they want to know if their frames
9750 are visible. See the comment in frame.h for
9751 FRAME_SAMPLE_VISIBILITY. */
9752 {
9753 Lisp_Object tail, frame;
9754
9755 number_of_visible_frames = 0;
9756
9757 FOR_EACH_FRAME (tail, frame)
9758 {
9759 struct frame *f = XFRAME (frame);
9760
9761 FRAME_SAMPLE_VISIBILITY (f);
9762 if (FRAME_VISIBLE_P (f))
9763 ++number_of_visible_frames;
9764 clear_desired_matrices (f);
9765 }
9766 }
9767
9768
9769 /* Notice any pending interrupt request to change frame size. */
9770 do_pending_window_change (1);
9771
9772 /* Clear frames marked as garbaged. */
9773 if (frame_garbaged)
9774 clear_garbaged_frames ();
9775
9776 /* Build menubar and tool-bar items. */
9777 prepare_menu_bars ();
9778
9779 if (windows_or_buffers_changed)
9780 update_mode_lines++;
9781
9782 /* Detect case that we need to write or remove a star in the mode line. */
9783 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
9784 {
9785 w->update_mode_line = Qt;
9786 if (buffer_shared > 1)
9787 update_mode_lines++;
9788 }
9789
9790 /* If %c is in the mode line, update it if needed. */
9791 if (!NILP (w->column_number_displayed)
9792 /* This alternative quickly identifies a common case
9793 where no change is needed. */
9794 && !(PT == XFASTINT (w->last_point)
9795 && XFASTINT (w->last_modified) >= MODIFF
9796 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
9797 && (XFASTINT (w->column_number_displayed)
9798 != (int) current_column ())) /* iftc */
9799 w->update_mode_line = Qt;
9800
9801 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
9802
9803 /* The variable buffer_shared is set in redisplay_window and
9804 indicates that we redisplay a buffer in different windows. See
9805 there. */
9806 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
9807 || cursor_type_changed);
9808
9809 /* If specs for an arrow have changed, do thorough redisplay
9810 to ensure we remove any arrow that should no longer exist. */
9811 if (overlay_arrows_changed_p ())
9812 consider_all_windows_p = windows_or_buffers_changed = 1;
9813
9814 /* Normally the message* functions will have already displayed and
9815 updated the echo area, but the frame may have been trashed, or
9816 the update may have been preempted, so display the echo area
9817 again here. Checking message_cleared_p captures the case that
9818 the echo area should be cleared. */
9819 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
9820 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
9821 || (message_cleared_p
9822 && minibuf_level == 0
9823 /* If the mini-window is currently selected, this means the
9824 echo-area doesn't show through. */
9825 && !MINI_WINDOW_P (XWINDOW (selected_window))))
9826 {
9827 int window_height_changed_p = echo_area_display (0);
9828 must_finish = 1;
9829
9830 /* If we don't display the current message, don't clear the
9831 message_cleared_p flag, because, if we did, we wouldn't clear
9832 the echo area in the next redisplay which doesn't preserve
9833 the echo area. */
9834 if (!display_last_displayed_message_p)
9835 message_cleared_p = 0;
9836
9837 if (fonts_changed_p)
9838 goto retry;
9839 else if (window_height_changed_p)
9840 {
9841 consider_all_windows_p = 1;
9842 ++update_mode_lines;
9843 ++windows_or_buffers_changed;
9844
9845 /* If window configuration was changed, frames may have been
9846 marked garbaged. Clear them or we will experience
9847 surprises wrt scrolling. */
9848 if (frame_garbaged)
9849 clear_garbaged_frames ();
9850 }
9851 }
9852 else if (EQ (selected_window, minibuf_window)
9853 && (current_buffer->clip_changed
9854 || XFASTINT (w->last_modified) < MODIFF
9855 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
9856 && resize_mini_window (w, 0))
9857 {
9858 /* Resized active mini-window to fit the size of what it is
9859 showing if its contents might have changed. */
9860 must_finish = 1;
9861 consider_all_windows_p = 1;
9862 ++windows_or_buffers_changed;
9863 ++update_mode_lines;
9864
9865 /* If window configuration was changed, frames may have been
9866 marked garbaged. Clear them or we will experience
9867 surprises wrt scrolling. */
9868 if (frame_garbaged)
9869 clear_garbaged_frames ();
9870 }
9871
9872
9873 /* If showing the region, and mark has changed, we must redisplay
9874 the whole window. The assignment to this_line_start_pos prevents
9875 the optimization directly below this if-statement. */
9876 if (((!NILP (Vtransient_mark_mode)
9877 && !NILP (XBUFFER (w->buffer)->mark_active))
9878 != !NILP (w->region_showing))
9879 || (!NILP (w->region_showing)
9880 && !EQ (w->region_showing,
9881 Fmarker_position (XBUFFER (w->buffer)->mark))))
9882 CHARPOS (this_line_start_pos) = 0;
9883
9884 /* Optimize the case that only the line containing the cursor in the
9885 selected window has changed. Variables starting with this_ are
9886 set in display_line and record information about the line
9887 containing the cursor. */
9888 tlbufpos = this_line_start_pos;
9889 tlendpos = this_line_end_pos;
9890 if (!consider_all_windows_p
9891 && CHARPOS (tlbufpos) > 0
9892 && NILP (w->update_mode_line)
9893 && !current_buffer->clip_changed
9894 && !current_buffer->prevent_redisplay_optimizations_p
9895 && FRAME_VISIBLE_P (XFRAME (w->frame))
9896 && !FRAME_OBSCURED_P (XFRAME (w->frame))
9897 /* Make sure recorded data applies to current buffer, etc. */
9898 && this_line_buffer == current_buffer
9899 && current_buffer == XBUFFER (w->buffer)
9900 && NILP (w->force_start)
9901 && NILP (w->optional_new_start)
9902 /* Point must be on the line that we have info recorded about. */
9903 && PT >= CHARPOS (tlbufpos)
9904 && PT <= Z - CHARPOS (tlendpos)
9905 /* All text outside that line, including its final newline,
9906 must be unchanged */
9907 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
9908 CHARPOS (tlendpos)))
9909 {
9910 if (CHARPOS (tlbufpos) > BEGV
9911 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
9912 && (CHARPOS (tlbufpos) == ZV
9913 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
9914 /* Former continuation line has disappeared by becoming empty */
9915 goto cancel;
9916 else if (XFASTINT (w->last_modified) < MODIFF
9917 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
9918 || MINI_WINDOW_P (w))
9919 {
9920 /* We have to handle the case of continuation around a
9921 wide-column character (See the comment in indent.c around
9922 line 885).
9923
9924 For instance, in the following case:
9925
9926 -------- Insert --------
9927 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
9928 J_I_ ==> J_I_ `^^' are cursors.
9929 ^^ ^^
9930 -------- --------
9931
9932 As we have to redraw the line above, we should goto cancel. */
9933
9934 struct it it;
9935 int line_height_before = this_line_pixel_height;
9936
9937 /* Note that start_display will handle the case that the
9938 line starting at tlbufpos is a continuation lines. */
9939 start_display (&it, w, tlbufpos);
9940
9941 /* Implementation note: It this still necessary? */
9942 if (it.current_x != this_line_start_x)
9943 goto cancel;
9944
9945 TRACE ((stderr, "trying display optimization 1\n"));
9946 w->cursor.vpos = -1;
9947 overlay_arrow_seen = 0;
9948 it.vpos = this_line_vpos;
9949 it.current_y = this_line_y;
9950 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
9951 display_line (&it);
9952
9953 /* If line contains point, is not continued,
9954 and ends at same distance from eob as before, we win */
9955 if (w->cursor.vpos >= 0
9956 /* Line is not continued, otherwise this_line_start_pos
9957 would have been set to 0 in display_line. */
9958 && CHARPOS (this_line_start_pos)
9959 /* Line ends as before. */
9960 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
9961 /* Line has same height as before. Otherwise other lines
9962 would have to be shifted up or down. */
9963 && this_line_pixel_height == line_height_before)
9964 {
9965 /* If this is not the window's last line, we must adjust
9966 the charstarts of the lines below. */
9967 if (it.current_y < it.last_visible_y)
9968 {
9969 struct glyph_row *row
9970 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
9971 int delta, delta_bytes;
9972
9973 if (Z - CHARPOS (tlendpos) == ZV)
9974 {
9975 /* This line ends at end of (accessible part of)
9976 buffer. There is no newline to count. */
9977 delta = (Z
9978 - CHARPOS (tlendpos)
9979 - MATRIX_ROW_START_CHARPOS (row));
9980 delta_bytes = (Z_BYTE
9981 - BYTEPOS (tlendpos)
9982 - MATRIX_ROW_START_BYTEPOS (row));
9983 }
9984 else
9985 {
9986 /* This line ends in a newline. Must take
9987 account of the newline and the rest of the
9988 text that follows. */
9989 delta = (Z
9990 - CHARPOS (tlendpos)
9991 - MATRIX_ROW_START_CHARPOS (row));
9992 delta_bytes = (Z_BYTE
9993 - BYTEPOS (tlendpos)
9994 - MATRIX_ROW_START_BYTEPOS (row));
9995 }
9996
9997 increment_matrix_positions (w->current_matrix,
9998 this_line_vpos + 1,
9999 w->current_matrix->nrows,
10000 delta, delta_bytes);
10001 }
10002
10003 /* If this row displays text now but previously didn't,
10004 or vice versa, w->window_end_vpos may have to be
10005 adjusted. */
10006 if ((it.glyph_row - 1)->displays_text_p)
10007 {
10008 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
10009 XSETINT (w->window_end_vpos, this_line_vpos);
10010 }
10011 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
10012 && this_line_vpos > 0)
10013 XSETINT (w->window_end_vpos, this_line_vpos - 1);
10014 w->window_end_valid = Qnil;
10015
10016 /* Update hint: No need to try to scroll in update_window. */
10017 w->desired_matrix->no_scrolling_p = 1;
10018
10019 #if GLYPH_DEBUG
10020 *w->desired_matrix->method = 0;
10021 debug_method_add (w, "optimization 1");
10022 #endif
10023 #ifdef HAVE_WINDOW_SYSTEM
10024 update_window_fringes (w, 0);
10025 #endif
10026 goto update;
10027 }
10028 else
10029 goto cancel;
10030 }
10031 else if (/* Cursor position hasn't changed. */
10032 PT == XFASTINT (w->last_point)
10033 /* Make sure the cursor was last displayed
10034 in this window. Otherwise we have to reposition it. */
10035 && 0 <= w->cursor.vpos
10036 && WINDOW_TOTAL_LINES (w) > w->cursor.vpos)
10037 {
10038 if (!must_finish)
10039 {
10040 do_pending_window_change (1);
10041
10042 /* We used to always goto end_of_redisplay here, but this
10043 isn't enough if we have a blinking cursor. */
10044 if (w->cursor_off_p == w->last_cursor_off_p)
10045 goto end_of_redisplay;
10046 }
10047 goto update;
10048 }
10049 /* If highlighting the region, or if the cursor is in the echo area,
10050 then we can't just move the cursor. */
10051 else if (! (!NILP (Vtransient_mark_mode)
10052 && !NILP (current_buffer->mark_active))
10053 && (EQ (selected_window, current_buffer->last_selected_window)
10054 || highlight_nonselected_windows)
10055 && NILP (w->region_showing)
10056 && NILP (Vshow_trailing_whitespace)
10057 && !cursor_in_echo_area)
10058 {
10059 struct it it;
10060 struct glyph_row *row;
10061
10062 /* Skip from tlbufpos to PT and see where it is. Note that
10063 PT may be in invisible text. If so, we will end at the
10064 next visible position. */
10065 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
10066 NULL, DEFAULT_FACE_ID);
10067 it.current_x = this_line_start_x;
10068 it.current_y = this_line_y;
10069 it.vpos = this_line_vpos;
10070
10071 /* The call to move_it_to stops in front of PT, but
10072 moves over before-strings. */
10073 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
10074
10075 if (it.vpos == this_line_vpos
10076 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
10077 row->enabled_p))
10078 {
10079 xassert (this_line_vpos == it.vpos);
10080 xassert (this_line_y == it.current_y);
10081 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
10082 #if GLYPH_DEBUG
10083 *w->desired_matrix->method = 0;
10084 debug_method_add (w, "optimization 3");
10085 #endif
10086 goto update;
10087 }
10088 else
10089 goto cancel;
10090 }
10091
10092 cancel:
10093 /* Text changed drastically or point moved off of line. */
10094 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
10095 }
10096
10097 CHARPOS (this_line_start_pos) = 0;
10098 consider_all_windows_p |= buffer_shared > 1;
10099 ++clear_face_cache_count;
10100
10101
10102 /* Build desired matrices, and update the display. If
10103 consider_all_windows_p is non-zero, do it for all windows on all
10104 frames. Otherwise do it for selected_window, only. */
10105
10106 if (consider_all_windows_p)
10107 {
10108 Lisp_Object tail, frame;
10109 int i, n = 0, size = 50;
10110 struct frame **updated
10111 = (struct frame **) alloca (size * sizeof *updated);
10112
10113 /* Clear the face cache eventually. */
10114 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
10115 {
10116 clear_face_cache (0);
10117 clear_face_cache_count = 0;
10118 }
10119
10120 /* Recompute # windows showing selected buffer. This will be
10121 incremented each time such a window is displayed. */
10122 buffer_shared = 0;
10123
10124 FOR_EACH_FRAME (tail, frame)
10125 {
10126 struct frame *f = XFRAME (frame);
10127
10128 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
10129 {
10130 if (! EQ (frame, selected_frame))
10131 /* Select the frame, for the sake of frame-local
10132 variables. */
10133 select_frame_for_redisplay (frame);
10134
10135 #ifdef HAVE_WINDOW_SYSTEM
10136 if (clear_face_cache_count % 50 == 0
10137 && FRAME_WINDOW_P (f))
10138 clear_image_cache (f, 0);
10139 #endif /* HAVE_WINDOW_SYSTEM */
10140
10141 /* Mark all the scroll bars to be removed; we'll redeem
10142 the ones we want when we redisplay their windows. */
10143 if (FRAME_DISPLAY (f)->condemn_scroll_bars_hook)
10144 FRAME_DISPLAY (f)->condemn_scroll_bars_hook (f);
10145
10146 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
10147 redisplay_windows (FRAME_ROOT_WINDOW (f));
10148
10149 /* Any scroll bars which redisplay_windows should have
10150 nuked should now go away. */
10151 if (FRAME_DISPLAY (f)->judge_scroll_bars_hook)
10152 FRAME_DISPLAY (f)->judge_scroll_bars_hook (f);
10153
10154 /* If fonts changed, display again. */
10155 /* ??? rms: I suspect it is a mistake to jump all the way
10156 back to retry here. It should just retry this frame. */
10157 if (fonts_changed_p)
10158 goto retry;
10159
10160 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
10161 {
10162 /* See if we have to hscroll. */
10163 if (hscroll_windows (f->root_window))
10164 goto retry;
10165
10166 /* Prevent various kinds of signals during display
10167 update. stdio is not robust about handling
10168 signals, which can cause an apparent I/O
10169 error. */
10170 if (interrupt_input)
10171 unrequest_sigio ();
10172 STOP_POLLING;
10173
10174 /* Update the display. */
10175 set_window_update_flags (XWINDOW (f->root_window), 1);
10176 pause |= update_frame (f, 0, 0);
10177 #if 0 /* Exiting the loop can leave the wrong value for buffer_shared. */
10178 if (pause)
10179 break;
10180 #endif
10181
10182 if (n == size)
10183 {
10184 int nbytes = size * sizeof *updated;
10185 struct frame **p = (struct frame **) alloca (2 * nbytes);
10186 bcopy (updated, p, nbytes);
10187 size *= 2;
10188 }
10189
10190 updated[n++] = f;
10191 }
10192 }
10193 }
10194
10195 if (!pause)
10196 {
10197 /* Do the mark_window_display_accurate after all windows have
10198 been redisplayed because this call resets flags in buffers
10199 which are needed for proper redisplay. */
10200 for (i = 0; i < n; ++i)
10201 {
10202 struct frame *f = updated[i];
10203 mark_window_display_accurate (f->root_window, 1);
10204 if (FRAME_DISPLAY (f)->frame_up_to_date_hook)
10205 FRAME_DISPLAY (f)->frame_up_to_date_hook (f);
10206 }
10207 }
10208 }
10209 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
10210 {
10211 Lisp_Object mini_window;
10212 struct frame *mini_frame;
10213
10214 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
10215 /* Use list_of_error, not Qerror, so that
10216 we catch only errors and don't run the debugger. */
10217 internal_condition_case_1 (redisplay_window_1, selected_window,
10218 list_of_error,
10219 redisplay_window_error);
10220
10221 /* Compare desired and current matrices, perform output. */
10222
10223 update:
10224 /* If fonts changed, display again. */
10225 if (fonts_changed_p)
10226 goto retry;
10227
10228 /* Prevent various kinds of signals during display update.
10229 stdio is not robust about handling signals,
10230 which can cause an apparent I/O error. */
10231 if (interrupt_input)
10232 unrequest_sigio ();
10233 STOP_POLLING;
10234
10235 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
10236 {
10237 if (hscroll_windows (selected_window))
10238 goto retry;
10239
10240 XWINDOW (selected_window)->must_be_updated_p = 1;
10241 pause = update_frame (sf, 0, 0);
10242 }
10243
10244 /* We may have called echo_area_display at the top of this
10245 function. If the echo area is on another frame, that may
10246 have put text on a frame other than the selected one, so the
10247 above call to update_frame would not have caught it. Catch
10248 it here. */
10249 mini_window = FRAME_MINIBUF_WINDOW (sf);
10250 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
10251
10252 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
10253 {
10254 XWINDOW (mini_window)->must_be_updated_p = 1;
10255 pause |= update_frame (mini_frame, 0, 0);
10256 if (!pause && hscroll_windows (mini_window))
10257 goto retry;
10258 }
10259 }
10260
10261 /* If display was paused because of pending input, make sure we do a
10262 thorough update the next time. */
10263 if (pause)
10264 {
10265 /* Prevent the optimization at the beginning of
10266 redisplay_internal that tries a single-line update of the
10267 line containing the cursor in the selected window. */
10268 CHARPOS (this_line_start_pos) = 0;
10269
10270 /* Let the overlay arrow be updated the next time. */
10271 update_overlay_arrows (0);
10272
10273 /* If we pause after scrolling, some rows in the current
10274 matrices of some windows are not valid. */
10275 if (!WINDOW_FULL_WIDTH_P (w)
10276 && !FRAME_WINDOW_P (XFRAME (w->frame)))
10277 update_mode_lines = 1;
10278 }
10279 else
10280 {
10281 if (!consider_all_windows_p)
10282 {
10283 /* This has already been done above if
10284 consider_all_windows_p is set. */
10285 mark_window_display_accurate_1 (w, 1);
10286
10287 /* Say overlay arrows are up to date. */
10288 update_overlay_arrows (1);
10289
10290 if (FRAME_DISPLAY (sf)->frame_up_to_date_hook != 0)
10291 FRAME_DISPLAY (sf)->frame_up_to_date_hook (sf);
10292 }
10293
10294 update_mode_lines = 0;
10295 windows_or_buffers_changed = 0;
10296 cursor_type_changed = 0;
10297 }
10298
10299 /* Start SIGIO interrupts coming again. Having them off during the
10300 code above makes it less likely one will discard output, but not
10301 impossible, since there might be stuff in the system buffer here.
10302 But it is much hairier to try to do anything about that. */
10303 if (interrupt_input)
10304 request_sigio ();
10305 RESUME_POLLING;
10306
10307 /* If a frame has become visible which was not before, redisplay
10308 again, so that we display it. Expose events for such a frame
10309 (which it gets when becoming visible) don't call the parts of
10310 redisplay constructing glyphs, so simply exposing a frame won't
10311 display anything in this case. So, we have to display these
10312 frames here explicitly. */
10313 if (!pause)
10314 {
10315 Lisp_Object tail, frame;
10316 int new_count = 0;
10317
10318 FOR_EACH_FRAME (tail, frame)
10319 {
10320 int this_is_visible = 0;
10321
10322 if (XFRAME (frame)->visible)
10323 this_is_visible = 1;
10324 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
10325 if (XFRAME (frame)->visible)
10326 this_is_visible = 1;
10327
10328 if (this_is_visible)
10329 new_count++;
10330 }
10331
10332 if (new_count != number_of_visible_frames)
10333 windows_or_buffers_changed++;
10334 }
10335
10336 /* Change frame size now if a change is pending. */
10337 do_pending_window_change (1);
10338
10339 /* If we just did a pending size change, or have additional
10340 visible frames, redisplay again. */
10341 if (windows_or_buffers_changed && !pause)
10342 goto retry;
10343
10344 end_of_redisplay:
10345 unbind_to (count, Qnil);
10346 RESUME_POLLING;
10347 }
10348
10349
10350 /* Redisplay, but leave alone any recent echo area message unless
10351 another message has been requested in its place.
10352
10353 This is useful in situations where you need to redisplay but no
10354 user action has occurred, making it inappropriate for the message
10355 area to be cleared. See tracking_off and
10356 wait_reading_process_input for examples of these situations.
10357
10358 FROM_WHERE is an integer saying from where this function was
10359 called. This is useful for debugging. */
10360
10361 void
10362 redisplay_preserve_echo_area (from_where)
10363 int from_where;
10364 {
10365 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
10366
10367 if (!NILP (echo_area_buffer[1]))
10368 {
10369 /* We have a previously displayed message, but no current
10370 message. Redisplay the previous message. */
10371 display_last_displayed_message_p = 1;
10372 redisplay_internal (1);
10373 display_last_displayed_message_p = 0;
10374 }
10375 else
10376 redisplay_internal (1);
10377 }
10378
10379
10380 /* Function registered with record_unwind_protect in
10381 redisplay_internal. Reset redisplaying_p to the value it had
10382 before redisplay_internal was called, and clear
10383 prevent_freeing_realized_faces_p. It also selects the previously
10384 selected frame. */
10385
10386 static Lisp_Object
10387 unwind_redisplay (val)
10388 Lisp_Object val;
10389 {
10390 Lisp_Object old_redisplaying_p, old_frame;
10391
10392 old_redisplaying_p = XCAR (val);
10393 redisplaying_p = XFASTINT (old_redisplaying_p);
10394 old_frame = XCDR (val);
10395 if (! EQ (old_frame, selected_frame))
10396 select_frame_for_redisplay (old_frame);
10397 return Qnil;
10398 }
10399
10400
10401 /* Mark the display of window W as accurate or inaccurate. If
10402 ACCURATE_P is non-zero mark display of W as accurate. If
10403 ACCURATE_P is zero, arrange for W to be redisplayed the next time
10404 redisplay_internal is called. */
10405
10406 static void
10407 mark_window_display_accurate_1 (w, accurate_p)
10408 struct window *w;
10409 int accurate_p;
10410 {
10411 if (BUFFERP (w->buffer))
10412 {
10413 struct buffer *b = XBUFFER (w->buffer);
10414
10415 w->last_modified
10416 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
10417 w->last_overlay_modified
10418 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
10419 w->last_had_star
10420 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
10421
10422 if (accurate_p)
10423 {
10424 b->clip_changed = 0;
10425 b->prevent_redisplay_optimizations_p = 0;
10426
10427 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
10428 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
10429 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
10430 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
10431
10432 w->current_matrix->buffer = b;
10433 w->current_matrix->begv = BUF_BEGV (b);
10434 w->current_matrix->zv = BUF_ZV (b);
10435
10436 w->last_cursor = w->cursor;
10437 w->last_cursor_off_p = w->cursor_off_p;
10438
10439 if (w == XWINDOW (selected_window))
10440 w->last_point = make_number (BUF_PT (b));
10441 else
10442 w->last_point = make_number (XMARKER (w->pointm)->charpos);
10443 }
10444 }
10445
10446 if (accurate_p)
10447 {
10448 w->window_end_valid = w->buffer;
10449 #if 0 /* This is incorrect with variable-height lines. */
10450 xassert (XINT (w->window_end_vpos)
10451 < (WINDOW_TOTAL_LINES (w)
10452 - (WINDOW_WANTS_MODELINE_P (w) ? 1 : 0)));
10453 #endif
10454 w->update_mode_line = Qnil;
10455 }
10456 }
10457
10458
10459 /* Mark the display of windows in the window tree rooted at WINDOW as
10460 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
10461 windows as accurate. If ACCURATE_P is zero, arrange for windows to
10462 be redisplayed the next time redisplay_internal is called. */
10463
10464 void
10465 mark_window_display_accurate (window, accurate_p)
10466 Lisp_Object window;
10467 int accurate_p;
10468 {
10469 struct window *w;
10470
10471 for (; !NILP (window); window = w->next)
10472 {
10473 w = XWINDOW (window);
10474 mark_window_display_accurate_1 (w, accurate_p);
10475
10476 if (!NILP (w->vchild))
10477 mark_window_display_accurate (w->vchild, accurate_p);
10478 if (!NILP (w->hchild))
10479 mark_window_display_accurate (w->hchild, accurate_p);
10480 }
10481
10482 if (accurate_p)
10483 {
10484 update_overlay_arrows (1);
10485 }
10486 else
10487 {
10488 /* Force a thorough redisplay the next time by setting
10489 last_arrow_position and last_arrow_string to t, which is
10490 unequal to any useful value of Voverlay_arrow_... */
10491 update_overlay_arrows (-1);
10492 }
10493 }
10494
10495
10496 /* Return value in display table DP (Lisp_Char_Table *) for character
10497 C. Since a display table doesn't have any parent, we don't have to
10498 follow parent. Do not call this function directly but use the
10499 macro DISP_CHAR_VECTOR. */
10500
10501 Lisp_Object
10502 disp_char_vector (dp, c)
10503 struct Lisp_Char_Table *dp;
10504 int c;
10505 {
10506 int code[4], i;
10507 Lisp_Object val;
10508
10509 if (SINGLE_BYTE_CHAR_P (c))
10510 return (dp->contents[c]);
10511
10512 SPLIT_CHAR (c, code[0], code[1], code[2]);
10513 if (code[1] < 32)
10514 code[1] = -1;
10515 else if (code[2] < 32)
10516 code[2] = -1;
10517
10518 /* Here, the possible range of code[0] (== charset ID) is
10519 128..max_charset. Since the top level char table contains data
10520 for multibyte characters after 256th element, we must increment
10521 code[0] by 128 to get a correct index. */
10522 code[0] += 128;
10523 code[3] = -1; /* anchor */
10524
10525 for (i = 0; code[i] >= 0; i++, dp = XCHAR_TABLE (val))
10526 {
10527 val = dp->contents[code[i]];
10528 if (!SUB_CHAR_TABLE_P (val))
10529 return (NILP (val) ? dp->defalt : val);
10530 }
10531
10532 /* Here, val is a sub char table. We return the default value of
10533 it. */
10534 return (dp->defalt);
10535 }
10536
10537
10538 \f
10539 /***********************************************************************
10540 Window Redisplay
10541 ***********************************************************************/
10542
10543 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
10544
10545 static void
10546 redisplay_windows (window)
10547 Lisp_Object window;
10548 {
10549 while (!NILP (window))
10550 {
10551 struct window *w = XWINDOW (window);
10552
10553 if (!NILP (w->hchild))
10554 redisplay_windows (w->hchild);
10555 else if (!NILP (w->vchild))
10556 redisplay_windows (w->vchild);
10557 else
10558 {
10559 displayed_buffer = XBUFFER (w->buffer);
10560 /* Use list_of_error, not Qerror, so that
10561 we catch only errors and don't run the debugger. */
10562 internal_condition_case_1 (redisplay_window_0, window,
10563 list_of_error,
10564 redisplay_window_error);
10565 }
10566
10567 window = w->next;
10568 }
10569 }
10570
10571 static Lisp_Object
10572 redisplay_window_error ()
10573 {
10574 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
10575 return Qnil;
10576 }
10577
10578 static Lisp_Object
10579 redisplay_window_0 (window)
10580 Lisp_Object window;
10581 {
10582 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
10583 redisplay_window (window, 0);
10584 return Qnil;
10585 }
10586
10587 static Lisp_Object
10588 redisplay_window_1 (window)
10589 Lisp_Object window;
10590 {
10591 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
10592 redisplay_window (window, 1);
10593 return Qnil;
10594 }
10595 \f
10596
10597 /* Increment GLYPH until it reaches END or CONDITION fails while
10598 adding (GLYPH)->pixel_width to X. */
10599
10600 #define SKIP_GLYPHS(glyph, end, x, condition) \
10601 do \
10602 { \
10603 (x) += (glyph)->pixel_width; \
10604 ++(glyph); \
10605 } \
10606 while ((glyph) < (end) && (condition))
10607
10608
10609 /* Set cursor position of W. PT is assumed to be displayed in ROW.
10610 DELTA is the number of bytes by which positions recorded in ROW
10611 differ from current buffer positions. */
10612
10613 void
10614 set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
10615 struct window *w;
10616 struct glyph_row *row;
10617 struct glyph_matrix *matrix;
10618 int delta, delta_bytes, dy, dvpos;
10619 {
10620 struct glyph *glyph = row->glyphs[TEXT_AREA];
10621 struct glyph *end = glyph + row->used[TEXT_AREA];
10622 /* The first glyph that starts a sequence of glyphs from string. */
10623 struct glyph *string_start;
10624 /* The X coordinate of string_start. */
10625 int string_start_x;
10626 /* The last known character position. */
10627 int last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
10628 /* The last known character position before string_start. */
10629 int string_before_pos;
10630 int x = row->x;
10631 int pt_old = PT - delta;
10632
10633 /* Skip over glyphs not having an object at the start of the row.
10634 These are special glyphs like truncation marks on terminal
10635 frames. */
10636 if (row->displays_text_p)
10637 while (glyph < end
10638 && INTEGERP (glyph->object)
10639 && glyph->charpos < 0)
10640 {
10641 x += glyph->pixel_width;
10642 ++glyph;
10643 }
10644
10645 string_start = NULL;
10646 while (glyph < end
10647 && !INTEGERP (glyph->object)
10648 && (!BUFFERP (glyph->object)
10649 || (last_pos = glyph->charpos) < pt_old))
10650 {
10651 if (! STRINGP (glyph->object))
10652 {
10653 string_start = NULL;
10654 x += glyph->pixel_width;
10655 ++glyph;
10656 }
10657 else
10658 {
10659 string_before_pos = last_pos;
10660 string_start = glyph;
10661 string_start_x = x;
10662 /* Skip all glyphs from string. */
10663 SKIP_GLYPHS (glyph, end, x, STRINGP (glyph->object));
10664 }
10665 }
10666
10667 if (string_start
10668 && (glyph == end || !BUFFERP (glyph->object) || last_pos > pt_old))
10669 {
10670 /* We may have skipped over point because the previous glyphs
10671 are from string. As there's no easy way to know the
10672 character position of the current glyph, find the correct
10673 glyph on point by scanning from string_start again. */
10674 Lisp_Object limit;
10675 Lisp_Object string;
10676 int pos;
10677
10678 limit = make_number (pt_old + 1);
10679 end = glyph;
10680 glyph = string_start;
10681 x = string_start_x;
10682 string = glyph->object;
10683 pos = string_buffer_position (w, string, string_before_pos);
10684 /* If STRING is from overlay, LAST_POS == 0. We skip such glyphs
10685 because we always put cursor after overlay strings. */
10686 while (pos == 0 && glyph < end)
10687 {
10688 string = glyph->object;
10689 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
10690 if (glyph < end)
10691 pos = string_buffer_position (w, glyph->object, string_before_pos);
10692 }
10693
10694 while (glyph < end)
10695 {
10696 pos = XINT (Fnext_single_char_property_change
10697 (make_number (pos), Qdisplay, Qnil, limit));
10698 if (pos > pt_old)
10699 break;
10700 /* Skip glyphs from the same string. */
10701 string = glyph->object;
10702 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
10703 /* Skip glyphs from an overlay. */
10704 while (glyph < end
10705 && ! string_buffer_position (w, glyph->object, pos))
10706 {
10707 string = glyph->object;
10708 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
10709 }
10710 }
10711 }
10712
10713 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
10714 w->cursor.x = x;
10715 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
10716 w->cursor.y = row->y + dy;
10717
10718 if (w == XWINDOW (selected_window))
10719 {
10720 if (!row->continued_p
10721 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
10722 && row->x == 0)
10723 {
10724 this_line_buffer = XBUFFER (w->buffer);
10725
10726 CHARPOS (this_line_start_pos)
10727 = MATRIX_ROW_START_CHARPOS (row) + delta;
10728 BYTEPOS (this_line_start_pos)
10729 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
10730
10731 CHARPOS (this_line_end_pos)
10732 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
10733 BYTEPOS (this_line_end_pos)
10734 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
10735
10736 this_line_y = w->cursor.y;
10737 this_line_pixel_height = row->height;
10738 this_line_vpos = w->cursor.vpos;
10739 this_line_start_x = row->x;
10740 }
10741 else
10742 CHARPOS (this_line_start_pos) = 0;
10743 }
10744 }
10745
10746
10747 /* Run window scroll functions, if any, for WINDOW with new window
10748 start STARTP. Sets the window start of WINDOW to that position.
10749
10750 We assume that the window's buffer is really current. */
10751
10752 static INLINE struct text_pos
10753 run_window_scroll_functions (window, startp)
10754 Lisp_Object window;
10755 struct text_pos startp;
10756 {
10757 struct window *w = XWINDOW (window);
10758 SET_MARKER_FROM_TEXT_POS (w->start, startp);
10759
10760 if (current_buffer != XBUFFER (w->buffer))
10761 abort ();
10762
10763 if (!NILP (Vwindow_scroll_functions))
10764 {
10765 run_hook_with_args_2 (Qwindow_scroll_functions, window,
10766 make_number (CHARPOS (startp)));
10767 SET_TEXT_POS_FROM_MARKER (startp, w->start);
10768 /* In case the hook functions switch buffers. */
10769 if (current_buffer != XBUFFER (w->buffer))
10770 set_buffer_internal_1 (XBUFFER (w->buffer));
10771 }
10772
10773 return startp;
10774 }
10775
10776
10777 /* Make sure the line containing the cursor is fully visible.
10778 A value of 1 means there is nothing to be done.
10779 (Either the line is fully visible, or it cannot be made so,
10780 or we cannot tell.)
10781
10782 If FORCE_P is non-zero, return 0 even if partial visible cursor row
10783 is higher than window.
10784
10785 A value of 0 means the caller should do scrolling
10786 as if point had gone off the screen. */
10787
10788 static int
10789 make_cursor_line_fully_visible (w, force_p)
10790 struct window *w;
10791 int force_p;
10792 {
10793 struct glyph_matrix *matrix;
10794 struct glyph_row *row;
10795 int window_height;
10796
10797 /* It's not always possible to find the cursor, e.g, when a window
10798 is full of overlay strings. Don't do anything in that case. */
10799 if (w->cursor.vpos < 0)
10800 return 1;
10801
10802 matrix = w->desired_matrix;
10803 row = MATRIX_ROW (matrix, w->cursor.vpos);
10804
10805 /* If the cursor row is not partially visible, there's nothing to do. */
10806 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (row))
10807 return 1;
10808
10809 /* If the row the cursor is in is taller than the window's height,
10810 it's not clear what to do, so do nothing. */
10811 window_height = window_box_height (w);
10812 if (row->height >= window_height)
10813 {
10814 if (!force_p || w->vscroll)
10815 return 1;
10816 }
10817 return 0;
10818
10819 #if 0
10820 /* This code used to try to scroll the window just enough to make
10821 the line visible. It returned 0 to say that the caller should
10822 allocate larger glyph matrices. */
10823
10824 if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (w, row))
10825 {
10826 int dy = row->height - row->visible_height;
10827 w->vscroll = 0;
10828 w->cursor.y += dy;
10829 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
10830 }
10831 else /* MATRIX_ROW_PARTIALLY_VISIBLE_AT_BOTTOM_P (w, row)) */
10832 {
10833 int dy = - (row->height - row->visible_height);
10834 w->vscroll = dy;
10835 w->cursor.y += dy;
10836 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
10837 }
10838
10839 /* When we change the cursor y-position of the selected window,
10840 change this_line_y as well so that the display optimization for
10841 the cursor line of the selected window in redisplay_internal uses
10842 the correct y-position. */
10843 if (w == XWINDOW (selected_window))
10844 this_line_y = w->cursor.y;
10845
10846 /* If vscrolling requires a larger glyph matrix, arrange for a fresh
10847 redisplay with larger matrices. */
10848 if (matrix->nrows < required_matrix_height (w))
10849 {
10850 fonts_changed_p = 1;
10851 return 0;
10852 }
10853
10854 return 1;
10855 #endif /* 0 */
10856 }
10857
10858
10859 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
10860 non-zero means only WINDOW is redisplayed in redisplay_internal.
10861 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
10862 in redisplay_window to bring a partially visible line into view in
10863 the case that only the cursor has moved.
10864
10865 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
10866 last screen line's vertical height extends past the end of the screen.
10867
10868 Value is
10869
10870 1 if scrolling succeeded
10871
10872 0 if scrolling didn't find point.
10873
10874 -1 if new fonts have been loaded so that we must interrupt
10875 redisplay, adjust glyph matrices, and try again. */
10876
10877 enum
10878 {
10879 SCROLLING_SUCCESS,
10880 SCROLLING_FAILED,
10881 SCROLLING_NEED_LARGER_MATRICES
10882 };
10883
10884 static int
10885 try_scrolling (window, just_this_one_p, scroll_conservatively,
10886 scroll_step, temp_scroll_step, last_line_misfit)
10887 Lisp_Object window;
10888 int just_this_one_p;
10889 EMACS_INT scroll_conservatively, scroll_step;
10890 int temp_scroll_step;
10891 int last_line_misfit;
10892 {
10893 struct window *w = XWINDOW (window);
10894 struct frame *f = XFRAME (w->frame);
10895 struct text_pos scroll_margin_pos;
10896 struct text_pos pos;
10897 struct text_pos startp;
10898 struct it it;
10899 Lisp_Object window_end;
10900 int this_scroll_margin;
10901 int dy = 0;
10902 int scroll_max;
10903 int rc;
10904 int amount_to_scroll = 0;
10905 Lisp_Object aggressive;
10906 int height;
10907 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
10908
10909 #if GLYPH_DEBUG
10910 debug_method_add (w, "try_scrolling");
10911 #endif
10912
10913 SET_TEXT_POS_FROM_MARKER (startp, w->start);
10914
10915 /* Compute scroll margin height in pixels. We scroll when point is
10916 within this distance from the top or bottom of the window. */
10917 if (scroll_margin > 0)
10918 {
10919 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
10920 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
10921 }
10922 else
10923 this_scroll_margin = 0;
10924
10925 /* Force scroll_conservatively to have a reasonable value so it doesn't
10926 cause an overflow while computing how much to scroll. */
10927 if (scroll_conservatively)
10928 scroll_conservatively = min (scroll_conservatively,
10929 MOST_POSITIVE_FIXNUM / FRAME_LINE_HEIGHT (f));
10930
10931 /* Compute how much we should try to scroll maximally to bring point
10932 into view. */
10933 if (scroll_step || scroll_conservatively || temp_scroll_step)
10934 scroll_max = max (scroll_step,
10935 max (scroll_conservatively, temp_scroll_step));
10936 else if (NUMBERP (current_buffer->scroll_down_aggressively)
10937 || NUMBERP (current_buffer->scroll_up_aggressively))
10938 /* We're trying to scroll because of aggressive scrolling
10939 but no scroll_step is set. Choose an arbitrary one. Maybe
10940 there should be a variable for this. */
10941 scroll_max = 10;
10942 else
10943 scroll_max = 0;
10944 scroll_max *= FRAME_LINE_HEIGHT (f);
10945
10946 /* Decide whether we have to scroll down. Start at the window end
10947 and move this_scroll_margin up to find the position of the scroll
10948 margin. */
10949 window_end = Fwindow_end (window, Qt);
10950
10951 too_near_end:
10952
10953 CHARPOS (scroll_margin_pos) = XINT (window_end);
10954 BYTEPOS (scroll_margin_pos) = CHAR_TO_BYTE (CHARPOS (scroll_margin_pos));
10955
10956 if (this_scroll_margin || extra_scroll_margin_lines)
10957 {
10958 start_display (&it, w, scroll_margin_pos);
10959 if (this_scroll_margin)
10960 move_it_vertically (&it, - this_scroll_margin);
10961 if (extra_scroll_margin_lines)
10962 move_it_by_lines (&it, - extra_scroll_margin_lines, 0);
10963 scroll_margin_pos = it.current.pos;
10964 }
10965
10966 if (PT >= CHARPOS (scroll_margin_pos))
10967 {
10968 int y0;
10969
10970 /* Point is in the scroll margin at the bottom of the window, or
10971 below. Compute a new window start that makes point visible. */
10972
10973 /* Compute the distance from the scroll margin to PT.
10974 Give up if the distance is greater than scroll_max. */
10975 start_display (&it, w, scroll_margin_pos);
10976 y0 = it.current_y;
10977 move_it_to (&it, PT, 0, it.last_visible_y, -1,
10978 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
10979
10980 /* To make point visible, we have to move the window start
10981 down so that the line the cursor is in is visible, which
10982 means we have to add in the height of the cursor line. */
10983 dy = line_bottom_y (&it) - y0;
10984
10985 if (dy > scroll_max)
10986 return SCROLLING_FAILED;
10987
10988 /* Move the window start down. If scrolling conservatively,
10989 move it just enough down to make point visible. If
10990 scroll_step is set, move it down by scroll_step. */
10991 start_display (&it, w, startp);
10992
10993 if (scroll_conservatively)
10994 /* Set AMOUNT_TO_SCROLL to at least one line,
10995 and at most scroll_conservatively lines. */
10996 amount_to_scroll
10997 = min (max (dy, FRAME_LINE_HEIGHT (f)),
10998 FRAME_LINE_HEIGHT (f) * scroll_conservatively);
10999 else if (scroll_step || temp_scroll_step)
11000 amount_to_scroll = scroll_max;
11001 else
11002 {
11003 aggressive = current_buffer->scroll_up_aggressively;
11004 height = WINDOW_BOX_TEXT_HEIGHT (w);
11005 if (NUMBERP (aggressive))
11006 {
11007 double float_amount = XFLOATINT (aggressive) * height;
11008 amount_to_scroll = float_amount;
11009 if (amount_to_scroll == 0 && float_amount > 0)
11010 amount_to_scroll = 1;
11011 }
11012 }
11013
11014 if (amount_to_scroll <= 0)
11015 return SCROLLING_FAILED;
11016
11017 /* If moving by amount_to_scroll leaves STARTP unchanged,
11018 move it down one screen line. */
11019
11020 move_it_vertically (&it, amount_to_scroll);
11021 if (CHARPOS (it.current.pos) == CHARPOS (startp))
11022 move_it_by_lines (&it, 1, 1);
11023 startp = it.current.pos;
11024 }
11025 else
11026 {
11027 /* See if point is inside the scroll margin at the top of the
11028 window. */
11029 scroll_margin_pos = startp;
11030 if (this_scroll_margin)
11031 {
11032 start_display (&it, w, startp);
11033 move_it_vertically (&it, this_scroll_margin);
11034 scroll_margin_pos = it.current.pos;
11035 }
11036
11037 if (PT < CHARPOS (scroll_margin_pos))
11038 {
11039 /* Point is in the scroll margin at the top of the window or
11040 above what is displayed in the window. */
11041 int y0;
11042
11043 /* Compute the vertical distance from PT to the scroll
11044 margin position. Give up if distance is greater than
11045 scroll_max. */
11046 SET_TEXT_POS (pos, PT, PT_BYTE);
11047 start_display (&it, w, pos);
11048 y0 = it.current_y;
11049 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
11050 it.last_visible_y, -1,
11051 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
11052 dy = it.current_y - y0;
11053 if (dy > scroll_max)
11054 return SCROLLING_FAILED;
11055
11056 /* Compute new window start. */
11057 start_display (&it, w, startp);
11058
11059 if (scroll_conservatively)
11060 amount_to_scroll =
11061 max (dy, FRAME_LINE_HEIGHT (f) * max (scroll_step, temp_scroll_step));
11062 else if (scroll_step || temp_scroll_step)
11063 amount_to_scroll = scroll_max;
11064 else
11065 {
11066 aggressive = current_buffer->scroll_down_aggressively;
11067 height = WINDOW_BOX_TEXT_HEIGHT (w);
11068 if (NUMBERP (aggressive))
11069 {
11070 double float_amount = XFLOATINT (aggressive) * height;
11071 amount_to_scroll = float_amount;
11072 if (amount_to_scroll == 0 && float_amount > 0)
11073 amount_to_scroll = 1;
11074 }
11075 }
11076
11077 if (amount_to_scroll <= 0)
11078 return SCROLLING_FAILED;
11079
11080 move_it_vertically (&it, - amount_to_scroll);
11081 startp = it.current.pos;
11082 }
11083 }
11084
11085 /* Run window scroll functions. */
11086 startp = run_window_scroll_functions (window, startp);
11087
11088 /* Display the window. Give up if new fonts are loaded, or if point
11089 doesn't appear. */
11090 if (!try_window (window, startp))
11091 rc = SCROLLING_NEED_LARGER_MATRICES;
11092 else if (w->cursor.vpos < 0)
11093 {
11094 clear_glyph_matrix (w->desired_matrix);
11095 rc = SCROLLING_FAILED;
11096 }
11097 else
11098 {
11099 /* Maybe forget recorded base line for line number display. */
11100 if (!just_this_one_p
11101 || current_buffer->clip_changed
11102 || BEG_UNCHANGED < CHARPOS (startp))
11103 w->base_line_number = Qnil;
11104
11105 /* If cursor ends up on a partially visible line,
11106 treat that as being off the bottom of the screen. */
11107 if (! make_cursor_line_fully_visible (w, extra_scroll_margin_lines <= 1))
11108 {
11109 clear_glyph_matrix (w->desired_matrix);
11110 ++extra_scroll_margin_lines;
11111 goto too_near_end;
11112 }
11113 rc = SCROLLING_SUCCESS;
11114 }
11115
11116 return rc;
11117 }
11118
11119
11120 /* Compute a suitable window start for window W if display of W starts
11121 on a continuation line. Value is non-zero if a new window start
11122 was computed.
11123
11124 The new window start will be computed, based on W's width, starting
11125 from the start of the continued line. It is the start of the
11126 screen line with the minimum distance from the old start W->start. */
11127
11128 static int
11129 compute_window_start_on_continuation_line (w)
11130 struct window *w;
11131 {
11132 struct text_pos pos, start_pos;
11133 int window_start_changed_p = 0;
11134
11135 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
11136
11137 /* If window start is on a continuation line... Window start may be
11138 < BEGV in case there's invisible text at the start of the
11139 buffer (M-x rmail, for example). */
11140 if (CHARPOS (start_pos) > BEGV
11141 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
11142 {
11143 struct it it;
11144 struct glyph_row *row;
11145
11146 /* Handle the case that the window start is out of range. */
11147 if (CHARPOS (start_pos) < BEGV)
11148 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
11149 else if (CHARPOS (start_pos) > ZV)
11150 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
11151
11152 /* Find the start of the continued line. This should be fast
11153 because scan_buffer is fast (newline cache). */
11154 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
11155 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
11156 row, DEFAULT_FACE_ID);
11157 reseat_at_previous_visible_line_start (&it);
11158
11159 /* If the line start is "too far" away from the window start,
11160 say it takes too much time to compute a new window start. */
11161 if (CHARPOS (start_pos) - IT_CHARPOS (it)
11162 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
11163 {
11164 int min_distance, distance;
11165
11166 /* Move forward by display lines to find the new window
11167 start. If window width was enlarged, the new start can
11168 be expected to be > the old start. If window width was
11169 decreased, the new window start will be < the old start.
11170 So, we're looking for the display line start with the
11171 minimum distance from the old window start. */
11172 pos = it.current.pos;
11173 min_distance = INFINITY;
11174 while ((distance = abs (CHARPOS (start_pos) - IT_CHARPOS (it))),
11175 distance < min_distance)
11176 {
11177 min_distance = distance;
11178 pos = it.current.pos;
11179 move_it_by_lines (&it, 1, 0);
11180 }
11181
11182 /* Set the window start there. */
11183 SET_MARKER_FROM_TEXT_POS (w->start, pos);
11184 window_start_changed_p = 1;
11185 }
11186 }
11187
11188 return window_start_changed_p;
11189 }
11190
11191
11192 /* Try cursor movement in case text has not changed in window WINDOW,
11193 with window start STARTP. Value is
11194
11195 CURSOR_MOVEMENT_SUCCESS if successful
11196
11197 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
11198
11199 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
11200 display. *SCROLL_STEP is set to 1, under certain circumstances, if
11201 we want to scroll as if scroll-step were set to 1. See the code.
11202
11203 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
11204 which case we have to abort this redisplay, and adjust matrices
11205 first. */
11206
11207 enum
11208 {
11209 CURSOR_MOVEMENT_SUCCESS,
11210 CURSOR_MOVEMENT_CANNOT_BE_USED,
11211 CURSOR_MOVEMENT_MUST_SCROLL,
11212 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
11213 };
11214
11215 static int
11216 try_cursor_movement (window, startp, scroll_step)
11217 Lisp_Object window;
11218 struct text_pos startp;
11219 int *scroll_step;
11220 {
11221 struct window *w = XWINDOW (window);
11222 struct frame *f = XFRAME (w->frame);
11223 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
11224
11225 #if GLYPH_DEBUG
11226 if (inhibit_try_cursor_movement)
11227 return rc;
11228 #endif
11229
11230 /* Handle case where text has not changed, only point, and it has
11231 not moved off the frame. */
11232 if (/* Point may be in this window. */
11233 PT >= CHARPOS (startp)
11234 /* Selective display hasn't changed. */
11235 && !current_buffer->clip_changed
11236 /* Function force-mode-line-update is used to force a thorough
11237 redisplay. It sets either windows_or_buffers_changed or
11238 update_mode_lines. So don't take a shortcut here for these
11239 cases. */
11240 && !update_mode_lines
11241 && !windows_or_buffers_changed
11242 && !cursor_type_changed
11243 /* Can't use this case if highlighting a region. When a
11244 region exists, cursor movement has to do more than just
11245 set the cursor. */
11246 && !(!NILP (Vtransient_mark_mode)
11247 && !NILP (current_buffer->mark_active))
11248 && NILP (w->region_showing)
11249 && NILP (Vshow_trailing_whitespace)
11250 /* Right after splitting windows, last_point may be nil. */
11251 && INTEGERP (w->last_point)
11252 /* This code is not used for mini-buffer for the sake of the case
11253 of redisplaying to replace an echo area message; since in
11254 that case the mini-buffer contents per se are usually
11255 unchanged. This code is of no real use in the mini-buffer
11256 since the handling of this_line_start_pos, etc., in redisplay
11257 handles the same cases. */
11258 && !EQ (window, minibuf_window)
11259 /* When splitting windows or for new windows, it happens that
11260 redisplay is called with a nil window_end_vpos or one being
11261 larger than the window. This should really be fixed in
11262 window.c. I don't have this on my list, now, so we do
11263 approximately the same as the old redisplay code. --gerd. */
11264 && INTEGERP (w->window_end_vpos)
11265 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
11266 && (FRAME_WINDOW_P (f)
11267 || !overlay_arrow_in_current_buffer_p ()))
11268 {
11269 int this_scroll_margin, top_scroll_margin;
11270 struct glyph_row *row = NULL;
11271
11272 #if GLYPH_DEBUG
11273 debug_method_add (w, "cursor movement");
11274 #endif
11275
11276 /* Scroll if point within this distance from the top or bottom
11277 of the window. This is a pixel value. */
11278 this_scroll_margin = max (0, scroll_margin);
11279 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
11280 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
11281
11282 top_scroll_margin = this_scroll_margin;
11283 if (WINDOW_WANTS_HEADER_LINE_P (w))
11284 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
11285
11286 /* Start with the row the cursor was displayed during the last
11287 not paused redisplay. Give up if that row is not valid. */
11288 if (w->last_cursor.vpos < 0
11289 || w->last_cursor.vpos >= w->current_matrix->nrows)
11290 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11291 else
11292 {
11293 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
11294 if (row->mode_line_p)
11295 ++row;
11296 if (!row->enabled_p)
11297 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11298 }
11299
11300 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
11301 {
11302 int scroll_p = 0;
11303 int last_y = window_text_bottom_y (w) - this_scroll_margin;
11304
11305 if (PT > XFASTINT (w->last_point))
11306 {
11307 /* Point has moved forward. */
11308 while (MATRIX_ROW_END_CHARPOS (row) < PT
11309 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
11310 {
11311 xassert (row->enabled_p);
11312 ++row;
11313 }
11314
11315 /* The end position of a row equals the start position
11316 of the next row. If PT is there, we would rather
11317 display it in the next line. */
11318 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
11319 && MATRIX_ROW_END_CHARPOS (row) == PT
11320 && !cursor_row_p (w, row))
11321 ++row;
11322
11323 /* If within the scroll margin, scroll. Note that
11324 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
11325 the next line would be drawn, and that
11326 this_scroll_margin can be zero. */
11327 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
11328 || PT > MATRIX_ROW_END_CHARPOS (row)
11329 /* Line is completely visible last line in window
11330 and PT is to be set in the next line. */
11331 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
11332 && PT == MATRIX_ROW_END_CHARPOS (row)
11333 && !row->ends_at_zv_p
11334 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
11335 scroll_p = 1;
11336 }
11337 else if (PT < XFASTINT (w->last_point))
11338 {
11339 /* Cursor has to be moved backward. Note that PT >=
11340 CHARPOS (startp) because of the outer
11341 if-statement. */
11342 while (!row->mode_line_p
11343 && (MATRIX_ROW_START_CHARPOS (row) > PT
11344 || (MATRIX_ROW_START_CHARPOS (row) == PT
11345 && MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)))
11346 && (row->y > top_scroll_margin
11347 || CHARPOS (startp) == BEGV))
11348 {
11349 xassert (row->enabled_p);
11350 --row;
11351 }
11352
11353 /* Consider the following case: Window starts at BEGV,
11354 there is invisible, intangible text at BEGV, so that
11355 display starts at some point START > BEGV. It can
11356 happen that we are called with PT somewhere between
11357 BEGV and START. Try to handle that case. */
11358 if (row < w->current_matrix->rows
11359 || row->mode_line_p)
11360 {
11361 row = w->current_matrix->rows;
11362 if (row->mode_line_p)
11363 ++row;
11364 }
11365
11366 /* Due to newlines in overlay strings, we may have to
11367 skip forward over overlay strings. */
11368 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
11369 && MATRIX_ROW_END_CHARPOS (row) == PT
11370 && !cursor_row_p (w, row))
11371 ++row;
11372
11373 /* If within the scroll margin, scroll. */
11374 if (row->y < top_scroll_margin
11375 && CHARPOS (startp) != BEGV)
11376 scroll_p = 1;
11377 }
11378
11379 if (PT < MATRIX_ROW_START_CHARPOS (row)
11380 || PT > MATRIX_ROW_END_CHARPOS (row))
11381 {
11382 /* if PT is not in the glyph row, give up. */
11383 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11384 }
11385 else if (MATRIX_ROW_PARTIALLY_VISIBLE_P (row))
11386 {
11387 if (PT == MATRIX_ROW_END_CHARPOS (row)
11388 && !row->ends_at_zv_p
11389 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
11390 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11391 else if (row->height > window_box_height (w))
11392 {
11393 /* If we end up in a partially visible line, let's
11394 make it fully visible, except when it's taller
11395 than the window, in which case we can't do much
11396 about it. */
11397 *scroll_step = 1;
11398 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11399 }
11400 else
11401 {
11402 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11403 if (!make_cursor_line_fully_visible (w, 0))
11404 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11405 else
11406 rc = CURSOR_MOVEMENT_SUCCESS;
11407 }
11408 }
11409 else if (scroll_p)
11410 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11411 else
11412 {
11413 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11414 rc = CURSOR_MOVEMENT_SUCCESS;
11415 }
11416 }
11417 }
11418
11419 return rc;
11420 }
11421
11422 void
11423 set_vertical_scroll_bar (w)
11424 struct window *w;
11425 {
11426 int start, end, whole;
11427
11428 /* Calculate the start and end positions for the current window.
11429 At some point, it would be nice to choose between scrollbars
11430 which reflect the whole buffer size, with special markers
11431 indicating narrowing, and scrollbars which reflect only the
11432 visible region.
11433
11434 Note that mini-buffers sometimes aren't displaying any text. */
11435 if (!MINI_WINDOW_P (w)
11436 || (w == XWINDOW (minibuf_window)
11437 && NILP (echo_area_buffer[0])))
11438 {
11439 struct buffer *buf = XBUFFER (w->buffer);
11440 whole = BUF_ZV (buf) - BUF_BEGV (buf);
11441 start = marker_position (w->start) - BUF_BEGV (buf);
11442 /* I don't think this is guaranteed to be right. For the
11443 moment, we'll pretend it is. */
11444 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
11445
11446 if (end < start)
11447 end = start;
11448 if (whole < (end - start))
11449 whole = end - start;
11450 }
11451 else
11452 start = end = whole = 0;
11453
11454 /* Indicate what this scroll bar ought to be displaying now. */
11455 if (FRAME_DISPLAY (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
11456 (*FRAME_DISPLAY (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
11457 (w, end - start, whole, start);
11458 }
11459
11460
11461 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
11462 selected_window is redisplayed.
11463
11464 We can return without actually redisplaying the window if
11465 fonts_changed_p is nonzero. In that case, redisplay_internal will
11466 retry. */
11467
11468 static void
11469 redisplay_window (window, just_this_one_p)
11470 Lisp_Object window;
11471 int just_this_one_p;
11472 {
11473 struct window *w = XWINDOW (window);
11474 struct frame *f = XFRAME (w->frame);
11475 struct buffer *buffer = XBUFFER (w->buffer);
11476 struct buffer *old = current_buffer;
11477 struct text_pos lpoint, opoint, startp;
11478 int update_mode_line;
11479 int tem;
11480 struct it it;
11481 /* Record it now because it's overwritten. */
11482 int current_matrix_up_to_date_p = 0;
11483 int used_current_matrix_p = 0;
11484 /* This is less strict than current_matrix_up_to_date_p.
11485 It indictes that the buffer contents and narrowing are unchanged. */
11486 int buffer_unchanged_p = 0;
11487 int temp_scroll_step = 0;
11488 int count = SPECPDL_INDEX ();
11489 int rc;
11490 int centering_position;
11491 int last_line_misfit = 0;
11492
11493 SET_TEXT_POS (lpoint, PT, PT_BYTE);
11494 opoint = lpoint;
11495
11496 /* W must be a leaf window here. */
11497 xassert (!NILP (w->buffer));
11498 #if GLYPH_DEBUG
11499 *w->desired_matrix->method = 0;
11500 #endif
11501
11502 specbind (Qinhibit_point_motion_hooks, Qt);
11503
11504 reconsider_clip_changes (w, buffer);
11505
11506 /* Has the mode line to be updated? */
11507 update_mode_line = (!NILP (w->update_mode_line)
11508 || update_mode_lines
11509 || buffer->clip_changed
11510 || buffer->prevent_redisplay_optimizations_p);
11511
11512 if (MINI_WINDOW_P (w))
11513 {
11514 if (w == XWINDOW (echo_area_window)
11515 && !NILP (echo_area_buffer[0]))
11516 {
11517 if (update_mode_line)
11518 /* We may have to update a tty frame's menu bar or a
11519 tool-bar. Example `M-x C-h C-h C-g'. */
11520 goto finish_menu_bars;
11521 else
11522 /* We've already displayed the echo area glyphs in this window. */
11523 goto finish_scroll_bars;
11524 }
11525 else if ((w != XWINDOW (minibuf_window)
11526 || minibuf_level == 0)
11527 /* When buffer is nonempty, redisplay window normally. */
11528 && BUF_Z (XBUFFER (w->buffer)) == BUF_BEG (XBUFFER (w->buffer))
11529 /* Quail displays non-mini buffers in minibuffer window.
11530 In that case, redisplay the window normally. */
11531 && !NILP (Fmemq (w->buffer, Vminibuffer_list)))
11532 {
11533 /* W is a mini-buffer window, but it's not active, so clear
11534 it. */
11535 int yb = window_text_bottom_y (w);
11536 struct glyph_row *row;
11537 int y;
11538
11539 for (y = 0, row = w->desired_matrix->rows;
11540 y < yb;
11541 y += row->height, ++row)
11542 blank_row (w, row, y);
11543 goto finish_scroll_bars;
11544 }
11545
11546 clear_glyph_matrix (w->desired_matrix);
11547 }
11548
11549 /* Otherwise set up data on this window; select its buffer and point
11550 value. */
11551 /* Really select the buffer, for the sake of buffer-local
11552 variables. */
11553 set_buffer_internal_1 (XBUFFER (w->buffer));
11554 SET_TEXT_POS (opoint, PT, PT_BYTE);
11555
11556 current_matrix_up_to_date_p
11557 = (!NILP (w->window_end_valid)
11558 && !current_buffer->clip_changed
11559 && !current_buffer->prevent_redisplay_optimizations_p
11560 && XFASTINT (w->last_modified) >= MODIFF
11561 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
11562
11563 buffer_unchanged_p
11564 = (!NILP (w->window_end_valid)
11565 && !current_buffer->clip_changed
11566 && XFASTINT (w->last_modified) >= MODIFF
11567 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
11568
11569 /* When windows_or_buffers_changed is non-zero, we can't rely on
11570 the window end being valid, so set it to nil there. */
11571 if (windows_or_buffers_changed)
11572 {
11573 /* If window starts on a continuation line, maybe adjust the
11574 window start in case the window's width changed. */
11575 if (XMARKER (w->start)->buffer == current_buffer)
11576 compute_window_start_on_continuation_line (w);
11577
11578 w->window_end_valid = Qnil;
11579 }
11580
11581 /* Some sanity checks. */
11582 CHECK_WINDOW_END (w);
11583 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
11584 abort ();
11585 if (BYTEPOS (opoint) < CHARPOS (opoint))
11586 abort ();
11587
11588 /* If %c is in mode line, update it if needed. */
11589 if (!NILP (w->column_number_displayed)
11590 /* This alternative quickly identifies a common case
11591 where no change is needed. */
11592 && !(PT == XFASTINT (w->last_point)
11593 && XFASTINT (w->last_modified) >= MODIFF
11594 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
11595 && (XFASTINT (w->column_number_displayed)
11596 != (int) current_column ())) /* iftc */
11597 update_mode_line = 1;
11598
11599 /* Count number of windows showing the selected buffer. An indirect
11600 buffer counts as its base buffer. */
11601 if (!just_this_one_p)
11602 {
11603 struct buffer *current_base, *window_base;
11604 current_base = current_buffer;
11605 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
11606 if (current_base->base_buffer)
11607 current_base = current_base->base_buffer;
11608 if (window_base->base_buffer)
11609 window_base = window_base->base_buffer;
11610 if (current_base == window_base)
11611 buffer_shared++;
11612 }
11613
11614 /* Point refers normally to the selected window. For any other
11615 window, set up appropriate value. */
11616 if (!EQ (window, selected_window))
11617 {
11618 int new_pt = XMARKER (w->pointm)->charpos;
11619 int new_pt_byte = marker_byte_position (w->pointm);
11620 if (new_pt < BEGV)
11621 {
11622 new_pt = BEGV;
11623 new_pt_byte = BEGV_BYTE;
11624 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
11625 }
11626 else if (new_pt > (ZV - 1))
11627 {
11628 new_pt = ZV;
11629 new_pt_byte = ZV_BYTE;
11630 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
11631 }
11632
11633 /* We don't use SET_PT so that the point-motion hooks don't run. */
11634 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
11635 }
11636
11637 /* If any of the character widths specified in the display table
11638 have changed, invalidate the width run cache. It's true that
11639 this may be a bit late to catch such changes, but the rest of
11640 redisplay goes (non-fatally) haywire when the display table is
11641 changed, so why should we worry about doing any better? */
11642 if (current_buffer->width_run_cache)
11643 {
11644 struct Lisp_Char_Table *disptab = buffer_display_table ();
11645
11646 if (! disptab_matches_widthtab (disptab,
11647 XVECTOR (current_buffer->width_table)))
11648 {
11649 invalidate_region_cache (current_buffer,
11650 current_buffer->width_run_cache,
11651 BEG, Z);
11652 recompute_width_table (current_buffer, disptab);
11653 }
11654 }
11655
11656 /* If window-start is screwed up, choose a new one. */
11657 if (XMARKER (w->start)->buffer != current_buffer)
11658 goto recenter;
11659
11660 SET_TEXT_POS_FROM_MARKER (startp, w->start);
11661
11662 /* If someone specified a new starting point but did not insist,
11663 check whether it can be used. */
11664 if (!NILP (w->optional_new_start)
11665 && CHARPOS (startp) >= BEGV
11666 && CHARPOS (startp) <= ZV)
11667 {
11668 w->optional_new_start = Qnil;
11669 start_display (&it, w, startp);
11670 move_it_to (&it, PT, 0, it.last_visible_y, -1,
11671 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
11672 if (IT_CHARPOS (it) == PT)
11673 w->force_start = Qt;
11674 /* IT may overshoot PT if text at PT is invisible. */
11675 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
11676 w->force_start = Qt;
11677
11678
11679 }
11680
11681 /* Handle case where place to start displaying has been specified,
11682 unless the specified location is outside the accessible range. */
11683 if (!NILP (w->force_start)
11684 || w->frozen_window_start_p)
11685 {
11686 /* We set this later on if we have to adjust point. */
11687 int new_vpos = -1;
11688
11689 w->force_start = Qnil;
11690 w->vscroll = 0;
11691 w->window_end_valid = Qnil;
11692
11693 /* Forget any recorded base line for line number display. */
11694 if (!buffer_unchanged_p)
11695 w->base_line_number = Qnil;
11696
11697 /* Redisplay the mode line. Select the buffer properly for that.
11698 Also, run the hook window-scroll-functions
11699 because we have scrolled. */
11700 /* Note, we do this after clearing force_start because
11701 if there's an error, it is better to forget about force_start
11702 than to get into an infinite loop calling the hook functions
11703 and having them get more errors. */
11704 if (!update_mode_line
11705 || ! NILP (Vwindow_scroll_functions))
11706 {
11707 update_mode_line = 1;
11708 w->update_mode_line = Qt;
11709 startp = run_window_scroll_functions (window, startp);
11710 }
11711
11712 w->last_modified = make_number (0);
11713 w->last_overlay_modified = make_number (0);
11714 if (CHARPOS (startp) < BEGV)
11715 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
11716 else if (CHARPOS (startp) > ZV)
11717 SET_TEXT_POS (startp, ZV, ZV_BYTE);
11718
11719 /* Redisplay, then check if cursor has been set during the
11720 redisplay. Give up if new fonts were loaded. */
11721 if (!try_window (window, startp))
11722 {
11723 w->force_start = Qt;
11724 clear_glyph_matrix (w->desired_matrix);
11725 goto need_larger_matrices;
11726 }
11727
11728 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
11729 {
11730 /* If point does not appear, try to move point so it does
11731 appear. The desired matrix has been built above, so we
11732 can use it here. */
11733 new_vpos = window_box_height (w) / 2;
11734 }
11735
11736 if (!make_cursor_line_fully_visible (w, 0))
11737 {
11738 /* Point does appear, but on a line partly visible at end of window.
11739 Move it back to a fully-visible line. */
11740 new_vpos = window_box_height (w);
11741 }
11742
11743 /* If we need to move point for either of the above reasons,
11744 now actually do it. */
11745 if (new_vpos >= 0)
11746 {
11747 struct glyph_row *row;
11748
11749 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
11750 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
11751 ++row;
11752
11753 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
11754 MATRIX_ROW_START_BYTEPOS (row));
11755
11756 if (w != XWINDOW (selected_window))
11757 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
11758 else if (current_buffer == old)
11759 SET_TEXT_POS (lpoint, PT, PT_BYTE);
11760
11761 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
11762
11763 /* If we are highlighting the region, then we just changed
11764 the region, so redisplay to show it. */
11765 if (!NILP (Vtransient_mark_mode)
11766 && !NILP (current_buffer->mark_active))
11767 {
11768 clear_glyph_matrix (w->desired_matrix);
11769 if (!try_window (window, startp))
11770 goto need_larger_matrices;
11771 }
11772 }
11773
11774 #if GLYPH_DEBUG
11775 debug_method_add (w, "forced window start");
11776 #endif
11777 goto done;
11778 }
11779
11780 /* Handle case where text has not changed, only point, and it has
11781 not moved off the frame, and we are not retrying after hscroll.
11782 (current_matrix_up_to_date_p is nonzero when retrying.) */
11783 if (current_matrix_up_to_date_p
11784 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
11785 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
11786 {
11787 switch (rc)
11788 {
11789 case CURSOR_MOVEMENT_SUCCESS:
11790 used_current_matrix_p = 1;
11791 goto done;
11792
11793 #if 0 /* try_cursor_movement never returns this value. */
11794 case CURSOR_MOVEMENT_NEED_LARGER_MATRICES:
11795 goto need_larger_matrices;
11796 #endif
11797
11798 case CURSOR_MOVEMENT_MUST_SCROLL:
11799 goto try_to_scroll;
11800
11801 default:
11802 abort ();
11803 }
11804 }
11805 /* If current starting point was originally the beginning of a line
11806 but no longer is, find a new starting point. */
11807 else if (!NILP (w->start_at_line_beg)
11808 && !(CHARPOS (startp) <= BEGV
11809 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
11810 {
11811 #if GLYPH_DEBUG
11812 debug_method_add (w, "recenter 1");
11813 #endif
11814 goto recenter;
11815 }
11816
11817 /* Try scrolling with try_window_id. Value is > 0 if update has
11818 been done, it is -1 if we know that the same window start will
11819 not work. It is 0 if unsuccessful for some other reason. */
11820 else if ((tem = try_window_id (w)) != 0)
11821 {
11822 #if GLYPH_DEBUG
11823 debug_method_add (w, "try_window_id %d", tem);
11824 #endif
11825
11826 if (fonts_changed_p)
11827 goto need_larger_matrices;
11828 if (tem > 0)
11829 goto done;
11830
11831 /* Otherwise try_window_id has returned -1 which means that we
11832 don't want the alternative below this comment to execute. */
11833 }
11834 else if (CHARPOS (startp) >= BEGV
11835 && CHARPOS (startp) <= ZV
11836 && PT >= CHARPOS (startp)
11837 && (CHARPOS (startp) < ZV
11838 /* Avoid starting at end of buffer. */
11839 || CHARPOS (startp) == BEGV
11840 || (XFASTINT (w->last_modified) >= MODIFF
11841 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
11842 {
11843 #if GLYPH_DEBUG
11844 debug_method_add (w, "same window start");
11845 #endif
11846
11847 /* Try to redisplay starting at same place as before.
11848 If point has not moved off frame, accept the results. */
11849 if (!current_matrix_up_to_date_p
11850 /* Don't use try_window_reusing_current_matrix in this case
11851 because a window scroll function can have changed the
11852 buffer. */
11853 || !NILP (Vwindow_scroll_functions)
11854 || MINI_WINDOW_P (w)
11855 || !(used_current_matrix_p =
11856 try_window_reusing_current_matrix (w)))
11857 {
11858 IF_DEBUG (debug_method_add (w, "1"));
11859 try_window (window, startp);
11860 }
11861
11862 if (fonts_changed_p)
11863 goto need_larger_matrices;
11864
11865 if (w->cursor.vpos >= 0)
11866 {
11867 if (!just_this_one_p
11868 || current_buffer->clip_changed
11869 || BEG_UNCHANGED < CHARPOS (startp))
11870 /* Forget any recorded base line for line number display. */
11871 w->base_line_number = Qnil;
11872
11873 if (!make_cursor_line_fully_visible (w, 1))
11874 {
11875 clear_glyph_matrix (w->desired_matrix);
11876 last_line_misfit = 1;
11877 }
11878 /* Drop through and scroll. */
11879 else
11880 goto done;
11881 }
11882 else
11883 clear_glyph_matrix (w->desired_matrix);
11884 }
11885
11886 try_to_scroll:
11887
11888 w->last_modified = make_number (0);
11889 w->last_overlay_modified = make_number (0);
11890
11891 /* Redisplay the mode line. Select the buffer properly for that. */
11892 if (!update_mode_line)
11893 {
11894 update_mode_line = 1;
11895 w->update_mode_line = Qt;
11896 }
11897
11898 /* Try to scroll by specified few lines. */
11899 if ((scroll_conservatively
11900 || scroll_step
11901 || temp_scroll_step
11902 || NUMBERP (current_buffer->scroll_up_aggressively)
11903 || NUMBERP (current_buffer->scroll_down_aggressively))
11904 && !current_buffer->clip_changed
11905 && CHARPOS (startp) >= BEGV
11906 && CHARPOS (startp) <= ZV)
11907 {
11908 /* The function returns -1 if new fonts were loaded, 1 if
11909 successful, 0 if not successful. */
11910 int rc = try_scrolling (window, just_this_one_p,
11911 scroll_conservatively,
11912 scroll_step,
11913 temp_scroll_step, last_line_misfit);
11914 switch (rc)
11915 {
11916 case SCROLLING_SUCCESS:
11917 goto done;
11918
11919 case SCROLLING_NEED_LARGER_MATRICES:
11920 goto need_larger_matrices;
11921
11922 case SCROLLING_FAILED:
11923 break;
11924
11925 default:
11926 abort ();
11927 }
11928 }
11929
11930 /* Finally, just choose place to start which centers point */
11931
11932 recenter:
11933 centering_position = window_box_height (w) / 2;
11934
11935 point_at_top:
11936 /* Jump here with centering_position already set to 0. */
11937
11938 #if GLYPH_DEBUG
11939 debug_method_add (w, "recenter");
11940 #endif
11941
11942 /* w->vscroll = 0; */
11943
11944 /* Forget any previously recorded base line for line number display. */
11945 if (!buffer_unchanged_p)
11946 w->base_line_number = Qnil;
11947
11948 /* Move backward half the height of the window. */
11949 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
11950 it.current_y = it.last_visible_y;
11951 move_it_vertically_backward (&it, centering_position);
11952 xassert (IT_CHARPOS (it) >= BEGV);
11953
11954 /* The function move_it_vertically_backward may move over more
11955 than the specified y-distance. If it->w is small, e.g. a
11956 mini-buffer window, we may end up in front of the window's
11957 display area. Start displaying at the start of the line
11958 containing PT in this case. */
11959 if (it.current_y <= 0)
11960 {
11961 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
11962 move_it_vertically (&it, 0);
11963 xassert (IT_CHARPOS (it) <= PT);
11964 it.current_y = 0;
11965 }
11966
11967 it.current_x = it.hpos = 0;
11968
11969 /* Set startp here explicitly in case that helps avoid an infinite loop
11970 in case the window-scroll-functions functions get errors. */
11971 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
11972
11973 /* Run scroll hooks. */
11974 startp = run_window_scroll_functions (window, it.current.pos);
11975
11976 /* Redisplay the window. */
11977 if (!current_matrix_up_to_date_p
11978 || windows_or_buffers_changed
11979 || cursor_type_changed
11980 /* Don't use try_window_reusing_current_matrix in this case
11981 because it can have changed the buffer. */
11982 || !NILP (Vwindow_scroll_functions)
11983 || !just_this_one_p
11984 || MINI_WINDOW_P (w)
11985 || !(used_current_matrix_p =
11986 try_window_reusing_current_matrix (w)))
11987 try_window (window, startp);
11988
11989 /* If new fonts have been loaded (due to fontsets), give up. We
11990 have to start a new redisplay since we need to re-adjust glyph
11991 matrices. */
11992 if (fonts_changed_p)
11993 goto need_larger_matrices;
11994
11995 /* If cursor did not appear assume that the middle of the window is
11996 in the first line of the window. Do it again with the next line.
11997 (Imagine a window of height 100, displaying two lines of height
11998 60. Moving back 50 from it->last_visible_y will end in the first
11999 line.) */
12000 if (w->cursor.vpos < 0)
12001 {
12002 if (!NILP (w->window_end_valid)
12003 && PT >= Z - XFASTINT (w->window_end_pos))
12004 {
12005 clear_glyph_matrix (w->desired_matrix);
12006 move_it_by_lines (&it, 1, 0);
12007 try_window (window, it.current.pos);
12008 }
12009 else if (PT < IT_CHARPOS (it))
12010 {
12011 clear_glyph_matrix (w->desired_matrix);
12012 move_it_by_lines (&it, -1, 0);
12013 try_window (window, it.current.pos);
12014 }
12015 else
12016 {
12017 /* Not much we can do about it. */
12018 }
12019 }
12020
12021 /* Consider the following case: Window starts at BEGV, there is
12022 invisible, intangible text at BEGV, so that display starts at
12023 some point START > BEGV. It can happen that we are called with
12024 PT somewhere between BEGV and START. Try to handle that case. */
12025 if (w->cursor.vpos < 0)
12026 {
12027 struct glyph_row *row = w->current_matrix->rows;
12028 if (row->mode_line_p)
12029 ++row;
12030 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
12031 }
12032
12033 if (!make_cursor_line_fully_visible (w, centering_position > 0))
12034 {
12035 /* If vscroll is enabled, disable it and try again. */
12036 if (w->vscroll)
12037 {
12038 w->vscroll = 0;
12039 clear_glyph_matrix (w->desired_matrix);
12040 goto recenter;
12041 }
12042
12043 /* If centering point failed to make the whole line visible,
12044 put point at the top instead. That has to make the whole line
12045 visible, if it can be done. */
12046 clear_glyph_matrix (w->desired_matrix);
12047 centering_position = 0;
12048 goto point_at_top;
12049 }
12050
12051 done:
12052
12053 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12054 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
12055 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
12056 ? Qt : Qnil);
12057
12058 /* Display the mode line, if we must. */
12059 if ((update_mode_line
12060 /* If window not full width, must redo its mode line
12061 if (a) the window to its side is being redone and
12062 (b) we do a frame-based redisplay. This is a consequence
12063 of how inverted lines are drawn in frame-based redisplay. */
12064 || (!just_this_one_p
12065 && !FRAME_WINDOW_P (f)
12066 && !WINDOW_FULL_WIDTH_P (w))
12067 /* Line number to display. */
12068 || INTEGERP (w->base_line_pos)
12069 /* Column number is displayed and different from the one displayed. */
12070 || (!NILP (w->column_number_displayed)
12071 && (XFASTINT (w->column_number_displayed)
12072 != (int) current_column ()))) /* iftc */
12073 /* This means that the window has a mode line. */
12074 && (WINDOW_WANTS_MODELINE_P (w)
12075 || WINDOW_WANTS_HEADER_LINE_P (w)))
12076 {
12077 display_mode_lines (w);
12078
12079 /* If mode line height has changed, arrange for a thorough
12080 immediate redisplay using the correct mode line height. */
12081 if (WINDOW_WANTS_MODELINE_P (w)
12082 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
12083 {
12084 fonts_changed_p = 1;
12085 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
12086 = DESIRED_MODE_LINE_HEIGHT (w);
12087 }
12088
12089 /* If top line height has changed, arrange for a thorough
12090 immediate redisplay using the correct mode line height. */
12091 if (WINDOW_WANTS_HEADER_LINE_P (w)
12092 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
12093 {
12094 fonts_changed_p = 1;
12095 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
12096 = DESIRED_HEADER_LINE_HEIGHT (w);
12097 }
12098
12099 if (fonts_changed_p)
12100 goto need_larger_matrices;
12101 }
12102
12103 if (!line_number_displayed
12104 && !BUFFERP (w->base_line_pos))
12105 {
12106 w->base_line_pos = Qnil;
12107 w->base_line_number = Qnil;
12108 }
12109
12110 finish_menu_bars:
12111
12112 /* When we reach a frame's selected window, redo the frame's menu bar. */
12113 if (update_mode_line
12114 && EQ (FRAME_SELECTED_WINDOW (f), window))
12115 {
12116 int redisplay_menu_p = 0;
12117 int redisplay_tool_bar_p = 0;
12118
12119 if (FRAME_WINDOW_P (f))
12120 {
12121 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
12122 || defined (USE_GTK)
12123 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
12124 #else
12125 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
12126 #endif
12127 }
12128 else
12129 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
12130
12131 if (redisplay_menu_p)
12132 display_menu_bar (w);
12133
12134 #ifdef HAVE_WINDOW_SYSTEM
12135 #ifdef USE_GTK
12136 redisplay_tool_bar_p = FRAME_EXTERNAL_TOOL_BAR (f);
12137 #else
12138 redisplay_tool_bar_p = WINDOWP (f->tool_bar_window)
12139 && (FRAME_TOOL_BAR_LINES (f) > 0
12140 || auto_resize_tool_bars_p);
12141
12142 #endif
12143
12144 if (redisplay_tool_bar_p)
12145 redisplay_tool_bar (f);
12146 #endif
12147 }
12148
12149 #ifdef HAVE_WINDOW_SYSTEM
12150 if (update_window_fringes (w, 0)
12151 && !just_this_one_p
12152 && (used_current_matrix_p || overlay_arrow_seen)
12153 && !w->pseudo_window_p)
12154 {
12155 update_begin (f);
12156 BLOCK_INPUT;
12157 draw_window_fringes (w);
12158 UNBLOCK_INPUT;
12159 update_end (f);
12160 }
12161 #endif /* HAVE_WINDOW_SYSTEM */
12162
12163 /* We go to this label, with fonts_changed_p nonzero,
12164 if it is necessary to try again using larger glyph matrices.
12165 We have to redeem the scroll bar even in this case,
12166 because the loop in redisplay_internal expects that. */
12167 need_larger_matrices:
12168 ;
12169 finish_scroll_bars:
12170
12171 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
12172 {
12173 /* Set the thumb's position and size. */
12174 set_vertical_scroll_bar (w);
12175
12176 /* Note that we actually used the scroll bar attached to this
12177 window, so it shouldn't be deleted at the end of redisplay. */
12178 if (FRAME_DISPLAY (f)->redeem_scroll_bar_hook)
12179 (*FRAME_DISPLAY (f)->redeem_scroll_bar_hook) (w);
12180 }
12181
12182 /* Restore current_buffer and value of point in it. */
12183 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
12184 set_buffer_internal_1 (old);
12185 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
12186
12187 unbind_to (count, Qnil);
12188 }
12189
12190
12191 /* Build the complete desired matrix of WINDOW with a window start
12192 buffer position POS. Value is non-zero if successful. It is zero
12193 if fonts were loaded during redisplay which makes re-adjusting
12194 glyph matrices necessary. */
12195
12196 int
12197 try_window (window, pos)
12198 Lisp_Object window;
12199 struct text_pos pos;
12200 {
12201 struct window *w = XWINDOW (window);
12202 struct it it;
12203 struct glyph_row *last_text_row = NULL;
12204
12205 /* Make POS the new window start. */
12206 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
12207
12208 /* Mark cursor position as unknown. No overlay arrow seen. */
12209 w->cursor.vpos = -1;
12210 overlay_arrow_seen = 0;
12211
12212 /* Initialize iterator and info to start at POS. */
12213 start_display (&it, w, pos);
12214
12215 /* Display all lines of W. */
12216 while (it.current_y < it.last_visible_y)
12217 {
12218 if (display_line (&it))
12219 last_text_row = it.glyph_row - 1;
12220 if (fonts_changed_p)
12221 return 0;
12222 }
12223
12224 /* If bottom moved off end of frame, change mode line percentage. */
12225 if (XFASTINT (w->window_end_pos) <= 0
12226 && Z != IT_CHARPOS (it))
12227 w->update_mode_line = Qt;
12228
12229 /* Set window_end_pos to the offset of the last character displayed
12230 on the window from the end of current_buffer. Set
12231 window_end_vpos to its row number. */
12232 if (last_text_row)
12233 {
12234 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
12235 w->window_end_bytepos
12236 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
12237 w->window_end_pos
12238 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
12239 w->window_end_vpos
12240 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
12241 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
12242 ->displays_text_p);
12243 }
12244 else
12245 {
12246 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
12247 w->window_end_pos = make_number (Z - ZV);
12248 w->window_end_vpos = make_number (0);
12249 }
12250
12251 /* But that is not valid info until redisplay finishes. */
12252 w->window_end_valid = Qnil;
12253 return 1;
12254 }
12255
12256
12257 \f
12258 /************************************************************************
12259 Window redisplay reusing current matrix when buffer has not changed
12260 ************************************************************************/
12261
12262 /* Try redisplay of window W showing an unchanged buffer with a
12263 different window start than the last time it was displayed by
12264 reusing its current matrix. Value is non-zero if successful.
12265 W->start is the new window start. */
12266
12267 static int
12268 try_window_reusing_current_matrix (w)
12269 struct window *w;
12270 {
12271 struct frame *f = XFRAME (w->frame);
12272 struct glyph_row *row, *bottom_row;
12273 struct it it;
12274 struct run run;
12275 struct text_pos start, new_start;
12276 int nrows_scrolled, i;
12277 struct glyph_row *last_text_row;
12278 struct glyph_row *last_reused_text_row;
12279 struct glyph_row *start_row;
12280 int start_vpos, min_y, max_y;
12281
12282 #if GLYPH_DEBUG
12283 if (inhibit_try_window_reusing)
12284 return 0;
12285 #endif
12286
12287 if (/* This function doesn't handle terminal frames. */
12288 !FRAME_WINDOW_P (f)
12289 /* Don't try to reuse the display if windows have been split
12290 or such. */
12291 || windows_or_buffers_changed
12292 || cursor_type_changed)
12293 return 0;
12294
12295 /* Can't do this if region may have changed. */
12296 if ((!NILP (Vtransient_mark_mode)
12297 && !NILP (current_buffer->mark_active))
12298 || !NILP (w->region_showing)
12299 || !NILP (Vshow_trailing_whitespace))
12300 return 0;
12301
12302 /* If top-line visibility has changed, give up. */
12303 if (WINDOW_WANTS_HEADER_LINE_P (w)
12304 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
12305 return 0;
12306
12307 /* Give up if old or new display is scrolled vertically. We could
12308 make this function handle this, but right now it doesn't. */
12309 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
12310 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (start_row))
12311 return 0;
12312
12313 /* The variable new_start now holds the new window start. The old
12314 start `start' can be determined from the current matrix. */
12315 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
12316 start = start_row->start.pos;
12317 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
12318
12319 /* Clear the desired matrix for the display below. */
12320 clear_glyph_matrix (w->desired_matrix);
12321
12322 if (CHARPOS (new_start) <= CHARPOS (start))
12323 {
12324 int first_row_y;
12325
12326 /* Don't use this method if the display starts with an ellipsis
12327 displayed for invisible text. It's not easy to handle that case
12328 below, and it's certainly not worth the effort since this is
12329 not a frequent case. */
12330 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
12331 return 0;
12332
12333 IF_DEBUG (debug_method_add (w, "twu1"));
12334
12335 /* Display up to a row that can be reused. The variable
12336 last_text_row is set to the last row displayed that displays
12337 text. Note that it.vpos == 0 if or if not there is a
12338 header-line; it's not the same as the MATRIX_ROW_VPOS! */
12339 start_display (&it, w, new_start);
12340 first_row_y = it.current_y;
12341 w->cursor.vpos = -1;
12342 last_text_row = last_reused_text_row = NULL;
12343
12344 while (it.current_y < it.last_visible_y
12345 && IT_CHARPOS (it) < CHARPOS (start)
12346 && !fonts_changed_p)
12347 if (display_line (&it))
12348 last_text_row = it.glyph_row - 1;
12349
12350 /* A value of current_y < last_visible_y means that we stopped
12351 at the previous window start, which in turn means that we
12352 have at least one reusable row. */
12353 if (it.current_y < it.last_visible_y)
12354 {
12355 /* IT.vpos always starts from 0; it counts text lines. */
12356 nrows_scrolled = it.vpos;
12357
12358 /* Find PT if not already found in the lines displayed. */
12359 if (w->cursor.vpos < 0)
12360 {
12361 int dy = it.current_y - first_row_y;
12362
12363 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
12364 row = row_containing_pos (w, PT, row, NULL, dy);
12365 if (row)
12366 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
12367 dy, nrows_scrolled);
12368 else
12369 {
12370 clear_glyph_matrix (w->desired_matrix);
12371 return 0;
12372 }
12373 }
12374
12375 /* Scroll the display. Do it before the current matrix is
12376 changed. The problem here is that update has not yet
12377 run, i.e. part of the current matrix is not up to date.
12378 scroll_run_hook will clear the cursor, and use the
12379 current matrix to get the height of the row the cursor is
12380 in. */
12381 run.current_y = first_row_y;
12382 run.desired_y = it.current_y;
12383 run.height = it.last_visible_y - it.current_y;
12384
12385 if (run.height > 0 && run.current_y != run.desired_y)
12386 {
12387 update_begin (f);
12388 FRAME_RIF (f)->update_window_begin_hook (w);
12389 FRAME_RIF (f)->clear_window_mouse_face (w);
12390 FRAME_RIF (f)->scroll_run_hook (w, &run);
12391 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
12392 update_end (f);
12393 }
12394
12395 /* Shift current matrix down by nrows_scrolled lines. */
12396 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
12397 rotate_matrix (w->current_matrix,
12398 start_vpos,
12399 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
12400 nrows_scrolled);
12401
12402 /* Disable lines that must be updated. */
12403 for (i = 0; i < it.vpos; ++i)
12404 (start_row + i)->enabled_p = 0;
12405
12406 /* Re-compute Y positions. */
12407 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
12408 max_y = it.last_visible_y;
12409 for (row = start_row + nrows_scrolled;
12410 row < bottom_row;
12411 ++row)
12412 {
12413 row->y = it.current_y;
12414 row->visible_height = row->height;
12415
12416 if (row->y < min_y)
12417 row->visible_height -= min_y - row->y;
12418 if (row->y + row->height > max_y)
12419 row->visible_height -= row->y + row->height - max_y;
12420 row->redraw_fringe_bitmaps_p = 1;
12421
12422 it.current_y += row->height;
12423
12424 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
12425 last_reused_text_row = row;
12426 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
12427 break;
12428 }
12429
12430 /* Disable lines in the current matrix which are now
12431 below the window. */
12432 for (++row; row < bottom_row; ++row)
12433 row->enabled_p = 0;
12434 }
12435
12436 /* Update window_end_pos etc.; last_reused_text_row is the last
12437 reused row from the current matrix containing text, if any.
12438 The value of last_text_row is the last displayed line
12439 containing text. */
12440 if (last_reused_text_row)
12441 {
12442 w->window_end_bytepos
12443 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
12444 w->window_end_pos
12445 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
12446 w->window_end_vpos
12447 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
12448 w->current_matrix));
12449 }
12450 else if (last_text_row)
12451 {
12452 w->window_end_bytepos
12453 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
12454 w->window_end_pos
12455 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
12456 w->window_end_vpos
12457 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
12458 }
12459 else
12460 {
12461 /* This window must be completely empty. */
12462 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
12463 w->window_end_pos = make_number (Z - ZV);
12464 w->window_end_vpos = make_number (0);
12465 }
12466 w->window_end_valid = Qnil;
12467
12468 /* Update hint: don't try scrolling again in update_window. */
12469 w->desired_matrix->no_scrolling_p = 1;
12470
12471 #if GLYPH_DEBUG
12472 debug_method_add (w, "try_window_reusing_current_matrix 1");
12473 #endif
12474 return 1;
12475 }
12476 else if (CHARPOS (new_start) > CHARPOS (start))
12477 {
12478 struct glyph_row *pt_row, *row;
12479 struct glyph_row *first_reusable_row;
12480 struct glyph_row *first_row_to_display;
12481 int dy;
12482 int yb = window_text_bottom_y (w);
12483
12484 /* Find the row starting at new_start, if there is one. Don't
12485 reuse a partially visible line at the end. */
12486 first_reusable_row = start_row;
12487 while (first_reusable_row->enabled_p
12488 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
12489 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
12490 < CHARPOS (new_start)))
12491 ++first_reusable_row;
12492
12493 /* Give up if there is no row to reuse. */
12494 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
12495 || !first_reusable_row->enabled_p
12496 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
12497 != CHARPOS (new_start)))
12498 return 0;
12499
12500 /* We can reuse fully visible rows beginning with
12501 first_reusable_row to the end of the window. Set
12502 first_row_to_display to the first row that cannot be reused.
12503 Set pt_row to the row containing point, if there is any. */
12504 pt_row = NULL;
12505 for (first_row_to_display = first_reusable_row;
12506 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
12507 ++first_row_to_display)
12508 {
12509 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
12510 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
12511 pt_row = first_row_to_display;
12512 }
12513
12514 /* Start displaying at the start of first_row_to_display. */
12515 xassert (first_row_to_display->y < yb);
12516 init_to_row_start (&it, w, first_row_to_display);
12517
12518 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
12519 - start_vpos);
12520 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
12521 - nrows_scrolled);
12522 it.current_y = (first_row_to_display->y - first_reusable_row->y
12523 + WINDOW_HEADER_LINE_HEIGHT (w));
12524
12525 /* Display lines beginning with first_row_to_display in the
12526 desired matrix. Set last_text_row to the last row displayed
12527 that displays text. */
12528 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
12529 if (pt_row == NULL)
12530 w->cursor.vpos = -1;
12531 last_text_row = NULL;
12532 while (it.current_y < it.last_visible_y && !fonts_changed_p)
12533 if (display_line (&it))
12534 last_text_row = it.glyph_row - 1;
12535
12536 /* Give up If point isn't in a row displayed or reused. */
12537 if (w->cursor.vpos < 0)
12538 {
12539 clear_glyph_matrix (w->desired_matrix);
12540 return 0;
12541 }
12542
12543 /* If point is in a reused row, adjust y and vpos of the cursor
12544 position. */
12545 if (pt_row)
12546 {
12547 w->cursor.vpos -= nrows_scrolled;
12548 w->cursor.y -= first_reusable_row->y - start_row->y;
12549 }
12550
12551 /* Scroll the display. */
12552 run.current_y = first_reusable_row->y;
12553 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
12554 run.height = it.last_visible_y - run.current_y;
12555 dy = run.current_y - run.desired_y;
12556
12557 if (run.height)
12558 {
12559 update_begin (f);
12560 FRAME_RIF (f)->update_window_begin_hook (w);
12561 FRAME_RIF (f)->clear_window_mouse_face (w);
12562 FRAME_RIF (f)->scroll_run_hook (w, &run);
12563 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
12564 update_end (f);
12565 }
12566
12567 /* Adjust Y positions of reused rows. */
12568 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
12569 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
12570 max_y = it.last_visible_y;
12571 for (row = first_reusable_row; row < first_row_to_display; ++row)
12572 {
12573 row->y -= dy;
12574 row->visible_height = row->height;
12575 if (row->y < min_y)
12576 row->visible_height -= min_y - row->y;
12577 if (row->y + row->height > max_y)
12578 row->visible_height -= row->y + row->height - max_y;
12579 row->redraw_fringe_bitmaps_p = 1;
12580 }
12581
12582 /* Scroll the current matrix. */
12583 xassert (nrows_scrolled > 0);
12584 rotate_matrix (w->current_matrix,
12585 start_vpos,
12586 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
12587 -nrows_scrolled);
12588
12589 /* Disable rows not reused. */
12590 for (row -= nrows_scrolled; row < bottom_row; ++row)
12591 row->enabled_p = 0;
12592
12593 /* Point may have moved to a different line, so we cannot assume that
12594 the previous cursor position is valid; locate the correct row. */
12595 if (pt_row)
12596 {
12597 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
12598 row < bottom_row && PT >= MATRIX_ROW_END_CHARPOS (row);
12599 row++)
12600 {
12601 w->cursor.vpos++;
12602 w->cursor.y = row->y;
12603 }
12604 if (row < bottom_row)
12605 {
12606 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
12607 while (glyph->charpos < PT)
12608 {
12609 w->cursor.hpos++;
12610 w->cursor.x += glyph->pixel_width;
12611 glyph++;
12612 }
12613 }
12614 }
12615
12616 /* Adjust window end. A null value of last_text_row means that
12617 the window end is in reused rows which in turn means that
12618 only its vpos can have changed. */
12619 if (last_text_row)
12620 {
12621 w->window_end_bytepos
12622 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
12623 w->window_end_pos
12624 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
12625 w->window_end_vpos
12626 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
12627 }
12628 else
12629 {
12630 w->window_end_vpos
12631 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
12632 }
12633
12634 w->window_end_valid = Qnil;
12635 w->desired_matrix->no_scrolling_p = 1;
12636
12637 #if GLYPH_DEBUG
12638 debug_method_add (w, "try_window_reusing_current_matrix 2");
12639 #endif
12640 return 1;
12641 }
12642
12643 return 0;
12644 }
12645
12646
12647 \f
12648 /************************************************************************
12649 Window redisplay reusing current matrix when buffer has changed
12650 ************************************************************************/
12651
12652 static struct glyph_row *find_last_unchanged_at_beg_row P_ ((struct window *));
12653 static struct glyph_row *find_first_unchanged_at_end_row P_ ((struct window *,
12654 int *, int *));
12655 static struct glyph_row *
12656 find_last_row_displaying_text P_ ((struct glyph_matrix *, struct it *,
12657 struct glyph_row *));
12658
12659
12660 /* Return the last row in MATRIX displaying text. If row START is
12661 non-null, start searching with that row. IT gives the dimensions
12662 of the display. Value is null if matrix is empty; otherwise it is
12663 a pointer to the row found. */
12664
12665 static struct glyph_row *
12666 find_last_row_displaying_text (matrix, it, start)
12667 struct glyph_matrix *matrix;
12668 struct it *it;
12669 struct glyph_row *start;
12670 {
12671 struct glyph_row *row, *row_found;
12672
12673 /* Set row_found to the last row in IT->w's current matrix
12674 displaying text. The loop looks funny but think of partially
12675 visible lines. */
12676 row_found = NULL;
12677 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
12678 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
12679 {
12680 xassert (row->enabled_p);
12681 row_found = row;
12682 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
12683 break;
12684 ++row;
12685 }
12686
12687 return row_found;
12688 }
12689
12690
12691 /* Return the last row in the current matrix of W that is not affected
12692 by changes at the start of current_buffer that occurred since W's
12693 current matrix was built. Value is null if no such row exists.
12694
12695 BEG_UNCHANGED us the number of characters unchanged at the start of
12696 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
12697 first changed character in current_buffer. Characters at positions <
12698 BEG + BEG_UNCHANGED are at the same buffer positions as they were
12699 when the current matrix was built. */
12700
12701 static struct glyph_row *
12702 find_last_unchanged_at_beg_row (w)
12703 struct window *w;
12704 {
12705 int first_changed_pos = BEG + BEG_UNCHANGED;
12706 struct glyph_row *row;
12707 struct glyph_row *row_found = NULL;
12708 int yb = window_text_bottom_y (w);
12709
12710 /* Find the last row displaying unchanged text. */
12711 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
12712 while (MATRIX_ROW_DISPLAYS_TEXT_P (row)
12713 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos)
12714 {
12715 if (/* If row ends before first_changed_pos, it is unchanged,
12716 except in some case. */
12717 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
12718 /* When row ends in ZV and we write at ZV it is not
12719 unchanged. */
12720 && !row->ends_at_zv_p
12721 /* When first_changed_pos is the end of a continued line,
12722 row is not unchanged because it may be no longer
12723 continued. */
12724 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
12725 && (row->continued_p
12726 || row->exact_window_width_line_p)))
12727 row_found = row;
12728
12729 /* Stop if last visible row. */
12730 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
12731 break;
12732
12733 ++row;
12734 }
12735
12736 return row_found;
12737 }
12738
12739
12740 /* Find the first glyph row in the current matrix of W that is not
12741 affected by changes at the end of current_buffer since the
12742 time W's current matrix was built.
12743
12744 Return in *DELTA the number of chars by which buffer positions in
12745 unchanged text at the end of current_buffer must be adjusted.
12746
12747 Return in *DELTA_BYTES the corresponding number of bytes.
12748
12749 Value is null if no such row exists, i.e. all rows are affected by
12750 changes. */
12751
12752 static struct glyph_row *
12753 find_first_unchanged_at_end_row (w, delta, delta_bytes)
12754 struct window *w;
12755 int *delta, *delta_bytes;
12756 {
12757 struct glyph_row *row;
12758 struct glyph_row *row_found = NULL;
12759
12760 *delta = *delta_bytes = 0;
12761
12762 /* Display must not have been paused, otherwise the current matrix
12763 is not up to date. */
12764 if (NILP (w->window_end_valid))
12765 abort ();
12766
12767 /* A value of window_end_pos >= END_UNCHANGED means that the window
12768 end is in the range of changed text. If so, there is no
12769 unchanged row at the end of W's current matrix. */
12770 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
12771 return NULL;
12772
12773 /* Set row to the last row in W's current matrix displaying text. */
12774 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
12775
12776 /* If matrix is entirely empty, no unchanged row exists. */
12777 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
12778 {
12779 /* The value of row is the last glyph row in the matrix having a
12780 meaningful buffer position in it. The end position of row
12781 corresponds to window_end_pos. This allows us to translate
12782 buffer positions in the current matrix to current buffer
12783 positions for characters not in changed text. */
12784 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
12785 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
12786 int last_unchanged_pos, last_unchanged_pos_old;
12787 struct glyph_row *first_text_row
12788 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
12789
12790 *delta = Z - Z_old;
12791 *delta_bytes = Z_BYTE - Z_BYTE_old;
12792
12793 /* Set last_unchanged_pos to the buffer position of the last
12794 character in the buffer that has not been changed. Z is the
12795 index + 1 of the last character in current_buffer, i.e. by
12796 subtracting END_UNCHANGED we get the index of the last
12797 unchanged character, and we have to add BEG to get its buffer
12798 position. */
12799 last_unchanged_pos = Z - END_UNCHANGED + BEG;
12800 last_unchanged_pos_old = last_unchanged_pos - *delta;
12801
12802 /* Search backward from ROW for a row displaying a line that
12803 starts at a minimum position >= last_unchanged_pos_old. */
12804 for (; row > first_text_row; --row)
12805 {
12806 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
12807 abort ();
12808
12809 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
12810 row_found = row;
12811 }
12812 }
12813
12814 if (row_found && !MATRIX_ROW_DISPLAYS_TEXT_P (row_found))
12815 abort ();
12816
12817 return row_found;
12818 }
12819
12820
12821 /* Make sure that glyph rows in the current matrix of window W
12822 reference the same glyph memory as corresponding rows in the
12823 frame's frame matrix. This function is called after scrolling W's
12824 current matrix on a terminal frame in try_window_id and
12825 try_window_reusing_current_matrix. */
12826
12827 static void
12828 sync_frame_with_window_matrix_rows (w)
12829 struct window *w;
12830 {
12831 struct frame *f = XFRAME (w->frame);
12832 struct glyph_row *window_row, *window_row_end, *frame_row;
12833
12834 /* Preconditions: W must be a leaf window and full-width. Its frame
12835 must have a frame matrix. */
12836 xassert (NILP (w->hchild) && NILP (w->vchild));
12837 xassert (WINDOW_FULL_WIDTH_P (w));
12838 xassert (!FRAME_WINDOW_P (f));
12839
12840 /* If W is a full-width window, glyph pointers in W's current matrix
12841 have, by definition, to be the same as glyph pointers in the
12842 corresponding frame matrix. Note that frame matrices have no
12843 marginal areas (see build_frame_matrix). */
12844 window_row = w->current_matrix->rows;
12845 window_row_end = window_row + w->current_matrix->nrows;
12846 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
12847 while (window_row < window_row_end)
12848 {
12849 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
12850 struct glyph *end = window_row->glyphs[LAST_AREA];
12851
12852 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
12853 frame_row->glyphs[TEXT_AREA] = start;
12854 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
12855 frame_row->glyphs[LAST_AREA] = end;
12856
12857 /* Disable frame rows whose corresponding window rows have
12858 been disabled in try_window_id. */
12859 if (!window_row->enabled_p)
12860 frame_row->enabled_p = 0;
12861
12862 ++window_row, ++frame_row;
12863 }
12864 }
12865
12866
12867 /* Find the glyph row in window W containing CHARPOS. Consider all
12868 rows between START and END (not inclusive). END null means search
12869 all rows to the end of the display area of W. Value is the row
12870 containing CHARPOS or null. */
12871
12872 struct glyph_row *
12873 row_containing_pos (w, charpos, start, end, dy)
12874 struct window *w;
12875 int charpos;
12876 struct glyph_row *start, *end;
12877 int dy;
12878 {
12879 struct glyph_row *row = start;
12880 int last_y;
12881
12882 /* If we happen to start on a header-line, skip that. */
12883 if (row->mode_line_p)
12884 ++row;
12885
12886 if ((end && row >= end) || !row->enabled_p)
12887 return NULL;
12888
12889 last_y = window_text_bottom_y (w) - dy;
12890
12891 while (1)
12892 {
12893 /* Give up if we have gone too far. */
12894 if (end && row >= end)
12895 return NULL;
12896 /* This formerly returned if they were equal.
12897 I think that both quantities are of a "last plus one" type;
12898 if so, when they are equal, the row is within the screen. -- rms. */
12899 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
12900 return NULL;
12901
12902 /* If it is in this row, return this row. */
12903 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
12904 || (MATRIX_ROW_END_CHARPOS (row) == charpos
12905 /* The end position of a row equals the start
12906 position of the next row. If CHARPOS is there, we
12907 would rather display it in the next line, except
12908 when this line ends in ZV. */
12909 && !row->ends_at_zv_p
12910 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
12911 && charpos >= MATRIX_ROW_START_CHARPOS (row))
12912 return row;
12913 ++row;
12914 }
12915 }
12916
12917
12918 /* Try to redisplay window W by reusing its existing display. W's
12919 current matrix must be up to date when this function is called,
12920 i.e. window_end_valid must not be nil.
12921
12922 Value is
12923
12924 1 if display has been updated
12925 0 if otherwise unsuccessful
12926 -1 if redisplay with same window start is known not to succeed
12927
12928 The following steps are performed:
12929
12930 1. Find the last row in the current matrix of W that is not
12931 affected by changes at the start of current_buffer. If no such row
12932 is found, give up.
12933
12934 2. Find the first row in W's current matrix that is not affected by
12935 changes at the end of current_buffer. Maybe there is no such row.
12936
12937 3. Display lines beginning with the row + 1 found in step 1 to the
12938 row found in step 2 or, if step 2 didn't find a row, to the end of
12939 the window.
12940
12941 4. If cursor is not known to appear on the window, give up.
12942
12943 5. If display stopped at the row found in step 2, scroll the
12944 display and current matrix as needed.
12945
12946 6. Maybe display some lines at the end of W, if we must. This can
12947 happen under various circumstances, like a partially visible line
12948 becoming fully visible, or because newly displayed lines are displayed
12949 in smaller font sizes.
12950
12951 7. Update W's window end information. */
12952
12953 static int
12954 try_window_id (w)
12955 struct window *w;
12956 {
12957 struct frame *f = XFRAME (w->frame);
12958 struct glyph_matrix *current_matrix = w->current_matrix;
12959 struct glyph_matrix *desired_matrix = w->desired_matrix;
12960 struct glyph_row *last_unchanged_at_beg_row;
12961 struct glyph_row *first_unchanged_at_end_row;
12962 struct glyph_row *row;
12963 struct glyph_row *bottom_row;
12964 int bottom_vpos;
12965 struct it it;
12966 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
12967 struct text_pos start_pos;
12968 struct run run;
12969 int first_unchanged_at_end_vpos = 0;
12970 struct glyph_row *last_text_row, *last_text_row_at_end;
12971 struct text_pos start;
12972 int first_changed_charpos, last_changed_charpos;
12973
12974 #if GLYPH_DEBUG
12975 if (inhibit_try_window_id)
12976 return 0;
12977 #endif
12978
12979 /* This is handy for debugging. */
12980 #if 0
12981 #define GIVE_UP(X) \
12982 do { \
12983 fprintf (stderr, "try_window_id give up %d\n", (X)); \
12984 return 0; \
12985 } while (0)
12986 #else
12987 #define GIVE_UP(X) return 0
12988 #endif
12989
12990 SET_TEXT_POS_FROM_MARKER (start, w->start);
12991
12992 /* Don't use this for mini-windows because these can show
12993 messages and mini-buffers, and we don't handle that here. */
12994 if (MINI_WINDOW_P (w))
12995 GIVE_UP (1);
12996
12997 /* This flag is used to prevent redisplay optimizations. */
12998 if (windows_or_buffers_changed || cursor_type_changed)
12999 GIVE_UP (2);
13000
13001 /* Verify that narrowing has not changed.
13002 Also verify that we were not told to prevent redisplay optimizations.
13003 It would be nice to further
13004 reduce the number of cases where this prevents try_window_id. */
13005 if (current_buffer->clip_changed
13006 || current_buffer->prevent_redisplay_optimizations_p)
13007 GIVE_UP (3);
13008
13009 /* Window must either use window-based redisplay or be full width. */
13010 if (!FRAME_WINDOW_P (f)
13011 && (!FRAME_LINE_INS_DEL_OK (f)
13012 || !WINDOW_FULL_WIDTH_P (w)))
13013 GIVE_UP (4);
13014
13015 /* Give up if point is not known NOT to appear in W. */
13016 if (PT < CHARPOS (start))
13017 GIVE_UP (5);
13018
13019 /* Another way to prevent redisplay optimizations. */
13020 if (XFASTINT (w->last_modified) == 0)
13021 GIVE_UP (6);
13022
13023 /* Verify that window is not hscrolled. */
13024 if (XFASTINT (w->hscroll) != 0)
13025 GIVE_UP (7);
13026
13027 /* Verify that display wasn't paused. */
13028 if (NILP (w->window_end_valid))
13029 GIVE_UP (8);
13030
13031 /* Can't use this if highlighting a region because a cursor movement
13032 will do more than just set the cursor. */
13033 if (!NILP (Vtransient_mark_mode)
13034 && !NILP (current_buffer->mark_active))
13035 GIVE_UP (9);
13036
13037 /* Likewise if highlighting trailing whitespace. */
13038 if (!NILP (Vshow_trailing_whitespace))
13039 GIVE_UP (11);
13040
13041 /* Likewise if showing a region. */
13042 if (!NILP (w->region_showing))
13043 GIVE_UP (10);
13044
13045 /* Can use this if overlay arrow position and or string have changed. */
13046 if (overlay_arrows_changed_p ())
13047 GIVE_UP (12);
13048
13049
13050 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
13051 only if buffer has really changed. The reason is that the gap is
13052 initially at Z for freshly visited files. The code below would
13053 set end_unchanged to 0 in that case. */
13054 if (MODIFF > SAVE_MODIFF
13055 /* This seems to happen sometimes after saving a buffer. */
13056 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
13057 {
13058 if (GPT - BEG < BEG_UNCHANGED)
13059 BEG_UNCHANGED = GPT - BEG;
13060 if (Z - GPT < END_UNCHANGED)
13061 END_UNCHANGED = Z - GPT;
13062 }
13063
13064 /* The position of the first and last character that has been changed. */
13065 first_changed_charpos = BEG + BEG_UNCHANGED;
13066 last_changed_charpos = Z - END_UNCHANGED;
13067
13068 /* If window starts after a line end, and the last change is in
13069 front of that newline, then changes don't affect the display.
13070 This case happens with stealth-fontification. Note that although
13071 the display is unchanged, glyph positions in the matrix have to
13072 be adjusted, of course. */
13073 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
13074 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
13075 && ((last_changed_charpos < CHARPOS (start)
13076 && CHARPOS (start) == BEGV)
13077 || (last_changed_charpos < CHARPOS (start) - 1
13078 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
13079 {
13080 int Z_old, delta, Z_BYTE_old, delta_bytes;
13081 struct glyph_row *r0;
13082
13083 /* Compute how many chars/bytes have been added to or removed
13084 from the buffer. */
13085 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
13086 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
13087 delta = Z - Z_old;
13088 delta_bytes = Z_BYTE - Z_BYTE_old;
13089
13090 /* Give up if PT is not in the window. Note that it already has
13091 been checked at the start of try_window_id that PT is not in
13092 front of the window start. */
13093 if (PT >= MATRIX_ROW_END_CHARPOS (row) + delta)
13094 GIVE_UP (13);
13095
13096 /* If window start is unchanged, we can reuse the whole matrix
13097 as is, after adjusting glyph positions. No need to compute
13098 the window end again, since its offset from Z hasn't changed. */
13099 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
13100 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + delta
13101 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + delta_bytes
13102 /* PT must not be in a partially visible line. */
13103 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + delta
13104 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
13105 {
13106 /* Adjust positions in the glyph matrix. */
13107 if (delta || delta_bytes)
13108 {
13109 struct glyph_row *r1
13110 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
13111 increment_matrix_positions (w->current_matrix,
13112 MATRIX_ROW_VPOS (r0, current_matrix),
13113 MATRIX_ROW_VPOS (r1, current_matrix),
13114 delta, delta_bytes);
13115 }
13116
13117 /* Set the cursor. */
13118 row = row_containing_pos (w, PT, r0, NULL, 0);
13119 if (row)
13120 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
13121 else
13122 abort ();
13123 return 1;
13124 }
13125 }
13126
13127 /* Handle the case that changes are all below what is displayed in
13128 the window, and that PT is in the window. This shortcut cannot
13129 be taken if ZV is visible in the window, and text has been added
13130 there that is visible in the window. */
13131 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
13132 /* ZV is not visible in the window, or there are no
13133 changes at ZV, actually. */
13134 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
13135 || first_changed_charpos == last_changed_charpos))
13136 {
13137 struct glyph_row *r0;
13138
13139 /* Give up if PT is not in the window. Note that it already has
13140 been checked at the start of try_window_id that PT is not in
13141 front of the window start. */
13142 if (PT >= MATRIX_ROW_END_CHARPOS (row))
13143 GIVE_UP (14);
13144
13145 /* If window start is unchanged, we can reuse the whole matrix
13146 as is, without changing glyph positions since no text has
13147 been added/removed in front of the window end. */
13148 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
13149 if (TEXT_POS_EQUAL_P (start, r0->start.pos)
13150 /* PT must not be in a partially visible line. */
13151 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
13152 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
13153 {
13154 /* We have to compute the window end anew since text
13155 can have been added/removed after it. */
13156 w->window_end_pos
13157 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
13158 w->window_end_bytepos
13159 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
13160
13161 /* Set the cursor. */
13162 row = row_containing_pos (w, PT, r0, NULL, 0);
13163 if (row)
13164 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
13165 else
13166 abort ();
13167 return 2;
13168 }
13169 }
13170
13171 /* Give up if window start is in the changed area.
13172
13173 The condition used to read
13174
13175 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
13176
13177 but why that was tested escapes me at the moment. */
13178 if (CHARPOS (start) >= first_changed_charpos
13179 && CHARPOS (start) <= last_changed_charpos)
13180 GIVE_UP (15);
13181
13182 /* Check that window start agrees with the start of the first glyph
13183 row in its current matrix. Check this after we know the window
13184 start is not in changed text, otherwise positions would not be
13185 comparable. */
13186 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
13187 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
13188 GIVE_UP (16);
13189
13190 /* Give up if the window ends in strings. Overlay strings
13191 at the end are difficult to handle, so don't try. */
13192 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
13193 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
13194 GIVE_UP (20);
13195
13196 /* Compute the position at which we have to start displaying new
13197 lines. Some of the lines at the top of the window might be
13198 reusable because they are not displaying changed text. Find the
13199 last row in W's current matrix not affected by changes at the
13200 start of current_buffer. Value is null if changes start in the
13201 first line of window. */
13202 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
13203 if (last_unchanged_at_beg_row)
13204 {
13205 /* Avoid starting to display in the moddle of a character, a TAB
13206 for instance. This is easier than to set up the iterator
13207 exactly, and it's not a frequent case, so the additional
13208 effort wouldn't really pay off. */
13209 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
13210 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
13211 && last_unchanged_at_beg_row > w->current_matrix->rows)
13212 --last_unchanged_at_beg_row;
13213
13214 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
13215 GIVE_UP (17);
13216
13217 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
13218 GIVE_UP (18);
13219 start_pos = it.current.pos;
13220
13221 /* Start displaying new lines in the desired matrix at the same
13222 vpos we would use in the current matrix, i.e. below
13223 last_unchanged_at_beg_row. */
13224 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
13225 current_matrix);
13226 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
13227 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
13228
13229 xassert (it.hpos == 0 && it.current_x == 0);
13230 }
13231 else
13232 {
13233 /* There are no reusable lines at the start of the window.
13234 Start displaying in the first text line. */
13235 start_display (&it, w, start);
13236 it.vpos = it.first_vpos;
13237 start_pos = it.current.pos;
13238 }
13239
13240 /* Find the first row that is not affected by changes at the end of
13241 the buffer. Value will be null if there is no unchanged row, in
13242 which case we must redisplay to the end of the window. delta
13243 will be set to the value by which buffer positions beginning with
13244 first_unchanged_at_end_row have to be adjusted due to text
13245 changes. */
13246 first_unchanged_at_end_row
13247 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
13248 IF_DEBUG (debug_delta = delta);
13249 IF_DEBUG (debug_delta_bytes = delta_bytes);
13250
13251 /* Set stop_pos to the buffer position up to which we will have to
13252 display new lines. If first_unchanged_at_end_row != NULL, this
13253 is the buffer position of the start of the line displayed in that
13254 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
13255 that we don't stop at a buffer position. */
13256 stop_pos = 0;
13257 if (first_unchanged_at_end_row)
13258 {
13259 xassert (last_unchanged_at_beg_row == NULL
13260 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
13261
13262 /* If this is a continuation line, move forward to the next one
13263 that isn't. Changes in lines above affect this line.
13264 Caution: this may move first_unchanged_at_end_row to a row
13265 not displaying text. */
13266 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
13267 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
13268 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
13269 < it.last_visible_y))
13270 ++first_unchanged_at_end_row;
13271
13272 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
13273 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
13274 >= it.last_visible_y))
13275 first_unchanged_at_end_row = NULL;
13276 else
13277 {
13278 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
13279 + delta);
13280 first_unchanged_at_end_vpos
13281 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
13282 xassert (stop_pos >= Z - END_UNCHANGED);
13283 }
13284 }
13285 else if (last_unchanged_at_beg_row == NULL)
13286 GIVE_UP (19);
13287
13288
13289 #if GLYPH_DEBUG
13290
13291 /* Either there is no unchanged row at the end, or the one we have
13292 now displays text. This is a necessary condition for the window
13293 end pos calculation at the end of this function. */
13294 xassert (first_unchanged_at_end_row == NULL
13295 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
13296
13297 debug_last_unchanged_at_beg_vpos
13298 = (last_unchanged_at_beg_row
13299 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
13300 : -1);
13301 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
13302
13303 #endif /* GLYPH_DEBUG != 0 */
13304
13305
13306 /* Display new lines. Set last_text_row to the last new line
13307 displayed which has text on it, i.e. might end up as being the
13308 line where the window_end_vpos is. */
13309 w->cursor.vpos = -1;
13310 last_text_row = NULL;
13311 overlay_arrow_seen = 0;
13312 while (it.current_y < it.last_visible_y
13313 && !fonts_changed_p
13314 && (first_unchanged_at_end_row == NULL
13315 || IT_CHARPOS (it) < stop_pos))
13316 {
13317 if (display_line (&it))
13318 last_text_row = it.glyph_row - 1;
13319 }
13320
13321 if (fonts_changed_p)
13322 return -1;
13323
13324
13325 /* Compute differences in buffer positions, y-positions etc. for
13326 lines reused at the bottom of the window. Compute what we can
13327 scroll. */
13328 if (first_unchanged_at_end_row
13329 /* No lines reused because we displayed everything up to the
13330 bottom of the window. */
13331 && it.current_y < it.last_visible_y)
13332 {
13333 dvpos = (it.vpos
13334 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
13335 current_matrix));
13336 dy = it.current_y - first_unchanged_at_end_row->y;
13337 run.current_y = first_unchanged_at_end_row->y;
13338 run.desired_y = run.current_y + dy;
13339 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
13340 }
13341 else
13342 {
13343 delta = dvpos = dy = run.current_y = run.desired_y = run.height = 0;
13344 first_unchanged_at_end_row = NULL;
13345 }
13346 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
13347
13348
13349 /* Find the cursor if not already found. We have to decide whether
13350 PT will appear on this window (it sometimes doesn't, but this is
13351 not a very frequent case.) This decision has to be made before
13352 the current matrix is altered. A value of cursor.vpos < 0 means
13353 that PT is either in one of the lines beginning at
13354 first_unchanged_at_end_row or below the window. Don't care for
13355 lines that might be displayed later at the window end; as
13356 mentioned, this is not a frequent case. */
13357 if (w->cursor.vpos < 0)
13358 {
13359 /* Cursor in unchanged rows at the top? */
13360 if (PT < CHARPOS (start_pos)
13361 && last_unchanged_at_beg_row)
13362 {
13363 row = row_containing_pos (w, PT,
13364 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
13365 last_unchanged_at_beg_row + 1, 0);
13366 if (row)
13367 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13368 }
13369
13370 /* Start from first_unchanged_at_end_row looking for PT. */
13371 else if (first_unchanged_at_end_row)
13372 {
13373 row = row_containing_pos (w, PT - delta,
13374 first_unchanged_at_end_row, NULL, 0);
13375 if (row)
13376 set_cursor_from_row (w, row, w->current_matrix, delta,
13377 delta_bytes, dy, dvpos);
13378 }
13379
13380 /* Give up if cursor was not found. */
13381 if (w->cursor.vpos < 0)
13382 {
13383 clear_glyph_matrix (w->desired_matrix);
13384 return -1;
13385 }
13386 }
13387
13388 /* Don't let the cursor end in the scroll margins. */
13389 {
13390 int this_scroll_margin, cursor_height;
13391
13392 this_scroll_margin = max (0, scroll_margin);
13393 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
13394 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
13395 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
13396
13397 if ((w->cursor.y < this_scroll_margin
13398 && CHARPOS (start) > BEGV)
13399 /* Old redisplay didn't take scroll margin into account at the bottom,
13400 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
13401 || w->cursor.y + cursor_height + this_scroll_margin > it.last_visible_y)
13402 {
13403 w->cursor.vpos = -1;
13404 clear_glyph_matrix (w->desired_matrix);
13405 return -1;
13406 }
13407 }
13408
13409 /* Scroll the display. Do it before changing the current matrix so
13410 that xterm.c doesn't get confused about where the cursor glyph is
13411 found. */
13412 if (dy && run.height)
13413 {
13414 update_begin (f);
13415
13416 if (FRAME_WINDOW_P (f))
13417 {
13418 FRAME_RIF (f)->update_window_begin_hook (w);
13419 FRAME_RIF (f)->clear_window_mouse_face (w);
13420 FRAME_RIF (f)->scroll_run_hook (w, &run);
13421 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
13422 }
13423 else
13424 {
13425 /* Terminal frame. In this case, dvpos gives the number of
13426 lines to scroll by; dvpos < 0 means scroll up. */
13427 int first_unchanged_at_end_vpos
13428 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
13429 int from = WINDOW_TOP_EDGE_LINE (w) + first_unchanged_at_end_vpos;
13430 int end = (WINDOW_TOP_EDGE_LINE (w)
13431 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
13432 + window_internal_height (w));
13433
13434 /* Perform the operation on the screen. */
13435 if (dvpos > 0)
13436 {
13437 /* Scroll last_unchanged_at_beg_row to the end of the
13438 window down dvpos lines. */
13439 set_terminal_window (f, end);
13440
13441 /* On dumb terminals delete dvpos lines at the end
13442 before inserting dvpos empty lines. */
13443 if (!FRAME_SCROLL_REGION_OK (f))
13444 ins_del_lines (f, end - dvpos, -dvpos);
13445
13446 /* Insert dvpos empty lines in front of
13447 last_unchanged_at_beg_row. */
13448 ins_del_lines (f, from, dvpos);
13449 }
13450 else if (dvpos < 0)
13451 {
13452 /* Scroll up last_unchanged_at_beg_vpos to the end of
13453 the window to last_unchanged_at_beg_vpos - |dvpos|. */
13454 set_terminal_window (f, end);
13455
13456 /* Delete dvpos lines in front of
13457 last_unchanged_at_beg_vpos. ins_del_lines will set
13458 the cursor to the given vpos and emit |dvpos| delete
13459 line sequences. */
13460 ins_del_lines (f, from + dvpos, dvpos);
13461
13462 /* On a dumb terminal insert dvpos empty lines at the
13463 end. */
13464 if (!FRAME_SCROLL_REGION_OK (f))
13465 ins_del_lines (f, end + dvpos, -dvpos);
13466 }
13467
13468 set_terminal_window (f, 0);
13469 }
13470
13471 update_end (f);
13472 }
13473
13474 /* Shift reused rows of the current matrix to the right position.
13475 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
13476 text. */
13477 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
13478 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
13479 if (dvpos < 0)
13480 {
13481 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
13482 bottom_vpos, dvpos);
13483 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
13484 bottom_vpos, 0);
13485 }
13486 else if (dvpos > 0)
13487 {
13488 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
13489 bottom_vpos, dvpos);
13490 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
13491 first_unchanged_at_end_vpos + dvpos, 0);
13492 }
13493
13494 /* For frame-based redisplay, make sure that current frame and window
13495 matrix are in sync with respect to glyph memory. */
13496 if (!FRAME_WINDOW_P (f))
13497 sync_frame_with_window_matrix_rows (w);
13498
13499 /* Adjust buffer positions in reused rows. */
13500 if (delta)
13501 increment_matrix_positions (current_matrix,
13502 first_unchanged_at_end_vpos + dvpos,
13503 bottom_vpos, delta, delta_bytes);
13504
13505 /* Adjust Y positions. */
13506 if (dy)
13507 shift_glyph_matrix (w, current_matrix,
13508 first_unchanged_at_end_vpos + dvpos,
13509 bottom_vpos, dy);
13510
13511 if (first_unchanged_at_end_row)
13512 first_unchanged_at_end_row += dvpos;
13513
13514 /* If scrolling up, there may be some lines to display at the end of
13515 the window. */
13516 last_text_row_at_end = NULL;
13517 if (dy < 0)
13518 {
13519 /* Scrolling up can leave for example a partially visible line
13520 at the end of the window to be redisplayed. */
13521 /* Set last_row to the glyph row in the current matrix where the
13522 window end line is found. It has been moved up or down in
13523 the matrix by dvpos. */
13524 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
13525 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
13526
13527 /* If last_row is the window end line, it should display text. */
13528 xassert (last_row->displays_text_p);
13529
13530 /* If window end line was partially visible before, begin
13531 displaying at that line. Otherwise begin displaying with the
13532 line following it. */
13533 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
13534 {
13535 init_to_row_start (&it, w, last_row);
13536 it.vpos = last_vpos;
13537 it.current_y = last_row->y;
13538 }
13539 else
13540 {
13541 init_to_row_end (&it, w, last_row);
13542 it.vpos = 1 + last_vpos;
13543 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
13544 ++last_row;
13545 }
13546
13547 /* We may start in a continuation line. If so, we have to
13548 get the right continuation_lines_width and current_x. */
13549 it.continuation_lines_width = last_row->continuation_lines_width;
13550 it.hpos = it.current_x = 0;
13551
13552 /* Display the rest of the lines at the window end. */
13553 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
13554 while (it.current_y < it.last_visible_y
13555 && !fonts_changed_p)
13556 {
13557 /* Is it always sure that the display agrees with lines in
13558 the current matrix? I don't think so, so we mark rows
13559 displayed invalid in the current matrix by setting their
13560 enabled_p flag to zero. */
13561 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
13562 if (display_line (&it))
13563 last_text_row_at_end = it.glyph_row - 1;
13564 }
13565 }
13566
13567 /* Update window_end_pos and window_end_vpos. */
13568 if (first_unchanged_at_end_row
13569 && first_unchanged_at_end_row->y < it.last_visible_y
13570 && !last_text_row_at_end)
13571 {
13572 /* Window end line if one of the preserved rows from the current
13573 matrix. Set row to the last row displaying text in current
13574 matrix starting at first_unchanged_at_end_row, after
13575 scrolling. */
13576 xassert (first_unchanged_at_end_row->displays_text_p);
13577 row = find_last_row_displaying_text (w->current_matrix, &it,
13578 first_unchanged_at_end_row);
13579 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
13580
13581 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
13582 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
13583 w->window_end_vpos
13584 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
13585 xassert (w->window_end_bytepos >= 0);
13586 IF_DEBUG (debug_method_add (w, "A"));
13587 }
13588 else if (last_text_row_at_end)
13589 {
13590 w->window_end_pos
13591 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
13592 w->window_end_bytepos
13593 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
13594 w->window_end_vpos
13595 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
13596 xassert (w->window_end_bytepos >= 0);
13597 IF_DEBUG (debug_method_add (w, "B"));
13598 }
13599 else if (last_text_row)
13600 {
13601 /* We have displayed either to the end of the window or at the
13602 end of the window, i.e. the last row with text is to be found
13603 in the desired matrix. */
13604 w->window_end_pos
13605 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
13606 w->window_end_bytepos
13607 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
13608 w->window_end_vpos
13609 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
13610 xassert (w->window_end_bytepos >= 0);
13611 }
13612 else if (first_unchanged_at_end_row == NULL
13613 && last_text_row == NULL
13614 && last_text_row_at_end == NULL)
13615 {
13616 /* Displayed to end of window, but no line containing text was
13617 displayed. Lines were deleted at the end of the window. */
13618 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
13619 int vpos = XFASTINT (w->window_end_vpos);
13620 struct glyph_row *current_row = current_matrix->rows + vpos;
13621 struct glyph_row *desired_row = desired_matrix->rows + vpos;
13622
13623 for (row = NULL;
13624 row == NULL && vpos >= first_vpos;
13625 --vpos, --current_row, --desired_row)
13626 {
13627 if (desired_row->enabled_p)
13628 {
13629 if (desired_row->displays_text_p)
13630 row = desired_row;
13631 }
13632 else if (current_row->displays_text_p)
13633 row = current_row;
13634 }
13635
13636 xassert (row != NULL);
13637 w->window_end_vpos = make_number (vpos + 1);
13638 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
13639 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
13640 xassert (w->window_end_bytepos >= 0);
13641 IF_DEBUG (debug_method_add (w, "C"));
13642 }
13643 else
13644 abort ();
13645
13646 #if 0 /* This leads to problems, for instance when the cursor is
13647 at ZV, and the cursor line displays no text. */
13648 /* Disable rows below what's displayed in the window. This makes
13649 debugging easier. */
13650 enable_glyph_matrix_rows (current_matrix,
13651 XFASTINT (w->window_end_vpos) + 1,
13652 bottom_vpos, 0);
13653 #endif
13654
13655 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
13656 debug_end_vpos = XFASTINT (w->window_end_vpos));
13657
13658 /* Record that display has not been completed. */
13659 w->window_end_valid = Qnil;
13660 w->desired_matrix->no_scrolling_p = 1;
13661 return 3;
13662
13663 #undef GIVE_UP
13664 }
13665
13666
13667 \f
13668 /***********************************************************************
13669 More debugging support
13670 ***********************************************************************/
13671
13672 #if GLYPH_DEBUG
13673
13674 void dump_glyph_row P_ ((struct glyph_row *, int, int));
13675 void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
13676 void dump_glyph P_ ((struct glyph_row *, struct glyph *, int));
13677
13678
13679 /* Dump the contents of glyph matrix MATRIX on stderr.
13680
13681 GLYPHS 0 means don't show glyph contents.
13682 GLYPHS 1 means show glyphs in short form
13683 GLYPHS > 1 means show glyphs in long form. */
13684
13685 void
13686 dump_glyph_matrix (matrix, glyphs)
13687 struct glyph_matrix *matrix;
13688 int glyphs;
13689 {
13690 int i;
13691 for (i = 0; i < matrix->nrows; ++i)
13692 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
13693 }
13694
13695
13696 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
13697 the glyph row and area where the glyph comes from. */
13698
13699 void
13700 dump_glyph (row, glyph, area)
13701 struct glyph_row *row;
13702 struct glyph *glyph;
13703 int area;
13704 {
13705 if (glyph->type == CHAR_GLYPH)
13706 {
13707 fprintf (stderr,
13708 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
13709 glyph - row->glyphs[TEXT_AREA],
13710 'C',
13711 glyph->charpos,
13712 (BUFFERP (glyph->object)
13713 ? 'B'
13714 : (STRINGP (glyph->object)
13715 ? 'S'
13716 : '-')),
13717 glyph->pixel_width,
13718 glyph->u.ch,
13719 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
13720 ? glyph->u.ch
13721 : '.'),
13722 glyph->face_id,
13723 glyph->left_box_line_p,
13724 glyph->right_box_line_p);
13725 }
13726 else if (glyph->type == STRETCH_GLYPH)
13727 {
13728 fprintf (stderr,
13729 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
13730 glyph - row->glyphs[TEXT_AREA],
13731 'S',
13732 glyph->charpos,
13733 (BUFFERP (glyph->object)
13734 ? 'B'
13735 : (STRINGP (glyph->object)
13736 ? 'S'
13737 : '-')),
13738 glyph->pixel_width,
13739 0,
13740 '.',
13741 glyph->face_id,
13742 glyph->left_box_line_p,
13743 glyph->right_box_line_p);
13744 }
13745 else if (glyph->type == IMAGE_GLYPH)
13746 {
13747 fprintf (stderr,
13748 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
13749 glyph - row->glyphs[TEXT_AREA],
13750 'I',
13751 glyph->charpos,
13752 (BUFFERP (glyph->object)
13753 ? 'B'
13754 : (STRINGP (glyph->object)
13755 ? 'S'
13756 : '-')),
13757 glyph->pixel_width,
13758 glyph->u.img_id,
13759 '.',
13760 glyph->face_id,
13761 glyph->left_box_line_p,
13762 glyph->right_box_line_p);
13763 }
13764 }
13765
13766
13767 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
13768 GLYPHS 0 means don't show glyph contents.
13769 GLYPHS 1 means show glyphs in short form
13770 GLYPHS > 1 means show glyphs in long form. */
13771
13772 void
13773 dump_glyph_row (row, vpos, glyphs)
13774 struct glyph_row *row;
13775 int vpos, glyphs;
13776 {
13777 if (glyphs != 1)
13778 {
13779 fprintf (stderr, "Row Start End Used oEI><O\\CTZFesm X Y W H V A P\n");
13780 fprintf (stderr, "=======================================================================\n");
13781
13782 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d%1.1d\
13783 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
13784 vpos,
13785 MATRIX_ROW_START_CHARPOS (row),
13786 MATRIX_ROW_END_CHARPOS (row),
13787 row->used[TEXT_AREA],
13788 row->contains_overlapping_glyphs_p,
13789 row->enabled_p,
13790 row->truncated_on_left_p,
13791 row->truncated_on_right_p,
13792 row->overlay_arrow_p,
13793 row->continued_p,
13794 MATRIX_ROW_CONTINUATION_LINE_P (row),
13795 row->displays_text_p,
13796 row->ends_at_zv_p,
13797 row->fill_line_p,
13798 row->ends_in_middle_of_char_p,
13799 row->starts_in_middle_of_char_p,
13800 row->mouse_face_p,
13801 row->x,
13802 row->y,
13803 row->pixel_width,
13804 row->height,
13805 row->visible_height,
13806 row->ascent,
13807 row->phys_ascent);
13808 fprintf (stderr, "%9d %5d\t%5d\n", row->start.overlay_string_index,
13809 row->end.overlay_string_index,
13810 row->continuation_lines_width);
13811 fprintf (stderr, "%9d %5d\n",
13812 CHARPOS (row->start.string_pos),
13813 CHARPOS (row->end.string_pos));
13814 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
13815 row->end.dpvec_index);
13816 }
13817
13818 if (glyphs > 1)
13819 {
13820 int area;
13821
13822 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
13823 {
13824 struct glyph *glyph = row->glyphs[area];
13825 struct glyph *glyph_end = glyph + row->used[area];
13826
13827 /* Glyph for a line end in text. */
13828 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
13829 ++glyph_end;
13830
13831 if (glyph < glyph_end)
13832 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
13833
13834 for (; glyph < glyph_end; ++glyph)
13835 dump_glyph (row, glyph, area);
13836 }
13837 }
13838 else if (glyphs == 1)
13839 {
13840 int area;
13841
13842 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
13843 {
13844 char *s = (char *) alloca (row->used[area] + 1);
13845 int i;
13846
13847 for (i = 0; i < row->used[area]; ++i)
13848 {
13849 struct glyph *glyph = row->glyphs[area] + i;
13850 if (glyph->type == CHAR_GLYPH
13851 && glyph->u.ch < 0x80
13852 && glyph->u.ch >= ' ')
13853 s[i] = glyph->u.ch;
13854 else
13855 s[i] = '.';
13856 }
13857
13858 s[i] = '\0';
13859 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
13860 }
13861 }
13862 }
13863
13864
13865 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
13866 Sdump_glyph_matrix, 0, 1, "p",
13867 doc: /* Dump the current matrix of the selected window to stderr.
13868 Shows contents of glyph row structures. With non-nil
13869 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
13870 glyphs in short form, otherwise show glyphs in long form. */)
13871 (glyphs)
13872 Lisp_Object glyphs;
13873 {
13874 struct window *w = XWINDOW (selected_window);
13875 struct buffer *buffer = XBUFFER (w->buffer);
13876
13877 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
13878 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
13879 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
13880 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
13881 fprintf (stderr, "=============================================\n");
13882 dump_glyph_matrix (w->current_matrix,
13883 NILP (glyphs) ? 0 : XINT (glyphs));
13884 return Qnil;
13885 }
13886
13887
13888 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
13889 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
13890 ()
13891 {
13892 struct frame *f = XFRAME (selected_frame);
13893 dump_glyph_matrix (f->current_matrix, 1);
13894 return Qnil;
13895 }
13896
13897
13898 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
13899 doc: /* Dump glyph row ROW to stderr.
13900 GLYPH 0 means don't dump glyphs.
13901 GLYPH 1 means dump glyphs in short form.
13902 GLYPH > 1 or omitted means dump glyphs in long form. */)
13903 (row, glyphs)
13904 Lisp_Object row, glyphs;
13905 {
13906 struct glyph_matrix *matrix;
13907 int vpos;
13908
13909 CHECK_NUMBER (row);
13910 matrix = XWINDOW (selected_window)->current_matrix;
13911 vpos = XINT (row);
13912 if (vpos >= 0 && vpos < matrix->nrows)
13913 dump_glyph_row (MATRIX_ROW (matrix, vpos),
13914 vpos,
13915 INTEGERP (glyphs) ? XINT (glyphs) : 2);
13916 return Qnil;
13917 }
13918
13919
13920 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
13921 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
13922 GLYPH 0 means don't dump glyphs.
13923 GLYPH 1 means dump glyphs in short form.
13924 GLYPH > 1 or omitted means dump glyphs in long form. */)
13925 (row, glyphs)
13926 Lisp_Object row, glyphs;
13927 {
13928 struct frame *sf = SELECTED_FRAME ();
13929 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
13930 int vpos;
13931
13932 CHECK_NUMBER (row);
13933 vpos = XINT (row);
13934 if (vpos >= 0 && vpos < m->nrows)
13935 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
13936 INTEGERP (glyphs) ? XINT (glyphs) : 2);
13937 return Qnil;
13938 }
13939
13940
13941 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
13942 doc: /* Toggle tracing of redisplay.
13943 With ARG, turn tracing on if and only if ARG is positive. */)
13944 (arg)
13945 Lisp_Object arg;
13946 {
13947 if (NILP (arg))
13948 trace_redisplay_p = !trace_redisplay_p;
13949 else
13950 {
13951 arg = Fprefix_numeric_value (arg);
13952 trace_redisplay_p = XINT (arg) > 0;
13953 }
13954
13955 return Qnil;
13956 }
13957
13958
13959 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
13960 doc: /* Like `format', but print result to stderr.
13961 usage: (trace-to-stderr STRING &rest OBJECTS) */)
13962 (nargs, args)
13963 int nargs;
13964 Lisp_Object *args;
13965 {
13966 Lisp_Object s = Fformat (nargs, args);
13967 fprintf (stderr, "%s", SDATA (s));
13968 return Qnil;
13969 }
13970
13971 #endif /* GLYPH_DEBUG */
13972
13973
13974 \f
13975 /***********************************************************************
13976 Building Desired Matrix Rows
13977 ***********************************************************************/
13978
13979 /* Return a temporary glyph row holding the glyphs of an overlay
13980 arrow. Only used for non-window-redisplay windows. */
13981
13982 static struct glyph_row *
13983 get_overlay_arrow_glyph_row (w, overlay_arrow_string)
13984 struct window *w;
13985 Lisp_Object overlay_arrow_string;
13986 {
13987 struct frame *f = XFRAME (WINDOW_FRAME (w));
13988 struct buffer *buffer = XBUFFER (w->buffer);
13989 struct buffer *old = current_buffer;
13990 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
13991 int arrow_len = SCHARS (overlay_arrow_string);
13992 const unsigned char *arrow_end = arrow_string + arrow_len;
13993 const unsigned char *p;
13994 struct it it;
13995 int multibyte_p;
13996 int n_glyphs_before;
13997
13998 set_buffer_temp (buffer);
13999 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
14000 it.glyph_row->used[TEXT_AREA] = 0;
14001 SET_TEXT_POS (it.position, 0, 0);
14002
14003 multibyte_p = !NILP (buffer->enable_multibyte_characters);
14004 p = arrow_string;
14005 while (p < arrow_end)
14006 {
14007 Lisp_Object face, ilisp;
14008
14009 /* Get the next character. */
14010 if (multibyte_p)
14011 it.c = string_char_and_length (p, arrow_len, &it.len);
14012 else
14013 it.c = *p, it.len = 1;
14014 p += it.len;
14015
14016 /* Get its face. */
14017 ilisp = make_number (p - arrow_string);
14018 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
14019 it.face_id = compute_char_face (f, it.c, face);
14020
14021 /* Compute its width, get its glyphs. */
14022 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
14023 SET_TEXT_POS (it.position, -1, -1);
14024 PRODUCE_GLYPHS (&it);
14025
14026 /* If this character doesn't fit any more in the line, we have
14027 to remove some glyphs. */
14028 if (it.current_x > it.last_visible_x)
14029 {
14030 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
14031 break;
14032 }
14033 }
14034
14035 set_buffer_temp (old);
14036 return it.glyph_row;
14037 }
14038
14039
14040 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
14041 glyphs are only inserted for terminal frames since we can't really
14042 win with truncation glyphs when partially visible glyphs are
14043 involved. Which glyphs to insert is determined by
14044 produce_special_glyphs. */
14045
14046 static void
14047 insert_left_trunc_glyphs (it)
14048 struct it *it;
14049 {
14050 struct it truncate_it;
14051 struct glyph *from, *end, *to, *toend;
14052
14053 xassert (!FRAME_WINDOW_P (it->f));
14054
14055 /* Get the truncation glyphs. */
14056 truncate_it = *it;
14057 truncate_it.current_x = 0;
14058 truncate_it.face_id = DEFAULT_FACE_ID;
14059 truncate_it.glyph_row = &scratch_glyph_row;
14060 truncate_it.glyph_row->used[TEXT_AREA] = 0;
14061 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
14062 truncate_it.object = make_number (0);
14063 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
14064
14065 /* Overwrite glyphs from IT with truncation glyphs. */
14066 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
14067 end = from + truncate_it.glyph_row->used[TEXT_AREA];
14068 to = it->glyph_row->glyphs[TEXT_AREA];
14069 toend = to + it->glyph_row->used[TEXT_AREA];
14070
14071 while (from < end)
14072 *to++ = *from++;
14073
14074 /* There may be padding glyphs left over. Overwrite them too. */
14075 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
14076 {
14077 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
14078 while (from < end)
14079 *to++ = *from++;
14080 }
14081
14082 if (to > toend)
14083 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
14084 }
14085
14086
14087 /* Compute the pixel height and width of IT->glyph_row.
14088
14089 Most of the time, ascent and height of a display line will be equal
14090 to the max_ascent and max_height values of the display iterator
14091 structure. This is not the case if
14092
14093 1. We hit ZV without displaying anything. In this case, max_ascent
14094 and max_height will be zero.
14095
14096 2. We have some glyphs that don't contribute to the line height.
14097 (The glyph row flag contributes_to_line_height_p is for future
14098 pixmap extensions).
14099
14100 The first case is easily covered by using default values because in
14101 these cases, the line height does not really matter, except that it
14102 must not be zero. */
14103
14104 static void
14105 compute_line_metrics (it)
14106 struct it *it;
14107 {
14108 struct glyph_row *row = it->glyph_row;
14109 int area, i;
14110
14111 if (FRAME_WINDOW_P (it->f))
14112 {
14113 int i, min_y, max_y;
14114
14115 /* The line may consist of one space only, that was added to
14116 place the cursor on it. If so, the row's height hasn't been
14117 computed yet. */
14118 if (row->height == 0)
14119 {
14120 if (it->max_ascent + it->max_descent == 0)
14121 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
14122 row->ascent = it->max_ascent;
14123 row->height = it->max_ascent + it->max_descent;
14124 row->phys_ascent = it->max_phys_ascent;
14125 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
14126 }
14127
14128 /* Compute the width of this line. */
14129 row->pixel_width = row->x;
14130 for (i = 0; i < row->used[TEXT_AREA]; ++i)
14131 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
14132
14133 xassert (row->pixel_width >= 0);
14134 xassert (row->ascent >= 0 && row->height > 0);
14135
14136 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
14137 || MATRIX_ROW_OVERLAPS_PRED_P (row));
14138
14139 /* If first line's physical ascent is larger than its logical
14140 ascent, use the physical ascent, and make the row taller.
14141 This makes accented characters fully visible. */
14142 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
14143 && row->phys_ascent > row->ascent)
14144 {
14145 row->height += row->phys_ascent - row->ascent;
14146 row->ascent = row->phys_ascent;
14147 }
14148
14149 /* Compute how much of the line is visible. */
14150 row->visible_height = row->height;
14151
14152 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
14153 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
14154
14155 if (row->y < min_y)
14156 row->visible_height -= min_y - row->y;
14157 if (row->y + row->height > max_y)
14158 row->visible_height -= row->y + row->height - max_y;
14159 }
14160 else
14161 {
14162 row->pixel_width = row->used[TEXT_AREA];
14163 if (row->continued_p)
14164 row->pixel_width -= it->continuation_pixel_width;
14165 else if (row->truncated_on_right_p)
14166 row->pixel_width -= it->truncation_pixel_width;
14167 row->ascent = row->phys_ascent = 0;
14168 row->height = row->phys_height = row->visible_height = 1;
14169 }
14170
14171 /* Compute a hash code for this row. */
14172 row->hash = 0;
14173 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
14174 for (i = 0; i < row->used[area]; ++i)
14175 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
14176 + row->glyphs[area][i].u.val
14177 + row->glyphs[area][i].face_id
14178 + row->glyphs[area][i].padding_p
14179 + (row->glyphs[area][i].type << 2));
14180
14181 it->max_ascent = it->max_descent = 0;
14182 it->max_phys_ascent = it->max_phys_descent = 0;
14183 }
14184
14185
14186 /* Append one space to the glyph row of iterator IT if doing a
14187 window-based redisplay. The space has the same face as
14188 IT->face_id. Value is non-zero if a space was added.
14189
14190 This function is called to make sure that there is always one glyph
14191 at the end of a glyph row that the cursor can be set on under
14192 window-systems. (If there weren't such a glyph we would not know
14193 how wide and tall a box cursor should be displayed).
14194
14195 At the same time this space let's a nicely handle clearing to the
14196 end of the line if the row ends in italic text. */
14197
14198 static int
14199 append_space_for_newline (it, default_face_p)
14200 struct it *it;
14201 int default_face_p;
14202 {
14203 if (FRAME_WINDOW_P (it->f))
14204 {
14205 int n = it->glyph_row->used[TEXT_AREA];
14206
14207 if (it->glyph_row->glyphs[TEXT_AREA] + n
14208 < it->glyph_row->glyphs[1 + TEXT_AREA])
14209 {
14210 /* Save some values that must not be changed.
14211 Must save IT->c and IT->len because otherwise
14212 ITERATOR_AT_END_P wouldn't work anymore after
14213 append_space_for_newline has been called. */
14214 enum display_element_type saved_what = it->what;
14215 int saved_c = it->c, saved_len = it->len;
14216 int saved_x = it->current_x;
14217 int saved_face_id = it->face_id;
14218 struct text_pos saved_pos;
14219 Lisp_Object saved_object;
14220 struct face *face;
14221
14222 saved_object = it->object;
14223 saved_pos = it->position;
14224
14225 it->what = IT_CHARACTER;
14226 bzero (&it->position, sizeof it->position);
14227 it->object = make_number (0);
14228 it->c = ' ';
14229 it->len = 1;
14230
14231 if (default_face_p)
14232 it->face_id = DEFAULT_FACE_ID;
14233 else if (it->face_before_selective_p)
14234 it->face_id = it->saved_face_id;
14235 face = FACE_FROM_ID (it->f, it->face_id);
14236 it->face_id = FACE_FOR_CHAR (it->f, face, 0);
14237
14238 PRODUCE_GLYPHS (it);
14239
14240 it->override_ascent = -1;
14241 it->constrain_row_ascent_descent_p = 0;
14242 it->current_x = saved_x;
14243 it->object = saved_object;
14244 it->position = saved_pos;
14245 it->what = saved_what;
14246 it->face_id = saved_face_id;
14247 it->len = saved_len;
14248 it->c = saved_c;
14249 return 1;
14250 }
14251 }
14252
14253 return 0;
14254 }
14255
14256
14257 /* Extend the face of the last glyph in the text area of IT->glyph_row
14258 to the end of the display line. Called from display_line.
14259 If the glyph row is empty, add a space glyph to it so that we
14260 know the face to draw. Set the glyph row flag fill_line_p. */
14261
14262 static void
14263 extend_face_to_end_of_line (it)
14264 struct it *it;
14265 {
14266 struct face *face;
14267 struct frame *f = it->f;
14268
14269 /* If line is already filled, do nothing. */
14270 if (it->current_x >= it->last_visible_x)
14271 return;
14272
14273 /* Face extension extends the background and box of IT->face_id
14274 to the end of the line. If the background equals the background
14275 of the frame, we don't have to do anything. */
14276 if (it->face_before_selective_p)
14277 face = FACE_FROM_ID (it->f, it->saved_face_id);
14278 else
14279 face = FACE_FROM_ID (f, it->face_id);
14280
14281 if (FRAME_WINDOW_P (f)
14282 && face->box == FACE_NO_BOX
14283 && face->background == FRAME_BACKGROUND_PIXEL (f)
14284 && !face->stipple)
14285 return;
14286
14287 /* Set the glyph row flag indicating that the face of the last glyph
14288 in the text area has to be drawn to the end of the text area. */
14289 it->glyph_row->fill_line_p = 1;
14290
14291 /* If current character of IT is not ASCII, make sure we have the
14292 ASCII face. This will be automatically undone the next time
14293 get_next_display_element returns a multibyte character. Note
14294 that the character will always be single byte in unibyte text. */
14295 if (!SINGLE_BYTE_CHAR_P (it->c))
14296 {
14297 it->face_id = FACE_FOR_CHAR (f, face, 0);
14298 }
14299
14300 if (FRAME_WINDOW_P (f))
14301 {
14302 /* If the row is empty, add a space with the current face of IT,
14303 so that we know which face to draw. */
14304 if (it->glyph_row->used[TEXT_AREA] == 0)
14305 {
14306 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
14307 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
14308 it->glyph_row->used[TEXT_AREA] = 1;
14309 }
14310 }
14311 else
14312 {
14313 /* Save some values that must not be changed. */
14314 int saved_x = it->current_x;
14315 struct text_pos saved_pos;
14316 Lisp_Object saved_object;
14317 enum display_element_type saved_what = it->what;
14318 int saved_face_id = it->face_id;
14319
14320 saved_object = it->object;
14321 saved_pos = it->position;
14322
14323 it->what = IT_CHARACTER;
14324 bzero (&it->position, sizeof it->position);
14325 it->object = make_number (0);
14326 it->c = ' ';
14327 it->len = 1;
14328 it->face_id = face->id;
14329
14330 PRODUCE_GLYPHS (it);
14331
14332 while (it->current_x <= it->last_visible_x)
14333 PRODUCE_GLYPHS (it);
14334
14335 /* Don't count these blanks really. It would let us insert a left
14336 truncation glyph below and make us set the cursor on them, maybe. */
14337 it->current_x = saved_x;
14338 it->object = saved_object;
14339 it->position = saved_pos;
14340 it->what = saved_what;
14341 it->face_id = saved_face_id;
14342 }
14343 }
14344
14345
14346 /* Value is non-zero if text starting at CHARPOS in current_buffer is
14347 trailing whitespace. */
14348
14349 static int
14350 trailing_whitespace_p (charpos)
14351 int charpos;
14352 {
14353 int bytepos = CHAR_TO_BYTE (charpos);
14354 int c = 0;
14355
14356 while (bytepos < ZV_BYTE
14357 && (c = FETCH_CHAR (bytepos),
14358 c == ' ' || c == '\t'))
14359 ++bytepos;
14360
14361 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
14362 {
14363 if (bytepos != PT_BYTE)
14364 return 1;
14365 }
14366 return 0;
14367 }
14368
14369
14370 /* Highlight trailing whitespace, if any, in ROW. */
14371
14372 void
14373 highlight_trailing_whitespace (f, row)
14374 struct frame *f;
14375 struct glyph_row *row;
14376 {
14377 int used = row->used[TEXT_AREA];
14378
14379 if (used)
14380 {
14381 struct glyph *start = row->glyphs[TEXT_AREA];
14382 struct glyph *glyph = start + used - 1;
14383
14384 /* Skip over glyphs inserted to display the cursor at the
14385 end of a line, for extending the face of the last glyph
14386 to the end of the line on terminals, and for truncation
14387 and continuation glyphs. */
14388 while (glyph >= start
14389 && glyph->type == CHAR_GLYPH
14390 && INTEGERP (glyph->object))
14391 --glyph;
14392
14393 /* If last glyph is a space or stretch, and it's trailing
14394 whitespace, set the face of all trailing whitespace glyphs in
14395 IT->glyph_row to `trailing-whitespace'. */
14396 if (glyph >= start
14397 && BUFFERP (glyph->object)
14398 && (glyph->type == STRETCH_GLYPH
14399 || (glyph->type == CHAR_GLYPH
14400 && glyph->u.ch == ' '))
14401 && trailing_whitespace_p (glyph->charpos))
14402 {
14403 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
14404
14405 while (glyph >= start
14406 && BUFFERP (glyph->object)
14407 && (glyph->type == STRETCH_GLYPH
14408 || (glyph->type == CHAR_GLYPH
14409 && glyph->u.ch == ' ')))
14410 (glyph--)->face_id = face_id;
14411 }
14412 }
14413 }
14414
14415
14416 /* Value is non-zero if glyph row ROW in window W should be
14417 used to hold the cursor. */
14418
14419 static int
14420 cursor_row_p (w, row)
14421 struct window *w;
14422 struct glyph_row *row;
14423 {
14424 int cursor_row_p = 1;
14425
14426 if (PT == MATRIX_ROW_END_CHARPOS (row))
14427 {
14428 /* If the row ends with a newline from a string, we don't want
14429 the cursor there (if the row is continued it doesn't end in a
14430 newline). */
14431 if (CHARPOS (row->end.string_pos) >= 0
14432 || MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
14433 cursor_row_p = row->continued_p;
14434
14435 /* If the row ends at ZV, display the cursor at the end of that
14436 row instead of at the start of the row below. */
14437 else if (row->ends_at_zv_p)
14438 cursor_row_p = 1;
14439 else
14440 cursor_row_p = 0;
14441 }
14442
14443 return cursor_row_p;
14444 }
14445
14446
14447 /* Construct the glyph row IT->glyph_row in the desired matrix of
14448 IT->w from text at the current position of IT. See dispextern.h
14449 for an overview of struct it. Value is non-zero if
14450 IT->glyph_row displays text, as opposed to a line displaying ZV
14451 only. */
14452
14453 static int
14454 display_line (it)
14455 struct it *it;
14456 {
14457 struct glyph_row *row = it->glyph_row;
14458 int overlay_arrow_bitmap;
14459 Lisp_Object overlay_arrow_string;
14460
14461 /* We always start displaying at hpos zero even if hscrolled. */
14462 xassert (it->hpos == 0 && it->current_x == 0);
14463
14464 /* We must not display in a row that's not a text row. */
14465 xassert (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
14466 < it->w->desired_matrix->nrows);
14467
14468 /* Is IT->w showing the region? */
14469 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
14470
14471 /* Clear the result glyph row and enable it. */
14472 prepare_desired_row (row);
14473
14474 row->y = it->current_y;
14475 row->start = it->start;
14476 row->continuation_lines_width = it->continuation_lines_width;
14477 row->displays_text_p = 1;
14478 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
14479 it->starts_in_middle_of_char_p = 0;
14480
14481 /* Arrange the overlays nicely for our purposes. Usually, we call
14482 display_line on only one line at a time, in which case this
14483 can't really hurt too much, or we call it on lines which appear
14484 one after another in the buffer, in which case all calls to
14485 recenter_overlay_lists but the first will be pretty cheap. */
14486 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
14487
14488 /* Move over display elements that are not visible because we are
14489 hscrolled. This may stop at an x-position < IT->first_visible_x
14490 if the first glyph is partially visible or if we hit a line end. */
14491 if (it->current_x < it->first_visible_x)
14492 move_it_in_display_line_to (it, ZV, it->first_visible_x,
14493 MOVE_TO_POS | MOVE_TO_X);
14494
14495 /* Get the initial row height. This is either the height of the
14496 text hscrolled, if there is any, or zero. */
14497 row->ascent = it->max_ascent;
14498 row->height = it->max_ascent + it->max_descent;
14499 row->phys_ascent = it->max_phys_ascent;
14500 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
14501
14502 /* Loop generating characters. The loop is left with IT on the next
14503 character to display. */
14504 while (1)
14505 {
14506 int n_glyphs_before, hpos_before, x_before;
14507 int x, i, nglyphs;
14508 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
14509
14510 /* Retrieve the next thing to display. Value is zero if end of
14511 buffer reached. */
14512 if (!get_next_display_element (it))
14513 {
14514 /* Maybe add a space at the end of this line that is used to
14515 display the cursor there under X. Set the charpos of the
14516 first glyph of blank lines not corresponding to any text
14517 to -1. */
14518 #ifdef HAVE_WINDOW_SYSTEM
14519 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
14520 row->exact_window_width_line_p = 1;
14521 else
14522 #endif /* HAVE_WINDOW_SYSTEM */
14523 if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
14524 || row->used[TEXT_AREA] == 0)
14525 {
14526 row->glyphs[TEXT_AREA]->charpos = -1;
14527 row->displays_text_p = 0;
14528
14529 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines)
14530 && (!MINI_WINDOW_P (it->w)
14531 || (minibuf_level && EQ (it->window, minibuf_window))))
14532 row->indicate_empty_line_p = 1;
14533 }
14534
14535 it->continuation_lines_width = 0;
14536 row->ends_at_zv_p = 1;
14537 break;
14538 }
14539
14540 /* Now, get the metrics of what we want to display. This also
14541 generates glyphs in `row' (which is IT->glyph_row). */
14542 n_glyphs_before = row->used[TEXT_AREA];
14543 x = it->current_x;
14544
14545 /* Remember the line height so far in case the next element doesn't
14546 fit on the line. */
14547 if (!it->truncate_lines_p)
14548 {
14549 ascent = it->max_ascent;
14550 descent = it->max_descent;
14551 phys_ascent = it->max_phys_ascent;
14552 phys_descent = it->max_phys_descent;
14553 }
14554
14555 PRODUCE_GLYPHS (it);
14556
14557 /* If this display element was in marginal areas, continue with
14558 the next one. */
14559 if (it->area != TEXT_AREA)
14560 {
14561 row->ascent = max (row->ascent, it->max_ascent);
14562 row->height = max (row->height, it->max_ascent + it->max_descent);
14563 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
14564 row->phys_height = max (row->phys_height,
14565 it->max_phys_ascent + it->max_phys_descent);
14566 set_iterator_to_next (it, 1);
14567 continue;
14568 }
14569
14570 /* Does the display element fit on the line? If we truncate
14571 lines, we should draw past the right edge of the window. If
14572 we don't truncate, we want to stop so that we can display the
14573 continuation glyph before the right margin. If lines are
14574 continued, there are two possible strategies for characters
14575 resulting in more than 1 glyph (e.g. tabs): Display as many
14576 glyphs as possible in this line and leave the rest for the
14577 continuation line, or display the whole element in the next
14578 line. Original redisplay did the former, so we do it also. */
14579 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
14580 hpos_before = it->hpos;
14581 x_before = x;
14582
14583 if (/* Not a newline. */
14584 nglyphs > 0
14585 /* Glyphs produced fit entirely in the line. */
14586 && it->current_x < it->last_visible_x)
14587 {
14588 it->hpos += nglyphs;
14589 row->ascent = max (row->ascent, it->max_ascent);
14590 row->height = max (row->height, it->max_ascent + it->max_descent);
14591 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
14592 row->phys_height = max (row->phys_height,
14593 it->max_phys_ascent + it->max_phys_descent);
14594 if (it->current_x - it->pixel_width < it->first_visible_x)
14595 row->x = x - it->first_visible_x;
14596 }
14597 else
14598 {
14599 int new_x;
14600 struct glyph *glyph;
14601
14602 for (i = 0; i < nglyphs; ++i, x = new_x)
14603 {
14604 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
14605 new_x = x + glyph->pixel_width;
14606
14607 if (/* Lines are continued. */
14608 !it->truncate_lines_p
14609 && (/* Glyph doesn't fit on the line. */
14610 new_x > it->last_visible_x
14611 /* Or it fits exactly on a window system frame. */
14612 || (new_x == it->last_visible_x
14613 && FRAME_WINDOW_P (it->f))))
14614 {
14615 /* End of a continued line. */
14616
14617 if (it->hpos == 0
14618 || (new_x == it->last_visible_x
14619 && FRAME_WINDOW_P (it->f)))
14620 {
14621 /* Current glyph is the only one on the line or
14622 fits exactly on the line. We must continue
14623 the line because we can't draw the cursor
14624 after the glyph. */
14625 row->continued_p = 1;
14626 it->current_x = new_x;
14627 it->continuation_lines_width += new_x;
14628 ++it->hpos;
14629 if (i == nglyphs - 1)
14630 {
14631 set_iterator_to_next (it, 1);
14632 #ifdef HAVE_WINDOW_SYSTEM
14633 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
14634 {
14635 if (!get_next_display_element (it))
14636 {
14637 row->exact_window_width_line_p = 1;
14638 it->continuation_lines_width = 0;
14639 row->continued_p = 0;
14640 row->ends_at_zv_p = 1;
14641 }
14642 else if (ITERATOR_AT_END_OF_LINE_P (it))
14643 {
14644 row->continued_p = 0;
14645 row->exact_window_width_line_p = 1;
14646 }
14647 }
14648 #endif /* HAVE_WINDOW_SYSTEM */
14649 }
14650 }
14651 else if (CHAR_GLYPH_PADDING_P (*glyph)
14652 && !FRAME_WINDOW_P (it->f))
14653 {
14654 /* A padding glyph that doesn't fit on this line.
14655 This means the whole character doesn't fit
14656 on the line. */
14657 row->used[TEXT_AREA] = n_glyphs_before;
14658
14659 /* Fill the rest of the row with continuation
14660 glyphs like in 20.x. */
14661 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
14662 < row->glyphs[1 + TEXT_AREA])
14663 produce_special_glyphs (it, IT_CONTINUATION);
14664
14665 row->continued_p = 1;
14666 it->current_x = x_before;
14667 it->continuation_lines_width += x_before;
14668
14669 /* Restore the height to what it was before the
14670 element not fitting on the line. */
14671 it->max_ascent = ascent;
14672 it->max_descent = descent;
14673 it->max_phys_ascent = phys_ascent;
14674 it->max_phys_descent = phys_descent;
14675 }
14676 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
14677 {
14678 /* A TAB that extends past the right edge of the
14679 window. This produces a single glyph on
14680 window system frames. We leave the glyph in
14681 this row and let it fill the row, but don't
14682 consume the TAB. */
14683 it->continuation_lines_width += it->last_visible_x;
14684 row->ends_in_middle_of_char_p = 1;
14685 row->continued_p = 1;
14686 glyph->pixel_width = it->last_visible_x - x;
14687 it->starts_in_middle_of_char_p = 1;
14688 }
14689 else
14690 {
14691 /* Something other than a TAB that draws past
14692 the right edge of the window. Restore
14693 positions to values before the element. */
14694 row->used[TEXT_AREA] = n_glyphs_before + i;
14695
14696 /* Display continuation glyphs. */
14697 if (!FRAME_WINDOW_P (it->f))
14698 produce_special_glyphs (it, IT_CONTINUATION);
14699 row->continued_p = 1;
14700
14701 it->continuation_lines_width += x;
14702
14703 if (nglyphs > 1 && i > 0)
14704 {
14705 row->ends_in_middle_of_char_p = 1;
14706 it->starts_in_middle_of_char_p = 1;
14707 }
14708
14709 /* Restore the height to what it was before the
14710 element not fitting on the line. */
14711 it->max_ascent = ascent;
14712 it->max_descent = descent;
14713 it->max_phys_ascent = phys_ascent;
14714 it->max_phys_descent = phys_descent;
14715 }
14716
14717 break;
14718 }
14719 else if (new_x > it->first_visible_x)
14720 {
14721 /* Increment number of glyphs actually displayed. */
14722 ++it->hpos;
14723
14724 if (x < it->first_visible_x)
14725 /* Glyph is partially visible, i.e. row starts at
14726 negative X position. */
14727 row->x = x - it->first_visible_x;
14728 }
14729 else
14730 {
14731 /* Glyph is completely off the left margin of the
14732 window. This should not happen because of the
14733 move_it_in_display_line at the start of this
14734 function, unless the text display area of the
14735 window is empty. */
14736 xassert (it->first_visible_x <= it->last_visible_x);
14737 }
14738 }
14739
14740 row->ascent = max (row->ascent, it->max_ascent);
14741 row->height = max (row->height, it->max_ascent + it->max_descent);
14742 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
14743 row->phys_height = max (row->phys_height,
14744 it->max_phys_ascent + it->max_phys_descent);
14745
14746 /* End of this display line if row is continued. */
14747 if (row->continued_p || row->ends_at_zv_p)
14748 break;
14749 }
14750
14751 at_end_of_line:
14752 /* Is this a line end? If yes, we're also done, after making
14753 sure that a non-default face is extended up to the right
14754 margin of the window. */
14755 if (ITERATOR_AT_END_OF_LINE_P (it))
14756 {
14757 int used_before = row->used[TEXT_AREA];
14758
14759 row->ends_in_newline_from_string_p = STRINGP (it->object);
14760
14761 #ifdef HAVE_WINDOW_SYSTEM
14762 /* Add a space at the end of the line that is used to
14763 display the cursor there. */
14764 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
14765 append_space_for_newline (it, 0);
14766 #endif /* HAVE_WINDOW_SYSTEM */
14767
14768 /* Extend the face to the end of the line. */
14769 extend_face_to_end_of_line (it);
14770
14771 /* Make sure we have the position. */
14772 if (used_before == 0)
14773 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
14774
14775 /* Consume the line end. This skips over invisible lines. */
14776 set_iterator_to_next (it, 1);
14777 it->continuation_lines_width = 0;
14778 break;
14779 }
14780
14781 /* Proceed with next display element. Note that this skips
14782 over lines invisible because of selective display. */
14783 set_iterator_to_next (it, 1);
14784
14785 /* If we truncate lines, we are done when the last displayed
14786 glyphs reach past the right margin of the window. */
14787 if (it->truncate_lines_p
14788 && (FRAME_WINDOW_P (it->f)
14789 ? (it->current_x >= it->last_visible_x)
14790 : (it->current_x > it->last_visible_x)))
14791 {
14792 /* Maybe add truncation glyphs. */
14793 if (!FRAME_WINDOW_P (it->f))
14794 {
14795 int i, n;
14796
14797 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
14798 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
14799 break;
14800
14801 for (n = row->used[TEXT_AREA]; i < n; ++i)
14802 {
14803 row->used[TEXT_AREA] = i;
14804 produce_special_glyphs (it, IT_TRUNCATION);
14805 }
14806 }
14807 #ifdef HAVE_WINDOW_SYSTEM
14808 else
14809 {
14810 /* Don't truncate if we can overflow newline into fringe. */
14811 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
14812 {
14813 if (!get_next_display_element (it))
14814 {
14815 #ifdef HAVE_WINDOW_SYSTEM
14816 it->continuation_lines_width = 0;
14817 row->ends_at_zv_p = 1;
14818 row->exact_window_width_line_p = 1;
14819 break;
14820 #endif /* HAVE_WINDOW_SYSTEM */
14821 }
14822 if (ITERATOR_AT_END_OF_LINE_P (it))
14823 {
14824 row->exact_window_width_line_p = 1;
14825 goto at_end_of_line;
14826 }
14827 }
14828 }
14829 #endif /* HAVE_WINDOW_SYSTEM */
14830
14831 row->truncated_on_right_p = 1;
14832 it->continuation_lines_width = 0;
14833 reseat_at_next_visible_line_start (it, 0);
14834 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
14835 it->hpos = hpos_before;
14836 it->current_x = x_before;
14837 break;
14838 }
14839 }
14840
14841 /* If line is not empty and hscrolled, maybe insert truncation glyphs
14842 at the left window margin. */
14843 if (it->first_visible_x
14844 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
14845 {
14846 if (!FRAME_WINDOW_P (it->f))
14847 insert_left_trunc_glyphs (it);
14848 row->truncated_on_left_p = 1;
14849 }
14850
14851 /* If the start of this line is the overlay arrow-position, then
14852 mark this glyph row as the one containing the overlay arrow.
14853 This is clearly a mess with variable size fonts. It would be
14854 better to let it be displayed like cursors under X. */
14855 if (! overlay_arrow_seen
14856 && (overlay_arrow_string
14857 = overlay_arrow_at_row (it->f, row, &overlay_arrow_bitmap),
14858 !NILP (overlay_arrow_string)))
14859 {
14860 /* Overlay arrow in window redisplay is a fringe bitmap. */
14861 if (!FRAME_WINDOW_P (it->f))
14862 {
14863 struct glyph_row *arrow_row
14864 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
14865 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
14866 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
14867 struct glyph *p = row->glyphs[TEXT_AREA];
14868 struct glyph *p2, *end;
14869
14870 /* Copy the arrow glyphs. */
14871 while (glyph < arrow_end)
14872 *p++ = *glyph++;
14873
14874 /* Throw away padding glyphs. */
14875 p2 = p;
14876 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
14877 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
14878 ++p2;
14879 if (p2 > p)
14880 {
14881 while (p2 < end)
14882 *p++ = *p2++;
14883 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
14884 }
14885 }
14886
14887 overlay_arrow_seen = 1;
14888 it->w->overlay_arrow_bitmap = overlay_arrow_bitmap;
14889 row->overlay_arrow_p = 1;
14890 }
14891
14892 /* Compute pixel dimensions of this line. */
14893 compute_line_metrics (it);
14894
14895 /* Remember the position at which this line ends. */
14896 row->end = it->current;
14897
14898 /* Save fringe bitmaps in this row. */
14899 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
14900 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
14901 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
14902 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
14903
14904 it->left_user_fringe_bitmap = 0;
14905 it->left_user_fringe_face_id = 0;
14906 it->right_user_fringe_bitmap = 0;
14907 it->right_user_fringe_face_id = 0;
14908
14909 /* Maybe set the cursor. */
14910 if (it->w->cursor.vpos < 0
14911 && PT >= MATRIX_ROW_START_CHARPOS (row)
14912 && PT <= MATRIX_ROW_END_CHARPOS (row)
14913 && cursor_row_p (it->w, row))
14914 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
14915
14916 /* Highlight trailing whitespace. */
14917 if (!NILP (Vshow_trailing_whitespace))
14918 highlight_trailing_whitespace (it->f, it->glyph_row);
14919
14920 /* Prepare for the next line. This line starts horizontally at (X
14921 HPOS) = (0 0). Vertical positions are incremented. As a
14922 convenience for the caller, IT->glyph_row is set to the next
14923 row to be used. */
14924 it->current_x = it->hpos = 0;
14925 it->current_y += row->height;
14926 ++it->vpos;
14927 ++it->glyph_row;
14928 it->start = it->current;
14929 return row->displays_text_p;
14930 }
14931
14932
14933 \f
14934 /***********************************************************************
14935 Menu Bar
14936 ***********************************************************************/
14937
14938 /* Redisplay the menu bar in the frame for window W.
14939
14940 The menu bar of X frames that don't have X toolkit support is
14941 displayed in a special window W->frame->menu_bar_window.
14942
14943 The menu bar of terminal frames is treated specially as far as
14944 glyph matrices are concerned. Menu bar lines are not part of
14945 windows, so the update is done directly on the frame matrix rows
14946 for the menu bar. */
14947
14948 static void
14949 display_menu_bar (w)
14950 struct window *w;
14951 {
14952 struct frame *f = XFRAME (WINDOW_FRAME (w));
14953 struct it it;
14954 Lisp_Object items;
14955 int i;
14956
14957 /* Don't do all this for graphical frames. */
14958 #ifdef HAVE_NTGUI
14959 if (!NILP (Vwindow_system))
14960 return;
14961 #endif
14962 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
14963 if (FRAME_X_P (f))
14964 return;
14965 #endif
14966 #ifdef MAC_OS
14967 if (FRAME_MAC_P (f))
14968 return;
14969 #endif
14970
14971 #ifdef USE_X_TOOLKIT
14972 xassert (!FRAME_WINDOW_P (f));
14973 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
14974 it.first_visible_x = 0;
14975 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
14976 #else /* not USE_X_TOOLKIT */
14977 if (FRAME_WINDOW_P (f))
14978 {
14979 /* Menu bar lines are displayed in the desired matrix of the
14980 dummy window menu_bar_window. */
14981 struct window *menu_w;
14982 xassert (WINDOWP (f->menu_bar_window));
14983 menu_w = XWINDOW (f->menu_bar_window);
14984 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
14985 MENU_FACE_ID);
14986 it.first_visible_x = 0;
14987 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
14988 }
14989 else
14990 {
14991 /* This is a TTY frame, i.e. character hpos/vpos are used as
14992 pixel x/y. */
14993 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
14994 MENU_FACE_ID);
14995 it.first_visible_x = 0;
14996 it.last_visible_x = FRAME_COLS (f);
14997 }
14998 #endif /* not USE_X_TOOLKIT */
14999
15000 if (! mode_line_inverse_video)
15001 /* Force the menu-bar to be displayed in the default face. */
15002 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
15003
15004 /* Clear all rows of the menu bar. */
15005 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
15006 {
15007 struct glyph_row *row = it.glyph_row + i;
15008 clear_glyph_row (row);
15009 row->enabled_p = 1;
15010 row->full_width_p = 1;
15011 }
15012
15013 /* Display all items of the menu bar. */
15014 items = FRAME_MENU_BAR_ITEMS (it.f);
15015 for (i = 0; i < XVECTOR (items)->size; i += 4)
15016 {
15017 Lisp_Object string;
15018
15019 /* Stop at nil string. */
15020 string = AREF (items, i + 1);
15021 if (NILP (string))
15022 break;
15023
15024 /* Remember where item was displayed. */
15025 AREF (items, i + 3) = make_number (it.hpos);
15026
15027 /* Display the item, pad with one space. */
15028 if (it.current_x < it.last_visible_x)
15029 display_string (NULL, string, Qnil, 0, 0, &it,
15030 SCHARS (string) + 1, 0, 0, -1);
15031 }
15032
15033 /* Fill out the line with spaces. */
15034 if (it.current_x < it.last_visible_x)
15035 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
15036
15037 /* Compute the total height of the lines. */
15038 compute_line_metrics (&it);
15039 }
15040
15041
15042 \f
15043 /***********************************************************************
15044 Mode Line
15045 ***********************************************************************/
15046
15047 /* Redisplay mode lines in the window tree whose root is WINDOW. If
15048 FORCE is non-zero, redisplay mode lines unconditionally.
15049 Otherwise, redisplay only mode lines that are garbaged. Value is
15050 the number of windows whose mode lines were redisplayed. */
15051
15052 static int
15053 redisplay_mode_lines (window, force)
15054 Lisp_Object window;
15055 int force;
15056 {
15057 int nwindows = 0;
15058
15059 while (!NILP (window))
15060 {
15061 struct window *w = XWINDOW (window);
15062
15063 if (WINDOWP (w->hchild))
15064 nwindows += redisplay_mode_lines (w->hchild, force);
15065 else if (WINDOWP (w->vchild))
15066 nwindows += redisplay_mode_lines (w->vchild, force);
15067 else if (force
15068 || FRAME_GARBAGED_P (XFRAME (w->frame))
15069 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
15070 {
15071 struct text_pos lpoint;
15072 struct buffer *old = current_buffer;
15073
15074 /* Set the window's buffer for the mode line display. */
15075 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15076 set_buffer_internal_1 (XBUFFER (w->buffer));
15077
15078 /* Point refers normally to the selected window. For any
15079 other window, set up appropriate value. */
15080 if (!EQ (window, selected_window))
15081 {
15082 struct text_pos pt;
15083
15084 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
15085 if (CHARPOS (pt) < BEGV)
15086 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
15087 else if (CHARPOS (pt) > (ZV - 1))
15088 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
15089 else
15090 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
15091 }
15092
15093 /* Display mode lines. */
15094 clear_glyph_matrix (w->desired_matrix);
15095 if (display_mode_lines (w))
15096 {
15097 ++nwindows;
15098 w->must_be_updated_p = 1;
15099 }
15100
15101 /* Restore old settings. */
15102 set_buffer_internal_1 (old);
15103 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
15104 }
15105
15106 window = w->next;
15107 }
15108
15109 return nwindows;
15110 }
15111
15112
15113 /* Display the mode and/or top line of window W. Value is the number
15114 of mode lines displayed. */
15115
15116 static int
15117 display_mode_lines (w)
15118 struct window *w;
15119 {
15120 Lisp_Object old_selected_window, old_selected_frame;
15121 int n = 0;
15122
15123 old_selected_frame = selected_frame;
15124 selected_frame = w->frame;
15125 old_selected_window = selected_window;
15126 XSETWINDOW (selected_window, w);
15127
15128 /* These will be set while the mode line specs are processed. */
15129 line_number_displayed = 0;
15130 w->column_number_displayed = Qnil;
15131
15132 if (WINDOW_WANTS_MODELINE_P (w))
15133 {
15134 struct window *sel_w = XWINDOW (old_selected_window);
15135
15136 /* Select mode line face based on the real selected window. */
15137 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
15138 current_buffer->mode_line_format);
15139 ++n;
15140 }
15141
15142 if (WINDOW_WANTS_HEADER_LINE_P (w))
15143 {
15144 display_mode_line (w, HEADER_LINE_FACE_ID,
15145 current_buffer->header_line_format);
15146 ++n;
15147 }
15148
15149 selected_frame = old_selected_frame;
15150 selected_window = old_selected_window;
15151 return n;
15152 }
15153
15154
15155 /* Display mode or top line of window W. FACE_ID specifies which line
15156 to display; it is either MODE_LINE_FACE_ID or HEADER_LINE_FACE_ID.
15157 FORMAT is the mode line format to display. Value is the pixel
15158 height of the mode line displayed. */
15159
15160 static int
15161 display_mode_line (w, face_id, format)
15162 struct window *w;
15163 enum face_id face_id;
15164 Lisp_Object format;
15165 {
15166 struct it it;
15167 struct face *face;
15168
15169 init_iterator (&it, w, -1, -1, NULL, face_id);
15170 prepare_desired_row (it.glyph_row);
15171
15172 it.glyph_row->mode_line_p = 1;
15173
15174 if (! mode_line_inverse_video)
15175 /* Force the mode-line to be displayed in the default face. */
15176 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
15177
15178 /* Temporarily make frame's keyboard the current kboard so that
15179 kboard-local variables in the mode_line_format will get the right
15180 values. */
15181 push_frame_kboard (it.f);
15182 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
15183 pop_frame_kboard ();
15184
15185 /* Fill up with spaces. */
15186 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
15187
15188 compute_line_metrics (&it);
15189 it.glyph_row->full_width_p = 1;
15190 it.glyph_row->continued_p = 0;
15191 it.glyph_row->truncated_on_left_p = 0;
15192 it.glyph_row->truncated_on_right_p = 0;
15193
15194 /* Make a 3D mode-line have a shadow at its right end. */
15195 face = FACE_FROM_ID (it.f, face_id);
15196 extend_face_to_end_of_line (&it);
15197 if (face->box != FACE_NO_BOX)
15198 {
15199 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
15200 + it.glyph_row->used[TEXT_AREA] - 1);
15201 last->right_box_line_p = 1;
15202 }
15203
15204 return it.glyph_row->height;
15205 }
15206
15207 /* Alist that caches the results of :propertize.
15208 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
15209 Lisp_Object mode_line_proptrans_alist;
15210
15211 /* List of strings making up the mode-line. */
15212 Lisp_Object mode_line_string_list;
15213
15214 /* Base face property when building propertized mode line string. */
15215 static Lisp_Object mode_line_string_face;
15216 static Lisp_Object mode_line_string_face_prop;
15217
15218
15219 /* Contribute ELT to the mode line for window IT->w. How it
15220 translates into text depends on its data type.
15221
15222 IT describes the display environment in which we display, as usual.
15223
15224 DEPTH is the depth in recursion. It is used to prevent
15225 infinite recursion here.
15226
15227 FIELD_WIDTH is the number of characters the display of ELT should
15228 occupy in the mode line, and PRECISION is the maximum number of
15229 characters to display from ELT's representation. See
15230 display_string for details.
15231
15232 Returns the hpos of the end of the text generated by ELT.
15233
15234 PROPS is a property list to add to any string we encounter.
15235
15236 If RISKY is nonzero, remove (disregard) any properties in any string
15237 we encounter, and ignore :eval and :propertize.
15238
15239 If the global variable `frame_title_ptr' is non-NULL, then the output
15240 is passed to `store_frame_title' instead of `display_string'. */
15241
15242 static int
15243 display_mode_element (it, depth, field_width, precision, elt, props, risky)
15244 struct it *it;
15245 int depth;
15246 int field_width, precision;
15247 Lisp_Object elt, props;
15248 int risky;
15249 {
15250 int n = 0, field, prec;
15251 int literal = 0;
15252
15253 tail_recurse:
15254 if (depth > 100)
15255 elt = build_string ("*too-deep*");
15256
15257 depth++;
15258
15259 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
15260 {
15261 case Lisp_String:
15262 {
15263 /* A string: output it and check for %-constructs within it. */
15264 unsigned char c;
15265 const unsigned char *this, *lisp_string;
15266
15267 if (!NILP (props) || risky)
15268 {
15269 Lisp_Object oprops, aelt;
15270 oprops = Ftext_properties_at (make_number (0), elt);
15271
15272 if (NILP (Fequal (props, oprops)) || risky)
15273 {
15274 /* If the starting string has properties,
15275 merge the specified ones onto the existing ones. */
15276 if (! NILP (oprops) && !risky)
15277 {
15278 Lisp_Object tem;
15279
15280 oprops = Fcopy_sequence (oprops);
15281 tem = props;
15282 while (CONSP (tem))
15283 {
15284 oprops = Fplist_put (oprops, XCAR (tem),
15285 XCAR (XCDR (tem)));
15286 tem = XCDR (XCDR (tem));
15287 }
15288 props = oprops;
15289 }
15290
15291 aelt = Fassoc (elt, mode_line_proptrans_alist);
15292 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
15293 {
15294 mode_line_proptrans_alist
15295 = Fcons (aelt, Fdelq (aelt, mode_line_proptrans_alist));
15296 elt = XCAR (aelt);
15297 }
15298 else
15299 {
15300 Lisp_Object tem;
15301
15302 elt = Fcopy_sequence (elt);
15303 Fset_text_properties (make_number (0), Flength (elt),
15304 props, elt);
15305 /* Add this item to mode_line_proptrans_alist. */
15306 mode_line_proptrans_alist
15307 = Fcons (Fcons (elt, props),
15308 mode_line_proptrans_alist);
15309 /* Truncate mode_line_proptrans_alist
15310 to at most 50 elements. */
15311 tem = Fnthcdr (make_number (50),
15312 mode_line_proptrans_alist);
15313 if (! NILP (tem))
15314 XSETCDR (tem, Qnil);
15315 }
15316 }
15317 }
15318
15319 this = SDATA (elt);
15320 lisp_string = this;
15321
15322 if (literal)
15323 {
15324 prec = precision - n;
15325 if (frame_title_ptr)
15326 n += store_frame_title (SDATA (elt), -1, prec);
15327 else if (!NILP (mode_line_string_list))
15328 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
15329 else
15330 n += display_string (NULL, elt, Qnil, 0, 0, it,
15331 0, prec, 0, STRING_MULTIBYTE (elt));
15332
15333 break;
15334 }
15335
15336 while ((precision <= 0 || n < precision)
15337 && *this
15338 && (frame_title_ptr
15339 || !NILP (mode_line_string_list)
15340 || it->current_x < it->last_visible_x))
15341 {
15342 const unsigned char *last = this;
15343
15344 /* Advance to end of string or next format specifier. */
15345 while ((c = *this++) != '\0' && c != '%')
15346 ;
15347
15348 if (this - 1 != last)
15349 {
15350 /* Output to end of string or up to '%'. Field width
15351 is length of string. Don't output more than
15352 PRECISION allows us. */
15353 --this;
15354
15355 prec = chars_in_text (last, this - last);
15356 if (precision > 0 && prec > precision - n)
15357 prec = precision - n;
15358
15359 if (frame_title_ptr)
15360 n += store_frame_title (last, 0, prec);
15361 else if (!NILP (mode_line_string_list))
15362 {
15363 int bytepos = last - lisp_string;
15364 int charpos = string_byte_to_char (elt, bytepos);
15365 n += store_mode_line_string (NULL,
15366 Fsubstring (elt, make_number (charpos),
15367 make_number (charpos + prec)),
15368 0, 0, 0, Qnil);
15369 }
15370 else
15371 {
15372 int bytepos = last - lisp_string;
15373 int charpos = string_byte_to_char (elt, bytepos);
15374 n += display_string (NULL, elt, Qnil, 0, charpos,
15375 it, 0, prec, 0,
15376 STRING_MULTIBYTE (elt));
15377 }
15378 }
15379 else /* c == '%' */
15380 {
15381 const unsigned char *percent_position = this;
15382
15383 /* Get the specified minimum width. Zero means
15384 don't pad. */
15385 field = 0;
15386 while ((c = *this++) >= '0' && c <= '9')
15387 field = field * 10 + c - '0';
15388
15389 /* Don't pad beyond the total padding allowed. */
15390 if (field_width - n > 0 && field > field_width - n)
15391 field = field_width - n;
15392
15393 /* Note that either PRECISION <= 0 or N < PRECISION. */
15394 prec = precision - n;
15395
15396 if (c == 'M')
15397 n += display_mode_element (it, depth, field, prec,
15398 Vglobal_mode_string, props,
15399 risky);
15400 else if (c != 0)
15401 {
15402 int multibyte;
15403 int bytepos, charpos;
15404 unsigned char *spec;
15405
15406 bytepos = percent_position - lisp_string;
15407 charpos = (STRING_MULTIBYTE (elt)
15408 ? string_byte_to_char (elt, bytepos)
15409 : bytepos);
15410
15411 spec
15412 = decode_mode_spec (it->w, c, field, prec, &multibyte);
15413
15414 if (frame_title_ptr)
15415 n += store_frame_title (spec, field, prec);
15416 else if (!NILP (mode_line_string_list))
15417 {
15418 int len = strlen (spec);
15419 Lisp_Object tem = make_string (spec, len);
15420 props = Ftext_properties_at (make_number (charpos), elt);
15421 /* Should only keep face property in props */
15422 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
15423 }
15424 else
15425 {
15426 int nglyphs_before, nwritten;
15427
15428 nglyphs_before = it->glyph_row->used[TEXT_AREA];
15429 nwritten = display_string (spec, Qnil, elt,
15430 charpos, 0, it,
15431 field, prec, 0,
15432 multibyte);
15433
15434 /* Assign to the glyphs written above the
15435 string where the `%x' came from, position
15436 of the `%'. */
15437 if (nwritten > 0)
15438 {
15439 struct glyph *glyph
15440 = (it->glyph_row->glyphs[TEXT_AREA]
15441 + nglyphs_before);
15442 int i;
15443
15444 for (i = 0; i < nwritten; ++i)
15445 {
15446 glyph[i].object = elt;
15447 glyph[i].charpos = charpos;
15448 }
15449
15450 n += nwritten;
15451 }
15452 }
15453 }
15454 else /* c == 0 */
15455 break;
15456 }
15457 }
15458 }
15459 break;
15460
15461 case Lisp_Symbol:
15462 /* A symbol: process the value of the symbol recursively
15463 as if it appeared here directly. Avoid error if symbol void.
15464 Special case: if value of symbol is a string, output the string
15465 literally. */
15466 {
15467 register Lisp_Object tem;
15468
15469 /* If the variable is not marked as risky to set
15470 then its contents are risky to use. */
15471 if (NILP (Fget (elt, Qrisky_local_variable)))
15472 risky = 1;
15473
15474 tem = Fboundp (elt);
15475 if (!NILP (tem))
15476 {
15477 tem = Fsymbol_value (elt);
15478 /* If value is a string, output that string literally:
15479 don't check for % within it. */
15480 if (STRINGP (tem))
15481 literal = 1;
15482
15483 if (!EQ (tem, elt))
15484 {
15485 /* Give up right away for nil or t. */
15486 elt = tem;
15487 goto tail_recurse;
15488 }
15489 }
15490 }
15491 break;
15492
15493 case Lisp_Cons:
15494 {
15495 register Lisp_Object car, tem;
15496
15497 /* A cons cell: five distinct cases.
15498 If first element is :eval or :propertize, do something special.
15499 If first element is a string or a cons, process all the elements
15500 and effectively concatenate them.
15501 If first element is a negative number, truncate displaying cdr to
15502 at most that many characters. If positive, pad (with spaces)
15503 to at least that many characters.
15504 If first element is a symbol, process the cadr or caddr recursively
15505 according to whether the symbol's value is non-nil or nil. */
15506 car = XCAR (elt);
15507 if (EQ (car, QCeval))
15508 {
15509 /* An element of the form (:eval FORM) means evaluate FORM
15510 and use the result as mode line elements. */
15511
15512 if (risky)
15513 break;
15514
15515 if (CONSP (XCDR (elt)))
15516 {
15517 Lisp_Object spec;
15518 spec = safe_eval (XCAR (XCDR (elt)));
15519 n += display_mode_element (it, depth, field_width - n,
15520 precision - n, spec, props,
15521 risky);
15522 }
15523 }
15524 else if (EQ (car, QCpropertize))
15525 {
15526 /* An element of the form (:propertize ELT PROPS...)
15527 means display ELT but applying properties PROPS. */
15528
15529 if (risky)
15530 break;
15531
15532 if (CONSP (XCDR (elt)))
15533 n += display_mode_element (it, depth, field_width - n,
15534 precision - n, XCAR (XCDR (elt)),
15535 XCDR (XCDR (elt)), risky);
15536 }
15537 else if (SYMBOLP (car))
15538 {
15539 tem = Fboundp (car);
15540 elt = XCDR (elt);
15541 if (!CONSP (elt))
15542 goto invalid;
15543 /* elt is now the cdr, and we know it is a cons cell.
15544 Use its car if CAR has a non-nil value. */
15545 if (!NILP (tem))
15546 {
15547 tem = Fsymbol_value (car);
15548 if (!NILP (tem))
15549 {
15550 elt = XCAR (elt);
15551 goto tail_recurse;
15552 }
15553 }
15554 /* Symbol's value is nil (or symbol is unbound)
15555 Get the cddr of the original list
15556 and if possible find the caddr and use that. */
15557 elt = XCDR (elt);
15558 if (NILP (elt))
15559 break;
15560 else if (!CONSP (elt))
15561 goto invalid;
15562 elt = XCAR (elt);
15563 goto tail_recurse;
15564 }
15565 else if (INTEGERP (car))
15566 {
15567 register int lim = XINT (car);
15568 elt = XCDR (elt);
15569 if (lim < 0)
15570 {
15571 /* Negative int means reduce maximum width. */
15572 if (precision <= 0)
15573 precision = -lim;
15574 else
15575 precision = min (precision, -lim);
15576 }
15577 else if (lim > 0)
15578 {
15579 /* Padding specified. Don't let it be more than
15580 current maximum. */
15581 if (precision > 0)
15582 lim = min (precision, lim);
15583
15584 /* If that's more padding than already wanted, queue it.
15585 But don't reduce padding already specified even if
15586 that is beyond the current truncation point. */
15587 field_width = max (lim, field_width);
15588 }
15589 goto tail_recurse;
15590 }
15591 else if (STRINGP (car) || CONSP (car))
15592 {
15593 register int limit = 50;
15594 /* Limit is to protect against circular lists. */
15595 while (CONSP (elt)
15596 && --limit > 0
15597 && (precision <= 0 || n < precision))
15598 {
15599 n += display_mode_element (it, depth, field_width - n,
15600 precision - n, XCAR (elt),
15601 props, risky);
15602 elt = XCDR (elt);
15603 }
15604 }
15605 }
15606 break;
15607
15608 default:
15609 invalid:
15610 elt = build_string ("*invalid*");
15611 goto tail_recurse;
15612 }
15613
15614 /* Pad to FIELD_WIDTH. */
15615 if (field_width > 0 && n < field_width)
15616 {
15617 if (frame_title_ptr)
15618 n += store_frame_title ("", field_width - n, 0);
15619 else if (!NILP (mode_line_string_list))
15620 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
15621 else
15622 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
15623 0, 0, 0);
15624 }
15625
15626 return n;
15627 }
15628
15629 /* Store a mode-line string element in mode_line_string_list.
15630
15631 If STRING is non-null, display that C string. Otherwise, the Lisp
15632 string LISP_STRING is displayed.
15633
15634 FIELD_WIDTH is the minimum number of output glyphs to produce.
15635 If STRING has fewer characters than FIELD_WIDTH, pad to the right
15636 with spaces. FIELD_WIDTH <= 0 means don't pad.
15637
15638 PRECISION is the maximum number of characters to output from
15639 STRING. PRECISION <= 0 means don't truncate the string.
15640
15641 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
15642 properties to the string.
15643
15644 PROPS are the properties to add to the string.
15645 The mode_line_string_face face property is always added to the string.
15646 */
15647
15648 static int store_mode_line_string (string, lisp_string, copy_string, field_width, precision, props)
15649 char *string;
15650 Lisp_Object lisp_string;
15651 int copy_string;
15652 int field_width;
15653 int precision;
15654 Lisp_Object props;
15655 {
15656 int len;
15657 int n = 0;
15658
15659 if (string != NULL)
15660 {
15661 len = strlen (string);
15662 if (precision > 0 && len > precision)
15663 len = precision;
15664 lisp_string = make_string (string, len);
15665 if (NILP (props))
15666 props = mode_line_string_face_prop;
15667 else if (!NILP (mode_line_string_face))
15668 {
15669 Lisp_Object face = Fplist_get (props, Qface);
15670 props = Fcopy_sequence (props);
15671 if (NILP (face))
15672 face = mode_line_string_face;
15673 else
15674 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
15675 props = Fplist_put (props, Qface, face);
15676 }
15677 Fadd_text_properties (make_number (0), make_number (len),
15678 props, lisp_string);
15679 }
15680 else
15681 {
15682 len = XFASTINT (Flength (lisp_string));
15683 if (precision > 0 && len > precision)
15684 {
15685 len = precision;
15686 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
15687 precision = -1;
15688 }
15689 if (!NILP (mode_line_string_face))
15690 {
15691 Lisp_Object face;
15692 if (NILP (props))
15693 props = Ftext_properties_at (make_number (0), lisp_string);
15694 face = Fplist_get (props, Qface);
15695 if (NILP (face))
15696 face = mode_line_string_face;
15697 else
15698 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
15699 props = Fcons (Qface, Fcons (face, Qnil));
15700 if (copy_string)
15701 lisp_string = Fcopy_sequence (lisp_string);
15702 }
15703 if (!NILP (props))
15704 Fadd_text_properties (make_number (0), make_number (len),
15705 props, lisp_string);
15706 }
15707
15708 if (len > 0)
15709 {
15710 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
15711 n += len;
15712 }
15713
15714 if (field_width > len)
15715 {
15716 field_width -= len;
15717 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
15718 if (!NILP (props))
15719 Fadd_text_properties (make_number (0), make_number (field_width),
15720 props, lisp_string);
15721 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
15722 n += field_width;
15723 }
15724
15725 return n;
15726 }
15727
15728
15729 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
15730 0, 3, 0,
15731 doc: /* Return the mode-line of selected window as a string.
15732 First optional arg FORMAT specifies a different format string (see
15733 `mode-line-format' for details) to use. If FORMAT is t, return
15734 the buffer's header-line. Second optional arg WINDOW specifies a
15735 different window to use as the context for the formatting.
15736 If third optional arg NO-PROPS is non-nil, string is not propertized. */)
15737 (format, window, no_props)
15738 Lisp_Object format, window, no_props;
15739 {
15740 struct it it;
15741 int len;
15742 struct window *w;
15743 struct buffer *old_buffer = NULL;
15744 enum face_id face_id = DEFAULT_FACE_ID;
15745
15746 if (NILP (window))
15747 window = selected_window;
15748 CHECK_WINDOW (window);
15749 w = XWINDOW (window);
15750 CHECK_BUFFER (w->buffer);
15751
15752 if (XBUFFER (w->buffer) != current_buffer)
15753 {
15754 old_buffer = current_buffer;
15755 set_buffer_internal_1 (XBUFFER (w->buffer));
15756 }
15757
15758 if (NILP (format) || EQ (format, Qt))
15759 {
15760 face_id = NILP (format)
15761 ? CURRENT_MODE_LINE_FACE_ID (w) :
15762 HEADER_LINE_FACE_ID;
15763 format = NILP (format)
15764 ? current_buffer->mode_line_format
15765 : current_buffer->header_line_format;
15766 }
15767
15768 init_iterator (&it, w, -1, -1, NULL, face_id);
15769
15770 if (NILP (no_props))
15771 {
15772 mode_line_string_face =
15773 (face_id == MODE_LINE_FACE_ID ? Qmode_line :
15774 face_id == MODE_LINE_INACTIVE_FACE_ID ? Qmode_line_inactive :
15775 face_id == HEADER_LINE_FACE_ID ? Qheader_line : Qnil);
15776
15777 mode_line_string_face_prop =
15778 NILP (mode_line_string_face) ? Qnil :
15779 Fcons (Qface, Fcons (mode_line_string_face, Qnil));
15780
15781 /* We need a dummy last element in mode_line_string_list to
15782 indicate we are building the propertized mode-line string.
15783 Using mode_line_string_face_prop here GC protects it. */
15784 mode_line_string_list =
15785 Fcons (mode_line_string_face_prop, Qnil);
15786 frame_title_ptr = NULL;
15787 }
15788 else
15789 {
15790 mode_line_string_face_prop = Qnil;
15791 mode_line_string_list = Qnil;
15792 frame_title_ptr = frame_title_buf;
15793 }
15794
15795 push_frame_kboard (it.f);
15796 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
15797 pop_frame_kboard ();
15798
15799 if (old_buffer)
15800 set_buffer_internal_1 (old_buffer);
15801
15802 if (NILP (no_props))
15803 {
15804 Lisp_Object str;
15805 mode_line_string_list = Fnreverse (mode_line_string_list);
15806 str = Fmapconcat (intern ("identity"), XCDR (mode_line_string_list),
15807 make_string ("", 0));
15808 mode_line_string_face_prop = Qnil;
15809 mode_line_string_list = Qnil;
15810 return str;
15811 }
15812
15813 len = frame_title_ptr - frame_title_buf;
15814 if (len > 0 && frame_title_ptr[-1] == '-')
15815 {
15816 /* Mode lines typically ends with numerous dashes; reduce to two dashes. */
15817 while (frame_title_ptr > frame_title_buf && *--frame_title_ptr == '-')
15818 ;
15819 frame_title_ptr += 3; /* restore last non-dash + two dashes */
15820 if (len > frame_title_ptr - frame_title_buf)
15821 len = frame_title_ptr - frame_title_buf;
15822 }
15823
15824 frame_title_ptr = NULL;
15825 return make_string (frame_title_buf, len);
15826 }
15827
15828 /* Write a null-terminated, right justified decimal representation of
15829 the positive integer D to BUF using a minimal field width WIDTH. */
15830
15831 static void
15832 pint2str (buf, width, d)
15833 register char *buf;
15834 register int width;
15835 register int d;
15836 {
15837 register char *p = buf;
15838
15839 if (d <= 0)
15840 *p++ = '0';
15841 else
15842 {
15843 while (d > 0)
15844 {
15845 *p++ = d % 10 + '0';
15846 d /= 10;
15847 }
15848 }
15849
15850 for (width -= (int) (p - buf); width > 0; --width)
15851 *p++ = ' ';
15852 *p-- = '\0';
15853 while (p > buf)
15854 {
15855 d = *buf;
15856 *buf++ = *p;
15857 *p-- = d;
15858 }
15859 }
15860
15861 /* Write a null-terminated, right justified decimal and "human
15862 readable" representation of the nonnegative integer D to BUF using
15863 a minimal field width WIDTH. D should be smaller than 999.5e24. */
15864
15865 static const char power_letter[] =
15866 {
15867 0, /* not used */
15868 'k', /* kilo */
15869 'M', /* mega */
15870 'G', /* giga */
15871 'T', /* tera */
15872 'P', /* peta */
15873 'E', /* exa */
15874 'Z', /* zetta */
15875 'Y' /* yotta */
15876 };
15877
15878 static void
15879 pint2hrstr (buf, width, d)
15880 char *buf;
15881 int width;
15882 int d;
15883 {
15884 /* We aim to represent the nonnegative integer D as
15885 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
15886 int quotient = d;
15887 int remainder = 0;
15888 /* -1 means: do not use TENTHS. */
15889 int tenths = -1;
15890 int exponent = 0;
15891
15892 /* Length of QUOTIENT.TENTHS as a string. */
15893 int length;
15894
15895 char * psuffix;
15896 char * p;
15897
15898 if (1000 <= quotient)
15899 {
15900 /* Scale to the appropriate EXPONENT. */
15901 do
15902 {
15903 remainder = quotient % 1000;
15904 quotient /= 1000;
15905 exponent++;
15906 }
15907 while (1000 <= quotient);
15908
15909 /* Round to nearest and decide whether to use TENTHS or not. */
15910 if (quotient <= 9)
15911 {
15912 tenths = remainder / 100;
15913 if (50 <= remainder % 100)
15914 if (tenths < 9)
15915 tenths++;
15916 else
15917 {
15918 quotient++;
15919 if (quotient == 10)
15920 tenths = -1;
15921 else
15922 tenths = 0;
15923 }
15924 }
15925 else
15926 if (500 <= remainder)
15927 if (quotient < 999)
15928 quotient++;
15929 else
15930 {
15931 quotient = 1;
15932 exponent++;
15933 tenths = 0;
15934 }
15935 }
15936
15937 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
15938 if (tenths == -1 && quotient <= 99)
15939 if (quotient <= 9)
15940 length = 1;
15941 else
15942 length = 2;
15943 else
15944 length = 3;
15945 p = psuffix = buf + max (width, length);
15946
15947 /* Print EXPONENT. */
15948 if (exponent)
15949 *psuffix++ = power_letter[exponent];
15950 *psuffix = '\0';
15951
15952 /* Print TENTHS. */
15953 if (tenths >= 0)
15954 {
15955 *--p = '0' + tenths;
15956 *--p = '.';
15957 }
15958
15959 /* Print QUOTIENT. */
15960 do
15961 {
15962 int digit = quotient % 10;
15963 *--p = '0' + digit;
15964 }
15965 while ((quotient /= 10) != 0);
15966
15967 /* Print leading spaces. */
15968 while (buf < p)
15969 *--p = ' ';
15970 }
15971
15972 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
15973 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
15974 type of CODING_SYSTEM. Return updated pointer into BUF. */
15975
15976 static unsigned char invalid_eol_type[] = "(*invalid*)";
15977
15978 static char *
15979 decode_mode_spec_coding (coding_system, buf, eol_flag)
15980 Lisp_Object coding_system;
15981 register char *buf;
15982 int eol_flag;
15983 {
15984 Lisp_Object val;
15985 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
15986 const unsigned char *eol_str;
15987 int eol_str_len;
15988 /* The EOL conversion we are using. */
15989 Lisp_Object eoltype;
15990
15991 val = Fget (coding_system, Qcoding_system);
15992 eoltype = Qnil;
15993
15994 if (!VECTORP (val)) /* Not yet decided. */
15995 {
15996 if (multibyte)
15997 *buf++ = '-';
15998 if (eol_flag)
15999 eoltype = eol_mnemonic_undecided;
16000 /* Don't mention EOL conversion if it isn't decided. */
16001 }
16002 else
16003 {
16004 Lisp_Object eolvalue;
16005
16006 eolvalue = Fget (coding_system, Qeol_type);
16007
16008 if (multibyte)
16009 *buf++ = XFASTINT (AREF (val, 1));
16010
16011 if (eol_flag)
16012 {
16013 /* The EOL conversion that is normal on this system. */
16014
16015 if (NILP (eolvalue)) /* Not yet decided. */
16016 eoltype = eol_mnemonic_undecided;
16017 else if (VECTORP (eolvalue)) /* Not yet decided. */
16018 eoltype = eol_mnemonic_undecided;
16019 else /* INTEGERP (eolvalue) -- 0:LF, 1:CRLF, 2:CR */
16020 eoltype = (XFASTINT (eolvalue) == 0
16021 ? eol_mnemonic_unix
16022 : (XFASTINT (eolvalue) == 1
16023 ? eol_mnemonic_dos : eol_mnemonic_mac));
16024 }
16025 }
16026
16027 if (eol_flag)
16028 {
16029 /* Mention the EOL conversion if it is not the usual one. */
16030 if (STRINGP (eoltype))
16031 {
16032 eol_str = SDATA (eoltype);
16033 eol_str_len = SBYTES (eoltype);
16034 }
16035 else if (INTEGERP (eoltype)
16036 && CHAR_VALID_P (XINT (eoltype), 0))
16037 {
16038 unsigned char *tmp = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
16039 eol_str_len = CHAR_STRING (XINT (eoltype), tmp);
16040 eol_str = tmp;
16041 }
16042 else
16043 {
16044 eol_str = invalid_eol_type;
16045 eol_str_len = sizeof (invalid_eol_type) - 1;
16046 }
16047 bcopy (eol_str, buf, eol_str_len);
16048 buf += eol_str_len;
16049 }
16050
16051 return buf;
16052 }
16053
16054 /* Return a string for the output of a mode line %-spec for window W,
16055 generated by character C. PRECISION >= 0 means don't return a
16056 string longer than that value. FIELD_WIDTH > 0 means pad the
16057 string returned with spaces to that value. Return 1 in *MULTIBYTE
16058 if the result is multibyte text. */
16059
16060 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
16061
16062 static char *
16063 decode_mode_spec (w, c, field_width, precision, multibyte)
16064 struct window *w;
16065 register int c;
16066 int field_width, precision;
16067 int *multibyte;
16068 {
16069 Lisp_Object obj;
16070 struct frame *f = XFRAME (WINDOW_FRAME (w));
16071 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
16072 struct buffer *b = XBUFFER (w->buffer);
16073
16074 obj = Qnil;
16075 *multibyte = 0;
16076
16077 switch (c)
16078 {
16079 case '*':
16080 if (!NILP (b->read_only))
16081 return "%";
16082 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
16083 return "*";
16084 return "-";
16085
16086 case '+':
16087 /* This differs from %* only for a modified read-only buffer. */
16088 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
16089 return "*";
16090 if (!NILP (b->read_only))
16091 return "%";
16092 return "-";
16093
16094 case '&':
16095 /* This differs from %* in ignoring read-only-ness. */
16096 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
16097 return "*";
16098 return "-";
16099
16100 case '%':
16101 return "%";
16102
16103 case '[':
16104 {
16105 int i;
16106 char *p;
16107
16108 if (command_loop_level > 5)
16109 return "[[[... ";
16110 p = decode_mode_spec_buf;
16111 for (i = 0; i < command_loop_level; i++)
16112 *p++ = '[';
16113 *p = 0;
16114 return decode_mode_spec_buf;
16115 }
16116
16117 case ']':
16118 {
16119 int i;
16120 char *p;
16121
16122 if (command_loop_level > 5)
16123 return " ...]]]";
16124 p = decode_mode_spec_buf;
16125 for (i = 0; i < command_loop_level; i++)
16126 *p++ = ']';
16127 *p = 0;
16128 return decode_mode_spec_buf;
16129 }
16130
16131 case '-':
16132 {
16133 register int i;
16134
16135 /* Let lots_of_dashes be a string of infinite length. */
16136 if (!NILP (mode_line_string_list))
16137 return "--";
16138 if (field_width <= 0
16139 || field_width > sizeof (lots_of_dashes))
16140 {
16141 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
16142 decode_mode_spec_buf[i] = '-';
16143 decode_mode_spec_buf[i] = '\0';
16144 return decode_mode_spec_buf;
16145 }
16146 else
16147 return lots_of_dashes;
16148 }
16149
16150 case 'b':
16151 obj = b->name;
16152 break;
16153
16154 case 'c':
16155 {
16156 int col = (int) current_column (); /* iftc */
16157 w->column_number_displayed = make_number (col);
16158 pint2str (decode_mode_spec_buf, field_width, col);
16159 return decode_mode_spec_buf;
16160 }
16161
16162 case 'F':
16163 /* %F displays the frame name. */
16164 if (!NILP (f->title))
16165 return (char *) SDATA (f->title);
16166 if (f->explicit_name || ! FRAME_WINDOW_P (f))
16167 return (char *) SDATA (f->name);
16168 return "Emacs";
16169
16170 case 'f':
16171 obj = b->filename;
16172 break;
16173
16174 case 'i':
16175 {
16176 int size = ZV - BEGV;
16177 pint2str (decode_mode_spec_buf, field_width, size);
16178 return decode_mode_spec_buf;
16179 }
16180
16181 case 'I':
16182 {
16183 int size = ZV - BEGV;
16184 pint2hrstr (decode_mode_spec_buf, field_width, size);
16185 return decode_mode_spec_buf;
16186 }
16187
16188 case 'l':
16189 {
16190 int startpos = XMARKER (w->start)->charpos;
16191 int startpos_byte = marker_byte_position (w->start);
16192 int line, linepos, linepos_byte, topline;
16193 int nlines, junk;
16194 int height = WINDOW_TOTAL_LINES (w);
16195
16196 /* If we decided that this buffer isn't suitable for line numbers,
16197 don't forget that too fast. */
16198 if (EQ (w->base_line_pos, w->buffer))
16199 goto no_value;
16200 /* But do forget it, if the window shows a different buffer now. */
16201 else if (BUFFERP (w->base_line_pos))
16202 w->base_line_pos = Qnil;
16203
16204 /* If the buffer is very big, don't waste time. */
16205 if (INTEGERP (Vline_number_display_limit)
16206 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
16207 {
16208 w->base_line_pos = Qnil;
16209 w->base_line_number = Qnil;
16210 goto no_value;
16211 }
16212
16213 if (!NILP (w->base_line_number)
16214 && !NILP (w->base_line_pos)
16215 && XFASTINT (w->base_line_pos) <= startpos)
16216 {
16217 line = XFASTINT (w->base_line_number);
16218 linepos = XFASTINT (w->base_line_pos);
16219 linepos_byte = buf_charpos_to_bytepos (b, linepos);
16220 }
16221 else
16222 {
16223 line = 1;
16224 linepos = BUF_BEGV (b);
16225 linepos_byte = BUF_BEGV_BYTE (b);
16226 }
16227
16228 /* Count lines from base line to window start position. */
16229 nlines = display_count_lines (linepos, linepos_byte,
16230 startpos_byte,
16231 startpos, &junk);
16232
16233 topline = nlines + line;
16234
16235 /* Determine a new base line, if the old one is too close
16236 or too far away, or if we did not have one.
16237 "Too close" means it's plausible a scroll-down would
16238 go back past it. */
16239 if (startpos == BUF_BEGV (b))
16240 {
16241 w->base_line_number = make_number (topline);
16242 w->base_line_pos = make_number (BUF_BEGV (b));
16243 }
16244 else if (nlines < height + 25 || nlines > height * 3 + 50
16245 || linepos == BUF_BEGV (b))
16246 {
16247 int limit = BUF_BEGV (b);
16248 int limit_byte = BUF_BEGV_BYTE (b);
16249 int position;
16250 int distance = (height * 2 + 30) * line_number_display_limit_width;
16251
16252 if (startpos - distance > limit)
16253 {
16254 limit = startpos - distance;
16255 limit_byte = CHAR_TO_BYTE (limit);
16256 }
16257
16258 nlines = display_count_lines (startpos, startpos_byte,
16259 limit_byte,
16260 - (height * 2 + 30),
16261 &position);
16262 /* If we couldn't find the lines we wanted within
16263 line_number_display_limit_width chars per line,
16264 give up on line numbers for this window. */
16265 if (position == limit_byte && limit == startpos - distance)
16266 {
16267 w->base_line_pos = w->buffer;
16268 w->base_line_number = Qnil;
16269 goto no_value;
16270 }
16271
16272 w->base_line_number = make_number (topline - nlines);
16273 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
16274 }
16275
16276 /* Now count lines from the start pos to point. */
16277 nlines = display_count_lines (startpos, startpos_byte,
16278 PT_BYTE, PT, &junk);
16279
16280 /* Record that we did display the line number. */
16281 line_number_displayed = 1;
16282
16283 /* Make the string to show. */
16284 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
16285 return decode_mode_spec_buf;
16286 no_value:
16287 {
16288 char* p = decode_mode_spec_buf;
16289 int pad = field_width - 2;
16290 while (pad-- > 0)
16291 *p++ = ' ';
16292 *p++ = '?';
16293 *p++ = '?';
16294 *p = '\0';
16295 return decode_mode_spec_buf;
16296 }
16297 }
16298 break;
16299
16300 case 'm':
16301 obj = b->mode_name;
16302 break;
16303
16304 case 'n':
16305 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
16306 return " Narrow";
16307 break;
16308
16309 case 'p':
16310 {
16311 int pos = marker_position (w->start);
16312 int total = BUF_ZV (b) - BUF_BEGV (b);
16313
16314 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
16315 {
16316 if (pos <= BUF_BEGV (b))
16317 return "All";
16318 else
16319 return "Bottom";
16320 }
16321 else if (pos <= BUF_BEGV (b))
16322 return "Top";
16323 else
16324 {
16325 if (total > 1000000)
16326 /* Do it differently for a large value, to avoid overflow. */
16327 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
16328 else
16329 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
16330 /* We can't normally display a 3-digit number,
16331 so get us a 2-digit number that is close. */
16332 if (total == 100)
16333 total = 99;
16334 sprintf (decode_mode_spec_buf, "%2d%%", total);
16335 return decode_mode_spec_buf;
16336 }
16337 }
16338
16339 /* Display percentage of size above the bottom of the screen. */
16340 case 'P':
16341 {
16342 int toppos = marker_position (w->start);
16343 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
16344 int total = BUF_ZV (b) - BUF_BEGV (b);
16345
16346 if (botpos >= BUF_ZV (b))
16347 {
16348 if (toppos <= BUF_BEGV (b))
16349 return "All";
16350 else
16351 return "Bottom";
16352 }
16353 else
16354 {
16355 if (total > 1000000)
16356 /* Do it differently for a large value, to avoid overflow. */
16357 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
16358 else
16359 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
16360 /* We can't normally display a 3-digit number,
16361 so get us a 2-digit number that is close. */
16362 if (total == 100)
16363 total = 99;
16364 if (toppos <= BUF_BEGV (b))
16365 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
16366 else
16367 sprintf (decode_mode_spec_buf, "%2d%%", total);
16368 return decode_mode_spec_buf;
16369 }
16370 }
16371
16372 case 's':
16373 /* status of process */
16374 obj = Fget_buffer_process (w->buffer);
16375 if (NILP (obj))
16376 return "no process";
16377 #ifdef subprocesses
16378 obj = Fsymbol_name (Fprocess_status (obj));
16379 #endif
16380 break;
16381
16382 case 't': /* indicate TEXT or BINARY */
16383 #ifdef MODE_LINE_BINARY_TEXT
16384 return MODE_LINE_BINARY_TEXT (b);
16385 #else
16386 return "T";
16387 #endif
16388
16389 case 'z':
16390 /* coding-system (not including end-of-line format) */
16391 case 'Z':
16392 /* coding-system (including end-of-line type) */
16393 {
16394 int eol_flag = (c == 'Z');
16395 char *p = decode_mode_spec_buf;
16396
16397 if (! FRAME_WINDOW_P (f))
16398 {
16399 /* No need to mention EOL here--the terminal never needs
16400 to do EOL conversion. */
16401 p = decode_mode_spec_coding (FRAME_KEYBOARD_CODING (f)->symbol, p, 0);
16402 p = decode_mode_spec_coding (FRAME_TERMINAL_CODING (f)->symbol, p, 0);
16403 }
16404 p = decode_mode_spec_coding (b->buffer_file_coding_system,
16405 p, eol_flag);
16406
16407 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
16408 #ifdef subprocesses
16409 obj = Fget_buffer_process (Fcurrent_buffer ());
16410 if (PROCESSP (obj))
16411 {
16412 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
16413 p, eol_flag);
16414 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
16415 p, eol_flag);
16416 }
16417 #endif /* subprocesses */
16418 #endif /* 0 */
16419 *p = 0;
16420 return decode_mode_spec_buf;
16421 }
16422 }
16423
16424 if (STRINGP (obj))
16425 {
16426 *multibyte = STRING_MULTIBYTE (obj);
16427 return (char *) SDATA (obj);
16428 }
16429 else
16430 return "";
16431 }
16432
16433
16434 /* Count up to COUNT lines starting from START / START_BYTE.
16435 But don't go beyond LIMIT_BYTE.
16436 Return the number of lines thus found (always nonnegative).
16437
16438 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
16439
16440 static int
16441 display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
16442 int start, start_byte, limit_byte, count;
16443 int *byte_pos_ptr;
16444 {
16445 register unsigned char *cursor;
16446 unsigned char *base;
16447
16448 register int ceiling;
16449 register unsigned char *ceiling_addr;
16450 int orig_count = count;
16451
16452 /* If we are not in selective display mode,
16453 check only for newlines. */
16454 int selective_display = (!NILP (current_buffer->selective_display)
16455 && !INTEGERP (current_buffer->selective_display));
16456
16457 if (count > 0)
16458 {
16459 while (start_byte < limit_byte)
16460 {
16461 ceiling = BUFFER_CEILING_OF (start_byte);
16462 ceiling = min (limit_byte - 1, ceiling);
16463 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
16464 base = (cursor = BYTE_POS_ADDR (start_byte));
16465 while (1)
16466 {
16467 if (selective_display)
16468 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
16469 ;
16470 else
16471 while (*cursor != '\n' && ++cursor != ceiling_addr)
16472 ;
16473
16474 if (cursor != ceiling_addr)
16475 {
16476 if (--count == 0)
16477 {
16478 start_byte += cursor - base + 1;
16479 *byte_pos_ptr = start_byte;
16480 return orig_count;
16481 }
16482 else
16483 if (++cursor == ceiling_addr)
16484 break;
16485 }
16486 else
16487 break;
16488 }
16489 start_byte += cursor - base;
16490 }
16491 }
16492 else
16493 {
16494 while (start_byte > limit_byte)
16495 {
16496 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
16497 ceiling = max (limit_byte, ceiling);
16498 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
16499 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
16500 while (1)
16501 {
16502 if (selective_display)
16503 while (--cursor != ceiling_addr
16504 && *cursor != '\n' && *cursor != 015)
16505 ;
16506 else
16507 while (--cursor != ceiling_addr && *cursor != '\n')
16508 ;
16509
16510 if (cursor != ceiling_addr)
16511 {
16512 if (++count == 0)
16513 {
16514 start_byte += cursor - base + 1;
16515 *byte_pos_ptr = start_byte;
16516 /* When scanning backwards, we should
16517 not count the newline posterior to which we stop. */
16518 return - orig_count - 1;
16519 }
16520 }
16521 else
16522 break;
16523 }
16524 /* Here we add 1 to compensate for the last decrement
16525 of CURSOR, which took it past the valid range. */
16526 start_byte += cursor - base + 1;
16527 }
16528 }
16529
16530 *byte_pos_ptr = limit_byte;
16531
16532 if (count < 0)
16533 return - orig_count + count;
16534 return orig_count - count;
16535
16536 }
16537
16538
16539 \f
16540 /***********************************************************************
16541 Displaying strings
16542 ***********************************************************************/
16543
16544 /* Display a NUL-terminated string, starting with index START.
16545
16546 If STRING is non-null, display that C string. Otherwise, the Lisp
16547 string LISP_STRING is displayed.
16548
16549 If FACE_STRING is not nil, FACE_STRING_POS is a position in
16550 FACE_STRING. Display STRING or LISP_STRING with the face at
16551 FACE_STRING_POS in FACE_STRING:
16552
16553 Display the string in the environment given by IT, but use the
16554 standard display table, temporarily.
16555
16556 FIELD_WIDTH is the minimum number of output glyphs to produce.
16557 If STRING has fewer characters than FIELD_WIDTH, pad to the right
16558 with spaces. If STRING has more characters, more than FIELD_WIDTH
16559 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
16560
16561 PRECISION is the maximum number of characters to output from
16562 STRING. PRECISION < 0 means don't truncate the string.
16563
16564 This is roughly equivalent to printf format specifiers:
16565
16566 FIELD_WIDTH PRECISION PRINTF
16567 ----------------------------------------
16568 -1 -1 %s
16569 -1 10 %.10s
16570 10 -1 %10s
16571 20 10 %20.10s
16572
16573 MULTIBYTE zero means do not display multibyte chars, > 0 means do
16574 display them, and < 0 means obey the current buffer's value of
16575 enable_multibyte_characters.
16576
16577 Value is the number of glyphs produced. */
16578
16579 static int
16580 display_string (string, lisp_string, face_string, face_string_pos,
16581 start, it, field_width, precision, max_x, multibyte)
16582 unsigned char *string;
16583 Lisp_Object lisp_string;
16584 Lisp_Object face_string;
16585 int face_string_pos;
16586 int start;
16587 struct it *it;
16588 int field_width, precision, max_x;
16589 int multibyte;
16590 {
16591 int hpos_at_start = it->hpos;
16592 int saved_face_id = it->face_id;
16593 struct glyph_row *row = it->glyph_row;
16594
16595 /* Initialize the iterator IT for iteration over STRING beginning
16596 with index START. */
16597 reseat_to_string (it, string, lisp_string, start,
16598 precision, field_width, multibyte);
16599
16600 /* If displaying STRING, set up the face of the iterator
16601 from LISP_STRING, if that's given. */
16602 if (STRINGP (face_string))
16603 {
16604 int endptr;
16605 struct face *face;
16606
16607 it->face_id
16608 = face_at_string_position (it->w, face_string, face_string_pos,
16609 0, it->region_beg_charpos,
16610 it->region_end_charpos,
16611 &endptr, it->base_face_id, 0);
16612 face = FACE_FROM_ID (it->f, it->face_id);
16613 it->face_box_p = face->box != FACE_NO_BOX;
16614 }
16615
16616 /* Set max_x to the maximum allowed X position. Don't let it go
16617 beyond the right edge of the window. */
16618 if (max_x <= 0)
16619 max_x = it->last_visible_x;
16620 else
16621 max_x = min (max_x, it->last_visible_x);
16622
16623 /* Skip over display elements that are not visible. because IT->w is
16624 hscrolled. */
16625 if (it->current_x < it->first_visible_x)
16626 move_it_in_display_line_to (it, 100000, it->first_visible_x,
16627 MOVE_TO_POS | MOVE_TO_X);
16628
16629 row->ascent = it->max_ascent;
16630 row->height = it->max_ascent + it->max_descent;
16631 row->phys_ascent = it->max_phys_ascent;
16632 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
16633
16634 /* This condition is for the case that we are called with current_x
16635 past last_visible_x. */
16636 while (it->current_x < max_x)
16637 {
16638 int x_before, x, n_glyphs_before, i, nglyphs;
16639
16640 /* Get the next display element. */
16641 if (!get_next_display_element (it))
16642 break;
16643
16644 /* Produce glyphs. */
16645 x_before = it->current_x;
16646 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
16647 PRODUCE_GLYPHS (it);
16648
16649 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
16650 i = 0;
16651 x = x_before;
16652 while (i < nglyphs)
16653 {
16654 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
16655
16656 if (!it->truncate_lines_p
16657 && x + glyph->pixel_width > max_x)
16658 {
16659 /* End of continued line or max_x reached. */
16660 if (CHAR_GLYPH_PADDING_P (*glyph))
16661 {
16662 /* A wide character is unbreakable. */
16663 it->glyph_row->used[TEXT_AREA] = n_glyphs_before;
16664 it->current_x = x_before;
16665 }
16666 else
16667 {
16668 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
16669 it->current_x = x;
16670 }
16671 break;
16672 }
16673 else if (x + glyph->pixel_width > it->first_visible_x)
16674 {
16675 /* Glyph is at least partially visible. */
16676 ++it->hpos;
16677 if (x < it->first_visible_x)
16678 it->glyph_row->x = x - it->first_visible_x;
16679 }
16680 else
16681 {
16682 /* Glyph is off the left margin of the display area.
16683 Should not happen. */
16684 abort ();
16685 }
16686
16687 row->ascent = max (row->ascent, it->max_ascent);
16688 row->height = max (row->height, it->max_ascent + it->max_descent);
16689 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
16690 row->phys_height = max (row->phys_height,
16691 it->max_phys_ascent + it->max_phys_descent);
16692 x += glyph->pixel_width;
16693 ++i;
16694 }
16695
16696 /* Stop if max_x reached. */
16697 if (i < nglyphs)
16698 break;
16699
16700 /* Stop at line ends. */
16701 if (ITERATOR_AT_END_OF_LINE_P (it))
16702 {
16703 it->continuation_lines_width = 0;
16704 break;
16705 }
16706
16707 set_iterator_to_next (it, 1);
16708
16709 /* Stop if truncating at the right edge. */
16710 if (it->truncate_lines_p
16711 && it->current_x >= it->last_visible_x)
16712 {
16713 /* Add truncation mark, but don't do it if the line is
16714 truncated at a padding space. */
16715 if (IT_CHARPOS (*it) < it->string_nchars)
16716 {
16717 if (!FRAME_WINDOW_P (it->f))
16718 {
16719 int i, n;
16720
16721 if (it->current_x > it->last_visible_x)
16722 {
16723 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
16724 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
16725 break;
16726 for (n = row->used[TEXT_AREA]; i < n; ++i)
16727 {
16728 row->used[TEXT_AREA] = i;
16729 produce_special_glyphs (it, IT_TRUNCATION);
16730 }
16731 }
16732 produce_special_glyphs (it, IT_TRUNCATION);
16733 }
16734 it->glyph_row->truncated_on_right_p = 1;
16735 }
16736 break;
16737 }
16738 }
16739
16740 /* Maybe insert a truncation at the left. */
16741 if (it->first_visible_x
16742 && IT_CHARPOS (*it) > 0)
16743 {
16744 if (!FRAME_WINDOW_P (it->f))
16745 insert_left_trunc_glyphs (it);
16746 it->glyph_row->truncated_on_left_p = 1;
16747 }
16748
16749 it->face_id = saved_face_id;
16750
16751 /* Value is number of columns displayed. */
16752 return it->hpos - hpos_at_start;
16753 }
16754
16755
16756 \f
16757 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
16758 appears as an element of LIST or as the car of an element of LIST.
16759 If PROPVAL is a list, compare each element against LIST in that
16760 way, and return 1/2 if any element of PROPVAL is found in LIST.
16761 Otherwise return 0. This function cannot quit.
16762 The return value is 2 if the text is invisible but with an ellipsis
16763 and 1 if it's invisible and without an ellipsis. */
16764
16765 int
16766 invisible_p (propval, list)
16767 register Lisp_Object propval;
16768 Lisp_Object list;
16769 {
16770 register Lisp_Object tail, proptail;
16771
16772 for (tail = list; CONSP (tail); tail = XCDR (tail))
16773 {
16774 register Lisp_Object tem;
16775 tem = XCAR (tail);
16776 if (EQ (propval, tem))
16777 return 1;
16778 if (CONSP (tem) && EQ (propval, XCAR (tem)))
16779 return NILP (XCDR (tem)) ? 1 : 2;
16780 }
16781
16782 if (CONSP (propval))
16783 {
16784 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
16785 {
16786 Lisp_Object propelt;
16787 propelt = XCAR (proptail);
16788 for (tail = list; CONSP (tail); tail = XCDR (tail))
16789 {
16790 register Lisp_Object tem;
16791 tem = XCAR (tail);
16792 if (EQ (propelt, tem))
16793 return 1;
16794 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
16795 return NILP (XCDR (tem)) ? 1 : 2;
16796 }
16797 }
16798 }
16799
16800 return 0;
16801 }
16802
16803 /* Calculate a width or height in pixels from a specification using
16804 the following elements:
16805
16806 SPEC ::=
16807 NUM - a (fractional) multiple of the default font width/height
16808 (NUM) - specifies exactly NUM pixels
16809 UNIT - a fixed number of pixels, see below.
16810 ELEMENT - size of a display element in pixels, see below.
16811 (NUM . SPEC) - equals NUM * SPEC
16812 (+ SPEC SPEC ...) - add pixel values
16813 (- SPEC SPEC ...) - subtract pixel values
16814 (- SPEC) - negate pixel value
16815
16816 NUM ::=
16817 INT or FLOAT - a number constant
16818 SYMBOL - use symbol's (buffer local) variable binding.
16819
16820 UNIT ::=
16821 in - pixels per inch *)
16822 mm - pixels per 1/1000 meter *)
16823 cm - pixels per 1/100 meter *)
16824 width - width of current font in pixels.
16825 height - height of current font in pixels.
16826
16827 *) using the ratio(s) defined in display-pixels-per-inch.
16828
16829 ELEMENT ::=
16830
16831 left-fringe - left fringe width in pixels
16832 right-fringe - right fringe width in pixels
16833
16834 left-margin - left margin width in pixels
16835 right-margin - right margin width in pixels
16836
16837 scroll-bar - scroll-bar area width in pixels
16838
16839 Examples:
16840
16841 Pixels corresponding to 5 inches:
16842 (5 . in)
16843
16844 Total width of non-text areas on left side of window (if scroll-bar is on left):
16845 '(space :width (+ left-fringe left-margin scroll-bar))
16846
16847 Align to first text column (in header line):
16848 '(space :align-to 0)
16849
16850 Align to middle of text area minus half the width of variable `my-image'
16851 containing a loaded image:
16852 '(space :align-to (0.5 . (- text my-image)))
16853
16854 Width of left margin minus width of 1 character in the default font:
16855 '(space :width (- left-margin 1))
16856
16857 Width of left margin minus width of 2 characters in the current font:
16858 '(space :width (- left-margin (2 . width)))
16859
16860 Center 1 character over left-margin (in header line):
16861 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
16862
16863 Different ways to express width of left fringe plus left margin minus one pixel:
16864 '(space :width (- (+ left-fringe left-margin) (1)))
16865 '(space :width (+ left-fringe left-margin (- (1))))
16866 '(space :width (+ left-fringe left-margin (-1)))
16867
16868 */
16869
16870 #define NUMVAL(X) \
16871 ((INTEGERP (X) || FLOATP (X)) \
16872 ? XFLOATINT (X) \
16873 : - 1)
16874
16875 int
16876 calc_pixel_width_or_height (res, it, prop, font, width_p, align_to)
16877 double *res;
16878 struct it *it;
16879 Lisp_Object prop;
16880 void *font;
16881 int width_p, *align_to;
16882 {
16883 double pixels;
16884
16885 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
16886 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
16887
16888 if (NILP (prop))
16889 return OK_PIXELS (0);
16890
16891 if (SYMBOLP (prop))
16892 {
16893 if (SCHARS (SYMBOL_NAME (prop)) == 2)
16894 {
16895 char *unit = SDATA (SYMBOL_NAME (prop));
16896
16897 if (unit[0] == 'i' && unit[1] == 'n')
16898 pixels = 1.0;
16899 else if (unit[0] == 'm' && unit[1] == 'm')
16900 pixels = 25.4;
16901 else if (unit[0] == 'c' && unit[1] == 'm')
16902 pixels = 2.54;
16903 else
16904 pixels = 0;
16905 if (pixels > 0)
16906 {
16907 double ppi;
16908 if ((ppi = NUMVAL (Vdisplay_pixels_per_inch), ppi > 0)
16909 || (CONSP (Vdisplay_pixels_per_inch)
16910 && (ppi = (width_p
16911 ? NUMVAL (XCAR (Vdisplay_pixels_per_inch))
16912 : NUMVAL (XCDR (Vdisplay_pixels_per_inch))),
16913 ppi > 0)))
16914 return OK_PIXELS (ppi / pixels);
16915
16916 return 0;
16917 }
16918 }
16919
16920 #ifdef HAVE_WINDOW_SYSTEM
16921 if (EQ (prop, Qheight))
16922 return OK_PIXELS (font ? FONT_HEIGHT ((XFontStruct *)font) : FRAME_LINE_HEIGHT (it->f));
16923 if (EQ (prop, Qwidth))
16924 return OK_PIXELS (font ? FONT_WIDTH ((XFontStruct *)font) : FRAME_COLUMN_WIDTH (it->f));
16925 #else
16926 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
16927 return OK_PIXELS (1);
16928 #endif
16929
16930 if (EQ (prop, Qtext))
16931 return OK_PIXELS (width_p
16932 ? window_box_width (it->w, TEXT_AREA)
16933 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
16934
16935 if (align_to && *align_to < 0)
16936 {
16937 *res = 0;
16938 if (EQ (prop, Qleft))
16939 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
16940 if (EQ (prop, Qright))
16941 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
16942 if (EQ (prop, Qcenter))
16943 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
16944 + window_box_width (it->w, TEXT_AREA) / 2);
16945 if (EQ (prop, Qleft_fringe))
16946 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
16947 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
16948 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
16949 if (EQ (prop, Qright_fringe))
16950 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
16951 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
16952 : window_box_right_offset (it->w, TEXT_AREA));
16953 if (EQ (prop, Qleft_margin))
16954 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
16955 if (EQ (prop, Qright_margin))
16956 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
16957 if (EQ (prop, Qscroll_bar))
16958 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
16959 ? 0
16960 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
16961 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
16962 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
16963 : 0)));
16964 }
16965 else
16966 {
16967 if (EQ (prop, Qleft_fringe))
16968 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
16969 if (EQ (prop, Qright_fringe))
16970 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
16971 if (EQ (prop, Qleft_margin))
16972 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
16973 if (EQ (prop, Qright_margin))
16974 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
16975 if (EQ (prop, Qscroll_bar))
16976 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
16977 }
16978
16979 prop = Fbuffer_local_value (prop, it->w->buffer);
16980 }
16981
16982 if (INTEGERP (prop) || FLOATP (prop))
16983 {
16984 int base_unit = (width_p
16985 ? FRAME_COLUMN_WIDTH (it->f)
16986 : FRAME_LINE_HEIGHT (it->f));
16987 return OK_PIXELS (XFLOATINT (prop) * base_unit);
16988 }
16989
16990 if (CONSP (prop))
16991 {
16992 Lisp_Object car = XCAR (prop);
16993 Lisp_Object cdr = XCDR (prop);
16994
16995 if (SYMBOLP (car))
16996 {
16997 #ifdef HAVE_WINDOW_SYSTEM
16998 if (valid_image_p (prop))
16999 {
17000 int id = lookup_image (it->f, prop);
17001 struct image *img = IMAGE_FROM_ID (it->f, id);
17002
17003 return OK_PIXELS (width_p ? img->width : img->height);
17004 }
17005 #endif
17006 if (EQ (car, Qplus) || EQ (car, Qminus))
17007 {
17008 int first = 1;
17009 double px;
17010
17011 pixels = 0;
17012 while (CONSP (cdr))
17013 {
17014 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
17015 font, width_p, align_to))
17016 return 0;
17017 if (first)
17018 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
17019 else
17020 pixels += px;
17021 cdr = XCDR (cdr);
17022 }
17023 if (EQ (car, Qminus))
17024 pixels = -pixels;
17025 return OK_PIXELS (pixels);
17026 }
17027
17028 car = Fbuffer_local_value (car, it->w->buffer);
17029 }
17030
17031 if (INTEGERP (car) || FLOATP (car))
17032 {
17033 double fact;
17034 pixels = XFLOATINT (car);
17035 if (NILP (cdr))
17036 return OK_PIXELS (pixels);
17037 if (calc_pixel_width_or_height (&fact, it, cdr,
17038 font, width_p, align_to))
17039 return OK_PIXELS (pixels * fact);
17040 return 0;
17041 }
17042
17043 return 0;
17044 }
17045
17046 return 0;
17047 }
17048
17049 \f
17050 /***********************************************************************
17051 Glyph Display
17052 ***********************************************************************/
17053
17054 #ifdef HAVE_WINDOW_SYSTEM
17055
17056 #if GLYPH_DEBUG
17057
17058 void
17059 dump_glyph_string (s)
17060 struct glyph_string *s;
17061 {
17062 fprintf (stderr, "glyph string\n");
17063 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
17064 s->x, s->y, s->width, s->height);
17065 fprintf (stderr, " ybase = %d\n", s->ybase);
17066 fprintf (stderr, " hl = %d\n", s->hl);
17067 fprintf (stderr, " left overhang = %d, right = %d\n",
17068 s->left_overhang, s->right_overhang);
17069 fprintf (stderr, " nchars = %d\n", s->nchars);
17070 fprintf (stderr, " extends to end of line = %d\n",
17071 s->extends_to_end_of_line_p);
17072 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
17073 fprintf (stderr, " bg width = %d\n", s->background_width);
17074 }
17075
17076 #endif /* GLYPH_DEBUG */
17077
17078 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
17079 of XChar2b structures for S; it can't be allocated in
17080 init_glyph_string because it must be allocated via `alloca'. W
17081 is the window on which S is drawn. ROW and AREA are the glyph row
17082 and area within the row from which S is constructed. START is the
17083 index of the first glyph structure covered by S. HL is a
17084 face-override for drawing S. */
17085
17086 #ifdef HAVE_NTGUI
17087 #define OPTIONAL_HDC(hdc) hdc,
17088 #define DECLARE_HDC(hdc) HDC hdc;
17089 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
17090 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
17091 #endif
17092
17093 #ifndef OPTIONAL_HDC
17094 #define OPTIONAL_HDC(hdc)
17095 #define DECLARE_HDC(hdc)
17096 #define ALLOCATE_HDC(hdc, f)
17097 #define RELEASE_HDC(hdc, f)
17098 #endif
17099
17100 static void
17101 init_glyph_string (s, OPTIONAL_HDC (hdc) char2b, w, row, area, start, hl)
17102 struct glyph_string *s;
17103 DECLARE_HDC (hdc)
17104 XChar2b *char2b;
17105 struct window *w;
17106 struct glyph_row *row;
17107 enum glyph_row_area area;
17108 int start;
17109 enum draw_glyphs_face hl;
17110 {
17111 bzero (s, sizeof *s);
17112 s->w = w;
17113 s->f = XFRAME (w->frame);
17114 #ifdef HAVE_NTGUI
17115 s->hdc = hdc;
17116 #endif
17117 s->display = FRAME_X_DISPLAY (s->f);
17118 s->window = FRAME_X_WINDOW (s->f);
17119 s->char2b = char2b;
17120 s->hl = hl;
17121 s->row = row;
17122 s->area = area;
17123 s->first_glyph = row->glyphs[area] + start;
17124 s->height = row->height;
17125 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
17126
17127 /* Display the internal border below the tool-bar window. */
17128 if (s->w == XWINDOW (s->f->tool_bar_window))
17129 s->y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
17130
17131 s->ybase = s->y + row->ascent;
17132 }
17133
17134
17135 /* Append the list of glyph strings with head H and tail T to the list
17136 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
17137
17138 static INLINE void
17139 append_glyph_string_lists (head, tail, h, t)
17140 struct glyph_string **head, **tail;
17141 struct glyph_string *h, *t;
17142 {
17143 if (h)
17144 {
17145 if (*head)
17146 (*tail)->next = h;
17147 else
17148 *head = h;
17149 h->prev = *tail;
17150 *tail = t;
17151 }
17152 }
17153
17154
17155 /* Prepend the list of glyph strings with head H and tail T to the
17156 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
17157 result. */
17158
17159 static INLINE void
17160 prepend_glyph_string_lists (head, tail, h, t)
17161 struct glyph_string **head, **tail;
17162 struct glyph_string *h, *t;
17163 {
17164 if (h)
17165 {
17166 if (*head)
17167 (*head)->prev = t;
17168 else
17169 *tail = t;
17170 t->next = *head;
17171 *head = h;
17172 }
17173 }
17174
17175
17176 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
17177 Set *HEAD and *TAIL to the resulting list. */
17178
17179 static INLINE void
17180 append_glyph_string (head, tail, s)
17181 struct glyph_string **head, **tail;
17182 struct glyph_string *s;
17183 {
17184 s->next = s->prev = NULL;
17185 append_glyph_string_lists (head, tail, s, s);
17186 }
17187
17188
17189 /* Get face and two-byte form of character glyph GLYPH on frame F.
17190 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
17191 a pointer to a realized face that is ready for display. */
17192
17193 static INLINE struct face *
17194 get_glyph_face_and_encoding (f, glyph, char2b, two_byte_p)
17195 struct frame *f;
17196 struct glyph *glyph;
17197 XChar2b *char2b;
17198 int *two_byte_p;
17199 {
17200 struct face *face;
17201
17202 xassert (glyph->type == CHAR_GLYPH);
17203 face = FACE_FROM_ID (f, glyph->face_id);
17204
17205 if (two_byte_p)
17206 *two_byte_p = 0;
17207
17208 if (!glyph->multibyte_p)
17209 {
17210 /* Unibyte case. We don't have to encode, but we have to make
17211 sure to use a face suitable for unibyte. */
17212 STORE_XCHAR2B (char2b, 0, glyph->u.ch);
17213 }
17214 else if (glyph->u.ch < 128
17215 && glyph->face_id < BASIC_FACE_ID_SENTINEL)
17216 {
17217 /* Case of ASCII in a face known to fit ASCII. */
17218 STORE_XCHAR2B (char2b, 0, glyph->u.ch);
17219 }
17220 else
17221 {
17222 int c1, c2, charset;
17223
17224 /* Split characters into bytes. If c2 is -1 afterwards, C is
17225 really a one-byte character so that byte1 is zero. */
17226 SPLIT_CHAR (glyph->u.ch, charset, c1, c2);
17227 if (c2 > 0)
17228 STORE_XCHAR2B (char2b, c1, c2);
17229 else
17230 STORE_XCHAR2B (char2b, 0, c1);
17231
17232 /* Maybe encode the character in *CHAR2B. */
17233 if (charset != CHARSET_ASCII)
17234 {
17235 struct font_info *font_info
17236 = FONT_INFO_FROM_ID (f, face->font_info_id);
17237 if (font_info)
17238 glyph->font_type
17239 = FRAME_RIF (f)->encode_char (glyph->u.ch, char2b, font_info, two_byte_p);
17240 }
17241 }
17242
17243 /* Make sure X resources of the face are allocated. */
17244 xassert (face != NULL);
17245 PREPARE_FACE_FOR_DISPLAY (f, face);
17246 return face;
17247 }
17248
17249
17250 /* Fill glyph string S with composition components specified by S->cmp.
17251
17252 FACES is an array of faces for all components of this composition.
17253 S->gidx is the index of the first component for S.
17254 OVERLAPS_P non-zero means S should draw the foreground only, and
17255 use its physical height for clipping.
17256
17257 Value is the index of a component not in S. */
17258
17259 static int
17260 fill_composite_glyph_string (s, faces, overlaps_p)
17261 struct glyph_string *s;
17262 struct face **faces;
17263 int overlaps_p;
17264 {
17265 int i;
17266
17267 xassert (s);
17268
17269 s->for_overlaps_p = overlaps_p;
17270
17271 s->face = faces[s->gidx];
17272 s->font = s->face->font;
17273 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
17274
17275 /* For all glyphs of this composition, starting at the offset
17276 S->gidx, until we reach the end of the definition or encounter a
17277 glyph that requires the different face, add it to S. */
17278 ++s->nchars;
17279 for (i = s->gidx + 1; i < s->cmp->glyph_len && faces[i] == s->face; ++i)
17280 ++s->nchars;
17281
17282 /* All glyph strings for the same composition has the same width,
17283 i.e. the width set for the first component of the composition. */
17284
17285 s->width = s->first_glyph->pixel_width;
17286
17287 /* If the specified font could not be loaded, use the frame's
17288 default font, but record the fact that we couldn't load it in
17289 the glyph string so that we can draw rectangles for the
17290 characters of the glyph string. */
17291 if (s->font == NULL)
17292 {
17293 s->font_not_found_p = 1;
17294 s->font = FRAME_FONT (s->f);
17295 }
17296
17297 /* Adjust base line for subscript/superscript text. */
17298 s->ybase += s->first_glyph->voffset;
17299
17300 xassert (s->face && s->face->gc);
17301
17302 /* This glyph string must always be drawn with 16-bit functions. */
17303 s->two_byte_p = 1;
17304
17305 return s->gidx + s->nchars;
17306 }
17307
17308
17309 /* Fill glyph string S from a sequence of character glyphs.
17310
17311 FACE_ID is the face id of the string. START is the index of the
17312 first glyph to consider, END is the index of the last + 1.
17313 OVERLAPS_P non-zero means S should draw the foreground only, and
17314 use its physical height for clipping.
17315
17316 Value is the index of the first glyph not in S. */
17317
17318 static int
17319 fill_glyph_string (s, face_id, start, end, overlaps_p)
17320 struct glyph_string *s;
17321 int face_id;
17322 int start, end, overlaps_p;
17323 {
17324 struct glyph *glyph, *last;
17325 int voffset;
17326 int glyph_not_available_p;
17327
17328 xassert (s->f == XFRAME (s->w->frame));
17329 xassert (s->nchars == 0);
17330 xassert (start >= 0 && end > start);
17331
17332 s->for_overlaps_p = overlaps_p,
17333 glyph = s->row->glyphs[s->area] + start;
17334 last = s->row->glyphs[s->area] + end;
17335 voffset = glyph->voffset;
17336
17337 glyph_not_available_p = glyph->glyph_not_available_p;
17338
17339 while (glyph < last
17340 && glyph->type == CHAR_GLYPH
17341 && glyph->voffset == voffset
17342 /* Same face id implies same font, nowadays. */
17343 && glyph->face_id == face_id
17344 && glyph->glyph_not_available_p == glyph_not_available_p)
17345 {
17346 int two_byte_p;
17347
17348 s->face = get_glyph_face_and_encoding (s->f, glyph,
17349 s->char2b + s->nchars,
17350 &two_byte_p);
17351 s->two_byte_p = two_byte_p;
17352 ++s->nchars;
17353 xassert (s->nchars <= end - start);
17354 s->width += glyph->pixel_width;
17355 ++glyph;
17356 }
17357
17358 s->font = s->face->font;
17359 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
17360
17361 /* If the specified font could not be loaded, use the frame's font,
17362 but record the fact that we couldn't load it in
17363 S->font_not_found_p so that we can draw rectangles for the
17364 characters of the glyph string. */
17365 if (s->font == NULL || glyph_not_available_p)
17366 {
17367 s->font_not_found_p = 1;
17368 s->font = FRAME_FONT (s->f);
17369 }
17370
17371 /* Adjust base line for subscript/superscript text. */
17372 s->ybase += voffset;
17373
17374 xassert (s->face && s->face->gc);
17375 return glyph - s->row->glyphs[s->area];
17376 }
17377
17378
17379 /* Fill glyph string S from image glyph S->first_glyph. */
17380
17381 static void
17382 fill_image_glyph_string (s)
17383 struct glyph_string *s;
17384 {
17385 xassert (s->first_glyph->type == IMAGE_GLYPH);
17386 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
17387 xassert (s->img);
17388 s->slice = s->first_glyph->slice;
17389 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
17390 s->font = s->face->font;
17391 s->width = s->first_glyph->pixel_width;
17392
17393 /* Adjust base line for subscript/superscript text. */
17394 s->ybase += s->first_glyph->voffset;
17395 }
17396
17397
17398 /* Fill glyph string S from a sequence of stretch glyphs.
17399
17400 ROW is the glyph row in which the glyphs are found, AREA is the
17401 area within the row. START is the index of the first glyph to
17402 consider, END is the index of the last + 1.
17403
17404 Value is the index of the first glyph not in S. */
17405
17406 static int
17407 fill_stretch_glyph_string (s, row, area, start, end)
17408 struct glyph_string *s;
17409 struct glyph_row *row;
17410 enum glyph_row_area area;
17411 int start, end;
17412 {
17413 struct glyph *glyph, *last;
17414 int voffset, face_id;
17415
17416 xassert (s->first_glyph->type == STRETCH_GLYPH);
17417
17418 glyph = s->row->glyphs[s->area] + start;
17419 last = s->row->glyphs[s->area] + end;
17420 face_id = glyph->face_id;
17421 s->face = FACE_FROM_ID (s->f, face_id);
17422 s->font = s->face->font;
17423 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
17424 s->width = glyph->pixel_width;
17425 voffset = glyph->voffset;
17426
17427 for (++glyph;
17428 (glyph < last
17429 && glyph->type == STRETCH_GLYPH
17430 && glyph->voffset == voffset
17431 && glyph->face_id == face_id);
17432 ++glyph)
17433 s->width += glyph->pixel_width;
17434
17435 /* Adjust base line for subscript/superscript text. */
17436 s->ybase += voffset;
17437
17438 /* The case that face->gc == 0 is handled when drawing the glyph
17439 string by calling PREPARE_FACE_FOR_DISPLAY. */
17440 xassert (s->face);
17441 return glyph - s->row->glyphs[s->area];
17442 }
17443
17444
17445 /* EXPORT for RIF:
17446 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
17447 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
17448 assumed to be zero. */
17449
17450 void
17451 x_get_glyph_overhangs (glyph, f, left, right)
17452 struct glyph *glyph;
17453 struct frame *f;
17454 int *left, *right;
17455 {
17456 *left = *right = 0;
17457
17458 if (glyph->type == CHAR_GLYPH)
17459 {
17460 XFontStruct *font;
17461 struct face *face;
17462 struct font_info *font_info;
17463 XChar2b char2b;
17464 XCharStruct *pcm;
17465
17466 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
17467 font = face->font;
17468 font_info = FONT_INFO_FROM_ID (f, face->font_info_id);
17469 if (font /* ++KFS: Should this be font_info ? */
17470 && (pcm = FRAME_RIF (f)->per_char_metric (font, &char2b, glyph->font_type)))
17471 {
17472 if (pcm->rbearing > pcm->width)
17473 *right = pcm->rbearing - pcm->width;
17474 if (pcm->lbearing < 0)
17475 *left = -pcm->lbearing;
17476 }
17477 }
17478 }
17479
17480
17481 /* Return the index of the first glyph preceding glyph string S that
17482 is overwritten by S because of S's left overhang. Value is -1
17483 if no glyphs are overwritten. */
17484
17485 static int
17486 left_overwritten (s)
17487 struct glyph_string *s;
17488 {
17489 int k;
17490
17491 if (s->left_overhang)
17492 {
17493 int x = 0, i;
17494 struct glyph *glyphs = s->row->glyphs[s->area];
17495 int first = s->first_glyph - glyphs;
17496
17497 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
17498 x -= glyphs[i].pixel_width;
17499
17500 k = i + 1;
17501 }
17502 else
17503 k = -1;
17504
17505 return k;
17506 }
17507
17508
17509 /* Return the index of the first glyph preceding glyph string S that
17510 is overwriting S because of its right overhang. Value is -1 if no
17511 glyph in front of S overwrites S. */
17512
17513 static int
17514 left_overwriting (s)
17515 struct glyph_string *s;
17516 {
17517 int i, k, x;
17518 struct glyph *glyphs = s->row->glyphs[s->area];
17519 int first = s->first_glyph - glyphs;
17520
17521 k = -1;
17522 x = 0;
17523 for (i = first - 1; i >= 0; --i)
17524 {
17525 int left, right;
17526 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
17527 if (x + right > 0)
17528 k = i;
17529 x -= glyphs[i].pixel_width;
17530 }
17531
17532 return k;
17533 }
17534
17535
17536 /* Return the index of the last glyph following glyph string S that is
17537 not overwritten by S because of S's right overhang. Value is -1 if
17538 no such glyph is found. */
17539
17540 static int
17541 right_overwritten (s)
17542 struct glyph_string *s;
17543 {
17544 int k = -1;
17545
17546 if (s->right_overhang)
17547 {
17548 int x = 0, i;
17549 struct glyph *glyphs = s->row->glyphs[s->area];
17550 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
17551 int end = s->row->used[s->area];
17552
17553 for (i = first; i < end && s->right_overhang > x; ++i)
17554 x += glyphs[i].pixel_width;
17555
17556 k = i;
17557 }
17558
17559 return k;
17560 }
17561
17562
17563 /* Return the index of the last glyph following glyph string S that
17564 overwrites S because of its left overhang. Value is negative
17565 if no such glyph is found. */
17566
17567 static int
17568 right_overwriting (s)
17569 struct glyph_string *s;
17570 {
17571 int i, k, x;
17572 int end = s->row->used[s->area];
17573 struct glyph *glyphs = s->row->glyphs[s->area];
17574 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
17575
17576 k = -1;
17577 x = 0;
17578 for (i = first; i < end; ++i)
17579 {
17580 int left, right;
17581 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
17582 if (x - left < 0)
17583 k = i;
17584 x += glyphs[i].pixel_width;
17585 }
17586
17587 return k;
17588 }
17589
17590
17591 /* Get face and two-byte form of character C in face FACE_ID on frame
17592 F. The encoding of C is returned in *CHAR2B. MULTIBYTE_P non-zero
17593 means we want to display multibyte text. DISPLAY_P non-zero means
17594 make sure that X resources for the face returned are allocated.
17595 Value is a pointer to a realized face that is ready for display if
17596 DISPLAY_P is non-zero. */
17597
17598 static INLINE struct face *
17599 get_char_face_and_encoding (f, c, face_id, char2b, multibyte_p, display_p)
17600 struct frame *f;
17601 int c, face_id;
17602 XChar2b *char2b;
17603 int multibyte_p, display_p;
17604 {
17605 struct face *face = FACE_FROM_ID (f, face_id);
17606
17607 if (!multibyte_p)
17608 {
17609 /* Unibyte case. We don't have to encode, but we have to make
17610 sure to use a face suitable for unibyte. */
17611 STORE_XCHAR2B (char2b, 0, c);
17612 face_id = FACE_FOR_CHAR (f, face, c);
17613 face = FACE_FROM_ID (f, face_id);
17614 }
17615 else if (c < 128 && face_id < BASIC_FACE_ID_SENTINEL)
17616 {
17617 /* Case of ASCII in a face known to fit ASCII. */
17618 STORE_XCHAR2B (char2b, 0, c);
17619 }
17620 else
17621 {
17622 int c1, c2, charset;
17623
17624 /* Split characters into bytes. If c2 is -1 afterwards, C is
17625 really a one-byte character so that byte1 is zero. */
17626 SPLIT_CHAR (c, charset, c1, c2);
17627 if (c2 > 0)
17628 STORE_XCHAR2B (char2b, c1, c2);
17629 else
17630 STORE_XCHAR2B (char2b, 0, c1);
17631
17632 /* Maybe encode the character in *CHAR2B. */
17633 if (face->font != NULL)
17634 {
17635 struct font_info *font_info
17636 = FONT_INFO_FROM_ID (f, face->font_info_id);
17637 if (font_info)
17638 FRAME_RIF (f)->encode_char (c, char2b, font_info, 0);
17639 }
17640 }
17641
17642 /* Make sure X resources of the face are allocated. */
17643 #ifdef HAVE_X_WINDOWS
17644 if (display_p)
17645 #endif
17646 {
17647 xassert (face != NULL);
17648 PREPARE_FACE_FOR_DISPLAY (f, face);
17649 }
17650
17651 return face;
17652 }
17653
17654
17655 /* Set background width of glyph string S. START is the index of the
17656 first glyph following S. LAST_X is the right-most x-position + 1
17657 in the drawing area. */
17658
17659 static INLINE void
17660 set_glyph_string_background_width (s, start, last_x)
17661 struct glyph_string *s;
17662 int start;
17663 int last_x;
17664 {
17665 /* If the face of this glyph string has to be drawn to the end of
17666 the drawing area, set S->extends_to_end_of_line_p. */
17667 struct face *default_face = FACE_FROM_ID (s->f, DEFAULT_FACE_ID);
17668
17669 if (start == s->row->used[s->area]
17670 && s->area == TEXT_AREA
17671 && ((s->hl == DRAW_NORMAL_TEXT
17672 && (s->row->fill_line_p
17673 || s->face->background != default_face->background
17674 || s->face->stipple != default_face->stipple
17675 || s->row->mouse_face_p))
17676 || s->hl == DRAW_MOUSE_FACE
17677 || ((s->hl == DRAW_IMAGE_RAISED || s->hl == DRAW_IMAGE_SUNKEN)
17678 && s->row->fill_line_p)))
17679 s->extends_to_end_of_line_p = 1;
17680
17681 /* If S extends its face to the end of the line, set its
17682 background_width to the distance to the right edge of the drawing
17683 area. */
17684 if (s->extends_to_end_of_line_p)
17685 s->background_width = last_x - s->x + 1;
17686 else
17687 s->background_width = s->width;
17688 }
17689
17690
17691 /* Compute overhangs and x-positions for glyph string S and its
17692 predecessors, or successors. X is the starting x-position for S.
17693 BACKWARD_P non-zero means process predecessors. */
17694
17695 static void
17696 compute_overhangs_and_x (s, x, backward_p)
17697 struct glyph_string *s;
17698 int x;
17699 int backward_p;
17700 {
17701 if (backward_p)
17702 {
17703 while (s)
17704 {
17705 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
17706 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
17707 x -= s->width;
17708 s->x = x;
17709 s = s->prev;
17710 }
17711 }
17712 else
17713 {
17714 while (s)
17715 {
17716 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
17717 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
17718 s->x = x;
17719 x += s->width;
17720 s = s->next;
17721 }
17722 }
17723 }
17724
17725
17726
17727 /* The following macros are only called from draw_glyphs below.
17728 They reference the following parameters of that function directly:
17729 `w', `row', `area', and `overlap_p'
17730 as well as the following local variables:
17731 `s', `f', and `hdc' (in W32) */
17732
17733 #ifdef HAVE_NTGUI
17734 /* On W32, silently add local `hdc' variable to argument list of
17735 init_glyph_string. */
17736 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
17737 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
17738 #else
17739 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
17740 init_glyph_string (s, char2b, w, row, area, start, hl)
17741 #endif
17742
17743 /* Add a glyph string for a stretch glyph to the list of strings
17744 between HEAD and TAIL. START is the index of the stretch glyph in
17745 row area AREA of glyph row ROW. END is the index of the last glyph
17746 in that glyph row area. X is the current output position assigned
17747 to the new glyph string constructed. HL overrides that face of the
17748 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
17749 is the right-most x-position of the drawing area. */
17750
17751 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
17752 and below -- keep them on one line. */
17753 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
17754 do \
17755 { \
17756 s = (struct glyph_string *) alloca (sizeof *s); \
17757 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
17758 START = fill_stretch_glyph_string (s, row, area, START, END); \
17759 append_glyph_string (&HEAD, &TAIL, s); \
17760 s->x = (X); \
17761 } \
17762 while (0)
17763
17764
17765 /* Add a glyph string for an image glyph to the list of strings
17766 between HEAD and TAIL. START is the index of the image glyph in
17767 row area AREA of glyph row ROW. END is the index of the last glyph
17768 in that glyph row area. X is the current output position assigned
17769 to the new glyph string constructed. HL overrides that face of the
17770 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
17771 is the right-most x-position of the drawing area. */
17772
17773 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
17774 do \
17775 { \
17776 s = (struct glyph_string *) alloca (sizeof *s); \
17777 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
17778 fill_image_glyph_string (s); \
17779 append_glyph_string (&HEAD, &TAIL, s); \
17780 ++START; \
17781 s->x = (X); \
17782 } \
17783 while (0)
17784
17785
17786 /* Add a glyph string for a sequence of character glyphs to the list
17787 of strings between HEAD and TAIL. START is the index of the first
17788 glyph in row area AREA of glyph row ROW that is part of the new
17789 glyph string. END is the index of the last glyph in that glyph row
17790 area. X is the current output position assigned to the new glyph
17791 string constructed. HL overrides that face of the glyph; e.g. it
17792 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
17793 right-most x-position of the drawing area. */
17794
17795 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
17796 do \
17797 { \
17798 int c, face_id; \
17799 XChar2b *char2b; \
17800 \
17801 c = (row)->glyphs[area][START].u.ch; \
17802 face_id = (row)->glyphs[area][START].face_id; \
17803 \
17804 s = (struct glyph_string *) alloca (sizeof *s); \
17805 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
17806 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
17807 append_glyph_string (&HEAD, &TAIL, s); \
17808 s->x = (X); \
17809 START = fill_glyph_string (s, face_id, START, END, overlaps_p); \
17810 } \
17811 while (0)
17812
17813
17814 /* Add a glyph string for a composite sequence to the list of strings
17815 between HEAD and TAIL. START is the index of the first glyph in
17816 row area AREA of glyph row ROW that is part of the new glyph
17817 string. END is the index of the last glyph in that glyph row area.
17818 X is the current output position assigned to the new glyph string
17819 constructed. HL overrides that face of the glyph; e.g. it is
17820 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
17821 x-position of the drawing area. */
17822
17823 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
17824 do { \
17825 int cmp_id = (row)->glyphs[area][START].u.cmp_id; \
17826 int face_id = (row)->glyphs[area][START].face_id; \
17827 struct face *base_face = FACE_FROM_ID (f, face_id); \
17828 struct composition *cmp = composition_table[cmp_id]; \
17829 int glyph_len = cmp->glyph_len; \
17830 XChar2b *char2b; \
17831 struct face **faces; \
17832 struct glyph_string *first_s = NULL; \
17833 int n; \
17834 \
17835 base_face = base_face->ascii_face; \
17836 char2b = (XChar2b *) alloca ((sizeof *char2b) * glyph_len); \
17837 faces = (struct face **) alloca ((sizeof *faces) * glyph_len); \
17838 /* At first, fill in `char2b' and `faces'. */ \
17839 for (n = 0; n < glyph_len; n++) \
17840 { \
17841 int c = COMPOSITION_GLYPH (cmp, n); \
17842 int this_face_id = FACE_FOR_CHAR (f, base_face, c); \
17843 faces[n] = FACE_FROM_ID (f, this_face_id); \
17844 get_char_face_and_encoding (f, c, this_face_id, \
17845 char2b + n, 1, 1); \
17846 } \
17847 \
17848 /* Make glyph_strings for each glyph sequence that is drawable by \
17849 the same face, and append them to HEAD/TAIL. */ \
17850 for (n = 0; n < cmp->glyph_len;) \
17851 { \
17852 s = (struct glyph_string *) alloca (sizeof *s); \
17853 INIT_GLYPH_STRING (s, char2b + n, w, row, area, START, HL); \
17854 append_glyph_string (&(HEAD), &(TAIL), s); \
17855 s->cmp = cmp; \
17856 s->gidx = n; \
17857 s->x = (X); \
17858 \
17859 if (n == 0) \
17860 first_s = s; \
17861 \
17862 n = fill_composite_glyph_string (s, faces, overlaps_p); \
17863 } \
17864 \
17865 ++START; \
17866 s = first_s; \
17867 } while (0)
17868
17869
17870 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
17871 of AREA of glyph row ROW on window W between indices START and END.
17872 HL overrides the face for drawing glyph strings, e.g. it is
17873 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
17874 x-positions of the drawing area.
17875
17876 This is an ugly monster macro construct because we must use alloca
17877 to allocate glyph strings (because draw_glyphs can be called
17878 asynchronously). */
17879
17880 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
17881 do \
17882 { \
17883 HEAD = TAIL = NULL; \
17884 while (START < END) \
17885 { \
17886 struct glyph *first_glyph = (row)->glyphs[area] + START; \
17887 switch (first_glyph->type) \
17888 { \
17889 case CHAR_GLYPH: \
17890 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
17891 HL, X, LAST_X); \
17892 break; \
17893 \
17894 case COMPOSITE_GLYPH: \
17895 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
17896 HL, X, LAST_X); \
17897 break; \
17898 \
17899 case STRETCH_GLYPH: \
17900 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
17901 HL, X, LAST_X); \
17902 break; \
17903 \
17904 case IMAGE_GLYPH: \
17905 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
17906 HL, X, LAST_X); \
17907 break; \
17908 \
17909 default: \
17910 abort (); \
17911 } \
17912 \
17913 set_glyph_string_background_width (s, START, LAST_X); \
17914 (X) += s->width; \
17915 } \
17916 } \
17917 while (0)
17918
17919
17920 /* Draw glyphs between START and END in AREA of ROW on window W,
17921 starting at x-position X. X is relative to AREA in W. HL is a
17922 face-override with the following meaning:
17923
17924 DRAW_NORMAL_TEXT draw normally
17925 DRAW_CURSOR draw in cursor face
17926 DRAW_MOUSE_FACE draw in mouse face.
17927 DRAW_INVERSE_VIDEO draw in mode line face
17928 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
17929 DRAW_IMAGE_RAISED draw an image with a raised relief around it
17930
17931 If OVERLAPS_P is non-zero, draw only the foreground of characters
17932 and clip to the physical height of ROW.
17933
17934 Value is the x-position reached, relative to AREA of W. */
17935
17936 static int
17937 draw_glyphs (w, x, row, area, start, end, hl, overlaps_p)
17938 struct window *w;
17939 int x;
17940 struct glyph_row *row;
17941 enum glyph_row_area area;
17942 int start, end;
17943 enum draw_glyphs_face hl;
17944 int overlaps_p;
17945 {
17946 struct glyph_string *head, *tail;
17947 struct glyph_string *s;
17948 int last_x, area_width;
17949 int x_reached;
17950 int i, j;
17951 struct frame *f = XFRAME (WINDOW_FRAME (w));
17952 DECLARE_HDC (hdc);
17953
17954 ALLOCATE_HDC (hdc, f);
17955
17956 /* Let's rather be paranoid than getting a SEGV. */
17957 end = min (end, row->used[area]);
17958 start = max (0, start);
17959 start = min (end, start);
17960
17961 /* Translate X to frame coordinates. Set last_x to the right
17962 end of the drawing area. */
17963 if (row->full_width_p)
17964 {
17965 /* X is relative to the left edge of W, without scroll bars
17966 or fringes. */
17967 x += WINDOW_LEFT_EDGE_X (w);
17968 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
17969 }
17970 else
17971 {
17972 int area_left = window_box_left (w, area);
17973 x += area_left;
17974 area_width = window_box_width (w, area);
17975 last_x = area_left + area_width;
17976 }
17977
17978 /* Build a doubly-linked list of glyph_string structures between
17979 head and tail from what we have to draw. Note that the macro
17980 BUILD_GLYPH_STRINGS will modify its start parameter. That's
17981 the reason we use a separate variable `i'. */
17982 i = start;
17983 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
17984 if (tail)
17985 x_reached = tail->x + tail->background_width;
17986 else
17987 x_reached = x;
17988
17989 /* If there are any glyphs with lbearing < 0 or rbearing > width in
17990 the row, redraw some glyphs in front or following the glyph
17991 strings built above. */
17992 if (head && !overlaps_p && row->contains_overlapping_glyphs_p)
17993 {
17994 int dummy_x = 0;
17995 struct glyph_string *h, *t;
17996
17997 /* Compute overhangs for all glyph strings. */
17998 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
17999 for (s = head; s; s = s->next)
18000 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
18001
18002 /* Prepend glyph strings for glyphs in front of the first glyph
18003 string that are overwritten because of the first glyph
18004 string's left overhang. The background of all strings
18005 prepended must be drawn because the first glyph string
18006 draws over it. */
18007 i = left_overwritten (head);
18008 if (i >= 0)
18009 {
18010 j = i;
18011 BUILD_GLYPH_STRINGS (j, start, h, t,
18012 DRAW_NORMAL_TEXT, dummy_x, last_x);
18013 start = i;
18014 compute_overhangs_and_x (t, head->x, 1);
18015 prepend_glyph_string_lists (&head, &tail, h, t);
18016 }
18017
18018 /* Prepend glyph strings for glyphs in front of the first glyph
18019 string that overwrite that glyph string because of their
18020 right overhang. For these strings, only the foreground must
18021 be drawn, because it draws over the glyph string at `head'.
18022 The background must not be drawn because this would overwrite
18023 right overhangs of preceding glyphs for which no glyph
18024 strings exist. */
18025 i = left_overwriting (head);
18026 if (i >= 0)
18027 {
18028 BUILD_GLYPH_STRINGS (i, start, h, t,
18029 DRAW_NORMAL_TEXT, dummy_x, last_x);
18030 for (s = h; s; s = s->next)
18031 s->background_filled_p = 1;
18032 compute_overhangs_and_x (t, head->x, 1);
18033 prepend_glyph_string_lists (&head, &tail, h, t);
18034 }
18035
18036 /* Append glyphs strings for glyphs following the last glyph
18037 string tail that are overwritten by tail. The background of
18038 these strings has to be drawn because tail's foreground draws
18039 over it. */
18040 i = right_overwritten (tail);
18041 if (i >= 0)
18042 {
18043 BUILD_GLYPH_STRINGS (end, i, h, t,
18044 DRAW_NORMAL_TEXT, x, last_x);
18045 compute_overhangs_and_x (h, tail->x + tail->width, 0);
18046 append_glyph_string_lists (&head, &tail, h, t);
18047 }
18048
18049 /* Append glyph strings for glyphs following the last glyph
18050 string tail that overwrite tail. The foreground of such
18051 glyphs has to be drawn because it writes into the background
18052 of tail. The background must not be drawn because it could
18053 paint over the foreground of following glyphs. */
18054 i = right_overwriting (tail);
18055 if (i >= 0)
18056 {
18057 BUILD_GLYPH_STRINGS (end, i, h, t,
18058 DRAW_NORMAL_TEXT, x, last_x);
18059 for (s = h; s; s = s->next)
18060 s->background_filled_p = 1;
18061 compute_overhangs_and_x (h, tail->x + tail->width, 0);
18062 append_glyph_string_lists (&head, &tail, h, t);
18063 }
18064 }
18065
18066 /* Draw all strings. */
18067 for (s = head; s; s = s->next)
18068 FRAME_RIF (f)->draw_glyph_string (s);
18069
18070 if (area == TEXT_AREA
18071 && !row->full_width_p
18072 /* When drawing overlapping rows, only the glyph strings'
18073 foreground is drawn, which doesn't erase a cursor
18074 completely. */
18075 && !overlaps_p)
18076 {
18077 int x0 = head ? head->x : x;
18078 int x1 = tail ? tail->x + tail->background_width : x;
18079
18080 int text_left = window_box_left (w, TEXT_AREA);
18081 x0 -= text_left;
18082 x1 -= text_left;
18083
18084 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
18085 row->y, MATRIX_ROW_BOTTOM_Y (row));
18086 }
18087
18088 /* Value is the x-position up to which drawn, relative to AREA of W.
18089 This doesn't include parts drawn because of overhangs. */
18090 if (row->full_width_p)
18091 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
18092 else
18093 x_reached -= window_box_left (w, area);
18094
18095 RELEASE_HDC (hdc, f);
18096
18097 return x_reached;
18098 }
18099
18100
18101 /* Store one glyph for IT->char_to_display in IT->glyph_row.
18102 Called from x_produce_glyphs when IT->glyph_row is non-null. */
18103
18104 static INLINE void
18105 append_glyph (it)
18106 struct it *it;
18107 {
18108 struct glyph *glyph;
18109 enum glyph_row_area area = it->area;
18110
18111 xassert (it->glyph_row);
18112 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
18113
18114 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
18115 if (glyph < it->glyph_row->glyphs[area + 1])
18116 {
18117 glyph->charpos = CHARPOS (it->position);
18118 glyph->object = it->object;
18119 glyph->pixel_width = it->pixel_width;
18120 glyph->ascent = it->ascent;
18121 glyph->descent = it->descent;
18122 glyph->voffset = it->voffset;
18123 glyph->type = CHAR_GLYPH;
18124 glyph->multibyte_p = it->multibyte_p;
18125 glyph->left_box_line_p = it->start_of_box_run_p;
18126 glyph->right_box_line_p = it->end_of_box_run_p;
18127 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
18128 || it->phys_descent > it->descent);
18129 glyph->padding_p = 0;
18130 glyph->glyph_not_available_p = it->glyph_not_available_p;
18131 glyph->face_id = it->face_id;
18132 glyph->u.ch = it->char_to_display;
18133 glyph->slice = null_glyph_slice;
18134 glyph->font_type = FONT_TYPE_UNKNOWN;
18135 ++it->glyph_row->used[area];
18136 }
18137 }
18138
18139 /* Store one glyph for the composition IT->cmp_id in IT->glyph_row.
18140 Called from x_produce_glyphs when IT->glyph_row is non-null. */
18141
18142 static INLINE void
18143 append_composite_glyph (it)
18144 struct it *it;
18145 {
18146 struct glyph *glyph;
18147 enum glyph_row_area area = it->area;
18148
18149 xassert (it->glyph_row);
18150
18151 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
18152 if (glyph < it->glyph_row->glyphs[area + 1])
18153 {
18154 glyph->charpos = CHARPOS (it->position);
18155 glyph->object = it->object;
18156 glyph->pixel_width = it->pixel_width;
18157 glyph->ascent = it->ascent;
18158 glyph->descent = it->descent;
18159 glyph->voffset = it->voffset;
18160 glyph->type = COMPOSITE_GLYPH;
18161 glyph->multibyte_p = it->multibyte_p;
18162 glyph->left_box_line_p = it->start_of_box_run_p;
18163 glyph->right_box_line_p = it->end_of_box_run_p;
18164 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
18165 || it->phys_descent > it->descent);
18166 glyph->padding_p = 0;
18167 glyph->glyph_not_available_p = 0;
18168 glyph->face_id = it->face_id;
18169 glyph->u.cmp_id = it->cmp_id;
18170 glyph->slice = null_glyph_slice;
18171 glyph->font_type = FONT_TYPE_UNKNOWN;
18172 ++it->glyph_row->used[area];
18173 }
18174 }
18175
18176
18177 /* Change IT->ascent and IT->height according to the setting of
18178 IT->voffset. */
18179
18180 static INLINE void
18181 take_vertical_position_into_account (it)
18182 struct it *it;
18183 {
18184 if (it->voffset)
18185 {
18186 if (it->voffset < 0)
18187 /* Increase the ascent so that we can display the text higher
18188 in the line. */
18189 it->ascent -= it->voffset;
18190 else
18191 /* Increase the descent so that we can display the text lower
18192 in the line. */
18193 it->descent += it->voffset;
18194 }
18195 }
18196
18197
18198 /* Produce glyphs/get display metrics for the image IT is loaded with.
18199 See the description of struct display_iterator in dispextern.h for
18200 an overview of struct display_iterator. */
18201
18202 static void
18203 produce_image_glyph (it)
18204 struct it *it;
18205 {
18206 struct image *img;
18207 struct face *face;
18208 int face_ascent, glyph_ascent;
18209 struct glyph_slice slice;
18210
18211 xassert (it->what == IT_IMAGE);
18212
18213 face = FACE_FROM_ID (it->f, it->face_id);
18214 xassert (face);
18215 /* Make sure X resources of the face is loaded. */
18216 PREPARE_FACE_FOR_DISPLAY (it->f, face);
18217
18218 if (it->image_id < 0)
18219 {
18220 /* Fringe bitmap. */
18221 it->ascent = it->phys_ascent = 0;
18222 it->descent = it->phys_descent = 0;
18223 it->pixel_width = 0;
18224 it->nglyphs = 0;
18225 return;
18226 }
18227
18228 img = IMAGE_FROM_ID (it->f, it->image_id);
18229 xassert (img);
18230 /* Make sure X resources of the image is loaded. */
18231 prepare_image_for_display (it->f, img);
18232
18233 slice.x = slice.y = 0;
18234 slice.width = img->width;
18235 slice.height = img->height;
18236
18237 if (INTEGERP (it->slice.x))
18238 slice.x = XINT (it->slice.x);
18239 else if (FLOATP (it->slice.x))
18240 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
18241
18242 if (INTEGERP (it->slice.y))
18243 slice.y = XINT (it->slice.y);
18244 else if (FLOATP (it->slice.y))
18245 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
18246
18247 if (INTEGERP (it->slice.width))
18248 slice.width = XINT (it->slice.width);
18249 else if (FLOATP (it->slice.width))
18250 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
18251
18252 if (INTEGERP (it->slice.height))
18253 slice.height = XINT (it->slice.height);
18254 else if (FLOATP (it->slice.height))
18255 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
18256
18257 if (slice.x >= img->width)
18258 slice.x = img->width;
18259 if (slice.y >= img->height)
18260 slice.y = img->height;
18261 if (slice.x + slice.width >= img->width)
18262 slice.width = img->width - slice.x;
18263 if (slice.y + slice.height > img->height)
18264 slice.height = img->height - slice.y;
18265
18266 if (slice.width == 0 || slice.height == 0)
18267 return;
18268
18269 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
18270
18271 it->descent = slice.height - glyph_ascent;
18272 if (slice.y == 0)
18273 it->descent += img->vmargin;
18274 if (slice.y + slice.height == img->height)
18275 it->descent += img->vmargin;
18276 it->phys_descent = it->descent;
18277
18278 it->pixel_width = slice.width;
18279 if (slice.x == 0)
18280 it->pixel_width += img->hmargin;
18281 if (slice.x + slice.width == img->width)
18282 it->pixel_width += img->hmargin;
18283
18284 /* It's quite possible for images to have an ascent greater than
18285 their height, so don't get confused in that case. */
18286 if (it->descent < 0)
18287 it->descent = 0;
18288
18289 #if 0 /* this breaks image tiling */
18290 /* If this glyph is alone on the last line, adjust it.ascent to minimum row ascent. */
18291 face_ascent = face->font ? FONT_BASE (face->font) : FRAME_BASELINE_OFFSET (it->f);
18292 if (face_ascent > it->ascent)
18293 it->ascent = it->phys_ascent = face_ascent;
18294 #endif
18295
18296 it->nglyphs = 1;
18297
18298 if (face->box != FACE_NO_BOX)
18299 {
18300 if (face->box_line_width > 0)
18301 {
18302 if (slice.y == 0)
18303 it->ascent += face->box_line_width;
18304 if (slice.y + slice.height == img->height)
18305 it->descent += face->box_line_width;
18306 }
18307
18308 if (it->start_of_box_run_p && slice.x == 0)
18309 it->pixel_width += abs (face->box_line_width);
18310 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
18311 it->pixel_width += abs (face->box_line_width);
18312 }
18313
18314 take_vertical_position_into_account (it);
18315
18316 if (it->glyph_row)
18317 {
18318 struct glyph *glyph;
18319 enum glyph_row_area area = it->area;
18320
18321 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
18322 if (glyph < it->glyph_row->glyphs[area + 1])
18323 {
18324 glyph->charpos = CHARPOS (it->position);
18325 glyph->object = it->object;
18326 glyph->pixel_width = it->pixel_width;
18327 glyph->ascent = glyph_ascent;
18328 glyph->descent = it->descent;
18329 glyph->voffset = it->voffset;
18330 glyph->type = IMAGE_GLYPH;
18331 glyph->multibyte_p = it->multibyte_p;
18332 glyph->left_box_line_p = it->start_of_box_run_p;
18333 glyph->right_box_line_p = it->end_of_box_run_p;
18334 glyph->overlaps_vertically_p = 0;
18335 glyph->padding_p = 0;
18336 glyph->glyph_not_available_p = 0;
18337 glyph->face_id = it->face_id;
18338 glyph->u.img_id = img->id;
18339 glyph->slice = slice;
18340 glyph->font_type = FONT_TYPE_UNKNOWN;
18341 ++it->glyph_row->used[area];
18342 }
18343 }
18344 }
18345
18346
18347 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
18348 of the glyph, WIDTH and HEIGHT are the width and height of the
18349 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
18350
18351 static void
18352 append_stretch_glyph (it, object, width, height, ascent)
18353 struct it *it;
18354 Lisp_Object object;
18355 int width, height;
18356 int ascent;
18357 {
18358 struct glyph *glyph;
18359 enum glyph_row_area area = it->area;
18360
18361 xassert (ascent >= 0 && ascent <= height);
18362
18363 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
18364 if (glyph < it->glyph_row->glyphs[area + 1])
18365 {
18366 glyph->charpos = CHARPOS (it->position);
18367 glyph->object = object;
18368 glyph->pixel_width = width;
18369 glyph->ascent = ascent;
18370 glyph->descent = height - ascent;
18371 glyph->voffset = it->voffset;
18372 glyph->type = STRETCH_GLYPH;
18373 glyph->multibyte_p = it->multibyte_p;
18374 glyph->left_box_line_p = it->start_of_box_run_p;
18375 glyph->right_box_line_p = it->end_of_box_run_p;
18376 glyph->overlaps_vertically_p = 0;
18377 glyph->padding_p = 0;
18378 glyph->glyph_not_available_p = 0;
18379 glyph->face_id = it->face_id;
18380 glyph->u.stretch.ascent = ascent;
18381 glyph->u.stretch.height = height;
18382 glyph->slice = null_glyph_slice;
18383 glyph->font_type = FONT_TYPE_UNKNOWN;
18384 ++it->glyph_row->used[area];
18385 }
18386 }
18387
18388
18389 /* Produce a stretch glyph for iterator IT. IT->object is the value
18390 of the glyph property displayed. The value must be a list
18391 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
18392 being recognized:
18393
18394 1. `:width WIDTH' specifies that the space should be WIDTH *
18395 canonical char width wide. WIDTH may be an integer or floating
18396 point number.
18397
18398 2. `:relative-width FACTOR' specifies that the width of the stretch
18399 should be computed from the width of the first character having the
18400 `glyph' property, and should be FACTOR times that width.
18401
18402 3. `:align-to HPOS' specifies that the space should be wide enough
18403 to reach HPOS, a value in canonical character units.
18404
18405 Exactly one of the above pairs must be present.
18406
18407 4. `:height HEIGHT' specifies that the height of the stretch produced
18408 should be HEIGHT, measured in canonical character units.
18409
18410 5. `:relative-height FACTOR' specifies that the height of the
18411 stretch should be FACTOR times the height of the characters having
18412 the glyph property.
18413
18414 Either none or exactly one of 4 or 5 must be present.
18415
18416 6. `:ascent ASCENT' specifies that ASCENT percent of the height
18417 of the stretch should be used for the ascent of the stretch.
18418 ASCENT must be in the range 0 <= ASCENT <= 100. */
18419
18420 static void
18421 produce_stretch_glyph (it)
18422 struct it *it;
18423 {
18424 /* (space :width WIDTH :height HEIGHT ...) */
18425 Lisp_Object prop, plist;
18426 int width = 0, height = 0, align_to = -1;
18427 int zero_width_ok_p = 0, zero_height_ok_p = 0;
18428 int ascent = 0;
18429 double tem;
18430 struct face *face = FACE_FROM_ID (it->f, it->face_id);
18431 XFontStruct *font = face->font ? face->font : FRAME_FONT (it->f);
18432
18433 PREPARE_FACE_FOR_DISPLAY (it->f, face);
18434
18435 /* List should start with `space'. */
18436 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
18437 plist = XCDR (it->object);
18438
18439 /* Compute the width of the stretch. */
18440 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
18441 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
18442 {
18443 /* Absolute width `:width WIDTH' specified and valid. */
18444 zero_width_ok_p = 1;
18445 width = (int)tem;
18446 }
18447 else if (prop = Fplist_get (plist, QCrelative_width),
18448 NUMVAL (prop) > 0)
18449 {
18450 /* Relative width `:relative-width FACTOR' specified and valid.
18451 Compute the width of the characters having the `glyph'
18452 property. */
18453 struct it it2;
18454 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
18455
18456 it2 = *it;
18457 if (it->multibyte_p)
18458 {
18459 int maxlen = ((IT_BYTEPOS (*it) >= GPT ? ZV : GPT)
18460 - IT_BYTEPOS (*it));
18461 it2.c = STRING_CHAR_AND_LENGTH (p, maxlen, it2.len);
18462 }
18463 else
18464 it2.c = *p, it2.len = 1;
18465
18466 it2.glyph_row = NULL;
18467 it2.what = IT_CHARACTER;
18468 x_produce_glyphs (&it2);
18469 width = NUMVAL (prop) * it2.pixel_width;
18470 }
18471 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
18472 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
18473 {
18474 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
18475 align_to = (align_to < 0
18476 ? 0
18477 : align_to - window_box_left_offset (it->w, TEXT_AREA));
18478 else if (align_to < 0)
18479 align_to = window_box_left_offset (it->w, TEXT_AREA);
18480 width = max (0, (int)tem + align_to - it->current_x);
18481 zero_width_ok_p = 1;
18482 }
18483 else
18484 /* Nothing specified -> width defaults to canonical char width. */
18485 width = FRAME_COLUMN_WIDTH (it->f);
18486
18487 if (width <= 0 && (width < 0 || !zero_width_ok_p))
18488 width = 1;
18489
18490 /* Compute height. */
18491 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
18492 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
18493 {
18494 height = (int)tem;
18495 zero_height_ok_p = 1;
18496 }
18497 else if (prop = Fplist_get (plist, QCrelative_height),
18498 NUMVAL (prop) > 0)
18499 height = FONT_HEIGHT (font) * NUMVAL (prop);
18500 else
18501 height = FONT_HEIGHT (font);
18502
18503 if (height <= 0 && (height < 0 || !zero_height_ok_p))
18504 height = 1;
18505
18506 /* Compute percentage of height used for ascent. If
18507 `:ascent ASCENT' is present and valid, use that. Otherwise,
18508 derive the ascent from the font in use. */
18509 if (prop = Fplist_get (plist, QCascent),
18510 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
18511 ascent = height * NUMVAL (prop) / 100.0;
18512 else if (!NILP (prop)
18513 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
18514 ascent = min (max (0, (int)tem), height);
18515 else
18516 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
18517
18518 if (width > 0 && height > 0 && it->glyph_row)
18519 {
18520 Lisp_Object object = it->stack[it->sp - 1].string;
18521 if (!STRINGP (object))
18522 object = it->w->buffer;
18523 append_stretch_glyph (it, object, width, height, ascent);
18524 }
18525
18526 it->pixel_width = width;
18527 it->ascent = it->phys_ascent = ascent;
18528 it->descent = it->phys_descent = height - it->ascent;
18529 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
18530
18531 if (width > 0 && height > 0 && face->box != FACE_NO_BOX)
18532 {
18533 if (face->box_line_width > 0)
18534 {
18535 it->ascent += face->box_line_width;
18536 it->descent += face->box_line_width;
18537 }
18538
18539 if (it->start_of_box_run_p)
18540 it->pixel_width += abs (face->box_line_width);
18541 if (it->end_of_box_run_p)
18542 it->pixel_width += abs (face->box_line_width);
18543 }
18544
18545 take_vertical_position_into_account (it);
18546 }
18547
18548 /* Calculate line-height and line-spacing properties.
18549 An integer value specifies explicit pixel value.
18550 A float value specifies relative value to current face height.
18551 A cons (float . face-name) specifies relative value to
18552 height of specified face font.
18553
18554 Returns height in pixels, or nil. */
18555
18556 static Lisp_Object
18557 calc_line_height_property (it, prop, font, boff, total)
18558 struct it *it;
18559 Lisp_Object prop;
18560 XFontStruct *font;
18561 int boff, *total;
18562 {
18563 Lisp_Object position, val;
18564 Lisp_Object face_name = Qnil;
18565 int ascent, descent, height, override;
18566
18567 if (STRINGP (it->object))
18568 position = make_number (IT_STRING_CHARPOS (*it));
18569 else
18570 position = make_number (IT_CHARPOS (*it));
18571
18572 val = Fget_char_property (position, prop, it->object);
18573
18574 if (NILP (val))
18575 return val;
18576
18577 if (total && CONSP (val) && EQ (XCAR (val), Qtotal))
18578 {
18579 *total = 1;
18580 val = XCDR (val);
18581 }
18582
18583 if (INTEGERP (val))
18584 return val;
18585
18586 if (CONSP (val))
18587 {
18588 face_name = XCDR (val);
18589 val = XCAR (val);
18590 }
18591 else if (SYMBOLP (val))
18592 {
18593 face_name = val;
18594 val = Qnil;
18595 }
18596
18597 override = EQ (prop, Qline_height);
18598
18599 if (NILP (face_name))
18600 {
18601 font = FRAME_FONT (it->f);
18602 boff = FRAME_BASELINE_OFFSET (it->f);
18603 }
18604 else if (EQ (face_name, Qt))
18605 {
18606 override = 0;
18607 }
18608 else
18609 {
18610 int face_id;
18611 struct face *face;
18612 struct font_info *font_info;
18613
18614 face_id = lookup_named_face (it->f, face_name, ' ');
18615 if (face_id < 0)
18616 return make_number (-1);
18617
18618 face = FACE_FROM_ID (it->f, face_id);
18619 font = face->font;
18620 if (font == NULL)
18621 return make_number (-1);
18622
18623 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
18624 boff = font_info->baseline_offset;
18625 if (font_info->vertical_centering)
18626 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
18627 }
18628
18629 ascent = FONT_BASE (font) + boff;
18630 descent = FONT_DESCENT (font) - boff;
18631
18632 if (override)
18633 {
18634 it->override_ascent = ascent;
18635 it->override_descent = descent;
18636 it->override_boff = boff;
18637 }
18638
18639 height = ascent + descent;
18640 if (FLOATP (val))
18641 height = (int)(XFLOAT_DATA (val) * height);
18642 else if (INTEGERP (val))
18643 height *= XINT (val);
18644
18645 return make_number (height);
18646 }
18647
18648
18649 /* RIF:
18650 Produce glyphs/get display metrics for the display element IT is
18651 loaded with. See the description of struct display_iterator in
18652 dispextern.h for an overview of struct display_iterator. */
18653
18654 void
18655 x_produce_glyphs (it)
18656 struct it *it;
18657 {
18658 int extra_line_spacing = it->extra_line_spacing;
18659
18660 it->glyph_not_available_p = 0;
18661
18662 if (it->what == IT_CHARACTER)
18663 {
18664 XChar2b char2b;
18665 XFontStruct *font;
18666 struct face *face = FACE_FROM_ID (it->f, it->face_id);
18667 XCharStruct *pcm;
18668 int font_not_found_p;
18669 struct font_info *font_info;
18670 int boff; /* baseline offset */
18671 /* We may change it->multibyte_p upon unibyte<->multibyte
18672 conversion. So, save the current value now and restore it
18673 later.
18674
18675 Note: It seems that we don't have to record multibyte_p in
18676 struct glyph because the character code itself tells if or
18677 not the character is multibyte. Thus, in the future, we must
18678 consider eliminating the field `multibyte_p' in the struct
18679 glyph. */
18680 int saved_multibyte_p = it->multibyte_p;
18681
18682 /* Maybe translate single-byte characters to multibyte, or the
18683 other way. */
18684 it->char_to_display = it->c;
18685 if (!ASCII_BYTE_P (it->c))
18686 {
18687 if (unibyte_display_via_language_environment
18688 && SINGLE_BYTE_CHAR_P (it->c)
18689 && (it->c >= 0240
18690 || !NILP (Vnonascii_translation_table)))
18691 {
18692 it->char_to_display = unibyte_char_to_multibyte (it->c);
18693 it->multibyte_p = 1;
18694 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
18695 face = FACE_FROM_ID (it->f, it->face_id);
18696 }
18697 else if (!SINGLE_BYTE_CHAR_P (it->c)
18698 && !it->multibyte_p)
18699 {
18700 it->multibyte_p = 1;
18701 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
18702 face = FACE_FROM_ID (it->f, it->face_id);
18703 }
18704 }
18705
18706 /* Get font to use. Encode IT->char_to_display. */
18707 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
18708 &char2b, it->multibyte_p, 0);
18709 font = face->font;
18710
18711 /* When no suitable font found, use the default font. */
18712 font_not_found_p = font == NULL;
18713 if (font_not_found_p)
18714 {
18715 font = FRAME_FONT (it->f);
18716 boff = FRAME_BASELINE_OFFSET (it->f);
18717 font_info = NULL;
18718 }
18719 else
18720 {
18721 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
18722 boff = font_info->baseline_offset;
18723 if (font_info->vertical_centering)
18724 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
18725 }
18726
18727 if (it->char_to_display >= ' '
18728 && (!it->multibyte_p || it->char_to_display < 128))
18729 {
18730 /* Either unibyte or ASCII. */
18731 int stretched_p;
18732
18733 it->nglyphs = 1;
18734
18735 pcm = FRAME_RIF (it->f)->per_char_metric
18736 (font, &char2b, FONT_TYPE_FOR_UNIBYTE (font, it->char_to_display));
18737
18738 if (it->override_ascent >= 0)
18739 {
18740 it->ascent = it->override_ascent;
18741 it->descent = it->override_descent;
18742 boff = it->override_boff;
18743 }
18744 else
18745 {
18746 it->ascent = FONT_BASE (font) + boff;
18747 it->descent = FONT_DESCENT (font) - boff;
18748 }
18749
18750 if (pcm)
18751 {
18752 it->phys_ascent = pcm->ascent + boff;
18753 it->phys_descent = pcm->descent - boff;
18754 it->pixel_width = pcm->width;
18755 }
18756 else
18757 {
18758 it->glyph_not_available_p = 1;
18759 it->phys_ascent = it->ascent;
18760 it->phys_descent = it->descent;
18761 it->pixel_width = FONT_WIDTH (font);
18762 }
18763
18764 if (it->constrain_row_ascent_descent_p)
18765 {
18766 if (it->descent > it->max_descent)
18767 {
18768 it->ascent += it->descent - it->max_descent;
18769 it->descent = it->max_descent;
18770 }
18771 if (it->ascent > it->max_ascent)
18772 {
18773 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
18774 it->ascent = it->max_ascent;
18775 }
18776 it->phys_ascent = min (it->phys_ascent, it->ascent);
18777 it->phys_descent = min (it->phys_descent, it->descent);
18778 extra_line_spacing = 0;
18779 }
18780
18781 /* If this is a space inside a region of text with
18782 `space-width' property, change its width. */
18783 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
18784 if (stretched_p)
18785 it->pixel_width *= XFLOATINT (it->space_width);
18786
18787 /* If face has a box, add the box thickness to the character
18788 height. If character has a box line to the left and/or
18789 right, add the box line width to the character's width. */
18790 if (face->box != FACE_NO_BOX)
18791 {
18792 int thick = face->box_line_width;
18793
18794 if (thick > 0)
18795 {
18796 it->ascent += thick;
18797 it->descent += thick;
18798 }
18799 else
18800 thick = -thick;
18801
18802 if (it->start_of_box_run_p)
18803 it->pixel_width += thick;
18804 if (it->end_of_box_run_p)
18805 it->pixel_width += thick;
18806 }
18807
18808 /* If face has an overline, add the height of the overline
18809 (1 pixel) and a 1 pixel margin to the character height. */
18810 if (face->overline_p)
18811 it->ascent += 2;
18812
18813 if (it->constrain_row_ascent_descent_p)
18814 {
18815 if (it->ascent > it->max_ascent)
18816 it->ascent = it->max_ascent;
18817 if (it->descent > it->max_descent)
18818 it->descent = it->max_descent;
18819 }
18820
18821 take_vertical_position_into_account (it);
18822
18823 /* If we have to actually produce glyphs, do it. */
18824 if (it->glyph_row)
18825 {
18826 if (stretched_p)
18827 {
18828 /* Translate a space with a `space-width' property
18829 into a stretch glyph. */
18830 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
18831 / FONT_HEIGHT (font));
18832 append_stretch_glyph (it, it->object, it->pixel_width,
18833 it->ascent + it->descent, ascent);
18834 }
18835 else
18836 append_glyph (it);
18837
18838 /* If characters with lbearing or rbearing are displayed
18839 in this line, record that fact in a flag of the
18840 glyph row. This is used to optimize X output code. */
18841 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
18842 it->glyph_row->contains_overlapping_glyphs_p = 1;
18843 }
18844 }
18845 else if (it->char_to_display == '\n')
18846 {
18847 /* A newline has no width but we need the height of the line.
18848 But if previous part of the line set a height, don't
18849 increase that height */
18850
18851 Lisp_Object height;
18852
18853 it->override_ascent = -1;
18854 it->pixel_width = 0;
18855 it->nglyphs = 0;
18856
18857 height = calc_line_height_property(it, Qline_height, font, boff, 0);
18858
18859 if (it->override_ascent >= 0)
18860 {
18861 it->ascent = it->override_ascent;
18862 it->descent = it->override_descent;
18863 boff = it->override_boff;
18864 }
18865 else
18866 {
18867 it->ascent = FONT_BASE (font) + boff;
18868 it->descent = FONT_DESCENT (font) - boff;
18869 }
18870
18871 if (EQ (height, make_number(0)))
18872 {
18873 if (it->descent > it->max_descent)
18874 {
18875 it->ascent += it->descent - it->max_descent;
18876 it->descent = it->max_descent;
18877 }
18878 if (it->ascent > it->max_ascent)
18879 {
18880 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
18881 it->ascent = it->max_ascent;
18882 }
18883 it->phys_ascent = min (it->phys_ascent, it->ascent);
18884 it->phys_descent = min (it->phys_descent, it->descent);
18885 it->constrain_row_ascent_descent_p = 1;
18886 extra_line_spacing = 0;
18887 }
18888 else
18889 {
18890 Lisp_Object spacing;
18891 int total = 0;
18892
18893 it->phys_ascent = it->ascent;
18894 it->phys_descent = it->descent;
18895
18896 if ((it->max_ascent > 0 || it->max_descent > 0)
18897 && face->box != FACE_NO_BOX
18898 && face->box_line_width > 0)
18899 {
18900 it->ascent += face->box_line_width;
18901 it->descent += face->box_line_width;
18902 }
18903 if (!NILP (height)
18904 && XINT (height) > it->ascent + it->descent)
18905 it->ascent = XINT (height) - it->descent;
18906
18907 spacing = calc_line_height_property(it, Qline_spacing, font, boff, &total);
18908 if (INTEGERP (spacing))
18909 {
18910 extra_line_spacing = XINT (spacing);
18911 if (total)
18912 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
18913 }
18914 }
18915 }
18916 else if (it->char_to_display == '\t')
18917 {
18918 int tab_width = it->tab_width * FRAME_COLUMN_WIDTH (it->f);
18919 int x = it->current_x + it->continuation_lines_width;
18920 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
18921
18922 /* If the distance from the current position to the next tab
18923 stop is less than a canonical character width, use the
18924 tab stop after that. */
18925 if (next_tab_x - x < FRAME_COLUMN_WIDTH (it->f))
18926 next_tab_x += tab_width;
18927
18928 it->pixel_width = next_tab_x - x;
18929 it->nglyphs = 1;
18930 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
18931 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
18932
18933 if (it->glyph_row)
18934 {
18935 append_stretch_glyph (it, it->object, it->pixel_width,
18936 it->ascent + it->descent, it->ascent);
18937 }
18938 }
18939 else
18940 {
18941 /* A multi-byte character. Assume that the display width of the
18942 character is the width of the character multiplied by the
18943 width of the font. */
18944
18945 /* If we found a font, this font should give us the right
18946 metrics. If we didn't find a font, use the frame's
18947 default font and calculate the width of the character
18948 from the charset width; this is what old redisplay code
18949 did. */
18950
18951 pcm = FRAME_RIF (it->f)->per_char_metric (font, &char2b,
18952 FONT_TYPE_FOR_MULTIBYTE (font, it->c));
18953
18954 if (font_not_found_p || !pcm)
18955 {
18956 int charset = CHAR_CHARSET (it->char_to_display);
18957
18958 it->glyph_not_available_p = 1;
18959 it->pixel_width = (FRAME_COLUMN_WIDTH (it->f)
18960 * CHARSET_WIDTH (charset));
18961 it->phys_ascent = FONT_BASE (font) + boff;
18962 it->phys_descent = FONT_DESCENT (font) - boff;
18963 }
18964 else
18965 {
18966 it->pixel_width = pcm->width;
18967 it->phys_ascent = pcm->ascent + boff;
18968 it->phys_descent = pcm->descent - boff;
18969 if (it->glyph_row
18970 && (pcm->lbearing < 0
18971 || pcm->rbearing > pcm->width))
18972 it->glyph_row->contains_overlapping_glyphs_p = 1;
18973 }
18974 it->nglyphs = 1;
18975 it->ascent = FONT_BASE (font) + boff;
18976 it->descent = FONT_DESCENT (font) - boff;
18977 if (face->box != FACE_NO_BOX)
18978 {
18979 int thick = face->box_line_width;
18980
18981 if (thick > 0)
18982 {
18983 it->ascent += thick;
18984 it->descent += thick;
18985 }
18986 else
18987 thick = - thick;
18988
18989 if (it->start_of_box_run_p)
18990 it->pixel_width += thick;
18991 if (it->end_of_box_run_p)
18992 it->pixel_width += thick;
18993 }
18994
18995 /* If face has an overline, add the height of the overline
18996 (1 pixel) and a 1 pixel margin to the character height. */
18997 if (face->overline_p)
18998 it->ascent += 2;
18999
19000 take_vertical_position_into_account (it);
19001
19002 if (it->glyph_row)
19003 append_glyph (it);
19004 }
19005 it->multibyte_p = saved_multibyte_p;
19006 }
19007 else if (it->what == IT_COMPOSITION)
19008 {
19009 /* Note: A composition is represented as one glyph in the
19010 glyph matrix. There are no padding glyphs. */
19011 XChar2b char2b;
19012 XFontStruct *font;
19013 struct face *face = FACE_FROM_ID (it->f, it->face_id);
19014 XCharStruct *pcm;
19015 int font_not_found_p;
19016 struct font_info *font_info;
19017 int boff; /* baseline offset */
19018 struct composition *cmp = composition_table[it->cmp_id];
19019
19020 /* Maybe translate single-byte characters to multibyte. */
19021 it->char_to_display = it->c;
19022 if (unibyte_display_via_language_environment
19023 && SINGLE_BYTE_CHAR_P (it->c)
19024 && (it->c >= 0240
19025 || (it->c >= 0200
19026 && !NILP (Vnonascii_translation_table))))
19027 {
19028 it->char_to_display = unibyte_char_to_multibyte (it->c);
19029 }
19030
19031 /* Get face and font to use. Encode IT->char_to_display. */
19032 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
19033 face = FACE_FROM_ID (it->f, it->face_id);
19034 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
19035 &char2b, it->multibyte_p, 0);
19036 font = face->font;
19037
19038 /* When no suitable font found, use the default font. */
19039 font_not_found_p = font == NULL;
19040 if (font_not_found_p)
19041 {
19042 font = FRAME_FONT (it->f);
19043 boff = FRAME_BASELINE_OFFSET (it->f);
19044 font_info = NULL;
19045 }
19046 else
19047 {
19048 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
19049 boff = font_info->baseline_offset;
19050 if (font_info->vertical_centering)
19051 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
19052 }
19053
19054 /* There are no padding glyphs, so there is only one glyph to
19055 produce for the composition. Important is that pixel_width,
19056 ascent and descent are the values of what is drawn by
19057 draw_glyphs (i.e. the values of the overall glyphs composed). */
19058 it->nglyphs = 1;
19059
19060 /* If we have not yet calculated pixel size data of glyphs of
19061 the composition for the current face font, calculate them
19062 now. Theoretically, we have to check all fonts for the
19063 glyphs, but that requires much time and memory space. So,
19064 here we check only the font of the first glyph. This leads
19065 to incorrect display very rarely, and C-l (recenter) can
19066 correct the display anyway. */
19067 if (cmp->font != (void *) font)
19068 {
19069 /* Ascent and descent of the font of the first character of
19070 this composition (adjusted by baseline offset). Ascent
19071 and descent of overall glyphs should not be less than
19072 them respectively. */
19073 int font_ascent = FONT_BASE (font) + boff;
19074 int font_descent = FONT_DESCENT (font) - boff;
19075 /* Bounding box of the overall glyphs. */
19076 int leftmost, rightmost, lowest, highest;
19077 int i, width, ascent, descent;
19078
19079 cmp->font = (void *) font;
19080
19081 /* Initialize the bounding box. */
19082 if (font_info
19083 && (pcm = FRAME_RIF (it->f)->per_char_metric (font, &char2b,
19084 FONT_TYPE_FOR_MULTIBYTE (font, it->c))))
19085 {
19086 width = pcm->width;
19087 ascent = pcm->ascent;
19088 descent = pcm->descent;
19089 }
19090 else
19091 {
19092 width = FONT_WIDTH (font);
19093 ascent = FONT_BASE (font);
19094 descent = FONT_DESCENT (font);
19095 }
19096
19097 rightmost = width;
19098 lowest = - descent + boff;
19099 highest = ascent + boff;
19100 leftmost = 0;
19101
19102 if (font_info
19103 && font_info->default_ascent
19104 && CHAR_TABLE_P (Vuse_default_ascent)
19105 && !NILP (Faref (Vuse_default_ascent,
19106 make_number (it->char_to_display))))
19107 highest = font_info->default_ascent + boff;
19108
19109 /* Draw the first glyph at the normal position. It may be
19110 shifted to right later if some other glyphs are drawn at
19111 the left. */
19112 cmp->offsets[0] = 0;
19113 cmp->offsets[1] = boff;
19114
19115 /* Set cmp->offsets for the remaining glyphs. */
19116 for (i = 1; i < cmp->glyph_len; i++)
19117 {
19118 int left, right, btm, top;
19119 int ch = COMPOSITION_GLYPH (cmp, i);
19120 int face_id = FACE_FOR_CHAR (it->f, face, ch);
19121
19122 face = FACE_FROM_ID (it->f, face_id);
19123 get_char_face_and_encoding (it->f, ch, face->id,
19124 &char2b, it->multibyte_p, 0);
19125 font = face->font;
19126 if (font == NULL)
19127 {
19128 font = FRAME_FONT (it->f);
19129 boff = FRAME_BASELINE_OFFSET (it->f);
19130 font_info = NULL;
19131 }
19132 else
19133 {
19134 font_info
19135 = FONT_INFO_FROM_ID (it->f, face->font_info_id);
19136 boff = font_info->baseline_offset;
19137 if (font_info->vertical_centering)
19138 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
19139 }
19140
19141 if (font_info
19142 && (pcm = FRAME_RIF (it->f)->per_char_metric (font, &char2b,
19143 FONT_TYPE_FOR_MULTIBYTE (font, ch))))
19144 {
19145 width = pcm->width;
19146 ascent = pcm->ascent;
19147 descent = pcm->descent;
19148 }
19149 else
19150 {
19151 width = FONT_WIDTH (font);
19152 ascent = 1;
19153 descent = 0;
19154 }
19155
19156 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
19157 {
19158 /* Relative composition with or without
19159 alternate chars. */
19160 left = (leftmost + rightmost - width) / 2;
19161 btm = - descent + boff;
19162 if (font_info && font_info->relative_compose
19163 && (! CHAR_TABLE_P (Vignore_relative_composition)
19164 || NILP (Faref (Vignore_relative_composition,
19165 make_number (ch)))))
19166 {
19167
19168 if (- descent >= font_info->relative_compose)
19169 /* One extra pixel between two glyphs. */
19170 btm = highest + 1;
19171 else if (ascent <= 0)
19172 /* One extra pixel between two glyphs. */
19173 btm = lowest - 1 - ascent - descent;
19174 }
19175 }
19176 else
19177 {
19178 /* A composition rule is specified by an integer
19179 value that encodes global and new reference
19180 points (GREF and NREF). GREF and NREF are
19181 specified by numbers as below:
19182
19183 0---1---2 -- ascent
19184 | |
19185 | |
19186 | |
19187 9--10--11 -- center
19188 | |
19189 ---3---4---5--- baseline
19190 | |
19191 6---7---8 -- descent
19192 */
19193 int rule = COMPOSITION_RULE (cmp, i);
19194 int gref, nref, grefx, grefy, nrefx, nrefy;
19195
19196 COMPOSITION_DECODE_RULE (rule, gref, nref);
19197 grefx = gref % 3, nrefx = nref % 3;
19198 grefy = gref / 3, nrefy = nref / 3;
19199
19200 left = (leftmost
19201 + grefx * (rightmost - leftmost) / 2
19202 - nrefx * width / 2);
19203 btm = ((grefy == 0 ? highest
19204 : grefy == 1 ? 0
19205 : grefy == 2 ? lowest
19206 : (highest + lowest) / 2)
19207 - (nrefy == 0 ? ascent + descent
19208 : nrefy == 1 ? descent - boff
19209 : nrefy == 2 ? 0
19210 : (ascent + descent) / 2));
19211 }
19212
19213 cmp->offsets[i * 2] = left;
19214 cmp->offsets[i * 2 + 1] = btm + descent;
19215
19216 /* Update the bounding box of the overall glyphs. */
19217 right = left + width;
19218 top = btm + descent + ascent;
19219 if (left < leftmost)
19220 leftmost = left;
19221 if (right > rightmost)
19222 rightmost = right;
19223 if (top > highest)
19224 highest = top;
19225 if (btm < lowest)
19226 lowest = btm;
19227 }
19228
19229 /* If there are glyphs whose x-offsets are negative,
19230 shift all glyphs to the right and make all x-offsets
19231 non-negative. */
19232 if (leftmost < 0)
19233 {
19234 for (i = 0; i < cmp->glyph_len; i++)
19235 cmp->offsets[i * 2] -= leftmost;
19236 rightmost -= leftmost;
19237 }
19238
19239 cmp->pixel_width = rightmost;
19240 cmp->ascent = highest;
19241 cmp->descent = - lowest;
19242 if (cmp->ascent < font_ascent)
19243 cmp->ascent = font_ascent;
19244 if (cmp->descent < font_descent)
19245 cmp->descent = font_descent;
19246 }
19247
19248 it->pixel_width = cmp->pixel_width;
19249 it->ascent = it->phys_ascent = cmp->ascent;
19250 it->descent = it->phys_descent = cmp->descent;
19251
19252 if (face->box != FACE_NO_BOX)
19253 {
19254 int thick = face->box_line_width;
19255
19256 if (thick > 0)
19257 {
19258 it->ascent += thick;
19259 it->descent += thick;
19260 }
19261 else
19262 thick = - thick;
19263
19264 if (it->start_of_box_run_p)
19265 it->pixel_width += thick;
19266 if (it->end_of_box_run_p)
19267 it->pixel_width += thick;
19268 }
19269
19270 /* If face has an overline, add the height of the overline
19271 (1 pixel) and a 1 pixel margin to the character height. */
19272 if (face->overline_p)
19273 it->ascent += 2;
19274
19275 take_vertical_position_into_account (it);
19276
19277 if (it->glyph_row)
19278 append_composite_glyph (it);
19279 }
19280 else if (it->what == IT_IMAGE)
19281 produce_image_glyph (it);
19282 else if (it->what == IT_STRETCH)
19283 produce_stretch_glyph (it);
19284
19285 /* Accumulate dimensions. Note: can't assume that it->descent > 0
19286 because this isn't true for images with `:ascent 100'. */
19287 xassert (it->ascent >= 0 && it->descent >= 0);
19288 if (it->area == TEXT_AREA)
19289 it->current_x += it->pixel_width;
19290
19291 if (extra_line_spacing > 0)
19292 it->descent += extra_line_spacing;
19293
19294 it->max_ascent = max (it->max_ascent, it->ascent);
19295 it->max_descent = max (it->max_descent, it->descent);
19296 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
19297 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
19298 }
19299
19300 /* EXPORT for RIF:
19301 Output LEN glyphs starting at START at the nominal cursor position.
19302 Advance the nominal cursor over the text. The global variable
19303 updated_window contains the window being updated, updated_row is
19304 the glyph row being updated, and updated_area is the area of that
19305 row being updated. */
19306
19307 void
19308 x_write_glyphs (start, len)
19309 struct glyph *start;
19310 int len;
19311 {
19312 int x, hpos;
19313
19314 xassert (updated_window && updated_row);
19315 BLOCK_INPUT;
19316
19317 /* Write glyphs. */
19318
19319 hpos = start - updated_row->glyphs[updated_area];
19320 x = draw_glyphs (updated_window, output_cursor.x,
19321 updated_row, updated_area,
19322 hpos, hpos + len,
19323 DRAW_NORMAL_TEXT, 0);
19324
19325 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
19326 if (updated_area == TEXT_AREA
19327 && updated_window->phys_cursor_on_p
19328 && updated_window->phys_cursor.vpos == output_cursor.vpos
19329 && updated_window->phys_cursor.hpos >= hpos
19330 && updated_window->phys_cursor.hpos < hpos + len)
19331 updated_window->phys_cursor_on_p = 0;
19332
19333 UNBLOCK_INPUT;
19334
19335 /* Advance the output cursor. */
19336 output_cursor.hpos += len;
19337 output_cursor.x = x;
19338 }
19339
19340
19341 /* EXPORT for RIF:
19342 Insert LEN glyphs from START at the nominal cursor position. */
19343
19344 void
19345 x_insert_glyphs (start, len)
19346 struct glyph *start;
19347 int len;
19348 {
19349 struct frame *f;
19350 struct window *w;
19351 int line_height, shift_by_width, shifted_region_width;
19352 struct glyph_row *row;
19353 struct glyph *glyph;
19354 int frame_x, frame_y, hpos;
19355
19356 xassert (updated_window && updated_row);
19357 BLOCK_INPUT;
19358 w = updated_window;
19359 f = XFRAME (WINDOW_FRAME (w));
19360
19361 /* Get the height of the line we are in. */
19362 row = updated_row;
19363 line_height = row->height;
19364
19365 /* Get the width of the glyphs to insert. */
19366 shift_by_width = 0;
19367 for (glyph = start; glyph < start + len; ++glyph)
19368 shift_by_width += glyph->pixel_width;
19369
19370 /* Get the width of the region to shift right. */
19371 shifted_region_width = (window_box_width (w, updated_area)
19372 - output_cursor.x
19373 - shift_by_width);
19374
19375 /* Shift right. */
19376 frame_x = window_box_left (w, updated_area) + output_cursor.x;
19377 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
19378
19379 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
19380 line_height, shift_by_width);
19381
19382 /* Write the glyphs. */
19383 hpos = start - row->glyphs[updated_area];
19384 draw_glyphs (w, output_cursor.x, row, updated_area,
19385 hpos, hpos + len,
19386 DRAW_NORMAL_TEXT, 0);
19387
19388 /* Advance the output cursor. */
19389 output_cursor.hpos += len;
19390 output_cursor.x += shift_by_width;
19391 UNBLOCK_INPUT;
19392 }
19393
19394
19395 /* EXPORT for RIF:
19396 Erase the current text line from the nominal cursor position
19397 (inclusive) to pixel column TO_X (exclusive). The idea is that
19398 everything from TO_X onward is already erased.
19399
19400 TO_X is a pixel position relative to updated_area of
19401 updated_window. TO_X == -1 means clear to the end of this area. */
19402
19403 void
19404 x_clear_end_of_line (to_x)
19405 int to_x;
19406 {
19407 struct frame *f;
19408 struct window *w = updated_window;
19409 int max_x, min_y, max_y;
19410 int from_x, from_y, to_y;
19411
19412 xassert (updated_window && updated_row);
19413 f = XFRAME (w->frame);
19414
19415 if (updated_row->full_width_p)
19416 max_x = WINDOW_TOTAL_WIDTH (w);
19417 else
19418 max_x = window_box_width (w, updated_area);
19419 max_y = window_text_bottom_y (w);
19420
19421 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
19422 of window. For TO_X > 0, truncate to end of drawing area. */
19423 if (to_x == 0)
19424 return;
19425 else if (to_x < 0)
19426 to_x = max_x;
19427 else
19428 to_x = min (to_x, max_x);
19429
19430 to_y = min (max_y, output_cursor.y + updated_row->height);
19431
19432 /* Notice if the cursor will be cleared by this operation. */
19433 if (!updated_row->full_width_p)
19434 notice_overwritten_cursor (w, updated_area,
19435 output_cursor.x, -1,
19436 updated_row->y,
19437 MATRIX_ROW_BOTTOM_Y (updated_row));
19438
19439 from_x = output_cursor.x;
19440
19441 /* Translate to frame coordinates. */
19442 if (updated_row->full_width_p)
19443 {
19444 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
19445 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
19446 }
19447 else
19448 {
19449 int area_left = window_box_left (w, updated_area);
19450 from_x += area_left;
19451 to_x += area_left;
19452 }
19453
19454 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
19455 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
19456 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
19457
19458 /* Prevent inadvertently clearing to end of the X window. */
19459 if (to_x > from_x && to_y > from_y)
19460 {
19461 BLOCK_INPUT;
19462 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
19463 to_x - from_x, to_y - from_y);
19464 UNBLOCK_INPUT;
19465 }
19466 }
19467
19468 #endif /* HAVE_WINDOW_SYSTEM */
19469
19470
19471 \f
19472 /***********************************************************************
19473 Cursor types
19474 ***********************************************************************/
19475
19476 /* Value is the internal representation of the specified cursor type
19477 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
19478 of the bar cursor. */
19479
19480 static enum text_cursor_kinds
19481 get_specified_cursor_type (arg, width)
19482 Lisp_Object arg;
19483 int *width;
19484 {
19485 enum text_cursor_kinds type;
19486
19487 if (NILP (arg))
19488 return NO_CURSOR;
19489
19490 if (EQ (arg, Qbox))
19491 return FILLED_BOX_CURSOR;
19492
19493 if (EQ (arg, Qhollow))
19494 return HOLLOW_BOX_CURSOR;
19495
19496 if (EQ (arg, Qbar))
19497 {
19498 *width = 2;
19499 return BAR_CURSOR;
19500 }
19501
19502 if (CONSP (arg)
19503 && EQ (XCAR (arg), Qbar)
19504 && INTEGERP (XCDR (arg))
19505 && XINT (XCDR (arg)) >= 0)
19506 {
19507 *width = XINT (XCDR (arg));
19508 return BAR_CURSOR;
19509 }
19510
19511 if (EQ (arg, Qhbar))
19512 {
19513 *width = 2;
19514 return HBAR_CURSOR;
19515 }
19516
19517 if (CONSP (arg)
19518 && EQ (XCAR (arg), Qhbar)
19519 && INTEGERP (XCDR (arg))
19520 && XINT (XCDR (arg)) >= 0)
19521 {
19522 *width = XINT (XCDR (arg));
19523 return HBAR_CURSOR;
19524 }
19525
19526 /* Treat anything unknown as "hollow box cursor".
19527 It was bad to signal an error; people have trouble fixing
19528 .Xdefaults with Emacs, when it has something bad in it. */
19529 type = HOLLOW_BOX_CURSOR;
19530
19531 return type;
19532 }
19533
19534 /* Set the default cursor types for specified frame. */
19535 void
19536 set_frame_cursor_types (f, arg)
19537 struct frame *f;
19538 Lisp_Object arg;
19539 {
19540 int width;
19541 Lisp_Object tem;
19542
19543 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
19544 FRAME_CURSOR_WIDTH (f) = width;
19545
19546 /* By default, set up the blink-off state depending on the on-state. */
19547
19548 tem = Fassoc (arg, Vblink_cursor_alist);
19549 if (!NILP (tem))
19550 {
19551 FRAME_BLINK_OFF_CURSOR (f)
19552 = get_specified_cursor_type (XCDR (tem), &width);
19553 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
19554 }
19555 else
19556 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
19557 }
19558
19559
19560 /* Return the cursor we want to be displayed in window W. Return
19561 width of bar/hbar cursor through WIDTH arg. Return with
19562 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
19563 (i.e. if the `system caret' should track this cursor).
19564
19565 In a mini-buffer window, we want the cursor only to appear if we
19566 are reading input from this window. For the selected window, we
19567 want the cursor type given by the frame parameter or buffer local
19568 setting of cursor-type. If explicitly marked off, draw no cursor.
19569 In all other cases, we want a hollow box cursor. */
19570
19571 static enum text_cursor_kinds
19572 get_window_cursor_type (w, glyph, width, active_cursor)
19573 struct window *w;
19574 struct glyph *glyph;
19575 int *width;
19576 int *active_cursor;
19577 {
19578 struct frame *f = XFRAME (w->frame);
19579 struct buffer *b = XBUFFER (w->buffer);
19580 int cursor_type = DEFAULT_CURSOR;
19581 Lisp_Object alt_cursor;
19582 int non_selected = 0;
19583
19584 *active_cursor = 1;
19585
19586 /* Echo area */
19587 if (cursor_in_echo_area
19588 && FRAME_HAS_MINIBUF_P (f)
19589 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
19590 {
19591 if (w == XWINDOW (echo_area_window))
19592 {
19593 *width = FRAME_CURSOR_WIDTH (f);
19594 return FRAME_DESIRED_CURSOR (f);
19595 }
19596
19597 *active_cursor = 0;
19598 non_selected = 1;
19599 }
19600
19601 /* Nonselected window or nonselected frame. */
19602 else if (w != XWINDOW (f->selected_window)
19603 #ifdef HAVE_WINDOW_SYSTEM
19604 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame
19605 #endif
19606 )
19607 {
19608 *active_cursor = 0;
19609
19610 if (MINI_WINDOW_P (w) && minibuf_level == 0)
19611 return NO_CURSOR;
19612
19613 non_selected = 1;
19614 }
19615
19616 /* Never display a cursor in a window in which cursor-type is nil. */
19617 if (NILP (b->cursor_type))
19618 return NO_CURSOR;
19619
19620 /* Use cursor-in-non-selected-windows for non-selected window or frame. */
19621 if (non_selected)
19622 {
19623 alt_cursor = Fbuffer_local_value (Qcursor_in_non_selected_windows, w->buffer);
19624 return get_specified_cursor_type (alt_cursor, width);
19625 }
19626
19627 /* Get the normal cursor type for this window. */
19628 if (EQ (b->cursor_type, Qt))
19629 {
19630 cursor_type = FRAME_DESIRED_CURSOR (f);
19631 *width = FRAME_CURSOR_WIDTH (f);
19632 }
19633 else
19634 cursor_type = get_specified_cursor_type (b->cursor_type, width);
19635
19636 /* Use normal cursor if not blinked off. */
19637 if (!w->cursor_off_p)
19638 {
19639 if (glyph != NULL && glyph->type == IMAGE_GLYPH) {
19640 if (cursor_type == FILLED_BOX_CURSOR)
19641 cursor_type = HOLLOW_BOX_CURSOR;
19642 }
19643 return cursor_type;
19644 }
19645
19646 /* Cursor is blinked off, so determine how to "toggle" it. */
19647
19648 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
19649 if ((alt_cursor = Fassoc (b->cursor_type, Vblink_cursor_alist), !NILP (alt_cursor)))
19650 return get_specified_cursor_type (XCDR (alt_cursor), width);
19651
19652 /* Then see if frame has specified a specific blink off cursor type. */
19653 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
19654 {
19655 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
19656 return FRAME_BLINK_OFF_CURSOR (f);
19657 }
19658
19659 #if 0
19660 /* Some people liked having a permanently visible blinking cursor,
19661 while others had very strong opinions against it. So it was
19662 decided to remove it. KFS 2003-09-03 */
19663
19664 /* Finally perform built-in cursor blinking:
19665 filled box <-> hollow box
19666 wide [h]bar <-> narrow [h]bar
19667 narrow [h]bar <-> no cursor
19668 other type <-> no cursor */
19669
19670 if (cursor_type == FILLED_BOX_CURSOR)
19671 return HOLLOW_BOX_CURSOR;
19672
19673 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
19674 {
19675 *width = 1;
19676 return cursor_type;
19677 }
19678 #endif
19679
19680 return NO_CURSOR;
19681 }
19682
19683
19684 #ifdef HAVE_WINDOW_SYSTEM
19685
19686 /* Notice when the text cursor of window W has been completely
19687 overwritten by a drawing operation that outputs glyphs in AREA
19688 starting at X0 and ending at X1 in the line starting at Y0 and
19689 ending at Y1. X coordinates are area-relative. X1 < 0 means all
19690 the rest of the line after X0 has been written. Y coordinates
19691 are window-relative. */
19692
19693 static void
19694 notice_overwritten_cursor (w, area, x0, x1, y0, y1)
19695 struct window *w;
19696 enum glyph_row_area area;
19697 int x0, y0, x1, y1;
19698 {
19699 int cx0, cx1, cy0, cy1;
19700 struct glyph_row *row;
19701
19702 if (!w->phys_cursor_on_p)
19703 return;
19704 if (area != TEXT_AREA)
19705 return;
19706
19707 row = w->current_matrix->rows + w->phys_cursor.vpos;
19708 if (!row->displays_text_p)
19709 return;
19710
19711 if (row->cursor_in_fringe_p)
19712 {
19713 row->cursor_in_fringe_p = 0;
19714 draw_fringe_bitmap (w, row, 0);
19715 w->phys_cursor_on_p = 0;
19716 return;
19717 }
19718
19719 cx0 = w->phys_cursor.x;
19720 cx1 = cx0 + w->phys_cursor_width;
19721 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
19722 return;
19723
19724 /* The cursor image will be completely removed from the
19725 screen if the output area intersects the cursor area in
19726 y-direction. When we draw in [y0 y1[, and some part of
19727 the cursor is at y < y0, that part must have been drawn
19728 before. When scrolling, the cursor is erased before
19729 actually scrolling, so we don't come here. When not
19730 scrolling, the rows above the old cursor row must have
19731 changed, and in this case these rows must have written
19732 over the cursor image.
19733
19734 Likewise if part of the cursor is below y1, with the
19735 exception of the cursor being in the first blank row at
19736 the buffer and window end because update_text_area
19737 doesn't draw that row. (Except when it does, but
19738 that's handled in update_text_area.) */
19739
19740 cy0 = w->phys_cursor.y;
19741 cy1 = cy0 + w->phys_cursor_height;
19742 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
19743 return;
19744
19745 w->phys_cursor_on_p = 0;
19746 }
19747
19748 #endif /* HAVE_WINDOW_SYSTEM */
19749
19750 \f
19751 /************************************************************************
19752 Mouse Face
19753 ************************************************************************/
19754
19755 #ifdef HAVE_WINDOW_SYSTEM
19756
19757 /* EXPORT for RIF:
19758 Fix the display of area AREA of overlapping row ROW in window W. */
19759
19760 void
19761 x_fix_overlapping_area (w, row, area)
19762 struct window *w;
19763 struct glyph_row *row;
19764 enum glyph_row_area area;
19765 {
19766 int i, x;
19767
19768 BLOCK_INPUT;
19769
19770 x = 0;
19771 for (i = 0; i < row->used[area];)
19772 {
19773 if (row->glyphs[area][i].overlaps_vertically_p)
19774 {
19775 int start = i, start_x = x;
19776
19777 do
19778 {
19779 x += row->glyphs[area][i].pixel_width;
19780 ++i;
19781 }
19782 while (i < row->used[area]
19783 && row->glyphs[area][i].overlaps_vertically_p);
19784
19785 draw_glyphs (w, start_x, row, area,
19786 start, i,
19787 DRAW_NORMAL_TEXT, 1);
19788 }
19789 else
19790 {
19791 x += row->glyphs[area][i].pixel_width;
19792 ++i;
19793 }
19794 }
19795
19796 UNBLOCK_INPUT;
19797 }
19798
19799
19800 /* EXPORT:
19801 Draw the cursor glyph of window W in glyph row ROW. See the
19802 comment of draw_glyphs for the meaning of HL. */
19803
19804 void
19805 draw_phys_cursor_glyph (w, row, hl)
19806 struct window *w;
19807 struct glyph_row *row;
19808 enum draw_glyphs_face hl;
19809 {
19810 /* If cursor hpos is out of bounds, don't draw garbage. This can
19811 happen in mini-buffer windows when switching between echo area
19812 glyphs and mini-buffer. */
19813 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
19814 {
19815 int on_p = w->phys_cursor_on_p;
19816 int x1;
19817 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
19818 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
19819 hl, 0);
19820 w->phys_cursor_on_p = on_p;
19821
19822 if (hl == DRAW_CURSOR)
19823 w->phys_cursor_width = x1 - w->phys_cursor.x;
19824 /* When we erase the cursor, and ROW is overlapped by other
19825 rows, make sure that these overlapping parts of other rows
19826 are redrawn. */
19827 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
19828 {
19829 if (row > w->current_matrix->rows
19830 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
19831 x_fix_overlapping_area (w, row - 1, TEXT_AREA);
19832
19833 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
19834 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
19835 x_fix_overlapping_area (w, row + 1, TEXT_AREA);
19836 }
19837 }
19838 }
19839
19840
19841 /* EXPORT:
19842 Erase the image of a cursor of window W from the screen. */
19843
19844 void
19845 erase_phys_cursor (w)
19846 struct window *w;
19847 {
19848 struct frame *f = XFRAME (w->frame);
19849 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
19850 int hpos = w->phys_cursor.hpos;
19851 int vpos = w->phys_cursor.vpos;
19852 int mouse_face_here_p = 0;
19853 struct glyph_matrix *active_glyphs = w->current_matrix;
19854 struct glyph_row *cursor_row;
19855 struct glyph *cursor_glyph;
19856 enum draw_glyphs_face hl;
19857
19858 /* No cursor displayed or row invalidated => nothing to do on the
19859 screen. */
19860 if (w->phys_cursor_type == NO_CURSOR)
19861 goto mark_cursor_off;
19862
19863 /* VPOS >= active_glyphs->nrows means that window has been resized.
19864 Don't bother to erase the cursor. */
19865 if (vpos >= active_glyphs->nrows)
19866 goto mark_cursor_off;
19867
19868 /* If row containing cursor is marked invalid, there is nothing we
19869 can do. */
19870 cursor_row = MATRIX_ROW (active_glyphs, vpos);
19871 if (!cursor_row->enabled_p)
19872 goto mark_cursor_off;
19873
19874 /* If row is completely invisible, don't attempt to delete a cursor which
19875 isn't there. This can happen if cursor is at top of a window, and
19876 we switch to a buffer with a header line in that window. */
19877 if (cursor_row->visible_height <= 0)
19878 goto mark_cursor_off;
19879
19880 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
19881 if (cursor_row->cursor_in_fringe_p)
19882 {
19883 cursor_row->cursor_in_fringe_p = 0;
19884 draw_fringe_bitmap (w, cursor_row, 0);
19885 goto mark_cursor_off;
19886 }
19887
19888 /* This can happen when the new row is shorter than the old one.
19889 In this case, either draw_glyphs or clear_end_of_line
19890 should have cleared the cursor. Note that we wouldn't be
19891 able to erase the cursor in this case because we don't have a
19892 cursor glyph at hand. */
19893 if (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])
19894 goto mark_cursor_off;
19895
19896 /* If the cursor is in the mouse face area, redisplay that when
19897 we clear the cursor. */
19898 if (! NILP (dpyinfo->mouse_face_window)
19899 && w == XWINDOW (dpyinfo->mouse_face_window)
19900 && (vpos > dpyinfo->mouse_face_beg_row
19901 || (vpos == dpyinfo->mouse_face_beg_row
19902 && hpos >= dpyinfo->mouse_face_beg_col))
19903 && (vpos < dpyinfo->mouse_face_end_row
19904 || (vpos == dpyinfo->mouse_face_end_row
19905 && hpos < dpyinfo->mouse_face_end_col))
19906 /* Don't redraw the cursor's spot in mouse face if it is at the
19907 end of a line (on a newline). The cursor appears there, but
19908 mouse highlighting does not. */
19909 && cursor_row->used[TEXT_AREA] > hpos)
19910 mouse_face_here_p = 1;
19911
19912 /* Maybe clear the display under the cursor. */
19913 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
19914 {
19915 int x, y;
19916 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
19917
19918 cursor_glyph = get_phys_cursor_glyph (w);
19919 if (cursor_glyph == NULL)
19920 goto mark_cursor_off;
19921
19922 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
19923 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
19924
19925 FRAME_RIF (f)->clear_frame_area (f, x, y,
19926 cursor_glyph->pixel_width, cursor_row->visible_height);
19927 }
19928
19929 /* Erase the cursor by redrawing the character underneath it. */
19930 if (mouse_face_here_p)
19931 hl = DRAW_MOUSE_FACE;
19932 else
19933 hl = DRAW_NORMAL_TEXT;
19934 draw_phys_cursor_glyph (w, cursor_row, hl);
19935
19936 mark_cursor_off:
19937 w->phys_cursor_on_p = 0;
19938 w->phys_cursor_type = NO_CURSOR;
19939 }
19940
19941
19942 /* EXPORT:
19943 Display or clear cursor of window W. If ON is zero, clear the
19944 cursor. If it is non-zero, display the cursor. If ON is nonzero,
19945 where to put the cursor is specified by HPOS, VPOS, X and Y. */
19946
19947 void
19948 display_and_set_cursor (w, on, hpos, vpos, x, y)
19949 struct window *w;
19950 int on, hpos, vpos, x, y;
19951 {
19952 struct frame *f = XFRAME (w->frame);
19953 int new_cursor_type;
19954 int new_cursor_width;
19955 int active_cursor;
19956 struct glyph_row *glyph_row;
19957 struct glyph *glyph;
19958
19959 /* This is pointless on invisible frames, and dangerous on garbaged
19960 windows and frames; in the latter case, the frame or window may
19961 be in the midst of changing its size, and x and y may be off the
19962 window. */
19963 if (! FRAME_VISIBLE_P (f)
19964 || FRAME_GARBAGED_P (f)
19965 || vpos >= w->current_matrix->nrows
19966 || hpos >= w->current_matrix->matrix_w)
19967 return;
19968
19969 /* If cursor is off and we want it off, return quickly. */
19970 if (!on && !w->phys_cursor_on_p)
19971 return;
19972
19973 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
19974 /* If cursor row is not enabled, we don't really know where to
19975 display the cursor. */
19976 if (!glyph_row->enabled_p)
19977 {
19978 w->phys_cursor_on_p = 0;
19979 return;
19980 }
19981
19982 glyph = NULL;
19983 if (!glyph_row->exact_window_width_line_p
19984 || hpos < glyph_row->used[TEXT_AREA])
19985 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
19986
19987 xassert (interrupt_input_blocked);
19988
19989 /* Set new_cursor_type to the cursor we want to be displayed. */
19990 new_cursor_type = get_window_cursor_type (w, glyph,
19991 &new_cursor_width, &active_cursor);
19992
19993 /* If cursor is currently being shown and we don't want it to be or
19994 it is in the wrong place, or the cursor type is not what we want,
19995 erase it. */
19996 if (w->phys_cursor_on_p
19997 && (!on
19998 || w->phys_cursor.x != x
19999 || w->phys_cursor.y != y
20000 || new_cursor_type != w->phys_cursor_type
20001 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
20002 && new_cursor_width != w->phys_cursor_width)))
20003 erase_phys_cursor (w);
20004
20005 /* Don't check phys_cursor_on_p here because that flag is only set
20006 to zero in some cases where we know that the cursor has been
20007 completely erased, to avoid the extra work of erasing the cursor
20008 twice. In other words, phys_cursor_on_p can be 1 and the cursor
20009 still not be visible, or it has only been partly erased. */
20010 if (on)
20011 {
20012 w->phys_cursor_ascent = glyph_row->ascent;
20013 w->phys_cursor_height = glyph_row->height;
20014
20015 /* Set phys_cursor_.* before x_draw_.* is called because some
20016 of them may need the information. */
20017 w->phys_cursor.x = x;
20018 w->phys_cursor.y = glyph_row->y;
20019 w->phys_cursor.hpos = hpos;
20020 w->phys_cursor.vpos = vpos;
20021 }
20022
20023 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
20024 new_cursor_type, new_cursor_width,
20025 on, active_cursor);
20026 }
20027
20028
20029 /* Switch the display of W's cursor on or off, according to the value
20030 of ON. */
20031
20032 static void
20033 update_window_cursor (w, on)
20034 struct window *w;
20035 int on;
20036 {
20037 /* Don't update cursor in windows whose frame is in the process
20038 of being deleted. */
20039 if (w->current_matrix)
20040 {
20041 BLOCK_INPUT;
20042 display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
20043 w->phys_cursor.x, w->phys_cursor.y);
20044 UNBLOCK_INPUT;
20045 }
20046 }
20047
20048
20049 /* Call update_window_cursor with parameter ON_P on all leaf windows
20050 in the window tree rooted at W. */
20051
20052 static void
20053 update_cursor_in_window_tree (w, on_p)
20054 struct window *w;
20055 int on_p;
20056 {
20057 while (w)
20058 {
20059 if (!NILP (w->hchild))
20060 update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
20061 else if (!NILP (w->vchild))
20062 update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
20063 else
20064 update_window_cursor (w, on_p);
20065
20066 w = NILP (w->next) ? 0 : XWINDOW (w->next);
20067 }
20068 }
20069
20070
20071 /* EXPORT:
20072 Display the cursor on window W, or clear it, according to ON_P.
20073 Don't change the cursor's position. */
20074
20075 void
20076 x_update_cursor (f, on_p)
20077 struct frame *f;
20078 int on_p;
20079 {
20080 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
20081 }
20082
20083
20084 /* EXPORT:
20085 Clear the cursor of window W to background color, and mark the
20086 cursor as not shown. This is used when the text where the cursor
20087 is is about to be rewritten. */
20088
20089 void
20090 x_clear_cursor (w)
20091 struct window *w;
20092 {
20093 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
20094 update_window_cursor (w, 0);
20095 }
20096
20097
20098 /* EXPORT:
20099 Display the active region described by mouse_face_* according to DRAW. */
20100
20101 void
20102 show_mouse_face (dpyinfo, draw)
20103 Display_Info *dpyinfo;
20104 enum draw_glyphs_face draw;
20105 {
20106 struct window *w = XWINDOW (dpyinfo->mouse_face_window);
20107 struct frame *f = XFRAME (WINDOW_FRAME (w));
20108
20109 if (/* If window is in the process of being destroyed, don't bother
20110 to do anything. */
20111 w->current_matrix != NULL
20112 /* Don't update mouse highlight if hidden */
20113 && (draw != DRAW_MOUSE_FACE || !dpyinfo->mouse_face_hidden)
20114 /* Recognize when we are called to operate on rows that don't exist
20115 anymore. This can happen when a window is split. */
20116 && dpyinfo->mouse_face_end_row < w->current_matrix->nrows)
20117 {
20118 int phys_cursor_on_p = w->phys_cursor_on_p;
20119 struct glyph_row *row, *first, *last;
20120
20121 first = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_beg_row);
20122 last = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_end_row);
20123
20124 for (row = first; row <= last && row->enabled_p; ++row)
20125 {
20126 int start_hpos, end_hpos, start_x;
20127
20128 /* For all but the first row, the highlight starts at column 0. */
20129 if (row == first)
20130 {
20131 start_hpos = dpyinfo->mouse_face_beg_col;
20132 start_x = dpyinfo->mouse_face_beg_x;
20133 }
20134 else
20135 {
20136 start_hpos = 0;
20137 start_x = 0;
20138 }
20139
20140 if (row == last)
20141 end_hpos = dpyinfo->mouse_face_end_col;
20142 else
20143 end_hpos = row->used[TEXT_AREA];
20144
20145 if (end_hpos > start_hpos)
20146 {
20147 draw_glyphs (w, start_x, row, TEXT_AREA,
20148 start_hpos, end_hpos,
20149 draw, 0);
20150
20151 row->mouse_face_p
20152 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
20153 }
20154 }
20155
20156 /* When we've written over the cursor, arrange for it to
20157 be displayed again. */
20158 if (phys_cursor_on_p && !w->phys_cursor_on_p)
20159 {
20160 BLOCK_INPUT;
20161 display_and_set_cursor (w, 1,
20162 w->phys_cursor.hpos, w->phys_cursor.vpos,
20163 w->phys_cursor.x, w->phys_cursor.y);
20164 UNBLOCK_INPUT;
20165 }
20166 }
20167
20168 /* Change the mouse cursor. */
20169 if (draw == DRAW_NORMAL_TEXT)
20170 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
20171 else if (draw == DRAW_MOUSE_FACE)
20172 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
20173 else
20174 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
20175 }
20176
20177 /* EXPORT:
20178 Clear out the mouse-highlighted active region.
20179 Redraw it un-highlighted first. Value is non-zero if mouse
20180 face was actually drawn unhighlighted. */
20181
20182 int
20183 clear_mouse_face (dpyinfo)
20184 Display_Info *dpyinfo;
20185 {
20186 int cleared = 0;
20187
20188 if (!dpyinfo->mouse_face_hidden && !NILP (dpyinfo->mouse_face_window))
20189 {
20190 show_mouse_face (dpyinfo, DRAW_NORMAL_TEXT);
20191 cleared = 1;
20192 }
20193
20194 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
20195 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
20196 dpyinfo->mouse_face_window = Qnil;
20197 dpyinfo->mouse_face_overlay = Qnil;
20198 return cleared;
20199 }
20200
20201
20202 /* EXPORT:
20203 Non-zero if physical cursor of window W is within mouse face. */
20204
20205 int
20206 cursor_in_mouse_face_p (w)
20207 struct window *w;
20208 {
20209 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
20210 int in_mouse_face = 0;
20211
20212 if (WINDOWP (dpyinfo->mouse_face_window)
20213 && XWINDOW (dpyinfo->mouse_face_window) == w)
20214 {
20215 int hpos = w->phys_cursor.hpos;
20216 int vpos = w->phys_cursor.vpos;
20217
20218 if (vpos >= dpyinfo->mouse_face_beg_row
20219 && vpos <= dpyinfo->mouse_face_end_row
20220 && (vpos > dpyinfo->mouse_face_beg_row
20221 || hpos >= dpyinfo->mouse_face_beg_col)
20222 && (vpos < dpyinfo->mouse_face_end_row
20223 || hpos < dpyinfo->mouse_face_end_col
20224 || dpyinfo->mouse_face_past_end))
20225 in_mouse_face = 1;
20226 }
20227
20228 return in_mouse_face;
20229 }
20230
20231
20232
20233 \f
20234 /* Find the glyph matrix position of buffer position CHARPOS in window
20235 *W. HPOS, *VPOS, *X, and *Y are set to the positions found. W's
20236 current glyphs must be up to date. If CHARPOS is above window
20237 start return (0, 0, 0, 0). If CHARPOS is after end of W, return end
20238 of last line in W. In the row containing CHARPOS, stop before glyphs
20239 having STOP as object. */
20240
20241 #if 1 /* This is a version of fast_find_position that's more correct
20242 in the presence of hscrolling, for example. I didn't install
20243 it right away because the problem fixed is minor, it failed
20244 in 20.x as well, and I think it's too risky to install
20245 so near the release of 21.1. 2001-09-25 gerd. */
20246
20247 static int
20248 fast_find_position (w, charpos, hpos, vpos, x, y, stop)
20249 struct window *w;
20250 int charpos;
20251 int *hpos, *vpos, *x, *y;
20252 Lisp_Object stop;
20253 {
20254 struct glyph_row *row, *first;
20255 struct glyph *glyph, *end;
20256 int past_end = 0;
20257
20258 first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
20259 row = row_containing_pos (w, charpos, first, NULL, 0);
20260 if (row == NULL)
20261 {
20262 if (charpos < MATRIX_ROW_START_CHARPOS (first))
20263 {
20264 *x = *y = *hpos = *vpos = 0;
20265 return 1;
20266 }
20267 else
20268 {
20269 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
20270 past_end = 1;
20271 }
20272 }
20273
20274 *x = row->x;
20275 *y = row->y;
20276 *vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
20277
20278 glyph = row->glyphs[TEXT_AREA];
20279 end = glyph + row->used[TEXT_AREA];
20280
20281 /* Skip over glyphs not having an object at the start of the row.
20282 These are special glyphs like truncation marks on terminal
20283 frames. */
20284 if (row->displays_text_p)
20285 while (glyph < end
20286 && INTEGERP (glyph->object)
20287 && !EQ (stop, glyph->object)
20288 && glyph->charpos < 0)
20289 {
20290 *x += glyph->pixel_width;
20291 ++glyph;
20292 }
20293
20294 while (glyph < end
20295 && !INTEGERP (glyph->object)
20296 && !EQ (stop, glyph->object)
20297 && (!BUFFERP (glyph->object)
20298 || glyph->charpos < charpos))
20299 {
20300 *x += glyph->pixel_width;
20301 ++glyph;
20302 }
20303
20304 *hpos = glyph - row->glyphs[TEXT_AREA];
20305 return !past_end;
20306 }
20307
20308 #else /* not 1 */
20309
20310 static int
20311 fast_find_position (w, pos, hpos, vpos, x, y, stop)
20312 struct window *w;
20313 int pos;
20314 int *hpos, *vpos, *x, *y;
20315 Lisp_Object stop;
20316 {
20317 int i;
20318 int lastcol;
20319 int maybe_next_line_p = 0;
20320 int line_start_position;
20321 int yb = window_text_bottom_y (w);
20322 struct glyph_row *row, *best_row;
20323 int row_vpos, best_row_vpos;
20324 int current_x;
20325
20326 row = best_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
20327 row_vpos = best_row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
20328
20329 while (row->y < yb)
20330 {
20331 if (row->used[TEXT_AREA])
20332 line_start_position = row->glyphs[TEXT_AREA]->charpos;
20333 else
20334 line_start_position = 0;
20335
20336 if (line_start_position > pos)
20337 break;
20338 /* If the position sought is the end of the buffer,
20339 don't include the blank lines at the bottom of the window. */
20340 else if (line_start_position == pos
20341 && pos == BUF_ZV (XBUFFER (w->buffer)))
20342 {
20343 maybe_next_line_p = 1;
20344 break;
20345 }
20346 else if (line_start_position > 0)
20347 {
20348 best_row = row;
20349 best_row_vpos = row_vpos;
20350 }
20351
20352 if (row->y + row->height >= yb)
20353 break;
20354
20355 ++row;
20356 ++row_vpos;
20357 }
20358
20359 /* Find the right column within BEST_ROW. */
20360 lastcol = 0;
20361 current_x = best_row->x;
20362 for (i = 0; i < best_row->used[TEXT_AREA]; i++)
20363 {
20364 struct glyph *glyph = best_row->glyphs[TEXT_AREA] + i;
20365 int charpos = glyph->charpos;
20366
20367 if (BUFFERP (glyph->object))
20368 {
20369 if (charpos == pos)
20370 {
20371 *hpos = i;
20372 *vpos = best_row_vpos;
20373 *x = current_x;
20374 *y = best_row->y;
20375 return 1;
20376 }
20377 else if (charpos > pos)
20378 break;
20379 }
20380 else if (EQ (glyph->object, stop))
20381 break;
20382
20383 if (charpos > 0)
20384 lastcol = i;
20385 current_x += glyph->pixel_width;
20386 }
20387
20388 /* If we're looking for the end of the buffer,
20389 and we didn't find it in the line we scanned,
20390 use the start of the following line. */
20391 if (maybe_next_line_p)
20392 {
20393 ++best_row;
20394 ++best_row_vpos;
20395 lastcol = 0;
20396 current_x = best_row->x;
20397 }
20398
20399 *vpos = best_row_vpos;
20400 *hpos = lastcol + 1;
20401 *x = current_x;
20402 *y = best_row->y;
20403 return 0;
20404 }
20405
20406 #endif /* not 1 */
20407
20408
20409 /* Find the position of the glyph for position POS in OBJECT in
20410 window W's current matrix, and return in *X, *Y the pixel
20411 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
20412
20413 RIGHT_P non-zero means return the position of the right edge of the
20414 glyph, RIGHT_P zero means return the left edge position.
20415
20416 If no glyph for POS exists in the matrix, return the position of
20417 the glyph with the next smaller position that is in the matrix, if
20418 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
20419 exists in the matrix, return the position of the glyph with the
20420 next larger position in OBJECT.
20421
20422 Value is non-zero if a glyph was found. */
20423
20424 static int
20425 fast_find_string_pos (w, pos, object, hpos, vpos, x, y, right_p)
20426 struct window *w;
20427 int pos;
20428 Lisp_Object object;
20429 int *hpos, *vpos, *x, *y;
20430 int right_p;
20431 {
20432 int yb = window_text_bottom_y (w);
20433 struct glyph_row *r;
20434 struct glyph *best_glyph = NULL;
20435 struct glyph_row *best_row = NULL;
20436 int best_x = 0;
20437
20438 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
20439 r->enabled_p && r->y < yb;
20440 ++r)
20441 {
20442 struct glyph *g = r->glyphs[TEXT_AREA];
20443 struct glyph *e = g + r->used[TEXT_AREA];
20444 int gx;
20445
20446 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
20447 if (EQ (g->object, object))
20448 {
20449 if (g->charpos == pos)
20450 {
20451 best_glyph = g;
20452 best_x = gx;
20453 best_row = r;
20454 goto found;
20455 }
20456 else if (best_glyph == NULL
20457 || ((abs (g->charpos - pos)
20458 < abs (best_glyph->charpos - pos))
20459 && (right_p
20460 ? g->charpos < pos
20461 : g->charpos > pos)))
20462 {
20463 best_glyph = g;
20464 best_x = gx;
20465 best_row = r;
20466 }
20467 }
20468 }
20469
20470 found:
20471
20472 if (best_glyph)
20473 {
20474 *x = best_x;
20475 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
20476
20477 if (right_p)
20478 {
20479 *x += best_glyph->pixel_width;
20480 ++*hpos;
20481 }
20482
20483 *y = best_row->y;
20484 *vpos = best_row - w->current_matrix->rows;
20485 }
20486
20487 return best_glyph != NULL;
20488 }
20489
20490
20491 /* See if position X, Y is within a hot-spot of an image. */
20492
20493 static int
20494 on_hot_spot_p (hot_spot, x, y)
20495 Lisp_Object hot_spot;
20496 int x, y;
20497 {
20498 if (!CONSP (hot_spot))
20499 return 0;
20500
20501 if (EQ (XCAR (hot_spot), Qrect))
20502 {
20503 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
20504 Lisp_Object rect = XCDR (hot_spot);
20505 Lisp_Object tem;
20506 if (!CONSP (rect))
20507 return 0;
20508 if (!CONSP (XCAR (rect)))
20509 return 0;
20510 if (!CONSP (XCDR (rect)))
20511 return 0;
20512 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
20513 return 0;
20514 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
20515 return 0;
20516 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
20517 return 0;
20518 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
20519 return 0;
20520 return 1;
20521 }
20522 else if (EQ (XCAR (hot_spot), Qcircle))
20523 {
20524 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
20525 Lisp_Object circ = XCDR (hot_spot);
20526 Lisp_Object lr, lx0, ly0;
20527 if (CONSP (circ)
20528 && CONSP (XCAR (circ))
20529 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
20530 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
20531 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
20532 {
20533 double r = XFLOATINT (lr);
20534 double dx = XINT (lx0) - x;
20535 double dy = XINT (ly0) - y;
20536 return (dx * dx + dy * dy <= r * r);
20537 }
20538 }
20539 else if (EQ (XCAR (hot_spot), Qpoly))
20540 {
20541 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
20542 if (VECTORP (XCDR (hot_spot)))
20543 {
20544 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
20545 Lisp_Object *poly = v->contents;
20546 int n = v->size;
20547 int i;
20548 int inside = 0;
20549 Lisp_Object lx, ly;
20550 int x0, y0;
20551
20552 /* Need an even number of coordinates, and at least 3 edges. */
20553 if (n < 6 || n & 1)
20554 return 0;
20555
20556 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
20557 If count is odd, we are inside polygon. Pixels on edges
20558 may or may not be included depending on actual geometry of the
20559 polygon. */
20560 if ((lx = poly[n-2], !INTEGERP (lx))
20561 || (ly = poly[n-1], !INTEGERP (lx)))
20562 return 0;
20563 x0 = XINT (lx), y0 = XINT (ly);
20564 for (i = 0; i < n; i += 2)
20565 {
20566 int x1 = x0, y1 = y0;
20567 if ((lx = poly[i], !INTEGERP (lx))
20568 || (ly = poly[i+1], !INTEGERP (ly)))
20569 return 0;
20570 x0 = XINT (lx), y0 = XINT (ly);
20571
20572 /* Does this segment cross the X line? */
20573 if (x0 >= x)
20574 {
20575 if (x1 >= x)
20576 continue;
20577 }
20578 else if (x1 < x)
20579 continue;
20580 if (y > y0 && y > y1)
20581 continue;
20582 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
20583 inside = !inside;
20584 }
20585 return inside;
20586 }
20587 }
20588 return 0;
20589 }
20590
20591 Lisp_Object
20592 find_hot_spot (map, x, y)
20593 Lisp_Object map;
20594 int x, y;
20595 {
20596 while (CONSP (map))
20597 {
20598 if (CONSP (XCAR (map))
20599 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
20600 return XCAR (map);
20601 map = XCDR (map);
20602 }
20603
20604 return Qnil;
20605 }
20606
20607 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
20608 3, 3, 0,
20609 doc: /* Lookup in image map MAP coordinates X and Y.
20610 An image map is an alist where each element has the format (AREA ID PLIST).
20611 An AREA is specified as either a rectangle, a circle, or a polygon:
20612 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
20613 pixel coordinates of the upper left and bottom right corners.
20614 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
20615 and the radius of the circle; r may be a float or integer.
20616 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
20617 vector describes one corner in the polygon.
20618 Returns the alist element for the first matching AREA in MAP. */)
20619 (map, x, y)
20620 Lisp_Object map;
20621 Lisp_Object x, y;
20622 {
20623 if (NILP (map))
20624 return Qnil;
20625
20626 CHECK_NUMBER (x);
20627 CHECK_NUMBER (y);
20628
20629 return find_hot_spot (map, XINT (x), XINT (y));
20630 }
20631
20632
20633 /* Display frame CURSOR, optionally using shape defined by POINTER. */
20634 static void
20635 define_frame_cursor1 (f, cursor, pointer)
20636 struct frame *f;
20637 Cursor cursor;
20638 Lisp_Object pointer;
20639 {
20640 if (!NILP (pointer))
20641 {
20642 if (EQ (pointer, Qarrow))
20643 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
20644 else if (EQ (pointer, Qhand))
20645 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
20646 else if (EQ (pointer, Qtext))
20647 cursor = FRAME_X_OUTPUT (f)->text_cursor;
20648 else if (EQ (pointer, intern ("hdrag")))
20649 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
20650 #ifdef HAVE_X_WINDOWS
20651 else if (EQ (pointer, intern ("vdrag")))
20652 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
20653 #endif
20654 else if (EQ (pointer, intern ("hourglass")))
20655 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
20656 else if (EQ (pointer, Qmodeline))
20657 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
20658 else
20659 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
20660 }
20661
20662 if (cursor != No_Cursor)
20663 FRAME_RIF (f)->define_frame_cursor (f, cursor);
20664 }
20665
20666 /* Take proper action when mouse has moved to the mode or header line
20667 or marginal area AREA of window W, x-position X and y-position Y.
20668 X is relative to the start of the text display area of W, so the
20669 width of bitmap areas and scroll bars must be subtracted to get a
20670 position relative to the start of the mode line. */
20671
20672 static void
20673 note_mode_line_or_margin_highlight (w, x, y, area)
20674 struct window *w;
20675 int x, y;
20676 enum window_part area;
20677 {
20678 struct frame *f = XFRAME (w->frame);
20679 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
20680 Cursor cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
20681 Lisp_Object pointer = Qnil;
20682 int charpos, dx, dy, width, height;
20683 Lisp_Object string, object = Qnil;
20684 Lisp_Object pos, help;
20685
20686 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
20687 string = mode_line_string (w, area, &x, &y, &charpos,
20688 &object, &dx, &dy, &width, &height);
20689 else
20690 {
20691 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
20692 string = marginal_area_string (w, area, &x, &y, &charpos,
20693 &object, &dx, &dy, &width, &height);
20694 }
20695
20696 help = Qnil;
20697
20698 if (IMAGEP (object))
20699 {
20700 Lisp_Object image_map, hotspot;
20701 if ((image_map = Fplist_get (XCDR (object), QCmap),
20702 !NILP (image_map))
20703 && (hotspot = find_hot_spot (image_map, dx, dy),
20704 CONSP (hotspot))
20705 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
20706 {
20707 Lisp_Object area_id, plist;
20708
20709 area_id = XCAR (hotspot);
20710 /* Could check AREA_ID to see if we enter/leave this hot-spot.
20711 If so, we could look for mouse-enter, mouse-leave
20712 properties in PLIST (and do something...). */
20713 if ((plist = XCDR (hotspot), CONSP (plist)))
20714 {
20715 pointer = Fplist_get (plist, Qpointer);
20716 if (NILP (pointer))
20717 pointer = Qhand;
20718 help = Fplist_get (plist, Qhelp_echo);
20719 if (!NILP (help))
20720 {
20721 help_echo_string = help;
20722 /* Is this correct? ++kfs */
20723 XSETWINDOW (help_echo_window, w);
20724 help_echo_object = w->buffer;
20725 help_echo_pos = charpos;
20726 }
20727 }
20728 if (NILP (pointer))
20729 pointer = Fplist_get (XCDR (object), QCpointer);
20730 }
20731 }
20732
20733 if (STRINGP (string))
20734 {
20735 pos = make_number (charpos);
20736 /* If we're on a string with `help-echo' text property, arrange
20737 for the help to be displayed. This is done by setting the
20738 global variable help_echo_string to the help string. */
20739 help = Fget_text_property (pos, Qhelp_echo, string);
20740 if (!NILP (help))
20741 {
20742 help_echo_string = help;
20743 XSETWINDOW (help_echo_window, w);
20744 help_echo_object = string;
20745 help_echo_pos = charpos;
20746 }
20747
20748 if (NILP (pointer))
20749 pointer = Fget_text_property (pos, Qpointer, string);
20750
20751 /* Change the mouse pointer according to what is under X/Y. */
20752 if (NILP (pointer) && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
20753 {
20754 Lisp_Object map;
20755 map = Fget_text_property (pos, Qlocal_map, string);
20756 if (!KEYMAPP (map))
20757 map = Fget_text_property (pos, Qkeymap, string);
20758 if (!KEYMAPP (map))
20759 cursor = dpyinfo->vertical_scroll_bar_cursor;
20760 }
20761 }
20762
20763 define_frame_cursor1 (f, cursor, pointer);
20764 }
20765
20766
20767 /* EXPORT:
20768 Take proper action when the mouse has moved to position X, Y on
20769 frame F as regards highlighting characters that have mouse-face
20770 properties. Also de-highlighting chars where the mouse was before.
20771 X and Y can be negative or out of range. */
20772
20773 void
20774 note_mouse_highlight (f, x, y)
20775 struct frame *f;
20776 int x, y;
20777 {
20778 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
20779 enum window_part part;
20780 Lisp_Object window;
20781 struct window *w;
20782 Cursor cursor = No_Cursor;
20783 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
20784 struct buffer *b;
20785
20786 /* When a menu is active, don't highlight because this looks odd. */
20787 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NTGUI)
20788 if (popup_activated ())
20789 return;
20790 #endif
20791
20792 if (NILP (Vmouse_highlight)
20793 || !f->glyphs_initialized_p)
20794 return;
20795
20796 dpyinfo->mouse_face_mouse_x = x;
20797 dpyinfo->mouse_face_mouse_y = y;
20798 dpyinfo->mouse_face_mouse_frame = f;
20799
20800 if (dpyinfo->mouse_face_defer)
20801 return;
20802
20803 if (gc_in_progress)
20804 {
20805 dpyinfo->mouse_face_deferred_gc = 1;
20806 return;
20807 }
20808
20809 /* Which window is that in? */
20810 window = window_from_coordinates (f, x, y, &part, 0, 0, 1);
20811
20812 /* If we were displaying active text in another window, clear that. */
20813 if (! EQ (window, dpyinfo->mouse_face_window))
20814 clear_mouse_face (dpyinfo);
20815
20816 /* Not on a window -> return. */
20817 if (!WINDOWP (window))
20818 return;
20819
20820 /* Reset help_echo_string. It will get recomputed below. */
20821 help_echo_string = Qnil;
20822
20823 /* Convert to window-relative pixel coordinates. */
20824 w = XWINDOW (window);
20825 frame_to_window_pixel_xy (w, &x, &y);
20826
20827 /* Handle tool-bar window differently since it doesn't display a
20828 buffer. */
20829 if (EQ (window, f->tool_bar_window))
20830 {
20831 note_tool_bar_highlight (f, x, y);
20832 return;
20833 }
20834
20835 /* Mouse is on the mode, header line or margin? */
20836 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
20837 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
20838 {
20839 note_mode_line_or_margin_highlight (w, x, y, part);
20840 return;
20841 }
20842
20843 if (part == ON_VERTICAL_BORDER)
20844 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
20845 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE)
20846 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
20847 else
20848 cursor = FRAME_X_OUTPUT (f)->text_cursor;
20849
20850 /* Are we in a window whose display is up to date?
20851 And verify the buffer's text has not changed. */
20852 b = XBUFFER (w->buffer);
20853 if (part == ON_TEXT
20854 && EQ (w->window_end_valid, w->buffer)
20855 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
20856 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
20857 {
20858 int hpos, vpos, pos, i, dx, dy, area;
20859 struct glyph *glyph;
20860 Lisp_Object object;
20861 Lisp_Object mouse_face = Qnil, overlay = Qnil, position;
20862 Lisp_Object *overlay_vec = NULL;
20863 int noverlays;
20864 struct buffer *obuf;
20865 int obegv, ozv, same_region;
20866
20867 /* Find the glyph under X/Y. */
20868 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
20869
20870 /* Look for :pointer property on image. */
20871 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
20872 {
20873 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
20874 if (img != NULL && IMAGEP (img->spec))
20875 {
20876 Lisp_Object image_map, hotspot;
20877 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
20878 !NILP (image_map))
20879 && (hotspot = find_hot_spot (image_map,
20880 glyph->slice.x + dx,
20881 glyph->slice.y + dy),
20882 CONSP (hotspot))
20883 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
20884 {
20885 Lisp_Object area_id, plist;
20886
20887 area_id = XCAR (hotspot);
20888 /* Could check AREA_ID to see if we enter/leave this hot-spot.
20889 If so, we could look for mouse-enter, mouse-leave
20890 properties in PLIST (and do something...). */
20891 if ((plist = XCDR (hotspot), CONSP (plist)))
20892 {
20893 pointer = Fplist_get (plist, Qpointer);
20894 if (NILP (pointer))
20895 pointer = Qhand;
20896 help_echo_string = Fplist_get (plist, Qhelp_echo);
20897 if (!NILP (help_echo_string))
20898 {
20899 help_echo_window = window;
20900 help_echo_object = glyph->object;
20901 help_echo_pos = glyph->charpos;
20902 }
20903 }
20904 }
20905 if (NILP (pointer))
20906 pointer = Fplist_get (XCDR (img->spec), QCpointer);
20907 }
20908 }
20909
20910 /* Clear mouse face if X/Y not over text. */
20911 if (glyph == NULL
20912 || area != TEXT_AREA
20913 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p)
20914 {
20915 if (clear_mouse_face (dpyinfo))
20916 cursor = No_Cursor;
20917 if (NILP (pointer))
20918 {
20919 if (area != TEXT_AREA)
20920 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
20921 else
20922 pointer = Vvoid_text_area_pointer;
20923 }
20924 goto set_cursor;
20925 }
20926
20927 pos = glyph->charpos;
20928 object = glyph->object;
20929 if (!STRINGP (object) && !BUFFERP (object))
20930 goto set_cursor;
20931
20932 /* If we get an out-of-range value, return now; avoid an error. */
20933 if (BUFFERP (object) && pos > BUF_Z (b))
20934 goto set_cursor;
20935
20936 /* Make the window's buffer temporarily current for
20937 overlays_at and compute_char_face. */
20938 obuf = current_buffer;
20939 current_buffer = b;
20940 obegv = BEGV;
20941 ozv = ZV;
20942 BEGV = BEG;
20943 ZV = Z;
20944
20945 /* Is this char mouse-active or does it have help-echo? */
20946 position = make_number (pos);
20947
20948 if (BUFFERP (object))
20949 {
20950 /* Put all the overlays we want in a vector in overlay_vec. */
20951 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
20952 /* Sort overlays into increasing priority order. */
20953 noverlays = sort_overlays (overlay_vec, noverlays, w);
20954 }
20955 else
20956 noverlays = 0;
20957
20958 same_region = (EQ (window, dpyinfo->mouse_face_window)
20959 && vpos >= dpyinfo->mouse_face_beg_row
20960 && vpos <= dpyinfo->mouse_face_end_row
20961 && (vpos > dpyinfo->mouse_face_beg_row
20962 || hpos >= dpyinfo->mouse_face_beg_col)
20963 && (vpos < dpyinfo->mouse_face_end_row
20964 || hpos < dpyinfo->mouse_face_end_col
20965 || dpyinfo->mouse_face_past_end));
20966
20967 if (same_region)
20968 cursor = No_Cursor;
20969
20970 /* Check mouse-face highlighting. */
20971 if (! same_region
20972 /* If there exists an overlay with mouse-face overlapping
20973 the one we are currently highlighting, we have to
20974 check if we enter the overlapping overlay, and then
20975 highlight only that. */
20976 || (OVERLAYP (dpyinfo->mouse_face_overlay)
20977 && mouse_face_overlay_overlaps (dpyinfo->mouse_face_overlay)))
20978 {
20979 /* Find the highest priority overlay that has a mouse-face
20980 property. */
20981 overlay = Qnil;
20982 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
20983 {
20984 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
20985 if (!NILP (mouse_face))
20986 overlay = overlay_vec[i];
20987 }
20988
20989 /* If we're actually highlighting the same overlay as
20990 before, there's no need to do that again. */
20991 if (!NILP (overlay)
20992 && EQ (overlay, dpyinfo->mouse_face_overlay))
20993 goto check_help_echo;
20994
20995 dpyinfo->mouse_face_overlay = overlay;
20996
20997 /* Clear the display of the old active region, if any. */
20998 if (clear_mouse_face (dpyinfo))
20999 cursor = No_Cursor;
21000
21001 /* If no overlay applies, get a text property. */
21002 if (NILP (overlay))
21003 mouse_face = Fget_text_property (position, Qmouse_face, object);
21004
21005 /* Handle the overlay case. */
21006 if (!NILP (overlay))
21007 {
21008 /* Find the range of text around this char that
21009 should be active. */
21010 Lisp_Object before, after;
21011 int ignore;
21012
21013 before = Foverlay_start (overlay);
21014 after = Foverlay_end (overlay);
21015 /* Record this as the current active region. */
21016 fast_find_position (w, XFASTINT (before),
21017 &dpyinfo->mouse_face_beg_col,
21018 &dpyinfo->mouse_face_beg_row,
21019 &dpyinfo->mouse_face_beg_x,
21020 &dpyinfo->mouse_face_beg_y, Qnil);
21021
21022 dpyinfo->mouse_face_past_end
21023 = !fast_find_position (w, XFASTINT (after),
21024 &dpyinfo->mouse_face_end_col,
21025 &dpyinfo->mouse_face_end_row,
21026 &dpyinfo->mouse_face_end_x,
21027 &dpyinfo->mouse_face_end_y, Qnil);
21028 dpyinfo->mouse_face_window = window;
21029
21030 dpyinfo->mouse_face_face_id
21031 = face_at_buffer_position (w, pos, 0, 0,
21032 &ignore, pos + 1,
21033 !dpyinfo->mouse_face_hidden);
21034
21035 /* Display it as active. */
21036 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
21037 cursor = No_Cursor;
21038 }
21039 /* Handle the text property case. */
21040 else if (!NILP (mouse_face) && BUFFERP (object))
21041 {
21042 /* Find the range of text around this char that
21043 should be active. */
21044 Lisp_Object before, after, beginning, end;
21045 int ignore;
21046
21047 beginning = Fmarker_position (w->start);
21048 end = make_number (BUF_Z (XBUFFER (object))
21049 - XFASTINT (w->window_end_pos));
21050 before
21051 = Fprevious_single_property_change (make_number (pos + 1),
21052 Qmouse_face,
21053 object, beginning);
21054 after
21055 = Fnext_single_property_change (position, Qmouse_face,
21056 object, end);
21057
21058 /* Record this as the current active region. */
21059 fast_find_position (w, XFASTINT (before),
21060 &dpyinfo->mouse_face_beg_col,
21061 &dpyinfo->mouse_face_beg_row,
21062 &dpyinfo->mouse_face_beg_x,
21063 &dpyinfo->mouse_face_beg_y, Qnil);
21064 dpyinfo->mouse_face_past_end
21065 = !fast_find_position (w, XFASTINT (after),
21066 &dpyinfo->mouse_face_end_col,
21067 &dpyinfo->mouse_face_end_row,
21068 &dpyinfo->mouse_face_end_x,
21069 &dpyinfo->mouse_face_end_y, Qnil);
21070 dpyinfo->mouse_face_window = window;
21071
21072 if (BUFFERP (object))
21073 dpyinfo->mouse_face_face_id
21074 = face_at_buffer_position (w, pos, 0, 0,
21075 &ignore, pos + 1,
21076 !dpyinfo->mouse_face_hidden);
21077
21078 /* Display it as active. */
21079 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
21080 cursor = No_Cursor;
21081 }
21082 else if (!NILP (mouse_face) && STRINGP (object))
21083 {
21084 Lisp_Object b, e;
21085 int ignore;
21086
21087 b = Fprevious_single_property_change (make_number (pos + 1),
21088 Qmouse_face,
21089 object, Qnil);
21090 e = Fnext_single_property_change (position, Qmouse_face,
21091 object, Qnil);
21092 if (NILP (b))
21093 b = make_number (0);
21094 if (NILP (e))
21095 e = make_number (SCHARS (object) - 1);
21096 fast_find_string_pos (w, XINT (b), object,
21097 &dpyinfo->mouse_face_beg_col,
21098 &dpyinfo->mouse_face_beg_row,
21099 &dpyinfo->mouse_face_beg_x,
21100 &dpyinfo->mouse_face_beg_y, 0);
21101 fast_find_string_pos (w, XINT (e), object,
21102 &dpyinfo->mouse_face_end_col,
21103 &dpyinfo->mouse_face_end_row,
21104 &dpyinfo->mouse_face_end_x,
21105 &dpyinfo->mouse_face_end_y, 1);
21106 dpyinfo->mouse_face_past_end = 0;
21107 dpyinfo->mouse_face_window = window;
21108 dpyinfo->mouse_face_face_id
21109 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
21110 glyph->face_id, 1);
21111 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
21112 cursor = No_Cursor;
21113 }
21114 else if (STRINGP (object) && NILP (mouse_face))
21115 {
21116 /* A string which doesn't have mouse-face, but
21117 the text ``under'' it might have. */
21118 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
21119 int start = MATRIX_ROW_START_CHARPOS (r);
21120
21121 pos = string_buffer_position (w, object, start);
21122 if (pos > 0)
21123 mouse_face = get_char_property_and_overlay (make_number (pos),
21124 Qmouse_face,
21125 w->buffer,
21126 &overlay);
21127 if (!NILP (mouse_face) && !NILP (overlay))
21128 {
21129 Lisp_Object before = Foverlay_start (overlay);
21130 Lisp_Object after = Foverlay_end (overlay);
21131 int ignore;
21132
21133 /* Note that we might not be able to find position
21134 BEFORE in the glyph matrix if the overlay is
21135 entirely covered by a `display' property. In
21136 this case, we overshoot. So let's stop in
21137 the glyph matrix before glyphs for OBJECT. */
21138 fast_find_position (w, XFASTINT (before),
21139 &dpyinfo->mouse_face_beg_col,
21140 &dpyinfo->mouse_face_beg_row,
21141 &dpyinfo->mouse_face_beg_x,
21142 &dpyinfo->mouse_face_beg_y,
21143 object);
21144
21145 dpyinfo->mouse_face_past_end
21146 = !fast_find_position (w, XFASTINT (after),
21147 &dpyinfo->mouse_face_end_col,
21148 &dpyinfo->mouse_face_end_row,
21149 &dpyinfo->mouse_face_end_x,
21150 &dpyinfo->mouse_face_end_y,
21151 Qnil);
21152 dpyinfo->mouse_face_window = window;
21153 dpyinfo->mouse_face_face_id
21154 = face_at_buffer_position (w, pos, 0, 0,
21155 &ignore, pos + 1,
21156 !dpyinfo->mouse_face_hidden);
21157
21158 /* Display it as active. */
21159 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
21160 cursor = No_Cursor;
21161 }
21162 }
21163 }
21164
21165 check_help_echo:
21166
21167 /* Look for a `help-echo' property. */
21168 if (NILP (help_echo_string)) {
21169 Lisp_Object help, overlay;
21170
21171 /* Check overlays first. */
21172 help = overlay = Qnil;
21173 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
21174 {
21175 overlay = overlay_vec[i];
21176 help = Foverlay_get (overlay, Qhelp_echo);
21177 }
21178
21179 if (!NILP (help))
21180 {
21181 help_echo_string = help;
21182 help_echo_window = window;
21183 help_echo_object = overlay;
21184 help_echo_pos = pos;
21185 }
21186 else
21187 {
21188 Lisp_Object object = glyph->object;
21189 int charpos = glyph->charpos;
21190
21191 /* Try text properties. */
21192 if (STRINGP (object)
21193 && charpos >= 0
21194 && charpos < SCHARS (object))
21195 {
21196 help = Fget_text_property (make_number (charpos),
21197 Qhelp_echo, object);
21198 if (NILP (help))
21199 {
21200 /* If the string itself doesn't specify a help-echo,
21201 see if the buffer text ``under'' it does. */
21202 struct glyph_row *r
21203 = MATRIX_ROW (w->current_matrix, vpos);
21204 int start = MATRIX_ROW_START_CHARPOS (r);
21205 int pos = string_buffer_position (w, object, start);
21206 if (pos > 0)
21207 {
21208 help = Fget_char_property (make_number (pos),
21209 Qhelp_echo, w->buffer);
21210 if (!NILP (help))
21211 {
21212 charpos = pos;
21213 object = w->buffer;
21214 }
21215 }
21216 }
21217 }
21218 else if (BUFFERP (object)
21219 && charpos >= BEGV
21220 && charpos < ZV)
21221 help = Fget_text_property (make_number (charpos), Qhelp_echo,
21222 object);
21223
21224 if (!NILP (help))
21225 {
21226 help_echo_string = help;
21227 help_echo_window = window;
21228 help_echo_object = object;
21229 help_echo_pos = charpos;
21230 }
21231 }
21232 }
21233
21234 /* Look for a `pointer' property. */
21235 if (NILP (pointer))
21236 {
21237 /* Check overlays first. */
21238 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
21239 pointer = Foverlay_get (overlay_vec[i], Qpointer);
21240
21241 if (NILP (pointer))
21242 {
21243 Lisp_Object object = glyph->object;
21244 int charpos = glyph->charpos;
21245
21246 /* Try text properties. */
21247 if (STRINGP (object)
21248 && charpos >= 0
21249 && charpos < SCHARS (object))
21250 {
21251 pointer = Fget_text_property (make_number (charpos),
21252 Qpointer, object);
21253 if (NILP (pointer))
21254 {
21255 /* If the string itself doesn't specify a pointer,
21256 see if the buffer text ``under'' it does. */
21257 struct glyph_row *r
21258 = MATRIX_ROW (w->current_matrix, vpos);
21259 int start = MATRIX_ROW_START_CHARPOS (r);
21260 int pos = string_buffer_position (w, object, start);
21261 if (pos > 0)
21262 pointer = Fget_char_property (make_number (pos),
21263 Qpointer, w->buffer);
21264 }
21265 }
21266 else if (BUFFERP (object)
21267 && charpos >= BEGV
21268 && charpos < ZV)
21269 pointer = Fget_text_property (make_number (charpos),
21270 Qpointer, object);
21271 }
21272 }
21273
21274 BEGV = obegv;
21275 ZV = ozv;
21276 current_buffer = obuf;
21277 }
21278
21279 set_cursor:
21280
21281 define_frame_cursor1 (f, cursor, pointer);
21282 }
21283
21284
21285 /* EXPORT for RIF:
21286 Clear any mouse-face on window W. This function is part of the
21287 redisplay interface, and is called from try_window_id and similar
21288 functions to ensure the mouse-highlight is off. */
21289
21290 void
21291 x_clear_window_mouse_face (w)
21292 struct window *w;
21293 {
21294 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
21295 Lisp_Object window;
21296
21297 BLOCK_INPUT;
21298 XSETWINDOW (window, w);
21299 if (EQ (window, dpyinfo->mouse_face_window))
21300 clear_mouse_face (dpyinfo);
21301 UNBLOCK_INPUT;
21302 }
21303
21304
21305 /* EXPORT:
21306 Just discard the mouse face information for frame F, if any.
21307 This is used when the size of F is changed. */
21308
21309 void
21310 cancel_mouse_face (f)
21311 struct frame *f;
21312 {
21313 Lisp_Object window;
21314 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
21315
21316 window = dpyinfo->mouse_face_window;
21317 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
21318 {
21319 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
21320 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
21321 dpyinfo->mouse_face_window = Qnil;
21322 }
21323 }
21324
21325
21326 #endif /* HAVE_WINDOW_SYSTEM */
21327
21328 \f
21329 /***********************************************************************
21330 Exposure Events
21331 ***********************************************************************/
21332
21333 #ifdef HAVE_WINDOW_SYSTEM
21334
21335 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
21336 which intersects rectangle R. R is in window-relative coordinates. */
21337
21338 static void
21339 expose_area (w, row, r, area)
21340 struct window *w;
21341 struct glyph_row *row;
21342 XRectangle *r;
21343 enum glyph_row_area area;
21344 {
21345 struct glyph *first = row->glyphs[area];
21346 struct glyph *end = row->glyphs[area] + row->used[area];
21347 struct glyph *last;
21348 int first_x, start_x, x;
21349
21350 if (area == TEXT_AREA && row->fill_line_p)
21351 /* If row extends face to end of line write the whole line. */
21352 draw_glyphs (w, 0, row, area,
21353 0, row->used[area],
21354 DRAW_NORMAL_TEXT, 0);
21355 else
21356 {
21357 /* Set START_X to the window-relative start position for drawing glyphs of
21358 AREA. The first glyph of the text area can be partially visible.
21359 The first glyphs of other areas cannot. */
21360 start_x = window_box_left_offset (w, area);
21361 x = start_x;
21362 if (area == TEXT_AREA)
21363 x += row->x;
21364
21365 /* Find the first glyph that must be redrawn. */
21366 while (first < end
21367 && x + first->pixel_width < r->x)
21368 {
21369 x += first->pixel_width;
21370 ++first;
21371 }
21372
21373 /* Find the last one. */
21374 last = first;
21375 first_x = x;
21376 while (last < end
21377 && x < r->x + r->width)
21378 {
21379 x += last->pixel_width;
21380 ++last;
21381 }
21382
21383 /* Repaint. */
21384 if (last > first)
21385 draw_glyphs (w, first_x - start_x, row, area,
21386 first - row->glyphs[area], last - row->glyphs[area],
21387 DRAW_NORMAL_TEXT, 0);
21388 }
21389 }
21390
21391
21392 /* Redraw the parts of the glyph row ROW on window W intersecting
21393 rectangle R. R is in window-relative coordinates. Value is
21394 non-zero if mouse-face was overwritten. */
21395
21396 static int
21397 expose_line (w, row, r)
21398 struct window *w;
21399 struct glyph_row *row;
21400 XRectangle *r;
21401 {
21402 xassert (row->enabled_p);
21403
21404 if (row->mode_line_p || w->pseudo_window_p)
21405 draw_glyphs (w, 0, row, TEXT_AREA,
21406 0, row->used[TEXT_AREA],
21407 DRAW_NORMAL_TEXT, 0);
21408 else
21409 {
21410 if (row->used[LEFT_MARGIN_AREA])
21411 expose_area (w, row, r, LEFT_MARGIN_AREA);
21412 if (row->used[TEXT_AREA])
21413 expose_area (w, row, r, TEXT_AREA);
21414 if (row->used[RIGHT_MARGIN_AREA])
21415 expose_area (w, row, r, RIGHT_MARGIN_AREA);
21416 draw_row_fringe_bitmaps (w, row);
21417 }
21418
21419 return row->mouse_face_p;
21420 }
21421
21422
21423 /* Redraw those parts of glyphs rows during expose event handling that
21424 overlap other rows. Redrawing of an exposed line writes over parts
21425 of lines overlapping that exposed line; this function fixes that.
21426
21427 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
21428 row in W's current matrix that is exposed and overlaps other rows.
21429 LAST_OVERLAPPING_ROW is the last such row. */
21430
21431 static void
21432 expose_overlaps (w, first_overlapping_row, last_overlapping_row)
21433 struct window *w;
21434 struct glyph_row *first_overlapping_row;
21435 struct glyph_row *last_overlapping_row;
21436 {
21437 struct glyph_row *row;
21438
21439 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
21440 if (row->overlapping_p)
21441 {
21442 xassert (row->enabled_p && !row->mode_line_p);
21443
21444 if (row->used[LEFT_MARGIN_AREA])
21445 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA);
21446
21447 if (row->used[TEXT_AREA])
21448 x_fix_overlapping_area (w, row, TEXT_AREA);
21449
21450 if (row->used[RIGHT_MARGIN_AREA])
21451 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA);
21452 }
21453 }
21454
21455
21456 /* Return non-zero if W's cursor intersects rectangle R. */
21457
21458 static int
21459 phys_cursor_in_rect_p (w, r)
21460 struct window *w;
21461 XRectangle *r;
21462 {
21463 XRectangle cr, result;
21464 struct glyph *cursor_glyph;
21465
21466 cursor_glyph = get_phys_cursor_glyph (w);
21467 if (cursor_glyph)
21468 {
21469 /* r is relative to W's box, but w->phys_cursor.x is relative
21470 to left edge of W's TEXT area. Adjust it. */
21471 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
21472 cr.y = w->phys_cursor.y;
21473 cr.width = cursor_glyph->pixel_width;
21474 cr.height = w->phys_cursor_height;
21475 /* ++KFS: W32 version used W32-specific IntersectRect here, but
21476 I assume the effect is the same -- and this is portable. */
21477 return x_intersect_rectangles (&cr, r, &result);
21478 }
21479 /* If we don't understand the format, pretend we're not in the hot-spot. */
21480 return 0;
21481 }
21482
21483
21484 /* EXPORT:
21485 Draw a vertical window border to the right of window W if W doesn't
21486 have vertical scroll bars. */
21487
21488 void
21489 x_draw_vertical_border (w)
21490 struct window *w;
21491 {
21492 struct frame *f = XFRAME (WINDOW_FRAME (w));
21493
21494 /* We could do better, if we knew what type of scroll-bar the adjacent
21495 windows (on either side) have... But we don't :-(
21496 However, I think this works ok. ++KFS 2003-04-25 */
21497
21498 /* Redraw borders between horizontally adjacent windows. Don't
21499 do it for frames with vertical scroll bars because either the
21500 right scroll bar of a window, or the left scroll bar of its
21501 neighbor will suffice as a border. */
21502 if (!WINDOW_RIGHTMOST_P (w)
21503 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
21504 {
21505 int x0, x1, y0, y1;
21506
21507 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
21508 y1 -= 1;
21509
21510 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
21511 }
21512 else if (!WINDOW_LEFTMOST_P (w)
21513 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
21514 {
21515 int x0, x1, y0, y1;
21516
21517 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
21518 y1 -= 1;
21519
21520 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
21521 }
21522 }
21523
21524
21525 /* Redraw the part of window W intersection rectangle FR. Pixel
21526 coordinates in FR are frame-relative. Call this function with
21527 input blocked. Value is non-zero if the exposure overwrites
21528 mouse-face. */
21529
21530 static int
21531 expose_window (w, fr)
21532 struct window *w;
21533 XRectangle *fr;
21534 {
21535 struct frame *f = XFRAME (w->frame);
21536 XRectangle wr, r;
21537 int mouse_face_overwritten_p = 0;
21538
21539 /* If window is not yet fully initialized, do nothing. This can
21540 happen when toolkit scroll bars are used and a window is split.
21541 Reconfiguring the scroll bar will generate an expose for a newly
21542 created window. */
21543 if (w->current_matrix == NULL)
21544 return 0;
21545
21546 /* When we're currently updating the window, display and current
21547 matrix usually don't agree. Arrange for a thorough display
21548 later. */
21549 if (w == updated_window)
21550 {
21551 SET_FRAME_GARBAGED (f);
21552 return 0;
21553 }
21554
21555 /* Frame-relative pixel rectangle of W. */
21556 wr.x = WINDOW_LEFT_EDGE_X (w);
21557 wr.y = WINDOW_TOP_EDGE_Y (w);
21558 wr.width = WINDOW_TOTAL_WIDTH (w);
21559 wr.height = WINDOW_TOTAL_HEIGHT (w);
21560
21561 if (x_intersect_rectangles (fr, &wr, &r))
21562 {
21563 int yb = window_text_bottom_y (w);
21564 struct glyph_row *row;
21565 int cursor_cleared_p;
21566 struct glyph_row *first_overlapping_row, *last_overlapping_row;
21567
21568 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
21569 r.x, r.y, r.width, r.height));
21570
21571 /* Convert to window coordinates. */
21572 r.x -= WINDOW_LEFT_EDGE_X (w);
21573 r.y -= WINDOW_TOP_EDGE_Y (w);
21574
21575 /* Turn off the cursor. */
21576 if (!w->pseudo_window_p
21577 && phys_cursor_in_rect_p (w, &r))
21578 {
21579 x_clear_cursor (w);
21580 cursor_cleared_p = 1;
21581 }
21582 else
21583 cursor_cleared_p = 0;
21584
21585 /* Update lines intersecting rectangle R. */
21586 first_overlapping_row = last_overlapping_row = NULL;
21587 for (row = w->current_matrix->rows;
21588 row->enabled_p;
21589 ++row)
21590 {
21591 int y0 = row->y;
21592 int y1 = MATRIX_ROW_BOTTOM_Y (row);
21593
21594 if ((y0 >= r.y && y0 < r.y + r.height)
21595 || (y1 > r.y && y1 < r.y + r.height)
21596 || (r.y >= y0 && r.y < y1)
21597 || (r.y + r.height > y0 && r.y + r.height < y1))
21598 {
21599 if (row->overlapping_p)
21600 {
21601 if (first_overlapping_row == NULL)
21602 first_overlapping_row = row;
21603 last_overlapping_row = row;
21604 }
21605
21606 if (expose_line (w, row, &r))
21607 mouse_face_overwritten_p = 1;
21608 }
21609
21610 if (y1 >= yb)
21611 break;
21612 }
21613
21614 /* Display the mode line if there is one. */
21615 if (WINDOW_WANTS_MODELINE_P (w)
21616 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
21617 row->enabled_p)
21618 && row->y < r.y + r.height)
21619 {
21620 if (expose_line (w, row, &r))
21621 mouse_face_overwritten_p = 1;
21622 }
21623
21624 if (!w->pseudo_window_p)
21625 {
21626 /* Fix the display of overlapping rows. */
21627 if (first_overlapping_row)
21628 expose_overlaps (w, first_overlapping_row, last_overlapping_row);
21629
21630 /* Draw border between windows. */
21631 x_draw_vertical_border (w);
21632
21633 /* Turn the cursor on again. */
21634 if (cursor_cleared_p)
21635 update_window_cursor (w, 1);
21636 }
21637 }
21638
21639 #ifdef HAVE_CARBON
21640 /* Display scroll bar for this window. */
21641 if (!NILP (w->vertical_scroll_bar))
21642 {
21643 /* ++KFS:
21644 If this doesn't work here (maybe some header files are missing),
21645 make a function in macterm.c and call it to do the job! */
21646 ControlHandle ch
21647 = SCROLL_BAR_CONTROL_HANDLE (XSCROLL_BAR (w->vertical_scroll_bar));
21648
21649 Draw1Control (ch);
21650 }
21651 #endif
21652
21653 return mouse_face_overwritten_p;
21654 }
21655
21656
21657
21658 /* Redraw (parts) of all windows in the window tree rooted at W that
21659 intersect R. R contains frame pixel coordinates. Value is
21660 non-zero if the exposure overwrites mouse-face. */
21661
21662 static int
21663 expose_window_tree (w, r)
21664 struct window *w;
21665 XRectangle *r;
21666 {
21667 struct frame *f = XFRAME (w->frame);
21668 int mouse_face_overwritten_p = 0;
21669
21670 while (w && !FRAME_GARBAGED_P (f))
21671 {
21672 if (!NILP (w->hchild))
21673 mouse_face_overwritten_p
21674 |= expose_window_tree (XWINDOW (w->hchild), r);
21675 else if (!NILP (w->vchild))
21676 mouse_face_overwritten_p
21677 |= expose_window_tree (XWINDOW (w->vchild), r);
21678 else
21679 mouse_face_overwritten_p |= expose_window (w, r);
21680
21681 w = NILP (w->next) ? NULL : XWINDOW (w->next);
21682 }
21683
21684 return mouse_face_overwritten_p;
21685 }
21686
21687
21688 /* EXPORT:
21689 Redisplay an exposed area of frame F. X and Y are the upper-left
21690 corner of the exposed rectangle. W and H are width and height of
21691 the exposed area. All are pixel values. W or H zero means redraw
21692 the entire frame. */
21693
21694 void
21695 expose_frame (f, x, y, w, h)
21696 struct frame *f;
21697 int x, y, w, h;
21698 {
21699 XRectangle r;
21700 int mouse_face_overwritten_p = 0;
21701
21702 TRACE ((stderr, "expose_frame "));
21703
21704 /* No need to redraw if frame will be redrawn soon. */
21705 if (FRAME_GARBAGED_P (f))
21706 {
21707 TRACE ((stderr, " garbaged\n"));
21708 return;
21709 }
21710
21711 #ifdef HAVE_CARBON
21712 /* MAC_TODO: this is a kludge, but if scroll bars are not activated
21713 or deactivated here, for unknown reasons, activated scroll bars
21714 are shown in deactivated frames in some instances. */
21715 if (f == FRAME_MAC_DISPLAY_INFO (f)->x_focus_frame)
21716 activate_scroll_bars (f);
21717 else
21718 deactivate_scroll_bars (f);
21719 #endif
21720
21721 /* If basic faces haven't been realized yet, there is no point in
21722 trying to redraw anything. This can happen when we get an expose
21723 event while Emacs is starting, e.g. by moving another window. */
21724 if (FRAME_FACE_CACHE (f) == NULL
21725 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
21726 {
21727 TRACE ((stderr, " no faces\n"));
21728 return;
21729 }
21730
21731 if (w == 0 || h == 0)
21732 {
21733 r.x = r.y = 0;
21734 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
21735 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
21736 }
21737 else
21738 {
21739 r.x = x;
21740 r.y = y;
21741 r.width = w;
21742 r.height = h;
21743 }
21744
21745 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
21746 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
21747
21748 if (WINDOWP (f->tool_bar_window))
21749 mouse_face_overwritten_p
21750 |= expose_window (XWINDOW (f->tool_bar_window), &r);
21751
21752 #ifdef HAVE_X_WINDOWS
21753 #ifndef MSDOS
21754 #ifndef USE_X_TOOLKIT
21755 if (WINDOWP (f->menu_bar_window))
21756 mouse_face_overwritten_p
21757 |= expose_window (XWINDOW (f->menu_bar_window), &r);
21758 #endif /* not USE_X_TOOLKIT */
21759 #endif
21760 #endif
21761
21762 /* Some window managers support a focus-follows-mouse style with
21763 delayed raising of frames. Imagine a partially obscured frame,
21764 and moving the mouse into partially obscured mouse-face on that
21765 frame. The visible part of the mouse-face will be highlighted,
21766 then the WM raises the obscured frame. With at least one WM, KDE
21767 2.1, Emacs is not getting any event for the raising of the frame
21768 (even tried with SubstructureRedirectMask), only Expose events.
21769 These expose events will draw text normally, i.e. not
21770 highlighted. Which means we must redo the highlight here.
21771 Subsume it under ``we love X''. --gerd 2001-08-15 */
21772 /* Included in Windows version because Windows most likely does not
21773 do the right thing if any third party tool offers
21774 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
21775 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
21776 {
21777 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
21778 if (f == dpyinfo->mouse_face_mouse_frame)
21779 {
21780 int x = dpyinfo->mouse_face_mouse_x;
21781 int y = dpyinfo->mouse_face_mouse_y;
21782 clear_mouse_face (dpyinfo);
21783 note_mouse_highlight (f, x, y);
21784 }
21785 }
21786 }
21787
21788
21789 /* EXPORT:
21790 Determine the intersection of two rectangles R1 and R2. Return
21791 the intersection in *RESULT. Value is non-zero if RESULT is not
21792 empty. */
21793
21794 int
21795 x_intersect_rectangles (r1, r2, result)
21796 XRectangle *r1, *r2, *result;
21797 {
21798 XRectangle *left, *right;
21799 XRectangle *upper, *lower;
21800 int intersection_p = 0;
21801
21802 /* Rearrange so that R1 is the left-most rectangle. */
21803 if (r1->x < r2->x)
21804 left = r1, right = r2;
21805 else
21806 left = r2, right = r1;
21807
21808 /* X0 of the intersection is right.x0, if this is inside R1,
21809 otherwise there is no intersection. */
21810 if (right->x <= left->x + left->width)
21811 {
21812 result->x = right->x;
21813
21814 /* The right end of the intersection is the minimum of the
21815 the right ends of left and right. */
21816 result->width = (min (left->x + left->width, right->x + right->width)
21817 - result->x);
21818
21819 /* Same game for Y. */
21820 if (r1->y < r2->y)
21821 upper = r1, lower = r2;
21822 else
21823 upper = r2, lower = r1;
21824
21825 /* The upper end of the intersection is lower.y0, if this is inside
21826 of upper. Otherwise, there is no intersection. */
21827 if (lower->y <= upper->y + upper->height)
21828 {
21829 result->y = lower->y;
21830
21831 /* The lower end of the intersection is the minimum of the lower
21832 ends of upper and lower. */
21833 result->height = (min (lower->y + lower->height,
21834 upper->y + upper->height)
21835 - result->y);
21836 intersection_p = 1;
21837 }
21838 }
21839
21840 return intersection_p;
21841 }
21842
21843 #endif /* HAVE_WINDOW_SYSTEM */
21844
21845 \f
21846 /***********************************************************************
21847 Initialization
21848 ***********************************************************************/
21849
21850 void
21851 syms_of_xdisp ()
21852 {
21853 Vwith_echo_area_save_vector = Qnil;
21854 staticpro (&Vwith_echo_area_save_vector);
21855
21856 Vmessage_stack = Qnil;
21857 staticpro (&Vmessage_stack);
21858
21859 Qinhibit_redisplay = intern ("inhibit-redisplay");
21860 staticpro (&Qinhibit_redisplay);
21861
21862 message_dolog_marker1 = Fmake_marker ();
21863 staticpro (&message_dolog_marker1);
21864 message_dolog_marker2 = Fmake_marker ();
21865 staticpro (&message_dolog_marker2);
21866 message_dolog_marker3 = Fmake_marker ();
21867 staticpro (&message_dolog_marker3);
21868
21869 #if GLYPH_DEBUG
21870 defsubr (&Sdump_frame_glyph_matrix);
21871 defsubr (&Sdump_glyph_matrix);
21872 defsubr (&Sdump_glyph_row);
21873 defsubr (&Sdump_tool_bar_row);
21874 defsubr (&Strace_redisplay);
21875 defsubr (&Strace_to_stderr);
21876 #endif
21877 #ifdef HAVE_WINDOW_SYSTEM
21878 defsubr (&Stool_bar_lines_needed);
21879 defsubr (&Slookup_image_map);
21880 #endif
21881 defsubr (&Sformat_mode_line);
21882
21883 staticpro (&Qmenu_bar_update_hook);
21884 Qmenu_bar_update_hook = intern ("menu-bar-update-hook");
21885
21886 staticpro (&Qoverriding_terminal_local_map);
21887 Qoverriding_terminal_local_map = intern ("overriding-terminal-local-map");
21888
21889 staticpro (&Qoverriding_local_map);
21890 Qoverriding_local_map = intern ("overriding-local-map");
21891
21892 staticpro (&Qwindow_scroll_functions);
21893 Qwindow_scroll_functions = intern ("window-scroll-functions");
21894
21895 staticpro (&Qredisplay_end_trigger_functions);
21896 Qredisplay_end_trigger_functions = intern ("redisplay-end-trigger-functions");
21897
21898 staticpro (&Qinhibit_point_motion_hooks);
21899 Qinhibit_point_motion_hooks = intern ("inhibit-point-motion-hooks");
21900
21901 QCdata = intern (":data");
21902 staticpro (&QCdata);
21903 Qdisplay = intern ("display");
21904 staticpro (&Qdisplay);
21905 Qspace_width = intern ("space-width");
21906 staticpro (&Qspace_width);
21907 Qraise = intern ("raise");
21908 staticpro (&Qraise);
21909 Qslice = intern ("slice");
21910 staticpro (&Qslice);
21911 Qspace = intern ("space");
21912 staticpro (&Qspace);
21913 Qmargin = intern ("margin");
21914 staticpro (&Qmargin);
21915 Qpointer = intern ("pointer");
21916 staticpro (&Qpointer);
21917 Qleft_margin = intern ("left-margin");
21918 staticpro (&Qleft_margin);
21919 Qright_margin = intern ("right-margin");
21920 staticpro (&Qright_margin);
21921 Qcenter = intern ("center");
21922 staticpro (&Qcenter);
21923 Qline_height = intern ("line-height");
21924 staticpro (&Qline_height);
21925 Qtotal = intern ("total");
21926 staticpro (&Qtotal);
21927 QCalign_to = intern (":align-to");
21928 staticpro (&QCalign_to);
21929 QCrelative_width = intern (":relative-width");
21930 staticpro (&QCrelative_width);
21931 QCrelative_height = intern (":relative-height");
21932 staticpro (&QCrelative_height);
21933 QCeval = intern (":eval");
21934 staticpro (&QCeval);
21935 QCpropertize = intern (":propertize");
21936 staticpro (&QCpropertize);
21937 QCfile = intern (":file");
21938 staticpro (&QCfile);
21939 Qfontified = intern ("fontified");
21940 staticpro (&Qfontified);
21941 Qfontification_functions = intern ("fontification-functions");
21942 staticpro (&Qfontification_functions);
21943 Qtrailing_whitespace = intern ("trailing-whitespace");
21944 staticpro (&Qtrailing_whitespace);
21945 Qimage = intern ("image");
21946 staticpro (&Qimage);
21947 QCmap = intern (":map");
21948 staticpro (&QCmap);
21949 QCpointer = intern (":pointer");
21950 staticpro (&QCpointer);
21951 Qrect = intern ("rect");
21952 staticpro (&Qrect);
21953 Qcircle = intern ("circle");
21954 staticpro (&Qcircle);
21955 Qpoly = intern ("poly");
21956 staticpro (&Qpoly);
21957 Qmessage_truncate_lines = intern ("message-truncate-lines");
21958 staticpro (&Qmessage_truncate_lines);
21959 Qcursor_in_non_selected_windows = intern ("cursor-in-non-selected-windows");
21960 staticpro (&Qcursor_in_non_selected_windows);
21961 Qgrow_only = intern ("grow-only");
21962 staticpro (&Qgrow_only);
21963 Qinhibit_menubar_update = intern ("inhibit-menubar-update");
21964 staticpro (&Qinhibit_menubar_update);
21965 Qinhibit_eval_during_redisplay = intern ("inhibit-eval-during-redisplay");
21966 staticpro (&Qinhibit_eval_during_redisplay);
21967 Qposition = intern ("position");
21968 staticpro (&Qposition);
21969 Qbuffer_position = intern ("buffer-position");
21970 staticpro (&Qbuffer_position);
21971 Qobject = intern ("object");
21972 staticpro (&Qobject);
21973 Qbar = intern ("bar");
21974 staticpro (&Qbar);
21975 Qhbar = intern ("hbar");
21976 staticpro (&Qhbar);
21977 Qbox = intern ("box");
21978 staticpro (&Qbox);
21979 Qhollow = intern ("hollow");
21980 staticpro (&Qhollow);
21981 Qhand = intern ("hand");
21982 staticpro (&Qhand);
21983 Qarrow = intern ("arrow");
21984 staticpro (&Qarrow);
21985 Qtext = intern ("text");
21986 staticpro (&Qtext);
21987 Qrisky_local_variable = intern ("risky-local-variable");
21988 staticpro (&Qrisky_local_variable);
21989 Qinhibit_free_realized_faces = intern ("inhibit-free-realized-faces");
21990 staticpro (&Qinhibit_free_realized_faces);
21991
21992 list_of_error = Fcons (Fcons (intern ("error"),
21993 Fcons (intern ("void-variable"), Qnil)),
21994 Qnil);
21995 staticpro (&list_of_error);
21996
21997 Qlast_arrow_position = intern ("last-arrow-position");
21998 staticpro (&Qlast_arrow_position);
21999 Qlast_arrow_string = intern ("last-arrow-string");
22000 staticpro (&Qlast_arrow_string);
22001
22002 Qoverlay_arrow_string = intern ("overlay-arrow-string");
22003 staticpro (&Qoverlay_arrow_string);
22004 Qoverlay_arrow_bitmap = intern ("overlay-arrow-bitmap");
22005 staticpro (&Qoverlay_arrow_bitmap);
22006
22007 echo_buffer[0] = echo_buffer[1] = Qnil;
22008 staticpro (&echo_buffer[0]);
22009 staticpro (&echo_buffer[1]);
22010
22011 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
22012 staticpro (&echo_area_buffer[0]);
22013 staticpro (&echo_area_buffer[1]);
22014
22015 Vmessages_buffer_name = build_string ("*Messages*");
22016 staticpro (&Vmessages_buffer_name);
22017
22018 mode_line_proptrans_alist = Qnil;
22019 staticpro (&mode_line_proptrans_alist);
22020
22021 mode_line_string_list = Qnil;
22022 staticpro (&mode_line_string_list);
22023
22024 help_echo_string = Qnil;
22025 staticpro (&help_echo_string);
22026 help_echo_object = Qnil;
22027 staticpro (&help_echo_object);
22028 help_echo_window = Qnil;
22029 staticpro (&help_echo_window);
22030 previous_help_echo_string = Qnil;
22031 staticpro (&previous_help_echo_string);
22032 help_echo_pos = -1;
22033
22034 #ifdef HAVE_WINDOW_SYSTEM
22035 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
22036 doc: /* *Non-nil means draw block cursor as wide as the glyph under it.
22037 For example, if a block cursor is over a tab, it will be drawn as
22038 wide as that tab on the display. */);
22039 x_stretch_cursor_p = 0;
22040 #endif
22041
22042 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
22043 doc: /* *Non-nil means highlight trailing whitespace.
22044 The face used for trailing whitespace is `trailing-whitespace'. */);
22045 Vshow_trailing_whitespace = Qnil;
22046
22047 DEFVAR_LISP ("void-text-area-pointer", &Vvoid_text_area_pointer,
22048 doc: /* *The pointer shape to show in void text areas.
22049 Nil means to show the text pointer. Other options are `arrow', `text',
22050 `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
22051 Vvoid_text_area_pointer = Qarrow;
22052
22053 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
22054 doc: /* Non-nil means don't actually do any redisplay.
22055 This is used for internal purposes. */);
22056 Vinhibit_redisplay = Qnil;
22057
22058 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
22059 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
22060 Vglobal_mode_string = Qnil;
22061
22062 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
22063 doc: /* Marker for where to display an arrow on top of the buffer text.
22064 This must be the beginning of a line in order to work.
22065 See also `overlay-arrow-string'. */);
22066 Voverlay_arrow_position = Qnil;
22067
22068 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
22069 doc: /* String to display as an arrow in non-window frames.
22070 See also `overlay-arrow-position'. */);
22071 Voverlay_arrow_string = Qnil;
22072
22073 DEFVAR_LISP ("overlay-arrow-variable-list", &Voverlay_arrow_variable_list,
22074 doc: /* List of variables (symbols) which hold markers for overlay arrows.
22075 The symbols on this list are examined during redisplay to determine
22076 where to display overlay arrows. */);
22077 Voverlay_arrow_variable_list
22078 = Fcons (intern ("overlay-arrow-position"), Qnil);
22079
22080 DEFVAR_INT ("scroll-step", &scroll_step,
22081 doc: /* *The number of lines to try scrolling a window by when point moves out.
22082 If that fails to bring point back on frame, point is centered instead.
22083 If this is zero, point is always centered after it moves off frame.
22084 If you want scrolling to always be a line at a time, you should set
22085 `scroll-conservatively' to a large value rather than set this to 1. */);
22086
22087 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
22088 doc: /* *Scroll up to this many lines, to bring point back on screen.
22089 A value of zero means to scroll the text to center point vertically
22090 in the window. */);
22091 scroll_conservatively = 0;
22092
22093 DEFVAR_INT ("scroll-margin", &scroll_margin,
22094 doc: /* *Number of lines of margin at the top and bottom of a window.
22095 Recenter the window whenever point gets within this many lines
22096 of the top or bottom of the window. */);
22097 scroll_margin = 0;
22098
22099 DEFVAR_LISP ("display-pixels-per-inch", &Vdisplay_pixels_per_inch,
22100 doc: /* Pixels per inch on current display.
22101 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
22102 Vdisplay_pixels_per_inch = make_float (72.0);
22103
22104 #if GLYPH_DEBUG
22105 DEFVAR_INT ("debug-end-pos", &debug_end_pos, doc: /* Don't ask. */);
22106 #endif
22107
22108 DEFVAR_BOOL ("truncate-partial-width-windows",
22109 &truncate_partial_width_windows,
22110 doc: /* *Non-nil means truncate lines in all windows less than full frame wide. */);
22111 truncate_partial_width_windows = 1;
22112
22113 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
22114 doc: /* nil means display the mode-line/header-line/menu-bar in the default face.
22115 Any other value means to use the appropriate face, `mode-line',
22116 `header-line', or `menu' respectively. */);
22117 mode_line_inverse_video = 1;
22118
22119 DEFVAR_LISP ("line-number-display-limit", &Vline_number_display_limit,
22120 doc: /* *Maximum buffer size for which line number should be displayed.
22121 If the buffer is bigger than this, the line number does not appear
22122 in the mode line. A value of nil means no limit. */);
22123 Vline_number_display_limit = Qnil;
22124
22125 DEFVAR_INT ("line-number-display-limit-width",
22126 &line_number_display_limit_width,
22127 doc: /* *Maximum line width (in characters) for line number display.
22128 If the average length of the lines near point is bigger than this, then the
22129 line number may be omitted from the mode line. */);
22130 line_number_display_limit_width = 200;
22131
22132 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
22133 doc: /* *Non-nil means highlight region even in nonselected windows. */);
22134 highlight_nonselected_windows = 0;
22135
22136 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
22137 doc: /* Non-nil if more than one frame is visible on this display.
22138 Minibuffer-only frames don't count, but iconified frames do.
22139 This variable is not guaranteed to be accurate except while processing
22140 `frame-title-format' and `icon-title-format'. */);
22141
22142 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
22143 doc: /* Template for displaying the title bar of visible frames.
22144 \(Assuming the window manager supports this feature.)
22145 This variable has the same structure as `mode-line-format' (which see),
22146 and is used only on frames for which no explicit name has been set
22147 \(see `modify-frame-parameters'). */);
22148
22149 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
22150 doc: /* Template for displaying the title bar of an iconified frame.
22151 \(Assuming the window manager supports this feature.)
22152 This variable has the same structure as `mode-line-format' (which see),
22153 and is used only on frames for which no explicit name has been set
22154 \(see `modify-frame-parameters'). */);
22155 Vicon_title_format
22156 = Vframe_title_format
22157 = Fcons (intern ("multiple-frames"),
22158 Fcons (build_string ("%b"),
22159 Fcons (Fcons (empty_string,
22160 Fcons (intern ("invocation-name"),
22161 Fcons (build_string ("@"),
22162 Fcons (intern ("system-name"),
22163 Qnil)))),
22164 Qnil)));
22165
22166 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
22167 doc: /* Maximum number of lines to keep in the message log buffer.
22168 If nil, disable message logging. If t, log messages but don't truncate
22169 the buffer when it becomes large. */);
22170 Vmessage_log_max = make_number (50);
22171
22172 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
22173 doc: /* Functions called before redisplay, if window sizes have changed.
22174 The value should be a list of functions that take one argument.
22175 Just before redisplay, for each frame, if any of its windows have changed
22176 size since the last redisplay, or have been split or deleted,
22177 all the functions in the list are called, with the frame as argument. */);
22178 Vwindow_size_change_functions = Qnil;
22179
22180 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
22181 doc: /* List of Functions to call before redisplaying a window with scrolling.
22182 Each function is called with two arguments, the window
22183 and its new display-start position. Note that the value of `window-end'
22184 is not valid when these functions are called. */);
22185 Vwindow_scroll_functions = Qnil;
22186
22187 DEFVAR_BOOL ("mouse-autoselect-window", &mouse_autoselect_window,
22188 doc: /* *Non-nil means autoselect window with mouse pointer. */);
22189 mouse_autoselect_window = 0;
22190
22191 DEFVAR_BOOL ("auto-resize-tool-bars", &auto_resize_tool_bars_p,
22192 doc: /* *Non-nil means automatically resize tool-bars.
22193 This increases a tool-bar's height if not all tool-bar items are visible.
22194 It decreases a tool-bar's height when it would display blank lines
22195 otherwise. */);
22196 auto_resize_tool_bars_p = 1;
22197
22198 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
22199 doc: /* *Non-nil means raise tool-bar buttons when the mouse moves over them. */);
22200 auto_raise_tool_bar_buttons_p = 1;
22201
22202 DEFVAR_LISP ("tool-bar-button-margin", &Vtool_bar_button_margin,
22203 doc: /* *Margin around tool-bar buttons in pixels.
22204 If an integer, use that for both horizontal and vertical margins.
22205 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
22206 HORZ specifying the horizontal margin, and VERT specifying the
22207 vertical margin. */);
22208 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
22209
22210 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
22211 doc: /* *Relief thickness of tool-bar buttons. */);
22212 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
22213
22214 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
22215 doc: /* List of functions to call to fontify regions of text.
22216 Each function is called with one argument POS. Functions must
22217 fontify a region starting at POS in the current buffer, and give
22218 fontified regions the property `fontified'. */);
22219 Vfontification_functions = Qnil;
22220 Fmake_variable_buffer_local (Qfontification_functions);
22221
22222 DEFVAR_BOOL ("unibyte-display-via-language-environment",
22223 &unibyte_display_via_language_environment,
22224 doc: /* *Non-nil means display unibyte text according to language environment.
22225 Specifically this means that unibyte non-ASCII characters
22226 are displayed by converting them to the equivalent multibyte characters
22227 according to the current language environment. As a result, they are
22228 displayed according to the current fontset. */);
22229 unibyte_display_via_language_environment = 0;
22230
22231 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
22232 doc: /* *Maximum height for resizing mini-windows.
22233 If a float, it specifies a fraction of the mini-window frame's height.
22234 If an integer, it specifies a number of lines. */);
22235 Vmax_mini_window_height = make_float (0.25);
22236
22237 DEFVAR_LISP ("resize-mini-windows", &Vresize_mini_windows,
22238 doc: /* *How to resize mini-windows.
22239 A value of nil means don't automatically resize mini-windows.
22240 A value of t means resize them to fit the text displayed in them.
22241 A value of `grow-only', the default, means let mini-windows grow
22242 only, until their display becomes empty, at which point the windows
22243 go back to their normal size. */);
22244 Vresize_mini_windows = Qgrow_only;
22245
22246 DEFVAR_LISP ("cursor-in-non-selected-windows",
22247 &Vcursor_in_non_selected_windows,
22248 doc: /* *Cursor type to display in non-selected windows.
22249 t means to use hollow box cursor. See `cursor-type' for other values. */);
22250 Vcursor_in_non_selected_windows = Qt;
22251
22252 DEFVAR_LISP ("blink-cursor-alist", &Vblink_cursor_alist,
22253 doc: /* Alist specifying how to blink the cursor off.
22254 Each element has the form (ON-STATE . OFF-STATE). Whenever the
22255 `cursor-type' frame-parameter or variable equals ON-STATE,
22256 comparing using `equal', Emacs uses OFF-STATE to specify
22257 how to blink it off. */);
22258 Vblink_cursor_alist = Qnil;
22259
22260 DEFVAR_BOOL ("auto-hscroll-mode", &automatic_hscrolling_p,
22261 doc: /* *Non-nil means scroll the display automatically to make point visible. */);
22262 automatic_hscrolling_p = 1;
22263
22264 DEFVAR_INT ("hscroll-margin", &hscroll_margin,
22265 doc: /* *How many columns away from the window edge point is allowed to get
22266 before automatic hscrolling will horizontally scroll the window. */);
22267 hscroll_margin = 5;
22268
22269 DEFVAR_LISP ("hscroll-step", &Vhscroll_step,
22270 doc: /* *How many columns to scroll the window when point gets too close to the edge.
22271 When point is less than `automatic-hscroll-margin' columns from the window
22272 edge, automatic hscrolling will scroll the window by the amount of columns
22273 determined by this variable. If its value is a positive integer, scroll that
22274 many columns. If it's a positive floating-point number, it specifies the
22275 fraction of the window's width to scroll. If it's nil or zero, point will be
22276 centered horizontally after the scroll. Any other value, including negative
22277 numbers, are treated as if the value were zero.
22278
22279 Automatic hscrolling always moves point outside the scroll margin, so if
22280 point was more than scroll step columns inside the margin, the window will
22281 scroll more than the value given by the scroll step.
22282
22283 Note that the lower bound for automatic hscrolling specified by `scroll-left'
22284 and `scroll-right' overrides this variable's effect. */);
22285 Vhscroll_step = make_number (0);
22286
22287 DEFVAR_BOOL ("message-truncate-lines", &message_truncate_lines,
22288 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
22289 Bind this around calls to `message' to let it take effect. */);
22290 message_truncate_lines = 0;
22291
22292 DEFVAR_LISP ("menu-bar-update-hook", &Vmenu_bar_update_hook,
22293 doc: /* Normal hook run for clicks on menu bar, before displaying a submenu.
22294 Can be used to update submenus whose contents should vary. */);
22295 Vmenu_bar_update_hook = Qnil;
22296
22297 DEFVAR_BOOL ("inhibit-menubar-update", &inhibit_menubar_update,
22298 doc: /* Non-nil means don't update menu bars. Internal use only. */);
22299 inhibit_menubar_update = 0;
22300
22301 DEFVAR_BOOL ("inhibit-eval-during-redisplay", &inhibit_eval_during_redisplay,
22302 doc: /* Non-nil means don't eval Lisp during redisplay. */);
22303 inhibit_eval_during_redisplay = 0;
22304
22305 DEFVAR_BOOL ("inhibit-free-realized-faces", &inhibit_free_realized_faces,
22306 doc: /* Non-nil means don't free realized faces. Internal use only. */);
22307 inhibit_free_realized_faces = 0;
22308
22309 #if GLYPH_DEBUG
22310 DEFVAR_BOOL ("inhibit-try-window-id", &inhibit_try_window_id,
22311 doc: /* Inhibit try_window_id display optimization. */);
22312 inhibit_try_window_id = 0;
22313
22314 DEFVAR_BOOL ("inhibit-try-window-reusing", &inhibit_try_window_reusing,
22315 doc: /* Inhibit try_window_reusing display optimization. */);
22316 inhibit_try_window_reusing = 0;
22317
22318 DEFVAR_BOOL ("inhibit-try-cursor-movement", &inhibit_try_cursor_movement,
22319 doc: /* Inhibit try_cursor_movement display optimization. */);
22320 inhibit_try_cursor_movement = 0;
22321 #endif /* GLYPH_DEBUG */
22322 }
22323
22324
22325 /* Initialize this module when Emacs starts. */
22326
22327 void
22328 init_xdisp ()
22329 {
22330 Lisp_Object root_window;
22331 struct window *mini_w;
22332
22333 current_header_line_height = current_mode_line_height = -1;
22334
22335 CHARPOS (this_line_start_pos) = 0;
22336
22337 mini_w = XWINDOW (minibuf_window);
22338 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
22339
22340 if (!noninteractive)
22341 {
22342 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
22343 int i;
22344
22345 XWINDOW (root_window)->top_line = make_number (FRAME_TOP_MARGIN (f));
22346 set_window_height (root_window,
22347 FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f),
22348 0);
22349 mini_w->top_line = make_number (FRAME_LINES (f) - 1);
22350 set_window_height (minibuf_window, 1, 0);
22351
22352 XWINDOW (root_window)->total_cols = make_number (FRAME_COLS (f));
22353 mini_w->total_cols = make_number (FRAME_COLS (f));
22354
22355 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
22356 scratch_glyph_row.glyphs[TEXT_AREA + 1]
22357 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
22358
22359 /* The default ellipsis glyphs `...'. */
22360 for (i = 0; i < 3; ++i)
22361 default_invis_vector[i] = make_number ('.');
22362 }
22363
22364 {
22365 /* Allocate the buffer for frame titles.
22366 Also used for `format-mode-line'. */
22367 int size = 100;
22368 frame_title_buf = (char *) xmalloc (size);
22369 frame_title_buf_end = frame_title_buf + size;
22370 frame_title_ptr = NULL;
22371 }
22372
22373 help_echo_showing_p = 0;
22374 }
22375
22376
22377 /* arch-tag: eacc864d-bb6a-4b74-894a-1a4399a1358b
22378 (do not change this comment) */