]> 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
6315 /* Do nothing if called asynchronously. Inserting text into
6316 a buffer may call after-change-functions and alike and
6317 that would means running Lisp asynchronously. */
6318 if (handling_signal)
6319 return;
6320
6321 fmt = msg = Qnil;
6322 GCPRO4 (fmt, msg, arg1, arg2);
6323
6324 args[0] = fmt = build_string (format);
6325 args[1] = arg1;
6326 args[2] = arg2;
6327 msg = Fformat (3, args);
6328
6329 len = SBYTES (msg) + 1;
6330 buffer = (char *) alloca (len);
6331 bcopy (SDATA (msg), buffer, len);
6332
6333 message_dolog (buffer, len - 1, 1, 0);
6334 UNGCPRO;
6335 }
6336
6337
6338 /* Output a newline in the *Messages* buffer if "needs" one. */
6339
6340 void
6341 message_log_maybe_newline ()
6342 {
6343 if (message_log_need_newline)
6344 message_dolog ("", 0, 1, 0);
6345 }
6346
6347
6348 /* Add a string M of length NBYTES to the message log, optionally
6349 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
6350 nonzero, means interpret the contents of M as multibyte. This
6351 function calls low-level routines in order to bypass text property
6352 hooks, etc. which might not be safe to run. */
6353
6354 void
6355 message_dolog (m, nbytes, nlflag, multibyte)
6356 const char *m;
6357 int nbytes, nlflag, multibyte;
6358 {
6359 if (!NILP (Vmemory_full))
6360 return;
6361
6362 if (!NILP (Vmessage_log_max))
6363 {
6364 struct buffer *oldbuf;
6365 Lisp_Object oldpoint, oldbegv, oldzv;
6366 int old_windows_or_buffers_changed = windows_or_buffers_changed;
6367 int point_at_end = 0;
6368 int zv_at_end = 0;
6369 Lisp_Object old_deactivate_mark, tem;
6370 struct gcpro gcpro1;
6371
6372 old_deactivate_mark = Vdeactivate_mark;
6373 oldbuf = current_buffer;
6374 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
6375 current_buffer->undo_list = Qt;
6376
6377 oldpoint = message_dolog_marker1;
6378 set_marker_restricted (oldpoint, make_number (PT), Qnil);
6379 oldbegv = message_dolog_marker2;
6380 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
6381 oldzv = message_dolog_marker3;
6382 set_marker_restricted (oldzv, make_number (ZV), Qnil);
6383 GCPRO1 (old_deactivate_mark);
6384
6385 if (PT == Z)
6386 point_at_end = 1;
6387 if (ZV == Z)
6388 zv_at_end = 1;
6389
6390 BEGV = BEG;
6391 BEGV_BYTE = BEG_BYTE;
6392 ZV = Z;
6393 ZV_BYTE = Z_BYTE;
6394 TEMP_SET_PT_BOTH (Z, Z_BYTE);
6395
6396 /* Insert the string--maybe converting multibyte to single byte
6397 or vice versa, so that all the text fits the buffer. */
6398 if (multibyte
6399 && NILP (current_buffer->enable_multibyte_characters))
6400 {
6401 int i, c, char_bytes;
6402 unsigned char work[1];
6403
6404 /* Convert a multibyte string to single-byte
6405 for the *Message* buffer. */
6406 for (i = 0; i < nbytes; i += char_bytes)
6407 {
6408 c = string_char_and_length (m + i, nbytes - i, &char_bytes);
6409 work[0] = (SINGLE_BYTE_CHAR_P (c)
6410 ? c
6411 : multibyte_char_to_unibyte (c, Qnil));
6412 insert_1_both (work, 1, 1, 1, 0, 0);
6413 }
6414 }
6415 else if (! multibyte
6416 && ! NILP (current_buffer->enable_multibyte_characters))
6417 {
6418 int i, c, char_bytes;
6419 unsigned char *msg = (unsigned char *) m;
6420 unsigned char str[MAX_MULTIBYTE_LENGTH];
6421 /* Convert a single-byte string to multibyte
6422 for the *Message* buffer. */
6423 for (i = 0; i < nbytes; i++)
6424 {
6425 c = unibyte_char_to_multibyte (msg[i]);
6426 char_bytes = CHAR_STRING (c, str);
6427 insert_1_both (str, 1, char_bytes, 1, 0, 0);
6428 }
6429 }
6430 else if (nbytes)
6431 insert_1 (m, nbytes, 1, 0, 0);
6432
6433 if (nlflag)
6434 {
6435 int this_bol, this_bol_byte, prev_bol, prev_bol_byte, dup;
6436 insert_1 ("\n", 1, 1, 0, 0);
6437
6438 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
6439 this_bol = PT;
6440 this_bol_byte = PT_BYTE;
6441
6442 /* See if this line duplicates the previous one.
6443 If so, combine duplicates. */
6444 if (this_bol > BEG)
6445 {
6446 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
6447 prev_bol = PT;
6448 prev_bol_byte = PT_BYTE;
6449
6450 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
6451 this_bol, this_bol_byte);
6452 if (dup)
6453 {
6454 del_range_both (prev_bol, prev_bol_byte,
6455 this_bol, this_bol_byte, 0);
6456 if (dup > 1)
6457 {
6458 char dupstr[40];
6459 int duplen;
6460
6461 /* If you change this format, don't forget to also
6462 change message_log_check_duplicate. */
6463 sprintf (dupstr, " [%d times]", dup);
6464 duplen = strlen (dupstr);
6465 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
6466 insert_1 (dupstr, duplen, 1, 0, 1);
6467 }
6468 }
6469 }
6470
6471 /* If we have more than the desired maximum number of lines
6472 in the *Messages* buffer now, delete the oldest ones.
6473 This is safe because we don't have undo in this buffer. */
6474
6475 if (NATNUMP (Vmessage_log_max))
6476 {
6477 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
6478 -XFASTINT (Vmessage_log_max) - 1, 0);
6479 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
6480 }
6481 }
6482 BEGV = XMARKER (oldbegv)->charpos;
6483 BEGV_BYTE = marker_byte_position (oldbegv);
6484
6485 if (zv_at_end)
6486 {
6487 ZV = Z;
6488 ZV_BYTE = Z_BYTE;
6489 }
6490 else
6491 {
6492 ZV = XMARKER (oldzv)->charpos;
6493 ZV_BYTE = marker_byte_position (oldzv);
6494 }
6495
6496 if (point_at_end)
6497 TEMP_SET_PT_BOTH (Z, Z_BYTE);
6498 else
6499 /* We can't do Fgoto_char (oldpoint) because it will run some
6500 Lisp code. */
6501 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
6502 XMARKER (oldpoint)->bytepos);
6503
6504 UNGCPRO;
6505 unchain_marker (XMARKER (oldpoint));
6506 unchain_marker (XMARKER (oldbegv));
6507 unchain_marker (XMARKER (oldzv));
6508
6509 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
6510 set_buffer_internal (oldbuf);
6511 if (NILP (tem))
6512 windows_or_buffers_changed = old_windows_or_buffers_changed;
6513 message_log_need_newline = !nlflag;
6514 Vdeactivate_mark = old_deactivate_mark;
6515 }
6516 }
6517
6518
6519 /* We are at the end of the buffer after just having inserted a newline.
6520 (Note: We depend on the fact we won't be crossing the gap.)
6521 Check to see if the most recent message looks a lot like the previous one.
6522 Return 0 if different, 1 if the new one should just replace it, or a
6523 value N > 1 if we should also append " [N times]". */
6524
6525 static int
6526 message_log_check_duplicate (prev_bol, prev_bol_byte, this_bol, this_bol_byte)
6527 int prev_bol, this_bol;
6528 int prev_bol_byte, this_bol_byte;
6529 {
6530 int i;
6531 int len = Z_BYTE - 1 - this_bol_byte;
6532 int seen_dots = 0;
6533 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
6534 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
6535
6536 for (i = 0; i < len; i++)
6537 {
6538 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
6539 seen_dots = 1;
6540 if (p1[i] != p2[i])
6541 return seen_dots;
6542 }
6543 p1 += len;
6544 if (*p1 == '\n')
6545 return 2;
6546 if (*p1++ == ' ' && *p1++ == '[')
6547 {
6548 int n = 0;
6549 while (*p1 >= '0' && *p1 <= '9')
6550 n = n * 10 + *p1++ - '0';
6551 if (strncmp (p1, " times]\n", 8) == 0)
6552 return n+1;
6553 }
6554 return 0;
6555 }
6556
6557
6558 /* Display an echo area message M with a specified length of NBYTES
6559 bytes. The string may include null characters. If M is 0, clear
6560 out any existing message, and let the mini-buffer text show
6561 through.
6562
6563 The buffer M must continue to exist until after the echo area gets
6564 cleared or some other message gets displayed there. This means do
6565 not pass text that is stored in a Lisp string; do not pass text in
6566 a buffer that was alloca'd. */
6567
6568 void
6569 message2 (m, nbytes, multibyte)
6570 const char *m;
6571 int nbytes;
6572 int multibyte;
6573 {
6574 /* First flush out any partial line written with print. */
6575 message_log_maybe_newline ();
6576 if (m)
6577 message_dolog (m, nbytes, 1, multibyte);
6578 message2_nolog (m, nbytes, multibyte);
6579 }
6580
6581
6582 /* The non-logging counterpart of message2. */
6583
6584 void
6585 message2_nolog (m, nbytes, multibyte)
6586 const char *m;
6587 int nbytes, multibyte;
6588 {
6589 struct frame *sf = SELECTED_FRAME ();
6590 message_enable_multibyte = multibyte;
6591
6592 if (noninteractive)
6593 {
6594 if (noninteractive_need_newline)
6595 putc ('\n', stderr);
6596 noninteractive_need_newline = 0;
6597 if (m)
6598 fwrite (m, nbytes, 1, stderr);
6599 if (cursor_in_echo_area == 0)
6600 fprintf (stderr, "\n");
6601 fflush (stderr);
6602 }
6603 /* A null message buffer means that the frame hasn't really been
6604 initialized yet. Error messages get reported properly by
6605 cmd_error, so this must be just an informative message; toss it. */
6606 else if (INTERACTIVE
6607 && sf->glyphs_initialized_p
6608 && FRAME_MESSAGE_BUF (sf))
6609 {
6610 Lisp_Object mini_window;
6611 struct frame *f;
6612
6613 /* Get the frame containing the mini-buffer
6614 that the selected frame is using. */
6615 mini_window = FRAME_MINIBUF_WINDOW (sf);
6616 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
6617
6618 FRAME_SAMPLE_VISIBILITY (f);
6619 if (FRAME_VISIBLE_P (sf)
6620 && ! FRAME_VISIBLE_P (f))
6621 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
6622
6623 if (m)
6624 {
6625 set_message (m, Qnil, nbytes, multibyte);
6626 if (minibuffer_auto_raise)
6627 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
6628 }
6629 else
6630 clear_message (1, 1);
6631
6632 do_pending_window_change (0);
6633 echo_area_display (1);
6634 do_pending_window_change (0);
6635 if (FRAME_DISPLAY (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
6636 (*FRAME_DISPLAY (f)->frame_up_to_date_hook) (f);
6637 }
6638 }
6639
6640
6641 /* Display an echo area message M with a specified length of NBYTES
6642 bytes. The string may include null characters. If M is not a
6643 string, clear out any existing message, and let the mini-buffer
6644 text show through. */
6645
6646 void
6647 message3 (m, nbytes, multibyte)
6648 Lisp_Object m;
6649 int nbytes;
6650 int multibyte;
6651 {
6652 struct gcpro gcpro1;
6653
6654 GCPRO1 (m);
6655
6656 /* First flush out any partial line written with print. */
6657 message_log_maybe_newline ();
6658 if (STRINGP (m))
6659 message_dolog (SDATA (m), nbytes, 1, multibyte);
6660 message3_nolog (m, nbytes, multibyte);
6661
6662 UNGCPRO;
6663 }
6664
6665
6666 /* The non-logging version of message3. */
6667
6668 void
6669 message3_nolog (m, nbytes, multibyte)
6670 Lisp_Object m;
6671 int nbytes, multibyte;
6672 {
6673 struct frame *sf = SELECTED_FRAME ();
6674 message_enable_multibyte = multibyte;
6675
6676 if (noninteractive)
6677 {
6678 if (noninteractive_need_newline)
6679 putc ('\n', stderr);
6680 noninteractive_need_newline = 0;
6681 if (STRINGP (m))
6682 fwrite (SDATA (m), nbytes, 1, stderr);
6683 if (cursor_in_echo_area == 0)
6684 fprintf (stderr, "\n");
6685 fflush (stderr);
6686 }
6687 /* A null message buffer means that the frame hasn't really been
6688 initialized yet. Error messages get reported properly by
6689 cmd_error, so this must be just an informative message; toss it. */
6690 else if (INTERACTIVE
6691 && sf->glyphs_initialized_p
6692 && FRAME_MESSAGE_BUF (sf))
6693 {
6694 Lisp_Object mini_window;
6695 Lisp_Object frame;
6696 struct frame *f;
6697
6698 /* Get the frame containing the mini-buffer
6699 that the selected frame is using. */
6700 mini_window = FRAME_MINIBUF_WINDOW (sf);
6701 frame = XWINDOW (mini_window)->frame;
6702 f = XFRAME (frame);
6703
6704 FRAME_SAMPLE_VISIBILITY (f);
6705 if (FRAME_VISIBLE_P (sf)
6706 && !FRAME_VISIBLE_P (f))
6707 Fmake_frame_visible (frame);
6708
6709 if (STRINGP (m) && SCHARS (m) > 0)
6710 {
6711 set_message (NULL, m, nbytes, multibyte);
6712 if (minibuffer_auto_raise)
6713 Fraise_frame (frame);
6714 }
6715 else
6716 clear_message (1, 1);
6717
6718 do_pending_window_change (0);
6719 echo_area_display (1);
6720 do_pending_window_change (0);
6721 if (FRAME_DISPLAY (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
6722 (*FRAME_DISPLAY (f)->frame_up_to_date_hook) (f);
6723 }
6724 }
6725
6726
6727 /* Display a null-terminated echo area message M. If M is 0, clear
6728 out any existing message, and let the mini-buffer text show through.
6729
6730 The buffer M must continue to exist until after the echo area gets
6731 cleared or some other message gets displayed there. Do not pass
6732 text that is stored in a Lisp string. Do not pass text in a buffer
6733 that was alloca'd. */
6734
6735 void
6736 message1 (m)
6737 char *m;
6738 {
6739 message2 (m, (m ? strlen (m) : 0), 0);
6740 }
6741
6742
6743 /* The non-logging counterpart of message1. */
6744
6745 void
6746 message1_nolog (m)
6747 char *m;
6748 {
6749 message2_nolog (m, (m ? strlen (m) : 0), 0);
6750 }
6751
6752 /* Display a message M which contains a single %s
6753 which gets replaced with STRING. */
6754
6755 void
6756 message_with_string (m, string, log)
6757 char *m;
6758 Lisp_Object string;
6759 int log;
6760 {
6761 CHECK_STRING (string);
6762
6763 if (noninteractive)
6764 {
6765 if (m)
6766 {
6767 if (noninteractive_need_newline)
6768 putc ('\n', stderr);
6769 noninteractive_need_newline = 0;
6770 fprintf (stderr, m, SDATA (string));
6771 if (cursor_in_echo_area == 0)
6772 fprintf (stderr, "\n");
6773 fflush (stderr);
6774 }
6775 }
6776 else if (INTERACTIVE)
6777 {
6778 /* The frame whose minibuffer we're going to display the message on.
6779 It may be larger than the selected frame, so we need
6780 to use its buffer, not the selected frame's buffer. */
6781 Lisp_Object mini_window;
6782 struct frame *f, *sf = SELECTED_FRAME ();
6783
6784 /* Get the frame containing the minibuffer
6785 that the selected frame is using. */
6786 mini_window = FRAME_MINIBUF_WINDOW (sf);
6787 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
6788
6789 /* A null message buffer means that the frame hasn't really been
6790 initialized yet. Error messages get reported properly by
6791 cmd_error, so this must be just an informative message; toss it. */
6792 if (FRAME_MESSAGE_BUF (f))
6793 {
6794 Lisp_Object args[2], message;
6795 struct gcpro gcpro1, gcpro2;
6796
6797 args[0] = build_string (m);
6798 args[1] = message = string;
6799 GCPRO2 (args[0], message);
6800 gcpro1.nvars = 2;
6801
6802 message = Fformat (2, args);
6803
6804 if (log)
6805 message3 (message, SBYTES (message), STRING_MULTIBYTE (message));
6806 else
6807 message3_nolog (message, SBYTES (message), STRING_MULTIBYTE (message));
6808
6809 UNGCPRO;
6810
6811 /* Print should start at the beginning of the message
6812 buffer next time. */
6813 message_buf_print = 0;
6814 }
6815 }
6816 }
6817
6818
6819 /* Dump an informative message to the minibuf. If M is 0, clear out
6820 any existing message, and let the mini-buffer text show through. */
6821
6822 /* VARARGS 1 */
6823 void
6824 message (m, a1, a2, a3)
6825 char *m;
6826 EMACS_INT a1, a2, a3;
6827 {
6828 if (noninteractive)
6829 {
6830 if (m)
6831 {
6832 if (noninteractive_need_newline)
6833 putc ('\n', stderr);
6834 noninteractive_need_newline = 0;
6835 fprintf (stderr, m, a1, a2, a3);
6836 if (cursor_in_echo_area == 0)
6837 fprintf (stderr, "\n");
6838 fflush (stderr);
6839 }
6840 }
6841 else if (INTERACTIVE)
6842 {
6843 /* The frame whose mini-buffer we're going to display the message
6844 on. It may be larger than the selected frame, so we need to
6845 use its buffer, not the selected frame's buffer. */
6846 Lisp_Object mini_window;
6847 struct frame *f, *sf = SELECTED_FRAME ();
6848
6849 /* Get the frame containing the mini-buffer
6850 that the selected frame is using. */
6851 mini_window = FRAME_MINIBUF_WINDOW (sf);
6852 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
6853
6854 /* A null message buffer means that the frame hasn't really been
6855 initialized yet. Error messages get reported properly by
6856 cmd_error, so this must be just an informative message; toss
6857 it. */
6858 if (FRAME_MESSAGE_BUF (f))
6859 {
6860 if (m)
6861 {
6862 int len;
6863 #ifdef NO_ARG_ARRAY
6864 char *a[3];
6865 a[0] = (char *) a1;
6866 a[1] = (char *) a2;
6867 a[2] = (char *) a3;
6868
6869 len = doprnt (FRAME_MESSAGE_BUF (f),
6870 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
6871 #else
6872 len = doprnt (FRAME_MESSAGE_BUF (f),
6873 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3,
6874 (char **) &a1);
6875 #endif /* NO_ARG_ARRAY */
6876
6877 message2 (FRAME_MESSAGE_BUF (f), len, 0);
6878 }
6879 else
6880 message1 (0);
6881
6882 /* Print should start at the beginning of the message
6883 buffer next time. */
6884 message_buf_print = 0;
6885 }
6886 }
6887 }
6888
6889
6890 /* The non-logging version of message. */
6891
6892 void
6893 message_nolog (m, a1, a2, a3)
6894 char *m;
6895 EMACS_INT a1, a2, a3;
6896 {
6897 Lisp_Object old_log_max;
6898 old_log_max = Vmessage_log_max;
6899 Vmessage_log_max = Qnil;
6900 message (m, a1, a2, a3);
6901 Vmessage_log_max = old_log_max;
6902 }
6903
6904
6905 /* Display the current message in the current mini-buffer. This is
6906 only called from error handlers in process.c, and is not time
6907 critical. */
6908
6909 void
6910 update_echo_area ()
6911 {
6912 if (!NILP (echo_area_buffer[0]))
6913 {
6914 Lisp_Object string;
6915 string = Fcurrent_message ();
6916 message3 (string, SBYTES (string),
6917 !NILP (current_buffer->enable_multibyte_characters));
6918 }
6919 }
6920
6921
6922 /* Make sure echo area buffers in `echo_buffers' are live.
6923 If they aren't, make new ones. */
6924
6925 static void
6926 ensure_echo_area_buffers ()
6927 {
6928 int i;
6929
6930 for (i = 0; i < 2; ++i)
6931 if (!BUFFERP (echo_buffer[i])
6932 || NILP (XBUFFER (echo_buffer[i])->name))
6933 {
6934 char name[30];
6935 Lisp_Object old_buffer;
6936 int j;
6937
6938 old_buffer = echo_buffer[i];
6939 sprintf (name, " *Echo Area %d*", i);
6940 echo_buffer[i] = Fget_buffer_create (build_string (name));
6941 XBUFFER (echo_buffer[i])->truncate_lines = Qnil;
6942
6943 for (j = 0; j < 2; ++j)
6944 if (EQ (old_buffer, echo_area_buffer[j]))
6945 echo_area_buffer[j] = echo_buffer[i];
6946 }
6947 }
6948
6949
6950 /* Call FN with args A1..A4 with either the current or last displayed
6951 echo_area_buffer as current buffer.
6952
6953 WHICH zero means use the current message buffer
6954 echo_area_buffer[0]. If that is nil, choose a suitable buffer
6955 from echo_buffer[] and clear it.
6956
6957 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
6958 suitable buffer from echo_buffer[] and clear it.
6959
6960 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
6961 that the current message becomes the last displayed one, make
6962 choose a suitable buffer for echo_area_buffer[0], and clear it.
6963
6964 Value is what FN returns. */
6965
6966 static int
6967 with_echo_area_buffer (w, which, fn, a1, a2, a3, a4)
6968 struct window *w;
6969 int which;
6970 int (*fn) P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
6971 EMACS_INT a1;
6972 Lisp_Object a2;
6973 EMACS_INT a3, a4;
6974 {
6975 Lisp_Object buffer;
6976 int this_one, the_other, clear_buffer_p, rc;
6977 int count = SPECPDL_INDEX ();
6978
6979 /* If buffers aren't live, make new ones. */
6980 ensure_echo_area_buffers ();
6981
6982 clear_buffer_p = 0;
6983
6984 if (which == 0)
6985 this_one = 0, the_other = 1;
6986 else if (which > 0)
6987 this_one = 1, the_other = 0;
6988 else
6989 {
6990 this_one = 0, the_other = 1;
6991 clear_buffer_p = 1;
6992
6993 /* We need a fresh one in case the current echo buffer equals
6994 the one containing the last displayed echo area message. */
6995 if (!NILP (echo_area_buffer[this_one])
6996 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
6997 echo_area_buffer[this_one] = Qnil;
6998 }
6999
7000 /* Choose a suitable buffer from echo_buffer[] is we don't
7001 have one. */
7002 if (NILP (echo_area_buffer[this_one]))
7003 {
7004 echo_area_buffer[this_one]
7005 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
7006 ? echo_buffer[the_other]
7007 : echo_buffer[this_one]);
7008 clear_buffer_p = 1;
7009 }
7010
7011 buffer = echo_area_buffer[this_one];
7012
7013 /* Don't get confused by reusing the buffer used for echoing
7014 for a different purpose. */
7015 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
7016 cancel_echoing ();
7017
7018 record_unwind_protect (unwind_with_echo_area_buffer,
7019 with_echo_area_buffer_unwind_data (w));
7020
7021 /* Make the echo area buffer current. Note that for display
7022 purposes, it is not necessary that the displayed window's buffer
7023 == current_buffer, except for text property lookup. So, let's
7024 only set that buffer temporarily here without doing a full
7025 Fset_window_buffer. We must also change w->pointm, though,
7026 because otherwise an assertions in unshow_buffer fails, and Emacs
7027 aborts. */
7028 set_buffer_internal_1 (XBUFFER (buffer));
7029 if (w)
7030 {
7031 w->buffer = buffer;
7032 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
7033 }
7034
7035 current_buffer->undo_list = Qt;
7036 current_buffer->read_only = Qnil;
7037 specbind (Qinhibit_read_only, Qt);
7038 specbind (Qinhibit_modification_hooks, Qt);
7039
7040 if (clear_buffer_p && Z > BEG)
7041 del_range (BEG, Z);
7042
7043 xassert (BEGV >= BEG);
7044 xassert (ZV <= Z && ZV >= BEGV);
7045
7046 rc = fn (a1, a2, a3, a4);
7047
7048 xassert (BEGV >= BEG);
7049 xassert (ZV <= Z && ZV >= BEGV);
7050
7051 unbind_to (count, Qnil);
7052 return rc;
7053 }
7054
7055
7056 /* Save state that should be preserved around the call to the function
7057 FN called in with_echo_area_buffer. */
7058
7059 static Lisp_Object
7060 with_echo_area_buffer_unwind_data (w)
7061 struct window *w;
7062 {
7063 int i = 0;
7064 Lisp_Object vector;
7065
7066 /* Reduce consing by keeping one vector in
7067 Vwith_echo_area_save_vector. */
7068 vector = Vwith_echo_area_save_vector;
7069 Vwith_echo_area_save_vector = Qnil;
7070
7071 if (NILP (vector))
7072 vector = Fmake_vector (make_number (7), Qnil);
7073
7074 XSETBUFFER (AREF (vector, i), current_buffer); ++i;
7075 AREF (vector, i) = Vdeactivate_mark, ++i;
7076 AREF (vector, i) = make_number (windows_or_buffers_changed), ++i;
7077
7078 if (w)
7079 {
7080 XSETWINDOW (AREF (vector, i), w); ++i;
7081 AREF (vector, i) = w->buffer; ++i;
7082 AREF (vector, i) = make_number (XMARKER (w->pointm)->charpos); ++i;
7083 AREF (vector, i) = make_number (XMARKER (w->pointm)->bytepos); ++i;
7084 }
7085 else
7086 {
7087 int end = i + 4;
7088 for (; i < end; ++i)
7089 AREF (vector, i) = Qnil;
7090 }
7091
7092 xassert (i == ASIZE (vector));
7093 return vector;
7094 }
7095
7096
7097 /* Restore global state from VECTOR which was created by
7098 with_echo_area_buffer_unwind_data. */
7099
7100 static Lisp_Object
7101 unwind_with_echo_area_buffer (vector)
7102 Lisp_Object vector;
7103 {
7104 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
7105 Vdeactivate_mark = AREF (vector, 1);
7106 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
7107
7108 if (WINDOWP (AREF (vector, 3)))
7109 {
7110 struct window *w;
7111 Lisp_Object buffer, charpos, bytepos;
7112
7113 w = XWINDOW (AREF (vector, 3));
7114 buffer = AREF (vector, 4);
7115 charpos = AREF (vector, 5);
7116 bytepos = AREF (vector, 6);
7117
7118 w->buffer = buffer;
7119 set_marker_both (w->pointm, buffer,
7120 XFASTINT (charpos), XFASTINT (bytepos));
7121 }
7122
7123 Vwith_echo_area_save_vector = vector;
7124 return Qnil;
7125 }
7126
7127
7128 /* Set up the echo area for use by print functions. MULTIBYTE_P
7129 non-zero means we will print multibyte. */
7130
7131 void
7132 setup_echo_area_for_printing (multibyte_p)
7133 int multibyte_p;
7134 {
7135 /* If we can't find an echo area any more, exit. */
7136 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
7137 Fkill_emacs (Qnil);
7138
7139 ensure_echo_area_buffers ();
7140
7141 if (!message_buf_print)
7142 {
7143 /* A message has been output since the last time we printed.
7144 Choose a fresh echo area buffer. */
7145 if (EQ (echo_area_buffer[1], echo_buffer[0]))
7146 echo_area_buffer[0] = echo_buffer[1];
7147 else
7148 echo_area_buffer[0] = echo_buffer[0];
7149
7150 /* Switch to that buffer and clear it. */
7151 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
7152 current_buffer->truncate_lines = Qnil;
7153
7154 if (Z > BEG)
7155 {
7156 int count = SPECPDL_INDEX ();
7157 specbind (Qinhibit_read_only, Qt);
7158 /* Note that undo recording is always disabled. */
7159 del_range (BEG, Z);
7160 unbind_to (count, Qnil);
7161 }
7162 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
7163
7164 /* Set up the buffer for the multibyteness we need. */
7165 if (multibyte_p
7166 != !NILP (current_buffer->enable_multibyte_characters))
7167 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
7168
7169 /* Raise the frame containing the echo area. */
7170 if (minibuffer_auto_raise)
7171 {
7172 struct frame *sf = SELECTED_FRAME ();
7173 Lisp_Object mini_window;
7174 mini_window = FRAME_MINIBUF_WINDOW (sf);
7175 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
7176 }
7177
7178 message_log_maybe_newline ();
7179 message_buf_print = 1;
7180 }
7181 else
7182 {
7183 if (NILP (echo_area_buffer[0]))
7184 {
7185 if (EQ (echo_area_buffer[1], echo_buffer[0]))
7186 echo_area_buffer[0] = echo_buffer[1];
7187 else
7188 echo_area_buffer[0] = echo_buffer[0];
7189 }
7190
7191 if (current_buffer != XBUFFER (echo_area_buffer[0]))
7192 {
7193 /* Someone switched buffers between print requests. */
7194 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
7195 current_buffer->truncate_lines = Qnil;
7196 }
7197 }
7198 }
7199
7200
7201 /* Display an echo area message in window W. Value is non-zero if W's
7202 height is changed. If display_last_displayed_message_p is
7203 non-zero, display the message that was last displayed, otherwise
7204 display the current message. */
7205
7206 static int
7207 display_echo_area (w)
7208 struct window *w;
7209 {
7210 int i, no_message_p, window_height_changed_p, count;
7211
7212 /* Temporarily disable garbage collections while displaying the echo
7213 area. This is done because a GC can print a message itself.
7214 That message would modify the echo area buffer's contents while a
7215 redisplay of the buffer is going on, and seriously confuse
7216 redisplay. */
7217 count = inhibit_garbage_collection ();
7218
7219 /* If there is no message, we must call display_echo_area_1
7220 nevertheless because it resizes the window. But we will have to
7221 reset the echo_area_buffer in question to nil at the end because
7222 with_echo_area_buffer will sets it to an empty buffer. */
7223 i = display_last_displayed_message_p ? 1 : 0;
7224 no_message_p = NILP (echo_area_buffer[i]);
7225
7226 window_height_changed_p
7227 = with_echo_area_buffer (w, display_last_displayed_message_p,
7228 display_echo_area_1,
7229 (EMACS_INT) w, Qnil, 0, 0);
7230
7231 if (no_message_p)
7232 echo_area_buffer[i] = Qnil;
7233
7234 unbind_to (count, Qnil);
7235 return window_height_changed_p;
7236 }
7237
7238
7239 /* Helper for display_echo_area. Display the current buffer which
7240 contains the current echo area message in window W, a mini-window,
7241 a pointer to which is passed in A1. A2..A4 are currently not used.
7242 Change the height of W so that all of the message is displayed.
7243 Value is non-zero if height of W was changed. */
7244
7245 static int
7246 display_echo_area_1 (a1, a2, a3, a4)
7247 EMACS_INT a1;
7248 Lisp_Object a2;
7249 EMACS_INT a3, a4;
7250 {
7251 struct window *w = (struct window *) a1;
7252 Lisp_Object window;
7253 struct text_pos start;
7254 int window_height_changed_p = 0;
7255
7256 /* Do this before displaying, so that we have a large enough glyph
7257 matrix for the display. */
7258 window_height_changed_p = resize_mini_window (w, 0);
7259
7260 /* Display. */
7261 clear_glyph_matrix (w->desired_matrix);
7262 XSETWINDOW (window, w);
7263 SET_TEXT_POS (start, BEG, BEG_BYTE);
7264 try_window (window, start);
7265
7266 return window_height_changed_p;
7267 }
7268
7269
7270 /* Resize the echo area window to exactly the size needed for the
7271 currently displayed message, if there is one. If a mini-buffer
7272 is active, don't shrink it. */
7273
7274 void
7275 resize_echo_area_exactly ()
7276 {
7277 if (BUFFERP (echo_area_buffer[0])
7278 && WINDOWP (echo_area_window))
7279 {
7280 struct window *w = XWINDOW (echo_area_window);
7281 int resized_p;
7282 Lisp_Object resize_exactly;
7283
7284 if (minibuf_level == 0)
7285 resize_exactly = Qt;
7286 else
7287 resize_exactly = Qnil;
7288
7289 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
7290 (EMACS_INT) w, resize_exactly, 0, 0);
7291 if (resized_p)
7292 {
7293 ++windows_or_buffers_changed;
7294 ++update_mode_lines;
7295 redisplay_internal (0);
7296 }
7297 }
7298 }
7299
7300
7301 /* Callback function for with_echo_area_buffer, when used from
7302 resize_echo_area_exactly. A1 contains a pointer to the window to
7303 resize, EXACTLY non-nil means resize the mini-window exactly to the
7304 size of the text displayed. A3 and A4 are not used. Value is what
7305 resize_mini_window returns. */
7306
7307 static int
7308 resize_mini_window_1 (a1, exactly, a3, a4)
7309 EMACS_INT a1;
7310 Lisp_Object exactly;
7311 EMACS_INT a3, a4;
7312 {
7313 return resize_mini_window ((struct window *) a1, !NILP (exactly));
7314 }
7315
7316
7317 /* Resize mini-window W to fit the size of its contents. EXACT:P
7318 means size the window exactly to the size needed. Otherwise, it's
7319 only enlarged until W's buffer is empty. Value is non-zero if
7320 the window height has been changed. */
7321
7322 int
7323 resize_mini_window (w, exact_p)
7324 struct window *w;
7325 int exact_p;
7326 {
7327 struct frame *f = XFRAME (w->frame);
7328 int window_height_changed_p = 0;
7329
7330 xassert (MINI_WINDOW_P (w));
7331
7332 /* Don't resize windows while redisplaying a window; it would
7333 confuse redisplay functions when the size of the window they are
7334 displaying changes from under them. Such a resizing can happen,
7335 for instance, when which-func prints a long message while
7336 we are running fontification-functions. We're running these
7337 functions with safe_call which binds inhibit-redisplay to t. */
7338 if (!NILP (Vinhibit_redisplay))
7339 return 0;
7340
7341 /* Nil means don't try to resize. */
7342 if (NILP (Vresize_mini_windows)
7343 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
7344 return 0;
7345
7346 if (!FRAME_MINIBUF_ONLY_P (f))
7347 {
7348 struct it it;
7349 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
7350 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
7351 int height, max_height;
7352 int unit = FRAME_LINE_HEIGHT (f);
7353 struct text_pos start;
7354 struct buffer *old_current_buffer = NULL;
7355
7356 if (current_buffer != XBUFFER (w->buffer))
7357 {
7358 old_current_buffer = current_buffer;
7359 set_buffer_internal (XBUFFER (w->buffer));
7360 }
7361
7362 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
7363
7364 /* Compute the max. number of lines specified by the user. */
7365 if (FLOATP (Vmax_mini_window_height))
7366 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
7367 else if (INTEGERP (Vmax_mini_window_height))
7368 max_height = XINT (Vmax_mini_window_height);
7369 else
7370 max_height = total_height / 4;
7371
7372 /* Correct that max. height if it's bogus. */
7373 max_height = max (1, max_height);
7374 max_height = min (total_height, max_height);
7375
7376 /* Find out the height of the text in the window. */
7377 if (it.truncate_lines_p)
7378 height = 1;
7379 else
7380 {
7381 last_height = 0;
7382 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
7383 if (it.max_ascent == 0 && it.max_descent == 0)
7384 height = it.current_y + last_height;
7385 else
7386 height = it.current_y + it.max_ascent + it.max_descent;
7387 height -= it.extra_line_spacing;
7388 height = (height + unit - 1) / unit;
7389 }
7390
7391 /* Compute a suitable window start. */
7392 if (height > max_height)
7393 {
7394 height = max_height;
7395 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
7396 move_it_vertically_backward (&it, (height - 1) * unit);
7397 start = it.current.pos;
7398 }
7399 else
7400 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
7401 SET_MARKER_FROM_TEXT_POS (w->start, start);
7402
7403 if (EQ (Vresize_mini_windows, Qgrow_only))
7404 {
7405 /* Let it grow only, until we display an empty message, in which
7406 case the window shrinks again. */
7407 if (height > WINDOW_TOTAL_LINES (w))
7408 {
7409 int old_height = WINDOW_TOTAL_LINES (w);
7410 freeze_window_starts (f, 1);
7411 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
7412 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
7413 }
7414 else if (height < WINDOW_TOTAL_LINES (w)
7415 && (exact_p || BEGV == ZV))
7416 {
7417 int old_height = WINDOW_TOTAL_LINES (w);
7418 freeze_window_starts (f, 0);
7419 shrink_mini_window (w);
7420 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
7421 }
7422 }
7423 else
7424 {
7425 /* Always resize to exact size needed. */
7426 if (height > WINDOW_TOTAL_LINES (w))
7427 {
7428 int old_height = WINDOW_TOTAL_LINES (w);
7429 freeze_window_starts (f, 1);
7430 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
7431 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
7432 }
7433 else if (height < WINDOW_TOTAL_LINES (w))
7434 {
7435 int old_height = WINDOW_TOTAL_LINES (w);
7436 freeze_window_starts (f, 0);
7437 shrink_mini_window (w);
7438
7439 if (height)
7440 {
7441 freeze_window_starts (f, 1);
7442 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
7443 }
7444
7445 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
7446 }
7447 }
7448
7449 if (old_current_buffer)
7450 set_buffer_internal (old_current_buffer);
7451 }
7452
7453 return window_height_changed_p;
7454 }
7455
7456
7457 /* Value is the current message, a string, or nil if there is no
7458 current message. */
7459
7460 Lisp_Object
7461 current_message ()
7462 {
7463 Lisp_Object msg;
7464
7465 if (NILP (echo_area_buffer[0]))
7466 msg = Qnil;
7467 else
7468 {
7469 with_echo_area_buffer (0, 0, current_message_1,
7470 (EMACS_INT) &msg, Qnil, 0, 0);
7471 if (NILP (msg))
7472 echo_area_buffer[0] = Qnil;
7473 }
7474
7475 return msg;
7476 }
7477
7478
7479 static int
7480 current_message_1 (a1, a2, a3, a4)
7481 EMACS_INT a1;
7482 Lisp_Object a2;
7483 EMACS_INT a3, a4;
7484 {
7485 Lisp_Object *msg = (Lisp_Object *) a1;
7486
7487 if (Z > BEG)
7488 *msg = make_buffer_string (BEG, Z, 1);
7489 else
7490 *msg = Qnil;
7491 return 0;
7492 }
7493
7494
7495 /* Push the current message on Vmessage_stack for later restauration
7496 by restore_message. Value is non-zero if the current message isn't
7497 empty. This is a relatively infrequent operation, so it's not
7498 worth optimizing. */
7499
7500 int
7501 push_message ()
7502 {
7503 Lisp_Object msg;
7504 msg = current_message ();
7505 Vmessage_stack = Fcons (msg, Vmessage_stack);
7506 return STRINGP (msg);
7507 }
7508
7509
7510 /* Restore message display from the top of Vmessage_stack. */
7511
7512 void
7513 restore_message ()
7514 {
7515 Lisp_Object msg;
7516
7517 xassert (CONSP (Vmessage_stack));
7518 msg = XCAR (Vmessage_stack);
7519 if (STRINGP (msg))
7520 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
7521 else
7522 message3_nolog (msg, 0, 0);
7523 }
7524
7525
7526 /* Handler for record_unwind_protect calling pop_message. */
7527
7528 Lisp_Object
7529 pop_message_unwind (dummy)
7530 Lisp_Object dummy;
7531 {
7532 pop_message ();
7533 return Qnil;
7534 }
7535
7536 /* Pop the top-most entry off Vmessage_stack. */
7537
7538 void
7539 pop_message ()
7540 {
7541 xassert (CONSP (Vmessage_stack));
7542 Vmessage_stack = XCDR (Vmessage_stack);
7543 }
7544
7545
7546 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
7547 exits. If the stack is not empty, we have a missing pop_message
7548 somewhere. */
7549
7550 void
7551 check_message_stack ()
7552 {
7553 if (!NILP (Vmessage_stack))
7554 abort ();
7555 }
7556
7557
7558 /* Truncate to NCHARS what will be displayed in the echo area the next
7559 time we display it---but don't redisplay it now. */
7560
7561 void
7562 truncate_echo_area (nchars)
7563 int nchars;
7564 {
7565 if (nchars == 0)
7566 echo_area_buffer[0] = Qnil;
7567 /* A null message buffer means that the frame hasn't really been
7568 initialized yet. Error messages get reported properly by
7569 cmd_error, so this must be just an informative message; toss it. */
7570 else if (!noninteractive
7571 && INTERACTIVE
7572 && !NILP (echo_area_buffer[0]))
7573 {
7574 struct frame *sf = SELECTED_FRAME ();
7575 if (FRAME_MESSAGE_BUF (sf))
7576 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
7577 }
7578 }
7579
7580
7581 /* Helper function for truncate_echo_area. Truncate the current
7582 message to at most NCHARS characters. */
7583
7584 static int
7585 truncate_message_1 (nchars, a2, a3, a4)
7586 EMACS_INT nchars;
7587 Lisp_Object a2;
7588 EMACS_INT a3, a4;
7589 {
7590 if (BEG + nchars < Z)
7591 del_range (BEG + nchars, Z);
7592 if (Z == BEG)
7593 echo_area_buffer[0] = Qnil;
7594 return 0;
7595 }
7596
7597
7598 /* Set the current message to a substring of S or STRING.
7599
7600 If STRING is a Lisp string, set the message to the first NBYTES
7601 bytes from STRING. NBYTES zero means use the whole string. If
7602 STRING is multibyte, the message will be displayed multibyte.
7603
7604 If S is not null, set the message to the first LEN bytes of S. LEN
7605 zero means use the whole string. MULTIBYTE_P non-zero means S is
7606 multibyte. Display the message multibyte in that case. */
7607
7608 void
7609 set_message (s, string, nbytes, multibyte_p)
7610 const char *s;
7611 Lisp_Object string;
7612 int nbytes, multibyte_p;
7613 {
7614 message_enable_multibyte
7615 = ((s && multibyte_p)
7616 || (STRINGP (string) && STRING_MULTIBYTE (string)));
7617
7618 with_echo_area_buffer (0, -1, set_message_1,
7619 (EMACS_INT) s, string, nbytes, multibyte_p);
7620 message_buf_print = 0;
7621 help_echo_showing_p = 0;
7622 }
7623
7624
7625 /* Helper function for set_message. Arguments have the same meaning
7626 as there, with A1 corresponding to S and A2 corresponding to STRING
7627 This function is called with the echo area buffer being
7628 current. */
7629
7630 static int
7631 set_message_1 (a1, a2, nbytes, multibyte_p)
7632 EMACS_INT a1;
7633 Lisp_Object a2;
7634 EMACS_INT nbytes, multibyte_p;
7635 {
7636 const char *s = (const char *) a1;
7637 Lisp_Object string = a2;
7638
7639 xassert (BEG == Z);
7640
7641 /* Change multibyteness of the echo buffer appropriately. */
7642 if (message_enable_multibyte
7643 != !NILP (current_buffer->enable_multibyte_characters))
7644 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
7645
7646 current_buffer->truncate_lines = message_truncate_lines ? Qt : Qnil;
7647
7648 /* Insert new message at BEG. */
7649 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
7650
7651 if (STRINGP (string))
7652 {
7653 int nchars;
7654
7655 if (nbytes == 0)
7656 nbytes = SBYTES (string);
7657 nchars = string_byte_to_char (string, nbytes);
7658
7659 /* This function takes care of single/multibyte conversion. We
7660 just have to ensure that the echo area buffer has the right
7661 setting of enable_multibyte_characters. */
7662 insert_from_string (string, 0, 0, nchars, nbytes, 1);
7663 }
7664 else if (s)
7665 {
7666 if (nbytes == 0)
7667 nbytes = strlen (s);
7668
7669 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
7670 {
7671 /* Convert from multi-byte to single-byte. */
7672 int i, c, n;
7673 unsigned char work[1];
7674
7675 /* Convert a multibyte string to single-byte. */
7676 for (i = 0; i < nbytes; i += n)
7677 {
7678 c = string_char_and_length (s + i, nbytes - i, &n);
7679 work[0] = (SINGLE_BYTE_CHAR_P (c)
7680 ? c
7681 : multibyte_char_to_unibyte (c, Qnil));
7682 insert_1_both (work, 1, 1, 1, 0, 0);
7683 }
7684 }
7685 else if (!multibyte_p
7686 && !NILP (current_buffer->enable_multibyte_characters))
7687 {
7688 /* Convert from single-byte to multi-byte. */
7689 int i, c, n;
7690 const unsigned char *msg = (const unsigned char *) s;
7691 unsigned char str[MAX_MULTIBYTE_LENGTH];
7692
7693 /* Convert a single-byte string to multibyte. */
7694 for (i = 0; i < nbytes; i++)
7695 {
7696 c = unibyte_char_to_multibyte (msg[i]);
7697 n = CHAR_STRING (c, str);
7698 insert_1_both (str, 1, n, 1, 0, 0);
7699 }
7700 }
7701 else
7702 insert_1 (s, nbytes, 1, 0, 0);
7703 }
7704
7705 return 0;
7706 }
7707
7708
7709 /* Clear messages. CURRENT_P non-zero means clear the current
7710 message. LAST_DISPLAYED_P non-zero means clear the message
7711 last displayed. */
7712
7713 void
7714 clear_message (current_p, last_displayed_p)
7715 int current_p, last_displayed_p;
7716 {
7717 if (current_p)
7718 {
7719 echo_area_buffer[0] = Qnil;
7720 message_cleared_p = 1;
7721 }
7722
7723 if (last_displayed_p)
7724 echo_area_buffer[1] = Qnil;
7725
7726 message_buf_print = 0;
7727 }
7728
7729 /* Clear garbaged frames.
7730
7731 This function is used where the old redisplay called
7732 redraw_garbaged_frames which in turn called redraw_frame which in
7733 turn called clear_frame. The call to clear_frame was a source of
7734 flickering. I believe a clear_frame is not necessary. It should
7735 suffice in the new redisplay to invalidate all current matrices,
7736 and ensure a complete redisplay of all windows. */
7737
7738 static void
7739 clear_garbaged_frames ()
7740 {
7741 if (frame_garbaged)
7742 {
7743 Lisp_Object tail, frame;
7744 int changed_count = 0;
7745
7746 FOR_EACH_FRAME (tail, frame)
7747 {
7748 struct frame *f = XFRAME (frame);
7749
7750 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
7751 {
7752 if (f->resized_p)
7753 {
7754 Fredraw_frame (frame);
7755 f->force_flush_display_p = 1;
7756 }
7757 clear_current_matrices (f);
7758 changed_count++;
7759 f->garbaged = 0;
7760 f->resized_p = 0;
7761 }
7762 }
7763
7764 frame_garbaged = 0;
7765 if (changed_count)
7766 ++windows_or_buffers_changed;
7767 }
7768 }
7769
7770
7771 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
7772 is non-zero update selected_frame. Value is non-zero if the
7773 mini-windows height has been changed. */
7774
7775 static int
7776 echo_area_display (update_frame_p)
7777 int update_frame_p;
7778 {
7779 Lisp_Object mini_window;
7780 struct window *w;
7781 struct frame *f;
7782 int window_height_changed_p = 0;
7783 struct frame *sf = SELECTED_FRAME ();
7784
7785 mini_window = FRAME_MINIBUF_WINDOW (sf);
7786 w = XWINDOW (mini_window);
7787 f = XFRAME (WINDOW_FRAME (w));
7788
7789 /* Don't display if frame is invisible or not yet initialized. */
7790 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
7791 return 0;
7792
7793 /* The terminal frame is used as the first Emacs frame on the Mac OS. */
7794 #ifndef MAC_OS8
7795 #ifdef HAVE_WINDOW_SYSTEM
7796 /* When Emacs starts, selected_frame may be the initial terminal
7797 frame. If we let this through, a message would be displayed on
7798 the terminal. */
7799 if (FRAME_TERMCAP_P (XFRAME (selected_frame))
7800 && FRAME_TTY (XFRAME (selected_frame))->type == NULL)
7801 return 0;
7802 #endif /* HAVE_WINDOW_SYSTEM */
7803 #endif
7804
7805 /* Redraw garbaged frames. */
7806 if (frame_garbaged)
7807 clear_garbaged_frames ();
7808
7809 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
7810 {
7811 echo_area_window = mini_window;
7812 window_height_changed_p = display_echo_area (w);
7813 w->must_be_updated_p = 1;
7814
7815 /* Update the display, unless called from redisplay_internal.
7816 Also don't update the screen during redisplay itself. The
7817 update will happen at the end of redisplay, and an update
7818 here could cause confusion. */
7819 if (update_frame_p && !redisplaying_p)
7820 {
7821 int n = 0;
7822
7823 /* If the display update has been interrupted by pending
7824 input, update mode lines in the frame. Due to the
7825 pending input, it might have been that redisplay hasn't
7826 been called, so that mode lines above the echo area are
7827 garbaged. This looks odd, so we prevent it here. */
7828 if (!display_completed)
7829 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
7830
7831 if (window_height_changed_p
7832 /* Don't do this if Emacs is shutting down. Redisplay
7833 needs to run hooks. */
7834 && !NILP (Vrun_hooks))
7835 {
7836 /* Must update other windows. Likewise as in other
7837 cases, don't let this update be interrupted by
7838 pending input. */
7839 int count = SPECPDL_INDEX ();
7840 specbind (Qredisplay_dont_pause, Qt);
7841 windows_or_buffers_changed = 1;
7842 redisplay_internal (0);
7843 unbind_to (count, Qnil);
7844 }
7845 else if (FRAME_WINDOW_P (f) && n == 0)
7846 {
7847 /* Window configuration is the same as before.
7848 Can do with a display update of the echo area,
7849 unless we displayed some mode lines. */
7850 update_single_window (w, 1);
7851 FRAME_RIF (f)->flush_display (f);
7852 }
7853 else
7854 update_frame (f, 1, 1);
7855
7856 /* If cursor is in the echo area, make sure that the next
7857 redisplay displays the minibuffer, so that the cursor will
7858 be replaced with what the minibuffer wants. */
7859 if (cursor_in_echo_area)
7860 ++windows_or_buffers_changed;
7861 }
7862 }
7863 else if (!EQ (mini_window, selected_window))
7864 windows_or_buffers_changed++;
7865
7866 /* Last displayed message is now the current message. */
7867 echo_area_buffer[1] = echo_area_buffer[0];
7868
7869 /* Prevent redisplay optimization in redisplay_internal by resetting
7870 this_line_start_pos. This is done because the mini-buffer now
7871 displays the message instead of its buffer text. */
7872 if (EQ (mini_window, selected_window))
7873 CHARPOS (this_line_start_pos) = 0;
7874
7875 return window_height_changed_p;
7876 }
7877
7878
7879 \f
7880 /***********************************************************************
7881 Frame Titles
7882 ***********************************************************************/
7883
7884
7885 /* The frame title buffering code is also used by Fformat_mode_line.
7886 So it is not conditioned by HAVE_WINDOW_SYSTEM. */
7887
7888 /* A buffer for constructing frame titles in it; allocated from the
7889 heap in init_xdisp and resized as needed in store_frame_title_char. */
7890
7891 static char *frame_title_buf;
7892
7893 /* The buffer's end, and a current output position in it. */
7894
7895 static char *frame_title_buf_end;
7896 static char *frame_title_ptr;
7897
7898
7899 /* Store a single character C for the frame title in frame_title_buf.
7900 Re-allocate frame_title_buf if necessary. */
7901
7902 static void
7903 #ifdef PROTOTYPES
7904 store_frame_title_char (char c)
7905 #else
7906 store_frame_title_char (c)
7907 char c;
7908 #endif
7909 {
7910 /* If output position has reached the end of the allocated buffer,
7911 double the buffer's size. */
7912 if (frame_title_ptr == frame_title_buf_end)
7913 {
7914 int len = frame_title_ptr - frame_title_buf;
7915 int new_size = 2 * len * sizeof *frame_title_buf;
7916 frame_title_buf = (char *) xrealloc (frame_title_buf, new_size);
7917 frame_title_buf_end = frame_title_buf + new_size;
7918 frame_title_ptr = frame_title_buf + len;
7919 }
7920
7921 *frame_title_ptr++ = c;
7922 }
7923
7924
7925 /* Store part of a frame title in frame_title_buf, beginning at
7926 frame_title_ptr. STR is the string to store. Do not copy
7927 characters that yield more columns than PRECISION; PRECISION <= 0
7928 means copy the whole string. Pad with spaces until FIELD_WIDTH
7929 number of characters have been copied; FIELD_WIDTH <= 0 means don't
7930 pad. Called from display_mode_element when it is used to build a
7931 frame title. */
7932
7933 static int
7934 store_frame_title (str, field_width, precision)
7935 const unsigned char *str;
7936 int field_width, precision;
7937 {
7938 int n = 0;
7939 int dummy, nbytes;
7940
7941 /* Copy at most PRECISION chars from STR. */
7942 nbytes = strlen (str);
7943 n+= c_string_width (str, nbytes, precision, &dummy, &nbytes);
7944 while (nbytes--)
7945 store_frame_title_char (*str++);
7946
7947 /* Fill up with spaces until FIELD_WIDTH reached. */
7948 while (field_width > 0
7949 && n < field_width)
7950 {
7951 store_frame_title_char (' ');
7952 ++n;
7953 }
7954
7955 return n;
7956 }
7957
7958 #ifdef HAVE_WINDOW_SYSTEM
7959
7960 /* Set the title of FRAME, if it has changed. The title format is
7961 Vicon_title_format if FRAME is iconified, otherwise it is
7962 frame_title_format. */
7963
7964 static void
7965 x_consider_frame_title (frame)
7966 Lisp_Object frame;
7967 {
7968 struct frame *f = XFRAME (frame);
7969
7970 if (FRAME_WINDOW_P (f)
7971 || FRAME_MINIBUF_ONLY_P (f)
7972 || f->explicit_name)
7973 {
7974 /* Do we have more than one visible frame on this X display? */
7975 Lisp_Object tail;
7976 Lisp_Object fmt;
7977 struct buffer *obuf;
7978 int len;
7979 struct it it;
7980
7981 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
7982 {
7983 Lisp_Object other_frame = XCAR (tail);
7984 struct frame *tf = XFRAME (other_frame);
7985
7986 if (tf != f
7987 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
7988 && !FRAME_MINIBUF_ONLY_P (tf)
7989 && !EQ (other_frame, tip_frame)
7990 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
7991 break;
7992 }
7993
7994 /* Set global variable indicating that multiple frames exist. */
7995 multiple_frames = CONSP (tail);
7996
7997 /* Switch to the buffer of selected window of the frame. Set up
7998 frame_title_ptr so that display_mode_element will output into it;
7999 then display the title. */
8000 obuf = current_buffer;
8001 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
8002 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
8003 frame_title_ptr = frame_title_buf;
8004 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
8005 NULL, DEFAULT_FACE_ID);
8006 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
8007 len = frame_title_ptr - frame_title_buf;
8008 frame_title_ptr = NULL;
8009 set_buffer_internal_1 (obuf);
8010
8011 /* Set the title only if it's changed. This avoids consing in
8012 the common case where it hasn't. (If it turns out that we've
8013 already wasted too much time by walking through the list with
8014 display_mode_element, then we might need to optimize at a
8015 higher level than this.) */
8016 if (! STRINGP (f->name)
8017 || SBYTES (f->name) != len
8018 || bcmp (frame_title_buf, SDATA (f->name), len) != 0)
8019 x_implicitly_set_name (f, make_string (frame_title_buf, len), Qnil);
8020 }
8021 }
8022
8023 #endif /* not HAVE_WINDOW_SYSTEM */
8024
8025
8026
8027 \f
8028 /***********************************************************************
8029 Menu Bars
8030 ***********************************************************************/
8031
8032
8033 /* Prepare for redisplay by updating menu-bar item lists when
8034 appropriate. This can call eval. */
8035
8036 void
8037 prepare_menu_bars ()
8038 {
8039 int all_windows;
8040 struct gcpro gcpro1, gcpro2;
8041 struct frame *f;
8042 Lisp_Object tooltip_frame;
8043
8044 #ifdef HAVE_WINDOW_SYSTEM
8045 tooltip_frame = tip_frame;
8046 #else
8047 tooltip_frame = Qnil;
8048 #endif
8049
8050 /* Update all frame titles based on their buffer names, etc. We do
8051 this before the menu bars so that the buffer-menu will show the
8052 up-to-date frame titles. */
8053 #ifdef HAVE_WINDOW_SYSTEM
8054 if (windows_or_buffers_changed || update_mode_lines)
8055 {
8056 Lisp_Object tail, frame;
8057
8058 FOR_EACH_FRAME (tail, frame)
8059 {
8060 f = XFRAME (frame);
8061 if (!EQ (frame, tooltip_frame)
8062 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
8063 x_consider_frame_title (frame);
8064 }
8065 }
8066 #endif /* HAVE_WINDOW_SYSTEM */
8067
8068 /* Update the menu bar item lists, if appropriate. This has to be
8069 done before any actual redisplay or generation of display lines. */
8070 all_windows = (update_mode_lines
8071 || buffer_shared > 1
8072 || windows_or_buffers_changed);
8073 if (all_windows)
8074 {
8075 Lisp_Object tail, frame;
8076 int count = SPECPDL_INDEX ();
8077
8078 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
8079
8080 FOR_EACH_FRAME (tail, frame)
8081 {
8082 f = XFRAME (frame);
8083
8084 /* Ignore tooltip frame. */
8085 if (EQ (frame, tooltip_frame))
8086 continue;
8087
8088 /* If a window on this frame changed size, report that to
8089 the user and clear the size-change flag. */
8090 if (FRAME_WINDOW_SIZES_CHANGED (f))
8091 {
8092 Lisp_Object functions;
8093
8094 /* Clear flag first in case we get an error below. */
8095 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
8096 functions = Vwindow_size_change_functions;
8097 GCPRO2 (tail, functions);
8098
8099 while (CONSP (functions))
8100 {
8101 call1 (XCAR (functions), frame);
8102 functions = XCDR (functions);
8103 }
8104 UNGCPRO;
8105 }
8106
8107 GCPRO1 (tail);
8108 update_menu_bar (f, 0);
8109 #ifdef HAVE_WINDOW_SYSTEM
8110 update_tool_bar (f, 0);
8111 #endif
8112 UNGCPRO;
8113 }
8114
8115 unbind_to (count, Qnil);
8116 }
8117 else
8118 {
8119 struct frame *sf = SELECTED_FRAME ();
8120 update_menu_bar (sf, 1);
8121 #ifdef HAVE_WINDOW_SYSTEM
8122 update_tool_bar (sf, 1);
8123 #endif
8124 }
8125
8126 /* Motif needs this. See comment in xmenu.c. Turn it off when
8127 pending_menu_activation is not defined. */
8128 #ifdef USE_X_TOOLKIT
8129 pending_menu_activation = 0;
8130 #endif
8131 }
8132
8133
8134 /* Update the menu bar item list for frame F. This has to be done
8135 before we start to fill in any display lines, because it can call
8136 eval.
8137
8138 If SAVE_MATCH_DATA is non-zero, we must save and restore it here. */
8139
8140 static void
8141 update_menu_bar (f, save_match_data)
8142 struct frame *f;
8143 int save_match_data;
8144 {
8145 Lisp_Object window;
8146 register struct window *w;
8147
8148 /* If called recursively during a menu update, do nothing. This can
8149 happen when, for instance, an activate-menubar-hook causes a
8150 redisplay. */
8151 if (inhibit_menubar_update)
8152 return;
8153
8154 window = FRAME_SELECTED_WINDOW (f);
8155 w = XWINDOW (window);
8156
8157 #if 0 /* The if statement below this if statement used to include the
8158 condition !NILP (w->update_mode_line), rather than using
8159 update_mode_lines directly, and this if statement may have
8160 been added to make that condition work. Now the if
8161 statement below matches its comment, this isn't needed. */
8162 if (update_mode_lines)
8163 w->update_mode_line = Qt;
8164 #endif
8165
8166 if (FRAME_WINDOW_P (f)
8167 ?
8168 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
8169 || defined (USE_GTK)
8170 FRAME_EXTERNAL_MENU_BAR (f)
8171 #else
8172 FRAME_MENU_BAR_LINES (f) > 0
8173 #endif
8174 : FRAME_MENU_BAR_LINES (f) > 0)
8175 {
8176 /* If the user has switched buffers or windows, we need to
8177 recompute to reflect the new bindings. But we'll
8178 recompute when update_mode_lines is set too; that means
8179 that people can use force-mode-line-update to request
8180 that the menu bar be recomputed. The adverse effect on
8181 the rest of the redisplay algorithm is about the same as
8182 windows_or_buffers_changed anyway. */
8183 if (windows_or_buffers_changed
8184 /* This used to test w->update_mode_line, but we believe
8185 there is no need to recompute the menu in that case. */
8186 || update_mode_lines
8187 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
8188 < BUF_MODIFF (XBUFFER (w->buffer)))
8189 != !NILP (w->last_had_star))
8190 || ((!NILP (Vtransient_mark_mode)
8191 && !NILP (XBUFFER (w->buffer)->mark_active))
8192 != !NILP (w->region_showing)))
8193 {
8194 struct buffer *prev = current_buffer;
8195 int count = SPECPDL_INDEX ();
8196
8197 specbind (Qinhibit_menubar_update, Qt);
8198
8199 set_buffer_internal_1 (XBUFFER (w->buffer));
8200 if (save_match_data)
8201 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
8202 if (NILP (Voverriding_local_map_menu_flag))
8203 {
8204 specbind (Qoverriding_terminal_local_map, Qnil);
8205 specbind (Qoverriding_local_map, Qnil);
8206 }
8207
8208 /* Run the Lucid hook. */
8209 safe_run_hooks (Qactivate_menubar_hook);
8210
8211 /* If it has changed current-menubar from previous value,
8212 really recompute the menu-bar from the value. */
8213 if (! NILP (Vlucid_menu_bar_dirty_flag))
8214 call0 (Qrecompute_lucid_menubar);
8215
8216 safe_run_hooks (Qmenu_bar_update_hook);
8217 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
8218
8219 /* Redisplay the menu bar in case we changed it. */
8220 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
8221 || defined (USE_GTK)
8222 if (FRAME_WINDOW_P (f)
8223 #if defined (MAC_OS)
8224 /* All frames on Mac OS share the same menubar. So only the
8225 selected frame should be allowed to set it. */
8226 && f == SELECTED_FRAME ()
8227 #endif
8228 )
8229 set_frame_menubar (f, 0, 0);
8230 else
8231 /* On a terminal screen, the menu bar is an ordinary screen
8232 line, and this makes it get updated. */
8233 w->update_mode_line = Qt;
8234 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
8235 /* In the non-toolkit version, the menu bar is an ordinary screen
8236 line, and this makes it get updated. */
8237 w->update_mode_line = Qt;
8238 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
8239
8240 unbind_to (count, Qnil);
8241 set_buffer_internal_1 (prev);
8242 }
8243 }
8244 }
8245
8246
8247 \f
8248 /***********************************************************************
8249 Output Cursor
8250 ***********************************************************************/
8251
8252 #ifdef HAVE_WINDOW_SYSTEM
8253
8254 /* EXPORT:
8255 Nominal cursor position -- where to draw output.
8256 HPOS and VPOS are window relative glyph matrix coordinates.
8257 X and Y are window relative pixel coordinates. */
8258
8259 struct cursor_pos output_cursor;
8260
8261
8262 /* EXPORT:
8263 Set the global variable output_cursor to CURSOR. All cursor
8264 positions are relative to updated_window. */
8265
8266 void
8267 set_output_cursor (cursor)
8268 struct cursor_pos *cursor;
8269 {
8270 output_cursor.hpos = cursor->hpos;
8271 output_cursor.vpos = cursor->vpos;
8272 output_cursor.x = cursor->x;
8273 output_cursor.y = cursor->y;
8274 }
8275
8276
8277 /* EXPORT for RIF:
8278 Set a nominal cursor position.
8279
8280 HPOS and VPOS are column/row positions in a window glyph matrix. X
8281 and Y are window text area relative pixel positions.
8282
8283 If this is done during an update, updated_window will contain the
8284 window that is being updated and the position is the future output
8285 cursor position for that window. If updated_window is null, use
8286 selected_window and display the cursor at the given position. */
8287
8288 void
8289 x_cursor_to (vpos, hpos, y, x)
8290 int vpos, hpos, y, x;
8291 {
8292 struct window *w;
8293
8294 /* If updated_window is not set, work on selected_window. */
8295 if (updated_window)
8296 w = updated_window;
8297 else
8298 w = XWINDOW (selected_window);
8299
8300 /* Set the output cursor. */
8301 output_cursor.hpos = hpos;
8302 output_cursor.vpos = vpos;
8303 output_cursor.x = x;
8304 output_cursor.y = y;
8305
8306 /* If not called as part of an update, really display the cursor.
8307 This will also set the cursor position of W. */
8308 if (updated_window == NULL)
8309 {
8310 BLOCK_INPUT;
8311 display_and_set_cursor (w, 1, hpos, vpos, x, y);
8312 if (FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
8313 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (SELECTED_FRAME ());
8314 UNBLOCK_INPUT;
8315 }
8316 }
8317
8318 #endif /* HAVE_WINDOW_SYSTEM */
8319
8320 \f
8321 /***********************************************************************
8322 Tool-bars
8323 ***********************************************************************/
8324
8325 #ifdef HAVE_WINDOW_SYSTEM
8326
8327 /* Where the mouse was last time we reported a mouse event. */
8328
8329 FRAME_PTR last_mouse_frame;
8330
8331 /* Tool-bar item index of the item on which a mouse button was pressed
8332 or -1. */
8333
8334 int last_tool_bar_item;
8335
8336
8337 /* Update the tool-bar item list for frame F. This has to be done
8338 before we start to fill in any display lines. Called from
8339 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
8340 and restore it here. */
8341
8342 static void
8343 update_tool_bar (f, save_match_data)
8344 struct frame *f;
8345 int save_match_data;
8346 {
8347 #ifdef USE_GTK
8348 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
8349 #else
8350 int do_update = WINDOWP (f->tool_bar_window)
8351 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
8352 #endif
8353
8354 if (do_update)
8355 {
8356 Lisp_Object window;
8357 struct window *w;
8358
8359 window = FRAME_SELECTED_WINDOW (f);
8360 w = XWINDOW (window);
8361
8362 /* If the user has switched buffers or windows, we need to
8363 recompute to reflect the new bindings. But we'll
8364 recompute when update_mode_lines is set too; that means
8365 that people can use force-mode-line-update to request
8366 that the menu bar be recomputed. The adverse effect on
8367 the rest of the redisplay algorithm is about the same as
8368 windows_or_buffers_changed anyway. */
8369 if (windows_or_buffers_changed
8370 || !NILP (w->update_mode_line)
8371 || update_mode_lines
8372 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
8373 < BUF_MODIFF (XBUFFER (w->buffer)))
8374 != !NILP (w->last_had_star))
8375 || ((!NILP (Vtransient_mark_mode)
8376 && !NILP (XBUFFER (w->buffer)->mark_active))
8377 != !NILP (w->region_showing)))
8378 {
8379 struct buffer *prev = current_buffer;
8380 int count = SPECPDL_INDEX ();
8381 Lisp_Object old_tool_bar;
8382 struct gcpro gcpro1;
8383
8384 /* Set current_buffer to the buffer of the selected
8385 window of the frame, so that we get the right local
8386 keymaps. */
8387 set_buffer_internal_1 (XBUFFER (w->buffer));
8388
8389 /* Save match data, if we must. */
8390 if (save_match_data)
8391 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
8392
8393 /* Make sure that we don't accidentally use bogus keymaps. */
8394 if (NILP (Voverriding_local_map_menu_flag))
8395 {
8396 specbind (Qoverriding_terminal_local_map, Qnil);
8397 specbind (Qoverriding_local_map, Qnil);
8398 }
8399
8400 old_tool_bar = f->tool_bar_items;
8401 GCPRO1 (old_tool_bar);
8402
8403 /* Build desired tool-bar items from keymaps. */
8404 BLOCK_INPUT;
8405 f->tool_bar_items
8406 = tool_bar_items (f->tool_bar_items, &f->n_tool_bar_items);
8407 UNBLOCK_INPUT;
8408
8409 /* Redisplay the tool-bar if we changed it. */
8410 if (! NILP (Fequal (old_tool_bar, f->tool_bar_items)))
8411 w->update_mode_line = Qt;
8412
8413 UNGCPRO;
8414
8415 unbind_to (count, Qnil);
8416 set_buffer_internal_1 (prev);
8417 }
8418 }
8419 }
8420
8421
8422 /* Set F->desired_tool_bar_string to a Lisp string representing frame
8423 F's desired tool-bar contents. F->tool_bar_items must have
8424 been set up previously by calling prepare_menu_bars. */
8425
8426 static void
8427 build_desired_tool_bar_string (f)
8428 struct frame *f;
8429 {
8430 int i, size, size_needed;
8431 struct gcpro gcpro1, gcpro2, gcpro3;
8432 Lisp_Object image, plist, props;
8433
8434 image = plist = props = Qnil;
8435 GCPRO3 (image, plist, props);
8436
8437 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
8438 Otherwise, make a new string. */
8439
8440 /* The size of the string we might be able to reuse. */
8441 size = (STRINGP (f->desired_tool_bar_string)
8442 ? SCHARS (f->desired_tool_bar_string)
8443 : 0);
8444
8445 /* We need one space in the string for each image. */
8446 size_needed = f->n_tool_bar_items;
8447
8448 /* Reuse f->desired_tool_bar_string, if possible. */
8449 if (size < size_needed || NILP (f->desired_tool_bar_string))
8450 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
8451 make_number (' '));
8452 else
8453 {
8454 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
8455 Fremove_text_properties (make_number (0), make_number (size),
8456 props, f->desired_tool_bar_string);
8457 }
8458
8459 /* Put a `display' property on the string for the images to display,
8460 put a `menu_item' property on tool-bar items with a value that
8461 is the index of the item in F's tool-bar item vector. */
8462 for (i = 0; i < f->n_tool_bar_items; ++i)
8463 {
8464 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
8465
8466 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
8467 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
8468 int hmargin, vmargin, relief, idx, end;
8469 extern Lisp_Object QCrelief, QCmargin, QCconversion;
8470
8471 /* If image is a vector, choose the image according to the
8472 button state. */
8473 image = PROP (TOOL_BAR_ITEM_IMAGES);
8474 if (VECTORP (image))
8475 {
8476 if (enabled_p)
8477 idx = (selected_p
8478 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
8479 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
8480 else
8481 idx = (selected_p
8482 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
8483 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
8484
8485 xassert (ASIZE (image) >= idx);
8486 image = AREF (image, idx);
8487 }
8488 else
8489 idx = -1;
8490
8491 /* Ignore invalid image specifications. */
8492 if (!valid_image_p (image))
8493 continue;
8494
8495 /* Display the tool-bar button pressed, or depressed. */
8496 plist = Fcopy_sequence (XCDR (image));
8497
8498 /* Compute margin and relief to draw. */
8499 relief = (tool_bar_button_relief >= 0
8500 ? tool_bar_button_relief
8501 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
8502 hmargin = vmargin = relief;
8503
8504 if (INTEGERP (Vtool_bar_button_margin)
8505 && XINT (Vtool_bar_button_margin) > 0)
8506 {
8507 hmargin += XFASTINT (Vtool_bar_button_margin);
8508 vmargin += XFASTINT (Vtool_bar_button_margin);
8509 }
8510 else if (CONSP (Vtool_bar_button_margin))
8511 {
8512 if (INTEGERP (XCAR (Vtool_bar_button_margin))
8513 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
8514 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
8515
8516 if (INTEGERP (XCDR (Vtool_bar_button_margin))
8517 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
8518 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
8519 }
8520
8521 if (auto_raise_tool_bar_buttons_p)
8522 {
8523 /* Add a `:relief' property to the image spec if the item is
8524 selected. */
8525 if (selected_p)
8526 {
8527 plist = Fplist_put (plist, QCrelief, make_number (-relief));
8528 hmargin -= relief;
8529 vmargin -= relief;
8530 }
8531 }
8532 else
8533 {
8534 /* If image is selected, display it pressed, i.e. with a
8535 negative relief. If it's not selected, display it with a
8536 raised relief. */
8537 plist = Fplist_put (plist, QCrelief,
8538 (selected_p
8539 ? make_number (-relief)
8540 : make_number (relief)));
8541 hmargin -= relief;
8542 vmargin -= relief;
8543 }
8544
8545 /* Put a margin around the image. */
8546 if (hmargin || vmargin)
8547 {
8548 if (hmargin == vmargin)
8549 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
8550 else
8551 plist = Fplist_put (plist, QCmargin,
8552 Fcons (make_number (hmargin),
8553 make_number (vmargin)));
8554 }
8555
8556 /* If button is not enabled, and we don't have special images
8557 for the disabled state, make the image appear disabled by
8558 applying an appropriate algorithm to it. */
8559 if (!enabled_p && idx < 0)
8560 plist = Fplist_put (plist, QCconversion, Qdisabled);
8561
8562 /* Put a `display' text property on the string for the image to
8563 display. Put a `menu-item' property on the string that gives
8564 the start of this item's properties in the tool-bar items
8565 vector. */
8566 image = Fcons (Qimage, plist);
8567 props = list4 (Qdisplay, image,
8568 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
8569
8570 /* Let the last image hide all remaining spaces in the tool bar
8571 string. The string can be longer than needed when we reuse a
8572 previous string. */
8573 if (i + 1 == f->n_tool_bar_items)
8574 end = SCHARS (f->desired_tool_bar_string);
8575 else
8576 end = i + 1;
8577 Fadd_text_properties (make_number (i), make_number (end),
8578 props, f->desired_tool_bar_string);
8579 #undef PROP
8580 }
8581
8582 UNGCPRO;
8583 }
8584
8585
8586 /* Display one line of the tool-bar of frame IT->f. */
8587
8588 static void
8589 display_tool_bar_line (it)
8590 struct it *it;
8591 {
8592 struct glyph_row *row = it->glyph_row;
8593 int max_x = it->last_visible_x;
8594 struct glyph *last;
8595
8596 prepare_desired_row (row);
8597 row->y = it->current_y;
8598
8599 /* Note that this isn't made use of if the face hasn't a box,
8600 so there's no need to check the face here. */
8601 it->start_of_box_run_p = 1;
8602
8603 while (it->current_x < max_x)
8604 {
8605 int x_before, x, n_glyphs_before, i, nglyphs;
8606
8607 /* Get the next display element. */
8608 if (!get_next_display_element (it))
8609 break;
8610
8611 /* Produce glyphs. */
8612 x_before = it->current_x;
8613 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
8614 PRODUCE_GLYPHS (it);
8615
8616 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
8617 i = 0;
8618 x = x_before;
8619 while (i < nglyphs)
8620 {
8621 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
8622
8623 if (x + glyph->pixel_width > max_x)
8624 {
8625 /* Glyph doesn't fit on line. */
8626 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
8627 it->current_x = x;
8628 goto out;
8629 }
8630
8631 ++it->hpos;
8632 x += glyph->pixel_width;
8633 ++i;
8634 }
8635
8636 /* Stop at line ends. */
8637 if (ITERATOR_AT_END_OF_LINE_P (it))
8638 break;
8639
8640 set_iterator_to_next (it, 1);
8641 }
8642
8643 out:;
8644
8645 row->displays_text_p = row->used[TEXT_AREA] != 0;
8646 extend_face_to_end_of_line (it);
8647 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
8648 last->right_box_line_p = 1;
8649 if (last == row->glyphs[TEXT_AREA])
8650 last->left_box_line_p = 1;
8651 compute_line_metrics (it);
8652
8653 /* If line is empty, make it occupy the rest of the tool-bar. */
8654 if (!row->displays_text_p)
8655 {
8656 row->height = row->phys_height = it->last_visible_y - row->y;
8657 row->ascent = row->phys_ascent = 0;
8658 }
8659
8660 row->full_width_p = 1;
8661 row->continued_p = 0;
8662 row->truncated_on_left_p = 0;
8663 row->truncated_on_right_p = 0;
8664
8665 it->current_x = it->hpos = 0;
8666 it->current_y += row->height;
8667 ++it->vpos;
8668 ++it->glyph_row;
8669 }
8670
8671
8672 /* Value is the number of screen lines needed to make all tool-bar
8673 items of frame F visible. */
8674
8675 static int
8676 tool_bar_lines_needed (f)
8677 struct frame *f;
8678 {
8679 struct window *w = XWINDOW (f->tool_bar_window);
8680 struct it it;
8681
8682 /* Initialize an iterator for iteration over
8683 F->desired_tool_bar_string in the tool-bar window of frame F. */
8684 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
8685 it.first_visible_x = 0;
8686 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
8687 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
8688
8689 while (!ITERATOR_AT_END_P (&it))
8690 {
8691 it.glyph_row = w->desired_matrix->rows;
8692 clear_glyph_row (it.glyph_row);
8693 display_tool_bar_line (&it);
8694 }
8695
8696 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
8697 }
8698
8699
8700 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
8701 0, 1, 0,
8702 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
8703 (frame)
8704 Lisp_Object frame;
8705 {
8706 struct frame *f;
8707 struct window *w;
8708 int nlines = 0;
8709
8710 if (NILP (frame))
8711 frame = selected_frame;
8712 else
8713 CHECK_FRAME (frame);
8714 f = XFRAME (frame);
8715
8716 if (WINDOWP (f->tool_bar_window)
8717 || (w = XWINDOW (f->tool_bar_window),
8718 WINDOW_TOTAL_LINES (w) > 0))
8719 {
8720 update_tool_bar (f, 1);
8721 if (f->n_tool_bar_items)
8722 {
8723 build_desired_tool_bar_string (f);
8724 nlines = tool_bar_lines_needed (f);
8725 }
8726 }
8727
8728 return make_number (nlines);
8729 }
8730
8731
8732 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
8733 height should be changed. */
8734
8735 static int
8736 redisplay_tool_bar (f)
8737 struct frame *f;
8738 {
8739 struct window *w;
8740 struct it it;
8741 struct glyph_row *row;
8742 int change_height_p = 0;
8743
8744 #ifdef USE_GTK
8745 if (FRAME_EXTERNAL_TOOL_BAR (f))
8746 update_frame_tool_bar (f);
8747 return 0;
8748 #endif
8749
8750 /* If frame hasn't a tool-bar window or if it is zero-height, don't
8751 do anything. This means you must start with tool-bar-lines
8752 non-zero to get the auto-sizing effect. Or in other words, you
8753 can turn off tool-bars by specifying tool-bar-lines zero. */
8754 if (!WINDOWP (f->tool_bar_window)
8755 || (w = XWINDOW (f->tool_bar_window),
8756 WINDOW_TOTAL_LINES (w) == 0))
8757 return 0;
8758
8759 /* Set up an iterator for the tool-bar window. */
8760 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
8761 it.first_visible_x = 0;
8762 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
8763 row = it.glyph_row;
8764
8765 /* Build a string that represents the contents of the tool-bar. */
8766 build_desired_tool_bar_string (f);
8767 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
8768
8769 /* Display as many lines as needed to display all tool-bar items. */
8770 while (it.current_y < it.last_visible_y)
8771 display_tool_bar_line (&it);
8772
8773 /* It doesn't make much sense to try scrolling in the tool-bar
8774 window, so don't do it. */
8775 w->desired_matrix->no_scrolling_p = 1;
8776 w->must_be_updated_p = 1;
8777
8778 if (auto_resize_tool_bars_p)
8779 {
8780 int nlines;
8781
8782 /* If we couldn't display everything, change the tool-bar's
8783 height. */
8784 if (IT_STRING_CHARPOS (it) < it.end_charpos)
8785 change_height_p = 1;
8786
8787 /* If there are blank lines at the end, except for a partially
8788 visible blank line at the end that is smaller than
8789 FRAME_LINE_HEIGHT, change the tool-bar's height. */
8790 row = it.glyph_row - 1;
8791 if (!row->displays_text_p
8792 && row->height >= FRAME_LINE_HEIGHT (f))
8793 change_height_p = 1;
8794
8795 /* If row displays tool-bar items, but is partially visible,
8796 change the tool-bar's height. */
8797 if (row->displays_text_p
8798 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y)
8799 change_height_p = 1;
8800
8801 /* Resize windows as needed by changing the `tool-bar-lines'
8802 frame parameter. */
8803 if (change_height_p
8804 && (nlines = tool_bar_lines_needed (f),
8805 nlines != WINDOW_TOTAL_LINES (w)))
8806 {
8807 extern Lisp_Object Qtool_bar_lines;
8808 Lisp_Object frame;
8809 int old_height = WINDOW_TOTAL_LINES (w);
8810
8811 XSETFRAME (frame, f);
8812 clear_glyph_matrix (w->desired_matrix);
8813 Fmodify_frame_parameters (frame,
8814 Fcons (Fcons (Qtool_bar_lines,
8815 make_number (nlines)),
8816 Qnil));
8817 if (WINDOW_TOTAL_LINES (w) != old_height)
8818 fonts_changed_p = 1;
8819 }
8820 }
8821
8822 return change_height_p;
8823 }
8824
8825
8826 /* Get information about the tool-bar item which is displayed in GLYPH
8827 on frame F. Return in *PROP_IDX the index where tool-bar item
8828 properties start in F->tool_bar_items. Value is zero if
8829 GLYPH doesn't display a tool-bar item. */
8830
8831 static int
8832 tool_bar_item_info (f, glyph, prop_idx)
8833 struct frame *f;
8834 struct glyph *glyph;
8835 int *prop_idx;
8836 {
8837 Lisp_Object prop;
8838 int success_p;
8839 int charpos;
8840
8841 /* This function can be called asynchronously, which means we must
8842 exclude any possibility that Fget_text_property signals an
8843 error. */
8844 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
8845 charpos = max (0, charpos);
8846
8847 /* Get the text property `menu-item' at pos. The value of that
8848 property is the start index of this item's properties in
8849 F->tool_bar_items. */
8850 prop = Fget_text_property (make_number (charpos),
8851 Qmenu_item, f->current_tool_bar_string);
8852 if (INTEGERP (prop))
8853 {
8854 *prop_idx = XINT (prop);
8855 success_p = 1;
8856 }
8857 else
8858 success_p = 0;
8859
8860 return success_p;
8861 }
8862
8863 \f
8864 /* Get information about the tool-bar item at position X/Y on frame F.
8865 Return in *GLYPH a pointer to the glyph of the tool-bar item in
8866 the current matrix of the tool-bar window of F, or NULL if not
8867 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
8868 item in F->tool_bar_items. Value is
8869
8870 -1 if X/Y is not on a tool-bar item
8871 0 if X/Y is on the same item that was highlighted before.
8872 1 otherwise. */
8873
8874 static int
8875 get_tool_bar_item (f, x, y, glyph, hpos, vpos, prop_idx)
8876 struct frame *f;
8877 int x, y;
8878 struct glyph **glyph;
8879 int *hpos, *vpos, *prop_idx;
8880 {
8881 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
8882 struct window *w = XWINDOW (f->tool_bar_window);
8883 int area;
8884
8885 /* Find the glyph under X/Y. */
8886 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
8887 if (*glyph == NULL)
8888 return -1;
8889
8890 /* Get the start of this tool-bar item's properties in
8891 f->tool_bar_items. */
8892 if (!tool_bar_item_info (f, *glyph, prop_idx))
8893 return -1;
8894
8895 /* Is mouse on the highlighted item? */
8896 if (EQ (f->tool_bar_window, dpyinfo->mouse_face_window)
8897 && *vpos >= dpyinfo->mouse_face_beg_row
8898 && *vpos <= dpyinfo->mouse_face_end_row
8899 && (*vpos > dpyinfo->mouse_face_beg_row
8900 || *hpos >= dpyinfo->mouse_face_beg_col)
8901 && (*vpos < dpyinfo->mouse_face_end_row
8902 || *hpos < dpyinfo->mouse_face_end_col
8903 || dpyinfo->mouse_face_past_end))
8904 return 0;
8905
8906 return 1;
8907 }
8908
8909
8910 /* EXPORT:
8911 Handle mouse button event on the tool-bar of frame F, at
8912 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
8913 0 for button release. MODIFIERS is event modifiers for button
8914 release. */
8915
8916 void
8917 handle_tool_bar_click (f, x, y, down_p, modifiers)
8918 struct frame *f;
8919 int x, y, down_p;
8920 unsigned int modifiers;
8921 {
8922 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
8923 struct window *w = XWINDOW (f->tool_bar_window);
8924 int hpos, vpos, prop_idx;
8925 struct glyph *glyph;
8926 Lisp_Object enabled_p;
8927
8928 /* If not on the highlighted tool-bar item, return. */
8929 frame_to_window_pixel_xy (w, &x, &y);
8930 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
8931 return;
8932
8933 /* If item is disabled, do nothing. */
8934 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
8935 if (NILP (enabled_p))
8936 return;
8937
8938 if (down_p)
8939 {
8940 /* Show item in pressed state. */
8941 show_mouse_face (dpyinfo, DRAW_IMAGE_SUNKEN);
8942 dpyinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
8943 last_tool_bar_item = prop_idx;
8944 }
8945 else
8946 {
8947 Lisp_Object key, frame;
8948 struct input_event event;
8949 EVENT_INIT (event);
8950
8951 /* Show item in released state. */
8952 show_mouse_face (dpyinfo, DRAW_IMAGE_RAISED);
8953 dpyinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
8954
8955 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
8956
8957 XSETFRAME (frame, f);
8958 event.kind = TOOL_BAR_EVENT;
8959 event.frame_or_window = frame;
8960 event.arg = frame;
8961 kbd_buffer_store_event (&event);
8962
8963 event.kind = TOOL_BAR_EVENT;
8964 event.frame_or_window = frame;
8965 event.arg = key;
8966 event.modifiers = modifiers;
8967 kbd_buffer_store_event (&event);
8968 last_tool_bar_item = -1;
8969 }
8970 }
8971
8972
8973 /* Possibly highlight a tool-bar item on frame F when mouse moves to
8974 tool-bar window-relative coordinates X/Y. Called from
8975 note_mouse_highlight. */
8976
8977 static void
8978 note_tool_bar_highlight (f, x, y)
8979 struct frame *f;
8980 int x, y;
8981 {
8982 Lisp_Object window = f->tool_bar_window;
8983 struct window *w = XWINDOW (window);
8984 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
8985 int hpos, vpos;
8986 struct glyph *glyph;
8987 struct glyph_row *row;
8988 int i;
8989 Lisp_Object enabled_p;
8990 int prop_idx;
8991 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
8992 int mouse_down_p, rc;
8993
8994 /* Function note_mouse_highlight is called with negative x(y
8995 values when mouse moves outside of the frame. */
8996 if (x <= 0 || y <= 0)
8997 {
8998 clear_mouse_face (dpyinfo);
8999 return;
9000 }
9001
9002 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
9003 if (rc < 0)
9004 {
9005 /* Not on tool-bar item. */
9006 clear_mouse_face (dpyinfo);
9007 return;
9008 }
9009 else if (rc == 0)
9010 /* On same tool-bar item as before. */
9011 goto set_help_echo;
9012
9013 clear_mouse_face (dpyinfo);
9014
9015 /* Mouse is down, but on different tool-bar item? */
9016 mouse_down_p = (dpyinfo->grabbed
9017 && f == last_mouse_frame
9018 && FRAME_LIVE_P (f));
9019 if (mouse_down_p
9020 && last_tool_bar_item != prop_idx)
9021 return;
9022
9023 dpyinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
9024 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
9025
9026 /* If tool-bar item is not enabled, don't highlight it. */
9027 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
9028 if (!NILP (enabled_p))
9029 {
9030 /* Compute the x-position of the glyph. In front and past the
9031 image is a space. We include this in the highlighted area. */
9032 row = MATRIX_ROW (w->current_matrix, vpos);
9033 for (i = x = 0; i < hpos; ++i)
9034 x += row->glyphs[TEXT_AREA][i].pixel_width;
9035
9036 /* Record this as the current active region. */
9037 dpyinfo->mouse_face_beg_col = hpos;
9038 dpyinfo->mouse_face_beg_row = vpos;
9039 dpyinfo->mouse_face_beg_x = x;
9040 dpyinfo->mouse_face_beg_y = row->y;
9041 dpyinfo->mouse_face_past_end = 0;
9042
9043 dpyinfo->mouse_face_end_col = hpos + 1;
9044 dpyinfo->mouse_face_end_row = vpos;
9045 dpyinfo->mouse_face_end_x = x + glyph->pixel_width;
9046 dpyinfo->mouse_face_end_y = row->y;
9047 dpyinfo->mouse_face_window = window;
9048 dpyinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
9049
9050 /* Display it as active. */
9051 show_mouse_face (dpyinfo, draw);
9052 dpyinfo->mouse_face_image_state = draw;
9053 }
9054
9055 set_help_echo:
9056
9057 /* Set help_echo_string to a help string to display for this tool-bar item.
9058 XTread_socket does the rest. */
9059 help_echo_object = help_echo_window = Qnil;
9060 help_echo_pos = -1;
9061 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
9062 if (NILP (help_echo_string))
9063 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
9064 }
9065
9066 #endif /* HAVE_WINDOW_SYSTEM */
9067
9068
9069 \f
9070 /************************************************************************
9071 Horizontal scrolling
9072 ************************************************************************/
9073
9074 static int hscroll_window_tree P_ ((Lisp_Object));
9075 static int hscroll_windows P_ ((Lisp_Object));
9076
9077 /* For all leaf windows in the window tree rooted at WINDOW, set their
9078 hscroll value so that PT is (i) visible in the window, and (ii) so
9079 that it is not within a certain margin at the window's left and
9080 right border. Value is non-zero if any window's hscroll has been
9081 changed. */
9082
9083 static int
9084 hscroll_window_tree (window)
9085 Lisp_Object window;
9086 {
9087 int hscrolled_p = 0;
9088 int hscroll_relative_p = FLOATP (Vhscroll_step);
9089 int hscroll_step_abs = 0;
9090 double hscroll_step_rel = 0;
9091
9092 if (hscroll_relative_p)
9093 {
9094 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
9095 if (hscroll_step_rel < 0)
9096 {
9097 hscroll_relative_p = 0;
9098 hscroll_step_abs = 0;
9099 }
9100 }
9101 else if (INTEGERP (Vhscroll_step))
9102 {
9103 hscroll_step_abs = XINT (Vhscroll_step);
9104 if (hscroll_step_abs < 0)
9105 hscroll_step_abs = 0;
9106 }
9107 else
9108 hscroll_step_abs = 0;
9109
9110 while (WINDOWP (window))
9111 {
9112 struct window *w = XWINDOW (window);
9113
9114 if (WINDOWP (w->hchild))
9115 hscrolled_p |= hscroll_window_tree (w->hchild);
9116 else if (WINDOWP (w->vchild))
9117 hscrolled_p |= hscroll_window_tree (w->vchild);
9118 else if (w->cursor.vpos >= 0)
9119 {
9120 int h_margin;
9121 int text_area_width;
9122 struct glyph_row *current_cursor_row
9123 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
9124 struct glyph_row *desired_cursor_row
9125 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
9126 struct glyph_row *cursor_row
9127 = (desired_cursor_row->enabled_p
9128 ? desired_cursor_row
9129 : current_cursor_row);
9130
9131 text_area_width = window_box_width (w, TEXT_AREA);
9132
9133 /* Scroll when cursor is inside this scroll margin. */
9134 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
9135
9136 if ((XFASTINT (w->hscroll)
9137 && w->cursor.x <= h_margin)
9138 || (cursor_row->enabled_p
9139 && cursor_row->truncated_on_right_p
9140 && (w->cursor.x >= text_area_width - h_margin)))
9141 {
9142 struct it it;
9143 int hscroll;
9144 struct buffer *saved_current_buffer;
9145 int pt;
9146 int wanted_x;
9147
9148 /* Find point in a display of infinite width. */
9149 saved_current_buffer = current_buffer;
9150 current_buffer = XBUFFER (w->buffer);
9151
9152 if (w == XWINDOW (selected_window))
9153 pt = BUF_PT (current_buffer);
9154 else
9155 {
9156 pt = marker_position (w->pointm);
9157 pt = max (BEGV, pt);
9158 pt = min (ZV, pt);
9159 }
9160
9161 /* Move iterator to pt starting at cursor_row->start in
9162 a line with infinite width. */
9163 init_to_row_start (&it, w, cursor_row);
9164 it.last_visible_x = INFINITY;
9165 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
9166 current_buffer = saved_current_buffer;
9167
9168 /* Position cursor in window. */
9169 if (!hscroll_relative_p && hscroll_step_abs == 0)
9170 hscroll = max (0, (it.current_x
9171 - (ITERATOR_AT_END_OF_LINE_P (&it)
9172 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
9173 : (text_area_width / 2))))
9174 / FRAME_COLUMN_WIDTH (it.f);
9175 else if (w->cursor.x >= text_area_width - h_margin)
9176 {
9177 if (hscroll_relative_p)
9178 wanted_x = text_area_width * (1 - hscroll_step_rel)
9179 - h_margin;
9180 else
9181 wanted_x = text_area_width
9182 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
9183 - h_margin;
9184 hscroll
9185 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
9186 }
9187 else
9188 {
9189 if (hscroll_relative_p)
9190 wanted_x = text_area_width * hscroll_step_rel
9191 + h_margin;
9192 else
9193 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
9194 + h_margin;
9195 hscroll
9196 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
9197 }
9198 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
9199
9200 /* Don't call Fset_window_hscroll if value hasn't
9201 changed because it will prevent redisplay
9202 optimizations. */
9203 if (XFASTINT (w->hscroll) != hscroll)
9204 {
9205 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
9206 w->hscroll = make_number (hscroll);
9207 hscrolled_p = 1;
9208 }
9209 }
9210 }
9211
9212 window = w->next;
9213 }
9214
9215 /* Value is non-zero if hscroll of any leaf window has been changed. */
9216 return hscrolled_p;
9217 }
9218
9219
9220 /* Set hscroll so that cursor is visible and not inside horizontal
9221 scroll margins for all windows in the tree rooted at WINDOW. See
9222 also hscroll_window_tree above. Value is non-zero if any window's
9223 hscroll has been changed. If it has, desired matrices on the frame
9224 of WINDOW are cleared. */
9225
9226 static int
9227 hscroll_windows (window)
9228 Lisp_Object window;
9229 {
9230 int hscrolled_p;
9231
9232 if (automatic_hscrolling_p)
9233 {
9234 hscrolled_p = hscroll_window_tree (window);
9235 if (hscrolled_p)
9236 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
9237 }
9238 else
9239 hscrolled_p = 0;
9240 return hscrolled_p;
9241 }
9242
9243
9244 \f
9245 /************************************************************************
9246 Redisplay
9247 ************************************************************************/
9248
9249 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
9250 to a non-zero value. This is sometimes handy to have in a debugger
9251 session. */
9252
9253 #if GLYPH_DEBUG
9254
9255 /* First and last unchanged row for try_window_id. */
9256
9257 int debug_first_unchanged_at_end_vpos;
9258 int debug_last_unchanged_at_beg_vpos;
9259
9260 /* Delta vpos and y. */
9261
9262 int debug_dvpos, debug_dy;
9263
9264 /* Delta in characters and bytes for try_window_id. */
9265
9266 int debug_delta, debug_delta_bytes;
9267
9268 /* Values of window_end_pos and window_end_vpos at the end of
9269 try_window_id. */
9270
9271 EMACS_INT debug_end_pos, debug_end_vpos;
9272
9273 /* Append a string to W->desired_matrix->method. FMT is a printf
9274 format string. A1...A9 are a supplement for a variable-length
9275 argument list. If trace_redisplay_p is non-zero also printf the
9276 resulting string to stderr. */
9277
9278 static void
9279 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
9280 struct window *w;
9281 char *fmt;
9282 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
9283 {
9284 char buffer[512];
9285 char *method = w->desired_matrix->method;
9286 int len = strlen (method);
9287 int size = sizeof w->desired_matrix->method;
9288 int remaining = size - len - 1;
9289
9290 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
9291 if (len && remaining)
9292 {
9293 method[len] = '|';
9294 --remaining, ++len;
9295 }
9296
9297 strncpy (method + len, buffer, remaining);
9298
9299 if (trace_redisplay_p)
9300 fprintf (stderr, "%p (%s): %s\n",
9301 w,
9302 ((BUFFERP (w->buffer)
9303 && STRINGP (XBUFFER (w->buffer)->name))
9304 ? (char *) SDATA (XBUFFER (w->buffer)->name)
9305 : "no buffer"),
9306 buffer);
9307 }
9308
9309 #endif /* GLYPH_DEBUG */
9310
9311
9312 /* Value is non-zero if all changes in window W, which displays
9313 current_buffer, are in the text between START and END. START is a
9314 buffer position, END is given as a distance from Z. Used in
9315 redisplay_internal for display optimization. */
9316
9317 static INLINE int
9318 text_outside_line_unchanged_p (w, start, end)
9319 struct window *w;
9320 int start, end;
9321 {
9322 int unchanged_p = 1;
9323
9324 /* If text or overlays have changed, see where. */
9325 if (XFASTINT (w->last_modified) < MODIFF
9326 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
9327 {
9328 /* Gap in the line? */
9329 if (GPT < start || Z - GPT < end)
9330 unchanged_p = 0;
9331
9332 /* Changes start in front of the line, or end after it? */
9333 if (unchanged_p
9334 && (BEG_UNCHANGED < start - 1
9335 || END_UNCHANGED < end))
9336 unchanged_p = 0;
9337
9338 /* If selective display, can't optimize if changes start at the
9339 beginning of the line. */
9340 if (unchanged_p
9341 && INTEGERP (current_buffer->selective_display)
9342 && XINT (current_buffer->selective_display) > 0
9343 && (BEG_UNCHANGED < start || GPT <= start))
9344 unchanged_p = 0;
9345
9346 /* If there are overlays at the start or end of the line, these
9347 may have overlay strings with newlines in them. A change at
9348 START, for instance, may actually concern the display of such
9349 overlay strings as well, and they are displayed on different
9350 lines. So, quickly rule out this case. (For the future, it
9351 might be desirable to implement something more telling than
9352 just BEG/END_UNCHANGED.) */
9353 if (unchanged_p)
9354 {
9355 if (BEG + BEG_UNCHANGED == start
9356 && overlay_touches_p (start))
9357 unchanged_p = 0;
9358 if (END_UNCHANGED == end
9359 && overlay_touches_p (Z - end))
9360 unchanged_p = 0;
9361 }
9362 }
9363
9364 return unchanged_p;
9365 }
9366
9367
9368 /* Do a frame update, taking possible shortcuts into account. This is
9369 the main external entry point for redisplay.
9370
9371 If the last redisplay displayed an echo area message and that message
9372 is no longer requested, we clear the echo area or bring back the
9373 mini-buffer if that is in use. */
9374
9375 void
9376 redisplay ()
9377 {
9378 redisplay_internal (0);
9379 }
9380
9381
9382 static Lisp_Object
9383 overlay_arrow_string_or_property (var, pbitmap)
9384 Lisp_Object var;
9385 int *pbitmap;
9386 {
9387 Lisp_Object pstr = Fget (var, Qoverlay_arrow_string);
9388 Lisp_Object bitmap;
9389
9390 if (pbitmap)
9391 {
9392 *pbitmap = 0;
9393 if (bitmap = Fget (var, Qoverlay_arrow_bitmap), INTEGERP (bitmap))
9394 *pbitmap = XINT (bitmap);
9395 }
9396
9397 if (!NILP (pstr))
9398 return pstr;
9399 return Voverlay_arrow_string;
9400 }
9401
9402 /* Return 1 if there are any overlay-arrows in current_buffer. */
9403 static int
9404 overlay_arrow_in_current_buffer_p ()
9405 {
9406 Lisp_Object vlist;
9407
9408 for (vlist = Voverlay_arrow_variable_list;
9409 CONSP (vlist);
9410 vlist = XCDR (vlist))
9411 {
9412 Lisp_Object var = XCAR (vlist);
9413 Lisp_Object val;
9414
9415 if (!SYMBOLP (var))
9416 continue;
9417 val = find_symbol_value (var);
9418 if (MARKERP (val)
9419 && current_buffer == XMARKER (val)->buffer)
9420 return 1;
9421 }
9422 return 0;
9423 }
9424
9425
9426 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
9427 has changed. */
9428
9429 static int
9430 overlay_arrows_changed_p ()
9431 {
9432 Lisp_Object vlist;
9433
9434 for (vlist = Voverlay_arrow_variable_list;
9435 CONSP (vlist);
9436 vlist = XCDR (vlist))
9437 {
9438 Lisp_Object var = XCAR (vlist);
9439 Lisp_Object val, pstr;
9440
9441 if (!SYMBOLP (var))
9442 continue;
9443 val = find_symbol_value (var);
9444 if (!MARKERP (val))
9445 continue;
9446 if (! EQ (COERCE_MARKER (val),
9447 Fget (var, Qlast_arrow_position))
9448 || ! (pstr = overlay_arrow_string_or_property (var, 0),
9449 EQ (pstr, Fget (var, Qlast_arrow_string))))
9450 return 1;
9451 }
9452 return 0;
9453 }
9454
9455 /* Mark overlay arrows to be updated on next redisplay. */
9456
9457 static void
9458 update_overlay_arrows (up_to_date)
9459 int up_to_date;
9460 {
9461 Lisp_Object vlist;
9462
9463 for (vlist = Voverlay_arrow_variable_list;
9464 CONSP (vlist);
9465 vlist = XCDR (vlist))
9466 {
9467 Lisp_Object var = XCAR (vlist);
9468
9469 if (!SYMBOLP (var))
9470 continue;
9471
9472 if (up_to_date > 0)
9473 {
9474 Lisp_Object val = find_symbol_value (var);
9475 Fput (var, Qlast_arrow_position,
9476 COERCE_MARKER (val));
9477 Fput (var, Qlast_arrow_string,
9478 overlay_arrow_string_or_property (var, 0));
9479 }
9480 else if (up_to_date < 0
9481 || !NILP (Fget (var, Qlast_arrow_position)))
9482 {
9483 Fput (var, Qlast_arrow_position, Qt);
9484 Fput (var, Qlast_arrow_string, Qt);
9485 }
9486 }
9487 }
9488
9489
9490 /* Return overlay arrow string at row, or nil. */
9491
9492 static Lisp_Object
9493 overlay_arrow_at_row (f, row, pbitmap)
9494 struct frame *f;
9495 struct glyph_row *row;
9496 int *pbitmap;
9497 {
9498 Lisp_Object vlist;
9499
9500 for (vlist = Voverlay_arrow_variable_list;
9501 CONSP (vlist);
9502 vlist = XCDR (vlist))
9503 {
9504 Lisp_Object var = XCAR (vlist);
9505 Lisp_Object val;
9506
9507 if (!SYMBOLP (var))
9508 continue;
9509
9510 val = find_symbol_value (var);
9511
9512 if (MARKERP (val)
9513 && current_buffer == XMARKER (val)->buffer
9514 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
9515 {
9516 val = overlay_arrow_string_or_property (var, pbitmap);
9517 if (FRAME_WINDOW_P (f))
9518 return Qt;
9519 else if (STRINGP (val))
9520 return val;
9521 break;
9522 }
9523 }
9524
9525 *pbitmap = 0;
9526 return Qnil;
9527 }
9528
9529 /* Return 1 if point moved out of or into a composition. Otherwise
9530 return 0. PREV_BUF and PREV_PT are the last point buffer and
9531 position. BUF and PT are the current point buffer and position. */
9532
9533 int
9534 check_point_in_composition (prev_buf, prev_pt, buf, pt)
9535 struct buffer *prev_buf, *buf;
9536 int prev_pt, pt;
9537 {
9538 int start, end;
9539 Lisp_Object prop;
9540 Lisp_Object buffer;
9541
9542 XSETBUFFER (buffer, buf);
9543 /* Check a composition at the last point if point moved within the
9544 same buffer. */
9545 if (prev_buf == buf)
9546 {
9547 if (prev_pt == pt)
9548 /* Point didn't move. */
9549 return 0;
9550
9551 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
9552 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
9553 && COMPOSITION_VALID_P (start, end, prop)
9554 && start < prev_pt && end > prev_pt)
9555 /* The last point was within the composition. Return 1 iff
9556 point moved out of the composition. */
9557 return (pt <= start || pt >= end);
9558 }
9559
9560 /* Check a composition at the current point. */
9561 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
9562 && find_composition (pt, -1, &start, &end, &prop, buffer)
9563 && COMPOSITION_VALID_P (start, end, prop)
9564 && start < pt && end > pt);
9565 }
9566
9567
9568 /* Reconsider the setting of B->clip_changed which is displayed
9569 in window W. */
9570
9571 static INLINE void
9572 reconsider_clip_changes (w, b)
9573 struct window *w;
9574 struct buffer *b;
9575 {
9576 if (b->clip_changed
9577 && !NILP (w->window_end_valid)
9578 && w->current_matrix->buffer == b
9579 && w->current_matrix->zv == BUF_ZV (b)
9580 && w->current_matrix->begv == BUF_BEGV (b))
9581 b->clip_changed = 0;
9582
9583 /* If display wasn't paused, and W is not a tool bar window, see if
9584 point has been moved into or out of a composition. In that case,
9585 we set b->clip_changed to 1 to force updating the screen. If
9586 b->clip_changed has already been set to 1, we can skip this
9587 check. */
9588 if (!b->clip_changed
9589 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
9590 {
9591 int pt;
9592
9593 if (w == XWINDOW (selected_window))
9594 pt = BUF_PT (current_buffer);
9595 else
9596 pt = marker_position (w->pointm);
9597
9598 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
9599 || pt != XINT (w->last_point))
9600 && check_point_in_composition (w->current_matrix->buffer,
9601 XINT (w->last_point),
9602 XBUFFER (w->buffer), pt))
9603 b->clip_changed = 1;
9604 }
9605 }
9606 \f
9607
9608 /* Select FRAME to forward the values of frame-local variables into C
9609 variables so that the redisplay routines can access those values
9610 directly. */
9611
9612 static void
9613 select_frame_for_redisplay (frame)
9614 Lisp_Object frame;
9615 {
9616 Lisp_Object tail, sym, val;
9617 Lisp_Object old = selected_frame;
9618
9619 selected_frame = frame;
9620
9621 for (tail = XFRAME (frame)->param_alist; CONSP (tail); tail = XCDR (tail))
9622 if (CONSP (XCAR (tail))
9623 && (sym = XCAR (XCAR (tail)),
9624 SYMBOLP (sym))
9625 && (sym = indirect_variable (sym),
9626 val = SYMBOL_VALUE (sym),
9627 (BUFFER_LOCAL_VALUEP (val)
9628 || SOME_BUFFER_LOCAL_VALUEP (val)))
9629 && XBUFFER_LOCAL_VALUE (val)->check_frame)
9630 Fsymbol_value (sym);
9631
9632 for (tail = XFRAME (old)->param_alist; CONSP (tail); tail = XCDR (tail))
9633 if (CONSP (XCAR (tail))
9634 && (sym = XCAR (XCAR (tail)),
9635 SYMBOLP (sym))
9636 && (sym = indirect_variable (sym),
9637 val = SYMBOL_VALUE (sym),
9638 (BUFFER_LOCAL_VALUEP (val)
9639 || SOME_BUFFER_LOCAL_VALUEP (val)))
9640 && XBUFFER_LOCAL_VALUE (val)->check_frame)
9641 Fsymbol_value (sym);
9642 }
9643
9644
9645 #define STOP_POLLING \
9646 do { if (! polling_stopped_here) stop_polling (); \
9647 polling_stopped_here = 1; } while (0)
9648
9649 #define RESUME_POLLING \
9650 do { if (polling_stopped_here) start_polling (); \
9651 polling_stopped_here = 0; } while (0)
9652
9653
9654 /* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
9655 response to any user action; therefore, we should preserve the echo
9656 area. (Actually, our caller does that job.) Perhaps in the future
9657 avoid recentering windows if it is not necessary; currently that
9658 causes some problems. */
9659
9660 static void
9661 redisplay_internal (preserve_echo_area)
9662 int preserve_echo_area;
9663 {
9664 struct window *w = XWINDOW (selected_window);
9665 struct frame *f = XFRAME (w->frame);
9666 int pause;
9667 int must_finish = 0;
9668 struct text_pos tlbufpos, tlendpos;
9669 int number_of_visible_frames;
9670 int count;
9671 struct frame *sf = SELECTED_FRAME ();
9672 int polling_stopped_here = 0;
9673
9674 /* Non-zero means redisplay has to consider all windows on all
9675 frames. Zero means, only selected_window is considered. */
9676 int consider_all_windows_p;
9677
9678 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
9679
9680 /* No redisplay if running in batch mode or frame is not yet fully
9681 initialized, or redisplay is explicitly turned off by setting
9682 Vinhibit_redisplay. */
9683 if (noninteractive
9684 || !NILP (Vinhibit_redisplay)
9685 || !f->glyphs_initialized_p)
9686 return;
9687
9688 /* The flag redisplay_performed_directly_p is set by
9689 direct_output_for_insert when it already did the whole screen
9690 update necessary. */
9691 if (redisplay_performed_directly_p)
9692 {
9693 redisplay_performed_directly_p = 0;
9694 if (!hscroll_windows (selected_window))
9695 return;
9696 }
9697
9698 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
9699 if (popup_activated ())
9700 return;
9701 #endif
9702
9703 /* I don't think this happens but let's be paranoid. */
9704 if (redisplaying_p)
9705 return;
9706
9707 /* Record a function that resets redisplaying_p to its old value
9708 when we leave this function. */
9709 count = SPECPDL_INDEX ();
9710 record_unwind_protect (unwind_redisplay,
9711 Fcons (make_number (redisplaying_p), selected_frame));
9712 ++redisplaying_p;
9713 specbind (Qinhibit_free_realized_faces, Qnil);
9714
9715 retry:
9716 pause = 0;
9717 reconsider_clip_changes (w, current_buffer);
9718
9719 /* If new fonts have been loaded that make a glyph matrix adjustment
9720 necessary, do it. */
9721 if (fonts_changed_p)
9722 {
9723 adjust_glyphs (NULL);
9724 ++windows_or_buffers_changed;
9725 fonts_changed_p = 0;
9726 }
9727
9728 /* If face_change_count is non-zero, init_iterator will free all
9729 realized faces, which includes the faces referenced from current
9730 matrices. So, we can't reuse current matrices in this case. */
9731 if (face_change_count)
9732 ++windows_or_buffers_changed;
9733
9734 if (FRAME_TERMCAP_P (sf)
9735 && FRAME_TTY (sf)->previous_terminal_frame != sf)
9736 {
9737 /* Since frames on a single ASCII terminal share the same
9738 display area, displaying a different frame means redisplay
9739 the whole thing. */
9740 windows_or_buffers_changed++;
9741 SET_FRAME_GARBAGED (sf);
9742 FRAME_TTY (sf)->previous_terminal_frame = sf;
9743 }
9744
9745 /* Set the visible flags for all frames. Do this before checking
9746 for resized or garbaged frames; they want to know if their frames
9747 are visible. See the comment in frame.h for
9748 FRAME_SAMPLE_VISIBILITY. */
9749 {
9750 Lisp_Object tail, frame;
9751
9752 number_of_visible_frames = 0;
9753
9754 FOR_EACH_FRAME (tail, frame)
9755 {
9756 struct frame *f = XFRAME (frame);
9757
9758 FRAME_SAMPLE_VISIBILITY (f);
9759 if (FRAME_VISIBLE_P (f))
9760 ++number_of_visible_frames;
9761 clear_desired_matrices (f);
9762 }
9763 }
9764
9765
9766 /* Notice any pending interrupt request to change frame size. */
9767 do_pending_window_change (1);
9768
9769 /* Clear frames marked as garbaged. */
9770 if (frame_garbaged)
9771 clear_garbaged_frames ();
9772
9773 /* Build menubar and tool-bar items. */
9774 prepare_menu_bars ();
9775
9776 if (windows_or_buffers_changed)
9777 update_mode_lines++;
9778
9779 /* Detect case that we need to write or remove a star in the mode line. */
9780 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
9781 {
9782 w->update_mode_line = Qt;
9783 if (buffer_shared > 1)
9784 update_mode_lines++;
9785 }
9786
9787 /* If %c is in the mode line, update it if needed. */
9788 if (!NILP (w->column_number_displayed)
9789 /* This alternative quickly identifies a common case
9790 where no change is needed. */
9791 && !(PT == XFASTINT (w->last_point)
9792 && XFASTINT (w->last_modified) >= MODIFF
9793 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
9794 && (XFASTINT (w->column_number_displayed)
9795 != (int) current_column ())) /* iftc */
9796 w->update_mode_line = Qt;
9797
9798 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
9799
9800 /* The variable buffer_shared is set in redisplay_window and
9801 indicates that we redisplay a buffer in different windows. See
9802 there. */
9803 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
9804 || cursor_type_changed);
9805
9806 /* If specs for an arrow have changed, do thorough redisplay
9807 to ensure we remove any arrow that should no longer exist. */
9808 if (overlay_arrows_changed_p ())
9809 consider_all_windows_p = windows_or_buffers_changed = 1;
9810
9811 /* Normally the message* functions will have already displayed and
9812 updated the echo area, but the frame may have been trashed, or
9813 the update may have been preempted, so display the echo area
9814 again here. Checking message_cleared_p captures the case that
9815 the echo area should be cleared. */
9816 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
9817 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
9818 || (message_cleared_p
9819 && minibuf_level == 0
9820 /* If the mini-window is currently selected, this means the
9821 echo-area doesn't show through. */
9822 && !MINI_WINDOW_P (XWINDOW (selected_window))))
9823 {
9824 int window_height_changed_p = echo_area_display (0);
9825 must_finish = 1;
9826
9827 /* If we don't display the current message, don't clear the
9828 message_cleared_p flag, because, if we did, we wouldn't clear
9829 the echo area in the next redisplay which doesn't preserve
9830 the echo area. */
9831 if (!display_last_displayed_message_p)
9832 message_cleared_p = 0;
9833
9834 if (fonts_changed_p)
9835 goto retry;
9836 else if (window_height_changed_p)
9837 {
9838 consider_all_windows_p = 1;
9839 ++update_mode_lines;
9840 ++windows_or_buffers_changed;
9841
9842 /* If window configuration was changed, frames may have been
9843 marked garbaged. Clear them or we will experience
9844 surprises wrt scrolling. */
9845 if (frame_garbaged)
9846 clear_garbaged_frames ();
9847 }
9848 }
9849 else if (EQ (selected_window, minibuf_window)
9850 && (current_buffer->clip_changed
9851 || XFASTINT (w->last_modified) < MODIFF
9852 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
9853 && resize_mini_window (w, 0))
9854 {
9855 /* Resized active mini-window to fit the size of what it is
9856 showing if its contents might have changed. */
9857 must_finish = 1;
9858 consider_all_windows_p = 1;
9859 ++windows_or_buffers_changed;
9860 ++update_mode_lines;
9861
9862 /* If window configuration was changed, frames may have been
9863 marked garbaged. Clear them or we will experience
9864 surprises wrt scrolling. */
9865 if (frame_garbaged)
9866 clear_garbaged_frames ();
9867 }
9868
9869
9870 /* If showing the region, and mark has changed, we must redisplay
9871 the whole window. The assignment to this_line_start_pos prevents
9872 the optimization directly below this if-statement. */
9873 if (((!NILP (Vtransient_mark_mode)
9874 && !NILP (XBUFFER (w->buffer)->mark_active))
9875 != !NILP (w->region_showing))
9876 || (!NILP (w->region_showing)
9877 && !EQ (w->region_showing,
9878 Fmarker_position (XBUFFER (w->buffer)->mark))))
9879 CHARPOS (this_line_start_pos) = 0;
9880
9881 /* Optimize the case that only the line containing the cursor in the
9882 selected window has changed. Variables starting with this_ are
9883 set in display_line and record information about the line
9884 containing the cursor. */
9885 tlbufpos = this_line_start_pos;
9886 tlendpos = this_line_end_pos;
9887 if (!consider_all_windows_p
9888 && CHARPOS (tlbufpos) > 0
9889 && NILP (w->update_mode_line)
9890 && !current_buffer->clip_changed
9891 && !current_buffer->prevent_redisplay_optimizations_p
9892 && FRAME_VISIBLE_P (XFRAME (w->frame))
9893 && !FRAME_OBSCURED_P (XFRAME (w->frame))
9894 /* Make sure recorded data applies to current buffer, etc. */
9895 && this_line_buffer == current_buffer
9896 && current_buffer == XBUFFER (w->buffer)
9897 && NILP (w->force_start)
9898 && NILP (w->optional_new_start)
9899 /* Point must be on the line that we have info recorded about. */
9900 && PT >= CHARPOS (tlbufpos)
9901 && PT <= Z - CHARPOS (tlendpos)
9902 /* All text outside that line, including its final newline,
9903 must be unchanged */
9904 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
9905 CHARPOS (tlendpos)))
9906 {
9907 if (CHARPOS (tlbufpos) > BEGV
9908 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
9909 && (CHARPOS (tlbufpos) == ZV
9910 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
9911 /* Former continuation line has disappeared by becoming empty */
9912 goto cancel;
9913 else if (XFASTINT (w->last_modified) < MODIFF
9914 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
9915 || MINI_WINDOW_P (w))
9916 {
9917 /* We have to handle the case of continuation around a
9918 wide-column character (See the comment in indent.c around
9919 line 885).
9920
9921 For instance, in the following case:
9922
9923 -------- Insert --------
9924 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
9925 J_I_ ==> J_I_ `^^' are cursors.
9926 ^^ ^^
9927 -------- --------
9928
9929 As we have to redraw the line above, we should goto cancel. */
9930
9931 struct it it;
9932 int line_height_before = this_line_pixel_height;
9933
9934 /* Note that start_display will handle the case that the
9935 line starting at tlbufpos is a continuation lines. */
9936 start_display (&it, w, tlbufpos);
9937
9938 /* Implementation note: It this still necessary? */
9939 if (it.current_x != this_line_start_x)
9940 goto cancel;
9941
9942 TRACE ((stderr, "trying display optimization 1\n"));
9943 w->cursor.vpos = -1;
9944 overlay_arrow_seen = 0;
9945 it.vpos = this_line_vpos;
9946 it.current_y = this_line_y;
9947 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
9948 display_line (&it);
9949
9950 /* If line contains point, is not continued,
9951 and ends at same distance from eob as before, we win */
9952 if (w->cursor.vpos >= 0
9953 /* Line is not continued, otherwise this_line_start_pos
9954 would have been set to 0 in display_line. */
9955 && CHARPOS (this_line_start_pos)
9956 /* Line ends as before. */
9957 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
9958 /* Line has same height as before. Otherwise other lines
9959 would have to be shifted up or down. */
9960 && this_line_pixel_height == line_height_before)
9961 {
9962 /* If this is not the window's last line, we must adjust
9963 the charstarts of the lines below. */
9964 if (it.current_y < it.last_visible_y)
9965 {
9966 struct glyph_row *row
9967 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
9968 int delta, delta_bytes;
9969
9970 if (Z - CHARPOS (tlendpos) == ZV)
9971 {
9972 /* This line ends at end of (accessible part of)
9973 buffer. There is no newline to count. */
9974 delta = (Z
9975 - CHARPOS (tlendpos)
9976 - MATRIX_ROW_START_CHARPOS (row));
9977 delta_bytes = (Z_BYTE
9978 - BYTEPOS (tlendpos)
9979 - MATRIX_ROW_START_BYTEPOS (row));
9980 }
9981 else
9982 {
9983 /* This line ends in a newline. Must take
9984 account of the newline and the rest of the
9985 text that follows. */
9986 delta = (Z
9987 - CHARPOS (tlendpos)
9988 - MATRIX_ROW_START_CHARPOS (row));
9989 delta_bytes = (Z_BYTE
9990 - BYTEPOS (tlendpos)
9991 - MATRIX_ROW_START_BYTEPOS (row));
9992 }
9993
9994 increment_matrix_positions (w->current_matrix,
9995 this_line_vpos + 1,
9996 w->current_matrix->nrows,
9997 delta, delta_bytes);
9998 }
9999
10000 /* If this row displays text now but previously didn't,
10001 or vice versa, w->window_end_vpos may have to be
10002 adjusted. */
10003 if ((it.glyph_row - 1)->displays_text_p)
10004 {
10005 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
10006 XSETINT (w->window_end_vpos, this_line_vpos);
10007 }
10008 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
10009 && this_line_vpos > 0)
10010 XSETINT (w->window_end_vpos, this_line_vpos - 1);
10011 w->window_end_valid = Qnil;
10012
10013 /* Update hint: No need to try to scroll in update_window. */
10014 w->desired_matrix->no_scrolling_p = 1;
10015
10016 #if GLYPH_DEBUG
10017 *w->desired_matrix->method = 0;
10018 debug_method_add (w, "optimization 1");
10019 #endif
10020 #ifdef HAVE_WINDOW_SYSTEM
10021 update_window_fringes (w, 0);
10022 #endif
10023 goto update;
10024 }
10025 else
10026 goto cancel;
10027 }
10028 else if (/* Cursor position hasn't changed. */
10029 PT == XFASTINT (w->last_point)
10030 /* Make sure the cursor was last displayed
10031 in this window. Otherwise we have to reposition it. */
10032 && 0 <= w->cursor.vpos
10033 && WINDOW_TOTAL_LINES (w) > w->cursor.vpos)
10034 {
10035 if (!must_finish)
10036 {
10037 do_pending_window_change (1);
10038
10039 /* We used to always goto end_of_redisplay here, but this
10040 isn't enough if we have a blinking cursor. */
10041 if (w->cursor_off_p == w->last_cursor_off_p)
10042 goto end_of_redisplay;
10043 }
10044 goto update;
10045 }
10046 /* If highlighting the region, or if the cursor is in the echo area,
10047 then we can't just move the cursor. */
10048 else if (! (!NILP (Vtransient_mark_mode)
10049 && !NILP (current_buffer->mark_active))
10050 && (EQ (selected_window, current_buffer->last_selected_window)
10051 || highlight_nonselected_windows)
10052 && NILP (w->region_showing)
10053 && NILP (Vshow_trailing_whitespace)
10054 && !cursor_in_echo_area)
10055 {
10056 struct it it;
10057 struct glyph_row *row;
10058
10059 /* Skip from tlbufpos to PT and see where it is. Note that
10060 PT may be in invisible text. If so, we will end at the
10061 next visible position. */
10062 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
10063 NULL, DEFAULT_FACE_ID);
10064 it.current_x = this_line_start_x;
10065 it.current_y = this_line_y;
10066 it.vpos = this_line_vpos;
10067
10068 /* The call to move_it_to stops in front of PT, but
10069 moves over before-strings. */
10070 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
10071
10072 if (it.vpos == this_line_vpos
10073 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
10074 row->enabled_p))
10075 {
10076 xassert (this_line_vpos == it.vpos);
10077 xassert (this_line_y == it.current_y);
10078 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
10079 #if GLYPH_DEBUG
10080 *w->desired_matrix->method = 0;
10081 debug_method_add (w, "optimization 3");
10082 #endif
10083 goto update;
10084 }
10085 else
10086 goto cancel;
10087 }
10088
10089 cancel:
10090 /* Text changed drastically or point moved off of line. */
10091 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
10092 }
10093
10094 CHARPOS (this_line_start_pos) = 0;
10095 consider_all_windows_p |= buffer_shared > 1;
10096 ++clear_face_cache_count;
10097
10098
10099 /* Build desired matrices, and update the display. If
10100 consider_all_windows_p is non-zero, do it for all windows on all
10101 frames. Otherwise do it for selected_window, only. */
10102
10103 if (consider_all_windows_p)
10104 {
10105 Lisp_Object tail, frame;
10106 int i, n = 0, size = 50;
10107 struct frame **updated
10108 = (struct frame **) alloca (size * sizeof *updated);
10109
10110 /* Clear the face cache eventually. */
10111 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
10112 {
10113 clear_face_cache (0);
10114 clear_face_cache_count = 0;
10115 }
10116
10117 /* Recompute # windows showing selected buffer. This will be
10118 incremented each time such a window is displayed. */
10119 buffer_shared = 0;
10120
10121 FOR_EACH_FRAME (tail, frame)
10122 {
10123 struct frame *f = XFRAME (frame);
10124
10125 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
10126 {
10127 if (! EQ (frame, selected_frame))
10128 /* Select the frame, for the sake of frame-local
10129 variables. */
10130 select_frame_for_redisplay (frame);
10131
10132 #ifdef HAVE_WINDOW_SYSTEM
10133 if (clear_face_cache_count % 50 == 0
10134 && FRAME_WINDOW_P (f))
10135 clear_image_cache (f, 0);
10136 #endif /* HAVE_WINDOW_SYSTEM */
10137
10138 /* Mark all the scroll bars to be removed; we'll redeem
10139 the ones we want when we redisplay their windows. */
10140 if (FRAME_DISPLAY (f)->condemn_scroll_bars_hook)
10141 FRAME_DISPLAY (f)->condemn_scroll_bars_hook (f);
10142
10143 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
10144 redisplay_windows (FRAME_ROOT_WINDOW (f));
10145
10146 /* Any scroll bars which redisplay_windows should have
10147 nuked should now go away. */
10148 if (FRAME_DISPLAY (f)->judge_scroll_bars_hook)
10149 FRAME_DISPLAY (f)->judge_scroll_bars_hook (f);
10150
10151 /* If fonts changed, display again. */
10152 /* ??? rms: I suspect it is a mistake to jump all the way
10153 back to retry here. It should just retry this frame. */
10154 if (fonts_changed_p)
10155 goto retry;
10156
10157 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
10158 {
10159 /* See if we have to hscroll. */
10160 if (hscroll_windows (f->root_window))
10161 goto retry;
10162
10163 /* Prevent various kinds of signals during display
10164 update. stdio is not robust about handling
10165 signals, which can cause an apparent I/O
10166 error. */
10167 if (interrupt_input)
10168 unrequest_sigio ();
10169 STOP_POLLING;
10170
10171 /* Update the display. */
10172 set_window_update_flags (XWINDOW (f->root_window), 1);
10173 pause |= update_frame (f, 0, 0);
10174 #if 0 /* Exiting the loop can leave the wrong value for buffer_shared. */
10175 if (pause)
10176 break;
10177 #endif
10178
10179 if (n == size)
10180 {
10181 int nbytes = size * sizeof *updated;
10182 struct frame **p = (struct frame **) alloca (2 * nbytes);
10183 bcopy (updated, p, nbytes);
10184 size *= 2;
10185 }
10186
10187 updated[n++] = f;
10188 }
10189 }
10190 }
10191
10192 if (!pause)
10193 {
10194 /* Do the mark_window_display_accurate after all windows have
10195 been redisplayed because this call resets flags in buffers
10196 which are needed for proper redisplay. */
10197 for (i = 0; i < n; ++i)
10198 {
10199 struct frame *f = updated[i];
10200 mark_window_display_accurate (f->root_window, 1);
10201 if (FRAME_DISPLAY (f)->frame_up_to_date_hook)
10202 FRAME_DISPLAY (f)->frame_up_to_date_hook (f);
10203 }
10204 }
10205 }
10206 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
10207 {
10208 Lisp_Object mini_window;
10209 struct frame *mini_frame;
10210
10211 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
10212 /* Use list_of_error, not Qerror, so that
10213 we catch only errors and don't run the debugger. */
10214 internal_condition_case_1 (redisplay_window_1, selected_window,
10215 list_of_error,
10216 redisplay_window_error);
10217
10218 /* Compare desired and current matrices, perform output. */
10219
10220 update:
10221 /* If fonts changed, display again. */
10222 if (fonts_changed_p)
10223 goto retry;
10224
10225 /* Prevent various kinds of signals during display update.
10226 stdio is not robust about handling signals,
10227 which can cause an apparent I/O error. */
10228 if (interrupt_input)
10229 unrequest_sigio ();
10230 STOP_POLLING;
10231
10232 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
10233 {
10234 if (hscroll_windows (selected_window))
10235 goto retry;
10236
10237 XWINDOW (selected_window)->must_be_updated_p = 1;
10238 pause = update_frame (sf, 0, 0);
10239 }
10240
10241 /* We may have called echo_area_display at the top of this
10242 function. If the echo area is on another frame, that may
10243 have put text on a frame other than the selected one, so the
10244 above call to update_frame would not have caught it. Catch
10245 it here. */
10246 mini_window = FRAME_MINIBUF_WINDOW (sf);
10247 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
10248
10249 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
10250 {
10251 XWINDOW (mini_window)->must_be_updated_p = 1;
10252 pause |= update_frame (mini_frame, 0, 0);
10253 if (!pause && hscroll_windows (mini_window))
10254 goto retry;
10255 }
10256 }
10257
10258 /* If display was paused because of pending input, make sure we do a
10259 thorough update the next time. */
10260 if (pause)
10261 {
10262 /* Prevent the optimization at the beginning of
10263 redisplay_internal that tries a single-line update of the
10264 line containing the cursor in the selected window. */
10265 CHARPOS (this_line_start_pos) = 0;
10266
10267 /* Let the overlay arrow be updated the next time. */
10268 update_overlay_arrows (0);
10269
10270 /* If we pause after scrolling, some rows in the current
10271 matrices of some windows are not valid. */
10272 if (!WINDOW_FULL_WIDTH_P (w)
10273 && !FRAME_WINDOW_P (XFRAME (w->frame)))
10274 update_mode_lines = 1;
10275 }
10276 else
10277 {
10278 if (!consider_all_windows_p)
10279 {
10280 /* This has already been done above if
10281 consider_all_windows_p is set. */
10282 mark_window_display_accurate_1 (w, 1);
10283
10284 /* Say overlay arrows are up to date. */
10285 update_overlay_arrows (1);
10286
10287 if (FRAME_DISPLAY (sf)->frame_up_to_date_hook != 0)
10288 FRAME_DISPLAY (sf)->frame_up_to_date_hook (sf);
10289 }
10290
10291 update_mode_lines = 0;
10292 windows_or_buffers_changed = 0;
10293 cursor_type_changed = 0;
10294 }
10295
10296 /* Start SIGIO interrupts coming again. Having them off during the
10297 code above makes it less likely one will discard output, but not
10298 impossible, since there might be stuff in the system buffer here.
10299 But it is much hairier to try to do anything about that. */
10300 if (interrupt_input)
10301 request_sigio ();
10302 RESUME_POLLING;
10303
10304 /* If a frame has become visible which was not before, redisplay
10305 again, so that we display it. Expose events for such a frame
10306 (which it gets when becoming visible) don't call the parts of
10307 redisplay constructing glyphs, so simply exposing a frame won't
10308 display anything in this case. So, we have to display these
10309 frames here explicitly. */
10310 if (!pause)
10311 {
10312 Lisp_Object tail, frame;
10313 int new_count = 0;
10314
10315 FOR_EACH_FRAME (tail, frame)
10316 {
10317 int this_is_visible = 0;
10318
10319 if (XFRAME (frame)->visible)
10320 this_is_visible = 1;
10321 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
10322 if (XFRAME (frame)->visible)
10323 this_is_visible = 1;
10324
10325 if (this_is_visible)
10326 new_count++;
10327 }
10328
10329 if (new_count != number_of_visible_frames)
10330 windows_or_buffers_changed++;
10331 }
10332
10333 /* Change frame size now if a change is pending. */
10334 do_pending_window_change (1);
10335
10336 /* If we just did a pending size change, or have additional
10337 visible frames, redisplay again. */
10338 if (windows_or_buffers_changed && !pause)
10339 goto retry;
10340
10341 end_of_redisplay:
10342 unbind_to (count, Qnil);
10343 RESUME_POLLING;
10344 }
10345
10346
10347 /* Redisplay, but leave alone any recent echo area message unless
10348 another message has been requested in its place.
10349
10350 This is useful in situations where you need to redisplay but no
10351 user action has occurred, making it inappropriate for the message
10352 area to be cleared. See tracking_off and
10353 wait_reading_process_input for examples of these situations.
10354
10355 FROM_WHERE is an integer saying from where this function was
10356 called. This is useful for debugging. */
10357
10358 void
10359 redisplay_preserve_echo_area (from_where)
10360 int from_where;
10361 {
10362 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
10363
10364 if (!NILP (echo_area_buffer[1]))
10365 {
10366 /* We have a previously displayed message, but no current
10367 message. Redisplay the previous message. */
10368 display_last_displayed_message_p = 1;
10369 redisplay_internal (1);
10370 display_last_displayed_message_p = 0;
10371 }
10372 else
10373 redisplay_internal (1);
10374 }
10375
10376
10377 /* Function registered with record_unwind_protect in
10378 redisplay_internal. Reset redisplaying_p to the value it had
10379 before redisplay_internal was called, and clear
10380 prevent_freeing_realized_faces_p. It also selects the previously
10381 selected frame. */
10382
10383 static Lisp_Object
10384 unwind_redisplay (val)
10385 Lisp_Object val;
10386 {
10387 Lisp_Object old_redisplaying_p, old_frame;
10388
10389 old_redisplaying_p = XCAR (val);
10390 redisplaying_p = XFASTINT (old_redisplaying_p);
10391 old_frame = XCDR (val);
10392 if (! EQ (old_frame, selected_frame))
10393 select_frame_for_redisplay (old_frame);
10394 return Qnil;
10395 }
10396
10397
10398 /* Mark the display of window W as accurate or inaccurate. If
10399 ACCURATE_P is non-zero mark display of W as accurate. If
10400 ACCURATE_P is zero, arrange for W to be redisplayed the next time
10401 redisplay_internal is called. */
10402
10403 static void
10404 mark_window_display_accurate_1 (w, accurate_p)
10405 struct window *w;
10406 int accurate_p;
10407 {
10408 if (BUFFERP (w->buffer))
10409 {
10410 struct buffer *b = XBUFFER (w->buffer);
10411
10412 w->last_modified
10413 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
10414 w->last_overlay_modified
10415 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
10416 w->last_had_star
10417 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
10418
10419 if (accurate_p)
10420 {
10421 b->clip_changed = 0;
10422 b->prevent_redisplay_optimizations_p = 0;
10423
10424 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
10425 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
10426 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
10427 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
10428
10429 w->current_matrix->buffer = b;
10430 w->current_matrix->begv = BUF_BEGV (b);
10431 w->current_matrix->zv = BUF_ZV (b);
10432
10433 w->last_cursor = w->cursor;
10434 w->last_cursor_off_p = w->cursor_off_p;
10435
10436 if (w == XWINDOW (selected_window))
10437 w->last_point = make_number (BUF_PT (b));
10438 else
10439 w->last_point = make_number (XMARKER (w->pointm)->charpos);
10440 }
10441 }
10442
10443 if (accurate_p)
10444 {
10445 w->window_end_valid = w->buffer;
10446 #if 0 /* This is incorrect with variable-height lines. */
10447 xassert (XINT (w->window_end_vpos)
10448 < (WINDOW_TOTAL_LINES (w)
10449 - (WINDOW_WANTS_MODELINE_P (w) ? 1 : 0)));
10450 #endif
10451 w->update_mode_line = Qnil;
10452 }
10453 }
10454
10455
10456 /* Mark the display of windows in the window tree rooted at WINDOW as
10457 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
10458 windows as accurate. If ACCURATE_P is zero, arrange for windows to
10459 be redisplayed the next time redisplay_internal is called. */
10460
10461 void
10462 mark_window_display_accurate (window, accurate_p)
10463 Lisp_Object window;
10464 int accurate_p;
10465 {
10466 struct window *w;
10467
10468 for (; !NILP (window); window = w->next)
10469 {
10470 w = XWINDOW (window);
10471 mark_window_display_accurate_1 (w, accurate_p);
10472
10473 if (!NILP (w->vchild))
10474 mark_window_display_accurate (w->vchild, accurate_p);
10475 if (!NILP (w->hchild))
10476 mark_window_display_accurate (w->hchild, accurate_p);
10477 }
10478
10479 if (accurate_p)
10480 {
10481 update_overlay_arrows (1);
10482 }
10483 else
10484 {
10485 /* Force a thorough redisplay the next time by setting
10486 last_arrow_position and last_arrow_string to t, which is
10487 unequal to any useful value of Voverlay_arrow_... */
10488 update_overlay_arrows (-1);
10489 }
10490 }
10491
10492
10493 /* Return value in display table DP (Lisp_Char_Table *) for character
10494 C. Since a display table doesn't have any parent, we don't have to
10495 follow parent. Do not call this function directly but use the
10496 macro DISP_CHAR_VECTOR. */
10497
10498 Lisp_Object
10499 disp_char_vector (dp, c)
10500 struct Lisp_Char_Table *dp;
10501 int c;
10502 {
10503 int code[4], i;
10504 Lisp_Object val;
10505
10506 if (SINGLE_BYTE_CHAR_P (c))
10507 return (dp->contents[c]);
10508
10509 SPLIT_CHAR (c, code[0], code[1], code[2]);
10510 if (code[1] < 32)
10511 code[1] = -1;
10512 else if (code[2] < 32)
10513 code[2] = -1;
10514
10515 /* Here, the possible range of code[0] (== charset ID) is
10516 128..max_charset. Since the top level char table contains data
10517 for multibyte characters after 256th element, we must increment
10518 code[0] by 128 to get a correct index. */
10519 code[0] += 128;
10520 code[3] = -1; /* anchor */
10521
10522 for (i = 0; code[i] >= 0; i++, dp = XCHAR_TABLE (val))
10523 {
10524 val = dp->contents[code[i]];
10525 if (!SUB_CHAR_TABLE_P (val))
10526 return (NILP (val) ? dp->defalt : val);
10527 }
10528
10529 /* Here, val is a sub char table. We return the default value of
10530 it. */
10531 return (dp->defalt);
10532 }
10533
10534
10535 \f
10536 /***********************************************************************
10537 Window Redisplay
10538 ***********************************************************************/
10539
10540 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
10541
10542 static void
10543 redisplay_windows (window)
10544 Lisp_Object window;
10545 {
10546 while (!NILP (window))
10547 {
10548 struct window *w = XWINDOW (window);
10549
10550 if (!NILP (w->hchild))
10551 redisplay_windows (w->hchild);
10552 else if (!NILP (w->vchild))
10553 redisplay_windows (w->vchild);
10554 else
10555 {
10556 displayed_buffer = XBUFFER (w->buffer);
10557 /* Use list_of_error, not Qerror, so that
10558 we catch only errors and don't run the debugger. */
10559 internal_condition_case_1 (redisplay_window_0, window,
10560 list_of_error,
10561 redisplay_window_error);
10562 }
10563
10564 window = w->next;
10565 }
10566 }
10567
10568 static Lisp_Object
10569 redisplay_window_error ()
10570 {
10571 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
10572 return Qnil;
10573 }
10574
10575 static Lisp_Object
10576 redisplay_window_0 (window)
10577 Lisp_Object window;
10578 {
10579 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
10580 redisplay_window (window, 0);
10581 return Qnil;
10582 }
10583
10584 static Lisp_Object
10585 redisplay_window_1 (window)
10586 Lisp_Object window;
10587 {
10588 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
10589 redisplay_window (window, 1);
10590 return Qnil;
10591 }
10592 \f
10593
10594 /* Increment GLYPH until it reaches END or CONDITION fails while
10595 adding (GLYPH)->pixel_width to X. */
10596
10597 #define SKIP_GLYPHS(glyph, end, x, condition) \
10598 do \
10599 { \
10600 (x) += (glyph)->pixel_width; \
10601 ++(glyph); \
10602 } \
10603 while ((glyph) < (end) && (condition))
10604
10605
10606 /* Set cursor position of W. PT is assumed to be displayed in ROW.
10607 DELTA is the number of bytes by which positions recorded in ROW
10608 differ from current buffer positions. */
10609
10610 void
10611 set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
10612 struct window *w;
10613 struct glyph_row *row;
10614 struct glyph_matrix *matrix;
10615 int delta, delta_bytes, dy, dvpos;
10616 {
10617 struct glyph *glyph = row->glyphs[TEXT_AREA];
10618 struct glyph *end = glyph + row->used[TEXT_AREA];
10619 /* The first glyph that starts a sequence of glyphs from string. */
10620 struct glyph *string_start;
10621 /* The X coordinate of string_start. */
10622 int string_start_x;
10623 /* The last known character position. */
10624 int last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
10625 /* The last known character position before string_start. */
10626 int string_before_pos;
10627 int x = row->x;
10628 int pt_old = PT - delta;
10629
10630 /* Skip over glyphs not having an object at the start of the row.
10631 These are special glyphs like truncation marks on terminal
10632 frames. */
10633 if (row->displays_text_p)
10634 while (glyph < end
10635 && INTEGERP (glyph->object)
10636 && glyph->charpos < 0)
10637 {
10638 x += glyph->pixel_width;
10639 ++glyph;
10640 }
10641
10642 string_start = NULL;
10643 while (glyph < end
10644 && !INTEGERP (glyph->object)
10645 && (!BUFFERP (glyph->object)
10646 || (last_pos = glyph->charpos) < pt_old))
10647 {
10648 if (! STRINGP (glyph->object))
10649 {
10650 string_start = NULL;
10651 x += glyph->pixel_width;
10652 ++glyph;
10653 }
10654 else
10655 {
10656 string_before_pos = last_pos;
10657 string_start = glyph;
10658 string_start_x = x;
10659 /* Skip all glyphs from string. */
10660 SKIP_GLYPHS (glyph, end, x, STRINGP (glyph->object));
10661 }
10662 }
10663
10664 if (string_start
10665 && (glyph == end || !BUFFERP (glyph->object) || last_pos > pt_old))
10666 {
10667 /* We may have skipped over point because the previous glyphs
10668 are from string. As there's no easy way to know the
10669 character position of the current glyph, find the correct
10670 glyph on point by scanning from string_start again. */
10671 Lisp_Object limit;
10672 Lisp_Object string;
10673 int pos;
10674
10675 limit = make_number (pt_old + 1);
10676 end = glyph;
10677 glyph = string_start;
10678 x = string_start_x;
10679 string = glyph->object;
10680 pos = string_buffer_position (w, string, string_before_pos);
10681 /* If STRING is from overlay, LAST_POS == 0. We skip such glyphs
10682 because we always put cursor after overlay strings. */
10683 while (pos == 0 && glyph < end)
10684 {
10685 string = glyph->object;
10686 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
10687 if (glyph < end)
10688 pos = string_buffer_position (w, glyph->object, string_before_pos);
10689 }
10690
10691 while (glyph < end)
10692 {
10693 pos = XINT (Fnext_single_char_property_change
10694 (make_number (pos), Qdisplay, Qnil, limit));
10695 if (pos > pt_old)
10696 break;
10697 /* Skip glyphs from the same string. */
10698 string = glyph->object;
10699 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
10700 /* Skip glyphs from an overlay. */
10701 while (glyph < end
10702 && ! string_buffer_position (w, glyph->object, pos))
10703 {
10704 string = glyph->object;
10705 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
10706 }
10707 }
10708 }
10709
10710 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
10711 w->cursor.x = x;
10712 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
10713 w->cursor.y = row->y + dy;
10714
10715 if (w == XWINDOW (selected_window))
10716 {
10717 if (!row->continued_p
10718 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
10719 && row->x == 0)
10720 {
10721 this_line_buffer = XBUFFER (w->buffer);
10722
10723 CHARPOS (this_line_start_pos)
10724 = MATRIX_ROW_START_CHARPOS (row) + delta;
10725 BYTEPOS (this_line_start_pos)
10726 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
10727
10728 CHARPOS (this_line_end_pos)
10729 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
10730 BYTEPOS (this_line_end_pos)
10731 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
10732
10733 this_line_y = w->cursor.y;
10734 this_line_pixel_height = row->height;
10735 this_line_vpos = w->cursor.vpos;
10736 this_line_start_x = row->x;
10737 }
10738 else
10739 CHARPOS (this_line_start_pos) = 0;
10740 }
10741 }
10742
10743
10744 /* Run window scroll functions, if any, for WINDOW with new window
10745 start STARTP. Sets the window start of WINDOW to that position.
10746
10747 We assume that the window's buffer is really current. */
10748
10749 static INLINE struct text_pos
10750 run_window_scroll_functions (window, startp)
10751 Lisp_Object window;
10752 struct text_pos startp;
10753 {
10754 struct window *w = XWINDOW (window);
10755 SET_MARKER_FROM_TEXT_POS (w->start, startp);
10756
10757 if (current_buffer != XBUFFER (w->buffer))
10758 abort ();
10759
10760 if (!NILP (Vwindow_scroll_functions))
10761 {
10762 run_hook_with_args_2 (Qwindow_scroll_functions, window,
10763 make_number (CHARPOS (startp)));
10764 SET_TEXT_POS_FROM_MARKER (startp, w->start);
10765 /* In case the hook functions switch buffers. */
10766 if (current_buffer != XBUFFER (w->buffer))
10767 set_buffer_internal_1 (XBUFFER (w->buffer));
10768 }
10769
10770 return startp;
10771 }
10772
10773
10774 /* Make sure the line containing the cursor is fully visible.
10775 A value of 1 means there is nothing to be done.
10776 (Either the line is fully visible, or it cannot be made so,
10777 or we cannot tell.)
10778
10779 If FORCE_P is non-zero, return 0 even if partial visible cursor row
10780 is higher than window.
10781
10782 A value of 0 means the caller should do scrolling
10783 as if point had gone off the screen. */
10784
10785 static int
10786 make_cursor_line_fully_visible (w, force_p)
10787 struct window *w;
10788 int force_p;
10789 {
10790 struct glyph_matrix *matrix;
10791 struct glyph_row *row;
10792 int window_height;
10793
10794 /* It's not always possible to find the cursor, e.g, when a window
10795 is full of overlay strings. Don't do anything in that case. */
10796 if (w->cursor.vpos < 0)
10797 return 1;
10798
10799 matrix = w->desired_matrix;
10800 row = MATRIX_ROW (matrix, w->cursor.vpos);
10801
10802 /* If the cursor row is not partially visible, there's nothing to do. */
10803 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (row))
10804 return 1;
10805
10806 /* If the row the cursor is in is taller than the window's height,
10807 it's not clear what to do, so do nothing. */
10808 window_height = window_box_height (w);
10809 if (row->height >= window_height)
10810 {
10811 if (!force_p || w->vscroll)
10812 return 1;
10813 }
10814 return 0;
10815
10816 #if 0
10817 /* This code used to try to scroll the window just enough to make
10818 the line visible. It returned 0 to say that the caller should
10819 allocate larger glyph matrices. */
10820
10821 if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (w, row))
10822 {
10823 int dy = row->height - row->visible_height;
10824 w->vscroll = 0;
10825 w->cursor.y += dy;
10826 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
10827 }
10828 else /* MATRIX_ROW_PARTIALLY_VISIBLE_AT_BOTTOM_P (w, row)) */
10829 {
10830 int dy = - (row->height - row->visible_height);
10831 w->vscroll = dy;
10832 w->cursor.y += dy;
10833 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
10834 }
10835
10836 /* When we change the cursor y-position of the selected window,
10837 change this_line_y as well so that the display optimization for
10838 the cursor line of the selected window in redisplay_internal uses
10839 the correct y-position. */
10840 if (w == XWINDOW (selected_window))
10841 this_line_y = w->cursor.y;
10842
10843 /* If vscrolling requires a larger glyph matrix, arrange for a fresh
10844 redisplay with larger matrices. */
10845 if (matrix->nrows < required_matrix_height (w))
10846 {
10847 fonts_changed_p = 1;
10848 return 0;
10849 }
10850
10851 return 1;
10852 #endif /* 0 */
10853 }
10854
10855
10856 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
10857 non-zero means only WINDOW is redisplayed in redisplay_internal.
10858 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
10859 in redisplay_window to bring a partially visible line into view in
10860 the case that only the cursor has moved.
10861
10862 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
10863 last screen line's vertical height extends past the end of the screen.
10864
10865 Value is
10866
10867 1 if scrolling succeeded
10868
10869 0 if scrolling didn't find point.
10870
10871 -1 if new fonts have been loaded so that we must interrupt
10872 redisplay, adjust glyph matrices, and try again. */
10873
10874 enum
10875 {
10876 SCROLLING_SUCCESS,
10877 SCROLLING_FAILED,
10878 SCROLLING_NEED_LARGER_MATRICES
10879 };
10880
10881 static int
10882 try_scrolling (window, just_this_one_p, scroll_conservatively,
10883 scroll_step, temp_scroll_step, last_line_misfit)
10884 Lisp_Object window;
10885 int just_this_one_p;
10886 EMACS_INT scroll_conservatively, scroll_step;
10887 int temp_scroll_step;
10888 int last_line_misfit;
10889 {
10890 struct window *w = XWINDOW (window);
10891 struct frame *f = XFRAME (w->frame);
10892 struct text_pos scroll_margin_pos;
10893 struct text_pos pos;
10894 struct text_pos startp;
10895 struct it it;
10896 Lisp_Object window_end;
10897 int this_scroll_margin;
10898 int dy = 0;
10899 int scroll_max;
10900 int rc;
10901 int amount_to_scroll = 0;
10902 Lisp_Object aggressive;
10903 int height;
10904 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
10905
10906 #if GLYPH_DEBUG
10907 debug_method_add (w, "try_scrolling");
10908 #endif
10909
10910 SET_TEXT_POS_FROM_MARKER (startp, w->start);
10911
10912 /* Compute scroll margin height in pixels. We scroll when point is
10913 within this distance from the top or bottom of the window. */
10914 if (scroll_margin > 0)
10915 {
10916 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
10917 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
10918 }
10919 else
10920 this_scroll_margin = 0;
10921
10922 /* Force scroll_conservatively to have a reasonable value so it doesn't
10923 cause an overflow while computing how much to scroll. */
10924 if (scroll_conservatively)
10925 scroll_conservatively = min (scroll_conservatively,
10926 MOST_POSITIVE_FIXNUM / FRAME_LINE_HEIGHT (f));
10927
10928 /* Compute how much we should try to scroll maximally to bring point
10929 into view. */
10930 if (scroll_step || scroll_conservatively || temp_scroll_step)
10931 scroll_max = max (scroll_step,
10932 max (scroll_conservatively, temp_scroll_step));
10933 else if (NUMBERP (current_buffer->scroll_down_aggressively)
10934 || NUMBERP (current_buffer->scroll_up_aggressively))
10935 /* We're trying to scroll because of aggressive scrolling
10936 but no scroll_step is set. Choose an arbitrary one. Maybe
10937 there should be a variable for this. */
10938 scroll_max = 10;
10939 else
10940 scroll_max = 0;
10941 scroll_max *= FRAME_LINE_HEIGHT (f);
10942
10943 /* Decide whether we have to scroll down. Start at the window end
10944 and move this_scroll_margin up to find the position of the scroll
10945 margin. */
10946 window_end = Fwindow_end (window, Qt);
10947
10948 too_near_end:
10949
10950 CHARPOS (scroll_margin_pos) = XINT (window_end);
10951 BYTEPOS (scroll_margin_pos) = CHAR_TO_BYTE (CHARPOS (scroll_margin_pos));
10952
10953 if (this_scroll_margin || extra_scroll_margin_lines)
10954 {
10955 start_display (&it, w, scroll_margin_pos);
10956 if (this_scroll_margin)
10957 move_it_vertically (&it, - this_scroll_margin);
10958 if (extra_scroll_margin_lines)
10959 move_it_by_lines (&it, - extra_scroll_margin_lines, 0);
10960 scroll_margin_pos = it.current.pos;
10961 }
10962
10963 if (PT >= CHARPOS (scroll_margin_pos))
10964 {
10965 int y0;
10966
10967 /* Point is in the scroll margin at the bottom of the window, or
10968 below. Compute a new window start that makes point visible. */
10969
10970 /* Compute the distance from the scroll margin to PT.
10971 Give up if the distance is greater than scroll_max. */
10972 start_display (&it, w, scroll_margin_pos);
10973 y0 = it.current_y;
10974 move_it_to (&it, PT, 0, it.last_visible_y, -1,
10975 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
10976
10977 /* To make point visible, we have to move the window start
10978 down so that the line the cursor is in is visible, which
10979 means we have to add in the height of the cursor line. */
10980 dy = line_bottom_y (&it) - y0;
10981
10982 if (dy > scroll_max)
10983 return SCROLLING_FAILED;
10984
10985 /* Move the window start down. If scrolling conservatively,
10986 move it just enough down to make point visible. If
10987 scroll_step is set, move it down by scroll_step. */
10988 start_display (&it, w, startp);
10989
10990 if (scroll_conservatively)
10991 /* Set AMOUNT_TO_SCROLL to at least one line,
10992 and at most scroll_conservatively lines. */
10993 amount_to_scroll
10994 = min (max (dy, FRAME_LINE_HEIGHT (f)),
10995 FRAME_LINE_HEIGHT (f) * scroll_conservatively);
10996 else if (scroll_step || temp_scroll_step)
10997 amount_to_scroll = scroll_max;
10998 else
10999 {
11000 aggressive = current_buffer->scroll_up_aggressively;
11001 height = WINDOW_BOX_TEXT_HEIGHT (w);
11002 if (NUMBERP (aggressive))
11003 {
11004 double float_amount = XFLOATINT (aggressive) * height;
11005 amount_to_scroll = float_amount;
11006 if (amount_to_scroll == 0 && float_amount > 0)
11007 amount_to_scroll = 1;
11008 }
11009 }
11010
11011 if (amount_to_scroll <= 0)
11012 return SCROLLING_FAILED;
11013
11014 /* If moving by amount_to_scroll leaves STARTP unchanged,
11015 move it down one screen line. */
11016
11017 move_it_vertically (&it, amount_to_scroll);
11018 if (CHARPOS (it.current.pos) == CHARPOS (startp))
11019 move_it_by_lines (&it, 1, 1);
11020 startp = it.current.pos;
11021 }
11022 else
11023 {
11024 /* See if point is inside the scroll margin at the top of the
11025 window. */
11026 scroll_margin_pos = startp;
11027 if (this_scroll_margin)
11028 {
11029 start_display (&it, w, startp);
11030 move_it_vertically (&it, this_scroll_margin);
11031 scroll_margin_pos = it.current.pos;
11032 }
11033
11034 if (PT < CHARPOS (scroll_margin_pos))
11035 {
11036 /* Point is in the scroll margin at the top of the window or
11037 above what is displayed in the window. */
11038 int y0;
11039
11040 /* Compute the vertical distance from PT to the scroll
11041 margin position. Give up if distance is greater than
11042 scroll_max. */
11043 SET_TEXT_POS (pos, PT, PT_BYTE);
11044 start_display (&it, w, pos);
11045 y0 = it.current_y;
11046 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
11047 it.last_visible_y, -1,
11048 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
11049 dy = it.current_y - y0;
11050 if (dy > scroll_max)
11051 return SCROLLING_FAILED;
11052
11053 /* Compute new window start. */
11054 start_display (&it, w, startp);
11055
11056 if (scroll_conservatively)
11057 amount_to_scroll =
11058 max (dy, FRAME_LINE_HEIGHT (f) * max (scroll_step, temp_scroll_step));
11059 else if (scroll_step || temp_scroll_step)
11060 amount_to_scroll = scroll_max;
11061 else
11062 {
11063 aggressive = current_buffer->scroll_down_aggressively;
11064 height = WINDOW_BOX_TEXT_HEIGHT (w);
11065 if (NUMBERP (aggressive))
11066 {
11067 double float_amount = XFLOATINT (aggressive) * height;
11068 amount_to_scroll = float_amount;
11069 if (amount_to_scroll == 0 && float_amount > 0)
11070 amount_to_scroll = 1;
11071 }
11072 }
11073
11074 if (amount_to_scroll <= 0)
11075 return SCROLLING_FAILED;
11076
11077 move_it_vertically (&it, - amount_to_scroll);
11078 startp = it.current.pos;
11079 }
11080 }
11081
11082 /* Run window scroll functions. */
11083 startp = run_window_scroll_functions (window, startp);
11084
11085 /* Display the window. Give up if new fonts are loaded, or if point
11086 doesn't appear. */
11087 if (!try_window (window, startp))
11088 rc = SCROLLING_NEED_LARGER_MATRICES;
11089 else if (w->cursor.vpos < 0)
11090 {
11091 clear_glyph_matrix (w->desired_matrix);
11092 rc = SCROLLING_FAILED;
11093 }
11094 else
11095 {
11096 /* Maybe forget recorded base line for line number display. */
11097 if (!just_this_one_p
11098 || current_buffer->clip_changed
11099 || BEG_UNCHANGED < CHARPOS (startp))
11100 w->base_line_number = Qnil;
11101
11102 /* If cursor ends up on a partially visible line,
11103 treat that as being off the bottom of the screen. */
11104 if (! make_cursor_line_fully_visible (w, extra_scroll_margin_lines <= 1))
11105 {
11106 clear_glyph_matrix (w->desired_matrix);
11107 ++extra_scroll_margin_lines;
11108 goto too_near_end;
11109 }
11110 rc = SCROLLING_SUCCESS;
11111 }
11112
11113 return rc;
11114 }
11115
11116
11117 /* Compute a suitable window start for window W if display of W starts
11118 on a continuation line. Value is non-zero if a new window start
11119 was computed.
11120
11121 The new window start will be computed, based on W's width, starting
11122 from the start of the continued line. It is the start of the
11123 screen line with the minimum distance from the old start W->start. */
11124
11125 static int
11126 compute_window_start_on_continuation_line (w)
11127 struct window *w;
11128 {
11129 struct text_pos pos, start_pos;
11130 int window_start_changed_p = 0;
11131
11132 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
11133
11134 /* If window start is on a continuation line... Window start may be
11135 < BEGV in case there's invisible text at the start of the
11136 buffer (M-x rmail, for example). */
11137 if (CHARPOS (start_pos) > BEGV
11138 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
11139 {
11140 struct it it;
11141 struct glyph_row *row;
11142
11143 /* Handle the case that the window start is out of range. */
11144 if (CHARPOS (start_pos) < BEGV)
11145 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
11146 else if (CHARPOS (start_pos) > ZV)
11147 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
11148
11149 /* Find the start of the continued line. This should be fast
11150 because scan_buffer is fast (newline cache). */
11151 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
11152 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
11153 row, DEFAULT_FACE_ID);
11154 reseat_at_previous_visible_line_start (&it);
11155
11156 /* If the line start is "too far" away from the window start,
11157 say it takes too much time to compute a new window start. */
11158 if (CHARPOS (start_pos) - IT_CHARPOS (it)
11159 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
11160 {
11161 int min_distance, distance;
11162
11163 /* Move forward by display lines to find the new window
11164 start. If window width was enlarged, the new start can
11165 be expected to be > the old start. If window width was
11166 decreased, the new window start will be < the old start.
11167 So, we're looking for the display line start with the
11168 minimum distance from the old window start. */
11169 pos = it.current.pos;
11170 min_distance = INFINITY;
11171 while ((distance = abs (CHARPOS (start_pos) - IT_CHARPOS (it))),
11172 distance < min_distance)
11173 {
11174 min_distance = distance;
11175 pos = it.current.pos;
11176 move_it_by_lines (&it, 1, 0);
11177 }
11178
11179 /* Set the window start there. */
11180 SET_MARKER_FROM_TEXT_POS (w->start, pos);
11181 window_start_changed_p = 1;
11182 }
11183 }
11184
11185 return window_start_changed_p;
11186 }
11187
11188
11189 /* Try cursor movement in case text has not changed in window WINDOW,
11190 with window start STARTP. Value is
11191
11192 CURSOR_MOVEMENT_SUCCESS if successful
11193
11194 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
11195
11196 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
11197 display. *SCROLL_STEP is set to 1, under certain circumstances, if
11198 we want to scroll as if scroll-step were set to 1. See the code.
11199
11200 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
11201 which case we have to abort this redisplay, and adjust matrices
11202 first. */
11203
11204 enum
11205 {
11206 CURSOR_MOVEMENT_SUCCESS,
11207 CURSOR_MOVEMENT_CANNOT_BE_USED,
11208 CURSOR_MOVEMENT_MUST_SCROLL,
11209 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
11210 };
11211
11212 static int
11213 try_cursor_movement (window, startp, scroll_step)
11214 Lisp_Object window;
11215 struct text_pos startp;
11216 int *scroll_step;
11217 {
11218 struct window *w = XWINDOW (window);
11219 struct frame *f = XFRAME (w->frame);
11220 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
11221
11222 #if GLYPH_DEBUG
11223 if (inhibit_try_cursor_movement)
11224 return rc;
11225 #endif
11226
11227 /* Handle case where text has not changed, only point, and it has
11228 not moved off the frame. */
11229 if (/* Point may be in this window. */
11230 PT >= CHARPOS (startp)
11231 /* Selective display hasn't changed. */
11232 && !current_buffer->clip_changed
11233 /* Function force-mode-line-update is used to force a thorough
11234 redisplay. It sets either windows_or_buffers_changed or
11235 update_mode_lines. So don't take a shortcut here for these
11236 cases. */
11237 && !update_mode_lines
11238 && !windows_or_buffers_changed
11239 && !cursor_type_changed
11240 /* Can't use this case if highlighting a region. When a
11241 region exists, cursor movement has to do more than just
11242 set the cursor. */
11243 && !(!NILP (Vtransient_mark_mode)
11244 && !NILP (current_buffer->mark_active))
11245 && NILP (w->region_showing)
11246 && NILP (Vshow_trailing_whitespace)
11247 /* Right after splitting windows, last_point may be nil. */
11248 && INTEGERP (w->last_point)
11249 /* This code is not used for mini-buffer for the sake of the case
11250 of redisplaying to replace an echo area message; since in
11251 that case the mini-buffer contents per se are usually
11252 unchanged. This code is of no real use in the mini-buffer
11253 since the handling of this_line_start_pos, etc., in redisplay
11254 handles the same cases. */
11255 && !EQ (window, minibuf_window)
11256 /* When splitting windows or for new windows, it happens that
11257 redisplay is called with a nil window_end_vpos or one being
11258 larger than the window. This should really be fixed in
11259 window.c. I don't have this on my list, now, so we do
11260 approximately the same as the old redisplay code. --gerd. */
11261 && INTEGERP (w->window_end_vpos)
11262 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
11263 && (FRAME_WINDOW_P (f)
11264 || !overlay_arrow_in_current_buffer_p ()))
11265 {
11266 int this_scroll_margin;
11267 struct glyph_row *row = NULL;
11268
11269 #if GLYPH_DEBUG
11270 debug_method_add (w, "cursor movement");
11271 #endif
11272
11273 /* Scroll if point within this distance from the top or bottom
11274 of the window. This is a pixel value. */
11275 this_scroll_margin = max (0, scroll_margin);
11276 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
11277 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
11278
11279 /* Start with the row the cursor was displayed during the last
11280 not paused redisplay. Give up if that row is not valid. */
11281 if (w->last_cursor.vpos < 0
11282 || w->last_cursor.vpos >= w->current_matrix->nrows)
11283 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11284 else
11285 {
11286 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
11287 if (row->mode_line_p)
11288 ++row;
11289 if (!row->enabled_p)
11290 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11291 }
11292
11293 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
11294 {
11295 int scroll_p = 0;
11296 int last_y = window_text_bottom_y (w) - this_scroll_margin;
11297
11298 if (PT > XFASTINT (w->last_point))
11299 {
11300 /* Point has moved forward. */
11301 while (MATRIX_ROW_END_CHARPOS (row) < PT
11302 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
11303 {
11304 xassert (row->enabled_p);
11305 ++row;
11306 }
11307
11308 /* The end position of a row equals the start position
11309 of the next row. If PT is there, we would rather
11310 display it in the next line. */
11311 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
11312 && MATRIX_ROW_END_CHARPOS (row) == PT
11313 && !cursor_row_p (w, row))
11314 ++row;
11315
11316 /* If within the scroll margin, scroll. Note that
11317 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
11318 the next line would be drawn, and that
11319 this_scroll_margin can be zero. */
11320 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
11321 || PT > MATRIX_ROW_END_CHARPOS (row)
11322 /* Line is completely visible last line in window
11323 and PT is to be set in the next line. */
11324 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
11325 && PT == MATRIX_ROW_END_CHARPOS (row)
11326 && !row->ends_at_zv_p
11327 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
11328 scroll_p = 1;
11329 }
11330 else if (PT < XFASTINT (w->last_point))
11331 {
11332 /* Cursor has to be moved backward. Note that PT >=
11333 CHARPOS (startp) because of the outer
11334 if-statement. */
11335 while (!row->mode_line_p
11336 && (MATRIX_ROW_START_CHARPOS (row) > PT
11337 || (MATRIX_ROW_START_CHARPOS (row) == PT
11338 && MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)))
11339 && (row->y > this_scroll_margin
11340 || CHARPOS (startp) == BEGV))
11341 {
11342 xassert (row->enabled_p);
11343 --row;
11344 }
11345
11346 /* Consider the following case: Window starts at BEGV,
11347 there is invisible, intangible text at BEGV, so that
11348 display starts at some point START > BEGV. It can
11349 happen that we are called with PT somewhere between
11350 BEGV and START. Try to handle that case. */
11351 if (row < w->current_matrix->rows
11352 || row->mode_line_p)
11353 {
11354 row = w->current_matrix->rows;
11355 if (row->mode_line_p)
11356 ++row;
11357 }
11358
11359 /* Due to newlines in overlay strings, we may have to
11360 skip forward over overlay strings. */
11361 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
11362 && MATRIX_ROW_END_CHARPOS (row) == PT
11363 && !cursor_row_p (w, row))
11364 ++row;
11365
11366 /* If within the scroll margin, scroll. */
11367 if (row->y < this_scroll_margin
11368 && CHARPOS (startp) != BEGV)
11369 scroll_p = 1;
11370 }
11371
11372 if (PT < MATRIX_ROW_START_CHARPOS (row)
11373 || PT > MATRIX_ROW_END_CHARPOS (row))
11374 {
11375 /* if PT is not in the glyph row, give up. */
11376 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11377 }
11378 else if (MATRIX_ROW_PARTIALLY_VISIBLE_P (row))
11379 {
11380 if (PT == MATRIX_ROW_END_CHARPOS (row)
11381 && !row->ends_at_zv_p
11382 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
11383 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11384 else if (row->height > window_box_height (w))
11385 {
11386 /* If we end up in a partially visible line, let's
11387 make it fully visible, except when it's taller
11388 than the window, in which case we can't do much
11389 about it. */
11390 *scroll_step = 1;
11391 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11392 }
11393 else
11394 {
11395 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11396 if (!make_cursor_line_fully_visible (w, 0))
11397 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11398 else
11399 rc = CURSOR_MOVEMENT_SUCCESS;
11400 }
11401 }
11402 else if (scroll_p)
11403 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11404 else
11405 {
11406 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11407 rc = CURSOR_MOVEMENT_SUCCESS;
11408 }
11409 }
11410 }
11411
11412 return rc;
11413 }
11414
11415 void
11416 set_vertical_scroll_bar (w)
11417 struct window *w;
11418 {
11419 int start, end, whole;
11420
11421 /* Calculate the start and end positions for the current window.
11422 At some point, it would be nice to choose between scrollbars
11423 which reflect the whole buffer size, with special markers
11424 indicating narrowing, and scrollbars which reflect only the
11425 visible region.
11426
11427 Note that mini-buffers sometimes aren't displaying any text. */
11428 if (!MINI_WINDOW_P (w)
11429 || (w == XWINDOW (minibuf_window)
11430 && NILP (echo_area_buffer[0])))
11431 {
11432 struct buffer *buf = XBUFFER (w->buffer);
11433 whole = BUF_ZV (buf) - BUF_BEGV (buf);
11434 start = marker_position (w->start) - BUF_BEGV (buf);
11435 /* I don't think this is guaranteed to be right. For the
11436 moment, we'll pretend it is. */
11437 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
11438
11439 if (end < start)
11440 end = start;
11441 if (whole < (end - start))
11442 whole = end - start;
11443 }
11444 else
11445 start = end = whole = 0;
11446
11447 /* Indicate what this scroll bar ought to be displaying now. */
11448 if (FRAME_DISPLAY (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
11449 (*FRAME_DISPLAY (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
11450 (w, end - start, whole, start);
11451 }
11452
11453
11454 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
11455 selected_window is redisplayed.
11456
11457 We can return without actually redisplaying the window if
11458 fonts_changed_p is nonzero. In that case, redisplay_internal will
11459 retry. */
11460
11461 static void
11462 redisplay_window (window, just_this_one_p)
11463 Lisp_Object window;
11464 int just_this_one_p;
11465 {
11466 struct window *w = XWINDOW (window);
11467 struct frame *f = XFRAME (w->frame);
11468 struct buffer *buffer = XBUFFER (w->buffer);
11469 struct buffer *old = current_buffer;
11470 struct text_pos lpoint, opoint, startp;
11471 int update_mode_line;
11472 int tem;
11473 struct it it;
11474 /* Record it now because it's overwritten. */
11475 int current_matrix_up_to_date_p = 0;
11476 int used_current_matrix_p = 0;
11477 /* This is less strict than current_matrix_up_to_date_p.
11478 It indictes that the buffer contents and narrowing are unchanged. */
11479 int buffer_unchanged_p = 0;
11480 int temp_scroll_step = 0;
11481 int count = SPECPDL_INDEX ();
11482 int rc;
11483 int centering_position;
11484 int last_line_misfit = 0;
11485
11486 SET_TEXT_POS (lpoint, PT, PT_BYTE);
11487 opoint = lpoint;
11488
11489 /* W must be a leaf window here. */
11490 xassert (!NILP (w->buffer));
11491 #if GLYPH_DEBUG
11492 *w->desired_matrix->method = 0;
11493 #endif
11494
11495 specbind (Qinhibit_point_motion_hooks, Qt);
11496
11497 reconsider_clip_changes (w, buffer);
11498
11499 /* Has the mode line to be updated? */
11500 update_mode_line = (!NILP (w->update_mode_line)
11501 || update_mode_lines
11502 || buffer->clip_changed
11503 || buffer->prevent_redisplay_optimizations_p);
11504
11505 if (MINI_WINDOW_P (w))
11506 {
11507 if (w == XWINDOW (echo_area_window)
11508 && !NILP (echo_area_buffer[0]))
11509 {
11510 if (update_mode_line)
11511 /* We may have to update a tty frame's menu bar or a
11512 tool-bar. Example `M-x C-h C-h C-g'. */
11513 goto finish_menu_bars;
11514 else
11515 /* We've already displayed the echo area glyphs in this window. */
11516 goto finish_scroll_bars;
11517 }
11518 else if ((w != XWINDOW (minibuf_window)
11519 || minibuf_level == 0)
11520 /* When buffer is nonempty, redisplay window normally. */
11521 && BUF_Z (XBUFFER (w->buffer)) == BUF_BEG (XBUFFER (w->buffer))
11522 /* Quail displays non-mini buffers in minibuffer window.
11523 In that case, redisplay the window normally. */
11524 && !NILP (Fmemq (w->buffer, Vminibuffer_list)))
11525 {
11526 /* W is a mini-buffer window, but it's not active, so clear
11527 it. */
11528 int yb = window_text_bottom_y (w);
11529 struct glyph_row *row;
11530 int y;
11531
11532 for (y = 0, row = w->desired_matrix->rows;
11533 y < yb;
11534 y += row->height, ++row)
11535 blank_row (w, row, y);
11536 goto finish_scroll_bars;
11537 }
11538
11539 clear_glyph_matrix (w->desired_matrix);
11540 }
11541
11542 /* Otherwise set up data on this window; select its buffer and point
11543 value. */
11544 /* Really select the buffer, for the sake of buffer-local
11545 variables. */
11546 set_buffer_internal_1 (XBUFFER (w->buffer));
11547 SET_TEXT_POS (opoint, PT, PT_BYTE);
11548
11549 current_matrix_up_to_date_p
11550 = (!NILP (w->window_end_valid)
11551 && !current_buffer->clip_changed
11552 && !current_buffer->prevent_redisplay_optimizations_p
11553 && XFASTINT (w->last_modified) >= MODIFF
11554 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
11555
11556 buffer_unchanged_p
11557 = (!NILP (w->window_end_valid)
11558 && !current_buffer->clip_changed
11559 && XFASTINT (w->last_modified) >= MODIFF
11560 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
11561
11562 /* When windows_or_buffers_changed is non-zero, we can't rely on
11563 the window end being valid, so set it to nil there. */
11564 if (windows_or_buffers_changed)
11565 {
11566 /* If window starts on a continuation line, maybe adjust the
11567 window start in case the window's width changed. */
11568 if (XMARKER (w->start)->buffer == current_buffer)
11569 compute_window_start_on_continuation_line (w);
11570
11571 w->window_end_valid = Qnil;
11572 }
11573
11574 /* Some sanity checks. */
11575 CHECK_WINDOW_END (w);
11576 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
11577 abort ();
11578 if (BYTEPOS (opoint) < CHARPOS (opoint))
11579 abort ();
11580
11581 /* If %c is in mode line, update it if needed. */
11582 if (!NILP (w->column_number_displayed)
11583 /* This alternative quickly identifies a common case
11584 where no change is needed. */
11585 && !(PT == XFASTINT (w->last_point)
11586 && XFASTINT (w->last_modified) >= MODIFF
11587 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
11588 && (XFASTINT (w->column_number_displayed)
11589 != (int) current_column ())) /* iftc */
11590 update_mode_line = 1;
11591
11592 /* Count number of windows showing the selected buffer. An indirect
11593 buffer counts as its base buffer. */
11594 if (!just_this_one_p)
11595 {
11596 struct buffer *current_base, *window_base;
11597 current_base = current_buffer;
11598 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
11599 if (current_base->base_buffer)
11600 current_base = current_base->base_buffer;
11601 if (window_base->base_buffer)
11602 window_base = window_base->base_buffer;
11603 if (current_base == window_base)
11604 buffer_shared++;
11605 }
11606
11607 /* Point refers normally to the selected window. For any other
11608 window, set up appropriate value. */
11609 if (!EQ (window, selected_window))
11610 {
11611 int new_pt = XMARKER (w->pointm)->charpos;
11612 int new_pt_byte = marker_byte_position (w->pointm);
11613 if (new_pt < BEGV)
11614 {
11615 new_pt = BEGV;
11616 new_pt_byte = BEGV_BYTE;
11617 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
11618 }
11619 else if (new_pt > (ZV - 1))
11620 {
11621 new_pt = ZV;
11622 new_pt_byte = ZV_BYTE;
11623 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
11624 }
11625
11626 /* We don't use SET_PT so that the point-motion hooks don't run. */
11627 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
11628 }
11629
11630 /* If any of the character widths specified in the display table
11631 have changed, invalidate the width run cache. It's true that
11632 this may be a bit late to catch such changes, but the rest of
11633 redisplay goes (non-fatally) haywire when the display table is
11634 changed, so why should we worry about doing any better? */
11635 if (current_buffer->width_run_cache)
11636 {
11637 struct Lisp_Char_Table *disptab = buffer_display_table ();
11638
11639 if (! disptab_matches_widthtab (disptab,
11640 XVECTOR (current_buffer->width_table)))
11641 {
11642 invalidate_region_cache (current_buffer,
11643 current_buffer->width_run_cache,
11644 BEG, Z);
11645 recompute_width_table (current_buffer, disptab);
11646 }
11647 }
11648
11649 /* If window-start is screwed up, choose a new one. */
11650 if (XMARKER (w->start)->buffer != current_buffer)
11651 goto recenter;
11652
11653 SET_TEXT_POS_FROM_MARKER (startp, w->start);
11654
11655 /* If someone specified a new starting point but did not insist,
11656 check whether it can be used. */
11657 if (!NILP (w->optional_new_start)
11658 && CHARPOS (startp) >= BEGV
11659 && CHARPOS (startp) <= ZV)
11660 {
11661 w->optional_new_start = Qnil;
11662 start_display (&it, w, startp);
11663 move_it_to (&it, PT, 0, it.last_visible_y, -1,
11664 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
11665 if (IT_CHARPOS (it) == PT)
11666 w->force_start = Qt;
11667 /* IT may overshoot PT if text at PT is invisible. */
11668 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
11669 w->force_start = Qt;
11670
11671
11672 }
11673
11674 /* Handle case where place to start displaying has been specified,
11675 unless the specified location is outside the accessible range. */
11676 if (!NILP (w->force_start)
11677 || w->frozen_window_start_p)
11678 {
11679 /* We set this later on if we have to adjust point. */
11680 int new_vpos = -1;
11681
11682 w->force_start = Qnil;
11683 w->vscroll = 0;
11684 w->window_end_valid = Qnil;
11685
11686 /* Forget any recorded base line for line number display. */
11687 if (!buffer_unchanged_p)
11688 w->base_line_number = Qnil;
11689
11690 /* Redisplay the mode line. Select the buffer properly for that.
11691 Also, run the hook window-scroll-functions
11692 because we have scrolled. */
11693 /* Note, we do this after clearing force_start because
11694 if there's an error, it is better to forget about force_start
11695 than to get into an infinite loop calling the hook functions
11696 and having them get more errors. */
11697 if (!update_mode_line
11698 || ! NILP (Vwindow_scroll_functions))
11699 {
11700 update_mode_line = 1;
11701 w->update_mode_line = Qt;
11702 startp = run_window_scroll_functions (window, startp);
11703 }
11704
11705 w->last_modified = make_number (0);
11706 w->last_overlay_modified = make_number (0);
11707 if (CHARPOS (startp) < BEGV)
11708 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
11709 else if (CHARPOS (startp) > ZV)
11710 SET_TEXT_POS (startp, ZV, ZV_BYTE);
11711
11712 /* Redisplay, then check if cursor has been set during the
11713 redisplay. Give up if new fonts were loaded. */
11714 if (!try_window (window, startp))
11715 {
11716 w->force_start = Qt;
11717 clear_glyph_matrix (w->desired_matrix);
11718 goto need_larger_matrices;
11719 }
11720
11721 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
11722 {
11723 /* If point does not appear, try to move point so it does
11724 appear. The desired matrix has been built above, so we
11725 can use it here. */
11726 new_vpos = window_box_height (w) / 2;
11727 }
11728
11729 if (!make_cursor_line_fully_visible (w, 0))
11730 {
11731 /* Point does appear, but on a line partly visible at end of window.
11732 Move it back to a fully-visible line. */
11733 new_vpos = window_box_height (w);
11734 }
11735
11736 /* If we need to move point for either of the above reasons,
11737 now actually do it. */
11738 if (new_vpos >= 0)
11739 {
11740 struct glyph_row *row;
11741
11742 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
11743 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
11744 ++row;
11745
11746 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
11747 MATRIX_ROW_START_BYTEPOS (row));
11748
11749 if (w != XWINDOW (selected_window))
11750 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
11751 else if (current_buffer == old)
11752 SET_TEXT_POS (lpoint, PT, PT_BYTE);
11753
11754 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
11755
11756 /* If we are highlighting the region, then we just changed
11757 the region, so redisplay to show it. */
11758 if (!NILP (Vtransient_mark_mode)
11759 && !NILP (current_buffer->mark_active))
11760 {
11761 clear_glyph_matrix (w->desired_matrix);
11762 if (!try_window (window, startp))
11763 goto need_larger_matrices;
11764 }
11765 }
11766
11767 #if GLYPH_DEBUG
11768 debug_method_add (w, "forced window start");
11769 #endif
11770 goto done;
11771 }
11772
11773 /* Handle case where text has not changed, only point, and it has
11774 not moved off the frame, and we are not retrying after hscroll.
11775 (current_matrix_up_to_date_p is nonzero when retrying.) */
11776 if (current_matrix_up_to_date_p
11777 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
11778 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
11779 {
11780 switch (rc)
11781 {
11782 case CURSOR_MOVEMENT_SUCCESS:
11783 used_current_matrix_p = 1;
11784 goto done;
11785
11786 #if 0 /* try_cursor_movement never returns this value. */
11787 case CURSOR_MOVEMENT_NEED_LARGER_MATRICES:
11788 goto need_larger_matrices;
11789 #endif
11790
11791 case CURSOR_MOVEMENT_MUST_SCROLL:
11792 goto try_to_scroll;
11793
11794 default:
11795 abort ();
11796 }
11797 }
11798 /* If current starting point was originally the beginning of a line
11799 but no longer is, find a new starting point. */
11800 else if (!NILP (w->start_at_line_beg)
11801 && !(CHARPOS (startp) <= BEGV
11802 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
11803 {
11804 #if GLYPH_DEBUG
11805 debug_method_add (w, "recenter 1");
11806 #endif
11807 goto recenter;
11808 }
11809
11810 /* Try scrolling with try_window_id. Value is > 0 if update has
11811 been done, it is -1 if we know that the same window start will
11812 not work. It is 0 if unsuccessful for some other reason. */
11813 else if ((tem = try_window_id (w)) != 0)
11814 {
11815 #if GLYPH_DEBUG
11816 debug_method_add (w, "try_window_id %d", tem);
11817 #endif
11818
11819 if (fonts_changed_p)
11820 goto need_larger_matrices;
11821 if (tem > 0)
11822 goto done;
11823
11824 /* Otherwise try_window_id has returned -1 which means that we
11825 don't want the alternative below this comment to execute. */
11826 }
11827 else if (CHARPOS (startp) >= BEGV
11828 && CHARPOS (startp) <= ZV
11829 && PT >= CHARPOS (startp)
11830 && (CHARPOS (startp) < ZV
11831 /* Avoid starting at end of buffer. */
11832 || CHARPOS (startp) == BEGV
11833 || (XFASTINT (w->last_modified) >= MODIFF
11834 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
11835 {
11836 #if GLYPH_DEBUG
11837 debug_method_add (w, "same window start");
11838 #endif
11839
11840 /* Try to redisplay starting at same place as before.
11841 If point has not moved off frame, accept the results. */
11842 if (!current_matrix_up_to_date_p
11843 /* Don't use try_window_reusing_current_matrix in this case
11844 because a window scroll function can have changed the
11845 buffer. */
11846 || !NILP (Vwindow_scroll_functions)
11847 || MINI_WINDOW_P (w)
11848 || !(used_current_matrix_p =
11849 try_window_reusing_current_matrix (w)))
11850 {
11851 IF_DEBUG (debug_method_add (w, "1"));
11852 try_window (window, startp);
11853 }
11854
11855 if (fonts_changed_p)
11856 goto need_larger_matrices;
11857
11858 if (w->cursor.vpos >= 0)
11859 {
11860 if (!just_this_one_p
11861 || current_buffer->clip_changed
11862 || BEG_UNCHANGED < CHARPOS (startp))
11863 /* Forget any recorded base line for line number display. */
11864 w->base_line_number = Qnil;
11865
11866 if (!make_cursor_line_fully_visible (w, 1))
11867 {
11868 clear_glyph_matrix (w->desired_matrix);
11869 last_line_misfit = 1;
11870 }
11871 /* Drop through and scroll. */
11872 else
11873 goto done;
11874 }
11875 else
11876 clear_glyph_matrix (w->desired_matrix);
11877 }
11878
11879 try_to_scroll:
11880
11881 w->last_modified = make_number (0);
11882 w->last_overlay_modified = make_number (0);
11883
11884 /* Redisplay the mode line. Select the buffer properly for that. */
11885 if (!update_mode_line)
11886 {
11887 update_mode_line = 1;
11888 w->update_mode_line = Qt;
11889 }
11890
11891 /* Try to scroll by specified few lines. */
11892 if ((scroll_conservatively
11893 || scroll_step
11894 || temp_scroll_step
11895 || NUMBERP (current_buffer->scroll_up_aggressively)
11896 || NUMBERP (current_buffer->scroll_down_aggressively))
11897 && !current_buffer->clip_changed
11898 && CHARPOS (startp) >= BEGV
11899 && CHARPOS (startp) <= ZV)
11900 {
11901 /* The function returns -1 if new fonts were loaded, 1 if
11902 successful, 0 if not successful. */
11903 int rc = try_scrolling (window, just_this_one_p,
11904 scroll_conservatively,
11905 scroll_step,
11906 temp_scroll_step, last_line_misfit);
11907 switch (rc)
11908 {
11909 case SCROLLING_SUCCESS:
11910 goto done;
11911
11912 case SCROLLING_NEED_LARGER_MATRICES:
11913 goto need_larger_matrices;
11914
11915 case SCROLLING_FAILED:
11916 break;
11917
11918 default:
11919 abort ();
11920 }
11921 }
11922
11923 /* Finally, just choose place to start which centers point */
11924
11925 recenter:
11926 centering_position = window_box_height (w) / 2;
11927
11928 point_at_top:
11929 /* Jump here with centering_position already set to 0. */
11930
11931 #if GLYPH_DEBUG
11932 debug_method_add (w, "recenter");
11933 #endif
11934
11935 /* w->vscroll = 0; */
11936
11937 /* Forget any previously recorded base line for line number display. */
11938 if (!buffer_unchanged_p)
11939 w->base_line_number = Qnil;
11940
11941 /* Move backward half the height of the window. */
11942 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
11943 it.current_y = it.last_visible_y;
11944 move_it_vertically_backward (&it, centering_position);
11945 xassert (IT_CHARPOS (it) >= BEGV);
11946
11947 /* The function move_it_vertically_backward may move over more
11948 than the specified y-distance. If it->w is small, e.g. a
11949 mini-buffer window, we may end up in front of the window's
11950 display area. Start displaying at the start of the line
11951 containing PT in this case. */
11952 if (it.current_y <= 0)
11953 {
11954 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
11955 move_it_vertically (&it, 0);
11956 xassert (IT_CHARPOS (it) <= PT);
11957 it.current_y = 0;
11958 }
11959
11960 it.current_x = it.hpos = 0;
11961
11962 /* Set startp here explicitly in case that helps avoid an infinite loop
11963 in case the window-scroll-functions functions get errors. */
11964 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
11965
11966 /* Run scroll hooks. */
11967 startp = run_window_scroll_functions (window, it.current.pos);
11968
11969 /* Redisplay the window. */
11970 if (!current_matrix_up_to_date_p
11971 || windows_or_buffers_changed
11972 || cursor_type_changed
11973 /* Don't use try_window_reusing_current_matrix in this case
11974 because it can have changed the buffer. */
11975 || !NILP (Vwindow_scroll_functions)
11976 || !just_this_one_p
11977 || MINI_WINDOW_P (w)
11978 || !(used_current_matrix_p =
11979 try_window_reusing_current_matrix (w)))
11980 try_window (window, startp);
11981
11982 /* If new fonts have been loaded (due to fontsets), give up. We
11983 have to start a new redisplay since we need to re-adjust glyph
11984 matrices. */
11985 if (fonts_changed_p)
11986 goto need_larger_matrices;
11987
11988 /* If cursor did not appear assume that the middle of the window is
11989 in the first line of the window. Do it again with the next line.
11990 (Imagine a window of height 100, displaying two lines of height
11991 60. Moving back 50 from it->last_visible_y will end in the first
11992 line.) */
11993 if (w->cursor.vpos < 0)
11994 {
11995 if (!NILP (w->window_end_valid)
11996 && PT >= Z - XFASTINT (w->window_end_pos))
11997 {
11998 clear_glyph_matrix (w->desired_matrix);
11999 move_it_by_lines (&it, 1, 0);
12000 try_window (window, it.current.pos);
12001 }
12002 else if (PT < IT_CHARPOS (it))
12003 {
12004 clear_glyph_matrix (w->desired_matrix);
12005 move_it_by_lines (&it, -1, 0);
12006 try_window (window, it.current.pos);
12007 }
12008 else
12009 {
12010 /* Not much we can do about it. */
12011 }
12012 }
12013
12014 /* Consider the following case: Window starts at BEGV, there is
12015 invisible, intangible text at BEGV, so that display starts at
12016 some point START > BEGV. It can happen that we are called with
12017 PT somewhere between BEGV and START. Try to handle that case. */
12018 if (w->cursor.vpos < 0)
12019 {
12020 struct glyph_row *row = w->current_matrix->rows;
12021 if (row->mode_line_p)
12022 ++row;
12023 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
12024 }
12025
12026 if (!make_cursor_line_fully_visible (w, centering_position > 0))
12027 {
12028 /* If vscroll is enabled, disable it and try again. */
12029 if (w->vscroll)
12030 {
12031 w->vscroll = 0;
12032 clear_glyph_matrix (w->desired_matrix);
12033 goto recenter;
12034 }
12035
12036 /* If centering point failed to make the whole line visible,
12037 put point at the top instead. That has to make the whole line
12038 visible, if it can be done. */
12039 clear_glyph_matrix (w->desired_matrix);
12040 centering_position = 0;
12041 goto point_at_top;
12042 }
12043
12044 done:
12045
12046 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12047 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
12048 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
12049 ? Qt : Qnil);
12050
12051 /* Display the mode line, if we must. */
12052 if ((update_mode_line
12053 /* If window not full width, must redo its mode line
12054 if (a) the window to its side is being redone and
12055 (b) we do a frame-based redisplay. This is a consequence
12056 of how inverted lines are drawn in frame-based redisplay. */
12057 || (!just_this_one_p
12058 && !FRAME_WINDOW_P (f)
12059 && !WINDOW_FULL_WIDTH_P (w))
12060 /* Line number to display. */
12061 || INTEGERP (w->base_line_pos)
12062 /* Column number is displayed and different from the one displayed. */
12063 || (!NILP (w->column_number_displayed)
12064 && (XFASTINT (w->column_number_displayed)
12065 != (int) current_column ()))) /* iftc */
12066 /* This means that the window has a mode line. */
12067 && (WINDOW_WANTS_MODELINE_P (w)
12068 || WINDOW_WANTS_HEADER_LINE_P (w)))
12069 {
12070 display_mode_lines (w);
12071
12072 /* If mode line height has changed, arrange for a thorough
12073 immediate redisplay using the correct mode line height. */
12074 if (WINDOW_WANTS_MODELINE_P (w)
12075 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
12076 {
12077 fonts_changed_p = 1;
12078 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
12079 = DESIRED_MODE_LINE_HEIGHT (w);
12080 }
12081
12082 /* If top line height has changed, arrange for a thorough
12083 immediate redisplay using the correct mode line height. */
12084 if (WINDOW_WANTS_HEADER_LINE_P (w)
12085 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
12086 {
12087 fonts_changed_p = 1;
12088 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
12089 = DESIRED_HEADER_LINE_HEIGHT (w);
12090 }
12091
12092 if (fonts_changed_p)
12093 goto need_larger_matrices;
12094 }
12095
12096 if (!line_number_displayed
12097 && !BUFFERP (w->base_line_pos))
12098 {
12099 w->base_line_pos = Qnil;
12100 w->base_line_number = Qnil;
12101 }
12102
12103 finish_menu_bars:
12104
12105 /* When we reach a frame's selected window, redo the frame's menu bar. */
12106 if (update_mode_line
12107 && EQ (FRAME_SELECTED_WINDOW (f), window))
12108 {
12109 int redisplay_menu_p = 0;
12110 int redisplay_tool_bar_p = 0;
12111
12112 if (FRAME_WINDOW_P (f))
12113 {
12114 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
12115 || defined (USE_GTK)
12116 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
12117 #else
12118 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
12119 #endif
12120 }
12121 else
12122 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
12123
12124 if (redisplay_menu_p)
12125 display_menu_bar (w);
12126
12127 #ifdef HAVE_WINDOW_SYSTEM
12128 #ifdef USE_GTK
12129 redisplay_tool_bar_p = FRAME_EXTERNAL_TOOL_BAR (f);
12130 #else
12131 redisplay_tool_bar_p = WINDOWP (f->tool_bar_window)
12132 && (FRAME_TOOL_BAR_LINES (f) > 0
12133 || auto_resize_tool_bars_p);
12134
12135 #endif
12136
12137 if (redisplay_tool_bar_p)
12138 redisplay_tool_bar (f);
12139 #endif
12140 }
12141
12142 #ifdef HAVE_WINDOW_SYSTEM
12143 if (update_window_fringes (w, 0)
12144 && !just_this_one_p
12145 && (used_current_matrix_p || overlay_arrow_seen)
12146 && !w->pseudo_window_p)
12147 {
12148 update_begin (f);
12149 BLOCK_INPUT;
12150 draw_window_fringes (w);
12151 UNBLOCK_INPUT;
12152 update_end (f);
12153 }
12154 #endif /* HAVE_WINDOW_SYSTEM */
12155
12156 /* We go to this label, with fonts_changed_p nonzero,
12157 if it is necessary to try again using larger glyph matrices.
12158 We have to redeem the scroll bar even in this case,
12159 because the loop in redisplay_internal expects that. */
12160 need_larger_matrices:
12161 ;
12162 finish_scroll_bars:
12163
12164 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
12165 {
12166 /* Set the thumb's position and size. */
12167 set_vertical_scroll_bar (w);
12168
12169 /* Note that we actually used the scroll bar attached to this
12170 window, so it shouldn't be deleted at the end of redisplay. */
12171 if (FRAME_DISPLAY (f)->redeem_scroll_bar_hook)
12172 (*FRAME_DISPLAY (f)->redeem_scroll_bar_hook) (w);
12173 }
12174
12175 /* Restore current_buffer and value of point in it. */
12176 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
12177 set_buffer_internal_1 (old);
12178 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
12179
12180 unbind_to (count, Qnil);
12181 }
12182
12183
12184 /* Build the complete desired matrix of WINDOW with a window start
12185 buffer position POS. Value is non-zero if successful. It is zero
12186 if fonts were loaded during redisplay which makes re-adjusting
12187 glyph matrices necessary. */
12188
12189 int
12190 try_window (window, pos)
12191 Lisp_Object window;
12192 struct text_pos pos;
12193 {
12194 struct window *w = XWINDOW (window);
12195 struct it it;
12196 struct glyph_row *last_text_row = NULL;
12197
12198 /* Make POS the new window start. */
12199 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
12200
12201 /* Mark cursor position as unknown. No overlay arrow seen. */
12202 w->cursor.vpos = -1;
12203 overlay_arrow_seen = 0;
12204
12205 /* Initialize iterator and info to start at POS. */
12206 start_display (&it, w, pos);
12207
12208 /* Display all lines of W. */
12209 while (it.current_y < it.last_visible_y)
12210 {
12211 if (display_line (&it))
12212 last_text_row = it.glyph_row - 1;
12213 if (fonts_changed_p)
12214 return 0;
12215 }
12216
12217 /* If bottom moved off end of frame, change mode line percentage. */
12218 if (XFASTINT (w->window_end_pos) <= 0
12219 && Z != IT_CHARPOS (it))
12220 w->update_mode_line = Qt;
12221
12222 /* Set window_end_pos to the offset of the last character displayed
12223 on the window from the end of current_buffer. Set
12224 window_end_vpos to its row number. */
12225 if (last_text_row)
12226 {
12227 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
12228 w->window_end_bytepos
12229 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
12230 w->window_end_pos
12231 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
12232 w->window_end_vpos
12233 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
12234 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
12235 ->displays_text_p);
12236 }
12237 else
12238 {
12239 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
12240 w->window_end_pos = make_number (Z - ZV);
12241 w->window_end_vpos = make_number (0);
12242 }
12243
12244 /* But that is not valid info until redisplay finishes. */
12245 w->window_end_valid = Qnil;
12246 return 1;
12247 }
12248
12249
12250 \f
12251 /************************************************************************
12252 Window redisplay reusing current matrix when buffer has not changed
12253 ************************************************************************/
12254
12255 /* Try redisplay of window W showing an unchanged buffer with a
12256 different window start than the last time it was displayed by
12257 reusing its current matrix. Value is non-zero if successful.
12258 W->start is the new window start. */
12259
12260 static int
12261 try_window_reusing_current_matrix (w)
12262 struct window *w;
12263 {
12264 struct frame *f = XFRAME (w->frame);
12265 struct glyph_row *row, *bottom_row;
12266 struct it it;
12267 struct run run;
12268 struct text_pos start, new_start;
12269 int nrows_scrolled, i;
12270 struct glyph_row *last_text_row;
12271 struct glyph_row *last_reused_text_row;
12272 struct glyph_row *start_row;
12273 int start_vpos, min_y, max_y;
12274
12275 #if GLYPH_DEBUG
12276 if (inhibit_try_window_reusing)
12277 return 0;
12278 #endif
12279
12280 if (/* This function doesn't handle terminal frames. */
12281 !FRAME_WINDOW_P (f)
12282 /* Don't try to reuse the display if windows have been split
12283 or such. */
12284 || windows_or_buffers_changed
12285 || cursor_type_changed)
12286 return 0;
12287
12288 /* Can't do this if region may have changed. */
12289 if ((!NILP (Vtransient_mark_mode)
12290 && !NILP (current_buffer->mark_active))
12291 || !NILP (w->region_showing)
12292 || !NILP (Vshow_trailing_whitespace))
12293 return 0;
12294
12295 /* If top-line visibility has changed, give up. */
12296 if (WINDOW_WANTS_HEADER_LINE_P (w)
12297 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
12298 return 0;
12299
12300 /* Give up if old or new display is scrolled vertically. We could
12301 make this function handle this, but right now it doesn't. */
12302 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
12303 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (start_row))
12304 return 0;
12305
12306 /* The variable new_start now holds the new window start. The old
12307 start `start' can be determined from the current matrix. */
12308 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
12309 start = start_row->start.pos;
12310 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
12311
12312 /* Clear the desired matrix for the display below. */
12313 clear_glyph_matrix (w->desired_matrix);
12314
12315 if (CHARPOS (new_start) <= CHARPOS (start))
12316 {
12317 int first_row_y;
12318
12319 /* Don't use this method if the display starts with an ellipsis
12320 displayed for invisible text. It's not easy to handle that case
12321 below, and it's certainly not worth the effort since this is
12322 not a frequent case. */
12323 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
12324 return 0;
12325
12326 IF_DEBUG (debug_method_add (w, "twu1"));
12327
12328 /* Display up to a row that can be reused. The variable
12329 last_text_row is set to the last row displayed that displays
12330 text. Note that it.vpos == 0 if or if not there is a
12331 header-line; it's not the same as the MATRIX_ROW_VPOS! */
12332 start_display (&it, w, new_start);
12333 first_row_y = it.current_y;
12334 w->cursor.vpos = -1;
12335 last_text_row = last_reused_text_row = NULL;
12336
12337 while (it.current_y < it.last_visible_y
12338 && IT_CHARPOS (it) < CHARPOS (start)
12339 && !fonts_changed_p)
12340 if (display_line (&it))
12341 last_text_row = it.glyph_row - 1;
12342
12343 /* A value of current_y < last_visible_y means that we stopped
12344 at the previous window start, which in turn means that we
12345 have at least one reusable row. */
12346 if (it.current_y < it.last_visible_y)
12347 {
12348 /* IT.vpos always starts from 0; it counts text lines. */
12349 nrows_scrolled = it.vpos;
12350
12351 /* Find PT if not already found in the lines displayed. */
12352 if (w->cursor.vpos < 0)
12353 {
12354 int dy = it.current_y - first_row_y;
12355
12356 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
12357 row = row_containing_pos (w, PT, row, NULL, dy);
12358 if (row)
12359 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
12360 dy, nrows_scrolled);
12361 else
12362 {
12363 clear_glyph_matrix (w->desired_matrix);
12364 return 0;
12365 }
12366 }
12367
12368 /* Scroll the display. Do it before the current matrix is
12369 changed. The problem here is that update has not yet
12370 run, i.e. part of the current matrix is not up to date.
12371 scroll_run_hook will clear the cursor, and use the
12372 current matrix to get the height of the row the cursor is
12373 in. */
12374 run.current_y = first_row_y;
12375 run.desired_y = it.current_y;
12376 run.height = it.last_visible_y - it.current_y;
12377
12378 if (run.height > 0 && run.current_y != run.desired_y)
12379 {
12380 update_begin (f);
12381 FRAME_RIF (f)->update_window_begin_hook (w);
12382 FRAME_RIF (f)->clear_window_mouse_face (w);
12383 FRAME_RIF (f)->scroll_run_hook (w, &run);
12384 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
12385 update_end (f);
12386 }
12387
12388 /* Shift current matrix down by nrows_scrolled lines. */
12389 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
12390 rotate_matrix (w->current_matrix,
12391 start_vpos,
12392 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
12393 nrows_scrolled);
12394
12395 /* Disable lines that must be updated. */
12396 for (i = 0; i < it.vpos; ++i)
12397 (start_row + i)->enabled_p = 0;
12398
12399 /* Re-compute Y positions. */
12400 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
12401 max_y = it.last_visible_y;
12402 for (row = start_row + nrows_scrolled;
12403 row < bottom_row;
12404 ++row)
12405 {
12406 row->y = it.current_y;
12407 row->visible_height = row->height;
12408
12409 if (row->y < min_y)
12410 row->visible_height -= min_y - row->y;
12411 if (row->y + row->height > max_y)
12412 row->visible_height -= row->y + row->height - max_y;
12413 row->redraw_fringe_bitmaps_p = 1;
12414
12415 it.current_y += row->height;
12416
12417 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
12418 last_reused_text_row = row;
12419 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
12420 break;
12421 }
12422
12423 /* Disable lines in the current matrix which are now
12424 below the window. */
12425 for (++row; row < bottom_row; ++row)
12426 row->enabled_p = 0;
12427 }
12428
12429 /* Update window_end_pos etc.; last_reused_text_row is the last
12430 reused row from the current matrix containing text, if any.
12431 The value of last_text_row is the last displayed line
12432 containing text. */
12433 if (last_reused_text_row)
12434 {
12435 w->window_end_bytepos
12436 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
12437 w->window_end_pos
12438 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
12439 w->window_end_vpos
12440 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
12441 w->current_matrix));
12442 }
12443 else if (last_text_row)
12444 {
12445 w->window_end_bytepos
12446 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
12447 w->window_end_pos
12448 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
12449 w->window_end_vpos
12450 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
12451 }
12452 else
12453 {
12454 /* This window must be completely empty. */
12455 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
12456 w->window_end_pos = make_number (Z - ZV);
12457 w->window_end_vpos = make_number (0);
12458 }
12459 w->window_end_valid = Qnil;
12460
12461 /* Update hint: don't try scrolling again in update_window. */
12462 w->desired_matrix->no_scrolling_p = 1;
12463
12464 #if GLYPH_DEBUG
12465 debug_method_add (w, "try_window_reusing_current_matrix 1");
12466 #endif
12467 return 1;
12468 }
12469 else if (CHARPOS (new_start) > CHARPOS (start))
12470 {
12471 struct glyph_row *pt_row, *row;
12472 struct glyph_row *first_reusable_row;
12473 struct glyph_row *first_row_to_display;
12474 int dy;
12475 int yb = window_text_bottom_y (w);
12476
12477 /* Find the row starting at new_start, if there is one. Don't
12478 reuse a partially visible line at the end. */
12479 first_reusable_row = start_row;
12480 while (first_reusable_row->enabled_p
12481 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
12482 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
12483 < CHARPOS (new_start)))
12484 ++first_reusable_row;
12485
12486 /* Give up if there is no row to reuse. */
12487 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
12488 || !first_reusable_row->enabled_p
12489 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
12490 != CHARPOS (new_start)))
12491 return 0;
12492
12493 /* We can reuse fully visible rows beginning with
12494 first_reusable_row to the end of the window. Set
12495 first_row_to_display to the first row that cannot be reused.
12496 Set pt_row to the row containing point, if there is any. */
12497 pt_row = NULL;
12498 for (first_row_to_display = first_reusable_row;
12499 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
12500 ++first_row_to_display)
12501 {
12502 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
12503 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
12504 pt_row = first_row_to_display;
12505 }
12506
12507 /* Start displaying at the start of first_row_to_display. */
12508 xassert (first_row_to_display->y < yb);
12509 init_to_row_start (&it, w, first_row_to_display);
12510
12511 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
12512 - start_vpos);
12513 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
12514 - nrows_scrolled);
12515 it.current_y = (first_row_to_display->y - first_reusable_row->y
12516 + WINDOW_HEADER_LINE_HEIGHT (w));
12517
12518 /* Display lines beginning with first_row_to_display in the
12519 desired matrix. Set last_text_row to the last row displayed
12520 that displays text. */
12521 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
12522 if (pt_row == NULL)
12523 w->cursor.vpos = -1;
12524 last_text_row = NULL;
12525 while (it.current_y < it.last_visible_y && !fonts_changed_p)
12526 if (display_line (&it))
12527 last_text_row = it.glyph_row - 1;
12528
12529 /* Give up If point isn't in a row displayed or reused. */
12530 if (w->cursor.vpos < 0)
12531 {
12532 clear_glyph_matrix (w->desired_matrix);
12533 return 0;
12534 }
12535
12536 /* If point is in a reused row, adjust y and vpos of the cursor
12537 position. */
12538 if (pt_row)
12539 {
12540 w->cursor.vpos -= MATRIX_ROW_VPOS (first_reusable_row,
12541 w->current_matrix);
12542 w->cursor.y -= first_reusable_row->y;
12543 }
12544
12545 /* Scroll the display. */
12546 run.current_y = first_reusable_row->y;
12547 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
12548 run.height = it.last_visible_y - run.current_y;
12549 dy = run.current_y - run.desired_y;
12550
12551 if (run.height)
12552 {
12553 update_begin (f);
12554 FRAME_RIF (f)->update_window_begin_hook (w);
12555 FRAME_RIF (f)->clear_window_mouse_face (w);
12556 FRAME_RIF (f)->scroll_run_hook (w, &run);
12557 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
12558 update_end (f);
12559 }
12560
12561 /* Adjust Y positions of reused rows. */
12562 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
12563 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
12564 max_y = it.last_visible_y;
12565 for (row = first_reusable_row; row < first_row_to_display; ++row)
12566 {
12567 row->y -= dy;
12568 row->visible_height = row->height;
12569 if (row->y < min_y)
12570 row->visible_height -= min_y - row->y;
12571 if (row->y + row->height > max_y)
12572 row->visible_height -= row->y + row->height - max_y;
12573 row->redraw_fringe_bitmaps_p = 1;
12574 }
12575
12576 /* Scroll the current matrix. */
12577 xassert (nrows_scrolled > 0);
12578 rotate_matrix (w->current_matrix,
12579 start_vpos,
12580 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
12581 -nrows_scrolled);
12582
12583 /* Disable rows not reused. */
12584 for (row -= nrows_scrolled; row < bottom_row; ++row)
12585 row->enabled_p = 0;
12586
12587 /* Adjust window end. A null value of last_text_row means that
12588 the window end is in reused rows which in turn means that
12589 only its vpos can have changed. */
12590 if (last_text_row)
12591 {
12592 w->window_end_bytepos
12593 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
12594 w->window_end_pos
12595 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
12596 w->window_end_vpos
12597 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
12598 }
12599 else
12600 {
12601 w->window_end_vpos
12602 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
12603 }
12604
12605 w->window_end_valid = Qnil;
12606 w->desired_matrix->no_scrolling_p = 1;
12607
12608 #if GLYPH_DEBUG
12609 debug_method_add (w, "try_window_reusing_current_matrix 2");
12610 #endif
12611 return 1;
12612 }
12613
12614 return 0;
12615 }
12616
12617
12618 \f
12619 /************************************************************************
12620 Window redisplay reusing current matrix when buffer has changed
12621 ************************************************************************/
12622
12623 static struct glyph_row *find_last_unchanged_at_beg_row P_ ((struct window *));
12624 static struct glyph_row *find_first_unchanged_at_end_row P_ ((struct window *,
12625 int *, int *));
12626 static struct glyph_row *
12627 find_last_row_displaying_text P_ ((struct glyph_matrix *, struct it *,
12628 struct glyph_row *));
12629
12630
12631 /* Return the last row in MATRIX displaying text. If row START is
12632 non-null, start searching with that row. IT gives the dimensions
12633 of the display. Value is null if matrix is empty; otherwise it is
12634 a pointer to the row found. */
12635
12636 static struct glyph_row *
12637 find_last_row_displaying_text (matrix, it, start)
12638 struct glyph_matrix *matrix;
12639 struct it *it;
12640 struct glyph_row *start;
12641 {
12642 struct glyph_row *row, *row_found;
12643
12644 /* Set row_found to the last row in IT->w's current matrix
12645 displaying text. The loop looks funny but think of partially
12646 visible lines. */
12647 row_found = NULL;
12648 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
12649 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
12650 {
12651 xassert (row->enabled_p);
12652 row_found = row;
12653 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
12654 break;
12655 ++row;
12656 }
12657
12658 return row_found;
12659 }
12660
12661
12662 /* Return the last row in the current matrix of W that is not affected
12663 by changes at the start of current_buffer that occurred since W's
12664 current matrix was built. Value is null if no such row exists.
12665
12666 BEG_UNCHANGED us the number of characters unchanged at the start of
12667 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
12668 first changed character in current_buffer. Characters at positions <
12669 BEG + BEG_UNCHANGED are at the same buffer positions as they were
12670 when the current matrix was built. */
12671
12672 static struct glyph_row *
12673 find_last_unchanged_at_beg_row (w)
12674 struct window *w;
12675 {
12676 int first_changed_pos = BEG + BEG_UNCHANGED;
12677 struct glyph_row *row;
12678 struct glyph_row *row_found = NULL;
12679 int yb = window_text_bottom_y (w);
12680
12681 /* Find the last row displaying unchanged text. */
12682 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
12683 while (MATRIX_ROW_DISPLAYS_TEXT_P (row)
12684 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos)
12685 {
12686 if (/* If row ends before first_changed_pos, it is unchanged,
12687 except in some case. */
12688 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
12689 /* When row ends in ZV and we write at ZV it is not
12690 unchanged. */
12691 && !row->ends_at_zv_p
12692 /* When first_changed_pos is the end of a continued line,
12693 row is not unchanged because it may be no longer
12694 continued. */
12695 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
12696 && (row->continued_p
12697 || row->exact_window_width_line_p)))
12698 row_found = row;
12699
12700 /* Stop if last visible row. */
12701 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
12702 break;
12703
12704 ++row;
12705 }
12706
12707 return row_found;
12708 }
12709
12710
12711 /* Find the first glyph row in the current matrix of W that is not
12712 affected by changes at the end of current_buffer since the
12713 time W's current matrix was built.
12714
12715 Return in *DELTA the number of chars by which buffer positions in
12716 unchanged text at the end of current_buffer must be adjusted.
12717
12718 Return in *DELTA_BYTES the corresponding number of bytes.
12719
12720 Value is null if no such row exists, i.e. all rows are affected by
12721 changes. */
12722
12723 static struct glyph_row *
12724 find_first_unchanged_at_end_row (w, delta, delta_bytes)
12725 struct window *w;
12726 int *delta, *delta_bytes;
12727 {
12728 struct glyph_row *row;
12729 struct glyph_row *row_found = NULL;
12730
12731 *delta = *delta_bytes = 0;
12732
12733 /* Display must not have been paused, otherwise the current matrix
12734 is not up to date. */
12735 if (NILP (w->window_end_valid))
12736 abort ();
12737
12738 /* A value of window_end_pos >= END_UNCHANGED means that the window
12739 end is in the range of changed text. If so, there is no
12740 unchanged row at the end of W's current matrix. */
12741 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
12742 return NULL;
12743
12744 /* Set row to the last row in W's current matrix displaying text. */
12745 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
12746
12747 /* If matrix is entirely empty, no unchanged row exists. */
12748 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
12749 {
12750 /* The value of row is the last glyph row in the matrix having a
12751 meaningful buffer position in it. The end position of row
12752 corresponds to window_end_pos. This allows us to translate
12753 buffer positions in the current matrix to current buffer
12754 positions for characters not in changed text. */
12755 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
12756 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
12757 int last_unchanged_pos, last_unchanged_pos_old;
12758 struct glyph_row *first_text_row
12759 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
12760
12761 *delta = Z - Z_old;
12762 *delta_bytes = Z_BYTE - Z_BYTE_old;
12763
12764 /* Set last_unchanged_pos to the buffer position of the last
12765 character in the buffer that has not been changed. Z is the
12766 index + 1 of the last character in current_buffer, i.e. by
12767 subtracting END_UNCHANGED we get the index of the last
12768 unchanged character, and we have to add BEG to get its buffer
12769 position. */
12770 last_unchanged_pos = Z - END_UNCHANGED + BEG;
12771 last_unchanged_pos_old = last_unchanged_pos - *delta;
12772
12773 /* Search backward from ROW for a row displaying a line that
12774 starts at a minimum position >= last_unchanged_pos_old. */
12775 for (; row > first_text_row; --row)
12776 {
12777 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
12778 abort ();
12779
12780 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
12781 row_found = row;
12782 }
12783 }
12784
12785 if (row_found && !MATRIX_ROW_DISPLAYS_TEXT_P (row_found))
12786 abort ();
12787
12788 return row_found;
12789 }
12790
12791
12792 /* Make sure that glyph rows in the current matrix of window W
12793 reference the same glyph memory as corresponding rows in the
12794 frame's frame matrix. This function is called after scrolling W's
12795 current matrix on a terminal frame in try_window_id and
12796 try_window_reusing_current_matrix. */
12797
12798 static void
12799 sync_frame_with_window_matrix_rows (w)
12800 struct window *w;
12801 {
12802 struct frame *f = XFRAME (w->frame);
12803 struct glyph_row *window_row, *window_row_end, *frame_row;
12804
12805 /* Preconditions: W must be a leaf window and full-width. Its frame
12806 must have a frame matrix. */
12807 xassert (NILP (w->hchild) && NILP (w->vchild));
12808 xassert (WINDOW_FULL_WIDTH_P (w));
12809 xassert (!FRAME_WINDOW_P (f));
12810
12811 /* If W is a full-width window, glyph pointers in W's current matrix
12812 have, by definition, to be the same as glyph pointers in the
12813 corresponding frame matrix. Note that frame matrices have no
12814 marginal areas (see build_frame_matrix). */
12815 window_row = w->current_matrix->rows;
12816 window_row_end = window_row + w->current_matrix->nrows;
12817 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
12818 while (window_row < window_row_end)
12819 {
12820 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
12821 struct glyph *end = window_row->glyphs[LAST_AREA];
12822
12823 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
12824 frame_row->glyphs[TEXT_AREA] = start;
12825 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
12826 frame_row->glyphs[LAST_AREA] = end;
12827
12828 /* Disable frame rows whose corresponding window rows have
12829 been disabled in try_window_id. */
12830 if (!window_row->enabled_p)
12831 frame_row->enabled_p = 0;
12832
12833 ++window_row, ++frame_row;
12834 }
12835 }
12836
12837
12838 /* Find the glyph row in window W containing CHARPOS. Consider all
12839 rows between START and END (not inclusive). END null means search
12840 all rows to the end of the display area of W. Value is the row
12841 containing CHARPOS or null. */
12842
12843 struct glyph_row *
12844 row_containing_pos (w, charpos, start, end, dy)
12845 struct window *w;
12846 int charpos;
12847 struct glyph_row *start, *end;
12848 int dy;
12849 {
12850 struct glyph_row *row = start;
12851 int last_y;
12852
12853 /* If we happen to start on a header-line, skip that. */
12854 if (row->mode_line_p)
12855 ++row;
12856
12857 if ((end && row >= end) || !row->enabled_p)
12858 return NULL;
12859
12860 last_y = window_text_bottom_y (w) - dy;
12861
12862 while (1)
12863 {
12864 /* Give up if we have gone too far. */
12865 if (end && row >= end)
12866 return NULL;
12867 /* This formerly returned if they were equal.
12868 I think that both quantities are of a "last plus one" type;
12869 if so, when they are equal, the row is within the screen. -- rms. */
12870 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
12871 return NULL;
12872
12873 /* If it is in this row, return this row. */
12874 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
12875 || (MATRIX_ROW_END_CHARPOS (row) == charpos
12876 /* The end position of a row equals the start
12877 position of the next row. If CHARPOS is there, we
12878 would rather display it in the next line, except
12879 when this line ends in ZV. */
12880 && !row->ends_at_zv_p
12881 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
12882 && charpos >= MATRIX_ROW_START_CHARPOS (row))
12883 return row;
12884 ++row;
12885 }
12886 }
12887
12888
12889 /* Try to redisplay window W by reusing its existing display. W's
12890 current matrix must be up to date when this function is called,
12891 i.e. window_end_valid must not be nil.
12892
12893 Value is
12894
12895 1 if display has been updated
12896 0 if otherwise unsuccessful
12897 -1 if redisplay with same window start is known not to succeed
12898
12899 The following steps are performed:
12900
12901 1. Find the last row in the current matrix of W that is not
12902 affected by changes at the start of current_buffer. If no such row
12903 is found, give up.
12904
12905 2. Find the first row in W's current matrix that is not affected by
12906 changes at the end of current_buffer. Maybe there is no such row.
12907
12908 3. Display lines beginning with the row + 1 found in step 1 to the
12909 row found in step 2 or, if step 2 didn't find a row, to the end of
12910 the window.
12911
12912 4. If cursor is not known to appear on the window, give up.
12913
12914 5. If display stopped at the row found in step 2, scroll the
12915 display and current matrix as needed.
12916
12917 6. Maybe display some lines at the end of W, if we must. This can
12918 happen under various circumstances, like a partially visible line
12919 becoming fully visible, or because newly displayed lines are displayed
12920 in smaller font sizes.
12921
12922 7. Update W's window end information. */
12923
12924 static int
12925 try_window_id (w)
12926 struct window *w;
12927 {
12928 struct frame *f = XFRAME (w->frame);
12929 struct glyph_matrix *current_matrix = w->current_matrix;
12930 struct glyph_matrix *desired_matrix = w->desired_matrix;
12931 struct glyph_row *last_unchanged_at_beg_row;
12932 struct glyph_row *first_unchanged_at_end_row;
12933 struct glyph_row *row;
12934 struct glyph_row *bottom_row;
12935 int bottom_vpos;
12936 struct it it;
12937 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
12938 struct text_pos start_pos;
12939 struct run run;
12940 int first_unchanged_at_end_vpos = 0;
12941 struct glyph_row *last_text_row, *last_text_row_at_end;
12942 struct text_pos start;
12943 int first_changed_charpos, last_changed_charpos;
12944
12945 #if GLYPH_DEBUG
12946 if (inhibit_try_window_id)
12947 return 0;
12948 #endif
12949
12950 /* This is handy for debugging. */
12951 #if 0
12952 #define GIVE_UP(X) \
12953 do { \
12954 fprintf (stderr, "try_window_id give up %d\n", (X)); \
12955 return 0; \
12956 } while (0)
12957 #else
12958 #define GIVE_UP(X) return 0
12959 #endif
12960
12961 SET_TEXT_POS_FROM_MARKER (start, w->start);
12962
12963 /* Don't use this for mini-windows because these can show
12964 messages and mini-buffers, and we don't handle that here. */
12965 if (MINI_WINDOW_P (w))
12966 GIVE_UP (1);
12967
12968 /* This flag is used to prevent redisplay optimizations. */
12969 if (windows_or_buffers_changed || cursor_type_changed)
12970 GIVE_UP (2);
12971
12972 /* Verify that narrowing has not changed.
12973 Also verify that we were not told to prevent redisplay optimizations.
12974 It would be nice to further
12975 reduce the number of cases where this prevents try_window_id. */
12976 if (current_buffer->clip_changed
12977 || current_buffer->prevent_redisplay_optimizations_p)
12978 GIVE_UP (3);
12979
12980 /* Window must either use window-based redisplay or be full width. */
12981 if (!FRAME_WINDOW_P (f)
12982 && (!FRAME_LINE_INS_DEL_OK (f)
12983 || !WINDOW_FULL_WIDTH_P (w)))
12984 GIVE_UP (4);
12985
12986 /* Give up if point is not known NOT to appear in W. */
12987 if (PT < CHARPOS (start))
12988 GIVE_UP (5);
12989
12990 /* Another way to prevent redisplay optimizations. */
12991 if (XFASTINT (w->last_modified) == 0)
12992 GIVE_UP (6);
12993
12994 /* Verify that window is not hscrolled. */
12995 if (XFASTINT (w->hscroll) != 0)
12996 GIVE_UP (7);
12997
12998 /* Verify that display wasn't paused. */
12999 if (NILP (w->window_end_valid))
13000 GIVE_UP (8);
13001
13002 /* Can't use this if highlighting a region because a cursor movement
13003 will do more than just set the cursor. */
13004 if (!NILP (Vtransient_mark_mode)
13005 && !NILP (current_buffer->mark_active))
13006 GIVE_UP (9);
13007
13008 /* Likewise if highlighting trailing whitespace. */
13009 if (!NILP (Vshow_trailing_whitespace))
13010 GIVE_UP (11);
13011
13012 /* Likewise if showing a region. */
13013 if (!NILP (w->region_showing))
13014 GIVE_UP (10);
13015
13016 /* Can use this if overlay arrow position and or string have changed. */
13017 if (overlay_arrows_changed_p ())
13018 GIVE_UP (12);
13019
13020
13021 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
13022 only if buffer has really changed. The reason is that the gap is
13023 initially at Z for freshly visited files. The code below would
13024 set end_unchanged to 0 in that case. */
13025 if (MODIFF > SAVE_MODIFF
13026 /* This seems to happen sometimes after saving a buffer. */
13027 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
13028 {
13029 if (GPT - BEG < BEG_UNCHANGED)
13030 BEG_UNCHANGED = GPT - BEG;
13031 if (Z - GPT < END_UNCHANGED)
13032 END_UNCHANGED = Z - GPT;
13033 }
13034
13035 /* The position of the first and last character that has been changed. */
13036 first_changed_charpos = BEG + BEG_UNCHANGED;
13037 last_changed_charpos = Z - END_UNCHANGED;
13038
13039 /* If window starts after a line end, and the last change is in
13040 front of that newline, then changes don't affect the display.
13041 This case happens with stealth-fontification. Note that although
13042 the display is unchanged, glyph positions in the matrix have to
13043 be adjusted, of course. */
13044 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
13045 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
13046 && ((last_changed_charpos < CHARPOS (start)
13047 && CHARPOS (start) == BEGV)
13048 || (last_changed_charpos < CHARPOS (start) - 1
13049 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
13050 {
13051 int Z_old, delta, Z_BYTE_old, delta_bytes;
13052 struct glyph_row *r0;
13053
13054 /* Compute how many chars/bytes have been added to or removed
13055 from the buffer. */
13056 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
13057 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
13058 delta = Z - Z_old;
13059 delta_bytes = Z_BYTE - Z_BYTE_old;
13060
13061 /* Give up if PT is not in the window. Note that it already has
13062 been checked at the start of try_window_id that PT is not in
13063 front of the window start. */
13064 if (PT >= MATRIX_ROW_END_CHARPOS (row) + delta)
13065 GIVE_UP (13);
13066
13067 /* If window start is unchanged, we can reuse the whole matrix
13068 as is, after adjusting glyph positions. No need to compute
13069 the window end again, since its offset from Z hasn't changed. */
13070 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
13071 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + delta
13072 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + delta_bytes
13073 /* PT must not be in a partially visible line. */
13074 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + delta
13075 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
13076 {
13077 /* Adjust positions in the glyph matrix. */
13078 if (delta || delta_bytes)
13079 {
13080 struct glyph_row *r1
13081 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
13082 increment_matrix_positions (w->current_matrix,
13083 MATRIX_ROW_VPOS (r0, current_matrix),
13084 MATRIX_ROW_VPOS (r1, current_matrix),
13085 delta, delta_bytes);
13086 }
13087
13088 /* Set the cursor. */
13089 row = row_containing_pos (w, PT, r0, NULL, 0);
13090 if (row)
13091 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
13092 else
13093 abort ();
13094 return 1;
13095 }
13096 }
13097
13098 /* Handle the case that changes are all below what is displayed in
13099 the window, and that PT is in the window. This shortcut cannot
13100 be taken if ZV is visible in the window, and text has been added
13101 there that is visible in the window. */
13102 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
13103 /* ZV is not visible in the window, or there are no
13104 changes at ZV, actually. */
13105 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
13106 || first_changed_charpos == last_changed_charpos))
13107 {
13108 struct glyph_row *r0;
13109
13110 /* Give up if PT is not in the window. Note that it already has
13111 been checked at the start of try_window_id that PT is not in
13112 front of the window start. */
13113 if (PT >= MATRIX_ROW_END_CHARPOS (row))
13114 GIVE_UP (14);
13115
13116 /* If window start is unchanged, we can reuse the whole matrix
13117 as is, without changing glyph positions since no text has
13118 been added/removed in front of the window end. */
13119 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
13120 if (TEXT_POS_EQUAL_P (start, r0->start.pos)
13121 /* PT must not be in a partially visible line. */
13122 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
13123 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
13124 {
13125 /* We have to compute the window end anew since text
13126 can have been added/removed after it. */
13127 w->window_end_pos
13128 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
13129 w->window_end_bytepos
13130 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
13131
13132 /* Set the cursor. */
13133 row = row_containing_pos (w, PT, r0, NULL, 0);
13134 if (row)
13135 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
13136 else
13137 abort ();
13138 return 2;
13139 }
13140 }
13141
13142 /* Give up if window start is in the changed area.
13143
13144 The condition used to read
13145
13146 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
13147
13148 but why that was tested escapes me at the moment. */
13149 if (CHARPOS (start) >= first_changed_charpos
13150 && CHARPOS (start) <= last_changed_charpos)
13151 GIVE_UP (15);
13152
13153 /* Check that window start agrees with the start of the first glyph
13154 row in its current matrix. Check this after we know the window
13155 start is not in changed text, otherwise positions would not be
13156 comparable. */
13157 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
13158 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
13159 GIVE_UP (16);
13160
13161 /* Give up if the window ends in strings. Overlay strings
13162 at the end are difficult to handle, so don't try. */
13163 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
13164 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
13165 GIVE_UP (20);
13166
13167 /* Compute the position at which we have to start displaying new
13168 lines. Some of the lines at the top of the window might be
13169 reusable because they are not displaying changed text. Find the
13170 last row in W's current matrix not affected by changes at the
13171 start of current_buffer. Value is null if changes start in the
13172 first line of window. */
13173 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
13174 if (last_unchanged_at_beg_row)
13175 {
13176 /* Avoid starting to display in the moddle of a character, a TAB
13177 for instance. This is easier than to set up the iterator
13178 exactly, and it's not a frequent case, so the additional
13179 effort wouldn't really pay off. */
13180 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
13181 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
13182 && last_unchanged_at_beg_row > w->current_matrix->rows)
13183 --last_unchanged_at_beg_row;
13184
13185 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
13186 GIVE_UP (17);
13187
13188 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
13189 GIVE_UP (18);
13190 start_pos = it.current.pos;
13191
13192 /* Start displaying new lines in the desired matrix at the same
13193 vpos we would use in the current matrix, i.e. below
13194 last_unchanged_at_beg_row. */
13195 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
13196 current_matrix);
13197 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
13198 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
13199
13200 xassert (it.hpos == 0 && it.current_x == 0);
13201 }
13202 else
13203 {
13204 /* There are no reusable lines at the start of the window.
13205 Start displaying in the first text line. */
13206 start_display (&it, w, start);
13207 it.vpos = it.first_vpos;
13208 start_pos = it.current.pos;
13209 }
13210
13211 /* Find the first row that is not affected by changes at the end of
13212 the buffer. Value will be null if there is no unchanged row, in
13213 which case we must redisplay to the end of the window. delta
13214 will be set to the value by which buffer positions beginning with
13215 first_unchanged_at_end_row have to be adjusted due to text
13216 changes. */
13217 first_unchanged_at_end_row
13218 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
13219 IF_DEBUG (debug_delta = delta);
13220 IF_DEBUG (debug_delta_bytes = delta_bytes);
13221
13222 /* Set stop_pos to the buffer position up to which we will have to
13223 display new lines. If first_unchanged_at_end_row != NULL, this
13224 is the buffer position of the start of the line displayed in that
13225 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
13226 that we don't stop at a buffer position. */
13227 stop_pos = 0;
13228 if (first_unchanged_at_end_row)
13229 {
13230 xassert (last_unchanged_at_beg_row == NULL
13231 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
13232
13233 /* If this is a continuation line, move forward to the next one
13234 that isn't. Changes in lines above affect this line.
13235 Caution: this may move first_unchanged_at_end_row to a row
13236 not displaying text. */
13237 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
13238 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
13239 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
13240 < it.last_visible_y))
13241 ++first_unchanged_at_end_row;
13242
13243 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
13244 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
13245 >= it.last_visible_y))
13246 first_unchanged_at_end_row = NULL;
13247 else
13248 {
13249 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
13250 + delta);
13251 first_unchanged_at_end_vpos
13252 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
13253 xassert (stop_pos >= Z - END_UNCHANGED);
13254 }
13255 }
13256 else if (last_unchanged_at_beg_row == NULL)
13257 GIVE_UP (19);
13258
13259
13260 #if GLYPH_DEBUG
13261
13262 /* Either there is no unchanged row at the end, or the one we have
13263 now displays text. This is a necessary condition for the window
13264 end pos calculation at the end of this function. */
13265 xassert (first_unchanged_at_end_row == NULL
13266 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
13267
13268 debug_last_unchanged_at_beg_vpos
13269 = (last_unchanged_at_beg_row
13270 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
13271 : -1);
13272 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
13273
13274 #endif /* GLYPH_DEBUG != 0 */
13275
13276
13277 /* Display new lines. Set last_text_row to the last new line
13278 displayed which has text on it, i.e. might end up as being the
13279 line where the window_end_vpos is. */
13280 w->cursor.vpos = -1;
13281 last_text_row = NULL;
13282 overlay_arrow_seen = 0;
13283 while (it.current_y < it.last_visible_y
13284 && !fonts_changed_p
13285 && (first_unchanged_at_end_row == NULL
13286 || IT_CHARPOS (it) < stop_pos))
13287 {
13288 if (display_line (&it))
13289 last_text_row = it.glyph_row - 1;
13290 }
13291
13292 if (fonts_changed_p)
13293 return -1;
13294
13295
13296 /* Compute differences in buffer positions, y-positions etc. for
13297 lines reused at the bottom of the window. Compute what we can
13298 scroll. */
13299 if (first_unchanged_at_end_row
13300 /* No lines reused because we displayed everything up to the
13301 bottom of the window. */
13302 && it.current_y < it.last_visible_y)
13303 {
13304 dvpos = (it.vpos
13305 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
13306 current_matrix));
13307 dy = it.current_y - first_unchanged_at_end_row->y;
13308 run.current_y = first_unchanged_at_end_row->y;
13309 run.desired_y = run.current_y + dy;
13310 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
13311 }
13312 else
13313 {
13314 delta = dvpos = dy = run.current_y = run.desired_y = run.height = 0;
13315 first_unchanged_at_end_row = NULL;
13316 }
13317 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
13318
13319
13320 /* Find the cursor if not already found. We have to decide whether
13321 PT will appear on this window (it sometimes doesn't, but this is
13322 not a very frequent case.) This decision has to be made before
13323 the current matrix is altered. A value of cursor.vpos < 0 means
13324 that PT is either in one of the lines beginning at
13325 first_unchanged_at_end_row or below the window. Don't care for
13326 lines that might be displayed later at the window end; as
13327 mentioned, this is not a frequent case. */
13328 if (w->cursor.vpos < 0)
13329 {
13330 /* Cursor in unchanged rows at the top? */
13331 if (PT < CHARPOS (start_pos)
13332 && last_unchanged_at_beg_row)
13333 {
13334 row = row_containing_pos (w, PT,
13335 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
13336 last_unchanged_at_beg_row + 1, 0);
13337 if (row)
13338 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13339 }
13340
13341 /* Start from first_unchanged_at_end_row looking for PT. */
13342 else if (first_unchanged_at_end_row)
13343 {
13344 row = row_containing_pos (w, PT - delta,
13345 first_unchanged_at_end_row, NULL, 0);
13346 if (row)
13347 set_cursor_from_row (w, row, w->current_matrix, delta,
13348 delta_bytes, dy, dvpos);
13349 }
13350
13351 /* Give up if cursor was not found. */
13352 if (w->cursor.vpos < 0)
13353 {
13354 clear_glyph_matrix (w->desired_matrix);
13355 return -1;
13356 }
13357 }
13358
13359 /* Don't let the cursor end in the scroll margins. */
13360 {
13361 int this_scroll_margin, cursor_height;
13362
13363 this_scroll_margin = max (0, scroll_margin);
13364 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
13365 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
13366 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
13367
13368 if ((w->cursor.y < this_scroll_margin
13369 && CHARPOS (start) > BEGV)
13370 /* Don't take scroll margin into account at the bottom because
13371 old redisplay didn't do it either. */
13372 || w->cursor.y + cursor_height > it.last_visible_y)
13373 {
13374 w->cursor.vpos = -1;
13375 clear_glyph_matrix (w->desired_matrix);
13376 return -1;
13377 }
13378 }
13379
13380 /* Scroll the display. Do it before changing the current matrix so
13381 that xterm.c doesn't get confused about where the cursor glyph is
13382 found. */
13383 if (dy && run.height)
13384 {
13385 update_begin (f);
13386
13387 if (FRAME_WINDOW_P (f))
13388 {
13389 FRAME_RIF (f)->update_window_begin_hook (w);
13390 FRAME_RIF (f)->clear_window_mouse_face (w);
13391 FRAME_RIF (f)->scroll_run_hook (w, &run);
13392 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
13393 }
13394 else
13395 {
13396 /* Terminal frame. In this case, dvpos gives the number of
13397 lines to scroll by; dvpos < 0 means scroll up. */
13398 int first_unchanged_at_end_vpos
13399 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
13400 int from = WINDOW_TOP_EDGE_LINE (w) + first_unchanged_at_end_vpos;
13401 int end = (WINDOW_TOP_EDGE_LINE (w)
13402 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
13403 + window_internal_height (w));
13404
13405 /* Perform the operation on the screen. */
13406 if (dvpos > 0)
13407 {
13408 /* Scroll last_unchanged_at_beg_row to the end of the
13409 window down dvpos lines. */
13410 set_terminal_window (f, end);
13411
13412 /* On dumb terminals delete dvpos lines at the end
13413 before inserting dvpos empty lines. */
13414 if (!FRAME_SCROLL_REGION_OK (f))
13415 ins_del_lines (f, end - dvpos, -dvpos);
13416
13417 /* Insert dvpos empty lines in front of
13418 last_unchanged_at_beg_row. */
13419 ins_del_lines (f, from, dvpos);
13420 }
13421 else if (dvpos < 0)
13422 {
13423 /* Scroll up last_unchanged_at_beg_vpos to the end of
13424 the window to last_unchanged_at_beg_vpos - |dvpos|. */
13425 set_terminal_window (f, end);
13426
13427 /* Delete dvpos lines in front of
13428 last_unchanged_at_beg_vpos. ins_del_lines will set
13429 the cursor to the given vpos and emit |dvpos| delete
13430 line sequences. */
13431 ins_del_lines (f, from + dvpos, dvpos);
13432
13433 /* On a dumb terminal insert dvpos empty lines at the
13434 end. */
13435 if (!FRAME_SCROLL_REGION_OK (f))
13436 ins_del_lines (f, end + dvpos, -dvpos);
13437 }
13438
13439 set_terminal_window (f, 0);
13440 }
13441
13442 update_end (f);
13443 }
13444
13445 /* Shift reused rows of the current matrix to the right position.
13446 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
13447 text. */
13448 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
13449 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
13450 if (dvpos < 0)
13451 {
13452 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
13453 bottom_vpos, dvpos);
13454 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
13455 bottom_vpos, 0);
13456 }
13457 else if (dvpos > 0)
13458 {
13459 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
13460 bottom_vpos, dvpos);
13461 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
13462 first_unchanged_at_end_vpos + dvpos, 0);
13463 }
13464
13465 /* For frame-based redisplay, make sure that current frame and window
13466 matrix are in sync with respect to glyph memory. */
13467 if (!FRAME_WINDOW_P (f))
13468 sync_frame_with_window_matrix_rows (w);
13469
13470 /* Adjust buffer positions in reused rows. */
13471 if (delta)
13472 increment_matrix_positions (current_matrix,
13473 first_unchanged_at_end_vpos + dvpos,
13474 bottom_vpos, delta, delta_bytes);
13475
13476 /* Adjust Y positions. */
13477 if (dy)
13478 shift_glyph_matrix (w, current_matrix,
13479 first_unchanged_at_end_vpos + dvpos,
13480 bottom_vpos, dy);
13481
13482 if (first_unchanged_at_end_row)
13483 first_unchanged_at_end_row += dvpos;
13484
13485 /* If scrolling up, there may be some lines to display at the end of
13486 the window. */
13487 last_text_row_at_end = NULL;
13488 if (dy < 0)
13489 {
13490 /* Scrolling up can leave for example a partially visible line
13491 at the end of the window to be redisplayed. */
13492 /* Set last_row to the glyph row in the current matrix where the
13493 window end line is found. It has been moved up or down in
13494 the matrix by dvpos. */
13495 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
13496 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
13497
13498 /* If last_row is the window end line, it should display text. */
13499 xassert (last_row->displays_text_p);
13500
13501 /* If window end line was partially visible before, begin
13502 displaying at that line. Otherwise begin displaying with the
13503 line following it. */
13504 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
13505 {
13506 init_to_row_start (&it, w, last_row);
13507 it.vpos = last_vpos;
13508 it.current_y = last_row->y;
13509 }
13510 else
13511 {
13512 init_to_row_end (&it, w, last_row);
13513 it.vpos = 1 + last_vpos;
13514 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
13515 ++last_row;
13516 }
13517
13518 /* We may start in a continuation line. If so, we have to
13519 get the right continuation_lines_width and current_x. */
13520 it.continuation_lines_width = last_row->continuation_lines_width;
13521 it.hpos = it.current_x = 0;
13522
13523 /* Display the rest of the lines at the window end. */
13524 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
13525 while (it.current_y < it.last_visible_y
13526 && !fonts_changed_p)
13527 {
13528 /* Is it always sure that the display agrees with lines in
13529 the current matrix? I don't think so, so we mark rows
13530 displayed invalid in the current matrix by setting their
13531 enabled_p flag to zero. */
13532 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
13533 if (display_line (&it))
13534 last_text_row_at_end = it.glyph_row - 1;
13535 }
13536 }
13537
13538 /* Update window_end_pos and window_end_vpos. */
13539 if (first_unchanged_at_end_row
13540 && first_unchanged_at_end_row->y < it.last_visible_y
13541 && !last_text_row_at_end)
13542 {
13543 /* Window end line if one of the preserved rows from the current
13544 matrix. Set row to the last row displaying text in current
13545 matrix starting at first_unchanged_at_end_row, after
13546 scrolling. */
13547 xassert (first_unchanged_at_end_row->displays_text_p);
13548 row = find_last_row_displaying_text (w->current_matrix, &it,
13549 first_unchanged_at_end_row);
13550 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
13551
13552 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
13553 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
13554 w->window_end_vpos
13555 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
13556 xassert (w->window_end_bytepos >= 0);
13557 IF_DEBUG (debug_method_add (w, "A"));
13558 }
13559 else if (last_text_row_at_end)
13560 {
13561 w->window_end_pos
13562 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
13563 w->window_end_bytepos
13564 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
13565 w->window_end_vpos
13566 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
13567 xassert (w->window_end_bytepos >= 0);
13568 IF_DEBUG (debug_method_add (w, "B"));
13569 }
13570 else if (last_text_row)
13571 {
13572 /* We have displayed either to the end of the window or at the
13573 end of the window, i.e. the last row with text is to be found
13574 in the desired matrix. */
13575 w->window_end_pos
13576 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
13577 w->window_end_bytepos
13578 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
13579 w->window_end_vpos
13580 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
13581 xassert (w->window_end_bytepos >= 0);
13582 }
13583 else if (first_unchanged_at_end_row == NULL
13584 && last_text_row == NULL
13585 && last_text_row_at_end == NULL)
13586 {
13587 /* Displayed to end of window, but no line containing text was
13588 displayed. Lines were deleted at the end of the window. */
13589 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
13590 int vpos = XFASTINT (w->window_end_vpos);
13591 struct glyph_row *current_row = current_matrix->rows + vpos;
13592 struct glyph_row *desired_row = desired_matrix->rows + vpos;
13593
13594 for (row = NULL;
13595 row == NULL && vpos >= first_vpos;
13596 --vpos, --current_row, --desired_row)
13597 {
13598 if (desired_row->enabled_p)
13599 {
13600 if (desired_row->displays_text_p)
13601 row = desired_row;
13602 }
13603 else if (current_row->displays_text_p)
13604 row = current_row;
13605 }
13606
13607 xassert (row != NULL);
13608 w->window_end_vpos = make_number (vpos + 1);
13609 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
13610 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
13611 xassert (w->window_end_bytepos >= 0);
13612 IF_DEBUG (debug_method_add (w, "C"));
13613 }
13614 else
13615 abort ();
13616
13617 #if 0 /* This leads to problems, for instance when the cursor is
13618 at ZV, and the cursor line displays no text. */
13619 /* Disable rows below what's displayed in the window. This makes
13620 debugging easier. */
13621 enable_glyph_matrix_rows (current_matrix,
13622 XFASTINT (w->window_end_vpos) + 1,
13623 bottom_vpos, 0);
13624 #endif
13625
13626 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
13627 debug_end_vpos = XFASTINT (w->window_end_vpos));
13628
13629 /* Record that display has not been completed. */
13630 w->window_end_valid = Qnil;
13631 w->desired_matrix->no_scrolling_p = 1;
13632 return 3;
13633
13634 #undef GIVE_UP
13635 }
13636
13637
13638 \f
13639 /***********************************************************************
13640 More debugging support
13641 ***********************************************************************/
13642
13643 #if GLYPH_DEBUG
13644
13645 void dump_glyph_row P_ ((struct glyph_row *, int, int));
13646 void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
13647 void dump_glyph P_ ((struct glyph_row *, struct glyph *, int));
13648
13649
13650 /* Dump the contents of glyph matrix MATRIX on stderr.
13651
13652 GLYPHS 0 means don't show glyph contents.
13653 GLYPHS 1 means show glyphs in short form
13654 GLYPHS > 1 means show glyphs in long form. */
13655
13656 void
13657 dump_glyph_matrix (matrix, glyphs)
13658 struct glyph_matrix *matrix;
13659 int glyphs;
13660 {
13661 int i;
13662 for (i = 0; i < matrix->nrows; ++i)
13663 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
13664 }
13665
13666
13667 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
13668 the glyph row and area where the glyph comes from. */
13669
13670 void
13671 dump_glyph (row, glyph, area)
13672 struct glyph_row *row;
13673 struct glyph *glyph;
13674 int area;
13675 {
13676 if (glyph->type == CHAR_GLYPH)
13677 {
13678 fprintf (stderr,
13679 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
13680 glyph - row->glyphs[TEXT_AREA],
13681 'C',
13682 glyph->charpos,
13683 (BUFFERP (glyph->object)
13684 ? 'B'
13685 : (STRINGP (glyph->object)
13686 ? 'S'
13687 : '-')),
13688 glyph->pixel_width,
13689 glyph->u.ch,
13690 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
13691 ? glyph->u.ch
13692 : '.'),
13693 glyph->face_id,
13694 glyph->left_box_line_p,
13695 glyph->right_box_line_p);
13696 }
13697 else if (glyph->type == STRETCH_GLYPH)
13698 {
13699 fprintf (stderr,
13700 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
13701 glyph - row->glyphs[TEXT_AREA],
13702 'S',
13703 glyph->charpos,
13704 (BUFFERP (glyph->object)
13705 ? 'B'
13706 : (STRINGP (glyph->object)
13707 ? 'S'
13708 : '-')),
13709 glyph->pixel_width,
13710 0,
13711 '.',
13712 glyph->face_id,
13713 glyph->left_box_line_p,
13714 glyph->right_box_line_p);
13715 }
13716 else if (glyph->type == IMAGE_GLYPH)
13717 {
13718 fprintf (stderr,
13719 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
13720 glyph - row->glyphs[TEXT_AREA],
13721 'I',
13722 glyph->charpos,
13723 (BUFFERP (glyph->object)
13724 ? 'B'
13725 : (STRINGP (glyph->object)
13726 ? 'S'
13727 : '-')),
13728 glyph->pixel_width,
13729 glyph->u.img_id,
13730 '.',
13731 glyph->face_id,
13732 glyph->left_box_line_p,
13733 glyph->right_box_line_p);
13734 }
13735 }
13736
13737
13738 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
13739 GLYPHS 0 means don't show glyph contents.
13740 GLYPHS 1 means show glyphs in short form
13741 GLYPHS > 1 means show glyphs in long form. */
13742
13743 void
13744 dump_glyph_row (row, vpos, glyphs)
13745 struct glyph_row *row;
13746 int vpos, glyphs;
13747 {
13748 if (glyphs != 1)
13749 {
13750 fprintf (stderr, "Row Start End Used oEI><O\\CTZFesm X Y W H V A P\n");
13751 fprintf (stderr, "=======================================================================\n");
13752
13753 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d%1.1d\
13754 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
13755 vpos,
13756 MATRIX_ROW_START_CHARPOS (row),
13757 MATRIX_ROW_END_CHARPOS (row),
13758 row->used[TEXT_AREA],
13759 row->contains_overlapping_glyphs_p,
13760 row->enabled_p,
13761 row->truncated_on_left_p,
13762 row->truncated_on_right_p,
13763 row->overlay_arrow_p,
13764 row->continued_p,
13765 MATRIX_ROW_CONTINUATION_LINE_P (row),
13766 row->displays_text_p,
13767 row->ends_at_zv_p,
13768 row->fill_line_p,
13769 row->ends_in_middle_of_char_p,
13770 row->starts_in_middle_of_char_p,
13771 row->mouse_face_p,
13772 row->x,
13773 row->y,
13774 row->pixel_width,
13775 row->height,
13776 row->visible_height,
13777 row->ascent,
13778 row->phys_ascent);
13779 fprintf (stderr, "%9d %5d\t%5d\n", row->start.overlay_string_index,
13780 row->end.overlay_string_index,
13781 row->continuation_lines_width);
13782 fprintf (stderr, "%9d %5d\n",
13783 CHARPOS (row->start.string_pos),
13784 CHARPOS (row->end.string_pos));
13785 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
13786 row->end.dpvec_index);
13787 }
13788
13789 if (glyphs > 1)
13790 {
13791 int area;
13792
13793 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
13794 {
13795 struct glyph *glyph = row->glyphs[area];
13796 struct glyph *glyph_end = glyph + row->used[area];
13797
13798 /* Glyph for a line end in text. */
13799 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
13800 ++glyph_end;
13801
13802 if (glyph < glyph_end)
13803 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
13804
13805 for (; glyph < glyph_end; ++glyph)
13806 dump_glyph (row, glyph, area);
13807 }
13808 }
13809 else if (glyphs == 1)
13810 {
13811 int area;
13812
13813 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
13814 {
13815 char *s = (char *) alloca (row->used[area] + 1);
13816 int i;
13817
13818 for (i = 0; i < row->used[area]; ++i)
13819 {
13820 struct glyph *glyph = row->glyphs[area] + i;
13821 if (glyph->type == CHAR_GLYPH
13822 && glyph->u.ch < 0x80
13823 && glyph->u.ch >= ' ')
13824 s[i] = glyph->u.ch;
13825 else
13826 s[i] = '.';
13827 }
13828
13829 s[i] = '\0';
13830 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
13831 }
13832 }
13833 }
13834
13835
13836 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
13837 Sdump_glyph_matrix, 0, 1, "p",
13838 doc: /* Dump the current matrix of the selected window to stderr.
13839 Shows contents of glyph row structures. With non-nil
13840 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
13841 glyphs in short form, otherwise show glyphs in long form. */)
13842 (glyphs)
13843 Lisp_Object glyphs;
13844 {
13845 struct window *w = XWINDOW (selected_window);
13846 struct buffer *buffer = XBUFFER (w->buffer);
13847
13848 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
13849 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
13850 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
13851 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
13852 fprintf (stderr, "=============================================\n");
13853 dump_glyph_matrix (w->current_matrix,
13854 NILP (glyphs) ? 0 : XINT (glyphs));
13855 return Qnil;
13856 }
13857
13858
13859 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
13860 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
13861 ()
13862 {
13863 struct frame *f = XFRAME (selected_frame);
13864 dump_glyph_matrix (f->current_matrix, 1);
13865 return Qnil;
13866 }
13867
13868
13869 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
13870 doc: /* Dump glyph row ROW to stderr.
13871 GLYPH 0 means don't dump glyphs.
13872 GLYPH 1 means dump glyphs in short form.
13873 GLYPH > 1 or omitted means dump glyphs in long form. */)
13874 (row, glyphs)
13875 Lisp_Object row, glyphs;
13876 {
13877 struct glyph_matrix *matrix;
13878 int vpos;
13879
13880 CHECK_NUMBER (row);
13881 matrix = XWINDOW (selected_window)->current_matrix;
13882 vpos = XINT (row);
13883 if (vpos >= 0 && vpos < matrix->nrows)
13884 dump_glyph_row (MATRIX_ROW (matrix, vpos),
13885 vpos,
13886 INTEGERP (glyphs) ? XINT (glyphs) : 2);
13887 return Qnil;
13888 }
13889
13890
13891 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
13892 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
13893 GLYPH 0 means don't dump glyphs.
13894 GLYPH 1 means dump glyphs in short form.
13895 GLYPH > 1 or omitted means dump glyphs in long form. */)
13896 (row, glyphs)
13897 Lisp_Object row, glyphs;
13898 {
13899 struct frame *sf = SELECTED_FRAME ();
13900 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
13901 int vpos;
13902
13903 CHECK_NUMBER (row);
13904 vpos = XINT (row);
13905 if (vpos >= 0 && vpos < m->nrows)
13906 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
13907 INTEGERP (glyphs) ? XINT (glyphs) : 2);
13908 return Qnil;
13909 }
13910
13911
13912 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
13913 doc: /* Toggle tracing of redisplay.
13914 With ARG, turn tracing on if and only if ARG is positive. */)
13915 (arg)
13916 Lisp_Object arg;
13917 {
13918 if (NILP (arg))
13919 trace_redisplay_p = !trace_redisplay_p;
13920 else
13921 {
13922 arg = Fprefix_numeric_value (arg);
13923 trace_redisplay_p = XINT (arg) > 0;
13924 }
13925
13926 return Qnil;
13927 }
13928
13929
13930 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
13931 doc: /* Like `format', but print result to stderr.
13932 usage: (trace-to-stderr STRING &rest OBJECTS) */)
13933 (nargs, args)
13934 int nargs;
13935 Lisp_Object *args;
13936 {
13937 Lisp_Object s = Fformat (nargs, args);
13938 fprintf (stderr, "%s", SDATA (s));
13939 return Qnil;
13940 }
13941
13942 #endif /* GLYPH_DEBUG */
13943
13944
13945 \f
13946 /***********************************************************************
13947 Building Desired Matrix Rows
13948 ***********************************************************************/
13949
13950 /* Return a temporary glyph row holding the glyphs of an overlay
13951 arrow. Only used for non-window-redisplay windows. */
13952
13953 static struct glyph_row *
13954 get_overlay_arrow_glyph_row (w, overlay_arrow_string)
13955 struct window *w;
13956 Lisp_Object overlay_arrow_string;
13957 {
13958 struct frame *f = XFRAME (WINDOW_FRAME (w));
13959 struct buffer *buffer = XBUFFER (w->buffer);
13960 struct buffer *old = current_buffer;
13961 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
13962 int arrow_len = SCHARS (overlay_arrow_string);
13963 const unsigned char *arrow_end = arrow_string + arrow_len;
13964 const unsigned char *p;
13965 struct it it;
13966 int multibyte_p;
13967 int n_glyphs_before;
13968
13969 set_buffer_temp (buffer);
13970 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
13971 it.glyph_row->used[TEXT_AREA] = 0;
13972 SET_TEXT_POS (it.position, 0, 0);
13973
13974 multibyte_p = !NILP (buffer->enable_multibyte_characters);
13975 p = arrow_string;
13976 while (p < arrow_end)
13977 {
13978 Lisp_Object face, ilisp;
13979
13980 /* Get the next character. */
13981 if (multibyte_p)
13982 it.c = string_char_and_length (p, arrow_len, &it.len);
13983 else
13984 it.c = *p, it.len = 1;
13985 p += it.len;
13986
13987 /* Get its face. */
13988 ilisp = make_number (p - arrow_string);
13989 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
13990 it.face_id = compute_char_face (f, it.c, face);
13991
13992 /* Compute its width, get its glyphs. */
13993 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
13994 SET_TEXT_POS (it.position, -1, -1);
13995 PRODUCE_GLYPHS (&it);
13996
13997 /* If this character doesn't fit any more in the line, we have
13998 to remove some glyphs. */
13999 if (it.current_x > it.last_visible_x)
14000 {
14001 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
14002 break;
14003 }
14004 }
14005
14006 set_buffer_temp (old);
14007 return it.glyph_row;
14008 }
14009
14010
14011 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
14012 glyphs are only inserted for terminal frames since we can't really
14013 win with truncation glyphs when partially visible glyphs are
14014 involved. Which glyphs to insert is determined by
14015 produce_special_glyphs. */
14016
14017 static void
14018 insert_left_trunc_glyphs (it)
14019 struct it *it;
14020 {
14021 struct it truncate_it;
14022 struct glyph *from, *end, *to, *toend;
14023
14024 xassert (!FRAME_WINDOW_P (it->f));
14025
14026 /* Get the truncation glyphs. */
14027 truncate_it = *it;
14028 truncate_it.current_x = 0;
14029 truncate_it.face_id = DEFAULT_FACE_ID;
14030 truncate_it.glyph_row = &scratch_glyph_row;
14031 truncate_it.glyph_row->used[TEXT_AREA] = 0;
14032 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
14033 truncate_it.object = make_number (0);
14034 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
14035
14036 /* Overwrite glyphs from IT with truncation glyphs. */
14037 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
14038 end = from + truncate_it.glyph_row->used[TEXT_AREA];
14039 to = it->glyph_row->glyphs[TEXT_AREA];
14040 toend = to + it->glyph_row->used[TEXT_AREA];
14041
14042 while (from < end)
14043 *to++ = *from++;
14044
14045 /* There may be padding glyphs left over. Overwrite them too. */
14046 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
14047 {
14048 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
14049 while (from < end)
14050 *to++ = *from++;
14051 }
14052
14053 if (to > toend)
14054 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
14055 }
14056
14057
14058 /* Compute the pixel height and width of IT->glyph_row.
14059
14060 Most of the time, ascent and height of a display line will be equal
14061 to the max_ascent and max_height values of the display iterator
14062 structure. This is not the case if
14063
14064 1. We hit ZV without displaying anything. In this case, max_ascent
14065 and max_height will be zero.
14066
14067 2. We have some glyphs that don't contribute to the line height.
14068 (The glyph row flag contributes_to_line_height_p is for future
14069 pixmap extensions).
14070
14071 The first case is easily covered by using default values because in
14072 these cases, the line height does not really matter, except that it
14073 must not be zero. */
14074
14075 static void
14076 compute_line_metrics (it)
14077 struct it *it;
14078 {
14079 struct glyph_row *row = it->glyph_row;
14080 int area, i;
14081
14082 if (FRAME_WINDOW_P (it->f))
14083 {
14084 int i, min_y, max_y;
14085
14086 /* The line may consist of one space only, that was added to
14087 place the cursor on it. If so, the row's height hasn't been
14088 computed yet. */
14089 if (row->height == 0)
14090 {
14091 if (it->max_ascent + it->max_descent == 0)
14092 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
14093 row->ascent = it->max_ascent;
14094 row->height = it->max_ascent + it->max_descent;
14095 row->phys_ascent = it->max_phys_ascent;
14096 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
14097 }
14098
14099 /* Compute the width of this line. */
14100 row->pixel_width = row->x;
14101 for (i = 0; i < row->used[TEXT_AREA]; ++i)
14102 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
14103
14104 xassert (row->pixel_width >= 0);
14105 xassert (row->ascent >= 0 && row->height > 0);
14106
14107 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
14108 || MATRIX_ROW_OVERLAPS_PRED_P (row));
14109
14110 /* If first line's physical ascent is larger than its logical
14111 ascent, use the physical ascent, and make the row taller.
14112 This makes accented characters fully visible. */
14113 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
14114 && row->phys_ascent > row->ascent)
14115 {
14116 row->height += row->phys_ascent - row->ascent;
14117 row->ascent = row->phys_ascent;
14118 }
14119
14120 /* Compute how much of the line is visible. */
14121 row->visible_height = row->height;
14122
14123 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
14124 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
14125
14126 if (row->y < min_y)
14127 row->visible_height -= min_y - row->y;
14128 if (row->y + row->height > max_y)
14129 row->visible_height -= row->y + row->height - max_y;
14130 }
14131 else
14132 {
14133 row->pixel_width = row->used[TEXT_AREA];
14134 if (row->continued_p)
14135 row->pixel_width -= it->continuation_pixel_width;
14136 else if (row->truncated_on_right_p)
14137 row->pixel_width -= it->truncation_pixel_width;
14138 row->ascent = row->phys_ascent = 0;
14139 row->height = row->phys_height = row->visible_height = 1;
14140 }
14141
14142 /* Compute a hash code for this row. */
14143 row->hash = 0;
14144 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
14145 for (i = 0; i < row->used[area]; ++i)
14146 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
14147 + row->glyphs[area][i].u.val
14148 + row->glyphs[area][i].face_id
14149 + row->glyphs[area][i].padding_p
14150 + (row->glyphs[area][i].type << 2));
14151
14152 it->max_ascent = it->max_descent = 0;
14153 it->max_phys_ascent = it->max_phys_descent = 0;
14154 }
14155
14156
14157 /* Append one space to the glyph row of iterator IT if doing a
14158 window-based redisplay. The space has the same face as
14159 IT->face_id. Value is non-zero if a space was added.
14160
14161 This function is called to make sure that there is always one glyph
14162 at the end of a glyph row that the cursor can be set on under
14163 window-systems. (If there weren't such a glyph we would not know
14164 how wide and tall a box cursor should be displayed).
14165
14166 At the same time this space let's a nicely handle clearing to the
14167 end of the line if the row ends in italic text. */
14168
14169 static int
14170 append_space_for_newline (it, default_face_p)
14171 struct it *it;
14172 int default_face_p;
14173 {
14174 if (FRAME_WINDOW_P (it->f))
14175 {
14176 int n = it->glyph_row->used[TEXT_AREA];
14177
14178 if (it->glyph_row->glyphs[TEXT_AREA] + n
14179 < it->glyph_row->glyphs[1 + TEXT_AREA])
14180 {
14181 /* Save some values that must not be changed.
14182 Must save IT->c and IT->len because otherwise
14183 ITERATOR_AT_END_P wouldn't work anymore after
14184 append_space_for_newline has been called. */
14185 enum display_element_type saved_what = it->what;
14186 int saved_c = it->c, saved_len = it->len;
14187 int saved_x = it->current_x;
14188 int saved_face_id = it->face_id;
14189 struct text_pos saved_pos;
14190 Lisp_Object saved_object;
14191 struct face *face;
14192
14193 saved_object = it->object;
14194 saved_pos = it->position;
14195
14196 it->what = IT_CHARACTER;
14197 bzero (&it->position, sizeof it->position);
14198 it->object = make_number (0);
14199 it->c = ' ';
14200 it->len = 1;
14201
14202 if (default_face_p)
14203 it->face_id = DEFAULT_FACE_ID;
14204 else if (it->face_before_selective_p)
14205 it->face_id = it->saved_face_id;
14206 face = FACE_FROM_ID (it->f, it->face_id);
14207 it->face_id = FACE_FOR_CHAR (it->f, face, 0);
14208
14209 PRODUCE_GLYPHS (it);
14210
14211 it->override_ascent = -1;
14212 it->constrain_row_ascent_descent_p = 0;
14213 it->current_x = saved_x;
14214 it->object = saved_object;
14215 it->position = saved_pos;
14216 it->what = saved_what;
14217 it->face_id = saved_face_id;
14218 it->len = saved_len;
14219 it->c = saved_c;
14220 return 1;
14221 }
14222 }
14223
14224 return 0;
14225 }
14226
14227
14228 /* Extend the face of the last glyph in the text area of IT->glyph_row
14229 to the end of the display line. Called from display_line.
14230 If the glyph row is empty, add a space glyph to it so that we
14231 know the face to draw. Set the glyph row flag fill_line_p. */
14232
14233 static void
14234 extend_face_to_end_of_line (it)
14235 struct it *it;
14236 {
14237 struct face *face;
14238 struct frame *f = it->f;
14239
14240 /* If line is already filled, do nothing. */
14241 if (it->current_x >= it->last_visible_x)
14242 return;
14243
14244 /* Face extension extends the background and box of IT->face_id
14245 to the end of the line. If the background equals the background
14246 of the frame, we don't have to do anything. */
14247 if (it->face_before_selective_p)
14248 face = FACE_FROM_ID (it->f, it->saved_face_id);
14249 else
14250 face = FACE_FROM_ID (f, it->face_id);
14251
14252 if (FRAME_WINDOW_P (f)
14253 && face->box == FACE_NO_BOX
14254 && face->background == FRAME_BACKGROUND_PIXEL (f)
14255 && !face->stipple)
14256 return;
14257
14258 /* Set the glyph row flag indicating that the face of the last glyph
14259 in the text area has to be drawn to the end of the text area. */
14260 it->glyph_row->fill_line_p = 1;
14261
14262 /* If current character of IT is not ASCII, make sure we have the
14263 ASCII face. This will be automatically undone the next time
14264 get_next_display_element returns a multibyte character. Note
14265 that the character will always be single byte in unibyte text. */
14266 if (!SINGLE_BYTE_CHAR_P (it->c))
14267 {
14268 it->face_id = FACE_FOR_CHAR (f, face, 0);
14269 }
14270
14271 if (FRAME_WINDOW_P (f))
14272 {
14273 /* If the row is empty, add a space with the current face of IT,
14274 so that we know which face to draw. */
14275 if (it->glyph_row->used[TEXT_AREA] == 0)
14276 {
14277 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
14278 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
14279 it->glyph_row->used[TEXT_AREA] = 1;
14280 }
14281 }
14282 else
14283 {
14284 /* Save some values that must not be changed. */
14285 int saved_x = it->current_x;
14286 struct text_pos saved_pos;
14287 Lisp_Object saved_object;
14288 enum display_element_type saved_what = it->what;
14289 int saved_face_id = it->face_id;
14290
14291 saved_object = it->object;
14292 saved_pos = it->position;
14293
14294 it->what = IT_CHARACTER;
14295 bzero (&it->position, sizeof it->position);
14296 it->object = make_number (0);
14297 it->c = ' ';
14298 it->len = 1;
14299 it->face_id = face->id;
14300
14301 PRODUCE_GLYPHS (it);
14302
14303 while (it->current_x <= it->last_visible_x)
14304 PRODUCE_GLYPHS (it);
14305
14306 /* Don't count these blanks really. It would let us insert a left
14307 truncation glyph below and make us set the cursor on them, maybe. */
14308 it->current_x = saved_x;
14309 it->object = saved_object;
14310 it->position = saved_pos;
14311 it->what = saved_what;
14312 it->face_id = saved_face_id;
14313 }
14314 }
14315
14316
14317 /* Value is non-zero if text starting at CHARPOS in current_buffer is
14318 trailing whitespace. */
14319
14320 static int
14321 trailing_whitespace_p (charpos)
14322 int charpos;
14323 {
14324 int bytepos = CHAR_TO_BYTE (charpos);
14325 int c = 0;
14326
14327 while (bytepos < ZV_BYTE
14328 && (c = FETCH_CHAR (bytepos),
14329 c == ' ' || c == '\t'))
14330 ++bytepos;
14331
14332 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
14333 {
14334 if (bytepos != PT_BYTE)
14335 return 1;
14336 }
14337 return 0;
14338 }
14339
14340
14341 /* Highlight trailing whitespace, if any, in ROW. */
14342
14343 void
14344 highlight_trailing_whitespace (f, row)
14345 struct frame *f;
14346 struct glyph_row *row;
14347 {
14348 int used = row->used[TEXT_AREA];
14349
14350 if (used)
14351 {
14352 struct glyph *start = row->glyphs[TEXT_AREA];
14353 struct glyph *glyph = start + used - 1;
14354
14355 /* Skip over glyphs inserted to display the cursor at the
14356 end of a line, for extending the face of the last glyph
14357 to the end of the line on terminals, and for truncation
14358 and continuation glyphs. */
14359 while (glyph >= start
14360 && glyph->type == CHAR_GLYPH
14361 && INTEGERP (glyph->object))
14362 --glyph;
14363
14364 /* If last glyph is a space or stretch, and it's trailing
14365 whitespace, set the face of all trailing whitespace glyphs in
14366 IT->glyph_row to `trailing-whitespace'. */
14367 if (glyph >= start
14368 && BUFFERP (glyph->object)
14369 && (glyph->type == STRETCH_GLYPH
14370 || (glyph->type == CHAR_GLYPH
14371 && glyph->u.ch == ' '))
14372 && trailing_whitespace_p (glyph->charpos))
14373 {
14374 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
14375
14376 while (glyph >= start
14377 && BUFFERP (glyph->object)
14378 && (glyph->type == STRETCH_GLYPH
14379 || (glyph->type == CHAR_GLYPH
14380 && glyph->u.ch == ' ')))
14381 (glyph--)->face_id = face_id;
14382 }
14383 }
14384 }
14385
14386
14387 /* Value is non-zero if glyph row ROW in window W should be
14388 used to hold the cursor. */
14389
14390 static int
14391 cursor_row_p (w, row)
14392 struct window *w;
14393 struct glyph_row *row;
14394 {
14395 int cursor_row_p = 1;
14396
14397 if (PT == MATRIX_ROW_END_CHARPOS (row))
14398 {
14399 /* If the row ends with a newline from a string, we don't want
14400 the cursor there (if the row is continued it doesn't end in a
14401 newline). */
14402 if (CHARPOS (row->end.string_pos) >= 0
14403 || MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
14404 cursor_row_p = row->continued_p;
14405
14406 /* If the row ends at ZV, display the cursor at the end of that
14407 row instead of at the start of the row below. */
14408 else if (row->ends_at_zv_p)
14409 cursor_row_p = 1;
14410 else
14411 cursor_row_p = 0;
14412 }
14413
14414 return cursor_row_p;
14415 }
14416
14417
14418 /* Construct the glyph row IT->glyph_row in the desired matrix of
14419 IT->w from text at the current position of IT. See dispextern.h
14420 for an overview of struct it. Value is non-zero if
14421 IT->glyph_row displays text, as opposed to a line displaying ZV
14422 only. */
14423
14424 static int
14425 display_line (it)
14426 struct it *it;
14427 {
14428 struct glyph_row *row = it->glyph_row;
14429 int overlay_arrow_bitmap;
14430 Lisp_Object overlay_arrow_string;
14431
14432 /* We always start displaying at hpos zero even if hscrolled. */
14433 xassert (it->hpos == 0 && it->current_x == 0);
14434
14435 /* We must not display in a row that's not a text row. */
14436 xassert (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
14437 < it->w->desired_matrix->nrows);
14438
14439 /* Is IT->w showing the region? */
14440 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
14441
14442 /* Clear the result glyph row and enable it. */
14443 prepare_desired_row (row);
14444
14445 row->y = it->current_y;
14446 row->start = it->start;
14447 row->continuation_lines_width = it->continuation_lines_width;
14448 row->displays_text_p = 1;
14449 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
14450 it->starts_in_middle_of_char_p = 0;
14451
14452 /* Arrange the overlays nicely for our purposes. Usually, we call
14453 display_line on only one line at a time, in which case this
14454 can't really hurt too much, or we call it on lines which appear
14455 one after another in the buffer, in which case all calls to
14456 recenter_overlay_lists but the first will be pretty cheap. */
14457 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
14458
14459 /* Move over display elements that are not visible because we are
14460 hscrolled. This may stop at an x-position < IT->first_visible_x
14461 if the first glyph is partially visible or if we hit a line end. */
14462 if (it->current_x < it->first_visible_x)
14463 move_it_in_display_line_to (it, ZV, it->first_visible_x,
14464 MOVE_TO_POS | MOVE_TO_X);
14465
14466 /* Get the initial row height. This is either the height of the
14467 text hscrolled, if there is any, or zero. */
14468 row->ascent = it->max_ascent;
14469 row->height = it->max_ascent + it->max_descent;
14470 row->phys_ascent = it->max_phys_ascent;
14471 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
14472
14473 /* Loop generating characters. The loop is left with IT on the next
14474 character to display. */
14475 while (1)
14476 {
14477 int n_glyphs_before, hpos_before, x_before;
14478 int x, i, nglyphs;
14479 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
14480
14481 /* Retrieve the next thing to display. Value is zero if end of
14482 buffer reached. */
14483 if (!get_next_display_element (it))
14484 {
14485 /* Maybe add a space at the end of this line that is used to
14486 display the cursor there under X. Set the charpos of the
14487 first glyph of blank lines not corresponding to any text
14488 to -1. */
14489 #ifdef HAVE_WINDOW_SYSTEM
14490 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
14491 row->exact_window_width_line_p = 1;
14492 else
14493 #endif /* HAVE_WINDOW_SYSTEM */
14494 if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
14495 || row->used[TEXT_AREA] == 0)
14496 {
14497 row->glyphs[TEXT_AREA]->charpos = -1;
14498 row->displays_text_p = 0;
14499
14500 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines)
14501 && (!MINI_WINDOW_P (it->w)
14502 || (minibuf_level && EQ (it->window, minibuf_window))))
14503 row->indicate_empty_line_p = 1;
14504 }
14505
14506 it->continuation_lines_width = 0;
14507 row->ends_at_zv_p = 1;
14508 break;
14509 }
14510
14511 /* Now, get the metrics of what we want to display. This also
14512 generates glyphs in `row' (which is IT->glyph_row). */
14513 n_glyphs_before = row->used[TEXT_AREA];
14514 x = it->current_x;
14515
14516 /* Remember the line height so far in case the next element doesn't
14517 fit on the line. */
14518 if (!it->truncate_lines_p)
14519 {
14520 ascent = it->max_ascent;
14521 descent = it->max_descent;
14522 phys_ascent = it->max_phys_ascent;
14523 phys_descent = it->max_phys_descent;
14524 }
14525
14526 PRODUCE_GLYPHS (it);
14527
14528 /* If this display element was in marginal areas, continue with
14529 the next one. */
14530 if (it->area != TEXT_AREA)
14531 {
14532 row->ascent = max (row->ascent, it->max_ascent);
14533 row->height = max (row->height, it->max_ascent + it->max_descent);
14534 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
14535 row->phys_height = max (row->phys_height,
14536 it->max_phys_ascent + it->max_phys_descent);
14537 set_iterator_to_next (it, 1);
14538 continue;
14539 }
14540
14541 /* Does the display element fit on the line? If we truncate
14542 lines, we should draw past the right edge of the window. If
14543 we don't truncate, we want to stop so that we can display the
14544 continuation glyph before the right margin. If lines are
14545 continued, there are two possible strategies for characters
14546 resulting in more than 1 glyph (e.g. tabs): Display as many
14547 glyphs as possible in this line and leave the rest for the
14548 continuation line, or display the whole element in the next
14549 line. Original redisplay did the former, so we do it also. */
14550 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
14551 hpos_before = it->hpos;
14552 x_before = x;
14553
14554 if (/* Not a newline. */
14555 nglyphs > 0
14556 /* Glyphs produced fit entirely in the line. */
14557 && it->current_x < it->last_visible_x)
14558 {
14559 it->hpos += nglyphs;
14560 row->ascent = max (row->ascent, it->max_ascent);
14561 row->height = max (row->height, it->max_ascent + it->max_descent);
14562 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
14563 row->phys_height = max (row->phys_height,
14564 it->max_phys_ascent + it->max_phys_descent);
14565 if (it->current_x - it->pixel_width < it->first_visible_x)
14566 row->x = x - it->first_visible_x;
14567 }
14568 else
14569 {
14570 int new_x;
14571 struct glyph *glyph;
14572
14573 for (i = 0; i < nglyphs; ++i, x = new_x)
14574 {
14575 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
14576 new_x = x + glyph->pixel_width;
14577
14578 if (/* Lines are continued. */
14579 !it->truncate_lines_p
14580 && (/* Glyph doesn't fit on the line. */
14581 new_x > it->last_visible_x
14582 /* Or it fits exactly on a window system frame. */
14583 || (new_x == it->last_visible_x
14584 && FRAME_WINDOW_P (it->f))))
14585 {
14586 /* End of a continued line. */
14587
14588 if (it->hpos == 0
14589 || (new_x == it->last_visible_x
14590 && FRAME_WINDOW_P (it->f)))
14591 {
14592 /* Current glyph is the only one on the line or
14593 fits exactly on the line. We must continue
14594 the line because we can't draw the cursor
14595 after the glyph. */
14596 row->continued_p = 1;
14597 it->current_x = new_x;
14598 it->continuation_lines_width += new_x;
14599 ++it->hpos;
14600 if (i == nglyphs - 1)
14601 {
14602 set_iterator_to_next (it, 1);
14603 #ifdef HAVE_WINDOW_SYSTEM
14604 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
14605 {
14606 if (!get_next_display_element (it))
14607 {
14608 row->exact_window_width_line_p = 1;
14609 it->continuation_lines_width = 0;
14610 row->continued_p = 0;
14611 row->ends_at_zv_p = 1;
14612 }
14613 else if (ITERATOR_AT_END_OF_LINE_P (it))
14614 {
14615 row->continued_p = 0;
14616 row->exact_window_width_line_p = 1;
14617 }
14618 }
14619 #endif /* HAVE_WINDOW_SYSTEM */
14620 }
14621 }
14622 else if (CHAR_GLYPH_PADDING_P (*glyph)
14623 && !FRAME_WINDOW_P (it->f))
14624 {
14625 /* A padding glyph that doesn't fit on this line.
14626 This means the whole character doesn't fit
14627 on the line. */
14628 row->used[TEXT_AREA] = n_glyphs_before;
14629
14630 /* Fill the rest of the row with continuation
14631 glyphs like in 20.x. */
14632 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
14633 < row->glyphs[1 + TEXT_AREA])
14634 produce_special_glyphs (it, IT_CONTINUATION);
14635
14636 row->continued_p = 1;
14637 it->current_x = x_before;
14638 it->continuation_lines_width += x_before;
14639
14640 /* Restore the height to what it was before the
14641 element not fitting on the line. */
14642 it->max_ascent = ascent;
14643 it->max_descent = descent;
14644 it->max_phys_ascent = phys_ascent;
14645 it->max_phys_descent = phys_descent;
14646 }
14647 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
14648 {
14649 /* A TAB that extends past the right edge of the
14650 window. This produces a single glyph on
14651 window system frames. We leave the glyph in
14652 this row and let it fill the row, but don't
14653 consume the TAB. */
14654 it->continuation_lines_width += it->last_visible_x;
14655 row->ends_in_middle_of_char_p = 1;
14656 row->continued_p = 1;
14657 glyph->pixel_width = it->last_visible_x - x;
14658 it->starts_in_middle_of_char_p = 1;
14659 }
14660 else
14661 {
14662 /* Something other than a TAB that draws past
14663 the right edge of the window. Restore
14664 positions to values before the element. */
14665 row->used[TEXT_AREA] = n_glyphs_before + i;
14666
14667 /* Display continuation glyphs. */
14668 if (!FRAME_WINDOW_P (it->f))
14669 produce_special_glyphs (it, IT_CONTINUATION);
14670 row->continued_p = 1;
14671
14672 it->continuation_lines_width += x;
14673
14674 if (nglyphs > 1 && i > 0)
14675 {
14676 row->ends_in_middle_of_char_p = 1;
14677 it->starts_in_middle_of_char_p = 1;
14678 }
14679
14680 /* Restore the height to what it was before the
14681 element not fitting on the line. */
14682 it->max_ascent = ascent;
14683 it->max_descent = descent;
14684 it->max_phys_ascent = phys_ascent;
14685 it->max_phys_descent = phys_descent;
14686 }
14687
14688 break;
14689 }
14690 else if (new_x > it->first_visible_x)
14691 {
14692 /* Increment number of glyphs actually displayed. */
14693 ++it->hpos;
14694
14695 if (x < it->first_visible_x)
14696 /* Glyph is partially visible, i.e. row starts at
14697 negative X position. */
14698 row->x = x - it->first_visible_x;
14699 }
14700 else
14701 {
14702 /* Glyph is completely off the left margin of the
14703 window. This should not happen because of the
14704 move_it_in_display_line at the start of this
14705 function, unless the text display area of the
14706 window is empty. */
14707 xassert (it->first_visible_x <= it->last_visible_x);
14708 }
14709 }
14710
14711 row->ascent = max (row->ascent, it->max_ascent);
14712 row->height = max (row->height, it->max_ascent + it->max_descent);
14713 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
14714 row->phys_height = max (row->phys_height,
14715 it->max_phys_ascent + it->max_phys_descent);
14716
14717 /* End of this display line if row is continued. */
14718 if (row->continued_p || row->ends_at_zv_p)
14719 break;
14720 }
14721
14722 at_end_of_line:
14723 /* Is this a line end? If yes, we're also done, after making
14724 sure that a non-default face is extended up to the right
14725 margin of the window. */
14726 if (ITERATOR_AT_END_OF_LINE_P (it))
14727 {
14728 int used_before = row->used[TEXT_AREA];
14729
14730 row->ends_in_newline_from_string_p = STRINGP (it->object);
14731
14732 #ifdef HAVE_WINDOW_SYSTEM
14733 /* Add a space at the end of the line that is used to
14734 display the cursor there. */
14735 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
14736 append_space_for_newline (it, 0);
14737 #endif /* HAVE_WINDOW_SYSTEM */
14738
14739 /* Extend the face to the end of the line. */
14740 extend_face_to_end_of_line (it);
14741
14742 /* Make sure we have the position. */
14743 if (used_before == 0)
14744 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
14745
14746 /* Consume the line end. This skips over invisible lines. */
14747 set_iterator_to_next (it, 1);
14748 it->continuation_lines_width = 0;
14749 break;
14750 }
14751
14752 /* Proceed with next display element. Note that this skips
14753 over lines invisible because of selective display. */
14754 set_iterator_to_next (it, 1);
14755
14756 /* If we truncate lines, we are done when the last displayed
14757 glyphs reach past the right margin of the window. */
14758 if (it->truncate_lines_p
14759 && (FRAME_WINDOW_P (it->f)
14760 ? (it->current_x >= it->last_visible_x)
14761 : (it->current_x > it->last_visible_x)))
14762 {
14763 /* Maybe add truncation glyphs. */
14764 if (!FRAME_WINDOW_P (it->f))
14765 {
14766 int i, n;
14767
14768 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
14769 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
14770 break;
14771
14772 for (n = row->used[TEXT_AREA]; i < n; ++i)
14773 {
14774 row->used[TEXT_AREA] = i;
14775 produce_special_glyphs (it, IT_TRUNCATION);
14776 }
14777 }
14778 #ifdef HAVE_WINDOW_SYSTEM
14779 else
14780 {
14781 /* Don't truncate if we can overflow newline into fringe. */
14782 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
14783 {
14784 if (!get_next_display_element (it))
14785 {
14786 #ifdef HAVE_WINDOW_SYSTEM
14787 it->continuation_lines_width = 0;
14788 row->ends_at_zv_p = 1;
14789 row->exact_window_width_line_p = 1;
14790 break;
14791 #endif /* HAVE_WINDOW_SYSTEM */
14792 }
14793 if (ITERATOR_AT_END_OF_LINE_P (it))
14794 {
14795 row->exact_window_width_line_p = 1;
14796 goto at_end_of_line;
14797 }
14798 }
14799 }
14800 #endif /* HAVE_WINDOW_SYSTEM */
14801
14802 row->truncated_on_right_p = 1;
14803 it->continuation_lines_width = 0;
14804 reseat_at_next_visible_line_start (it, 0);
14805 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
14806 it->hpos = hpos_before;
14807 it->current_x = x_before;
14808 break;
14809 }
14810 }
14811
14812 /* If line is not empty and hscrolled, maybe insert truncation glyphs
14813 at the left window margin. */
14814 if (it->first_visible_x
14815 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
14816 {
14817 if (!FRAME_WINDOW_P (it->f))
14818 insert_left_trunc_glyphs (it);
14819 row->truncated_on_left_p = 1;
14820 }
14821
14822 /* If the start of this line is the overlay arrow-position, then
14823 mark this glyph row as the one containing the overlay arrow.
14824 This is clearly a mess with variable size fonts. It would be
14825 better to let it be displayed like cursors under X. */
14826 if (! overlay_arrow_seen
14827 && (overlay_arrow_string
14828 = overlay_arrow_at_row (it->f, row, &overlay_arrow_bitmap),
14829 !NILP (overlay_arrow_string)))
14830 {
14831 /* Overlay arrow in window redisplay is a fringe bitmap. */
14832 if (!FRAME_WINDOW_P (it->f))
14833 {
14834 struct glyph_row *arrow_row
14835 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
14836 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
14837 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
14838 struct glyph *p = row->glyphs[TEXT_AREA];
14839 struct glyph *p2, *end;
14840
14841 /* Copy the arrow glyphs. */
14842 while (glyph < arrow_end)
14843 *p++ = *glyph++;
14844
14845 /* Throw away padding glyphs. */
14846 p2 = p;
14847 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
14848 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
14849 ++p2;
14850 if (p2 > p)
14851 {
14852 while (p2 < end)
14853 *p++ = *p2++;
14854 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
14855 }
14856 }
14857
14858 overlay_arrow_seen = 1;
14859 it->w->overlay_arrow_bitmap = overlay_arrow_bitmap;
14860 row->overlay_arrow_p = 1;
14861 }
14862
14863 /* Compute pixel dimensions of this line. */
14864 compute_line_metrics (it);
14865
14866 /* Remember the position at which this line ends. */
14867 row->end = it->current;
14868
14869 /* Save fringe bitmaps in this row. */
14870 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
14871 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
14872 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
14873 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
14874
14875 it->left_user_fringe_bitmap = 0;
14876 it->left_user_fringe_face_id = 0;
14877 it->right_user_fringe_bitmap = 0;
14878 it->right_user_fringe_face_id = 0;
14879
14880 /* Maybe set the cursor. */
14881 if (it->w->cursor.vpos < 0
14882 && PT >= MATRIX_ROW_START_CHARPOS (row)
14883 && PT <= MATRIX_ROW_END_CHARPOS (row)
14884 && cursor_row_p (it->w, row))
14885 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
14886
14887 /* Highlight trailing whitespace. */
14888 if (!NILP (Vshow_trailing_whitespace))
14889 highlight_trailing_whitespace (it->f, it->glyph_row);
14890
14891 /* Prepare for the next line. This line starts horizontally at (X
14892 HPOS) = (0 0). Vertical positions are incremented. As a
14893 convenience for the caller, IT->glyph_row is set to the next
14894 row to be used. */
14895 it->current_x = it->hpos = 0;
14896 it->current_y += row->height;
14897 ++it->vpos;
14898 ++it->glyph_row;
14899 it->start = it->current;
14900 return row->displays_text_p;
14901 }
14902
14903
14904 \f
14905 /***********************************************************************
14906 Menu Bar
14907 ***********************************************************************/
14908
14909 /* Redisplay the menu bar in the frame for window W.
14910
14911 The menu bar of X frames that don't have X toolkit support is
14912 displayed in a special window W->frame->menu_bar_window.
14913
14914 The menu bar of terminal frames is treated specially as far as
14915 glyph matrices are concerned. Menu bar lines are not part of
14916 windows, so the update is done directly on the frame matrix rows
14917 for the menu bar. */
14918
14919 static void
14920 display_menu_bar (w)
14921 struct window *w;
14922 {
14923 struct frame *f = XFRAME (WINDOW_FRAME (w));
14924 struct it it;
14925 Lisp_Object items;
14926 int i;
14927
14928 /* Don't do all this for graphical frames. */
14929 #ifdef HAVE_NTGUI
14930 if (!NILP (Vwindow_system))
14931 return;
14932 #endif
14933 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
14934 if (FRAME_X_P (f))
14935 return;
14936 #endif
14937 #ifdef MAC_OS
14938 if (FRAME_MAC_P (f))
14939 return;
14940 #endif
14941
14942 #ifdef USE_X_TOOLKIT
14943 xassert (!FRAME_WINDOW_P (f));
14944 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
14945 it.first_visible_x = 0;
14946 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
14947 #else /* not USE_X_TOOLKIT */
14948 if (FRAME_WINDOW_P (f))
14949 {
14950 /* Menu bar lines are displayed in the desired matrix of the
14951 dummy window menu_bar_window. */
14952 struct window *menu_w;
14953 xassert (WINDOWP (f->menu_bar_window));
14954 menu_w = XWINDOW (f->menu_bar_window);
14955 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
14956 MENU_FACE_ID);
14957 it.first_visible_x = 0;
14958 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
14959 }
14960 else
14961 {
14962 /* This is a TTY frame, i.e. character hpos/vpos are used as
14963 pixel x/y. */
14964 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
14965 MENU_FACE_ID);
14966 it.first_visible_x = 0;
14967 it.last_visible_x = FRAME_COLS (f);
14968 }
14969 #endif /* not USE_X_TOOLKIT */
14970
14971 if (! mode_line_inverse_video)
14972 /* Force the menu-bar to be displayed in the default face. */
14973 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
14974
14975 /* Clear all rows of the menu bar. */
14976 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
14977 {
14978 struct glyph_row *row = it.glyph_row + i;
14979 clear_glyph_row (row);
14980 row->enabled_p = 1;
14981 row->full_width_p = 1;
14982 }
14983
14984 /* Display all items of the menu bar. */
14985 items = FRAME_MENU_BAR_ITEMS (it.f);
14986 for (i = 0; i < XVECTOR (items)->size; i += 4)
14987 {
14988 Lisp_Object string;
14989
14990 /* Stop at nil string. */
14991 string = AREF (items, i + 1);
14992 if (NILP (string))
14993 break;
14994
14995 /* Remember where item was displayed. */
14996 AREF (items, i + 3) = make_number (it.hpos);
14997
14998 /* Display the item, pad with one space. */
14999 if (it.current_x < it.last_visible_x)
15000 display_string (NULL, string, Qnil, 0, 0, &it,
15001 SCHARS (string) + 1, 0, 0, -1);
15002 }
15003
15004 /* Fill out the line with spaces. */
15005 if (it.current_x < it.last_visible_x)
15006 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
15007
15008 /* Compute the total height of the lines. */
15009 compute_line_metrics (&it);
15010 }
15011
15012
15013 \f
15014 /***********************************************************************
15015 Mode Line
15016 ***********************************************************************/
15017
15018 /* Redisplay mode lines in the window tree whose root is WINDOW. If
15019 FORCE is non-zero, redisplay mode lines unconditionally.
15020 Otherwise, redisplay only mode lines that are garbaged. Value is
15021 the number of windows whose mode lines were redisplayed. */
15022
15023 static int
15024 redisplay_mode_lines (window, force)
15025 Lisp_Object window;
15026 int force;
15027 {
15028 int nwindows = 0;
15029
15030 while (!NILP (window))
15031 {
15032 struct window *w = XWINDOW (window);
15033
15034 if (WINDOWP (w->hchild))
15035 nwindows += redisplay_mode_lines (w->hchild, force);
15036 else if (WINDOWP (w->vchild))
15037 nwindows += redisplay_mode_lines (w->vchild, force);
15038 else if (force
15039 || FRAME_GARBAGED_P (XFRAME (w->frame))
15040 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
15041 {
15042 struct text_pos lpoint;
15043 struct buffer *old = current_buffer;
15044
15045 /* Set the window's buffer for the mode line display. */
15046 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15047 set_buffer_internal_1 (XBUFFER (w->buffer));
15048
15049 /* Point refers normally to the selected window. For any
15050 other window, set up appropriate value. */
15051 if (!EQ (window, selected_window))
15052 {
15053 struct text_pos pt;
15054
15055 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
15056 if (CHARPOS (pt) < BEGV)
15057 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
15058 else if (CHARPOS (pt) > (ZV - 1))
15059 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
15060 else
15061 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
15062 }
15063
15064 /* Display mode lines. */
15065 clear_glyph_matrix (w->desired_matrix);
15066 if (display_mode_lines (w))
15067 {
15068 ++nwindows;
15069 w->must_be_updated_p = 1;
15070 }
15071
15072 /* Restore old settings. */
15073 set_buffer_internal_1 (old);
15074 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
15075 }
15076
15077 window = w->next;
15078 }
15079
15080 return nwindows;
15081 }
15082
15083
15084 /* Display the mode and/or top line of window W. Value is the number
15085 of mode lines displayed. */
15086
15087 static int
15088 display_mode_lines (w)
15089 struct window *w;
15090 {
15091 Lisp_Object old_selected_window, old_selected_frame;
15092 int n = 0;
15093
15094 old_selected_frame = selected_frame;
15095 selected_frame = w->frame;
15096 old_selected_window = selected_window;
15097 XSETWINDOW (selected_window, w);
15098
15099 /* These will be set while the mode line specs are processed. */
15100 line_number_displayed = 0;
15101 w->column_number_displayed = Qnil;
15102
15103 if (WINDOW_WANTS_MODELINE_P (w))
15104 {
15105 struct window *sel_w = XWINDOW (old_selected_window);
15106
15107 /* Select mode line face based on the real selected window. */
15108 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
15109 current_buffer->mode_line_format);
15110 ++n;
15111 }
15112
15113 if (WINDOW_WANTS_HEADER_LINE_P (w))
15114 {
15115 display_mode_line (w, HEADER_LINE_FACE_ID,
15116 current_buffer->header_line_format);
15117 ++n;
15118 }
15119
15120 selected_frame = old_selected_frame;
15121 selected_window = old_selected_window;
15122 return n;
15123 }
15124
15125
15126 /* Display mode or top line of window W. FACE_ID specifies which line
15127 to display; it is either MODE_LINE_FACE_ID or HEADER_LINE_FACE_ID.
15128 FORMAT is the mode line format to display. Value is the pixel
15129 height of the mode line displayed. */
15130
15131 static int
15132 display_mode_line (w, face_id, format)
15133 struct window *w;
15134 enum face_id face_id;
15135 Lisp_Object format;
15136 {
15137 struct it it;
15138 struct face *face;
15139
15140 init_iterator (&it, w, -1, -1, NULL, face_id);
15141 prepare_desired_row (it.glyph_row);
15142
15143 it.glyph_row->mode_line_p = 1;
15144
15145 if (! mode_line_inverse_video)
15146 /* Force the mode-line to be displayed in the default face. */
15147 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
15148
15149 /* Temporarily make frame's keyboard the current kboard so that
15150 kboard-local variables in the mode_line_format will get the right
15151 values. */
15152 push_frame_kboard (it.f);
15153 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
15154 pop_frame_kboard ();
15155
15156 /* Fill up with spaces. */
15157 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
15158
15159 compute_line_metrics (&it);
15160 it.glyph_row->full_width_p = 1;
15161 it.glyph_row->continued_p = 0;
15162 it.glyph_row->truncated_on_left_p = 0;
15163 it.glyph_row->truncated_on_right_p = 0;
15164
15165 /* Make a 3D mode-line have a shadow at its right end. */
15166 face = FACE_FROM_ID (it.f, face_id);
15167 extend_face_to_end_of_line (&it);
15168 if (face->box != FACE_NO_BOX)
15169 {
15170 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
15171 + it.glyph_row->used[TEXT_AREA] - 1);
15172 last->right_box_line_p = 1;
15173 }
15174
15175 return it.glyph_row->height;
15176 }
15177
15178 /* Alist that caches the results of :propertize.
15179 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
15180 Lisp_Object mode_line_proptrans_alist;
15181
15182 /* List of strings making up the mode-line. */
15183 Lisp_Object mode_line_string_list;
15184
15185 /* Base face property when building propertized mode line string. */
15186 static Lisp_Object mode_line_string_face;
15187 static Lisp_Object mode_line_string_face_prop;
15188
15189
15190 /* Contribute ELT to the mode line for window IT->w. How it
15191 translates into text depends on its data type.
15192
15193 IT describes the display environment in which we display, as usual.
15194
15195 DEPTH is the depth in recursion. It is used to prevent
15196 infinite recursion here.
15197
15198 FIELD_WIDTH is the number of characters the display of ELT should
15199 occupy in the mode line, and PRECISION is the maximum number of
15200 characters to display from ELT's representation. See
15201 display_string for details.
15202
15203 Returns the hpos of the end of the text generated by ELT.
15204
15205 PROPS is a property list to add to any string we encounter.
15206
15207 If RISKY is nonzero, remove (disregard) any properties in any string
15208 we encounter, and ignore :eval and :propertize.
15209
15210 If the global variable `frame_title_ptr' is non-NULL, then the output
15211 is passed to `store_frame_title' instead of `display_string'. */
15212
15213 static int
15214 display_mode_element (it, depth, field_width, precision, elt, props, risky)
15215 struct it *it;
15216 int depth;
15217 int field_width, precision;
15218 Lisp_Object elt, props;
15219 int risky;
15220 {
15221 int n = 0, field, prec;
15222 int literal = 0;
15223
15224 tail_recurse:
15225 if (depth > 100)
15226 elt = build_string ("*too-deep*");
15227
15228 depth++;
15229
15230 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
15231 {
15232 case Lisp_String:
15233 {
15234 /* A string: output it and check for %-constructs within it. */
15235 unsigned char c;
15236 const unsigned char *this, *lisp_string;
15237
15238 if (!NILP (props) || risky)
15239 {
15240 Lisp_Object oprops, aelt;
15241 oprops = Ftext_properties_at (make_number (0), elt);
15242
15243 if (NILP (Fequal (props, oprops)) || risky)
15244 {
15245 /* If the starting string has properties,
15246 merge the specified ones onto the existing ones. */
15247 if (! NILP (oprops) && !risky)
15248 {
15249 Lisp_Object tem;
15250
15251 oprops = Fcopy_sequence (oprops);
15252 tem = props;
15253 while (CONSP (tem))
15254 {
15255 oprops = Fplist_put (oprops, XCAR (tem),
15256 XCAR (XCDR (tem)));
15257 tem = XCDR (XCDR (tem));
15258 }
15259 props = oprops;
15260 }
15261
15262 aelt = Fassoc (elt, mode_line_proptrans_alist);
15263 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
15264 {
15265 mode_line_proptrans_alist
15266 = Fcons (aelt, Fdelq (aelt, mode_line_proptrans_alist));
15267 elt = XCAR (aelt);
15268 }
15269 else
15270 {
15271 Lisp_Object tem;
15272
15273 elt = Fcopy_sequence (elt);
15274 Fset_text_properties (make_number (0), Flength (elt),
15275 props, elt);
15276 /* Add this item to mode_line_proptrans_alist. */
15277 mode_line_proptrans_alist
15278 = Fcons (Fcons (elt, props),
15279 mode_line_proptrans_alist);
15280 /* Truncate mode_line_proptrans_alist
15281 to at most 50 elements. */
15282 tem = Fnthcdr (make_number (50),
15283 mode_line_proptrans_alist);
15284 if (! NILP (tem))
15285 XSETCDR (tem, Qnil);
15286 }
15287 }
15288 }
15289
15290 this = SDATA (elt);
15291 lisp_string = this;
15292
15293 if (literal)
15294 {
15295 prec = precision - n;
15296 if (frame_title_ptr)
15297 n += store_frame_title (SDATA (elt), -1, prec);
15298 else if (!NILP (mode_line_string_list))
15299 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
15300 else
15301 n += display_string (NULL, elt, Qnil, 0, 0, it,
15302 0, prec, 0, STRING_MULTIBYTE (elt));
15303
15304 break;
15305 }
15306
15307 while ((precision <= 0 || n < precision)
15308 && *this
15309 && (frame_title_ptr
15310 || !NILP (mode_line_string_list)
15311 || it->current_x < it->last_visible_x))
15312 {
15313 const unsigned char *last = this;
15314
15315 /* Advance to end of string or next format specifier. */
15316 while ((c = *this++) != '\0' && c != '%')
15317 ;
15318
15319 if (this - 1 != last)
15320 {
15321 /* Output to end of string or up to '%'. Field width
15322 is length of string. Don't output more than
15323 PRECISION allows us. */
15324 --this;
15325
15326 prec = chars_in_text (last, this - last);
15327 if (precision > 0 && prec > precision - n)
15328 prec = precision - n;
15329
15330 if (frame_title_ptr)
15331 n += store_frame_title (last, 0, prec);
15332 else if (!NILP (mode_line_string_list))
15333 {
15334 int bytepos = last - lisp_string;
15335 int charpos = string_byte_to_char (elt, bytepos);
15336 n += store_mode_line_string (NULL,
15337 Fsubstring (elt, make_number (charpos),
15338 make_number (charpos + prec)),
15339 0, 0, 0, Qnil);
15340 }
15341 else
15342 {
15343 int bytepos = last - lisp_string;
15344 int charpos = string_byte_to_char (elt, bytepos);
15345 n += display_string (NULL, elt, Qnil, 0, charpos,
15346 it, 0, prec, 0,
15347 STRING_MULTIBYTE (elt));
15348 }
15349 }
15350 else /* c == '%' */
15351 {
15352 const unsigned char *percent_position = this;
15353
15354 /* Get the specified minimum width. Zero means
15355 don't pad. */
15356 field = 0;
15357 while ((c = *this++) >= '0' && c <= '9')
15358 field = field * 10 + c - '0';
15359
15360 /* Don't pad beyond the total padding allowed. */
15361 if (field_width - n > 0 && field > field_width - n)
15362 field = field_width - n;
15363
15364 /* Note that either PRECISION <= 0 or N < PRECISION. */
15365 prec = precision - n;
15366
15367 if (c == 'M')
15368 n += display_mode_element (it, depth, field, prec,
15369 Vglobal_mode_string, props,
15370 risky);
15371 else if (c != 0)
15372 {
15373 int multibyte;
15374 int bytepos, charpos;
15375 unsigned char *spec;
15376
15377 bytepos = percent_position - lisp_string;
15378 charpos = (STRING_MULTIBYTE (elt)
15379 ? string_byte_to_char (elt, bytepos)
15380 : bytepos);
15381
15382 spec
15383 = decode_mode_spec (it->w, c, field, prec, &multibyte);
15384
15385 if (frame_title_ptr)
15386 n += store_frame_title (spec, field, prec);
15387 else if (!NILP (mode_line_string_list))
15388 {
15389 int len = strlen (spec);
15390 Lisp_Object tem = make_string (spec, len);
15391 props = Ftext_properties_at (make_number (charpos), elt);
15392 /* Should only keep face property in props */
15393 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
15394 }
15395 else
15396 {
15397 int nglyphs_before, nwritten;
15398
15399 nglyphs_before = it->glyph_row->used[TEXT_AREA];
15400 nwritten = display_string (spec, Qnil, elt,
15401 charpos, 0, it,
15402 field, prec, 0,
15403 multibyte);
15404
15405 /* Assign to the glyphs written above the
15406 string where the `%x' came from, position
15407 of the `%'. */
15408 if (nwritten > 0)
15409 {
15410 struct glyph *glyph
15411 = (it->glyph_row->glyphs[TEXT_AREA]
15412 + nglyphs_before);
15413 int i;
15414
15415 for (i = 0; i < nwritten; ++i)
15416 {
15417 glyph[i].object = elt;
15418 glyph[i].charpos = charpos;
15419 }
15420
15421 n += nwritten;
15422 }
15423 }
15424 }
15425 else /* c == 0 */
15426 break;
15427 }
15428 }
15429 }
15430 break;
15431
15432 case Lisp_Symbol:
15433 /* A symbol: process the value of the symbol recursively
15434 as if it appeared here directly. Avoid error if symbol void.
15435 Special case: if value of symbol is a string, output the string
15436 literally. */
15437 {
15438 register Lisp_Object tem;
15439
15440 /* If the variable is not marked as risky to set
15441 then its contents are risky to use. */
15442 if (NILP (Fget (elt, Qrisky_local_variable)))
15443 risky = 1;
15444
15445 tem = Fboundp (elt);
15446 if (!NILP (tem))
15447 {
15448 tem = Fsymbol_value (elt);
15449 /* If value is a string, output that string literally:
15450 don't check for % within it. */
15451 if (STRINGP (tem))
15452 literal = 1;
15453
15454 if (!EQ (tem, elt))
15455 {
15456 /* Give up right away for nil or t. */
15457 elt = tem;
15458 goto tail_recurse;
15459 }
15460 }
15461 }
15462 break;
15463
15464 case Lisp_Cons:
15465 {
15466 register Lisp_Object car, tem;
15467
15468 /* A cons cell: five distinct cases.
15469 If first element is :eval or :propertize, do something special.
15470 If first element is a string or a cons, process all the elements
15471 and effectively concatenate them.
15472 If first element is a negative number, truncate displaying cdr to
15473 at most that many characters. If positive, pad (with spaces)
15474 to at least that many characters.
15475 If first element is a symbol, process the cadr or caddr recursively
15476 according to whether the symbol's value is non-nil or nil. */
15477 car = XCAR (elt);
15478 if (EQ (car, QCeval))
15479 {
15480 /* An element of the form (:eval FORM) means evaluate FORM
15481 and use the result as mode line elements. */
15482
15483 if (risky)
15484 break;
15485
15486 if (CONSP (XCDR (elt)))
15487 {
15488 Lisp_Object spec;
15489 spec = safe_eval (XCAR (XCDR (elt)));
15490 n += display_mode_element (it, depth, field_width - n,
15491 precision - n, spec, props,
15492 risky);
15493 }
15494 }
15495 else if (EQ (car, QCpropertize))
15496 {
15497 /* An element of the form (:propertize ELT PROPS...)
15498 means display ELT but applying properties PROPS. */
15499
15500 if (risky)
15501 break;
15502
15503 if (CONSP (XCDR (elt)))
15504 n += display_mode_element (it, depth, field_width - n,
15505 precision - n, XCAR (XCDR (elt)),
15506 XCDR (XCDR (elt)), risky);
15507 }
15508 else if (SYMBOLP (car))
15509 {
15510 tem = Fboundp (car);
15511 elt = XCDR (elt);
15512 if (!CONSP (elt))
15513 goto invalid;
15514 /* elt is now the cdr, and we know it is a cons cell.
15515 Use its car if CAR has a non-nil value. */
15516 if (!NILP (tem))
15517 {
15518 tem = Fsymbol_value (car);
15519 if (!NILP (tem))
15520 {
15521 elt = XCAR (elt);
15522 goto tail_recurse;
15523 }
15524 }
15525 /* Symbol's value is nil (or symbol is unbound)
15526 Get the cddr of the original list
15527 and if possible find the caddr and use that. */
15528 elt = XCDR (elt);
15529 if (NILP (elt))
15530 break;
15531 else if (!CONSP (elt))
15532 goto invalid;
15533 elt = XCAR (elt);
15534 goto tail_recurse;
15535 }
15536 else if (INTEGERP (car))
15537 {
15538 register int lim = XINT (car);
15539 elt = XCDR (elt);
15540 if (lim < 0)
15541 {
15542 /* Negative int means reduce maximum width. */
15543 if (precision <= 0)
15544 precision = -lim;
15545 else
15546 precision = min (precision, -lim);
15547 }
15548 else if (lim > 0)
15549 {
15550 /* Padding specified. Don't let it be more than
15551 current maximum. */
15552 if (precision > 0)
15553 lim = min (precision, lim);
15554
15555 /* If that's more padding than already wanted, queue it.
15556 But don't reduce padding already specified even if
15557 that is beyond the current truncation point. */
15558 field_width = max (lim, field_width);
15559 }
15560 goto tail_recurse;
15561 }
15562 else if (STRINGP (car) || CONSP (car))
15563 {
15564 register int limit = 50;
15565 /* Limit is to protect against circular lists. */
15566 while (CONSP (elt)
15567 && --limit > 0
15568 && (precision <= 0 || n < precision))
15569 {
15570 n += display_mode_element (it, depth, field_width - n,
15571 precision - n, XCAR (elt),
15572 props, risky);
15573 elt = XCDR (elt);
15574 }
15575 }
15576 }
15577 break;
15578
15579 default:
15580 invalid:
15581 elt = build_string ("*invalid*");
15582 goto tail_recurse;
15583 }
15584
15585 /* Pad to FIELD_WIDTH. */
15586 if (field_width > 0 && n < field_width)
15587 {
15588 if (frame_title_ptr)
15589 n += store_frame_title ("", field_width - n, 0);
15590 else if (!NILP (mode_line_string_list))
15591 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
15592 else
15593 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
15594 0, 0, 0);
15595 }
15596
15597 return n;
15598 }
15599
15600 /* Store a mode-line string element in mode_line_string_list.
15601
15602 If STRING is non-null, display that C string. Otherwise, the Lisp
15603 string LISP_STRING is displayed.
15604
15605 FIELD_WIDTH is the minimum number of output glyphs to produce.
15606 If STRING has fewer characters than FIELD_WIDTH, pad to the right
15607 with spaces. FIELD_WIDTH <= 0 means don't pad.
15608
15609 PRECISION is the maximum number of characters to output from
15610 STRING. PRECISION <= 0 means don't truncate the string.
15611
15612 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
15613 properties to the string.
15614
15615 PROPS are the properties to add to the string.
15616 The mode_line_string_face face property is always added to the string.
15617 */
15618
15619 static int store_mode_line_string (string, lisp_string, copy_string, field_width, precision, props)
15620 char *string;
15621 Lisp_Object lisp_string;
15622 int copy_string;
15623 int field_width;
15624 int precision;
15625 Lisp_Object props;
15626 {
15627 int len;
15628 int n = 0;
15629
15630 if (string != NULL)
15631 {
15632 len = strlen (string);
15633 if (precision > 0 && len > precision)
15634 len = precision;
15635 lisp_string = make_string (string, len);
15636 if (NILP (props))
15637 props = mode_line_string_face_prop;
15638 else if (!NILP (mode_line_string_face))
15639 {
15640 Lisp_Object face = Fplist_get (props, Qface);
15641 props = Fcopy_sequence (props);
15642 if (NILP (face))
15643 face = mode_line_string_face;
15644 else
15645 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
15646 props = Fplist_put (props, Qface, face);
15647 }
15648 Fadd_text_properties (make_number (0), make_number (len),
15649 props, lisp_string);
15650 }
15651 else
15652 {
15653 len = XFASTINT (Flength (lisp_string));
15654 if (precision > 0 && len > precision)
15655 {
15656 len = precision;
15657 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
15658 precision = -1;
15659 }
15660 if (!NILP (mode_line_string_face))
15661 {
15662 Lisp_Object face;
15663 if (NILP (props))
15664 props = Ftext_properties_at (make_number (0), lisp_string);
15665 face = Fplist_get (props, Qface);
15666 if (NILP (face))
15667 face = mode_line_string_face;
15668 else
15669 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
15670 props = Fcons (Qface, Fcons (face, Qnil));
15671 if (copy_string)
15672 lisp_string = Fcopy_sequence (lisp_string);
15673 }
15674 if (!NILP (props))
15675 Fadd_text_properties (make_number (0), make_number (len),
15676 props, lisp_string);
15677 }
15678
15679 if (len > 0)
15680 {
15681 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
15682 n += len;
15683 }
15684
15685 if (field_width > len)
15686 {
15687 field_width -= len;
15688 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
15689 if (!NILP (props))
15690 Fadd_text_properties (make_number (0), make_number (field_width),
15691 props, lisp_string);
15692 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
15693 n += field_width;
15694 }
15695
15696 return n;
15697 }
15698
15699
15700 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
15701 0, 3, 0,
15702 doc: /* Return the mode-line of selected window as a string.
15703 First optional arg FORMAT specifies a different format string (see
15704 `mode-line-format' for details) to use. If FORMAT is t, return
15705 the buffer's header-line. Second optional arg WINDOW specifies a
15706 different window to use as the context for the formatting.
15707 If third optional arg NO-PROPS is non-nil, string is not propertized. */)
15708 (format, window, no_props)
15709 Lisp_Object format, window, no_props;
15710 {
15711 struct it it;
15712 int len;
15713 struct window *w;
15714 struct buffer *old_buffer = NULL;
15715 enum face_id face_id = DEFAULT_FACE_ID;
15716
15717 if (NILP (window))
15718 window = selected_window;
15719 CHECK_WINDOW (window);
15720 w = XWINDOW (window);
15721 CHECK_BUFFER (w->buffer);
15722
15723 if (XBUFFER (w->buffer) != current_buffer)
15724 {
15725 old_buffer = current_buffer;
15726 set_buffer_internal_1 (XBUFFER (w->buffer));
15727 }
15728
15729 if (NILP (format) || EQ (format, Qt))
15730 {
15731 face_id = NILP (format)
15732 ? CURRENT_MODE_LINE_FACE_ID (w) :
15733 HEADER_LINE_FACE_ID;
15734 format = NILP (format)
15735 ? current_buffer->mode_line_format
15736 : current_buffer->header_line_format;
15737 }
15738
15739 init_iterator (&it, w, -1, -1, NULL, face_id);
15740
15741 if (NILP (no_props))
15742 {
15743 mode_line_string_face =
15744 (face_id == MODE_LINE_FACE_ID ? Qmode_line :
15745 face_id == MODE_LINE_INACTIVE_FACE_ID ? Qmode_line_inactive :
15746 face_id == HEADER_LINE_FACE_ID ? Qheader_line : Qnil);
15747
15748 mode_line_string_face_prop =
15749 NILP (mode_line_string_face) ? Qnil :
15750 Fcons (Qface, Fcons (mode_line_string_face, Qnil));
15751
15752 /* We need a dummy last element in mode_line_string_list to
15753 indicate we are building the propertized mode-line string.
15754 Using mode_line_string_face_prop here GC protects it. */
15755 mode_line_string_list =
15756 Fcons (mode_line_string_face_prop, Qnil);
15757 frame_title_ptr = NULL;
15758 }
15759 else
15760 {
15761 mode_line_string_face_prop = Qnil;
15762 mode_line_string_list = Qnil;
15763 frame_title_ptr = frame_title_buf;
15764 }
15765
15766 push_frame_kboard (it.f);
15767 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
15768 pop_frame_kboard ();
15769
15770 if (old_buffer)
15771 set_buffer_internal_1 (old_buffer);
15772
15773 if (NILP (no_props))
15774 {
15775 Lisp_Object str;
15776 mode_line_string_list = Fnreverse (mode_line_string_list);
15777 str = Fmapconcat (intern ("identity"), XCDR (mode_line_string_list),
15778 make_string ("", 0));
15779 mode_line_string_face_prop = Qnil;
15780 mode_line_string_list = Qnil;
15781 return str;
15782 }
15783
15784 len = frame_title_ptr - frame_title_buf;
15785 if (len > 0 && frame_title_ptr[-1] == '-')
15786 {
15787 /* Mode lines typically ends with numerous dashes; reduce to two dashes. */
15788 while (frame_title_ptr > frame_title_buf && *--frame_title_ptr == '-')
15789 ;
15790 frame_title_ptr += 3; /* restore last non-dash + two dashes */
15791 if (len > frame_title_ptr - frame_title_buf)
15792 len = frame_title_ptr - frame_title_buf;
15793 }
15794
15795 frame_title_ptr = NULL;
15796 return make_string (frame_title_buf, len);
15797 }
15798
15799 /* Write a null-terminated, right justified decimal representation of
15800 the positive integer D to BUF using a minimal field width WIDTH. */
15801
15802 static void
15803 pint2str (buf, width, d)
15804 register char *buf;
15805 register int width;
15806 register int d;
15807 {
15808 register char *p = buf;
15809
15810 if (d <= 0)
15811 *p++ = '0';
15812 else
15813 {
15814 while (d > 0)
15815 {
15816 *p++ = d % 10 + '0';
15817 d /= 10;
15818 }
15819 }
15820
15821 for (width -= (int) (p - buf); width > 0; --width)
15822 *p++ = ' ';
15823 *p-- = '\0';
15824 while (p > buf)
15825 {
15826 d = *buf;
15827 *buf++ = *p;
15828 *p-- = d;
15829 }
15830 }
15831
15832 /* Write a null-terminated, right justified decimal and "human
15833 readable" representation of the nonnegative integer D to BUF using
15834 a minimal field width WIDTH. D should be smaller than 999.5e24. */
15835
15836 static const char power_letter[] =
15837 {
15838 0, /* not used */
15839 'k', /* kilo */
15840 'M', /* mega */
15841 'G', /* giga */
15842 'T', /* tera */
15843 'P', /* peta */
15844 'E', /* exa */
15845 'Z', /* zetta */
15846 'Y' /* yotta */
15847 };
15848
15849 static void
15850 pint2hrstr (buf, width, d)
15851 char *buf;
15852 int width;
15853 int d;
15854 {
15855 /* We aim to represent the nonnegative integer D as
15856 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
15857 int quotient = d;
15858 int remainder = 0;
15859 /* -1 means: do not use TENTHS. */
15860 int tenths = -1;
15861 int exponent = 0;
15862
15863 /* Length of QUOTIENT.TENTHS as a string. */
15864 int length;
15865
15866 char * psuffix;
15867 char * p;
15868
15869 if (1000 <= quotient)
15870 {
15871 /* Scale to the appropriate EXPONENT. */
15872 do
15873 {
15874 remainder = quotient % 1000;
15875 quotient /= 1000;
15876 exponent++;
15877 }
15878 while (1000 <= quotient);
15879
15880 /* Round to nearest and decide whether to use TENTHS or not. */
15881 if (quotient <= 9)
15882 {
15883 tenths = remainder / 100;
15884 if (50 <= remainder % 100)
15885 if (tenths < 9)
15886 tenths++;
15887 else
15888 {
15889 quotient++;
15890 if (quotient == 10)
15891 tenths = -1;
15892 else
15893 tenths = 0;
15894 }
15895 }
15896 else
15897 if (500 <= remainder)
15898 if (quotient < 999)
15899 quotient++;
15900 else
15901 {
15902 quotient = 1;
15903 exponent++;
15904 tenths = 0;
15905 }
15906 }
15907
15908 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
15909 if (tenths == -1 && quotient <= 99)
15910 if (quotient <= 9)
15911 length = 1;
15912 else
15913 length = 2;
15914 else
15915 length = 3;
15916 p = psuffix = buf + max (width, length);
15917
15918 /* Print EXPONENT. */
15919 if (exponent)
15920 *psuffix++ = power_letter[exponent];
15921 *psuffix = '\0';
15922
15923 /* Print TENTHS. */
15924 if (tenths >= 0)
15925 {
15926 *--p = '0' + tenths;
15927 *--p = '.';
15928 }
15929
15930 /* Print QUOTIENT. */
15931 do
15932 {
15933 int digit = quotient % 10;
15934 *--p = '0' + digit;
15935 }
15936 while ((quotient /= 10) != 0);
15937
15938 /* Print leading spaces. */
15939 while (buf < p)
15940 *--p = ' ';
15941 }
15942
15943 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
15944 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
15945 type of CODING_SYSTEM. Return updated pointer into BUF. */
15946
15947 static unsigned char invalid_eol_type[] = "(*invalid*)";
15948
15949 static char *
15950 decode_mode_spec_coding (coding_system, buf, eol_flag)
15951 Lisp_Object coding_system;
15952 register char *buf;
15953 int eol_flag;
15954 {
15955 Lisp_Object val;
15956 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
15957 const unsigned char *eol_str;
15958 int eol_str_len;
15959 /* The EOL conversion we are using. */
15960 Lisp_Object eoltype;
15961
15962 val = Fget (coding_system, Qcoding_system);
15963 eoltype = Qnil;
15964
15965 if (!VECTORP (val)) /* Not yet decided. */
15966 {
15967 if (multibyte)
15968 *buf++ = '-';
15969 if (eol_flag)
15970 eoltype = eol_mnemonic_undecided;
15971 /* Don't mention EOL conversion if it isn't decided. */
15972 }
15973 else
15974 {
15975 Lisp_Object eolvalue;
15976
15977 eolvalue = Fget (coding_system, Qeol_type);
15978
15979 if (multibyte)
15980 *buf++ = XFASTINT (AREF (val, 1));
15981
15982 if (eol_flag)
15983 {
15984 /* The EOL conversion that is normal on this system. */
15985
15986 if (NILP (eolvalue)) /* Not yet decided. */
15987 eoltype = eol_mnemonic_undecided;
15988 else if (VECTORP (eolvalue)) /* Not yet decided. */
15989 eoltype = eol_mnemonic_undecided;
15990 else /* INTEGERP (eolvalue) -- 0:LF, 1:CRLF, 2:CR */
15991 eoltype = (XFASTINT (eolvalue) == 0
15992 ? eol_mnemonic_unix
15993 : (XFASTINT (eolvalue) == 1
15994 ? eol_mnemonic_dos : eol_mnemonic_mac));
15995 }
15996 }
15997
15998 if (eol_flag)
15999 {
16000 /* Mention the EOL conversion if it is not the usual one. */
16001 if (STRINGP (eoltype))
16002 {
16003 eol_str = SDATA (eoltype);
16004 eol_str_len = SBYTES (eoltype);
16005 }
16006 else if (INTEGERP (eoltype)
16007 && CHAR_VALID_P (XINT (eoltype), 0))
16008 {
16009 unsigned char *tmp = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
16010 eol_str_len = CHAR_STRING (XINT (eoltype), tmp);
16011 eol_str = tmp;
16012 }
16013 else
16014 {
16015 eol_str = invalid_eol_type;
16016 eol_str_len = sizeof (invalid_eol_type) - 1;
16017 }
16018 bcopy (eol_str, buf, eol_str_len);
16019 buf += eol_str_len;
16020 }
16021
16022 return buf;
16023 }
16024
16025 /* Return a string for the output of a mode line %-spec for window W,
16026 generated by character C. PRECISION >= 0 means don't return a
16027 string longer than that value. FIELD_WIDTH > 0 means pad the
16028 string returned with spaces to that value. Return 1 in *MULTIBYTE
16029 if the result is multibyte text. */
16030
16031 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
16032
16033 static char *
16034 decode_mode_spec (w, c, field_width, precision, multibyte)
16035 struct window *w;
16036 register int c;
16037 int field_width, precision;
16038 int *multibyte;
16039 {
16040 Lisp_Object obj;
16041 struct frame *f = XFRAME (WINDOW_FRAME (w));
16042 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
16043 struct buffer *b = XBUFFER (w->buffer);
16044
16045 obj = Qnil;
16046 *multibyte = 0;
16047
16048 switch (c)
16049 {
16050 case '*':
16051 if (!NILP (b->read_only))
16052 return "%";
16053 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
16054 return "*";
16055 return "-";
16056
16057 case '+':
16058 /* This differs from %* only for a modified read-only buffer. */
16059 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
16060 return "*";
16061 if (!NILP (b->read_only))
16062 return "%";
16063 return "-";
16064
16065 case '&':
16066 /* This differs from %* in ignoring read-only-ness. */
16067 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
16068 return "*";
16069 return "-";
16070
16071 case '%':
16072 return "%";
16073
16074 case '[':
16075 {
16076 int i;
16077 char *p;
16078
16079 if (command_loop_level > 5)
16080 return "[[[... ";
16081 p = decode_mode_spec_buf;
16082 for (i = 0; i < command_loop_level; i++)
16083 *p++ = '[';
16084 *p = 0;
16085 return decode_mode_spec_buf;
16086 }
16087
16088 case ']':
16089 {
16090 int i;
16091 char *p;
16092
16093 if (command_loop_level > 5)
16094 return " ...]]]";
16095 p = decode_mode_spec_buf;
16096 for (i = 0; i < command_loop_level; i++)
16097 *p++ = ']';
16098 *p = 0;
16099 return decode_mode_spec_buf;
16100 }
16101
16102 case '-':
16103 {
16104 register int i;
16105
16106 /* Let lots_of_dashes be a string of infinite length. */
16107 if (!NILP (mode_line_string_list))
16108 return "--";
16109 if (field_width <= 0
16110 || field_width > sizeof (lots_of_dashes))
16111 {
16112 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
16113 decode_mode_spec_buf[i] = '-';
16114 decode_mode_spec_buf[i] = '\0';
16115 return decode_mode_spec_buf;
16116 }
16117 else
16118 return lots_of_dashes;
16119 }
16120
16121 case 'b':
16122 obj = b->name;
16123 break;
16124
16125 case 'c':
16126 {
16127 int col = (int) current_column (); /* iftc */
16128 w->column_number_displayed = make_number (col);
16129 pint2str (decode_mode_spec_buf, field_width, col);
16130 return decode_mode_spec_buf;
16131 }
16132
16133 case 'F':
16134 /* %F displays the frame name. */
16135 if (!NILP (f->title))
16136 return (char *) SDATA (f->title);
16137 if (f->explicit_name || ! FRAME_WINDOW_P (f))
16138 return (char *) SDATA (f->name);
16139 return "Emacs";
16140
16141 case 'f':
16142 obj = b->filename;
16143 break;
16144
16145 case 'i':
16146 {
16147 int size = ZV - BEGV;
16148 pint2str (decode_mode_spec_buf, field_width, size);
16149 return decode_mode_spec_buf;
16150 }
16151
16152 case 'I':
16153 {
16154 int size = ZV - BEGV;
16155 pint2hrstr (decode_mode_spec_buf, field_width, size);
16156 return decode_mode_spec_buf;
16157 }
16158
16159 case 'l':
16160 {
16161 int startpos = XMARKER (w->start)->charpos;
16162 int startpos_byte = marker_byte_position (w->start);
16163 int line, linepos, linepos_byte, topline;
16164 int nlines, junk;
16165 int height = WINDOW_TOTAL_LINES (w);
16166
16167 /* If we decided that this buffer isn't suitable for line numbers,
16168 don't forget that too fast. */
16169 if (EQ (w->base_line_pos, w->buffer))
16170 goto no_value;
16171 /* But do forget it, if the window shows a different buffer now. */
16172 else if (BUFFERP (w->base_line_pos))
16173 w->base_line_pos = Qnil;
16174
16175 /* If the buffer is very big, don't waste time. */
16176 if (INTEGERP (Vline_number_display_limit)
16177 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
16178 {
16179 w->base_line_pos = Qnil;
16180 w->base_line_number = Qnil;
16181 goto no_value;
16182 }
16183
16184 if (!NILP (w->base_line_number)
16185 && !NILP (w->base_line_pos)
16186 && XFASTINT (w->base_line_pos) <= startpos)
16187 {
16188 line = XFASTINT (w->base_line_number);
16189 linepos = XFASTINT (w->base_line_pos);
16190 linepos_byte = buf_charpos_to_bytepos (b, linepos);
16191 }
16192 else
16193 {
16194 line = 1;
16195 linepos = BUF_BEGV (b);
16196 linepos_byte = BUF_BEGV_BYTE (b);
16197 }
16198
16199 /* Count lines from base line to window start position. */
16200 nlines = display_count_lines (linepos, linepos_byte,
16201 startpos_byte,
16202 startpos, &junk);
16203
16204 topline = nlines + line;
16205
16206 /* Determine a new base line, if the old one is too close
16207 or too far away, or if we did not have one.
16208 "Too close" means it's plausible a scroll-down would
16209 go back past it. */
16210 if (startpos == BUF_BEGV (b))
16211 {
16212 w->base_line_number = make_number (topline);
16213 w->base_line_pos = make_number (BUF_BEGV (b));
16214 }
16215 else if (nlines < height + 25 || nlines > height * 3 + 50
16216 || linepos == BUF_BEGV (b))
16217 {
16218 int limit = BUF_BEGV (b);
16219 int limit_byte = BUF_BEGV_BYTE (b);
16220 int position;
16221 int distance = (height * 2 + 30) * line_number_display_limit_width;
16222
16223 if (startpos - distance > limit)
16224 {
16225 limit = startpos - distance;
16226 limit_byte = CHAR_TO_BYTE (limit);
16227 }
16228
16229 nlines = display_count_lines (startpos, startpos_byte,
16230 limit_byte,
16231 - (height * 2 + 30),
16232 &position);
16233 /* If we couldn't find the lines we wanted within
16234 line_number_display_limit_width chars per line,
16235 give up on line numbers for this window. */
16236 if (position == limit_byte && limit == startpos - distance)
16237 {
16238 w->base_line_pos = w->buffer;
16239 w->base_line_number = Qnil;
16240 goto no_value;
16241 }
16242
16243 w->base_line_number = make_number (topline - nlines);
16244 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
16245 }
16246
16247 /* Now count lines from the start pos to point. */
16248 nlines = display_count_lines (startpos, startpos_byte,
16249 PT_BYTE, PT, &junk);
16250
16251 /* Record that we did display the line number. */
16252 line_number_displayed = 1;
16253
16254 /* Make the string to show. */
16255 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
16256 return decode_mode_spec_buf;
16257 no_value:
16258 {
16259 char* p = decode_mode_spec_buf;
16260 int pad = field_width - 2;
16261 while (pad-- > 0)
16262 *p++ = ' ';
16263 *p++ = '?';
16264 *p++ = '?';
16265 *p = '\0';
16266 return decode_mode_spec_buf;
16267 }
16268 }
16269 break;
16270
16271 case 'm':
16272 obj = b->mode_name;
16273 break;
16274
16275 case 'n':
16276 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
16277 return " Narrow";
16278 break;
16279
16280 case 'p':
16281 {
16282 int pos = marker_position (w->start);
16283 int total = BUF_ZV (b) - BUF_BEGV (b);
16284
16285 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
16286 {
16287 if (pos <= BUF_BEGV (b))
16288 return "All";
16289 else
16290 return "Bottom";
16291 }
16292 else if (pos <= BUF_BEGV (b))
16293 return "Top";
16294 else
16295 {
16296 if (total > 1000000)
16297 /* Do it differently for a large value, to avoid overflow. */
16298 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
16299 else
16300 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
16301 /* We can't normally display a 3-digit number,
16302 so get us a 2-digit number that is close. */
16303 if (total == 100)
16304 total = 99;
16305 sprintf (decode_mode_spec_buf, "%2d%%", total);
16306 return decode_mode_spec_buf;
16307 }
16308 }
16309
16310 /* Display percentage of size above the bottom of the screen. */
16311 case 'P':
16312 {
16313 int toppos = marker_position (w->start);
16314 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
16315 int total = BUF_ZV (b) - BUF_BEGV (b);
16316
16317 if (botpos >= BUF_ZV (b))
16318 {
16319 if (toppos <= BUF_BEGV (b))
16320 return "All";
16321 else
16322 return "Bottom";
16323 }
16324 else
16325 {
16326 if (total > 1000000)
16327 /* Do it differently for a large value, to avoid overflow. */
16328 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
16329 else
16330 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
16331 /* We can't normally display a 3-digit number,
16332 so get us a 2-digit number that is close. */
16333 if (total == 100)
16334 total = 99;
16335 if (toppos <= BUF_BEGV (b))
16336 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
16337 else
16338 sprintf (decode_mode_spec_buf, "%2d%%", total);
16339 return decode_mode_spec_buf;
16340 }
16341 }
16342
16343 case 's':
16344 /* status of process */
16345 obj = Fget_buffer_process (w->buffer);
16346 if (NILP (obj))
16347 return "no process";
16348 #ifdef subprocesses
16349 obj = Fsymbol_name (Fprocess_status (obj));
16350 #endif
16351 break;
16352
16353 case 't': /* indicate TEXT or BINARY */
16354 #ifdef MODE_LINE_BINARY_TEXT
16355 return MODE_LINE_BINARY_TEXT (b);
16356 #else
16357 return "T";
16358 #endif
16359
16360 case 'z':
16361 /* coding-system (not including end-of-line format) */
16362 case 'Z':
16363 /* coding-system (including end-of-line type) */
16364 {
16365 int eol_flag = (c == 'Z');
16366 char *p = decode_mode_spec_buf;
16367
16368 if (! FRAME_WINDOW_P (f))
16369 {
16370 /* No need to mention EOL here--the terminal never needs
16371 to do EOL conversion. */
16372 p = decode_mode_spec_coding (FRAME_KEYBOARD_CODING (f)->symbol, p, 0);
16373 p = decode_mode_spec_coding (FRAME_TERMINAL_CODING (f)->symbol, p, 0);
16374 }
16375 p = decode_mode_spec_coding (b->buffer_file_coding_system,
16376 p, eol_flag);
16377
16378 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
16379 #ifdef subprocesses
16380 obj = Fget_buffer_process (Fcurrent_buffer ());
16381 if (PROCESSP (obj))
16382 {
16383 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
16384 p, eol_flag);
16385 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
16386 p, eol_flag);
16387 }
16388 #endif /* subprocesses */
16389 #endif /* 0 */
16390 *p = 0;
16391 return decode_mode_spec_buf;
16392 }
16393 }
16394
16395 if (STRINGP (obj))
16396 {
16397 *multibyte = STRING_MULTIBYTE (obj);
16398 return (char *) SDATA (obj);
16399 }
16400 else
16401 return "";
16402 }
16403
16404
16405 /* Count up to COUNT lines starting from START / START_BYTE.
16406 But don't go beyond LIMIT_BYTE.
16407 Return the number of lines thus found (always nonnegative).
16408
16409 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
16410
16411 static int
16412 display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
16413 int start, start_byte, limit_byte, count;
16414 int *byte_pos_ptr;
16415 {
16416 register unsigned char *cursor;
16417 unsigned char *base;
16418
16419 register int ceiling;
16420 register unsigned char *ceiling_addr;
16421 int orig_count = count;
16422
16423 /* If we are not in selective display mode,
16424 check only for newlines. */
16425 int selective_display = (!NILP (current_buffer->selective_display)
16426 && !INTEGERP (current_buffer->selective_display));
16427
16428 if (count > 0)
16429 {
16430 while (start_byte < limit_byte)
16431 {
16432 ceiling = BUFFER_CEILING_OF (start_byte);
16433 ceiling = min (limit_byte - 1, ceiling);
16434 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
16435 base = (cursor = BYTE_POS_ADDR (start_byte));
16436 while (1)
16437 {
16438 if (selective_display)
16439 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
16440 ;
16441 else
16442 while (*cursor != '\n' && ++cursor != ceiling_addr)
16443 ;
16444
16445 if (cursor != ceiling_addr)
16446 {
16447 if (--count == 0)
16448 {
16449 start_byte += cursor - base + 1;
16450 *byte_pos_ptr = start_byte;
16451 return orig_count;
16452 }
16453 else
16454 if (++cursor == ceiling_addr)
16455 break;
16456 }
16457 else
16458 break;
16459 }
16460 start_byte += cursor - base;
16461 }
16462 }
16463 else
16464 {
16465 while (start_byte > limit_byte)
16466 {
16467 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
16468 ceiling = max (limit_byte, ceiling);
16469 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
16470 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
16471 while (1)
16472 {
16473 if (selective_display)
16474 while (--cursor != ceiling_addr
16475 && *cursor != '\n' && *cursor != 015)
16476 ;
16477 else
16478 while (--cursor != ceiling_addr && *cursor != '\n')
16479 ;
16480
16481 if (cursor != ceiling_addr)
16482 {
16483 if (++count == 0)
16484 {
16485 start_byte += cursor - base + 1;
16486 *byte_pos_ptr = start_byte;
16487 /* When scanning backwards, we should
16488 not count the newline posterior to which we stop. */
16489 return - orig_count - 1;
16490 }
16491 }
16492 else
16493 break;
16494 }
16495 /* Here we add 1 to compensate for the last decrement
16496 of CURSOR, which took it past the valid range. */
16497 start_byte += cursor - base + 1;
16498 }
16499 }
16500
16501 *byte_pos_ptr = limit_byte;
16502
16503 if (count < 0)
16504 return - orig_count + count;
16505 return orig_count - count;
16506
16507 }
16508
16509
16510 \f
16511 /***********************************************************************
16512 Displaying strings
16513 ***********************************************************************/
16514
16515 /* Display a NUL-terminated string, starting with index START.
16516
16517 If STRING is non-null, display that C string. Otherwise, the Lisp
16518 string LISP_STRING is displayed.
16519
16520 If FACE_STRING is not nil, FACE_STRING_POS is a position in
16521 FACE_STRING. Display STRING or LISP_STRING with the face at
16522 FACE_STRING_POS in FACE_STRING:
16523
16524 Display the string in the environment given by IT, but use the
16525 standard display table, temporarily.
16526
16527 FIELD_WIDTH is the minimum number of output glyphs to produce.
16528 If STRING has fewer characters than FIELD_WIDTH, pad to the right
16529 with spaces. If STRING has more characters, more than FIELD_WIDTH
16530 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
16531
16532 PRECISION is the maximum number of characters to output from
16533 STRING. PRECISION < 0 means don't truncate the string.
16534
16535 This is roughly equivalent to printf format specifiers:
16536
16537 FIELD_WIDTH PRECISION PRINTF
16538 ----------------------------------------
16539 -1 -1 %s
16540 -1 10 %.10s
16541 10 -1 %10s
16542 20 10 %20.10s
16543
16544 MULTIBYTE zero means do not display multibyte chars, > 0 means do
16545 display them, and < 0 means obey the current buffer's value of
16546 enable_multibyte_characters.
16547
16548 Value is the number of glyphs produced. */
16549
16550 static int
16551 display_string (string, lisp_string, face_string, face_string_pos,
16552 start, it, field_width, precision, max_x, multibyte)
16553 unsigned char *string;
16554 Lisp_Object lisp_string;
16555 Lisp_Object face_string;
16556 int face_string_pos;
16557 int start;
16558 struct it *it;
16559 int field_width, precision, max_x;
16560 int multibyte;
16561 {
16562 int hpos_at_start = it->hpos;
16563 int saved_face_id = it->face_id;
16564 struct glyph_row *row = it->glyph_row;
16565
16566 /* Initialize the iterator IT for iteration over STRING beginning
16567 with index START. */
16568 reseat_to_string (it, string, lisp_string, start,
16569 precision, field_width, multibyte);
16570
16571 /* If displaying STRING, set up the face of the iterator
16572 from LISP_STRING, if that's given. */
16573 if (STRINGP (face_string))
16574 {
16575 int endptr;
16576 struct face *face;
16577
16578 it->face_id
16579 = face_at_string_position (it->w, face_string, face_string_pos,
16580 0, it->region_beg_charpos,
16581 it->region_end_charpos,
16582 &endptr, it->base_face_id, 0);
16583 face = FACE_FROM_ID (it->f, it->face_id);
16584 it->face_box_p = face->box != FACE_NO_BOX;
16585 }
16586
16587 /* Set max_x to the maximum allowed X position. Don't let it go
16588 beyond the right edge of the window. */
16589 if (max_x <= 0)
16590 max_x = it->last_visible_x;
16591 else
16592 max_x = min (max_x, it->last_visible_x);
16593
16594 /* Skip over display elements that are not visible. because IT->w is
16595 hscrolled. */
16596 if (it->current_x < it->first_visible_x)
16597 move_it_in_display_line_to (it, 100000, it->first_visible_x,
16598 MOVE_TO_POS | MOVE_TO_X);
16599
16600 row->ascent = it->max_ascent;
16601 row->height = it->max_ascent + it->max_descent;
16602 row->phys_ascent = it->max_phys_ascent;
16603 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
16604
16605 /* This condition is for the case that we are called with current_x
16606 past last_visible_x. */
16607 while (it->current_x < max_x)
16608 {
16609 int x_before, x, n_glyphs_before, i, nglyphs;
16610
16611 /* Get the next display element. */
16612 if (!get_next_display_element (it))
16613 break;
16614
16615 /* Produce glyphs. */
16616 x_before = it->current_x;
16617 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
16618 PRODUCE_GLYPHS (it);
16619
16620 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
16621 i = 0;
16622 x = x_before;
16623 while (i < nglyphs)
16624 {
16625 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
16626
16627 if (!it->truncate_lines_p
16628 && x + glyph->pixel_width > max_x)
16629 {
16630 /* End of continued line or max_x reached. */
16631 if (CHAR_GLYPH_PADDING_P (*glyph))
16632 {
16633 /* A wide character is unbreakable. */
16634 it->glyph_row->used[TEXT_AREA] = n_glyphs_before;
16635 it->current_x = x_before;
16636 }
16637 else
16638 {
16639 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
16640 it->current_x = x;
16641 }
16642 break;
16643 }
16644 else if (x + glyph->pixel_width > it->first_visible_x)
16645 {
16646 /* Glyph is at least partially visible. */
16647 ++it->hpos;
16648 if (x < it->first_visible_x)
16649 it->glyph_row->x = x - it->first_visible_x;
16650 }
16651 else
16652 {
16653 /* Glyph is off the left margin of the display area.
16654 Should not happen. */
16655 abort ();
16656 }
16657
16658 row->ascent = max (row->ascent, it->max_ascent);
16659 row->height = max (row->height, it->max_ascent + it->max_descent);
16660 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
16661 row->phys_height = max (row->phys_height,
16662 it->max_phys_ascent + it->max_phys_descent);
16663 x += glyph->pixel_width;
16664 ++i;
16665 }
16666
16667 /* Stop if max_x reached. */
16668 if (i < nglyphs)
16669 break;
16670
16671 /* Stop at line ends. */
16672 if (ITERATOR_AT_END_OF_LINE_P (it))
16673 {
16674 it->continuation_lines_width = 0;
16675 break;
16676 }
16677
16678 set_iterator_to_next (it, 1);
16679
16680 /* Stop if truncating at the right edge. */
16681 if (it->truncate_lines_p
16682 && it->current_x >= it->last_visible_x)
16683 {
16684 /* Add truncation mark, but don't do it if the line is
16685 truncated at a padding space. */
16686 if (IT_CHARPOS (*it) < it->string_nchars)
16687 {
16688 if (!FRAME_WINDOW_P (it->f))
16689 {
16690 int i, n;
16691
16692 if (it->current_x > it->last_visible_x)
16693 {
16694 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
16695 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
16696 break;
16697 for (n = row->used[TEXT_AREA]; i < n; ++i)
16698 {
16699 row->used[TEXT_AREA] = i;
16700 produce_special_glyphs (it, IT_TRUNCATION);
16701 }
16702 }
16703 produce_special_glyphs (it, IT_TRUNCATION);
16704 }
16705 it->glyph_row->truncated_on_right_p = 1;
16706 }
16707 break;
16708 }
16709 }
16710
16711 /* Maybe insert a truncation at the left. */
16712 if (it->first_visible_x
16713 && IT_CHARPOS (*it) > 0)
16714 {
16715 if (!FRAME_WINDOW_P (it->f))
16716 insert_left_trunc_glyphs (it);
16717 it->glyph_row->truncated_on_left_p = 1;
16718 }
16719
16720 it->face_id = saved_face_id;
16721
16722 /* Value is number of columns displayed. */
16723 return it->hpos - hpos_at_start;
16724 }
16725
16726
16727 \f
16728 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
16729 appears as an element of LIST or as the car of an element of LIST.
16730 If PROPVAL is a list, compare each element against LIST in that
16731 way, and return 1/2 if any element of PROPVAL is found in LIST.
16732 Otherwise return 0. This function cannot quit.
16733 The return value is 2 if the text is invisible but with an ellipsis
16734 and 1 if it's invisible and without an ellipsis. */
16735
16736 int
16737 invisible_p (propval, list)
16738 register Lisp_Object propval;
16739 Lisp_Object list;
16740 {
16741 register Lisp_Object tail, proptail;
16742
16743 for (tail = list; CONSP (tail); tail = XCDR (tail))
16744 {
16745 register Lisp_Object tem;
16746 tem = XCAR (tail);
16747 if (EQ (propval, tem))
16748 return 1;
16749 if (CONSP (tem) && EQ (propval, XCAR (tem)))
16750 return NILP (XCDR (tem)) ? 1 : 2;
16751 }
16752
16753 if (CONSP (propval))
16754 {
16755 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
16756 {
16757 Lisp_Object propelt;
16758 propelt = XCAR (proptail);
16759 for (tail = list; CONSP (tail); tail = XCDR (tail))
16760 {
16761 register Lisp_Object tem;
16762 tem = XCAR (tail);
16763 if (EQ (propelt, tem))
16764 return 1;
16765 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
16766 return NILP (XCDR (tem)) ? 1 : 2;
16767 }
16768 }
16769 }
16770
16771 return 0;
16772 }
16773
16774 /* Calculate a width or height in pixels from a specification using
16775 the following elements:
16776
16777 SPEC ::=
16778 NUM - a (fractional) multiple of the default font width/height
16779 (NUM) - specifies exactly NUM pixels
16780 UNIT - a fixed number of pixels, see below.
16781 ELEMENT - size of a display element in pixels, see below.
16782 (NUM . SPEC) - equals NUM * SPEC
16783 (+ SPEC SPEC ...) - add pixel values
16784 (- SPEC SPEC ...) - subtract pixel values
16785 (- SPEC) - negate pixel value
16786
16787 NUM ::=
16788 INT or FLOAT - a number constant
16789 SYMBOL - use symbol's (buffer local) variable binding.
16790
16791 UNIT ::=
16792 in - pixels per inch *)
16793 mm - pixels per 1/1000 meter *)
16794 cm - pixels per 1/100 meter *)
16795 width - width of current font in pixels.
16796 height - height of current font in pixels.
16797
16798 *) using the ratio(s) defined in display-pixels-per-inch.
16799
16800 ELEMENT ::=
16801
16802 left-fringe - left fringe width in pixels
16803 right-fringe - right fringe width in pixels
16804
16805 left-margin - left margin width in pixels
16806 right-margin - right margin width in pixels
16807
16808 scroll-bar - scroll-bar area width in pixels
16809
16810 Examples:
16811
16812 Pixels corresponding to 5 inches:
16813 (5 . in)
16814
16815 Total width of non-text areas on left side of window (if scroll-bar is on left):
16816 '(space :width (+ left-fringe left-margin scroll-bar))
16817
16818 Align to first text column (in header line):
16819 '(space :align-to 0)
16820
16821 Align to middle of text area minus half the width of variable `my-image'
16822 containing a loaded image:
16823 '(space :align-to (0.5 . (- text my-image)))
16824
16825 Width of left margin minus width of 1 character in the default font:
16826 '(space :width (- left-margin 1))
16827
16828 Width of left margin minus width of 2 characters in the current font:
16829 '(space :width (- left-margin (2 . width)))
16830
16831 Center 1 character over left-margin (in header line):
16832 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
16833
16834 Different ways to express width of left fringe plus left margin minus one pixel:
16835 '(space :width (- (+ left-fringe left-margin) (1)))
16836 '(space :width (+ left-fringe left-margin (- (1))))
16837 '(space :width (+ left-fringe left-margin (-1)))
16838
16839 */
16840
16841 #define NUMVAL(X) \
16842 ((INTEGERP (X) || FLOATP (X)) \
16843 ? XFLOATINT (X) \
16844 : - 1)
16845
16846 int
16847 calc_pixel_width_or_height (res, it, prop, font, width_p, align_to)
16848 double *res;
16849 struct it *it;
16850 Lisp_Object prop;
16851 void *font;
16852 int width_p, *align_to;
16853 {
16854 double pixels;
16855
16856 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
16857 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
16858
16859 if (NILP (prop))
16860 return OK_PIXELS (0);
16861
16862 if (SYMBOLP (prop))
16863 {
16864 if (SCHARS (SYMBOL_NAME (prop)) == 2)
16865 {
16866 char *unit = SDATA (SYMBOL_NAME (prop));
16867
16868 if (unit[0] == 'i' && unit[1] == 'n')
16869 pixels = 1.0;
16870 else if (unit[0] == 'm' && unit[1] == 'm')
16871 pixels = 25.4;
16872 else if (unit[0] == 'c' && unit[1] == 'm')
16873 pixels = 2.54;
16874 else
16875 pixels = 0;
16876 if (pixels > 0)
16877 {
16878 double ppi;
16879 if ((ppi = NUMVAL (Vdisplay_pixels_per_inch), ppi > 0)
16880 || (CONSP (Vdisplay_pixels_per_inch)
16881 && (ppi = (width_p
16882 ? NUMVAL (XCAR (Vdisplay_pixels_per_inch))
16883 : NUMVAL (XCDR (Vdisplay_pixels_per_inch))),
16884 ppi > 0)))
16885 return OK_PIXELS (ppi / pixels);
16886
16887 return 0;
16888 }
16889 }
16890
16891 #ifdef HAVE_WINDOW_SYSTEM
16892 if (EQ (prop, Qheight))
16893 return OK_PIXELS (font ? FONT_HEIGHT ((XFontStruct *)font) : FRAME_LINE_HEIGHT (it->f));
16894 if (EQ (prop, Qwidth))
16895 return OK_PIXELS (font ? FONT_WIDTH ((XFontStruct *)font) : FRAME_COLUMN_WIDTH (it->f));
16896 #else
16897 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
16898 return OK_PIXELS (1);
16899 #endif
16900
16901 if (EQ (prop, Qtext))
16902 return OK_PIXELS (width_p
16903 ? window_box_width (it->w, TEXT_AREA)
16904 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
16905
16906 if (align_to && *align_to < 0)
16907 {
16908 *res = 0;
16909 if (EQ (prop, Qleft))
16910 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
16911 if (EQ (prop, Qright))
16912 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
16913 if (EQ (prop, Qcenter))
16914 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
16915 + window_box_width (it->w, TEXT_AREA) / 2);
16916 if (EQ (prop, Qleft_fringe))
16917 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
16918 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
16919 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
16920 if (EQ (prop, Qright_fringe))
16921 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
16922 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
16923 : window_box_right_offset (it->w, TEXT_AREA));
16924 if (EQ (prop, Qleft_margin))
16925 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
16926 if (EQ (prop, Qright_margin))
16927 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
16928 if (EQ (prop, Qscroll_bar))
16929 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
16930 ? 0
16931 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
16932 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
16933 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
16934 : 0)));
16935 }
16936 else
16937 {
16938 if (EQ (prop, Qleft_fringe))
16939 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
16940 if (EQ (prop, Qright_fringe))
16941 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
16942 if (EQ (prop, Qleft_margin))
16943 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
16944 if (EQ (prop, Qright_margin))
16945 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
16946 if (EQ (prop, Qscroll_bar))
16947 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
16948 }
16949
16950 prop = Fbuffer_local_value (prop, it->w->buffer);
16951 }
16952
16953 if (INTEGERP (prop) || FLOATP (prop))
16954 {
16955 int base_unit = (width_p
16956 ? FRAME_COLUMN_WIDTH (it->f)
16957 : FRAME_LINE_HEIGHT (it->f));
16958 return OK_PIXELS (XFLOATINT (prop) * base_unit);
16959 }
16960
16961 if (CONSP (prop))
16962 {
16963 Lisp_Object car = XCAR (prop);
16964 Lisp_Object cdr = XCDR (prop);
16965
16966 if (SYMBOLP (car))
16967 {
16968 #ifdef HAVE_WINDOW_SYSTEM
16969 if (valid_image_p (prop))
16970 {
16971 int id = lookup_image (it->f, prop);
16972 struct image *img = IMAGE_FROM_ID (it->f, id);
16973
16974 return OK_PIXELS (width_p ? img->width : img->height);
16975 }
16976 #endif
16977 if (EQ (car, Qplus) || EQ (car, Qminus))
16978 {
16979 int first = 1;
16980 double px;
16981
16982 pixels = 0;
16983 while (CONSP (cdr))
16984 {
16985 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
16986 font, width_p, align_to))
16987 return 0;
16988 if (first)
16989 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
16990 else
16991 pixels += px;
16992 cdr = XCDR (cdr);
16993 }
16994 if (EQ (car, Qminus))
16995 pixels = -pixels;
16996 return OK_PIXELS (pixels);
16997 }
16998
16999 car = Fbuffer_local_value (car, it->w->buffer);
17000 }
17001
17002 if (INTEGERP (car) || FLOATP (car))
17003 {
17004 double fact;
17005 pixels = XFLOATINT (car);
17006 if (NILP (cdr))
17007 return OK_PIXELS (pixels);
17008 if (calc_pixel_width_or_height (&fact, it, cdr,
17009 font, width_p, align_to))
17010 return OK_PIXELS (pixels * fact);
17011 return 0;
17012 }
17013
17014 return 0;
17015 }
17016
17017 return 0;
17018 }
17019
17020 \f
17021 /***********************************************************************
17022 Glyph Display
17023 ***********************************************************************/
17024
17025 #ifdef HAVE_WINDOW_SYSTEM
17026
17027 #if GLYPH_DEBUG
17028
17029 void
17030 dump_glyph_string (s)
17031 struct glyph_string *s;
17032 {
17033 fprintf (stderr, "glyph string\n");
17034 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
17035 s->x, s->y, s->width, s->height);
17036 fprintf (stderr, " ybase = %d\n", s->ybase);
17037 fprintf (stderr, " hl = %d\n", s->hl);
17038 fprintf (stderr, " left overhang = %d, right = %d\n",
17039 s->left_overhang, s->right_overhang);
17040 fprintf (stderr, " nchars = %d\n", s->nchars);
17041 fprintf (stderr, " extends to end of line = %d\n",
17042 s->extends_to_end_of_line_p);
17043 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
17044 fprintf (stderr, " bg width = %d\n", s->background_width);
17045 }
17046
17047 #endif /* GLYPH_DEBUG */
17048
17049 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
17050 of XChar2b structures for S; it can't be allocated in
17051 init_glyph_string because it must be allocated via `alloca'. W
17052 is the window on which S is drawn. ROW and AREA are the glyph row
17053 and area within the row from which S is constructed. START is the
17054 index of the first glyph structure covered by S. HL is a
17055 face-override for drawing S. */
17056
17057 #ifdef HAVE_NTGUI
17058 #define OPTIONAL_HDC(hdc) hdc,
17059 #define DECLARE_HDC(hdc) HDC hdc;
17060 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
17061 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
17062 #endif
17063
17064 #ifndef OPTIONAL_HDC
17065 #define OPTIONAL_HDC(hdc)
17066 #define DECLARE_HDC(hdc)
17067 #define ALLOCATE_HDC(hdc, f)
17068 #define RELEASE_HDC(hdc, f)
17069 #endif
17070
17071 static void
17072 init_glyph_string (s, OPTIONAL_HDC (hdc) char2b, w, row, area, start, hl)
17073 struct glyph_string *s;
17074 DECLARE_HDC (hdc)
17075 XChar2b *char2b;
17076 struct window *w;
17077 struct glyph_row *row;
17078 enum glyph_row_area area;
17079 int start;
17080 enum draw_glyphs_face hl;
17081 {
17082 bzero (s, sizeof *s);
17083 s->w = w;
17084 s->f = XFRAME (w->frame);
17085 #ifdef HAVE_NTGUI
17086 s->hdc = hdc;
17087 #endif
17088 s->display = FRAME_X_DISPLAY (s->f);
17089 s->window = FRAME_X_WINDOW (s->f);
17090 s->char2b = char2b;
17091 s->hl = hl;
17092 s->row = row;
17093 s->area = area;
17094 s->first_glyph = row->glyphs[area] + start;
17095 s->height = row->height;
17096 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
17097
17098 /* Display the internal border below the tool-bar window. */
17099 if (s->w == XWINDOW (s->f->tool_bar_window))
17100 s->y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
17101
17102 s->ybase = s->y + row->ascent;
17103 }
17104
17105
17106 /* Append the list of glyph strings with head H and tail T to the list
17107 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
17108
17109 static INLINE void
17110 append_glyph_string_lists (head, tail, h, t)
17111 struct glyph_string **head, **tail;
17112 struct glyph_string *h, *t;
17113 {
17114 if (h)
17115 {
17116 if (*head)
17117 (*tail)->next = h;
17118 else
17119 *head = h;
17120 h->prev = *tail;
17121 *tail = t;
17122 }
17123 }
17124
17125
17126 /* Prepend the list of glyph strings with head H and tail T to the
17127 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
17128 result. */
17129
17130 static INLINE void
17131 prepend_glyph_string_lists (head, tail, h, t)
17132 struct glyph_string **head, **tail;
17133 struct glyph_string *h, *t;
17134 {
17135 if (h)
17136 {
17137 if (*head)
17138 (*head)->prev = t;
17139 else
17140 *tail = t;
17141 t->next = *head;
17142 *head = h;
17143 }
17144 }
17145
17146
17147 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
17148 Set *HEAD and *TAIL to the resulting list. */
17149
17150 static INLINE void
17151 append_glyph_string (head, tail, s)
17152 struct glyph_string **head, **tail;
17153 struct glyph_string *s;
17154 {
17155 s->next = s->prev = NULL;
17156 append_glyph_string_lists (head, tail, s, s);
17157 }
17158
17159
17160 /* Get face and two-byte form of character glyph GLYPH on frame F.
17161 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
17162 a pointer to a realized face that is ready for display. */
17163
17164 static INLINE struct face *
17165 get_glyph_face_and_encoding (f, glyph, char2b, two_byte_p)
17166 struct frame *f;
17167 struct glyph *glyph;
17168 XChar2b *char2b;
17169 int *two_byte_p;
17170 {
17171 struct face *face;
17172
17173 xassert (glyph->type == CHAR_GLYPH);
17174 face = FACE_FROM_ID (f, glyph->face_id);
17175
17176 if (two_byte_p)
17177 *two_byte_p = 0;
17178
17179 if (!glyph->multibyte_p)
17180 {
17181 /* Unibyte case. We don't have to encode, but we have to make
17182 sure to use a face suitable for unibyte. */
17183 STORE_XCHAR2B (char2b, 0, glyph->u.ch);
17184 }
17185 else if (glyph->u.ch < 128
17186 && glyph->face_id < BASIC_FACE_ID_SENTINEL)
17187 {
17188 /* Case of ASCII in a face known to fit ASCII. */
17189 STORE_XCHAR2B (char2b, 0, glyph->u.ch);
17190 }
17191 else
17192 {
17193 int c1, c2, charset;
17194
17195 /* Split characters into bytes. If c2 is -1 afterwards, C is
17196 really a one-byte character so that byte1 is zero. */
17197 SPLIT_CHAR (glyph->u.ch, charset, c1, c2);
17198 if (c2 > 0)
17199 STORE_XCHAR2B (char2b, c1, c2);
17200 else
17201 STORE_XCHAR2B (char2b, 0, c1);
17202
17203 /* Maybe encode the character in *CHAR2B. */
17204 if (charset != CHARSET_ASCII)
17205 {
17206 struct font_info *font_info
17207 = FONT_INFO_FROM_ID (f, face->font_info_id);
17208 if (font_info)
17209 glyph->font_type
17210 = FRAME_RIF (f)->encode_char (glyph->u.ch, char2b, font_info, two_byte_p);
17211 }
17212 }
17213
17214 /* Make sure X resources of the face are allocated. */
17215 xassert (face != NULL);
17216 PREPARE_FACE_FOR_DISPLAY (f, face);
17217 return face;
17218 }
17219
17220
17221 /* Fill glyph string S with composition components specified by S->cmp.
17222
17223 FACES is an array of faces for all components of this composition.
17224 S->gidx is the index of the first component for S.
17225 OVERLAPS_P non-zero means S should draw the foreground only, and
17226 use its physical height for clipping.
17227
17228 Value is the index of a component not in S. */
17229
17230 static int
17231 fill_composite_glyph_string (s, faces, overlaps_p)
17232 struct glyph_string *s;
17233 struct face **faces;
17234 int overlaps_p;
17235 {
17236 int i;
17237
17238 xassert (s);
17239
17240 s->for_overlaps_p = overlaps_p;
17241
17242 s->face = faces[s->gidx];
17243 s->font = s->face->font;
17244 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
17245
17246 /* For all glyphs of this composition, starting at the offset
17247 S->gidx, until we reach the end of the definition or encounter a
17248 glyph that requires the different face, add it to S. */
17249 ++s->nchars;
17250 for (i = s->gidx + 1; i < s->cmp->glyph_len && faces[i] == s->face; ++i)
17251 ++s->nchars;
17252
17253 /* All glyph strings for the same composition has the same width,
17254 i.e. the width set for the first component of the composition. */
17255
17256 s->width = s->first_glyph->pixel_width;
17257
17258 /* If the specified font could not be loaded, use the frame's
17259 default font, but record the fact that we couldn't load it in
17260 the glyph string so that we can draw rectangles for the
17261 characters of the glyph string. */
17262 if (s->font == NULL)
17263 {
17264 s->font_not_found_p = 1;
17265 s->font = FRAME_FONT (s->f);
17266 }
17267
17268 /* Adjust base line for subscript/superscript text. */
17269 s->ybase += s->first_glyph->voffset;
17270
17271 xassert (s->face && s->face->gc);
17272
17273 /* This glyph string must always be drawn with 16-bit functions. */
17274 s->two_byte_p = 1;
17275
17276 return s->gidx + s->nchars;
17277 }
17278
17279
17280 /* Fill glyph string S from a sequence of character glyphs.
17281
17282 FACE_ID is the face id of the string. START is the index of the
17283 first glyph to consider, END is the index of the last + 1.
17284 OVERLAPS_P non-zero means S should draw the foreground only, and
17285 use its physical height for clipping.
17286
17287 Value is the index of the first glyph not in S. */
17288
17289 static int
17290 fill_glyph_string (s, face_id, start, end, overlaps_p)
17291 struct glyph_string *s;
17292 int face_id;
17293 int start, end, overlaps_p;
17294 {
17295 struct glyph *glyph, *last;
17296 int voffset;
17297 int glyph_not_available_p;
17298
17299 xassert (s->f == XFRAME (s->w->frame));
17300 xassert (s->nchars == 0);
17301 xassert (start >= 0 && end > start);
17302
17303 s->for_overlaps_p = overlaps_p,
17304 glyph = s->row->glyphs[s->area] + start;
17305 last = s->row->glyphs[s->area] + end;
17306 voffset = glyph->voffset;
17307
17308 glyph_not_available_p = glyph->glyph_not_available_p;
17309
17310 while (glyph < last
17311 && glyph->type == CHAR_GLYPH
17312 && glyph->voffset == voffset
17313 /* Same face id implies same font, nowadays. */
17314 && glyph->face_id == face_id
17315 && glyph->glyph_not_available_p == glyph_not_available_p)
17316 {
17317 int two_byte_p;
17318
17319 s->face = get_glyph_face_and_encoding (s->f, glyph,
17320 s->char2b + s->nchars,
17321 &two_byte_p);
17322 s->two_byte_p = two_byte_p;
17323 ++s->nchars;
17324 xassert (s->nchars <= end - start);
17325 s->width += glyph->pixel_width;
17326 ++glyph;
17327 }
17328
17329 s->font = s->face->font;
17330 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
17331
17332 /* If the specified font could not be loaded, use the frame's font,
17333 but record the fact that we couldn't load it in
17334 S->font_not_found_p so that we can draw rectangles for the
17335 characters of the glyph string. */
17336 if (s->font == NULL || glyph_not_available_p)
17337 {
17338 s->font_not_found_p = 1;
17339 s->font = FRAME_FONT (s->f);
17340 }
17341
17342 /* Adjust base line for subscript/superscript text. */
17343 s->ybase += voffset;
17344
17345 xassert (s->face && s->face->gc);
17346 return glyph - s->row->glyphs[s->area];
17347 }
17348
17349
17350 /* Fill glyph string S from image glyph S->first_glyph. */
17351
17352 static void
17353 fill_image_glyph_string (s)
17354 struct glyph_string *s;
17355 {
17356 xassert (s->first_glyph->type == IMAGE_GLYPH);
17357 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
17358 xassert (s->img);
17359 s->slice = s->first_glyph->slice;
17360 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
17361 s->font = s->face->font;
17362 s->width = s->first_glyph->pixel_width;
17363
17364 /* Adjust base line for subscript/superscript text. */
17365 s->ybase += s->first_glyph->voffset;
17366 }
17367
17368
17369 /* Fill glyph string S from a sequence of stretch glyphs.
17370
17371 ROW is the glyph row in which the glyphs are found, AREA is the
17372 area within the row. START is the index of the first glyph to
17373 consider, END is the index of the last + 1.
17374
17375 Value is the index of the first glyph not in S. */
17376
17377 static int
17378 fill_stretch_glyph_string (s, row, area, start, end)
17379 struct glyph_string *s;
17380 struct glyph_row *row;
17381 enum glyph_row_area area;
17382 int start, end;
17383 {
17384 struct glyph *glyph, *last;
17385 int voffset, face_id;
17386
17387 xassert (s->first_glyph->type == STRETCH_GLYPH);
17388
17389 glyph = s->row->glyphs[s->area] + start;
17390 last = s->row->glyphs[s->area] + end;
17391 face_id = glyph->face_id;
17392 s->face = FACE_FROM_ID (s->f, face_id);
17393 s->font = s->face->font;
17394 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
17395 s->width = glyph->pixel_width;
17396 voffset = glyph->voffset;
17397
17398 for (++glyph;
17399 (glyph < last
17400 && glyph->type == STRETCH_GLYPH
17401 && glyph->voffset == voffset
17402 && glyph->face_id == face_id);
17403 ++glyph)
17404 s->width += glyph->pixel_width;
17405
17406 /* Adjust base line for subscript/superscript text. */
17407 s->ybase += voffset;
17408
17409 /* The case that face->gc == 0 is handled when drawing the glyph
17410 string by calling PREPARE_FACE_FOR_DISPLAY. */
17411 xassert (s->face);
17412 return glyph - s->row->glyphs[s->area];
17413 }
17414
17415
17416 /* EXPORT for RIF:
17417 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
17418 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
17419 assumed to be zero. */
17420
17421 void
17422 x_get_glyph_overhangs (glyph, f, left, right)
17423 struct glyph *glyph;
17424 struct frame *f;
17425 int *left, *right;
17426 {
17427 *left = *right = 0;
17428
17429 if (glyph->type == CHAR_GLYPH)
17430 {
17431 XFontStruct *font;
17432 struct face *face;
17433 struct font_info *font_info;
17434 XChar2b char2b;
17435 XCharStruct *pcm;
17436
17437 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
17438 font = face->font;
17439 font_info = FONT_INFO_FROM_ID (f, face->font_info_id);
17440 if (font /* ++KFS: Should this be font_info ? */
17441 && (pcm = FRAME_RIF (f)->per_char_metric (font, &char2b, glyph->font_type)))
17442 {
17443 if (pcm->rbearing > pcm->width)
17444 *right = pcm->rbearing - pcm->width;
17445 if (pcm->lbearing < 0)
17446 *left = -pcm->lbearing;
17447 }
17448 }
17449 }
17450
17451
17452 /* Return the index of the first glyph preceding glyph string S that
17453 is overwritten by S because of S's left overhang. Value is -1
17454 if no glyphs are overwritten. */
17455
17456 static int
17457 left_overwritten (s)
17458 struct glyph_string *s;
17459 {
17460 int k;
17461
17462 if (s->left_overhang)
17463 {
17464 int x = 0, i;
17465 struct glyph *glyphs = s->row->glyphs[s->area];
17466 int first = s->first_glyph - glyphs;
17467
17468 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
17469 x -= glyphs[i].pixel_width;
17470
17471 k = i + 1;
17472 }
17473 else
17474 k = -1;
17475
17476 return k;
17477 }
17478
17479
17480 /* Return the index of the first glyph preceding glyph string S that
17481 is overwriting S because of its right overhang. Value is -1 if no
17482 glyph in front of S overwrites S. */
17483
17484 static int
17485 left_overwriting (s)
17486 struct glyph_string *s;
17487 {
17488 int i, k, x;
17489 struct glyph *glyphs = s->row->glyphs[s->area];
17490 int first = s->first_glyph - glyphs;
17491
17492 k = -1;
17493 x = 0;
17494 for (i = first - 1; i >= 0; --i)
17495 {
17496 int left, right;
17497 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
17498 if (x + right > 0)
17499 k = i;
17500 x -= glyphs[i].pixel_width;
17501 }
17502
17503 return k;
17504 }
17505
17506
17507 /* Return the index of the last glyph following glyph string S that is
17508 not overwritten by S because of S's right overhang. Value is -1 if
17509 no such glyph is found. */
17510
17511 static int
17512 right_overwritten (s)
17513 struct glyph_string *s;
17514 {
17515 int k = -1;
17516
17517 if (s->right_overhang)
17518 {
17519 int x = 0, i;
17520 struct glyph *glyphs = s->row->glyphs[s->area];
17521 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
17522 int end = s->row->used[s->area];
17523
17524 for (i = first; i < end && s->right_overhang > x; ++i)
17525 x += glyphs[i].pixel_width;
17526
17527 k = i;
17528 }
17529
17530 return k;
17531 }
17532
17533
17534 /* Return the index of the last glyph following glyph string S that
17535 overwrites S because of its left overhang. Value is negative
17536 if no such glyph is found. */
17537
17538 static int
17539 right_overwriting (s)
17540 struct glyph_string *s;
17541 {
17542 int i, k, x;
17543 int end = s->row->used[s->area];
17544 struct glyph *glyphs = s->row->glyphs[s->area];
17545 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
17546
17547 k = -1;
17548 x = 0;
17549 for (i = first; i < end; ++i)
17550 {
17551 int left, right;
17552 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
17553 if (x - left < 0)
17554 k = i;
17555 x += glyphs[i].pixel_width;
17556 }
17557
17558 return k;
17559 }
17560
17561
17562 /* Get face and two-byte form of character C in face FACE_ID on frame
17563 F. The encoding of C is returned in *CHAR2B. MULTIBYTE_P non-zero
17564 means we want to display multibyte text. DISPLAY_P non-zero means
17565 make sure that X resources for the face returned are allocated.
17566 Value is a pointer to a realized face that is ready for display if
17567 DISPLAY_P is non-zero. */
17568
17569 static INLINE struct face *
17570 get_char_face_and_encoding (f, c, face_id, char2b, multibyte_p, display_p)
17571 struct frame *f;
17572 int c, face_id;
17573 XChar2b *char2b;
17574 int multibyte_p, display_p;
17575 {
17576 struct face *face = FACE_FROM_ID (f, face_id);
17577
17578 if (!multibyte_p)
17579 {
17580 /* Unibyte case. We don't have to encode, but we have to make
17581 sure to use a face suitable for unibyte. */
17582 STORE_XCHAR2B (char2b, 0, c);
17583 face_id = FACE_FOR_CHAR (f, face, c);
17584 face = FACE_FROM_ID (f, face_id);
17585 }
17586 else if (c < 128 && face_id < BASIC_FACE_ID_SENTINEL)
17587 {
17588 /* Case of ASCII in a face known to fit ASCII. */
17589 STORE_XCHAR2B (char2b, 0, c);
17590 }
17591 else
17592 {
17593 int c1, c2, charset;
17594
17595 /* Split characters into bytes. If c2 is -1 afterwards, C is
17596 really a one-byte character so that byte1 is zero. */
17597 SPLIT_CHAR (c, charset, c1, c2);
17598 if (c2 > 0)
17599 STORE_XCHAR2B (char2b, c1, c2);
17600 else
17601 STORE_XCHAR2B (char2b, 0, c1);
17602
17603 /* Maybe encode the character in *CHAR2B. */
17604 if (face->font != NULL)
17605 {
17606 struct font_info *font_info
17607 = FONT_INFO_FROM_ID (f, face->font_info_id);
17608 if (font_info)
17609 FRAME_RIF (f)->encode_char (c, char2b, font_info, 0);
17610 }
17611 }
17612
17613 /* Make sure X resources of the face are allocated. */
17614 #ifdef HAVE_X_WINDOWS
17615 if (display_p)
17616 #endif
17617 {
17618 xassert (face != NULL);
17619 PREPARE_FACE_FOR_DISPLAY (f, face);
17620 }
17621
17622 return face;
17623 }
17624
17625
17626 /* Set background width of glyph string S. START is the index of the
17627 first glyph following S. LAST_X is the right-most x-position + 1
17628 in the drawing area. */
17629
17630 static INLINE void
17631 set_glyph_string_background_width (s, start, last_x)
17632 struct glyph_string *s;
17633 int start;
17634 int last_x;
17635 {
17636 /* If the face of this glyph string has to be drawn to the end of
17637 the drawing area, set S->extends_to_end_of_line_p. */
17638 struct face *default_face = FACE_FROM_ID (s->f, DEFAULT_FACE_ID);
17639
17640 if (start == s->row->used[s->area]
17641 && s->area == TEXT_AREA
17642 && ((s->hl == DRAW_NORMAL_TEXT
17643 && (s->row->fill_line_p
17644 || s->face->background != default_face->background
17645 || s->face->stipple != default_face->stipple
17646 || s->row->mouse_face_p))
17647 || s->hl == DRAW_MOUSE_FACE
17648 || ((s->hl == DRAW_IMAGE_RAISED || s->hl == DRAW_IMAGE_SUNKEN)
17649 && s->row->fill_line_p)))
17650 s->extends_to_end_of_line_p = 1;
17651
17652 /* If S extends its face to the end of the line, set its
17653 background_width to the distance to the right edge of the drawing
17654 area. */
17655 if (s->extends_to_end_of_line_p)
17656 s->background_width = last_x - s->x + 1;
17657 else
17658 s->background_width = s->width;
17659 }
17660
17661
17662 /* Compute overhangs and x-positions for glyph string S and its
17663 predecessors, or successors. X is the starting x-position for S.
17664 BACKWARD_P non-zero means process predecessors. */
17665
17666 static void
17667 compute_overhangs_and_x (s, x, backward_p)
17668 struct glyph_string *s;
17669 int x;
17670 int backward_p;
17671 {
17672 if (backward_p)
17673 {
17674 while (s)
17675 {
17676 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
17677 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
17678 x -= s->width;
17679 s->x = x;
17680 s = s->prev;
17681 }
17682 }
17683 else
17684 {
17685 while (s)
17686 {
17687 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
17688 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
17689 s->x = x;
17690 x += s->width;
17691 s = s->next;
17692 }
17693 }
17694 }
17695
17696
17697
17698 /* The following macros are only called from draw_glyphs below.
17699 They reference the following parameters of that function directly:
17700 `w', `row', `area', and `overlap_p'
17701 as well as the following local variables:
17702 `s', `f', and `hdc' (in W32) */
17703
17704 #ifdef HAVE_NTGUI
17705 /* On W32, silently add local `hdc' variable to argument list of
17706 init_glyph_string. */
17707 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
17708 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
17709 #else
17710 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
17711 init_glyph_string (s, char2b, w, row, area, start, hl)
17712 #endif
17713
17714 /* Add a glyph string for a stretch glyph to the list of strings
17715 between HEAD and TAIL. START is the index of the stretch glyph in
17716 row area AREA of glyph row ROW. END is the index of the last glyph
17717 in that glyph row area. X is the current output position assigned
17718 to the new glyph string constructed. HL overrides that face of the
17719 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
17720 is the right-most x-position of the drawing area. */
17721
17722 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
17723 and below -- keep them on one line. */
17724 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
17725 do \
17726 { \
17727 s = (struct glyph_string *) alloca (sizeof *s); \
17728 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
17729 START = fill_stretch_glyph_string (s, row, area, START, END); \
17730 append_glyph_string (&HEAD, &TAIL, s); \
17731 s->x = (X); \
17732 } \
17733 while (0)
17734
17735
17736 /* Add a glyph string for an image glyph to the list of strings
17737 between HEAD and TAIL. START is the index of the image glyph in
17738 row area AREA of glyph row ROW. END is the index of the last glyph
17739 in that glyph row area. X is the current output position assigned
17740 to the new glyph string constructed. HL overrides that face of the
17741 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
17742 is the right-most x-position of the drawing area. */
17743
17744 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
17745 do \
17746 { \
17747 s = (struct glyph_string *) alloca (sizeof *s); \
17748 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
17749 fill_image_glyph_string (s); \
17750 append_glyph_string (&HEAD, &TAIL, s); \
17751 ++START; \
17752 s->x = (X); \
17753 } \
17754 while (0)
17755
17756
17757 /* Add a glyph string for a sequence of character glyphs to the list
17758 of strings between HEAD and TAIL. START is the index of the first
17759 glyph in row area AREA of glyph row ROW that is part of the new
17760 glyph string. END is the index of the last glyph in that glyph row
17761 area. X is the current output position assigned to the new glyph
17762 string constructed. HL overrides that face of the glyph; e.g. it
17763 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
17764 right-most x-position of the drawing area. */
17765
17766 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
17767 do \
17768 { \
17769 int c, face_id; \
17770 XChar2b *char2b; \
17771 \
17772 c = (row)->glyphs[area][START].u.ch; \
17773 face_id = (row)->glyphs[area][START].face_id; \
17774 \
17775 s = (struct glyph_string *) alloca (sizeof *s); \
17776 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
17777 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
17778 append_glyph_string (&HEAD, &TAIL, s); \
17779 s->x = (X); \
17780 START = fill_glyph_string (s, face_id, START, END, overlaps_p); \
17781 } \
17782 while (0)
17783
17784
17785 /* Add a glyph string for a composite sequence to the list of strings
17786 between HEAD and TAIL. START is the index of the first glyph in
17787 row area AREA of glyph row ROW that is part of the new glyph
17788 string. END is the index of the last glyph in that glyph row area.
17789 X is the current output position assigned to the new glyph string
17790 constructed. HL overrides that face of the glyph; e.g. it is
17791 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
17792 x-position of the drawing area. */
17793
17794 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
17795 do { \
17796 int cmp_id = (row)->glyphs[area][START].u.cmp_id; \
17797 int face_id = (row)->glyphs[area][START].face_id; \
17798 struct face *base_face = FACE_FROM_ID (f, face_id); \
17799 struct composition *cmp = composition_table[cmp_id]; \
17800 int glyph_len = cmp->glyph_len; \
17801 XChar2b *char2b; \
17802 struct face **faces; \
17803 struct glyph_string *first_s = NULL; \
17804 int n; \
17805 \
17806 base_face = base_face->ascii_face; \
17807 char2b = (XChar2b *) alloca ((sizeof *char2b) * glyph_len); \
17808 faces = (struct face **) alloca ((sizeof *faces) * glyph_len); \
17809 /* At first, fill in `char2b' and `faces'. */ \
17810 for (n = 0; n < glyph_len; n++) \
17811 { \
17812 int c = COMPOSITION_GLYPH (cmp, n); \
17813 int this_face_id = FACE_FOR_CHAR (f, base_face, c); \
17814 faces[n] = FACE_FROM_ID (f, this_face_id); \
17815 get_char_face_and_encoding (f, c, this_face_id, \
17816 char2b + n, 1, 1); \
17817 } \
17818 \
17819 /* Make glyph_strings for each glyph sequence that is drawable by \
17820 the same face, and append them to HEAD/TAIL. */ \
17821 for (n = 0; n < cmp->glyph_len;) \
17822 { \
17823 s = (struct glyph_string *) alloca (sizeof *s); \
17824 INIT_GLYPH_STRING (s, char2b + n, w, row, area, START, HL); \
17825 append_glyph_string (&(HEAD), &(TAIL), s); \
17826 s->cmp = cmp; \
17827 s->gidx = n; \
17828 s->x = (X); \
17829 \
17830 if (n == 0) \
17831 first_s = s; \
17832 \
17833 n = fill_composite_glyph_string (s, faces, overlaps_p); \
17834 } \
17835 \
17836 ++START; \
17837 s = first_s; \
17838 } while (0)
17839
17840
17841 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
17842 of AREA of glyph row ROW on window W between indices START and END.
17843 HL overrides the face for drawing glyph strings, e.g. it is
17844 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
17845 x-positions of the drawing area.
17846
17847 This is an ugly monster macro construct because we must use alloca
17848 to allocate glyph strings (because draw_glyphs can be called
17849 asynchronously). */
17850
17851 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
17852 do \
17853 { \
17854 HEAD = TAIL = NULL; \
17855 while (START < END) \
17856 { \
17857 struct glyph *first_glyph = (row)->glyphs[area] + START; \
17858 switch (first_glyph->type) \
17859 { \
17860 case CHAR_GLYPH: \
17861 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
17862 HL, X, LAST_X); \
17863 break; \
17864 \
17865 case COMPOSITE_GLYPH: \
17866 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
17867 HL, X, LAST_X); \
17868 break; \
17869 \
17870 case STRETCH_GLYPH: \
17871 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
17872 HL, X, LAST_X); \
17873 break; \
17874 \
17875 case IMAGE_GLYPH: \
17876 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
17877 HL, X, LAST_X); \
17878 break; \
17879 \
17880 default: \
17881 abort (); \
17882 } \
17883 \
17884 set_glyph_string_background_width (s, START, LAST_X); \
17885 (X) += s->width; \
17886 } \
17887 } \
17888 while (0)
17889
17890
17891 /* Draw glyphs between START and END in AREA of ROW on window W,
17892 starting at x-position X. X is relative to AREA in W. HL is a
17893 face-override with the following meaning:
17894
17895 DRAW_NORMAL_TEXT draw normally
17896 DRAW_CURSOR draw in cursor face
17897 DRAW_MOUSE_FACE draw in mouse face.
17898 DRAW_INVERSE_VIDEO draw in mode line face
17899 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
17900 DRAW_IMAGE_RAISED draw an image with a raised relief around it
17901
17902 If OVERLAPS_P is non-zero, draw only the foreground of characters
17903 and clip to the physical height of ROW.
17904
17905 Value is the x-position reached, relative to AREA of W. */
17906
17907 static int
17908 draw_glyphs (w, x, row, area, start, end, hl, overlaps_p)
17909 struct window *w;
17910 int x;
17911 struct glyph_row *row;
17912 enum glyph_row_area area;
17913 int start, end;
17914 enum draw_glyphs_face hl;
17915 int overlaps_p;
17916 {
17917 struct glyph_string *head, *tail;
17918 struct glyph_string *s;
17919 int last_x, area_width;
17920 int x_reached;
17921 int i, j;
17922 struct frame *f = XFRAME (WINDOW_FRAME (w));
17923 DECLARE_HDC (hdc);
17924
17925 ALLOCATE_HDC (hdc, f);
17926
17927 /* Let's rather be paranoid than getting a SEGV. */
17928 end = min (end, row->used[area]);
17929 start = max (0, start);
17930 start = min (end, start);
17931
17932 /* Translate X to frame coordinates. Set last_x to the right
17933 end of the drawing area. */
17934 if (row->full_width_p)
17935 {
17936 /* X is relative to the left edge of W, without scroll bars
17937 or fringes. */
17938 x += WINDOW_LEFT_EDGE_X (w);
17939 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
17940 }
17941 else
17942 {
17943 int area_left = window_box_left (w, area);
17944 x += area_left;
17945 area_width = window_box_width (w, area);
17946 last_x = area_left + area_width;
17947 }
17948
17949 /* Build a doubly-linked list of glyph_string structures between
17950 head and tail from what we have to draw. Note that the macro
17951 BUILD_GLYPH_STRINGS will modify its start parameter. That's
17952 the reason we use a separate variable `i'. */
17953 i = start;
17954 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
17955 if (tail)
17956 x_reached = tail->x + tail->background_width;
17957 else
17958 x_reached = x;
17959
17960 /* If there are any glyphs with lbearing < 0 or rbearing > width in
17961 the row, redraw some glyphs in front or following the glyph
17962 strings built above. */
17963 if (head && !overlaps_p && row->contains_overlapping_glyphs_p)
17964 {
17965 int dummy_x = 0;
17966 struct glyph_string *h, *t;
17967
17968 /* Compute overhangs for all glyph strings. */
17969 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
17970 for (s = head; s; s = s->next)
17971 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
17972
17973 /* Prepend glyph strings for glyphs in front of the first glyph
17974 string that are overwritten because of the first glyph
17975 string's left overhang. The background of all strings
17976 prepended must be drawn because the first glyph string
17977 draws over it. */
17978 i = left_overwritten (head);
17979 if (i >= 0)
17980 {
17981 j = i;
17982 BUILD_GLYPH_STRINGS (j, start, h, t,
17983 DRAW_NORMAL_TEXT, dummy_x, last_x);
17984 start = i;
17985 compute_overhangs_and_x (t, head->x, 1);
17986 prepend_glyph_string_lists (&head, &tail, h, t);
17987 }
17988
17989 /* Prepend glyph strings for glyphs in front of the first glyph
17990 string that overwrite that glyph string because of their
17991 right overhang. For these strings, only the foreground must
17992 be drawn, because it draws over the glyph string at `head'.
17993 The background must not be drawn because this would overwrite
17994 right overhangs of preceding glyphs for which no glyph
17995 strings exist. */
17996 i = left_overwriting (head);
17997 if (i >= 0)
17998 {
17999 BUILD_GLYPH_STRINGS (i, start, h, t,
18000 DRAW_NORMAL_TEXT, dummy_x, last_x);
18001 for (s = h; s; s = s->next)
18002 s->background_filled_p = 1;
18003 compute_overhangs_and_x (t, head->x, 1);
18004 prepend_glyph_string_lists (&head, &tail, h, t);
18005 }
18006
18007 /* Append glyphs strings for glyphs following the last glyph
18008 string tail that are overwritten by tail. The background of
18009 these strings has to be drawn because tail's foreground draws
18010 over it. */
18011 i = right_overwritten (tail);
18012 if (i >= 0)
18013 {
18014 BUILD_GLYPH_STRINGS (end, i, h, t,
18015 DRAW_NORMAL_TEXT, x, last_x);
18016 compute_overhangs_and_x (h, tail->x + tail->width, 0);
18017 append_glyph_string_lists (&head, &tail, h, t);
18018 }
18019
18020 /* Append glyph strings for glyphs following the last glyph
18021 string tail that overwrite tail. The foreground of such
18022 glyphs has to be drawn because it writes into the background
18023 of tail. The background must not be drawn because it could
18024 paint over the foreground of following glyphs. */
18025 i = right_overwriting (tail);
18026 if (i >= 0)
18027 {
18028 BUILD_GLYPH_STRINGS (end, i, h, t,
18029 DRAW_NORMAL_TEXT, x, last_x);
18030 for (s = h; s; s = s->next)
18031 s->background_filled_p = 1;
18032 compute_overhangs_and_x (h, tail->x + tail->width, 0);
18033 append_glyph_string_lists (&head, &tail, h, t);
18034 }
18035 }
18036
18037 /* Draw all strings. */
18038 for (s = head; s; s = s->next)
18039 FRAME_RIF (f)->draw_glyph_string (s);
18040
18041 if (area == TEXT_AREA
18042 && !row->full_width_p
18043 /* When drawing overlapping rows, only the glyph strings'
18044 foreground is drawn, which doesn't erase a cursor
18045 completely. */
18046 && !overlaps_p)
18047 {
18048 int x0 = head ? head->x : x;
18049 int x1 = tail ? tail->x + tail->background_width : x;
18050
18051 int text_left = window_box_left (w, TEXT_AREA);
18052 x0 -= text_left;
18053 x1 -= text_left;
18054
18055 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
18056 row->y, MATRIX_ROW_BOTTOM_Y (row));
18057 }
18058
18059 /* Value is the x-position up to which drawn, relative to AREA of W.
18060 This doesn't include parts drawn because of overhangs. */
18061 if (row->full_width_p)
18062 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
18063 else
18064 x_reached -= window_box_left (w, area);
18065
18066 RELEASE_HDC (hdc, f);
18067
18068 return x_reached;
18069 }
18070
18071
18072 /* Store one glyph for IT->char_to_display in IT->glyph_row.
18073 Called from x_produce_glyphs when IT->glyph_row is non-null. */
18074
18075 static INLINE void
18076 append_glyph (it)
18077 struct it *it;
18078 {
18079 struct glyph *glyph;
18080 enum glyph_row_area area = it->area;
18081
18082 xassert (it->glyph_row);
18083 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
18084
18085 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
18086 if (glyph < it->glyph_row->glyphs[area + 1])
18087 {
18088 glyph->charpos = CHARPOS (it->position);
18089 glyph->object = it->object;
18090 glyph->pixel_width = it->pixel_width;
18091 glyph->ascent = it->ascent;
18092 glyph->descent = it->descent;
18093 glyph->voffset = it->voffset;
18094 glyph->type = CHAR_GLYPH;
18095 glyph->multibyte_p = it->multibyte_p;
18096 glyph->left_box_line_p = it->start_of_box_run_p;
18097 glyph->right_box_line_p = it->end_of_box_run_p;
18098 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
18099 || it->phys_descent > it->descent);
18100 glyph->padding_p = 0;
18101 glyph->glyph_not_available_p = it->glyph_not_available_p;
18102 glyph->face_id = it->face_id;
18103 glyph->u.ch = it->char_to_display;
18104 glyph->slice = null_glyph_slice;
18105 glyph->font_type = FONT_TYPE_UNKNOWN;
18106 ++it->glyph_row->used[area];
18107 }
18108 }
18109
18110 /* Store one glyph for the composition IT->cmp_id in IT->glyph_row.
18111 Called from x_produce_glyphs when IT->glyph_row is non-null. */
18112
18113 static INLINE void
18114 append_composite_glyph (it)
18115 struct it *it;
18116 {
18117 struct glyph *glyph;
18118 enum glyph_row_area area = it->area;
18119
18120 xassert (it->glyph_row);
18121
18122 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
18123 if (glyph < it->glyph_row->glyphs[area + 1])
18124 {
18125 glyph->charpos = CHARPOS (it->position);
18126 glyph->object = it->object;
18127 glyph->pixel_width = it->pixel_width;
18128 glyph->ascent = it->ascent;
18129 glyph->descent = it->descent;
18130 glyph->voffset = it->voffset;
18131 glyph->type = COMPOSITE_GLYPH;
18132 glyph->multibyte_p = it->multibyte_p;
18133 glyph->left_box_line_p = it->start_of_box_run_p;
18134 glyph->right_box_line_p = it->end_of_box_run_p;
18135 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
18136 || it->phys_descent > it->descent);
18137 glyph->padding_p = 0;
18138 glyph->glyph_not_available_p = 0;
18139 glyph->face_id = it->face_id;
18140 glyph->u.cmp_id = it->cmp_id;
18141 glyph->slice = null_glyph_slice;
18142 glyph->font_type = FONT_TYPE_UNKNOWN;
18143 ++it->glyph_row->used[area];
18144 }
18145 }
18146
18147
18148 /* Change IT->ascent and IT->height according to the setting of
18149 IT->voffset. */
18150
18151 static INLINE void
18152 take_vertical_position_into_account (it)
18153 struct it *it;
18154 {
18155 if (it->voffset)
18156 {
18157 if (it->voffset < 0)
18158 /* Increase the ascent so that we can display the text higher
18159 in the line. */
18160 it->ascent -= it->voffset;
18161 else
18162 /* Increase the descent so that we can display the text lower
18163 in the line. */
18164 it->descent += it->voffset;
18165 }
18166 }
18167
18168
18169 /* Produce glyphs/get display metrics for the image IT is loaded with.
18170 See the description of struct display_iterator in dispextern.h for
18171 an overview of struct display_iterator. */
18172
18173 static void
18174 produce_image_glyph (it)
18175 struct it *it;
18176 {
18177 struct image *img;
18178 struct face *face;
18179 int face_ascent, glyph_ascent;
18180 struct glyph_slice slice;
18181
18182 xassert (it->what == IT_IMAGE);
18183
18184 face = FACE_FROM_ID (it->f, it->face_id);
18185 xassert (face);
18186 /* Make sure X resources of the face is loaded. */
18187 PREPARE_FACE_FOR_DISPLAY (it->f, face);
18188
18189 if (it->image_id < 0)
18190 {
18191 /* Fringe bitmap. */
18192 it->ascent = it->phys_ascent = 0;
18193 it->descent = it->phys_descent = 0;
18194 it->pixel_width = 0;
18195 it->nglyphs = 0;
18196 return;
18197 }
18198
18199 img = IMAGE_FROM_ID (it->f, it->image_id);
18200 xassert (img);
18201 /* Make sure X resources of the image is loaded. */
18202 prepare_image_for_display (it->f, img);
18203
18204 slice.x = slice.y = 0;
18205 slice.width = img->width;
18206 slice.height = img->height;
18207
18208 if (INTEGERP (it->slice.x))
18209 slice.x = XINT (it->slice.x);
18210 else if (FLOATP (it->slice.x))
18211 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
18212
18213 if (INTEGERP (it->slice.y))
18214 slice.y = XINT (it->slice.y);
18215 else if (FLOATP (it->slice.y))
18216 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
18217
18218 if (INTEGERP (it->slice.width))
18219 slice.width = XINT (it->slice.width);
18220 else if (FLOATP (it->slice.width))
18221 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
18222
18223 if (INTEGERP (it->slice.height))
18224 slice.height = XINT (it->slice.height);
18225 else if (FLOATP (it->slice.height))
18226 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
18227
18228 if (slice.x >= img->width)
18229 slice.x = img->width;
18230 if (slice.y >= img->height)
18231 slice.y = img->height;
18232 if (slice.x + slice.width >= img->width)
18233 slice.width = img->width - slice.x;
18234 if (slice.y + slice.height > img->height)
18235 slice.height = img->height - slice.y;
18236
18237 if (slice.width == 0 || slice.height == 0)
18238 return;
18239
18240 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
18241
18242 it->descent = slice.height - glyph_ascent;
18243 if (slice.y == 0)
18244 it->descent += img->vmargin;
18245 if (slice.y + slice.height == img->height)
18246 it->descent += img->vmargin;
18247 it->phys_descent = it->descent;
18248
18249 it->pixel_width = slice.width;
18250 if (slice.x == 0)
18251 it->pixel_width += img->hmargin;
18252 if (slice.x + slice.width == img->width)
18253 it->pixel_width += img->hmargin;
18254
18255 /* It's quite possible for images to have an ascent greater than
18256 their height, so don't get confused in that case. */
18257 if (it->descent < 0)
18258 it->descent = 0;
18259
18260 #if 0 /* this breaks image tiling */
18261 /* If this glyph is alone on the last line, adjust it.ascent to minimum row ascent. */
18262 face_ascent = face->font ? FONT_BASE (face->font) : FRAME_BASELINE_OFFSET (it->f);
18263 if (face_ascent > it->ascent)
18264 it->ascent = it->phys_ascent = face_ascent;
18265 #endif
18266
18267 it->nglyphs = 1;
18268
18269 if (face->box != FACE_NO_BOX)
18270 {
18271 if (face->box_line_width > 0)
18272 {
18273 if (slice.y == 0)
18274 it->ascent += face->box_line_width;
18275 if (slice.y + slice.height == img->height)
18276 it->descent += face->box_line_width;
18277 }
18278
18279 if (it->start_of_box_run_p && slice.x == 0)
18280 it->pixel_width += abs (face->box_line_width);
18281 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
18282 it->pixel_width += abs (face->box_line_width);
18283 }
18284
18285 take_vertical_position_into_account (it);
18286
18287 if (it->glyph_row)
18288 {
18289 struct glyph *glyph;
18290 enum glyph_row_area area = it->area;
18291
18292 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
18293 if (glyph < it->glyph_row->glyphs[area + 1])
18294 {
18295 glyph->charpos = CHARPOS (it->position);
18296 glyph->object = it->object;
18297 glyph->pixel_width = it->pixel_width;
18298 glyph->ascent = glyph_ascent;
18299 glyph->descent = it->descent;
18300 glyph->voffset = it->voffset;
18301 glyph->type = IMAGE_GLYPH;
18302 glyph->multibyte_p = it->multibyte_p;
18303 glyph->left_box_line_p = it->start_of_box_run_p;
18304 glyph->right_box_line_p = it->end_of_box_run_p;
18305 glyph->overlaps_vertically_p = 0;
18306 glyph->padding_p = 0;
18307 glyph->glyph_not_available_p = 0;
18308 glyph->face_id = it->face_id;
18309 glyph->u.img_id = img->id;
18310 glyph->slice = slice;
18311 glyph->font_type = FONT_TYPE_UNKNOWN;
18312 ++it->glyph_row->used[area];
18313 }
18314 }
18315 }
18316
18317
18318 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
18319 of the glyph, WIDTH and HEIGHT are the width and height of the
18320 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
18321
18322 static void
18323 append_stretch_glyph (it, object, width, height, ascent)
18324 struct it *it;
18325 Lisp_Object object;
18326 int width, height;
18327 int ascent;
18328 {
18329 struct glyph *glyph;
18330 enum glyph_row_area area = it->area;
18331
18332 xassert (ascent >= 0 && ascent <= height);
18333
18334 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
18335 if (glyph < it->glyph_row->glyphs[area + 1])
18336 {
18337 glyph->charpos = CHARPOS (it->position);
18338 glyph->object = object;
18339 glyph->pixel_width = width;
18340 glyph->ascent = ascent;
18341 glyph->descent = height - ascent;
18342 glyph->voffset = it->voffset;
18343 glyph->type = STRETCH_GLYPH;
18344 glyph->multibyte_p = it->multibyte_p;
18345 glyph->left_box_line_p = it->start_of_box_run_p;
18346 glyph->right_box_line_p = it->end_of_box_run_p;
18347 glyph->overlaps_vertically_p = 0;
18348 glyph->padding_p = 0;
18349 glyph->glyph_not_available_p = 0;
18350 glyph->face_id = it->face_id;
18351 glyph->u.stretch.ascent = ascent;
18352 glyph->u.stretch.height = height;
18353 glyph->slice = null_glyph_slice;
18354 glyph->font_type = FONT_TYPE_UNKNOWN;
18355 ++it->glyph_row->used[area];
18356 }
18357 }
18358
18359
18360 /* Produce a stretch glyph for iterator IT. IT->object is the value
18361 of the glyph property displayed. The value must be a list
18362 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
18363 being recognized:
18364
18365 1. `:width WIDTH' specifies that the space should be WIDTH *
18366 canonical char width wide. WIDTH may be an integer or floating
18367 point number.
18368
18369 2. `:relative-width FACTOR' specifies that the width of the stretch
18370 should be computed from the width of the first character having the
18371 `glyph' property, and should be FACTOR times that width.
18372
18373 3. `:align-to HPOS' specifies that the space should be wide enough
18374 to reach HPOS, a value in canonical character units.
18375
18376 Exactly one of the above pairs must be present.
18377
18378 4. `:height HEIGHT' specifies that the height of the stretch produced
18379 should be HEIGHT, measured in canonical character units.
18380
18381 5. `:relative-height FACTOR' specifies that the height of the
18382 stretch should be FACTOR times the height of the characters having
18383 the glyph property.
18384
18385 Either none or exactly one of 4 or 5 must be present.
18386
18387 6. `:ascent ASCENT' specifies that ASCENT percent of the height
18388 of the stretch should be used for the ascent of the stretch.
18389 ASCENT must be in the range 0 <= ASCENT <= 100. */
18390
18391 static void
18392 produce_stretch_glyph (it)
18393 struct it *it;
18394 {
18395 /* (space :width WIDTH :height HEIGHT ...) */
18396 Lisp_Object prop, plist;
18397 int width = 0, height = 0, align_to = -1;
18398 int zero_width_ok_p = 0, zero_height_ok_p = 0;
18399 int ascent = 0;
18400 double tem;
18401 struct face *face = FACE_FROM_ID (it->f, it->face_id);
18402 XFontStruct *font = face->font ? face->font : FRAME_FONT (it->f);
18403
18404 PREPARE_FACE_FOR_DISPLAY (it->f, face);
18405
18406 /* List should start with `space'. */
18407 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
18408 plist = XCDR (it->object);
18409
18410 /* Compute the width of the stretch. */
18411 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
18412 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
18413 {
18414 /* Absolute width `:width WIDTH' specified and valid. */
18415 zero_width_ok_p = 1;
18416 width = (int)tem;
18417 }
18418 else if (prop = Fplist_get (plist, QCrelative_width),
18419 NUMVAL (prop) > 0)
18420 {
18421 /* Relative width `:relative-width FACTOR' specified and valid.
18422 Compute the width of the characters having the `glyph'
18423 property. */
18424 struct it it2;
18425 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
18426
18427 it2 = *it;
18428 if (it->multibyte_p)
18429 {
18430 int maxlen = ((IT_BYTEPOS (*it) >= GPT ? ZV : GPT)
18431 - IT_BYTEPOS (*it));
18432 it2.c = STRING_CHAR_AND_LENGTH (p, maxlen, it2.len);
18433 }
18434 else
18435 it2.c = *p, it2.len = 1;
18436
18437 it2.glyph_row = NULL;
18438 it2.what = IT_CHARACTER;
18439 x_produce_glyphs (&it2);
18440 width = NUMVAL (prop) * it2.pixel_width;
18441 }
18442 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
18443 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
18444 {
18445 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
18446 align_to = (align_to < 0
18447 ? 0
18448 : align_to - window_box_left_offset (it->w, TEXT_AREA));
18449 else if (align_to < 0)
18450 align_to = window_box_left_offset (it->w, TEXT_AREA);
18451 width = max (0, (int)tem + align_to - it->current_x);
18452 zero_width_ok_p = 1;
18453 }
18454 else
18455 /* Nothing specified -> width defaults to canonical char width. */
18456 width = FRAME_COLUMN_WIDTH (it->f);
18457
18458 if (width <= 0 && (width < 0 || !zero_width_ok_p))
18459 width = 1;
18460
18461 /* Compute height. */
18462 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
18463 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
18464 {
18465 height = (int)tem;
18466 zero_height_ok_p = 1;
18467 }
18468 else if (prop = Fplist_get (plist, QCrelative_height),
18469 NUMVAL (prop) > 0)
18470 height = FONT_HEIGHT (font) * NUMVAL (prop);
18471 else
18472 height = FONT_HEIGHT (font);
18473
18474 if (height <= 0 && (height < 0 || !zero_height_ok_p))
18475 height = 1;
18476
18477 /* Compute percentage of height used for ascent. If
18478 `:ascent ASCENT' is present and valid, use that. Otherwise,
18479 derive the ascent from the font in use. */
18480 if (prop = Fplist_get (plist, QCascent),
18481 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
18482 ascent = height * NUMVAL (prop) / 100.0;
18483 else if (!NILP (prop)
18484 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
18485 ascent = min (max (0, (int)tem), height);
18486 else
18487 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
18488
18489 if (width > 0 && height > 0 && it->glyph_row)
18490 {
18491 Lisp_Object object = it->stack[it->sp - 1].string;
18492 if (!STRINGP (object))
18493 object = it->w->buffer;
18494 append_stretch_glyph (it, object, width, height, ascent);
18495 }
18496
18497 it->pixel_width = width;
18498 it->ascent = it->phys_ascent = ascent;
18499 it->descent = it->phys_descent = height - it->ascent;
18500 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
18501
18502 if (width > 0 && height > 0 && face->box != FACE_NO_BOX)
18503 {
18504 if (face->box_line_width > 0)
18505 {
18506 it->ascent += face->box_line_width;
18507 it->descent += face->box_line_width;
18508 }
18509
18510 if (it->start_of_box_run_p)
18511 it->pixel_width += abs (face->box_line_width);
18512 if (it->end_of_box_run_p)
18513 it->pixel_width += abs (face->box_line_width);
18514 }
18515
18516 take_vertical_position_into_account (it);
18517 }
18518
18519 /* Calculate line-height and line-spacing properties.
18520 An integer value specifies explicit pixel value.
18521 A float value specifies relative value to current face height.
18522 A cons (float . face-name) specifies relative value to
18523 height of specified face font.
18524
18525 Returns height in pixels, or nil. */
18526
18527 static Lisp_Object
18528 calc_line_height_property (it, prop, font, boff, total)
18529 struct it *it;
18530 Lisp_Object prop;
18531 XFontStruct *font;
18532 int boff, *total;
18533 {
18534 Lisp_Object position, val;
18535 Lisp_Object face_name = Qnil;
18536 int ascent, descent, height, override;
18537
18538 if (STRINGP (it->object))
18539 position = make_number (IT_STRING_CHARPOS (*it));
18540 else
18541 position = make_number (IT_CHARPOS (*it));
18542
18543 val = Fget_char_property (position, prop, it->object);
18544
18545 if (NILP (val))
18546 return val;
18547
18548 if (total && CONSP (val) && EQ (XCAR (val), Qtotal))
18549 {
18550 *total = 1;
18551 val = XCDR (val);
18552 }
18553
18554 if (INTEGERP (val))
18555 return val;
18556
18557 if (CONSP (val))
18558 {
18559 face_name = XCDR (val);
18560 val = XCAR (val);
18561 }
18562 else if (SYMBOLP (val))
18563 {
18564 face_name = val;
18565 val = Qnil;
18566 }
18567
18568 override = EQ (prop, Qline_height);
18569
18570 if (NILP (face_name))
18571 {
18572 font = FRAME_FONT (it->f);
18573 boff = FRAME_BASELINE_OFFSET (it->f);
18574 }
18575 else if (EQ (face_name, Qt))
18576 {
18577 override = 0;
18578 }
18579 else
18580 {
18581 int face_id;
18582 struct face *face;
18583 struct font_info *font_info;
18584
18585 face_id = lookup_named_face (it->f, face_name, ' ');
18586 if (face_id < 0)
18587 return make_number (-1);
18588
18589 face = FACE_FROM_ID (it->f, face_id);
18590 font = face->font;
18591 if (font == NULL)
18592 return make_number (-1);
18593
18594 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
18595 boff = font_info->baseline_offset;
18596 if (font_info->vertical_centering)
18597 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
18598 }
18599
18600 ascent = FONT_BASE (font) + boff;
18601 descent = FONT_DESCENT (font) - boff;
18602
18603 if (override)
18604 {
18605 it->override_ascent = ascent;
18606 it->override_descent = descent;
18607 it->override_boff = boff;
18608 }
18609
18610 height = ascent + descent;
18611 if (FLOATP (val))
18612 height = (int)(XFLOAT_DATA (val) * height);
18613 else if (INTEGERP (val))
18614 height *= XINT (val);
18615
18616 return make_number (height);
18617 }
18618
18619
18620 /* RIF:
18621 Produce glyphs/get display metrics for the display element IT is
18622 loaded with. See the description of struct display_iterator in
18623 dispextern.h for an overview of struct display_iterator. */
18624
18625 void
18626 x_produce_glyphs (it)
18627 struct it *it;
18628 {
18629 int extra_line_spacing = it->extra_line_spacing;
18630
18631 it->glyph_not_available_p = 0;
18632
18633 if (it->what == IT_CHARACTER)
18634 {
18635 XChar2b char2b;
18636 XFontStruct *font;
18637 struct face *face = FACE_FROM_ID (it->f, it->face_id);
18638 XCharStruct *pcm;
18639 int font_not_found_p;
18640 struct font_info *font_info;
18641 int boff; /* baseline offset */
18642 /* We may change it->multibyte_p upon unibyte<->multibyte
18643 conversion. So, save the current value now and restore it
18644 later.
18645
18646 Note: It seems that we don't have to record multibyte_p in
18647 struct glyph because the character code itself tells if or
18648 not the character is multibyte. Thus, in the future, we must
18649 consider eliminating the field `multibyte_p' in the struct
18650 glyph. */
18651 int saved_multibyte_p = it->multibyte_p;
18652
18653 /* Maybe translate single-byte characters to multibyte, or the
18654 other way. */
18655 it->char_to_display = it->c;
18656 if (!ASCII_BYTE_P (it->c))
18657 {
18658 if (unibyte_display_via_language_environment
18659 && SINGLE_BYTE_CHAR_P (it->c)
18660 && (it->c >= 0240
18661 || !NILP (Vnonascii_translation_table)))
18662 {
18663 it->char_to_display = unibyte_char_to_multibyte (it->c);
18664 it->multibyte_p = 1;
18665 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
18666 face = FACE_FROM_ID (it->f, it->face_id);
18667 }
18668 else if (!SINGLE_BYTE_CHAR_P (it->c)
18669 && !it->multibyte_p)
18670 {
18671 it->multibyte_p = 1;
18672 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
18673 face = FACE_FROM_ID (it->f, it->face_id);
18674 }
18675 }
18676
18677 /* Get font to use. Encode IT->char_to_display. */
18678 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
18679 &char2b, it->multibyte_p, 0);
18680 font = face->font;
18681
18682 /* When no suitable font found, use the default font. */
18683 font_not_found_p = font == NULL;
18684 if (font_not_found_p)
18685 {
18686 font = FRAME_FONT (it->f);
18687 boff = FRAME_BASELINE_OFFSET (it->f);
18688 font_info = NULL;
18689 }
18690 else
18691 {
18692 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
18693 boff = font_info->baseline_offset;
18694 if (font_info->vertical_centering)
18695 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
18696 }
18697
18698 if (it->char_to_display >= ' '
18699 && (!it->multibyte_p || it->char_to_display < 128))
18700 {
18701 /* Either unibyte or ASCII. */
18702 int stretched_p;
18703
18704 it->nglyphs = 1;
18705
18706 pcm = FRAME_RIF (it->f)->per_char_metric
18707 (font, &char2b, FONT_TYPE_FOR_UNIBYTE (font, it->char_to_display));
18708
18709 if (it->override_ascent >= 0)
18710 {
18711 it->ascent = it->override_ascent;
18712 it->descent = it->override_descent;
18713 boff = it->override_boff;
18714 }
18715 else
18716 {
18717 it->ascent = FONT_BASE (font) + boff;
18718 it->descent = FONT_DESCENT (font) - boff;
18719 }
18720
18721 if (pcm)
18722 {
18723 it->phys_ascent = pcm->ascent + boff;
18724 it->phys_descent = pcm->descent - boff;
18725 it->pixel_width = pcm->width;
18726 }
18727 else
18728 {
18729 it->glyph_not_available_p = 1;
18730 it->phys_ascent = it->ascent;
18731 it->phys_descent = it->descent;
18732 it->pixel_width = FONT_WIDTH (font);
18733 }
18734
18735 if (it->constrain_row_ascent_descent_p)
18736 {
18737 if (it->descent > it->max_descent)
18738 {
18739 it->ascent += it->descent - it->max_descent;
18740 it->descent = it->max_descent;
18741 }
18742 if (it->ascent > it->max_ascent)
18743 {
18744 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
18745 it->ascent = it->max_ascent;
18746 }
18747 it->phys_ascent = min (it->phys_ascent, it->ascent);
18748 it->phys_descent = min (it->phys_descent, it->descent);
18749 extra_line_spacing = 0;
18750 }
18751
18752 /* If this is a space inside a region of text with
18753 `space-width' property, change its width. */
18754 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
18755 if (stretched_p)
18756 it->pixel_width *= XFLOATINT (it->space_width);
18757
18758 /* If face has a box, add the box thickness to the character
18759 height. If character has a box line to the left and/or
18760 right, add the box line width to the character's width. */
18761 if (face->box != FACE_NO_BOX)
18762 {
18763 int thick = face->box_line_width;
18764
18765 if (thick > 0)
18766 {
18767 it->ascent += thick;
18768 it->descent += thick;
18769 }
18770 else
18771 thick = -thick;
18772
18773 if (it->start_of_box_run_p)
18774 it->pixel_width += thick;
18775 if (it->end_of_box_run_p)
18776 it->pixel_width += thick;
18777 }
18778
18779 /* If face has an overline, add the height of the overline
18780 (1 pixel) and a 1 pixel margin to the character height. */
18781 if (face->overline_p)
18782 it->ascent += 2;
18783
18784 if (it->constrain_row_ascent_descent_p)
18785 {
18786 if (it->ascent > it->max_ascent)
18787 it->ascent = it->max_ascent;
18788 if (it->descent > it->max_descent)
18789 it->descent = it->max_descent;
18790 }
18791
18792 take_vertical_position_into_account (it);
18793
18794 /* If we have to actually produce glyphs, do it. */
18795 if (it->glyph_row)
18796 {
18797 if (stretched_p)
18798 {
18799 /* Translate a space with a `space-width' property
18800 into a stretch glyph. */
18801 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
18802 / FONT_HEIGHT (font));
18803 append_stretch_glyph (it, it->object, it->pixel_width,
18804 it->ascent + it->descent, ascent);
18805 }
18806 else
18807 append_glyph (it);
18808
18809 /* If characters with lbearing or rbearing are displayed
18810 in this line, record that fact in a flag of the
18811 glyph row. This is used to optimize X output code. */
18812 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
18813 it->glyph_row->contains_overlapping_glyphs_p = 1;
18814 }
18815 }
18816 else if (it->char_to_display == '\n')
18817 {
18818 /* A newline has no width but we need the height of the line.
18819 But if previous part of the line set a height, don't
18820 increase that height */
18821
18822 Lisp_Object height;
18823
18824 it->override_ascent = -1;
18825 it->pixel_width = 0;
18826 it->nglyphs = 0;
18827
18828 height = calc_line_height_property(it, Qline_height, font, boff, 0);
18829
18830 if (it->override_ascent >= 0)
18831 {
18832 it->ascent = it->override_ascent;
18833 it->descent = it->override_descent;
18834 boff = it->override_boff;
18835 }
18836 else
18837 {
18838 it->ascent = FONT_BASE (font) + boff;
18839 it->descent = FONT_DESCENT (font) - boff;
18840 }
18841
18842 if (EQ (height, make_number(0)))
18843 {
18844 if (it->descent > it->max_descent)
18845 {
18846 it->ascent += it->descent - it->max_descent;
18847 it->descent = it->max_descent;
18848 }
18849 if (it->ascent > it->max_ascent)
18850 {
18851 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
18852 it->ascent = it->max_ascent;
18853 }
18854 it->phys_ascent = min (it->phys_ascent, it->ascent);
18855 it->phys_descent = min (it->phys_descent, it->descent);
18856 it->constrain_row_ascent_descent_p = 1;
18857 extra_line_spacing = 0;
18858 }
18859 else
18860 {
18861 Lisp_Object spacing;
18862 int total = 0;
18863
18864 it->phys_ascent = it->ascent;
18865 it->phys_descent = it->descent;
18866
18867 if ((it->max_ascent > 0 || it->max_descent > 0)
18868 && face->box != FACE_NO_BOX
18869 && face->box_line_width > 0)
18870 {
18871 it->ascent += face->box_line_width;
18872 it->descent += face->box_line_width;
18873 }
18874 if (!NILP (height)
18875 && XINT (height) > it->ascent + it->descent)
18876 it->ascent = XINT (height) - it->descent;
18877
18878 spacing = calc_line_height_property(it, Qline_spacing, font, boff, &total);
18879 if (INTEGERP (spacing))
18880 {
18881 extra_line_spacing = XINT (spacing);
18882 if (total)
18883 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
18884 }
18885 }
18886 }
18887 else if (it->char_to_display == '\t')
18888 {
18889 int tab_width = it->tab_width * FRAME_COLUMN_WIDTH (it->f);
18890 int x = it->current_x + it->continuation_lines_width;
18891 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
18892
18893 /* If the distance from the current position to the next tab
18894 stop is less than a canonical character width, use the
18895 tab stop after that. */
18896 if (next_tab_x - x < FRAME_COLUMN_WIDTH (it->f))
18897 next_tab_x += tab_width;
18898
18899 it->pixel_width = next_tab_x - x;
18900 it->nglyphs = 1;
18901 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
18902 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
18903
18904 if (it->glyph_row)
18905 {
18906 append_stretch_glyph (it, it->object, it->pixel_width,
18907 it->ascent + it->descent, it->ascent);
18908 }
18909 }
18910 else
18911 {
18912 /* A multi-byte character. Assume that the display width of the
18913 character is the width of the character multiplied by the
18914 width of the font. */
18915
18916 /* If we found a font, this font should give us the right
18917 metrics. If we didn't find a font, use the frame's
18918 default font and calculate the width of the character
18919 from the charset width; this is what old redisplay code
18920 did. */
18921
18922 pcm = FRAME_RIF (it->f)->per_char_metric (font, &char2b,
18923 FONT_TYPE_FOR_MULTIBYTE (font, it->c));
18924
18925 if (font_not_found_p || !pcm)
18926 {
18927 int charset = CHAR_CHARSET (it->char_to_display);
18928
18929 it->glyph_not_available_p = 1;
18930 it->pixel_width = (FRAME_COLUMN_WIDTH (it->f)
18931 * CHARSET_WIDTH (charset));
18932 it->phys_ascent = FONT_BASE (font) + boff;
18933 it->phys_descent = FONT_DESCENT (font) - boff;
18934 }
18935 else
18936 {
18937 it->pixel_width = pcm->width;
18938 it->phys_ascent = pcm->ascent + boff;
18939 it->phys_descent = pcm->descent - boff;
18940 if (it->glyph_row
18941 && (pcm->lbearing < 0
18942 || pcm->rbearing > pcm->width))
18943 it->glyph_row->contains_overlapping_glyphs_p = 1;
18944 }
18945 it->nglyphs = 1;
18946 it->ascent = FONT_BASE (font) + boff;
18947 it->descent = FONT_DESCENT (font) - boff;
18948 if (face->box != FACE_NO_BOX)
18949 {
18950 int thick = face->box_line_width;
18951
18952 if (thick > 0)
18953 {
18954 it->ascent += thick;
18955 it->descent += thick;
18956 }
18957 else
18958 thick = - thick;
18959
18960 if (it->start_of_box_run_p)
18961 it->pixel_width += thick;
18962 if (it->end_of_box_run_p)
18963 it->pixel_width += thick;
18964 }
18965
18966 /* If face has an overline, add the height of the overline
18967 (1 pixel) and a 1 pixel margin to the character height. */
18968 if (face->overline_p)
18969 it->ascent += 2;
18970
18971 take_vertical_position_into_account (it);
18972
18973 if (it->glyph_row)
18974 append_glyph (it);
18975 }
18976 it->multibyte_p = saved_multibyte_p;
18977 }
18978 else if (it->what == IT_COMPOSITION)
18979 {
18980 /* Note: A composition is represented as one glyph in the
18981 glyph matrix. There are no padding glyphs. */
18982 XChar2b char2b;
18983 XFontStruct *font;
18984 struct face *face = FACE_FROM_ID (it->f, it->face_id);
18985 XCharStruct *pcm;
18986 int font_not_found_p;
18987 struct font_info *font_info;
18988 int boff; /* baseline offset */
18989 struct composition *cmp = composition_table[it->cmp_id];
18990
18991 /* Maybe translate single-byte characters to multibyte. */
18992 it->char_to_display = it->c;
18993 if (unibyte_display_via_language_environment
18994 && SINGLE_BYTE_CHAR_P (it->c)
18995 && (it->c >= 0240
18996 || (it->c >= 0200
18997 && !NILP (Vnonascii_translation_table))))
18998 {
18999 it->char_to_display = unibyte_char_to_multibyte (it->c);
19000 }
19001
19002 /* Get face and font to use. Encode IT->char_to_display. */
19003 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
19004 face = FACE_FROM_ID (it->f, it->face_id);
19005 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
19006 &char2b, it->multibyte_p, 0);
19007 font = face->font;
19008
19009 /* When no suitable font found, use the default font. */
19010 font_not_found_p = font == NULL;
19011 if (font_not_found_p)
19012 {
19013 font = FRAME_FONT (it->f);
19014 boff = FRAME_BASELINE_OFFSET (it->f);
19015 font_info = NULL;
19016 }
19017 else
19018 {
19019 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
19020 boff = font_info->baseline_offset;
19021 if (font_info->vertical_centering)
19022 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
19023 }
19024
19025 /* There are no padding glyphs, so there is only one glyph to
19026 produce for the composition. Important is that pixel_width,
19027 ascent and descent are the values of what is drawn by
19028 draw_glyphs (i.e. the values of the overall glyphs composed). */
19029 it->nglyphs = 1;
19030
19031 /* If we have not yet calculated pixel size data of glyphs of
19032 the composition for the current face font, calculate them
19033 now. Theoretically, we have to check all fonts for the
19034 glyphs, but that requires much time and memory space. So,
19035 here we check only the font of the first glyph. This leads
19036 to incorrect display very rarely, and C-l (recenter) can
19037 correct the display anyway. */
19038 if (cmp->font != (void *) font)
19039 {
19040 /* Ascent and descent of the font of the first character of
19041 this composition (adjusted by baseline offset). Ascent
19042 and descent of overall glyphs should not be less than
19043 them respectively. */
19044 int font_ascent = FONT_BASE (font) + boff;
19045 int font_descent = FONT_DESCENT (font) - boff;
19046 /* Bounding box of the overall glyphs. */
19047 int leftmost, rightmost, lowest, highest;
19048 int i, width, ascent, descent;
19049
19050 cmp->font = (void *) font;
19051
19052 /* Initialize the bounding box. */
19053 if (font_info
19054 && (pcm = FRAME_RIF (it->f)->per_char_metric (font, &char2b,
19055 FONT_TYPE_FOR_MULTIBYTE (font, it->c))))
19056 {
19057 width = pcm->width;
19058 ascent = pcm->ascent;
19059 descent = pcm->descent;
19060 }
19061 else
19062 {
19063 width = FONT_WIDTH (font);
19064 ascent = FONT_BASE (font);
19065 descent = FONT_DESCENT (font);
19066 }
19067
19068 rightmost = width;
19069 lowest = - descent + boff;
19070 highest = ascent + boff;
19071 leftmost = 0;
19072
19073 if (font_info
19074 && font_info->default_ascent
19075 && CHAR_TABLE_P (Vuse_default_ascent)
19076 && !NILP (Faref (Vuse_default_ascent,
19077 make_number (it->char_to_display))))
19078 highest = font_info->default_ascent + boff;
19079
19080 /* Draw the first glyph at the normal position. It may be
19081 shifted to right later if some other glyphs are drawn at
19082 the left. */
19083 cmp->offsets[0] = 0;
19084 cmp->offsets[1] = boff;
19085
19086 /* Set cmp->offsets for the remaining glyphs. */
19087 for (i = 1; i < cmp->glyph_len; i++)
19088 {
19089 int left, right, btm, top;
19090 int ch = COMPOSITION_GLYPH (cmp, i);
19091 int face_id = FACE_FOR_CHAR (it->f, face, ch);
19092
19093 face = FACE_FROM_ID (it->f, face_id);
19094 get_char_face_and_encoding (it->f, ch, face->id,
19095 &char2b, it->multibyte_p, 0);
19096 font = face->font;
19097 if (font == NULL)
19098 {
19099 font = FRAME_FONT (it->f);
19100 boff = FRAME_BASELINE_OFFSET (it->f);
19101 font_info = NULL;
19102 }
19103 else
19104 {
19105 font_info
19106 = FONT_INFO_FROM_ID (it->f, face->font_info_id);
19107 boff = font_info->baseline_offset;
19108 if (font_info->vertical_centering)
19109 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
19110 }
19111
19112 if (font_info
19113 && (pcm = FRAME_RIF (it->f)->per_char_metric (font, &char2b,
19114 FONT_TYPE_FOR_MULTIBYTE (font, ch))))
19115 {
19116 width = pcm->width;
19117 ascent = pcm->ascent;
19118 descent = pcm->descent;
19119 }
19120 else
19121 {
19122 width = FONT_WIDTH (font);
19123 ascent = 1;
19124 descent = 0;
19125 }
19126
19127 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
19128 {
19129 /* Relative composition with or without
19130 alternate chars. */
19131 left = (leftmost + rightmost - width) / 2;
19132 btm = - descent + boff;
19133 if (font_info && font_info->relative_compose
19134 && (! CHAR_TABLE_P (Vignore_relative_composition)
19135 || NILP (Faref (Vignore_relative_composition,
19136 make_number (ch)))))
19137 {
19138
19139 if (- descent >= font_info->relative_compose)
19140 /* One extra pixel between two glyphs. */
19141 btm = highest + 1;
19142 else if (ascent <= 0)
19143 /* One extra pixel between two glyphs. */
19144 btm = lowest - 1 - ascent - descent;
19145 }
19146 }
19147 else
19148 {
19149 /* A composition rule is specified by an integer
19150 value that encodes global and new reference
19151 points (GREF and NREF). GREF and NREF are
19152 specified by numbers as below:
19153
19154 0---1---2 -- ascent
19155 | |
19156 | |
19157 | |
19158 9--10--11 -- center
19159 | |
19160 ---3---4---5--- baseline
19161 | |
19162 6---7---8 -- descent
19163 */
19164 int rule = COMPOSITION_RULE (cmp, i);
19165 int gref, nref, grefx, grefy, nrefx, nrefy;
19166
19167 COMPOSITION_DECODE_RULE (rule, gref, nref);
19168 grefx = gref % 3, nrefx = nref % 3;
19169 grefy = gref / 3, nrefy = nref / 3;
19170
19171 left = (leftmost
19172 + grefx * (rightmost - leftmost) / 2
19173 - nrefx * width / 2);
19174 btm = ((grefy == 0 ? highest
19175 : grefy == 1 ? 0
19176 : grefy == 2 ? lowest
19177 : (highest + lowest) / 2)
19178 - (nrefy == 0 ? ascent + descent
19179 : nrefy == 1 ? descent - boff
19180 : nrefy == 2 ? 0
19181 : (ascent + descent) / 2));
19182 }
19183
19184 cmp->offsets[i * 2] = left;
19185 cmp->offsets[i * 2 + 1] = btm + descent;
19186
19187 /* Update the bounding box of the overall glyphs. */
19188 right = left + width;
19189 top = btm + descent + ascent;
19190 if (left < leftmost)
19191 leftmost = left;
19192 if (right > rightmost)
19193 rightmost = right;
19194 if (top > highest)
19195 highest = top;
19196 if (btm < lowest)
19197 lowest = btm;
19198 }
19199
19200 /* If there are glyphs whose x-offsets are negative,
19201 shift all glyphs to the right and make all x-offsets
19202 non-negative. */
19203 if (leftmost < 0)
19204 {
19205 for (i = 0; i < cmp->glyph_len; i++)
19206 cmp->offsets[i * 2] -= leftmost;
19207 rightmost -= leftmost;
19208 }
19209
19210 cmp->pixel_width = rightmost;
19211 cmp->ascent = highest;
19212 cmp->descent = - lowest;
19213 if (cmp->ascent < font_ascent)
19214 cmp->ascent = font_ascent;
19215 if (cmp->descent < font_descent)
19216 cmp->descent = font_descent;
19217 }
19218
19219 it->pixel_width = cmp->pixel_width;
19220 it->ascent = it->phys_ascent = cmp->ascent;
19221 it->descent = it->phys_descent = cmp->descent;
19222
19223 if (face->box != FACE_NO_BOX)
19224 {
19225 int thick = face->box_line_width;
19226
19227 if (thick > 0)
19228 {
19229 it->ascent += thick;
19230 it->descent += thick;
19231 }
19232 else
19233 thick = - thick;
19234
19235 if (it->start_of_box_run_p)
19236 it->pixel_width += thick;
19237 if (it->end_of_box_run_p)
19238 it->pixel_width += thick;
19239 }
19240
19241 /* If face has an overline, add the height of the overline
19242 (1 pixel) and a 1 pixel margin to the character height. */
19243 if (face->overline_p)
19244 it->ascent += 2;
19245
19246 take_vertical_position_into_account (it);
19247
19248 if (it->glyph_row)
19249 append_composite_glyph (it);
19250 }
19251 else if (it->what == IT_IMAGE)
19252 produce_image_glyph (it);
19253 else if (it->what == IT_STRETCH)
19254 produce_stretch_glyph (it);
19255
19256 /* Accumulate dimensions. Note: can't assume that it->descent > 0
19257 because this isn't true for images with `:ascent 100'. */
19258 xassert (it->ascent >= 0 && it->descent >= 0);
19259 if (it->area == TEXT_AREA)
19260 it->current_x += it->pixel_width;
19261
19262 if (extra_line_spacing > 0)
19263 it->descent += extra_line_spacing;
19264
19265 it->max_ascent = max (it->max_ascent, it->ascent);
19266 it->max_descent = max (it->max_descent, it->descent);
19267 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
19268 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
19269 }
19270
19271 /* EXPORT for RIF:
19272 Output LEN glyphs starting at START at the nominal cursor position.
19273 Advance the nominal cursor over the text. The global variable
19274 updated_window contains the window being updated, updated_row is
19275 the glyph row being updated, and updated_area is the area of that
19276 row being updated. */
19277
19278 void
19279 x_write_glyphs (start, len)
19280 struct glyph *start;
19281 int len;
19282 {
19283 int x, hpos;
19284
19285 xassert (updated_window && updated_row);
19286 BLOCK_INPUT;
19287
19288 /* Write glyphs. */
19289
19290 hpos = start - updated_row->glyphs[updated_area];
19291 x = draw_glyphs (updated_window, output_cursor.x,
19292 updated_row, updated_area,
19293 hpos, hpos + len,
19294 DRAW_NORMAL_TEXT, 0);
19295
19296 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
19297 if (updated_area == TEXT_AREA
19298 && updated_window->phys_cursor_on_p
19299 && updated_window->phys_cursor.vpos == output_cursor.vpos
19300 && updated_window->phys_cursor.hpos >= hpos
19301 && updated_window->phys_cursor.hpos < hpos + len)
19302 updated_window->phys_cursor_on_p = 0;
19303
19304 UNBLOCK_INPUT;
19305
19306 /* Advance the output cursor. */
19307 output_cursor.hpos += len;
19308 output_cursor.x = x;
19309 }
19310
19311
19312 /* EXPORT for RIF:
19313 Insert LEN glyphs from START at the nominal cursor position. */
19314
19315 void
19316 x_insert_glyphs (start, len)
19317 struct glyph *start;
19318 int len;
19319 {
19320 struct frame *f;
19321 struct window *w;
19322 int line_height, shift_by_width, shifted_region_width;
19323 struct glyph_row *row;
19324 struct glyph *glyph;
19325 int frame_x, frame_y, hpos;
19326
19327 xassert (updated_window && updated_row);
19328 BLOCK_INPUT;
19329 w = updated_window;
19330 f = XFRAME (WINDOW_FRAME (w));
19331
19332 /* Get the height of the line we are in. */
19333 row = updated_row;
19334 line_height = row->height;
19335
19336 /* Get the width of the glyphs to insert. */
19337 shift_by_width = 0;
19338 for (glyph = start; glyph < start + len; ++glyph)
19339 shift_by_width += glyph->pixel_width;
19340
19341 /* Get the width of the region to shift right. */
19342 shifted_region_width = (window_box_width (w, updated_area)
19343 - output_cursor.x
19344 - shift_by_width);
19345
19346 /* Shift right. */
19347 frame_x = window_box_left (w, updated_area) + output_cursor.x;
19348 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
19349
19350 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
19351 line_height, shift_by_width);
19352
19353 /* Write the glyphs. */
19354 hpos = start - row->glyphs[updated_area];
19355 draw_glyphs (w, output_cursor.x, row, updated_area,
19356 hpos, hpos + len,
19357 DRAW_NORMAL_TEXT, 0);
19358
19359 /* Advance the output cursor. */
19360 output_cursor.hpos += len;
19361 output_cursor.x += shift_by_width;
19362 UNBLOCK_INPUT;
19363 }
19364
19365
19366 /* EXPORT for RIF:
19367 Erase the current text line from the nominal cursor position
19368 (inclusive) to pixel column TO_X (exclusive). The idea is that
19369 everything from TO_X onward is already erased.
19370
19371 TO_X is a pixel position relative to updated_area of
19372 updated_window. TO_X == -1 means clear to the end of this area. */
19373
19374 void
19375 x_clear_end_of_line (to_x)
19376 int to_x;
19377 {
19378 struct frame *f;
19379 struct window *w = updated_window;
19380 int max_x, min_y, max_y;
19381 int from_x, from_y, to_y;
19382
19383 xassert (updated_window && updated_row);
19384 f = XFRAME (w->frame);
19385
19386 if (updated_row->full_width_p)
19387 max_x = WINDOW_TOTAL_WIDTH (w);
19388 else
19389 max_x = window_box_width (w, updated_area);
19390 max_y = window_text_bottom_y (w);
19391
19392 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
19393 of window. For TO_X > 0, truncate to end of drawing area. */
19394 if (to_x == 0)
19395 return;
19396 else if (to_x < 0)
19397 to_x = max_x;
19398 else
19399 to_x = min (to_x, max_x);
19400
19401 to_y = min (max_y, output_cursor.y + updated_row->height);
19402
19403 /* Notice if the cursor will be cleared by this operation. */
19404 if (!updated_row->full_width_p)
19405 notice_overwritten_cursor (w, updated_area,
19406 output_cursor.x, -1,
19407 updated_row->y,
19408 MATRIX_ROW_BOTTOM_Y (updated_row));
19409
19410 from_x = output_cursor.x;
19411
19412 /* Translate to frame coordinates. */
19413 if (updated_row->full_width_p)
19414 {
19415 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
19416 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
19417 }
19418 else
19419 {
19420 int area_left = window_box_left (w, updated_area);
19421 from_x += area_left;
19422 to_x += area_left;
19423 }
19424
19425 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
19426 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
19427 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
19428
19429 /* Prevent inadvertently clearing to end of the X window. */
19430 if (to_x > from_x && to_y > from_y)
19431 {
19432 BLOCK_INPUT;
19433 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
19434 to_x - from_x, to_y - from_y);
19435 UNBLOCK_INPUT;
19436 }
19437 }
19438
19439 #endif /* HAVE_WINDOW_SYSTEM */
19440
19441
19442 \f
19443 /***********************************************************************
19444 Cursor types
19445 ***********************************************************************/
19446
19447 /* Value is the internal representation of the specified cursor type
19448 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
19449 of the bar cursor. */
19450
19451 static enum text_cursor_kinds
19452 get_specified_cursor_type (arg, width)
19453 Lisp_Object arg;
19454 int *width;
19455 {
19456 enum text_cursor_kinds type;
19457
19458 if (NILP (arg))
19459 return NO_CURSOR;
19460
19461 if (EQ (arg, Qbox))
19462 return FILLED_BOX_CURSOR;
19463
19464 if (EQ (arg, Qhollow))
19465 return HOLLOW_BOX_CURSOR;
19466
19467 if (EQ (arg, Qbar))
19468 {
19469 *width = 2;
19470 return BAR_CURSOR;
19471 }
19472
19473 if (CONSP (arg)
19474 && EQ (XCAR (arg), Qbar)
19475 && INTEGERP (XCDR (arg))
19476 && XINT (XCDR (arg)) >= 0)
19477 {
19478 *width = XINT (XCDR (arg));
19479 return BAR_CURSOR;
19480 }
19481
19482 if (EQ (arg, Qhbar))
19483 {
19484 *width = 2;
19485 return HBAR_CURSOR;
19486 }
19487
19488 if (CONSP (arg)
19489 && EQ (XCAR (arg), Qhbar)
19490 && INTEGERP (XCDR (arg))
19491 && XINT (XCDR (arg)) >= 0)
19492 {
19493 *width = XINT (XCDR (arg));
19494 return HBAR_CURSOR;
19495 }
19496
19497 /* Treat anything unknown as "hollow box cursor".
19498 It was bad to signal an error; people have trouble fixing
19499 .Xdefaults with Emacs, when it has something bad in it. */
19500 type = HOLLOW_BOX_CURSOR;
19501
19502 return type;
19503 }
19504
19505 /* Set the default cursor types for specified frame. */
19506 void
19507 set_frame_cursor_types (f, arg)
19508 struct frame *f;
19509 Lisp_Object arg;
19510 {
19511 int width;
19512 Lisp_Object tem;
19513
19514 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
19515 FRAME_CURSOR_WIDTH (f) = width;
19516
19517 /* By default, set up the blink-off state depending on the on-state. */
19518
19519 tem = Fassoc (arg, Vblink_cursor_alist);
19520 if (!NILP (tem))
19521 {
19522 FRAME_BLINK_OFF_CURSOR (f)
19523 = get_specified_cursor_type (XCDR (tem), &width);
19524 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
19525 }
19526 else
19527 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
19528 }
19529
19530
19531 /* Return the cursor we want to be displayed in window W. Return
19532 width of bar/hbar cursor through WIDTH arg. Return with
19533 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
19534 (i.e. if the `system caret' should track this cursor).
19535
19536 In a mini-buffer window, we want the cursor only to appear if we
19537 are reading input from this window. For the selected window, we
19538 want the cursor type given by the frame parameter or buffer local
19539 setting of cursor-type. If explicitly marked off, draw no cursor.
19540 In all other cases, we want a hollow box cursor. */
19541
19542 static enum text_cursor_kinds
19543 get_window_cursor_type (w, glyph, width, active_cursor)
19544 struct window *w;
19545 struct glyph *glyph;
19546 int *width;
19547 int *active_cursor;
19548 {
19549 struct frame *f = XFRAME (w->frame);
19550 struct buffer *b = XBUFFER (w->buffer);
19551 int cursor_type = DEFAULT_CURSOR;
19552 Lisp_Object alt_cursor;
19553 int non_selected = 0;
19554
19555 *active_cursor = 1;
19556
19557 /* Echo area */
19558 if (cursor_in_echo_area
19559 && FRAME_HAS_MINIBUF_P (f)
19560 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
19561 {
19562 if (w == XWINDOW (echo_area_window))
19563 {
19564 *width = FRAME_CURSOR_WIDTH (f);
19565 return FRAME_DESIRED_CURSOR (f);
19566 }
19567
19568 *active_cursor = 0;
19569 non_selected = 1;
19570 }
19571
19572 /* Nonselected window or nonselected frame. */
19573 else if (w != XWINDOW (f->selected_window)
19574 #ifdef HAVE_WINDOW_SYSTEM
19575 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame
19576 #endif
19577 )
19578 {
19579 *active_cursor = 0;
19580
19581 if (MINI_WINDOW_P (w) && minibuf_level == 0)
19582 return NO_CURSOR;
19583
19584 non_selected = 1;
19585 }
19586
19587 /* Never display a cursor in a window in which cursor-type is nil. */
19588 if (NILP (b->cursor_type))
19589 return NO_CURSOR;
19590
19591 /* Use cursor-in-non-selected-windows for non-selected window or frame. */
19592 if (non_selected)
19593 {
19594 alt_cursor = Fbuffer_local_value (Qcursor_in_non_selected_windows, w->buffer);
19595 return get_specified_cursor_type (alt_cursor, width);
19596 }
19597
19598 /* Get the normal cursor type for this window. */
19599 if (EQ (b->cursor_type, Qt))
19600 {
19601 cursor_type = FRAME_DESIRED_CURSOR (f);
19602 *width = FRAME_CURSOR_WIDTH (f);
19603 }
19604 else
19605 cursor_type = get_specified_cursor_type (b->cursor_type, width);
19606
19607 /* Use normal cursor if not blinked off. */
19608 if (!w->cursor_off_p)
19609 {
19610 if (glyph != NULL && glyph->type == IMAGE_GLYPH) {
19611 if (cursor_type == FILLED_BOX_CURSOR)
19612 cursor_type = HOLLOW_BOX_CURSOR;
19613 }
19614 return cursor_type;
19615 }
19616
19617 /* Cursor is blinked off, so determine how to "toggle" it. */
19618
19619 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
19620 if ((alt_cursor = Fassoc (b->cursor_type, Vblink_cursor_alist), !NILP (alt_cursor)))
19621 return get_specified_cursor_type (XCDR (alt_cursor), width);
19622
19623 /* Then see if frame has specified a specific blink off cursor type. */
19624 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
19625 {
19626 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
19627 return FRAME_BLINK_OFF_CURSOR (f);
19628 }
19629
19630 #if 0
19631 /* Some people liked having a permanently visible blinking cursor,
19632 while others had very strong opinions against it. So it was
19633 decided to remove it. KFS 2003-09-03 */
19634
19635 /* Finally perform built-in cursor blinking:
19636 filled box <-> hollow box
19637 wide [h]bar <-> narrow [h]bar
19638 narrow [h]bar <-> no cursor
19639 other type <-> no cursor */
19640
19641 if (cursor_type == FILLED_BOX_CURSOR)
19642 return HOLLOW_BOX_CURSOR;
19643
19644 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
19645 {
19646 *width = 1;
19647 return cursor_type;
19648 }
19649 #endif
19650
19651 return NO_CURSOR;
19652 }
19653
19654
19655 #ifdef HAVE_WINDOW_SYSTEM
19656
19657 /* Notice when the text cursor of window W has been completely
19658 overwritten by a drawing operation that outputs glyphs in AREA
19659 starting at X0 and ending at X1 in the line starting at Y0 and
19660 ending at Y1. X coordinates are area-relative. X1 < 0 means all
19661 the rest of the line after X0 has been written. Y coordinates
19662 are window-relative. */
19663
19664 static void
19665 notice_overwritten_cursor (w, area, x0, x1, y0, y1)
19666 struct window *w;
19667 enum glyph_row_area area;
19668 int x0, y0, x1, y1;
19669 {
19670 int cx0, cx1, cy0, cy1;
19671 struct glyph_row *row;
19672
19673 if (!w->phys_cursor_on_p)
19674 return;
19675 if (area != TEXT_AREA)
19676 return;
19677
19678 row = w->current_matrix->rows + w->phys_cursor.vpos;
19679 if (!row->displays_text_p)
19680 return;
19681
19682 if (row->cursor_in_fringe_p)
19683 {
19684 row->cursor_in_fringe_p = 0;
19685 draw_fringe_bitmap (w, row, 0);
19686 w->phys_cursor_on_p = 0;
19687 return;
19688 }
19689
19690 cx0 = w->phys_cursor.x;
19691 cx1 = cx0 + w->phys_cursor_width;
19692 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
19693 return;
19694
19695 /* The cursor image will be completely removed from the
19696 screen if the output area intersects the cursor area in
19697 y-direction. When we draw in [y0 y1[, and some part of
19698 the cursor is at y < y0, that part must have been drawn
19699 before. When scrolling, the cursor is erased before
19700 actually scrolling, so we don't come here. When not
19701 scrolling, the rows above the old cursor row must have
19702 changed, and in this case these rows must have written
19703 over the cursor image.
19704
19705 Likewise if part of the cursor is below y1, with the
19706 exception of the cursor being in the first blank row at
19707 the buffer and window end because update_text_area
19708 doesn't draw that row. (Except when it does, but
19709 that's handled in update_text_area.) */
19710
19711 cy0 = w->phys_cursor.y;
19712 cy1 = cy0 + w->phys_cursor_height;
19713 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
19714 return;
19715
19716 w->phys_cursor_on_p = 0;
19717 }
19718
19719 #endif /* HAVE_WINDOW_SYSTEM */
19720
19721 \f
19722 /************************************************************************
19723 Mouse Face
19724 ************************************************************************/
19725
19726 #ifdef HAVE_WINDOW_SYSTEM
19727
19728 /* EXPORT for RIF:
19729 Fix the display of area AREA of overlapping row ROW in window W. */
19730
19731 void
19732 x_fix_overlapping_area (w, row, area)
19733 struct window *w;
19734 struct glyph_row *row;
19735 enum glyph_row_area area;
19736 {
19737 int i, x;
19738
19739 BLOCK_INPUT;
19740
19741 x = 0;
19742 for (i = 0; i < row->used[area];)
19743 {
19744 if (row->glyphs[area][i].overlaps_vertically_p)
19745 {
19746 int start = i, start_x = x;
19747
19748 do
19749 {
19750 x += row->glyphs[area][i].pixel_width;
19751 ++i;
19752 }
19753 while (i < row->used[area]
19754 && row->glyphs[area][i].overlaps_vertically_p);
19755
19756 draw_glyphs (w, start_x, row, area,
19757 start, i,
19758 DRAW_NORMAL_TEXT, 1);
19759 }
19760 else
19761 {
19762 x += row->glyphs[area][i].pixel_width;
19763 ++i;
19764 }
19765 }
19766
19767 UNBLOCK_INPUT;
19768 }
19769
19770
19771 /* EXPORT:
19772 Draw the cursor glyph of window W in glyph row ROW. See the
19773 comment of draw_glyphs for the meaning of HL. */
19774
19775 void
19776 draw_phys_cursor_glyph (w, row, hl)
19777 struct window *w;
19778 struct glyph_row *row;
19779 enum draw_glyphs_face hl;
19780 {
19781 /* If cursor hpos is out of bounds, don't draw garbage. This can
19782 happen in mini-buffer windows when switching between echo area
19783 glyphs and mini-buffer. */
19784 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
19785 {
19786 int on_p = w->phys_cursor_on_p;
19787 int x1;
19788 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
19789 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
19790 hl, 0);
19791 w->phys_cursor_on_p = on_p;
19792
19793 if (hl == DRAW_CURSOR)
19794 w->phys_cursor_width = x1 - w->phys_cursor.x;
19795 /* When we erase the cursor, and ROW is overlapped by other
19796 rows, make sure that these overlapping parts of other rows
19797 are redrawn. */
19798 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
19799 {
19800 if (row > w->current_matrix->rows
19801 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
19802 x_fix_overlapping_area (w, row - 1, TEXT_AREA);
19803
19804 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
19805 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
19806 x_fix_overlapping_area (w, row + 1, TEXT_AREA);
19807 }
19808 }
19809 }
19810
19811
19812 /* EXPORT:
19813 Erase the image of a cursor of window W from the screen. */
19814
19815 void
19816 erase_phys_cursor (w)
19817 struct window *w;
19818 {
19819 struct frame *f = XFRAME (w->frame);
19820 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
19821 int hpos = w->phys_cursor.hpos;
19822 int vpos = w->phys_cursor.vpos;
19823 int mouse_face_here_p = 0;
19824 struct glyph_matrix *active_glyphs = w->current_matrix;
19825 struct glyph_row *cursor_row;
19826 struct glyph *cursor_glyph;
19827 enum draw_glyphs_face hl;
19828
19829 /* No cursor displayed or row invalidated => nothing to do on the
19830 screen. */
19831 if (w->phys_cursor_type == NO_CURSOR)
19832 goto mark_cursor_off;
19833
19834 /* VPOS >= active_glyphs->nrows means that window has been resized.
19835 Don't bother to erase the cursor. */
19836 if (vpos >= active_glyphs->nrows)
19837 goto mark_cursor_off;
19838
19839 /* If row containing cursor is marked invalid, there is nothing we
19840 can do. */
19841 cursor_row = MATRIX_ROW (active_glyphs, vpos);
19842 if (!cursor_row->enabled_p)
19843 goto mark_cursor_off;
19844
19845 /* If row is completely invisible, don't attempt to delete a cursor which
19846 isn't there. This can happen if cursor is at top of a window, and
19847 we switch to a buffer with a header line in that window. */
19848 if (cursor_row->visible_height <= 0)
19849 goto mark_cursor_off;
19850
19851 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
19852 if (cursor_row->cursor_in_fringe_p)
19853 {
19854 cursor_row->cursor_in_fringe_p = 0;
19855 draw_fringe_bitmap (w, cursor_row, 0);
19856 goto mark_cursor_off;
19857 }
19858
19859 /* This can happen when the new row is shorter than the old one.
19860 In this case, either draw_glyphs or clear_end_of_line
19861 should have cleared the cursor. Note that we wouldn't be
19862 able to erase the cursor in this case because we don't have a
19863 cursor glyph at hand. */
19864 if (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])
19865 goto mark_cursor_off;
19866
19867 /* If the cursor is in the mouse face area, redisplay that when
19868 we clear the cursor. */
19869 if (! NILP (dpyinfo->mouse_face_window)
19870 && w == XWINDOW (dpyinfo->mouse_face_window)
19871 && (vpos > dpyinfo->mouse_face_beg_row
19872 || (vpos == dpyinfo->mouse_face_beg_row
19873 && hpos >= dpyinfo->mouse_face_beg_col))
19874 && (vpos < dpyinfo->mouse_face_end_row
19875 || (vpos == dpyinfo->mouse_face_end_row
19876 && hpos < dpyinfo->mouse_face_end_col))
19877 /* Don't redraw the cursor's spot in mouse face if it is at the
19878 end of a line (on a newline). The cursor appears there, but
19879 mouse highlighting does not. */
19880 && cursor_row->used[TEXT_AREA] > hpos)
19881 mouse_face_here_p = 1;
19882
19883 /* Maybe clear the display under the cursor. */
19884 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
19885 {
19886 int x, y;
19887 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
19888
19889 cursor_glyph = get_phys_cursor_glyph (w);
19890 if (cursor_glyph == NULL)
19891 goto mark_cursor_off;
19892
19893 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
19894 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
19895
19896 FRAME_RIF (f)->clear_frame_area (f, x, y,
19897 cursor_glyph->pixel_width, cursor_row->visible_height);
19898 }
19899
19900 /* Erase the cursor by redrawing the character underneath it. */
19901 if (mouse_face_here_p)
19902 hl = DRAW_MOUSE_FACE;
19903 else
19904 hl = DRAW_NORMAL_TEXT;
19905 draw_phys_cursor_glyph (w, cursor_row, hl);
19906
19907 mark_cursor_off:
19908 w->phys_cursor_on_p = 0;
19909 w->phys_cursor_type = NO_CURSOR;
19910 }
19911
19912
19913 /* EXPORT:
19914 Display or clear cursor of window W. If ON is zero, clear the
19915 cursor. If it is non-zero, display the cursor. If ON is nonzero,
19916 where to put the cursor is specified by HPOS, VPOS, X and Y. */
19917
19918 void
19919 display_and_set_cursor (w, on, hpos, vpos, x, y)
19920 struct window *w;
19921 int on, hpos, vpos, x, y;
19922 {
19923 struct frame *f = XFRAME (w->frame);
19924 int new_cursor_type;
19925 int new_cursor_width;
19926 int active_cursor;
19927 struct glyph_row *glyph_row;
19928 struct glyph *glyph;
19929
19930 /* This is pointless on invisible frames, and dangerous on garbaged
19931 windows and frames; in the latter case, the frame or window may
19932 be in the midst of changing its size, and x and y may be off the
19933 window. */
19934 if (! FRAME_VISIBLE_P (f)
19935 || FRAME_GARBAGED_P (f)
19936 || vpos >= w->current_matrix->nrows
19937 || hpos >= w->current_matrix->matrix_w)
19938 return;
19939
19940 /* If cursor is off and we want it off, return quickly. */
19941 if (!on && !w->phys_cursor_on_p)
19942 return;
19943
19944 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
19945 /* If cursor row is not enabled, we don't really know where to
19946 display the cursor. */
19947 if (!glyph_row->enabled_p)
19948 {
19949 w->phys_cursor_on_p = 0;
19950 return;
19951 }
19952
19953 glyph = NULL;
19954 if (!glyph_row->exact_window_width_line_p
19955 || hpos < glyph_row->used[TEXT_AREA])
19956 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
19957
19958 xassert (interrupt_input_blocked);
19959
19960 /* Set new_cursor_type to the cursor we want to be displayed. */
19961 new_cursor_type = get_window_cursor_type (w, glyph,
19962 &new_cursor_width, &active_cursor);
19963
19964 /* If cursor is currently being shown and we don't want it to be or
19965 it is in the wrong place, or the cursor type is not what we want,
19966 erase it. */
19967 if (w->phys_cursor_on_p
19968 && (!on
19969 || w->phys_cursor.x != x
19970 || w->phys_cursor.y != y
19971 || new_cursor_type != w->phys_cursor_type
19972 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
19973 && new_cursor_width != w->phys_cursor_width)))
19974 erase_phys_cursor (w);
19975
19976 /* Don't check phys_cursor_on_p here because that flag is only set
19977 to zero in some cases where we know that the cursor has been
19978 completely erased, to avoid the extra work of erasing the cursor
19979 twice. In other words, phys_cursor_on_p can be 1 and the cursor
19980 still not be visible, or it has only been partly erased. */
19981 if (on)
19982 {
19983 w->phys_cursor_ascent = glyph_row->ascent;
19984 w->phys_cursor_height = glyph_row->height;
19985
19986 /* Set phys_cursor_.* before x_draw_.* is called because some
19987 of them may need the information. */
19988 w->phys_cursor.x = x;
19989 w->phys_cursor.y = glyph_row->y;
19990 w->phys_cursor.hpos = hpos;
19991 w->phys_cursor.vpos = vpos;
19992 }
19993
19994 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
19995 new_cursor_type, new_cursor_width,
19996 on, active_cursor);
19997 }
19998
19999
20000 /* Switch the display of W's cursor on or off, according to the value
20001 of ON. */
20002
20003 static void
20004 update_window_cursor (w, on)
20005 struct window *w;
20006 int on;
20007 {
20008 /* Don't update cursor in windows whose frame is in the process
20009 of being deleted. */
20010 if (w->current_matrix)
20011 {
20012 BLOCK_INPUT;
20013 display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
20014 w->phys_cursor.x, w->phys_cursor.y);
20015 UNBLOCK_INPUT;
20016 }
20017 }
20018
20019
20020 /* Call update_window_cursor with parameter ON_P on all leaf windows
20021 in the window tree rooted at W. */
20022
20023 static void
20024 update_cursor_in_window_tree (w, on_p)
20025 struct window *w;
20026 int on_p;
20027 {
20028 while (w)
20029 {
20030 if (!NILP (w->hchild))
20031 update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
20032 else if (!NILP (w->vchild))
20033 update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
20034 else
20035 update_window_cursor (w, on_p);
20036
20037 w = NILP (w->next) ? 0 : XWINDOW (w->next);
20038 }
20039 }
20040
20041
20042 /* EXPORT:
20043 Display the cursor on window W, or clear it, according to ON_P.
20044 Don't change the cursor's position. */
20045
20046 void
20047 x_update_cursor (f, on_p)
20048 struct frame *f;
20049 int on_p;
20050 {
20051 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
20052 }
20053
20054
20055 /* EXPORT:
20056 Clear the cursor of window W to background color, and mark the
20057 cursor as not shown. This is used when the text where the cursor
20058 is is about to be rewritten. */
20059
20060 void
20061 x_clear_cursor (w)
20062 struct window *w;
20063 {
20064 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
20065 update_window_cursor (w, 0);
20066 }
20067
20068
20069 /* EXPORT:
20070 Display the active region described by mouse_face_* according to DRAW. */
20071
20072 void
20073 show_mouse_face (dpyinfo, draw)
20074 Display_Info *dpyinfo;
20075 enum draw_glyphs_face draw;
20076 {
20077 struct window *w = XWINDOW (dpyinfo->mouse_face_window);
20078 struct frame *f = XFRAME (WINDOW_FRAME (w));
20079
20080 if (/* If window is in the process of being destroyed, don't bother
20081 to do anything. */
20082 w->current_matrix != NULL
20083 /* Don't update mouse highlight if hidden */
20084 && (draw != DRAW_MOUSE_FACE || !dpyinfo->mouse_face_hidden)
20085 /* Recognize when we are called to operate on rows that don't exist
20086 anymore. This can happen when a window is split. */
20087 && dpyinfo->mouse_face_end_row < w->current_matrix->nrows)
20088 {
20089 int phys_cursor_on_p = w->phys_cursor_on_p;
20090 struct glyph_row *row, *first, *last;
20091
20092 first = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_beg_row);
20093 last = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_end_row);
20094
20095 for (row = first; row <= last && row->enabled_p; ++row)
20096 {
20097 int start_hpos, end_hpos, start_x;
20098
20099 /* For all but the first row, the highlight starts at column 0. */
20100 if (row == first)
20101 {
20102 start_hpos = dpyinfo->mouse_face_beg_col;
20103 start_x = dpyinfo->mouse_face_beg_x;
20104 }
20105 else
20106 {
20107 start_hpos = 0;
20108 start_x = 0;
20109 }
20110
20111 if (row == last)
20112 end_hpos = dpyinfo->mouse_face_end_col;
20113 else
20114 end_hpos = row->used[TEXT_AREA];
20115
20116 if (end_hpos > start_hpos)
20117 {
20118 draw_glyphs (w, start_x, row, TEXT_AREA,
20119 start_hpos, end_hpos,
20120 draw, 0);
20121
20122 row->mouse_face_p
20123 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
20124 }
20125 }
20126
20127 /* When we've written over the cursor, arrange for it to
20128 be displayed again. */
20129 if (phys_cursor_on_p && !w->phys_cursor_on_p)
20130 {
20131 BLOCK_INPUT;
20132 display_and_set_cursor (w, 1,
20133 w->phys_cursor.hpos, w->phys_cursor.vpos,
20134 w->phys_cursor.x, w->phys_cursor.y);
20135 UNBLOCK_INPUT;
20136 }
20137 }
20138
20139 /* Change the mouse cursor. */
20140 if (draw == DRAW_NORMAL_TEXT)
20141 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
20142 else if (draw == DRAW_MOUSE_FACE)
20143 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
20144 else
20145 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
20146 }
20147
20148 /* EXPORT:
20149 Clear out the mouse-highlighted active region.
20150 Redraw it un-highlighted first. Value is non-zero if mouse
20151 face was actually drawn unhighlighted. */
20152
20153 int
20154 clear_mouse_face (dpyinfo)
20155 Display_Info *dpyinfo;
20156 {
20157 int cleared = 0;
20158
20159 if (!dpyinfo->mouse_face_hidden && !NILP (dpyinfo->mouse_face_window))
20160 {
20161 show_mouse_face (dpyinfo, DRAW_NORMAL_TEXT);
20162 cleared = 1;
20163 }
20164
20165 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
20166 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
20167 dpyinfo->mouse_face_window = Qnil;
20168 dpyinfo->mouse_face_overlay = Qnil;
20169 return cleared;
20170 }
20171
20172
20173 /* EXPORT:
20174 Non-zero if physical cursor of window W is within mouse face. */
20175
20176 int
20177 cursor_in_mouse_face_p (w)
20178 struct window *w;
20179 {
20180 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
20181 int in_mouse_face = 0;
20182
20183 if (WINDOWP (dpyinfo->mouse_face_window)
20184 && XWINDOW (dpyinfo->mouse_face_window) == w)
20185 {
20186 int hpos = w->phys_cursor.hpos;
20187 int vpos = w->phys_cursor.vpos;
20188
20189 if (vpos >= dpyinfo->mouse_face_beg_row
20190 && vpos <= dpyinfo->mouse_face_end_row
20191 && (vpos > dpyinfo->mouse_face_beg_row
20192 || hpos >= dpyinfo->mouse_face_beg_col)
20193 && (vpos < dpyinfo->mouse_face_end_row
20194 || hpos < dpyinfo->mouse_face_end_col
20195 || dpyinfo->mouse_face_past_end))
20196 in_mouse_face = 1;
20197 }
20198
20199 return in_mouse_face;
20200 }
20201
20202
20203
20204 \f
20205 /* Find the glyph matrix position of buffer position CHARPOS in window
20206 *W. HPOS, *VPOS, *X, and *Y are set to the positions found. W's
20207 current glyphs must be up to date. If CHARPOS is above window
20208 start return (0, 0, 0, 0). If CHARPOS is after end of W, return end
20209 of last line in W. In the row containing CHARPOS, stop before glyphs
20210 having STOP as object. */
20211
20212 #if 1 /* This is a version of fast_find_position that's more correct
20213 in the presence of hscrolling, for example. I didn't install
20214 it right away because the problem fixed is minor, it failed
20215 in 20.x as well, and I think it's too risky to install
20216 so near the release of 21.1. 2001-09-25 gerd. */
20217
20218 static int
20219 fast_find_position (w, charpos, hpos, vpos, x, y, stop)
20220 struct window *w;
20221 int charpos;
20222 int *hpos, *vpos, *x, *y;
20223 Lisp_Object stop;
20224 {
20225 struct glyph_row *row, *first;
20226 struct glyph *glyph, *end;
20227 int past_end = 0;
20228
20229 first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
20230 row = row_containing_pos (w, charpos, first, NULL, 0);
20231 if (row == NULL)
20232 {
20233 if (charpos < MATRIX_ROW_START_CHARPOS (first))
20234 {
20235 *x = *y = *hpos = *vpos = 0;
20236 return 1;
20237 }
20238 else
20239 {
20240 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
20241 past_end = 1;
20242 }
20243 }
20244
20245 *x = row->x;
20246 *y = row->y;
20247 *vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
20248
20249 glyph = row->glyphs[TEXT_AREA];
20250 end = glyph + row->used[TEXT_AREA];
20251
20252 /* Skip over glyphs not having an object at the start of the row.
20253 These are special glyphs like truncation marks on terminal
20254 frames. */
20255 if (row->displays_text_p)
20256 while (glyph < end
20257 && INTEGERP (glyph->object)
20258 && !EQ (stop, glyph->object)
20259 && glyph->charpos < 0)
20260 {
20261 *x += glyph->pixel_width;
20262 ++glyph;
20263 }
20264
20265 while (glyph < end
20266 && !INTEGERP (glyph->object)
20267 && !EQ (stop, glyph->object)
20268 && (!BUFFERP (glyph->object)
20269 || glyph->charpos < charpos))
20270 {
20271 *x += glyph->pixel_width;
20272 ++glyph;
20273 }
20274
20275 *hpos = glyph - row->glyphs[TEXT_AREA];
20276 return !past_end;
20277 }
20278
20279 #else /* not 1 */
20280
20281 static int
20282 fast_find_position (w, pos, hpos, vpos, x, y, stop)
20283 struct window *w;
20284 int pos;
20285 int *hpos, *vpos, *x, *y;
20286 Lisp_Object stop;
20287 {
20288 int i;
20289 int lastcol;
20290 int maybe_next_line_p = 0;
20291 int line_start_position;
20292 int yb = window_text_bottom_y (w);
20293 struct glyph_row *row, *best_row;
20294 int row_vpos, best_row_vpos;
20295 int current_x;
20296
20297 row = best_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
20298 row_vpos = best_row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
20299
20300 while (row->y < yb)
20301 {
20302 if (row->used[TEXT_AREA])
20303 line_start_position = row->glyphs[TEXT_AREA]->charpos;
20304 else
20305 line_start_position = 0;
20306
20307 if (line_start_position > pos)
20308 break;
20309 /* If the position sought is the end of the buffer,
20310 don't include the blank lines at the bottom of the window. */
20311 else if (line_start_position == pos
20312 && pos == BUF_ZV (XBUFFER (w->buffer)))
20313 {
20314 maybe_next_line_p = 1;
20315 break;
20316 }
20317 else if (line_start_position > 0)
20318 {
20319 best_row = row;
20320 best_row_vpos = row_vpos;
20321 }
20322
20323 if (row->y + row->height >= yb)
20324 break;
20325
20326 ++row;
20327 ++row_vpos;
20328 }
20329
20330 /* Find the right column within BEST_ROW. */
20331 lastcol = 0;
20332 current_x = best_row->x;
20333 for (i = 0; i < best_row->used[TEXT_AREA]; i++)
20334 {
20335 struct glyph *glyph = best_row->glyphs[TEXT_AREA] + i;
20336 int charpos = glyph->charpos;
20337
20338 if (BUFFERP (glyph->object))
20339 {
20340 if (charpos == pos)
20341 {
20342 *hpos = i;
20343 *vpos = best_row_vpos;
20344 *x = current_x;
20345 *y = best_row->y;
20346 return 1;
20347 }
20348 else if (charpos > pos)
20349 break;
20350 }
20351 else if (EQ (glyph->object, stop))
20352 break;
20353
20354 if (charpos > 0)
20355 lastcol = i;
20356 current_x += glyph->pixel_width;
20357 }
20358
20359 /* If we're looking for the end of the buffer,
20360 and we didn't find it in the line we scanned,
20361 use the start of the following line. */
20362 if (maybe_next_line_p)
20363 {
20364 ++best_row;
20365 ++best_row_vpos;
20366 lastcol = 0;
20367 current_x = best_row->x;
20368 }
20369
20370 *vpos = best_row_vpos;
20371 *hpos = lastcol + 1;
20372 *x = current_x;
20373 *y = best_row->y;
20374 return 0;
20375 }
20376
20377 #endif /* not 1 */
20378
20379
20380 /* Find the position of the glyph for position POS in OBJECT in
20381 window W's current matrix, and return in *X, *Y the pixel
20382 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
20383
20384 RIGHT_P non-zero means return the position of the right edge of the
20385 glyph, RIGHT_P zero means return the left edge position.
20386
20387 If no glyph for POS exists in the matrix, return the position of
20388 the glyph with the next smaller position that is in the matrix, if
20389 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
20390 exists in the matrix, return the position of the glyph with the
20391 next larger position in OBJECT.
20392
20393 Value is non-zero if a glyph was found. */
20394
20395 static int
20396 fast_find_string_pos (w, pos, object, hpos, vpos, x, y, right_p)
20397 struct window *w;
20398 int pos;
20399 Lisp_Object object;
20400 int *hpos, *vpos, *x, *y;
20401 int right_p;
20402 {
20403 int yb = window_text_bottom_y (w);
20404 struct glyph_row *r;
20405 struct glyph *best_glyph = NULL;
20406 struct glyph_row *best_row = NULL;
20407 int best_x = 0;
20408
20409 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
20410 r->enabled_p && r->y < yb;
20411 ++r)
20412 {
20413 struct glyph *g = r->glyphs[TEXT_AREA];
20414 struct glyph *e = g + r->used[TEXT_AREA];
20415 int gx;
20416
20417 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
20418 if (EQ (g->object, object))
20419 {
20420 if (g->charpos == pos)
20421 {
20422 best_glyph = g;
20423 best_x = gx;
20424 best_row = r;
20425 goto found;
20426 }
20427 else if (best_glyph == NULL
20428 || ((abs (g->charpos - pos)
20429 < abs (best_glyph->charpos - pos))
20430 && (right_p
20431 ? g->charpos < pos
20432 : g->charpos > pos)))
20433 {
20434 best_glyph = g;
20435 best_x = gx;
20436 best_row = r;
20437 }
20438 }
20439 }
20440
20441 found:
20442
20443 if (best_glyph)
20444 {
20445 *x = best_x;
20446 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
20447
20448 if (right_p)
20449 {
20450 *x += best_glyph->pixel_width;
20451 ++*hpos;
20452 }
20453
20454 *y = best_row->y;
20455 *vpos = best_row - w->current_matrix->rows;
20456 }
20457
20458 return best_glyph != NULL;
20459 }
20460
20461
20462 /* See if position X, Y is within a hot-spot of an image. */
20463
20464 static int
20465 on_hot_spot_p (hot_spot, x, y)
20466 Lisp_Object hot_spot;
20467 int x, y;
20468 {
20469 if (!CONSP (hot_spot))
20470 return 0;
20471
20472 if (EQ (XCAR (hot_spot), Qrect))
20473 {
20474 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
20475 Lisp_Object rect = XCDR (hot_spot);
20476 Lisp_Object tem;
20477 if (!CONSP (rect))
20478 return 0;
20479 if (!CONSP (XCAR (rect)))
20480 return 0;
20481 if (!CONSP (XCDR (rect)))
20482 return 0;
20483 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
20484 return 0;
20485 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
20486 return 0;
20487 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
20488 return 0;
20489 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
20490 return 0;
20491 return 1;
20492 }
20493 else if (EQ (XCAR (hot_spot), Qcircle))
20494 {
20495 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
20496 Lisp_Object circ = XCDR (hot_spot);
20497 Lisp_Object lr, lx0, ly0;
20498 if (CONSP (circ)
20499 && CONSP (XCAR (circ))
20500 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
20501 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
20502 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
20503 {
20504 double r = XFLOATINT (lr);
20505 double dx = XINT (lx0) - x;
20506 double dy = XINT (ly0) - y;
20507 return (dx * dx + dy * dy <= r * r);
20508 }
20509 }
20510 else if (EQ (XCAR (hot_spot), Qpoly))
20511 {
20512 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
20513 if (VECTORP (XCDR (hot_spot)))
20514 {
20515 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
20516 Lisp_Object *poly = v->contents;
20517 int n = v->size;
20518 int i;
20519 int inside = 0;
20520 Lisp_Object lx, ly;
20521 int x0, y0;
20522
20523 /* Need an even number of coordinates, and at least 3 edges. */
20524 if (n < 6 || n & 1)
20525 return 0;
20526
20527 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
20528 If count is odd, we are inside polygon. Pixels on edges
20529 may or may not be included depending on actual geometry of the
20530 polygon. */
20531 if ((lx = poly[n-2], !INTEGERP (lx))
20532 || (ly = poly[n-1], !INTEGERP (lx)))
20533 return 0;
20534 x0 = XINT (lx), y0 = XINT (ly);
20535 for (i = 0; i < n; i += 2)
20536 {
20537 int x1 = x0, y1 = y0;
20538 if ((lx = poly[i], !INTEGERP (lx))
20539 || (ly = poly[i+1], !INTEGERP (ly)))
20540 return 0;
20541 x0 = XINT (lx), y0 = XINT (ly);
20542
20543 /* Does this segment cross the X line? */
20544 if (x0 >= x)
20545 {
20546 if (x1 >= x)
20547 continue;
20548 }
20549 else if (x1 < x)
20550 continue;
20551 if (y > y0 && y > y1)
20552 continue;
20553 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
20554 inside = !inside;
20555 }
20556 return inside;
20557 }
20558 }
20559 return 0;
20560 }
20561
20562 Lisp_Object
20563 find_hot_spot (map, x, y)
20564 Lisp_Object map;
20565 int x, y;
20566 {
20567 while (CONSP (map))
20568 {
20569 if (CONSP (XCAR (map))
20570 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
20571 return XCAR (map);
20572 map = XCDR (map);
20573 }
20574
20575 return Qnil;
20576 }
20577
20578 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
20579 3, 3, 0,
20580 doc: /* Lookup in image map MAP coordinates X and Y.
20581 An image map is an alist where each element has the format (AREA ID PLIST).
20582 An AREA is specified as either a rectangle, a circle, or a polygon:
20583 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
20584 pixel coordinates of the upper left and bottom right corners.
20585 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
20586 and the radius of the circle; r may be a float or integer.
20587 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
20588 vector describes one corner in the polygon.
20589 Returns the alist element for the first matching AREA in MAP. */)
20590 (map, x, y)
20591 Lisp_Object map;
20592 Lisp_Object x, y;
20593 {
20594 if (NILP (map))
20595 return Qnil;
20596
20597 CHECK_NUMBER (x);
20598 CHECK_NUMBER (y);
20599
20600 return find_hot_spot (map, XINT (x), XINT (y));
20601 }
20602
20603
20604 /* Display frame CURSOR, optionally using shape defined by POINTER. */
20605 static void
20606 define_frame_cursor1 (f, cursor, pointer)
20607 struct frame *f;
20608 Cursor cursor;
20609 Lisp_Object pointer;
20610 {
20611 if (!NILP (pointer))
20612 {
20613 if (EQ (pointer, Qarrow))
20614 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
20615 else if (EQ (pointer, Qhand))
20616 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
20617 else if (EQ (pointer, Qtext))
20618 cursor = FRAME_X_OUTPUT (f)->text_cursor;
20619 else if (EQ (pointer, intern ("hdrag")))
20620 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
20621 #ifdef HAVE_X_WINDOWS
20622 else if (EQ (pointer, intern ("vdrag")))
20623 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
20624 #endif
20625 else if (EQ (pointer, intern ("hourglass")))
20626 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
20627 else if (EQ (pointer, Qmodeline))
20628 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
20629 else
20630 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
20631 }
20632
20633 if (cursor != No_Cursor)
20634 FRAME_RIF (f)->define_frame_cursor (f, cursor);
20635 }
20636
20637 /* Take proper action when mouse has moved to the mode or header line
20638 or marginal area AREA of window W, x-position X and y-position Y.
20639 X is relative to the start of the text display area of W, so the
20640 width of bitmap areas and scroll bars must be subtracted to get a
20641 position relative to the start of the mode line. */
20642
20643 static void
20644 note_mode_line_or_margin_highlight (w, x, y, area)
20645 struct window *w;
20646 int x, y;
20647 enum window_part area;
20648 {
20649 struct frame *f = XFRAME (w->frame);
20650 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
20651 Cursor cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
20652 Lisp_Object pointer = Qnil;
20653 int charpos, dx, dy, width, height;
20654 Lisp_Object string, object = Qnil;
20655 Lisp_Object pos, help;
20656
20657 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
20658 string = mode_line_string (w, area, &x, &y, &charpos,
20659 &object, &dx, &dy, &width, &height);
20660 else
20661 {
20662 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
20663 string = marginal_area_string (w, area, &x, &y, &charpos,
20664 &object, &dx, &dy, &width, &height);
20665 }
20666
20667 help = Qnil;
20668
20669 if (IMAGEP (object))
20670 {
20671 Lisp_Object image_map, hotspot;
20672 if ((image_map = Fplist_get (XCDR (object), QCmap),
20673 !NILP (image_map))
20674 && (hotspot = find_hot_spot (image_map, dx, dy),
20675 CONSP (hotspot))
20676 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
20677 {
20678 Lisp_Object area_id, plist;
20679
20680 area_id = XCAR (hotspot);
20681 /* Could check AREA_ID to see if we enter/leave this hot-spot.
20682 If so, we could look for mouse-enter, mouse-leave
20683 properties in PLIST (and do something...). */
20684 if ((plist = XCDR (hotspot), CONSP (plist)))
20685 {
20686 pointer = Fplist_get (plist, Qpointer);
20687 if (NILP (pointer))
20688 pointer = Qhand;
20689 help = Fplist_get (plist, Qhelp_echo);
20690 if (!NILP (help))
20691 {
20692 help_echo_string = help;
20693 /* Is this correct? ++kfs */
20694 XSETWINDOW (help_echo_window, w);
20695 help_echo_object = w->buffer;
20696 help_echo_pos = charpos;
20697 }
20698 }
20699 if (NILP (pointer))
20700 pointer = Fplist_get (XCDR (object), QCpointer);
20701 }
20702 }
20703
20704 if (STRINGP (string))
20705 {
20706 pos = make_number (charpos);
20707 /* If we're on a string with `help-echo' text property, arrange
20708 for the help to be displayed. This is done by setting the
20709 global variable help_echo_string to the help string. */
20710 help = Fget_text_property (pos, Qhelp_echo, string);
20711 if (!NILP (help))
20712 {
20713 help_echo_string = help;
20714 XSETWINDOW (help_echo_window, w);
20715 help_echo_object = string;
20716 help_echo_pos = charpos;
20717 }
20718
20719 if (NILP (pointer))
20720 pointer = Fget_text_property (pos, Qpointer, string);
20721
20722 /* Change the mouse pointer according to what is under X/Y. */
20723 if (NILP (pointer) && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
20724 {
20725 Lisp_Object map;
20726 map = Fget_text_property (pos, Qlocal_map, string);
20727 if (!KEYMAPP (map))
20728 map = Fget_text_property (pos, Qkeymap, string);
20729 if (!KEYMAPP (map))
20730 cursor = dpyinfo->vertical_scroll_bar_cursor;
20731 }
20732 }
20733
20734 define_frame_cursor1 (f, cursor, pointer);
20735 }
20736
20737
20738 /* EXPORT:
20739 Take proper action when the mouse has moved to position X, Y on
20740 frame F as regards highlighting characters that have mouse-face
20741 properties. Also de-highlighting chars where the mouse was before.
20742 X and Y can be negative or out of range. */
20743
20744 void
20745 note_mouse_highlight (f, x, y)
20746 struct frame *f;
20747 int x, y;
20748 {
20749 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
20750 enum window_part part;
20751 Lisp_Object window;
20752 struct window *w;
20753 Cursor cursor = No_Cursor;
20754 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
20755 struct buffer *b;
20756
20757 /* When a menu is active, don't highlight because this looks odd. */
20758 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NTGUI)
20759 if (popup_activated ())
20760 return;
20761 #endif
20762
20763 if (NILP (Vmouse_highlight)
20764 || !f->glyphs_initialized_p)
20765 return;
20766
20767 dpyinfo->mouse_face_mouse_x = x;
20768 dpyinfo->mouse_face_mouse_y = y;
20769 dpyinfo->mouse_face_mouse_frame = f;
20770
20771 if (dpyinfo->mouse_face_defer)
20772 return;
20773
20774 if (gc_in_progress)
20775 {
20776 dpyinfo->mouse_face_deferred_gc = 1;
20777 return;
20778 }
20779
20780 /* Which window is that in? */
20781 window = window_from_coordinates (f, x, y, &part, 0, 0, 1);
20782
20783 /* If we were displaying active text in another window, clear that. */
20784 if (! EQ (window, dpyinfo->mouse_face_window))
20785 clear_mouse_face (dpyinfo);
20786
20787 /* Not on a window -> return. */
20788 if (!WINDOWP (window))
20789 return;
20790
20791 /* Reset help_echo_string. It will get recomputed below. */
20792 help_echo_string = Qnil;
20793
20794 /* Convert to window-relative pixel coordinates. */
20795 w = XWINDOW (window);
20796 frame_to_window_pixel_xy (w, &x, &y);
20797
20798 /* Handle tool-bar window differently since it doesn't display a
20799 buffer. */
20800 if (EQ (window, f->tool_bar_window))
20801 {
20802 note_tool_bar_highlight (f, x, y);
20803 return;
20804 }
20805
20806 /* Mouse is on the mode, header line or margin? */
20807 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
20808 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
20809 {
20810 note_mode_line_or_margin_highlight (w, x, y, part);
20811 return;
20812 }
20813
20814 if (part == ON_VERTICAL_BORDER)
20815 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
20816 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE)
20817 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
20818 else
20819 cursor = FRAME_X_OUTPUT (f)->text_cursor;
20820
20821 /* Are we in a window whose display is up to date?
20822 And verify the buffer's text has not changed. */
20823 b = XBUFFER (w->buffer);
20824 if (part == ON_TEXT
20825 && EQ (w->window_end_valid, w->buffer)
20826 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
20827 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
20828 {
20829 int hpos, vpos, pos, i, dx, dy, area;
20830 struct glyph *glyph;
20831 Lisp_Object object;
20832 Lisp_Object mouse_face = Qnil, overlay = Qnil, position;
20833 Lisp_Object *overlay_vec = NULL;
20834 int noverlays;
20835 struct buffer *obuf;
20836 int obegv, ozv, same_region;
20837
20838 /* Find the glyph under X/Y. */
20839 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
20840
20841 /* Look for :pointer property on image. */
20842 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
20843 {
20844 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
20845 if (img != NULL && IMAGEP (img->spec))
20846 {
20847 Lisp_Object image_map, hotspot;
20848 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
20849 !NILP (image_map))
20850 && (hotspot = find_hot_spot (image_map,
20851 glyph->slice.x + dx,
20852 glyph->slice.y + dy),
20853 CONSP (hotspot))
20854 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
20855 {
20856 Lisp_Object area_id, plist;
20857
20858 area_id = XCAR (hotspot);
20859 /* Could check AREA_ID to see if we enter/leave this hot-spot.
20860 If so, we could look for mouse-enter, mouse-leave
20861 properties in PLIST (and do something...). */
20862 if ((plist = XCDR (hotspot), CONSP (plist)))
20863 {
20864 pointer = Fplist_get (plist, Qpointer);
20865 if (NILP (pointer))
20866 pointer = Qhand;
20867 help_echo_string = Fplist_get (plist, Qhelp_echo);
20868 if (!NILP (help_echo_string))
20869 {
20870 help_echo_window = window;
20871 help_echo_object = glyph->object;
20872 help_echo_pos = glyph->charpos;
20873 }
20874 }
20875 }
20876 if (NILP (pointer))
20877 pointer = Fplist_get (XCDR (img->spec), QCpointer);
20878 }
20879 }
20880
20881 /* Clear mouse face if X/Y not over text. */
20882 if (glyph == NULL
20883 || area != TEXT_AREA
20884 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p)
20885 {
20886 if (clear_mouse_face (dpyinfo))
20887 cursor = No_Cursor;
20888 if (NILP (pointer))
20889 {
20890 if (area != TEXT_AREA)
20891 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
20892 else
20893 pointer = Vvoid_text_area_pointer;
20894 }
20895 goto set_cursor;
20896 }
20897
20898 pos = glyph->charpos;
20899 object = glyph->object;
20900 if (!STRINGP (object) && !BUFFERP (object))
20901 goto set_cursor;
20902
20903 /* If we get an out-of-range value, return now; avoid an error. */
20904 if (BUFFERP (object) && pos > BUF_Z (b))
20905 goto set_cursor;
20906
20907 /* Make the window's buffer temporarily current for
20908 overlays_at and compute_char_face. */
20909 obuf = current_buffer;
20910 current_buffer = b;
20911 obegv = BEGV;
20912 ozv = ZV;
20913 BEGV = BEG;
20914 ZV = Z;
20915
20916 /* Is this char mouse-active or does it have help-echo? */
20917 position = make_number (pos);
20918
20919 if (BUFFERP (object))
20920 {
20921 /* Put all the overlays we want in a vector in overlay_vec. */
20922 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
20923 /* Sort overlays into increasing priority order. */
20924 noverlays = sort_overlays (overlay_vec, noverlays, w);
20925 }
20926 else
20927 noverlays = 0;
20928
20929 same_region = (EQ (window, dpyinfo->mouse_face_window)
20930 && vpos >= dpyinfo->mouse_face_beg_row
20931 && vpos <= dpyinfo->mouse_face_end_row
20932 && (vpos > dpyinfo->mouse_face_beg_row
20933 || hpos >= dpyinfo->mouse_face_beg_col)
20934 && (vpos < dpyinfo->mouse_face_end_row
20935 || hpos < dpyinfo->mouse_face_end_col
20936 || dpyinfo->mouse_face_past_end));
20937
20938 if (same_region)
20939 cursor = No_Cursor;
20940
20941 /* Check mouse-face highlighting. */
20942 if (! same_region
20943 /* If there exists an overlay with mouse-face overlapping
20944 the one we are currently highlighting, we have to
20945 check if we enter the overlapping overlay, and then
20946 highlight only that. */
20947 || (OVERLAYP (dpyinfo->mouse_face_overlay)
20948 && mouse_face_overlay_overlaps (dpyinfo->mouse_face_overlay)))
20949 {
20950 /* Find the highest priority overlay that has a mouse-face
20951 property. */
20952 overlay = Qnil;
20953 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
20954 {
20955 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
20956 if (!NILP (mouse_face))
20957 overlay = overlay_vec[i];
20958 }
20959
20960 /* If we're actually highlighting the same overlay as
20961 before, there's no need to do that again. */
20962 if (!NILP (overlay)
20963 && EQ (overlay, dpyinfo->mouse_face_overlay))
20964 goto check_help_echo;
20965
20966 dpyinfo->mouse_face_overlay = overlay;
20967
20968 /* Clear the display of the old active region, if any. */
20969 if (clear_mouse_face (dpyinfo))
20970 cursor = No_Cursor;
20971
20972 /* If no overlay applies, get a text property. */
20973 if (NILP (overlay))
20974 mouse_face = Fget_text_property (position, Qmouse_face, object);
20975
20976 /* Handle the overlay case. */
20977 if (!NILP (overlay))
20978 {
20979 /* Find the range of text around this char that
20980 should be active. */
20981 Lisp_Object before, after;
20982 int ignore;
20983
20984 before = Foverlay_start (overlay);
20985 after = Foverlay_end (overlay);
20986 /* Record this as the current active region. */
20987 fast_find_position (w, XFASTINT (before),
20988 &dpyinfo->mouse_face_beg_col,
20989 &dpyinfo->mouse_face_beg_row,
20990 &dpyinfo->mouse_face_beg_x,
20991 &dpyinfo->mouse_face_beg_y, Qnil);
20992
20993 dpyinfo->mouse_face_past_end
20994 = !fast_find_position (w, XFASTINT (after),
20995 &dpyinfo->mouse_face_end_col,
20996 &dpyinfo->mouse_face_end_row,
20997 &dpyinfo->mouse_face_end_x,
20998 &dpyinfo->mouse_face_end_y, Qnil);
20999 dpyinfo->mouse_face_window = window;
21000
21001 dpyinfo->mouse_face_face_id
21002 = face_at_buffer_position (w, pos, 0, 0,
21003 &ignore, pos + 1,
21004 !dpyinfo->mouse_face_hidden);
21005
21006 /* Display it as active. */
21007 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
21008 cursor = No_Cursor;
21009 }
21010 /* Handle the text property case. */
21011 else if (!NILP (mouse_face) && BUFFERP (object))
21012 {
21013 /* Find the range of text around this char that
21014 should be active. */
21015 Lisp_Object before, after, beginning, end;
21016 int ignore;
21017
21018 beginning = Fmarker_position (w->start);
21019 end = make_number (BUF_Z (XBUFFER (object))
21020 - XFASTINT (w->window_end_pos));
21021 before
21022 = Fprevious_single_property_change (make_number (pos + 1),
21023 Qmouse_face,
21024 object, beginning);
21025 after
21026 = Fnext_single_property_change (position, Qmouse_face,
21027 object, end);
21028
21029 /* Record this as the current active region. */
21030 fast_find_position (w, XFASTINT (before),
21031 &dpyinfo->mouse_face_beg_col,
21032 &dpyinfo->mouse_face_beg_row,
21033 &dpyinfo->mouse_face_beg_x,
21034 &dpyinfo->mouse_face_beg_y, Qnil);
21035 dpyinfo->mouse_face_past_end
21036 = !fast_find_position (w, XFASTINT (after),
21037 &dpyinfo->mouse_face_end_col,
21038 &dpyinfo->mouse_face_end_row,
21039 &dpyinfo->mouse_face_end_x,
21040 &dpyinfo->mouse_face_end_y, Qnil);
21041 dpyinfo->mouse_face_window = window;
21042
21043 if (BUFFERP (object))
21044 dpyinfo->mouse_face_face_id
21045 = face_at_buffer_position (w, pos, 0, 0,
21046 &ignore, pos + 1,
21047 !dpyinfo->mouse_face_hidden);
21048
21049 /* Display it as active. */
21050 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
21051 cursor = No_Cursor;
21052 }
21053 else if (!NILP (mouse_face) && STRINGP (object))
21054 {
21055 Lisp_Object b, e;
21056 int ignore;
21057
21058 b = Fprevious_single_property_change (make_number (pos + 1),
21059 Qmouse_face,
21060 object, Qnil);
21061 e = Fnext_single_property_change (position, Qmouse_face,
21062 object, Qnil);
21063 if (NILP (b))
21064 b = make_number (0);
21065 if (NILP (e))
21066 e = make_number (SCHARS (object) - 1);
21067 fast_find_string_pos (w, XINT (b), object,
21068 &dpyinfo->mouse_face_beg_col,
21069 &dpyinfo->mouse_face_beg_row,
21070 &dpyinfo->mouse_face_beg_x,
21071 &dpyinfo->mouse_face_beg_y, 0);
21072 fast_find_string_pos (w, XINT (e), object,
21073 &dpyinfo->mouse_face_end_col,
21074 &dpyinfo->mouse_face_end_row,
21075 &dpyinfo->mouse_face_end_x,
21076 &dpyinfo->mouse_face_end_y, 1);
21077 dpyinfo->mouse_face_past_end = 0;
21078 dpyinfo->mouse_face_window = window;
21079 dpyinfo->mouse_face_face_id
21080 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
21081 glyph->face_id, 1);
21082 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
21083 cursor = No_Cursor;
21084 }
21085 else if (STRINGP (object) && NILP (mouse_face))
21086 {
21087 /* A string which doesn't have mouse-face, but
21088 the text ``under'' it might have. */
21089 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
21090 int start = MATRIX_ROW_START_CHARPOS (r);
21091
21092 pos = string_buffer_position (w, object, start);
21093 if (pos > 0)
21094 mouse_face = get_char_property_and_overlay (make_number (pos),
21095 Qmouse_face,
21096 w->buffer,
21097 &overlay);
21098 if (!NILP (mouse_face) && !NILP (overlay))
21099 {
21100 Lisp_Object before = Foverlay_start (overlay);
21101 Lisp_Object after = Foverlay_end (overlay);
21102 int ignore;
21103
21104 /* Note that we might not be able to find position
21105 BEFORE in the glyph matrix if the overlay is
21106 entirely covered by a `display' property. In
21107 this case, we overshoot. So let's stop in
21108 the glyph matrix before glyphs for OBJECT. */
21109 fast_find_position (w, XFASTINT (before),
21110 &dpyinfo->mouse_face_beg_col,
21111 &dpyinfo->mouse_face_beg_row,
21112 &dpyinfo->mouse_face_beg_x,
21113 &dpyinfo->mouse_face_beg_y,
21114 object);
21115
21116 dpyinfo->mouse_face_past_end
21117 = !fast_find_position (w, XFASTINT (after),
21118 &dpyinfo->mouse_face_end_col,
21119 &dpyinfo->mouse_face_end_row,
21120 &dpyinfo->mouse_face_end_x,
21121 &dpyinfo->mouse_face_end_y,
21122 Qnil);
21123 dpyinfo->mouse_face_window = window;
21124 dpyinfo->mouse_face_face_id
21125 = face_at_buffer_position (w, pos, 0, 0,
21126 &ignore, pos + 1,
21127 !dpyinfo->mouse_face_hidden);
21128
21129 /* Display it as active. */
21130 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
21131 cursor = No_Cursor;
21132 }
21133 }
21134 }
21135
21136 check_help_echo:
21137
21138 /* Look for a `help-echo' property. */
21139 if (NILP (help_echo_string)) {
21140 Lisp_Object help, overlay;
21141
21142 /* Check overlays first. */
21143 help = overlay = Qnil;
21144 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
21145 {
21146 overlay = overlay_vec[i];
21147 help = Foverlay_get (overlay, Qhelp_echo);
21148 }
21149
21150 if (!NILP (help))
21151 {
21152 help_echo_string = help;
21153 help_echo_window = window;
21154 help_echo_object = overlay;
21155 help_echo_pos = pos;
21156 }
21157 else
21158 {
21159 Lisp_Object object = glyph->object;
21160 int charpos = glyph->charpos;
21161
21162 /* Try text properties. */
21163 if (STRINGP (object)
21164 && charpos >= 0
21165 && charpos < SCHARS (object))
21166 {
21167 help = Fget_text_property (make_number (charpos),
21168 Qhelp_echo, object);
21169 if (NILP (help))
21170 {
21171 /* If the string itself doesn't specify a help-echo,
21172 see if the buffer text ``under'' it does. */
21173 struct glyph_row *r
21174 = MATRIX_ROW (w->current_matrix, vpos);
21175 int start = MATRIX_ROW_START_CHARPOS (r);
21176 int pos = string_buffer_position (w, object, start);
21177 if (pos > 0)
21178 {
21179 help = Fget_char_property (make_number (pos),
21180 Qhelp_echo, w->buffer);
21181 if (!NILP (help))
21182 {
21183 charpos = pos;
21184 object = w->buffer;
21185 }
21186 }
21187 }
21188 }
21189 else if (BUFFERP (object)
21190 && charpos >= BEGV
21191 && charpos < ZV)
21192 help = Fget_text_property (make_number (charpos), Qhelp_echo,
21193 object);
21194
21195 if (!NILP (help))
21196 {
21197 help_echo_string = help;
21198 help_echo_window = window;
21199 help_echo_object = object;
21200 help_echo_pos = charpos;
21201 }
21202 }
21203 }
21204
21205 /* Look for a `pointer' property. */
21206 if (NILP (pointer))
21207 {
21208 /* Check overlays first. */
21209 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
21210 pointer = Foverlay_get (overlay_vec[i], Qpointer);
21211
21212 if (NILP (pointer))
21213 {
21214 Lisp_Object object = glyph->object;
21215 int charpos = glyph->charpos;
21216
21217 /* Try text properties. */
21218 if (STRINGP (object)
21219 && charpos >= 0
21220 && charpos < SCHARS (object))
21221 {
21222 pointer = Fget_text_property (make_number (charpos),
21223 Qpointer, object);
21224 if (NILP (pointer))
21225 {
21226 /* If the string itself doesn't specify a pointer,
21227 see if the buffer text ``under'' it does. */
21228 struct glyph_row *r
21229 = MATRIX_ROW (w->current_matrix, vpos);
21230 int start = MATRIX_ROW_START_CHARPOS (r);
21231 int pos = string_buffer_position (w, object, start);
21232 if (pos > 0)
21233 pointer = Fget_char_property (make_number (pos),
21234 Qpointer, w->buffer);
21235 }
21236 }
21237 else if (BUFFERP (object)
21238 && charpos >= BEGV
21239 && charpos < ZV)
21240 pointer = Fget_text_property (make_number (charpos),
21241 Qpointer, object);
21242 }
21243 }
21244
21245 BEGV = obegv;
21246 ZV = ozv;
21247 current_buffer = obuf;
21248 }
21249
21250 set_cursor:
21251
21252 define_frame_cursor1 (f, cursor, pointer);
21253 }
21254
21255
21256 /* EXPORT for RIF:
21257 Clear any mouse-face on window W. This function is part of the
21258 redisplay interface, and is called from try_window_id and similar
21259 functions to ensure the mouse-highlight is off. */
21260
21261 void
21262 x_clear_window_mouse_face (w)
21263 struct window *w;
21264 {
21265 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
21266 Lisp_Object window;
21267
21268 BLOCK_INPUT;
21269 XSETWINDOW (window, w);
21270 if (EQ (window, dpyinfo->mouse_face_window))
21271 clear_mouse_face (dpyinfo);
21272 UNBLOCK_INPUT;
21273 }
21274
21275
21276 /* EXPORT:
21277 Just discard the mouse face information for frame F, if any.
21278 This is used when the size of F is changed. */
21279
21280 void
21281 cancel_mouse_face (f)
21282 struct frame *f;
21283 {
21284 Lisp_Object window;
21285 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
21286
21287 window = dpyinfo->mouse_face_window;
21288 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
21289 {
21290 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
21291 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
21292 dpyinfo->mouse_face_window = Qnil;
21293 }
21294 }
21295
21296
21297 #endif /* HAVE_WINDOW_SYSTEM */
21298
21299 \f
21300 /***********************************************************************
21301 Exposure Events
21302 ***********************************************************************/
21303
21304 #ifdef HAVE_WINDOW_SYSTEM
21305
21306 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
21307 which intersects rectangle R. R is in window-relative coordinates. */
21308
21309 static void
21310 expose_area (w, row, r, area)
21311 struct window *w;
21312 struct glyph_row *row;
21313 XRectangle *r;
21314 enum glyph_row_area area;
21315 {
21316 struct glyph *first = row->glyphs[area];
21317 struct glyph *end = row->glyphs[area] + row->used[area];
21318 struct glyph *last;
21319 int first_x, start_x, x;
21320
21321 if (area == TEXT_AREA && row->fill_line_p)
21322 /* If row extends face to end of line write the whole line. */
21323 draw_glyphs (w, 0, row, area,
21324 0, row->used[area],
21325 DRAW_NORMAL_TEXT, 0);
21326 else
21327 {
21328 /* Set START_X to the window-relative start position for drawing glyphs of
21329 AREA. The first glyph of the text area can be partially visible.
21330 The first glyphs of other areas cannot. */
21331 start_x = window_box_left_offset (w, area);
21332 x = start_x;
21333 if (area == TEXT_AREA)
21334 x += row->x;
21335
21336 /* Find the first glyph that must be redrawn. */
21337 while (first < end
21338 && x + first->pixel_width < r->x)
21339 {
21340 x += first->pixel_width;
21341 ++first;
21342 }
21343
21344 /* Find the last one. */
21345 last = first;
21346 first_x = x;
21347 while (last < end
21348 && x < r->x + r->width)
21349 {
21350 x += last->pixel_width;
21351 ++last;
21352 }
21353
21354 /* Repaint. */
21355 if (last > first)
21356 draw_glyphs (w, first_x - start_x, row, area,
21357 first - row->glyphs[area], last - row->glyphs[area],
21358 DRAW_NORMAL_TEXT, 0);
21359 }
21360 }
21361
21362
21363 /* Redraw the parts of the glyph row ROW on window W intersecting
21364 rectangle R. R is in window-relative coordinates. Value is
21365 non-zero if mouse-face was overwritten. */
21366
21367 static int
21368 expose_line (w, row, r)
21369 struct window *w;
21370 struct glyph_row *row;
21371 XRectangle *r;
21372 {
21373 xassert (row->enabled_p);
21374
21375 if (row->mode_line_p || w->pseudo_window_p)
21376 draw_glyphs (w, 0, row, TEXT_AREA,
21377 0, row->used[TEXT_AREA],
21378 DRAW_NORMAL_TEXT, 0);
21379 else
21380 {
21381 if (row->used[LEFT_MARGIN_AREA])
21382 expose_area (w, row, r, LEFT_MARGIN_AREA);
21383 if (row->used[TEXT_AREA])
21384 expose_area (w, row, r, TEXT_AREA);
21385 if (row->used[RIGHT_MARGIN_AREA])
21386 expose_area (w, row, r, RIGHT_MARGIN_AREA);
21387 draw_row_fringe_bitmaps (w, row);
21388 }
21389
21390 return row->mouse_face_p;
21391 }
21392
21393
21394 /* Redraw those parts of glyphs rows during expose event handling that
21395 overlap other rows. Redrawing of an exposed line writes over parts
21396 of lines overlapping that exposed line; this function fixes that.
21397
21398 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
21399 row in W's current matrix that is exposed and overlaps other rows.
21400 LAST_OVERLAPPING_ROW is the last such row. */
21401
21402 static void
21403 expose_overlaps (w, first_overlapping_row, last_overlapping_row)
21404 struct window *w;
21405 struct glyph_row *first_overlapping_row;
21406 struct glyph_row *last_overlapping_row;
21407 {
21408 struct glyph_row *row;
21409
21410 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
21411 if (row->overlapping_p)
21412 {
21413 xassert (row->enabled_p && !row->mode_line_p);
21414
21415 if (row->used[LEFT_MARGIN_AREA])
21416 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA);
21417
21418 if (row->used[TEXT_AREA])
21419 x_fix_overlapping_area (w, row, TEXT_AREA);
21420
21421 if (row->used[RIGHT_MARGIN_AREA])
21422 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA);
21423 }
21424 }
21425
21426
21427 /* Return non-zero if W's cursor intersects rectangle R. */
21428
21429 static int
21430 phys_cursor_in_rect_p (w, r)
21431 struct window *w;
21432 XRectangle *r;
21433 {
21434 XRectangle cr, result;
21435 struct glyph *cursor_glyph;
21436
21437 cursor_glyph = get_phys_cursor_glyph (w);
21438 if (cursor_glyph)
21439 {
21440 /* r is relative to W's box, but w->phys_cursor.x is relative
21441 to left edge of W's TEXT area. Adjust it. */
21442 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
21443 cr.y = w->phys_cursor.y;
21444 cr.width = cursor_glyph->pixel_width;
21445 cr.height = w->phys_cursor_height;
21446 /* ++KFS: W32 version used W32-specific IntersectRect here, but
21447 I assume the effect is the same -- and this is portable. */
21448 return x_intersect_rectangles (&cr, r, &result);
21449 }
21450 /* If we don't understand the format, pretend we're not in the hot-spot. */
21451 return 0;
21452 }
21453
21454
21455 /* EXPORT:
21456 Draw a vertical window border to the right of window W if W doesn't
21457 have vertical scroll bars. */
21458
21459 void
21460 x_draw_vertical_border (w)
21461 struct window *w;
21462 {
21463 struct frame *f = XFRAME (WINDOW_FRAME (w));
21464
21465 /* We could do better, if we knew what type of scroll-bar the adjacent
21466 windows (on either side) have... But we don't :-(
21467 However, I think this works ok. ++KFS 2003-04-25 */
21468
21469 /* Redraw borders between horizontally adjacent windows. Don't
21470 do it for frames with vertical scroll bars because either the
21471 right scroll bar of a window, or the left scroll bar of its
21472 neighbor will suffice as a border. */
21473 if (!WINDOW_RIGHTMOST_P (w)
21474 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
21475 {
21476 int x0, x1, y0, y1;
21477
21478 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
21479 y1 -= 1;
21480
21481 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
21482 }
21483 else if (!WINDOW_LEFTMOST_P (w)
21484 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
21485 {
21486 int x0, x1, y0, y1;
21487
21488 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
21489 y1 -= 1;
21490
21491 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
21492 }
21493 }
21494
21495
21496 /* Redraw the part of window W intersection rectangle FR. Pixel
21497 coordinates in FR are frame-relative. Call this function with
21498 input blocked. Value is non-zero if the exposure overwrites
21499 mouse-face. */
21500
21501 static int
21502 expose_window (w, fr)
21503 struct window *w;
21504 XRectangle *fr;
21505 {
21506 struct frame *f = XFRAME (w->frame);
21507 XRectangle wr, r;
21508 int mouse_face_overwritten_p = 0;
21509
21510 /* If window is not yet fully initialized, do nothing. This can
21511 happen when toolkit scroll bars are used and a window is split.
21512 Reconfiguring the scroll bar will generate an expose for a newly
21513 created window. */
21514 if (w->current_matrix == NULL)
21515 return 0;
21516
21517 /* When we're currently updating the window, display and current
21518 matrix usually don't agree. Arrange for a thorough display
21519 later. */
21520 if (w == updated_window)
21521 {
21522 SET_FRAME_GARBAGED (f);
21523 return 0;
21524 }
21525
21526 /* Frame-relative pixel rectangle of W. */
21527 wr.x = WINDOW_LEFT_EDGE_X (w);
21528 wr.y = WINDOW_TOP_EDGE_Y (w);
21529 wr.width = WINDOW_TOTAL_WIDTH (w);
21530 wr.height = WINDOW_TOTAL_HEIGHT (w);
21531
21532 if (x_intersect_rectangles (fr, &wr, &r))
21533 {
21534 int yb = window_text_bottom_y (w);
21535 struct glyph_row *row;
21536 int cursor_cleared_p;
21537 struct glyph_row *first_overlapping_row, *last_overlapping_row;
21538
21539 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
21540 r.x, r.y, r.width, r.height));
21541
21542 /* Convert to window coordinates. */
21543 r.x -= WINDOW_LEFT_EDGE_X (w);
21544 r.y -= WINDOW_TOP_EDGE_Y (w);
21545
21546 /* Turn off the cursor. */
21547 if (!w->pseudo_window_p
21548 && phys_cursor_in_rect_p (w, &r))
21549 {
21550 x_clear_cursor (w);
21551 cursor_cleared_p = 1;
21552 }
21553 else
21554 cursor_cleared_p = 0;
21555
21556 /* Update lines intersecting rectangle R. */
21557 first_overlapping_row = last_overlapping_row = NULL;
21558 for (row = w->current_matrix->rows;
21559 row->enabled_p;
21560 ++row)
21561 {
21562 int y0 = row->y;
21563 int y1 = MATRIX_ROW_BOTTOM_Y (row);
21564
21565 if ((y0 >= r.y && y0 < r.y + r.height)
21566 || (y1 > r.y && y1 < r.y + r.height)
21567 || (r.y >= y0 && r.y < y1)
21568 || (r.y + r.height > y0 && r.y + r.height < y1))
21569 {
21570 if (row->overlapping_p)
21571 {
21572 if (first_overlapping_row == NULL)
21573 first_overlapping_row = row;
21574 last_overlapping_row = row;
21575 }
21576
21577 if (expose_line (w, row, &r))
21578 mouse_face_overwritten_p = 1;
21579 }
21580
21581 if (y1 >= yb)
21582 break;
21583 }
21584
21585 /* Display the mode line if there is one. */
21586 if (WINDOW_WANTS_MODELINE_P (w)
21587 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
21588 row->enabled_p)
21589 && row->y < r.y + r.height)
21590 {
21591 if (expose_line (w, row, &r))
21592 mouse_face_overwritten_p = 1;
21593 }
21594
21595 if (!w->pseudo_window_p)
21596 {
21597 /* Fix the display of overlapping rows. */
21598 if (first_overlapping_row)
21599 expose_overlaps (w, first_overlapping_row, last_overlapping_row);
21600
21601 /* Draw border between windows. */
21602 x_draw_vertical_border (w);
21603
21604 /* Turn the cursor on again. */
21605 if (cursor_cleared_p)
21606 update_window_cursor (w, 1);
21607 }
21608 }
21609
21610 #ifdef HAVE_CARBON
21611 /* Display scroll bar for this window. */
21612 if (!NILP (w->vertical_scroll_bar))
21613 {
21614 /* ++KFS:
21615 If this doesn't work here (maybe some header files are missing),
21616 make a function in macterm.c and call it to do the job! */
21617 ControlHandle ch
21618 = SCROLL_BAR_CONTROL_HANDLE (XSCROLL_BAR (w->vertical_scroll_bar));
21619
21620 Draw1Control (ch);
21621 }
21622 #endif
21623
21624 return mouse_face_overwritten_p;
21625 }
21626
21627
21628
21629 /* Redraw (parts) of all windows in the window tree rooted at W that
21630 intersect R. R contains frame pixel coordinates. Value is
21631 non-zero if the exposure overwrites mouse-face. */
21632
21633 static int
21634 expose_window_tree (w, r)
21635 struct window *w;
21636 XRectangle *r;
21637 {
21638 struct frame *f = XFRAME (w->frame);
21639 int mouse_face_overwritten_p = 0;
21640
21641 while (w && !FRAME_GARBAGED_P (f))
21642 {
21643 if (!NILP (w->hchild))
21644 mouse_face_overwritten_p
21645 |= expose_window_tree (XWINDOW (w->hchild), r);
21646 else if (!NILP (w->vchild))
21647 mouse_face_overwritten_p
21648 |= expose_window_tree (XWINDOW (w->vchild), r);
21649 else
21650 mouse_face_overwritten_p |= expose_window (w, r);
21651
21652 w = NILP (w->next) ? NULL : XWINDOW (w->next);
21653 }
21654
21655 return mouse_face_overwritten_p;
21656 }
21657
21658
21659 /* EXPORT:
21660 Redisplay an exposed area of frame F. X and Y are the upper-left
21661 corner of the exposed rectangle. W and H are width and height of
21662 the exposed area. All are pixel values. W or H zero means redraw
21663 the entire frame. */
21664
21665 void
21666 expose_frame (f, x, y, w, h)
21667 struct frame *f;
21668 int x, y, w, h;
21669 {
21670 XRectangle r;
21671 int mouse_face_overwritten_p = 0;
21672
21673 TRACE ((stderr, "expose_frame "));
21674
21675 /* No need to redraw if frame will be redrawn soon. */
21676 if (FRAME_GARBAGED_P (f))
21677 {
21678 TRACE ((stderr, " garbaged\n"));
21679 return;
21680 }
21681
21682 #ifdef HAVE_CARBON
21683 /* MAC_TODO: this is a kludge, but if scroll bars are not activated
21684 or deactivated here, for unknown reasons, activated scroll bars
21685 are shown in deactivated frames in some instances. */
21686 if (f == FRAME_MAC_DISPLAY_INFO (f)->x_focus_frame)
21687 activate_scroll_bars (f);
21688 else
21689 deactivate_scroll_bars (f);
21690 #endif
21691
21692 /* If basic faces haven't been realized yet, there is no point in
21693 trying to redraw anything. This can happen when we get an expose
21694 event while Emacs is starting, e.g. by moving another window. */
21695 if (FRAME_FACE_CACHE (f) == NULL
21696 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
21697 {
21698 TRACE ((stderr, " no faces\n"));
21699 return;
21700 }
21701
21702 if (w == 0 || h == 0)
21703 {
21704 r.x = r.y = 0;
21705 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
21706 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
21707 }
21708 else
21709 {
21710 r.x = x;
21711 r.y = y;
21712 r.width = w;
21713 r.height = h;
21714 }
21715
21716 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
21717 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
21718
21719 if (WINDOWP (f->tool_bar_window))
21720 mouse_face_overwritten_p
21721 |= expose_window (XWINDOW (f->tool_bar_window), &r);
21722
21723 #ifdef HAVE_X_WINDOWS
21724 #ifndef MSDOS
21725 #ifndef USE_X_TOOLKIT
21726 if (WINDOWP (f->menu_bar_window))
21727 mouse_face_overwritten_p
21728 |= expose_window (XWINDOW (f->menu_bar_window), &r);
21729 #endif /* not USE_X_TOOLKIT */
21730 #endif
21731 #endif
21732
21733 /* Some window managers support a focus-follows-mouse style with
21734 delayed raising of frames. Imagine a partially obscured frame,
21735 and moving the mouse into partially obscured mouse-face on that
21736 frame. The visible part of the mouse-face will be highlighted,
21737 then the WM raises the obscured frame. With at least one WM, KDE
21738 2.1, Emacs is not getting any event for the raising of the frame
21739 (even tried with SubstructureRedirectMask), only Expose events.
21740 These expose events will draw text normally, i.e. not
21741 highlighted. Which means we must redo the highlight here.
21742 Subsume it under ``we love X''. --gerd 2001-08-15 */
21743 /* Included in Windows version because Windows most likely does not
21744 do the right thing if any third party tool offers
21745 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
21746 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
21747 {
21748 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
21749 if (f == dpyinfo->mouse_face_mouse_frame)
21750 {
21751 int x = dpyinfo->mouse_face_mouse_x;
21752 int y = dpyinfo->mouse_face_mouse_y;
21753 clear_mouse_face (dpyinfo);
21754 note_mouse_highlight (f, x, y);
21755 }
21756 }
21757 }
21758
21759
21760 /* EXPORT:
21761 Determine the intersection of two rectangles R1 and R2. Return
21762 the intersection in *RESULT. Value is non-zero if RESULT is not
21763 empty. */
21764
21765 int
21766 x_intersect_rectangles (r1, r2, result)
21767 XRectangle *r1, *r2, *result;
21768 {
21769 XRectangle *left, *right;
21770 XRectangle *upper, *lower;
21771 int intersection_p = 0;
21772
21773 /* Rearrange so that R1 is the left-most rectangle. */
21774 if (r1->x < r2->x)
21775 left = r1, right = r2;
21776 else
21777 left = r2, right = r1;
21778
21779 /* X0 of the intersection is right.x0, if this is inside R1,
21780 otherwise there is no intersection. */
21781 if (right->x <= left->x + left->width)
21782 {
21783 result->x = right->x;
21784
21785 /* The right end of the intersection is the minimum of the
21786 the right ends of left and right. */
21787 result->width = (min (left->x + left->width, right->x + right->width)
21788 - result->x);
21789
21790 /* Same game for Y. */
21791 if (r1->y < r2->y)
21792 upper = r1, lower = r2;
21793 else
21794 upper = r2, lower = r1;
21795
21796 /* The upper end of the intersection is lower.y0, if this is inside
21797 of upper. Otherwise, there is no intersection. */
21798 if (lower->y <= upper->y + upper->height)
21799 {
21800 result->y = lower->y;
21801
21802 /* The lower end of the intersection is the minimum of the lower
21803 ends of upper and lower. */
21804 result->height = (min (lower->y + lower->height,
21805 upper->y + upper->height)
21806 - result->y);
21807 intersection_p = 1;
21808 }
21809 }
21810
21811 return intersection_p;
21812 }
21813
21814 #endif /* HAVE_WINDOW_SYSTEM */
21815
21816 \f
21817 /***********************************************************************
21818 Initialization
21819 ***********************************************************************/
21820
21821 void
21822 syms_of_xdisp ()
21823 {
21824 Vwith_echo_area_save_vector = Qnil;
21825 staticpro (&Vwith_echo_area_save_vector);
21826
21827 Vmessage_stack = Qnil;
21828 staticpro (&Vmessage_stack);
21829
21830 Qinhibit_redisplay = intern ("inhibit-redisplay");
21831 staticpro (&Qinhibit_redisplay);
21832
21833 message_dolog_marker1 = Fmake_marker ();
21834 staticpro (&message_dolog_marker1);
21835 message_dolog_marker2 = Fmake_marker ();
21836 staticpro (&message_dolog_marker2);
21837 message_dolog_marker3 = Fmake_marker ();
21838 staticpro (&message_dolog_marker3);
21839
21840 #if GLYPH_DEBUG
21841 defsubr (&Sdump_frame_glyph_matrix);
21842 defsubr (&Sdump_glyph_matrix);
21843 defsubr (&Sdump_glyph_row);
21844 defsubr (&Sdump_tool_bar_row);
21845 defsubr (&Strace_redisplay);
21846 defsubr (&Strace_to_stderr);
21847 #endif
21848 #ifdef HAVE_WINDOW_SYSTEM
21849 defsubr (&Stool_bar_lines_needed);
21850 defsubr (&Slookup_image_map);
21851 #endif
21852 defsubr (&Sformat_mode_line);
21853
21854 staticpro (&Qmenu_bar_update_hook);
21855 Qmenu_bar_update_hook = intern ("menu-bar-update-hook");
21856
21857 staticpro (&Qoverriding_terminal_local_map);
21858 Qoverriding_terminal_local_map = intern ("overriding-terminal-local-map");
21859
21860 staticpro (&Qoverriding_local_map);
21861 Qoverriding_local_map = intern ("overriding-local-map");
21862
21863 staticpro (&Qwindow_scroll_functions);
21864 Qwindow_scroll_functions = intern ("window-scroll-functions");
21865
21866 staticpro (&Qredisplay_end_trigger_functions);
21867 Qredisplay_end_trigger_functions = intern ("redisplay-end-trigger-functions");
21868
21869 staticpro (&Qinhibit_point_motion_hooks);
21870 Qinhibit_point_motion_hooks = intern ("inhibit-point-motion-hooks");
21871
21872 QCdata = intern (":data");
21873 staticpro (&QCdata);
21874 Qdisplay = intern ("display");
21875 staticpro (&Qdisplay);
21876 Qspace_width = intern ("space-width");
21877 staticpro (&Qspace_width);
21878 Qraise = intern ("raise");
21879 staticpro (&Qraise);
21880 Qslice = intern ("slice");
21881 staticpro (&Qslice);
21882 Qspace = intern ("space");
21883 staticpro (&Qspace);
21884 Qmargin = intern ("margin");
21885 staticpro (&Qmargin);
21886 Qpointer = intern ("pointer");
21887 staticpro (&Qpointer);
21888 Qleft_margin = intern ("left-margin");
21889 staticpro (&Qleft_margin);
21890 Qright_margin = intern ("right-margin");
21891 staticpro (&Qright_margin);
21892 Qcenter = intern ("center");
21893 staticpro (&Qcenter);
21894 Qline_height = intern ("line-height");
21895 staticpro (&Qline_height);
21896 Qtotal = intern ("total");
21897 staticpro (&Qtotal);
21898 QCalign_to = intern (":align-to");
21899 staticpro (&QCalign_to);
21900 QCrelative_width = intern (":relative-width");
21901 staticpro (&QCrelative_width);
21902 QCrelative_height = intern (":relative-height");
21903 staticpro (&QCrelative_height);
21904 QCeval = intern (":eval");
21905 staticpro (&QCeval);
21906 QCpropertize = intern (":propertize");
21907 staticpro (&QCpropertize);
21908 QCfile = intern (":file");
21909 staticpro (&QCfile);
21910 Qfontified = intern ("fontified");
21911 staticpro (&Qfontified);
21912 Qfontification_functions = intern ("fontification-functions");
21913 staticpro (&Qfontification_functions);
21914 Qtrailing_whitespace = intern ("trailing-whitespace");
21915 staticpro (&Qtrailing_whitespace);
21916 Qimage = intern ("image");
21917 staticpro (&Qimage);
21918 QCmap = intern (":map");
21919 staticpro (&QCmap);
21920 QCpointer = intern (":pointer");
21921 staticpro (&QCpointer);
21922 Qrect = intern ("rect");
21923 staticpro (&Qrect);
21924 Qcircle = intern ("circle");
21925 staticpro (&Qcircle);
21926 Qpoly = intern ("poly");
21927 staticpro (&Qpoly);
21928 Qmessage_truncate_lines = intern ("message-truncate-lines");
21929 staticpro (&Qmessage_truncate_lines);
21930 Qcursor_in_non_selected_windows = intern ("cursor-in-non-selected-windows");
21931 staticpro (&Qcursor_in_non_selected_windows);
21932 Qgrow_only = intern ("grow-only");
21933 staticpro (&Qgrow_only);
21934 Qinhibit_menubar_update = intern ("inhibit-menubar-update");
21935 staticpro (&Qinhibit_menubar_update);
21936 Qinhibit_eval_during_redisplay = intern ("inhibit-eval-during-redisplay");
21937 staticpro (&Qinhibit_eval_during_redisplay);
21938 Qposition = intern ("position");
21939 staticpro (&Qposition);
21940 Qbuffer_position = intern ("buffer-position");
21941 staticpro (&Qbuffer_position);
21942 Qobject = intern ("object");
21943 staticpro (&Qobject);
21944 Qbar = intern ("bar");
21945 staticpro (&Qbar);
21946 Qhbar = intern ("hbar");
21947 staticpro (&Qhbar);
21948 Qbox = intern ("box");
21949 staticpro (&Qbox);
21950 Qhollow = intern ("hollow");
21951 staticpro (&Qhollow);
21952 Qhand = intern ("hand");
21953 staticpro (&Qhand);
21954 Qarrow = intern ("arrow");
21955 staticpro (&Qarrow);
21956 Qtext = intern ("text");
21957 staticpro (&Qtext);
21958 Qrisky_local_variable = intern ("risky-local-variable");
21959 staticpro (&Qrisky_local_variable);
21960 Qinhibit_free_realized_faces = intern ("inhibit-free-realized-faces");
21961 staticpro (&Qinhibit_free_realized_faces);
21962
21963 list_of_error = Fcons (Fcons (intern ("error"),
21964 Fcons (intern ("void-variable"), Qnil)),
21965 Qnil);
21966 staticpro (&list_of_error);
21967
21968 Qlast_arrow_position = intern ("last-arrow-position");
21969 staticpro (&Qlast_arrow_position);
21970 Qlast_arrow_string = intern ("last-arrow-string");
21971 staticpro (&Qlast_arrow_string);
21972
21973 Qoverlay_arrow_string = intern ("overlay-arrow-string");
21974 staticpro (&Qoverlay_arrow_string);
21975 Qoverlay_arrow_bitmap = intern ("overlay-arrow-bitmap");
21976 staticpro (&Qoverlay_arrow_bitmap);
21977
21978 echo_buffer[0] = echo_buffer[1] = Qnil;
21979 staticpro (&echo_buffer[0]);
21980 staticpro (&echo_buffer[1]);
21981
21982 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
21983 staticpro (&echo_area_buffer[0]);
21984 staticpro (&echo_area_buffer[1]);
21985
21986 Vmessages_buffer_name = build_string ("*Messages*");
21987 staticpro (&Vmessages_buffer_name);
21988
21989 mode_line_proptrans_alist = Qnil;
21990 staticpro (&mode_line_proptrans_alist);
21991
21992 mode_line_string_list = Qnil;
21993 staticpro (&mode_line_string_list);
21994
21995 help_echo_string = Qnil;
21996 staticpro (&help_echo_string);
21997 help_echo_object = Qnil;
21998 staticpro (&help_echo_object);
21999 help_echo_window = Qnil;
22000 staticpro (&help_echo_window);
22001 previous_help_echo_string = Qnil;
22002 staticpro (&previous_help_echo_string);
22003 help_echo_pos = -1;
22004
22005 #ifdef HAVE_WINDOW_SYSTEM
22006 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
22007 doc: /* *Non-nil means draw block cursor as wide as the glyph under it.
22008 For example, if a block cursor is over a tab, it will be drawn as
22009 wide as that tab on the display. */);
22010 x_stretch_cursor_p = 0;
22011 #endif
22012
22013 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
22014 doc: /* *Non-nil means highlight trailing whitespace.
22015 The face used for trailing whitespace is `trailing-whitespace'. */);
22016 Vshow_trailing_whitespace = Qnil;
22017
22018 DEFVAR_LISP ("void-text-area-pointer", &Vvoid_text_area_pointer,
22019 doc: /* *The pointer shape to show in void text areas.
22020 Nil means to show the text pointer. Other options are `arrow', `text',
22021 `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
22022 Vvoid_text_area_pointer = Qarrow;
22023
22024 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
22025 doc: /* Non-nil means don't actually do any redisplay.
22026 This is used for internal purposes. */);
22027 Vinhibit_redisplay = Qnil;
22028
22029 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
22030 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
22031 Vglobal_mode_string = Qnil;
22032
22033 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
22034 doc: /* Marker for where to display an arrow on top of the buffer text.
22035 This must be the beginning of a line in order to work.
22036 See also `overlay-arrow-string'. */);
22037 Voverlay_arrow_position = Qnil;
22038
22039 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
22040 doc: /* String to display as an arrow in non-window frames.
22041 See also `overlay-arrow-position'. */);
22042 Voverlay_arrow_string = Qnil;
22043
22044 DEFVAR_LISP ("overlay-arrow-variable-list", &Voverlay_arrow_variable_list,
22045 doc: /* List of variables (symbols) which hold markers for overlay arrows.
22046 The symbols on this list are examined during redisplay to determine
22047 where to display overlay arrows. */);
22048 Voverlay_arrow_variable_list
22049 = Fcons (intern ("overlay-arrow-position"), Qnil);
22050
22051 DEFVAR_INT ("scroll-step", &scroll_step,
22052 doc: /* *The number of lines to try scrolling a window by when point moves out.
22053 If that fails to bring point back on frame, point is centered instead.
22054 If this is zero, point is always centered after it moves off frame.
22055 If you want scrolling to always be a line at a time, you should set
22056 `scroll-conservatively' to a large value rather than set this to 1. */);
22057
22058 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
22059 doc: /* *Scroll up to this many lines, to bring point back on screen.
22060 A value of zero means to scroll the text to center point vertically
22061 in the window. */);
22062 scroll_conservatively = 0;
22063
22064 DEFVAR_INT ("scroll-margin", &scroll_margin,
22065 doc: /* *Number of lines of margin at the top and bottom of a window.
22066 Recenter the window whenever point gets within this many lines
22067 of the top or bottom of the window. */);
22068 scroll_margin = 0;
22069
22070 DEFVAR_LISP ("display-pixels-per-inch", &Vdisplay_pixels_per_inch,
22071 doc: /* Pixels per inch on current display.
22072 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
22073 Vdisplay_pixels_per_inch = make_float (72.0);
22074
22075 #if GLYPH_DEBUG
22076 DEFVAR_INT ("debug-end-pos", &debug_end_pos, doc: /* Don't ask. */);
22077 #endif
22078
22079 DEFVAR_BOOL ("truncate-partial-width-windows",
22080 &truncate_partial_width_windows,
22081 doc: /* *Non-nil means truncate lines in all windows less than full frame wide. */);
22082 truncate_partial_width_windows = 1;
22083
22084 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
22085 doc: /* nil means display the mode-line/header-line/menu-bar in the default face.
22086 Any other value means to use the appropriate face, `mode-line',
22087 `header-line', or `menu' respectively. */);
22088 mode_line_inverse_video = 1;
22089
22090 DEFVAR_LISP ("line-number-display-limit", &Vline_number_display_limit,
22091 doc: /* *Maximum buffer size for which line number should be displayed.
22092 If the buffer is bigger than this, the line number does not appear
22093 in the mode line. A value of nil means no limit. */);
22094 Vline_number_display_limit = Qnil;
22095
22096 DEFVAR_INT ("line-number-display-limit-width",
22097 &line_number_display_limit_width,
22098 doc: /* *Maximum line width (in characters) for line number display.
22099 If the average length of the lines near point is bigger than this, then the
22100 line number may be omitted from the mode line. */);
22101 line_number_display_limit_width = 200;
22102
22103 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
22104 doc: /* *Non-nil means highlight region even in nonselected windows. */);
22105 highlight_nonselected_windows = 0;
22106
22107 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
22108 doc: /* Non-nil if more than one frame is visible on this display.
22109 Minibuffer-only frames don't count, but iconified frames do.
22110 This variable is not guaranteed to be accurate except while processing
22111 `frame-title-format' and `icon-title-format'. */);
22112
22113 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
22114 doc: /* Template for displaying the title bar of visible frames.
22115 \(Assuming the window manager supports this feature.)
22116 This variable has the same structure as `mode-line-format' (which see),
22117 and is used only on frames for which no explicit name has been set
22118 \(see `modify-frame-parameters'). */);
22119
22120 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
22121 doc: /* Template for displaying the title bar of an iconified frame.
22122 \(Assuming the window manager supports this feature.)
22123 This variable has the same structure as `mode-line-format' (which see),
22124 and is used only on frames for which no explicit name has been set
22125 \(see `modify-frame-parameters'). */);
22126 Vicon_title_format
22127 = Vframe_title_format
22128 = Fcons (intern ("multiple-frames"),
22129 Fcons (build_string ("%b"),
22130 Fcons (Fcons (empty_string,
22131 Fcons (intern ("invocation-name"),
22132 Fcons (build_string ("@"),
22133 Fcons (intern ("system-name"),
22134 Qnil)))),
22135 Qnil)));
22136
22137 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
22138 doc: /* Maximum number of lines to keep in the message log buffer.
22139 If nil, disable message logging. If t, log messages but don't truncate
22140 the buffer when it becomes large. */);
22141 Vmessage_log_max = make_number (50);
22142
22143 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
22144 doc: /* Functions called before redisplay, if window sizes have changed.
22145 The value should be a list of functions that take one argument.
22146 Just before redisplay, for each frame, if any of its windows have changed
22147 size since the last redisplay, or have been split or deleted,
22148 all the functions in the list are called, with the frame as argument. */);
22149 Vwindow_size_change_functions = Qnil;
22150
22151 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
22152 doc: /* List of Functions to call before redisplaying a window with scrolling.
22153 Each function is called with two arguments, the window
22154 and its new display-start position. Note that the value of `window-end'
22155 is not valid when these functions are called. */);
22156 Vwindow_scroll_functions = Qnil;
22157
22158 DEFVAR_BOOL ("mouse-autoselect-window", &mouse_autoselect_window,
22159 doc: /* *Non-nil means autoselect window with mouse pointer. */);
22160 mouse_autoselect_window = 0;
22161
22162 DEFVAR_BOOL ("auto-resize-tool-bars", &auto_resize_tool_bars_p,
22163 doc: /* *Non-nil means automatically resize tool-bars.
22164 This increases a tool-bar's height if not all tool-bar items are visible.
22165 It decreases a tool-bar's height when it would display blank lines
22166 otherwise. */);
22167 auto_resize_tool_bars_p = 1;
22168
22169 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
22170 doc: /* *Non-nil means raise tool-bar buttons when the mouse moves over them. */);
22171 auto_raise_tool_bar_buttons_p = 1;
22172
22173 DEFVAR_LISP ("tool-bar-button-margin", &Vtool_bar_button_margin,
22174 doc: /* *Margin around tool-bar buttons in pixels.
22175 If an integer, use that for both horizontal and vertical margins.
22176 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
22177 HORZ specifying the horizontal margin, and VERT specifying the
22178 vertical margin. */);
22179 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
22180
22181 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
22182 doc: /* *Relief thickness of tool-bar buttons. */);
22183 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
22184
22185 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
22186 doc: /* List of functions to call to fontify regions of text.
22187 Each function is called with one argument POS. Functions must
22188 fontify a region starting at POS in the current buffer, and give
22189 fontified regions the property `fontified'. */);
22190 Vfontification_functions = Qnil;
22191 Fmake_variable_buffer_local (Qfontification_functions);
22192
22193 DEFVAR_BOOL ("unibyte-display-via-language-environment",
22194 &unibyte_display_via_language_environment,
22195 doc: /* *Non-nil means display unibyte text according to language environment.
22196 Specifically this means that unibyte non-ASCII characters
22197 are displayed by converting them to the equivalent multibyte characters
22198 according to the current language environment. As a result, they are
22199 displayed according to the current fontset. */);
22200 unibyte_display_via_language_environment = 0;
22201
22202 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
22203 doc: /* *Maximum height for resizing mini-windows.
22204 If a float, it specifies a fraction of the mini-window frame's height.
22205 If an integer, it specifies a number of lines. */);
22206 Vmax_mini_window_height = make_float (0.25);
22207
22208 DEFVAR_LISP ("resize-mini-windows", &Vresize_mini_windows,
22209 doc: /* *How to resize mini-windows.
22210 A value of nil means don't automatically resize mini-windows.
22211 A value of t means resize them to fit the text displayed in them.
22212 A value of `grow-only', the default, means let mini-windows grow
22213 only, until their display becomes empty, at which point the windows
22214 go back to their normal size. */);
22215 Vresize_mini_windows = Qgrow_only;
22216
22217 DEFVAR_LISP ("cursor-in-non-selected-windows",
22218 &Vcursor_in_non_selected_windows,
22219 doc: /* *Cursor type to display in non-selected windows.
22220 t means to use hollow box cursor. See `cursor-type' for other values. */);
22221 Vcursor_in_non_selected_windows = Qt;
22222
22223 DEFVAR_LISP ("blink-cursor-alist", &Vblink_cursor_alist,
22224 doc: /* Alist specifying how to blink the cursor off.
22225 Each element has the form (ON-STATE . OFF-STATE). Whenever the
22226 `cursor-type' frame-parameter or variable equals ON-STATE,
22227 comparing using `equal', Emacs uses OFF-STATE to specify
22228 how to blink it off. */);
22229 Vblink_cursor_alist = Qnil;
22230
22231 DEFVAR_BOOL ("auto-hscroll-mode", &automatic_hscrolling_p,
22232 doc: /* *Non-nil means scroll the display automatically to make point visible. */);
22233 automatic_hscrolling_p = 1;
22234
22235 DEFVAR_INT ("hscroll-margin", &hscroll_margin,
22236 doc: /* *How many columns away from the window edge point is allowed to get
22237 before automatic hscrolling will horizontally scroll the window. */);
22238 hscroll_margin = 5;
22239
22240 DEFVAR_LISP ("hscroll-step", &Vhscroll_step,
22241 doc: /* *How many columns to scroll the window when point gets too close to the edge.
22242 When point is less than `automatic-hscroll-margin' columns from the window
22243 edge, automatic hscrolling will scroll the window by the amount of columns
22244 determined by this variable. If its value is a positive integer, scroll that
22245 many columns. If it's a positive floating-point number, it specifies the
22246 fraction of the window's width to scroll. If it's nil or zero, point will be
22247 centered horizontally after the scroll. Any other value, including negative
22248 numbers, are treated as if the value were zero.
22249
22250 Automatic hscrolling always moves point outside the scroll margin, so if
22251 point was more than scroll step columns inside the margin, the window will
22252 scroll more than the value given by the scroll step.
22253
22254 Note that the lower bound for automatic hscrolling specified by `scroll-left'
22255 and `scroll-right' overrides this variable's effect. */);
22256 Vhscroll_step = make_number (0);
22257
22258 DEFVAR_BOOL ("message-truncate-lines", &message_truncate_lines,
22259 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
22260 Bind this around calls to `message' to let it take effect. */);
22261 message_truncate_lines = 0;
22262
22263 DEFVAR_LISP ("menu-bar-update-hook", &Vmenu_bar_update_hook,
22264 doc: /* Normal hook run for clicks on menu bar, before displaying a submenu.
22265 Can be used to update submenus whose contents should vary. */);
22266 Vmenu_bar_update_hook = Qnil;
22267
22268 DEFVAR_BOOL ("inhibit-menubar-update", &inhibit_menubar_update,
22269 doc: /* Non-nil means don't update menu bars. Internal use only. */);
22270 inhibit_menubar_update = 0;
22271
22272 DEFVAR_BOOL ("inhibit-eval-during-redisplay", &inhibit_eval_during_redisplay,
22273 doc: /* Non-nil means don't eval Lisp during redisplay. */);
22274 inhibit_eval_during_redisplay = 0;
22275
22276 DEFVAR_BOOL ("inhibit-free-realized-faces", &inhibit_free_realized_faces,
22277 doc: /* Non-nil means don't free realized faces. Internal use only. */);
22278 inhibit_free_realized_faces = 0;
22279
22280 #if GLYPH_DEBUG
22281 DEFVAR_BOOL ("inhibit-try-window-id", &inhibit_try_window_id,
22282 doc: /* Inhibit try_window_id display optimization. */);
22283 inhibit_try_window_id = 0;
22284
22285 DEFVAR_BOOL ("inhibit-try-window-reusing", &inhibit_try_window_reusing,
22286 doc: /* Inhibit try_window_reusing display optimization. */);
22287 inhibit_try_window_reusing = 0;
22288
22289 DEFVAR_BOOL ("inhibit-try-cursor-movement", &inhibit_try_cursor_movement,
22290 doc: /* Inhibit try_cursor_movement display optimization. */);
22291 inhibit_try_cursor_movement = 0;
22292 #endif /* GLYPH_DEBUG */
22293 }
22294
22295
22296 /* Initialize this module when Emacs starts. */
22297
22298 void
22299 init_xdisp ()
22300 {
22301 Lisp_Object root_window;
22302 struct window *mini_w;
22303
22304 current_header_line_height = current_mode_line_height = -1;
22305
22306 CHARPOS (this_line_start_pos) = 0;
22307
22308 mini_w = XWINDOW (minibuf_window);
22309 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
22310
22311 if (!noninteractive)
22312 {
22313 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
22314 int i;
22315
22316 XWINDOW (root_window)->top_line = make_number (FRAME_TOP_MARGIN (f));
22317 set_window_height (root_window,
22318 FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f),
22319 0);
22320 mini_w->top_line = make_number (FRAME_LINES (f) - 1);
22321 set_window_height (minibuf_window, 1, 0);
22322
22323 XWINDOW (root_window)->total_cols = make_number (FRAME_COLS (f));
22324 mini_w->total_cols = make_number (FRAME_COLS (f));
22325
22326 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
22327 scratch_glyph_row.glyphs[TEXT_AREA + 1]
22328 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
22329
22330 /* The default ellipsis glyphs `...'. */
22331 for (i = 0; i < 3; ++i)
22332 default_invis_vector[i] = make_number ('.');
22333 }
22334
22335 {
22336 /* Allocate the buffer for frame titles.
22337 Also used for `format-mode-line'. */
22338 int size = 100;
22339 frame_title_buf = (char *) xmalloc (size);
22340 frame_title_buf_end = frame_title_buf + size;
22341 frame_title_ptr = NULL;
22342 }
22343
22344 help_echo_showing_p = 0;
22345 }
22346
22347
22348 /* arch-tag: eacc864d-bb6a-4b74-894a-1a4399a1358b
22349 (do not change this comment) */