(speedbar-file-regexp): Give it a phony defvar before and a real
[bpt/emacs.git] / src / xdisp.c
1 /* Display generation from window structure and buffer text.
2 Copyright (C) 1985,86,87,88,93,94,95,97,98,99,2000,01,02,03,04
3 Free Software Foundation, Inc.
4
5 This file is part of GNU Emacs.
6
7 GNU Emacs is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
21
22 /* New redisplay written by Gerd Moellmann <gerd@gnu.org>.
23
24 Redisplay.
25
26 Emacs separates the task of updating the display from code
27 modifying global state, e.g. buffer text. This way functions
28 operating on buffers don't also have to be concerned with updating
29 the display.
30
31 Updating the display is triggered by the Lisp interpreter when it
32 decides it's time to do it. This is done either automatically for
33 you as part of the interpreter's command loop or as the result of
34 calling Lisp functions like `sit-for'. The C function `redisplay'
35 in xdisp.c is the only entry into the inner redisplay code. (Or,
36 let's say almost---see the description of direct update
37 operations, below.)
38
39 The following diagram shows how redisplay code is invoked. As you
40 can see, Lisp calls redisplay and vice versa. Under window systems
41 like X, some portions of the redisplay code are also called
42 asynchronously during mouse movement or expose events. It is very
43 important that these code parts do NOT use the C library (malloc,
44 free) because many C libraries under Unix are not reentrant. They
45 may also NOT call functions of the Lisp interpreter which could
46 change the interpreter's state. If you don't follow these rules,
47 you will encounter bugs which are very hard to explain.
48
49 (Direct functions, see below)
50 direct_output_for_insert,
51 direct_forward_char (dispnew.c)
52 +---------------------------------+
53 | |
54 | V
55 +--------------+ redisplay +----------------+
56 | Lisp machine |---------------->| Redisplay code |<--+
57 +--------------+ (xdisp.c) +----------------+ |
58 ^ | |
59 +----------------------------------+ |
60 Don't use this path when called |
61 asynchronously! |
62 |
63 expose_window (asynchronous) |
64 |
65 X expose events -----+
66
67 What does redisplay do? Obviously, it has to figure out somehow what
68 has been changed since the last time the display has been updated,
69 and to make these changes visible. Preferably it would do that in
70 a moderately intelligent way, i.e. fast.
71
72 Changes in buffer text can be deduced from window and buffer
73 structures, and from some global variables like `beg_unchanged' and
74 `end_unchanged'. The contents of the display are additionally
75 recorded in a `glyph matrix', a two-dimensional matrix of glyph
76 structures. Each row in such a matrix corresponds to a line on the
77 display, and each glyph in a row corresponds to a column displaying
78 a character, an image, or what else. This matrix is called the
79 `current glyph matrix' or `current matrix' in redisplay
80 terminology.
81
82 For buffer parts that have been changed since the last update, a
83 second glyph matrix is constructed, the so called `desired glyph
84 matrix' or short `desired matrix'. Current and desired matrix are
85 then compared to find a cheap way to update the display, e.g. by
86 reusing part of the display by scrolling lines.
87
88
89 Direct operations.
90
91 You will find a lot of redisplay optimizations when you start
92 looking at the innards of redisplay. The overall goal of all these
93 optimizations is to make redisplay fast because it is done
94 frequently.
95
96 Two optimizations are not found in xdisp.c. These are the direct
97 operations mentioned above. As the name suggests they follow a
98 different principle than the rest of redisplay. Instead of
99 building a desired matrix and then comparing it with the current
100 display, they perform their actions directly on the display and on
101 the current matrix.
102
103 One direct operation updates the display after one character has
104 been entered. The other one moves the cursor by one position
105 forward or backward. You find these functions under the names
106 `direct_output_for_insert' and `direct_output_forward_char' in
107 dispnew.c.
108
109
110 Desired matrices.
111
112 Desired matrices are always built per Emacs window. The function
113 `display_line' is the central function to look at if you are
114 interested. It constructs one row in a desired matrix given an
115 iterator structure containing both a buffer position and a
116 description of the environment in which the text is to be
117 displayed. But this is too early, read on.
118
119 Characters and pixmaps displayed for a range of buffer text depend
120 on various settings of buffers and windows, on overlays and text
121 properties, on display tables, on selective display. The good news
122 is that all this hairy stuff is hidden behind a small set of
123 interface functions taking an iterator structure (struct it)
124 argument.
125
126 Iteration over things to be displayed is then simple. It is
127 started by initializing an iterator with a call to init_iterator.
128 Calls to get_next_display_element fill the iterator structure with
129 relevant information about the next thing to display. Calls to
130 set_iterator_to_next move the iterator to the next thing.
131
132 Besides this, an iterator also contains information about the
133 display environment in which glyphs for display elements are to be
134 produced. It has fields for the width and height of the display,
135 the information whether long lines are truncated or continued, a
136 current X and Y position, and lots of other stuff you can better
137 see in dispextern.h.
138
139 Glyphs in a desired matrix are normally constructed in a loop
140 calling get_next_display_element and then produce_glyphs. The call
141 to produce_glyphs will fill the iterator structure with pixel
142 information about the element being displayed and at the same time
143 produce glyphs for it. If the display element fits on the line
144 being displayed, set_iterator_to_next is called next, otherwise the
145 glyphs produced are discarded.
146
147
148 Frame matrices.
149
150 That just couldn't be all, could it? What about terminal types not
151 supporting operations on sub-windows of the screen? To update the
152 display on such a terminal, window-based glyph matrices are not
153 well suited. To be able to reuse part of the display (scrolling
154 lines up and down), we must instead have a view of the whole
155 screen. This is what `frame matrices' are for. They are a trick.
156
157 Frames on terminals like above have a glyph pool. Windows on such
158 a frame sub-allocate their glyph memory from their frame's glyph
159 pool. The frame itself is given its own glyph matrices. By
160 coincidence---or maybe something else---rows in window glyph
161 matrices are slices of corresponding rows in frame matrices. Thus
162 writing to window matrices implicitly updates a frame matrix which
163 provides us with the view of the whole screen that we originally
164 wanted to have without having to move many bytes around. To be
165 honest, there is a little bit more done, but not much more. If you
166 plan to extend that code, take a look at dispnew.c. The function
167 build_frame_matrix is a good starting point. */
168
169 #include <config.h>
170 #include <stdio.h>
171
172 #include "lisp.h"
173 #include "keyboard.h"
174 #include "frame.h"
175 #include "window.h"
176 #include "termchar.h"
177 #include "dispextern.h"
178 #include "buffer.h"
179 #include "charset.h"
180 #include "indent.h"
181 #include "commands.h"
182 #include "keymap.h"
183 #include "macros.h"
184 #include "disptab.h"
185 #include "termhooks.h"
186 #include "intervals.h"
187 #include "coding.h"
188 #include "process.h"
189 #include "region-cache.h"
190 #include "fontset.h"
191 #include "blockinput.h"
192
193 #ifdef HAVE_X_WINDOWS
194 #include "xterm.h"
195 #endif
196 #ifdef WINDOWSNT
197 #include "w32term.h"
198 #endif
199 #ifdef MAC_OS
200 #include "macterm.h"
201 #endif
202
203 #ifndef FRAME_X_OUTPUT
204 #define FRAME_X_OUTPUT(f) ((f)->output_data.x)
205 #endif
206
207 #define INFINITY 10000000
208
209 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
210 || defined (USE_GTK)
211 extern void set_frame_menubar P_ ((struct frame *f, int, int));
212 extern int pending_menu_activation;
213 #endif
214
215 extern int interrupt_input;
216 extern int command_loop_level;
217
218 extern int minibuffer_auto_raise;
219 extern Lisp_Object Vminibuffer_list;
220
221 extern Lisp_Object Qface;
222 extern Lisp_Object Qmode_line, Qmode_line_inactive, Qheader_line;
223
224 extern Lisp_Object Voverriding_local_map;
225 extern Lisp_Object Voverriding_local_map_menu_flag;
226 extern Lisp_Object Qmenu_item;
227 extern Lisp_Object Qwhen;
228 extern Lisp_Object Qhelp_echo;
229
230 Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
231 Lisp_Object Qwindow_scroll_functions, Vwindow_scroll_functions;
232 Lisp_Object Qredisplay_end_trigger_functions;
233 Lisp_Object Qinhibit_point_motion_hooks;
234 Lisp_Object QCeval, QCfile, QCdata, QCpropertize;
235 Lisp_Object Qfontified;
236 Lisp_Object Qgrow_only;
237 Lisp_Object Qinhibit_eval_during_redisplay;
238 Lisp_Object Qbuffer_position, Qposition, Qobject;
239
240 /* Cursor shapes */
241 Lisp_Object Qbar, Qhbar, Qbox, Qhollow;
242
243 /* Pointer shapes */
244 Lisp_Object Qarrow, Qhand, Qtext;
245
246 Lisp_Object Qrisky_local_variable;
247
248 /* Holds the list (error). */
249 Lisp_Object list_of_error;
250
251 /* Functions called to fontify regions of text. */
252
253 Lisp_Object Vfontification_functions;
254 Lisp_Object Qfontification_functions;
255
256 /* Non-zero means automatically select any window when the mouse
257 cursor moves into it. */
258 int mouse_autoselect_window;
259
260 /* Non-zero means draw tool bar buttons raised when the mouse moves
261 over them. */
262
263 int auto_raise_tool_bar_buttons_p;
264
265 /* Margin around tool bar buttons in pixels. */
266
267 Lisp_Object Vtool_bar_button_margin;
268
269 /* Thickness of shadow to draw around tool bar buttons. */
270
271 EMACS_INT tool_bar_button_relief;
272
273 /* Non-zero means automatically resize tool-bars so that all tool-bar
274 items are visible, and no blank lines remain. */
275
276 int auto_resize_tool_bars_p;
277
278 /* Non-zero means draw block and hollow cursor as wide as the glyph
279 under it. For example, if a block cursor is over a tab, it will be
280 drawn as wide as that tab on the display. */
281
282 int x_stretch_cursor_p;
283
284 /* Non-nil means don't actually do any redisplay. */
285
286 Lisp_Object Vinhibit_redisplay, Qinhibit_redisplay;
287
288 /* Non-zero means Lisp evaluation during redisplay is inhibited. */
289
290 int inhibit_eval_during_redisplay;
291
292 /* Names of text properties relevant for redisplay. */
293
294 Lisp_Object Qdisplay;
295 extern Lisp_Object Qface, Qinvisible, Qwidth;
296
297 /* Symbols used in text property values. */
298
299 Lisp_Object Vdisplay_pixels_per_inch;
300 Lisp_Object Qspace, QCalign_to, QCrelative_width, QCrelative_height;
301 Lisp_Object Qleft_margin, Qright_margin, Qspace_width, Qraise;
302 Lisp_Object Qslice;
303 Lisp_Object Qcenter;
304 Lisp_Object Qmargin, Qpointer;
305 Lisp_Object Qline_height, Qtotal;
306 extern Lisp_Object Qheight;
307 extern Lisp_Object QCwidth, QCheight, QCascent;
308 extern Lisp_Object Qscroll_bar;
309
310 /* Non-nil means highlight trailing whitespace. */
311
312 Lisp_Object Vshow_trailing_whitespace;
313
314 #ifdef HAVE_WINDOW_SYSTEM
315 extern Lisp_Object Voverflow_newline_into_fringe;
316
317 /* Test if overflow newline into fringe. Called with iterator IT
318 at or past right window margin, and with IT->current_x set. */
319
320 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(it) \
321 (!NILP (Voverflow_newline_into_fringe) \
322 && FRAME_WINDOW_P (it->f) \
323 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) > 0 \
324 && it->current_x == it->last_visible_x)
325
326 #endif /* HAVE_WINDOW_SYSTEM */
327
328 /* Non-nil means show the text cursor in void text areas
329 i.e. in blank areas after eol and eob. This used to be
330 the default in 21.3. */
331
332 Lisp_Object Vvoid_text_area_pointer;
333
334 /* Name of the face used to highlight trailing whitespace. */
335
336 Lisp_Object Qtrailing_whitespace;
337
338 /* The symbol `image' which is the car of the lists used to represent
339 images in Lisp. */
340
341 Lisp_Object Qimage;
342
343 /* The image map types. */
344 Lisp_Object QCmap, QCpointer;
345 Lisp_Object Qrect, Qcircle, Qpoly;
346
347 /* Non-zero means print newline to stdout before next mini-buffer
348 message. */
349
350 int noninteractive_need_newline;
351
352 /* Non-zero means print newline to message log before next message. */
353
354 static int message_log_need_newline;
355
356 /* Three markers that message_dolog uses.
357 It could allocate them itself, but that causes trouble
358 in handling memory-full errors. */
359 static Lisp_Object message_dolog_marker1;
360 static Lisp_Object message_dolog_marker2;
361 static Lisp_Object message_dolog_marker3;
362 \f
363 /* The buffer position of the first character appearing entirely or
364 partially on the line of the selected window which contains the
365 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
366 redisplay optimization in redisplay_internal. */
367
368 static struct text_pos this_line_start_pos;
369
370 /* Number of characters past the end of the line above, including the
371 terminating newline. */
372
373 static struct text_pos this_line_end_pos;
374
375 /* The vertical positions and the height of this line. */
376
377 static int this_line_vpos;
378 static int this_line_y;
379 static int this_line_pixel_height;
380
381 /* X position at which this display line starts. Usually zero;
382 negative if first character is partially visible. */
383
384 static int this_line_start_x;
385
386 /* Buffer that this_line_.* variables are referring to. */
387
388 static struct buffer *this_line_buffer;
389
390 /* Nonzero means truncate lines in all windows less wide than the
391 frame. */
392
393 int truncate_partial_width_windows;
394
395 /* A flag to control how to display unibyte 8-bit character. */
396
397 int unibyte_display_via_language_environment;
398
399 /* Nonzero means we have more than one non-mini-buffer-only frame.
400 Not guaranteed to be accurate except while parsing
401 frame-title-format. */
402
403 int multiple_frames;
404
405 Lisp_Object Vglobal_mode_string;
406
407
408 /* List of variables (symbols) which hold markers for overlay arrows.
409 The symbols on this list are examined during redisplay to determine
410 where to display overlay arrows. */
411
412 Lisp_Object Voverlay_arrow_variable_list;
413
414 /* Marker for where to display an arrow on top of the buffer text. */
415
416 Lisp_Object Voverlay_arrow_position;
417
418 /* String to display for the arrow. Only used on terminal frames. */
419
420 Lisp_Object Voverlay_arrow_string;
421
422 /* Values of those variables at last redisplay are stored as
423 properties on `overlay-arrow-position' symbol. However, if
424 Voverlay_arrow_position is a marker, last-arrow-position is its
425 numerical position. */
426
427 Lisp_Object Qlast_arrow_position, Qlast_arrow_string;
428
429 /* Alternative overlay-arrow-string and overlay-arrow-bitmap
430 properties on a symbol in overlay-arrow-variable-list. */
431
432 Lisp_Object Qoverlay_arrow_string, Qoverlay_arrow_bitmap;
433
434 /* Like mode-line-format, but for the title bar on a visible frame. */
435
436 Lisp_Object Vframe_title_format;
437
438 /* Like mode-line-format, but for the title bar on an iconified frame. */
439
440 Lisp_Object Vicon_title_format;
441
442 /* List of functions to call when a window's size changes. These
443 functions get one arg, a frame on which one or more windows' sizes
444 have changed. */
445
446 static Lisp_Object Vwindow_size_change_functions;
447
448 Lisp_Object Qmenu_bar_update_hook, Vmenu_bar_update_hook;
449
450 /* Nonzero if overlay arrow has been displayed once in this window. */
451
452 static int overlay_arrow_seen;
453
454 /* Nonzero means highlight the region even in nonselected windows. */
455
456 int highlight_nonselected_windows;
457
458 /* If cursor motion alone moves point off frame, try scrolling this
459 many lines up or down if that will bring it back. */
460
461 static EMACS_INT scroll_step;
462
463 /* Nonzero means scroll just far enough to bring point back on the
464 screen, when appropriate. */
465
466 static EMACS_INT scroll_conservatively;
467
468 /* Recenter the window whenever point gets within this many lines of
469 the top or bottom of the window. This value is translated into a
470 pixel value by multiplying it with FRAME_LINE_HEIGHT, which means
471 that there is really a fixed pixel height scroll margin. */
472
473 EMACS_INT scroll_margin;
474
475 /* Number of windows showing the buffer of the selected window (or
476 another buffer with the same base buffer). keyboard.c refers to
477 this. */
478
479 int buffer_shared;
480
481 /* Vector containing glyphs for an ellipsis `...'. */
482
483 static Lisp_Object default_invis_vector[3];
484
485 /* Zero means display the mode-line/header-line/menu-bar in the default face
486 (this slightly odd definition is for compatibility with previous versions
487 of emacs), non-zero means display them using their respective faces.
488
489 This variable is deprecated. */
490
491 int mode_line_inverse_video;
492
493 /* Prompt to display in front of the mini-buffer contents. */
494
495 Lisp_Object minibuf_prompt;
496
497 /* Width of current mini-buffer prompt. Only set after display_line
498 of the line that contains the prompt. */
499
500 int minibuf_prompt_width;
501
502 /* This is the window where the echo area message was displayed. It
503 is always a mini-buffer window, but it may not be the same window
504 currently active as a mini-buffer. */
505
506 Lisp_Object echo_area_window;
507
508 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
509 pushes the current message and the value of
510 message_enable_multibyte on the stack, the function restore_message
511 pops the stack and displays MESSAGE again. */
512
513 Lisp_Object Vmessage_stack;
514
515 /* Nonzero means multibyte characters were enabled when the echo area
516 message was specified. */
517
518 int message_enable_multibyte;
519
520 /* Nonzero if we should redraw the mode lines on the next redisplay. */
521
522 int update_mode_lines;
523
524 /* Nonzero if window sizes or contents have changed since last
525 redisplay that finished. */
526
527 int windows_or_buffers_changed;
528
529 /* Nonzero means a frame's cursor type has been changed. */
530
531 int cursor_type_changed;
532
533 /* Nonzero after display_mode_line if %l was used and it displayed a
534 line number. */
535
536 int line_number_displayed;
537
538 /* Maximum buffer size for which to display line numbers. */
539
540 Lisp_Object Vline_number_display_limit;
541
542 /* Line width to consider when repositioning for line number display. */
543
544 static EMACS_INT line_number_display_limit_width;
545
546 /* Number of lines to keep in the message log buffer. t means
547 infinite. nil means don't log at all. */
548
549 Lisp_Object Vmessage_log_max;
550
551 /* The name of the *Messages* buffer, a string. */
552
553 static Lisp_Object Vmessages_buffer_name;
554
555 /* Current, index 0, and last displayed echo area message. Either
556 buffers from echo_buffers, or nil to indicate no message. */
557
558 Lisp_Object echo_area_buffer[2];
559
560 /* The buffers referenced from echo_area_buffer. */
561
562 static Lisp_Object echo_buffer[2];
563
564 /* A vector saved used in with_area_buffer to reduce consing. */
565
566 static Lisp_Object Vwith_echo_area_save_vector;
567
568 /* Non-zero means display_echo_area should display the last echo area
569 message again. Set by redisplay_preserve_echo_area. */
570
571 static int display_last_displayed_message_p;
572
573 /* Nonzero if echo area is being used by print; zero if being used by
574 message. */
575
576 int message_buf_print;
577
578 /* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
579
580 Lisp_Object Qinhibit_menubar_update;
581 int inhibit_menubar_update;
582
583 /* Maximum height for resizing mini-windows. Either a float
584 specifying a fraction of the available height, or an integer
585 specifying a number of lines. */
586
587 Lisp_Object Vmax_mini_window_height;
588
589 /* Non-zero means messages should be displayed with truncated
590 lines instead of being continued. */
591
592 int message_truncate_lines;
593 Lisp_Object Qmessage_truncate_lines;
594
595 /* Set to 1 in clear_message to make redisplay_internal aware
596 of an emptied echo area. */
597
598 static int message_cleared_p;
599
600 /* Non-zero means we want a hollow cursor in windows that are not
601 selected. Zero means there's no cursor in such windows. */
602
603 Lisp_Object Vcursor_in_non_selected_windows;
604 Lisp_Object Qcursor_in_non_selected_windows;
605
606 /* How to blink the default frame cursor off. */
607 Lisp_Object Vblink_cursor_alist;
608
609 /* A scratch glyph row with contents used for generating truncation
610 glyphs. Also used in direct_output_for_insert. */
611
612 #define MAX_SCRATCH_GLYPHS 100
613 struct glyph_row scratch_glyph_row;
614 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
615
616 /* Ascent and height of the last line processed by move_it_to. */
617
618 static int last_max_ascent, last_height;
619
620 /* Non-zero if there's a help-echo in the echo area. */
621
622 int help_echo_showing_p;
623
624 /* If >= 0, computed, exact values of mode-line and header-line height
625 to use in the macros CURRENT_MODE_LINE_HEIGHT and
626 CURRENT_HEADER_LINE_HEIGHT. */
627
628 int current_mode_line_height, current_header_line_height;
629
630 /* The maximum distance to look ahead for text properties. Values
631 that are too small let us call compute_char_face and similar
632 functions too often which is expensive. Values that are too large
633 let us call compute_char_face and alike too often because we
634 might not be interested in text properties that far away. */
635
636 #define TEXT_PROP_DISTANCE_LIMIT 100
637
638 #if GLYPH_DEBUG
639
640 /* Variables to turn off display optimizations from Lisp. */
641
642 int inhibit_try_window_id, inhibit_try_window_reusing;
643 int inhibit_try_cursor_movement;
644
645 /* Non-zero means print traces of redisplay if compiled with
646 GLYPH_DEBUG != 0. */
647
648 int trace_redisplay_p;
649
650 #endif /* GLYPH_DEBUG */
651
652 #ifdef DEBUG_TRACE_MOVE
653 /* Non-zero means trace with TRACE_MOVE to stderr. */
654 int trace_move;
655
656 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
657 #else
658 #define TRACE_MOVE(x) (void) 0
659 #endif
660
661 /* Non-zero means automatically scroll windows horizontally to make
662 point visible. */
663
664 int automatic_hscrolling_p;
665
666 /* How close to the margin can point get before the window is scrolled
667 horizontally. */
668 EMACS_INT hscroll_margin;
669
670 /* How much to scroll horizontally when point is inside the above margin. */
671 Lisp_Object Vhscroll_step;
672
673 /* 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 /* Record the previous terminal frame we displayed. */
770
771 static struct frame *previous_terminal_frame;
772
773 /* Non-zero while redisplay_internal is in progress. */
774
775 int redisplaying_p;
776
777 /* Non-zero means don't free realized faces. Bound while freeing
778 realized faces is dangerous because glyph matrices might still
779 reference them. */
780
781 int inhibit_free_realized_faces;
782 Lisp_Object Qinhibit_free_realized_faces;
783
784 /* If a string, XTread_socket generates an event to display that string.
785 (The display is done in read_char.) */
786
787 Lisp_Object help_echo_string;
788 Lisp_Object help_echo_window;
789 Lisp_Object help_echo_object;
790 int help_echo_pos;
791
792 /* Temporary variable for XTread_socket. */
793
794 Lisp_Object previous_help_echo_string;
795
796 /* Null glyph slice */
797
798 static struct glyph_slice null_glyph_slice = { 0, 0, 0, 0 };
799
800 \f
801 /* Function prototypes. */
802
803 static void setup_for_ellipsis P_ ((struct it *));
804 static void mark_window_display_accurate_1 P_ ((struct window *, int));
805 static int single_display_prop_string_p P_ ((Lisp_Object, Lisp_Object));
806 static int display_prop_string_p P_ ((Lisp_Object, Lisp_Object));
807 static int cursor_row_p P_ ((struct window *, struct glyph_row *));
808 static int redisplay_mode_lines P_ ((Lisp_Object, int));
809 static char *decode_mode_spec_coding P_ ((Lisp_Object, char *, int));
810
811 #if 0
812 static int invisible_text_between_p P_ ((struct it *, int, int));
813 #endif
814
815 static int next_element_from_ellipsis P_ ((struct it *));
816 static void pint2str P_ ((char *, int, int));
817 static void pint2hrstr P_ ((char *, int, int));
818 static struct text_pos run_window_scroll_functions P_ ((Lisp_Object,
819 struct text_pos));
820 static void reconsider_clip_changes P_ ((struct window *, struct buffer *));
821 static int text_outside_line_unchanged_p P_ ((struct window *, int, int));
822 static void store_frame_title_char P_ ((char));
823 static int store_frame_title P_ ((const unsigned char *, int, int));
824 static void x_consider_frame_title P_ ((Lisp_Object));
825 static void handle_stop P_ ((struct it *));
826 static int tool_bar_lines_needed P_ ((struct frame *));
827 static int single_display_prop_intangible_p P_ ((Lisp_Object));
828 static void ensure_echo_area_buffers P_ ((void));
829 static Lisp_Object unwind_with_echo_area_buffer P_ ((Lisp_Object));
830 static Lisp_Object with_echo_area_buffer_unwind_data P_ ((struct window *));
831 static int with_echo_area_buffer P_ ((struct window *, int,
832 int (*) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
833 EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
834 static void clear_garbaged_frames P_ ((void));
835 static int current_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
836 static int truncate_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
837 static int set_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
838 static int display_echo_area P_ ((struct window *));
839 static int display_echo_area_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
840 static int resize_mini_window_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
841 static Lisp_Object unwind_redisplay P_ ((Lisp_Object));
842 static int string_char_and_length P_ ((const unsigned char *, int, int *));
843 static struct text_pos display_prop_end P_ ((struct it *, Lisp_Object,
844 struct text_pos));
845 static int compute_window_start_on_continuation_line P_ ((struct window *));
846 static Lisp_Object safe_eval_handler P_ ((Lisp_Object));
847 static void insert_left_trunc_glyphs P_ ((struct it *));
848 static struct glyph_row *get_overlay_arrow_glyph_row P_ ((struct window *,
849 Lisp_Object));
850 static void extend_face_to_end_of_line P_ ((struct it *));
851 static int append_space_for_newline P_ ((struct it *, int));
852 static int make_cursor_line_fully_visible P_ ((struct window *, int));
853 static int try_scrolling P_ ((Lisp_Object, int, EMACS_INT, EMACS_INT, int, int));
854 static int try_cursor_movement P_ ((Lisp_Object, struct text_pos, int *));
855 static int trailing_whitespace_p P_ ((int));
856 static int message_log_check_duplicate P_ ((int, int, int, int));
857 static void push_it P_ ((struct it *));
858 static void pop_it P_ ((struct it *));
859 static void sync_frame_with_window_matrix_rows P_ ((struct window *));
860 static void select_frame_for_redisplay P_ ((Lisp_Object));
861 static void redisplay_internal P_ ((int));
862 static int echo_area_display P_ ((int));
863 static void redisplay_windows P_ ((Lisp_Object));
864 static void redisplay_window P_ ((Lisp_Object, int));
865 static Lisp_Object redisplay_window_error ();
866 static Lisp_Object redisplay_window_0 P_ ((Lisp_Object));
867 static Lisp_Object redisplay_window_1 P_ ((Lisp_Object));
868 static void update_menu_bar P_ ((struct frame *, int));
869 static int try_window_reusing_current_matrix P_ ((struct window *));
870 static int try_window_id P_ ((struct window *));
871 static int display_line P_ ((struct it *));
872 static int display_mode_lines P_ ((struct window *));
873 static int display_mode_line P_ ((struct window *, enum face_id, Lisp_Object));
874 static int display_mode_element P_ ((struct it *, int, int, int, Lisp_Object, Lisp_Object, int));
875 static int store_mode_line_string P_ ((char *, Lisp_Object, int, int, int, Lisp_Object));
876 static char *decode_mode_spec P_ ((struct window *, int, int, int, int *));
877 static void display_menu_bar P_ ((struct window *));
878 static int display_count_lines P_ ((int, int, int, int, int *));
879 static int display_string P_ ((unsigned char *, Lisp_Object, Lisp_Object,
880 int, int, struct it *, int, int, int, int));
881 static void compute_line_metrics P_ ((struct it *));
882 static void run_redisplay_end_trigger_hook P_ ((struct it *));
883 static int get_overlay_strings P_ ((struct it *, int));
884 static void next_overlay_string P_ ((struct it *));
885 static void reseat P_ ((struct it *, struct text_pos, int));
886 static void reseat_1 P_ ((struct it *, struct text_pos, int));
887 static void back_to_previous_visible_line_start P_ ((struct it *));
888 static void reseat_at_previous_visible_line_start P_ ((struct it *));
889 static void reseat_at_next_visible_line_start P_ ((struct it *, int));
890 static int next_element_from_display_vector P_ ((struct it *));
891 static int next_element_from_string P_ ((struct it *));
892 static int next_element_from_c_string P_ ((struct it *));
893 static int next_element_from_buffer P_ ((struct it *));
894 static int next_element_from_composition P_ ((struct it *));
895 static int next_element_from_image P_ ((struct it *));
896 static int next_element_from_stretch P_ ((struct it *));
897 static void load_overlay_strings P_ ((struct it *, int));
898 static int init_from_display_pos P_ ((struct it *, struct window *,
899 struct display_pos *));
900 static void reseat_to_string P_ ((struct it *, unsigned char *,
901 Lisp_Object, int, int, int, int));
902 static enum move_it_result move_it_in_display_line_to P_ ((struct it *,
903 int, int, int));
904 void move_it_vertically_backward P_ ((struct it *, int));
905 static void init_to_row_start P_ ((struct it *, struct window *,
906 struct glyph_row *));
907 static int init_to_row_end P_ ((struct it *, struct window *,
908 struct glyph_row *));
909 static void back_to_previous_line_start P_ ((struct it *));
910 static int forward_to_next_line_start P_ ((struct it *, int *));
911 static struct text_pos string_pos_nchars_ahead P_ ((struct text_pos,
912 Lisp_Object, int));
913 static struct text_pos string_pos P_ ((int, Lisp_Object));
914 static struct text_pos c_string_pos P_ ((int, unsigned char *, int));
915 static int number_of_chars P_ ((unsigned char *, int));
916 static void compute_stop_pos P_ ((struct it *));
917 static void compute_string_pos P_ ((struct text_pos *, struct text_pos,
918 Lisp_Object));
919 static int face_before_or_after_it_pos P_ ((struct it *, int));
920 static int next_overlay_change P_ ((int));
921 static int handle_single_display_prop P_ ((struct it *, Lisp_Object,
922 Lisp_Object, struct text_pos *,
923 int));
924 static int underlying_face_id P_ ((struct it *));
925 static int in_ellipses_for_invisible_text_p P_ ((struct display_pos *,
926 struct window *));
927
928 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
929 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
930
931 #ifdef HAVE_WINDOW_SYSTEM
932
933 static void update_tool_bar P_ ((struct frame *, int));
934 static void build_desired_tool_bar_string P_ ((struct frame *f));
935 static int redisplay_tool_bar P_ ((struct frame *));
936 static void display_tool_bar_line P_ ((struct it *));
937 static void notice_overwritten_cursor P_ ((struct window *,
938 enum glyph_row_area,
939 int, int, int, int));
940
941
942
943 #endif /* HAVE_WINDOW_SYSTEM */
944
945 \f
946 /***********************************************************************
947 Window display dimensions
948 ***********************************************************************/
949
950 /* Return the bottom boundary y-position for text lines in window W.
951 This is the first y position at which a line cannot start.
952 It is relative to the top of the window.
953
954 This is the height of W minus the height of a mode line, if any. */
955
956 INLINE int
957 window_text_bottom_y (w)
958 struct window *w;
959 {
960 int height = WINDOW_TOTAL_HEIGHT (w);
961
962 if (WINDOW_WANTS_MODELINE_P (w))
963 height -= CURRENT_MODE_LINE_HEIGHT (w);
964 return height;
965 }
966
967 /* Return the pixel width of display area AREA of window W. AREA < 0
968 means return the total width of W, not including fringes to
969 the left and right of the window. */
970
971 INLINE int
972 window_box_width (w, area)
973 struct window *w;
974 int area;
975 {
976 int cols = XFASTINT (w->total_cols);
977 int pixels = 0;
978
979 if (!w->pseudo_window_p)
980 {
981 cols -= WINDOW_SCROLL_BAR_COLS (w);
982
983 if (area == TEXT_AREA)
984 {
985 if (INTEGERP (w->left_margin_cols))
986 cols -= XFASTINT (w->left_margin_cols);
987 if (INTEGERP (w->right_margin_cols))
988 cols -= XFASTINT (w->right_margin_cols);
989 pixels = -WINDOW_TOTAL_FRINGE_WIDTH (w);
990 }
991 else if (area == LEFT_MARGIN_AREA)
992 {
993 cols = (INTEGERP (w->left_margin_cols)
994 ? XFASTINT (w->left_margin_cols) : 0);
995 pixels = 0;
996 }
997 else if (area == RIGHT_MARGIN_AREA)
998 {
999 cols = (INTEGERP (w->right_margin_cols)
1000 ? XFASTINT (w->right_margin_cols) : 0);
1001 pixels = 0;
1002 }
1003 }
1004
1005 return cols * WINDOW_FRAME_COLUMN_WIDTH (w) + pixels;
1006 }
1007
1008
1009 /* Return the pixel height of the display area of window W, not
1010 including mode lines of W, if any. */
1011
1012 INLINE int
1013 window_box_height (w)
1014 struct window *w;
1015 {
1016 struct frame *f = XFRAME (w->frame);
1017 int height = WINDOW_TOTAL_HEIGHT (w);
1018
1019 xassert (height >= 0);
1020
1021 /* Note: the code below that determines the mode-line/header-line
1022 height is essentially the same as that contained in the macro
1023 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
1024 the appropriate glyph row has its `mode_line_p' flag set,
1025 and if it doesn't, uses estimate_mode_line_height instead. */
1026
1027 if (WINDOW_WANTS_MODELINE_P (w))
1028 {
1029 struct glyph_row *ml_row
1030 = (w->current_matrix && w->current_matrix->rows
1031 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
1032 : 0);
1033 if (ml_row && ml_row->mode_line_p)
1034 height -= ml_row->height;
1035 else
1036 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
1037 }
1038
1039 if (WINDOW_WANTS_HEADER_LINE_P (w))
1040 {
1041 struct glyph_row *hl_row
1042 = (w->current_matrix && w->current_matrix->rows
1043 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1044 : 0);
1045 if (hl_row && hl_row->mode_line_p)
1046 height -= hl_row->height;
1047 else
1048 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1049 }
1050
1051 /* With a very small font and a mode-line that's taller than
1052 default, we might end up with a negative height. */
1053 return max (0, height);
1054 }
1055
1056 /* Return the window-relative coordinate of the left edge of display
1057 area AREA of window W. AREA < 0 means return the left edge of the
1058 whole window, to the right of the left fringe of W. */
1059
1060 INLINE int
1061 window_box_left_offset (w, area)
1062 struct window *w;
1063 int area;
1064 {
1065 int x;
1066
1067 if (w->pseudo_window_p)
1068 return 0;
1069
1070 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1071
1072 if (area == TEXT_AREA)
1073 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1074 + window_box_width (w, LEFT_MARGIN_AREA));
1075 else if (area == RIGHT_MARGIN_AREA)
1076 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1077 + window_box_width (w, LEFT_MARGIN_AREA)
1078 + window_box_width (w, TEXT_AREA)
1079 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1080 ? 0
1081 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1082 else if (area == LEFT_MARGIN_AREA
1083 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1084 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1085
1086 return x;
1087 }
1088
1089
1090 /* Return the window-relative coordinate of the right edge of display
1091 area AREA of window W. AREA < 0 means return the left edge of the
1092 whole window, to the left of the right fringe of W. */
1093
1094 INLINE int
1095 window_box_right_offset (w, area)
1096 struct window *w;
1097 int area;
1098 {
1099 return window_box_left_offset (w, area) + window_box_width (w, area);
1100 }
1101
1102 /* Return the frame-relative coordinate of the left edge of display
1103 area AREA of window W. AREA < 0 means return the left edge of the
1104 whole window, to the right of the left fringe of W. */
1105
1106 INLINE int
1107 window_box_left (w, area)
1108 struct window *w;
1109 int area;
1110 {
1111 struct frame *f = XFRAME (w->frame);
1112 int x;
1113
1114 if (w->pseudo_window_p)
1115 return FRAME_INTERNAL_BORDER_WIDTH (f);
1116
1117 x = (WINDOW_LEFT_EDGE_X (w)
1118 + window_box_left_offset (w, area));
1119
1120 return x;
1121 }
1122
1123
1124 /* Return the frame-relative coordinate of the right edge of display
1125 area AREA of window W. AREA < 0 means return the left edge of the
1126 whole window, to the left of the right fringe of W. */
1127
1128 INLINE int
1129 window_box_right (w, area)
1130 struct window *w;
1131 int area;
1132 {
1133 return window_box_left (w, area) + window_box_width (w, area);
1134 }
1135
1136 /* Get the bounding box of the display area AREA of window W, without
1137 mode lines, in frame-relative coordinates. AREA < 0 means the
1138 whole window, not including the left and right fringes of
1139 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1140 coordinates of the upper-left corner of the box. Return in
1141 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1142
1143 INLINE void
1144 window_box (w, area, box_x, box_y, box_width, box_height)
1145 struct window *w;
1146 int area;
1147 int *box_x, *box_y, *box_width, *box_height;
1148 {
1149 if (box_width)
1150 *box_width = window_box_width (w, area);
1151 if (box_height)
1152 *box_height = window_box_height (w);
1153 if (box_x)
1154 *box_x = window_box_left (w, area);
1155 if (box_y)
1156 {
1157 *box_y = WINDOW_TOP_EDGE_Y (w);
1158 if (WINDOW_WANTS_HEADER_LINE_P (w))
1159 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1160 }
1161 }
1162
1163
1164 /* Get the bounding box of the display area AREA of window W, without
1165 mode lines. AREA < 0 means the whole window, not including the
1166 left and right fringe of the window. Return in *TOP_LEFT_X
1167 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1168 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1169 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1170 box. */
1171
1172 INLINE void
1173 window_box_edges (w, area, top_left_x, top_left_y,
1174 bottom_right_x, bottom_right_y)
1175 struct window *w;
1176 int area;
1177 int *top_left_x, *top_left_y, *bottom_right_x, *bottom_right_y;
1178 {
1179 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
1180 bottom_right_y);
1181 *bottom_right_x += *top_left_x;
1182 *bottom_right_y += *top_left_y;
1183 }
1184
1185
1186 \f
1187 /***********************************************************************
1188 Utilities
1189 ***********************************************************************/
1190
1191 /* Return the bottom y-position of the line the iterator IT is in.
1192 This can modify IT's settings. */
1193
1194 int
1195 line_bottom_y (it)
1196 struct it *it;
1197 {
1198 int line_height = it->max_ascent + it->max_descent;
1199 int line_top_y = it->current_y;
1200
1201 if (line_height == 0)
1202 {
1203 if (last_height)
1204 line_height = last_height;
1205 else if (IT_CHARPOS (*it) < ZV)
1206 {
1207 move_it_by_lines (it, 1, 1);
1208 line_height = (it->max_ascent || it->max_descent
1209 ? it->max_ascent + it->max_descent
1210 : last_height);
1211 }
1212 else
1213 {
1214 struct glyph_row *row = it->glyph_row;
1215
1216 /* Use the default character height. */
1217 it->glyph_row = NULL;
1218 it->what = IT_CHARACTER;
1219 it->c = ' ';
1220 it->len = 1;
1221 PRODUCE_GLYPHS (it);
1222 line_height = it->ascent + it->descent;
1223 it->glyph_row = row;
1224 }
1225 }
1226
1227 return line_top_y + line_height;
1228 }
1229
1230
1231 /* Return 1 if position CHARPOS is visible in window W. Set *FULLY to
1232 1 if POS is visible and the line containing POS is fully visible.
1233 EXACT_MODE_LINE_HEIGHTS_P non-zero means compute exact mode-line
1234 and header-lines heights. */
1235
1236 int
1237 pos_visible_p (w, charpos, fully, x, y, exact_mode_line_heights_p)
1238 struct window *w;
1239 int charpos, *fully, *x, *y, exact_mode_line_heights_p;
1240 {
1241 struct it it;
1242 struct text_pos top;
1243 int visible_p;
1244 struct buffer *old_buffer = NULL;
1245
1246 if (XBUFFER (w->buffer) != current_buffer)
1247 {
1248 old_buffer = current_buffer;
1249 set_buffer_internal_1 (XBUFFER (w->buffer));
1250 }
1251
1252 *fully = visible_p = 0;
1253 SET_TEXT_POS_FROM_MARKER (top, w->start);
1254
1255 /* Compute exact mode line heights, if requested. */
1256 if (exact_mode_line_heights_p)
1257 {
1258 if (WINDOW_WANTS_MODELINE_P (w))
1259 current_mode_line_height
1260 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1261 current_buffer->mode_line_format);
1262
1263 if (WINDOW_WANTS_HEADER_LINE_P (w))
1264 current_header_line_height
1265 = display_mode_line (w, HEADER_LINE_FACE_ID,
1266 current_buffer->header_line_format);
1267 }
1268
1269 start_display (&it, w, top);
1270 move_it_to (&it, charpos, 0, it.last_visible_y, -1,
1271 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
1272
1273 /* Note that we may overshoot because of invisible text. */
1274 if (IT_CHARPOS (it) >= charpos)
1275 {
1276 int top_y = it.current_y;
1277 int bottom_y = line_bottom_y (&it);
1278 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1279
1280 if (top_y < window_top_y)
1281 visible_p = bottom_y > window_top_y;
1282 else if (top_y < it.last_visible_y)
1283 {
1284 visible_p = 1;
1285 *fully = bottom_y <= it.last_visible_y;
1286 }
1287 if (visible_p && x)
1288 {
1289 *x = it.current_x;
1290 *y = max (top_y + it.max_ascent - it.ascent, window_top_y);
1291 }
1292 }
1293 else if (it.current_y + it.max_ascent + it.max_descent > it.last_visible_y)
1294 {
1295 struct it it2;
1296
1297 it2 = it;
1298 move_it_by_lines (&it, 1, 0);
1299 if (charpos < IT_CHARPOS (it))
1300 {
1301 visible_p = 1;
1302 if (x)
1303 {
1304 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1305 *x = it2.current_x;
1306 *y = it2.current_y + it2.max_ascent - it2.ascent;
1307 }
1308 }
1309 }
1310
1311 if (old_buffer)
1312 set_buffer_internal_1 (old_buffer);
1313
1314 current_header_line_height = current_mode_line_height = -1;
1315
1316 return visible_p;
1317 }
1318
1319
1320 /* Return the next character from STR which is MAXLEN bytes long.
1321 Return in *LEN the length of the character. This is like
1322 STRING_CHAR_AND_LENGTH but never returns an invalid character. If
1323 we find one, we return a `?', but with the length of the invalid
1324 character. */
1325
1326 static INLINE int
1327 string_char_and_length (str, maxlen, len)
1328 const unsigned char *str;
1329 int maxlen, *len;
1330 {
1331 int c;
1332
1333 c = STRING_CHAR_AND_LENGTH (str, maxlen, *len);
1334 if (!CHAR_VALID_P (c, 1))
1335 /* We may not change the length here because other places in Emacs
1336 don't use this function, i.e. they silently accept invalid
1337 characters. */
1338 c = '?';
1339
1340 return c;
1341 }
1342
1343
1344
1345 /* Given a position POS containing a valid character and byte position
1346 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1347
1348 static struct text_pos
1349 string_pos_nchars_ahead (pos, string, nchars)
1350 struct text_pos pos;
1351 Lisp_Object string;
1352 int nchars;
1353 {
1354 xassert (STRINGP (string) && nchars >= 0);
1355
1356 if (STRING_MULTIBYTE (string))
1357 {
1358 int rest = SBYTES (string) - BYTEPOS (pos);
1359 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1360 int len;
1361
1362 while (nchars--)
1363 {
1364 string_char_and_length (p, rest, &len);
1365 p += len, rest -= len;
1366 xassert (rest >= 0);
1367 CHARPOS (pos) += 1;
1368 BYTEPOS (pos) += len;
1369 }
1370 }
1371 else
1372 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1373
1374 return pos;
1375 }
1376
1377
1378 /* Value is the text position, i.e. character and byte position,
1379 for character position CHARPOS in STRING. */
1380
1381 static INLINE struct text_pos
1382 string_pos (charpos, string)
1383 int charpos;
1384 Lisp_Object string;
1385 {
1386 struct text_pos pos;
1387 xassert (STRINGP (string));
1388 xassert (charpos >= 0);
1389 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1390 return pos;
1391 }
1392
1393
1394 /* Value is a text position, i.e. character and byte position, for
1395 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1396 means recognize multibyte characters. */
1397
1398 static struct text_pos
1399 c_string_pos (charpos, s, multibyte_p)
1400 int charpos;
1401 unsigned char *s;
1402 int multibyte_p;
1403 {
1404 struct text_pos pos;
1405
1406 xassert (s != NULL);
1407 xassert (charpos >= 0);
1408
1409 if (multibyte_p)
1410 {
1411 int rest = strlen (s), len;
1412
1413 SET_TEXT_POS (pos, 0, 0);
1414 while (charpos--)
1415 {
1416 string_char_and_length (s, rest, &len);
1417 s += len, rest -= len;
1418 xassert (rest >= 0);
1419 CHARPOS (pos) += 1;
1420 BYTEPOS (pos) += len;
1421 }
1422 }
1423 else
1424 SET_TEXT_POS (pos, charpos, charpos);
1425
1426 return pos;
1427 }
1428
1429
1430 /* Value is the number of characters in C string S. MULTIBYTE_P
1431 non-zero means recognize multibyte characters. */
1432
1433 static int
1434 number_of_chars (s, multibyte_p)
1435 unsigned char *s;
1436 int multibyte_p;
1437 {
1438 int nchars;
1439
1440 if (multibyte_p)
1441 {
1442 int rest = strlen (s), len;
1443 unsigned char *p = (unsigned char *) s;
1444
1445 for (nchars = 0; rest > 0; ++nchars)
1446 {
1447 string_char_and_length (p, rest, &len);
1448 rest -= len, p += len;
1449 }
1450 }
1451 else
1452 nchars = strlen (s);
1453
1454 return nchars;
1455 }
1456
1457
1458 /* Compute byte position NEWPOS->bytepos corresponding to
1459 NEWPOS->charpos. POS is a known position in string STRING.
1460 NEWPOS->charpos must be >= POS.charpos. */
1461
1462 static void
1463 compute_string_pos (newpos, pos, string)
1464 struct text_pos *newpos, pos;
1465 Lisp_Object string;
1466 {
1467 xassert (STRINGP (string));
1468 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1469
1470 if (STRING_MULTIBYTE (string))
1471 *newpos = string_pos_nchars_ahead (pos, string,
1472 CHARPOS (*newpos) - CHARPOS (pos));
1473 else
1474 BYTEPOS (*newpos) = CHARPOS (*newpos);
1475 }
1476
1477 /* EXPORT:
1478 Return an estimation of the pixel height of mode or top lines on
1479 frame F. FACE_ID specifies what line's height to estimate. */
1480
1481 int
1482 estimate_mode_line_height (f, face_id)
1483 struct frame *f;
1484 enum face_id face_id;
1485 {
1486 #ifdef HAVE_WINDOW_SYSTEM
1487 if (FRAME_WINDOW_P (f))
1488 {
1489 int height = FONT_HEIGHT (FRAME_FONT (f));
1490
1491 /* This function is called so early when Emacs starts that the face
1492 cache and mode line face are not yet initialized. */
1493 if (FRAME_FACE_CACHE (f))
1494 {
1495 struct face *face = FACE_FROM_ID (f, face_id);
1496 if (face)
1497 {
1498 if (face->font)
1499 height = FONT_HEIGHT (face->font);
1500 if (face->box_line_width > 0)
1501 height += 2 * face->box_line_width;
1502 }
1503 }
1504
1505 return height;
1506 }
1507 #endif
1508
1509 return 1;
1510 }
1511
1512 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1513 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1514 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1515 not force the value into range. */
1516
1517 void
1518 pixel_to_glyph_coords (f, pix_x, pix_y, x, y, bounds, noclip)
1519 FRAME_PTR f;
1520 register int pix_x, pix_y;
1521 int *x, *y;
1522 NativeRectangle *bounds;
1523 int noclip;
1524 {
1525
1526 #ifdef HAVE_WINDOW_SYSTEM
1527 if (FRAME_WINDOW_P (f))
1528 {
1529 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1530 even for negative values. */
1531 if (pix_x < 0)
1532 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1533 if (pix_y < 0)
1534 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1535
1536 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1537 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1538
1539 if (bounds)
1540 STORE_NATIVE_RECT (*bounds,
1541 FRAME_COL_TO_PIXEL_X (f, pix_x),
1542 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1543 FRAME_COLUMN_WIDTH (f) - 1,
1544 FRAME_LINE_HEIGHT (f) - 1);
1545
1546 if (!noclip)
1547 {
1548 if (pix_x < 0)
1549 pix_x = 0;
1550 else if (pix_x > FRAME_TOTAL_COLS (f))
1551 pix_x = FRAME_TOTAL_COLS (f);
1552
1553 if (pix_y < 0)
1554 pix_y = 0;
1555 else if (pix_y > FRAME_LINES (f))
1556 pix_y = FRAME_LINES (f);
1557 }
1558 }
1559 #endif
1560
1561 *x = pix_x;
1562 *y = pix_y;
1563 }
1564
1565
1566 /* Given HPOS/VPOS in the current matrix of W, return corresponding
1567 frame-relative pixel positions in *FRAME_X and *FRAME_Y. If we
1568 can't tell the positions because W's display is not up to date,
1569 return 0. */
1570
1571 int
1572 glyph_to_pixel_coords (w, hpos, vpos, frame_x, frame_y)
1573 struct window *w;
1574 int hpos, vpos;
1575 int *frame_x, *frame_y;
1576 {
1577 #ifdef HAVE_WINDOW_SYSTEM
1578 if (FRAME_WINDOW_P (XFRAME (WINDOW_FRAME (w))))
1579 {
1580 int success_p;
1581
1582 xassert (hpos >= 0 && hpos < w->current_matrix->matrix_w);
1583 xassert (vpos >= 0 && vpos < w->current_matrix->matrix_h);
1584
1585 if (display_completed)
1586 {
1587 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
1588 struct glyph *glyph = row->glyphs[TEXT_AREA];
1589 struct glyph *end = glyph + min (hpos, row->used[TEXT_AREA]);
1590
1591 hpos = row->x;
1592 vpos = row->y;
1593 while (glyph < end)
1594 {
1595 hpos += glyph->pixel_width;
1596 ++glyph;
1597 }
1598
1599 /* If first glyph is partially visible, its first visible position is still 0. */
1600 if (hpos < 0)
1601 hpos = 0;
1602
1603 success_p = 1;
1604 }
1605 else
1606 {
1607 hpos = vpos = 0;
1608 success_p = 0;
1609 }
1610
1611 *frame_x = WINDOW_TO_FRAME_PIXEL_X (w, hpos);
1612 *frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, vpos);
1613 return success_p;
1614 }
1615 #endif
1616
1617 *frame_x = hpos;
1618 *frame_y = vpos;
1619 return 1;
1620 }
1621
1622
1623 #ifdef HAVE_WINDOW_SYSTEM
1624
1625 /* Find the glyph under window-relative coordinates X/Y in window W.
1626 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1627 strings. Return in *HPOS and *VPOS the row and column number of
1628 the glyph found. Return in *AREA the glyph area containing X.
1629 Value is a pointer to the glyph found or null if X/Y is not on
1630 text, or we can't tell because W's current matrix is not up to
1631 date. */
1632
1633 static struct glyph *
1634 x_y_to_hpos_vpos (w, x, y, hpos, vpos, dx, dy, area)
1635 struct window *w;
1636 int x, y;
1637 int *hpos, *vpos, *dx, *dy, *area;
1638 {
1639 struct glyph *glyph, *end;
1640 struct glyph_row *row = NULL;
1641 int x0, i;
1642
1643 /* Find row containing Y. Give up if some row is not enabled. */
1644 for (i = 0; i < w->current_matrix->nrows; ++i)
1645 {
1646 row = MATRIX_ROW (w->current_matrix, i);
1647 if (!row->enabled_p)
1648 return NULL;
1649 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1650 break;
1651 }
1652
1653 *vpos = i;
1654 *hpos = 0;
1655
1656 /* Give up if Y is not in the window. */
1657 if (i == w->current_matrix->nrows)
1658 return NULL;
1659
1660 /* Get the glyph area containing X. */
1661 if (w->pseudo_window_p)
1662 {
1663 *area = TEXT_AREA;
1664 x0 = 0;
1665 }
1666 else
1667 {
1668 if (x < window_box_left_offset (w, TEXT_AREA))
1669 {
1670 *area = LEFT_MARGIN_AREA;
1671 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1672 }
1673 else if (x < window_box_right_offset (w, TEXT_AREA))
1674 {
1675 *area = TEXT_AREA;
1676 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1677 }
1678 else
1679 {
1680 *area = RIGHT_MARGIN_AREA;
1681 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1682 }
1683 }
1684
1685 /* Find glyph containing X. */
1686 glyph = row->glyphs[*area];
1687 end = glyph + row->used[*area];
1688 x -= x0;
1689 while (glyph < end && x >= glyph->pixel_width)
1690 {
1691 x -= glyph->pixel_width;
1692 ++glyph;
1693 }
1694
1695 if (glyph == end)
1696 return NULL;
1697
1698 if (dx)
1699 {
1700 *dx = x;
1701 *dy = y - (row->y + row->ascent - glyph->ascent);
1702 }
1703
1704 *hpos = glyph - row->glyphs[*area];
1705 return glyph;
1706 }
1707
1708
1709 /* EXPORT:
1710 Convert frame-relative x/y to coordinates relative to window W.
1711 Takes pseudo-windows into account. */
1712
1713 void
1714 frame_to_window_pixel_xy (w, x, y)
1715 struct window *w;
1716 int *x, *y;
1717 {
1718 if (w->pseudo_window_p)
1719 {
1720 /* A pseudo-window is always full-width, and starts at the
1721 left edge of the frame, plus a frame border. */
1722 struct frame *f = XFRAME (w->frame);
1723 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1724 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1725 }
1726 else
1727 {
1728 *x -= WINDOW_LEFT_EDGE_X (w);
1729 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1730 }
1731 }
1732
1733 /* EXPORT:
1734 Return in *R the clipping rectangle for glyph string S. */
1735
1736 void
1737 get_glyph_string_clip_rect (s, nr)
1738 struct glyph_string *s;
1739 NativeRectangle *nr;
1740 {
1741 XRectangle r;
1742
1743 if (s->row->full_width_p)
1744 {
1745 /* Draw full-width. X coordinates are relative to S->w->left_col. */
1746 r.x = WINDOW_LEFT_EDGE_X (s->w);
1747 r.width = WINDOW_TOTAL_WIDTH (s->w);
1748
1749 /* Unless displaying a mode or menu bar line, which are always
1750 fully visible, clip to the visible part of the row. */
1751 if (s->w->pseudo_window_p)
1752 r.height = s->row->visible_height;
1753 else
1754 r.height = s->height;
1755 }
1756 else
1757 {
1758 /* This is a text line that may be partially visible. */
1759 r.x = window_box_left (s->w, s->area);
1760 r.width = window_box_width (s->w, s->area);
1761 r.height = s->row->visible_height;
1762 }
1763
1764 /* If S draws overlapping rows, it's sufficient to use the top and
1765 bottom of the window for clipping because this glyph string
1766 intentionally draws over other lines. */
1767 if (s->for_overlaps_p)
1768 {
1769 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1770 r.height = window_text_bottom_y (s->w) - r.y;
1771 }
1772 else
1773 {
1774 /* Don't use S->y for clipping because it doesn't take partially
1775 visible lines into account. For example, it can be negative for
1776 partially visible lines at the top of a window. */
1777 if (!s->row->full_width_p
1778 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
1779 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1780 else
1781 r.y = max (0, s->row->y);
1782
1783 /* If drawing a tool-bar window, draw it over the internal border
1784 at the top of the window. */
1785 if (s->w == XWINDOW (s->f->tool_bar_window))
1786 r.y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
1787 }
1788
1789 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
1790
1791 /* If drawing the cursor, don't let glyph draw outside its
1792 advertised boundaries. Cleartype does this under some circumstances. */
1793 if (s->hl == DRAW_CURSOR)
1794 {
1795 struct glyph *glyph = s->first_glyph;
1796 int height;
1797
1798 if (s->x > r.x)
1799 {
1800 r.width -= s->x - r.x;
1801 r.x = s->x;
1802 }
1803 r.width = min (r.width, glyph->pixel_width);
1804
1805 /* Don't draw cursor glyph taller than our actual glyph. */
1806 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
1807 if (height < r.height)
1808 {
1809 int max_y = r.y + r.height;
1810 r.y = min (max_y, s->ybase + glyph->descent - height);
1811 r.height = min (max_y - r.y, height);
1812 }
1813 }
1814
1815 #ifdef CONVERT_FROM_XRECT
1816 CONVERT_FROM_XRECT (r, *nr);
1817 #else
1818 *nr = r;
1819 #endif
1820 }
1821
1822 #endif /* HAVE_WINDOW_SYSTEM */
1823
1824 \f
1825 /***********************************************************************
1826 Lisp form evaluation
1827 ***********************************************************************/
1828
1829 /* Error handler for safe_eval and safe_call. */
1830
1831 static Lisp_Object
1832 safe_eval_handler (arg)
1833 Lisp_Object arg;
1834 {
1835 add_to_log ("Error during redisplay: %s", arg, Qnil);
1836 return Qnil;
1837 }
1838
1839
1840 /* Evaluate SEXPR and return the result, or nil if something went
1841 wrong. Prevent redisplay during the evaluation. */
1842
1843 Lisp_Object
1844 safe_eval (sexpr)
1845 Lisp_Object sexpr;
1846 {
1847 Lisp_Object val;
1848
1849 if (inhibit_eval_during_redisplay)
1850 val = Qnil;
1851 else
1852 {
1853 int count = SPECPDL_INDEX ();
1854 struct gcpro gcpro1;
1855
1856 GCPRO1 (sexpr);
1857 specbind (Qinhibit_redisplay, Qt);
1858 /* Use Qt to ensure debugger does not run,
1859 so there is no possibility of wanting to redisplay. */
1860 val = internal_condition_case_1 (Feval, sexpr, Qt,
1861 safe_eval_handler);
1862 UNGCPRO;
1863 val = unbind_to (count, val);
1864 }
1865
1866 return val;
1867 }
1868
1869
1870 /* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
1871 Return the result, or nil if something went wrong. Prevent
1872 redisplay during the evaluation. */
1873
1874 Lisp_Object
1875 safe_call (nargs, args)
1876 int nargs;
1877 Lisp_Object *args;
1878 {
1879 Lisp_Object val;
1880
1881 if (inhibit_eval_during_redisplay)
1882 val = Qnil;
1883 else
1884 {
1885 int count = SPECPDL_INDEX ();
1886 struct gcpro gcpro1;
1887
1888 GCPRO1 (args[0]);
1889 gcpro1.nvars = nargs;
1890 specbind (Qinhibit_redisplay, Qt);
1891 /* Use Qt to ensure debugger does not run,
1892 so there is no possibility of wanting to redisplay. */
1893 val = internal_condition_case_2 (Ffuncall, nargs, args, Qt,
1894 safe_eval_handler);
1895 UNGCPRO;
1896 val = unbind_to (count, val);
1897 }
1898
1899 return val;
1900 }
1901
1902
1903 /* Call function FN with one argument ARG.
1904 Return the result, or nil if something went wrong. */
1905
1906 Lisp_Object
1907 safe_call1 (fn, arg)
1908 Lisp_Object fn, arg;
1909 {
1910 Lisp_Object args[2];
1911 args[0] = fn;
1912 args[1] = arg;
1913 return safe_call (2, args);
1914 }
1915
1916
1917 \f
1918 /***********************************************************************
1919 Debugging
1920 ***********************************************************************/
1921
1922 #if 0
1923
1924 /* Define CHECK_IT to perform sanity checks on iterators.
1925 This is for debugging. It is too slow to do unconditionally. */
1926
1927 static void
1928 check_it (it)
1929 struct it *it;
1930 {
1931 if (it->method == next_element_from_string)
1932 {
1933 xassert (STRINGP (it->string));
1934 xassert (IT_STRING_CHARPOS (*it) >= 0);
1935 }
1936 else
1937 {
1938 xassert (IT_STRING_CHARPOS (*it) < 0);
1939 if (it->method == next_element_from_buffer)
1940 {
1941 /* Check that character and byte positions agree. */
1942 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
1943 }
1944 }
1945
1946 if (it->dpvec)
1947 xassert (it->current.dpvec_index >= 0);
1948 else
1949 xassert (it->current.dpvec_index < 0);
1950 }
1951
1952 #define CHECK_IT(IT) check_it ((IT))
1953
1954 #else /* not 0 */
1955
1956 #define CHECK_IT(IT) (void) 0
1957
1958 #endif /* not 0 */
1959
1960
1961 #if GLYPH_DEBUG
1962
1963 /* Check that the window end of window W is what we expect it
1964 to be---the last row in the current matrix displaying text. */
1965
1966 static void
1967 check_window_end (w)
1968 struct window *w;
1969 {
1970 if (!MINI_WINDOW_P (w)
1971 && !NILP (w->window_end_valid))
1972 {
1973 struct glyph_row *row;
1974 xassert ((row = MATRIX_ROW (w->current_matrix,
1975 XFASTINT (w->window_end_vpos)),
1976 !row->enabled_p
1977 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
1978 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
1979 }
1980 }
1981
1982 #define CHECK_WINDOW_END(W) check_window_end ((W))
1983
1984 #else /* not GLYPH_DEBUG */
1985
1986 #define CHECK_WINDOW_END(W) (void) 0
1987
1988 #endif /* not GLYPH_DEBUG */
1989
1990
1991 \f
1992 /***********************************************************************
1993 Iterator initialization
1994 ***********************************************************************/
1995
1996 /* Initialize IT for displaying current_buffer in window W, starting
1997 at character position CHARPOS. CHARPOS < 0 means that no buffer
1998 position is specified which is useful when the iterator is assigned
1999 a position later. BYTEPOS is the byte position corresponding to
2000 CHARPOS. BYTEPOS < 0 means compute it from CHARPOS.
2001
2002 If ROW is not null, calls to produce_glyphs with IT as parameter
2003 will produce glyphs in that row.
2004
2005 BASE_FACE_ID is the id of a base face to use. It must be one of
2006 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2007 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2008 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2009
2010 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2011 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2012 will be initialized to use the corresponding mode line glyph row of
2013 the desired matrix of W. */
2014
2015 void
2016 init_iterator (it, w, charpos, bytepos, row, base_face_id)
2017 struct it *it;
2018 struct window *w;
2019 int charpos, bytepos;
2020 struct glyph_row *row;
2021 enum face_id base_face_id;
2022 {
2023 int highlight_region_p;
2024
2025 /* Some precondition checks. */
2026 xassert (w != NULL && it != NULL);
2027 xassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2028 && charpos <= ZV));
2029
2030 /* If face attributes have been changed since the last redisplay,
2031 free realized faces now because they depend on face definitions
2032 that might have changed. Don't free faces while there might be
2033 desired matrices pending which reference these faces. */
2034 if (face_change_count && !inhibit_free_realized_faces)
2035 {
2036 face_change_count = 0;
2037 free_all_realized_faces (Qnil);
2038 }
2039
2040 /* Use one of the mode line rows of W's desired matrix if
2041 appropriate. */
2042 if (row == NULL)
2043 {
2044 if (base_face_id == MODE_LINE_FACE_ID
2045 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2046 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2047 else if (base_face_id == HEADER_LINE_FACE_ID)
2048 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2049 }
2050
2051 /* Clear IT. */
2052 bzero (it, sizeof *it);
2053 it->current.overlay_string_index = -1;
2054 it->current.dpvec_index = -1;
2055 it->base_face_id = base_face_id;
2056 it->string = Qnil;
2057 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2058
2059 /* The window in which we iterate over current_buffer: */
2060 XSETWINDOW (it->window, w);
2061 it->w = w;
2062 it->f = XFRAME (w->frame);
2063
2064 /* Extra space between lines (on window systems only). */
2065 if (base_face_id == DEFAULT_FACE_ID
2066 && FRAME_WINDOW_P (it->f))
2067 {
2068 if (NATNUMP (current_buffer->extra_line_spacing))
2069 it->extra_line_spacing = XFASTINT (current_buffer->extra_line_spacing);
2070 else if (FLOATP (current_buffer->extra_line_spacing))
2071 it->extra_line_spacing = (XFLOAT_DATA (current_buffer->extra_line_spacing)
2072 * FRAME_LINE_HEIGHT (it->f));
2073 else if (it->f->extra_line_spacing > 0)
2074 it->extra_line_spacing = it->f->extra_line_spacing;
2075 }
2076
2077 /* If realized faces have been removed, e.g. because of face
2078 attribute changes of named faces, recompute them. When running
2079 in batch mode, the face cache of Vterminal_frame is null. If
2080 we happen to get called, make a dummy face cache. */
2081 if (noninteractive && FRAME_FACE_CACHE (it->f) == NULL)
2082 init_frame_faces (it->f);
2083 if (FRAME_FACE_CACHE (it->f)->used == 0)
2084 recompute_basic_faces (it->f);
2085
2086 /* Current value of the `slice', `space-width', and 'height' properties. */
2087 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2088 it->space_width = Qnil;
2089 it->font_height = Qnil;
2090 it->override_ascent = -1;
2091
2092 /* Are control characters displayed as `^C'? */
2093 it->ctl_arrow_p = !NILP (current_buffer->ctl_arrow);
2094
2095 /* -1 means everything between a CR and the following line end
2096 is invisible. >0 means lines indented more than this value are
2097 invisible. */
2098 it->selective = (INTEGERP (current_buffer->selective_display)
2099 ? XFASTINT (current_buffer->selective_display)
2100 : (!NILP (current_buffer->selective_display)
2101 ? -1 : 0));
2102 it->selective_display_ellipsis_p
2103 = !NILP (current_buffer->selective_display_ellipses);
2104
2105 /* Display table to use. */
2106 it->dp = window_display_table (w);
2107
2108 /* Are multibyte characters enabled in current_buffer? */
2109 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
2110
2111 /* Non-zero if we should highlight the region. */
2112 highlight_region_p
2113 = (!NILP (Vtransient_mark_mode)
2114 && !NILP (current_buffer->mark_active)
2115 && XMARKER (current_buffer->mark)->buffer != 0);
2116
2117 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
2118 start and end of a visible region in window IT->w. Set both to
2119 -1 to indicate no region. */
2120 if (highlight_region_p
2121 /* Maybe highlight only in selected window. */
2122 && (/* Either show region everywhere. */
2123 highlight_nonselected_windows
2124 /* Or show region in the selected window. */
2125 || w == XWINDOW (selected_window)
2126 /* Or show the region if we are in the mini-buffer and W is
2127 the window the mini-buffer refers to. */
2128 || (MINI_WINDOW_P (XWINDOW (selected_window))
2129 && WINDOWP (minibuf_selected_window)
2130 && w == XWINDOW (minibuf_selected_window))))
2131 {
2132 int charpos = marker_position (current_buffer->mark);
2133 it->region_beg_charpos = min (PT, charpos);
2134 it->region_end_charpos = max (PT, charpos);
2135 }
2136 else
2137 it->region_beg_charpos = it->region_end_charpos = -1;
2138
2139 /* Get the position at which the redisplay_end_trigger hook should
2140 be run, if it is to be run at all. */
2141 if (MARKERP (w->redisplay_end_trigger)
2142 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2143 it->redisplay_end_trigger_charpos
2144 = marker_position (w->redisplay_end_trigger);
2145 else if (INTEGERP (w->redisplay_end_trigger))
2146 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
2147
2148 /* Correct bogus values of tab_width. */
2149 it->tab_width = XINT (current_buffer->tab_width);
2150 if (it->tab_width <= 0 || it->tab_width > 1000)
2151 it->tab_width = 8;
2152
2153 /* Are lines in the display truncated? */
2154 it->truncate_lines_p
2155 = (base_face_id != DEFAULT_FACE_ID
2156 || XINT (it->w->hscroll)
2157 || (truncate_partial_width_windows
2158 && !WINDOW_FULL_WIDTH_P (it->w))
2159 || !NILP (current_buffer->truncate_lines));
2160
2161 /* Get dimensions of truncation and continuation glyphs. These are
2162 displayed as fringe bitmaps under X, so we don't need them for such
2163 frames. */
2164 if (!FRAME_WINDOW_P (it->f))
2165 {
2166 if (it->truncate_lines_p)
2167 {
2168 /* We will need the truncation glyph. */
2169 xassert (it->glyph_row == NULL);
2170 produce_special_glyphs (it, IT_TRUNCATION);
2171 it->truncation_pixel_width = it->pixel_width;
2172 }
2173 else
2174 {
2175 /* We will need the continuation glyph. */
2176 xassert (it->glyph_row == NULL);
2177 produce_special_glyphs (it, IT_CONTINUATION);
2178 it->continuation_pixel_width = it->pixel_width;
2179 }
2180
2181 /* Reset these values to zero because the produce_special_glyphs
2182 above has changed them. */
2183 it->pixel_width = it->ascent = it->descent = 0;
2184 it->phys_ascent = it->phys_descent = 0;
2185 }
2186
2187 /* Set this after getting the dimensions of truncation and
2188 continuation glyphs, so that we don't produce glyphs when calling
2189 produce_special_glyphs, above. */
2190 it->glyph_row = row;
2191 it->area = TEXT_AREA;
2192
2193 /* Get the dimensions of the display area. The display area
2194 consists of the visible window area plus a horizontally scrolled
2195 part to the left of the window. All x-values are relative to the
2196 start of this total display area. */
2197 if (base_face_id != DEFAULT_FACE_ID)
2198 {
2199 /* Mode lines, menu bar in terminal frames. */
2200 it->first_visible_x = 0;
2201 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
2202 }
2203 else
2204 {
2205 it->first_visible_x
2206 = XFASTINT (it->w->hscroll) * FRAME_COLUMN_WIDTH (it->f);
2207 it->last_visible_x = (it->first_visible_x
2208 + window_box_width (w, TEXT_AREA));
2209
2210 /* If we truncate lines, leave room for the truncator glyph(s) at
2211 the right margin. Otherwise, leave room for the continuation
2212 glyph(s). Truncation and continuation glyphs are not inserted
2213 for window-based redisplay. */
2214 if (!FRAME_WINDOW_P (it->f))
2215 {
2216 if (it->truncate_lines_p)
2217 it->last_visible_x -= it->truncation_pixel_width;
2218 else
2219 it->last_visible_x -= it->continuation_pixel_width;
2220 }
2221
2222 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2223 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2224 }
2225
2226 /* Leave room for a border glyph. */
2227 if (!FRAME_WINDOW_P (it->f)
2228 && !WINDOW_RIGHTMOST_P (it->w))
2229 it->last_visible_x -= 1;
2230
2231 it->last_visible_y = window_text_bottom_y (w);
2232
2233 /* For mode lines and alike, arrange for the first glyph having a
2234 left box line if the face specifies a box. */
2235 if (base_face_id != DEFAULT_FACE_ID)
2236 {
2237 struct face *face;
2238
2239 it->face_id = base_face_id;
2240
2241 /* If we have a boxed mode line, make the first character appear
2242 with a left box line. */
2243 face = FACE_FROM_ID (it->f, base_face_id);
2244 if (face->box != FACE_NO_BOX)
2245 it->start_of_box_run_p = 1;
2246 }
2247
2248 /* If a buffer position was specified, set the iterator there,
2249 getting overlays and face properties from that position. */
2250 if (charpos >= BUF_BEG (current_buffer))
2251 {
2252 it->end_charpos = ZV;
2253 it->face_id = -1;
2254 IT_CHARPOS (*it) = charpos;
2255
2256 /* Compute byte position if not specified. */
2257 if (bytepos < charpos)
2258 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
2259 else
2260 IT_BYTEPOS (*it) = bytepos;
2261
2262 it->start = it->current;
2263
2264 /* Compute faces etc. */
2265 reseat (it, it->current.pos, 1);
2266 }
2267
2268 CHECK_IT (it);
2269 }
2270
2271
2272 /* Initialize IT for the display of window W with window start POS. */
2273
2274 void
2275 start_display (it, w, pos)
2276 struct it *it;
2277 struct window *w;
2278 struct text_pos pos;
2279 {
2280 struct glyph_row *row;
2281 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2282
2283 row = w->desired_matrix->rows + first_vpos;
2284 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2285 it->first_vpos = first_vpos;
2286
2287 if (!it->truncate_lines_p)
2288 {
2289 int start_at_line_beg_p;
2290 int first_y = it->current_y;
2291
2292 /* If window start is not at a line start, skip forward to POS to
2293 get the correct continuation lines width. */
2294 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2295 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2296 if (!start_at_line_beg_p)
2297 {
2298 int new_x;
2299
2300 reseat_at_previous_visible_line_start (it);
2301 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
2302
2303 new_x = it->current_x + it->pixel_width;
2304
2305 /* If lines are continued, this line may end in the middle
2306 of a multi-glyph character (e.g. a control character
2307 displayed as \003, or in the middle of an overlay
2308 string). In this case move_it_to above will not have
2309 taken us to the start of the continuation line but to the
2310 end of the continued line. */
2311 if (it->current_x > 0
2312 && !it->truncate_lines_p /* Lines are continued. */
2313 && (/* And glyph doesn't fit on the line. */
2314 new_x > it->last_visible_x
2315 /* Or it fits exactly and we're on a window
2316 system frame. */
2317 || (new_x == it->last_visible_x
2318 && FRAME_WINDOW_P (it->f))))
2319 {
2320 if (it->current.dpvec_index >= 0
2321 || it->current.overlay_string_index >= 0)
2322 {
2323 set_iterator_to_next (it, 1);
2324 move_it_in_display_line_to (it, -1, -1, 0);
2325 }
2326
2327 it->continuation_lines_width += it->current_x;
2328 }
2329
2330 /* We're starting a new display line, not affected by the
2331 height of the continued line, so clear the appropriate
2332 fields in the iterator structure. */
2333 it->max_ascent = it->max_descent = 0;
2334 it->max_phys_ascent = it->max_phys_descent = 0;
2335
2336 it->current_y = first_y;
2337 it->vpos = 0;
2338 it->current_x = it->hpos = 0;
2339 }
2340 }
2341
2342 #if 0 /* Don't assert the following because start_display is sometimes
2343 called intentionally with a window start that is not at a
2344 line start. Please leave this code in as a comment. */
2345
2346 /* Window start should be on a line start, now. */
2347 xassert (it->continuation_lines_width
2348 || IT_CHARPOS (it) == BEGV
2349 || FETCH_BYTE (IT_BYTEPOS (it) - 1) == '\n');
2350 #endif /* 0 */
2351 }
2352
2353
2354 /* Return 1 if POS is a position in ellipses displayed for invisible
2355 text. W is the window we display, for text property lookup. */
2356
2357 static int
2358 in_ellipses_for_invisible_text_p (pos, w)
2359 struct display_pos *pos;
2360 struct window *w;
2361 {
2362 Lisp_Object prop, window;
2363 int ellipses_p = 0;
2364 int charpos = CHARPOS (pos->pos);
2365
2366 /* If POS specifies a position in a display vector, this might
2367 be for an ellipsis displayed for invisible text. We won't
2368 get the iterator set up for delivering that ellipsis unless
2369 we make sure that it gets aware of the invisible text. */
2370 if (pos->dpvec_index >= 0
2371 && pos->overlay_string_index < 0
2372 && CHARPOS (pos->string_pos) < 0
2373 && charpos > BEGV
2374 && (XSETWINDOW (window, w),
2375 prop = Fget_char_property (make_number (charpos),
2376 Qinvisible, window),
2377 !TEXT_PROP_MEANS_INVISIBLE (prop)))
2378 {
2379 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
2380 window);
2381 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
2382 }
2383
2384 return ellipses_p;
2385 }
2386
2387
2388 /* Initialize IT for stepping through current_buffer in window W,
2389 starting at position POS that includes overlay string and display
2390 vector/ control character translation position information. Value
2391 is zero if there are overlay strings with newlines at POS. */
2392
2393 static int
2394 init_from_display_pos (it, w, pos)
2395 struct it *it;
2396 struct window *w;
2397 struct display_pos *pos;
2398 {
2399 int charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
2400 int i, overlay_strings_with_newlines = 0;
2401
2402 /* If POS specifies a position in a display vector, this might
2403 be for an ellipsis displayed for invisible text. We won't
2404 get the iterator set up for delivering that ellipsis unless
2405 we make sure that it gets aware of the invisible text. */
2406 if (in_ellipses_for_invisible_text_p (pos, w))
2407 {
2408 --charpos;
2409 bytepos = 0;
2410 }
2411
2412 /* Keep in mind: the call to reseat in init_iterator skips invisible
2413 text, so we might end up at a position different from POS. This
2414 is only a problem when POS is a row start after a newline and an
2415 overlay starts there with an after-string, and the overlay has an
2416 invisible property. Since we don't skip invisible text in
2417 display_line and elsewhere immediately after consuming the
2418 newline before the row start, such a POS will not be in a string,
2419 but the call to init_iterator below will move us to the
2420 after-string. */
2421 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
2422
2423 for (i = 0; i < it->n_overlay_strings; ++i)
2424 {
2425 const char *s = SDATA (it->overlay_strings[i]);
2426 const char *e = s + SBYTES (it->overlay_strings[i]);
2427
2428 while (s < e && *s != '\n')
2429 ++s;
2430
2431 if (s < e)
2432 {
2433 overlay_strings_with_newlines = 1;
2434 break;
2435 }
2436 }
2437
2438 /* If position is within an overlay string, set up IT to the right
2439 overlay string. */
2440 if (pos->overlay_string_index >= 0)
2441 {
2442 int relative_index;
2443
2444 /* If the first overlay string happens to have a `display'
2445 property for an image, the iterator will be set up for that
2446 image, and we have to undo that setup first before we can
2447 correct the overlay string index. */
2448 if (it->method == next_element_from_image)
2449 pop_it (it);
2450
2451 /* We already have the first chunk of overlay strings in
2452 IT->overlay_strings. Load more until the one for
2453 pos->overlay_string_index is in IT->overlay_strings. */
2454 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
2455 {
2456 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
2457 it->current.overlay_string_index = 0;
2458 while (n--)
2459 {
2460 load_overlay_strings (it, 0);
2461 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
2462 }
2463 }
2464
2465 it->current.overlay_string_index = pos->overlay_string_index;
2466 relative_index = (it->current.overlay_string_index
2467 % OVERLAY_STRING_CHUNK_SIZE);
2468 it->string = it->overlay_strings[relative_index];
2469 xassert (STRINGP (it->string));
2470 it->current.string_pos = pos->string_pos;
2471 it->method = next_element_from_string;
2472 }
2473
2474 #if 0 /* This is bogus because POS not having an overlay string
2475 position does not mean it's after the string. Example: A
2476 line starting with a before-string and initialization of IT
2477 to the previous row's end position. */
2478 else if (it->current.overlay_string_index >= 0)
2479 {
2480 /* If POS says we're already after an overlay string ending at
2481 POS, make sure to pop the iterator because it will be in
2482 front of that overlay string. When POS is ZV, we've thereby
2483 also ``processed'' overlay strings at ZV. */
2484 while (it->sp)
2485 pop_it (it);
2486 it->current.overlay_string_index = -1;
2487 it->method = next_element_from_buffer;
2488 if (CHARPOS (pos->pos) == ZV)
2489 it->overlay_strings_at_end_processed_p = 1;
2490 }
2491 #endif /* 0 */
2492
2493 if (CHARPOS (pos->string_pos) >= 0)
2494 {
2495 /* Recorded position is not in an overlay string, but in another
2496 string. This can only be a string from a `display' property.
2497 IT should already be filled with that string. */
2498 it->current.string_pos = pos->string_pos;
2499 xassert (STRINGP (it->string));
2500 }
2501
2502 /* Restore position in display vector translations, control
2503 character translations or ellipses. */
2504 if (pos->dpvec_index >= 0)
2505 {
2506 if (it->dpvec == NULL)
2507 get_next_display_element (it);
2508 xassert (it->dpvec && it->current.dpvec_index == 0);
2509 it->current.dpvec_index = pos->dpvec_index;
2510 }
2511
2512 CHECK_IT (it);
2513 return !overlay_strings_with_newlines;
2514 }
2515
2516
2517 /* Initialize IT for stepping through current_buffer in window W
2518 starting at ROW->start. */
2519
2520 static void
2521 init_to_row_start (it, w, row)
2522 struct it *it;
2523 struct window *w;
2524 struct glyph_row *row;
2525 {
2526 init_from_display_pos (it, w, &row->start);
2527 it->start = row->start;
2528 it->continuation_lines_width = row->continuation_lines_width;
2529 CHECK_IT (it);
2530 }
2531
2532
2533 /* Initialize IT for stepping through current_buffer in window W
2534 starting in the line following ROW, i.e. starting at ROW->end.
2535 Value is zero if there are overlay strings with newlines at ROW's
2536 end position. */
2537
2538 static int
2539 init_to_row_end (it, w, row)
2540 struct it *it;
2541 struct window *w;
2542 struct glyph_row *row;
2543 {
2544 int success = 0;
2545
2546 if (init_from_display_pos (it, w, &row->end))
2547 {
2548 if (row->continued_p)
2549 it->continuation_lines_width
2550 = row->continuation_lines_width + row->pixel_width;
2551 CHECK_IT (it);
2552 success = 1;
2553 }
2554
2555 return success;
2556 }
2557
2558
2559
2560 \f
2561 /***********************************************************************
2562 Text properties
2563 ***********************************************************************/
2564
2565 /* Called when IT reaches IT->stop_charpos. Handle text property and
2566 overlay changes. Set IT->stop_charpos to the next position where
2567 to stop. */
2568
2569 static void
2570 handle_stop (it)
2571 struct it *it;
2572 {
2573 enum prop_handled handled;
2574 int handle_overlay_change_p = 1;
2575 struct props *p;
2576
2577 it->dpvec = NULL;
2578 it->current.dpvec_index = -1;
2579
2580 do
2581 {
2582 handled = HANDLED_NORMALLY;
2583
2584 /* Call text property handlers. */
2585 for (p = it_props; p->handler; ++p)
2586 {
2587 handled = p->handler (it);
2588
2589 if (handled == HANDLED_RECOMPUTE_PROPS)
2590 break;
2591 else if (handled == HANDLED_RETURN)
2592 return;
2593 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
2594 handle_overlay_change_p = 0;
2595 }
2596
2597 if (handled != HANDLED_RECOMPUTE_PROPS)
2598 {
2599 /* Don't check for overlay strings below when set to deliver
2600 characters from a display vector. */
2601 if (it->method == next_element_from_display_vector)
2602 handle_overlay_change_p = 0;
2603
2604 /* Handle overlay changes. */
2605 if (handle_overlay_change_p)
2606 handled = handle_overlay_change (it);
2607
2608 /* Determine where to stop next. */
2609 if (handled == HANDLED_NORMALLY)
2610 compute_stop_pos (it);
2611 }
2612 }
2613 while (handled == HANDLED_RECOMPUTE_PROPS);
2614 }
2615
2616
2617 /* Compute IT->stop_charpos from text property and overlay change
2618 information for IT's current position. */
2619
2620 static void
2621 compute_stop_pos (it)
2622 struct it *it;
2623 {
2624 register INTERVAL iv, next_iv;
2625 Lisp_Object object, limit, position;
2626
2627 /* If nowhere else, stop at the end. */
2628 it->stop_charpos = it->end_charpos;
2629
2630 if (STRINGP (it->string))
2631 {
2632 /* Strings are usually short, so don't limit the search for
2633 properties. */
2634 object = it->string;
2635 limit = Qnil;
2636 position = make_number (IT_STRING_CHARPOS (*it));
2637 }
2638 else
2639 {
2640 int charpos;
2641
2642 /* If next overlay change is in front of the current stop pos
2643 (which is IT->end_charpos), stop there. Note: value of
2644 next_overlay_change is point-max if no overlay change
2645 follows. */
2646 charpos = next_overlay_change (IT_CHARPOS (*it));
2647 if (charpos < it->stop_charpos)
2648 it->stop_charpos = charpos;
2649
2650 /* If showing the region, we have to stop at the region
2651 start or end because the face might change there. */
2652 if (it->region_beg_charpos > 0)
2653 {
2654 if (IT_CHARPOS (*it) < it->region_beg_charpos)
2655 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
2656 else if (IT_CHARPOS (*it) < it->region_end_charpos)
2657 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
2658 }
2659
2660 /* Set up variables for computing the stop position from text
2661 property changes. */
2662 XSETBUFFER (object, current_buffer);
2663 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
2664 position = make_number (IT_CHARPOS (*it));
2665
2666 }
2667
2668 /* Get the interval containing IT's position. Value is a null
2669 interval if there isn't such an interval. */
2670 iv = validate_interval_range (object, &position, &position, 0);
2671 if (!NULL_INTERVAL_P (iv))
2672 {
2673 Lisp_Object values_here[LAST_PROP_IDX];
2674 struct props *p;
2675
2676 /* Get properties here. */
2677 for (p = it_props; p->handler; ++p)
2678 values_here[p->idx] = textget (iv->plist, *p->name);
2679
2680 /* Look for an interval following iv that has different
2681 properties. */
2682 for (next_iv = next_interval (iv);
2683 (!NULL_INTERVAL_P (next_iv)
2684 && (NILP (limit)
2685 || XFASTINT (limit) > next_iv->position));
2686 next_iv = next_interval (next_iv))
2687 {
2688 for (p = it_props; p->handler; ++p)
2689 {
2690 Lisp_Object new_value;
2691
2692 new_value = textget (next_iv->plist, *p->name);
2693 if (!EQ (values_here[p->idx], new_value))
2694 break;
2695 }
2696
2697 if (p->handler)
2698 break;
2699 }
2700
2701 if (!NULL_INTERVAL_P (next_iv))
2702 {
2703 if (INTEGERP (limit)
2704 && next_iv->position >= XFASTINT (limit))
2705 /* No text property change up to limit. */
2706 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
2707 else
2708 /* Text properties change in next_iv. */
2709 it->stop_charpos = min (it->stop_charpos, next_iv->position);
2710 }
2711 }
2712
2713 xassert (STRINGP (it->string)
2714 || (it->stop_charpos >= BEGV
2715 && it->stop_charpos >= IT_CHARPOS (*it)));
2716 }
2717
2718
2719 /* Return the position of the next overlay change after POS in
2720 current_buffer. Value is point-max if no overlay change
2721 follows. This is like `next-overlay-change' but doesn't use
2722 xmalloc. */
2723
2724 static int
2725 next_overlay_change (pos)
2726 int pos;
2727 {
2728 int noverlays;
2729 int endpos;
2730 Lisp_Object *overlays;
2731 int i;
2732
2733 /* Get all overlays at the given position. */
2734 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
2735
2736 /* If any of these overlays ends before endpos,
2737 use its ending point instead. */
2738 for (i = 0; i < noverlays; ++i)
2739 {
2740 Lisp_Object oend;
2741 int oendpos;
2742
2743 oend = OVERLAY_END (overlays[i]);
2744 oendpos = OVERLAY_POSITION (oend);
2745 endpos = min (endpos, oendpos);
2746 }
2747
2748 return endpos;
2749 }
2750
2751
2752 \f
2753 /***********************************************************************
2754 Fontification
2755 ***********************************************************************/
2756
2757 /* Handle changes in the `fontified' property of the current buffer by
2758 calling hook functions from Qfontification_functions to fontify
2759 regions of text. */
2760
2761 static enum prop_handled
2762 handle_fontified_prop (it)
2763 struct it *it;
2764 {
2765 Lisp_Object prop, pos;
2766 enum prop_handled handled = HANDLED_NORMALLY;
2767
2768 /* Get the value of the `fontified' property at IT's current buffer
2769 position. (The `fontified' property doesn't have a special
2770 meaning in strings.) If the value is nil, call functions from
2771 Qfontification_functions. */
2772 if (!STRINGP (it->string)
2773 && it->s == NULL
2774 && !NILP (Vfontification_functions)
2775 && !NILP (Vrun_hooks)
2776 && (pos = make_number (IT_CHARPOS (*it)),
2777 prop = Fget_char_property (pos, Qfontified, Qnil),
2778 NILP (prop)))
2779 {
2780 int count = SPECPDL_INDEX ();
2781 Lisp_Object val;
2782
2783 val = Vfontification_functions;
2784 specbind (Qfontification_functions, Qnil);
2785
2786 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
2787 safe_call1 (val, pos);
2788 else
2789 {
2790 Lisp_Object globals, fn;
2791 struct gcpro gcpro1, gcpro2;
2792
2793 globals = Qnil;
2794 GCPRO2 (val, globals);
2795
2796 for (; CONSP (val); val = XCDR (val))
2797 {
2798 fn = XCAR (val);
2799
2800 if (EQ (fn, Qt))
2801 {
2802 /* A value of t indicates this hook has a local
2803 binding; it means to run the global binding too.
2804 In a global value, t should not occur. If it
2805 does, we must ignore it to avoid an endless
2806 loop. */
2807 for (globals = Fdefault_value (Qfontification_functions);
2808 CONSP (globals);
2809 globals = XCDR (globals))
2810 {
2811 fn = XCAR (globals);
2812 if (!EQ (fn, Qt))
2813 safe_call1 (fn, pos);
2814 }
2815 }
2816 else
2817 safe_call1 (fn, pos);
2818 }
2819
2820 UNGCPRO;
2821 }
2822
2823 unbind_to (count, Qnil);
2824
2825 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
2826 something. This avoids an endless loop if they failed to
2827 fontify the text for which reason ever. */
2828 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
2829 handled = HANDLED_RECOMPUTE_PROPS;
2830 }
2831
2832 return handled;
2833 }
2834
2835
2836 \f
2837 /***********************************************************************
2838 Faces
2839 ***********************************************************************/
2840
2841 /* Set up iterator IT from face properties at its current position.
2842 Called from handle_stop. */
2843
2844 static enum prop_handled
2845 handle_face_prop (it)
2846 struct it *it;
2847 {
2848 int new_face_id, next_stop;
2849
2850 if (!STRINGP (it->string))
2851 {
2852 new_face_id
2853 = face_at_buffer_position (it->w,
2854 IT_CHARPOS (*it),
2855 it->region_beg_charpos,
2856 it->region_end_charpos,
2857 &next_stop,
2858 (IT_CHARPOS (*it)
2859 + TEXT_PROP_DISTANCE_LIMIT),
2860 0);
2861
2862 /* Is this a start of a run of characters with box face?
2863 Caveat: this can be called for a freshly initialized
2864 iterator; face_id is -1 in this case. We know that the new
2865 face will not change until limit, i.e. if the new face has a
2866 box, all characters up to limit will have one. But, as
2867 usual, we don't know whether limit is really the end. */
2868 if (new_face_id != it->face_id)
2869 {
2870 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
2871
2872 /* If new face has a box but old face has not, this is
2873 the start of a run of characters with box, i.e. it has
2874 a shadow on the left side. The value of face_id of the
2875 iterator will be -1 if this is the initial call that gets
2876 the face. In this case, we have to look in front of IT's
2877 position and see whether there is a face != new_face_id. */
2878 it->start_of_box_run_p
2879 = (new_face->box != FACE_NO_BOX
2880 && (it->face_id >= 0
2881 || IT_CHARPOS (*it) == BEG
2882 || new_face_id != face_before_it_pos (it)));
2883 it->face_box_p = new_face->box != FACE_NO_BOX;
2884 }
2885 }
2886 else
2887 {
2888 int base_face_id, bufpos;
2889
2890 if (it->current.overlay_string_index >= 0)
2891 bufpos = IT_CHARPOS (*it);
2892 else
2893 bufpos = 0;
2894
2895 /* For strings from a buffer, i.e. overlay strings or strings
2896 from a `display' property, use the face at IT's current
2897 buffer position as the base face to merge with, so that
2898 overlay strings appear in the same face as surrounding
2899 text, unless they specify their own faces. */
2900 base_face_id = underlying_face_id (it);
2901
2902 new_face_id = face_at_string_position (it->w,
2903 it->string,
2904 IT_STRING_CHARPOS (*it),
2905 bufpos,
2906 it->region_beg_charpos,
2907 it->region_end_charpos,
2908 &next_stop,
2909 base_face_id, 0);
2910
2911 #if 0 /* This shouldn't be neccessary. Let's check it. */
2912 /* If IT is used to display a mode line we would really like to
2913 use the mode line face instead of the frame's default face. */
2914 if (it->glyph_row == MATRIX_MODE_LINE_ROW (it->w->desired_matrix)
2915 && new_face_id == DEFAULT_FACE_ID)
2916 new_face_id = CURRENT_MODE_LINE_FACE_ID (it->w);
2917 #endif
2918
2919 /* Is this a start of a run of characters with box? Caveat:
2920 this can be called for a freshly allocated iterator; face_id
2921 is -1 is this case. We know that the new face will not
2922 change until the next check pos, i.e. if the new face has a
2923 box, all characters up to that position will have a
2924 box. But, as usual, we don't know whether that position
2925 is really the end. */
2926 if (new_face_id != it->face_id)
2927 {
2928 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
2929 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
2930
2931 /* If new face has a box but old face hasn't, this is the
2932 start of a run of characters with box, i.e. it has a
2933 shadow on the left side. */
2934 it->start_of_box_run_p
2935 = new_face->box && (old_face == NULL || !old_face->box);
2936 it->face_box_p = new_face->box != FACE_NO_BOX;
2937 }
2938 }
2939
2940 it->face_id = new_face_id;
2941 return HANDLED_NORMALLY;
2942 }
2943
2944
2945 /* Return the ID of the face ``underlying'' IT's current position,
2946 which is in a string. If the iterator is associated with a
2947 buffer, return the face at IT's current buffer position.
2948 Otherwise, use the iterator's base_face_id. */
2949
2950 static int
2951 underlying_face_id (it)
2952 struct it *it;
2953 {
2954 int face_id = it->base_face_id, i;
2955
2956 xassert (STRINGP (it->string));
2957
2958 for (i = it->sp - 1; i >= 0; --i)
2959 if (NILP (it->stack[i].string))
2960 face_id = it->stack[i].face_id;
2961
2962 return face_id;
2963 }
2964
2965
2966 /* Compute the face one character before or after the current position
2967 of IT. BEFORE_P non-zero means get the face in front of IT's
2968 position. Value is the id of the face. */
2969
2970 static int
2971 face_before_or_after_it_pos (it, before_p)
2972 struct it *it;
2973 int before_p;
2974 {
2975 int face_id, limit;
2976 int next_check_charpos;
2977 struct text_pos pos;
2978
2979 xassert (it->s == NULL);
2980
2981 if (STRINGP (it->string))
2982 {
2983 int bufpos, base_face_id;
2984
2985 /* No face change past the end of the string (for the case
2986 we are padding with spaces). No face change before the
2987 string start. */
2988 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
2989 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
2990 return it->face_id;
2991
2992 /* Set pos to the position before or after IT's current position. */
2993 if (before_p)
2994 pos = string_pos (IT_STRING_CHARPOS (*it) - 1, it->string);
2995 else
2996 /* For composition, we must check the character after the
2997 composition. */
2998 pos = (it->what == IT_COMPOSITION
2999 ? string_pos (IT_STRING_CHARPOS (*it) + it->cmp_len, it->string)
3000 : string_pos (IT_STRING_CHARPOS (*it) + 1, it->string));
3001
3002 if (it->current.overlay_string_index >= 0)
3003 bufpos = IT_CHARPOS (*it);
3004 else
3005 bufpos = 0;
3006
3007 base_face_id = underlying_face_id (it);
3008
3009 /* Get the face for ASCII, or unibyte. */
3010 face_id = face_at_string_position (it->w,
3011 it->string,
3012 CHARPOS (pos),
3013 bufpos,
3014 it->region_beg_charpos,
3015 it->region_end_charpos,
3016 &next_check_charpos,
3017 base_face_id, 0);
3018
3019 /* Correct the face for charsets different from ASCII. Do it
3020 for the multibyte case only. The face returned above is
3021 suitable for unibyte text if IT->string is unibyte. */
3022 if (STRING_MULTIBYTE (it->string))
3023 {
3024 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos);
3025 int rest = SBYTES (it->string) - BYTEPOS (pos);
3026 int c, len;
3027 struct face *face = FACE_FROM_ID (it->f, face_id);
3028
3029 c = string_char_and_length (p, rest, &len);
3030 face_id = FACE_FOR_CHAR (it->f, face, c);
3031 }
3032 }
3033 else
3034 {
3035 if ((IT_CHARPOS (*it) >= ZV && !before_p)
3036 || (IT_CHARPOS (*it) <= BEGV && before_p))
3037 return it->face_id;
3038
3039 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
3040 pos = it->current.pos;
3041
3042 if (before_p)
3043 DEC_TEXT_POS (pos, it->multibyte_p);
3044 else
3045 {
3046 if (it->what == IT_COMPOSITION)
3047 /* For composition, we must check the position after the
3048 composition. */
3049 pos.charpos += it->cmp_len, pos.bytepos += it->len;
3050 else
3051 INC_TEXT_POS (pos, it->multibyte_p);
3052 }
3053
3054 /* Determine face for CHARSET_ASCII, or unibyte. */
3055 face_id = face_at_buffer_position (it->w,
3056 CHARPOS (pos),
3057 it->region_beg_charpos,
3058 it->region_end_charpos,
3059 &next_check_charpos,
3060 limit, 0);
3061
3062 /* Correct the face for charsets different from ASCII. Do it
3063 for the multibyte case only. The face returned above is
3064 suitable for unibyte text if current_buffer is unibyte. */
3065 if (it->multibyte_p)
3066 {
3067 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
3068 struct face *face = FACE_FROM_ID (it->f, face_id);
3069 face_id = FACE_FOR_CHAR (it->f, face, c);
3070 }
3071 }
3072
3073 return face_id;
3074 }
3075
3076
3077 \f
3078 /***********************************************************************
3079 Invisible text
3080 ***********************************************************************/
3081
3082 /* Set up iterator IT from invisible properties at its current
3083 position. Called from handle_stop. */
3084
3085 static enum prop_handled
3086 handle_invisible_prop (it)
3087 struct it *it;
3088 {
3089 enum prop_handled handled = HANDLED_NORMALLY;
3090
3091 if (STRINGP (it->string))
3092 {
3093 extern Lisp_Object Qinvisible;
3094 Lisp_Object prop, end_charpos, limit, charpos;
3095
3096 /* Get the value of the invisible text property at the
3097 current position. Value will be nil if there is no such
3098 property. */
3099 charpos = make_number (IT_STRING_CHARPOS (*it));
3100 prop = Fget_text_property (charpos, Qinvisible, it->string);
3101
3102 if (!NILP (prop)
3103 && IT_STRING_CHARPOS (*it) < it->end_charpos)
3104 {
3105 handled = HANDLED_RECOMPUTE_PROPS;
3106
3107 /* Get the position at which the next change of the
3108 invisible text property can be found in IT->string.
3109 Value will be nil if the property value is the same for
3110 all the rest of IT->string. */
3111 XSETINT (limit, SCHARS (it->string));
3112 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
3113 it->string, limit);
3114
3115 /* Text at current position is invisible. The next
3116 change in the property is at position end_charpos.
3117 Move IT's current position to that position. */
3118 if (INTEGERP (end_charpos)
3119 && XFASTINT (end_charpos) < XFASTINT (limit))
3120 {
3121 struct text_pos old;
3122 old = it->current.string_pos;
3123 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
3124 compute_string_pos (&it->current.string_pos, old, it->string);
3125 }
3126 else
3127 {
3128 /* The rest of the string is invisible. If this is an
3129 overlay string, proceed with the next overlay string
3130 or whatever comes and return a character from there. */
3131 if (it->current.overlay_string_index >= 0)
3132 {
3133 next_overlay_string (it);
3134 /* Don't check for overlay strings when we just
3135 finished processing them. */
3136 handled = HANDLED_OVERLAY_STRING_CONSUMED;
3137 }
3138 else
3139 {
3140 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
3141 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
3142 }
3143 }
3144 }
3145 }
3146 else
3147 {
3148 int invis_p, newpos, next_stop, start_charpos;
3149 Lisp_Object pos, prop, overlay;
3150
3151 /* First of all, is there invisible text at this position? */
3152 start_charpos = IT_CHARPOS (*it);
3153 pos = make_number (IT_CHARPOS (*it));
3154 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
3155 &overlay);
3156 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3157
3158 /* If we are on invisible text, skip over it. */
3159 if (invis_p && IT_CHARPOS (*it) < it->end_charpos)
3160 {
3161 /* Record whether we have to display an ellipsis for the
3162 invisible text. */
3163 int display_ellipsis_p = invis_p == 2;
3164
3165 handled = HANDLED_RECOMPUTE_PROPS;
3166
3167 /* Loop skipping over invisible text. The loop is left at
3168 ZV or with IT on the first char being visible again. */
3169 do
3170 {
3171 /* Try to skip some invisible text. Return value is the
3172 position reached which can be equal to IT's position
3173 if there is nothing invisible here. This skips both
3174 over invisible text properties and overlays with
3175 invisible property. */
3176 newpos = skip_invisible (IT_CHARPOS (*it),
3177 &next_stop, ZV, it->window);
3178
3179 /* If we skipped nothing at all we weren't at invisible
3180 text in the first place. If everything to the end of
3181 the buffer was skipped, end the loop. */
3182 if (newpos == IT_CHARPOS (*it) || newpos >= ZV)
3183 invis_p = 0;
3184 else
3185 {
3186 /* We skipped some characters but not necessarily
3187 all there are. Check if we ended up on visible
3188 text. Fget_char_property returns the property of
3189 the char before the given position, i.e. if we
3190 get invis_p = 0, this means that the char at
3191 newpos is visible. */
3192 pos = make_number (newpos);
3193 prop = Fget_char_property (pos, Qinvisible, it->window);
3194 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3195 }
3196
3197 /* If we ended up on invisible text, proceed to
3198 skip starting with next_stop. */
3199 if (invis_p)
3200 IT_CHARPOS (*it) = next_stop;
3201 }
3202 while (invis_p);
3203
3204 /* The position newpos is now either ZV or on visible text. */
3205 IT_CHARPOS (*it) = newpos;
3206 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
3207
3208 /* If there are before-strings at the start of invisible
3209 text, and the text is invisible because of a text
3210 property, arrange to show before-strings because 20.x did
3211 it that way. (If the text is invisible because of an
3212 overlay property instead of a text property, this is
3213 already handled in the overlay code.) */
3214 if (NILP (overlay)
3215 && get_overlay_strings (it, start_charpos))
3216 {
3217 handled = HANDLED_RECOMPUTE_PROPS;
3218 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
3219 }
3220 else if (display_ellipsis_p)
3221 setup_for_ellipsis (it);
3222 }
3223 }
3224
3225 return handled;
3226 }
3227
3228
3229 /* Make iterator IT return `...' next. */
3230
3231 static void
3232 setup_for_ellipsis (it)
3233 struct it *it;
3234 {
3235 if (it->dp
3236 && VECTORP (DISP_INVIS_VECTOR (it->dp)))
3237 {
3238 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
3239 it->dpvec = v->contents;
3240 it->dpend = v->contents + v->size;
3241 }
3242 else
3243 {
3244 /* Default `...'. */
3245 it->dpvec = default_invis_vector;
3246 it->dpend = default_invis_vector + 3;
3247 }
3248
3249 /* The ellipsis display does not replace the display of the
3250 character at the new position. Indicate this by setting
3251 IT->dpvec_char_len to zero. */
3252 it->dpvec_char_len = 0;
3253
3254 it->current.dpvec_index = 0;
3255 it->method = next_element_from_display_vector;
3256 }
3257
3258
3259 \f
3260 /***********************************************************************
3261 'display' property
3262 ***********************************************************************/
3263
3264 /* Set up iterator IT from `display' property at its current position.
3265 Called from handle_stop. */
3266
3267 static enum prop_handled
3268 handle_display_prop (it)
3269 struct it *it;
3270 {
3271 Lisp_Object prop, object;
3272 struct text_pos *position;
3273 int display_replaced_p = 0;
3274
3275 if (STRINGP (it->string))
3276 {
3277 object = it->string;
3278 position = &it->current.string_pos;
3279 }
3280 else
3281 {
3282 object = it->w->buffer;
3283 position = &it->current.pos;
3284 }
3285
3286 /* Reset those iterator values set from display property values. */
3287 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
3288 it->space_width = Qnil;
3289 it->font_height = Qnil;
3290 it->voffset = 0;
3291
3292 /* We don't support recursive `display' properties, i.e. string
3293 values that have a string `display' property, that have a string
3294 `display' property etc. */
3295 if (!it->string_from_display_prop_p)
3296 it->area = TEXT_AREA;
3297
3298 prop = Fget_char_property (make_number (position->charpos),
3299 Qdisplay, object);
3300 if (NILP (prop))
3301 return HANDLED_NORMALLY;
3302
3303 if (CONSP (prop)
3304 /* Simple properties. */
3305 && !EQ (XCAR (prop), Qimage)
3306 && !EQ (XCAR (prop), Qspace)
3307 && !EQ (XCAR (prop), Qwhen)
3308 && !EQ (XCAR (prop), Qslice)
3309 && !EQ (XCAR (prop), Qspace_width)
3310 && !EQ (XCAR (prop), Qheight)
3311 && !EQ (XCAR (prop), Qraise)
3312 /* Marginal area specifications. */
3313 && !(CONSP (XCAR (prop)) && EQ (XCAR (XCAR (prop)), Qmargin))
3314 && !EQ (XCAR (prop), Qleft_fringe)
3315 && !EQ (XCAR (prop), Qright_fringe)
3316 && !NILP (XCAR (prop)))
3317 {
3318 for (; CONSP (prop); prop = XCDR (prop))
3319 {
3320 if (handle_single_display_prop (it, XCAR (prop), object,
3321 position, display_replaced_p))
3322 display_replaced_p = 1;
3323 }
3324 }
3325 else if (VECTORP (prop))
3326 {
3327 int i;
3328 for (i = 0; i < ASIZE (prop); ++i)
3329 if (handle_single_display_prop (it, AREF (prop, i), object,
3330 position, display_replaced_p))
3331 display_replaced_p = 1;
3332 }
3333 else
3334 {
3335 if (handle_single_display_prop (it, prop, object, position, 0))
3336 display_replaced_p = 1;
3337 }
3338
3339 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
3340 }
3341
3342
3343 /* Value is the position of the end of the `display' property starting
3344 at START_POS in OBJECT. */
3345
3346 static struct text_pos
3347 display_prop_end (it, object, start_pos)
3348 struct it *it;
3349 Lisp_Object object;
3350 struct text_pos start_pos;
3351 {
3352 Lisp_Object end;
3353 struct text_pos end_pos;
3354
3355 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
3356 Qdisplay, object, Qnil);
3357 CHARPOS (end_pos) = XFASTINT (end);
3358 if (STRINGP (object))
3359 compute_string_pos (&end_pos, start_pos, it->string);
3360 else
3361 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
3362
3363 return end_pos;
3364 }
3365
3366
3367 /* Set up IT from a single `display' sub-property value PROP. OBJECT
3368 is the object in which the `display' property was found. *POSITION
3369 is the position at which it was found. DISPLAY_REPLACED_P non-zero
3370 means that we previously saw a display sub-property which already
3371 replaced text display with something else, for example an image;
3372 ignore such properties after the first one has been processed.
3373
3374 If PROP is a `space' or `image' sub-property, set *POSITION to the
3375 end position of the `display' property.
3376
3377 Value is non-zero if something was found which replaces the display
3378 of buffer or string text. */
3379
3380 static int
3381 handle_single_display_prop (it, prop, object, position,
3382 display_replaced_before_p)
3383 struct it *it;
3384 Lisp_Object prop;
3385 Lisp_Object object;
3386 struct text_pos *position;
3387 int display_replaced_before_p;
3388 {
3389 Lisp_Object value;
3390 int replaces_text_display_p = 0;
3391 Lisp_Object form;
3392
3393 /* If PROP is a list of the form `(when FORM . VALUE)', FORM is
3394 evaluated. If the result is nil, VALUE is ignored. */
3395 form = Qt;
3396 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
3397 {
3398 prop = XCDR (prop);
3399 if (!CONSP (prop))
3400 return 0;
3401 form = XCAR (prop);
3402 prop = XCDR (prop);
3403 }
3404
3405 if (!NILP (form) && !EQ (form, Qt))
3406 {
3407 int count = SPECPDL_INDEX ();
3408 struct gcpro gcpro1;
3409
3410 /* Bind `object' to the object having the `display' property, a
3411 buffer or string. Bind `position' to the position in the
3412 object where the property was found, and `buffer-position'
3413 to the current position in the buffer. */
3414 specbind (Qobject, object);
3415 specbind (Qposition, make_number (CHARPOS (*position)));
3416 specbind (Qbuffer_position,
3417 make_number (STRINGP (object)
3418 ? IT_CHARPOS (*it) : CHARPOS (*position)));
3419 GCPRO1 (form);
3420 form = safe_eval (form);
3421 UNGCPRO;
3422 unbind_to (count, Qnil);
3423 }
3424
3425 if (NILP (form))
3426 return 0;
3427
3428 if (CONSP (prop)
3429 && EQ (XCAR (prop), Qheight)
3430 && CONSP (XCDR (prop)))
3431 {
3432 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3433 return 0;
3434
3435 /* `(height HEIGHT)'. */
3436 it->font_height = XCAR (XCDR (prop));
3437 if (!NILP (it->font_height))
3438 {
3439 struct face *face = FACE_FROM_ID (it->f, it->face_id);
3440 int new_height = -1;
3441
3442 if (CONSP (it->font_height)
3443 && (EQ (XCAR (it->font_height), Qplus)
3444 || EQ (XCAR (it->font_height), Qminus))
3445 && CONSP (XCDR (it->font_height))
3446 && INTEGERP (XCAR (XCDR (it->font_height))))
3447 {
3448 /* `(+ N)' or `(- N)' where N is an integer. */
3449 int steps = XINT (XCAR (XCDR (it->font_height)));
3450 if (EQ (XCAR (it->font_height), Qplus))
3451 steps = - steps;
3452 it->face_id = smaller_face (it->f, it->face_id, steps);
3453 }
3454 else if (FUNCTIONP (it->font_height))
3455 {
3456 /* Call function with current height as argument.
3457 Value is the new height. */
3458 Lisp_Object height;
3459 height = safe_call1 (it->font_height,
3460 face->lface[LFACE_HEIGHT_INDEX]);
3461 if (NUMBERP (height))
3462 new_height = XFLOATINT (height);
3463 }
3464 else if (NUMBERP (it->font_height))
3465 {
3466 /* Value is a multiple of the canonical char height. */
3467 struct face *face;
3468
3469 face = FACE_FROM_ID (it->f, DEFAULT_FACE_ID);
3470 new_height = (XFLOATINT (it->font_height)
3471 * XINT (face->lface[LFACE_HEIGHT_INDEX]));
3472 }
3473 else
3474 {
3475 /* Evaluate IT->font_height with `height' bound to the
3476 current specified height to get the new height. */
3477 Lisp_Object value;
3478 int count = SPECPDL_INDEX ();
3479
3480 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
3481 value = safe_eval (it->font_height);
3482 unbind_to (count, Qnil);
3483
3484 if (NUMBERP (value))
3485 new_height = XFLOATINT (value);
3486 }
3487
3488 if (new_height > 0)
3489 it->face_id = face_with_height (it->f, it->face_id, new_height);
3490 }
3491 }
3492 else if (CONSP (prop)
3493 && EQ (XCAR (prop), Qspace_width)
3494 && CONSP (XCDR (prop)))
3495 {
3496 /* `(space_width WIDTH)'. */
3497 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3498 return 0;
3499
3500 value = XCAR (XCDR (prop));
3501 if (NUMBERP (value) && XFLOATINT (value) > 0)
3502 it->space_width = value;
3503 }
3504 else if (CONSP (prop)
3505 && EQ (XCAR (prop), Qslice))
3506 {
3507 /* `(slice X Y WIDTH HEIGHT)'. */
3508 Lisp_Object tem;
3509
3510 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3511 return 0;
3512
3513 if (tem = XCDR (prop), CONSP (tem))
3514 {
3515 it->slice.x = XCAR (tem);
3516 if (tem = XCDR (tem), CONSP (tem))
3517 {
3518 it->slice.y = XCAR (tem);
3519 if (tem = XCDR (tem), CONSP (tem))
3520 {
3521 it->slice.width = XCAR (tem);
3522 if (tem = XCDR (tem), CONSP (tem))
3523 it->slice.height = XCAR (tem);
3524 }
3525 }
3526 }
3527 }
3528 else if (CONSP (prop)
3529 && EQ (XCAR (prop), Qraise)
3530 && CONSP (XCDR (prop)))
3531 {
3532 /* `(raise FACTOR)'. */
3533 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3534 return 0;
3535
3536 #ifdef HAVE_WINDOW_SYSTEM
3537 value = XCAR (XCDR (prop));
3538 if (NUMBERP (value))
3539 {
3540 struct face *face = FACE_FROM_ID (it->f, it->face_id);
3541 it->voffset = - (XFLOATINT (value)
3542 * (FONT_HEIGHT (face->font)));
3543 }
3544 #endif /* HAVE_WINDOW_SYSTEM */
3545 }
3546 else if (!it->string_from_display_prop_p)
3547 {
3548 /* `((margin left-margin) VALUE)' or `((margin right-margin)
3549 VALUE) or `((margin nil) VALUE)' or VALUE. */
3550 Lisp_Object location, value;
3551 struct text_pos start_pos;
3552 int valid_p;
3553
3554 /* Characters having this form of property are not displayed, so
3555 we have to find the end of the property. */
3556 start_pos = *position;
3557 *position = display_prop_end (it, object, start_pos);
3558 value = Qnil;
3559
3560 /* Let's stop at the new position and assume that all
3561 text properties change there. */
3562 it->stop_charpos = position->charpos;
3563
3564 if (CONSP (prop)
3565 && (EQ (XCAR (prop), Qleft_fringe)
3566 || EQ (XCAR (prop), Qright_fringe))
3567 && CONSP (XCDR (prop)))
3568 {
3569 unsigned face_id = DEFAULT_FACE_ID;
3570
3571 /* Save current settings of IT so that we can restore them
3572 when we are finished with the glyph property value. */
3573
3574 /* `(left-fringe BITMAP FACE)'. */
3575 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3576 return 0;
3577
3578 #ifdef HAVE_WINDOW_SYSTEM
3579 value = XCAR (XCDR (prop));
3580 if (!NUMBERP (value)
3581 || !valid_fringe_bitmap_id_p (XINT (value)))
3582 return 0;
3583
3584 if (CONSP (XCDR (XCDR (prop))))
3585 {
3586 Lisp_Object face_name = XCAR (XCDR (XCDR (prop)));
3587
3588 face_id = lookup_named_face (it->f, face_name, 'A');
3589 if (face_id < 0)
3590 return 0;
3591 }
3592
3593 push_it (it);
3594
3595 it->area = TEXT_AREA;
3596 it->what = IT_IMAGE;
3597 it->image_id = -1; /* no image */
3598 it->position = start_pos;
3599 it->object = NILP (object) ? it->w->buffer : object;
3600 it->method = next_element_from_image;
3601 it->face_id = face_id;
3602
3603 /* Say that we haven't consumed the characters with
3604 `display' property yet. The call to pop_it in
3605 set_iterator_to_next will clean this up. */
3606 *position = start_pos;
3607
3608 if (EQ (XCAR (prop), Qleft_fringe))
3609 {
3610 it->left_user_fringe_bitmap = XINT (value);
3611 it->left_user_fringe_face_id = face_id;
3612 }
3613 else
3614 {
3615 it->right_user_fringe_bitmap = XINT (value);
3616 it->right_user_fringe_face_id = face_id;
3617 }
3618 #endif /* HAVE_WINDOW_SYSTEM */
3619 return 1;
3620 }
3621
3622 location = Qunbound;
3623 if (CONSP (prop) && CONSP (XCAR (prop)))
3624 {
3625 Lisp_Object tem;
3626
3627 value = XCDR (prop);
3628 if (CONSP (value))
3629 value = XCAR (value);
3630
3631 tem = XCAR (prop);
3632 if (EQ (XCAR (tem), Qmargin)
3633 && (tem = XCDR (tem),
3634 tem = CONSP (tem) ? XCAR (tem) : Qnil,
3635 (NILP (tem)
3636 || EQ (tem, Qleft_margin)
3637 || EQ (tem, Qright_margin))))
3638 location = tem;
3639 }
3640
3641 if (EQ (location, Qunbound))
3642 {
3643 location = Qnil;
3644 value = prop;
3645 }
3646
3647 valid_p = (STRINGP (value)
3648 #ifdef HAVE_WINDOW_SYSTEM
3649 || (!FRAME_TERMCAP_P (it->f) && valid_image_p (value))
3650 #endif /* not HAVE_WINDOW_SYSTEM */
3651 || (CONSP (value) && EQ (XCAR (value), Qspace)));
3652
3653 if ((EQ (location, Qleft_margin)
3654 || EQ (location, Qright_margin)
3655 || NILP (location))
3656 && valid_p
3657 && !display_replaced_before_p)
3658 {
3659 replaces_text_display_p = 1;
3660
3661 /* Save current settings of IT so that we can restore them
3662 when we are finished with the glyph property value. */
3663 push_it (it);
3664
3665 if (NILP (location))
3666 it->area = TEXT_AREA;
3667 else if (EQ (location, Qleft_margin))
3668 it->area = LEFT_MARGIN_AREA;
3669 else
3670 it->area = RIGHT_MARGIN_AREA;
3671
3672 if (STRINGP (value))
3673 {
3674 it->string = value;
3675 it->multibyte_p = STRING_MULTIBYTE (it->string);
3676 it->current.overlay_string_index = -1;
3677 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
3678 it->end_charpos = it->string_nchars = SCHARS (it->string);
3679 it->method = next_element_from_string;
3680 it->stop_charpos = 0;
3681 it->string_from_display_prop_p = 1;
3682 /* Say that we haven't consumed the characters with
3683 `display' property yet. The call to pop_it in
3684 set_iterator_to_next will clean this up. */
3685 *position = start_pos;
3686 }
3687 else if (CONSP (value) && EQ (XCAR (value), Qspace))
3688 {
3689 it->method = next_element_from_stretch;
3690 it->object = value;
3691 it->current.pos = it->position = start_pos;
3692 }
3693 #ifdef HAVE_WINDOW_SYSTEM
3694 else
3695 {
3696 it->what = IT_IMAGE;
3697 it->image_id = lookup_image (it->f, value);
3698 it->position = start_pos;
3699 it->object = NILP (object) ? it->w->buffer : object;
3700 it->method = next_element_from_image;
3701
3702 /* Say that we haven't consumed the characters with
3703 `display' property yet. The call to pop_it in
3704 set_iterator_to_next will clean this up. */
3705 *position = start_pos;
3706 }
3707 #endif /* HAVE_WINDOW_SYSTEM */
3708 }
3709 else
3710 /* Invalid property or property not supported. Restore
3711 the position to what it was before. */
3712 *position = start_pos;
3713 }
3714
3715 return replaces_text_display_p;
3716 }
3717
3718
3719 /* Check if PROP is a display sub-property value whose text should be
3720 treated as intangible. */
3721
3722 static int
3723 single_display_prop_intangible_p (prop)
3724 Lisp_Object prop;
3725 {
3726 /* Skip over `when FORM'. */
3727 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
3728 {
3729 prop = XCDR (prop);
3730 if (!CONSP (prop))
3731 return 0;
3732 prop = XCDR (prop);
3733 }
3734
3735 if (STRINGP (prop))
3736 return 1;
3737
3738 if (!CONSP (prop))
3739 return 0;
3740
3741 /* Skip over `margin LOCATION'. If LOCATION is in the margins,
3742 we don't need to treat text as intangible. */
3743 if (EQ (XCAR (prop), Qmargin))
3744 {
3745 prop = XCDR (prop);
3746 if (!CONSP (prop))
3747 return 0;
3748
3749 prop = XCDR (prop);
3750 if (!CONSP (prop)
3751 || EQ (XCAR (prop), Qleft_margin)
3752 || EQ (XCAR (prop), Qright_margin))
3753 return 0;
3754 }
3755
3756 return (CONSP (prop)
3757 && (EQ (XCAR (prop), Qimage)
3758 || EQ (XCAR (prop), Qspace)));
3759 }
3760
3761
3762 /* Check if PROP is a display property value whose text should be
3763 treated as intangible. */
3764
3765 int
3766 display_prop_intangible_p (prop)
3767 Lisp_Object prop;
3768 {
3769 if (CONSP (prop)
3770 && CONSP (XCAR (prop))
3771 && !EQ (Qmargin, XCAR (XCAR (prop))))
3772 {
3773 /* A list of sub-properties. */
3774 while (CONSP (prop))
3775 {
3776 if (single_display_prop_intangible_p (XCAR (prop)))
3777 return 1;
3778 prop = XCDR (prop);
3779 }
3780 }
3781 else if (VECTORP (prop))
3782 {
3783 /* A vector of sub-properties. */
3784 int i;
3785 for (i = 0; i < ASIZE (prop); ++i)
3786 if (single_display_prop_intangible_p (AREF (prop, i)))
3787 return 1;
3788 }
3789 else
3790 return single_display_prop_intangible_p (prop);
3791
3792 return 0;
3793 }
3794
3795
3796 /* Return 1 if PROP is a display sub-property value containing STRING. */
3797
3798 static int
3799 single_display_prop_string_p (prop, string)
3800 Lisp_Object prop, string;
3801 {
3802 if (EQ (string, prop))
3803 return 1;
3804
3805 /* Skip over `when FORM'. */
3806 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
3807 {
3808 prop = XCDR (prop);
3809 if (!CONSP (prop))
3810 return 0;
3811 prop = XCDR (prop);
3812 }
3813
3814 if (CONSP (prop))
3815 /* Skip over `margin LOCATION'. */
3816 if (EQ (XCAR (prop), Qmargin))
3817 {
3818 prop = XCDR (prop);
3819 if (!CONSP (prop))
3820 return 0;
3821
3822 prop = XCDR (prop);
3823 if (!CONSP (prop))
3824 return 0;
3825 }
3826
3827 return CONSP (prop) && EQ (XCAR (prop), string);
3828 }
3829
3830
3831 /* Return 1 if STRING appears in the `display' property PROP. */
3832
3833 static int
3834 display_prop_string_p (prop, string)
3835 Lisp_Object prop, string;
3836 {
3837 if (CONSP (prop)
3838 && CONSP (XCAR (prop))
3839 && !EQ (Qmargin, XCAR (XCAR (prop))))
3840 {
3841 /* A list of sub-properties. */
3842 while (CONSP (prop))
3843 {
3844 if (single_display_prop_string_p (XCAR (prop), string))
3845 return 1;
3846 prop = XCDR (prop);
3847 }
3848 }
3849 else if (VECTORP (prop))
3850 {
3851 /* A vector of sub-properties. */
3852 int i;
3853 for (i = 0; i < ASIZE (prop); ++i)
3854 if (single_display_prop_string_p (AREF (prop, i), string))
3855 return 1;
3856 }
3857 else
3858 return single_display_prop_string_p (prop, string);
3859
3860 return 0;
3861 }
3862
3863
3864 /* Determine from which buffer position in W's buffer STRING comes
3865 from. AROUND_CHARPOS is an approximate position where it could
3866 be from. Value is the buffer position or 0 if it couldn't be
3867 determined.
3868
3869 W's buffer must be current.
3870
3871 This function is necessary because we don't record buffer positions
3872 in glyphs generated from strings (to keep struct glyph small).
3873 This function may only use code that doesn't eval because it is
3874 called asynchronously from note_mouse_highlight. */
3875
3876 int
3877 string_buffer_position (w, string, around_charpos)
3878 struct window *w;
3879 Lisp_Object string;
3880 int around_charpos;
3881 {
3882 Lisp_Object limit, prop, pos;
3883 const int MAX_DISTANCE = 1000;
3884 int found = 0;
3885
3886 pos = make_number (around_charpos);
3887 limit = make_number (min (XINT (pos) + MAX_DISTANCE, ZV));
3888 while (!found && !EQ (pos, limit))
3889 {
3890 prop = Fget_char_property (pos, Qdisplay, Qnil);
3891 if (!NILP (prop) && display_prop_string_p (prop, string))
3892 found = 1;
3893 else
3894 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil, limit);
3895 }
3896
3897 if (!found)
3898 {
3899 pos = make_number (around_charpos);
3900 limit = make_number (max (XINT (pos) - MAX_DISTANCE, BEGV));
3901 while (!found && !EQ (pos, limit))
3902 {
3903 prop = Fget_char_property (pos, Qdisplay, Qnil);
3904 if (!NILP (prop) && display_prop_string_p (prop, string))
3905 found = 1;
3906 else
3907 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
3908 limit);
3909 }
3910 }
3911
3912 return found ? XINT (pos) : 0;
3913 }
3914
3915
3916 \f
3917 /***********************************************************************
3918 `composition' property
3919 ***********************************************************************/
3920
3921 /* Set up iterator IT from `composition' property at its current
3922 position. Called from handle_stop. */
3923
3924 static enum prop_handled
3925 handle_composition_prop (it)
3926 struct it *it;
3927 {
3928 Lisp_Object prop, string;
3929 int pos, pos_byte, end;
3930 enum prop_handled handled = HANDLED_NORMALLY;
3931
3932 if (STRINGP (it->string))
3933 {
3934 pos = IT_STRING_CHARPOS (*it);
3935 pos_byte = IT_STRING_BYTEPOS (*it);
3936 string = it->string;
3937 }
3938 else
3939 {
3940 pos = IT_CHARPOS (*it);
3941 pos_byte = IT_BYTEPOS (*it);
3942 string = Qnil;
3943 }
3944
3945 /* If there's a valid composition and point is not inside of the
3946 composition (in the case that the composition is from the current
3947 buffer), draw a glyph composed from the composition components. */
3948 if (find_composition (pos, -1, &pos, &end, &prop, string)
3949 && COMPOSITION_VALID_P (pos, end, prop)
3950 && (STRINGP (it->string) || (PT <= pos || PT >= end)))
3951 {
3952 int id = get_composition_id (pos, pos_byte, end - pos, prop, string);
3953
3954 if (id >= 0)
3955 {
3956 it->method = next_element_from_composition;
3957 it->cmp_id = id;
3958 it->cmp_len = COMPOSITION_LENGTH (prop);
3959 /* For a terminal, draw only the first character of the
3960 components. */
3961 it->c = COMPOSITION_GLYPH (composition_table[id], 0);
3962 it->len = (STRINGP (it->string)
3963 ? string_char_to_byte (it->string, end)
3964 : CHAR_TO_BYTE (end)) - pos_byte;
3965 it->stop_charpos = end;
3966 handled = HANDLED_RETURN;
3967 }
3968 }
3969
3970 return handled;
3971 }
3972
3973
3974 \f
3975 /***********************************************************************
3976 Overlay strings
3977 ***********************************************************************/
3978
3979 /* The following structure is used to record overlay strings for
3980 later sorting in load_overlay_strings. */
3981
3982 struct overlay_entry
3983 {
3984 Lisp_Object overlay;
3985 Lisp_Object string;
3986 int priority;
3987 int after_string_p;
3988 };
3989
3990
3991 /* Set up iterator IT from overlay strings at its current position.
3992 Called from handle_stop. */
3993
3994 static enum prop_handled
3995 handle_overlay_change (it)
3996 struct it *it;
3997 {
3998 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
3999 return HANDLED_RECOMPUTE_PROPS;
4000 else
4001 return HANDLED_NORMALLY;
4002 }
4003
4004
4005 /* Set up the next overlay string for delivery by IT, if there is an
4006 overlay string to deliver. Called by set_iterator_to_next when the
4007 end of the current overlay string is reached. If there are more
4008 overlay strings to display, IT->string and
4009 IT->current.overlay_string_index are set appropriately here.
4010 Otherwise IT->string is set to nil. */
4011
4012 static void
4013 next_overlay_string (it)
4014 struct it *it;
4015 {
4016 ++it->current.overlay_string_index;
4017 if (it->current.overlay_string_index == it->n_overlay_strings)
4018 {
4019 /* No more overlay strings. Restore IT's settings to what
4020 they were before overlay strings were processed, and
4021 continue to deliver from current_buffer. */
4022 int display_ellipsis_p = it->stack[it->sp - 1].display_ellipsis_p;
4023
4024 pop_it (it);
4025 xassert (it->stop_charpos >= BEGV
4026 && it->stop_charpos <= it->end_charpos);
4027 it->string = Qnil;
4028 it->current.overlay_string_index = -1;
4029 SET_TEXT_POS (it->current.string_pos, -1, -1);
4030 it->n_overlay_strings = 0;
4031 it->method = next_element_from_buffer;
4032
4033 /* If we're at the end of the buffer, record that we have
4034 processed the overlay strings there already, so that
4035 next_element_from_buffer doesn't try it again. */
4036 if (IT_CHARPOS (*it) >= it->end_charpos)
4037 it->overlay_strings_at_end_processed_p = 1;
4038
4039 /* If we have to display `...' for invisible text, set
4040 the iterator up for that. */
4041 if (display_ellipsis_p)
4042 setup_for_ellipsis (it);
4043 }
4044 else
4045 {
4046 /* There are more overlay strings to process. If
4047 IT->current.overlay_string_index has advanced to a position
4048 where we must load IT->overlay_strings with more strings, do
4049 it. */
4050 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
4051
4052 if (it->current.overlay_string_index && i == 0)
4053 load_overlay_strings (it, 0);
4054
4055 /* Initialize IT to deliver display elements from the overlay
4056 string. */
4057 it->string = it->overlay_strings[i];
4058 it->multibyte_p = STRING_MULTIBYTE (it->string);
4059 SET_TEXT_POS (it->current.string_pos, 0, 0);
4060 it->method = next_element_from_string;
4061 it->stop_charpos = 0;
4062 }
4063
4064 CHECK_IT (it);
4065 }
4066
4067
4068 /* Compare two overlay_entry structures E1 and E2. Used as a
4069 comparison function for qsort in load_overlay_strings. Overlay
4070 strings for the same position are sorted so that
4071
4072 1. All after-strings come in front of before-strings, except
4073 when they come from the same overlay.
4074
4075 2. Within after-strings, strings are sorted so that overlay strings
4076 from overlays with higher priorities come first.
4077
4078 2. Within before-strings, strings are sorted so that overlay
4079 strings from overlays with higher priorities come last.
4080
4081 Value is analogous to strcmp. */
4082
4083
4084 static int
4085 compare_overlay_entries (e1, e2)
4086 void *e1, *e2;
4087 {
4088 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
4089 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
4090 int result;
4091
4092 if (entry1->after_string_p != entry2->after_string_p)
4093 {
4094 /* Let after-strings appear in front of before-strings if
4095 they come from different overlays. */
4096 if (EQ (entry1->overlay, entry2->overlay))
4097 result = entry1->after_string_p ? 1 : -1;
4098 else
4099 result = entry1->after_string_p ? -1 : 1;
4100 }
4101 else if (entry1->after_string_p)
4102 /* After-strings sorted in order of decreasing priority. */
4103 result = entry2->priority - entry1->priority;
4104 else
4105 /* Before-strings sorted in order of increasing priority. */
4106 result = entry1->priority - entry2->priority;
4107
4108 return result;
4109 }
4110
4111
4112 /* Load the vector IT->overlay_strings with overlay strings from IT's
4113 current buffer position, or from CHARPOS if that is > 0. Set
4114 IT->n_overlays to the total number of overlay strings found.
4115
4116 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
4117 a time. On entry into load_overlay_strings,
4118 IT->current.overlay_string_index gives the number of overlay
4119 strings that have already been loaded by previous calls to this
4120 function.
4121
4122 IT->add_overlay_start contains an additional overlay start
4123 position to consider for taking overlay strings from, if non-zero.
4124 This position comes into play when the overlay has an `invisible'
4125 property, and both before and after-strings. When we've skipped to
4126 the end of the overlay, because of its `invisible' property, we
4127 nevertheless want its before-string to appear.
4128 IT->add_overlay_start will contain the overlay start position
4129 in this case.
4130
4131 Overlay strings are sorted so that after-string strings come in
4132 front of before-string strings. Within before and after-strings,
4133 strings are sorted by overlay priority. See also function
4134 compare_overlay_entries. */
4135
4136 static void
4137 load_overlay_strings (it, charpos)
4138 struct it *it;
4139 int charpos;
4140 {
4141 extern Lisp_Object Qafter_string, Qbefore_string, Qwindow, Qpriority;
4142 Lisp_Object overlay, window, str, invisible;
4143 struct Lisp_Overlay *ov;
4144 int start, end;
4145 int size = 20;
4146 int n = 0, i, j, invis_p;
4147 struct overlay_entry *entries
4148 = (struct overlay_entry *) alloca (size * sizeof *entries);
4149
4150 if (charpos <= 0)
4151 charpos = IT_CHARPOS (*it);
4152
4153 /* Append the overlay string STRING of overlay OVERLAY to vector
4154 `entries' which has size `size' and currently contains `n'
4155 elements. AFTER_P non-zero means STRING is an after-string of
4156 OVERLAY. */
4157 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
4158 do \
4159 { \
4160 Lisp_Object priority; \
4161 \
4162 if (n == size) \
4163 { \
4164 int new_size = 2 * size; \
4165 struct overlay_entry *old = entries; \
4166 entries = \
4167 (struct overlay_entry *) alloca (new_size \
4168 * sizeof *entries); \
4169 bcopy (old, entries, size * sizeof *entries); \
4170 size = new_size; \
4171 } \
4172 \
4173 entries[n].string = (STRING); \
4174 entries[n].overlay = (OVERLAY); \
4175 priority = Foverlay_get ((OVERLAY), Qpriority); \
4176 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
4177 entries[n].after_string_p = (AFTER_P); \
4178 ++n; \
4179 } \
4180 while (0)
4181
4182 /* Process overlay before the overlay center. */
4183 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
4184 {
4185 XSETMISC (overlay, ov);
4186 xassert (OVERLAYP (overlay));
4187 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4188 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4189
4190 if (end < charpos)
4191 break;
4192
4193 /* Skip this overlay if it doesn't start or end at IT's current
4194 position. */
4195 if (end != charpos && start != charpos)
4196 continue;
4197
4198 /* Skip this overlay if it doesn't apply to IT->w. */
4199 window = Foverlay_get (overlay, Qwindow);
4200 if (WINDOWP (window) && XWINDOW (window) != it->w)
4201 continue;
4202
4203 /* If the text ``under'' the overlay is invisible, both before-
4204 and after-strings from this overlay are visible; start and
4205 end position are indistinguishable. */
4206 invisible = Foverlay_get (overlay, Qinvisible);
4207 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4208
4209 /* If overlay has a non-empty before-string, record it. */
4210 if ((start == charpos || (end == charpos && invis_p))
4211 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4212 && SCHARS (str))
4213 RECORD_OVERLAY_STRING (overlay, str, 0);
4214
4215 /* If overlay has a non-empty after-string, record it. */
4216 if ((end == charpos || (start == charpos && invis_p))
4217 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4218 && SCHARS (str))
4219 RECORD_OVERLAY_STRING (overlay, str, 1);
4220 }
4221
4222 /* Process overlays after the overlay center. */
4223 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
4224 {
4225 XSETMISC (overlay, ov);
4226 xassert (OVERLAYP (overlay));
4227 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4228 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4229
4230 if (start > charpos)
4231 break;
4232
4233 /* Skip this overlay if it doesn't start or end at IT's current
4234 position. */
4235 if (end != charpos && start != charpos)
4236 continue;
4237
4238 /* Skip this overlay if it doesn't apply to IT->w. */
4239 window = Foverlay_get (overlay, Qwindow);
4240 if (WINDOWP (window) && XWINDOW (window) != it->w)
4241 continue;
4242
4243 /* If the text ``under'' the overlay is invisible, it has a zero
4244 dimension, and both before- and after-strings apply. */
4245 invisible = Foverlay_get (overlay, Qinvisible);
4246 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4247
4248 /* If overlay has a non-empty before-string, record it. */
4249 if ((start == charpos || (end == charpos && invis_p))
4250 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4251 && SCHARS (str))
4252 RECORD_OVERLAY_STRING (overlay, str, 0);
4253
4254 /* If overlay has a non-empty after-string, record it. */
4255 if ((end == charpos || (start == charpos && invis_p))
4256 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4257 && SCHARS (str))
4258 RECORD_OVERLAY_STRING (overlay, str, 1);
4259 }
4260
4261 #undef RECORD_OVERLAY_STRING
4262
4263 /* Sort entries. */
4264 if (n > 1)
4265 qsort (entries, n, sizeof *entries, compare_overlay_entries);
4266
4267 /* Record the total number of strings to process. */
4268 it->n_overlay_strings = n;
4269
4270 /* IT->current.overlay_string_index is the number of overlay strings
4271 that have already been consumed by IT. Copy some of the
4272 remaining overlay strings to IT->overlay_strings. */
4273 i = 0;
4274 j = it->current.overlay_string_index;
4275 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
4276 it->overlay_strings[i++] = entries[j++].string;
4277
4278 CHECK_IT (it);
4279 }
4280
4281
4282 /* Get the first chunk of overlay strings at IT's current buffer
4283 position, or at CHARPOS if that is > 0. Value is non-zero if at
4284 least one overlay string was found. */
4285
4286 static int
4287 get_overlay_strings (it, charpos)
4288 struct it *it;
4289 int charpos;
4290 {
4291 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
4292 process. This fills IT->overlay_strings with strings, and sets
4293 IT->n_overlay_strings to the total number of strings to process.
4294 IT->pos.overlay_string_index has to be set temporarily to zero
4295 because load_overlay_strings needs this; it must be set to -1
4296 when no overlay strings are found because a zero value would
4297 indicate a position in the first overlay string. */
4298 it->current.overlay_string_index = 0;
4299 load_overlay_strings (it, charpos);
4300
4301 /* If we found overlay strings, set up IT to deliver display
4302 elements from the first one. Otherwise set up IT to deliver
4303 from current_buffer. */
4304 if (it->n_overlay_strings)
4305 {
4306 /* Make sure we know settings in current_buffer, so that we can
4307 restore meaningful values when we're done with the overlay
4308 strings. */
4309 compute_stop_pos (it);
4310 xassert (it->face_id >= 0);
4311
4312 /* Save IT's settings. They are restored after all overlay
4313 strings have been processed. */
4314 xassert (it->sp == 0);
4315 push_it (it);
4316
4317 /* Set up IT to deliver display elements from the first overlay
4318 string. */
4319 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4320 it->string = it->overlay_strings[0];
4321 it->stop_charpos = 0;
4322 xassert (STRINGP (it->string));
4323 it->end_charpos = SCHARS (it->string);
4324 it->multibyte_p = STRING_MULTIBYTE (it->string);
4325 it->method = next_element_from_string;
4326 }
4327 else
4328 {
4329 it->string = Qnil;
4330 it->current.overlay_string_index = -1;
4331 it->method = next_element_from_buffer;
4332 }
4333
4334 CHECK_IT (it);
4335
4336 /* Value is non-zero if we found at least one overlay string. */
4337 return STRINGP (it->string);
4338 }
4339
4340
4341 \f
4342 /***********************************************************************
4343 Saving and restoring state
4344 ***********************************************************************/
4345
4346 /* Save current settings of IT on IT->stack. Called, for example,
4347 before setting up IT for an overlay string, to be able to restore
4348 IT's settings to what they were after the overlay string has been
4349 processed. */
4350
4351 static void
4352 push_it (it)
4353 struct it *it;
4354 {
4355 struct iterator_stack_entry *p;
4356
4357 xassert (it->sp < 2);
4358 p = it->stack + it->sp;
4359
4360 p->stop_charpos = it->stop_charpos;
4361 xassert (it->face_id >= 0);
4362 p->face_id = it->face_id;
4363 p->string = it->string;
4364 p->pos = it->current;
4365 p->end_charpos = it->end_charpos;
4366 p->string_nchars = it->string_nchars;
4367 p->area = it->area;
4368 p->multibyte_p = it->multibyte_p;
4369 p->slice = it->slice;
4370 p->space_width = it->space_width;
4371 p->font_height = it->font_height;
4372 p->voffset = it->voffset;
4373 p->string_from_display_prop_p = it->string_from_display_prop_p;
4374 p->display_ellipsis_p = 0;
4375 ++it->sp;
4376 }
4377
4378
4379 /* Restore IT's settings from IT->stack. Called, for example, when no
4380 more overlay strings must be processed, and we return to delivering
4381 display elements from a buffer, or when the end of a string from a
4382 `display' property is reached and we return to delivering display
4383 elements from an overlay string, or from a buffer. */
4384
4385 static void
4386 pop_it (it)
4387 struct it *it;
4388 {
4389 struct iterator_stack_entry *p;
4390
4391 xassert (it->sp > 0);
4392 --it->sp;
4393 p = it->stack + it->sp;
4394 it->stop_charpos = p->stop_charpos;
4395 it->face_id = p->face_id;
4396 it->string = p->string;
4397 it->current = p->pos;
4398 it->end_charpos = p->end_charpos;
4399 it->string_nchars = p->string_nchars;
4400 it->area = p->area;
4401 it->multibyte_p = p->multibyte_p;
4402 it->slice = p->slice;
4403 it->space_width = p->space_width;
4404 it->font_height = p->font_height;
4405 it->voffset = p->voffset;
4406 it->string_from_display_prop_p = p->string_from_display_prop_p;
4407 }
4408
4409
4410 \f
4411 /***********************************************************************
4412 Moving over lines
4413 ***********************************************************************/
4414
4415 /* Set IT's current position to the previous line start. */
4416
4417 static void
4418 back_to_previous_line_start (it)
4419 struct it *it;
4420 {
4421 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
4422 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
4423 }
4424
4425
4426 /* Move IT to the next line start.
4427
4428 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
4429 we skipped over part of the text (as opposed to moving the iterator
4430 continuously over the text). Otherwise, don't change the value
4431 of *SKIPPED_P.
4432
4433 Newlines may come from buffer text, overlay strings, or strings
4434 displayed via the `display' property. That's the reason we can't
4435 simply use find_next_newline_no_quit.
4436
4437 Note that this function may not skip over invisible text that is so
4438 because of text properties and immediately follows a newline. If
4439 it would, function reseat_at_next_visible_line_start, when called
4440 from set_iterator_to_next, would effectively make invisible
4441 characters following a newline part of the wrong glyph row, which
4442 leads to wrong cursor motion. */
4443
4444 static int
4445 forward_to_next_line_start (it, skipped_p)
4446 struct it *it;
4447 int *skipped_p;
4448 {
4449 int old_selective, newline_found_p, n;
4450 const int MAX_NEWLINE_DISTANCE = 500;
4451
4452 /* If already on a newline, just consume it to avoid unintended
4453 skipping over invisible text below. */
4454 if (it->what == IT_CHARACTER
4455 && it->c == '\n'
4456 && CHARPOS (it->position) == IT_CHARPOS (*it))
4457 {
4458 set_iterator_to_next (it, 0);
4459 it->c = 0;
4460 return 1;
4461 }
4462
4463 /* Don't handle selective display in the following. It's (a)
4464 unnecessary because it's done by the caller, and (b) leads to an
4465 infinite recursion because next_element_from_ellipsis indirectly
4466 calls this function. */
4467 old_selective = it->selective;
4468 it->selective = 0;
4469
4470 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
4471 from buffer text. */
4472 for (n = newline_found_p = 0;
4473 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
4474 n += STRINGP (it->string) ? 0 : 1)
4475 {
4476 if (!get_next_display_element (it))
4477 return 0;
4478 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
4479 set_iterator_to_next (it, 0);
4480 }
4481
4482 /* If we didn't find a newline near enough, see if we can use a
4483 short-cut. */
4484 if (!newline_found_p)
4485 {
4486 int start = IT_CHARPOS (*it);
4487 int limit = find_next_newline_no_quit (start, 1);
4488 Lisp_Object pos;
4489
4490 xassert (!STRINGP (it->string));
4491
4492 /* If there isn't any `display' property in sight, and no
4493 overlays, we can just use the position of the newline in
4494 buffer text. */
4495 if (it->stop_charpos >= limit
4496 || ((pos = Fnext_single_property_change (make_number (start),
4497 Qdisplay,
4498 Qnil, make_number (limit)),
4499 NILP (pos))
4500 && next_overlay_change (start) == ZV))
4501 {
4502 IT_CHARPOS (*it) = limit;
4503 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
4504 *skipped_p = newline_found_p = 1;
4505 }
4506 else
4507 {
4508 while (get_next_display_element (it)
4509 && !newline_found_p)
4510 {
4511 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
4512 set_iterator_to_next (it, 0);
4513 }
4514 }
4515 }
4516
4517 it->selective = old_selective;
4518 return newline_found_p;
4519 }
4520
4521
4522 /* Set IT's current position to the previous visible line start. Skip
4523 invisible text that is so either due to text properties or due to
4524 selective display. Caution: this does not change IT->current_x and
4525 IT->hpos. */
4526
4527 static void
4528 back_to_previous_visible_line_start (it)
4529 struct it *it;
4530 {
4531 int visible_p = 0;
4532
4533 /* Go back one newline if not on BEGV already. */
4534 if (IT_CHARPOS (*it) > BEGV)
4535 back_to_previous_line_start (it);
4536
4537 /* Move over lines that are invisible because of selective display
4538 or text properties. */
4539 while (IT_CHARPOS (*it) > BEGV
4540 && !visible_p)
4541 {
4542 visible_p = 1;
4543
4544 /* If selective > 0, then lines indented more than that values
4545 are invisible. */
4546 if (it->selective > 0
4547 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
4548 (double) it->selective)) /* iftc */
4549 visible_p = 0;
4550 else
4551 {
4552 Lisp_Object prop;
4553
4554 prop = Fget_char_property (make_number (IT_CHARPOS (*it)),
4555 Qinvisible, it->window);
4556 if (TEXT_PROP_MEANS_INVISIBLE (prop))
4557 visible_p = 0;
4558 }
4559
4560 if (visible_p)
4561 {
4562 struct it it2 = *it;
4563
4564 if (handle_display_prop (&it2) == HANDLED_RETURN)
4565 visible_p = 0;
4566 }
4567
4568 /* Back one more newline if the current one is invisible. */
4569 if (!visible_p)
4570 back_to_previous_line_start (it);
4571 }
4572
4573 xassert (IT_CHARPOS (*it) >= BEGV);
4574 xassert (IT_CHARPOS (*it) == BEGV
4575 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
4576 CHECK_IT (it);
4577 }
4578
4579
4580 /* Reseat iterator IT at the previous visible line start. Skip
4581 invisible text that is so either due to text properties or due to
4582 selective display. At the end, update IT's overlay information,
4583 face information etc. */
4584
4585 static void
4586 reseat_at_previous_visible_line_start (it)
4587 struct it *it;
4588 {
4589 back_to_previous_visible_line_start (it);
4590 reseat (it, it->current.pos, 1);
4591 CHECK_IT (it);
4592 }
4593
4594
4595 /* Reseat iterator IT on the next visible line start in the current
4596 buffer. ON_NEWLINE_P non-zero means position IT on the newline
4597 preceding the line start. Skip over invisible text that is so
4598 because of selective display. Compute faces, overlays etc at the
4599 new position. Note that this function does not skip over text that
4600 is invisible because of text properties. */
4601
4602 static void
4603 reseat_at_next_visible_line_start (it, on_newline_p)
4604 struct it *it;
4605 int on_newline_p;
4606 {
4607 int newline_found_p, skipped_p = 0;
4608
4609 newline_found_p = forward_to_next_line_start (it, &skipped_p);
4610
4611 /* Skip over lines that are invisible because they are indented
4612 more than the value of IT->selective. */
4613 if (it->selective > 0)
4614 while (IT_CHARPOS (*it) < ZV
4615 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
4616 (double) it->selective)) /* iftc */
4617 {
4618 xassert (FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
4619 newline_found_p = forward_to_next_line_start (it, &skipped_p);
4620 }
4621
4622 /* Position on the newline if that's what's requested. */
4623 if (on_newline_p && newline_found_p)
4624 {
4625 if (STRINGP (it->string))
4626 {
4627 if (IT_STRING_CHARPOS (*it) > 0)
4628 {
4629 --IT_STRING_CHARPOS (*it);
4630 --IT_STRING_BYTEPOS (*it);
4631 }
4632 }
4633 else if (IT_CHARPOS (*it) > BEGV)
4634 {
4635 --IT_CHARPOS (*it);
4636 --IT_BYTEPOS (*it);
4637 reseat (it, it->current.pos, 0);
4638 }
4639 }
4640 else if (skipped_p)
4641 reseat (it, it->current.pos, 0);
4642
4643 CHECK_IT (it);
4644 }
4645
4646
4647 \f
4648 /***********************************************************************
4649 Changing an iterator's position
4650 ***********************************************************************/
4651
4652 /* Change IT's current position to POS in current_buffer. If FORCE_P
4653 is non-zero, always check for text properties at the new position.
4654 Otherwise, text properties are only looked up if POS >=
4655 IT->check_charpos of a property. */
4656
4657 static void
4658 reseat (it, pos, force_p)
4659 struct it *it;
4660 struct text_pos pos;
4661 int force_p;
4662 {
4663 int original_pos = IT_CHARPOS (*it);
4664
4665 reseat_1 (it, pos, 0);
4666
4667 /* Determine where to check text properties. Avoid doing it
4668 where possible because text property lookup is very expensive. */
4669 if (force_p
4670 || CHARPOS (pos) > it->stop_charpos
4671 || CHARPOS (pos) < original_pos)
4672 handle_stop (it);
4673
4674 CHECK_IT (it);
4675 }
4676
4677
4678 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
4679 IT->stop_pos to POS, also. */
4680
4681 static void
4682 reseat_1 (it, pos, set_stop_p)
4683 struct it *it;
4684 struct text_pos pos;
4685 int set_stop_p;
4686 {
4687 /* Don't call this function when scanning a C string. */
4688 xassert (it->s == NULL);
4689
4690 /* POS must be a reasonable value. */
4691 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
4692
4693 it->current.pos = it->position = pos;
4694 XSETBUFFER (it->object, current_buffer);
4695 it->end_charpos = ZV;
4696 it->dpvec = NULL;
4697 it->current.dpvec_index = -1;
4698 it->current.overlay_string_index = -1;
4699 IT_STRING_CHARPOS (*it) = -1;
4700 IT_STRING_BYTEPOS (*it) = -1;
4701 it->string = Qnil;
4702 it->method = next_element_from_buffer;
4703 /* RMS: I added this to fix a bug in move_it_vertically_backward
4704 where it->area continued to relate to the starting point
4705 for the backward motion. Bug report from
4706 Nick Roberts <nick@nick.uklinux.net> on 19 May 2003.
4707 However, I am not sure whether reseat still does the right thing
4708 in general after this change. */
4709 it->area = TEXT_AREA;
4710 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
4711 it->sp = 0;
4712 it->face_before_selective_p = 0;
4713
4714 if (set_stop_p)
4715 it->stop_charpos = CHARPOS (pos);
4716 }
4717
4718
4719 /* Set up IT for displaying a string, starting at CHARPOS in window W.
4720 If S is non-null, it is a C string to iterate over. Otherwise,
4721 STRING gives a Lisp string to iterate over.
4722
4723 If PRECISION > 0, don't return more then PRECISION number of
4724 characters from the string.
4725
4726 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
4727 characters have been returned. FIELD_WIDTH < 0 means an infinite
4728 field width.
4729
4730 MULTIBYTE = 0 means disable processing of multibyte characters,
4731 MULTIBYTE > 0 means enable it,
4732 MULTIBYTE < 0 means use IT->multibyte_p.
4733
4734 IT must be initialized via a prior call to init_iterator before
4735 calling this function. */
4736
4737 static void
4738 reseat_to_string (it, s, string, charpos, precision, field_width, multibyte)
4739 struct it *it;
4740 unsigned char *s;
4741 Lisp_Object string;
4742 int charpos;
4743 int precision, field_width, multibyte;
4744 {
4745 /* No region in strings. */
4746 it->region_beg_charpos = it->region_end_charpos = -1;
4747
4748 /* No text property checks performed by default, but see below. */
4749 it->stop_charpos = -1;
4750
4751 /* Set iterator position and end position. */
4752 bzero (&it->current, sizeof it->current);
4753 it->current.overlay_string_index = -1;
4754 it->current.dpvec_index = -1;
4755 xassert (charpos >= 0);
4756
4757 /* If STRING is specified, use its multibyteness, otherwise use the
4758 setting of MULTIBYTE, if specified. */
4759 if (multibyte >= 0)
4760 it->multibyte_p = multibyte > 0;
4761
4762 if (s == NULL)
4763 {
4764 xassert (STRINGP (string));
4765 it->string = string;
4766 it->s = NULL;
4767 it->end_charpos = it->string_nchars = SCHARS (string);
4768 it->method = next_element_from_string;
4769 it->current.string_pos = string_pos (charpos, string);
4770 }
4771 else
4772 {
4773 it->s = s;
4774 it->string = Qnil;
4775
4776 /* Note that we use IT->current.pos, not it->current.string_pos,
4777 for displaying C strings. */
4778 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
4779 if (it->multibyte_p)
4780 {
4781 it->current.pos = c_string_pos (charpos, s, 1);
4782 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
4783 }
4784 else
4785 {
4786 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
4787 it->end_charpos = it->string_nchars = strlen (s);
4788 }
4789
4790 it->method = next_element_from_c_string;
4791 }
4792
4793 /* PRECISION > 0 means don't return more than PRECISION characters
4794 from the string. */
4795 if (precision > 0 && it->end_charpos - charpos > precision)
4796 it->end_charpos = it->string_nchars = charpos + precision;
4797
4798 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
4799 characters have been returned. FIELD_WIDTH == 0 means don't pad,
4800 FIELD_WIDTH < 0 means infinite field width. This is useful for
4801 padding with `-' at the end of a mode line. */
4802 if (field_width < 0)
4803 field_width = INFINITY;
4804 if (field_width > it->end_charpos - charpos)
4805 it->end_charpos = charpos + field_width;
4806
4807 /* Use the standard display table for displaying strings. */
4808 if (DISP_TABLE_P (Vstandard_display_table))
4809 it->dp = XCHAR_TABLE (Vstandard_display_table);
4810
4811 it->stop_charpos = charpos;
4812 CHECK_IT (it);
4813 }
4814
4815
4816 \f
4817 /***********************************************************************
4818 Iteration
4819 ***********************************************************************/
4820
4821 /* Load IT's display element fields with information about the next
4822 display element from the current position of IT. Value is zero if
4823 end of buffer (or C string) is reached. */
4824
4825 int
4826 get_next_display_element (it)
4827 struct it *it;
4828 {
4829 /* Non-zero means that we found a display element. Zero means that
4830 we hit the end of what we iterate over. Performance note: the
4831 function pointer `method' used here turns out to be faster than
4832 using a sequence of if-statements. */
4833 int success_p = (*it->method) (it);
4834
4835 if (it->what == IT_CHARACTER)
4836 {
4837 /* Map via display table or translate control characters.
4838 IT->c, IT->len etc. have been set to the next character by
4839 the function call above. If we have a display table, and it
4840 contains an entry for IT->c, translate it. Don't do this if
4841 IT->c itself comes from a display table, otherwise we could
4842 end up in an infinite recursion. (An alternative could be to
4843 count the recursion depth of this function and signal an
4844 error when a certain maximum depth is reached.) Is it worth
4845 it? */
4846 if (success_p && it->dpvec == NULL)
4847 {
4848 Lisp_Object dv;
4849
4850 if (it->dp
4851 && (dv = DISP_CHAR_VECTOR (it->dp, it->c),
4852 VECTORP (dv)))
4853 {
4854 struct Lisp_Vector *v = XVECTOR (dv);
4855
4856 /* Return the first character from the display table
4857 entry, if not empty. If empty, don't display the
4858 current character. */
4859 if (v->size)
4860 {
4861 it->dpvec_char_len = it->len;
4862 it->dpvec = v->contents;
4863 it->dpend = v->contents + v->size;
4864 it->current.dpvec_index = 0;
4865 it->method = next_element_from_display_vector;
4866 success_p = get_next_display_element (it);
4867 }
4868 else
4869 {
4870 set_iterator_to_next (it, 0);
4871 success_p = get_next_display_element (it);
4872 }
4873 }
4874
4875 /* Translate control characters into `\003' or `^C' form.
4876 Control characters coming from a display table entry are
4877 currently not translated because we use IT->dpvec to hold
4878 the translation. This could easily be changed but I
4879 don't believe that it is worth doing.
4880
4881 If it->multibyte_p is nonzero, eight-bit characters and
4882 non-printable multibyte characters are also translated to
4883 octal form.
4884
4885 If it->multibyte_p is zero, eight-bit characters that
4886 don't have corresponding multibyte char code are also
4887 translated to octal form. */
4888 else if ((it->c < ' '
4889 && (it->area != TEXT_AREA
4890 || (it->c != '\n' && it->c != '\t')))
4891 || (it->multibyte_p
4892 ? ((it->c >= 127
4893 && it->len == 1)
4894 || !CHAR_PRINTABLE_P (it->c))
4895 : (it->c >= 127
4896 && it->c == unibyte_char_to_multibyte (it->c))))
4897 {
4898 /* IT->c is a control character which must be displayed
4899 either as '\003' or as `^C' where the '\\' and '^'
4900 can be defined in the display table. Fill
4901 IT->ctl_chars with glyphs for what we have to
4902 display. Then, set IT->dpvec to these glyphs. */
4903 GLYPH g;
4904
4905 if (it->c < 128 && it->ctl_arrow_p)
4906 {
4907 /* Set IT->ctl_chars[0] to the glyph for `^'. */
4908 if (it->dp
4909 && INTEGERP (DISP_CTRL_GLYPH (it->dp))
4910 && GLYPH_CHAR_VALID_P (XINT (DISP_CTRL_GLYPH (it->dp))))
4911 g = XINT (DISP_CTRL_GLYPH (it->dp));
4912 else
4913 g = FAST_MAKE_GLYPH ('^', 0);
4914 XSETINT (it->ctl_chars[0], g);
4915
4916 g = FAST_MAKE_GLYPH (it->c ^ 0100, 0);
4917 XSETINT (it->ctl_chars[1], g);
4918
4919 /* Set up IT->dpvec and return first character from it. */
4920 it->dpvec_char_len = it->len;
4921 it->dpvec = it->ctl_chars;
4922 it->dpend = it->dpvec + 2;
4923 it->current.dpvec_index = 0;
4924 it->method = next_element_from_display_vector;
4925 get_next_display_element (it);
4926 }
4927 else
4928 {
4929 unsigned char str[MAX_MULTIBYTE_LENGTH];
4930 int len;
4931 int i;
4932 GLYPH escape_glyph;
4933
4934 /* Set IT->ctl_chars[0] to the glyph for `\\'. */
4935 if (it->dp
4936 && INTEGERP (DISP_ESCAPE_GLYPH (it->dp))
4937 && GLYPH_CHAR_VALID_P (XFASTINT (DISP_ESCAPE_GLYPH (it->dp))))
4938 escape_glyph = XFASTINT (DISP_ESCAPE_GLYPH (it->dp));
4939 else
4940 escape_glyph = FAST_MAKE_GLYPH ('\\', 0);
4941
4942 if (SINGLE_BYTE_CHAR_P (it->c))
4943 str[0] = it->c, len = 1;
4944 else
4945 {
4946 len = CHAR_STRING_NO_SIGNAL (it->c, str);
4947 if (len < 0)
4948 {
4949 /* It's an invalid character, which
4950 shouldn't happen actually, but due to
4951 bugs it may happen. Let's print the char
4952 as is, there's not much meaningful we can
4953 do with it. */
4954 str[0] = it->c;
4955 str[1] = it->c >> 8;
4956 str[2] = it->c >> 16;
4957 str[3] = it->c >> 24;
4958 len = 4;
4959 }
4960 }
4961
4962 for (i = 0; i < len; i++)
4963 {
4964 XSETINT (it->ctl_chars[i * 4], escape_glyph);
4965 /* Insert three more glyphs into IT->ctl_chars for
4966 the octal display of the character. */
4967 g = FAST_MAKE_GLYPH (((str[i] >> 6) & 7) + '0', 0);
4968 XSETINT (it->ctl_chars[i * 4 + 1], g);
4969 g = FAST_MAKE_GLYPH (((str[i] >> 3) & 7) + '0', 0);
4970 XSETINT (it->ctl_chars[i * 4 + 2], g);
4971 g = FAST_MAKE_GLYPH ((str[i] & 7) + '0', 0);
4972 XSETINT (it->ctl_chars[i * 4 + 3], g);
4973 }
4974
4975 /* Set up IT->dpvec and return the first character
4976 from it. */
4977 it->dpvec_char_len = it->len;
4978 it->dpvec = it->ctl_chars;
4979 it->dpend = it->dpvec + len * 4;
4980 it->current.dpvec_index = 0;
4981 it->method = next_element_from_display_vector;
4982 get_next_display_element (it);
4983 }
4984 }
4985 }
4986
4987 /* Adjust face id for a multibyte character. There are no
4988 multibyte character in unibyte text. */
4989 if (it->multibyte_p
4990 && success_p
4991 && FRAME_WINDOW_P (it->f))
4992 {
4993 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4994 it->face_id = FACE_FOR_CHAR (it->f, face, it->c);
4995 }
4996 }
4997
4998 /* Is this character the last one of a run of characters with
4999 box? If yes, set IT->end_of_box_run_p to 1. */
5000 if (it->face_box_p
5001 && it->s == NULL)
5002 {
5003 int face_id;
5004 struct face *face;
5005
5006 it->end_of_box_run_p
5007 = ((face_id = face_after_it_pos (it),
5008 face_id != it->face_id)
5009 && (face = FACE_FROM_ID (it->f, face_id),
5010 face->box == FACE_NO_BOX));
5011 }
5012
5013 /* Value is 0 if end of buffer or string reached. */
5014 return success_p;
5015 }
5016
5017
5018 /* Move IT to the next display element.
5019
5020 RESEAT_P non-zero means if called on a newline in buffer text,
5021 skip to the next visible line start.
5022
5023 Functions get_next_display_element and set_iterator_to_next are
5024 separate because I find this arrangement easier to handle than a
5025 get_next_display_element function that also increments IT's
5026 position. The way it is we can first look at an iterator's current
5027 display element, decide whether it fits on a line, and if it does,
5028 increment the iterator position. The other way around we probably
5029 would either need a flag indicating whether the iterator has to be
5030 incremented the next time, or we would have to implement a
5031 decrement position function which would not be easy to write. */
5032
5033 void
5034 set_iterator_to_next (it, reseat_p)
5035 struct it *it;
5036 int reseat_p;
5037 {
5038 /* Reset flags indicating start and end of a sequence of characters
5039 with box. Reset them at the start of this function because
5040 moving the iterator to a new position might set them. */
5041 it->start_of_box_run_p = it->end_of_box_run_p = 0;
5042
5043 if (it->method == next_element_from_buffer)
5044 {
5045 /* The current display element of IT is a character from
5046 current_buffer. Advance in the buffer, and maybe skip over
5047 invisible lines that are so because of selective display. */
5048 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
5049 reseat_at_next_visible_line_start (it, 0);
5050 else
5051 {
5052 xassert (it->len != 0);
5053 IT_BYTEPOS (*it) += it->len;
5054 IT_CHARPOS (*it) += 1;
5055 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
5056 }
5057 }
5058 else if (it->method == next_element_from_composition)
5059 {
5060 xassert (it->cmp_id >= 0 && it ->cmp_id < n_compositions);
5061 if (STRINGP (it->string))
5062 {
5063 IT_STRING_BYTEPOS (*it) += it->len;
5064 IT_STRING_CHARPOS (*it) += it->cmp_len;
5065 it->method = next_element_from_string;
5066 goto consider_string_end;
5067 }
5068 else
5069 {
5070 IT_BYTEPOS (*it) += it->len;
5071 IT_CHARPOS (*it) += it->cmp_len;
5072 it->method = next_element_from_buffer;
5073 }
5074 }
5075 else if (it->method == next_element_from_c_string)
5076 {
5077 /* Current display element of IT is from a C string. */
5078 IT_BYTEPOS (*it) += it->len;
5079 IT_CHARPOS (*it) += 1;
5080 }
5081 else if (it->method == next_element_from_display_vector)
5082 {
5083 /* Current display element of IT is from a display table entry.
5084 Advance in the display table definition. Reset it to null if
5085 end reached, and continue with characters from buffers/
5086 strings. */
5087 ++it->current.dpvec_index;
5088
5089 /* Restore face of the iterator to what they were before the
5090 display vector entry (these entries may contain faces). */
5091 it->face_id = it->saved_face_id;
5092
5093 if (it->dpvec + it->current.dpvec_index == it->dpend)
5094 {
5095 if (it->s)
5096 it->method = next_element_from_c_string;
5097 else if (STRINGP (it->string))
5098 it->method = next_element_from_string;
5099 else
5100 it->method = next_element_from_buffer;
5101
5102 it->dpvec = NULL;
5103 it->current.dpvec_index = -1;
5104
5105 /* Skip over characters which were displayed via IT->dpvec. */
5106 if (it->dpvec_char_len < 0)
5107 reseat_at_next_visible_line_start (it, 1);
5108 else if (it->dpvec_char_len > 0)
5109 {
5110 it->len = it->dpvec_char_len;
5111 set_iterator_to_next (it, reseat_p);
5112 }
5113 }
5114 }
5115 else if (it->method == next_element_from_string)
5116 {
5117 /* Current display element is a character from a Lisp string. */
5118 xassert (it->s == NULL && STRINGP (it->string));
5119 IT_STRING_BYTEPOS (*it) += it->len;
5120 IT_STRING_CHARPOS (*it) += 1;
5121
5122 consider_string_end:
5123
5124 if (it->current.overlay_string_index >= 0)
5125 {
5126 /* IT->string is an overlay string. Advance to the
5127 next, if there is one. */
5128 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
5129 next_overlay_string (it);
5130 }
5131 else
5132 {
5133 /* IT->string is not an overlay string. If we reached
5134 its end, and there is something on IT->stack, proceed
5135 with what is on the stack. This can be either another
5136 string, this time an overlay string, or a buffer. */
5137 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
5138 && it->sp > 0)
5139 {
5140 pop_it (it);
5141 if (!STRINGP (it->string))
5142 it->method = next_element_from_buffer;
5143 else
5144 goto consider_string_end;
5145 }
5146 }
5147 }
5148 else if (it->method == next_element_from_image
5149 || it->method == next_element_from_stretch)
5150 {
5151 /* The position etc with which we have to proceed are on
5152 the stack. The position may be at the end of a string,
5153 if the `display' property takes up the whole string. */
5154 pop_it (it);
5155 it->image_id = 0;
5156 if (STRINGP (it->string))
5157 {
5158 it->method = next_element_from_string;
5159 goto consider_string_end;
5160 }
5161 else
5162 it->method = next_element_from_buffer;
5163 }
5164 else
5165 /* There are no other methods defined, so this should be a bug. */
5166 abort ();
5167
5168 xassert (it->method != next_element_from_string
5169 || (STRINGP (it->string)
5170 && IT_STRING_CHARPOS (*it) >= 0));
5171 }
5172
5173
5174 /* Load IT's display element fields with information about the next
5175 display element which comes from a display table entry or from the
5176 result of translating a control character to one of the forms `^C'
5177 or `\003'. IT->dpvec holds the glyphs to return as characters. */
5178
5179 static int
5180 next_element_from_display_vector (it)
5181 struct it *it;
5182 {
5183 /* Precondition. */
5184 xassert (it->dpvec && it->current.dpvec_index >= 0);
5185
5186 /* Remember the current face id in case glyphs specify faces.
5187 IT's face is restored in set_iterator_to_next. */
5188 it->saved_face_id = it->face_id;
5189
5190 if (INTEGERP (*it->dpvec)
5191 && GLYPH_CHAR_VALID_P (XFASTINT (*it->dpvec)))
5192 {
5193 int lface_id;
5194 GLYPH g;
5195
5196 g = XFASTINT (it->dpvec[it->current.dpvec_index]);
5197 it->c = FAST_GLYPH_CHAR (g);
5198 it->len = CHAR_BYTES (it->c);
5199
5200 /* The entry may contain a face id to use. Such a face id is
5201 the id of a Lisp face, not a realized face. A face id of
5202 zero means no face is specified. */
5203 lface_id = FAST_GLYPH_FACE (g);
5204 if (lface_id)
5205 {
5206 /* The function returns -1 if lface_id is invalid. */
5207 int face_id = ascii_face_of_lisp_face (it->f, lface_id);
5208 if (face_id >= 0)
5209 it->face_id = face_id;
5210 }
5211 }
5212 else
5213 /* Display table entry is invalid. Return a space. */
5214 it->c = ' ', it->len = 1;
5215
5216 /* Don't change position and object of the iterator here. They are
5217 still the values of the character that had this display table
5218 entry or was translated, and that's what we want. */
5219 it->what = IT_CHARACTER;
5220 return 1;
5221 }
5222
5223
5224 /* Load IT with the next display element from Lisp string IT->string.
5225 IT->current.string_pos is the current position within the string.
5226 If IT->current.overlay_string_index >= 0, the Lisp string is an
5227 overlay string. */
5228
5229 static int
5230 next_element_from_string (it)
5231 struct it *it;
5232 {
5233 struct text_pos position;
5234
5235 xassert (STRINGP (it->string));
5236 xassert (IT_STRING_CHARPOS (*it) >= 0);
5237 position = it->current.string_pos;
5238
5239 /* Time to check for invisible text? */
5240 if (IT_STRING_CHARPOS (*it) < it->end_charpos
5241 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
5242 {
5243 handle_stop (it);
5244
5245 /* Since a handler may have changed IT->method, we must
5246 recurse here. */
5247 return get_next_display_element (it);
5248 }
5249
5250 if (it->current.overlay_string_index >= 0)
5251 {
5252 /* Get the next character from an overlay string. In overlay
5253 strings, There is no field width or padding with spaces to
5254 do. */
5255 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
5256 {
5257 it->what = IT_EOB;
5258 return 0;
5259 }
5260 else if (STRING_MULTIBYTE (it->string))
5261 {
5262 int remaining = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
5263 const unsigned char *s = (SDATA (it->string)
5264 + IT_STRING_BYTEPOS (*it));
5265 it->c = string_char_and_length (s, remaining, &it->len);
5266 }
5267 else
5268 {
5269 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
5270 it->len = 1;
5271 }
5272 }
5273 else
5274 {
5275 /* Get the next character from a Lisp string that is not an
5276 overlay string. Such strings come from the mode line, for
5277 example. We may have to pad with spaces, or truncate the
5278 string. See also next_element_from_c_string. */
5279 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
5280 {
5281 it->what = IT_EOB;
5282 return 0;
5283 }
5284 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
5285 {
5286 /* Pad with spaces. */
5287 it->c = ' ', it->len = 1;
5288 CHARPOS (position) = BYTEPOS (position) = -1;
5289 }
5290 else if (STRING_MULTIBYTE (it->string))
5291 {
5292 int maxlen = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
5293 const unsigned char *s = (SDATA (it->string)
5294 + IT_STRING_BYTEPOS (*it));
5295 it->c = string_char_and_length (s, maxlen, &it->len);
5296 }
5297 else
5298 {
5299 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
5300 it->len = 1;
5301 }
5302 }
5303
5304 /* Record what we have and where it came from. Note that we store a
5305 buffer position in IT->position although it could arguably be a
5306 string position. */
5307 it->what = IT_CHARACTER;
5308 it->object = it->string;
5309 it->position = position;
5310 return 1;
5311 }
5312
5313
5314 /* Load IT with next display element from C string IT->s.
5315 IT->string_nchars is the maximum number of characters to return
5316 from the string. IT->end_charpos may be greater than
5317 IT->string_nchars when this function is called, in which case we
5318 may have to return padding spaces. Value is zero if end of string
5319 reached, including padding spaces. */
5320
5321 static int
5322 next_element_from_c_string (it)
5323 struct it *it;
5324 {
5325 int success_p = 1;
5326
5327 xassert (it->s);
5328 it->what = IT_CHARACTER;
5329 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
5330 it->object = Qnil;
5331
5332 /* IT's position can be greater IT->string_nchars in case a field
5333 width or precision has been specified when the iterator was
5334 initialized. */
5335 if (IT_CHARPOS (*it) >= it->end_charpos)
5336 {
5337 /* End of the game. */
5338 it->what = IT_EOB;
5339 success_p = 0;
5340 }
5341 else if (IT_CHARPOS (*it) >= it->string_nchars)
5342 {
5343 /* Pad with spaces. */
5344 it->c = ' ', it->len = 1;
5345 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
5346 }
5347 else if (it->multibyte_p)
5348 {
5349 /* Implementation note: The calls to strlen apparently aren't a
5350 performance problem because there is no noticeable performance
5351 difference between Emacs running in unibyte or multibyte mode. */
5352 int maxlen = strlen (it->s) - IT_BYTEPOS (*it);
5353 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it),
5354 maxlen, &it->len);
5355 }
5356 else
5357 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
5358
5359 return success_p;
5360 }
5361
5362
5363 /* Set up IT to return characters from an ellipsis, if appropriate.
5364 The definition of the ellipsis glyphs may come from a display table
5365 entry. This function Fills IT with the first glyph from the
5366 ellipsis if an ellipsis is to be displayed. */
5367
5368 static int
5369 next_element_from_ellipsis (it)
5370 struct it *it;
5371 {
5372 if (it->selective_display_ellipsis_p)
5373 {
5374 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
5375 {
5376 /* Use the display table definition for `...'. Invalid glyphs
5377 will be handled by the method returning elements from dpvec. */
5378 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
5379 it->dpvec_char_len = it->len;
5380 it->dpvec = v->contents;
5381 it->dpend = v->contents + v->size;
5382 it->current.dpvec_index = 0;
5383 it->method = next_element_from_display_vector;
5384 }
5385 else
5386 {
5387 /* Use default `...' which is stored in default_invis_vector. */
5388 it->dpvec_char_len = it->len;
5389 it->dpvec = default_invis_vector;
5390 it->dpend = default_invis_vector + 3;
5391 it->current.dpvec_index = 0;
5392 it->method = next_element_from_display_vector;
5393 }
5394 }
5395 else
5396 {
5397 /* The face at the current position may be different from the
5398 face we find after the invisible text. Remember what it
5399 was in IT->saved_face_id, and signal that it's there by
5400 setting face_before_selective_p. */
5401 it->saved_face_id = it->face_id;
5402 it->method = next_element_from_buffer;
5403 reseat_at_next_visible_line_start (it, 1);
5404 it->face_before_selective_p = 1;
5405 }
5406
5407 return get_next_display_element (it);
5408 }
5409
5410
5411 /* Deliver an image display element. The iterator IT is already
5412 filled with image information (done in handle_display_prop). Value
5413 is always 1. */
5414
5415
5416 static int
5417 next_element_from_image (it)
5418 struct it *it;
5419 {
5420 it->what = IT_IMAGE;
5421 return 1;
5422 }
5423
5424
5425 /* Fill iterator IT with next display element from a stretch glyph
5426 property. IT->object is the value of the text property. Value is
5427 always 1. */
5428
5429 static int
5430 next_element_from_stretch (it)
5431 struct it *it;
5432 {
5433 it->what = IT_STRETCH;
5434 return 1;
5435 }
5436
5437
5438 /* Load IT with the next display element from current_buffer. Value
5439 is zero if end of buffer reached. IT->stop_charpos is the next
5440 position at which to stop and check for text properties or buffer
5441 end. */
5442
5443 static int
5444 next_element_from_buffer (it)
5445 struct it *it;
5446 {
5447 int success_p = 1;
5448
5449 /* Check this assumption, otherwise, we would never enter the
5450 if-statement, below. */
5451 xassert (IT_CHARPOS (*it) >= BEGV
5452 && IT_CHARPOS (*it) <= it->stop_charpos);
5453
5454 if (IT_CHARPOS (*it) >= it->stop_charpos)
5455 {
5456 if (IT_CHARPOS (*it) >= it->end_charpos)
5457 {
5458 int overlay_strings_follow_p;
5459
5460 /* End of the game, except when overlay strings follow that
5461 haven't been returned yet. */
5462 if (it->overlay_strings_at_end_processed_p)
5463 overlay_strings_follow_p = 0;
5464 else
5465 {
5466 it->overlay_strings_at_end_processed_p = 1;
5467 overlay_strings_follow_p = get_overlay_strings (it, 0);
5468 }
5469
5470 if (overlay_strings_follow_p)
5471 success_p = get_next_display_element (it);
5472 else
5473 {
5474 it->what = IT_EOB;
5475 it->position = it->current.pos;
5476 success_p = 0;
5477 }
5478 }
5479 else
5480 {
5481 handle_stop (it);
5482 return get_next_display_element (it);
5483 }
5484 }
5485 else
5486 {
5487 /* No face changes, overlays etc. in sight, so just return a
5488 character from current_buffer. */
5489 unsigned char *p;
5490
5491 /* Maybe run the redisplay end trigger hook. Performance note:
5492 This doesn't seem to cost measurable time. */
5493 if (it->redisplay_end_trigger_charpos
5494 && it->glyph_row
5495 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
5496 run_redisplay_end_trigger_hook (it);
5497
5498 /* Get the next character, maybe multibyte. */
5499 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
5500 if (it->multibyte_p && !ASCII_BYTE_P (*p))
5501 {
5502 int maxlen = ((IT_BYTEPOS (*it) >= GPT_BYTE ? ZV_BYTE : GPT_BYTE)
5503 - IT_BYTEPOS (*it));
5504 it->c = string_char_and_length (p, maxlen, &it->len);
5505 }
5506 else
5507 it->c = *p, it->len = 1;
5508
5509 /* Record what we have and where it came from. */
5510 it->what = IT_CHARACTER;;
5511 it->object = it->w->buffer;
5512 it->position = it->current.pos;
5513
5514 /* Normally we return the character found above, except when we
5515 really want to return an ellipsis for selective display. */
5516 if (it->selective)
5517 {
5518 if (it->c == '\n')
5519 {
5520 /* A value of selective > 0 means hide lines indented more
5521 than that number of columns. */
5522 if (it->selective > 0
5523 && IT_CHARPOS (*it) + 1 < ZV
5524 && indented_beyond_p (IT_CHARPOS (*it) + 1,
5525 IT_BYTEPOS (*it) + 1,
5526 (double) it->selective)) /* iftc */
5527 {
5528 success_p = next_element_from_ellipsis (it);
5529 it->dpvec_char_len = -1;
5530 }
5531 }
5532 else if (it->c == '\r' && it->selective == -1)
5533 {
5534 /* A value of selective == -1 means that everything from the
5535 CR to the end of the line is invisible, with maybe an
5536 ellipsis displayed for it. */
5537 success_p = next_element_from_ellipsis (it);
5538 it->dpvec_char_len = -1;
5539 }
5540 }
5541 }
5542
5543 /* Value is zero if end of buffer reached. */
5544 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
5545 return success_p;
5546 }
5547
5548
5549 /* Run the redisplay end trigger hook for IT. */
5550
5551 static void
5552 run_redisplay_end_trigger_hook (it)
5553 struct it *it;
5554 {
5555 Lisp_Object args[3];
5556
5557 /* IT->glyph_row should be non-null, i.e. we should be actually
5558 displaying something, or otherwise we should not run the hook. */
5559 xassert (it->glyph_row);
5560
5561 /* Set up hook arguments. */
5562 args[0] = Qredisplay_end_trigger_functions;
5563 args[1] = it->window;
5564 XSETINT (args[2], it->redisplay_end_trigger_charpos);
5565 it->redisplay_end_trigger_charpos = 0;
5566
5567 /* Since we are *trying* to run these functions, don't try to run
5568 them again, even if they get an error. */
5569 it->w->redisplay_end_trigger = Qnil;
5570 Frun_hook_with_args (3, args);
5571
5572 /* Notice if it changed the face of the character we are on. */
5573 handle_face_prop (it);
5574 }
5575
5576
5577 /* Deliver a composition display element. The iterator IT is already
5578 filled with composition information (done in
5579 handle_composition_prop). Value is always 1. */
5580
5581 static int
5582 next_element_from_composition (it)
5583 struct it *it;
5584 {
5585 it->what = IT_COMPOSITION;
5586 it->position = (STRINGP (it->string)
5587 ? it->current.string_pos
5588 : it->current.pos);
5589 return 1;
5590 }
5591
5592
5593 \f
5594 /***********************************************************************
5595 Moving an iterator without producing glyphs
5596 ***********************************************************************/
5597
5598 /* Move iterator IT to a specified buffer or X position within one
5599 line on the display without producing glyphs.
5600
5601 OP should be a bit mask including some or all of these bits:
5602 MOVE_TO_X: Stop on reaching x-position TO_X.
5603 MOVE_TO_POS: Stop on reaching buffer or string position TO_CHARPOS.
5604 Regardless of OP's value, stop in reaching the end of the display line.
5605
5606 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
5607 This means, in particular, that TO_X includes window's horizontal
5608 scroll amount.
5609
5610 The return value has several possible values that
5611 say what condition caused the scan to stop:
5612
5613 MOVE_POS_MATCH_OR_ZV
5614 - when TO_POS or ZV was reached.
5615
5616 MOVE_X_REACHED
5617 -when TO_X was reached before TO_POS or ZV were reached.
5618
5619 MOVE_LINE_CONTINUED
5620 - when we reached the end of the display area and the line must
5621 be continued.
5622
5623 MOVE_LINE_TRUNCATED
5624 - when we reached the end of the display area and the line is
5625 truncated.
5626
5627 MOVE_NEWLINE_OR_CR
5628 - when we stopped at a line end, i.e. a newline or a CR and selective
5629 display is on. */
5630
5631 static enum move_it_result
5632 move_it_in_display_line_to (it, to_charpos, to_x, op)
5633 struct it *it;
5634 int to_charpos, to_x, op;
5635 {
5636 enum move_it_result result = MOVE_UNDEFINED;
5637 struct glyph_row *saved_glyph_row;
5638
5639 /* Don't produce glyphs in produce_glyphs. */
5640 saved_glyph_row = it->glyph_row;
5641 it->glyph_row = NULL;
5642
5643 #define BUFFER_POS_REACHED_P() \
5644 ((op & MOVE_TO_POS) != 0 \
5645 && BUFFERP (it->object) \
5646 && IT_CHARPOS (*it) >= to_charpos)
5647
5648 while (1)
5649 {
5650 int x, i, ascent = 0, descent = 0;
5651
5652 /* Stop when ZV reached.
5653 We used to stop here when TO_CHARPOS reached as well, but that is
5654 too soon if this glyph does not fit on this line. So we handle it
5655 explicitly below. */
5656 if (!get_next_display_element (it)
5657 || (it->truncate_lines_p
5658 && BUFFER_POS_REACHED_P ()))
5659 {
5660 result = MOVE_POS_MATCH_OR_ZV;
5661 break;
5662 }
5663
5664 /* The call to produce_glyphs will get the metrics of the
5665 display element IT is loaded with. We record in x the
5666 x-position before this display element in case it does not
5667 fit on the line. */
5668 x = it->current_x;
5669
5670 /* Remember the line height so far in case the next element doesn't
5671 fit on the line. */
5672 if (!it->truncate_lines_p)
5673 {
5674 ascent = it->max_ascent;
5675 descent = it->max_descent;
5676 }
5677
5678 PRODUCE_GLYPHS (it);
5679
5680 if (it->area != TEXT_AREA)
5681 {
5682 set_iterator_to_next (it, 1);
5683 continue;
5684 }
5685
5686 /* The number of glyphs we get back in IT->nglyphs will normally
5687 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
5688 character on a terminal frame, or (iii) a line end. For the
5689 second case, IT->nglyphs - 1 padding glyphs will be present
5690 (on X frames, there is only one glyph produced for a
5691 composite character.
5692
5693 The behavior implemented below means, for continuation lines,
5694 that as many spaces of a TAB as fit on the current line are
5695 displayed there. For terminal frames, as many glyphs of a
5696 multi-glyph character are displayed in the current line, too.
5697 This is what the old redisplay code did, and we keep it that
5698 way. Under X, the whole shape of a complex character must
5699 fit on the line or it will be completely displayed in the
5700 next line.
5701
5702 Note that both for tabs and padding glyphs, all glyphs have
5703 the same width. */
5704 if (it->nglyphs)
5705 {
5706 /* More than one glyph or glyph doesn't fit on line. All
5707 glyphs have the same width. */
5708 int single_glyph_width = it->pixel_width / it->nglyphs;
5709 int new_x;
5710
5711 for (i = 0; i < it->nglyphs; ++i, x = new_x)
5712 {
5713 new_x = x + single_glyph_width;
5714
5715 /* We want to leave anything reaching TO_X to the caller. */
5716 if ((op & MOVE_TO_X) && new_x > to_x)
5717 {
5718 if (BUFFER_POS_REACHED_P ())
5719 goto buffer_pos_reached;
5720 it->current_x = x;
5721 result = MOVE_X_REACHED;
5722 break;
5723 }
5724 else if (/* Lines are continued. */
5725 !it->truncate_lines_p
5726 && (/* And glyph doesn't fit on the line. */
5727 new_x > it->last_visible_x
5728 /* Or it fits exactly and we're on a window
5729 system frame. */
5730 || (new_x == it->last_visible_x
5731 && FRAME_WINDOW_P (it->f))))
5732 {
5733 if (/* IT->hpos == 0 means the very first glyph
5734 doesn't fit on the line, e.g. a wide image. */
5735 it->hpos == 0
5736 || (new_x == it->last_visible_x
5737 && FRAME_WINDOW_P (it->f)))
5738 {
5739 ++it->hpos;
5740 it->current_x = new_x;
5741 if (i == it->nglyphs - 1)
5742 {
5743 set_iterator_to_next (it, 1);
5744 #ifdef HAVE_WINDOW_SYSTEM
5745 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
5746 {
5747 if (!get_next_display_element (it))
5748 {
5749 result = MOVE_POS_MATCH_OR_ZV;
5750 break;
5751 }
5752 if (BUFFER_POS_REACHED_P ())
5753 {
5754 if (ITERATOR_AT_END_OF_LINE_P (it))
5755 result = MOVE_POS_MATCH_OR_ZV;
5756 else
5757 result = MOVE_LINE_CONTINUED;
5758 break;
5759 }
5760 if (ITERATOR_AT_END_OF_LINE_P (it))
5761 {
5762 result = MOVE_NEWLINE_OR_CR;
5763 break;
5764 }
5765 }
5766 #endif /* HAVE_WINDOW_SYSTEM */
5767 }
5768 }
5769 else
5770 {
5771 it->current_x = x;
5772 it->max_ascent = ascent;
5773 it->max_descent = descent;
5774 }
5775
5776 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
5777 IT_CHARPOS (*it)));
5778 result = MOVE_LINE_CONTINUED;
5779 break;
5780 }
5781 else if (BUFFER_POS_REACHED_P ())
5782 goto buffer_pos_reached;
5783 else if (new_x > it->first_visible_x)
5784 {
5785 /* Glyph is visible. Increment number of glyphs that
5786 would be displayed. */
5787 ++it->hpos;
5788 }
5789 else
5790 {
5791 /* Glyph is completely off the left margin of the display
5792 area. Nothing to do. */
5793 }
5794 }
5795
5796 if (result != MOVE_UNDEFINED)
5797 break;
5798 }
5799 else if (BUFFER_POS_REACHED_P ())
5800 {
5801 buffer_pos_reached:
5802 it->current_x = x;
5803 it->max_ascent = ascent;
5804 it->max_descent = descent;
5805 result = MOVE_POS_MATCH_OR_ZV;
5806 break;
5807 }
5808 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
5809 {
5810 /* Stop when TO_X specified and reached. This check is
5811 necessary here because of lines consisting of a line end,
5812 only. The line end will not produce any glyphs and we
5813 would never get MOVE_X_REACHED. */
5814 xassert (it->nglyphs == 0);
5815 result = MOVE_X_REACHED;
5816 break;
5817 }
5818
5819 /* Is this a line end? If yes, we're done. */
5820 if (ITERATOR_AT_END_OF_LINE_P (it))
5821 {
5822 result = MOVE_NEWLINE_OR_CR;
5823 break;
5824 }
5825
5826 /* The current display element has been consumed. Advance
5827 to the next. */
5828 set_iterator_to_next (it, 1);
5829
5830 /* Stop if lines are truncated and IT's current x-position is
5831 past the right edge of the window now. */
5832 if (it->truncate_lines_p
5833 && it->current_x >= it->last_visible_x)
5834 {
5835 #ifdef HAVE_WINDOW_SYSTEM
5836 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
5837 {
5838 if (!get_next_display_element (it)
5839 || BUFFER_POS_REACHED_P ())
5840 {
5841 result = MOVE_POS_MATCH_OR_ZV;
5842 break;
5843 }
5844 if (ITERATOR_AT_END_OF_LINE_P (it))
5845 {
5846 result = MOVE_NEWLINE_OR_CR;
5847 break;
5848 }
5849 }
5850 #endif /* HAVE_WINDOW_SYSTEM */
5851 result = MOVE_LINE_TRUNCATED;
5852 break;
5853 }
5854 }
5855
5856 #undef BUFFER_POS_REACHED_P
5857
5858 /* Restore the iterator settings altered at the beginning of this
5859 function. */
5860 it->glyph_row = saved_glyph_row;
5861 return result;
5862 }
5863
5864
5865 /* Move IT forward until it satisfies one or more of the criteria in
5866 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
5867
5868 OP is a bit-mask that specifies where to stop, and in particular,
5869 which of those four position arguments makes a difference. See the
5870 description of enum move_operation_enum.
5871
5872 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
5873 screen line, this function will set IT to the next position >
5874 TO_CHARPOS. */
5875
5876 void
5877 move_it_to (it, to_charpos, to_x, to_y, to_vpos, op)
5878 struct it *it;
5879 int to_charpos, to_x, to_y, to_vpos;
5880 int op;
5881 {
5882 enum move_it_result skip, skip2 = MOVE_X_REACHED;
5883 int line_height;
5884 int reached = 0;
5885
5886 for (;;)
5887 {
5888 if (op & MOVE_TO_VPOS)
5889 {
5890 /* If no TO_CHARPOS and no TO_X specified, stop at the
5891 start of the line TO_VPOS. */
5892 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
5893 {
5894 if (it->vpos == to_vpos)
5895 {
5896 reached = 1;
5897 break;
5898 }
5899 else
5900 skip = move_it_in_display_line_to (it, -1, -1, 0);
5901 }
5902 else
5903 {
5904 /* TO_VPOS >= 0 means stop at TO_X in the line at
5905 TO_VPOS, or at TO_POS, whichever comes first. */
5906 if (it->vpos == to_vpos)
5907 {
5908 reached = 2;
5909 break;
5910 }
5911
5912 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
5913
5914 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
5915 {
5916 reached = 3;
5917 break;
5918 }
5919 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
5920 {
5921 /* We have reached TO_X but not in the line we want. */
5922 skip = move_it_in_display_line_to (it, to_charpos,
5923 -1, MOVE_TO_POS);
5924 if (skip == MOVE_POS_MATCH_OR_ZV)
5925 {
5926 reached = 4;
5927 break;
5928 }
5929 }
5930 }
5931 }
5932 else if (op & MOVE_TO_Y)
5933 {
5934 struct it it_backup;
5935
5936 /* TO_Y specified means stop at TO_X in the line containing
5937 TO_Y---or at TO_CHARPOS if this is reached first. The
5938 problem is that we can't really tell whether the line
5939 contains TO_Y before we have completely scanned it, and
5940 this may skip past TO_X. What we do is to first scan to
5941 TO_X.
5942
5943 If TO_X is not specified, use a TO_X of zero. The reason
5944 is to make the outcome of this function more predictable.
5945 If we didn't use TO_X == 0, we would stop at the end of
5946 the line which is probably not what a caller would expect
5947 to happen. */
5948 skip = move_it_in_display_line_to (it, to_charpos,
5949 ((op & MOVE_TO_X)
5950 ? to_x : 0),
5951 (MOVE_TO_X
5952 | (op & MOVE_TO_POS)));
5953
5954 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
5955 if (skip == MOVE_POS_MATCH_OR_ZV)
5956 {
5957 reached = 5;
5958 break;
5959 }
5960
5961 /* If TO_X was reached, we would like to know whether TO_Y
5962 is in the line. This can only be said if we know the
5963 total line height which requires us to scan the rest of
5964 the line. */
5965 if (skip == MOVE_X_REACHED)
5966 {
5967 it_backup = *it;
5968 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
5969 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
5970 op & MOVE_TO_POS);
5971 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
5972 }
5973
5974 /* Now, decide whether TO_Y is in this line. */
5975 line_height = it->max_ascent + it->max_descent;
5976 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
5977
5978 if (to_y >= it->current_y
5979 && to_y < it->current_y + line_height)
5980 {
5981 if (skip == MOVE_X_REACHED)
5982 /* If TO_Y is in this line and TO_X was reached above,
5983 we scanned too far. We have to restore IT's settings
5984 to the ones before skipping. */
5985 *it = it_backup;
5986 reached = 6;
5987 }
5988 else if (skip == MOVE_X_REACHED)
5989 {
5990 skip = skip2;
5991 if (skip == MOVE_POS_MATCH_OR_ZV)
5992 reached = 7;
5993 }
5994
5995 if (reached)
5996 break;
5997 }
5998 else
5999 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
6000
6001 switch (skip)
6002 {
6003 case MOVE_POS_MATCH_OR_ZV:
6004 reached = 8;
6005 goto out;
6006
6007 case MOVE_NEWLINE_OR_CR:
6008 set_iterator_to_next (it, 1);
6009 it->continuation_lines_width = 0;
6010 break;
6011
6012 case MOVE_LINE_TRUNCATED:
6013 it->continuation_lines_width = 0;
6014 reseat_at_next_visible_line_start (it, 0);
6015 if ((op & MOVE_TO_POS) != 0
6016 && IT_CHARPOS (*it) > to_charpos)
6017 {
6018 reached = 9;
6019 goto out;
6020 }
6021 break;
6022
6023 case MOVE_LINE_CONTINUED:
6024 it->continuation_lines_width += it->current_x;
6025 break;
6026
6027 default:
6028 abort ();
6029 }
6030
6031 /* Reset/increment for the next run. */
6032 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
6033 it->current_x = it->hpos = 0;
6034 it->current_y += it->max_ascent + it->max_descent;
6035 ++it->vpos;
6036 last_height = it->max_ascent + it->max_descent;
6037 last_max_ascent = it->max_ascent;
6038 it->max_ascent = it->max_descent = 0;
6039 }
6040
6041 out:
6042
6043 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
6044 }
6045
6046
6047 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
6048
6049 If DY > 0, move IT backward at least that many pixels. DY = 0
6050 means move IT backward to the preceding line start or BEGV. This
6051 function may move over more than DY pixels if IT->current_y - DY
6052 ends up in the middle of a line; in this case IT->current_y will be
6053 set to the top of the line moved to. */
6054
6055 void
6056 move_it_vertically_backward (it, dy)
6057 struct it *it;
6058 int dy;
6059 {
6060 int nlines, h;
6061 struct it it2, it3;
6062 int start_pos = IT_CHARPOS (*it);
6063
6064 xassert (dy >= 0);
6065
6066 /* Estimate how many newlines we must move back. */
6067 nlines = max (1, dy / FRAME_LINE_HEIGHT (it->f));
6068
6069 /* Set the iterator's position that many lines back. */
6070 while (nlines-- && IT_CHARPOS (*it) > BEGV)
6071 back_to_previous_visible_line_start (it);
6072
6073 /* Reseat the iterator here. When moving backward, we don't want
6074 reseat to skip forward over invisible text, set up the iterator
6075 to deliver from overlay strings at the new position etc. So,
6076 use reseat_1 here. */
6077 reseat_1 (it, it->current.pos, 1);
6078
6079 /* We are now surely at a line start. */
6080 it->current_x = it->hpos = 0;
6081 it->continuation_lines_width = 0;
6082
6083 /* Move forward and see what y-distance we moved. First move to the
6084 start of the next line so that we get its height. We need this
6085 height to be able to tell whether we reached the specified
6086 y-distance. */
6087 it2 = *it;
6088 it2.max_ascent = it2.max_descent = 0;
6089 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
6090 MOVE_TO_POS | MOVE_TO_VPOS);
6091 xassert (IT_CHARPOS (*it) >= BEGV);
6092 it3 = it2;
6093
6094 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
6095 xassert (IT_CHARPOS (*it) >= BEGV);
6096 /* H is the actual vertical distance from the position in *IT
6097 and the starting position. */
6098 h = it2.current_y - it->current_y;
6099 /* NLINES is the distance in number of lines. */
6100 nlines = it2.vpos - it->vpos;
6101
6102 /* Correct IT's y and vpos position
6103 so that they are relative to the starting point. */
6104 it->vpos -= nlines;
6105 it->current_y -= h;
6106
6107 if (dy == 0)
6108 {
6109 /* DY == 0 means move to the start of the screen line. The
6110 value of nlines is > 0 if continuation lines were involved. */
6111 if (nlines > 0)
6112 move_it_by_lines (it, nlines, 1);
6113 xassert (IT_CHARPOS (*it) <= start_pos);
6114 }
6115 else
6116 {
6117 /* The y-position we try to reach, relative to *IT.
6118 Note that H has been subtracted in front of the if-statement. */
6119 int target_y = it->current_y + h - dy;
6120 int y0 = it3.current_y;
6121 int y1 = line_bottom_y (&it3);
6122 int line_height = y1 - y0;
6123
6124 /* If we did not reach target_y, try to move further backward if
6125 we can. If we moved too far backward, try to move forward. */
6126 if (target_y < it->current_y
6127 /* This is heuristic. In a window that's 3 lines high, with
6128 a line height of 13 pixels each, recentering with point
6129 on the bottom line will try to move -39/2 = 19 pixels
6130 backward. Try to avoid moving into the first line. */
6131 && it->current_y - target_y > line_height / 3 * 2
6132 && IT_CHARPOS (*it) > BEGV)
6133 {
6134 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
6135 target_y - it->current_y));
6136 move_it_vertically (it, target_y - it->current_y);
6137 xassert (IT_CHARPOS (*it) >= BEGV);
6138 }
6139 else if (target_y >= it->current_y + line_height
6140 && IT_CHARPOS (*it) < ZV)
6141 {
6142 /* Should move forward by at least one line, maybe more.
6143
6144 Note: Calling move_it_by_lines can be expensive on
6145 terminal frames, where compute_motion is used (via
6146 vmotion) to do the job, when there are very long lines
6147 and truncate-lines is nil. That's the reason for
6148 treating terminal frames specially here. */
6149
6150 if (!FRAME_WINDOW_P (it->f))
6151 move_it_vertically (it, target_y - (it->current_y + line_height));
6152 else
6153 {
6154 do
6155 {
6156 move_it_by_lines (it, 1, 1);
6157 }
6158 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
6159 }
6160
6161 xassert (IT_CHARPOS (*it) >= BEGV);
6162 }
6163 }
6164 }
6165
6166
6167 /* Move IT by a specified amount of pixel lines DY. DY negative means
6168 move backwards. DY = 0 means move to start of screen line. At the
6169 end, IT will be on the start of a screen line. */
6170
6171 void
6172 move_it_vertically (it, dy)
6173 struct it *it;
6174 int dy;
6175 {
6176 if (dy <= 0)
6177 move_it_vertically_backward (it, -dy);
6178 else if (dy > 0)
6179 {
6180 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
6181 move_it_to (it, ZV, -1, it->current_y + dy, -1,
6182 MOVE_TO_POS | MOVE_TO_Y);
6183 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
6184
6185 /* If buffer ends in ZV without a newline, move to the start of
6186 the line to satisfy the post-condition. */
6187 if (IT_CHARPOS (*it) == ZV
6188 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
6189 move_it_by_lines (it, 0, 0);
6190 }
6191 }
6192
6193
6194 /* Move iterator IT past the end of the text line it is in. */
6195
6196 void
6197 move_it_past_eol (it)
6198 struct it *it;
6199 {
6200 enum move_it_result rc;
6201
6202 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
6203 if (rc == MOVE_NEWLINE_OR_CR)
6204 set_iterator_to_next (it, 0);
6205 }
6206
6207
6208 #if 0 /* Currently not used. */
6209
6210 /* Return non-zero if some text between buffer positions START_CHARPOS
6211 and END_CHARPOS is invisible. IT->window is the window for text
6212 property lookup. */
6213
6214 static int
6215 invisible_text_between_p (it, start_charpos, end_charpos)
6216 struct it *it;
6217 int start_charpos, end_charpos;
6218 {
6219 Lisp_Object prop, limit;
6220 int invisible_found_p;
6221
6222 xassert (it != NULL && start_charpos <= end_charpos);
6223
6224 /* Is text at START invisible? */
6225 prop = Fget_char_property (make_number (start_charpos), Qinvisible,
6226 it->window);
6227 if (TEXT_PROP_MEANS_INVISIBLE (prop))
6228 invisible_found_p = 1;
6229 else
6230 {
6231 limit = Fnext_single_char_property_change (make_number (start_charpos),
6232 Qinvisible, Qnil,
6233 make_number (end_charpos));
6234 invisible_found_p = XFASTINT (limit) < end_charpos;
6235 }
6236
6237 return invisible_found_p;
6238 }
6239
6240 #endif /* 0 */
6241
6242
6243 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
6244 negative means move up. DVPOS == 0 means move to the start of the
6245 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
6246 NEED_Y_P is zero, IT->current_y will be left unchanged.
6247
6248 Further optimization ideas: If we would know that IT->f doesn't use
6249 a face with proportional font, we could be faster for
6250 truncate-lines nil. */
6251
6252 void
6253 move_it_by_lines (it, dvpos, need_y_p)
6254 struct it *it;
6255 int dvpos, need_y_p;
6256 {
6257 struct position pos;
6258
6259 if (!FRAME_WINDOW_P (it->f))
6260 {
6261 struct text_pos textpos;
6262
6263 /* We can use vmotion on frames without proportional fonts. */
6264 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
6265 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
6266 reseat (it, textpos, 1);
6267 it->vpos += pos.vpos;
6268 it->current_y += pos.vpos;
6269 }
6270 else if (dvpos == 0)
6271 {
6272 /* DVPOS == 0 means move to the start of the screen line. */
6273 move_it_vertically_backward (it, 0);
6274 xassert (it->current_x == 0 && it->hpos == 0);
6275 }
6276 else if (dvpos > 0)
6277 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
6278 else
6279 {
6280 struct it it2;
6281 int start_charpos, i;
6282
6283 /* Start at the beginning of the screen line containing IT's
6284 position. */
6285 move_it_vertically_backward (it, 0);
6286
6287 /* Go back -DVPOS visible lines and reseat the iterator there. */
6288 start_charpos = IT_CHARPOS (*it);
6289 for (i = -dvpos; i && IT_CHARPOS (*it) > BEGV; --i)
6290 back_to_previous_visible_line_start (it);
6291 reseat (it, it->current.pos, 1);
6292 it->current_x = it->hpos = 0;
6293
6294 /* Above call may have moved too far if continuation lines
6295 are involved. Scan forward and see if it did. */
6296 it2 = *it;
6297 it2.vpos = it2.current_y = 0;
6298 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
6299 it->vpos -= it2.vpos;
6300 it->current_y -= it2.current_y;
6301 it->current_x = it->hpos = 0;
6302
6303 /* If we moved too far, move IT some lines forward. */
6304 if (it2.vpos > -dvpos)
6305 {
6306 int delta = it2.vpos + dvpos;
6307 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
6308 }
6309 }
6310 }
6311
6312 /* Return 1 if IT points into the middle of a display vector. */
6313
6314 int
6315 in_display_vector_p (it)
6316 struct it *it;
6317 {
6318 return (it->method == next_element_from_display_vector
6319 && it->current.dpvec_index > 0
6320 && it->dpvec + it->current.dpvec_index != it->dpend);
6321 }
6322
6323 \f
6324 /***********************************************************************
6325 Messages
6326 ***********************************************************************/
6327
6328
6329 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
6330 to *Messages*. */
6331
6332 void
6333 add_to_log (format, arg1, arg2)
6334 char *format;
6335 Lisp_Object arg1, arg2;
6336 {
6337 Lisp_Object args[3];
6338 Lisp_Object msg, fmt;
6339 char *buffer;
6340 int len;
6341 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
6342 USE_SAFE_ALLOCA;
6343
6344 /* Do nothing if called asynchronously. Inserting text into
6345 a buffer may call after-change-functions and alike and
6346 that would means running Lisp asynchronously. */
6347 if (handling_signal)
6348 return;
6349
6350 fmt = msg = Qnil;
6351 GCPRO4 (fmt, msg, arg1, arg2);
6352
6353 args[0] = fmt = build_string (format);
6354 args[1] = arg1;
6355 args[2] = arg2;
6356 msg = Fformat (3, args);
6357
6358 len = SBYTES (msg) + 1;
6359 SAFE_ALLOCA (buffer, char *, len);
6360 bcopy (SDATA (msg), buffer, len);
6361
6362 message_dolog (buffer, len - 1, 1, 0);
6363 SAFE_FREE (len);
6364
6365 UNGCPRO;
6366 }
6367
6368
6369 /* Output a newline in the *Messages* buffer if "needs" one. */
6370
6371 void
6372 message_log_maybe_newline ()
6373 {
6374 if (message_log_need_newline)
6375 message_dolog ("", 0, 1, 0);
6376 }
6377
6378
6379 /* Add a string M of length NBYTES to the message log, optionally
6380 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
6381 nonzero, means interpret the contents of M as multibyte. This
6382 function calls low-level routines in order to bypass text property
6383 hooks, etc. which might not be safe to run. */
6384
6385 void
6386 message_dolog (m, nbytes, nlflag, multibyte)
6387 const char *m;
6388 int nbytes, nlflag, multibyte;
6389 {
6390 if (!NILP (Vmemory_full))
6391 return;
6392
6393 if (!NILP (Vmessage_log_max))
6394 {
6395 struct buffer *oldbuf;
6396 Lisp_Object oldpoint, oldbegv, oldzv;
6397 int old_windows_or_buffers_changed = windows_or_buffers_changed;
6398 int point_at_end = 0;
6399 int zv_at_end = 0;
6400 Lisp_Object old_deactivate_mark, tem;
6401 struct gcpro gcpro1;
6402
6403 old_deactivate_mark = Vdeactivate_mark;
6404 oldbuf = current_buffer;
6405 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
6406 current_buffer->undo_list = Qt;
6407
6408 oldpoint = message_dolog_marker1;
6409 set_marker_restricted (oldpoint, make_number (PT), Qnil);
6410 oldbegv = message_dolog_marker2;
6411 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
6412 oldzv = message_dolog_marker3;
6413 set_marker_restricted (oldzv, make_number (ZV), Qnil);
6414 GCPRO1 (old_deactivate_mark);
6415
6416 if (PT == Z)
6417 point_at_end = 1;
6418 if (ZV == Z)
6419 zv_at_end = 1;
6420
6421 BEGV = BEG;
6422 BEGV_BYTE = BEG_BYTE;
6423 ZV = Z;
6424 ZV_BYTE = Z_BYTE;
6425 TEMP_SET_PT_BOTH (Z, Z_BYTE);
6426
6427 /* Insert the string--maybe converting multibyte to single byte
6428 or vice versa, so that all the text fits the buffer. */
6429 if (multibyte
6430 && NILP (current_buffer->enable_multibyte_characters))
6431 {
6432 int i, c, char_bytes;
6433 unsigned char work[1];
6434
6435 /* Convert a multibyte string to single-byte
6436 for the *Message* buffer. */
6437 for (i = 0; i < nbytes; i += char_bytes)
6438 {
6439 c = string_char_and_length (m + i, nbytes - i, &char_bytes);
6440 work[0] = (SINGLE_BYTE_CHAR_P (c)
6441 ? c
6442 : multibyte_char_to_unibyte (c, Qnil));
6443 insert_1_both (work, 1, 1, 1, 0, 0);
6444 }
6445 }
6446 else if (! multibyte
6447 && ! NILP (current_buffer->enable_multibyte_characters))
6448 {
6449 int i, c, char_bytes;
6450 unsigned char *msg = (unsigned char *) m;
6451 unsigned char str[MAX_MULTIBYTE_LENGTH];
6452 /* Convert a single-byte string to multibyte
6453 for the *Message* buffer. */
6454 for (i = 0; i < nbytes; i++)
6455 {
6456 c = unibyte_char_to_multibyte (msg[i]);
6457 char_bytes = CHAR_STRING (c, str);
6458 insert_1_both (str, 1, char_bytes, 1, 0, 0);
6459 }
6460 }
6461 else if (nbytes)
6462 insert_1 (m, nbytes, 1, 0, 0);
6463
6464 if (nlflag)
6465 {
6466 int this_bol, this_bol_byte, prev_bol, prev_bol_byte, dup;
6467 insert_1 ("\n", 1, 1, 0, 0);
6468
6469 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
6470 this_bol = PT;
6471 this_bol_byte = PT_BYTE;
6472
6473 /* See if this line duplicates the previous one.
6474 If so, combine duplicates. */
6475 if (this_bol > BEG)
6476 {
6477 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
6478 prev_bol = PT;
6479 prev_bol_byte = PT_BYTE;
6480
6481 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
6482 this_bol, this_bol_byte);
6483 if (dup)
6484 {
6485 del_range_both (prev_bol, prev_bol_byte,
6486 this_bol, this_bol_byte, 0);
6487 if (dup > 1)
6488 {
6489 char dupstr[40];
6490 int duplen;
6491
6492 /* If you change this format, don't forget to also
6493 change message_log_check_duplicate. */
6494 sprintf (dupstr, " [%d times]", dup);
6495 duplen = strlen (dupstr);
6496 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
6497 insert_1 (dupstr, duplen, 1, 0, 1);
6498 }
6499 }
6500 }
6501
6502 /* If we have more than the desired maximum number of lines
6503 in the *Messages* buffer now, delete the oldest ones.
6504 This is safe because we don't have undo in this buffer. */
6505
6506 if (NATNUMP (Vmessage_log_max))
6507 {
6508 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
6509 -XFASTINT (Vmessage_log_max) - 1, 0);
6510 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
6511 }
6512 }
6513 BEGV = XMARKER (oldbegv)->charpos;
6514 BEGV_BYTE = marker_byte_position (oldbegv);
6515
6516 if (zv_at_end)
6517 {
6518 ZV = Z;
6519 ZV_BYTE = Z_BYTE;
6520 }
6521 else
6522 {
6523 ZV = XMARKER (oldzv)->charpos;
6524 ZV_BYTE = marker_byte_position (oldzv);
6525 }
6526
6527 if (point_at_end)
6528 TEMP_SET_PT_BOTH (Z, Z_BYTE);
6529 else
6530 /* We can't do Fgoto_char (oldpoint) because it will run some
6531 Lisp code. */
6532 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
6533 XMARKER (oldpoint)->bytepos);
6534
6535 UNGCPRO;
6536 unchain_marker (XMARKER (oldpoint));
6537 unchain_marker (XMARKER (oldbegv));
6538 unchain_marker (XMARKER (oldzv));
6539
6540 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
6541 set_buffer_internal (oldbuf);
6542 if (NILP (tem))
6543 windows_or_buffers_changed = old_windows_or_buffers_changed;
6544 message_log_need_newline = !nlflag;
6545 Vdeactivate_mark = old_deactivate_mark;
6546 }
6547 }
6548
6549
6550 /* We are at the end of the buffer after just having inserted a newline.
6551 (Note: We depend on the fact we won't be crossing the gap.)
6552 Check to see if the most recent message looks a lot like the previous one.
6553 Return 0 if different, 1 if the new one should just replace it, or a
6554 value N > 1 if we should also append " [N times]". */
6555
6556 static int
6557 message_log_check_duplicate (prev_bol, prev_bol_byte, this_bol, this_bol_byte)
6558 int prev_bol, this_bol;
6559 int prev_bol_byte, this_bol_byte;
6560 {
6561 int i;
6562 int len = Z_BYTE - 1 - this_bol_byte;
6563 int seen_dots = 0;
6564 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
6565 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
6566
6567 for (i = 0; i < len; i++)
6568 {
6569 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
6570 seen_dots = 1;
6571 if (p1[i] != p2[i])
6572 return seen_dots;
6573 }
6574 p1 += len;
6575 if (*p1 == '\n')
6576 return 2;
6577 if (*p1++ == ' ' && *p1++ == '[')
6578 {
6579 int n = 0;
6580 while (*p1 >= '0' && *p1 <= '9')
6581 n = n * 10 + *p1++ - '0';
6582 if (strncmp (p1, " times]\n", 8) == 0)
6583 return n+1;
6584 }
6585 return 0;
6586 }
6587
6588
6589 /* Display an echo area message M with a specified length of NBYTES
6590 bytes. The string may include null characters. If M is 0, clear
6591 out any existing message, and let the mini-buffer text show
6592 through.
6593
6594 The buffer M must continue to exist until after the echo area gets
6595 cleared or some other message gets displayed there. This means do
6596 not pass text that is stored in a Lisp string; do not pass text in
6597 a buffer that was alloca'd. */
6598
6599 void
6600 message2 (m, nbytes, multibyte)
6601 const char *m;
6602 int nbytes;
6603 int multibyte;
6604 {
6605 /* First flush out any partial line written with print. */
6606 message_log_maybe_newline ();
6607 if (m)
6608 message_dolog (m, nbytes, 1, multibyte);
6609 message2_nolog (m, nbytes, multibyte);
6610 }
6611
6612
6613 /* The non-logging counterpart of message2. */
6614
6615 void
6616 message2_nolog (m, nbytes, multibyte)
6617 const char *m;
6618 int nbytes, multibyte;
6619 {
6620 struct frame *sf = SELECTED_FRAME ();
6621 message_enable_multibyte = multibyte;
6622
6623 if (noninteractive)
6624 {
6625 if (noninteractive_need_newline)
6626 putc ('\n', stderr);
6627 noninteractive_need_newline = 0;
6628 if (m)
6629 fwrite (m, nbytes, 1, stderr);
6630 if (cursor_in_echo_area == 0)
6631 fprintf (stderr, "\n");
6632 fflush (stderr);
6633 }
6634 /* A null message buffer means that the frame hasn't really been
6635 initialized yet. Error messages get reported properly by
6636 cmd_error, so this must be just an informative message; toss it. */
6637 else if (INTERACTIVE
6638 && sf->glyphs_initialized_p
6639 && FRAME_MESSAGE_BUF (sf))
6640 {
6641 Lisp_Object mini_window;
6642 struct frame *f;
6643
6644 /* Get the frame containing the mini-buffer
6645 that the selected frame is using. */
6646 mini_window = FRAME_MINIBUF_WINDOW (sf);
6647 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
6648
6649 FRAME_SAMPLE_VISIBILITY (f);
6650 if (FRAME_VISIBLE_P (sf)
6651 && ! FRAME_VISIBLE_P (f))
6652 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
6653
6654 if (m)
6655 {
6656 set_message (m, Qnil, nbytes, multibyte);
6657 if (minibuffer_auto_raise)
6658 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
6659 }
6660 else
6661 clear_message (1, 1);
6662
6663 do_pending_window_change (0);
6664 echo_area_display (1);
6665 do_pending_window_change (0);
6666 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
6667 (*frame_up_to_date_hook) (f);
6668 }
6669 }
6670
6671
6672 /* Display an echo area message M with a specified length of NBYTES
6673 bytes. The string may include null characters. If M is not a
6674 string, clear out any existing message, and let the mini-buffer
6675 text show through. */
6676
6677 void
6678 message3 (m, nbytes, multibyte)
6679 Lisp_Object m;
6680 int nbytes;
6681 int multibyte;
6682 {
6683 struct gcpro gcpro1;
6684
6685 GCPRO1 (m);
6686
6687 /* First flush out any partial line written with print. */
6688 message_log_maybe_newline ();
6689 if (STRINGP (m))
6690 message_dolog (SDATA (m), nbytes, 1, multibyte);
6691 message3_nolog (m, nbytes, multibyte);
6692
6693 UNGCPRO;
6694 }
6695
6696
6697 /* The non-logging version of message3. */
6698
6699 void
6700 message3_nolog (m, nbytes, multibyte)
6701 Lisp_Object m;
6702 int nbytes, multibyte;
6703 {
6704 struct frame *sf = SELECTED_FRAME ();
6705 message_enable_multibyte = multibyte;
6706
6707 if (noninteractive)
6708 {
6709 if (noninteractive_need_newline)
6710 putc ('\n', stderr);
6711 noninteractive_need_newline = 0;
6712 if (STRINGP (m))
6713 fwrite (SDATA (m), nbytes, 1, stderr);
6714 if (cursor_in_echo_area == 0)
6715 fprintf (stderr, "\n");
6716 fflush (stderr);
6717 }
6718 /* A null message buffer means that the frame hasn't really been
6719 initialized yet. Error messages get reported properly by
6720 cmd_error, so this must be just an informative message; toss it. */
6721 else if (INTERACTIVE
6722 && sf->glyphs_initialized_p
6723 && FRAME_MESSAGE_BUF (sf))
6724 {
6725 Lisp_Object mini_window;
6726 Lisp_Object frame;
6727 struct frame *f;
6728
6729 /* Get the frame containing the mini-buffer
6730 that the selected frame is using. */
6731 mini_window = FRAME_MINIBUF_WINDOW (sf);
6732 frame = XWINDOW (mini_window)->frame;
6733 f = XFRAME (frame);
6734
6735 FRAME_SAMPLE_VISIBILITY (f);
6736 if (FRAME_VISIBLE_P (sf)
6737 && !FRAME_VISIBLE_P (f))
6738 Fmake_frame_visible (frame);
6739
6740 if (STRINGP (m) && SCHARS (m) > 0)
6741 {
6742 set_message (NULL, m, nbytes, multibyte);
6743 if (minibuffer_auto_raise)
6744 Fraise_frame (frame);
6745 }
6746 else
6747 clear_message (1, 1);
6748
6749 do_pending_window_change (0);
6750 echo_area_display (1);
6751 do_pending_window_change (0);
6752 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
6753 (*frame_up_to_date_hook) (f);
6754 }
6755 }
6756
6757
6758 /* Display a null-terminated echo area message M. If M is 0, clear
6759 out any existing message, and let the mini-buffer text show through.
6760
6761 The buffer M must continue to exist until after the echo area gets
6762 cleared or some other message gets displayed there. Do not pass
6763 text that is stored in a Lisp string. Do not pass text in a buffer
6764 that was alloca'd. */
6765
6766 void
6767 message1 (m)
6768 char *m;
6769 {
6770 message2 (m, (m ? strlen (m) : 0), 0);
6771 }
6772
6773
6774 /* The non-logging counterpart of message1. */
6775
6776 void
6777 message1_nolog (m)
6778 char *m;
6779 {
6780 message2_nolog (m, (m ? strlen (m) : 0), 0);
6781 }
6782
6783 /* Display a message M which contains a single %s
6784 which gets replaced with STRING. */
6785
6786 void
6787 message_with_string (m, string, log)
6788 char *m;
6789 Lisp_Object string;
6790 int log;
6791 {
6792 CHECK_STRING (string);
6793
6794 if (noninteractive)
6795 {
6796 if (m)
6797 {
6798 if (noninteractive_need_newline)
6799 putc ('\n', stderr);
6800 noninteractive_need_newline = 0;
6801 fprintf (stderr, m, SDATA (string));
6802 if (cursor_in_echo_area == 0)
6803 fprintf (stderr, "\n");
6804 fflush (stderr);
6805 }
6806 }
6807 else if (INTERACTIVE)
6808 {
6809 /* The frame whose minibuffer we're going to display the message on.
6810 It may be larger than the selected frame, so we need
6811 to use its buffer, not the selected frame's buffer. */
6812 Lisp_Object mini_window;
6813 struct frame *f, *sf = SELECTED_FRAME ();
6814
6815 /* Get the frame containing the minibuffer
6816 that the selected frame is using. */
6817 mini_window = FRAME_MINIBUF_WINDOW (sf);
6818 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
6819
6820 /* A null message buffer means that the frame hasn't really been
6821 initialized yet. Error messages get reported properly by
6822 cmd_error, so this must be just an informative message; toss it. */
6823 if (FRAME_MESSAGE_BUF (f))
6824 {
6825 Lisp_Object args[2], message;
6826 struct gcpro gcpro1, gcpro2;
6827
6828 args[0] = build_string (m);
6829 args[1] = message = string;
6830 GCPRO2 (args[0], message);
6831 gcpro1.nvars = 2;
6832
6833 message = Fformat (2, args);
6834
6835 if (log)
6836 message3 (message, SBYTES (message), STRING_MULTIBYTE (message));
6837 else
6838 message3_nolog (message, SBYTES (message), STRING_MULTIBYTE (message));
6839
6840 UNGCPRO;
6841
6842 /* Print should start at the beginning of the message
6843 buffer next time. */
6844 message_buf_print = 0;
6845 }
6846 }
6847 }
6848
6849
6850 /* Dump an informative message to the minibuf. If M is 0, clear out
6851 any existing message, and let the mini-buffer text show through. */
6852
6853 /* VARARGS 1 */
6854 void
6855 message (m, a1, a2, a3)
6856 char *m;
6857 EMACS_INT a1, a2, a3;
6858 {
6859 if (noninteractive)
6860 {
6861 if (m)
6862 {
6863 if (noninteractive_need_newline)
6864 putc ('\n', stderr);
6865 noninteractive_need_newline = 0;
6866 fprintf (stderr, m, a1, a2, a3);
6867 if (cursor_in_echo_area == 0)
6868 fprintf (stderr, "\n");
6869 fflush (stderr);
6870 }
6871 }
6872 else if (INTERACTIVE)
6873 {
6874 /* The frame whose mini-buffer we're going to display the message
6875 on. It may be larger than the selected frame, so we need to
6876 use its buffer, not the selected frame's buffer. */
6877 Lisp_Object mini_window;
6878 struct frame *f, *sf = SELECTED_FRAME ();
6879
6880 /* Get the frame containing the mini-buffer
6881 that the selected frame is using. */
6882 mini_window = FRAME_MINIBUF_WINDOW (sf);
6883 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
6884
6885 /* A null message buffer means that the frame hasn't really been
6886 initialized yet. Error messages get reported properly by
6887 cmd_error, so this must be just an informative message; toss
6888 it. */
6889 if (FRAME_MESSAGE_BUF (f))
6890 {
6891 if (m)
6892 {
6893 int len;
6894 #ifdef NO_ARG_ARRAY
6895 char *a[3];
6896 a[0] = (char *) a1;
6897 a[1] = (char *) a2;
6898 a[2] = (char *) a3;
6899
6900 len = doprnt (FRAME_MESSAGE_BUF (f),
6901 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
6902 #else
6903 len = doprnt (FRAME_MESSAGE_BUF (f),
6904 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3,
6905 (char **) &a1);
6906 #endif /* NO_ARG_ARRAY */
6907
6908 message2 (FRAME_MESSAGE_BUF (f), len, 0);
6909 }
6910 else
6911 message1 (0);
6912
6913 /* Print should start at the beginning of the message
6914 buffer next time. */
6915 message_buf_print = 0;
6916 }
6917 }
6918 }
6919
6920
6921 /* The non-logging version of message. */
6922
6923 void
6924 message_nolog (m, a1, a2, a3)
6925 char *m;
6926 EMACS_INT a1, a2, a3;
6927 {
6928 Lisp_Object old_log_max;
6929 old_log_max = Vmessage_log_max;
6930 Vmessage_log_max = Qnil;
6931 message (m, a1, a2, a3);
6932 Vmessage_log_max = old_log_max;
6933 }
6934
6935
6936 /* Display the current message in the current mini-buffer. This is
6937 only called from error handlers in process.c, and is not time
6938 critical. */
6939
6940 void
6941 update_echo_area ()
6942 {
6943 if (!NILP (echo_area_buffer[0]))
6944 {
6945 Lisp_Object string;
6946 string = Fcurrent_message ();
6947 message3 (string, SBYTES (string),
6948 !NILP (current_buffer->enable_multibyte_characters));
6949 }
6950 }
6951
6952
6953 /* Make sure echo area buffers in `echo_buffers' are live.
6954 If they aren't, make new ones. */
6955
6956 static void
6957 ensure_echo_area_buffers ()
6958 {
6959 int i;
6960
6961 for (i = 0; i < 2; ++i)
6962 if (!BUFFERP (echo_buffer[i])
6963 || NILP (XBUFFER (echo_buffer[i])->name))
6964 {
6965 char name[30];
6966 Lisp_Object old_buffer;
6967 int j;
6968
6969 old_buffer = echo_buffer[i];
6970 sprintf (name, " *Echo Area %d*", i);
6971 echo_buffer[i] = Fget_buffer_create (build_string (name));
6972 XBUFFER (echo_buffer[i])->truncate_lines = Qnil;
6973
6974 for (j = 0; j < 2; ++j)
6975 if (EQ (old_buffer, echo_area_buffer[j]))
6976 echo_area_buffer[j] = echo_buffer[i];
6977 }
6978 }
6979
6980
6981 /* Call FN with args A1..A4 with either the current or last displayed
6982 echo_area_buffer as current buffer.
6983
6984 WHICH zero means use the current message buffer
6985 echo_area_buffer[0]. If that is nil, choose a suitable buffer
6986 from echo_buffer[] and clear it.
6987
6988 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
6989 suitable buffer from echo_buffer[] and clear it.
6990
6991 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
6992 that the current message becomes the last displayed one, make
6993 choose a suitable buffer for echo_area_buffer[0], and clear it.
6994
6995 Value is what FN returns. */
6996
6997 static int
6998 with_echo_area_buffer (w, which, fn, a1, a2, a3, a4)
6999 struct window *w;
7000 int which;
7001 int (*fn) P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
7002 EMACS_INT a1;
7003 Lisp_Object a2;
7004 EMACS_INT a3, a4;
7005 {
7006 Lisp_Object buffer;
7007 int this_one, the_other, clear_buffer_p, rc;
7008 int count = SPECPDL_INDEX ();
7009
7010 /* If buffers aren't live, make new ones. */
7011 ensure_echo_area_buffers ();
7012
7013 clear_buffer_p = 0;
7014
7015 if (which == 0)
7016 this_one = 0, the_other = 1;
7017 else if (which > 0)
7018 this_one = 1, the_other = 0;
7019 else
7020 {
7021 this_one = 0, the_other = 1;
7022 clear_buffer_p = 1;
7023
7024 /* We need a fresh one in case the current echo buffer equals
7025 the one containing the last displayed echo area message. */
7026 if (!NILP (echo_area_buffer[this_one])
7027 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
7028 echo_area_buffer[this_one] = Qnil;
7029 }
7030
7031 /* Choose a suitable buffer from echo_buffer[] is we don't
7032 have one. */
7033 if (NILP (echo_area_buffer[this_one]))
7034 {
7035 echo_area_buffer[this_one]
7036 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
7037 ? echo_buffer[the_other]
7038 : echo_buffer[this_one]);
7039 clear_buffer_p = 1;
7040 }
7041
7042 buffer = echo_area_buffer[this_one];
7043
7044 /* Don't get confused by reusing the buffer used for echoing
7045 for a different purpose. */
7046 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
7047 cancel_echoing ();
7048
7049 record_unwind_protect (unwind_with_echo_area_buffer,
7050 with_echo_area_buffer_unwind_data (w));
7051
7052 /* Make the echo area buffer current. Note that for display
7053 purposes, it is not necessary that the displayed window's buffer
7054 == current_buffer, except for text property lookup. So, let's
7055 only set that buffer temporarily here without doing a full
7056 Fset_window_buffer. We must also change w->pointm, though,
7057 because otherwise an assertions in unshow_buffer fails, and Emacs
7058 aborts. */
7059 set_buffer_internal_1 (XBUFFER (buffer));
7060 if (w)
7061 {
7062 w->buffer = buffer;
7063 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
7064 }
7065
7066 current_buffer->undo_list = Qt;
7067 current_buffer->read_only = Qnil;
7068 specbind (Qinhibit_read_only, Qt);
7069 specbind (Qinhibit_modification_hooks, Qt);
7070
7071 if (clear_buffer_p && Z > BEG)
7072 del_range (BEG, Z);
7073
7074 xassert (BEGV >= BEG);
7075 xassert (ZV <= Z && ZV >= BEGV);
7076
7077 rc = fn (a1, a2, a3, a4);
7078
7079 xassert (BEGV >= BEG);
7080 xassert (ZV <= Z && ZV >= BEGV);
7081
7082 unbind_to (count, Qnil);
7083 return rc;
7084 }
7085
7086
7087 /* Save state that should be preserved around the call to the function
7088 FN called in with_echo_area_buffer. */
7089
7090 static Lisp_Object
7091 with_echo_area_buffer_unwind_data (w)
7092 struct window *w;
7093 {
7094 int i = 0;
7095 Lisp_Object vector;
7096
7097 /* Reduce consing by keeping one vector in
7098 Vwith_echo_area_save_vector. */
7099 vector = Vwith_echo_area_save_vector;
7100 Vwith_echo_area_save_vector = Qnil;
7101
7102 if (NILP (vector))
7103 vector = Fmake_vector (make_number (7), Qnil);
7104
7105 XSETBUFFER (AREF (vector, i), current_buffer); ++i;
7106 AREF (vector, i) = Vdeactivate_mark, ++i;
7107 AREF (vector, i) = make_number (windows_or_buffers_changed), ++i;
7108
7109 if (w)
7110 {
7111 XSETWINDOW (AREF (vector, i), w); ++i;
7112 AREF (vector, i) = w->buffer; ++i;
7113 AREF (vector, i) = make_number (XMARKER (w->pointm)->charpos); ++i;
7114 AREF (vector, i) = make_number (XMARKER (w->pointm)->bytepos); ++i;
7115 }
7116 else
7117 {
7118 int end = i + 4;
7119 for (; i < end; ++i)
7120 AREF (vector, i) = Qnil;
7121 }
7122
7123 xassert (i == ASIZE (vector));
7124 return vector;
7125 }
7126
7127
7128 /* Restore global state from VECTOR which was created by
7129 with_echo_area_buffer_unwind_data. */
7130
7131 static Lisp_Object
7132 unwind_with_echo_area_buffer (vector)
7133 Lisp_Object vector;
7134 {
7135 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
7136 Vdeactivate_mark = AREF (vector, 1);
7137 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
7138
7139 if (WINDOWP (AREF (vector, 3)))
7140 {
7141 struct window *w;
7142 Lisp_Object buffer, charpos, bytepos;
7143
7144 w = XWINDOW (AREF (vector, 3));
7145 buffer = AREF (vector, 4);
7146 charpos = AREF (vector, 5);
7147 bytepos = AREF (vector, 6);
7148
7149 w->buffer = buffer;
7150 set_marker_both (w->pointm, buffer,
7151 XFASTINT (charpos), XFASTINT (bytepos));
7152 }
7153
7154 Vwith_echo_area_save_vector = vector;
7155 return Qnil;
7156 }
7157
7158
7159 /* Set up the echo area for use by print functions. MULTIBYTE_P
7160 non-zero means we will print multibyte. */
7161
7162 void
7163 setup_echo_area_for_printing (multibyte_p)
7164 int multibyte_p;
7165 {
7166 /* If we can't find an echo area any more, exit. */
7167 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
7168 Fkill_emacs (Qnil);
7169
7170 ensure_echo_area_buffers ();
7171
7172 if (!message_buf_print)
7173 {
7174 /* A message has been output since the last time we printed.
7175 Choose a fresh echo area buffer. */
7176 if (EQ (echo_area_buffer[1], echo_buffer[0]))
7177 echo_area_buffer[0] = echo_buffer[1];
7178 else
7179 echo_area_buffer[0] = echo_buffer[0];
7180
7181 /* Switch to that buffer and clear it. */
7182 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
7183 current_buffer->truncate_lines = Qnil;
7184
7185 if (Z > BEG)
7186 {
7187 int count = SPECPDL_INDEX ();
7188 specbind (Qinhibit_read_only, Qt);
7189 /* Note that undo recording is always disabled. */
7190 del_range (BEG, Z);
7191 unbind_to (count, Qnil);
7192 }
7193 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
7194
7195 /* Set up the buffer for the multibyteness we need. */
7196 if (multibyte_p
7197 != !NILP (current_buffer->enable_multibyte_characters))
7198 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
7199
7200 /* Raise the frame containing the echo area. */
7201 if (minibuffer_auto_raise)
7202 {
7203 struct frame *sf = SELECTED_FRAME ();
7204 Lisp_Object mini_window;
7205 mini_window = FRAME_MINIBUF_WINDOW (sf);
7206 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
7207 }
7208
7209 message_log_maybe_newline ();
7210 message_buf_print = 1;
7211 }
7212 else
7213 {
7214 if (NILP (echo_area_buffer[0]))
7215 {
7216 if (EQ (echo_area_buffer[1], echo_buffer[0]))
7217 echo_area_buffer[0] = echo_buffer[1];
7218 else
7219 echo_area_buffer[0] = echo_buffer[0];
7220 }
7221
7222 if (current_buffer != XBUFFER (echo_area_buffer[0]))
7223 {
7224 /* Someone switched buffers between print requests. */
7225 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
7226 current_buffer->truncate_lines = Qnil;
7227 }
7228 }
7229 }
7230
7231
7232 /* Display an echo area message in window W. Value is non-zero if W's
7233 height is changed. If display_last_displayed_message_p is
7234 non-zero, display the message that was last displayed, otherwise
7235 display the current message. */
7236
7237 static int
7238 display_echo_area (w)
7239 struct window *w;
7240 {
7241 int i, no_message_p, window_height_changed_p, count;
7242
7243 /* Temporarily disable garbage collections while displaying the echo
7244 area. This is done because a GC can print a message itself.
7245 That message would modify the echo area buffer's contents while a
7246 redisplay of the buffer is going on, and seriously confuse
7247 redisplay. */
7248 count = inhibit_garbage_collection ();
7249
7250 /* If there is no message, we must call display_echo_area_1
7251 nevertheless because it resizes the window. But we will have to
7252 reset the echo_area_buffer in question to nil at the end because
7253 with_echo_area_buffer will sets it to an empty buffer. */
7254 i = display_last_displayed_message_p ? 1 : 0;
7255 no_message_p = NILP (echo_area_buffer[i]);
7256
7257 window_height_changed_p
7258 = with_echo_area_buffer (w, display_last_displayed_message_p,
7259 display_echo_area_1,
7260 (EMACS_INT) w, Qnil, 0, 0);
7261
7262 if (no_message_p)
7263 echo_area_buffer[i] = Qnil;
7264
7265 unbind_to (count, Qnil);
7266 return window_height_changed_p;
7267 }
7268
7269
7270 /* Helper for display_echo_area. Display the current buffer which
7271 contains the current echo area message in window W, a mini-window,
7272 a pointer to which is passed in A1. A2..A4 are currently not used.
7273 Change the height of W so that all of the message is displayed.
7274 Value is non-zero if height of W was changed. */
7275
7276 static int
7277 display_echo_area_1 (a1, a2, a3, a4)
7278 EMACS_INT a1;
7279 Lisp_Object a2;
7280 EMACS_INT a3, a4;
7281 {
7282 struct window *w = (struct window *) a1;
7283 Lisp_Object window;
7284 struct text_pos start;
7285 int window_height_changed_p = 0;
7286
7287 /* Do this before displaying, so that we have a large enough glyph
7288 matrix for the display. */
7289 window_height_changed_p = resize_mini_window (w, 0);
7290
7291 /* Display. */
7292 clear_glyph_matrix (w->desired_matrix);
7293 XSETWINDOW (window, w);
7294 SET_TEXT_POS (start, BEG, BEG_BYTE);
7295 try_window (window, start);
7296
7297 return window_height_changed_p;
7298 }
7299
7300
7301 /* Resize the echo area window to exactly the size needed for the
7302 currently displayed message, if there is one. If a mini-buffer
7303 is active, don't shrink it. */
7304
7305 void
7306 resize_echo_area_exactly ()
7307 {
7308 if (BUFFERP (echo_area_buffer[0])
7309 && WINDOWP (echo_area_window))
7310 {
7311 struct window *w = XWINDOW (echo_area_window);
7312 int resized_p;
7313 Lisp_Object resize_exactly;
7314
7315 if (minibuf_level == 0)
7316 resize_exactly = Qt;
7317 else
7318 resize_exactly = Qnil;
7319
7320 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
7321 (EMACS_INT) w, resize_exactly, 0, 0);
7322 if (resized_p)
7323 {
7324 ++windows_or_buffers_changed;
7325 ++update_mode_lines;
7326 redisplay_internal (0);
7327 }
7328 }
7329 }
7330
7331
7332 /* Callback function for with_echo_area_buffer, when used from
7333 resize_echo_area_exactly. A1 contains a pointer to the window to
7334 resize, EXACTLY non-nil means resize the mini-window exactly to the
7335 size of the text displayed. A3 and A4 are not used. Value is what
7336 resize_mini_window returns. */
7337
7338 static int
7339 resize_mini_window_1 (a1, exactly, a3, a4)
7340 EMACS_INT a1;
7341 Lisp_Object exactly;
7342 EMACS_INT a3, a4;
7343 {
7344 return resize_mini_window ((struct window *) a1, !NILP (exactly));
7345 }
7346
7347
7348 /* Resize mini-window W to fit the size of its contents. EXACT:P
7349 means size the window exactly to the size needed. Otherwise, it's
7350 only enlarged until W's buffer is empty. Value is non-zero if
7351 the window height has been changed. */
7352
7353 int
7354 resize_mini_window (w, exact_p)
7355 struct window *w;
7356 int exact_p;
7357 {
7358 struct frame *f = XFRAME (w->frame);
7359 int window_height_changed_p = 0;
7360
7361 xassert (MINI_WINDOW_P (w));
7362
7363 /* Don't resize windows while redisplaying a window; it would
7364 confuse redisplay functions when the size of the window they are
7365 displaying changes from under them. Such a resizing can happen,
7366 for instance, when which-func prints a long message while
7367 we are running fontification-functions. We're running these
7368 functions with safe_call which binds inhibit-redisplay to t. */
7369 if (!NILP (Vinhibit_redisplay))
7370 return 0;
7371
7372 /* Nil means don't try to resize. */
7373 if (NILP (Vresize_mini_windows)
7374 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
7375 return 0;
7376
7377 if (!FRAME_MINIBUF_ONLY_P (f))
7378 {
7379 struct it it;
7380 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
7381 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
7382 int height, max_height;
7383 int unit = FRAME_LINE_HEIGHT (f);
7384 struct text_pos start;
7385 struct buffer *old_current_buffer = NULL;
7386
7387 if (current_buffer != XBUFFER (w->buffer))
7388 {
7389 old_current_buffer = current_buffer;
7390 set_buffer_internal (XBUFFER (w->buffer));
7391 }
7392
7393 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
7394
7395 /* Compute the max. number of lines specified by the user. */
7396 if (FLOATP (Vmax_mini_window_height))
7397 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
7398 else if (INTEGERP (Vmax_mini_window_height))
7399 max_height = XINT (Vmax_mini_window_height);
7400 else
7401 max_height = total_height / 4;
7402
7403 /* Correct that max. height if it's bogus. */
7404 max_height = max (1, max_height);
7405 max_height = min (total_height, max_height);
7406
7407 /* Find out the height of the text in the window. */
7408 if (it.truncate_lines_p)
7409 height = 1;
7410 else
7411 {
7412 last_height = 0;
7413 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
7414 if (it.max_ascent == 0 && it.max_descent == 0)
7415 height = it.current_y + last_height;
7416 else
7417 height = it.current_y + it.max_ascent + it.max_descent;
7418 height -= it.extra_line_spacing;
7419 height = (height + unit - 1) / unit;
7420 }
7421
7422 /* Compute a suitable window start. */
7423 if (height > max_height)
7424 {
7425 height = max_height;
7426 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
7427 move_it_vertically_backward (&it, (height - 1) * unit);
7428 start = it.current.pos;
7429 }
7430 else
7431 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
7432 SET_MARKER_FROM_TEXT_POS (w->start, start);
7433
7434 if (EQ (Vresize_mini_windows, Qgrow_only))
7435 {
7436 /* Let it grow only, until we display an empty message, in which
7437 case the window shrinks again. */
7438 if (height > WINDOW_TOTAL_LINES (w))
7439 {
7440 int old_height = WINDOW_TOTAL_LINES (w);
7441 freeze_window_starts (f, 1);
7442 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
7443 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
7444 }
7445 else if (height < WINDOW_TOTAL_LINES (w)
7446 && (exact_p || BEGV == ZV))
7447 {
7448 int old_height = WINDOW_TOTAL_LINES (w);
7449 freeze_window_starts (f, 0);
7450 shrink_mini_window (w);
7451 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
7452 }
7453 }
7454 else
7455 {
7456 /* Always resize to exact size needed. */
7457 if (height > WINDOW_TOTAL_LINES (w))
7458 {
7459 int old_height = WINDOW_TOTAL_LINES (w);
7460 freeze_window_starts (f, 1);
7461 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
7462 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
7463 }
7464 else if (height < WINDOW_TOTAL_LINES (w))
7465 {
7466 int old_height = WINDOW_TOTAL_LINES (w);
7467 freeze_window_starts (f, 0);
7468 shrink_mini_window (w);
7469
7470 if (height)
7471 {
7472 freeze_window_starts (f, 1);
7473 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
7474 }
7475
7476 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
7477 }
7478 }
7479
7480 if (old_current_buffer)
7481 set_buffer_internal (old_current_buffer);
7482 }
7483
7484 return window_height_changed_p;
7485 }
7486
7487
7488 /* Value is the current message, a string, or nil if there is no
7489 current message. */
7490
7491 Lisp_Object
7492 current_message ()
7493 {
7494 Lisp_Object msg;
7495
7496 if (NILP (echo_area_buffer[0]))
7497 msg = Qnil;
7498 else
7499 {
7500 with_echo_area_buffer (0, 0, current_message_1,
7501 (EMACS_INT) &msg, Qnil, 0, 0);
7502 if (NILP (msg))
7503 echo_area_buffer[0] = Qnil;
7504 }
7505
7506 return msg;
7507 }
7508
7509
7510 static int
7511 current_message_1 (a1, a2, a3, a4)
7512 EMACS_INT a1;
7513 Lisp_Object a2;
7514 EMACS_INT a3, a4;
7515 {
7516 Lisp_Object *msg = (Lisp_Object *) a1;
7517
7518 if (Z > BEG)
7519 *msg = make_buffer_string (BEG, Z, 1);
7520 else
7521 *msg = Qnil;
7522 return 0;
7523 }
7524
7525
7526 /* Push the current message on Vmessage_stack for later restauration
7527 by restore_message. Value is non-zero if the current message isn't
7528 empty. This is a relatively infrequent operation, so it's not
7529 worth optimizing. */
7530
7531 int
7532 push_message ()
7533 {
7534 Lisp_Object msg;
7535 msg = current_message ();
7536 Vmessage_stack = Fcons (msg, Vmessage_stack);
7537 return STRINGP (msg);
7538 }
7539
7540
7541 /* Restore message display from the top of Vmessage_stack. */
7542
7543 void
7544 restore_message ()
7545 {
7546 Lisp_Object msg;
7547
7548 xassert (CONSP (Vmessage_stack));
7549 msg = XCAR (Vmessage_stack);
7550 if (STRINGP (msg))
7551 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
7552 else
7553 message3_nolog (msg, 0, 0);
7554 }
7555
7556
7557 /* Handler for record_unwind_protect calling pop_message. */
7558
7559 Lisp_Object
7560 pop_message_unwind (dummy)
7561 Lisp_Object dummy;
7562 {
7563 pop_message ();
7564 return Qnil;
7565 }
7566
7567 /* Pop the top-most entry off Vmessage_stack. */
7568
7569 void
7570 pop_message ()
7571 {
7572 xassert (CONSP (Vmessage_stack));
7573 Vmessage_stack = XCDR (Vmessage_stack);
7574 }
7575
7576
7577 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
7578 exits. If the stack is not empty, we have a missing pop_message
7579 somewhere. */
7580
7581 void
7582 check_message_stack ()
7583 {
7584 if (!NILP (Vmessage_stack))
7585 abort ();
7586 }
7587
7588
7589 /* Truncate to NCHARS what will be displayed in the echo area the next
7590 time we display it---but don't redisplay it now. */
7591
7592 void
7593 truncate_echo_area (nchars)
7594 int nchars;
7595 {
7596 if (nchars == 0)
7597 echo_area_buffer[0] = Qnil;
7598 /* A null message buffer means that the frame hasn't really been
7599 initialized yet. Error messages get reported properly by
7600 cmd_error, so this must be just an informative message; toss it. */
7601 else if (!noninteractive
7602 && INTERACTIVE
7603 && !NILP (echo_area_buffer[0]))
7604 {
7605 struct frame *sf = SELECTED_FRAME ();
7606 if (FRAME_MESSAGE_BUF (sf))
7607 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
7608 }
7609 }
7610
7611
7612 /* Helper function for truncate_echo_area. Truncate the current
7613 message to at most NCHARS characters. */
7614
7615 static int
7616 truncate_message_1 (nchars, a2, a3, a4)
7617 EMACS_INT nchars;
7618 Lisp_Object a2;
7619 EMACS_INT a3, a4;
7620 {
7621 if (BEG + nchars < Z)
7622 del_range (BEG + nchars, Z);
7623 if (Z == BEG)
7624 echo_area_buffer[0] = Qnil;
7625 return 0;
7626 }
7627
7628
7629 /* Set the current message to a substring of S or STRING.
7630
7631 If STRING is a Lisp string, set the message to the first NBYTES
7632 bytes from STRING. NBYTES zero means use the whole string. If
7633 STRING is multibyte, the message will be displayed multibyte.
7634
7635 If S is not null, set the message to the first LEN bytes of S. LEN
7636 zero means use the whole string. MULTIBYTE_P non-zero means S is
7637 multibyte. Display the message multibyte in that case. */
7638
7639 void
7640 set_message (s, string, nbytes, multibyte_p)
7641 const char *s;
7642 Lisp_Object string;
7643 int nbytes, multibyte_p;
7644 {
7645 message_enable_multibyte
7646 = ((s && multibyte_p)
7647 || (STRINGP (string) && STRING_MULTIBYTE (string)));
7648
7649 with_echo_area_buffer (0, -1, set_message_1,
7650 (EMACS_INT) s, string, nbytes, multibyte_p);
7651 message_buf_print = 0;
7652 help_echo_showing_p = 0;
7653 }
7654
7655
7656 /* Helper function for set_message. Arguments have the same meaning
7657 as there, with A1 corresponding to S and A2 corresponding to STRING
7658 This function is called with the echo area buffer being
7659 current. */
7660
7661 static int
7662 set_message_1 (a1, a2, nbytes, multibyte_p)
7663 EMACS_INT a1;
7664 Lisp_Object a2;
7665 EMACS_INT nbytes, multibyte_p;
7666 {
7667 const char *s = (const char *) a1;
7668 Lisp_Object string = a2;
7669
7670 xassert (BEG == Z);
7671
7672 /* Change multibyteness of the echo buffer appropriately. */
7673 if (message_enable_multibyte
7674 != !NILP (current_buffer->enable_multibyte_characters))
7675 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
7676
7677 current_buffer->truncate_lines = message_truncate_lines ? Qt : Qnil;
7678
7679 /* Insert new message at BEG. */
7680 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
7681
7682 if (STRINGP (string))
7683 {
7684 int nchars;
7685
7686 if (nbytes == 0)
7687 nbytes = SBYTES (string);
7688 nchars = string_byte_to_char (string, nbytes);
7689
7690 /* This function takes care of single/multibyte conversion. We
7691 just have to ensure that the echo area buffer has the right
7692 setting of enable_multibyte_characters. */
7693 insert_from_string (string, 0, 0, nchars, nbytes, 1);
7694 }
7695 else if (s)
7696 {
7697 if (nbytes == 0)
7698 nbytes = strlen (s);
7699
7700 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
7701 {
7702 /* Convert from multi-byte to single-byte. */
7703 int i, c, n;
7704 unsigned char work[1];
7705
7706 /* Convert a multibyte string to single-byte. */
7707 for (i = 0; i < nbytes; i += n)
7708 {
7709 c = string_char_and_length (s + i, nbytes - i, &n);
7710 work[0] = (SINGLE_BYTE_CHAR_P (c)
7711 ? c
7712 : multibyte_char_to_unibyte (c, Qnil));
7713 insert_1_both (work, 1, 1, 1, 0, 0);
7714 }
7715 }
7716 else if (!multibyte_p
7717 && !NILP (current_buffer->enable_multibyte_characters))
7718 {
7719 /* Convert from single-byte to multi-byte. */
7720 int i, c, n;
7721 const unsigned char *msg = (const unsigned char *) s;
7722 unsigned char str[MAX_MULTIBYTE_LENGTH];
7723
7724 /* Convert a single-byte string to multibyte. */
7725 for (i = 0; i < nbytes; i++)
7726 {
7727 c = unibyte_char_to_multibyte (msg[i]);
7728 n = CHAR_STRING (c, str);
7729 insert_1_both (str, 1, n, 1, 0, 0);
7730 }
7731 }
7732 else
7733 insert_1 (s, nbytes, 1, 0, 0);
7734 }
7735
7736 return 0;
7737 }
7738
7739
7740 /* Clear messages. CURRENT_P non-zero means clear the current
7741 message. LAST_DISPLAYED_P non-zero means clear the message
7742 last displayed. */
7743
7744 void
7745 clear_message (current_p, last_displayed_p)
7746 int current_p, last_displayed_p;
7747 {
7748 if (current_p)
7749 {
7750 echo_area_buffer[0] = Qnil;
7751 message_cleared_p = 1;
7752 }
7753
7754 if (last_displayed_p)
7755 echo_area_buffer[1] = Qnil;
7756
7757 message_buf_print = 0;
7758 }
7759
7760 /* Clear garbaged frames.
7761
7762 This function is used where the old redisplay called
7763 redraw_garbaged_frames which in turn called redraw_frame which in
7764 turn called clear_frame. The call to clear_frame was a source of
7765 flickering. I believe a clear_frame is not necessary. It should
7766 suffice in the new redisplay to invalidate all current matrices,
7767 and ensure a complete redisplay of all windows. */
7768
7769 static void
7770 clear_garbaged_frames ()
7771 {
7772 if (frame_garbaged)
7773 {
7774 Lisp_Object tail, frame;
7775 int changed_count = 0;
7776
7777 FOR_EACH_FRAME (tail, frame)
7778 {
7779 struct frame *f = XFRAME (frame);
7780
7781 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
7782 {
7783 if (f->resized_p)
7784 {
7785 Fredraw_frame (frame);
7786 f->force_flush_display_p = 1;
7787 }
7788 clear_current_matrices (f);
7789 changed_count++;
7790 f->garbaged = 0;
7791 f->resized_p = 0;
7792 }
7793 }
7794
7795 frame_garbaged = 0;
7796 if (changed_count)
7797 ++windows_or_buffers_changed;
7798 }
7799 }
7800
7801
7802 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
7803 is non-zero update selected_frame. Value is non-zero if the
7804 mini-windows height has been changed. */
7805
7806 static int
7807 echo_area_display (update_frame_p)
7808 int update_frame_p;
7809 {
7810 Lisp_Object mini_window;
7811 struct window *w;
7812 struct frame *f;
7813 int window_height_changed_p = 0;
7814 struct frame *sf = SELECTED_FRAME ();
7815
7816 mini_window = FRAME_MINIBUF_WINDOW (sf);
7817 w = XWINDOW (mini_window);
7818 f = XFRAME (WINDOW_FRAME (w));
7819
7820 /* Don't display if frame is invisible or not yet initialized. */
7821 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
7822 return 0;
7823
7824 /* The terminal frame is used as the first Emacs frame on the Mac OS. */
7825 #ifndef MAC_OS8
7826 #ifdef HAVE_WINDOW_SYSTEM
7827 /* When Emacs starts, selected_frame may be a visible terminal
7828 frame, even if we run under a window system. If we let this
7829 through, a message would be displayed on the terminal. */
7830 if (EQ (selected_frame, Vterminal_frame)
7831 && !NILP (Vwindow_system))
7832 return 0;
7833 #endif /* HAVE_WINDOW_SYSTEM */
7834 #endif
7835
7836 /* Redraw garbaged frames. */
7837 if (frame_garbaged)
7838 clear_garbaged_frames ();
7839
7840 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
7841 {
7842 echo_area_window = mini_window;
7843 window_height_changed_p = display_echo_area (w);
7844 w->must_be_updated_p = 1;
7845
7846 /* Update the display, unless called from redisplay_internal.
7847 Also don't update the screen during redisplay itself. The
7848 update will happen at the end of redisplay, and an update
7849 here could cause confusion. */
7850 if (update_frame_p && !redisplaying_p)
7851 {
7852 int n = 0;
7853
7854 /* If the display update has been interrupted by pending
7855 input, update mode lines in the frame. Due to the
7856 pending input, it might have been that redisplay hasn't
7857 been called, so that mode lines above the echo area are
7858 garbaged. This looks odd, so we prevent it here. */
7859 if (!display_completed)
7860 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
7861
7862 if (window_height_changed_p
7863 /* Don't do this if Emacs is shutting down. Redisplay
7864 needs to run hooks. */
7865 && !NILP (Vrun_hooks))
7866 {
7867 /* Must update other windows. Likewise as in other
7868 cases, don't let this update be interrupted by
7869 pending input. */
7870 int count = SPECPDL_INDEX ();
7871 specbind (Qredisplay_dont_pause, Qt);
7872 windows_or_buffers_changed = 1;
7873 redisplay_internal (0);
7874 unbind_to (count, Qnil);
7875 }
7876 else if (FRAME_WINDOW_P (f) && n == 0)
7877 {
7878 /* Window configuration is the same as before.
7879 Can do with a display update of the echo area,
7880 unless we displayed some mode lines. */
7881 update_single_window (w, 1);
7882 rif->flush_display (f);
7883 }
7884 else
7885 update_frame (f, 1, 1);
7886
7887 /* If cursor is in the echo area, make sure that the next
7888 redisplay displays the minibuffer, so that the cursor will
7889 be replaced with what the minibuffer wants. */
7890 if (cursor_in_echo_area)
7891 ++windows_or_buffers_changed;
7892 }
7893 }
7894 else if (!EQ (mini_window, selected_window))
7895 windows_or_buffers_changed++;
7896
7897 /* Last displayed message is now the current message. */
7898 echo_area_buffer[1] = echo_area_buffer[0];
7899
7900 /* Prevent redisplay optimization in redisplay_internal by resetting
7901 this_line_start_pos. This is done because the mini-buffer now
7902 displays the message instead of its buffer text. */
7903 if (EQ (mini_window, selected_window))
7904 CHARPOS (this_line_start_pos) = 0;
7905
7906 return window_height_changed_p;
7907 }
7908
7909
7910 \f
7911 /***********************************************************************
7912 Frame Titles
7913 ***********************************************************************/
7914
7915
7916 /* The frame title buffering code is also used by Fformat_mode_line.
7917 So it is not conditioned by HAVE_WINDOW_SYSTEM. */
7918
7919 /* A buffer for constructing frame titles in it; allocated from the
7920 heap in init_xdisp and resized as needed in store_frame_title_char. */
7921
7922 static char *frame_title_buf;
7923
7924 /* The buffer's end, and a current output position in it. */
7925
7926 static char *frame_title_buf_end;
7927 static char *frame_title_ptr;
7928
7929
7930 /* Store a single character C for the frame title in frame_title_buf.
7931 Re-allocate frame_title_buf if necessary. */
7932
7933 static void
7934 #ifdef PROTOTYPES
7935 store_frame_title_char (char c)
7936 #else
7937 store_frame_title_char (c)
7938 char c;
7939 #endif
7940 {
7941 /* If output position has reached the end of the allocated buffer,
7942 double the buffer's size. */
7943 if (frame_title_ptr == frame_title_buf_end)
7944 {
7945 int len = frame_title_ptr - frame_title_buf;
7946 int new_size = 2 * len * sizeof *frame_title_buf;
7947 frame_title_buf = (char *) xrealloc (frame_title_buf, new_size);
7948 frame_title_buf_end = frame_title_buf + new_size;
7949 frame_title_ptr = frame_title_buf + len;
7950 }
7951
7952 *frame_title_ptr++ = c;
7953 }
7954
7955
7956 /* Store part of a frame title in frame_title_buf, beginning at
7957 frame_title_ptr. STR is the string to store. Do not copy
7958 characters that yield more columns than PRECISION; PRECISION <= 0
7959 means copy the whole string. Pad with spaces until FIELD_WIDTH
7960 number of characters have been copied; FIELD_WIDTH <= 0 means don't
7961 pad. Called from display_mode_element when it is used to build a
7962 frame title. */
7963
7964 static int
7965 store_frame_title (str, field_width, precision)
7966 const unsigned char *str;
7967 int field_width, precision;
7968 {
7969 int n = 0;
7970 int dummy, nbytes;
7971
7972 /* Copy at most PRECISION chars from STR. */
7973 nbytes = strlen (str);
7974 n+= c_string_width (str, nbytes, precision, &dummy, &nbytes);
7975 while (nbytes--)
7976 store_frame_title_char (*str++);
7977
7978 /* Fill up with spaces until FIELD_WIDTH reached. */
7979 while (field_width > 0
7980 && n < field_width)
7981 {
7982 store_frame_title_char (' ');
7983 ++n;
7984 }
7985
7986 return n;
7987 }
7988
7989 #ifdef HAVE_WINDOW_SYSTEM
7990
7991 /* Set the title of FRAME, if it has changed. The title format is
7992 Vicon_title_format if FRAME is iconified, otherwise it is
7993 frame_title_format. */
7994
7995 static void
7996 x_consider_frame_title (frame)
7997 Lisp_Object frame;
7998 {
7999 struct frame *f = XFRAME (frame);
8000
8001 if (FRAME_WINDOW_P (f)
8002 || FRAME_MINIBUF_ONLY_P (f)
8003 || f->explicit_name)
8004 {
8005 /* Do we have more than one visible frame on this X display? */
8006 Lisp_Object tail;
8007 Lisp_Object fmt;
8008 struct buffer *obuf;
8009 int len;
8010 struct it it;
8011
8012 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
8013 {
8014 Lisp_Object other_frame = XCAR (tail);
8015 struct frame *tf = XFRAME (other_frame);
8016
8017 if (tf != f
8018 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
8019 && !FRAME_MINIBUF_ONLY_P (tf)
8020 && !EQ (other_frame, tip_frame)
8021 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
8022 break;
8023 }
8024
8025 /* Set global variable indicating that multiple frames exist. */
8026 multiple_frames = CONSP (tail);
8027
8028 /* Switch to the buffer of selected window of the frame. Set up
8029 frame_title_ptr so that display_mode_element will output into it;
8030 then display the title. */
8031 obuf = current_buffer;
8032 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
8033 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
8034 frame_title_ptr = frame_title_buf;
8035 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
8036 NULL, DEFAULT_FACE_ID);
8037 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
8038 len = frame_title_ptr - frame_title_buf;
8039 frame_title_ptr = NULL;
8040 set_buffer_internal_1 (obuf);
8041
8042 /* Set the title only if it's changed. This avoids consing in
8043 the common case where it hasn't. (If it turns out that we've
8044 already wasted too much time by walking through the list with
8045 display_mode_element, then we might need to optimize at a
8046 higher level than this.) */
8047 if (! STRINGP (f->name)
8048 || SBYTES (f->name) != len
8049 || bcmp (frame_title_buf, SDATA (f->name), len) != 0)
8050 x_implicitly_set_name (f, make_string (frame_title_buf, len), Qnil);
8051 }
8052 }
8053
8054 #endif /* not HAVE_WINDOW_SYSTEM */
8055
8056
8057
8058 \f
8059 /***********************************************************************
8060 Menu Bars
8061 ***********************************************************************/
8062
8063
8064 /* Prepare for redisplay by updating menu-bar item lists when
8065 appropriate. This can call eval. */
8066
8067 void
8068 prepare_menu_bars ()
8069 {
8070 int all_windows;
8071 struct gcpro gcpro1, gcpro2;
8072 struct frame *f;
8073 Lisp_Object tooltip_frame;
8074
8075 #ifdef HAVE_WINDOW_SYSTEM
8076 tooltip_frame = tip_frame;
8077 #else
8078 tooltip_frame = Qnil;
8079 #endif
8080
8081 /* Update all frame titles based on their buffer names, etc. We do
8082 this before the menu bars so that the buffer-menu will show the
8083 up-to-date frame titles. */
8084 #ifdef HAVE_WINDOW_SYSTEM
8085 if (windows_or_buffers_changed || update_mode_lines)
8086 {
8087 Lisp_Object tail, frame;
8088
8089 FOR_EACH_FRAME (tail, frame)
8090 {
8091 f = XFRAME (frame);
8092 if (!EQ (frame, tooltip_frame)
8093 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
8094 x_consider_frame_title (frame);
8095 }
8096 }
8097 #endif /* HAVE_WINDOW_SYSTEM */
8098
8099 /* Update the menu bar item lists, if appropriate. This has to be
8100 done before any actual redisplay or generation of display lines. */
8101 all_windows = (update_mode_lines
8102 || buffer_shared > 1
8103 || windows_or_buffers_changed);
8104 if (all_windows)
8105 {
8106 Lisp_Object tail, frame;
8107 int count = SPECPDL_INDEX ();
8108
8109 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
8110
8111 FOR_EACH_FRAME (tail, frame)
8112 {
8113 f = XFRAME (frame);
8114
8115 /* Ignore tooltip frame. */
8116 if (EQ (frame, tooltip_frame))
8117 continue;
8118
8119 /* If a window on this frame changed size, report that to
8120 the user and clear the size-change flag. */
8121 if (FRAME_WINDOW_SIZES_CHANGED (f))
8122 {
8123 Lisp_Object functions;
8124
8125 /* Clear flag first in case we get an error below. */
8126 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
8127 functions = Vwindow_size_change_functions;
8128 GCPRO2 (tail, functions);
8129
8130 while (CONSP (functions))
8131 {
8132 call1 (XCAR (functions), frame);
8133 functions = XCDR (functions);
8134 }
8135 UNGCPRO;
8136 }
8137
8138 GCPRO1 (tail);
8139 update_menu_bar (f, 0);
8140 #ifdef HAVE_WINDOW_SYSTEM
8141 update_tool_bar (f, 0);
8142 #endif
8143 UNGCPRO;
8144 }
8145
8146 unbind_to (count, Qnil);
8147 }
8148 else
8149 {
8150 struct frame *sf = SELECTED_FRAME ();
8151 update_menu_bar (sf, 1);
8152 #ifdef HAVE_WINDOW_SYSTEM
8153 update_tool_bar (sf, 1);
8154 #endif
8155 }
8156
8157 /* Motif needs this. See comment in xmenu.c. Turn it off when
8158 pending_menu_activation is not defined. */
8159 #ifdef USE_X_TOOLKIT
8160 pending_menu_activation = 0;
8161 #endif
8162 }
8163
8164
8165 /* Update the menu bar item list for frame F. This has to be done
8166 before we start to fill in any display lines, because it can call
8167 eval.
8168
8169 If SAVE_MATCH_DATA is non-zero, we must save and restore it here. */
8170
8171 static void
8172 update_menu_bar (f, save_match_data)
8173 struct frame *f;
8174 int save_match_data;
8175 {
8176 Lisp_Object window;
8177 register struct window *w;
8178
8179 /* If called recursively during a menu update, do nothing. This can
8180 happen when, for instance, an activate-menubar-hook causes a
8181 redisplay. */
8182 if (inhibit_menubar_update)
8183 return;
8184
8185 window = FRAME_SELECTED_WINDOW (f);
8186 w = XWINDOW (window);
8187
8188 #if 0 /* The if statement below this if statement used to include the
8189 condition !NILP (w->update_mode_line), rather than using
8190 update_mode_lines directly, and this if statement may have
8191 been added to make that condition work. Now the if
8192 statement below matches its comment, this isn't needed. */
8193 if (update_mode_lines)
8194 w->update_mode_line = Qt;
8195 #endif
8196
8197 if (FRAME_WINDOW_P (f)
8198 ?
8199 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
8200 || defined (USE_GTK)
8201 FRAME_EXTERNAL_MENU_BAR (f)
8202 #else
8203 FRAME_MENU_BAR_LINES (f) > 0
8204 #endif
8205 : FRAME_MENU_BAR_LINES (f) > 0)
8206 {
8207 /* If the user has switched buffers or windows, we need to
8208 recompute to reflect the new bindings. But we'll
8209 recompute when update_mode_lines is set too; that means
8210 that people can use force-mode-line-update to request
8211 that the menu bar be recomputed. The adverse effect on
8212 the rest of the redisplay algorithm is about the same as
8213 windows_or_buffers_changed anyway. */
8214 if (windows_or_buffers_changed
8215 /* This used to test w->update_mode_line, but we believe
8216 there is no need to recompute the menu in that case. */
8217 || update_mode_lines
8218 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
8219 < BUF_MODIFF (XBUFFER (w->buffer)))
8220 != !NILP (w->last_had_star))
8221 || ((!NILP (Vtransient_mark_mode)
8222 && !NILP (XBUFFER (w->buffer)->mark_active))
8223 != !NILP (w->region_showing)))
8224 {
8225 struct buffer *prev = current_buffer;
8226 int count = SPECPDL_INDEX ();
8227
8228 specbind (Qinhibit_menubar_update, Qt);
8229
8230 set_buffer_internal_1 (XBUFFER (w->buffer));
8231 if (save_match_data)
8232 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
8233 if (NILP (Voverriding_local_map_menu_flag))
8234 {
8235 specbind (Qoverriding_terminal_local_map, Qnil);
8236 specbind (Qoverriding_local_map, Qnil);
8237 }
8238
8239 /* Run the Lucid hook. */
8240 safe_run_hooks (Qactivate_menubar_hook);
8241
8242 /* If it has changed current-menubar from previous value,
8243 really recompute the menu-bar from the value. */
8244 if (! NILP (Vlucid_menu_bar_dirty_flag))
8245 call0 (Qrecompute_lucid_menubar);
8246
8247 safe_run_hooks (Qmenu_bar_update_hook);
8248 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
8249
8250 /* Redisplay the menu bar in case we changed it. */
8251 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
8252 || defined (USE_GTK)
8253 if (FRAME_WINDOW_P (f)
8254 #if defined (MAC_OS)
8255 /* All frames on Mac OS share the same menubar. So only the
8256 selected frame should be allowed to set it. */
8257 && f == SELECTED_FRAME ()
8258 #endif
8259 )
8260 set_frame_menubar (f, 0, 0);
8261 else
8262 /* On a terminal screen, the menu bar is an ordinary screen
8263 line, and this makes it get updated. */
8264 w->update_mode_line = Qt;
8265 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
8266 /* In the non-toolkit version, the menu bar is an ordinary screen
8267 line, and this makes it get updated. */
8268 w->update_mode_line = Qt;
8269 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
8270
8271 unbind_to (count, Qnil);
8272 set_buffer_internal_1 (prev);
8273 }
8274 }
8275 }
8276
8277
8278 \f
8279 /***********************************************************************
8280 Output Cursor
8281 ***********************************************************************/
8282
8283 #ifdef HAVE_WINDOW_SYSTEM
8284
8285 /* EXPORT:
8286 Nominal cursor position -- where to draw output.
8287 HPOS and VPOS are window relative glyph matrix coordinates.
8288 X and Y are window relative pixel coordinates. */
8289
8290 struct cursor_pos output_cursor;
8291
8292
8293 /* EXPORT:
8294 Set the global variable output_cursor to CURSOR. All cursor
8295 positions are relative to updated_window. */
8296
8297 void
8298 set_output_cursor (cursor)
8299 struct cursor_pos *cursor;
8300 {
8301 output_cursor.hpos = cursor->hpos;
8302 output_cursor.vpos = cursor->vpos;
8303 output_cursor.x = cursor->x;
8304 output_cursor.y = cursor->y;
8305 }
8306
8307
8308 /* EXPORT for RIF:
8309 Set a nominal cursor position.
8310
8311 HPOS and VPOS are column/row positions in a window glyph matrix. X
8312 and Y are window text area relative pixel positions.
8313
8314 If this is done during an update, updated_window will contain the
8315 window that is being updated and the position is the future output
8316 cursor position for that window. If updated_window is null, use
8317 selected_window and display the cursor at the given position. */
8318
8319 void
8320 x_cursor_to (vpos, hpos, y, x)
8321 int vpos, hpos, y, x;
8322 {
8323 struct window *w;
8324
8325 /* If updated_window is not set, work on selected_window. */
8326 if (updated_window)
8327 w = updated_window;
8328 else
8329 w = XWINDOW (selected_window);
8330
8331 /* Set the output cursor. */
8332 output_cursor.hpos = hpos;
8333 output_cursor.vpos = vpos;
8334 output_cursor.x = x;
8335 output_cursor.y = y;
8336
8337 /* If not called as part of an update, really display the cursor.
8338 This will also set the cursor position of W. */
8339 if (updated_window == NULL)
8340 {
8341 BLOCK_INPUT;
8342 display_and_set_cursor (w, 1, hpos, vpos, x, y);
8343 if (rif->flush_display_optional)
8344 rif->flush_display_optional (SELECTED_FRAME ());
8345 UNBLOCK_INPUT;
8346 }
8347 }
8348
8349 #endif /* HAVE_WINDOW_SYSTEM */
8350
8351 \f
8352 /***********************************************************************
8353 Tool-bars
8354 ***********************************************************************/
8355
8356 #ifdef HAVE_WINDOW_SYSTEM
8357
8358 /* Where the mouse was last time we reported a mouse event. */
8359
8360 FRAME_PTR last_mouse_frame;
8361
8362 /* Tool-bar item index of the item on which a mouse button was pressed
8363 or -1. */
8364
8365 int last_tool_bar_item;
8366
8367
8368 /* Update the tool-bar item list for frame F. This has to be done
8369 before we start to fill in any display lines. Called from
8370 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
8371 and restore it here. */
8372
8373 static void
8374 update_tool_bar (f, save_match_data)
8375 struct frame *f;
8376 int save_match_data;
8377 {
8378 #ifdef USE_GTK
8379 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
8380 #else
8381 int do_update = WINDOWP (f->tool_bar_window)
8382 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
8383 #endif
8384
8385 if (do_update)
8386 {
8387 Lisp_Object window;
8388 struct window *w;
8389
8390 window = FRAME_SELECTED_WINDOW (f);
8391 w = XWINDOW (window);
8392
8393 /* If the user has switched buffers or windows, we need to
8394 recompute to reflect the new bindings. But we'll
8395 recompute when update_mode_lines is set too; that means
8396 that people can use force-mode-line-update to request
8397 that the menu bar be recomputed. The adverse effect on
8398 the rest of the redisplay algorithm is about the same as
8399 windows_or_buffers_changed anyway. */
8400 if (windows_or_buffers_changed
8401 || !NILP (w->update_mode_line)
8402 || update_mode_lines
8403 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
8404 < BUF_MODIFF (XBUFFER (w->buffer)))
8405 != !NILP (w->last_had_star))
8406 || ((!NILP (Vtransient_mark_mode)
8407 && !NILP (XBUFFER (w->buffer)->mark_active))
8408 != !NILP (w->region_showing)))
8409 {
8410 struct buffer *prev = current_buffer;
8411 int count = SPECPDL_INDEX ();
8412 Lisp_Object old_tool_bar;
8413 struct gcpro gcpro1;
8414
8415 /* Set current_buffer to the buffer of the selected
8416 window of the frame, so that we get the right local
8417 keymaps. */
8418 set_buffer_internal_1 (XBUFFER (w->buffer));
8419
8420 /* Save match data, if we must. */
8421 if (save_match_data)
8422 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
8423
8424 /* Make sure that we don't accidentally use bogus keymaps. */
8425 if (NILP (Voverriding_local_map_menu_flag))
8426 {
8427 specbind (Qoverriding_terminal_local_map, Qnil);
8428 specbind (Qoverriding_local_map, Qnil);
8429 }
8430
8431 old_tool_bar = f->tool_bar_items;
8432 GCPRO1 (old_tool_bar);
8433
8434 /* Build desired tool-bar items from keymaps. */
8435 BLOCK_INPUT;
8436 f->tool_bar_items
8437 = tool_bar_items (f->tool_bar_items, &f->n_tool_bar_items);
8438 UNBLOCK_INPUT;
8439
8440 /* Redisplay the tool-bar if we changed it. */
8441 if (! NILP (Fequal (old_tool_bar, f->tool_bar_items)))
8442 w->update_mode_line = Qt;
8443
8444 UNGCPRO;
8445
8446 unbind_to (count, Qnil);
8447 set_buffer_internal_1 (prev);
8448 }
8449 }
8450 }
8451
8452
8453 /* Set F->desired_tool_bar_string to a Lisp string representing frame
8454 F's desired tool-bar contents. F->tool_bar_items must have
8455 been set up previously by calling prepare_menu_bars. */
8456
8457 static void
8458 build_desired_tool_bar_string (f)
8459 struct frame *f;
8460 {
8461 int i, size, size_needed;
8462 struct gcpro gcpro1, gcpro2, gcpro3;
8463 Lisp_Object image, plist, props;
8464
8465 image = plist = props = Qnil;
8466 GCPRO3 (image, plist, props);
8467
8468 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
8469 Otherwise, make a new string. */
8470
8471 /* The size of the string we might be able to reuse. */
8472 size = (STRINGP (f->desired_tool_bar_string)
8473 ? SCHARS (f->desired_tool_bar_string)
8474 : 0);
8475
8476 /* We need one space in the string for each image. */
8477 size_needed = f->n_tool_bar_items;
8478
8479 /* Reuse f->desired_tool_bar_string, if possible. */
8480 if (size < size_needed || NILP (f->desired_tool_bar_string))
8481 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
8482 make_number (' '));
8483 else
8484 {
8485 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
8486 Fremove_text_properties (make_number (0), make_number (size),
8487 props, f->desired_tool_bar_string);
8488 }
8489
8490 /* Put a `display' property on the string for the images to display,
8491 put a `menu_item' property on tool-bar items with a value that
8492 is the index of the item in F's tool-bar item vector. */
8493 for (i = 0; i < f->n_tool_bar_items; ++i)
8494 {
8495 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
8496
8497 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
8498 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
8499 int hmargin, vmargin, relief, idx, end;
8500 extern Lisp_Object QCrelief, QCmargin, QCconversion;
8501
8502 /* If image is a vector, choose the image according to the
8503 button state. */
8504 image = PROP (TOOL_BAR_ITEM_IMAGES);
8505 if (VECTORP (image))
8506 {
8507 if (enabled_p)
8508 idx = (selected_p
8509 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
8510 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
8511 else
8512 idx = (selected_p
8513 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
8514 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
8515
8516 xassert (ASIZE (image) >= idx);
8517 image = AREF (image, idx);
8518 }
8519 else
8520 idx = -1;
8521
8522 /* Ignore invalid image specifications. */
8523 if (!valid_image_p (image))
8524 continue;
8525
8526 /* Display the tool-bar button pressed, or depressed. */
8527 plist = Fcopy_sequence (XCDR (image));
8528
8529 /* Compute margin and relief to draw. */
8530 relief = (tool_bar_button_relief >= 0
8531 ? tool_bar_button_relief
8532 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
8533 hmargin = vmargin = relief;
8534
8535 if (INTEGERP (Vtool_bar_button_margin)
8536 && XINT (Vtool_bar_button_margin) > 0)
8537 {
8538 hmargin += XFASTINT (Vtool_bar_button_margin);
8539 vmargin += XFASTINT (Vtool_bar_button_margin);
8540 }
8541 else if (CONSP (Vtool_bar_button_margin))
8542 {
8543 if (INTEGERP (XCAR (Vtool_bar_button_margin))
8544 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
8545 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
8546
8547 if (INTEGERP (XCDR (Vtool_bar_button_margin))
8548 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
8549 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
8550 }
8551
8552 if (auto_raise_tool_bar_buttons_p)
8553 {
8554 /* Add a `:relief' property to the image spec if the item is
8555 selected. */
8556 if (selected_p)
8557 {
8558 plist = Fplist_put (plist, QCrelief, make_number (-relief));
8559 hmargin -= relief;
8560 vmargin -= relief;
8561 }
8562 }
8563 else
8564 {
8565 /* If image is selected, display it pressed, i.e. with a
8566 negative relief. If it's not selected, display it with a
8567 raised relief. */
8568 plist = Fplist_put (plist, QCrelief,
8569 (selected_p
8570 ? make_number (-relief)
8571 : make_number (relief)));
8572 hmargin -= relief;
8573 vmargin -= relief;
8574 }
8575
8576 /* Put a margin around the image. */
8577 if (hmargin || vmargin)
8578 {
8579 if (hmargin == vmargin)
8580 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
8581 else
8582 plist = Fplist_put (plist, QCmargin,
8583 Fcons (make_number (hmargin),
8584 make_number (vmargin)));
8585 }
8586
8587 /* If button is not enabled, and we don't have special images
8588 for the disabled state, make the image appear disabled by
8589 applying an appropriate algorithm to it. */
8590 if (!enabled_p && idx < 0)
8591 plist = Fplist_put (plist, QCconversion, Qdisabled);
8592
8593 /* Put a `display' text property on the string for the image to
8594 display. Put a `menu-item' property on the string that gives
8595 the start of this item's properties in the tool-bar items
8596 vector. */
8597 image = Fcons (Qimage, plist);
8598 props = list4 (Qdisplay, image,
8599 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
8600
8601 /* Let the last image hide all remaining spaces in the tool bar
8602 string. The string can be longer than needed when we reuse a
8603 previous string. */
8604 if (i + 1 == f->n_tool_bar_items)
8605 end = SCHARS (f->desired_tool_bar_string);
8606 else
8607 end = i + 1;
8608 Fadd_text_properties (make_number (i), make_number (end),
8609 props, f->desired_tool_bar_string);
8610 #undef PROP
8611 }
8612
8613 UNGCPRO;
8614 }
8615
8616
8617 /* Display one line of the tool-bar of frame IT->f. */
8618
8619 static void
8620 display_tool_bar_line (it)
8621 struct it *it;
8622 {
8623 struct glyph_row *row = it->glyph_row;
8624 int max_x = it->last_visible_x;
8625 struct glyph *last;
8626
8627 prepare_desired_row (row);
8628 row->y = it->current_y;
8629
8630 /* Note that this isn't made use of if the face hasn't a box,
8631 so there's no need to check the face here. */
8632 it->start_of_box_run_p = 1;
8633
8634 while (it->current_x < max_x)
8635 {
8636 int x_before, x, n_glyphs_before, i, nglyphs;
8637
8638 /* Get the next display element. */
8639 if (!get_next_display_element (it))
8640 break;
8641
8642 /* Produce glyphs. */
8643 x_before = it->current_x;
8644 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
8645 PRODUCE_GLYPHS (it);
8646
8647 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
8648 i = 0;
8649 x = x_before;
8650 while (i < nglyphs)
8651 {
8652 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
8653
8654 if (x + glyph->pixel_width > max_x)
8655 {
8656 /* Glyph doesn't fit on line. */
8657 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
8658 it->current_x = x;
8659 goto out;
8660 }
8661
8662 ++it->hpos;
8663 x += glyph->pixel_width;
8664 ++i;
8665 }
8666
8667 /* Stop at line ends. */
8668 if (ITERATOR_AT_END_OF_LINE_P (it))
8669 break;
8670
8671 set_iterator_to_next (it, 1);
8672 }
8673
8674 out:;
8675
8676 row->displays_text_p = row->used[TEXT_AREA] != 0;
8677 extend_face_to_end_of_line (it);
8678 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
8679 last->right_box_line_p = 1;
8680 if (last == row->glyphs[TEXT_AREA])
8681 last->left_box_line_p = 1;
8682 compute_line_metrics (it);
8683
8684 /* If line is empty, make it occupy the rest of the tool-bar. */
8685 if (!row->displays_text_p)
8686 {
8687 row->height = row->phys_height = it->last_visible_y - row->y;
8688 row->ascent = row->phys_ascent = 0;
8689 }
8690
8691 row->full_width_p = 1;
8692 row->continued_p = 0;
8693 row->truncated_on_left_p = 0;
8694 row->truncated_on_right_p = 0;
8695
8696 it->current_x = it->hpos = 0;
8697 it->current_y += row->height;
8698 ++it->vpos;
8699 ++it->glyph_row;
8700 }
8701
8702
8703 /* Value is the number of screen lines needed to make all tool-bar
8704 items of frame F visible. */
8705
8706 static int
8707 tool_bar_lines_needed (f)
8708 struct frame *f;
8709 {
8710 struct window *w = XWINDOW (f->tool_bar_window);
8711 struct it it;
8712
8713 /* Initialize an iterator for iteration over
8714 F->desired_tool_bar_string in the tool-bar window of frame F. */
8715 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
8716 it.first_visible_x = 0;
8717 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
8718 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
8719
8720 while (!ITERATOR_AT_END_P (&it))
8721 {
8722 it.glyph_row = w->desired_matrix->rows;
8723 clear_glyph_row (it.glyph_row);
8724 display_tool_bar_line (&it);
8725 }
8726
8727 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
8728 }
8729
8730
8731 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
8732 0, 1, 0,
8733 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
8734 (frame)
8735 Lisp_Object frame;
8736 {
8737 struct frame *f;
8738 struct window *w;
8739 int nlines = 0;
8740
8741 if (NILP (frame))
8742 frame = selected_frame;
8743 else
8744 CHECK_FRAME (frame);
8745 f = XFRAME (frame);
8746
8747 if (WINDOWP (f->tool_bar_window)
8748 || (w = XWINDOW (f->tool_bar_window),
8749 WINDOW_TOTAL_LINES (w) > 0))
8750 {
8751 update_tool_bar (f, 1);
8752 if (f->n_tool_bar_items)
8753 {
8754 build_desired_tool_bar_string (f);
8755 nlines = tool_bar_lines_needed (f);
8756 }
8757 }
8758
8759 return make_number (nlines);
8760 }
8761
8762
8763 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
8764 height should be changed. */
8765
8766 static int
8767 redisplay_tool_bar (f)
8768 struct frame *f;
8769 {
8770 struct window *w;
8771 struct it it;
8772 struct glyph_row *row;
8773 int change_height_p = 0;
8774
8775 #ifdef USE_GTK
8776 if (FRAME_EXTERNAL_TOOL_BAR (f))
8777 update_frame_tool_bar (f);
8778 return 0;
8779 #endif
8780
8781 /* If frame hasn't a tool-bar window or if it is zero-height, don't
8782 do anything. This means you must start with tool-bar-lines
8783 non-zero to get the auto-sizing effect. Or in other words, you
8784 can turn off tool-bars by specifying tool-bar-lines zero. */
8785 if (!WINDOWP (f->tool_bar_window)
8786 || (w = XWINDOW (f->tool_bar_window),
8787 WINDOW_TOTAL_LINES (w) == 0))
8788 return 0;
8789
8790 /* Set up an iterator for the tool-bar window. */
8791 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
8792 it.first_visible_x = 0;
8793 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
8794 row = it.glyph_row;
8795
8796 /* Build a string that represents the contents of the tool-bar. */
8797 build_desired_tool_bar_string (f);
8798 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
8799
8800 /* Display as many lines as needed to display all tool-bar items. */
8801 while (it.current_y < it.last_visible_y)
8802 display_tool_bar_line (&it);
8803
8804 /* It doesn't make much sense to try scrolling in the tool-bar
8805 window, so don't do it. */
8806 w->desired_matrix->no_scrolling_p = 1;
8807 w->must_be_updated_p = 1;
8808
8809 if (auto_resize_tool_bars_p)
8810 {
8811 int nlines;
8812
8813 /* If we couldn't display everything, change the tool-bar's
8814 height. */
8815 if (IT_STRING_CHARPOS (it) < it.end_charpos)
8816 change_height_p = 1;
8817
8818 /* If there are blank lines at the end, except for a partially
8819 visible blank line at the end that is smaller than
8820 FRAME_LINE_HEIGHT, change the tool-bar's height. */
8821 row = it.glyph_row - 1;
8822 if (!row->displays_text_p
8823 && row->height >= FRAME_LINE_HEIGHT (f))
8824 change_height_p = 1;
8825
8826 /* If row displays tool-bar items, but is partially visible,
8827 change the tool-bar's height. */
8828 if (row->displays_text_p
8829 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y)
8830 change_height_p = 1;
8831
8832 /* Resize windows as needed by changing the `tool-bar-lines'
8833 frame parameter. */
8834 if (change_height_p
8835 && (nlines = tool_bar_lines_needed (f),
8836 nlines != WINDOW_TOTAL_LINES (w)))
8837 {
8838 extern Lisp_Object Qtool_bar_lines;
8839 Lisp_Object frame;
8840 int old_height = WINDOW_TOTAL_LINES (w);
8841
8842 XSETFRAME (frame, f);
8843 clear_glyph_matrix (w->desired_matrix);
8844 Fmodify_frame_parameters (frame,
8845 Fcons (Fcons (Qtool_bar_lines,
8846 make_number (nlines)),
8847 Qnil));
8848 if (WINDOW_TOTAL_LINES (w) != old_height)
8849 fonts_changed_p = 1;
8850 }
8851 }
8852
8853 return change_height_p;
8854 }
8855
8856
8857 /* Get information about the tool-bar item which is displayed in GLYPH
8858 on frame F. Return in *PROP_IDX the index where tool-bar item
8859 properties start in F->tool_bar_items. Value is zero if
8860 GLYPH doesn't display a tool-bar item. */
8861
8862 static int
8863 tool_bar_item_info (f, glyph, prop_idx)
8864 struct frame *f;
8865 struct glyph *glyph;
8866 int *prop_idx;
8867 {
8868 Lisp_Object prop;
8869 int success_p;
8870 int charpos;
8871
8872 /* This function can be called asynchronously, which means we must
8873 exclude any possibility that Fget_text_property signals an
8874 error. */
8875 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
8876 charpos = max (0, charpos);
8877
8878 /* Get the text property `menu-item' at pos. The value of that
8879 property is the start index of this item's properties in
8880 F->tool_bar_items. */
8881 prop = Fget_text_property (make_number (charpos),
8882 Qmenu_item, f->current_tool_bar_string);
8883 if (INTEGERP (prop))
8884 {
8885 *prop_idx = XINT (prop);
8886 success_p = 1;
8887 }
8888 else
8889 success_p = 0;
8890
8891 return success_p;
8892 }
8893
8894 \f
8895 /* Get information about the tool-bar item at position X/Y on frame F.
8896 Return in *GLYPH a pointer to the glyph of the tool-bar item in
8897 the current matrix of the tool-bar window of F, or NULL if not
8898 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
8899 item in F->tool_bar_items. Value is
8900
8901 -1 if X/Y is not on a tool-bar item
8902 0 if X/Y is on the same item that was highlighted before.
8903 1 otherwise. */
8904
8905 static int
8906 get_tool_bar_item (f, x, y, glyph, hpos, vpos, prop_idx)
8907 struct frame *f;
8908 int x, y;
8909 struct glyph **glyph;
8910 int *hpos, *vpos, *prop_idx;
8911 {
8912 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
8913 struct window *w = XWINDOW (f->tool_bar_window);
8914 int area;
8915
8916 /* Find the glyph under X/Y. */
8917 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
8918 if (*glyph == NULL)
8919 return -1;
8920
8921 /* Get the start of this tool-bar item's properties in
8922 f->tool_bar_items. */
8923 if (!tool_bar_item_info (f, *glyph, prop_idx))
8924 return -1;
8925
8926 /* Is mouse on the highlighted item? */
8927 if (EQ (f->tool_bar_window, dpyinfo->mouse_face_window)
8928 && *vpos >= dpyinfo->mouse_face_beg_row
8929 && *vpos <= dpyinfo->mouse_face_end_row
8930 && (*vpos > dpyinfo->mouse_face_beg_row
8931 || *hpos >= dpyinfo->mouse_face_beg_col)
8932 && (*vpos < dpyinfo->mouse_face_end_row
8933 || *hpos < dpyinfo->mouse_face_end_col
8934 || dpyinfo->mouse_face_past_end))
8935 return 0;
8936
8937 return 1;
8938 }
8939
8940
8941 /* EXPORT:
8942 Handle mouse button event on the tool-bar of frame F, at
8943 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
8944 0 for button release. MODIFIERS is event modifiers for button
8945 release. */
8946
8947 void
8948 handle_tool_bar_click (f, x, y, down_p, modifiers)
8949 struct frame *f;
8950 int x, y, down_p;
8951 unsigned int modifiers;
8952 {
8953 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
8954 struct window *w = XWINDOW (f->tool_bar_window);
8955 int hpos, vpos, prop_idx;
8956 struct glyph *glyph;
8957 Lisp_Object enabled_p;
8958
8959 /* If not on the highlighted tool-bar item, return. */
8960 frame_to_window_pixel_xy (w, &x, &y);
8961 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
8962 return;
8963
8964 /* If item is disabled, do nothing. */
8965 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
8966 if (NILP (enabled_p))
8967 return;
8968
8969 if (down_p)
8970 {
8971 /* Show item in pressed state. */
8972 show_mouse_face (dpyinfo, DRAW_IMAGE_SUNKEN);
8973 dpyinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
8974 last_tool_bar_item = prop_idx;
8975 }
8976 else
8977 {
8978 Lisp_Object key, frame;
8979 struct input_event event;
8980 EVENT_INIT (event);
8981
8982 /* Show item in released state. */
8983 show_mouse_face (dpyinfo, DRAW_IMAGE_RAISED);
8984 dpyinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
8985
8986 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
8987
8988 XSETFRAME (frame, f);
8989 event.kind = TOOL_BAR_EVENT;
8990 event.frame_or_window = frame;
8991 event.arg = frame;
8992 kbd_buffer_store_event (&event);
8993
8994 event.kind = TOOL_BAR_EVENT;
8995 event.frame_or_window = frame;
8996 event.arg = key;
8997 event.modifiers = modifiers;
8998 kbd_buffer_store_event (&event);
8999 last_tool_bar_item = -1;
9000 }
9001 }
9002
9003
9004 /* Possibly highlight a tool-bar item on frame F when mouse moves to
9005 tool-bar window-relative coordinates X/Y. Called from
9006 note_mouse_highlight. */
9007
9008 static void
9009 note_tool_bar_highlight (f, x, y)
9010 struct frame *f;
9011 int x, y;
9012 {
9013 Lisp_Object window = f->tool_bar_window;
9014 struct window *w = XWINDOW (window);
9015 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9016 int hpos, vpos;
9017 struct glyph *glyph;
9018 struct glyph_row *row;
9019 int i;
9020 Lisp_Object enabled_p;
9021 int prop_idx;
9022 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
9023 int mouse_down_p, rc;
9024
9025 /* Function note_mouse_highlight is called with negative x(y
9026 values when mouse moves outside of the frame. */
9027 if (x <= 0 || y <= 0)
9028 {
9029 clear_mouse_face (dpyinfo);
9030 return;
9031 }
9032
9033 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
9034 if (rc < 0)
9035 {
9036 /* Not on tool-bar item. */
9037 clear_mouse_face (dpyinfo);
9038 return;
9039 }
9040 else if (rc == 0)
9041 /* On same tool-bar item as before. */
9042 goto set_help_echo;
9043
9044 clear_mouse_face (dpyinfo);
9045
9046 /* Mouse is down, but on different tool-bar item? */
9047 mouse_down_p = (dpyinfo->grabbed
9048 && f == last_mouse_frame
9049 && FRAME_LIVE_P (f));
9050 if (mouse_down_p
9051 && last_tool_bar_item != prop_idx)
9052 return;
9053
9054 dpyinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
9055 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
9056
9057 /* If tool-bar item is not enabled, don't highlight it. */
9058 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
9059 if (!NILP (enabled_p))
9060 {
9061 /* Compute the x-position of the glyph. In front and past the
9062 image is a space. We include this in the highlighted area. */
9063 row = MATRIX_ROW (w->current_matrix, vpos);
9064 for (i = x = 0; i < hpos; ++i)
9065 x += row->glyphs[TEXT_AREA][i].pixel_width;
9066
9067 /* Record this as the current active region. */
9068 dpyinfo->mouse_face_beg_col = hpos;
9069 dpyinfo->mouse_face_beg_row = vpos;
9070 dpyinfo->mouse_face_beg_x = x;
9071 dpyinfo->mouse_face_beg_y = row->y;
9072 dpyinfo->mouse_face_past_end = 0;
9073
9074 dpyinfo->mouse_face_end_col = hpos + 1;
9075 dpyinfo->mouse_face_end_row = vpos;
9076 dpyinfo->mouse_face_end_x = x + glyph->pixel_width;
9077 dpyinfo->mouse_face_end_y = row->y;
9078 dpyinfo->mouse_face_window = window;
9079 dpyinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
9080
9081 /* Display it as active. */
9082 show_mouse_face (dpyinfo, draw);
9083 dpyinfo->mouse_face_image_state = draw;
9084 }
9085
9086 set_help_echo:
9087
9088 /* Set help_echo_string to a help string to display for this tool-bar item.
9089 XTread_socket does the rest. */
9090 help_echo_object = help_echo_window = Qnil;
9091 help_echo_pos = -1;
9092 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
9093 if (NILP (help_echo_string))
9094 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
9095 }
9096
9097 #endif /* HAVE_WINDOW_SYSTEM */
9098
9099
9100 \f
9101 /************************************************************************
9102 Horizontal scrolling
9103 ************************************************************************/
9104
9105 static int hscroll_window_tree P_ ((Lisp_Object));
9106 static int hscroll_windows P_ ((Lisp_Object));
9107
9108 /* For all leaf windows in the window tree rooted at WINDOW, set their
9109 hscroll value so that PT is (i) visible in the window, and (ii) so
9110 that it is not within a certain margin at the window's left and
9111 right border. Value is non-zero if any window's hscroll has been
9112 changed. */
9113
9114 static int
9115 hscroll_window_tree (window)
9116 Lisp_Object window;
9117 {
9118 int hscrolled_p = 0;
9119 int hscroll_relative_p = FLOATP (Vhscroll_step);
9120 int hscroll_step_abs = 0;
9121 double hscroll_step_rel = 0;
9122
9123 if (hscroll_relative_p)
9124 {
9125 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
9126 if (hscroll_step_rel < 0)
9127 {
9128 hscroll_relative_p = 0;
9129 hscroll_step_abs = 0;
9130 }
9131 }
9132 else if (INTEGERP (Vhscroll_step))
9133 {
9134 hscroll_step_abs = XINT (Vhscroll_step);
9135 if (hscroll_step_abs < 0)
9136 hscroll_step_abs = 0;
9137 }
9138 else
9139 hscroll_step_abs = 0;
9140
9141 while (WINDOWP (window))
9142 {
9143 struct window *w = XWINDOW (window);
9144
9145 if (WINDOWP (w->hchild))
9146 hscrolled_p |= hscroll_window_tree (w->hchild);
9147 else if (WINDOWP (w->vchild))
9148 hscrolled_p |= hscroll_window_tree (w->vchild);
9149 else if (w->cursor.vpos >= 0)
9150 {
9151 int h_margin;
9152 int text_area_width;
9153 struct glyph_row *current_cursor_row
9154 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
9155 struct glyph_row *desired_cursor_row
9156 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
9157 struct glyph_row *cursor_row
9158 = (desired_cursor_row->enabled_p
9159 ? desired_cursor_row
9160 : current_cursor_row);
9161
9162 text_area_width = window_box_width (w, TEXT_AREA);
9163
9164 /* Scroll when cursor is inside this scroll margin. */
9165 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
9166
9167 if ((XFASTINT (w->hscroll)
9168 && w->cursor.x <= h_margin)
9169 || (cursor_row->enabled_p
9170 && cursor_row->truncated_on_right_p
9171 && (w->cursor.x >= text_area_width - h_margin)))
9172 {
9173 struct it it;
9174 int hscroll;
9175 struct buffer *saved_current_buffer;
9176 int pt;
9177 int wanted_x;
9178
9179 /* Find point in a display of infinite width. */
9180 saved_current_buffer = current_buffer;
9181 current_buffer = XBUFFER (w->buffer);
9182
9183 if (w == XWINDOW (selected_window))
9184 pt = BUF_PT (current_buffer);
9185 else
9186 {
9187 pt = marker_position (w->pointm);
9188 pt = max (BEGV, pt);
9189 pt = min (ZV, pt);
9190 }
9191
9192 /* Move iterator to pt starting at cursor_row->start in
9193 a line with infinite width. */
9194 init_to_row_start (&it, w, cursor_row);
9195 it.last_visible_x = INFINITY;
9196 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
9197 current_buffer = saved_current_buffer;
9198
9199 /* Position cursor in window. */
9200 if (!hscroll_relative_p && hscroll_step_abs == 0)
9201 hscroll = max (0, (it.current_x
9202 - (ITERATOR_AT_END_OF_LINE_P (&it)
9203 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
9204 : (text_area_width / 2))))
9205 / FRAME_COLUMN_WIDTH (it.f);
9206 else if (w->cursor.x >= text_area_width - h_margin)
9207 {
9208 if (hscroll_relative_p)
9209 wanted_x = text_area_width * (1 - hscroll_step_rel)
9210 - h_margin;
9211 else
9212 wanted_x = text_area_width
9213 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
9214 - h_margin;
9215 hscroll
9216 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
9217 }
9218 else
9219 {
9220 if (hscroll_relative_p)
9221 wanted_x = text_area_width * hscroll_step_rel
9222 + h_margin;
9223 else
9224 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
9225 + h_margin;
9226 hscroll
9227 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
9228 }
9229 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
9230
9231 /* Don't call Fset_window_hscroll if value hasn't
9232 changed because it will prevent redisplay
9233 optimizations. */
9234 if (XFASTINT (w->hscroll) != hscroll)
9235 {
9236 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
9237 w->hscroll = make_number (hscroll);
9238 hscrolled_p = 1;
9239 }
9240 }
9241 }
9242
9243 window = w->next;
9244 }
9245
9246 /* Value is non-zero if hscroll of any leaf window has been changed. */
9247 return hscrolled_p;
9248 }
9249
9250
9251 /* Set hscroll so that cursor is visible and not inside horizontal
9252 scroll margins for all windows in the tree rooted at WINDOW. See
9253 also hscroll_window_tree above. Value is non-zero if any window's
9254 hscroll has been changed. If it has, desired matrices on the frame
9255 of WINDOW are cleared. */
9256
9257 static int
9258 hscroll_windows (window)
9259 Lisp_Object window;
9260 {
9261 int hscrolled_p;
9262
9263 if (automatic_hscrolling_p)
9264 {
9265 hscrolled_p = hscroll_window_tree (window);
9266 if (hscrolled_p)
9267 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
9268 }
9269 else
9270 hscrolled_p = 0;
9271 return hscrolled_p;
9272 }
9273
9274
9275 \f
9276 /************************************************************************
9277 Redisplay
9278 ************************************************************************/
9279
9280 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
9281 to a non-zero value. This is sometimes handy to have in a debugger
9282 session. */
9283
9284 #if GLYPH_DEBUG
9285
9286 /* First and last unchanged row for try_window_id. */
9287
9288 int debug_first_unchanged_at_end_vpos;
9289 int debug_last_unchanged_at_beg_vpos;
9290
9291 /* Delta vpos and y. */
9292
9293 int debug_dvpos, debug_dy;
9294
9295 /* Delta in characters and bytes for try_window_id. */
9296
9297 int debug_delta, debug_delta_bytes;
9298
9299 /* Values of window_end_pos and window_end_vpos at the end of
9300 try_window_id. */
9301
9302 EMACS_INT debug_end_pos, debug_end_vpos;
9303
9304 /* Append a string to W->desired_matrix->method. FMT is a printf
9305 format string. A1...A9 are a supplement for a variable-length
9306 argument list. If trace_redisplay_p is non-zero also printf the
9307 resulting string to stderr. */
9308
9309 static void
9310 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
9311 struct window *w;
9312 char *fmt;
9313 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
9314 {
9315 char buffer[512];
9316 char *method = w->desired_matrix->method;
9317 int len = strlen (method);
9318 int size = sizeof w->desired_matrix->method;
9319 int remaining = size - len - 1;
9320
9321 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
9322 if (len && remaining)
9323 {
9324 method[len] = '|';
9325 --remaining, ++len;
9326 }
9327
9328 strncpy (method + len, buffer, remaining);
9329
9330 if (trace_redisplay_p)
9331 fprintf (stderr, "%p (%s): %s\n",
9332 w,
9333 ((BUFFERP (w->buffer)
9334 && STRINGP (XBUFFER (w->buffer)->name))
9335 ? (char *) SDATA (XBUFFER (w->buffer)->name)
9336 : "no buffer"),
9337 buffer);
9338 }
9339
9340 #endif /* GLYPH_DEBUG */
9341
9342
9343 /* Value is non-zero if all changes in window W, which displays
9344 current_buffer, are in the text between START and END. START is a
9345 buffer position, END is given as a distance from Z. Used in
9346 redisplay_internal for display optimization. */
9347
9348 static INLINE int
9349 text_outside_line_unchanged_p (w, start, end)
9350 struct window *w;
9351 int start, end;
9352 {
9353 int unchanged_p = 1;
9354
9355 /* If text or overlays have changed, see where. */
9356 if (XFASTINT (w->last_modified) < MODIFF
9357 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
9358 {
9359 /* Gap in the line? */
9360 if (GPT < start || Z - GPT < end)
9361 unchanged_p = 0;
9362
9363 /* Changes start in front of the line, or end after it? */
9364 if (unchanged_p
9365 && (BEG_UNCHANGED < start - 1
9366 || END_UNCHANGED < end))
9367 unchanged_p = 0;
9368
9369 /* If selective display, can't optimize if changes start at the
9370 beginning of the line. */
9371 if (unchanged_p
9372 && INTEGERP (current_buffer->selective_display)
9373 && XINT (current_buffer->selective_display) > 0
9374 && (BEG_UNCHANGED < start || GPT <= start))
9375 unchanged_p = 0;
9376
9377 /* If there are overlays at the start or end of the line, these
9378 may have overlay strings with newlines in them. A change at
9379 START, for instance, may actually concern the display of such
9380 overlay strings as well, and they are displayed on different
9381 lines. So, quickly rule out this case. (For the future, it
9382 might be desirable to implement something more telling than
9383 just BEG/END_UNCHANGED.) */
9384 if (unchanged_p)
9385 {
9386 if (BEG + BEG_UNCHANGED == start
9387 && overlay_touches_p (start))
9388 unchanged_p = 0;
9389 if (END_UNCHANGED == end
9390 && overlay_touches_p (Z - end))
9391 unchanged_p = 0;
9392 }
9393 }
9394
9395 return unchanged_p;
9396 }
9397
9398
9399 /* Do a frame update, taking possible shortcuts into account. This is
9400 the main external entry point for redisplay.
9401
9402 If the last redisplay displayed an echo area message and that message
9403 is no longer requested, we clear the echo area or bring back the
9404 mini-buffer if that is in use. */
9405
9406 void
9407 redisplay ()
9408 {
9409 redisplay_internal (0);
9410 }
9411
9412
9413 static Lisp_Object
9414 overlay_arrow_string_or_property (var, pbitmap)
9415 Lisp_Object var;
9416 int *pbitmap;
9417 {
9418 Lisp_Object pstr = Fget (var, Qoverlay_arrow_string);
9419 Lisp_Object bitmap;
9420
9421 if (pbitmap)
9422 {
9423 *pbitmap = 0;
9424 if (bitmap = Fget (var, Qoverlay_arrow_bitmap), INTEGERP (bitmap))
9425 *pbitmap = XINT (bitmap);
9426 }
9427
9428 if (!NILP (pstr))
9429 return pstr;
9430 return Voverlay_arrow_string;
9431 }
9432
9433 /* Return 1 if there are any overlay-arrows in current_buffer. */
9434 static int
9435 overlay_arrow_in_current_buffer_p ()
9436 {
9437 Lisp_Object vlist;
9438
9439 for (vlist = Voverlay_arrow_variable_list;
9440 CONSP (vlist);
9441 vlist = XCDR (vlist))
9442 {
9443 Lisp_Object var = XCAR (vlist);
9444 Lisp_Object val;
9445
9446 if (!SYMBOLP (var))
9447 continue;
9448 val = find_symbol_value (var);
9449 if (MARKERP (val)
9450 && current_buffer == XMARKER (val)->buffer)
9451 return 1;
9452 }
9453 return 0;
9454 }
9455
9456
9457 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
9458 has changed. */
9459
9460 static int
9461 overlay_arrows_changed_p ()
9462 {
9463 Lisp_Object vlist;
9464
9465 for (vlist = Voverlay_arrow_variable_list;
9466 CONSP (vlist);
9467 vlist = XCDR (vlist))
9468 {
9469 Lisp_Object var = XCAR (vlist);
9470 Lisp_Object val, pstr;
9471
9472 if (!SYMBOLP (var))
9473 continue;
9474 val = find_symbol_value (var);
9475 if (!MARKERP (val))
9476 continue;
9477 if (! EQ (COERCE_MARKER (val),
9478 Fget (var, Qlast_arrow_position))
9479 || ! (pstr = overlay_arrow_string_or_property (var, 0),
9480 EQ (pstr, Fget (var, Qlast_arrow_string))))
9481 return 1;
9482 }
9483 return 0;
9484 }
9485
9486 /* Mark overlay arrows to be updated on next redisplay. */
9487
9488 static void
9489 update_overlay_arrows (up_to_date)
9490 int up_to_date;
9491 {
9492 Lisp_Object vlist;
9493
9494 for (vlist = Voverlay_arrow_variable_list;
9495 CONSP (vlist);
9496 vlist = XCDR (vlist))
9497 {
9498 Lisp_Object var = XCAR (vlist);
9499
9500 if (!SYMBOLP (var))
9501 continue;
9502
9503 if (up_to_date > 0)
9504 {
9505 Lisp_Object val = find_symbol_value (var);
9506 Fput (var, Qlast_arrow_position,
9507 COERCE_MARKER (val));
9508 Fput (var, Qlast_arrow_string,
9509 overlay_arrow_string_or_property (var, 0));
9510 }
9511 else if (up_to_date < 0
9512 || !NILP (Fget (var, Qlast_arrow_position)))
9513 {
9514 Fput (var, Qlast_arrow_position, Qt);
9515 Fput (var, Qlast_arrow_string, Qt);
9516 }
9517 }
9518 }
9519
9520
9521 /* Return overlay arrow string at row, or nil. */
9522
9523 static Lisp_Object
9524 overlay_arrow_at_row (f, row, pbitmap)
9525 struct frame *f;
9526 struct glyph_row *row;
9527 int *pbitmap;
9528 {
9529 Lisp_Object vlist;
9530
9531 for (vlist = Voverlay_arrow_variable_list;
9532 CONSP (vlist);
9533 vlist = XCDR (vlist))
9534 {
9535 Lisp_Object var = XCAR (vlist);
9536 Lisp_Object val;
9537
9538 if (!SYMBOLP (var))
9539 continue;
9540
9541 val = find_symbol_value (var);
9542
9543 if (MARKERP (val)
9544 && current_buffer == XMARKER (val)->buffer
9545 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
9546 {
9547 val = overlay_arrow_string_or_property (var, pbitmap);
9548 if (FRAME_WINDOW_P (f))
9549 return Qt;
9550 else if (STRINGP (val))
9551 return val;
9552 break;
9553 }
9554 }
9555
9556 *pbitmap = 0;
9557 return Qnil;
9558 }
9559
9560 /* Return 1 if point moved out of or into a composition. Otherwise
9561 return 0. PREV_BUF and PREV_PT are the last point buffer and
9562 position. BUF and PT are the current point buffer and position. */
9563
9564 int
9565 check_point_in_composition (prev_buf, prev_pt, buf, pt)
9566 struct buffer *prev_buf, *buf;
9567 int prev_pt, pt;
9568 {
9569 int start, end;
9570 Lisp_Object prop;
9571 Lisp_Object buffer;
9572
9573 XSETBUFFER (buffer, buf);
9574 /* Check a composition at the last point if point moved within the
9575 same buffer. */
9576 if (prev_buf == buf)
9577 {
9578 if (prev_pt == pt)
9579 /* Point didn't move. */
9580 return 0;
9581
9582 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
9583 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
9584 && COMPOSITION_VALID_P (start, end, prop)
9585 && start < prev_pt && end > prev_pt)
9586 /* The last point was within the composition. Return 1 iff
9587 point moved out of the composition. */
9588 return (pt <= start || pt >= end);
9589 }
9590
9591 /* Check a composition at the current point. */
9592 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
9593 && find_composition (pt, -1, &start, &end, &prop, buffer)
9594 && COMPOSITION_VALID_P (start, end, prop)
9595 && start < pt && end > pt);
9596 }
9597
9598
9599 /* Reconsider the setting of B->clip_changed which is displayed
9600 in window W. */
9601
9602 static INLINE void
9603 reconsider_clip_changes (w, b)
9604 struct window *w;
9605 struct buffer *b;
9606 {
9607 if (b->clip_changed
9608 && !NILP (w->window_end_valid)
9609 && w->current_matrix->buffer == b
9610 && w->current_matrix->zv == BUF_ZV (b)
9611 && w->current_matrix->begv == BUF_BEGV (b))
9612 b->clip_changed = 0;
9613
9614 /* If display wasn't paused, and W is not a tool bar window, see if
9615 point has been moved into or out of a composition. In that case,
9616 we set b->clip_changed to 1 to force updating the screen. If
9617 b->clip_changed has already been set to 1, we can skip this
9618 check. */
9619 if (!b->clip_changed
9620 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
9621 {
9622 int pt;
9623
9624 if (w == XWINDOW (selected_window))
9625 pt = BUF_PT (current_buffer);
9626 else
9627 pt = marker_position (w->pointm);
9628
9629 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
9630 || pt != XINT (w->last_point))
9631 && check_point_in_composition (w->current_matrix->buffer,
9632 XINT (w->last_point),
9633 XBUFFER (w->buffer), pt))
9634 b->clip_changed = 1;
9635 }
9636 }
9637 \f
9638
9639 /* Select FRAME to forward the values of frame-local variables into C
9640 variables so that the redisplay routines can access those values
9641 directly. */
9642
9643 static void
9644 select_frame_for_redisplay (frame)
9645 Lisp_Object frame;
9646 {
9647 Lisp_Object tail, sym, val;
9648 Lisp_Object old = selected_frame;
9649
9650 selected_frame = frame;
9651
9652 for (tail = XFRAME (frame)->param_alist; CONSP (tail); tail = XCDR (tail))
9653 if (CONSP (XCAR (tail))
9654 && (sym = XCAR (XCAR (tail)),
9655 SYMBOLP (sym))
9656 && (sym = indirect_variable (sym),
9657 val = SYMBOL_VALUE (sym),
9658 (BUFFER_LOCAL_VALUEP (val)
9659 || SOME_BUFFER_LOCAL_VALUEP (val)))
9660 && XBUFFER_LOCAL_VALUE (val)->check_frame)
9661 Fsymbol_value (sym);
9662
9663 for (tail = XFRAME (old)->param_alist; CONSP (tail); tail = XCDR (tail))
9664 if (CONSP (XCAR (tail))
9665 && (sym = XCAR (XCAR (tail)),
9666 SYMBOLP (sym))
9667 && (sym = indirect_variable (sym),
9668 val = SYMBOL_VALUE (sym),
9669 (BUFFER_LOCAL_VALUEP (val)
9670 || SOME_BUFFER_LOCAL_VALUEP (val)))
9671 && XBUFFER_LOCAL_VALUE (val)->check_frame)
9672 Fsymbol_value (sym);
9673 }
9674
9675
9676 #define STOP_POLLING \
9677 do { if (! polling_stopped_here) stop_polling (); \
9678 polling_stopped_here = 1; } while (0)
9679
9680 #define RESUME_POLLING \
9681 do { if (polling_stopped_here) start_polling (); \
9682 polling_stopped_here = 0; } while (0)
9683
9684
9685 /* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
9686 response to any user action; therefore, we should preserve the echo
9687 area. (Actually, our caller does that job.) Perhaps in the future
9688 avoid recentering windows if it is not necessary; currently that
9689 causes some problems. */
9690
9691 static void
9692 redisplay_internal (preserve_echo_area)
9693 int preserve_echo_area;
9694 {
9695 struct window *w = XWINDOW (selected_window);
9696 struct frame *f = XFRAME (w->frame);
9697 int pause;
9698 int must_finish = 0;
9699 struct text_pos tlbufpos, tlendpos;
9700 int number_of_visible_frames;
9701 int count;
9702 struct frame *sf = SELECTED_FRAME ();
9703 int polling_stopped_here = 0;
9704
9705 /* Non-zero means redisplay has to consider all windows on all
9706 frames. Zero means, only selected_window is considered. */
9707 int consider_all_windows_p;
9708
9709 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
9710
9711 /* No redisplay if running in batch mode or frame is not yet fully
9712 initialized, or redisplay is explicitly turned off by setting
9713 Vinhibit_redisplay. */
9714 if (noninteractive
9715 || !NILP (Vinhibit_redisplay)
9716 || !f->glyphs_initialized_p)
9717 return;
9718
9719 /* The flag redisplay_performed_directly_p is set by
9720 direct_output_for_insert when it already did the whole screen
9721 update necessary. */
9722 if (redisplay_performed_directly_p)
9723 {
9724 redisplay_performed_directly_p = 0;
9725 if (!hscroll_windows (selected_window))
9726 return;
9727 }
9728
9729 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
9730 if (popup_activated ())
9731 return;
9732 #endif
9733
9734 /* I don't think this happens but let's be paranoid. */
9735 if (redisplaying_p)
9736 return;
9737
9738 /* Record a function that resets redisplaying_p to its old value
9739 when we leave this function. */
9740 count = SPECPDL_INDEX ();
9741 record_unwind_protect (unwind_redisplay,
9742 Fcons (make_number (redisplaying_p), selected_frame));
9743 ++redisplaying_p;
9744 specbind (Qinhibit_free_realized_faces, Qnil);
9745
9746 retry:
9747 pause = 0;
9748 reconsider_clip_changes (w, current_buffer);
9749
9750 /* If new fonts have been loaded that make a glyph matrix adjustment
9751 necessary, do it. */
9752 if (fonts_changed_p)
9753 {
9754 adjust_glyphs (NULL);
9755 ++windows_or_buffers_changed;
9756 fonts_changed_p = 0;
9757 }
9758
9759 /* If face_change_count is non-zero, init_iterator will free all
9760 realized faces, which includes the faces referenced from current
9761 matrices. So, we can't reuse current matrices in this case. */
9762 if (face_change_count)
9763 ++windows_or_buffers_changed;
9764
9765 if (! FRAME_WINDOW_P (sf)
9766 && previous_terminal_frame != sf)
9767 {
9768 /* Since frames on an ASCII terminal share the same display
9769 area, displaying a different frame means redisplay the whole
9770 thing. */
9771 windows_or_buffers_changed++;
9772 SET_FRAME_GARBAGED (sf);
9773 XSETFRAME (Vterminal_frame, sf);
9774 }
9775 previous_terminal_frame = sf;
9776
9777 /* Set the visible flags for all frames. Do this before checking
9778 for resized or garbaged frames; they want to know if their frames
9779 are visible. See the comment in frame.h for
9780 FRAME_SAMPLE_VISIBILITY. */
9781 {
9782 Lisp_Object tail, frame;
9783
9784 number_of_visible_frames = 0;
9785
9786 FOR_EACH_FRAME (tail, frame)
9787 {
9788 struct frame *f = XFRAME (frame);
9789
9790 FRAME_SAMPLE_VISIBILITY (f);
9791 if (FRAME_VISIBLE_P (f))
9792 ++number_of_visible_frames;
9793 clear_desired_matrices (f);
9794 }
9795 }
9796
9797 /* Notice any pending interrupt request to change frame size. */
9798 do_pending_window_change (1);
9799
9800 /* Clear frames marked as garbaged. */
9801 if (frame_garbaged)
9802 clear_garbaged_frames ();
9803
9804 /* Build menubar and tool-bar items. */
9805 prepare_menu_bars ();
9806
9807 if (windows_or_buffers_changed)
9808 update_mode_lines++;
9809
9810 /* Detect case that we need to write or remove a star in the mode line. */
9811 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
9812 {
9813 w->update_mode_line = Qt;
9814 if (buffer_shared > 1)
9815 update_mode_lines++;
9816 }
9817
9818 /* If %c is in the mode line, update it if needed. */
9819 if (!NILP (w->column_number_displayed)
9820 /* This alternative quickly identifies a common case
9821 where no change is needed. */
9822 && !(PT == XFASTINT (w->last_point)
9823 && XFASTINT (w->last_modified) >= MODIFF
9824 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
9825 && (XFASTINT (w->column_number_displayed)
9826 != (int) current_column ())) /* iftc */
9827 w->update_mode_line = Qt;
9828
9829 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
9830
9831 /* The variable buffer_shared is set in redisplay_window and
9832 indicates that we redisplay a buffer in different windows. See
9833 there. */
9834 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
9835 || cursor_type_changed);
9836
9837 /* If specs for an arrow have changed, do thorough redisplay
9838 to ensure we remove any arrow that should no longer exist. */
9839 if (overlay_arrows_changed_p ())
9840 consider_all_windows_p = windows_or_buffers_changed = 1;
9841
9842 /* Normally the message* functions will have already displayed and
9843 updated the echo area, but the frame may have been trashed, or
9844 the update may have been preempted, so display the echo area
9845 again here. Checking message_cleared_p captures the case that
9846 the echo area should be cleared. */
9847 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
9848 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
9849 || (message_cleared_p
9850 && minibuf_level == 0
9851 /* If the mini-window is currently selected, this means the
9852 echo-area doesn't show through. */
9853 && !MINI_WINDOW_P (XWINDOW (selected_window))))
9854 {
9855 int window_height_changed_p = echo_area_display (0);
9856 must_finish = 1;
9857
9858 /* If we don't display the current message, don't clear the
9859 message_cleared_p flag, because, if we did, we wouldn't clear
9860 the echo area in the next redisplay which doesn't preserve
9861 the echo area. */
9862 if (!display_last_displayed_message_p)
9863 message_cleared_p = 0;
9864
9865 if (fonts_changed_p)
9866 goto retry;
9867 else if (window_height_changed_p)
9868 {
9869 consider_all_windows_p = 1;
9870 ++update_mode_lines;
9871 ++windows_or_buffers_changed;
9872
9873 /* If window configuration was changed, frames may have been
9874 marked garbaged. Clear them or we will experience
9875 surprises wrt scrolling. */
9876 if (frame_garbaged)
9877 clear_garbaged_frames ();
9878 }
9879 }
9880 else if (EQ (selected_window, minibuf_window)
9881 && (current_buffer->clip_changed
9882 || XFASTINT (w->last_modified) < MODIFF
9883 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
9884 && resize_mini_window (w, 0))
9885 {
9886 /* Resized active mini-window to fit the size of what it is
9887 showing if its contents might have changed. */
9888 must_finish = 1;
9889 consider_all_windows_p = 1;
9890 ++windows_or_buffers_changed;
9891 ++update_mode_lines;
9892
9893 /* If window configuration was changed, frames may have been
9894 marked garbaged. Clear them or we will experience
9895 surprises wrt scrolling. */
9896 if (frame_garbaged)
9897 clear_garbaged_frames ();
9898 }
9899
9900
9901 /* If showing the region, and mark has changed, we must redisplay
9902 the whole window. The assignment to this_line_start_pos prevents
9903 the optimization directly below this if-statement. */
9904 if (((!NILP (Vtransient_mark_mode)
9905 && !NILP (XBUFFER (w->buffer)->mark_active))
9906 != !NILP (w->region_showing))
9907 || (!NILP (w->region_showing)
9908 && !EQ (w->region_showing,
9909 Fmarker_position (XBUFFER (w->buffer)->mark))))
9910 CHARPOS (this_line_start_pos) = 0;
9911
9912 /* Optimize the case that only the line containing the cursor in the
9913 selected window has changed. Variables starting with this_ are
9914 set in display_line and record information about the line
9915 containing the cursor. */
9916 tlbufpos = this_line_start_pos;
9917 tlendpos = this_line_end_pos;
9918 if (!consider_all_windows_p
9919 && CHARPOS (tlbufpos) > 0
9920 && NILP (w->update_mode_line)
9921 && !current_buffer->clip_changed
9922 && !current_buffer->prevent_redisplay_optimizations_p
9923 && FRAME_VISIBLE_P (XFRAME (w->frame))
9924 && !FRAME_OBSCURED_P (XFRAME (w->frame))
9925 /* Make sure recorded data applies to current buffer, etc. */
9926 && this_line_buffer == current_buffer
9927 && current_buffer == XBUFFER (w->buffer)
9928 && NILP (w->force_start)
9929 && NILP (w->optional_new_start)
9930 /* Point must be on the line that we have info recorded about. */
9931 && PT >= CHARPOS (tlbufpos)
9932 && PT <= Z - CHARPOS (tlendpos)
9933 /* All text outside that line, including its final newline,
9934 must be unchanged */
9935 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
9936 CHARPOS (tlendpos)))
9937 {
9938 if (CHARPOS (tlbufpos) > BEGV
9939 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
9940 && (CHARPOS (tlbufpos) == ZV
9941 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
9942 /* Former continuation line has disappeared by becoming empty */
9943 goto cancel;
9944 else if (XFASTINT (w->last_modified) < MODIFF
9945 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
9946 || MINI_WINDOW_P (w))
9947 {
9948 /* We have to handle the case of continuation around a
9949 wide-column character (See the comment in indent.c around
9950 line 885).
9951
9952 For instance, in the following case:
9953
9954 -------- Insert --------
9955 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
9956 J_I_ ==> J_I_ `^^' are cursors.
9957 ^^ ^^
9958 -------- --------
9959
9960 As we have to redraw the line above, we should goto cancel. */
9961
9962 struct it it;
9963 int line_height_before = this_line_pixel_height;
9964
9965 /* Note that start_display will handle the case that the
9966 line starting at tlbufpos is a continuation lines. */
9967 start_display (&it, w, tlbufpos);
9968
9969 /* Implementation note: It this still necessary? */
9970 if (it.current_x != this_line_start_x)
9971 goto cancel;
9972
9973 TRACE ((stderr, "trying display optimization 1\n"));
9974 w->cursor.vpos = -1;
9975 overlay_arrow_seen = 0;
9976 it.vpos = this_line_vpos;
9977 it.current_y = this_line_y;
9978 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
9979 display_line (&it);
9980
9981 /* If line contains point, is not continued,
9982 and ends at same distance from eob as before, we win */
9983 if (w->cursor.vpos >= 0
9984 /* Line is not continued, otherwise this_line_start_pos
9985 would have been set to 0 in display_line. */
9986 && CHARPOS (this_line_start_pos)
9987 /* Line ends as before. */
9988 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
9989 /* Line has same height as before. Otherwise other lines
9990 would have to be shifted up or down. */
9991 && this_line_pixel_height == line_height_before)
9992 {
9993 /* If this is not the window's last line, we must adjust
9994 the charstarts of the lines below. */
9995 if (it.current_y < it.last_visible_y)
9996 {
9997 struct glyph_row *row
9998 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
9999 int delta, delta_bytes;
10000
10001 if (Z - CHARPOS (tlendpos) == ZV)
10002 {
10003 /* This line ends at end of (accessible part of)
10004 buffer. There is no newline to count. */
10005 delta = (Z
10006 - CHARPOS (tlendpos)
10007 - MATRIX_ROW_START_CHARPOS (row));
10008 delta_bytes = (Z_BYTE
10009 - BYTEPOS (tlendpos)
10010 - MATRIX_ROW_START_BYTEPOS (row));
10011 }
10012 else
10013 {
10014 /* This line ends in a newline. Must take
10015 account of the newline and the rest of the
10016 text that follows. */
10017 delta = (Z
10018 - CHARPOS (tlendpos)
10019 - MATRIX_ROW_START_CHARPOS (row));
10020 delta_bytes = (Z_BYTE
10021 - BYTEPOS (tlendpos)
10022 - MATRIX_ROW_START_BYTEPOS (row));
10023 }
10024
10025 increment_matrix_positions (w->current_matrix,
10026 this_line_vpos + 1,
10027 w->current_matrix->nrows,
10028 delta, delta_bytes);
10029 }
10030
10031 /* If this row displays text now but previously didn't,
10032 or vice versa, w->window_end_vpos may have to be
10033 adjusted. */
10034 if ((it.glyph_row - 1)->displays_text_p)
10035 {
10036 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
10037 XSETINT (w->window_end_vpos, this_line_vpos);
10038 }
10039 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
10040 && this_line_vpos > 0)
10041 XSETINT (w->window_end_vpos, this_line_vpos - 1);
10042 w->window_end_valid = Qnil;
10043
10044 /* Update hint: No need to try to scroll in update_window. */
10045 w->desired_matrix->no_scrolling_p = 1;
10046
10047 #if GLYPH_DEBUG
10048 *w->desired_matrix->method = 0;
10049 debug_method_add (w, "optimization 1");
10050 #endif
10051 #ifdef HAVE_WINDOW_SYSTEM
10052 update_window_fringes (w, 0);
10053 #endif
10054 goto update;
10055 }
10056 else
10057 goto cancel;
10058 }
10059 else if (/* Cursor position hasn't changed. */
10060 PT == XFASTINT (w->last_point)
10061 /* Make sure the cursor was last displayed
10062 in this window. Otherwise we have to reposition it. */
10063 && 0 <= w->cursor.vpos
10064 && WINDOW_TOTAL_LINES (w) > w->cursor.vpos)
10065 {
10066 if (!must_finish)
10067 {
10068 do_pending_window_change (1);
10069
10070 /* We used to always goto end_of_redisplay here, but this
10071 isn't enough if we have a blinking cursor. */
10072 if (w->cursor_off_p == w->last_cursor_off_p)
10073 goto end_of_redisplay;
10074 }
10075 goto update;
10076 }
10077 /* If highlighting the region, or if the cursor is in the echo area,
10078 then we can't just move the cursor. */
10079 else if (! (!NILP (Vtransient_mark_mode)
10080 && !NILP (current_buffer->mark_active))
10081 && (EQ (selected_window, current_buffer->last_selected_window)
10082 || highlight_nonselected_windows)
10083 && NILP (w->region_showing)
10084 && NILP (Vshow_trailing_whitespace)
10085 && !cursor_in_echo_area)
10086 {
10087 struct it it;
10088 struct glyph_row *row;
10089
10090 /* Skip from tlbufpos to PT and see where it is. Note that
10091 PT may be in invisible text. If so, we will end at the
10092 next visible position. */
10093 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
10094 NULL, DEFAULT_FACE_ID);
10095 it.current_x = this_line_start_x;
10096 it.current_y = this_line_y;
10097 it.vpos = this_line_vpos;
10098
10099 /* The call to move_it_to stops in front of PT, but
10100 moves over before-strings. */
10101 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
10102
10103 if (it.vpos == this_line_vpos
10104 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
10105 row->enabled_p))
10106 {
10107 xassert (this_line_vpos == it.vpos);
10108 xassert (this_line_y == it.current_y);
10109 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
10110 #if GLYPH_DEBUG
10111 *w->desired_matrix->method = 0;
10112 debug_method_add (w, "optimization 3");
10113 #endif
10114 goto update;
10115 }
10116 else
10117 goto cancel;
10118 }
10119
10120 cancel:
10121 /* Text changed drastically or point moved off of line. */
10122 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
10123 }
10124
10125 CHARPOS (this_line_start_pos) = 0;
10126 consider_all_windows_p |= buffer_shared > 1;
10127 ++clear_face_cache_count;
10128
10129
10130 /* Build desired matrices, and update the display. If
10131 consider_all_windows_p is non-zero, do it for all windows on all
10132 frames. Otherwise do it for selected_window, only. */
10133
10134 if (consider_all_windows_p)
10135 {
10136 Lisp_Object tail, frame;
10137 int i, n = 0, size = 50;
10138 struct frame **updated
10139 = (struct frame **) alloca (size * sizeof *updated);
10140
10141 /* Clear the face cache eventually. */
10142 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
10143 {
10144 clear_face_cache (0);
10145 clear_face_cache_count = 0;
10146 }
10147
10148 /* Recompute # windows showing selected buffer. This will be
10149 incremented each time such a window is displayed. */
10150 buffer_shared = 0;
10151
10152 FOR_EACH_FRAME (tail, frame)
10153 {
10154 struct frame *f = XFRAME (frame);
10155
10156 if (FRAME_WINDOW_P (f) || f == sf)
10157 {
10158 if (! EQ (frame, selected_frame))
10159 /* Select the frame, for the sake of frame-local
10160 variables. */
10161 select_frame_for_redisplay (frame);
10162
10163 #ifdef HAVE_WINDOW_SYSTEM
10164 if (clear_face_cache_count % 50 == 0
10165 && FRAME_WINDOW_P (f))
10166 clear_image_cache (f, 0);
10167 #endif /* HAVE_WINDOW_SYSTEM */
10168
10169 /* Mark all the scroll bars to be removed; we'll redeem
10170 the ones we want when we redisplay their windows. */
10171 if (condemn_scroll_bars_hook)
10172 condemn_scroll_bars_hook (f);
10173
10174 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
10175 redisplay_windows (FRAME_ROOT_WINDOW (f));
10176
10177 /* Any scroll bars which redisplay_windows should have
10178 nuked should now go away. */
10179 if (judge_scroll_bars_hook)
10180 judge_scroll_bars_hook (f);
10181
10182 /* If fonts changed, display again. */
10183 /* ??? rms: I suspect it is a mistake to jump all the way
10184 back to retry here. It should just retry this frame. */
10185 if (fonts_changed_p)
10186 goto retry;
10187
10188 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
10189 {
10190 /* See if we have to hscroll. */
10191 if (hscroll_windows (f->root_window))
10192 goto retry;
10193
10194 /* Prevent various kinds of signals during display
10195 update. stdio is not robust about handling
10196 signals, which can cause an apparent I/O
10197 error. */
10198 if (interrupt_input)
10199 unrequest_sigio ();
10200 STOP_POLLING;
10201
10202 /* Update the display. */
10203 set_window_update_flags (XWINDOW (f->root_window), 1);
10204 pause |= update_frame (f, 0, 0);
10205 #if 0 /* Exiting the loop can leave the wrong value for buffer_shared. */
10206 if (pause)
10207 break;
10208 #endif
10209
10210 if (n == size)
10211 {
10212 int nbytes = size * sizeof *updated;
10213 struct frame **p = (struct frame **) alloca (2 * nbytes);
10214 bcopy (updated, p, nbytes);
10215 size *= 2;
10216 }
10217
10218 updated[n++] = f;
10219 }
10220 }
10221 }
10222
10223 if (!pause)
10224 {
10225 /* Do the mark_window_display_accurate after all windows have
10226 been redisplayed because this call resets flags in buffers
10227 which are needed for proper redisplay. */
10228 for (i = 0; i < n; ++i)
10229 {
10230 struct frame *f = updated[i];
10231 mark_window_display_accurate (f->root_window, 1);
10232 if (frame_up_to_date_hook)
10233 frame_up_to_date_hook (f);
10234 }
10235 }
10236 }
10237 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
10238 {
10239 Lisp_Object mini_window;
10240 struct frame *mini_frame;
10241
10242 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
10243 /* Use list_of_error, not Qerror, so that
10244 we catch only errors and don't run the debugger. */
10245 internal_condition_case_1 (redisplay_window_1, selected_window,
10246 list_of_error,
10247 redisplay_window_error);
10248
10249 /* Compare desired and current matrices, perform output. */
10250
10251 update:
10252 /* If fonts changed, display again. */
10253 if (fonts_changed_p)
10254 goto retry;
10255
10256 /* Prevent various kinds of signals during display update.
10257 stdio is not robust about handling signals,
10258 which can cause an apparent I/O error. */
10259 if (interrupt_input)
10260 unrequest_sigio ();
10261 STOP_POLLING;
10262
10263 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
10264 {
10265 if (hscroll_windows (selected_window))
10266 goto retry;
10267
10268 XWINDOW (selected_window)->must_be_updated_p = 1;
10269 pause = update_frame (sf, 0, 0);
10270 }
10271
10272 /* We may have called echo_area_display at the top of this
10273 function. If the echo area is on another frame, that may
10274 have put text on a frame other than the selected one, so the
10275 above call to update_frame would not have caught it. Catch
10276 it here. */
10277 mini_window = FRAME_MINIBUF_WINDOW (sf);
10278 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
10279
10280 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
10281 {
10282 XWINDOW (mini_window)->must_be_updated_p = 1;
10283 pause |= update_frame (mini_frame, 0, 0);
10284 if (!pause && hscroll_windows (mini_window))
10285 goto retry;
10286 }
10287 }
10288
10289 /* If display was paused because of pending input, make sure we do a
10290 thorough update the next time. */
10291 if (pause)
10292 {
10293 /* Prevent the optimization at the beginning of
10294 redisplay_internal that tries a single-line update of the
10295 line containing the cursor in the selected window. */
10296 CHARPOS (this_line_start_pos) = 0;
10297
10298 /* Let the overlay arrow be updated the next time. */
10299 update_overlay_arrows (0);
10300
10301 /* If we pause after scrolling, some rows in the current
10302 matrices of some windows are not valid. */
10303 if (!WINDOW_FULL_WIDTH_P (w)
10304 && !FRAME_WINDOW_P (XFRAME (w->frame)))
10305 update_mode_lines = 1;
10306 }
10307 else
10308 {
10309 if (!consider_all_windows_p)
10310 {
10311 /* This has already been done above if
10312 consider_all_windows_p is set. */
10313 mark_window_display_accurate_1 (w, 1);
10314
10315 /* Say overlay arrows are up to date. */
10316 update_overlay_arrows (1);
10317
10318 if (frame_up_to_date_hook != 0)
10319 frame_up_to_date_hook (sf);
10320 }
10321
10322 update_mode_lines = 0;
10323 windows_or_buffers_changed = 0;
10324 cursor_type_changed = 0;
10325 }
10326
10327 /* Start SIGIO interrupts coming again. Having them off during the
10328 code above makes it less likely one will discard output, but not
10329 impossible, since there might be stuff in the system buffer here.
10330 But it is much hairier to try to do anything about that. */
10331 if (interrupt_input)
10332 request_sigio ();
10333 RESUME_POLLING;
10334
10335 /* If a frame has become visible which was not before, redisplay
10336 again, so that we display it. Expose events for such a frame
10337 (which it gets when becoming visible) don't call the parts of
10338 redisplay constructing glyphs, so simply exposing a frame won't
10339 display anything in this case. So, we have to display these
10340 frames here explicitly. */
10341 if (!pause)
10342 {
10343 Lisp_Object tail, frame;
10344 int new_count = 0;
10345
10346 FOR_EACH_FRAME (tail, frame)
10347 {
10348 int this_is_visible = 0;
10349
10350 if (XFRAME (frame)->visible)
10351 this_is_visible = 1;
10352 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
10353 if (XFRAME (frame)->visible)
10354 this_is_visible = 1;
10355
10356 if (this_is_visible)
10357 new_count++;
10358 }
10359
10360 if (new_count != number_of_visible_frames)
10361 windows_or_buffers_changed++;
10362 }
10363
10364 /* Change frame size now if a change is pending. */
10365 do_pending_window_change (1);
10366
10367 /* If we just did a pending size change, or have additional
10368 visible frames, redisplay again. */
10369 if (windows_or_buffers_changed && !pause)
10370 goto retry;
10371
10372 end_of_redisplay:
10373 unbind_to (count, Qnil);
10374 RESUME_POLLING;
10375 }
10376
10377
10378 /* Redisplay, but leave alone any recent echo area message unless
10379 another message has been requested in its place.
10380
10381 This is useful in situations where you need to redisplay but no
10382 user action has occurred, making it inappropriate for the message
10383 area to be cleared. See tracking_off and
10384 wait_reading_process_output for examples of these situations.
10385
10386 FROM_WHERE is an integer saying from where this function was
10387 called. This is useful for debugging. */
10388
10389 void
10390 redisplay_preserve_echo_area (from_where)
10391 int from_where;
10392 {
10393 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
10394
10395 if (!NILP (echo_area_buffer[1]))
10396 {
10397 /* We have a previously displayed message, but no current
10398 message. Redisplay the previous message. */
10399 display_last_displayed_message_p = 1;
10400 redisplay_internal (1);
10401 display_last_displayed_message_p = 0;
10402 }
10403 else
10404 redisplay_internal (1);
10405 }
10406
10407
10408 /* Function registered with record_unwind_protect in
10409 redisplay_internal. Reset redisplaying_p to the value it had
10410 before redisplay_internal was called, and clear
10411 prevent_freeing_realized_faces_p. It also selects the previously
10412 selected frame. */
10413
10414 static Lisp_Object
10415 unwind_redisplay (val)
10416 Lisp_Object val;
10417 {
10418 Lisp_Object old_redisplaying_p, old_frame;
10419
10420 old_redisplaying_p = XCAR (val);
10421 redisplaying_p = XFASTINT (old_redisplaying_p);
10422 old_frame = XCDR (val);
10423 if (! EQ (old_frame, selected_frame))
10424 select_frame_for_redisplay (old_frame);
10425 return Qnil;
10426 }
10427
10428
10429 /* Mark the display of window W as accurate or inaccurate. If
10430 ACCURATE_P is non-zero mark display of W as accurate. If
10431 ACCURATE_P is zero, arrange for W to be redisplayed the next time
10432 redisplay_internal is called. */
10433
10434 static void
10435 mark_window_display_accurate_1 (w, accurate_p)
10436 struct window *w;
10437 int accurate_p;
10438 {
10439 if (BUFFERP (w->buffer))
10440 {
10441 struct buffer *b = XBUFFER (w->buffer);
10442
10443 w->last_modified
10444 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
10445 w->last_overlay_modified
10446 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
10447 w->last_had_star
10448 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
10449
10450 if (accurate_p)
10451 {
10452 b->clip_changed = 0;
10453 b->prevent_redisplay_optimizations_p = 0;
10454
10455 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
10456 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
10457 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
10458 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
10459
10460 w->current_matrix->buffer = b;
10461 w->current_matrix->begv = BUF_BEGV (b);
10462 w->current_matrix->zv = BUF_ZV (b);
10463
10464 w->last_cursor = w->cursor;
10465 w->last_cursor_off_p = w->cursor_off_p;
10466
10467 if (w == XWINDOW (selected_window))
10468 w->last_point = make_number (BUF_PT (b));
10469 else
10470 w->last_point = make_number (XMARKER (w->pointm)->charpos);
10471 }
10472 }
10473
10474 if (accurate_p)
10475 {
10476 w->window_end_valid = w->buffer;
10477 #if 0 /* This is incorrect with variable-height lines. */
10478 xassert (XINT (w->window_end_vpos)
10479 < (WINDOW_TOTAL_LINES (w)
10480 - (WINDOW_WANTS_MODELINE_P (w) ? 1 : 0)));
10481 #endif
10482 w->update_mode_line = Qnil;
10483 }
10484 }
10485
10486
10487 /* Mark the display of windows in the window tree rooted at WINDOW as
10488 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
10489 windows as accurate. If ACCURATE_P is zero, arrange for windows to
10490 be redisplayed the next time redisplay_internal is called. */
10491
10492 void
10493 mark_window_display_accurate (window, accurate_p)
10494 Lisp_Object window;
10495 int accurate_p;
10496 {
10497 struct window *w;
10498
10499 for (; !NILP (window); window = w->next)
10500 {
10501 w = XWINDOW (window);
10502 mark_window_display_accurate_1 (w, accurate_p);
10503
10504 if (!NILP (w->vchild))
10505 mark_window_display_accurate (w->vchild, accurate_p);
10506 if (!NILP (w->hchild))
10507 mark_window_display_accurate (w->hchild, accurate_p);
10508 }
10509
10510 if (accurate_p)
10511 {
10512 update_overlay_arrows (1);
10513 }
10514 else
10515 {
10516 /* Force a thorough redisplay the next time by setting
10517 last_arrow_position and last_arrow_string to t, which is
10518 unequal to any useful value of Voverlay_arrow_... */
10519 update_overlay_arrows (-1);
10520 }
10521 }
10522
10523
10524 /* Return value in display table DP (Lisp_Char_Table *) for character
10525 C. Since a display table doesn't have any parent, we don't have to
10526 follow parent. Do not call this function directly but use the
10527 macro DISP_CHAR_VECTOR. */
10528
10529 Lisp_Object
10530 disp_char_vector (dp, c)
10531 struct Lisp_Char_Table *dp;
10532 int c;
10533 {
10534 int code[4], i;
10535 Lisp_Object val;
10536
10537 if (SINGLE_BYTE_CHAR_P (c))
10538 return (dp->contents[c]);
10539
10540 SPLIT_CHAR (c, code[0], code[1], code[2]);
10541 if (code[1] < 32)
10542 code[1] = -1;
10543 else if (code[2] < 32)
10544 code[2] = -1;
10545
10546 /* Here, the possible range of code[0] (== charset ID) is
10547 128..max_charset. Since the top level char table contains data
10548 for multibyte characters after 256th element, we must increment
10549 code[0] by 128 to get a correct index. */
10550 code[0] += 128;
10551 code[3] = -1; /* anchor */
10552
10553 for (i = 0; code[i] >= 0; i++, dp = XCHAR_TABLE (val))
10554 {
10555 val = dp->contents[code[i]];
10556 if (!SUB_CHAR_TABLE_P (val))
10557 return (NILP (val) ? dp->defalt : val);
10558 }
10559
10560 /* Here, val is a sub char table. We return the default value of
10561 it. */
10562 return (dp->defalt);
10563 }
10564
10565
10566 \f
10567 /***********************************************************************
10568 Window Redisplay
10569 ***********************************************************************/
10570
10571 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
10572
10573 static void
10574 redisplay_windows (window)
10575 Lisp_Object window;
10576 {
10577 while (!NILP (window))
10578 {
10579 struct window *w = XWINDOW (window);
10580
10581 if (!NILP (w->hchild))
10582 redisplay_windows (w->hchild);
10583 else if (!NILP (w->vchild))
10584 redisplay_windows (w->vchild);
10585 else
10586 {
10587 displayed_buffer = XBUFFER (w->buffer);
10588 /* Use list_of_error, not Qerror, so that
10589 we catch only errors and don't run the debugger. */
10590 internal_condition_case_1 (redisplay_window_0, window,
10591 list_of_error,
10592 redisplay_window_error);
10593 }
10594
10595 window = w->next;
10596 }
10597 }
10598
10599 static Lisp_Object
10600 redisplay_window_error ()
10601 {
10602 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
10603 return Qnil;
10604 }
10605
10606 static Lisp_Object
10607 redisplay_window_0 (window)
10608 Lisp_Object window;
10609 {
10610 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
10611 redisplay_window (window, 0);
10612 return Qnil;
10613 }
10614
10615 static Lisp_Object
10616 redisplay_window_1 (window)
10617 Lisp_Object window;
10618 {
10619 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
10620 redisplay_window (window, 1);
10621 return Qnil;
10622 }
10623 \f
10624
10625 /* Increment GLYPH until it reaches END or CONDITION fails while
10626 adding (GLYPH)->pixel_width to X. */
10627
10628 #define SKIP_GLYPHS(glyph, end, x, condition) \
10629 do \
10630 { \
10631 (x) += (glyph)->pixel_width; \
10632 ++(glyph); \
10633 } \
10634 while ((glyph) < (end) && (condition))
10635
10636
10637 /* Set cursor position of W. PT is assumed to be displayed in ROW.
10638 DELTA is the number of bytes by which positions recorded in ROW
10639 differ from current buffer positions. */
10640
10641 void
10642 set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
10643 struct window *w;
10644 struct glyph_row *row;
10645 struct glyph_matrix *matrix;
10646 int delta, delta_bytes, dy, dvpos;
10647 {
10648 struct glyph *glyph = row->glyphs[TEXT_AREA];
10649 struct glyph *end = glyph + row->used[TEXT_AREA];
10650 /* The first glyph that starts a sequence of glyphs from string. */
10651 struct glyph *string_start;
10652 /* The X coordinate of string_start. */
10653 int string_start_x;
10654 /* The last known character position. */
10655 int last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
10656 /* The last known character position before string_start. */
10657 int string_before_pos;
10658 int x = row->x;
10659 int pt_old = PT - delta;
10660
10661 /* Skip over glyphs not having an object at the start of the row.
10662 These are special glyphs like truncation marks on terminal
10663 frames. */
10664 if (row->displays_text_p)
10665 while (glyph < end
10666 && INTEGERP (glyph->object)
10667 && glyph->charpos < 0)
10668 {
10669 x += glyph->pixel_width;
10670 ++glyph;
10671 }
10672
10673 string_start = NULL;
10674 while (glyph < end
10675 && !INTEGERP (glyph->object)
10676 && (!BUFFERP (glyph->object)
10677 || (last_pos = glyph->charpos) < pt_old))
10678 {
10679 if (! STRINGP (glyph->object))
10680 {
10681 string_start = NULL;
10682 x += glyph->pixel_width;
10683 ++glyph;
10684 }
10685 else
10686 {
10687 string_before_pos = last_pos;
10688 string_start = glyph;
10689 string_start_x = x;
10690 /* Skip all glyphs from string. */
10691 SKIP_GLYPHS (glyph, end, x, STRINGP (glyph->object));
10692 }
10693 }
10694
10695 if (string_start
10696 && (glyph == end || !BUFFERP (glyph->object) || last_pos > pt_old))
10697 {
10698 /* We may have skipped over point because the previous glyphs
10699 are from string. As there's no easy way to know the
10700 character position of the current glyph, find the correct
10701 glyph on point by scanning from string_start again. */
10702 Lisp_Object limit;
10703 Lisp_Object string;
10704 int pos;
10705
10706 limit = make_number (pt_old + 1);
10707 end = glyph;
10708 glyph = string_start;
10709 x = string_start_x;
10710 string = glyph->object;
10711 pos = string_buffer_position (w, string, string_before_pos);
10712 /* If STRING is from overlay, LAST_POS == 0. We skip such glyphs
10713 because we always put cursor after overlay strings. */
10714 while (pos == 0 && glyph < end)
10715 {
10716 string = glyph->object;
10717 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
10718 if (glyph < end)
10719 pos = string_buffer_position (w, glyph->object, string_before_pos);
10720 }
10721
10722 while (glyph < end)
10723 {
10724 pos = XINT (Fnext_single_char_property_change
10725 (make_number (pos), Qdisplay, Qnil, limit));
10726 if (pos > pt_old)
10727 break;
10728 /* Skip glyphs from the same string. */
10729 string = glyph->object;
10730 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
10731 /* Skip glyphs from an overlay. */
10732 while (glyph < end
10733 && ! string_buffer_position (w, glyph->object, pos))
10734 {
10735 string = glyph->object;
10736 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
10737 }
10738 }
10739 }
10740
10741 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
10742 w->cursor.x = x;
10743 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
10744 w->cursor.y = row->y + dy;
10745
10746 if (w == XWINDOW (selected_window))
10747 {
10748 if (!row->continued_p
10749 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
10750 && row->x == 0)
10751 {
10752 this_line_buffer = XBUFFER (w->buffer);
10753
10754 CHARPOS (this_line_start_pos)
10755 = MATRIX_ROW_START_CHARPOS (row) + delta;
10756 BYTEPOS (this_line_start_pos)
10757 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
10758
10759 CHARPOS (this_line_end_pos)
10760 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
10761 BYTEPOS (this_line_end_pos)
10762 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
10763
10764 this_line_y = w->cursor.y;
10765 this_line_pixel_height = row->height;
10766 this_line_vpos = w->cursor.vpos;
10767 this_line_start_x = row->x;
10768 }
10769 else
10770 CHARPOS (this_line_start_pos) = 0;
10771 }
10772 }
10773
10774
10775 /* Run window scroll functions, if any, for WINDOW with new window
10776 start STARTP. Sets the window start of WINDOW to that position.
10777
10778 We assume that the window's buffer is really current. */
10779
10780 static INLINE struct text_pos
10781 run_window_scroll_functions (window, startp)
10782 Lisp_Object window;
10783 struct text_pos startp;
10784 {
10785 struct window *w = XWINDOW (window);
10786 SET_MARKER_FROM_TEXT_POS (w->start, startp);
10787
10788 if (current_buffer != XBUFFER (w->buffer))
10789 abort ();
10790
10791 if (!NILP (Vwindow_scroll_functions))
10792 {
10793 run_hook_with_args_2 (Qwindow_scroll_functions, window,
10794 make_number (CHARPOS (startp)));
10795 SET_TEXT_POS_FROM_MARKER (startp, w->start);
10796 /* In case the hook functions switch buffers. */
10797 if (current_buffer != XBUFFER (w->buffer))
10798 set_buffer_internal_1 (XBUFFER (w->buffer));
10799 }
10800
10801 return startp;
10802 }
10803
10804
10805 /* Make sure the line containing the cursor is fully visible.
10806 A value of 1 means there is nothing to be done.
10807 (Either the line is fully visible, or it cannot be made so,
10808 or we cannot tell.)
10809
10810 If FORCE_P is non-zero, return 0 even if partial visible cursor row
10811 is higher than window.
10812
10813 A value of 0 means the caller should do scrolling
10814 as if point had gone off the screen. */
10815
10816 static int
10817 make_cursor_line_fully_visible (w, force_p)
10818 struct window *w;
10819 int force_p;
10820 {
10821 struct glyph_matrix *matrix;
10822 struct glyph_row *row;
10823 int window_height;
10824
10825 /* It's not always possible to find the cursor, e.g, when a window
10826 is full of overlay strings. Don't do anything in that case. */
10827 if (w->cursor.vpos < 0)
10828 return 1;
10829
10830 matrix = w->desired_matrix;
10831 row = MATRIX_ROW (matrix, w->cursor.vpos);
10832
10833 /* If the cursor row is not partially visible, there's nothing to do. */
10834 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (row))
10835 return 1;
10836
10837 /* If the row the cursor is in is taller than the window's height,
10838 it's not clear what to do, so do nothing. */
10839 window_height = window_box_height (w);
10840 if (row->height >= window_height)
10841 {
10842 if (!force_p || w->vscroll)
10843 return 1;
10844 }
10845 return 0;
10846
10847 #if 0
10848 /* This code used to try to scroll the window just enough to make
10849 the line visible. It returned 0 to say that the caller should
10850 allocate larger glyph matrices. */
10851
10852 if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (w, row))
10853 {
10854 int dy = row->height - row->visible_height;
10855 w->vscroll = 0;
10856 w->cursor.y += dy;
10857 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
10858 }
10859 else /* MATRIX_ROW_PARTIALLY_VISIBLE_AT_BOTTOM_P (w, row)) */
10860 {
10861 int dy = - (row->height - row->visible_height);
10862 w->vscroll = dy;
10863 w->cursor.y += dy;
10864 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
10865 }
10866
10867 /* When we change the cursor y-position of the selected window,
10868 change this_line_y as well so that the display optimization for
10869 the cursor line of the selected window in redisplay_internal uses
10870 the correct y-position. */
10871 if (w == XWINDOW (selected_window))
10872 this_line_y = w->cursor.y;
10873
10874 /* If vscrolling requires a larger glyph matrix, arrange for a fresh
10875 redisplay with larger matrices. */
10876 if (matrix->nrows < required_matrix_height (w))
10877 {
10878 fonts_changed_p = 1;
10879 return 0;
10880 }
10881
10882 return 1;
10883 #endif /* 0 */
10884 }
10885
10886
10887 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
10888 non-zero means only WINDOW is redisplayed in redisplay_internal.
10889 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
10890 in redisplay_window to bring a partially visible line into view in
10891 the case that only the cursor has moved.
10892
10893 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
10894 last screen line's vertical height extends past the end of the screen.
10895
10896 Value is
10897
10898 1 if scrolling succeeded
10899
10900 0 if scrolling didn't find point.
10901
10902 -1 if new fonts have been loaded so that we must interrupt
10903 redisplay, adjust glyph matrices, and try again. */
10904
10905 enum
10906 {
10907 SCROLLING_SUCCESS,
10908 SCROLLING_FAILED,
10909 SCROLLING_NEED_LARGER_MATRICES
10910 };
10911
10912 static int
10913 try_scrolling (window, just_this_one_p, scroll_conservatively,
10914 scroll_step, temp_scroll_step, last_line_misfit)
10915 Lisp_Object window;
10916 int just_this_one_p;
10917 EMACS_INT scroll_conservatively, scroll_step;
10918 int temp_scroll_step;
10919 int last_line_misfit;
10920 {
10921 struct window *w = XWINDOW (window);
10922 struct frame *f = XFRAME (w->frame);
10923 struct text_pos scroll_margin_pos;
10924 struct text_pos pos;
10925 struct text_pos startp;
10926 struct it it;
10927 Lisp_Object window_end;
10928 int this_scroll_margin;
10929 int dy = 0;
10930 int scroll_max;
10931 int rc;
10932 int amount_to_scroll = 0;
10933 Lisp_Object aggressive;
10934 int height;
10935 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
10936
10937 #if GLYPH_DEBUG
10938 debug_method_add (w, "try_scrolling");
10939 #endif
10940
10941 SET_TEXT_POS_FROM_MARKER (startp, w->start);
10942
10943 /* Compute scroll margin height in pixels. We scroll when point is
10944 within this distance from the top or bottom of the window. */
10945 if (scroll_margin > 0)
10946 {
10947 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
10948 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
10949 }
10950 else
10951 this_scroll_margin = 0;
10952
10953 /* Force scroll_conservatively to have a reasonable value so it doesn't
10954 cause an overflow while computing how much to scroll. */
10955 if (scroll_conservatively)
10956 scroll_conservatively = min (scroll_conservatively,
10957 MOST_POSITIVE_FIXNUM / FRAME_LINE_HEIGHT (f));
10958
10959 /* Compute how much we should try to scroll maximally to bring point
10960 into view. */
10961 if (scroll_step || scroll_conservatively || temp_scroll_step)
10962 scroll_max = max (scroll_step,
10963 max (scroll_conservatively, temp_scroll_step));
10964 else if (NUMBERP (current_buffer->scroll_down_aggressively)
10965 || NUMBERP (current_buffer->scroll_up_aggressively))
10966 /* We're trying to scroll because of aggressive scrolling
10967 but no scroll_step is set. Choose an arbitrary one. Maybe
10968 there should be a variable for this. */
10969 scroll_max = 10;
10970 else
10971 scroll_max = 0;
10972 scroll_max *= FRAME_LINE_HEIGHT (f);
10973
10974 /* Decide whether we have to scroll down. Start at the window end
10975 and move this_scroll_margin up to find the position of the scroll
10976 margin. */
10977 window_end = Fwindow_end (window, Qt);
10978
10979 too_near_end:
10980
10981 CHARPOS (scroll_margin_pos) = XINT (window_end);
10982 BYTEPOS (scroll_margin_pos) = CHAR_TO_BYTE (CHARPOS (scroll_margin_pos));
10983
10984 if (this_scroll_margin || extra_scroll_margin_lines)
10985 {
10986 start_display (&it, w, scroll_margin_pos);
10987 if (this_scroll_margin)
10988 move_it_vertically (&it, - this_scroll_margin);
10989 if (extra_scroll_margin_lines)
10990 move_it_by_lines (&it, - extra_scroll_margin_lines, 0);
10991 scroll_margin_pos = it.current.pos;
10992 }
10993
10994 if (PT >= CHARPOS (scroll_margin_pos))
10995 {
10996 int y0;
10997
10998 /* Point is in the scroll margin at the bottom of the window, or
10999 below. Compute a new window start that makes point visible. */
11000
11001 /* Compute the distance from the scroll margin to PT.
11002 Give up if the distance is greater than scroll_max. */
11003 start_display (&it, w, scroll_margin_pos);
11004 y0 = it.current_y;
11005 move_it_to (&it, PT, 0, it.last_visible_y, -1,
11006 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
11007
11008 /* To make point visible, we have to move the window start
11009 down so that the line the cursor is in is visible, which
11010 means we have to add in the height of the cursor line. */
11011 dy = line_bottom_y (&it) - y0;
11012
11013 if (dy > scroll_max)
11014 return SCROLLING_FAILED;
11015
11016 /* Move the window start down. If scrolling conservatively,
11017 move it just enough down to make point visible. If
11018 scroll_step is set, move it down by scroll_step. */
11019 start_display (&it, w, startp);
11020
11021 if (scroll_conservatively)
11022 /* Set AMOUNT_TO_SCROLL to at least one line,
11023 and at most scroll_conservatively lines. */
11024 amount_to_scroll
11025 = min (max (dy, FRAME_LINE_HEIGHT (f)),
11026 FRAME_LINE_HEIGHT (f) * scroll_conservatively);
11027 else if (scroll_step || temp_scroll_step)
11028 amount_to_scroll = scroll_max;
11029 else
11030 {
11031 aggressive = current_buffer->scroll_up_aggressively;
11032 height = WINDOW_BOX_TEXT_HEIGHT (w);
11033 if (NUMBERP (aggressive))
11034 {
11035 double float_amount = XFLOATINT (aggressive) * height;
11036 amount_to_scroll = float_amount;
11037 if (amount_to_scroll == 0 && float_amount > 0)
11038 amount_to_scroll = 1;
11039 }
11040 }
11041
11042 if (amount_to_scroll <= 0)
11043 return SCROLLING_FAILED;
11044
11045 /* If moving by amount_to_scroll leaves STARTP unchanged,
11046 move it down one screen line. */
11047
11048 move_it_vertically (&it, amount_to_scroll);
11049 if (CHARPOS (it.current.pos) == CHARPOS (startp))
11050 move_it_by_lines (&it, 1, 1);
11051 startp = it.current.pos;
11052 }
11053 else
11054 {
11055 /* See if point is inside the scroll margin at the top of the
11056 window. */
11057 scroll_margin_pos = startp;
11058 if (this_scroll_margin)
11059 {
11060 start_display (&it, w, startp);
11061 move_it_vertically (&it, this_scroll_margin);
11062 scroll_margin_pos = it.current.pos;
11063 }
11064
11065 if (PT < CHARPOS (scroll_margin_pos))
11066 {
11067 /* Point is in the scroll margin at the top of the window or
11068 above what is displayed in the window. */
11069 int y0;
11070
11071 /* Compute the vertical distance from PT to the scroll
11072 margin position. Give up if distance is greater than
11073 scroll_max. */
11074 SET_TEXT_POS (pos, PT, PT_BYTE);
11075 start_display (&it, w, pos);
11076 y0 = it.current_y;
11077 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
11078 it.last_visible_y, -1,
11079 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
11080 dy = it.current_y - y0;
11081 if (dy > scroll_max)
11082 return SCROLLING_FAILED;
11083
11084 /* Compute new window start. */
11085 start_display (&it, w, startp);
11086
11087 if (scroll_conservatively)
11088 amount_to_scroll =
11089 max (dy, FRAME_LINE_HEIGHT (f) * max (scroll_step, temp_scroll_step));
11090 else if (scroll_step || temp_scroll_step)
11091 amount_to_scroll = scroll_max;
11092 else
11093 {
11094 aggressive = current_buffer->scroll_down_aggressively;
11095 height = WINDOW_BOX_TEXT_HEIGHT (w);
11096 if (NUMBERP (aggressive))
11097 {
11098 double float_amount = XFLOATINT (aggressive) * height;
11099 amount_to_scroll = float_amount;
11100 if (amount_to_scroll == 0 && float_amount > 0)
11101 amount_to_scroll = 1;
11102 }
11103 }
11104
11105 if (amount_to_scroll <= 0)
11106 return SCROLLING_FAILED;
11107
11108 move_it_vertically (&it, - amount_to_scroll);
11109 startp = it.current.pos;
11110 }
11111 }
11112
11113 /* Run window scroll functions. */
11114 startp = run_window_scroll_functions (window, startp);
11115
11116 /* Display the window. Give up if new fonts are loaded, or if point
11117 doesn't appear. */
11118 if (!try_window (window, startp))
11119 rc = SCROLLING_NEED_LARGER_MATRICES;
11120 else if (w->cursor.vpos < 0)
11121 {
11122 clear_glyph_matrix (w->desired_matrix);
11123 rc = SCROLLING_FAILED;
11124 }
11125 else
11126 {
11127 /* Maybe forget recorded base line for line number display. */
11128 if (!just_this_one_p
11129 || current_buffer->clip_changed
11130 || BEG_UNCHANGED < CHARPOS (startp))
11131 w->base_line_number = Qnil;
11132
11133 /* If cursor ends up on a partially visible line,
11134 treat that as being off the bottom of the screen. */
11135 if (! make_cursor_line_fully_visible (w, extra_scroll_margin_lines <= 1))
11136 {
11137 clear_glyph_matrix (w->desired_matrix);
11138 ++extra_scroll_margin_lines;
11139 goto too_near_end;
11140 }
11141 rc = SCROLLING_SUCCESS;
11142 }
11143
11144 return rc;
11145 }
11146
11147
11148 /* Compute a suitable window start for window W if display of W starts
11149 on a continuation line. Value is non-zero if a new window start
11150 was computed.
11151
11152 The new window start will be computed, based on W's width, starting
11153 from the start of the continued line. It is the start of the
11154 screen line with the minimum distance from the old start W->start. */
11155
11156 static int
11157 compute_window_start_on_continuation_line (w)
11158 struct window *w;
11159 {
11160 struct text_pos pos, start_pos;
11161 int window_start_changed_p = 0;
11162
11163 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
11164
11165 /* If window start is on a continuation line... Window start may be
11166 < BEGV in case there's invisible text at the start of the
11167 buffer (M-x rmail, for example). */
11168 if (CHARPOS (start_pos) > BEGV
11169 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
11170 {
11171 struct it it;
11172 struct glyph_row *row;
11173
11174 /* Handle the case that the window start is out of range. */
11175 if (CHARPOS (start_pos) < BEGV)
11176 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
11177 else if (CHARPOS (start_pos) > ZV)
11178 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
11179
11180 /* Find the start of the continued line. This should be fast
11181 because scan_buffer is fast (newline cache). */
11182 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
11183 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
11184 row, DEFAULT_FACE_ID);
11185 reseat_at_previous_visible_line_start (&it);
11186
11187 /* If the line start is "too far" away from the window start,
11188 say it takes too much time to compute a new window start. */
11189 if (CHARPOS (start_pos) - IT_CHARPOS (it)
11190 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
11191 {
11192 int min_distance, distance;
11193
11194 /* Move forward by display lines to find the new window
11195 start. If window width was enlarged, the new start can
11196 be expected to be > the old start. If window width was
11197 decreased, the new window start will be < the old start.
11198 So, we're looking for the display line start with the
11199 minimum distance from the old window start. */
11200 pos = it.current.pos;
11201 min_distance = INFINITY;
11202 while ((distance = abs (CHARPOS (start_pos) - IT_CHARPOS (it))),
11203 distance < min_distance)
11204 {
11205 min_distance = distance;
11206 pos = it.current.pos;
11207 move_it_by_lines (&it, 1, 0);
11208 }
11209
11210 /* Set the window start there. */
11211 SET_MARKER_FROM_TEXT_POS (w->start, pos);
11212 window_start_changed_p = 1;
11213 }
11214 }
11215
11216 return window_start_changed_p;
11217 }
11218
11219
11220 /* Try cursor movement in case text has not changed in window WINDOW,
11221 with window start STARTP. Value is
11222
11223 CURSOR_MOVEMENT_SUCCESS if successful
11224
11225 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
11226
11227 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
11228 display. *SCROLL_STEP is set to 1, under certain circumstances, if
11229 we want to scroll as if scroll-step were set to 1. See the code.
11230
11231 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
11232 which case we have to abort this redisplay, and adjust matrices
11233 first. */
11234
11235 enum
11236 {
11237 CURSOR_MOVEMENT_SUCCESS,
11238 CURSOR_MOVEMENT_CANNOT_BE_USED,
11239 CURSOR_MOVEMENT_MUST_SCROLL,
11240 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
11241 };
11242
11243 static int
11244 try_cursor_movement (window, startp, scroll_step)
11245 Lisp_Object window;
11246 struct text_pos startp;
11247 int *scroll_step;
11248 {
11249 struct window *w = XWINDOW (window);
11250 struct frame *f = XFRAME (w->frame);
11251 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
11252
11253 #if GLYPH_DEBUG
11254 if (inhibit_try_cursor_movement)
11255 return rc;
11256 #endif
11257
11258 /* Handle case where text has not changed, only point, and it has
11259 not moved off the frame. */
11260 if (/* Point may be in this window. */
11261 PT >= CHARPOS (startp)
11262 /* Selective display hasn't changed. */
11263 && !current_buffer->clip_changed
11264 /* Function force-mode-line-update is used to force a thorough
11265 redisplay. It sets either windows_or_buffers_changed or
11266 update_mode_lines. So don't take a shortcut here for these
11267 cases. */
11268 && !update_mode_lines
11269 && !windows_or_buffers_changed
11270 && !cursor_type_changed
11271 /* Can't use this case if highlighting a region. When a
11272 region exists, cursor movement has to do more than just
11273 set the cursor. */
11274 && !(!NILP (Vtransient_mark_mode)
11275 && !NILP (current_buffer->mark_active))
11276 && NILP (w->region_showing)
11277 && NILP (Vshow_trailing_whitespace)
11278 /* Right after splitting windows, last_point may be nil. */
11279 && INTEGERP (w->last_point)
11280 /* This code is not used for mini-buffer for the sake of the case
11281 of redisplaying to replace an echo area message; since in
11282 that case the mini-buffer contents per se are usually
11283 unchanged. This code is of no real use in the mini-buffer
11284 since the handling of this_line_start_pos, etc., in redisplay
11285 handles the same cases. */
11286 && !EQ (window, minibuf_window)
11287 /* When splitting windows or for new windows, it happens that
11288 redisplay is called with a nil window_end_vpos or one being
11289 larger than the window. This should really be fixed in
11290 window.c. I don't have this on my list, now, so we do
11291 approximately the same as the old redisplay code. --gerd. */
11292 && INTEGERP (w->window_end_vpos)
11293 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
11294 && (FRAME_WINDOW_P (f)
11295 || !overlay_arrow_in_current_buffer_p ()))
11296 {
11297 int this_scroll_margin, top_scroll_margin;
11298 struct glyph_row *row = NULL;
11299
11300 #if GLYPH_DEBUG
11301 debug_method_add (w, "cursor movement");
11302 #endif
11303
11304 /* Scroll if point within this distance from the top or bottom
11305 of the window. This is a pixel value. */
11306 this_scroll_margin = max (0, scroll_margin);
11307 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
11308 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
11309
11310 top_scroll_margin = this_scroll_margin;
11311 if (WINDOW_WANTS_HEADER_LINE_P (w))
11312 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
11313
11314 /* Start with the row the cursor was displayed during the last
11315 not paused redisplay. Give up if that row is not valid. */
11316 if (w->last_cursor.vpos < 0
11317 || w->last_cursor.vpos >= w->current_matrix->nrows)
11318 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11319 else
11320 {
11321 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
11322 if (row->mode_line_p)
11323 ++row;
11324 if (!row->enabled_p)
11325 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11326 }
11327
11328 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
11329 {
11330 int scroll_p = 0;
11331 int last_y = window_text_bottom_y (w) - this_scroll_margin;
11332
11333 if (PT > XFASTINT (w->last_point))
11334 {
11335 /* Point has moved forward. */
11336 while (MATRIX_ROW_END_CHARPOS (row) < PT
11337 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
11338 {
11339 xassert (row->enabled_p);
11340 ++row;
11341 }
11342
11343 /* The end position of a row equals the start position
11344 of the next row. If PT is there, we would rather
11345 display it in the next line. */
11346 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
11347 && MATRIX_ROW_END_CHARPOS (row) == PT
11348 && !cursor_row_p (w, row))
11349 ++row;
11350
11351 /* If within the scroll margin, scroll. Note that
11352 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
11353 the next line would be drawn, and that
11354 this_scroll_margin can be zero. */
11355 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
11356 || PT > MATRIX_ROW_END_CHARPOS (row)
11357 /* Line is completely visible last line in window
11358 and PT is to be set in the next line. */
11359 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
11360 && PT == MATRIX_ROW_END_CHARPOS (row)
11361 && !row->ends_at_zv_p
11362 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
11363 scroll_p = 1;
11364 }
11365 else if (PT < XFASTINT (w->last_point))
11366 {
11367 /* Cursor has to be moved backward. Note that PT >=
11368 CHARPOS (startp) because of the outer
11369 if-statement. */
11370 while (!row->mode_line_p
11371 && (MATRIX_ROW_START_CHARPOS (row) > PT
11372 || (MATRIX_ROW_START_CHARPOS (row) == PT
11373 && MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)))
11374 && (row->y > top_scroll_margin
11375 || CHARPOS (startp) == BEGV))
11376 {
11377 xassert (row->enabled_p);
11378 --row;
11379 }
11380
11381 /* Consider the following case: Window starts at BEGV,
11382 there is invisible, intangible text at BEGV, so that
11383 display starts at some point START > BEGV. It can
11384 happen that we are called with PT somewhere between
11385 BEGV and START. Try to handle that case. */
11386 if (row < w->current_matrix->rows
11387 || row->mode_line_p)
11388 {
11389 row = w->current_matrix->rows;
11390 if (row->mode_line_p)
11391 ++row;
11392 }
11393
11394 /* Due to newlines in overlay strings, we may have to
11395 skip forward over overlay strings. */
11396 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
11397 && MATRIX_ROW_END_CHARPOS (row) == PT
11398 && !cursor_row_p (w, row))
11399 ++row;
11400
11401 /* If within the scroll margin, scroll. */
11402 if (row->y < top_scroll_margin
11403 && CHARPOS (startp) != BEGV)
11404 scroll_p = 1;
11405 }
11406
11407 if (PT < MATRIX_ROW_START_CHARPOS (row)
11408 || PT > MATRIX_ROW_END_CHARPOS (row))
11409 {
11410 /* if PT is not in the glyph row, give up. */
11411 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11412 }
11413 else if (MATRIX_ROW_PARTIALLY_VISIBLE_P (row))
11414 {
11415 if (PT == MATRIX_ROW_END_CHARPOS (row)
11416 && !row->ends_at_zv_p
11417 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
11418 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11419 else if (row->height > window_box_height (w))
11420 {
11421 /* If we end up in a partially visible line, let's
11422 make it fully visible, except when it's taller
11423 than the window, in which case we can't do much
11424 about it. */
11425 *scroll_step = 1;
11426 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11427 }
11428 else
11429 {
11430 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11431 if (!make_cursor_line_fully_visible (w, 0))
11432 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11433 else
11434 rc = CURSOR_MOVEMENT_SUCCESS;
11435 }
11436 }
11437 else if (scroll_p)
11438 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11439 else
11440 {
11441 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11442 rc = CURSOR_MOVEMENT_SUCCESS;
11443 }
11444 }
11445 }
11446
11447 return rc;
11448 }
11449
11450 void
11451 set_vertical_scroll_bar (w)
11452 struct window *w;
11453 {
11454 int start, end, whole;
11455
11456 /* Calculate the start and end positions for the current window.
11457 At some point, it would be nice to choose between scrollbars
11458 which reflect the whole buffer size, with special markers
11459 indicating narrowing, and scrollbars which reflect only the
11460 visible region.
11461
11462 Note that mini-buffers sometimes aren't displaying any text. */
11463 if (!MINI_WINDOW_P (w)
11464 || (w == XWINDOW (minibuf_window)
11465 && NILP (echo_area_buffer[0])))
11466 {
11467 struct buffer *buf = XBUFFER (w->buffer);
11468 whole = BUF_ZV (buf) - BUF_BEGV (buf);
11469 start = marker_position (w->start) - BUF_BEGV (buf);
11470 /* I don't think this is guaranteed to be right. For the
11471 moment, we'll pretend it is. */
11472 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
11473
11474 if (end < start)
11475 end = start;
11476 if (whole < (end - start))
11477 whole = end - start;
11478 }
11479 else
11480 start = end = whole = 0;
11481
11482 /* Indicate what this scroll bar ought to be displaying now. */
11483 set_vertical_scroll_bar_hook (w, end - start, whole, start);
11484 }
11485
11486
11487 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
11488 selected_window is redisplayed.
11489
11490 We can return without actually redisplaying the window if
11491 fonts_changed_p is nonzero. In that case, redisplay_internal will
11492 retry. */
11493
11494 static void
11495 redisplay_window (window, just_this_one_p)
11496 Lisp_Object window;
11497 int just_this_one_p;
11498 {
11499 struct window *w = XWINDOW (window);
11500 struct frame *f = XFRAME (w->frame);
11501 struct buffer *buffer = XBUFFER (w->buffer);
11502 struct buffer *old = current_buffer;
11503 struct text_pos lpoint, opoint, startp;
11504 int update_mode_line;
11505 int tem;
11506 struct it it;
11507 /* Record it now because it's overwritten. */
11508 int current_matrix_up_to_date_p = 0;
11509 int used_current_matrix_p = 0;
11510 /* This is less strict than current_matrix_up_to_date_p.
11511 It indictes that the buffer contents and narrowing are unchanged. */
11512 int buffer_unchanged_p = 0;
11513 int temp_scroll_step = 0;
11514 int count = SPECPDL_INDEX ();
11515 int rc;
11516 int centering_position;
11517 int last_line_misfit = 0;
11518
11519 SET_TEXT_POS (lpoint, PT, PT_BYTE);
11520 opoint = lpoint;
11521
11522 /* W must be a leaf window here. */
11523 xassert (!NILP (w->buffer));
11524 #if GLYPH_DEBUG
11525 *w->desired_matrix->method = 0;
11526 #endif
11527
11528 specbind (Qinhibit_point_motion_hooks, Qt);
11529
11530 reconsider_clip_changes (w, buffer);
11531
11532 /* Has the mode line to be updated? */
11533 update_mode_line = (!NILP (w->update_mode_line)
11534 || update_mode_lines
11535 || buffer->clip_changed
11536 || buffer->prevent_redisplay_optimizations_p);
11537
11538 if (MINI_WINDOW_P (w))
11539 {
11540 if (w == XWINDOW (echo_area_window)
11541 && !NILP (echo_area_buffer[0]))
11542 {
11543 if (update_mode_line)
11544 /* We may have to update a tty frame's menu bar or a
11545 tool-bar. Example `M-x C-h C-h C-g'. */
11546 goto finish_menu_bars;
11547 else
11548 /* We've already displayed the echo area glyphs in this window. */
11549 goto finish_scroll_bars;
11550 }
11551 else if ((w != XWINDOW (minibuf_window)
11552 || minibuf_level == 0)
11553 /* When buffer is nonempty, redisplay window normally. */
11554 && BUF_Z (XBUFFER (w->buffer)) == BUF_BEG (XBUFFER (w->buffer))
11555 /* Quail displays non-mini buffers in minibuffer window.
11556 In that case, redisplay the window normally. */
11557 && !NILP (Fmemq (w->buffer, Vminibuffer_list)))
11558 {
11559 /* W is a mini-buffer window, but it's not active, so clear
11560 it. */
11561 int yb = window_text_bottom_y (w);
11562 struct glyph_row *row;
11563 int y;
11564
11565 for (y = 0, row = w->desired_matrix->rows;
11566 y < yb;
11567 y += row->height, ++row)
11568 blank_row (w, row, y);
11569 goto finish_scroll_bars;
11570 }
11571
11572 clear_glyph_matrix (w->desired_matrix);
11573 }
11574
11575 /* Otherwise set up data on this window; select its buffer and point
11576 value. */
11577 /* Really select the buffer, for the sake of buffer-local
11578 variables. */
11579 set_buffer_internal_1 (XBUFFER (w->buffer));
11580 SET_TEXT_POS (opoint, PT, PT_BYTE);
11581
11582 current_matrix_up_to_date_p
11583 = (!NILP (w->window_end_valid)
11584 && !current_buffer->clip_changed
11585 && !current_buffer->prevent_redisplay_optimizations_p
11586 && XFASTINT (w->last_modified) >= MODIFF
11587 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
11588
11589 buffer_unchanged_p
11590 = (!NILP (w->window_end_valid)
11591 && !current_buffer->clip_changed
11592 && XFASTINT (w->last_modified) >= MODIFF
11593 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
11594
11595 /* When windows_or_buffers_changed is non-zero, we can't rely on
11596 the window end being valid, so set it to nil there. */
11597 if (windows_or_buffers_changed)
11598 {
11599 /* If window starts on a continuation line, maybe adjust the
11600 window start in case the window's width changed. */
11601 if (XMARKER (w->start)->buffer == current_buffer)
11602 compute_window_start_on_continuation_line (w);
11603
11604 w->window_end_valid = Qnil;
11605 }
11606
11607 /* Some sanity checks. */
11608 CHECK_WINDOW_END (w);
11609 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
11610 abort ();
11611 if (BYTEPOS (opoint) < CHARPOS (opoint))
11612 abort ();
11613
11614 /* If %c is in mode line, update it if needed. */
11615 if (!NILP (w->column_number_displayed)
11616 /* This alternative quickly identifies a common case
11617 where no change is needed. */
11618 && !(PT == XFASTINT (w->last_point)
11619 && XFASTINT (w->last_modified) >= MODIFF
11620 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
11621 && (XFASTINT (w->column_number_displayed)
11622 != (int) current_column ())) /* iftc */
11623 update_mode_line = 1;
11624
11625 /* Count number of windows showing the selected buffer. An indirect
11626 buffer counts as its base buffer. */
11627 if (!just_this_one_p)
11628 {
11629 struct buffer *current_base, *window_base;
11630 current_base = current_buffer;
11631 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
11632 if (current_base->base_buffer)
11633 current_base = current_base->base_buffer;
11634 if (window_base->base_buffer)
11635 window_base = window_base->base_buffer;
11636 if (current_base == window_base)
11637 buffer_shared++;
11638 }
11639
11640 /* Point refers normally to the selected window. For any other
11641 window, set up appropriate value. */
11642 if (!EQ (window, selected_window))
11643 {
11644 int new_pt = XMARKER (w->pointm)->charpos;
11645 int new_pt_byte = marker_byte_position (w->pointm);
11646 if (new_pt < BEGV)
11647 {
11648 new_pt = BEGV;
11649 new_pt_byte = BEGV_BYTE;
11650 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
11651 }
11652 else if (new_pt > (ZV - 1))
11653 {
11654 new_pt = ZV;
11655 new_pt_byte = ZV_BYTE;
11656 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
11657 }
11658
11659 /* We don't use SET_PT so that the point-motion hooks don't run. */
11660 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
11661 }
11662
11663 /* If any of the character widths specified in the display table
11664 have changed, invalidate the width run cache. It's true that
11665 this may be a bit late to catch such changes, but the rest of
11666 redisplay goes (non-fatally) haywire when the display table is
11667 changed, so why should we worry about doing any better? */
11668 if (current_buffer->width_run_cache)
11669 {
11670 struct Lisp_Char_Table *disptab = buffer_display_table ();
11671
11672 if (! disptab_matches_widthtab (disptab,
11673 XVECTOR (current_buffer->width_table)))
11674 {
11675 invalidate_region_cache (current_buffer,
11676 current_buffer->width_run_cache,
11677 BEG, Z);
11678 recompute_width_table (current_buffer, disptab);
11679 }
11680 }
11681
11682 /* If window-start is screwed up, choose a new one. */
11683 if (XMARKER (w->start)->buffer != current_buffer)
11684 goto recenter;
11685
11686 SET_TEXT_POS_FROM_MARKER (startp, w->start);
11687
11688 /* If someone specified a new starting point but did not insist,
11689 check whether it can be used. */
11690 if (!NILP (w->optional_new_start)
11691 && CHARPOS (startp) >= BEGV
11692 && CHARPOS (startp) <= ZV)
11693 {
11694 w->optional_new_start = Qnil;
11695 start_display (&it, w, startp);
11696 move_it_to (&it, PT, 0, it.last_visible_y, -1,
11697 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
11698 if (IT_CHARPOS (it) == PT)
11699 w->force_start = Qt;
11700 /* IT may overshoot PT if text at PT is invisible. */
11701 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
11702 w->force_start = Qt;
11703
11704
11705 }
11706
11707 /* Handle case where place to start displaying has been specified,
11708 unless the specified location is outside the accessible range. */
11709 if (!NILP (w->force_start)
11710 || w->frozen_window_start_p)
11711 {
11712 /* We set this later on if we have to adjust point. */
11713 int new_vpos = -1;
11714
11715 w->force_start = Qnil;
11716 w->vscroll = 0;
11717 w->window_end_valid = Qnil;
11718
11719 /* Forget any recorded base line for line number display. */
11720 if (!buffer_unchanged_p)
11721 w->base_line_number = Qnil;
11722
11723 /* Redisplay the mode line. Select the buffer properly for that.
11724 Also, run the hook window-scroll-functions
11725 because we have scrolled. */
11726 /* Note, we do this after clearing force_start because
11727 if there's an error, it is better to forget about force_start
11728 than to get into an infinite loop calling the hook functions
11729 and having them get more errors. */
11730 if (!update_mode_line
11731 || ! NILP (Vwindow_scroll_functions))
11732 {
11733 update_mode_line = 1;
11734 w->update_mode_line = Qt;
11735 startp = run_window_scroll_functions (window, startp);
11736 }
11737
11738 w->last_modified = make_number (0);
11739 w->last_overlay_modified = make_number (0);
11740 if (CHARPOS (startp) < BEGV)
11741 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
11742 else if (CHARPOS (startp) > ZV)
11743 SET_TEXT_POS (startp, ZV, ZV_BYTE);
11744
11745 /* Redisplay, then check if cursor has been set during the
11746 redisplay. Give up if new fonts were loaded. */
11747 if (!try_window (window, startp))
11748 {
11749 w->force_start = Qt;
11750 clear_glyph_matrix (w->desired_matrix);
11751 goto need_larger_matrices;
11752 }
11753
11754 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
11755 {
11756 /* If point does not appear, try to move point so it does
11757 appear. The desired matrix has been built above, so we
11758 can use it here. */
11759 new_vpos = window_box_height (w) / 2;
11760 }
11761
11762 if (!make_cursor_line_fully_visible (w, 0))
11763 {
11764 /* Point does appear, but on a line partly visible at end of window.
11765 Move it back to a fully-visible line. */
11766 new_vpos = window_box_height (w);
11767 }
11768
11769 /* If we need to move point for either of the above reasons,
11770 now actually do it. */
11771 if (new_vpos >= 0)
11772 {
11773 struct glyph_row *row;
11774
11775 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
11776 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
11777 ++row;
11778
11779 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
11780 MATRIX_ROW_START_BYTEPOS (row));
11781
11782 if (w != XWINDOW (selected_window))
11783 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
11784 else if (current_buffer == old)
11785 SET_TEXT_POS (lpoint, PT, PT_BYTE);
11786
11787 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
11788
11789 /* If we are highlighting the region, then we just changed
11790 the region, so redisplay to show it. */
11791 if (!NILP (Vtransient_mark_mode)
11792 && !NILP (current_buffer->mark_active))
11793 {
11794 clear_glyph_matrix (w->desired_matrix);
11795 if (!try_window (window, startp))
11796 goto need_larger_matrices;
11797 }
11798 }
11799
11800 #if GLYPH_DEBUG
11801 debug_method_add (w, "forced window start");
11802 #endif
11803 goto done;
11804 }
11805
11806 /* Handle case where text has not changed, only point, and it has
11807 not moved off the frame, and we are not retrying after hscroll.
11808 (current_matrix_up_to_date_p is nonzero when retrying.) */
11809 if (current_matrix_up_to_date_p
11810 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
11811 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
11812 {
11813 switch (rc)
11814 {
11815 case CURSOR_MOVEMENT_SUCCESS:
11816 used_current_matrix_p = 1;
11817 goto done;
11818
11819 #if 0 /* try_cursor_movement never returns this value. */
11820 case CURSOR_MOVEMENT_NEED_LARGER_MATRICES:
11821 goto need_larger_matrices;
11822 #endif
11823
11824 case CURSOR_MOVEMENT_MUST_SCROLL:
11825 goto try_to_scroll;
11826
11827 default:
11828 abort ();
11829 }
11830 }
11831 /* If current starting point was originally the beginning of a line
11832 but no longer is, find a new starting point. */
11833 else if (!NILP (w->start_at_line_beg)
11834 && !(CHARPOS (startp) <= BEGV
11835 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
11836 {
11837 #if GLYPH_DEBUG
11838 debug_method_add (w, "recenter 1");
11839 #endif
11840 goto recenter;
11841 }
11842
11843 /* Try scrolling with try_window_id. Value is > 0 if update has
11844 been done, it is -1 if we know that the same window start will
11845 not work. It is 0 if unsuccessful for some other reason. */
11846 else if ((tem = try_window_id (w)) != 0)
11847 {
11848 #if GLYPH_DEBUG
11849 debug_method_add (w, "try_window_id %d", tem);
11850 #endif
11851
11852 if (fonts_changed_p)
11853 goto need_larger_matrices;
11854 if (tem > 0)
11855 goto done;
11856
11857 /* Otherwise try_window_id has returned -1 which means that we
11858 don't want the alternative below this comment to execute. */
11859 }
11860 else if (CHARPOS (startp) >= BEGV
11861 && CHARPOS (startp) <= ZV
11862 && PT >= CHARPOS (startp)
11863 && (CHARPOS (startp) < ZV
11864 /* Avoid starting at end of buffer. */
11865 || CHARPOS (startp) == BEGV
11866 || (XFASTINT (w->last_modified) >= MODIFF
11867 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
11868 {
11869 #if GLYPH_DEBUG
11870 debug_method_add (w, "same window start");
11871 #endif
11872
11873 /* Try to redisplay starting at same place as before.
11874 If point has not moved off frame, accept the results. */
11875 if (!current_matrix_up_to_date_p
11876 /* Don't use try_window_reusing_current_matrix in this case
11877 because a window scroll function can have changed the
11878 buffer. */
11879 || !NILP (Vwindow_scroll_functions)
11880 || MINI_WINDOW_P (w)
11881 || !(used_current_matrix_p =
11882 try_window_reusing_current_matrix (w)))
11883 {
11884 IF_DEBUG (debug_method_add (w, "1"));
11885 try_window (window, startp);
11886 }
11887
11888 if (fonts_changed_p)
11889 goto need_larger_matrices;
11890
11891 if (w->cursor.vpos >= 0)
11892 {
11893 if (!just_this_one_p
11894 || current_buffer->clip_changed
11895 || BEG_UNCHANGED < CHARPOS (startp))
11896 /* Forget any recorded base line for line number display. */
11897 w->base_line_number = Qnil;
11898
11899 if (!make_cursor_line_fully_visible (w, 1))
11900 {
11901 clear_glyph_matrix (w->desired_matrix);
11902 last_line_misfit = 1;
11903 }
11904 /* Drop through and scroll. */
11905 else
11906 goto done;
11907 }
11908 else
11909 clear_glyph_matrix (w->desired_matrix);
11910 }
11911
11912 try_to_scroll:
11913
11914 w->last_modified = make_number (0);
11915 w->last_overlay_modified = make_number (0);
11916
11917 /* Redisplay the mode line. Select the buffer properly for that. */
11918 if (!update_mode_line)
11919 {
11920 update_mode_line = 1;
11921 w->update_mode_line = Qt;
11922 }
11923
11924 /* Try to scroll by specified few lines. */
11925 if ((scroll_conservatively
11926 || scroll_step
11927 || temp_scroll_step
11928 || NUMBERP (current_buffer->scroll_up_aggressively)
11929 || NUMBERP (current_buffer->scroll_down_aggressively))
11930 && !current_buffer->clip_changed
11931 && CHARPOS (startp) >= BEGV
11932 && CHARPOS (startp) <= ZV)
11933 {
11934 /* The function returns -1 if new fonts were loaded, 1 if
11935 successful, 0 if not successful. */
11936 int rc = try_scrolling (window, just_this_one_p,
11937 scroll_conservatively,
11938 scroll_step,
11939 temp_scroll_step, last_line_misfit);
11940 switch (rc)
11941 {
11942 case SCROLLING_SUCCESS:
11943 goto done;
11944
11945 case SCROLLING_NEED_LARGER_MATRICES:
11946 goto need_larger_matrices;
11947
11948 case SCROLLING_FAILED:
11949 break;
11950
11951 default:
11952 abort ();
11953 }
11954 }
11955
11956 /* Finally, just choose place to start which centers point */
11957
11958 recenter:
11959 centering_position = window_box_height (w) / 2;
11960
11961 point_at_top:
11962 /* Jump here with centering_position already set to 0. */
11963
11964 #if GLYPH_DEBUG
11965 debug_method_add (w, "recenter");
11966 #endif
11967
11968 /* w->vscroll = 0; */
11969
11970 /* Forget any previously recorded base line for line number display. */
11971 if (!buffer_unchanged_p)
11972 w->base_line_number = Qnil;
11973
11974 /* Move backward half the height of the window. */
11975 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
11976 it.current_y = it.last_visible_y;
11977 move_it_vertically_backward (&it, centering_position);
11978 xassert (IT_CHARPOS (it) >= BEGV);
11979
11980 /* The function move_it_vertically_backward may move over more
11981 than the specified y-distance. If it->w is small, e.g. a
11982 mini-buffer window, we may end up in front of the window's
11983 display area. Start displaying at the start of the line
11984 containing PT in this case. */
11985 if (it.current_y <= 0)
11986 {
11987 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
11988 move_it_vertically (&it, 0);
11989 xassert (IT_CHARPOS (it) <= PT);
11990 it.current_y = 0;
11991 }
11992
11993 it.current_x = it.hpos = 0;
11994
11995 /* Set startp here explicitly in case that helps avoid an infinite loop
11996 in case the window-scroll-functions functions get errors. */
11997 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
11998
11999 /* Run scroll hooks. */
12000 startp = run_window_scroll_functions (window, it.current.pos);
12001
12002 /* Redisplay the window. */
12003 if (!current_matrix_up_to_date_p
12004 || windows_or_buffers_changed
12005 || cursor_type_changed
12006 /* Don't use try_window_reusing_current_matrix in this case
12007 because it can have changed the buffer. */
12008 || !NILP (Vwindow_scroll_functions)
12009 || !just_this_one_p
12010 || MINI_WINDOW_P (w)
12011 || !(used_current_matrix_p =
12012 try_window_reusing_current_matrix (w)))
12013 try_window (window, startp);
12014
12015 /* If new fonts have been loaded (due to fontsets), give up. We
12016 have to start a new redisplay since we need to re-adjust glyph
12017 matrices. */
12018 if (fonts_changed_p)
12019 goto need_larger_matrices;
12020
12021 /* If cursor did not appear assume that the middle of the window is
12022 in the first line of the window. Do it again with the next line.
12023 (Imagine a window of height 100, displaying two lines of height
12024 60. Moving back 50 from it->last_visible_y will end in the first
12025 line.) */
12026 if (w->cursor.vpos < 0)
12027 {
12028 if (!NILP (w->window_end_valid)
12029 && PT >= Z - XFASTINT (w->window_end_pos))
12030 {
12031 clear_glyph_matrix (w->desired_matrix);
12032 move_it_by_lines (&it, 1, 0);
12033 try_window (window, it.current.pos);
12034 }
12035 else if (PT < IT_CHARPOS (it))
12036 {
12037 clear_glyph_matrix (w->desired_matrix);
12038 move_it_by_lines (&it, -1, 0);
12039 try_window (window, it.current.pos);
12040 }
12041 else
12042 {
12043 /* Not much we can do about it. */
12044 }
12045 }
12046
12047 /* Consider the following case: Window starts at BEGV, there is
12048 invisible, intangible text at BEGV, so that display starts at
12049 some point START > BEGV. It can happen that we are called with
12050 PT somewhere between BEGV and START. Try to handle that case. */
12051 if (w->cursor.vpos < 0)
12052 {
12053 struct glyph_row *row = w->current_matrix->rows;
12054 if (row->mode_line_p)
12055 ++row;
12056 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
12057 }
12058
12059 if (!make_cursor_line_fully_visible (w, centering_position > 0))
12060 {
12061 /* If vscroll is enabled, disable it and try again. */
12062 if (w->vscroll)
12063 {
12064 w->vscroll = 0;
12065 clear_glyph_matrix (w->desired_matrix);
12066 goto recenter;
12067 }
12068
12069 /* If centering point failed to make the whole line visible,
12070 put point at the top instead. That has to make the whole line
12071 visible, if it can be done. */
12072 clear_glyph_matrix (w->desired_matrix);
12073 centering_position = 0;
12074 goto point_at_top;
12075 }
12076
12077 done:
12078
12079 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12080 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
12081 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
12082 ? Qt : Qnil);
12083
12084 /* Display the mode line, if we must. */
12085 if ((update_mode_line
12086 /* If window not full width, must redo its mode line
12087 if (a) the window to its side is being redone and
12088 (b) we do a frame-based redisplay. This is a consequence
12089 of how inverted lines are drawn in frame-based redisplay. */
12090 || (!just_this_one_p
12091 && !FRAME_WINDOW_P (f)
12092 && !WINDOW_FULL_WIDTH_P (w))
12093 /* Line number to display. */
12094 || INTEGERP (w->base_line_pos)
12095 /* Column number is displayed and different from the one displayed. */
12096 || (!NILP (w->column_number_displayed)
12097 && (XFASTINT (w->column_number_displayed)
12098 != (int) current_column ()))) /* iftc */
12099 /* This means that the window has a mode line. */
12100 && (WINDOW_WANTS_MODELINE_P (w)
12101 || WINDOW_WANTS_HEADER_LINE_P (w)))
12102 {
12103 display_mode_lines (w);
12104
12105 /* If mode line height has changed, arrange for a thorough
12106 immediate redisplay using the correct mode line height. */
12107 if (WINDOW_WANTS_MODELINE_P (w)
12108 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
12109 {
12110 fonts_changed_p = 1;
12111 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
12112 = DESIRED_MODE_LINE_HEIGHT (w);
12113 }
12114
12115 /* If top line height has changed, arrange for a thorough
12116 immediate redisplay using the correct mode line height. */
12117 if (WINDOW_WANTS_HEADER_LINE_P (w)
12118 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
12119 {
12120 fonts_changed_p = 1;
12121 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
12122 = DESIRED_HEADER_LINE_HEIGHT (w);
12123 }
12124
12125 if (fonts_changed_p)
12126 goto need_larger_matrices;
12127 }
12128
12129 if (!line_number_displayed
12130 && !BUFFERP (w->base_line_pos))
12131 {
12132 w->base_line_pos = Qnil;
12133 w->base_line_number = Qnil;
12134 }
12135
12136 finish_menu_bars:
12137
12138 /* When we reach a frame's selected window, redo the frame's menu bar. */
12139 if (update_mode_line
12140 && EQ (FRAME_SELECTED_WINDOW (f), window))
12141 {
12142 int redisplay_menu_p = 0;
12143 int redisplay_tool_bar_p = 0;
12144
12145 if (FRAME_WINDOW_P (f))
12146 {
12147 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
12148 || defined (USE_GTK)
12149 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
12150 #else
12151 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
12152 #endif
12153 }
12154 else
12155 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
12156
12157 if (redisplay_menu_p)
12158 display_menu_bar (w);
12159
12160 #ifdef HAVE_WINDOW_SYSTEM
12161 #ifdef USE_GTK
12162 redisplay_tool_bar_p = FRAME_EXTERNAL_TOOL_BAR (f);
12163 #else
12164 redisplay_tool_bar_p = WINDOWP (f->tool_bar_window)
12165 && (FRAME_TOOL_BAR_LINES (f) > 0
12166 || auto_resize_tool_bars_p);
12167
12168 #endif
12169
12170 if (redisplay_tool_bar_p)
12171 redisplay_tool_bar (f);
12172 #endif
12173 }
12174
12175 #ifdef HAVE_WINDOW_SYSTEM
12176 if (update_window_fringes (w, 0)
12177 && !just_this_one_p
12178 && (used_current_matrix_p || overlay_arrow_seen)
12179 && !w->pseudo_window_p)
12180 {
12181 update_begin (f);
12182 BLOCK_INPUT;
12183 draw_window_fringes (w);
12184 UNBLOCK_INPUT;
12185 update_end (f);
12186 }
12187 #endif /* HAVE_WINDOW_SYSTEM */
12188
12189 /* We go to this label, with fonts_changed_p nonzero,
12190 if it is necessary to try again using larger glyph matrices.
12191 We have to redeem the scroll bar even in this case,
12192 because the loop in redisplay_internal expects that. */
12193 need_larger_matrices:
12194 ;
12195 finish_scroll_bars:
12196
12197 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
12198 {
12199 /* Set the thumb's position and size. */
12200 set_vertical_scroll_bar (w);
12201
12202 /* Note that we actually used the scroll bar attached to this
12203 window, so it shouldn't be deleted at the end of redisplay. */
12204 redeem_scroll_bar_hook (w);
12205 }
12206
12207 /* Restore current_buffer and value of point in it. */
12208 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
12209 set_buffer_internal_1 (old);
12210 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
12211
12212 unbind_to (count, Qnil);
12213 }
12214
12215
12216 /* Build the complete desired matrix of WINDOW with a window start
12217 buffer position POS. Value is non-zero if successful. It is zero
12218 if fonts were loaded during redisplay which makes re-adjusting
12219 glyph matrices necessary. */
12220
12221 int
12222 try_window (window, pos)
12223 Lisp_Object window;
12224 struct text_pos pos;
12225 {
12226 struct window *w = XWINDOW (window);
12227 struct it it;
12228 struct glyph_row *last_text_row = NULL;
12229
12230 /* Make POS the new window start. */
12231 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
12232
12233 /* Mark cursor position as unknown. No overlay arrow seen. */
12234 w->cursor.vpos = -1;
12235 overlay_arrow_seen = 0;
12236
12237 /* Initialize iterator and info to start at POS. */
12238 start_display (&it, w, pos);
12239
12240 /* Display all lines of W. */
12241 while (it.current_y < it.last_visible_y)
12242 {
12243 if (display_line (&it))
12244 last_text_row = it.glyph_row - 1;
12245 if (fonts_changed_p)
12246 return 0;
12247 }
12248
12249 /* If bottom moved off end of frame, change mode line percentage. */
12250 if (XFASTINT (w->window_end_pos) <= 0
12251 && Z != IT_CHARPOS (it))
12252 w->update_mode_line = Qt;
12253
12254 /* Set window_end_pos to the offset of the last character displayed
12255 on the window from the end of current_buffer. Set
12256 window_end_vpos to its row number. */
12257 if (last_text_row)
12258 {
12259 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
12260 w->window_end_bytepos
12261 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
12262 w->window_end_pos
12263 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
12264 w->window_end_vpos
12265 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
12266 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
12267 ->displays_text_p);
12268 }
12269 else
12270 {
12271 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
12272 w->window_end_pos = make_number (Z - ZV);
12273 w->window_end_vpos = make_number (0);
12274 }
12275
12276 /* But that is not valid info until redisplay finishes. */
12277 w->window_end_valid = Qnil;
12278 return 1;
12279 }
12280
12281
12282 \f
12283 /************************************************************************
12284 Window redisplay reusing current matrix when buffer has not changed
12285 ************************************************************************/
12286
12287 /* Try redisplay of window W showing an unchanged buffer with a
12288 different window start than the last time it was displayed by
12289 reusing its current matrix. Value is non-zero if successful.
12290 W->start is the new window start. */
12291
12292 static int
12293 try_window_reusing_current_matrix (w)
12294 struct window *w;
12295 {
12296 struct frame *f = XFRAME (w->frame);
12297 struct glyph_row *row, *bottom_row;
12298 struct it it;
12299 struct run run;
12300 struct text_pos start, new_start;
12301 int nrows_scrolled, i;
12302 struct glyph_row *last_text_row;
12303 struct glyph_row *last_reused_text_row;
12304 struct glyph_row *start_row;
12305 int start_vpos, min_y, max_y;
12306
12307 #if GLYPH_DEBUG
12308 if (inhibit_try_window_reusing)
12309 return 0;
12310 #endif
12311
12312 if (/* This function doesn't handle terminal frames. */
12313 !FRAME_WINDOW_P (f)
12314 /* Don't try to reuse the display if windows have been split
12315 or such. */
12316 || windows_or_buffers_changed
12317 || cursor_type_changed)
12318 return 0;
12319
12320 /* Can't do this if region may have changed. */
12321 if ((!NILP (Vtransient_mark_mode)
12322 && !NILP (current_buffer->mark_active))
12323 || !NILP (w->region_showing)
12324 || !NILP (Vshow_trailing_whitespace))
12325 return 0;
12326
12327 /* If top-line visibility has changed, give up. */
12328 if (WINDOW_WANTS_HEADER_LINE_P (w)
12329 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
12330 return 0;
12331
12332 /* Give up if old or new display is scrolled vertically. We could
12333 make this function handle this, but right now it doesn't. */
12334 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
12335 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (start_row))
12336 return 0;
12337
12338 /* The variable new_start now holds the new window start. The old
12339 start `start' can be determined from the current matrix. */
12340 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
12341 start = start_row->start.pos;
12342 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
12343
12344 /* Clear the desired matrix for the display below. */
12345 clear_glyph_matrix (w->desired_matrix);
12346
12347 if (CHARPOS (new_start) <= CHARPOS (start))
12348 {
12349 int first_row_y;
12350
12351 /* Don't use this method if the display starts with an ellipsis
12352 displayed for invisible text. It's not easy to handle that case
12353 below, and it's certainly not worth the effort since this is
12354 not a frequent case. */
12355 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
12356 return 0;
12357
12358 IF_DEBUG (debug_method_add (w, "twu1"));
12359
12360 /* Display up to a row that can be reused. The variable
12361 last_text_row is set to the last row displayed that displays
12362 text. Note that it.vpos == 0 if or if not there is a
12363 header-line; it's not the same as the MATRIX_ROW_VPOS! */
12364 start_display (&it, w, new_start);
12365 first_row_y = it.current_y;
12366 w->cursor.vpos = -1;
12367 last_text_row = last_reused_text_row = NULL;
12368
12369 while (it.current_y < it.last_visible_y
12370 && IT_CHARPOS (it) < CHARPOS (start)
12371 && !fonts_changed_p)
12372 if (display_line (&it))
12373 last_text_row = it.glyph_row - 1;
12374
12375 /* A value of current_y < last_visible_y means that we stopped
12376 at the previous window start, which in turn means that we
12377 have at least one reusable row. */
12378 if (it.current_y < it.last_visible_y)
12379 {
12380 /* IT.vpos always starts from 0; it counts text lines. */
12381 nrows_scrolled = it.vpos;
12382
12383 /* Find PT if not already found in the lines displayed. */
12384 if (w->cursor.vpos < 0)
12385 {
12386 int dy = it.current_y - first_row_y;
12387
12388 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
12389 row = row_containing_pos (w, PT, row, NULL, dy);
12390 if (row)
12391 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
12392 dy, nrows_scrolled);
12393 else
12394 {
12395 clear_glyph_matrix (w->desired_matrix);
12396 return 0;
12397 }
12398 }
12399
12400 /* Scroll the display. Do it before the current matrix is
12401 changed. The problem here is that update has not yet
12402 run, i.e. part of the current matrix is not up to date.
12403 scroll_run_hook will clear the cursor, and use the
12404 current matrix to get the height of the row the cursor is
12405 in. */
12406 run.current_y = first_row_y;
12407 run.desired_y = it.current_y;
12408 run.height = it.last_visible_y - it.current_y;
12409
12410 if (run.height > 0 && run.current_y != run.desired_y)
12411 {
12412 update_begin (f);
12413 rif->update_window_begin_hook (w);
12414 rif->clear_window_mouse_face (w);
12415 rif->scroll_run_hook (w, &run);
12416 rif->update_window_end_hook (w, 0, 0);
12417 update_end (f);
12418 }
12419
12420 /* Shift current matrix down by nrows_scrolled lines. */
12421 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
12422 rotate_matrix (w->current_matrix,
12423 start_vpos,
12424 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
12425 nrows_scrolled);
12426
12427 /* Disable lines that must be updated. */
12428 for (i = 0; i < it.vpos; ++i)
12429 (start_row + i)->enabled_p = 0;
12430
12431 /* Re-compute Y positions. */
12432 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
12433 max_y = it.last_visible_y;
12434 for (row = start_row + nrows_scrolled;
12435 row < bottom_row;
12436 ++row)
12437 {
12438 row->y = it.current_y;
12439 row->visible_height = row->height;
12440
12441 if (row->y < min_y)
12442 row->visible_height -= min_y - row->y;
12443 if (row->y + row->height > max_y)
12444 row->visible_height -= row->y + row->height - max_y;
12445 row->redraw_fringe_bitmaps_p = 1;
12446
12447 it.current_y += row->height;
12448
12449 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
12450 last_reused_text_row = row;
12451 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
12452 break;
12453 }
12454
12455 /* Disable lines in the current matrix which are now
12456 below the window. */
12457 for (++row; row < bottom_row; ++row)
12458 row->enabled_p = 0;
12459 }
12460
12461 /* Update window_end_pos etc.; last_reused_text_row is the last
12462 reused row from the current matrix containing text, if any.
12463 The value of last_text_row is the last displayed line
12464 containing text. */
12465 if (last_reused_text_row)
12466 {
12467 w->window_end_bytepos
12468 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
12469 w->window_end_pos
12470 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
12471 w->window_end_vpos
12472 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
12473 w->current_matrix));
12474 }
12475 else if (last_text_row)
12476 {
12477 w->window_end_bytepos
12478 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
12479 w->window_end_pos
12480 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
12481 w->window_end_vpos
12482 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
12483 }
12484 else
12485 {
12486 /* This window must be completely empty. */
12487 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
12488 w->window_end_pos = make_number (Z - ZV);
12489 w->window_end_vpos = make_number (0);
12490 }
12491 w->window_end_valid = Qnil;
12492
12493 /* Update hint: don't try scrolling again in update_window. */
12494 w->desired_matrix->no_scrolling_p = 1;
12495
12496 #if GLYPH_DEBUG
12497 debug_method_add (w, "try_window_reusing_current_matrix 1");
12498 #endif
12499 return 1;
12500 }
12501 else if (CHARPOS (new_start) > CHARPOS (start))
12502 {
12503 struct glyph_row *pt_row, *row;
12504 struct glyph_row *first_reusable_row;
12505 struct glyph_row *first_row_to_display;
12506 int dy;
12507 int yb = window_text_bottom_y (w);
12508
12509 /* Find the row starting at new_start, if there is one. Don't
12510 reuse a partially visible line at the end. */
12511 first_reusable_row = start_row;
12512 while (first_reusable_row->enabled_p
12513 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
12514 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
12515 < CHARPOS (new_start)))
12516 ++first_reusable_row;
12517
12518 /* Give up if there is no row to reuse. */
12519 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
12520 || !first_reusable_row->enabled_p
12521 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
12522 != CHARPOS (new_start)))
12523 return 0;
12524
12525 /* We can reuse fully visible rows beginning with
12526 first_reusable_row to the end of the window. Set
12527 first_row_to_display to the first row that cannot be reused.
12528 Set pt_row to the row containing point, if there is any. */
12529 pt_row = NULL;
12530 for (first_row_to_display = first_reusable_row;
12531 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
12532 ++first_row_to_display)
12533 {
12534 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
12535 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
12536 pt_row = first_row_to_display;
12537 }
12538
12539 /* Start displaying at the start of first_row_to_display. */
12540 xassert (first_row_to_display->y < yb);
12541 init_to_row_start (&it, w, first_row_to_display);
12542
12543 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
12544 - start_vpos);
12545 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
12546 - nrows_scrolled);
12547 it.current_y = (first_row_to_display->y - first_reusable_row->y
12548 + WINDOW_HEADER_LINE_HEIGHT (w));
12549
12550 /* Display lines beginning with first_row_to_display in the
12551 desired matrix. Set last_text_row to the last row displayed
12552 that displays text. */
12553 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
12554 if (pt_row == NULL)
12555 w->cursor.vpos = -1;
12556 last_text_row = NULL;
12557 while (it.current_y < it.last_visible_y && !fonts_changed_p)
12558 if (display_line (&it))
12559 last_text_row = it.glyph_row - 1;
12560
12561 /* Give up If point isn't in a row displayed or reused. */
12562 if (w->cursor.vpos < 0)
12563 {
12564 clear_glyph_matrix (w->desired_matrix);
12565 return 0;
12566 }
12567
12568 /* If point is in a reused row, adjust y and vpos of the cursor
12569 position. */
12570 if (pt_row)
12571 {
12572 w->cursor.vpos -= nrows_scrolled;
12573 w->cursor.y -= first_reusable_row->y - start_row->y;
12574 }
12575
12576 /* Scroll the display. */
12577 run.current_y = first_reusable_row->y;
12578 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
12579 run.height = it.last_visible_y - run.current_y;
12580 dy = run.current_y - run.desired_y;
12581
12582 if (run.height)
12583 {
12584 update_begin (f);
12585 rif->update_window_begin_hook (w);
12586 rif->clear_window_mouse_face (w);
12587 rif->scroll_run_hook (w, &run);
12588 rif->update_window_end_hook (w, 0, 0);
12589 update_end (f);
12590 }
12591
12592 /* Adjust Y positions of reused rows. */
12593 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
12594 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
12595 max_y = it.last_visible_y;
12596 for (row = first_reusable_row; row < first_row_to_display; ++row)
12597 {
12598 row->y -= dy;
12599 row->visible_height = row->height;
12600 if (row->y < min_y)
12601 row->visible_height -= min_y - row->y;
12602 if (row->y + row->height > max_y)
12603 row->visible_height -= row->y + row->height - max_y;
12604 row->redraw_fringe_bitmaps_p = 1;
12605 }
12606
12607 /* Scroll the current matrix. */
12608 xassert (nrows_scrolled > 0);
12609 rotate_matrix (w->current_matrix,
12610 start_vpos,
12611 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
12612 -nrows_scrolled);
12613
12614 /* Disable rows not reused. */
12615 for (row -= nrows_scrolled; row < bottom_row; ++row)
12616 row->enabled_p = 0;
12617
12618 /* Point may have moved to a different line, so we cannot assume that
12619 the previous cursor position is valid; locate the correct row. */
12620 if (pt_row)
12621 {
12622 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
12623 row < bottom_row && PT >= MATRIX_ROW_END_CHARPOS (row);
12624 row++)
12625 {
12626 w->cursor.vpos++;
12627 w->cursor.y = row->y;
12628 }
12629 if (row < bottom_row)
12630 {
12631 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
12632 while (glyph->charpos < PT)
12633 {
12634 w->cursor.hpos++;
12635 w->cursor.x += glyph->pixel_width;
12636 glyph++;
12637 }
12638 }
12639 }
12640
12641 /* Adjust window end. A null value of last_text_row means that
12642 the window end is in reused rows which in turn means that
12643 only its vpos can have changed. */
12644 if (last_text_row)
12645 {
12646 w->window_end_bytepos
12647 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
12648 w->window_end_pos
12649 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
12650 w->window_end_vpos
12651 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
12652 }
12653 else
12654 {
12655 w->window_end_vpos
12656 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
12657 }
12658
12659 w->window_end_valid = Qnil;
12660 w->desired_matrix->no_scrolling_p = 1;
12661
12662 #if GLYPH_DEBUG
12663 debug_method_add (w, "try_window_reusing_current_matrix 2");
12664 #endif
12665 return 1;
12666 }
12667
12668 return 0;
12669 }
12670
12671
12672 \f
12673 /************************************************************************
12674 Window redisplay reusing current matrix when buffer has changed
12675 ************************************************************************/
12676
12677 static struct glyph_row *find_last_unchanged_at_beg_row P_ ((struct window *));
12678 static struct glyph_row *find_first_unchanged_at_end_row P_ ((struct window *,
12679 int *, int *));
12680 static struct glyph_row *
12681 find_last_row_displaying_text P_ ((struct glyph_matrix *, struct it *,
12682 struct glyph_row *));
12683
12684
12685 /* Return the last row in MATRIX displaying text. If row START is
12686 non-null, start searching with that row. IT gives the dimensions
12687 of the display. Value is null if matrix is empty; otherwise it is
12688 a pointer to the row found. */
12689
12690 static struct glyph_row *
12691 find_last_row_displaying_text (matrix, it, start)
12692 struct glyph_matrix *matrix;
12693 struct it *it;
12694 struct glyph_row *start;
12695 {
12696 struct glyph_row *row, *row_found;
12697
12698 /* Set row_found to the last row in IT->w's current matrix
12699 displaying text. The loop looks funny but think of partially
12700 visible lines. */
12701 row_found = NULL;
12702 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
12703 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
12704 {
12705 xassert (row->enabled_p);
12706 row_found = row;
12707 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
12708 break;
12709 ++row;
12710 }
12711
12712 return row_found;
12713 }
12714
12715
12716 /* Return the last row in the current matrix of W that is not affected
12717 by changes at the start of current_buffer that occurred since W's
12718 current matrix was built. Value is null if no such row exists.
12719
12720 BEG_UNCHANGED us the number of characters unchanged at the start of
12721 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
12722 first changed character in current_buffer. Characters at positions <
12723 BEG + BEG_UNCHANGED are at the same buffer positions as they were
12724 when the current matrix was built. */
12725
12726 static struct glyph_row *
12727 find_last_unchanged_at_beg_row (w)
12728 struct window *w;
12729 {
12730 int first_changed_pos = BEG + BEG_UNCHANGED;
12731 struct glyph_row *row;
12732 struct glyph_row *row_found = NULL;
12733 int yb = window_text_bottom_y (w);
12734
12735 /* Find the last row displaying unchanged text. */
12736 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
12737 while (MATRIX_ROW_DISPLAYS_TEXT_P (row)
12738 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos)
12739 {
12740 if (/* If row ends before first_changed_pos, it is unchanged,
12741 except in some case. */
12742 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
12743 /* When row ends in ZV and we write at ZV it is not
12744 unchanged. */
12745 && !row->ends_at_zv_p
12746 /* When first_changed_pos is the end of a continued line,
12747 row is not unchanged because it may be no longer
12748 continued. */
12749 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
12750 && (row->continued_p
12751 || row->exact_window_width_line_p)))
12752 row_found = row;
12753
12754 /* Stop if last visible row. */
12755 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
12756 break;
12757
12758 ++row;
12759 }
12760
12761 return row_found;
12762 }
12763
12764
12765 /* Find the first glyph row in the current matrix of W that is not
12766 affected by changes at the end of current_buffer since the
12767 time W's current matrix was built.
12768
12769 Return in *DELTA the number of chars by which buffer positions in
12770 unchanged text at the end of current_buffer must be adjusted.
12771
12772 Return in *DELTA_BYTES the corresponding number of bytes.
12773
12774 Value is null if no such row exists, i.e. all rows are affected by
12775 changes. */
12776
12777 static struct glyph_row *
12778 find_first_unchanged_at_end_row (w, delta, delta_bytes)
12779 struct window *w;
12780 int *delta, *delta_bytes;
12781 {
12782 struct glyph_row *row;
12783 struct glyph_row *row_found = NULL;
12784
12785 *delta = *delta_bytes = 0;
12786
12787 /* Display must not have been paused, otherwise the current matrix
12788 is not up to date. */
12789 if (NILP (w->window_end_valid))
12790 abort ();
12791
12792 /* A value of window_end_pos >= END_UNCHANGED means that the window
12793 end is in the range of changed text. If so, there is no
12794 unchanged row at the end of W's current matrix. */
12795 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
12796 return NULL;
12797
12798 /* Set row to the last row in W's current matrix displaying text. */
12799 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
12800
12801 /* If matrix is entirely empty, no unchanged row exists. */
12802 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
12803 {
12804 /* The value of row is the last glyph row in the matrix having a
12805 meaningful buffer position in it. The end position of row
12806 corresponds to window_end_pos. This allows us to translate
12807 buffer positions in the current matrix to current buffer
12808 positions for characters not in changed text. */
12809 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
12810 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
12811 int last_unchanged_pos, last_unchanged_pos_old;
12812 struct glyph_row *first_text_row
12813 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
12814
12815 *delta = Z - Z_old;
12816 *delta_bytes = Z_BYTE - Z_BYTE_old;
12817
12818 /* Set last_unchanged_pos to the buffer position of the last
12819 character in the buffer that has not been changed. Z is the
12820 index + 1 of the last character in current_buffer, i.e. by
12821 subtracting END_UNCHANGED we get the index of the last
12822 unchanged character, and we have to add BEG to get its buffer
12823 position. */
12824 last_unchanged_pos = Z - END_UNCHANGED + BEG;
12825 last_unchanged_pos_old = last_unchanged_pos - *delta;
12826
12827 /* Search backward from ROW for a row displaying a line that
12828 starts at a minimum position >= last_unchanged_pos_old. */
12829 for (; row > first_text_row; --row)
12830 {
12831 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
12832 abort ();
12833
12834 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
12835 row_found = row;
12836 }
12837 }
12838
12839 if (row_found && !MATRIX_ROW_DISPLAYS_TEXT_P (row_found))
12840 abort ();
12841
12842 return row_found;
12843 }
12844
12845
12846 /* Make sure that glyph rows in the current matrix of window W
12847 reference the same glyph memory as corresponding rows in the
12848 frame's frame matrix. This function is called after scrolling W's
12849 current matrix on a terminal frame in try_window_id and
12850 try_window_reusing_current_matrix. */
12851
12852 static void
12853 sync_frame_with_window_matrix_rows (w)
12854 struct window *w;
12855 {
12856 struct frame *f = XFRAME (w->frame);
12857 struct glyph_row *window_row, *window_row_end, *frame_row;
12858
12859 /* Preconditions: W must be a leaf window and full-width. Its frame
12860 must have a frame matrix. */
12861 xassert (NILP (w->hchild) && NILP (w->vchild));
12862 xassert (WINDOW_FULL_WIDTH_P (w));
12863 xassert (!FRAME_WINDOW_P (f));
12864
12865 /* If W is a full-width window, glyph pointers in W's current matrix
12866 have, by definition, to be the same as glyph pointers in the
12867 corresponding frame matrix. Note that frame matrices have no
12868 marginal areas (see build_frame_matrix). */
12869 window_row = w->current_matrix->rows;
12870 window_row_end = window_row + w->current_matrix->nrows;
12871 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
12872 while (window_row < window_row_end)
12873 {
12874 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
12875 struct glyph *end = window_row->glyphs[LAST_AREA];
12876
12877 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
12878 frame_row->glyphs[TEXT_AREA] = start;
12879 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
12880 frame_row->glyphs[LAST_AREA] = end;
12881
12882 /* Disable frame rows whose corresponding window rows have
12883 been disabled in try_window_id. */
12884 if (!window_row->enabled_p)
12885 frame_row->enabled_p = 0;
12886
12887 ++window_row, ++frame_row;
12888 }
12889 }
12890
12891
12892 /* Find the glyph row in window W containing CHARPOS. Consider all
12893 rows between START and END (not inclusive). END null means search
12894 all rows to the end of the display area of W. Value is the row
12895 containing CHARPOS or null. */
12896
12897 struct glyph_row *
12898 row_containing_pos (w, charpos, start, end, dy)
12899 struct window *w;
12900 int charpos;
12901 struct glyph_row *start, *end;
12902 int dy;
12903 {
12904 struct glyph_row *row = start;
12905 int last_y;
12906
12907 /* If we happen to start on a header-line, skip that. */
12908 if (row->mode_line_p)
12909 ++row;
12910
12911 if ((end && row >= end) || !row->enabled_p)
12912 return NULL;
12913
12914 last_y = window_text_bottom_y (w) - dy;
12915
12916 while (1)
12917 {
12918 /* Give up if we have gone too far. */
12919 if (end && row >= end)
12920 return NULL;
12921 /* This formerly returned if they were equal.
12922 I think that both quantities are of a "last plus one" type;
12923 if so, when they are equal, the row is within the screen. -- rms. */
12924 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
12925 return NULL;
12926
12927 /* If it is in this row, return this row. */
12928 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
12929 || (MATRIX_ROW_END_CHARPOS (row) == charpos
12930 /* The end position of a row equals the start
12931 position of the next row. If CHARPOS is there, we
12932 would rather display it in the next line, except
12933 when this line ends in ZV. */
12934 && !row->ends_at_zv_p
12935 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
12936 && charpos >= MATRIX_ROW_START_CHARPOS (row))
12937 return row;
12938 ++row;
12939 }
12940 }
12941
12942
12943 /* Try to redisplay window W by reusing its existing display. W's
12944 current matrix must be up to date when this function is called,
12945 i.e. window_end_valid must not be nil.
12946
12947 Value is
12948
12949 1 if display has been updated
12950 0 if otherwise unsuccessful
12951 -1 if redisplay with same window start is known not to succeed
12952
12953 The following steps are performed:
12954
12955 1. Find the last row in the current matrix of W that is not
12956 affected by changes at the start of current_buffer. If no such row
12957 is found, give up.
12958
12959 2. Find the first row in W's current matrix that is not affected by
12960 changes at the end of current_buffer. Maybe there is no such row.
12961
12962 3. Display lines beginning with the row + 1 found in step 1 to the
12963 row found in step 2 or, if step 2 didn't find a row, to the end of
12964 the window.
12965
12966 4. If cursor is not known to appear on the window, give up.
12967
12968 5. If display stopped at the row found in step 2, scroll the
12969 display and current matrix as needed.
12970
12971 6. Maybe display some lines at the end of W, if we must. This can
12972 happen under various circumstances, like a partially visible line
12973 becoming fully visible, or because newly displayed lines are displayed
12974 in smaller font sizes.
12975
12976 7. Update W's window end information. */
12977
12978 static int
12979 try_window_id (w)
12980 struct window *w;
12981 {
12982 struct frame *f = XFRAME (w->frame);
12983 struct glyph_matrix *current_matrix = w->current_matrix;
12984 struct glyph_matrix *desired_matrix = w->desired_matrix;
12985 struct glyph_row *last_unchanged_at_beg_row;
12986 struct glyph_row *first_unchanged_at_end_row;
12987 struct glyph_row *row;
12988 struct glyph_row *bottom_row;
12989 int bottom_vpos;
12990 struct it it;
12991 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
12992 struct text_pos start_pos;
12993 struct run run;
12994 int first_unchanged_at_end_vpos = 0;
12995 struct glyph_row *last_text_row, *last_text_row_at_end;
12996 struct text_pos start;
12997 int first_changed_charpos, last_changed_charpos;
12998
12999 #if GLYPH_DEBUG
13000 if (inhibit_try_window_id)
13001 return 0;
13002 #endif
13003
13004 /* This is handy for debugging. */
13005 #if 0
13006 #define GIVE_UP(X) \
13007 do { \
13008 fprintf (stderr, "try_window_id give up %d\n", (X)); \
13009 return 0; \
13010 } while (0)
13011 #else
13012 #define GIVE_UP(X) return 0
13013 #endif
13014
13015 SET_TEXT_POS_FROM_MARKER (start, w->start);
13016
13017 /* Don't use this for mini-windows because these can show
13018 messages and mini-buffers, and we don't handle that here. */
13019 if (MINI_WINDOW_P (w))
13020 GIVE_UP (1);
13021
13022 /* This flag is used to prevent redisplay optimizations. */
13023 if (windows_or_buffers_changed || cursor_type_changed)
13024 GIVE_UP (2);
13025
13026 /* Verify that narrowing has not changed.
13027 Also verify that we were not told to prevent redisplay optimizations.
13028 It would be nice to further
13029 reduce the number of cases where this prevents try_window_id. */
13030 if (current_buffer->clip_changed
13031 || current_buffer->prevent_redisplay_optimizations_p)
13032 GIVE_UP (3);
13033
13034 /* Window must either use window-based redisplay or be full width. */
13035 if (!FRAME_WINDOW_P (f)
13036 && (!line_ins_del_ok
13037 || !WINDOW_FULL_WIDTH_P (w)))
13038 GIVE_UP (4);
13039
13040 /* Give up if point is not known NOT to appear in W. */
13041 if (PT < CHARPOS (start))
13042 GIVE_UP (5);
13043
13044 /* Another way to prevent redisplay optimizations. */
13045 if (XFASTINT (w->last_modified) == 0)
13046 GIVE_UP (6);
13047
13048 /* Verify that window is not hscrolled. */
13049 if (XFASTINT (w->hscroll) != 0)
13050 GIVE_UP (7);
13051
13052 /* Verify that display wasn't paused. */
13053 if (NILP (w->window_end_valid))
13054 GIVE_UP (8);
13055
13056 /* Can't use this if highlighting a region because a cursor movement
13057 will do more than just set the cursor. */
13058 if (!NILP (Vtransient_mark_mode)
13059 && !NILP (current_buffer->mark_active))
13060 GIVE_UP (9);
13061
13062 /* Likewise if highlighting trailing whitespace. */
13063 if (!NILP (Vshow_trailing_whitespace))
13064 GIVE_UP (11);
13065
13066 /* Likewise if showing a region. */
13067 if (!NILP (w->region_showing))
13068 GIVE_UP (10);
13069
13070 /* Can use this if overlay arrow position and or string have changed. */
13071 if (overlay_arrows_changed_p ())
13072 GIVE_UP (12);
13073
13074
13075 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
13076 only if buffer has really changed. The reason is that the gap is
13077 initially at Z for freshly visited files. The code below would
13078 set end_unchanged to 0 in that case. */
13079 if (MODIFF > SAVE_MODIFF
13080 /* This seems to happen sometimes after saving a buffer. */
13081 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
13082 {
13083 if (GPT - BEG < BEG_UNCHANGED)
13084 BEG_UNCHANGED = GPT - BEG;
13085 if (Z - GPT < END_UNCHANGED)
13086 END_UNCHANGED = Z - GPT;
13087 }
13088
13089 /* The position of the first and last character that has been changed. */
13090 first_changed_charpos = BEG + BEG_UNCHANGED;
13091 last_changed_charpos = Z - END_UNCHANGED;
13092
13093 /* If window starts after a line end, and the last change is in
13094 front of that newline, then changes don't affect the display.
13095 This case happens with stealth-fontification. Note that although
13096 the display is unchanged, glyph positions in the matrix have to
13097 be adjusted, of course. */
13098 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
13099 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
13100 && ((last_changed_charpos < CHARPOS (start)
13101 && CHARPOS (start) == BEGV)
13102 || (last_changed_charpos < CHARPOS (start) - 1
13103 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
13104 {
13105 int Z_old, delta, Z_BYTE_old, delta_bytes;
13106 struct glyph_row *r0;
13107
13108 /* Compute how many chars/bytes have been added to or removed
13109 from the buffer. */
13110 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
13111 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
13112 delta = Z - Z_old;
13113 delta_bytes = Z_BYTE - Z_BYTE_old;
13114
13115 /* Give up if PT is not in the window. Note that it already has
13116 been checked at the start of try_window_id that PT is not in
13117 front of the window start. */
13118 if (PT >= MATRIX_ROW_END_CHARPOS (row) + delta)
13119 GIVE_UP (13);
13120
13121 /* If window start is unchanged, we can reuse the whole matrix
13122 as is, after adjusting glyph positions. No need to compute
13123 the window end again, since its offset from Z hasn't changed. */
13124 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
13125 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + delta
13126 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + delta_bytes
13127 /* PT must not be in a partially visible line. */
13128 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + delta
13129 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
13130 {
13131 /* Adjust positions in the glyph matrix. */
13132 if (delta || delta_bytes)
13133 {
13134 struct glyph_row *r1
13135 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
13136 increment_matrix_positions (w->current_matrix,
13137 MATRIX_ROW_VPOS (r0, current_matrix),
13138 MATRIX_ROW_VPOS (r1, current_matrix),
13139 delta, delta_bytes);
13140 }
13141
13142 /* Set the cursor. */
13143 row = row_containing_pos (w, PT, r0, NULL, 0);
13144 if (row)
13145 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
13146 else
13147 abort ();
13148 return 1;
13149 }
13150 }
13151
13152 /* Handle the case that changes are all below what is displayed in
13153 the window, and that PT is in the window. This shortcut cannot
13154 be taken if ZV is visible in the window, and text has been added
13155 there that is visible in the window. */
13156 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
13157 /* ZV is not visible in the window, or there are no
13158 changes at ZV, actually. */
13159 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
13160 || first_changed_charpos == last_changed_charpos))
13161 {
13162 struct glyph_row *r0;
13163
13164 /* Give up if PT is not in the window. Note that it already has
13165 been checked at the start of try_window_id that PT is not in
13166 front of the window start. */
13167 if (PT >= MATRIX_ROW_END_CHARPOS (row))
13168 GIVE_UP (14);
13169
13170 /* If window start is unchanged, we can reuse the whole matrix
13171 as is, without changing glyph positions since no text has
13172 been added/removed in front of the window end. */
13173 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
13174 if (TEXT_POS_EQUAL_P (start, r0->start.pos)
13175 /* PT must not be in a partially visible line. */
13176 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
13177 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
13178 {
13179 /* We have to compute the window end anew since text
13180 can have been added/removed after it. */
13181 w->window_end_pos
13182 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
13183 w->window_end_bytepos
13184 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
13185
13186 /* Set the cursor. */
13187 row = row_containing_pos (w, PT, r0, NULL, 0);
13188 if (row)
13189 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
13190 else
13191 abort ();
13192 return 2;
13193 }
13194 }
13195
13196 /* Give up if window start is in the changed area.
13197
13198 The condition used to read
13199
13200 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
13201
13202 but why that was tested escapes me at the moment. */
13203 if (CHARPOS (start) >= first_changed_charpos
13204 && CHARPOS (start) <= last_changed_charpos)
13205 GIVE_UP (15);
13206
13207 /* Check that window start agrees with the start of the first glyph
13208 row in its current matrix. Check this after we know the window
13209 start is not in changed text, otherwise positions would not be
13210 comparable. */
13211 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
13212 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
13213 GIVE_UP (16);
13214
13215 /* Give up if the window ends in strings. Overlay strings
13216 at the end are difficult to handle, so don't try. */
13217 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
13218 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
13219 GIVE_UP (20);
13220
13221 /* Compute the position at which we have to start displaying new
13222 lines. Some of the lines at the top of the window might be
13223 reusable because they are not displaying changed text. Find the
13224 last row in W's current matrix not affected by changes at the
13225 start of current_buffer. Value is null if changes start in the
13226 first line of window. */
13227 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
13228 if (last_unchanged_at_beg_row)
13229 {
13230 /* Avoid starting to display in the moddle of a character, a TAB
13231 for instance. This is easier than to set up the iterator
13232 exactly, and it's not a frequent case, so the additional
13233 effort wouldn't really pay off. */
13234 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
13235 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
13236 && last_unchanged_at_beg_row > w->current_matrix->rows)
13237 --last_unchanged_at_beg_row;
13238
13239 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
13240 GIVE_UP (17);
13241
13242 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
13243 GIVE_UP (18);
13244 start_pos = it.current.pos;
13245
13246 /* Start displaying new lines in the desired matrix at the same
13247 vpos we would use in the current matrix, i.e. below
13248 last_unchanged_at_beg_row. */
13249 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
13250 current_matrix);
13251 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
13252 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
13253
13254 xassert (it.hpos == 0 && it.current_x == 0);
13255 }
13256 else
13257 {
13258 /* There are no reusable lines at the start of the window.
13259 Start displaying in the first text line. */
13260 start_display (&it, w, start);
13261 it.vpos = it.first_vpos;
13262 start_pos = it.current.pos;
13263 }
13264
13265 /* Find the first row that is not affected by changes at the end of
13266 the buffer. Value will be null if there is no unchanged row, in
13267 which case we must redisplay to the end of the window. delta
13268 will be set to the value by which buffer positions beginning with
13269 first_unchanged_at_end_row have to be adjusted due to text
13270 changes. */
13271 first_unchanged_at_end_row
13272 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
13273 IF_DEBUG (debug_delta = delta);
13274 IF_DEBUG (debug_delta_bytes = delta_bytes);
13275
13276 /* Set stop_pos to the buffer position up to which we will have to
13277 display new lines. If first_unchanged_at_end_row != NULL, this
13278 is the buffer position of the start of the line displayed in that
13279 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
13280 that we don't stop at a buffer position. */
13281 stop_pos = 0;
13282 if (first_unchanged_at_end_row)
13283 {
13284 xassert (last_unchanged_at_beg_row == NULL
13285 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
13286
13287 /* If this is a continuation line, move forward to the next one
13288 that isn't. Changes in lines above affect this line.
13289 Caution: this may move first_unchanged_at_end_row to a row
13290 not displaying text. */
13291 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
13292 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
13293 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
13294 < it.last_visible_y))
13295 ++first_unchanged_at_end_row;
13296
13297 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
13298 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
13299 >= it.last_visible_y))
13300 first_unchanged_at_end_row = NULL;
13301 else
13302 {
13303 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
13304 + delta);
13305 first_unchanged_at_end_vpos
13306 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
13307 xassert (stop_pos >= Z - END_UNCHANGED);
13308 }
13309 }
13310 else if (last_unchanged_at_beg_row == NULL)
13311 GIVE_UP (19);
13312
13313
13314 #if GLYPH_DEBUG
13315
13316 /* Either there is no unchanged row at the end, or the one we have
13317 now displays text. This is a necessary condition for the window
13318 end pos calculation at the end of this function. */
13319 xassert (first_unchanged_at_end_row == NULL
13320 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
13321
13322 debug_last_unchanged_at_beg_vpos
13323 = (last_unchanged_at_beg_row
13324 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
13325 : -1);
13326 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
13327
13328 #endif /* GLYPH_DEBUG != 0 */
13329
13330
13331 /* Display new lines. Set last_text_row to the last new line
13332 displayed which has text on it, i.e. might end up as being the
13333 line where the window_end_vpos is. */
13334 w->cursor.vpos = -1;
13335 last_text_row = NULL;
13336 overlay_arrow_seen = 0;
13337 while (it.current_y < it.last_visible_y
13338 && !fonts_changed_p
13339 && (first_unchanged_at_end_row == NULL
13340 || IT_CHARPOS (it) < stop_pos))
13341 {
13342 if (display_line (&it))
13343 last_text_row = it.glyph_row - 1;
13344 }
13345
13346 if (fonts_changed_p)
13347 return -1;
13348
13349
13350 /* Compute differences in buffer positions, y-positions etc. for
13351 lines reused at the bottom of the window. Compute what we can
13352 scroll. */
13353 if (first_unchanged_at_end_row
13354 /* No lines reused because we displayed everything up to the
13355 bottom of the window. */
13356 && it.current_y < it.last_visible_y)
13357 {
13358 dvpos = (it.vpos
13359 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
13360 current_matrix));
13361 dy = it.current_y - first_unchanged_at_end_row->y;
13362 run.current_y = first_unchanged_at_end_row->y;
13363 run.desired_y = run.current_y + dy;
13364 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
13365 }
13366 else
13367 {
13368 delta = dvpos = dy = run.current_y = run.desired_y = run.height = 0;
13369 first_unchanged_at_end_row = NULL;
13370 }
13371 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
13372
13373
13374 /* Find the cursor if not already found. We have to decide whether
13375 PT will appear on this window (it sometimes doesn't, but this is
13376 not a very frequent case.) This decision has to be made before
13377 the current matrix is altered. A value of cursor.vpos < 0 means
13378 that PT is either in one of the lines beginning at
13379 first_unchanged_at_end_row or below the window. Don't care for
13380 lines that might be displayed later at the window end; as
13381 mentioned, this is not a frequent case. */
13382 if (w->cursor.vpos < 0)
13383 {
13384 /* Cursor in unchanged rows at the top? */
13385 if (PT < CHARPOS (start_pos)
13386 && last_unchanged_at_beg_row)
13387 {
13388 row = row_containing_pos (w, PT,
13389 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
13390 last_unchanged_at_beg_row + 1, 0);
13391 if (row)
13392 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13393 }
13394
13395 /* Start from first_unchanged_at_end_row looking for PT. */
13396 else if (first_unchanged_at_end_row)
13397 {
13398 row = row_containing_pos (w, PT - delta,
13399 first_unchanged_at_end_row, NULL, 0);
13400 if (row)
13401 set_cursor_from_row (w, row, w->current_matrix, delta,
13402 delta_bytes, dy, dvpos);
13403 }
13404
13405 /* Give up if cursor was not found. */
13406 if (w->cursor.vpos < 0)
13407 {
13408 clear_glyph_matrix (w->desired_matrix);
13409 return -1;
13410 }
13411 }
13412
13413 /* Don't let the cursor end in the scroll margins. */
13414 {
13415 int this_scroll_margin, cursor_height;
13416
13417 this_scroll_margin = max (0, scroll_margin);
13418 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
13419 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
13420 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
13421
13422 if ((w->cursor.y < this_scroll_margin
13423 && CHARPOS (start) > BEGV)
13424 /* Old redisplay didn't take scroll margin into account at the bottom,
13425 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
13426 || w->cursor.y + cursor_height + this_scroll_margin > it.last_visible_y)
13427 {
13428 w->cursor.vpos = -1;
13429 clear_glyph_matrix (w->desired_matrix);
13430 return -1;
13431 }
13432 }
13433
13434 /* Scroll the display. Do it before changing the current matrix so
13435 that xterm.c doesn't get confused about where the cursor glyph is
13436 found. */
13437 if (dy && run.height)
13438 {
13439 update_begin (f);
13440
13441 if (FRAME_WINDOW_P (f))
13442 {
13443 rif->update_window_begin_hook (w);
13444 rif->clear_window_mouse_face (w);
13445 rif->scroll_run_hook (w, &run);
13446 rif->update_window_end_hook (w, 0, 0);
13447 }
13448 else
13449 {
13450 /* Terminal frame. In this case, dvpos gives the number of
13451 lines to scroll by; dvpos < 0 means scroll up. */
13452 int first_unchanged_at_end_vpos
13453 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
13454 int from = WINDOW_TOP_EDGE_LINE (w) + first_unchanged_at_end_vpos;
13455 int end = (WINDOW_TOP_EDGE_LINE (w)
13456 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
13457 + window_internal_height (w));
13458
13459 /* Perform the operation on the screen. */
13460 if (dvpos > 0)
13461 {
13462 /* Scroll last_unchanged_at_beg_row to the end of the
13463 window down dvpos lines. */
13464 set_terminal_window (end);
13465
13466 /* On dumb terminals delete dvpos lines at the end
13467 before inserting dvpos empty lines. */
13468 if (!scroll_region_ok)
13469 ins_del_lines (end - dvpos, -dvpos);
13470
13471 /* Insert dvpos empty lines in front of
13472 last_unchanged_at_beg_row. */
13473 ins_del_lines (from, dvpos);
13474 }
13475 else if (dvpos < 0)
13476 {
13477 /* Scroll up last_unchanged_at_beg_vpos to the end of
13478 the window to last_unchanged_at_beg_vpos - |dvpos|. */
13479 set_terminal_window (end);
13480
13481 /* Delete dvpos lines in front of
13482 last_unchanged_at_beg_vpos. ins_del_lines will set
13483 the cursor to the given vpos and emit |dvpos| delete
13484 line sequences. */
13485 ins_del_lines (from + dvpos, dvpos);
13486
13487 /* On a dumb terminal insert dvpos empty lines at the
13488 end. */
13489 if (!scroll_region_ok)
13490 ins_del_lines (end + dvpos, -dvpos);
13491 }
13492
13493 set_terminal_window (0);
13494 }
13495
13496 update_end (f);
13497 }
13498
13499 /* Shift reused rows of the current matrix to the right position.
13500 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
13501 text. */
13502 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
13503 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
13504 if (dvpos < 0)
13505 {
13506 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
13507 bottom_vpos, dvpos);
13508 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
13509 bottom_vpos, 0);
13510 }
13511 else if (dvpos > 0)
13512 {
13513 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
13514 bottom_vpos, dvpos);
13515 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
13516 first_unchanged_at_end_vpos + dvpos, 0);
13517 }
13518
13519 /* For frame-based redisplay, make sure that current frame and window
13520 matrix are in sync with respect to glyph memory. */
13521 if (!FRAME_WINDOW_P (f))
13522 sync_frame_with_window_matrix_rows (w);
13523
13524 /* Adjust buffer positions in reused rows. */
13525 if (delta)
13526 increment_matrix_positions (current_matrix,
13527 first_unchanged_at_end_vpos + dvpos,
13528 bottom_vpos, delta, delta_bytes);
13529
13530 /* Adjust Y positions. */
13531 if (dy)
13532 shift_glyph_matrix (w, current_matrix,
13533 first_unchanged_at_end_vpos + dvpos,
13534 bottom_vpos, dy);
13535
13536 if (first_unchanged_at_end_row)
13537 first_unchanged_at_end_row += dvpos;
13538
13539 /* If scrolling up, there may be some lines to display at the end of
13540 the window. */
13541 last_text_row_at_end = NULL;
13542 if (dy < 0)
13543 {
13544 /* Scrolling up can leave for example a partially visible line
13545 at the end of the window to be redisplayed. */
13546 /* Set last_row to the glyph row in the current matrix where the
13547 window end line is found. It has been moved up or down in
13548 the matrix by dvpos. */
13549 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
13550 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
13551
13552 /* If last_row is the window end line, it should display text. */
13553 xassert (last_row->displays_text_p);
13554
13555 /* If window end line was partially visible before, begin
13556 displaying at that line. Otherwise begin displaying with the
13557 line following it. */
13558 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
13559 {
13560 init_to_row_start (&it, w, last_row);
13561 it.vpos = last_vpos;
13562 it.current_y = last_row->y;
13563 }
13564 else
13565 {
13566 init_to_row_end (&it, w, last_row);
13567 it.vpos = 1 + last_vpos;
13568 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
13569 ++last_row;
13570 }
13571
13572 /* We may start in a continuation line. If so, we have to
13573 get the right continuation_lines_width and current_x. */
13574 it.continuation_lines_width = last_row->continuation_lines_width;
13575 it.hpos = it.current_x = 0;
13576
13577 /* Display the rest of the lines at the window end. */
13578 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
13579 while (it.current_y < it.last_visible_y
13580 && !fonts_changed_p)
13581 {
13582 /* Is it always sure that the display agrees with lines in
13583 the current matrix? I don't think so, so we mark rows
13584 displayed invalid in the current matrix by setting their
13585 enabled_p flag to zero. */
13586 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
13587 if (display_line (&it))
13588 last_text_row_at_end = it.glyph_row - 1;
13589 }
13590 }
13591
13592 /* Update window_end_pos and window_end_vpos. */
13593 if (first_unchanged_at_end_row
13594 && first_unchanged_at_end_row->y < it.last_visible_y
13595 && !last_text_row_at_end)
13596 {
13597 /* Window end line if one of the preserved rows from the current
13598 matrix. Set row to the last row displaying text in current
13599 matrix starting at first_unchanged_at_end_row, after
13600 scrolling. */
13601 xassert (first_unchanged_at_end_row->displays_text_p);
13602 row = find_last_row_displaying_text (w->current_matrix, &it,
13603 first_unchanged_at_end_row);
13604 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
13605
13606 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
13607 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
13608 w->window_end_vpos
13609 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
13610 xassert (w->window_end_bytepos >= 0);
13611 IF_DEBUG (debug_method_add (w, "A"));
13612 }
13613 else if (last_text_row_at_end)
13614 {
13615 w->window_end_pos
13616 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
13617 w->window_end_bytepos
13618 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
13619 w->window_end_vpos
13620 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
13621 xassert (w->window_end_bytepos >= 0);
13622 IF_DEBUG (debug_method_add (w, "B"));
13623 }
13624 else if (last_text_row)
13625 {
13626 /* We have displayed either to the end of the window or at the
13627 end of the window, i.e. the last row with text is to be found
13628 in the desired matrix. */
13629 w->window_end_pos
13630 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
13631 w->window_end_bytepos
13632 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
13633 w->window_end_vpos
13634 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
13635 xassert (w->window_end_bytepos >= 0);
13636 }
13637 else if (first_unchanged_at_end_row == NULL
13638 && last_text_row == NULL
13639 && last_text_row_at_end == NULL)
13640 {
13641 /* Displayed to end of window, but no line containing text was
13642 displayed. Lines were deleted at the end of the window. */
13643 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
13644 int vpos = XFASTINT (w->window_end_vpos);
13645 struct glyph_row *current_row = current_matrix->rows + vpos;
13646 struct glyph_row *desired_row = desired_matrix->rows + vpos;
13647
13648 for (row = NULL;
13649 row == NULL && vpos >= first_vpos;
13650 --vpos, --current_row, --desired_row)
13651 {
13652 if (desired_row->enabled_p)
13653 {
13654 if (desired_row->displays_text_p)
13655 row = desired_row;
13656 }
13657 else if (current_row->displays_text_p)
13658 row = current_row;
13659 }
13660
13661 xassert (row != NULL);
13662 w->window_end_vpos = make_number (vpos + 1);
13663 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
13664 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
13665 xassert (w->window_end_bytepos >= 0);
13666 IF_DEBUG (debug_method_add (w, "C"));
13667 }
13668 else
13669 abort ();
13670
13671 #if 0 /* This leads to problems, for instance when the cursor is
13672 at ZV, and the cursor line displays no text. */
13673 /* Disable rows below what's displayed in the window. This makes
13674 debugging easier. */
13675 enable_glyph_matrix_rows (current_matrix,
13676 XFASTINT (w->window_end_vpos) + 1,
13677 bottom_vpos, 0);
13678 #endif
13679
13680 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
13681 debug_end_vpos = XFASTINT (w->window_end_vpos));
13682
13683 /* Record that display has not been completed. */
13684 w->window_end_valid = Qnil;
13685 w->desired_matrix->no_scrolling_p = 1;
13686 return 3;
13687
13688 #undef GIVE_UP
13689 }
13690
13691
13692 \f
13693 /***********************************************************************
13694 More debugging support
13695 ***********************************************************************/
13696
13697 #if GLYPH_DEBUG
13698
13699 void dump_glyph_row P_ ((struct glyph_row *, int, int));
13700 void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
13701 void dump_glyph P_ ((struct glyph_row *, struct glyph *, int));
13702
13703
13704 /* Dump the contents of glyph matrix MATRIX on stderr.
13705
13706 GLYPHS 0 means don't show glyph contents.
13707 GLYPHS 1 means show glyphs in short form
13708 GLYPHS > 1 means show glyphs in long form. */
13709
13710 void
13711 dump_glyph_matrix (matrix, glyphs)
13712 struct glyph_matrix *matrix;
13713 int glyphs;
13714 {
13715 int i;
13716 for (i = 0; i < matrix->nrows; ++i)
13717 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
13718 }
13719
13720
13721 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
13722 the glyph row and area where the glyph comes from. */
13723
13724 void
13725 dump_glyph (row, glyph, area)
13726 struct glyph_row *row;
13727 struct glyph *glyph;
13728 int area;
13729 {
13730 if (glyph->type == CHAR_GLYPH)
13731 {
13732 fprintf (stderr,
13733 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
13734 glyph - row->glyphs[TEXT_AREA],
13735 'C',
13736 glyph->charpos,
13737 (BUFFERP (glyph->object)
13738 ? 'B'
13739 : (STRINGP (glyph->object)
13740 ? 'S'
13741 : '-')),
13742 glyph->pixel_width,
13743 glyph->u.ch,
13744 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
13745 ? glyph->u.ch
13746 : '.'),
13747 glyph->face_id,
13748 glyph->left_box_line_p,
13749 glyph->right_box_line_p);
13750 }
13751 else if (glyph->type == STRETCH_GLYPH)
13752 {
13753 fprintf (stderr,
13754 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
13755 glyph - row->glyphs[TEXT_AREA],
13756 'S',
13757 glyph->charpos,
13758 (BUFFERP (glyph->object)
13759 ? 'B'
13760 : (STRINGP (glyph->object)
13761 ? 'S'
13762 : '-')),
13763 glyph->pixel_width,
13764 0,
13765 '.',
13766 glyph->face_id,
13767 glyph->left_box_line_p,
13768 glyph->right_box_line_p);
13769 }
13770 else if (glyph->type == IMAGE_GLYPH)
13771 {
13772 fprintf (stderr,
13773 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
13774 glyph - row->glyphs[TEXT_AREA],
13775 'I',
13776 glyph->charpos,
13777 (BUFFERP (glyph->object)
13778 ? 'B'
13779 : (STRINGP (glyph->object)
13780 ? 'S'
13781 : '-')),
13782 glyph->pixel_width,
13783 glyph->u.img_id,
13784 '.',
13785 glyph->face_id,
13786 glyph->left_box_line_p,
13787 glyph->right_box_line_p);
13788 }
13789 }
13790
13791
13792 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
13793 GLYPHS 0 means don't show glyph contents.
13794 GLYPHS 1 means show glyphs in short form
13795 GLYPHS > 1 means show glyphs in long form. */
13796
13797 void
13798 dump_glyph_row (row, vpos, glyphs)
13799 struct glyph_row *row;
13800 int vpos, glyphs;
13801 {
13802 if (glyphs != 1)
13803 {
13804 fprintf (stderr, "Row Start End Used oEI><O\\CTZFesm X Y W H V A P\n");
13805 fprintf (stderr, "=======================================================================\n");
13806
13807 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d%1.1d\
13808 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
13809 vpos,
13810 MATRIX_ROW_START_CHARPOS (row),
13811 MATRIX_ROW_END_CHARPOS (row),
13812 row->used[TEXT_AREA],
13813 row->contains_overlapping_glyphs_p,
13814 row->enabled_p,
13815 row->truncated_on_left_p,
13816 row->truncated_on_right_p,
13817 row->overlay_arrow_p,
13818 row->continued_p,
13819 MATRIX_ROW_CONTINUATION_LINE_P (row),
13820 row->displays_text_p,
13821 row->ends_at_zv_p,
13822 row->fill_line_p,
13823 row->ends_in_middle_of_char_p,
13824 row->starts_in_middle_of_char_p,
13825 row->mouse_face_p,
13826 row->x,
13827 row->y,
13828 row->pixel_width,
13829 row->height,
13830 row->visible_height,
13831 row->ascent,
13832 row->phys_ascent);
13833 fprintf (stderr, "%9d %5d\t%5d\n", row->start.overlay_string_index,
13834 row->end.overlay_string_index,
13835 row->continuation_lines_width);
13836 fprintf (stderr, "%9d %5d\n",
13837 CHARPOS (row->start.string_pos),
13838 CHARPOS (row->end.string_pos));
13839 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
13840 row->end.dpvec_index);
13841 }
13842
13843 if (glyphs > 1)
13844 {
13845 int area;
13846
13847 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
13848 {
13849 struct glyph *glyph = row->glyphs[area];
13850 struct glyph *glyph_end = glyph + row->used[area];
13851
13852 /* Glyph for a line end in text. */
13853 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
13854 ++glyph_end;
13855
13856 if (glyph < glyph_end)
13857 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
13858
13859 for (; glyph < glyph_end; ++glyph)
13860 dump_glyph (row, glyph, area);
13861 }
13862 }
13863 else if (glyphs == 1)
13864 {
13865 int area;
13866
13867 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
13868 {
13869 char *s = (char *) alloca (row->used[area] + 1);
13870 int i;
13871
13872 for (i = 0; i < row->used[area]; ++i)
13873 {
13874 struct glyph *glyph = row->glyphs[area] + i;
13875 if (glyph->type == CHAR_GLYPH
13876 && glyph->u.ch < 0x80
13877 && glyph->u.ch >= ' ')
13878 s[i] = glyph->u.ch;
13879 else
13880 s[i] = '.';
13881 }
13882
13883 s[i] = '\0';
13884 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
13885 }
13886 }
13887 }
13888
13889
13890 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
13891 Sdump_glyph_matrix, 0, 1, "p",
13892 doc: /* Dump the current matrix of the selected window to stderr.
13893 Shows contents of glyph row structures. With non-nil
13894 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
13895 glyphs in short form, otherwise show glyphs in long form. */)
13896 (glyphs)
13897 Lisp_Object glyphs;
13898 {
13899 struct window *w = XWINDOW (selected_window);
13900 struct buffer *buffer = XBUFFER (w->buffer);
13901
13902 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
13903 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
13904 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
13905 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
13906 fprintf (stderr, "=============================================\n");
13907 dump_glyph_matrix (w->current_matrix,
13908 NILP (glyphs) ? 0 : XINT (glyphs));
13909 return Qnil;
13910 }
13911
13912
13913 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
13914 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
13915 ()
13916 {
13917 struct frame *f = XFRAME (selected_frame);
13918 dump_glyph_matrix (f->current_matrix, 1);
13919 return Qnil;
13920 }
13921
13922
13923 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
13924 doc: /* Dump glyph row ROW to stderr.
13925 GLYPH 0 means don't dump glyphs.
13926 GLYPH 1 means dump glyphs in short form.
13927 GLYPH > 1 or omitted means dump glyphs in long form. */)
13928 (row, glyphs)
13929 Lisp_Object row, glyphs;
13930 {
13931 struct glyph_matrix *matrix;
13932 int vpos;
13933
13934 CHECK_NUMBER (row);
13935 matrix = XWINDOW (selected_window)->current_matrix;
13936 vpos = XINT (row);
13937 if (vpos >= 0 && vpos < matrix->nrows)
13938 dump_glyph_row (MATRIX_ROW (matrix, vpos),
13939 vpos,
13940 INTEGERP (glyphs) ? XINT (glyphs) : 2);
13941 return Qnil;
13942 }
13943
13944
13945 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
13946 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
13947 GLYPH 0 means don't dump glyphs.
13948 GLYPH 1 means dump glyphs in short form.
13949 GLYPH > 1 or omitted means dump glyphs in long form. */)
13950 (row, glyphs)
13951 Lisp_Object row, glyphs;
13952 {
13953 struct frame *sf = SELECTED_FRAME ();
13954 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
13955 int vpos;
13956
13957 CHECK_NUMBER (row);
13958 vpos = XINT (row);
13959 if (vpos >= 0 && vpos < m->nrows)
13960 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
13961 INTEGERP (glyphs) ? XINT (glyphs) : 2);
13962 return Qnil;
13963 }
13964
13965
13966 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
13967 doc: /* Toggle tracing of redisplay.
13968 With ARG, turn tracing on if and only if ARG is positive. */)
13969 (arg)
13970 Lisp_Object arg;
13971 {
13972 if (NILP (arg))
13973 trace_redisplay_p = !trace_redisplay_p;
13974 else
13975 {
13976 arg = Fprefix_numeric_value (arg);
13977 trace_redisplay_p = XINT (arg) > 0;
13978 }
13979
13980 return Qnil;
13981 }
13982
13983
13984 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
13985 doc: /* Like `format', but print result to stderr.
13986 usage: (trace-to-stderr STRING &rest OBJECTS) */)
13987 (nargs, args)
13988 int nargs;
13989 Lisp_Object *args;
13990 {
13991 Lisp_Object s = Fformat (nargs, args);
13992 fprintf (stderr, "%s", SDATA (s));
13993 return Qnil;
13994 }
13995
13996 #endif /* GLYPH_DEBUG */
13997
13998
13999 \f
14000 /***********************************************************************
14001 Building Desired Matrix Rows
14002 ***********************************************************************/
14003
14004 /* Return a temporary glyph row holding the glyphs of an overlay
14005 arrow. Only used for non-window-redisplay windows. */
14006
14007 static struct glyph_row *
14008 get_overlay_arrow_glyph_row (w, overlay_arrow_string)
14009 struct window *w;
14010 Lisp_Object overlay_arrow_string;
14011 {
14012 struct frame *f = XFRAME (WINDOW_FRAME (w));
14013 struct buffer *buffer = XBUFFER (w->buffer);
14014 struct buffer *old = current_buffer;
14015 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
14016 int arrow_len = SCHARS (overlay_arrow_string);
14017 const unsigned char *arrow_end = arrow_string + arrow_len;
14018 const unsigned char *p;
14019 struct it it;
14020 int multibyte_p;
14021 int n_glyphs_before;
14022
14023 set_buffer_temp (buffer);
14024 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
14025 it.glyph_row->used[TEXT_AREA] = 0;
14026 SET_TEXT_POS (it.position, 0, 0);
14027
14028 multibyte_p = !NILP (buffer->enable_multibyte_characters);
14029 p = arrow_string;
14030 while (p < arrow_end)
14031 {
14032 Lisp_Object face, ilisp;
14033
14034 /* Get the next character. */
14035 if (multibyte_p)
14036 it.c = string_char_and_length (p, arrow_len, &it.len);
14037 else
14038 it.c = *p, it.len = 1;
14039 p += it.len;
14040
14041 /* Get its face. */
14042 ilisp = make_number (p - arrow_string);
14043 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
14044 it.face_id = compute_char_face (f, it.c, face);
14045
14046 /* Compute its width, get its glyphs. */
14047 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
14048 SET_TEXT_POS (it.position, -1, -1);
14049 PRODUCE_GLYPHS (&it);
14050
14051 /* If this character doesn't fit any more in the line, we have
14052 to remove some glyphs. */
14053 if (it.current_x > it.last_visible_x)
14054 {
14055 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
14056 break;
14057 }
14058 }
14059
14060 set_buffer_temp (old);
14061 return it.glyph_row;
14062 }
14063
14064
14065 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
14066 glyphs are only inserted for terminal frames since we can't really
14067 win with truncation glyphs when partially visible glyphs are
14068 involved. Which glyphs to insert is determined by
14069 produce_special_glyphs. */
14070
14071 static void
14072 insert_left_trunc_glyphs (it)
14073 struct it *it;
14074 {
14075 struct it truncate_it;
14076 struct glyph *from, *end, *to, *toend;
14077
14078 xassert (!FRAME_WINDOW_P (it->f));
14079
14080 /* Get the truncation glyphs. */
14081 truncate_it = *it;
14082 truncate_it.current_x = 0;
14083 truncate_it.face_id = DEFAULT_FACE_ID;
14084 truncate_it.glyph_row = &scratch_glyph_row;
14085 truncate_it.glyph_row->used[TEXT_AREA] = 0;
14086 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
14087 truncate_it.object = make_number (0);
14088 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
14089
14090 /* Overwrite glyphs from IT with truncation glyphs. */
14091 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
14092 end = from + truncate_it.glyph_row->used[TEXT_AREA];
14093 to = it->glyph_row->glyphs[TEXT_AREA];
14094 toend = to + it->glyph_row->used[TEXT_AREA];
14095
14096 while (from < end)
14097 *to++ = *from++;
14098
14099 /* There may be padding glyphs left over. Overwrite them too. */
14100 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
14101 {
14102 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
14103 while (from < end)
14104 *to++ = *from++;
14105 }
14106
14107 if (to > toend)
14108 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
14109 }
14110
14111
14112 /* Compute the pixel height and width of IT->glyph_row.
14113
14114 Most of the time, ascent and height of a display line will be equal
14115 to the max_ascent and max_height values of the display iterator
14116 structure. This is not the case if
14117
14118 1. We hit ZV without displaying anything. In this case, max_ascent
14119 and max_height will be zero.
14120
14121 2. We have some glyphs that don't contribute to the line height.
14122 (The glyph row flag contributes_to_line_height_p is for future
14123 pixmap extensions).
14124
14125 The first case is easily covered by using default values because in
14126 these cases, the line height does not really matter, except that it
14127 must not be zero. */
14128
14129 static void
14130 compute_line_metrics (it)
14131 struct it *it;
14132 {
14133 struct glyph_row *row = it->glyph_row;
14134 int area, i;
14135
14136 if (FRAME_WINDOW_P (it->f))
14137 {
14138 int i, min_y, max_y;
14139
14140 /* The line may consist of one space only, that was added to
14141 place the cursor on it. If so, the row's height hasn't been
14142 computed yet. */
14143 if (row->height == 0)
14144 {
14145 if (it->max_ascent + it->max_descent == 0)
14146 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
14147 row->ascent = it->max_ascent;
14148 row->height = it->max_ascent + it->max_descent;
14149 row->phys_ascent = it->max_phys_ascent;
14150 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
14151 }
14152
14153 /* Compute the width of this line. */
14154 row->pixel_width = row->x;
14155 for (i = 0; i < row->used[TEXT_AREA]; ++i)
14156 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
14157
14158 xassert (row->pixel_width >= 0);
14159 xassert (row->ascent >= 0 && row->height > 0);
14160
14161 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
14162 || MATRIX_ROW_OVERLAPS_PRED_P (row));
14163
14164 /* If first line's physical ascent is larger than its logical
14165 ascent, use the physical ascent, and make the row taller.
14166 This makes accented characters fully visible. */
14167 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
14168 && row->phys_ascent > row->ascent)
14169 {
14170 row->height += row->phys_ascent - row->ascent;
14171 row->ascent = row->phys_ascent;
14172 }
14173
14174 /* Compute how much of the line is visible. */
14175 row->visible_height = row->height;
14176
14177 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
14178 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
14179
14180 if (row->y < min_y)
14181 row->visible_height -= min_y - row->y;
14182 if (row->y + row->height > max_y)
14183 row->visible_height -= row->y + row->height - max_y;
14184 }
14185 else
14186 {
14187 row->pixel_width = row->used[TEXT_AREA];
14188 if (row->continued_p)
14189 row->pixel_width -= it->continuation_pixel_width;
14190 else if (row->truncated_on_right_p)
14191 row->pixel_width -= it->truncation_pixel_width;
14192 row->ascent = row->phys_ascent = 0;
14193 row->height = row->phys_height = row->visible_height = 1;
14194 }
14195
14196 /* Compute a hash code for this row. */
14197 row->hash = 0;
14198 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
14199 for (i = 0; i < row->used[area]; ++i)
14200 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
14201 + row->glyphs[area][i].u.val
14202 + row->glyphs[area][i].face_id
14203 + row->glyphs[area][i].padding_p
14204 + (row->glyphs[area][i].type << 2));
14205
14206 it->max_ascent = it->max_descent = 0;
14207 it->max_phys_ascent = it->max_phys_descent = 0;
14208 }
14209
14210
14211 /* Append one space to the glyph row of iterator IT if doing a
14212 window-based redisplay. The space has the same face as
14213 IT->face_id. Value is non-zero if a space was added.
14214
14215 This function is called to make sure that there is always one glyph
14216 at the end of a glyph row that the cursor can be set on under
14217 window-systems. (If there weren't such a glyph we would not know
14218 how wide and tall a box cursor should be displayed).
14219
14220 At the same time this space let's a nicely handle clearing to the
14221 end of the line if the row ends in italic text. */
14222
14223 static int
14224 append_space_for_newline (it, default_face_p)
14225 struct it *it;
14226 int default_face_p;
14227 {
14228 if (FRAME_WINDOW_P (it->f))
14229 {
14230 int n = it->glyph_row->used[TEXT_AREA];
14231
14232 if (it->glyph_row->glyphs[TEXT_AREA] + n
14233 < it->glyph_row->glyphs[1 + TEXT_AREA])
14234 {
14235 /* Save some values that must not be changed.
14236 Must save IT->c and IT->len because otherwise
14237 ITERATOR_AT_END_P wouldn't work anymore after
14238 append_space_for_newline has been called. */
14239 enum display_element_type saved_what = it->what;
14240 int saved_c = it->c, saved_len = it->len;
14241 int saved_x = it->current_x;
14242 int saved_face_id = it->face_id;
14243 struct text_pos saved_pos;
14244 Lisp_Object saved_object;
14245 struct face *face;
14246
14247 saved_object = it->object;
14248 saved_pos = it->position;
14249
14250 it->what = IT_CHARACTER;
14251 bzero (&it->position, sizeof it->position);
14252 it->object = make_number (0);
14253 it->c = ' ';
14254 it->len = 1;
14255
14256 if (default_face_p)
14257 it->face_id = DEFAULT_FACE_ID;
14258 else if (it->face_before_selective_p)
14259 it->face_id = it->saved_face_id;
14260 face = FACE_FROM_ID (it->f, it->face_id);
14261 it->face_id = FACE_FOR_CHAR (it->f, face, 0);
14262
14263 PRODUCE_GLYPHS (it);
14264
14265 it->override_ascent = -1;
14266 it->constrain_row_ascent_descent_p = 0;
14267 it->current_x = saved_x;
14268 it->object = saved_object;
14269 it->position = saved_pos;
14270 it->what = saved_what;
14271 it->face_id = saved_face_id;
14272 it->len = saved_len;
14273 it->c = saved_c;
14274 return 1;
14275 }
14276 }
14277
14278 return 0;
14279 }
14280
14281
14282 /* Extend the face of the last glyph in the text area of IT->glyph_row
14283 to the end of the display line. Called from display_line.
14284 If the glyph row is empty, add a space glyph to it so that we
14285 know the face to draw. Set the glyph row flag fill_line_p. */
14286
14287 static void
14288 extend_face_to_end_of_line (it)
14289 struct it *it;
14290 {
14291 struct face *face;
14292 struct frame *f = it->f;
14293
14294 /* If line is already filled, do nothing. */
14295 if (it->current_x >= it->last_visible_x)
14296 return;
14297
14298 /* Face extension extends the background and box of IT->face_id
14299 to the end of the line. If the background equals the background
14300 of the frame, we don't have to do anything. */
14301 if (it->face_before_selective_p)
14302 face = FACE_FROM_ID (it->f, it->saved_face_id);
14303 else
14304 face = FACE_FROM_ID (f, it->face_id);
14305
14306 if (FRAME_WINDOW_P (f)
14307 && face->box == FACE_NO_BOX
14308 && face->background == FRAME_BACKGROUND_PIXEL (f)
14309 && !face->stipple)
14310 return;
14311
14312 /* Set the glyph row flag indicating that the face of the last glyph
14313 in the text area has to be drawn to the end of the text area. */
14314 it->glyph_row->fill_line_p = 1;
14315
14316 /* If current character of IT is not ASCII, make sure we have the
14317 ASCII face. This will be automatically undone the next time
14318 get_next_display_element returns a multibyte character. Note
14319 that the character will always be single byte in unibyte text. */
14320 if (!SINGLE_BYTE_CHAR_P (it->c))
14321 {
14322 it->face_id = FACE_FOR_CHAR (f, face, 0);
14323 }
14324
14325 if (FRAME_WINDOW_P (f))
14326 {
14327 /* If the row is empty, add a space with the current face of IT,
14328 so that we know which face to draw. */
14329 if (it->glyph_row->used[TEXT_AREA] == 0)
14330 {
14331 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
14332 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
14333 it->glyph_row->used[TEXT_AREA] = 1;
14334 }
14335 }
14336 else
14337 {
14338 /* Save some values that must not be changed. */
14339 int saved_x = it->current_x;
14340 struct text_pos saved_pos;
14341 Lisp_Object saved_object;
14342 enum display_element_type saved_what = it->what;
14343 int saved_face_id = it->face_id;
14344
14345 saved_object = it->object;
14346 saved_pos = it->position;
14347
14348 it->what = IT_CHARACTER;
14349 bzero (&it->position, sizeof it->position);
14350 it->object = make_number (0);
14351 it->c = ' ';
14352 it->len = 1;
14353 it->face_id = face->id;
14354
14355 PRODUCE_GLYPHS (it);
14356
14357 while (it->current_x <= it->last_visible_x)
14358 PRODUCE_GLYPHS (it);
14359
14360 /* Don't count these blanks really. It would let us insert a left
14361 truncation glyph below and make us set the cursor on them, maybe. */
14362 it->current_x = saved_x;
14363 it->object = saved_object;
14364 it->position = saved_pos;
14365 it->what = saved_what;
14366 it->face_id = saved_face_id;
14367 }
14368 }
14369
14370
14371 /* Value is non-zero if text starting at CHARPOS in current_buffer is
14372 trailing whitespace. */
14373
14374 static int
14375 trailing_whitespace_p (charpos)
14376 int charpos;
14377 {
14378 int bytepos = CHAR_TO_BYTE (charpos);
14379 int c = 0;
14380
14381 while (bytepos < ZV_BYTE
14382 && (c = FETCH_CHAR (bytepos),
14383 c == ' ' || c == '\t'))
14384 ++bytepos;
14385
14386 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
14387 {
14388 if (bytepos != PT_BYTE)
14389 return 1;
14390 }
14391 return 0;
14392 }
14393
14394
14395 /* Highlight trailing whitespace, if any, in ROW. */
14396
14397 void
14398 highlight_trailing_whitespace (f, row)
14399 struct frame *f;
14400 struct glyph_row *row;
14401 {
14402 int used = row->used[TEXT_AREA];
14403
14404 if (used)
14405 {
14406 struct glyph *start = row->glyphs[TEXT_AREA];
14407 struct glyph *glyph = start + used - 1;
14408
14409 /* Skip over glyphs inserted to display the cursor at the
14410 end of a line, for extending the face of the last glyph
14411 to the end of the line on terminals, and for truncation
14412 and continuation glyphs. */
14413 while (glyph >= start
14414 && glyph->type == CHAR_GLYPH
14415 && INTEGERP (glyph->object))
14416 --glyph;
14417
14418 /* If last glyph is a space or stretch, and it's trailing
14419 whitespace, set the face of all trailing whitespace glyphs in
14420 IT->glyph_row to `trailing-whitespace'. */
14421 if (glyph >= start
14422 && BUFFERP (glyph->object)
14423 && (glyph->type == STRETCH_GLYPH
14424 || (glyph->type == CHAR_GLYPH
14425 && glyph->u.ch == ' '))
14426 && trailing_whitespace_p (glyph->charpos))
14427 {
14428 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
14429
14430 while (glyph >= start
14431 && BUFFERP (glyph->object)
14432 && (glyph->type == STRETCH_GLYPH
14433 || (glyph->type == CHAR_GLYPH
14434 && glyph->u.ch == ' ')))
14435 (glyph--)->face_id = face_id;
14436 }
14437 }
14438 }
14439
14440
14441 /* Value is non-zero if glyph row ROW in window W should be
14442 used to hold the cursor. */
14443
14444 static int
14445 cursor_row_p (w, row)
14446 struct window *w;
14447 struct glyph_row *row;
14448 {
14449 int cursor_row_p = 1;
14450
14451 if (PT == MATRIX_ROW_END_CHARPOS (row))
14452 {
14453 /* If the row ends with a newline from a string, we don't want
14454 the cursor there (if the row is continued it doesn't end in a
14455 newline). */
14456 if (CHARPOS (row->end.string_pos) >= 0
14457 || MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
14458 cursor_row_p = row->continued_p;
14459
14460 /* If the row ends at ZV, display the cursor at the end of that
14461 row instead of at the start of the row below. */
14462 else if (row->ends_at_zv_p)
14463 cursor_row_p = 1;
14464 else
14465 cursor_row_p = 0;
14466 }
14467
14468 return cursor_row_p;
14469 }
14470
14471
14472 /* Construct the glyph row IT->glyph_row in the desired matrix of
14473 IT->w from text at the current position of IT. See dispextern.h
14474 for an overview of struct it. Value is non-zero if
14475 IT->glyph_row displays text, as opposed to a line displaying ZV
14476 only. */
14477
14478 static int
14479 display_line (it)
14480 struct it *it;
14481 {
14482 struct glyph_row *row = it->glyph_row;
14483 int overlay_arrow_bitmap;
14484 Lisp_Object overlay_arrow_string;
14485
14486 /* We always start displaying at hpos zero even if hscrolled. */
14487 xassert (it->hpos == 0 && it->current_x == 0);
14488
14489 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
14490 >= it->w->desired_matrix->nrows)
14491 {
14492 it->w->nrows_scale_factor++;
14493 fonts_changed_p = 1;
14494 return 0;
14495 }
14496
14497 /* Is IT->w showing the region? */
14498 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
14499
14500 /* Clear the result glyph row and enable it. */
14501 prepare_desired_row (row);
14502
14503 row->y = it->current_y;
14504 row->start = it->start;
14505 row->continuation_lines_width = it->continuation_lines_width;
14506 row->displays_text_p = 1;
14507 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
14508 it->starts_in_middle_of_char_p = 0;
14509
14510 /* Arrange the overlays nicely for our purposes. Usually, we call
14511 display_line on only one line at a time, in which case this
14512 can't really hurt too much, or we call it on lines which appear
14513 one after another in the buffer, in which case all calls to
14514 recenter_overlay_lists but the first will be pretty cheap. */
14515 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
14516
14517 /* Move over display elements that are not visible because we are
14518 hscrolled. This may stop at an x-position < IT->first_visible_x
14519 if the first glyph is partially visible or if we hit a line end. */
14520 if (it->current_x < it->first_visible_x)
14521 move_it_in_display_line_to (it, ZV, it->first_visible_x,
14522 MOVE_TO_POS | MOVE_TO_X);
14523
14524 /* Get the initial row height. This is either the height of the
14525 text hscrolled, if there is any, or zero. */
14526 row->ascent = it->max_ascent;
14527 row->height = it->max_ascent + it->max_descent;
14528 row->phys_ascent = it->max_phys_ascent;
14529 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
14530
14531 /* Loop generating characters. The loop is left with IT on the next
14532 character to display. */
14533 while (1)
14534 {
14535 int n_glyphs_before, hpos_before, x_before;
14536 int x, i, nglyphs;
14537 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
14538
14539 /* Retrieve the next thing to display. Value is zero if end of
14540 buffer reached. */
14541 if (!get_next_display_element (it))
14542 {
14543 /* Maybe add a space at the end of this line that is used to
14544 display the cursor there under X. Set the charpos of the
14545 first glyph of blank lines not corresponding to any text
14546 to -1. */
14547 #ifdef HAVE_WINDOW_SYSTEM
14548 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
14549 row->exact_window_width_line_p = 1;
14550 else
14551 #endif /* HAVE_WINDOW_SYSTEM */
14552 if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
14553 || row->used[TEXT_AREA] == 0)
14554 {
14555 row->glyphs[TEXT_AREA]->charpos = -1;
14556 row->displays_text_p = 0;
14557
14558 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines)
14559 && (!MINI_WINDOW_P (it->w)
14560 || (minibuf_level && EQ (it->window, minibuf_window))))
14561 row->indicate_empty_line_p = 1;
14562 }
14563
14564 it->continuation_lines_width = 0;
14565 row->ends_at_zv_p = 1;
14566 break;
14567 }
14568
14569 /* Now, get the metrics of what we want to display. This also
14570 generates glyphs in `row' (which is IT->glyph_row). */
14571 n_glyphs_before = row->used[TEXT_AREA];
14572 x = it->current_x;
14573
14574 /* Remember the line height so far in case the next element doesn't
14575 fit on the line. */
14576 if (!it->truncate_lines_p)
14577 {
14578 ascent = it->max_ascent;
14579 descent = it->max_descent;
14580 phys_ascent = it->max_phys_ascent;
14581 phys_descent = it->max_phys_descent;
14582 }
14583
14584 PRODUCE_GLYPHS (it);
14585
14586 /* If this display element was in marginal areas, continue with
14587 the next one. */
14588 if (it->area != TEXT_AREA)
14589 {
14590 row->ascent = max (row->ascent, it->max_ascent);
14591 row->height = max (row->height, it->max_ascent + it->max_descent);
14592 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
14593 row->phys_height = max (row->phys_height,
14594 it->max_phys_ascent + it->max_phys_descent);
14595 set_iterator_to_next (it, 1);
14596 continue;
14597 }
14598
14599 /* Does the display element fit on the line? If we truncate
14600 lines, we should draw past the right edge of the window. If
14601 we don't truncate, we want to stop so that we can display the
14602 continuation glyph before the right margin. If lines are
14603 continued, there are two possible strategies for characters
14604 resulting in more than 1 glyph (e.g. tabs): Display as many
14605 glyphs as possible in this line and leave the rest for the
14606 continuation line, or display the whole element in the next
14607 line. Original redisplay did the former, so we do it also. */
14608 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
14609 hpos_before = it->hpos;
14610 x_before = x;
14611
14612 if (/* Not a newline. */
14613 nglyphs > 0
14614 /* Glyphs produced fit entirely in the line. */
14615 && it->current_x < it->last_visible_x)
14616 {
14617 it->hpos += nglyphs;
14618 row->ascent = max (row->ascent, it->max_ascent);
14619 row->height = max (row->height, it->max_ascent + it->max_descent);
14620 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
14621 row->phys_height = max (row->phys_height,
14622 it->max_phys_ascent + it->max_phys_descent);
14623 if (it->current_x - it->pixel_width < it->first_visible_x)
14624 row->x = x - it->first_visible_x;
14625 }
14626 else
14627 {
14628 int new_x;
14629 struct glyph *glyph;
14630
14631 for (i = 0; i < nglyphs; ++i, x = new_x)
14632 {
14633 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
14634 new_x = x + glyph->pixel_width;
14635
14636 if (/* Lines are continued. */
14637 !it->truncate_lines_p
14638 && (/* Glyph doesn't fit on the line. */
14639 new_x > it->last_visible_x
14640 /* Or it fits exactly on a window system frame. */
14641 || (new_x == it->last_visible_x
14642 && FRAME_WINDOW_P (it->f))))
14643 {
14644 /* End of a continued line. */
14645
14646 if (it->hpos == 0
14647 || (new_x == it->last_visible_x
14648 && FRAME_WINDOW_P (it->f)))
14649 {
14650 /* Current glyph is the only one on the line or
14651 fits exactly on the line. We must continue
14652 the line because we can't draw the cursor
14653 after the glyph. */
14654 row->continued_p = 1;
14655 it->current_x = new_x;
14656 it->continuation_lines_width += new_x;
14657 ++it->hpos;
14658 if (i == nglyphs - 1)
14659 {
14660 set_iterator_to_next (it, 1);
14661 #ifdef HAVE_WINDOW_SYSTEM
14662 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
14663 {
14664 if (!get_next_display_element (it))
14665 {
14666 row->exact_window_width_line_p = 1;
14667 it->continuation_lines_width = 0;
14668 row->continued_p = 0;
14669 row->ends_at_zv_p = 1;
14670 }
14671 else if (ITERATOR_AT_END_OF_LINE_P (it))
14672 {
14673 row->continued_p = 0;
14674 row->exact_window_width_line_p = 1;
14675 }
14676 }
14677 #endif /* HAVE_WINDOW_SYSTEM */
14678 }
14679 }
14680 else if (CHAR_GLYPH_PADDING_P (*glyph)
14681 && !FRAME_WINDOW_P (it->f))
14682 {
14683 /* A padding glyph that doesn't fit on this line.
14684 This means the whole character doesn't fit
14685 on the line. */
14686 row->used[TEXT_AREA] = n_glyphs_before;
14687
14688 /* Fill the rest of the row with continuation
14689 glyphs like in 20.x. */
14690 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
14691 < row->glyphs[1 + TEXT_AREA])
14692 produce_special_glyphs (it, IT_CONTINUATION);
14693
14694 row->continued_p = 1;
14695 it->current_x = x_before;
14696 it->continuation_lines_width += x_before;
14697
14698 /* Restore the height to what it was before the
14699 element not fitting on the line. */
14700 it->max_ascent = ascent;
14701 it->max_descent = descent;
14702 it->max_phys_ascent = phys_ascent;
14703 it->max_phys_descent = phys_descent;
14704 }
14705 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
14706 {
14707 /* A TAB that extends past the right edge of the
14708 window. This produces a single glyph on
14709 window system frames. We leave the glyph in
14710 this row and let it fill the row, but don't
14711 consume the TAB. */
14712 it->continuation_lines_width += it->last_visible_x;
14713 row->ends_in_middle_of_char_p = 1;
14714 row->continued_p = 1;
14715 glyph->pixel_width = it->last_visible_x - x;
14716 it->starts_in_middle_of_char_p = 1;
14717 }
14718 else
14719 {
14720 /* Something other than a TAB that draws past
14721 the right edge of the window. Restore
14722 positions to values before the element. */
14723 row->used[TEXT_AREA] = n_glyphs_before + i;
14724
14725 /* Display continuation glyphs. */
14726 if (!FRAME_WINDOW_P (it->f))
14727 produce_special_glyphs (it, IT_CONTINUATION);
14728 row->continued_p = 1;
14729
14730 it->continuation_lines_width += x;
14731
14732 if (nglyphs > 1 && i > 0)
14733 {
14734 row->ends_in_middle_of_char_p = 1;
14735 it->starts_in_middle_of_char_p = 1;
14736 }
14737
14738 /* Restore the height to what it was before the
14739 element not fitting on the line. */
14740 it->max_ascent = ascent;
14741 it->max_descent = descent;
14742 it->max_phys_ascent = phys_ascent;
14743 it->max_phys_descent = phys_descent;
14744 }
14745
14746 break;
14747 }
14748 else if (new_x > it->first_visible_x)
14749 {
14750 /* Increment number of glyphs actually displayed. */
14751 ++it->hpos;
14752
14753 if (x < it->first_visible_x)
14754 /* Glyph is partially visible, i.e. row starts at
14755 negative X position. */
14756 row->x = x - it->first_visible_x;
14757 }
14758 else
14759 {
14760 /* Glyph is completely off the left margin of the
14761 window. This should not happen because of the
14762 move_it_in_display_line at the start of this
14763 function, unless the text display area of the
14764 window is empty. */
14765 xassert (it->first_visible_x <= it->last_visible_x);
14766 }
14767 }
14768
14769 row->ascent = max (row->ascent, it->max_ascent);
14770 row->height = max (row->height, it->max_ascent + it->max_descent);
14771 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
14772 row->phys_height = max (row->phys_height,
14773 it->max_phys_ascent + it->max_phys_descent);
14774
14775 /* End of this display line if row is continued. */
14776 if (row->continued_p || row->ends_at_zv_p)
14777 break;
14778 }
14779
14780 at_end_of_line:
14781 /* Is this a line end? If yes, we're also done, after making
14782 sure that a non-default face is extended up to the right
14783 margin of the window. */
14784 if (ITERATOR_AT_END_OF_LINE_P (it))
14785 {
14786 int used_before = row->used[TEXT_AREA];
14787
14788 row->ends_in_newline_from_string_p = STRINGP (it->object);
14789
14790 #ifdef HAVE_WINDOW_SYSTEM
14791 /* Add a space at the end of the line that is used to
14792 display the cursor there. */
14793 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
14794 append_space_for_newline (it, 0);
14795 #endif /* HAVE_WINDOW_SYSTEM */
14796
14797 /* Extend the face to the end of the line. */
14798 extend_face_to_end_of_line (it);
14799
14800 /* Make sure we have the position. */
14801 if (used_before == 0)
14802 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
14803
14804 /* Consume the line end. This skips over invisible lines. */
14805 set_iterator_to_next (it, 1);
14806 it->continuation_lines_width = 0;
14807 break;
14808 }
14809
14810 /* Proceed with next display element. Note that this skips
14811 over lines invisible because of selective display. */
14812 set_iterator_to_next (it, 1);
14813
14814 /* If we truncate lines, we are done when the last displayed
14815 glyphs reach past the right margin of the window. */
14816 if (it->truncate_lines_p
14817 && (FRAME_WINDOW_P (it->f)
14818 ? (it->current_x >= it->last_visible_x)
14819 : (it->current_x > it->last_visible_x)))
14820 {
14821 /* Maybe add truncation glyphs. */
14822 if (!FRAME_WINDOW_P (it->f))
14823 {
14824 int i, n;
14825
14826 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
14827 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
14828 break;
14829
14830 for (n = row->used[TEXT_AREA]; i < n; ++i)
14831 {
14832 row->used[TEXT_AREA] = i;
14833 produce_special_glyphs (it, IT_TRUNCATION);
14834 }
14835 }
14836 #ifdef HAVE_WINDOW_SYSTEM
14837 else
14838 {
14839 /* Don't truncate if we can overflow newline into fringe. */
14840 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
14841 {
14842 if (!get_next_display_element (it))
14843 {
14844 #ifdef HAVE_WINDOW_SYSTEM
14845 it->continuation_lines_width = 0;
14846 row->ends_at_zv_p = 1;
14847 row->exact_window_width_line_p = 1;
14848 break;
14849 #endif /* HAVE_WINDOW_SYSTEM */
14850 }
14851 if (ITERATOR_AT_END_OF_LINE_P (it))
14852 {
14853 row->exact_window_width_line_p = 1;
14854 goto at_end_of_line;
14855 }
14856 }
14857 }
14858 #endif /* HAVE_WINDOW_SYSTEM */
14859
14860 row->truncated_on_right_p = 1;
14861 it->continuation_lines_width = 0;
14862 reseat_at_next_visible_line_start (it, 0);
14863 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
14864 it->hpos = hpos_before;
14865 it->current_x = x_before;
14866 break;
14867 }
14868 }
14869
14870 /* If line is not empty and hscrolled, maybe insert truncation glyphs
14871 at the left window margin. */
14872 if (it->first_visible_x
14873 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
14874 {
14875 if (!FRAME_WINDOW_P (it->f))
14876 insert_left_trunc_glyphs (it);
14877 row->truncated_on_left_p = 1;
14878 }
14879
14880 /* If the start of this line is the overlay arrow-position, then
14881 mark this glyph row as the one containing the overlay arrow.
14882 This is clearly a mess with variable size fonts. It would be
14883 better to let it be displayed like cursors under X. */
14884 if (! overlay_arrow_seen
14885 && (overlay_arrow_string
14886 = overlay_arrow_at_row (it->f, row, &overlay_arrow_bitmap),
14887 !NILP (overlay_arrow_string)))
14888 {
14889 /* Overlay arrow in window redisplay is a fringe bitmap. */
14890 if (!FRAME_WINDOW_P (it->f))
14891 {
14892 struct glyph_row *arrow_row
14893 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
14894 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
14895 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
14896 struct glyph *p = row->glyphs[TEXT_AREA];
14897 struct glyph *p2, *end;
14898
14899 /* Copy the arrow glyphs. */
14900 while (glyph < arrow_end)
14901 *p++ = *glyph++;
14902
14903 /* Throw away padding glyphs. */
14904 p2 = p;
14905 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
14906 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
14907 ++p2;
14908 if (p2 > p)
14909 {
14910 while (p2 < end)
14911 *p++ = *p2++;
14912 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
14913 }
14914 }
14915
14916 overlay_arrow_seen = 1;
14917 it->w->overlay_arrow_bitmap = overlay_arrow_bitmap;
14918 row->overlay_arrow_p = 1;
14919 }
14920
14921 /* Compute pixel dimensions of this line. */
14922 compute_line_metrics (it);
14923
14924 /* Remember the position at which this line ends. */
14925 row->end = it->current;
14926
14927 /* Save fringe bitmaps in this row. */
14928 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
14929 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
14930 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
14931 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
14932
14933 it->left_user_fringe_bitmap = 0;
14934 it->left_user_fringe_face_id = 0;
14935 it->right_user_fringe_bitmap = 0;
14936 it->right_user_fringe_face_id = 0;
14937
14938 /* Maybe set the cursor. */
14939 if (it->w->cursor.vpos < 0
14940 && PT >= MATRIX_ROW_START_CHARPOS (row)
14941 && PT <= MATRIX_ROW_END_CHARPOS (row)
14942 && cursor_row_p (it->w, row))
14943 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
14944
14945 /* Highlight trailing whitespace. */
14946 if (!NILP (Vshow_trailing_whitespace))
14947 highlight_trailing_whitespace (it->f, it->glyph_row);
14948
14949 /* Prepare for the next line. This line starts horizontally at (X
14950 HPOS) = (0 0). Vertical positions are incremented. As a
14951 convenience for the caller, IT->glyph_row is set to the next
14952 row to be used. */
14953 it->current_x = it->hpos = 0;
14954 it->current_y += row->height;
14955 ++it->vpos;
14956 ++it->glyph_row;
14957 it->start = it->current;
14958 return row->displays_text_p;
14959 }
14960
14961
14962 \f
14963 /***********************************************************************
14964 Menu Bar
14965 ***********************************************************************/
14966
14967 /* Redisplay the menu bar in the frame for window W.
14968
14969 The menu bar of X frames that don't have X toolkit support is
14970 displayed in a special window W->frame->menu_bar_window.
14971
14972 The menu bar of terminal frames is treated specially as far as
14973 glyph matrices are concerned. Menu bar lines are not part of
14974 windows, so the update is done directly on the frame matrix rows
14975 for the menu bar. */
14976
14977 static void
14978 display_menu_bar (w)
14979 struct window *w;
14980 {
14981 struct frame *f = XFRAME (WINDOW_FRAME (w));
14982 struct it it;
14983 Lisp_Object items;
14984 int i;
14985
14986 /* Don't do all this for graphical frames. */
14987 #ifdef HAVE_NTGUI
14988 if (!NILP (Vwindow_system))
14989 return;
14990 #endif
14991 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
14992 if (FRAME_X_P (f))
14993 return;
14994 #endif
14995 #ifdef MAC_OS
14996 if (FRAME_MAC_P (f))
14997 return;
14998 #endif
14999
15000 #ifdef USE_X_TOOLKIT
15001 xassert (!FRAME_WINDOW_P (f));
15002 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
15003 it.first_visible_x = 0;
15004 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
15005 #else /* not USE_X_TOOLKIT */
15006 if (FRAME_WINDOW_P (f))
15007 {
15008 /* Menu bar lines are displayed in the desired matrix of the
15009 dummy window menu_bar_window. */
15010 struct window *menu_w;
15011 xassert (WINDOWP (f->menu_bar_window));
15012 menu_w = XWINDOW (f->menu_bar_window);
15013 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
15014 MENU_FACE_ID);
15015 it.first_visible_x = 0;
15016 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
15017 }
15018 else
15019 {
15020 /* This is a TTY frame, i.e. character hpos/vpos are used as
15021 pixel x/y. */
15022 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
15023 MENU_FACE_ID);
15024 it.first_visible_x = 0;
15025 it.last_visible_x = FRAME_COLS (f);
15026 }
15027 #endif /* not USE_X_TOOLKIT */
15028
15029 if (! mode_line_inverse_video)
15030 /* Force the menu-bar to be displayed in the default face. */
15031 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
15032
15033 /* Clear all rows of the menu bar. */
15034 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
15035 {
15036 struct glyph_row *row = it.glyph_row + i;
15037 clear_glyph_row (row);
15038 row->enabled_p = 1;
15039 row->full_width_p = 1;
15040 }
15041
15042 /* Display all items of the menu bar. */
15043 items = FRAME_MENU_BAR_ITEMS (it.f);
15044 for (i = 0; i < XVECTOR (items)->size; i += 4)
15045 {
15046 Lisp_Object string;
15047
15048 /* Stop at nil string. */
15049 string = AREF (items, i + 1);
15050 if (NILP (string))
15051 break;
15052
15053 /* Remember where item was displayed. */
15054 AREF (items, i + 3) = make_number (it.hpos);
15055
15056 /* Display the item, pad with one space. */
15057 if (it.current_x < it.last_visible_x)
15058 display_string (NULL, string, Qnil, 0, 0, &it,
15059 SCHARS (string) + 1, 0, 0, -1);
15060 }
15061
15062 /* Fill out the line with spaces. */
15063 if (it.current_x < it.last_visible_x)
15064 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
15065
15066 /* Compute the total height of the lines. */
15067 compute_line_metrics (&it);
15068 }
15069
15070
15071 \f
15072 /***********************************************************************
15073 Mode Line
15074 ***********************************************************************/
15075
15076 /* Redisplay mode lines in the window tree whose root is WINDOW. If
15077 FORCE is non-zero, redisplay mode lines unconditionally.
15078 Otherwise, redisplay only mode lines that are garbaged. Value is
15079 the number of windows whose mode lines were redisplayed. */
15080
15081 static int
15082 redisplay_mode_lines (window, force)
15083 Lisp_Object window;
15084 int force;
15085 {
15086 int nwindows = 0;
15087
15088 while (!NILP (window))
15089 {
15090 struct window *w = XWINDOW (window);
15091
15092 if (WINDOWP (w->hchild))
15093 nwindows += redisplay_mode_lines (w->hchild, force);
15094 else if (WINDOWP (w->vchild))
15095 nwindows += redisplay_mode_lines (w->vchild, force);
15096 else if (force
15097 || FRAME_GARBAGED_P (XFRAME (w->frame))
15098 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
15099 {
15100 struct text_pos lpoint;
15101 struct buffer *old = current_buffer;
15102
15103 /* Set the window's buffer for the mode line display. */
15104 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15105 set_buffer_internal_1 (XBUFFER (w->buffer));
15106
15107 /* Point refers normally to the selected window. For any
15108 other window, set up appropriate value. */
15109 if (!EQ (window, selected_window))
15110 {
15111 struct text_pos pt;
15112
15113 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
15114 if (CHARPOS (pt) < BEGV)
15115 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
15116 else if (CHARPOS (pt) > (ZV - 1))
15117 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
15118 else
15119 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
15120 }
15121
15122 /* Display mode lines. */
15123 clear_glyph_matrix (w->desired_matrix);
15124 if (display_mode_lines (w))
15125 {
15126 ++nwindows;
15127 w->must_be_updated_p = 1;
15128 }
15129
15130 /* Restore old settings. */
15131 set_buffer_internal_1 (old);
15132 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
15133 }
15134
15135 window = w->next;
15136 }
15137
15138 return nwindows;
15139 }
15140
15141
15142 /* Display the mode and/or top line of window W. Value is the number
15143 of mode lines displayed. */
15144
15145 static int
15146 display_mode_lines (w)
15147 struct window *w;
15148 {
15149 Lisp_Object old_selected_window, old_selected_frame;
15150 int n = 0;
15151
15152 old_selected_frame = selected_frame;
15153 selected_frame = w->frame;
15154 old_selected_window = selected_window;
15155 XSETWINDOW (selected_window, w);
15156
15157 /* These will be set while the mode line specs are processed. */
15158 line_number_displayed = 0;
15159 w->column_number_displayed = Qnil;
15160
15161 if (WINDOW_WANTS_MODELINE_P (w))
15162 {
15163 struct window *sel_w = XWINDOW (old_selected_window);
15164
15165 /* Select mode line face based on the real selected window. */
15166 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
15167 current_buffer->mode_line_format);
15168 ++n;
15169 }
15170
15171 if (WINDOW_WANTS_HEADER_LINE_P (w))
15172 {
15173 display_mode_line (w, HEADER_LINE_FACE_ID,
15174 current_buffer->header_line_format);
15175 ++n;
15176 }
15177
15178 selected_frame = old_selected_frame;
15179 selected_window = old_selected_window;
15180 return n;
15181 }
15182
15183
15184 /* Display mode or top line of window W. FACE_ID specifies which line
15185 to display; it is either MODE_LINE_FACE_ID or HEADER_LINE_FACE_ID.
15186 FORMAT is the mode line format to display. Value is the pixel
15187 height of the mode line displayed. */
15188
15189 static int
15190 display_mode_line (w, face_id, format)
15191 struct window *w;
15192 enum face_id face_id;
15193 Lisp_Object format;
15194 {
15195 struct it it;
15196 struct face *face;
15197
15198 init_iterator (&it, w, -1, -1, NULL, face_id);
15199 prepare_desired_row (it.glyph_row);
15200
15201 it.glyph_row->mode_line_p = 1;
15202
15203 if (! mode_line_inverse_video)
15204 /* Force the mode-line to be displayed in the default face. */
15205 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
15206
15207 /* Temporarily make frame's keyboard the current kboard so that
15208 kboard-local variables in the mode_line_format will get the right
15209 values. */
15210 push_frame_kboard (it.f);
15211 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
15212 pop_frame_kboard ();
15213
15214 /* Fill up with spaces. */
15215 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
15216
15217 compute_line_metrics (&it);
15218 it.glyph_row->full_width_p = 1;
15219 it.glyph_row->continued_p = 0;
15220 it.glyph_row->truncated_on_left_p = 0;
15221 it.glyph_row->truncated_on_right_p = 0;
15222
15223 /* Make a 3D mode-line have a shadow at its right end. */
15224 face = FACE_FROM_ID (it.f, face_id);
15225 extend_face_to_end_of_line (&it);
15226 if (face->box != FACE_NO_BOX)
15227 {
15228 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
15229 + it.glyph_row->used[TEXT_AREA] - 1);
15230 last->right_box_line_p = 1;
15231 }
15232
15233 return it.glyph_row->height;
15234 }
15235
15236 /* Alist that caches the results of :propertize.
15237 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
15238 Lisp_Object mode_line_proptrans_alist;
15239
15240 /* List of strings making up the mode-line. */
15241 Lisp_Object mode_line_string_list;
15242
15243 /* Base face property when building propertized mode line string. */
15244 static Lisp_Object mode_line_string_face;
15245 static Lisp_Object mode_line_string_face_prop;
15246
15247
15248 /* Contribute ELT to the mode line for window IT->w. How it
15249 translates into text depends on its data type.
15250
15251 IT describes the display environment in which we display, as usual.
15252
15253 DEPTH is the depth in recursion. It is used to prevent
15254 infinite recursion here.
15255
15256 FIELD_WIDTH is the number of characters the display of ELT should
15257 occupy in the mode line, and PRECISION is the maximum number of
15258 characters to display from ELT's representation. See
15259 display_string for details.
15260
15261 Returns the hpos of the end of the text generated by ELT.
15262
15263 PROPS is a property list to add to any string we encounter.
15264
15265 If RISKY is nonzero, remove (disregard) any properties in any string
15266 we encounter, and ignore :eval and :propertize.
15267
15268 If the global variable `frame_title_ptr' is non-NULL, then the output
15269 is passed to `store_frame_title' instead of `display_string'. */
15270
15271 static int
15272 display_mode_element (it, depth, field_width, precision, elt, props, risky)
15273 struct it *it;
15274 int depth;
15275 int field_width, precision;
15276 Lisp_Object elt, props;
15277 int risky;
15278 {
15279 int n = 0, field, prec;
15280 int literal = 0;
15281
15282 tail_recurse:
15283 if (depth > 100)
15284 elt = build_string ("*too-deep*");
15285
15286 depth++;
15287
15288 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
15289 {
15290 case Lisp_String:
15291 {
15292 /* A string: output it and check for %-constructs within it. */
15293 unsigned char c;
15294 const unsigned char *this, *lisp_string;
15295
15296 if (!NILP (props) || risky)
15297 {
15298 Lisp_Object oprops, aelt;
15299 oprops = Ftext_properties_at (make_number (0), elt);
15300
15301 if (NILP (Fequal (props, oprops)) || risky)
15302 {
15303 /* If the starting string has properties,
15304 merge the specified ones onto the existing ones. */
15305 if (! NILP (oprops) && !risky)
15306 {
15307 Lisp_Object tem;
15308
15309 oprops = Fcopy_sequence (oprops);
15310 tem = props;
15311 while (CONSP (tem))
15312 {
15313 oprops = Fplist_put (oprops, XCAR (tem),
15314 XCAR (XCDR (tem)));
15315 tem = XCDR (XCDR (tem));
15316 }
15317 props = oprops;
15318 }
15319
15320 aelt = Fassoc (elt, mode_line_proptrans_alist);
15321 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
15322 {
15323 mode_line_proptrans_alist
15324 = Fcons (aelt, Fdelq (aelt, mode_line_proptrans_alist));
15325 elt = XCAR (aelt);
15326 }
15327 else
15328 {
15329 Lisp_Object tem;
15330
15331 elt = Fcopy_sequence (elt);
15332 Fset_text_properties (make_number (0), Flength (elt),
15333 props, elt);
15334 /* Add this item to mode_line_proptrans_alist. */
15335 mode_line_proptrans_alist
15336 = Fcons (Fcons (elt, props),
15337 mode_line_proptrans_alist);
15338 /* Truncate mode_line_proptrans_alist
15339 to at most 50 elements. */
15340 tem = Fnthcdr (make_number (50),
15341 mode_line_proptrans_alist);
15342 if (! NILP (tem))
15343 XSETCDR (tem, Qnil);
15344 }
15345 }
15346 }
15347
15348 this = SDATA (elt);
15349 lisp_string = this;
15350
15351 if (literal)
15352 {
15353 prec = precision - n;
15354 if (frame_title_ptr)
15355 n += store_frame_title (SDATA (elt), -1, prec);
15356 else if (!NILP (mode_line_string_list))
15357 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
15358 else
15359 n += display_string (NULL, elt, Qnil, 0, 0, it,
15360 0, prec, 0, STRING_MULTIBYTE (elt));
15361
15362 break;
15363 }
15364
15365 while ((precision <= 0 || n < precision)
15366 && *this
15367 && (frame_title_ptr
15368 || !NILP (mode_line_string_list)
15369 || it->current_x < it->last_visible_x))
15370 {
15371 const unsigned char *last = this;
15372
15373 /* Advance to end of string or next format specifier. */
15374 while ((c = *this++) != '\0' && c != '%')
15375 ;
15376
15377 if (this - 1 != last)
15378 {
15379 /* Output to end of string or up to '%'. Field width
15380 is length of string. Don't output more than
15381 PRECISION allows us. */
15382 --this;
15383
15384 prec = chars_in_text (last, this - last);
15385 if (precision > 0 && prec > precision - n)
15386 prec = precision - n;
15387
15388 if (frame_title_ptr)
15389 n += store_frame_title (last, 0, prec);
15390 else if (!NILP (mode_line_string_list))
15391 {
15392 int bytepos = last - lisp_string;
15393 int charpos = string_byte_to_char (elt, bytepos);
15394 n += store_mode_line_string (NULL,
15395 Fsubstring (elt, make_number (charpos),
15396 make_number (charpos + prec)),
15397 0, 0, 0, Qnil);
15398 }
15399 else
15400 {
15401 int bytepos = last - lisp_string;
15402 int charpos = string_byte_to_char (elt, bytepos);
15403 n += display_string (NULL, elt, Qnil, 0, charpos,
15404 it, 0, prec, 0,
15405 STRING_MULTIBYTE (elt));
15406 }
15407 }
15408 else /* c == '%' */
15409 {
15410 const unsigned char *percent_position = this;
15411
15412 /* Get the specified minimum width. Zero means
15413 don't pad. */
15414 field = 0;
15415 while ((c = *this++) >= '0' && c <= '9')
15416 field = field * 10 + c - '0';
15417
15418 /* Don't pad beyond the total padding allowed. */
15419 if (field_width - n > 0 && field > field_width - n)
15420 field = field_width - n;
15421
15422 /* Note that either PRECISION <= 0 or N < PRECISION. */
15423 prec = precision - n;
15424
15425 if (c == 'M')
15426 n += display_mode_element (it, depth, field, prec,
15427 Vglobal_mode_string, props,
15428 risky);
15429 else if (c != 0)
15430 {
15431 int multibyte;
15432 int bytepos, charpos;
15433 unsigned char *spec;
15434
15435 bytepos = percent_position - lisp_string;
15436 charpos = (STRING_MULTIBYTE (elt)
15437 ? string_byte_to_char (elt, bytepos)
15438 : bytepos);
15439
15440 spec
15441 = decode_mode_spec (it->w, c, field, prec, &multibyte);
15442
15443 if (frame_title_ptr)
15444 n += store_frame_title (spec, field, prec);
15445 else if (!NILP (mode_line_string_list))
15446 {
15447 int len = strlen (spec);
15448 Lisp_Object tem = make_string (spec, len);
15449 props = Ftext_properties_at (make_number (charpos), elt);
15450 /* Should only keep face property in props */
15451 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
15452 }
15453 else
15454 {
15455 int nglyphs_before, nwritten;
15456
15457 nglyphs_before = it->glyph_row->used[TEXT_AREA];
15458 nwritten = display_string (spec, Qnil, elt,
15459 charpos, 0, it,
15460 field, prec, 0,
15461 multibyte);
15462
15463 /* Assign to the glyphs written above the
15464 string where the `%x' came from, position
15465 of the `%'. */
15466 if (nwritten > 0)
15467 {
15468 struct glyph *glyph
15469 = (it->glyph_row->glyphs[TEXT_AREA]
15470 + nglyphs_before);
15471 int i;
15472
15473 for (i = 0; i < nwritten; ++i)
15474 {
15475 glyph[i].object = elt;
15476 glyph[i].charpos = charpos;
15477 }
15478
15479 n += nwritten;
15480 }
15481 }
15482 }
15483 else /* c == 0 */
15484 break;
15485 }
15486 }
15487 }
15488 break;
15489
15490 case Lisp_Symbol:
15491 /* A symbol: process the value of the symbol recursively
15492 as if it appeared here directly. Avoid error if symbol void.
15493 Special case: if value of symbol is a string, output the string
15494 literally. */
15495 {
15496 register Lisp_Object tem;
15497
15498 /* If the variable is not marked as risky to set
15499 then its contents are risky to use. */
15500 if (NILP (Fget (elt, Qrisky_local_variable)))
15501 risky = 1;
15502
15503 tem = Fboundp (elt);
15504 if (!NILP (tem))
15505 {
15506 tem = Fsymbol_value (elt);
15507 /* If value is a string, output that string literally:
15508 don't check for % within it. */
15509 if (STRINGP (tem))
15510 literal = 1;
15511
15512 if (!EQ (tem, elt))
15513 {
15514 /* Give up right away for nil or t. */
15515 elt = tem;
15516 goto tail_recurse;
15517 }
15518 }
15519 }
15520 break;
15521
15522 case Lisp_Cons:
15523 {
15524 register Lisp_Object car, tem;
15525
15526 /* A cons cell: five distinct cases.
15527 If first element is :eval or :propertize, do something special.
15528 If first element is a string or a cons, process all the elements
15529 and effectively concatenate them.
15530 If first element is a negative number, truncate displaying cdr to
15531 at most that many characters. If positive, pad (with spaces)
15532 to at least that many characters.
15533 If first element is a symbol, process the cadr or caddr recursively
15534 according to whether the symbol's value is non-nil or nil. */
15535 car = XCAR (elt);
15536 if (EQ (car, QCeval))
15537 {
15538 /* An element of the form (:eval FORM) means evaluate FORM
15539 and use the result as mode line elements. */
15540
15541 if (risky)
15542 break;
15543
15544 if (CONSP (XCDR (elt)))
15545 {
15546 Lisp_Object spec;
15547 spec = safe_eval (XCAR (XCDR (elt)));
15548 n += display_mode_element (it, depth, field_width - n,
15549 precision - n, spec, props,
15550 risky);
15551 }
15552 }
15553 else if (EQ (car, QCpropertize))
15554 {
15555 /* An element of the form (:propertize ELT PROPS...)
15556 means display ELT but applying properties PROPS. */
15557
15558 if (risky)
15559 break;
15560
15561 if (CONSP (XCDR (elt)))
15562 n += display_mode_element (it, depth, field_width - n,
15563 precision - n, XCAR (XCDR (elt)),
15564 XCDR (XCDR (elt)), risky);
15565 }
15566 else if (SYMBOLP (car))
15567 {
15568 tem = Fboundp (car);
15569 elt = XCDR (elt);
15570 if (!CONSP (elt))
15571 goto invalid;
15572 /* elt is now the cdr, and we know it is a cons cell.
15573 Use its car if CAR has a non-nil value. */
15574 if (!NILP (tem))
15575 {
15576 tem = Fsymbol_value (car);
15577 if (!NILP (tem))
15578 {
15579 elt = XCAR (elt);
15580 goto tail_recurse;
15581 }
15582 }
15583 /* Symbol's value is nil (or symbol is unbound)
15584 Get the cddr of the original list
15585 and if possible find the caddr and use that. */
15586 elt = XCDR (elt);
15587 if (NILP (elt))
15588 break;
15589 else if (!CONSP (elt))
15590 goto invalid;
15591 elt = XCAR (elt);
15592 goto tail_recurse;
15593 }
15594 else if (INTEGERP (car))
15595 {
15596 register int lim = XINT (car);
15597 elt = XCDR (elt);
15598 if (lim < 0)
15599 {
15600 /* Negative int means reduce maximum width. */
15601 if (precision <= 0)
15602 precision = -lim;
15603 else
15604 precision = min (precision, -lim);
15605 }
15606 else if (lim > 0)
15607 {
15608 /* Padding specified. Don't let it be more than
15609 current maximum. */
15610 if (precision > 0)
15611 lim = min (precision, lim);
15612
15613 /* If that's more padding than already wanted, queue it.
15614 But don't reduce padding already specified even if
15615 that is beyond the current truncation point. */
15616 field_width = max (lim, field_width);
15617 }
15618 goto tail_recurse;
15619 }
15620 else if (STRINGP (car) || CONSP (car))
15621 {
15622 register int limit = 50;
15623 /* Limit is to protect against circular lists. */
15624 while (CONSP (elt)
15625 && --limit > 0
15626 && (precision <= 0 || n < precision))
15627 {
15628 n += display_mode_element (it, depth, field_width - n,
15629 precision - n, XCAR (elt),
15630 props, risky);
15631 elt = XCDR (elt);
15632 }
15633 }
15634 }
15635 break;
15636
15637 default:
15638 invalid:
15639 elt = build_string ("*invalid*");
15640 goto tail_recurse;
15641 }
15642
15643 /* Pad to FIELD_WIDTH. */
15644 if (field_width > 0 && n < field_width)
15645 {
15646 if (frame_title_ptr)
15647 n += store_frame_title ("", field_width - n, 0);
15648 else if (!NILP (mode_line_string_list))
15649 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
15650 else
15651 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
15652 0, 0, 0);
15653 }
15654
15655 return n;
15656 }
15657
15658 /* Store a mode-line string element in mode_line_string_list.
15659
15660 If STRING is non-null, display that C string. Otherwise, the Lisp
15661 string LISP_STRING is displayed.
15662
15663 FIELD_WIDTH is the minimum number of output glyphs to produce.
15664 If STRING has fewer characters than FIELD_WIDTH, pad to the right
15665 with spaces. FIELD_WIDTH <= 0 means don't pad.
15666
15667 PRECISION is the maximum number of characters to output from
15668 STRING. PRECISION <= 0 means don't truncate the string.
15669
15670 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
15671 properties to the string.
15672
15673 PROPS are the properties to add to the string.
15674 The mode_line_string_face face property is always added to the string.
15675 */
15676
15677 static int store_mode_line_string (string, lisp_string, copy_string, field_width, precision, props)
15678 char *string;
15679 Lisp_Object lisp_string;
15680 int copy_string;
15681 int field_width;
15682 int precision;
15683 Lisp_Object props;
15684 {
15685 int len;
15686 int n = 0;
15687
15688 if (string != NULL)
15689 {
15690 len = strlen (string);
15691 if (precision > 0 && len > precision)
15692 len = precision;
15693 lisp_string = make_string (string, len);
15694 if (NILP (props))
15695 props = mode_line_string_face_prop;
15696 else if (!NILP (mode_line_string_face))
15697 {
15698 Lisp_Object face = Fplist_get (props, Qface);
15699 props = Fcopy_sequence (props);
15700 if (NILP (face))
15701 face = mode_line_string_face;
15702 else
15703 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
15704 props = Fplist_put (props, Qface, face);
15705 }
15706 Fadd_text_properties (make_number (0), make_number (len),
15707 props, lisp_string);
15708 }
15709 else
15710 {
15711 len = XFASTINT (Flength (lisp_string));
15712 if (precision > 0 && len > precision)
15713 {
15714 len = precision;
15715 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
15716 precision = -1;
15717 }
15718 if (!NILP (mode_line_string_face))
15719 {
15720 Lisp_Object face;
15721 if (NILP (props))
15722 props = Ftext_properties_at (make_number (0), lisp_string);
15723 face = Fplist_get (props, Qface);
15724 if (NILP (face))
15725 face = mode_line_string_face;
15726 else
15727 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
15728 props = Fcons (Qface, Fcons (face, Qnil));
15729 if (copy_string)
15730 lisp_string = Fcopy_sequence (lisp_string);
15731 }
15732 if (!NILP (props))
15733 Fadd_text_properties (make_number (0), make_number (len),
15734 props, lisp_string);
15735 }
15736
15737 if (len > 0)
15738 {
15739 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
15740 n += len;
15741 }
15742
15743 if (field_width > len)
15744 {
15745 field_width -= len;
15746 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
15747 if (!NILP (props))
15748 Fadd_text_properties (make_number (0), make_number (field_width),
15749 props, lisp_string);
15750 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
15751 n += field_width;
15752 }
15753
15754 return n;
15755 }
15756
15757
15758 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
15759 0, 3, 0,
15760 doc: /* Return the mode-line of selected window as a string.
15761 First optional arg FORMAT specifies a different format string (see
15762 `mode-line-format' for details) to use. If FORMAT is t, return
15763 the buffer's header-line. Second optional arg WINDOW specifies a
15764 different window to use as the context for the formatting.
15765 If third optional arg NO-PROPS is non-nil, string is not propertized. */)
15766 (format, window, no_props)
15767 Lisp_Object format, window, no_props;
15768 {
15769 struct it it;
15770 int len;
15771 struct window *w;
15772 struct buffer *old_buffer = NULL;
15773 enum face_id face_id = DEFAULT_FACE_ID;
15774
15775 if (NILP (window))
15776 window = selected_window;
15777 CHECK_WINDOW (window);
15778 w = XWINDOW (window);
15779 CHECK_BUFFER (w->buffer);
15780
15781 if (XBUFFER (w->buffer) != current_buffer)
15782 {
15783 old_buffer = current_buffer;
15784 set_buffer_internal_1 (XBUFFER (w->buffer));
15785 }
15786
15787 if (NILP (format) || EQ (format, Qt))
15788 {
15789 face_id = NILP (format)
15790 ? CURRENT_MODE_LINE_FACE_ID (w) :
15791 HEADER_LINE_FACE_ID;
15792 format = NILP (format)
15793 ? current_buffer->mode_line_format
15794 : current_buffer->header_line_format;
15795 }
15796
15797 init_iterator (&it, w, -1, -1, NULL, face_id);
15798
15799 if (NILP (no_props))
15800 {
15801 mode_line_string_face =
15802 (face_id == MODE_LINE_FACE_ID ? Qmode_line :
15803 face_id == MODE_LINE_INACTIVE_FACE_ID ? Qmode_line_inactive :
15804 face_id == HEADER_LINE_FACE_ID ? Qheader_line : Qnil);
15805
15806 mode_line_string_face_prop =
15807 NILP (mode_line_string_face) ? Qnil :
15808 Fcons (Qface, Fcons (mode_line_string_face, Qnil));
15809
15810 /* We need a dummy last element in mode_line_string_list to
15811 indicate we are building the propertized mode-line string.
15812 Using mode_line_string_face_prop here GC protects it. */
15813 mode_line_string_list =
15814 Fcons (mode_line_string_face_prop, Qnil);
15815 frame_title_ptr = NULL;
15816 }
15817 else
15818 {
15819 mode_line_string_face_prop = Qnil;
15820 mode_line_string_list = Qnil;
15821 frame_title_ptr = frame_title_buf;
15822 }
15823
15824 push_frame_kboard (it.f);
15825 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
15826 pop_frame_kboard ();
15827
15828 if (old_buffer)
15829 set_buffer_internal_1 (old_buffer);
15830
15831 if (NILP (no_props))
15832 {
15833 Lisp_Object str;
15834 mode_line_string_list = Fnreverse (mode_line_string_list);
15835 str = Fmapconcat (intern ("identity"), XCDR (mode_line_string_list),
15836 make_string ("", 0));
15837 mode_line_string_face_prop = Qnil;
15838 mode_line_string_list = Qnil;
15839 return str;
15840 }
15841
15842 len = frame_title_ptr - frame_title_buf;
15843 if (len > 0 && frame_title_ptr[-1] == '-')
15844 {
15845 /* Mode lines typically ends with numerous dashes; reduce to two dashes. */
15846 while (frame_title_ptr > frame_title_buf && *--frame_title_ptr == '-')
15847 ;
15848 frame_title_ptr += 3; /* restore last non-dash + two dashes */
15849 if (len > frame_title_ptr - frame_title_buf)
15850 len = frame_title_ptr - frame_title_buf;
15851 }
15852
15853 frame_title_ptr = NULL;
15854 return make_string (frame_title_buf, len);
15855 }
15856
15857 /* Write a null-terminated, right justified decimal representation of
15858 the positive integer D to BUF using a minimal field width WIDTH. */
15859
15860 static void
15861 pint2str (buf, width, d)
15862 register char *buf;
15863 register int width;
15864 register int d;
15865 {
15866 register char *p = buf;
15867
15868 if (d <= 0)
15869 *p++ = '0';
15870 else
15871 {
15872 while (d > 0)
15873 {
15874 *p++ = d % 10 + '0';
15875 d /= 10;
15876 }
15877 }
15878
15879 for (width -= (int) (p - buf); width > 0; --width)
15880 *p++ = ' ';
15881 *p-- = '\0';
15882 while (p > buf)
15883 {
15884 d = *buf;
15885 *buf++ = *p;
15886 *p-- = d;
15887 }
15888 }
15889
15890 /* Write a null-terminated, right justified decimal and "human
15891 readable" representation of the nonnegative integer D to BUF using
15892 a minimal field width WIDTH. D should be smaller than 999.5e24. */
15893
15894 static const char power_letter[] =
15895 {
15896 0, /* not used */
15897 'k', /* kilo */
15898 'M', /* mega */
15899 'G', /* giga */
15900 'T', /* tera */
15901 'P', /* peta */
15902 'E', /* exa */
15903 'Z', /* zetta */
15904 'Y' /* yotta */
15905 };
15906
15907 static void
15908 pint2hrstr (buf, width, d)
15909 char *buf;
15910 int width;
15911 int d;
15912 {
15913 /* We aim to represent the nonnegative integer D as
15914 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
15915 int quotient = d;
15916 int remainder = 0;
15917 /* -1 means: do not use TENTHS. */
15918 int tenths = -1;
15919 int exponent = 0;
15920
15921 /* Length of QUOTIENT.TENTHS as a string. */
15922 int length;
15923
15924 char * psuffix;
15925 char * p;
15926
15927 if (1000 <= quotient)
15928 {
15929 /* Scale to the appropriate EXPONENT. */
15930 do
15931 {
15932 remainder = quotient % 1000;
15933 quotient /= 1000;
15934 exponent++;
15935 }
15936 while (1000 <= quotient);
15937
15938 /* Round to nearest and decide whether to use TENTHS or not. */
15939 if (quotient <= 9)
15940 {
15941 tenths = remainder / 100;
15942 if (50 <= remainder % 100)
15943 if (tenths < 9)
15944 tenths++;
15945 else
15946 {
15947 quotient++;
15948 if (quotient == 10)
15949 tenths = -1;
15950 else
15951 tenths = 0;
15952 }
15953 }
15954 else
15955 if (500 <= remainder)
15956 if (quotient < 999)
15957 quotient++;
15958 else
15959 {
15960 quotient = 1;
15961 exponent++;
15962 tenths = 0;
15963 }
15964 }
15965
15966 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
15967 if (tenths == -1 && quotient <= 99)
15968 if (quotient <= 9)
15969 length = 1;
15970 else
15971 length = 2;
15972 else
15973 length = 3;
15974 p = psuffix = buf + max (width, length);
15975
15976 /* Print EXPONENT. */
15977 if (exponent)
15978 *psuffix++ = power_letter[exponent];
15979 *psuffix = '\0';
15980
15981 /* Print TENTHS. */
15982 if (tenths >= 0)
15983 {
15984 *--p = '0' + tenths;
15985 *--p = '.';
15986 }
15987
15988 /* Print QUOTIENT. */
15989 do
15990 {
15991 int digit = quotient % 10;
15992 *--p = '0' + digit;
15993 }
15994 while ((quotient /= 10) != 0);
15995
15996 /* Print leading spaces. */
15997 while (buf < p)
15998 *--p = ' ';
15999 }
16000
16001 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
16002 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
16003 type of CODING_SYSTEM. Return updated pointer into BUF. */
16004
16005 static unsigned char invalid_eol_type[] = "(*invalid*)";
16006
16007 static char *
16008 decode_mode_spec_coding (coding_system, buf, eol_flag)
16009 Lisp_Object coding_system;
16010 register char *buf;
16011 int eol_flag;
16012 {
16013 Lisp_Object val;
16014 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
16015 const unsigned char *eol_str;
16016 int eol_str_len;
16017 /* The EOL conversion we are using. */
16018 Lisp_Object eoltype;
16019
16020 val = Fget (coding_system, Qcoding_system);
16021 eoltype = Qnil;
16022
16023 if (!VECTORP (val)) /* Not yet decided. */
16024 {
16025 if (multibyte)
16026 *buf++ = '-';
16027 if (eol_flag)
16028 eoltype = eol_mnemonic_undecided;
16029 /* Don't mention EOL conversion if it isn't decided. */
16030 }
16031 else
16032 {
16033 Lisp_Object eolvalue;
16034
16035 eolvalue = Fget (coding_system, Qeol_type);
16036
16037 if (multibyte)
16038 *buf++ = XFASTINT (AREF (val, 1));
16039
16040 if (eol_flag)
16041 {
16042 /* The EOL conversion that is normal on this system. */
16043
16044 if (NILP (eolvalue)) /* Not yet decided. */
16045 eoltype = eol_mnemonic_undecided;
16046 else if (VECTORP (eolvalue)) /* Not yet decided. */
16047 eoltype = eol_mnemonic_undecided;
16048 else /* INTEGERP (eolvalue) -- 0:LF, 1:CRLF, 2:CR */
16049 eoltype = (XFASTINT (eolvalue) == 0
16050 ? eol_mnemonic_unix
16051 : (XFASTINT (eolvalue) == 1
16052 ? eol_mnemonic_dos : eol_mnemonic_mac));
16053 }
16054 }
16055
16056 if (eol_flag)
16057 {
16058 /* Mention the EOL conversion if it is not the usual one. */
16059 if (STRINGP (eoltype))
16060 {
16061 eol_str = SDATA (eoltype);
16062 eol_str_len = SBYTES (eoltype);
16063 }
16064 else if (INTEGERP (eoltype)
16065 && CHAR_VALID_P (XINT (eoltype), 0))
16066 {
16067 unsigned char *tmp = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
16068 eol_str_len = CHAR_STRING (XINT (eoltype), tmp);
16069 eol_str = tmp;
16070 }
16071 else
16072 {
16073 eol_str = invalid_eol_type;
16074 eol_str_len = sizeof (invalid_eol_type) - 1;
16075 }
16076 bcopy (eol_str, buf, eol_str_len);
16077 buf += eol_str_len;
16078 }
16079
16080 return buf;
16081 }
16082
16083 /* Return a string for the output of a mode line %-spec for window W,
16084 generated by character C. PRECISION >= 0 means don't return a
16085 string longer than that value. FIELD_WIDTH > 0 means pad the
16086 string returned with spaces to that value. Return 1 in *MULTIBYTE
16087 if the result is multibyte text. */
16088
16089 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
16090
16091 static char *
16092 decode_mode_spec (w, c, field_width, precision, multibyte)
16093 struct window *w;
16094 register int c;
16095 int field_width, precision;
16096 int *multibyte;
16097 {
16098 Lisp_Object obj;
16099 struct frame *f = XFRAME (WINDOW_FRAME (w));
16100 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
16101 struct buffer *b = XBUFFER (w->buffer);
16102
16103 obj = Qnil;
16104 *multibyte = 0;
16105
16106 switch (c)
16107 {
16108 case '*':
16109 if (!NILP (b->read_only))
16110 return "%";
16111 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
16112 return "*";
16113 return "-";
16114
16115 case '+':
16116 /* This differs from %* only for a modified read-only buffer. */
16117 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
16118 return "*";
16119 if (!NILP (b->read_only))
16120 return "%";
16121 return "-";
16122
16123 case '&':
16124 /* This differs from %* in ignoring read-only-ness. */
16125 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
16126 return "*";
16127 return "-";
16128
16129 case '%':
16130 return "%";
16131
16132 case '[':
16133 {
16134 int i;
16135 char *p;
16136
16137 if (command_loop_level > 5)
16138 return "[[[... ";
16139 p = decode_mode_spec_buf;
16140 for (i = 0; i < command_loop_level; i++)
16141 *p++ = '[';
16142 *p = 0;
16143 return decode_mode_spec_buf;
16144 }
16145
16146 case ']':
16147 {
16148 int i;
16149 char *p;
16150
16151 if (command_loop_level > 5)
16152 return " ...]]]";
16153 p = decode_mode_spec_buf;
16154 for (i = 0; i < command_loop_level; i++)
16155 *p++ = ']';
16156 *p = 0;
16157 return decode_mode_spec_buf;
16158 }
16159
16160 case '-':
16161 {
16162 register int i;
16163
16164 /* Let lots_of_dashes be a string of infinite length. */
16165 if (!NILP (mode_line_string_list))
16166 return "--";
16167 if (field_width <= 0
16168 || field_width > sizeof (lots_of_dashes))
16169 {
16170 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
16171 decode_mode_spec_buf[i] = '-';
16172 decode_mode_spec_buf[i] = '\0';
16173 return decode_mode_spec_buf;
16174 }
16175 else
16176 return lots_of_dashes;
16177 }
16178
16179 case 'b':
16180 obj = b->name;
16181 break;
16182
16183 case 'c':
16184 {
16185 int col = (int) current_column (); /* iftc */
16186 w->column_number_displayed = make_number (col);
16187 pint2str (decode_mode_spec_buf, field_width, col);
16188 return decode_mode_spec_buf;
16189 }
16190
16191 case 'F':
16192 /* %F displays the frame name. */
16193 if (!NILP (f->title))
16194 return (char *) SDATA (f->title);
16195 if (f->explicit_name || ! FRAME_WINDOW_P (f))
16196 return (char *) SDATA (f->name);
16197 return "Emacs";
16198
16199 case 'f':
16200 obj = b->filename;
16201 break;
16202
16203 case 'i':
16204 {
16205 int size = ZV - BEGV;
16206 pint2str (decode_mode_spec_buf, field_width, size);
16207 return decode_mode_spec_buf;
16208 }
16209
16210 case 'I':
16211 {
16212 int size = ZV - BEGV;
16213 pint2hrstr (decode_mode_spec_buf, field_width, size);
16214 return decode_mode_spec_buf;
16215 }
16216
16217 case 'l':
16218 {
16219 int startpos = XMARKER (w->start)->charpos;
16220 int startpos_byte = marker_byte_position (w->start);
16221 int line, linepos, linepos_byte, topline;
16222 int nlines, junk;
16223 int height = WINDOW_TOTAL_LINES (w);
16224
16225 /* If we decided that this buffer isn't suitable for line numbers,
16226 don't forget that too fast. */
16227 if (EQ (w->base_line_pos, w->buffer))
16228 goto no_value;
16229 /* But do forget it, if the window shows a different buffer now. */
16230 else if (BUFFERP (w->base_line_pos))
16231 w->base_line_pos = Qnil;
16232
16233 /* If the buffer is very big, don't waste time. */
16234 if (INTEGERP (Vline_number_display_limit)
16235 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
16236 {
16237 w->base_line_pos = Qnil;
16238 w->base_line_number = Qnil;
16239 goto no_value;
16240 }
16241
16242 if (!NILP (w->base_line_number)
16243 && !NILP (w->base_line_pos)
16244 && XFASTINT (w->base_line_pos) <= startpos)
16245 {
16246 line = XFASTINT (w->base_line_number);
16247 linepos = XFASTINT (w->base_line_pos);
16248 linepos_byte = buf_charpos_to_bytepos (b, linepos);
16249 }
16250 else
16251 {
16252 line = 1;
16253 linepos = BUF_BEGV (b);
16254 linepos_byte = BUF_BEGV_BYTE (b);
16255 }
16256
16257 /* Count lines from base line to window start position. */
16258 nlines = display_count_lines (linepos, linepos_byte,
16259 startpos_byte,
16260 startpos, &junk);
16261
16262 topline = nlines + line;
16263
16264 /* Determine a new base line, if the old one is too close
16265 or too far away, or if we did not have one.
16266 "Too close" means it's plausible a scroll-down would
16267 go back past it. */
16268 if (startpos == BUF_BEGV (b))
16269 {
16270 w->base_line_number = make_number (topline);
16271 w->base_line_pos = make_number (BUF_BEGV (b));
16272 }
16273 else if (nlines < height + 25 || nlines > height * 3 + 50
16274 || linepos == BUF_BEGV (b))
16275 {
16276 int limit = BUF_BEGV (b);
16277 int limit_byte = BUF_BEGV_BYTE (b);
16278 int position;
16279 int distance = (height * 2 + 30) * line_number_display_limit_width;
16280
16281 if (startpos - distance > limit)
16282 {
16283 limit = startpos - distance;
16284 limit_byte = CHAR_TO_BYTE (limit);
16285 }
16286
16287 nlines = display_count_lines (startpos, startpos_byte,
16288 limit_byte,
16289 - (height * 2 + 30),
16290 &position);
16291 /* If we couldn't find the lines we wanted within
16292 line_number_display_limit_width chars per line,
16293 give up on line numbers for this window. */
16294 if (position == limit_byte && limit == startpos - distance)
16295 {
16296 w->base_line_pos = w->buffer;
16297 w->base_line_number = Qnil;
16298 goto no_value;
16299 }
16300
16301 w->base_line_number = make_number (topline - nlines);
16302 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
16303 }
16304
16305 /* Now count lines from the start pos to point. */
16306 nlines = display_count_lines (startpos, startpos_byte,
16307 PT_BYTE, PT, &junk);
16308
16309 /* Record that we did display the line number. */
16310 line_number_displayed = 1;
16311
16312 /* Make the string to show. */
16313 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
16314 return decode_mode_spec_buf;
16315 no_value:
16316 {
16317 char* p = decode_mode_spec_buf;
16318 int pad = field_width - 2;
16319 while (pad-- > 0)
16320 *p++ = ' ';
16321 *p++ = '?';
16322 *p++ = '?';
16323 *p = '\0';
16324 return decode_mode_spec_buf;
16325 }
16326 }
16327 break;
16328
16329 case 'm':
16330 obj = b->mode_name;
16331 break;
16332
16333 case 'n':
16334 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
16335 return " Narrow";
16336 break;
16337
16338 case 'p':
16339 {
16340 int pos = marker_position (w->start);
16341 int total = BUF_ZV (b) - BUF_BEGV (b);
16342
16343 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
16344 {
16345 if (pos <= BUF_BEGV (b))
16346 return "All";
16347 else
16348 return "Bottom";
16349 }
16350 else if (pos <= BUF_BEGV (b))
16351 return "Top";
16352 else
16353 {
16354 if (total > 1000000)
16355 /* Do it differently for a large value, to avoid overflow. */
16356 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
16357 else
16358 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
16359 /* We can't normally display a 3-digit number,
16360 so get us a 2-digit number that is close. */
16361 if (total == 100)
16362 total = 99;
16363 sprintf (decode_mode_spec_buf, "%2d%%", total);
16364 return decode_mode_spec_buf;
16365 }
16366 }
16367
16368 /* Display percentage of size above the bottom of the screen. */
16369 case 'P':
16370 {
16371 int toppos = marker_position (w->start);
16372 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
16373 int total = BUF_ZV (b) - BUF_BEGV (b);
16374
16375 if (botpos >= BUF_ZV (b))
16376 {
16377 if (toppos <= BUF_BEGV (b))
16378 return "All";
16379 else
16380 return "Bottom";
16381 }
16382 else
16383 {
16384 if (total > 1000000)
16385 /* Do it differently for a large value, to avoid overflow. */
16386 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
16387 else
16388 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
16389 /* We can't normally display a 3-digit number,
16390 so get us a 2-digit number that is close. */
16391 if (total == 100)
16392 total = 99;
16393 if (toppos <= BUF_BEGV (b))
16394 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
16395 else
16396 sprintf (decode_mode_spec_buf, "%2d%%", total);
16397 return decode_mode_spec_buf;
16398 }
16399 }
16400
16401 case 's':
16402 /* status of process */
16403 obj = Fget_buffer_process (w->buffer);
16404 if (NILP (obj))
16405 return "no process";
16406 #ifdef subprocesses
16407 obj = Fsymbol_name (Fprocess_status (obj));
16408 #endif
16409 break;
16410
16411 case 't': /* indicate TEXT or BINARY */
16412 #ifdef MODE_LINE_BINARY_TEXT
16413 return MODE_LINE_BINARY_TEXT (b);
16414 #else
16415 return "T";
16416 #endif
16417
16418 case 'z':
16419 /* coding-system (not including end-of-line format) */
16420 case 'Z':
16421 /* coding-system (including end-of-line type) */
16422 {
16423 int eol_flag = (c == 'Z');
16424 char *p = decode_mode_spec_buf;
16425
16426 if (! FRAME_WINDOW_P (f))
16427 {
16428 /* No need to mention EOL here--the terminal never needs
16429 to do EOL conversion. */
16430 p = decode_mode_spec_coding (keyboard_coding.symbol, p, 0);
16431 p = decode_mode_spec_coding (terminal_coding.symbol, p, 0);
16432 }
16433 p = decode_mode_spec_coding (b->buffer_file_coding_system,
16434 p, eol_flag);
16435
16436 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
16437 #ifdef subprocesses
16438 obj = Fget_buffer_process (Fcurrent_buffer ());
16439 if (PROCESSP (obj))
16440 {
16441 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
16442 p, eol_flag);
16443 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
16444 p, eol_flag);
16445 }
16446 #endif /* subprocesses */
16447 #endif /* 0 */
16448 *p = 0;
16449 return decode_mode_spec_buf;
16450 }
16451 }
16452
16453 if (STRINGP (obj))
16454 {
16455 *multibyte = STRING_MULTIBYTE (obj);
16456 return (char *) SDATA (obj);
16457 }
16458 else
16459 return "";
16460 }
16461
16462
16463 /* Count up to COUNT lines starting from START / START_BYTE.
16464 But don't go beyond LIMIT_BYTE.
16465 Return the number of lines thus found (always nonnegative).
16466
16467 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
16468
16469 static int
16470 display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
16471 int start, start_byte, limit_byte, count;
16472 int *byte_pos_ptr;
16473 {
16474 register unsigned char *cursor;
16475 unsigned char *base;
16476
16477 register int ceiling;
16478 register unsigned char *ceiling_addr;
16479 int orig_count = count;
16480
16481 /* If we are not in selective display mode,
16482 check only for newlines. */
16483 int selective_display = (!NILP (current_buffer->selective_display)
16484 && !INTEGERP (current_buffer->selective_display));
16485
16486 if (count > 0)
16487 {
16488 while (start_byte < limit_byte)
16489 {
16490 ceiling = BUFFER_CEILING_OF (start_byte);
16491 ceiling = min (limit_byte - 1, ceiling);
16492 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
16493 base = (cursor = BYTE_POS_ADDR (start_byte));
16494 while (1)
16495 {
16496 if (selective_display)
16497 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
16498 ;
16499 else
16500 while (*cursor != '\n' && ++cursor != ceiling_addr)
16501 ;
16502
16503 if (cursor != ceiling_addr)
16504 {
16505 if (--count == 0)
16506 {
16507 start_byte += cursor - base + 1;
16508 *byte_pos_ptr = start_byte;
16509 return orig_count;
16510 }
16511 else
16512 if (++cursor == ceiling_addr)
16513 break;
16514 }
16515 else
16516 break;
16517 }
16518 start_byte += cursor - base;
16519 }
16520 }
16521 else
16522 {
16523 while (start_byte > limit_byte)
16524 {
16525 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
16526 ceiling = max (limit_byte, ceiling);
16527 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
16528 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
16529 while (1)
16530 {
16531 if (selective_display)
16532 while (--cursor != ceiling_addr
16533 && *cursor != '\n' && *cursor != 015)
16534 ;
16535 else
16536 while (--cursor != ceiling_addr && *cursor != '\n')
16537 ;
16538
16539 if (cursor != ceiling_addr)
16540 {
16541 if (++count == 0)
16542 {
16543 start_byte += cursor - base + 1;
16544 *byte_pos_ptr = start_byte;
16545 /* When scanning backwards, we should
16546 not count the newline posterior to which we stop. */
16547 return - orig_count - 1;
16548 }
16549 }
16550 else
16551 break;
16552 }
16553 /* Here we add 1 to compensate for the last decrement
16554 of CURSOR, which took it past the valid range. */
16555 start_byte += cursor - base + 1;
16556 }
16557 }
16558
16559 *byte_pos_ptr = limit_byte;
16560
16561 if (count < 0)
16562 return - orig_count + count;
16563 return orig_count - count;
16564
16565 }
16566
16567
16568 \f
16569 /***********************************************************************
16570 Displaying strings
16571 ***********************************************************************/
16572
16573 /* Display a NUL-terminated string, starting with index START.
16574
16575 If STRING is non-null, display that C string. Otherwise, the Lisp
16576 string LISP_STRING is displayed.
16577
16578 If FACE_STRING is not nil, FACE_STRING_POS is a position in
16579 FACE_STRING. Display STRING or LISP_STRING with the face at
16580 FACE_STRING_POS in FACE_STRING:
16581
16582 Display the string in the environment given by IT, but use the
16583 standard display table, temporarily.
16584
16585 FIELD_WIDTH is the minimum number of output glyphs to produce.
16586 If STRING has fewer characters than FIELD_WIDTH, pad to the right
16587 with spaces. If STRING has more characters, more than FIELD_WIDTH
16588 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
16589
16590 PRECISION is the maximum number of characters to output from
16591 STRING. PRECISION < 0 means don't truncate the string.
16592
16593 This is roughly equivalent to printf format specifiers:
16594
16595 FIELD_WIDTH PRECISION PRINTF
16596 ----------------------------------------
16597 -1 -1 %s
16598 -1 10 %.10s
16599 10 -1 %10s
16600 20 10 %20.10s
16601
16602 MULTIBYTE zero means do not display multibyte chars, > 0 means do
16603 display them, and < 0 means obey the current buffer's value of
16604 enable_multibyte_characters.
16605
16606 Value is the number of glyphs produced. */
16607
16608 static int
16609 display_string (string, lisp_string, face_string, face_string_pos,
16610 start, it, field_width, precision, max_x, multibyte)
16611 unsigned char *string;
16612 Lisp_Object lisp_string;
16613 Lisp_Object face_string;
16614 int face_string_pos;
16615 int start;
16616 struct it *it;
16617 int field_width, precision, max_x;
16618 int multibyte;
16619 {
16620 int hpos_at_start = it->hpos;
16621 int saved_face_id = it->face_id;
16622 struct glyph_row *row = it->glyph_row;
16623
16624 /* Initialize the iterator IT for iteration over STRING beginning
16625 with index START. */
16626 reseat_to_string (it, string, lisp_string, start,
16627 precision, field_width, multibyte);
16628
16629 /* If displaying STRING, set up the face of the iterator
16630 from LISP_STRING, if that's given. */
16631 if (STRINGP (face_string))
16632 {
16633 int endptr;
16634 struct face *face;
16635
16636 it->face_id
16637 = face_at_string_position (it->w, face_string, face_string_pos,
16638 0, it->region_beg_charpos,
16639 it->region_end_charpos,
16640 &endptr, it->base_face_id, 0);
16641 face = FACE_FROM_ID (it->f, it->face_id);
16642 it->face_box_p = face->box != FACE_NO_BOX;
16643 }
16644
16645 /* Set max_x to the maximum allowed X position. Don't let it go
16646 beyond the right edge of the window. */
16647 if (max_x <= 0)
16648 max_x = it->last_visible_x;
16649 else
16650 max_x = min (max_x, it->last_visible_x);
16651
16652 /* Skip over display elements that are not visible. because IT->w is
16653 hscrolled. */
16654 if (it->current_x < it->first_visible_x)
16655 move_it_in_display_line_to (it, 100000, it->first_visible_x,
16656 MOVE_TO_POS | MOVE_TO_X);
16657
16658 row->ascent = it->max_ascent;
16659 row->height = it->max_ascent + it->max_descent;
16660 row->phys_ascent = it->max_phys_ascent;
16661 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
16662
16663 /* This condition is for the case that we are called with current_x
16664 past last_visible_x. */
16665 while (it->current_x < max_x)
16666 {
16667 int x_before, x, n_glyphs_before, i, nglyphs;
16668
16669 /* Get the next display element. */
16670 if (!get_next_display_element (it))
16671 break;
16672
16673 /* Produce glyphs. */
16674 x_before = it->current_x;
16675 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
16676 PRODUCE_GLYPHS (it);
16677
16678 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
16679 i = 0;
16680 x = x_before;
16681 while (i < nglyphs)
16682 {
16683 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
16684
16685 if (!it->truncate_lines_p
16686 && x + glyph->pixel_width > max_x)
16687 {
16688 /* End of continued line or max_x reached. */
16689 if (CHAR_GLYPH_PADDING_P (*glyph))
16690 {
16691 /* A wide character is unbreakable. */
16692 it->glyph_row->used[TEXT_AREA] = n_glyphs_before;
16693 it->current_x = x_before;
16694 }
16695 else
16696 {
16697 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
16698 it->current_x = x;
16699 }
16700 break;
16701 }
16702 else if (x + glyph->pixel_width > it->first_visible_x)
16703 {
16704 /* Glyph is at least partially visible. */
16705 ++it->hpos;
16706 if (x < it->first_visible_x)
16707 it->glyph_row->x = x - it->first_visible_x;
16708 }
16709 else
16710 {
16711 /* Glyph is off the left margin of the display area.
16712 Should not happen. */
16713 abort ();
16714 }
16715
16716 row->ascent = max (row->ascent, it->max_ascent);
16717 row->height = max (row->height, it->max_ascent + it->max_descent);
16718 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
16719 row->phys_height = max (row->phys_height,
16720 it->max_phys_ascent + it->max_phys_descent);
16721 x += glyph->pixel_width;
16722 ++i;
16723 }
16724
16725 /* Stop if max_x reached. */
16726 if (i < nglyphs)
16727 break;
16728
16729 /* Stop at line ends. */
16730 if (ITERATOR_AT_END_OF_LINE_P (it))
16731 {
16732 it->continuation_lines_width = 0;
16733 break;
16734 }
16735
16736 set_iterator_to_next (it, 1);
16737
16738 /* Stop if truncating at the right edge. */
16739 if (it->truncate_lines_p
16740 && it->current_x >= it->last_visible_x)
16741 {
16742 /* Add truncation mark, but don't do it if the line is
16743 truncated at a padding space. */
16744 if (IT_CHARPOS (*it) < it->string_nchars)
16745 {
16746 if (!FRAME_WINDOW_P (it->f))
16747 {
16748 int i, n;
16749
16750 if (it->current_x > it->last_visible_x)
16751 {
16752 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
16753 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
16754 break;
16755 for (n = row->used[TEXT_AREA]; i < n; ++i)
16756 {
16757 row->used[TEXT_AREA] = i;
16758 produce_special_glyphs (it, IT_TRUNCATION);
16759 }
16760 }
16761 produce_special_glyphs (it, IT_TRUNCATION);
16762 }
16763 it->glyph_row->truncated_on_right_p = 1;
16764 }
16765 break;
16766 }
16767 }
16768
16769 /* Maybe insert a truncation at the left. */
16770 if (it->first_visible_x
16771 && IT_CHARPOS (*it) > 0)
16772 {
16773 if (!FRAME_WINDOW_P (it->f))
16774 insert_left_trunc_glyphs (it);
16775 it->glyph_row->truncated_on_left_p = 1;
16776 }
16777
16778 it->face_id = saved_face_id;
16779
16780 /* Value is number of columns displayed. */
16781 return it->hpos - hpos_at_start;
16782 }
16783
16784
16785 \f
16786 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
16787 appears as an element of LIST or as the car of an element of LIST.
16788 If PROPVAL is a list, compare each element against LIST in that
16789 way, and return 1/2 if any element of PROPVAL is found in LIST.
16790 Otherwise return 0. This function cannot quit.
16791 The return value is 2 if the text is invisible but with an ellipsis
16792 and 1 if it's invisible and without an ellipsis. */
16793
16794 int
16795 invisible_p (propval, list)
16796 register Lisp_Object propval;
16797 Lisp_Object list;
16798 {
16799 register Lisp_Object tail, proptail;
16800
16801 for (tail = list; CONSP (tail); tail = XCDR (tail))
16802 {
16803 register Lisp_Object tem;
16804 tem = XCAR (tail);
16805 if (EQ (propval, tem))
16806 return 1;
16807 if (CONSP (tem) && EQ (propval, XCAR (tem)))
16808 return NILP (XCDR (tem)) ? 1 : 2;
16809 }
16810
16811 if (CONSP (propval))
16812 {
16813 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
16814 {
16815 Lisp_Object propelt;
16816 propelt = XCAR (proptail);
16817 for (tail = list; CONSP (tail); tail = XCDR (tail))
16818 {
16819 register Lisp_Object tem;
16820 tem = XCAR (tail);
16821 if (EQ (propelt, tem))
16822 return 1;
16823 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
16824 return NILP (XCDR (tem)) ? 1 : 2;
16825 }
16826 }
16827 }
16828
16829 return 0;
16830 }
16831
16832 /* Calculate a width or height in pixels from a specification using
16833 the following elements:
16834
16835 SPEC ::=
16836 NUM - a (fractional) multiple of the default font width/height
16837 (NUM) - specifies exactly NUM pixels
16838 UNIT - a fixed number of pixels, see below.
16839 ELEMENT - size of a display element in pixels, see below.
16840 (NUM . SPEC) - equals NUM * SPEC
16841 (+ SPEC SPEC ...) - add pixel values
16842 (- SPEC SPEC ...) - subtract pixel values
16843 (- SPEC) - negate pixel value
16844
16845 NUM ::=
16846 INT or FLOAT - a number constant
16847 SYMBOL - use symbol's (buffer local) variable binding.
16848
16849 UNIT ::=
16850 in - pixels per inch *)
16851 mm - pixels per 1/1000 meter *)
16852 cm - pixels per 1/100 meter *)
16853 width - width of current font in pixels.
16854 height - height of current font in pixels.
16855
16856 *) using the ratio(s) defined in display-pixels-per-inch.
16857
16858 ELEMENT ::=
16859
16860 left-fringe - left fringe width in pixels
16861 right-fringe - right fringe width in pixels
16862
16863 left-margin - left margin width in pixels
16864 right-margin - right margin width in pixels
16865
16866 scroll-bar - scroll-bar area width in pixels
16867
16868 Examples:
16869
16870 Pixels corresponding to 5 inches:
16871 (5 . in)
16872
16873 Total width of non-text areas on left side of window (if scroll-bar is on left):
16874 '(space :width (+ left-fringe left-margin scroll-bar))
16875
16876 Align to first text column (in header line):
16877 '(space :align-to 0)
16878
16879 Align to middle of text area minus half the width of variable `my-image'
16880 containing a loaded image:
16881 '(space :align-to (0.5 . (- text my-image)))
16882
16883 Width of left margin minus width of 1 character in the default font:
16884 '(space :width (- left-margin 1))
16885
16886 Width of left margin minus width of 2 characters in the current font:
16887 '(space :width (- left-margin (2 . width)))
16888
16889 Center 1 character over left-margin (in header line):
16890 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
16891
16892 Different ways to express width of left fringe plus left margin minus one pixel:
16893 '(space :width (- (+ left-fringe left-margin) (1)))
16894 '(space :width (+ left-fringe left-margin (- (1))))
16895 '(space :width (+ left-fringe left-margin (-1)))
16896
16897 */
16898
16899 #define NUMVAL(X) \
16900 ((INTEGERP (X) || FLOATP (X)) \
16901 ? XFLOATINT (X) \
16902 : - 1)
16903
16904 int
16905 calc_pixel_width_or_height (res, it, prop, font, width_p, align_to)
16906 double *res;
16907 struct it *it;
16908 Lisp_Object prop;
16909 void *font;
16910 int width_p, *align_to;
16911 {
16912 double pixels;
16913
16914 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
16915 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
16916
16917 if (NILP (prop))
16918 return OK_PIXELS (0);
16919
16920 if (SYMBOLP (prop))
16921 {
16922 if (SCHARS (SYMBOL_NAME (prop)) == 2)
16923 {
16924 char *unit = SDATA (SYMBOL_NAME (prop));
16925
16926 if (unit[0] == 'i' && unit[1] == 'n')
16927 pixels = 1.0;
16928 else if (unit[0] == 'm' && unit[1] == 'm')
16929 pixels = 25.4;
16930 else if (unit[0] == 'c' && unit[1] == 'm')
16931 pixels = 2.54;
16932 else
16933 pixels = 0;
16934 if (pixels > 0)
16935 {
16936 double ppi;
16937 if ((ppi = NUMVAL (Vdisplay_pixels_per_inch), ppi > 0)
16938 || (CONSP (Vdisplay_pixels_per_inch)
16939 && (ppi = (width_p
16940 ? NUMVAL (XCAR (Vdisplay_pixels_per_inch))
16941 : NUMVAL (XCDR (Vdisplay_pixels_per_inch))),
16942 ppi > 0)))
16943 return OK_PIXELS (ppi / pixels);
16944
16945 return 0;
16946 }
16947 }
16948
16949 #ifdef HAVE_WINDOW_SYSTEM
16950 if (EQ (prop, Qheight))
16951 return OK_PIXELS (font ? FONT_HEIGHT ((XFontStruct *)font) : FRAME_LINE_HEIGHT (it->f));
16952 if (EQ (prop, Qwidth))
16953 return OK_PIXELS (font ? FONT_WIDTH ((XFontStruct *)font) : FRAME_COLUMN_WIDTH (it->f));
16954 #else
16955 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
16956 return OK_PIXELS (1);
16957 #endif
16958
16959 if (EQ (prop, Qtext))
16960 return OK_PIXELS (width_p
16961 ? window_box_width (it->w, TEXT_AREA)
16962 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
16963
16964 if (align_to && *align_to < 0)
16965 {
16966 *res = 0;
16967 if (EQ (prop, Qleft))
16968 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
16969 if (EQ (prop, Qright))
16970 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
16971 if (EQ (prop, Qcenter))
16972 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
16973 + window_box_width (it->w, TEXT_AREA) / 2);
16974 if (EQ (prop, Qleft_fringe))
16975 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
16976 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
16977 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
16978 if (EQ (prop, Qright_fringe))
16979 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
16980 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
16981 : window_box_right_offset (it->w, TEXT_AREA));
16982 if (EQ (prop, Qleft_margin))
16983 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
16984 if (EQ (prop, Qright_margin))
16985 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
16986 if (EQ (prop, Qscroll_bar))
16987 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
16988 ? 0
16989 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
16990 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
16991 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
16992 : 0)));
16993 }
16994 else
16995 {
16996 if (EQ (prop, Qleft_fringe))
16997 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
16998 if (EQ (prop, Qright_fringe))
16999 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
17000 if (EQ (prop, Qleft_margin))
17001 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
17002 if (EQ (prop, Qright_margin))
17003 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
17004 if (EQ (prop, Qscroll_bar))
17005 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
17006 }
17007
17008 prop = Fbuffer_local_value (prop, it->w->buffer);
17009 }
17010
17011 if (INTEGERP (prop) || FLOATP (prop))
17012 {
17013 int base_unit = (width_p
17014 ? FRAME_COLUMN_WIDTH (it->f)
17015 : FRAME_LINE_HEIGHT (it->f));
17016 return OK_PIXELS (XFLOATINT (prop) * base_unit);
17017 }
17018
17019 if (CONSP (prop))
17020 {
17021 Lisp_Object car = XCAR (prop);
17022 Lisp_Object cdr = XCDR (prop);
17023
17024 if (SYMBOLP (car))
17025 {
17026 #ifdef HAVE_WINDOW_SYSTEM
17027 if (valid_image_p (prop))
17028 {
17029 int id = lookup_image (it->f, prop);
17030 struct image *img = IMAGE_FROM_ID (it->f, id);
17031
17032 return OK_PIXELS (width_p ? img->width : img->height);
17033 }
17034 #endif
17035 if (EQ (car, Qplus) || EQ (car, Qminus))
17036 {
17037 int first = 1;
17038 double px;
17039
17040 pixels = 0;
17041 while (CONSP (cdr))
17042 {
17043 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
17044 font, width_p, align_to))
17045 return 0;
17046 if (first)
17047 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
17048 else
17049 pixels += px;
17050 cdr = XCDR (cdr);
17051 }
17052 if (EQ (car, Qminus))
17053 pixels = -pixels;
17054 return OK_PIXELS (pixels);
17055 }
17056
17057 car = Fbuffer_local_value (car, it->w->buffer);
17058 }
17059
17060 if (INTEGERP (car) || FLOATP (car))
17061 {
17062 double fact;
17063 pixels = XFLOATINT (car);
17064 if (NILP (cdr))
17065 return OK_PIXELS (pixels);
17066 if (calc_pixel_width_or_height (&fact, it, cdr,
17067 font, width_p, align_to))
17068 return OK_PIXELS (pixels * fact);
17069 return 0;
17070 }
17071
17072 return 0;
17073 }
17074
17075 return 0;
17076 }
17077
17078 \f
17079 /***********************************************************************
17080 Glyph Display
17081 ***********************************************************************/
17082
17083 #ifdef HAVE_WINDOW_SYSTEM
17084
17085 #if GLYPH_DEBUG
17086
17087 void
17088 dump_glyph_string (s)
17089 struct glyph_string *s;
17090 {
17091 fprintf (stderr, "glyph string\n");
17092 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
17093 s->x, s->y, s->width, s->height);
17094 fprintf (stderr, " ybase = %d\n", s->ybase);
17095 fprintf (stderr, " hl = %d\n", s->hl);
17096 fprintf (stderr, " left overhang = %d, right = %d\n",
17097 s->left_overhang, s->right_overhang);
17098 fprintf (stderr, " nchars = %d\n", s->nchars);
17099 fprintf (stderr, " extends to end of line = %d\n",
17100 s->extends_to_end_of_line_p);
17101 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
17102 fprintf (stderr, " bg width = %d\n", s->background_width);
17103 }
17104
17105 #endif /* GLYPH_DEBUG */
17106
17107 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
17108 of XChar2b structures for S; it can't be allocated in
17109 init_glyph_string because it must be allocated via `alloca'. W
17110 is the window on which S is drawn. ROW and AREA are the glyph row
17111 and area within the row from which S is constructed. START is the
17112 index of the first glyph structure covered by S. HL is a
17113 face-override for drawing S. */
17114
17115 #ifdef HAVE_NTGUI
17116 #define OPTIONAL_HDC(hdc) hdc,
17117 #define DECLARE_HDC(hdc) HDC hdc;
17118 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
17119 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
17120 #endif
17121
17122 #ifndef OPTIONAL_HDC
17123 #define OPTIONAL_HDC(hdc)
17124 #define DECLARE_HDC(hdc)
17125 #define ALLOCATE_HDC(hdc, f)
17126 #define RELEASE_HDC(hdc, f)
17127 #endif
17128
17129 static void
17130 init_glyph_string (s, OPTIONAL_HDC (hdc) char2b, w, row, area, start, hl)
17131 struct glyph_string *s;
17132 DECLARE_HDC (hdc)
17133 XChar2b *char2b;
17134 struct window *w;
17135 struct glyph_row *row;
17136 enum glyph_row_area area;
17137 int start;
17138 enum draw_glyphs_face hl;
17139 {
17140 bzero (s, sizeof *s);
17141 s->w = w;
17142 s->f = XFRAME (w->frame);
17143 #ifdef HAVE_NTGUI
17144 s->hdc = hdc;
17145 #endif
17146 s->display = FRAME_X_DISPLAY (s->f);
17147 s->window = FRAME_X_WINDOW (s->f);
17148 s->char2b = char2b;
17149 s->hl = hl;
17150 s->row = row;
17151 s->area = area;
17152 s->first_glyph = row->glyphs[area] + start;
17153 s->height = row->height;
17154 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
17155
17156 /* Display the internal border below the tool-bar window. */
17157 if (s->w == XWINDOW (s->f->tool_bar_window))
17158 s->y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
17159
17160 s->ybase = s->y + row->ascent;
17161 }
17162
17163
17164 /* Append the list of glyph strings with head H and tail T to the list
17165 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
17166
17167 static INLINE void
17168 append_glyph_string_lists (head, tail, h, t)
17169 struct glyph_string **head, **tail;
17170 struct glyph_string *h, *t;
17171 {
17172 if (h)
17173 {
17174 if (*head)
17175 (*tail)->next = h;
17176 else
17177 *head = h;
17178 h->prev = *tail;
17179 *tail = t;
17180 }
17181 }
17182
17183
17184 /* Prepend the list of glyph strings with head H and tail T to the
17185 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
17186 result. */
17187
17188 static INLINE void
17189 prepend_glyph_string_lists (head, tail, h, t)
17190 struct glyph_string **head, **tail;
17191 struct glyph_string *h, *t;
17192 {
17193 if (h)
17194 {
17195 if (*head)
17196 (*head)->prev = t;
17197 else
17198 *tail = t;
17199 t->next = *head;
17200 *head = h;
17201 }
17202 }
17203
17204
17205 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
17206 Set *HEAD and *TAIL to the resulting list. */
17207
17208 static INLINE void
17209 append_glyph_string (head, tail, s)
17210 struct glyph_string **head, **tail;
17211 struct glyph_string *s;
17212 {
17213 s->next = s->prev = NULL;
17214 append_glyph_string_lists (head, tail, s, s);
17215 }
17216
17217
17218 /* Get face and two-byte form of character glyph GLYPH on frame F.
17219 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
17220 a pointer to a realized face that is ready for display. */
17221
17222 static INLINE struct face *
17223 get_glyph_face_and_encoding (f, glyph, char2b, two_byte_p)
17224 struct frame *f;
17225 struct glyph *glyph;
17226 XChar2b *char2b;
17227 int *two_byte_p;
17228 {
17229 struct face *face;
17230
17231 xassert (glyph->type == CHAR_GLYPH);
17232 face = FACE_FROM_ID (f, glyph->face_id);
17233
17234 if (two_byte_p)
17235 *two_byte_p = 0;
17236
17237 if (!glyph->multibyte_p)
17238 {
17239 /* Unibyte case. We don't have to encode, but we have to make
17240 sure to use a face suitable for unibyte. */
17241 STORE_XCHAR2B (char2b, 0, glyph->u.ch);
17242 }
17243 else if (glyph->u.ch < 128
17244 && glyph->face_id < BASIC_FACE_ID_SENTINEL)
17245 {
17246 /* Case of ASCII in a face known to fit ASCII. */
17247 STORE_XCHAR2B (char2b, 0, glyph->u.ch);
17248 }
17249 else
17250 {
17251 int c1, c2, charset;
17252
17253 /* Split characters into bytes. If c2 is -1 afterwards, C is
17254 really a one-byte character so that byte1 is zero. */
17255 SPLIT_CHAR (glyph->u.ch, charset, c1, c2);
17256 if (c2 > 0)
17257 STORE_XCHAR2B (char2b, c1, c2);
17258 else
17259 STORE_XCHAR2B (char2b, 0, c1);
17260
17261 /* Maybe encode the character in *CHAR2B. */
17262 if (charset != CHARSET_ASCII)
17263 {
17264 struct font_info *font_info
17265 = FONT_INFO_FROM_ID (f, face->font_info_id);
17266 if (font_info)
17267 glyph->font_type
17268 = rif->encode_char (glyph->u.ch, char2b, font_info, two_byte_p);
17269 }
17270 }
17271
17272 /* Make sure X resources of the face are allocated. */
17273 xassert (face != NULL);
17274 PREPARE_FACE_FOR_DISPLAY (f, face);
17275 return face;
17276 }
17277
17278
17279 /* Fill glyph string S with composition components specified by S->cmp.
17280
17281 FACES is an array of faces for all components of this composition.
17282 S->gidx is the index of the first component for S.
17283 OVERLAPS_P non-zero means S should draw the foreground only, and
17284 use its physical height for clipping.
17285
17286 Value is the index of a component not in S. */
17287
17288 static int
17289 fill_composite_glyph_string (s, faces, overlaps_p)
17290 struct glyph_string *s;
17291 struct face **faces;
17292 int overlaps_p;
17293 {
17294 int i;
17295
17296 xassert (s);
17297
17298 s->for_overlaps_p = overlaps_p;
17299
17300 s->face = faces[s->gidx];
17301 s->font = s->face->font;
17302 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
17303
17304 /* For all glyphs of this composition, starting at the offset
17305 S->gidx, until we reach the end of the definition or encounter a
17306 glyph that requires the different face, add it to S. */
17307 ++s->nchars;
17308 for (i = s->gidx + 1; i < s->cmp->glyph_len && faces[i] == s->face; ++i)
17309 ++s->nchars;
17310
17311 /* All glyph strings for the same composition has the same width,
17312 i.e. the width set for the first component of the composition. */
17313
17314 s->width = s->first_glyph->pixel_width;
17315
17316 /* If the specified font could not be loaded, use the frame's
17317 default font, but record the fact that we couldn't load it in
17318 the glyph string so that we can draw rectangles for the
17319 characters of the glyph string. */
17320 if (s->font == NULL)
17321 {
17322 s->font_not_found_p = 1;
17323 s->font = FRAME_FONT (s->f);
17324 }
17325
17326 /* Adjust base line for subscript/superscript text. */
17327 s->ybase += s->first_glyph->voffset;
17328
17329 xassert (s->face && s->face->gc);
17330
17331 /* This glyph string must always be drawn with 16-bit functions. */
17332 s->two_byte_p = 1;
17333
17334 return s->gidx + s->nchars;
17335 }
17336
17337
17338 /* Fill glyph string S from a sequence of character glyphs.
17339
17340 FACE_ID is the face id of the string. START is the index of the
17341 first glyph to consider, END is the index of the last + 1.
17342 OVERLAPS_P non-zero means S should draw the foreground only, and
17343 use its physical height for clipping.
17344
17345 Value is the index of the first glyph not in S. */
17346
17347 static int
17348 fill_glyph_string (s, face_id, start, end, overlaps_p)
17349 struct glyph_string *s;
17350 int face_id;
17351 int start, end, overlaps_p;
17352 {
17353 struct glyph *glyph, *last;
17354 int voffset;
17355 int glyph_not_available_p;
17356
17357 xassert (s->f == XFRAME (s->w->frame));
17358 xassert (s->nchars == 0);
17359 xassert (start >= 0 && end > start);
17360
17361 s->for_overlaps_p = overlaps_p,
17362 glyph = s->row->glyphs[s->area] + start;
17363 last = s->row->glyphs[s->area] + end;
17364 voffset = glyph->voffset;
17365
17366 glyph_not_available_p = glyph->glyph_not_available_p;
17367
17368 while (glyph < last
17369 && glyph->type == CHAR_GLYPH
17370 && glyph->voffset == voffset
17371 /* Same face id implies same font, nowadays. */
17372 && glyph->face_id == face_id
17373 && glyph->glyph_not_available_p == glyph_not_available_p)
17374 {
17375 int two_byte_p;
17376
17377 s->face = get_glyph_face_and_encoding (s->f, glyph,
17378 s->char2b + s->nchars,
17379 &two_byte_p);
17380 s->two_byte_p = two_byte_p;
17381 ++s->nchars;
17382 xassert (s->nchars <= end - start);
17383 s->width += glyph->pixel_width;
17384 ++glyph;
17385 }
17386
17387 s->font = s->face->font;
17388 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
17389
17390 /* If the specified font could not be loaded, use the frame's font,
17391 but record the fact that we couldn't load it in
17392 S->font_not_found_p so that we can draw rectangles for the
17393 characters of the glyph string. */
17394 if (s->font == NULL || glyph_not_available_p)
17395 {
17396 s->font_not_found_p = 1;
17397 s->font = FRAME_FONT (s->f);
17398 }
17399
17400 /* Adjust base line for subscript/superscript text. */
17401 s->ybase += voffset;
17402
17403 xassert (s->face && s->face->gc);
17404 return glyph - s->row->glyphs[s->area];
17405 }
17406
17407
17408 /* Fill glyph string S from image glyph S->first_glyph. */
17409
17410 static void
17411 fill_image_glyph_string (s)
17412 struct glyph_string *s;
17413 {
17414 xassert (s->first_glyph->type == IMAGE_GLYPH);
17415 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
17416 xassert (s->img);
17417 s->slice = s->first_glyph->slice;
17418 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
17419 s->font = s->face->font;
17420 s->width = s->first_glyph->pixel_width;
17421
17422 /* Adjust base line for subscript/superscript text. */
17423 s->ybase += s->first_glyph->voffset;
17424 }
17425
17426
17427 /* Fill glyph string S from a sequence of stretch glyphs.
17428
17429 ROW is the glyph row in which the glyphs are found, AREA is the
17430 area within the row. START is the index of the first glyph to
17431 consider, END is the index of the last + 1.
17432
17433 Value is the index of the first glyph not in S. */
17434
17435 static int
17436 fill_stretch_glyph_string (s, row, area, start, end)
17437 struct glyph_string *s;
17438 struct glyph_row *row;
17439 enum glyph_row_area area;
17440 int start, end;
17441 {
17442 struct glyph *glyph, *last;
17443 int voffset, face_id;
17444
17445 xassert (s->first_glyph->type == STRETCH_GLYPH);
17446
17447 glyph = s->row->glyphs[s->area] + start;
17448 last = s->row->glyphs[s->area] + end;
17449 face_id = glyph->face_id;
17450 s->face = FACE_FROM_ID (s->f, face_id);
17451 s->font = s->face->font;
17452 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
17453 s->width = glyph->pixel_width;
17454 voffset = glyph->voffset;
17455
17456 for (++glyph;
17457 (glyph < last
17458 && glyph->type == STRETCH_GLYPH
17459 && glyph->voffset == voffset
17460 && glyph->face_id == face_id);
17461 ++glyph)
17462 s->width += glyph->pixel_width;
17463
17464 /* Adjust base line for subscript/superscript text. */
17465 s->ybase += voffset;
17466
17467 /* The case that face->gc == 0 is handled when drawing the glyph
17468 string by calling PREPARE_FACE_FOR_DISPLAY. */
17469 xassert (s->face);
17470 return glyph - s->row->glyphs[s->area];
17471 }
17472
17473
17474 /* EXPORT for RIF:
17475 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
17476 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
17477 assumed to be zero. */
17478
17479 void
17480 x_get_glyph_overhangs (glyph, f, left, right)
17481 struct glyph *glyph;
17482 struct frame *f;
17483 int *left, *right;
17484 {
17485 *left = *right = 0;
17486
17487 if (glyph->type == CHAR_GLYPH)
17488 {
17489 XFontStruct *font;
17490 struct face *face;
17491 struct font_info *font_info;
17492 XChar2b char2b;
17493 XCharStruct *pcm;
17494
17495 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
17496 font = face->font;
17497 font_info = FONT_INFO_FROM_ID (f, face->font_info_id);
17498 if (font /* ++KFS: Should this be font_info ? */
17499 && (pcm = rif->per_char_metric (font, &char2b, glyph->font_type)))
17500 {
17501 if (pcm->rbearing > pcm->width)
17502 *right = pcm->rbearing - pcm->width;
17503 if (pcm->lbearing < 0)
17504 *left = -pcm->lbearing;
17505 }
17506 }
17507 }
17508
17509
17510 /* Return the index of the first glyph preceding glyph string S that
17511 is overwritten by S because of S's left overhang. Value is -1
17512 if no glyphs are overwritten. */
17513
17514 static int
17515 left_overwritten (s)
17516 struct glyph_string *s;
17517 {
17518 int k;
17519
17520 if (s->left_overhang)
17521 {
17522 int x = 0, i;
17523 struct glyph *glyphs = s->row->glyphs[s->area];
17524 int first = s->first_glyph - glyphs;
17525
17526 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
17527 x -= glyphs[i].pixel_width;
17528
17529 k = i + 1;
17530 }
17531 else
17532 k = -1;
17533
17534 return k;
17535 }
17536
17537
17538 /* Return the index of the first glyph preceding glyph string S that
17539 is overwriting S because of its right overhang. Value is -1 if no
17540 glyph in front of S overwrites S. */
17541
17542 static int
17543 left_overwriting (s)
17544 struct glyph_string *s;
17545 {
17546 int i, k, x;
17547 struct glyph *glyphs = s->row->glyphs[s->area];
17548 int first = s->first_glyph - glyphs;
17549
17550 k = -1;
17551 x = 0;
17552 for (i = first - 1; i >= 0; --i)
17553 {
17554 int left, right;
17555 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
17556 if (x + right > 0)
17557 k = i;
17558 x -= glyphs[i].pixel_width;
17559 }
17560
17561 return k;
17562 }
17563
17564
17565 /* Return the index of the last glyph following glyph string S that is
17566 not overwritten by S because of S's right overhang. Value is -1 if
17567 no such glyph is found. */
17568
17569 static int
17570 right_overwritten (s)
17571 struct glyph_string *s;
17572 {
17573 int k = -1;
17574
17575 if (s->right_overhang)
17576 {
17577 int x = 0, i;
17578 struct glyph *glyphs = s->row->glyphs[s->area];
17579 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
17580 int end = s->row->used[s->area];
17581
17582 for (i = first; i < end && s->right_overhang > x; ++i)
17583 x += glyphs[i].pixel_width;
17584
17585 k = i;
17586 }
17587
17588 return k;
17589 }
17590
17591
17592 /* Return the index of the last glyph following glyph string S that
17593 overwrites S because of its left overhang. Value is negative
17594 if no such glyph is found. */
17595
17596 static int
17597 right_overwriting (s)
17598 struct glyph_string *s;
17599 {
17600 int i, k, x;
17601 int end = s->row->used[s->area];
17602 struct glyph *glyphs = s->row->glyphs[s->area];
17603 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
17604
17605 k = -1;
17606 x = 0;
17607 for (i = first; i < end; ++i)
17608 {
17609 int left, right;
17610 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
17611 if (x - left < 0)
17612 k = i;
17613 x += glyphs[i].pixel_width;
17614 }
17615
17616 return k;
17617 }
17618
17619
17620 /* Get face and two-byte form of character C in face FACE_ID on frame
17621 F. The encoding of C is returned in *CHAR2B. MULTIBYTE_P non-zero
17622 means we want to display multibyte text. DISPLAY_P non-zero means
17623 make sure that X resources for the face returned are allocated.
17624 Value is a pointer to a realized face that is ready for display if
17625 DISPLAY_P is non-zero. */
17626
17627 static INLINE struct face *
17628 get_char_face_and_encoding (f, c, face_id, char2b, multibyte_p, display_p)
17629 struct frame *f;
17630 int c, face_id;
17631 XChar2b *char2b;
17632 int multibyte_p, display_p;
17633 {
17634 struct face *face = FACE_FROM_ID (f, face_id);
17635
17636 if (!multibyte_p)
17637 {
17638 /* Unibyte case. We don't have to encode, but we have to make
17639 sure to use a face suitable for unibyte. */
17640 STORE_XCHAR2B (char2b, 0, c);
17641 face_id = FACE_FOR_CHAR (f, face, c);
17642 face = FACE_FROM_ID (f, face_id);
17643 }
17644 else if (c < 128 && face_id < BASIC_FACE_ID_SENTINEL)
17645 {
17646 /* Case of ASCII in a face known to fit ASCII. */
17647 STORE_XCHAR2B (char2b, 0, c);
17648 }
17649 else
17650 {
17651 int c1, c2, charset;
17652
17653 /* Split characters into bytes. If c2 is -1 afterwards, C is
17654 really a one-byte character so that byte1 is zero. */
17655 SPLIT_CHAR (c, charset, c1, c2);
17656 if (c2 > 0)
17657 STORE_XCHAR2B (char2b, c1, c2);
17658 else
17659 STORE_XCHAR2B (char2b, 0, c1);
17660
17661 /* Maybe encode the character in *CHAR2B. */
17662 if (face->font != NULL)
17663 {
17664 struct font_info *font_info
17665 = FONT_INFO_FROM_ID (f, face->font_info_id);
17666 if (font_info)
17667 rif->encode_char (c, char2b, font_info, 0);
17668 }
17669 }
17670
17671 /* Make sure X resources of the face are allocated. */
17672 #ifdef HAVE_X_WINDOWS
17673 if (display_p)
17674 #endif
17675 {
17676 xassert (face != NULL);
17677 PREPARE_FACE_FOR_DISPLAY (f, face);
17678 }
17679
17680 return face;
17681 }
17682
17683
17684 /* Set background width of glyph string S. START is the index of the
17685 first glyph following S. LAST_X is the right-most x-position + 1
17686 in the drawing area. */
17687
17688 static INLINE void
17689 set_glyph_string_background_width (s, start, last_x)
17690 struct glyph_string *s;
17691 int start;
17692 int last_x;
17693 {
17694 /* If the face of this glyph string has to be drawn to the end of
17695 the drawing area, set S->extends_to_end_of_line_p. */
17696 struct face *default_face = FACE_FROM_ID (s->f, DEFAULT_FACE_ID);
17697
17698 if (start == s->row->used[s->area]
17699 && s->area == TEXT_AREA
17700 && ((s->hl == DRAW_NORMAL_TEXT
17701 && (s->row->fill_line_p
17702 || s->face->background != default_face->background
17703 || s->face->stipple != default_face->stipple
17704 || s->row->mouse_face_p))
17705 || s->hl == DRAW_MOUSE_FACE
17706 || ((s->hl == DRAW_IMAGE_RAISED || s->hl == DRAW_IMAGE_SUNKEN)
17707 && s->row->fill_line_p)))
17708 s->extends_to_end_of_line_p = 1;
17709
17710 /* If S extends its face to the end of the line, set its
17711 background_width to the distance to the right edge of the drawing
17712 area. */
17713 if (s->extends_to_end_of_line_p)
17714 s->background_width = last_x - s->x + 1;
17715 else
17716 s->background_width = s->width;
17717 }
17718
17719
17720 /* Compute overhangs and x-positions for glyph string S and its
17721 predecessors, or successors. X is the starting x-position for S.
17722 BACKWARD_P non-zero means process predecessors. */
17723
17724 static void
17725 compute_overhangs_and_x (s, x, backward_p)
17726 struct glyph_string *s;
17727 int x;
17728 int backward_p;
17729 {
17730 if (backward_p)
17731 {
17732 while (s)
17733 {
17734 if (rif->compute_glyph_string_overhangs)
17735 rif->compute_glyph_string_overhangs (s);
17736 x -= s->width;
17737 s->x = x;
17738 s = s->prev;
17739 }
17740 }
17741 else
17742 {
17743 while (s)
17744 {
17745 if (rif->compute_glyph_string_overhangs)
17746 rif->compute_glyph_string_overhangs (s);
17747 s->x = x;
17748 x += s->width;
17749 s = s->next;
17750 }
17751 }
17752 }
17753
17754
17755
17756 /* The following macros are only called from draw_glyphs below.
17757 They reference the following parameters of that function directly:
17758 `w', `row', `area', and `overlap_p'
17759 as well as the following local variables:
17760 `s', `f', and `hdc' (in W32) */
17761
17762 #ifdef HAVE_NTGUI
17763 /* On W32, silently add local `hdc' variable to argument list of
17764 init_glyph_string. */
17765 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
17766 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
17767 #else
17768 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
17769 init_glyph_string (s, char2b, w, row, area, start, hl)
17770 #endif
17771
17772 /* Add a glyph string for a stretch glyph to the list of strings
17773 between HEAD and TAIL. START is the index of the stretch glyph in
17774 row area AREA of glyph row ROW. END is the index of the last glyph
17775 in that glyph row area. X is the current output position assigned
17776 to the new glyph string constructed. HL overrides that face of the
17777 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
17778 is the right-most x-position of the drawing area. */
17779
17780 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
17781 and below -- keep them on one line. */
17782 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
17783 do \
17784 { \
17785 s = (struct glyph_string *) alloca (sizeof *s); \
17786 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
17787 START = fill_stretch_glyph_string (s, row, area, START, END); \
17788 append_glyph_string (&HEAD, &TAIL, s); \
17789 s->x = (X); \
17790 } \
17791 while (0)
17792
17793
17794 /* Add a glyph string for an image glyph to the list of strings
17795 between HEAD and TAIL. START is the index of the image glyph in
17796 row area AREA of glyph row ROW. END is the index of the last glyph
17797 in that glyph row area. X is the current output position assigned
17798 to the new glyph string constructed. HL overrides that face of the
17799 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
17800 is the right-most x-position of the drawing area. */
17801
17802 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
17803 do \
17804 { \
17805 s = (struct glyph_string *) alloca (sizeof *s); \
17806 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
17807 fill_image_glyph_string (s); \
17808 append_glyph_string (&HEAD, &TAIL, s); \
17809 ++START; \
17810 s->x = (X); \
17811 } \
17812 while (0)
17813
17814
17815 /* Add a glyph string for a sequence of character glyphs to the list
17816 of strings between HEAD and TAIL. START is the index of the first
17817 glyph in row area AREA of glyph row ROW that is part of the new
17818 glyph string. END is the index of the last glyph in that glyph row
17819 area. X is the current output position assigned to the new glyph
17820 string constructed. HL overrides that face of the glyph; e.g. it
17821 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
17822 right-most x-position of the drawing area. */
17823
17824 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
17825 do \
17826 { \
17827 int c, face_id; \
17828 XChar2b *char2b; \
17829 \
17830 c = (row)->glyphs[area][START].u.ch; \
17831 face_id = (row)->glyphs[area][START].face_id; \
17832 \
17833 s = (struct glyph_string *) alloca (sizeof *s); \
17834 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
17835 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
17836 append_glyph_string (&HEAD, &TAIL, s); \
17837 s->x = (X); \
17838 START = fill_glyph_string (s, face_id, START, END, overlaps_p); \
17839 } \
17840 while (0)
17841
17842
17843 /* Add a glyph string for a composite sequence to the list of strings
17844 between HEAD and TAIL. START is the index of the first glyph in
17845 row area AREA of glyph row ROW that is part of the new glyph
17846 string. END is the index of the last glyph in that glyph row area.
17847 X is the current output position assigned to the new glyph string
17848 constructed. HL overrides that face of the glyph; e.g. it is
17849 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
17850 x-position of the drawing area. */
17851
17852 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
17853 do { \
17854 int cmp_id = (row)->glyphs[area][START].u.cmp_id; \
17855 int face_id = (row)->glyphs[area][START].face_id; \
17856 struct face *base_face = FACE_FROM_ID (f, face_id); \
17857 struct composition *cmp = composition_table[cmp_id]; \
17858 int glyph_len = cmp->glyph_len; \
17859 XChar2b *char2b; \
17860 struct face **faces; \
17861 struct glyph_string *first_s = NULL; \
17862 int n; \
17863 \
17864 base_face = base_face->ascii_face; \
17865 char2b = (XChar2b *) alloca ((sizeof *char2b) * glyph_len); \
17866 faces = (struct face **) alloca ((sizeof *faces) * glyph_len); \
17867 /* At first, fill in `char2b' and `faces'. */ \
17868 for (n = 0; n < glyph_len; n++) \
17869 { \
17870 int c = COMPOSITION_GLYPH (cmp, n); \
17871 int this_face_id = FACE_FOR_CHAR (f, base_face, c); \
17872 faces[n] = FACE_FROM_ID (f, this_face_id); \
17873 get_char_face_and_encoding (f, c, this_face_id, \
17874 char2b + n, 1, 1); \
17875 } \
17876 \
17877 /* Make glyph_strings for each glyph sequence that is drawable by \
17878 the same face, and append them to HEAD/TAIL. */ \
17879 for (n = 0; n < cmp->glyph_len;) \
17880 { \
17881 s = (struct glyph_string *) alloca (sizeof *s); \
17882 INIT_GLYPH_STRING (s, char2b + n, w, row, area, START, HL); \
17883 append_glyph_string (&(HEAD), &(TAIL), s); \
17884 s->cmp = cmp; \
17885 s->gidx = n; \
17886 s->x = (X); \
17887 \
17888 if (n == 0) \
17889 first_s = s; \
17890 \
17891 n = fill_composite_glyph_string (s, faces, overlaps_p); \
17892 } \
17893 \
17894 ++START; \
17895 s = first_s; \
17896 } while (0)
17897
17898
17899 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
17900 of AREA of glyph row ROW on window W between indices START and END.
17901 HL overrides the face for drawing glyph strings, e.g. it is
17902 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
17903 x-positions of the drawing area.
17904
17905 This is an ugly monster macro construct because we must use alloca
17906 to allocate glyph strings (because draw_glyphs can be called
17907 asynchronously). */
17908
17909 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
17910 do \
17911 { \
17912 HEAD = TAIL = NULL; \
17913 while (START < END) \
17914 { \
17915 struct glyph *first_glyph = (row)->glyphs[area] + START; \
17916 switch (first_glyph->type) \
17917 { \
17918 case CHAR_GLYPH: \
17919 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
17920 HL, X, LAST_X); \
17921 break; \
17922 \
17923 case COMPOSITE_GLYPH: \
17924 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
17925 HL, X, LAST_X); \
17926 break; \
17927 \
17928 case STRETCH_GLYPH: \
17929 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
17930 HL, X, LAST_X); \
17931 break; \
17932 \
17933 case IMAGE_GLYPH: \
17934 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
17935 HL, X, LAST_X); \
17936 break; \
17937 \
17938 default: \
17939 abort (); \
17940 } \
17941 \
17942 set_glyph_string_background_width (s, START, LAST_X); \
17943 (X) += s->width; \
17944 } \
17945 } \
17946 while (0)
17947
17948
17949 /* Draw glyphs between START and END in AREA of ROW on window W,
17950 starting at x-position X. X is relative to AREA in W. HL is a
17951 face-override with the following meaning:
17952
17953 DRAW_NORMAL_TEXT draw normally
17954 DRAW_CURSOR draw in cursor face
17955 DRAW_MOUSE_FACE draw in mouse face.
17956 DRAW_INVERSE_VIDEO draw in mode line face
17957 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
17958 DRAW_IMAGE_RAISED draw an image with a raised relief around it
17959
17960 If OVERLAPS_P is non-zero, draw only the foreground of characters
17961 and clip to the physical height of ROW.
17962
17963 Value is the x-position reached, relative to AREA of W. */
17964
17965 static int
17966 draw_glyphs (w, x, row, area, start, end, hl, overlaps_p)
17967 struct window *w;
17968 int x;
17969 struct glyph_row *row;
17970 enum glyph_row_area area;
17971 int start, end;
17972 enum draw_glyphs_face hl;
17973 int overlaps_p;
17974 {
17975 struct glyph_string *head, *tail;
17976 struct glyph_string *s;
17977 int last_x, area_width;
17978 int x_reached;
17979 int i, j;
17980 struct frame *f = XFRAME (WINDOW_FRAME (w));
17981 DECLARE_HDC (hdc);
17982
17983 ALLOCATE_HDC (hdc, f);
17984
17985 /* Let's rather be paranoid than getting a SEGV. */
17986 end = min (end, row->used[area]);
17987 start = max (0, start);
17988 start = min (end, start);
17989
17990 /* Translate X to frame coordinates. Set last_x to the right
17991 end of the drawing area. */
17992 if (row->full_width_p)
17993 {
17994 /* X is relative to the left edge of W, without scroll bars
17995 or fringes. */
17996 x += WINDOW_LEFT_EDGE_X (w);
17997 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
17998 }
17999 else
18000 {
18001 int area_left = window_box_left (w, area);
18002 x += area_left;
18003 area_width = window_box_width (w, area);
18004 last_x = area_left + area_width;
18005 }
18006
18007 /* Build a doubly-linked list of glyph_string structures between
18008 head and tail from what we have to draw. Note that the macro
18009 BUILD_GLYPH_STRINGS will modify its start parameter. That's
18010 the reason we use a separate variable `i'. */
18011 i = start;
18012 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
18013 if (tail)
18014 x_reached = tail->x + tail->background_width;
18015 else
18016 x_reached = x;
18017
18018 /* If there are any glyphs with lbearing < 0 or rbearing > width in
18019 the row, redraw some glyphs in front or following the glyph
18020 strings built above. */
18021 if (head && !overlaps_p && row->contains_overlapping_glyphs_p)
18022 {
18023 int dummy_x = 0;
18024 struct glyph_string *h, *t;
18025
18026 /* Compute overhangs for all glyph strings. */
18027 if (rif->compute_glyph_string_overhangs)
18028 for (s = head; s; s = s->next)
18029 rif->compute_glyph_string_overhangs (s);
18030
18031 /* Prepend glyph strings for glyphs in front of the first glyph
18032 string that are overwritten because of the first glyph
18033 string's left overhang. The background of all strings
18034 prepended must be drawn because the first glyph string
18035 draws over it. */
18036 i = left_overwritten (head);
18037 if (i >= 0)
18038 {
18039 j = i;
18040 BUILD_GLYPH_STRINGS (j, start, h, t,
18041 DRAW_NORMAL_TEXT, dummy_x, last_x);
18042 start = i;
18043 compute_overhangs_and_x (t, head->x, 1);
18044 prepend_glyph_string_lists (&head, &tail, h, t);
18045 }
18046
18047 /* Prepend glyph strings for glyphs in front of the first glyph
18048 string that overwrite that glyph string because of their
18049 right overhang. For these strings, only the foreground must
18050 be drawn, because it draws over the glyph string at `head'.
18051 The background must not be drawn because this would overwrite
18052 right overhangs of preceding glyphs for which no glyph
18053 strings exist. */
18054 i = left_overwriting (head);
18055 if (i >= 0)
18056 {
18057 BUILD_GLYPH_STRINGS (i, start, h, t,
18058 DRAW_NORMAL_TEXT, dummy_x, last_x);
18059 for (s = h; s; s = s->next)
18060 s->background_filled_p = 1;
18061 compute_overhangs_and_x (t, head->x, 1);
18062 prepend_glyph_string_lists (&head, &tail, h, t);
18063 }
18064
18065 /* Append glyphs strings for glyphs following the last glyph
18066 string tail that are overwritten by tail. The background of
18067 these strings has to be drawn because tail's foreground draws
18068 over it. */
18069 i = right_overwritten (tail);
18070 if (i >= 0)
18071 {
18072 BUILD_GLYPH_STRINGS (end, i, h, t,
18073 DRAW_NORMAL_TEXT, x, last_x);
18074 compute_overhangs_and_x (h, tail->x + tail->width, 0);
18075 append_glyph_string_lists (&head, &tail, h, t);
18076 }
18077
18078 /* Append glyph strings for glyphs following the last glyph
18079 string tail that overwrite tail. The foreground of such
18080 glyphs has to be drawn because it writes into the background
18081 of tail. The background must not be drawn because it could
18082 paint over the foreground of following glyphs. */
18083 i = right_overwriting (tail);
18084 if (i >= 0)
18085 {
18086 BUILD_GLYPH_STRINGS (end, i, h, t,
18087 DRAW_NORMAL_TEXT, x, last_x);
18088 for (s = h; s; s = s->next)
18089 s->background_filled_p = 1;
18090 compute_overhangs_and_x (h, tail->x + tail->width, 0);
18091 append_glyph_string_lists (&head, &tail, h, t);
18092 }
18093 }
18094
18095 /* Draw all strings. */
18096 for (s = head; s; s = s->next)
18097 rif->draw_glyph_string (s);
18098
18099 if (area == TEXT_AREA
18100 && !row->full_width_p
18101 /* When drawing overlapping rows, only the glyph strings'
18102 foreground is drawn, which doesn't erase a cursor
18103 completely. */
18104 && !overlaps_p)
18105 {
18106 int x0 = head ? head->x : x;
18107 int x1 = tail ? tail->x + tail->background_width : x;
18108
18109 int text_left = window_box_left (w, TEXT_AREA);
18110 x0 -= text_left;
18111 x1 -= text_left;
18112
18113 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
18114 row->y, MATRIX_ROW_BOTTOM_Y (row));
18115 }
18116
18117 /* Value is the x-position up to which drawn, relative to AREA of W.
18118 This doesn't include parts drawn because of overhangs. */
18119 if (row->full_width_p)
18120 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
18121 else
18122 x_reached -= window_box_left (w, area);
18123
18124 RELEASE_HDC (hdc, f);
18125
18126 return x_reached;
18127 }
18128
18129
18130 /* Store one glyph for IT->char_to_display in IT->glyph_row.
18131 Called from x_produce_glyphs when IT->glyph_row is non-null. */
18132
18133 static INLINE void
18134 append_glyph (it)
18135 struct it *it;
18136 {
18137 struct glyph *glyph;
18138 enum glyph_row_area area = it->area;
18139
18140 xassert (it->glyph_row);
18141 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
18142
18143 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
18144 if (glyph < it->glyph_row->glyphs[area + 1])
18145 {
18146 glyph->charpos = CHARPOS (it->position);
18147 glyph->object = it->object;
18148 glyph->pixel_width = it->pixel_width;
18149 glyph->ascent = it->ascent;
18150 glyph->descent = it->descent;
18151 glyph->voffset = it->voffset;
18152 glyph->type = CHAR_GLYPH;
18153 glyph->multibyte_p = it->multibyte_p;
18154 glyph->left_box_line_p = it->start_of_box_run_p;
18155 glyph->right_box_line_p = it->end_of_box_run_p;
18156 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
18157 || it->phys_descent > it->descent);
18158 glyph->padding_p = 0;
18159 glyph->glyph_not_available_p = it->glyph_not_available_p;
18160 glyph->face_id = it->face_id;
18161 glyph->u.ch = it->char_to_display;
18162 glyph->slice = null_glyph_slice;
18163 glyph->font_type = FONT_TYPE_UNKNOWN;
18164 ++it->glyph_row->used[area];
18165 }
18166 else if (!fonts_changed_p)
18167 {
18168 it->w->ncols_scale_factor++;
18169 fonts_changed_p = 1;
18170 }
18171 }
18172
18173 /* Store one glyph for the composition IT->cmp_id in IT->glyph_row.
18174 Called from x_produce_glyphs when IT->glyph_row is non-null. */
18175
18176 static INLINE void
18177 append_composite_glyph (it)
18178 struct it *it;
18179 {
18180 struct glyph *glyph;
18181 enum glyph_row_area area = it->area;
18182
18183 xassert (it->glyph_row);
18184
18185 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
18186 if (glyph < it->glyph_row->glyphs[area + 1])
18187 {
18188 glyph->charpos = CHARPOS (it->position);
18189 glyph->object = it->object;
18190 glyph->pixel_width = it->pixel_width;
18191 glyph->ascent = it->ascent;
18192 glyph->descent = it->descent;
18193 glyph->voffset = it->voffset;
18194 glyph->type = COMPOSITE_GLYPH;
18195 glyph->multibyte_p = it->multibyte_p;
18196 glyph->left_box_line_p = it->start_of_box_run_p;
18197 glyph->right_box_line_p = it->end_of_box_run_p;
18198 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
18199 || it->phys_descent > it->descent);
18200 glyph->padding_p = 0;
18201 glyph->glyph_not_available_p = 0;
18202 glyph->face_id = it->face_id;
18203 glyph->u.cmp_id = it->cmp_id;
18204 glyph->slice = null_glyph_slice;
18205 glyph->font_type = FONT_TYPE_UNKNOWN;
18206 ++it->glyph_row->used[area];
18207 }
18208 else if (!fonts_changed_p)
18209 {
18210 it->w->ncols_scale_factor++;
18211 fonts_changed_p = 1;
18212 }
18213 }
18214
18215
18216 /* Change IT->ascent and IT->height according to the setting of
18217 IT->voffset. */
18218
18219 static INLINE void
18220 take_vertical_position_into_account (it)
18221 struct it *it;
18222 {
18223 if (it->voffset)
18224 {
18225 if (it->voffset < 0)
18226 /* Increase the ascent so that we can display the text higher
18227 in the line. */
18228 it->ascent -= it->voffset;
18229 else
18230 /* Increase the descent so that we can display the text lower
18231 in the line. */
18232 it->descent += it->voffset;
18233 }
18234 }
18235
18236
18237 /* Produce glyphs/get display metrics for the image IT is loaded with.
18238 See the description of struct display_iterator in dispextern.h for
18239 an overview of struct display_iterator. */
18240
18241 static void
18242 produce_image_glyph (it)
18243 struct it *it;
18244 {
18245 struct image *img;
18246 struct face *face;
18247 int face_ascent, glyph_ascent;
18248 struct glyph_slice slice;
18249
18250 xassert (it->what == IT_IMAGE);
18251
18252 face = FACE_FROM_ID (it->f, it->face_id);
18253 xassert (face);
18254 /* Make sure X resources of the face is loaded. */
18255 PREPARE_FACE_FOR_DISPLAY (it->f, face);
18256
18257 if (it->image_id < 0)
18258 {
18259 /* Fringe bitmap. */
18260 it->ascent = it->phys_ascent = 0;
18261 it->descent = it->phys_descent = 0;
18262 it->pixel_width = 0;
18263 it->nglyphs = 0;
18264 return;
18265 }
18266
18267 img = IMAGE_FROM_ID (it->f, it->image_id);
18268 xassert (img);
18269 /* Make sure X resources of the image is loaded. */
18270 prepare_image_for_display (it->f, img);
18271
18272 slice.x = slice.y = 0;
18273 slice.width = img->width;
18274 slice.height = img->height;
18275
18276 if (INTEGERP (it->slice.x))
18277 slice.x = XINT (it->slice.x);
18278 else if (FLOATP (it->slice.x))
18279 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
18280
18281 if (INTEGERP (it->slice.y))
18282 slice.y = XINT (it->slice.y);
18283 else if (FLOATP (it->slice.y))
18284 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
18285
18286 if (INTEGERP (it->slice.width))
18287 slice.width = XINT (it->slice.width);
18288 else if (FLOATP (it->slice.width))
18289 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
18290
18291 if (INTEGERP (it->slice.height))
18292 slice.height = XINT (it->slice.height);
18293 else if (FLOATP (it->slice.height))
18294 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
18295
18296 if (slice.x >= img->width)
18297 slice.x = img->width;
18298 if (slice.y >= img->height)
18299 slice.y = img->height;
18300 if (slice.x + slice.width >= img->width)
18301 slice.width = img->width - slice.x;
18302 if (slice.y + slice.height > img->height)
18303 slice.height = img->height - slice.y;
18304
18305 if (slice.width == 0 || slice.height == 0)
18306 return;
18307
18308 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
18309
18310 it->descent = slice.height - glyph_ascent;
18311 if (slice.y == 0)
18312 it->descent += img->vmargin;
18313 if (slice.y + slice.height == img->height)
18314 it->descent += img->vmargin;
18315 it->phys_descent = it->descent;
18316
18317 it->pixel_width = slice.width;
18318 if (slice.x == 0)
18319 it->pixel_width += img->hmargin;
18320 if (slice.x + slice.width == img->width)
18321 it->pixel_width += img->hmargin;
18322
18323 /* It's quite possible for images to have an ascent greater than
18324 their height, so don't get confused in that case. */
18325 if (it->descent < 0)
18326 it->descent = 0;
18327
18328 #if 0 /* this breaks image tiling */
18329 /* If this glyph is alone on the last line, adjust it.ascent to minimum row ascent. */
18330 face_ascent = face->font ? FONT_BASE (face->font) : FRAME_BASELINE_OFFSET (it->f);
18331 if (face_ascent > it->ascent)
18332 it->ascent = it->phys_ascent = face_ascent;
18333 #endif
18334
18335 it->nglyphs = 1;
18336
18337 if (face->box != FACE_NO_BOX)
18338 {
18339 if (face->box_line_width > 0)
18340 {
18341 if (slice.y == 0)
18342 it->ascent += face->box_line_width;
18343 if (slice.y + slice.height == img->height)
18344 it->descent += face->box_line_width;
18345 }
18346
18347 if (it->start_of_box_run_p && slice.x == 0)
18348 it->pixel_width += abs (face->box_line_width);
18349 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
18350 it->pixel_width += abs (face->box_line_width);
18351 }
18352
18353 take_vertical_position_into_account (it);
18354
18355 if (it->glyph_row)
18356 {
18357 struct glyph *glyph;
18358 enum glyph_row_area area = it->area;
18359
18360 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
18361 if (glyph < it->glyph_row->glyphs[area + 1])
18362 {
18363 glyph->charpos = CHARPOS (it->position);
18364 glyph->object = it->object;
18365 glyph->pixel_width = it->pixel_width;
18366 glyph->ascent = glyph_ascent;
18367 glyph->descent = it->descent;
18368 glyph->voffset = it->voffset;
18369 glyph->type = IMAGE_GLYPH;
18370 glyph->multibyte_p = it->multibyte_p;
18371 glyph->left_box_line_p = it->start_of_box_run_p;
18372 glyph->right_box_line_p = it->end_of_box_run_p;
18373 glyph->overlaps_vertically_p = 0;
18374 glyph->padding_p = 0;
18375 glyph->glyph_not_available_p = 0;
18376 glyph->face_id = it->face_id;
18377 glyph->u.img_id = img->id;
18378 glyph->slice = slice;
18379 glyph->font_type = FONT_TYPE_UNKNOWN;
18380 ++it->glyph_row->used[area];
18381 }
18382 else if (!fonts_changed_p)
18383 {
18384 it->w->ncols_scale_factor++;
18385 fonts_changed_p = 1;
18386 }
18387 }
18388 }
18389
18390
18391 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
18392 of the glyph, WIDTH and HEIGHT are the width and height of the
18393 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
18394
18395 static void
18396 append_stretch_glyph (it, object, width, height, ascent)
18397 struct it *it;
18398 Lisp_Object object;
18399 int width, height;
18400 int ascent;
18401 {
18402 struct glyph *glyph;
18403 enum glyph_row_area area = it->area;
18404
18405 xassert (ascent >= 0 && ascent <= height);
18406
18407 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
18408 if (glyph < it->glyph_row->glyphs[area + 1])
18409 {
18410 glyph->charpos = CHARPOS (it->position);
18411 glyph->object = object;
18412 glyph->pixel_width = width;
18413 glyph->ascent = ascent;
18414 glyph->descent = height - ascent;
18415 glyph->voffset = it->voffset;
18416 glyph->type = STRETCH_GLYPH;
18417 glyph->multibyte_p = it->multibyte_p;
18418 glyph->left_box_line_p = it->start_of_box_run_p;
18419 glyph->right_box_line_p = it->end_of_box_run_p;
18420 glyph->overlaps_vertically_p = 0;
18421 glyph->padding_p = 0;
18422 glyph->glyph_not_available_p = 0;
18423 glyph->face_id = it->face_id;
18424 glyph->u.stretch.ascent = ascent;
18425 glyph->u.stretch.height = height;
18426 glyph->slice = null_glyph_slice;
18427 glyph->font_type = FONT_TYPE_UNKNOWN;
18428 ++it->glyph_row->used[area];
18429 }
18430 else if (!fonts_changed_p)
18431 {
18432 it->w->ncols_scale_factor++;
18433 fonts_changed_p = 1;
18434 }
18435 }
18436
18437
18438 /* Produce a stretch glyph for iterator IT. IT->object is the value
18439 of the glyph property displayed. The value must be a list
18440 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
18441 being recognized:
18442
18443 1. `:width WIDTH' specifies that the space should be WIDTH *
18444 canonical char width wide. WIDTH may be an integer or floating
18445 point number.
18446
18447 2. `:relative-width FACTOR' specifies that the width of the stretch
18448 should be computed from the width of the first character having the
18449 `glyph' property, and should be FACTOR times that width.
18450
18451 3. `:align-to HPOS' specifies that the space should be wide enough
18452 to reach HPOS, a value in canonical character units.
18453
18454 Exactly one of the above pairs must be present.
18455
18456 4. `:height HEIGHT' specifies that the height of the stretch produced
18457 should be HEIGHT, measured in canonical character units.
18458
18459 5. `:relative-height FACTOR' specifies that the height of the
18460 stretch should be FACTOR times the height of the characters having
18461 the glyph property.
18462
18463 Either none or exactly one of 4 or 5 must be present.
18464
18465 6. `:ascent ASCENT' specifies that ASCENT percent of the height
18466 of the stretch should be used for the ascent of the stretch.
18467 ASCENT must be in the range 0 <= ASCENT <= 100. */
18468
18469 static void
18470 produce_stretch_glyph (it)
18471 struct it *it;
18472 {
18473 /* (space :width WIDTH :height HEIGHT ...) */
18474 Lisp_Object prop, plist;
18475 int width = 0, height = 0, align_to = -1;
18476 int zero_width_ok_p = 0, zero_height_ok_p = 0;
18477 int ascent = 0;
18478 double tem;
18479 struct face *face = FACE_FROM_ID (it->f, it->face_id);
18480 XFontStruct *font = face->font ? face->font : FRAME_FONT (it->f);
18481
18482 PREPARE_FACE_FOR_DISPLAY (it->f, face);
18483
18484 /* List should start with `space'. */
18485 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
18486 plist = XCDR (it->object);
18487
18488 /* Compute the width of the stretch. */
18489 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
18490 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
18491 {
18492 /* Absolute width `:width WIDTH' specified and valid. */
18493 zero_width_ok_p = 1;
18494 width = (int)tem;
18495 }
18496 else if (prop = Fplist_get (plist, QCrelative_width),
18497 NUMVAL (prop) > 0)
18498 {
18499 /* Relative width `:relative-width FACTOR' specified and valid.
18500 Compute the width of the characters having the `glyph'
18501 property. */
18502 struct it it2;
18503 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
18504
18505 it2 = *it;
18506 if (it->multibyte_p)
18507 {
18508 int maxlen = ((IT_BYTEPOS (*it) >= GPT ? ZV : GPT)
18509 - IT_BYTEPOS (*it));
18510 it2.c = STRING_CHAR_AND_LENGTH (p, maxlen, it2.len);
18511 }
18512 else
18513 it2.c = *p, it2.len = 1;
18514
18515 it2.glyph_row = NULL;
18516 it2.what = IT_CHARACTER;
18517 x_produce_glyphs (&it2);
18518 width = NUMVAL (prop) * it2.pixel_width;
18519 }
18520 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
18521 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
18522 {
18523 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
18524 align_to = (align_to < 0
18525 ? 0
18526 : align_to - window_box_left_offset (it->w, TEXT_AREA));
18527 else if (align_to < 0)
18528 align_to = window_box_left_offset (it->w, TEXT_AREA);
18529 width = max (0, (int)tem + align_to - it->current_x);
18530 zero_width_ok_p = 1;
18531 }
18532 else
18533 /* Nothing specified -> width defaults to canonical char width. */
18534 width = FRAME_COLUMN_WIDTH (it->f);
18535
18536 if (width <= 0 && (width < 0 || !zero_width_ok_p))
18537 width = 1;
18538
18539 /* Compute height. */
18540 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
18541 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
18542 {
18543 height = (int)tem;
18544 zero_height_ok_p = 1;
18545 }
18546 else if (prop = Fplist_get (plist, QCrelative_height),
18547 NUMVAL (prop) > 0)
18548 height = FONT_HEIGHT (font) * NUMVAL (prop);
18549 else
18550 height = FONT_HEIGHT (font);
18551
18552 if (height <= 0 && (height < 0 || !zero_height_ok_p))
18553 height = 1;
18554
18555 /* Compute percentage of height used for ascent. If
18556 `:ascent ASCENT' is present and valid, use that. Otherwise,
18557 derive the ascent from the font in use. */
18558 if (prop = Fplist_get (plist, QCascent),
18559 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
18560 ascent = height * NUMVAL (prop) / 100.0;
18561 else if (!NILP (prop)
18562 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
18563 ascent = min (max (0, (int)tem), height);
18564 else
18565 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
18566
18567 if (width > 0 && height > 0 && it->glyph_row)
18568 {
18569 Lisp_Object object = it->stack[it->sp - 1].string;
18570 if (!STRINGP (object))
18571 object = it->w->buffer;
18572 append_stretch_glyph (it, object, width, height, ascent);
18573 }
18574
18575 it->pixel_width = width;
18576 it->ascent = it->phys_ascent = ascent;
18577 it->descent = it->phys_descent = height - it->ascent;
18578 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
18579
18580 if (width > 0 && height > 0 && face->box != FACE_NO_BOX)
18581 {
18582 if (face->box_line_width > 0)
18583 {
18584 it->ascent += face->box_line_width;
18585 it->descent += face->box_line_width;
18586 }
18587
18588 if (it->start_of_box_run_p)
18589 it->pixel_width += abs (face->box_line_width);
18590 if (it->end_of_box_run_p)
18591 it->pixel_width += abs (face->box_line_width);
18592 }
18593
18594 take_vertical_position_into_account (it);
18595 }
18596
18597 /* Calculate line-height and line-spacing properties.
18598 An integer value specifies explicit pixel value.
18599 A float value specifies relative value to current face height.
18600 A cons (float . face-name) specifies relative value to
18601 height of specified face font.
18602
18603 Returns height in pixels, or nil. */
18604
18605 static Lisp_Object
18606 calc_line_height_property (it, prop, font, boff, total)
18607 struct it *it;
18608 Lisp_Object prop;
18609 XFontStruct *font;
18610 int boff, *total;
18611 {
18612 Lisp_Object position, val;
18613 Lisp_Object face_name = Qnil;
18614 int ascent, descent, height, override;
18615
18616 if (STRINGP (it->object))
18617 position = make_number (IT_STRING_CHARPOS (*it));
18618 else
18619 position = make_number (IT_CHARPOS (*it));
18620
18621 val = Fget_char_property (position, prop, it->object);
18622
18623 if (NILP (val))
18624 return val;
18625
18626 if (total && CONSP (val) && EQ (XCAR (val), Qtotal))
18627 {
18628 *total = 1;
18629 val = XCDR (val);
18630 }
18631
18632 if (INTEGERP (val))
18633 return val;
18634
18635 if (CONSP (val))
18636 {
18637 face_name = XCDR (val);
18638 val = XCAR (val);
18639 }
18640 else if (SYMBOLP (val))
18641 {
18642 face_name = val;
18643 val = Qnil;
18644 }
18645
18646 override = EQ (prop, Qline_height);
18647
18648 if (NILP (face_name))
18649 {
18650 font = FRAME_FONT (it->f);
18651 boff = FRAME_BASELINE_OFFSET (it->f);
18652 }
18653 else if (EQ (face_name, Qt))
18654 {
18655 override = 0;
18656 }
18657 else
18658 {
18659 int face_id;
18660 struct face *face;
18661 struct font_info *font_info;
18662
18663 face_id = lookup_named_face (it->f, face_name, ' ');
18664 if (face_id < 0)
18665 return make_number (-1);
18666
18667 face = FACE_FROM_ID (it->f, face_id);
18668 font = face->font;
18669 if (font == NULL)
18670 return make_number (-1);
18671
18672 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
18673 boff = font_info->baseline_offset;
18674 if (font_info->vertical_centering)
18675 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
18676 }
18677
18678 ascent = FONT_BASE (font) + boff;
18679 descent = FONT_DESCENT (font) - boff;
18680
18681 if (override)
18682 {
18683 it->override_ascent = ascent;
18684 it->override_descent = descent;
18685 it->override_boff = boff;
18686 }
18687
18688 height = ascent + descent;
18689 if (FLOATP (val))
18690 height = (int)(XFLOAT_DATA (val) * height);
18691 else if (INTEGERP (val))
18692 height *= XINT (val);
18693
18694 return make_number (height);
18695 }
18696
18697
18698 /* RIF:
18699 Produce glyphs/get display metrics for the display element IT is
18700 loaded with. See the description of struct display_iterator in
18701 dispextern.h for an overview of struct display_iterator. */
18702
18703 void
18704 x_produce_glyphs (it)
18705 struct it *it;
18706 {
18707 int extra_line_spacing = it->extra_line_spacing;
18708
18709 it->glyph_not_available_p = 0;
18710
18711 if (it->what == IT_CHARACTER)
18712 {
18713 XChar2b char2b;
18714 XFontStruct *font;
18715 struct face *face = FACE_FROM_ID (it->f, it->face_id);
18716 XCharStruct *pcm;
18717 int font_not_found_p;
18718 struct font_info *font_info;
18719 int boff; /* baseline offset */
18720 /* We may change it->multibyte_p upon unibyte<->multibyte
18721 conversion. So, save the current value now and restore it
18722 later.
18723
18724 Note: It seems that we don't have to record multibyte_p in
18725 struct glyph because the character code itself tells if or
18726 not the character is multibyte. Thus, in the future, we must
18727 consider eliminating the field `multibyte_p' in the struct
18728 glyph. */
18729 int saved_multibyte_p = it->multibyte_p;
18730
18731 /* Maybe translate single-byte characters to multibyte, or the
18732 other way. */
18733 it->char_to_display = it->c;
18734 if (!ASCII_BYTE_P (it->c))
18735 {
18736 if (unibyte_display_via_language_environment
18737 && SINGLE_BYTE_CHAR_P (it->c)
18738 && (it->c >= 0240
18739 || !NILP (Vnonascii_translation_table)))
18740 {
18741 it->char_to_display = unibyte_char_to_multibyte (it->c);
18742 it->multibyte_p = 1;
18743 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
18744 face = FACE_FROM_ID (it->f, it->face_id);
18745 }
18746 else if (!SINGLE_BYTE_CHAR_P (it->c)
18747 && !it->multibyte_p)
18748 {
18749 it->multibyte_p = 1;
18750 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
18751 face = FACE_FROM_ID (it->f, it->face_id);
18752 }
18753 }
18754
18755 /* Get font to use. Encode IT->char_to_display. */
18756 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
18757 &char2b, it->multibyte_p, 0);
18758 font = face->font;
18759
18760 /* When no suitable font found, use the default font. */
18761 font_not_found_p = font == NULL;
18762 if (font_not_found_p)
18763 {
18764 font = FRAME_FONT (it->f);
18765 boff = FRAME_BASELINE_OFFSET (it->f);
18766 font_info = NULL;
18767 }
18768 else
18769 {
18770 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
18771 boff = font_info->baseline_offset;
18772 if (font_info->vertical_centering)
18773 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
18774 }
18775
18776 if (it->char_to_display >= ' '
18777 && (!it->multibyte_p || it->char_to_display < 128))
18778 {
18779 /* Either unibyte or ASCII. */
18780 int stretched_p;
18781
18782 it->nglyphs = 1;
18783
18784 pcm = rif->per_char_metric (font, &char2b,
18785 FONT_TYPE_FOR_UNIBYTE (font, it->char_to_display));
18786
18787 if (it->override_ascent >= 0)
18788 {
18789 it->ascent = it->override_ascent;
18790 it->descent = it->override_descent;
18791 boff = it->override_boff;
18792 }
18793 else
18794 {
18795 it->ascent = FONT_BASE (font) + boff;
18796 it->descent = FONT_DESCENT (font) - boff;
18797 }
18798
18799 if (pcm)
18800 {
18801 it->phys_ascent = pcm->ascent + boff;
18802 it->phys_descent = pcm->descent - boff;
18803 it->pixel_width = pcm->width;
18804 }
18805 else
18806 {
18807 it->glyph_not_available_p = 1;
18808 it->phys_ascent = it->ascent;
18809 it->phys_descent = it->descent;
18810 it->pixel_width = FONT_WIDTH (font);
18811 }
18812
18813 if (it->constrain_row_ascent_descent_p)
18814 {
18815 if (it->descent > it->max_descent)
18816 {
18817 it->ascent += it->descent - it->max_descent;
18818 it->descent = it->max_descent;
18819 }
18820 if (it->ascent > it->max_ascent)
18821 {
18822 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
18823 it->ascent = it->max_ascent;
18824 }
18825 it->phys_ascent = min (it->phys_ascent, it->ascent);
18826 it->phys_descent = min (it->phys_descent, it->descent);
18827 extra_line_spacing = 0;
18828 }
18829
18830 /* If this is a space inside a region of text with
18831 `space-width' property, change its width. */
18832 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
18833 if (stretched_p)
18834 it->pixel_width *= XFLOATINT (it->space_width);
18835
18836 /* If face has a box, add the box thickness to the character
18837 height. If character has a box line to the left and/or
18838 right, add the box line width to the character's width. */
18839 if (face->box != FACE_NO_BOX)
18840 {
18841 int thick = face->box_line_width;
18842
18843 if (thick > 0)
18844 {
18845 it->ascent += thick;
18846 it->descent += thick;
18847 }
18848 else
18849 thick = -thick;
18850
18851 if (it->start_of_box_run_p)
18852 it->pixel_width += thick;
18853 if (it->end_of_box_run_p)
18854 it->pixel_width += thick;
18855 }
18856
18857 /* If face has an overline, add the height of the overline
18858 (1 pixel) and a 1 pixel margin to the character height. */
18859 if (face->overline_p)
18860 it->ascent += 2;
18861
18862 if (it->constrain_row_ascent_descent_p)
18863 {
18864 if (it->ascent > it->max_ascent)
18865 it->ascent = it->max_ascent;
18866 if (it->descent > it->max_descent)
18867 it->descent = it->max_descent;
18868 }
18869
18870 take_vertical_position_into_account (it);
18871
18872 /* If we have to actually produce glyphs, do it. */
18873 if (it->glyph_row)
18874 {
18875 if (stretched_p)
18876 {
18877 /* Translate a space with a `space-width' property
18878 into a stretch glyph. */
18879 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
18880 / FONT_HEIGHT (font));
18881 append_stretch_glyph (it, it->object, it->pixel_width,
18882 it->ascent + it->descent, ascent);
18883 }
18884 else
18885 append_glyph (it);
18886
18887 /* If characters with lbearing or rbearing are displayed
18888 in this line, record that fact in a flag of the
18889 glyph row. This is used to optimize X output code. */
18890 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
18891 it->glyph_row->contains_overlapping_glyphs_p = 1;
18892 }
18893 }
18894 else if (it->char_to_display == '\n')
18895 {
18896 /* A newline has no width but we need the height of the line.
18897 But if previous part of the line set a height, don't
18898 increase that height */
18899
18900 Lisp_Object height;
18901
18902 it->override_ascent = -1;
18903 it->pixel_width = 0;
18904 it->nglyphs = 0;
18905
18906 height = calc_line_height_property(it, Qline_height, font, boff, 0);
18907
18908 if (it->override_ascent >= 0)
18909 {
18910 it->ascent = it->override_ascent;
18911 it->descent = it->override_descent;
18912 boff = it->override_boff;
18913 }
18914 else
18915 {
18916 it->ascent = FONT_BASE (font) + boff;
18917 it->descent = FONT_DESCENT (font) - boff;
18918 }
18919
18920 if (EQ (height, make_number(0)))
18921 {
18922 if (it->descent > it->max_descent)
18923 {
18924 it->ascent += it->descent - it->max_descent;
18925 it->descent = it->max_descent;
18926 }
18927 if (it->ascent > it->max_ascent)
18928 {
18929 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
18930 it->ascent = it->max_ascent;
18931 }
18932 it->phys_ascent = min (it->phys_ascent, it->ascent);
18933 it->phys_descent = min (it->phys_descent, it->descent);
18934 it->constrain_row_ascent_descent_p = 1;
18935 extra_line_spacing = 0;
18936 }
18937 else
18938 {
18939 Lisp_Object spacing;
18940 int total = 0;
18941
18942 it->phys_ascent = it->ascent;
18943 it->phys_descent = it->descent;
18944
18945 if ((it->max_ascent > 0 || it->max_descent > 0)
18946 && face->box != FACE_NO_BOX
18947 && face->box_line_width > 0)
18948 {
18949 it->ascent += face->box_line_width;
18950 it->descent += face->box_line_width;
18951 }
18952 if (!NILP (height)
18953 && XINT (height) > it->ascent + it->descent)
18954 it->ascent = XINT (height) - it->descent;
18955
18956 spacing = calc_line_height_property(it, Qline_spacing, font, boff, &total);
18957 if (INTEGERP (spacing))
18958 {
18959 extra_line_spacing = XINT (spacing);
18960 if (total)
18961 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
18962 }
18963 }
18964 }
18965 else if (it->char_to_display == '\t')
18966 {
18967 int tab_width = it->tab_width * FRAME_COLUMN_WIDTH (it->f);
18968 int x = it->current_x + it->continuation_lines_width;
18969 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
18970
18971 /* If the distance from the current position to the next tab
18972 stop is less than a canonical character width, use the
18973 tab stop after that. */
18974 if (next_tab_x - x < FRAME_COLUMN_WIDTH (it->f))
18975 next_tab_x += tab_width;
18976
18977 it->pixel_width = next_tab_x - x;
18978 it->nglyphs = 1;
18979 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
18980 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
18981
18982 if (it->glyph_row)
18983 {
18984 append_stretch_glyph (it, it->object, it->pixel_width,
18985 it->ascent + it->descent, it->ascent);
18986 }
18987 }
18988 else
18989 {
18990 /* A multi-byte character. Assume that the display width of the
18991 character is the width of the character multiplied by the
18992 width of the font. */
18993
18994 /* If we found a font, this font should give us the right
18995 metrics. If we didn't find a font, use the frame's
18996 default font and calculate the width of the character
18997 from the charset width; this is what old redisplay code
18998 did. */
18999
19000 pcm = rif->per_char_metric (font, &char2b,
19001 FONT_TYPE_FOR_MULTIBYTE (font, it->c));
19002
19003 if (font_not_found_p || !pcm)
19004 {
19005 int charset = CHAR_CHARSET (it->char_to_display);
19006
19007 it->glyph_not_available_p = 1;
19008 it->pixel_width = (FRAME_COLUMN_WIDTH (it->f)
19009 * CHARSET_WIDTH (charset));
19010 it->phys_ascent = FONT_BASE (font) + boff;
19011 it->phys_descent = FONT_DESCENT (font) - boff;
19012 }
19013 else
19014 {
19015 it->pixel_width = pcm->width;
19016 it->phys_ascent = pcm->ascent + boff;
19017 it->phys_descent = pcm->descent - boff;
19018 if (it->glyph_row
19019 && (pcm->lbearing < 0
19020 || pcm->rbearing > pcm->width))
19021 it->glyph_row->contains_overlapping_glyphs_p = 1;
19022 }
19023 it->nglyphs = 1;
19024 it->ascent = FONT_BASE (font) + boff;
19025 it->descent = FONT_DESCENT (font) - boff;
19026 if (face->box != FACE_NO_BOX)
19027 {
19028 int thick = face->box_line_width;
19029
19030 if (thick > 0)
19031 {
19032 it->ascent += thick;
19033 it->descent += thick;
19034 }
19035 else
19036 thick = - thick;
19037
19038 if (it->start_of_box_run_p)
19039 it->pixel_width += thick;
19040 if (it->end_of_box_run_p)
19041 it->pixel_width += thick;
19042 }
19043
19044 /* If face has an overline, add the height of the overline
19045 (1 pixel) and a 1 pixel margin to the character height. */
19046 if (face->overline_p)
19047 it->ascent += 2;
19048
19049 take_vertical_position_into_account (it);
19050
19051 if (it->glyph_row)
19052 append_glyph (it);
19053 }
19054 it->multibyte_p = saved_multibyte_p;
19055 }
19056 else if (it->what == IT_COMPOSITION)
19057 {
19058 /* Note: A composition is represented as one glyph in the
19059 glyph matrix. There are no padding glyphs. */
19060 XChar2b char2b;
19061 XFontStruct *font;
19062 struct face *face = FACE_FROM_ID (it->f, it->face_id);
19063 XCharStruct *pcm;
19064 int font_not_found_p;
19065 struct font_info *font_info;
19066 int boff; /* baseline offset */
19067 struct composition *cmp = composition_table[it->cmp_id];
19068
19069 /* Maybe translate single-byte characters to multibyte. */
19070 it->char_to_display = it->c;
19071 if (unibyte_display_via_language_environment
19072 && SINGLE_BYTE_CHAR_P (it->c)
19073 && (it->c >= 0240
19074 || (it->c >= 0200
19075 && !NILP (Vnonascii_translation_table))))
19076 {
19077 it->char_to_display = unibyte_char_to_multibyte (it->c);
19078 }
19079
19080 /* Get face and font to use. Encode IT->char_to_display. */
19081 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
19082 face = FACE_FROM_ID (it->f, it->face_id);
19083 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
19084 &char2b, it->multibyte_p, 0);
19085 font = face->font;
19086
19087 /* When no suitable font found, use the default font. */
19088 font_not_found_p = font == NULL;
19089 if (font_not_found_p)
19090 {
19091 font = FRAME_FONT (it->f);
19092 boff = FRAME_BASELINE_OFFSET (it->f);
19093 font_info = NULL;
19094 }
19095 else
19096 {
19097 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
19098 boff = font_info->baseline_offset;
19099 if (font_info->vertical_centering)
19100 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
19101 }
19102
19103 /* There are no padding glyphs, so there is only one glyph to
19104 produce for the composition. Important is that pixel_width,
19105 ascent and descent are the values of what is drawn by
19106 draw_glyphs (i.e. the values of the overall glyphs composed). */
19107 it->nglyphs = 1;
19108
19109 /* If we have not yet calculated pixel size data of glyphs of
19110 the composition for the current face font, calculate them
19111 now. Theoretically, we have to check all fonts for the
19112 glyphs, but that requires much time and memory space. So,
19113 here we check only the font of the first glyph. This leads
19114 to incorrect display very rarely, and C-l (recenter) can
19115 correct the display anyway. */
19116 if (cmp->font != (void *) font)
19117 {
19118 /* Ascent and descent of the font of the first character of
19119 this composition (adjusted by baseline offset). Ascent
19120 and descent of overall glyphs should not be less than
19121 them respectively. */
19122 int font_ascent = FONT_BASE (font) + boff;
19123 int font_descent = FONT_DESCENT (font) - boff;
19124 /* Bounding box of the overall glyphs. */
19125 int leftmost, rightmost, lowest, highest;
19126 int i, width, ascent, descent;
19127
19128 cmp->font = (void *) font;
19129
19130 /* Initialize the bounding box. */
19131 if (font_info
19132 && (pcm = rif->per_char_metric (font, &char2b,
19133 FONT_TYPE_FOR_MULTIBYTE (font, it->c))))
19134 {
19135 width = pcm->width;
19136 ascent = pcm->ascent;
19137 descent = pcm->descent;
19138 }
19139 else
19140 {
19141 width = FONT_WIDTH (font);
19142 ascent = FONT_BASE (font);
19143 descent = FONT_DESCENT (font);
19144 }
19145
19146 rightmost = width;
19147 lowest = - descent + boff;
19148 highest = ascent + boff;
19149 leftmost = 0;
19150
19151 if (font_info
19152 && font_info->default_ascent
19153 && CHAR_TABLE_P (Vuse_default_ascent)
19154 && !NILP (Faref (Vuse_default_ascent,
19155 make_number (it->char_to_display))))
19156 highest = font_info->default_ascent + boff;
19157
19158 /* Draw the first glyph at the normal position. It may be
19159 shifted to right later if some other glyphs are drawn at
19160 the left. */
19161 cmp->offsets[0] = 0;
19162 cmp->offsets[1] = boff;
19163
19164 /* Set cmp->offsets for the remaining glyphs. */
19165 for (i = 1; i < cmp->glyph_len; i++)
19166 {
19167 int left, right, btm, top;
19168 int ch = COMPOSITION_GLYPH (cmp, i);
19169 int face_id = FACE_FOR_CHAR (it->f, face, ch);
19170
19171 face = FACE_FROM_ID (it->f, face_id);
19172 get_char_face_and_encoding (it->f, ch, face->id,
19173 &char2b, it->multibyte_p, 0);
19174 font = face->font;
19175 if (font == NULL)
19176 {
19177 font = FRAME_FONT (it->f);
19178 boff = FRAME_BASELINE_OFFSET (it->f);
19179 font_info = NULL;
19180 }
19181 else
19182 {
19183 font_info
19184 = FONT_INFO_FROM_ID (it->f, face->font_info_id);
19185 boff = font_info->baseline_offset;
19186 if (font_info->vertical_centering)
19187 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
19188 }
19189
19190 if (font_info
19191 && (pcm = rif->per_char_metric (font, &char2b,
19192 FONT_TYPE_FOR_MULTIBYTE (font, ch))))
19193 {
19194 width = pcm->width;
19195 ascent = pcm->ascent;
19196 descent = pcm->descent;
19197 }
19198 else
19199 {
19200 width = FONT_WIDTH (font);
19201 ascent = 1;
19202 descent = 0;
19203 }
19204
19205 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
19206 {
19207 /* Relative composition with or without
19208 alternate chars. */
19209 left = (leftmost + rightmost - width) / 2;
19210 btm = - descent + boff;
19211 if (font_info && font_info->relative_compose
19212 && (! CHAR_TABLE_P (Vignore_relative_composition)
19213 || NILP (Faref (Vignore_relative_composition,
19214 make_number (ch)))))
19215 {
19216
19217 if (- descent >= font_info->relative_compose)
19218 /* One extra pixel between two glyphs. */
19219 btm = highest + 1;
19220 else if (ascent <= 0)
19221 /* One extra pixel between two glyphs. */
19222 btm = lowest - 1 - ascent - descent;
19223 }
19224 }
19225 else
19226 {
19227 /* A composition rule is specified by an integer
19228 value that encodes global and new reference
19229 points (GREF and NREF). GREF and NREF are
19230 specified by numbers as below:
19231
19232 0---1---2 -- ascent
19233 | |
19234 | |
19235 | |
19236 9--10--11 -- center
19237 | |
19238 ---3---4---5--- baseline
19239 | |
19240 6---7---8 -- descent
19241 */
19242 int rule = COMPOSITION_RULE (cmp, i);
19243 int gref, nref, grefx, grefy, nrefx, nrefy;
19244
19245 COMPOSITION_DECODE_RULE (rule, gref, nref);
19246 grefx = gref % 3, nrefx = nref % 3;
19247 grefy = gref / 3, nrefy = nref / 3;
19248
19249 left = (leftmost
19250 + grefx * (rightmost - leftmost) / 2
19251 - nrefx * width / 2);
19252 btm = ((grefy == 0 ? highest
19253 : grefy == 1 ? 0
19254 : grefy == 2 ? lowest
19255 : (highest + lowest) / 2)
19256 - (nrefy == 0 ? ascent + descent
19257 : nrefy == 1 ? descent - boff
19258 : nrefy == 2 ? 0
19259 : (ascent + descent) / 2));
19260 }
19261
19262 cmp->offsets[i * 2] = left;
19263 cmp->offsets[i * 2 + 1] = btm + descent;
19264
19265 /* Update the bounding box of the overall glyphs. */
19266 right = left + width;
19267 top = btm + descent + ascent;
19268 if (left < leftmost)
19269 leftmost = left;
19270 if (right > rightmost)
19271 rightmost = right;
19272 if (top > highest)
19273 highest = top;
19274 if (btm < lowest)
19275 lowest = btm;
19276 }
19277
19278 /* If there are glyphs whose x-offsets are negative,
19279 shift all glyphs to the right and make all x-offsets
19280 non-negative. */
19281 if (leftmost < 0)
19282 {
19283 for (i = 0; i < cmp->glyph_len; i++)
19284 cmp->offsets[i * 2] -= leftmost;
19285 rightmost -= leftmost;
19286 }
19287
19288 cmp->pixel_width = rightmost;
19289 cmp->ascent = highest;
19290 cmp->descent = - lowest;
19291 if (cmp->ascent < font_ascent)
19292 cmp->ascent = font_ascent;
19293 if (cmp->descent < font_descent)
19294 cmp->descent = font_descent;
19295 }
19296
19297 it->pixel_width = cmp->pixel_width;
19298 it->ascent = it->phys_ascent = cmp->ascent;
19299 it->descent = it->phys_descent = cmp->descent;
19300
19301 if (face->box != FACE_NO_BOX)
19302 {
19303 int thick = face->box_line_width;
19304
19305 if (thick > 0)
19306 {
19307 it->ascent += thick;
19308 it->descent += thick;
19309 }
19310 else
19311 thick = - thick;
19312
19313 if (it->start_of_box_run_p)
19314 it->pixel_width += thick;
19315 if (it->end_of_box_run_p)
19316 it->pixel_width += thick;
19317 }
19318
19319 /* If face has an overline, add the height of the overline
19320 (1 pixel) and a 1 pixel margin to the character height. */
19321 if (face->overline_p)
19322 it->ascent += 2;
19323
19324 take_vertical_position_into_account (it);
19325
19326 if (it->glyph_row)
19327 append_composite_glyph (it);
19328 }
19329 else if (it->what == IT_IMAGE)
19330 produce_image_glyph (it);
19331 else if (it->what == IT_STRETCH)
19332 produce_stretch_glyph (it);
19333
19334 /* Accumulate dimensions. Note: can't assume that it->descent > 0
19335 because this isn't true for images with `:ascent 100'. */
19336 xassert (it->ascent >= 0 && it->descent >= 0);
19337 if (it->area == TEXT_AREA)
19338 it->current_x += it->pixel_width;
19339
19340 if (extra_line_spacing > 0)
19341 it->descent += extra_line_spacing;
19342
19343 it->max_ascent = max (it->max_ascent, it->ascent);
19344 it->max_descent = max (it->max_descent, it->descent);
19345 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
19346 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
19347 }
19348
19349 /* EXPORT for RIF:
19350 Output LEN glyphs starting at START at the nominal cursor position.
19351 Advance the nominal cursor over the text. The global variable
19352 updated_window contains the window being updated, updated_row is
19353 the glyph row being updated, and updated_area is the area of that
19354 row being updated. */
19355
19356 void
19357 x_write_glyphs (start, len)
19358 struct glyph *start;
19359 int len;
19360 {
19361 int x, hpos;
19362
19363 xassert (updated_window && updated_row);
19364 BLOCK_INPUT;
19365
19366 /* Write glyphs. */
19367
19368 hpos = start - updated_row->glyphs[updated_area];
19369 x = draw_glyphs (updated_window, output_cursor.x,
19370 updated_row, updated_area,
19371 hpos, hpos + len,
19372 DRAW_NORMAL_TEXT, 0);
19373
19374 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
19375 if (updated_area == TEXT_AREA
19376 && updated_window->phys_cursor_on_p
19377 && updated_window->phys_cursor.vpos == output_cursor.vpos
19378 && updated_window->phys_cursor.hpos >= hpos
19379 && updated_window->phys_cursor.hpos < hpos + len)
19380 updated_window->phys_cursor_on_p = 0;
19381
19382 UNBLOCK_INPUT;
19383
19384 /* Advance the output cursor. */
19385 output_cursor.hpos += len;
19386 output_cursor.x = x;
19387 }
19388
19389
19390 /* EXPORT for RIF:
19391 Insert LEN glyphs from START at the nominal cursor position. */
19392
19393 void
19394 x_insert_glyphs (start, len)
19395 struct glyph *start;
19396 int len;
19397 {
19398 struct frame *f;
19399 struct window *w;
19400 int line_height, shift_by_width, shifted_region_width;
19401 struct glyph_row *row;
19402 struct glyph *glyph;
19403 int frame_x, frame_y, hpos;
19404
19405 xassert (updated_window && updated_row);
19406 BLOCK_INPUT;
19407 w = updated_window;
19408 f = XFRAME (WINDOW_FRAME (w));
19409
19410 /* Get the height of the line we are in. */
19411 row = updated_row;
19412 line_height = row->height;
19413
19414 /* Get the width of the glyphs to insert. */
19415 shift_by_width = 0;
19416 for (glyph = start; glyph < start + len; ++glyph)
19417 shift_by_width += glyph->pixel_width;
19418
19419 /* Get the width of the region to shift right. */
19420 shifted_region_width = (window_box_width (w, updated_area)
19421 - output_cursor.x
19422 - shift_by_width);
19423
19424 /* Shift right. */
19425 frame_x = window_box_left (w, updated_area) + output_cursor.x;
19426 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
19427
19428 rif->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
19429 line_height, shift_by_width);
19430
19431 /* Write the glyphs. */
19432 hpos = start - row->glyphs[updated_area];
19433 draw_glyphs (w, output_cursor.x, row, updated_area,
19434 hpos, hpos + len,
19435 DRAW_NORMAL_TEXT, 0);
19436
19437 /* Advance the output cursor. */
19438 output_cursor.hpos += len;
19439 output_cursor.x += shift_by_width;
19440 UNBLOCK_INPUT;
19441 }
19442
19443
19444 /* EXPORT for RIF:
19445 Erase the current text line from the nominal cursor position
19446 (inclusive) to pixel column TO_X (exclusive). The idea is that
19447 everything from TO_X onward is already erased.
19448
19449 TO_X is a pixel position relative to updated_area of
19450 updated_window. TO_X == -1 means clear to the end of this area. */
19451
19452 void
19453 x_clear_end_of_line (to_x)
19454 int to_x;
19455 {
19456 struct frame *f;
19457 struct window *w = updated_window;
19458 int max_x, min_y, max_y;
19459 int from_x, from_y, to_y;
19460
19461 xassert (updated_window && updated_row);
19462 f = XFRAME (w->frame);
19463
19464 if (updated_row->full_width_p)
19465 max_x = WINDOW_TOTAL_WIDTH (w);
19466 else
19467 max_x = window_box_width (w, updated_area);
19468 max_y = window_text_bottom_y (w);
19469
19470 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
19471 of window. For TO_X > 0, truncate to end of drawing area. */
19472 if (to_x == 0)
19473 return;
19474 else if (to_x < 0)
19475 to_x = max_x;
19476 else
19477 to_x = min (to_x, max_x);
19478
19479 to_y = min (max_y, output_cursor.y + updated_row->height);
19480
19481 /* Notice if the cursor will be cleared by this operation. */
19482 if (!updated_row->full_width_p)
19483 notice_overwritten_cursor (w, updated_area,
19484 output_cursor.x, -1,
19485 updated_row->y,
19486 MATRIX_ROW_BOTTOM_Y (updated_row));
19487
19488 from_x = output_cursor.x;
19489
19490 /* Translate to frame coordinates. */
19491 if (updated_row->full_width_p)
19492 {
19493 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
19494 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
19495 }
19496 else
19497 {
19498 int area_left = window_box_left (w, updated_area);
19499 from_x += area_left;
19500 to_x += area_left;
19501 }
19502
19503 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
19504 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
19505 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
19506
19507 /* Prevent inadvertently clearing to end of the X window. */
19508 if (to_x > from_x && to_y > from_y)
19509 {
19510 BLOCK_INPUT;
19511 rif->clear_frame_area (f, from_x, from_y,
19512 to_x - from_x, to_y - from_y);
19513 UNBLOCK_INPUT;
19514 }
19515 }
19516
19517 #endif /* HAVE_WINDOW_SYSTEM */
19518
19519
19520 \f
19521 /***********************************************************************
19522 Cursor types
19523 ***********************************************************************/
19524
19525 /* Value is the internal representation of the specified cursor type
19526 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
19527 of the bar cursor. */
19528
19529 static enum text_cursor_kinds
19530 get_specified_cursor_type (arg, width)
19531 Lisp_Object arg;
19532 int *width;
19533 {
19534 enum text_cursor_kinds type;
19535
19536 if (NILP (arg))
19537 return NO_CURSOR;
19538
19539 if (EQ (arg, Qbox))
19540 return FILLED_BOX_CURSOR;
19541
19542 if (EQ (arg, Qhollow))
19543 return HOLLOW_BOX_CURSOR;
19544
19545 if (EQ (arg, Qbar))
19546 {
19547 *width = 2;
19548 return BAR_CURSOR;
19549 }
19550
19551 if (CONSP (arg)
19552 && EQ (XCAR (arg), Qbar)
19553 && INTEGERP (XCDR (arg))
19554 && XINT (XCDR (arg)) >= 0)
19555 {
19556 *width = XINT (XCDR (arg));
19557 return BAR_CURSOR;
19558 }
19559
19560 if (EQ (arg, Qhbar))
19561 {
19562 *width = 2;
19563 return HBAR_CURSOR;
19564 }
19565
19566 if (CONSP (arg)
19567 && EQ (XCAR (arg), Qhbar)
19568 && INTEGERP (XCDR (arg))
19569 && XINT (XCDR (arg)) >= 0)
19570 {
19571 *width = XINT (XCDR (arg));
19572 return HBAR_CURSOR;
19573 }
19574
19575 /* Treat anything unknown as "hollow box cursor".
19576 It was bad to signal an error; people have trouble fixing
19577 .Xdefaults with Emacs, when it has something bad in it. */
19578 type = HOLLOW_BOX_CURSOR;
19579
19580 return type;
19581 }
19582
19583 /* Set the default cursor types for specified frame. */
19584 void
19585 set_frame_cursor_types (f, arg)
19586 struct frame *f;
19587 Lisp_Object arg;
19588 {
19589 int width;
19590 Lisp_Object tem;
19591
19592 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
19593 FRAME_CURSOR_WIDTH (f) = width;
19594
19595 /* By default, set up the blink-off state depending on the on-state. */
19596
19597 tem = Fassoc (arg, Vblink_cursor_alist);
19598 if (!NILP (tem))
19599 {
19600 FRAME_BLINK_OFF_CURSOR (f)
19601 = get_specified_cursor_type (XCDR (tem), &width);
19602 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
19603 }
19604 else
19605 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
19606 }
19607
19608
19609 /* Return the cursor we want to be displayed in window W. Return
19610 width of bar/hbar cursor through WIDTH arg. Return with
19611 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
19612 (i.e. if the `system caret' should track this cursor).
19613
19614 In a mini-buffer window, we want the cursor only to appear if we
19615 are reading input from this window. For the selected window, we
19616 want the cursor type given by the frame parameter or buffer local
19617 setting of cursor-type. If explicitly marked off, draw no cursor.
19618 In all other cases, we want a hollow box cursor. */
19619
19620 static enum text_cursor_kinds
19621 get_window_cursor_type (w, glyph, width, active_cursor)
19622 struct window *w;
19623 struct glyph *glyph;
19624 int *width;
19625 int *active_cursor;
19626 {
19627 struct frame *f = XFRAME (w->frame);
19628 struct buffer *b = XBUFFER (w->buffer);
19629 int cursor_type = DEFAULT_CURSOR;
19630 Lisp_Object alt_cursor;
19631 int non_selected = 0;
19632
19633 *active_cursor = 1;
19634
19635 /* Echo area */
19636 if (cursor_in_echo_area
19637 && FRAME_HAS_MINIBUF_P (f)
19638 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
19639 {
19640 if (w == XWINDOW (echo_area_window))
19641 {
19642 *width = FRAME_CURSOR_WIDTH (f);
19643 return FRAME_DESIRED_CURSOR (f);
19644 }
19645
19646 *active_cursor = 0;
19647 non_selected = 1;
19648 }
19649
19650 /* Nonselected window or nonselected frame. */
19651 else if (w != XWINDOW (f->selected_window)
19652 #ifdef HAVE_WINDOW_SYSTEM
19653 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame
19654 #endif
19655 )
19656 {
19657 *active_cursor = 0;
19658
19659 if (MINI_WINDOW_P (w) && minibuf_level == 0)
19660 return NO_CURSOR;
19661
19662 non_selected = 1;
19663 }
19664
19665 /* Never display a cursor in a window in which cursor-type is nil. */
19666 if (NILP (b->cursor_type))
19667 return NO_CURSOR;
19668
19669 /* Use cursor-in-non-selected-windows for non-selected window or frame. */
19670 if (non_selected)
19671 {
19672 alt_cursor = Fbuffer_local_value (Qcursor_in_non_selected_windows, w->buffer);
19673 return get_specified_cursor_type (alt_cursor, width);
19674 }
19675
19676 /* Get the normal cursor type for this window. */
19677 if (EQ (b->cursor_type, Qt))
19678 {
19679 cursor_type = FRAME_DESIRED_CURSOR (f);
19680 *width = FRAME_CURSOR_WIDTH (f);
19681 }
19682 else
19683 cursor_type = get_specified_cursor_type (b->cursor_type, width);
19684
19685 /* Use normal cursor if not blinked off. */
19686 if (!w->cursor_off_p)
19687 {
19688 if (glyph != NULL && glyph->type == IMAGE_GLYPH) {
19689 if (cursor_type == FILLED_BOX_CURSOR)
19690 cursor_type = HOLLOW_BOX_CURSOR;
19691 }
19692 return cursor_type;
19693 }
19694
19695 /* Cursor is blinked off, so determine how to "toggle" it. */
19696
19697 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
19698 if ((alt_cursor = Fassoc (b->cursor_type, Vblink_cursor_alist), !NILP (alt_cursor)))
19699 return get_specified_cursor_type (XCDR (alt_cursor), width);
19700
19701 /* Then see if frame has specified a specific blink off cursor type. */
19702 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
19703 {
19704 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
19705 return FRAME_BLINK_OFF_CURSOR (f);
19706 }
19707
19708 #if 0
19709 /* Some people liked having a permanently visible blinking cursor,
19710 while others had very strong opinions against it. So it was
19711 decided to remove it. KFS 2003-09-03 */
19712
19713 /* Finally perform built-in cursor blinking:
19714 filled box <-> hollow box
19715 wide [h]bar <-> narrow [h]bar
19716 narrow [h]bar <-> no cursor
19717 other type <-> no cursor */
19718
19719 if (cursor_type == FILLED_BOX_CURSOR)
19720 return HOLLOW_BOX_CURSOR;
19721
19722 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
19723 {
19724 *width = 1;
19725 return cursor_type;
19726 }
19727 #endif
19728
19729 return NO_CURSOR;
19730 }
19731
19732
19733 #ifdef HAVE_WINDOW_SYSTEM
19734
19735 /* Notice when the text cursor of window W has been completely
19736 overwritten by a drawing operation that outputs glyphs in AREA
19737 starting at X0 and ending at X1 in the line starting at Y0 and
19738 ending at Y1. X coordinates are area-relative. X1 < 0 means all
19739 the rest of the line after X0 has been written. Y coordinates
19740 are window-relative. */
19741
19742 static void
19743 notice_overwritten_cursor (w, area, x0, x1, y0, y1)
19744 struct window *w;
19745 enum glyph_row_area area;
19746 int x0, y0, x1, y1;
19747 {
19748 int cx0, cx1, cy0, cy1;
19749 struct glyph_row *row;
19750
19751 if (!w->phys_cursor_on_p)
19752 return;
19753 if (area != TEXT_AREA)
19754 return;
19755
19756 row = w->current_matrix->rows + w->phys_cursor.vpos;
19757 if (!row->displays_text_p)
19758 return;
19759
19760 if (row->cursor_in_fringe_p)
19761 {
19762 row->cursor_in_fringe_p = 0;
19763 draw_fringe_bitmap (w, row, 0);
19764 w->phys_cursor_on_p = 0;
19765 return;
19766 }
19767
19768 cx0 = w->phys_cursor.x;
19769 cx1 = cx0 + w->phys_cursor_width;
19770 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
19771 return;
19772
19773 /* The cursor image will be completely removed from the
19774 screen if the output area intersects the cursor area in
19775 y-direction. When we draw in [y0 y1[, and some part of
19776 the cursor is at y < y0, that part must have been drawn
19777 before. When scrolling, the cursor is erased before
19778 actually scrolling, so we don't come here. When not
19779 scrolling, the rows above the old cursor row must have
19780 changed, and in this case these rows must have written
19781 over the cursor image.
19782
19783 Likewise if part of the cursor is below y1, with the
19784 exception of the cursor being in the first blank row at
19785 the buffer and window end because update_text_area
19786 doesn't draw that row. (Except when it does, but
19787 that's handled in update_text_area.) */
19788
19789 cy0 = w->phys_cursor.y;
19790 cy1 = cy0 + w->phys_cursor_height;
19791 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
19792 return;
19793
19794 w->phys_cursor_on_p = 0;
19795 }
19796
19797 #endif /* HAVE_WINDOW_SYSTEM */
19798
19799 \f
19800 /************************************************************************
19801 Mouse Face
19802 ************************************************************************/
19803
19804 #ifdef HAVE_WINDOW_SYSTEM
19805
19806 /* EXPORT for RIF:
19807 Fix the display of area AREA of overlapping row ROW in window W. */
19808
19809 void
19810 x_fix_overlapping_area (w, row, area)
19811 struct window *w;
19812 struct glyph_row *row;
19813 enum glyph_row_area area;
19814 {
19815 int i, x;
19816
19817 BLOCK_INPUT;
19818
19819 x = 0;
19820 for (i = 0; i < row->used[area];)
19821 {
19822 if (row->glyphs[area][i].overlaps_vertically_p)
19823 {
19824 int start = i, start_x = x;
19825
19826 do
19827 {
19828 x += row->glyphs[area][i].pixel_width;
19829 ++i;
19830 }
19831 while (i < row->used[area]
19832 && row->glyphs[area][i].overlaps_vertically_p);
19833
19834 draw_glyphs (w, start_x, row, area,
19835 start, i,
19836 DRAW_NORMAL_TEXT, 1);
19837 }
19838 else
19839 {
19840 x += row->glyphs[area][i].pixel_width;
19841 ++i;
19842 }
19843 }
19844
19845 UNBLOCK_INPUT;
19846 }
19847
19848
19849 /* EXPORT:
19850 Draw the cursor glyph of window W in glyph row ROW. See the
19851 comment of draw_glyphs for the meaning of HL. */
19852
19853 void
19854 draw_phys_cursor_glyph (w, row, hl)
19855 struct window *w;
19856 struct glyph_row *row;
19857 enum draw_glyphs_face hl;
19858 {
19859 /* If cursor hpos is out of bounds, don't draw garbage. This can
19860 happen in mini-buffer windows when switching between echo area
19861 glyphs and mini-buffer. */
19862 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
19863 {
19864 int on_p = w->phys_cursor_on_p;
19865 int x1;
19866 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
19867 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
19868 hl, 0);
19869 w->phys_cursor_on_p = on_p;
19870
19871 if (hl == DRAW_CURSOR)
19872 w->phys_cursor_width = x1 - w->phys_cursor.x;
19873 /* When we erase the cursor, and ROW is overlapped by other
19874 rows, make sure that these overlapping parts of other rows
19875 are redrawn. */
19876 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
19877 {
19878 if (row > w->current_matrix->rows
19879 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
19880 x_fix_overlapping_area (w, row - 1, TEXT_AREA);
19881
19882 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
19883 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
19884 x_fix_overlapping_area (w, row + 1, TEXT_AREA);
19885 }
19886 }
19887 }
19888
19889
19890 /* EXPORT:
19891 Erase the image of a cursor of window W from the screen. */
19892
19893 void
19894 erase_phys_cursor (w)
19895 struct window *w;
19896 {
19897 struct frame *f = XFRAME (w->frame);
19898 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
19899 int hpos = w->phys_cursor.hpos;
19900 int vpos = w->phys_cursor.vpos;
19901 int mouse_face_here_p = 0;
19902 struct glyph_matrix *active_glyphs = w->current_matrix;
19903 struct glyph_row *cursor_row;
19904 struct glyph *cursor_glyph;
19905 enum draw_glyphs_face hl;
19906
19907 /* No cursor displayed or row invalidated => nothing to do on the
19908 screen. */
19909 if (w->phys_cursor_type == NO_CURSOR)
19910 goto mark_cursor_off;
19911
19912 /* VPOS >= active_glyphs->nrows means that window has been resized.
19913 Don't bother to erase the cursor. */
19914 if (vpos >= active_glyphs->nrows)
19915 goto mark_cursor_off;
19916
19917 /* If row containing cursor is marked invalid, there is nothing we
19918 can do. */
19919 cursor_row = MATRIX_ROW (active_glyphs, vpos);
19920 if (!cursor_row->enabled_p)
19921 goto mark_cursor_off;
19922
19923 /* If row is completely invisible, don't attempt to delete a cursor which
19924 isn't there. This can happen if cursor is at top of a window, and
19925 we switch to a buffer with a header line in that window. */
19926 if (cursor_row->visible_height <= 0)
19927 goto mark_cursor_off;
19928
19929 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
19930 if (cursor_row->cursor_in_fringe_p)
19931 {
19932 cursor_row->cursor_in_fringe_p = 0;
19933 draw_fringe_bitmap (w, cursor_row, 0);
19934 goto mark_cursor_off;
19935 }
19936
19937 /* This can happen when the new row is shorter than the old one.
19938 In this case, either draw_glyphs or clear_end_of_line
19939 should have cleared the cursor. Note that we wouldn't be
19940 able to erase the cursor in this case because we don't have a
19941 cursor glyph at hand. */
19942 if (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])
19943 goto mark_cursor_off;
19944
19945 /* If the cursor is in the mouse face area, redisplay that when
19946 we clear the cursor. */
19947 if (! NILP (dpyinfo->mouse_face_window)
19948 && w == XWINDOW (dpyinfo->mouse_face_window)
19949 && (vpos > dpyinfo->mouse_face_beg_row
19950 || (vpos == dpyinfo->mouse_face_beg_row
19951 && hpos >= dpyinfo->mouse_face_beg_col))
19952 && (vpos < dpyinfo->mouse_face_end_row
19953 || (vpos == dpyinfo->mouse_face_end_row
19954 && hpos < dpyinfo->mouse_face_end_col))
19955 /* Don't redraw the cursor's spot in mouse face if it is at the
19956 end of a line (on a newline). The cursor appears there, but
19957 mouse highlighting does not. */
19958 && cursor_row->used[TEXT_AREA] > hpos)
19959 mouse_face_here_p = 1;
19960
19961 /* Maybe clear the display under the cursor. */
19962 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
19963 {
19964 int x, y;
19965 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
19966
19967 cursor_glyph = get_phys_cursor_glyph (w);
19968 if (cursor_glyph == NULL)
19969 goto mark_cursor_off;
19970
19971 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
19972 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
19973
19974 rif->clear_frame_area (f, x, y,
19975 cursor_glyph->pixel_width, cursor_row->visible_height);
19976 }
19977
19978 /* Erase the cursor by redrawing the character underneath it. */
19979 if (mouse_face_here_p)
19980 hl = DRAW_MOUSE_FACE;
19981 else
19982 hl = DRAW_NORMAL_TEXT;
19983 draw_phys_cursor_glyph (w, cursor_row, hl);
19984
19985 mark_cursor_off:
19986 w->phys_cursor_on_p = 0;
19987 w->phys_cursor_type = NO_CURSOR;
19988 }
19989
19990
19991 /* EXPORT:
19992 Display or clear cursor of window W. If ON is zero, clear the
19993 cursor. If it is non-zero, display the cursor. If ON is nonzero,
19994 where to put the cursor is specified by HPOS, VPOS, X and Y. */
19995
19996 void
19997 display_and_set_cursor (w, on, hpos, vpos, x, y)
19998 struct window *w;
19999 int on, hpos, vpos, x, y;
20000 {
20001 struct frame *f = XFRAME (w->frame);
20002 int new_cursor_type;
20003 int new_cursor_width;
20004 int active_cursor;
20005 struct glyph_row *glyph_row;
20006 struct glyph *glyph;
20007
20008 /* This is pointless on invisible frames, and dangerous on garbaged
20009 windows and frames; in the latter case, the frame or window may
20010 be in the midst of changing its size, and x and y may be off the
20011 window. */
20012 if (! FRAME_VISIBLE_P (f)
20013 || FRAME_GARBAGED_P (f)
20014 || vpos >= w->current_matrix->nrows
20015 || hpos >= w->current_matrix->matrix_w)
20016 return;
20017
20018 /* If cursor is off and we want it off, return quickly. */
20019 if (!on && !w->phys_cursor_on_p)
20020 return;
20021
20022 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
20023 /* If cursor row is not enabled, we don't really know where to
20024 display the cursor. */
20025 if (!glyph_row->enabled_p)
20026 {
20027 w->phys_cursor_on_p = 0;
20028 return;
20029 }
20030
20031 glyph = NULL;
20032 if (!glyph_row->exact_window_width_line_p
20033 || hpos < glyph_row->used[TEXT_AREA])
20034 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
20035
20036 xassert (interrupt_input_blocked);
20037
20038 /* Set new_cursor_type to the cursor we want to be displayed. */
20039 new_cursor_type = get_window_cursor_type (w, glyph,
20040 &new_cursor_width, &active_cursor);
20041
20042 /* If cursor is currently being shown and we don't want it to be or
20043 it is in the wrong place, or the cursor type is not what we want,
20044 erase it. */
20045 if (w->phys_cursor_on_p
20046 && (!on
20047 || w->phys_cursor.x != x
20048 || w->phys_cursor.y != y
20049 || new_cursor_type != w->phys_cursor_type
20050 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
20051 && new_cursor_width != w->phys_cursor_width)))
20052 erase_phys_cursor (w);
20053
20054 /* Don't check phys_cursor_on_p here because that flag is only set
20055 to zero in some cases where we know that the cursor has been
20056 completely erased, to avoid the extra work of erasing the cursor
20057 twice. In other words, phys_cursor_on_p can be 1 and the cursor
20058 still not be visible, or it has only been partly erased. */
20059 if (on)
20060 {
20061 w->phys_cursor_ascent = glyph_row->ascent;
20062 w->phys_cursor_height = glyph_row->height;
20063
20064 /* Set phys_cursor_.* before x_draw_.* is called because some
20065 of them may need the information. */
20066 w->phys_cursor.x = x;
20067 w->phys_cursor.y = glyph_row->y;
20068 w->phys_cursor.hpos = hpos;
20069 w->phys_cursor.vpos = vpos;
20070 }
20071
20072 rif->draw_window_cursor (w, glyph_row, x, y,
20073 new_cursor_type, new_cursor_width,
20074 on, active_cursor);
20075 }
20076
20077
20078 /* Switch the display of W's cursor on or off, according to the value
20079 of ON. */
20080
20081 static void
20082 update_window_cursor (w, on)
20083 struct window *w;
20084 int on;
20085 {
20086 /* Don't update cursor in windows whose frame is in the process
20087 of being deleted. */
20088 if (w->current_matrix)
20089 {
20090 BLOCK_INPUT;
20091 display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
20092 w->phys_cursor.x, w->phys_cursor.y);
20093 UNBLOCK_INPUT;
20094 }
20095 }
20096
20097
20098 /* Call update_window_cursor with parameter ON_P on all leaf windows
20099 in the window tree rooted at W. */
20100
20101 static void
20102 update_cursor_in_window_tree (w, on_p)
20103 struct window *w;
20104 int on_p;
20105 {
20106 while (w)
20107 {
20108 if (!NILP (w->hchild))
20109 update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
20110 else if (!NILP (w->vchild))
20111 update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
20112 else
20113 update_window_cursor (w, on_p);
20114
20115 w = NILP (w->next) ? 0 : XWINDOW (w->next);
20116 }
20117 }
20118
20119
20120 /* EXPORT:
20121 Display the cursor on window W, or clear it, according to ON_P.
20122 Don't change the cursor's position. */
20123
20124 void
20125 x_update_cursor (f, on_p)
20126 struct frame *f;
20127 int on_p;
20128 {
20129 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
20130 }
20131
20132
20133 /* EXPORT:
20134 Clear the cursor of window W to background color, and mark the
20135 cursor as not shown. This is used when the text where the cursor
20136 is is about to be rewritten. */
20137
20138 void
20139 x_clear_cursor (w)
20140 struct window *w;
20141 {
20142 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
20143 update_window_cursor (w, 0);
20144 }
20145
20146
20147 /* EXPORT:
20148 Display the active region described by mouse_face_* according to DRAW. */
20149
20150 void
20151 show_mouse_face (dpyinfo, draw)
20152 Display_Info *dpyinfo;
20153 enum draw_glyphs_face draw;
20154 {
20155 struct window *w = XWINDOW (dpyinfo->mouse_face_window);
20156 struct frame *f = XFRAME (WINDOW_FRAME (w));
20157
20158 if (/* If window is in the process of being destroyed, don't bother
20159 to do anything. */
20160 w->current_matrix != NULL
20161 /* Don't update mouse highlight if hidden */
20162 && (draw != DRAW_MOUSE_FACE || !dpyinfo->mouse_face_hidden)
20163 /* Recognize when we are called to operate on rows that don't exist
20164 anymore. This can happen when a window is split. */
20165 && dpyinfo->mouse_face_end_row < w->current_matrix->nrows)
20166 {
20167 int phys_cursor_on_p = w->phys_cursor_on_p;
20168 struct glyph_row *row, *first, *last;
20169
20170 first = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_beg_row);
20171 last = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_end_row);
20172
20173 for (row = first; row <= last && row->enabled_p; ++row)
20174 {
20175 int start_hpos, end_hpos, start_x;
20176
20177 /* For all but the first row, the highlight starts at column 0. */
20178 if (row == first)
20179 {
20180 start_hpos = dpyinfo->mouse_face_beg_col;
20181 start_x = dpyinfo->mouse_face_beg_x;
20182 }
20183 else
20184 {
20185 start_hpos = 0;
20186 start_x = 0;
20187 }
20188
20189 if (row == last)
20190 end_hpos = dpyinfo->mouse_face_end_col;
20191 else
20192 end_hpos = row->used[TEXT_AREA];
20193
20194 if (end_hpos > start_hpos)
20195 {
20196 draw_glyphs (w, start_x, row, TEXT_AREA,
20197 start_hpos, end_hpos,
20198 draw, 0);
20199
20200 row->mouse_face_p
20201 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
20202 }
20203 }
20204
20205 /* When we've written over the cursor, arrange for it to
20206 be displayed again. */
20207 if (phys_cursor_on_p && !w->phys_cursor_on_p)
20208 {
20209 BLOCK_INPUT;
20210 display_and_set_cursor (w, 1,
20211 w->phys_cursor.hpos, w->phys_cursor.vpos,
20212 w->phys_cursor.x, w->phys_cursor.y);
20213 UNBLOCK_INPUT;
20214 }
20215 }
20216
20217 /* Change the mouse cursor. */
20218 if (draw == DRAW_NORMAL_TEXT)
20219 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
20220 else if (draw == DRAW_MOUSE_FACE)
20221 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
20222 else
20223 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
20224 }
20225
20226 /* EXPORT:
20227 Clear out the mouse-highlighted active region.
20228 Redraw it un-highlighted first. Value is non-zero if mouse
20229 face was actually drawn unhighlighted. */
20230
20231 int
20232 clear_mouse_face (dpyinfo)
20233 Display_Info *dpyinfo;
20234 {
20235 int cleared = 0;
20236
20237 if (!dpyinfo->mouse_face_hidden && !NILP (dpyinfo->mouse_face_window))
20238 {
20239 show_mouse_face (dpyinfo, DRAW_NORMAL_TEXT);
20240 cleared = 1;
20241 }
20242
20243 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
20244 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
20245 dpyinfo->mouse_face_window = Qnil;
20246 dpyinfo->mouse_face_overlay = Qnil;
20247 return cleared;
20248 }
20249
20250
20251 /* EXPORT:
20252 Non-zero if physical cursor of window W is within mouse face. */
20253
20254 int
20255 cursor_in_mouse_face_p (w)
20256 struct window *w;
20257 {
20258 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
20259 int in_mouse_face = 0;
20260
20261 if (WINDOWP (dpyinfo->mouse_face_window)
20262 && XWINDOW (dpyinfo->mouse_face_window) == w)
20263 {
20264 int hpos = w->phys_cursor.hpos;
20265 int vpos = w->phys_cursor.vpos;
20266
20267 if (vpos >= dpyinfo->mouse_face_beg_row
20268 && vpos <= dpyinfo->mouse_face_end_row
20269 && (vpos > dpyinfo->mouse_face_beg_row
20270 || hpos >= dpyinfo->mouse_face_beg_col)
20271 && (vpos < dpyinfo->mouse_face_end_row
20272 || hpos < dpyinfo->mouse_face_end_col
20273 || dpyinfo->mouse_face_past_end))
20274 in_mouse_face = 1;
20275 }
20276
20277 return in_mouse_face;
20278 }
20279
20280
20281
20282 \f
20283 /* Find the glyph matrix position of buffer position CHARPOS in window
20284 *W. HPOS, *VPOS, *X, and *Y are set to the positions found. W's
20285 current glyphs must be up to date. If CHARPOS is above window
20286 start return (0, 0, 0, 0). If CHARPOS is after end of W, return end
20287 of last line in W. In the row containing CHARPOS, stop before glyphs
20288 having STOP as object. */
20289
20290 #if 1 /* This is a version of fast_find_position that's more correct
20291 in the presence of hscrolling, for example. I didn't install
20292 it right away because the problem fixed is minor, it failed
20293 in 20.x as well, and I think it's too risky to install
20294 so near the release of 21.1. 2001-09-25 gerd. */
20295
20296 static int
20297 fast_find_position (w, charpos, hpos, vpos, x, y, stop)
20298 struct window *w;
20299 int charpos;
20300 int *hpos, *vpos, *x, *y;
20301 Lisp_Object stop;
20302 {
20303 struct glyph_row *row, *first;
20304 struct glyph *glyph, *end;
20305 int past_end = 0;
20306
20307 first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
20308 row = row_containing_pos (w, charpos, first, NULL, 0);
20309 if (row == NULL)
20310 {
20311 if (charpos < MATRIX_ROW_START_CHARPOS (first))
20312 {
20313 *x = *y = *hpos = *vpos = 0;
20314 return 1;
20315 }
20316 else
20317 {
20318 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
20319 past_end = 1;
20320 }
20321 }
20322
20323 *x = row->x;
20324 *y = row->y;
20325 *vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
20326
20327 glyph = row->glyphs[TEXT_AREA];
20328 end = glyph + row->used[TEXT_AREA];
20329
20330 /* Skip over glyphs not having an object at the start of the row.
20331 These are special glyphs like truncation marks on terminal
20332 frames. */
20333 if (row->displays_text_p)
20334 while (glyph < end
20335 && INTEGERP (glyph->object)
20336 && !EQ (stop, glyph->object)
20337 && glyph->charpos < 0)
20338 {
20339 *x += glyph->pixel_width;
20340 ++glyph;
20341 }
20342
20343 while (glyph < end
20344 && !INTEGERP (glyph->object)
20345 && !EQ (stop, glyph->object)
20346 && (!BUFFERP (glyph->object)
20347 || glyph->charpos < charpos))
20348 {
20349 *x += glyph->pixel_width;
20350 ++glyph;
20351 }
20352
20353 *hpos = glyph - row->glyphs[TEXT_AREA];
20354 return !past_end;
20355 }
20356
20357 #else /* not 1 */
20358
20359 static int
20360 fast_find_position (w, pos, hpos, vpos, x, y, stop)
20361 struct window *w;
20362 int pos;
20363 int *hpos, *vpos, *x, *y;
20364 Lisp_Object stop;
20365 {
20366 int i;
20367 int lastcol;
20368 int maybe_next_line_p = 0;
20369 int line_start_position;
20370 int yb = window_text_bottom_y (w);
20371 struct glyph_row *row, *best_row;
20372 int row_vpos, best_row_vpos;
20373 int current_x;
20374
20375 row = best_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
20376 row_vpos = best_row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
20377
20378 while (row->y < yb)
20379 {
20380 if (row->used[TEXT_AREA])
20381 line_start_position = row->glyphs[TEXT_AREA]->charpos;
20382 else
20383 line_start_position = 0;
20384
20385 if (line_start_position > pos)
20386 break;
20387 /* If the position sought is the end of the buffer,
20388 don't include the blank lines at the bottom of the window. */
20389 else if (line_start_position == pos
20390 && pos == BUF_ZV (XBUFFER (w->buffer)))
20391 {
20392 maybe_next_line_p = 1;
20393 break;
20394 }
20395 else if (line_start_position > 0)
20396 {
20397 best_row = row;
20398 best_row_vpos = row_vpos;
20399 }
20400
20401 if (row->y + row->height >= yb)
20402 break;
20403
20404 ++row;
20405 ++row_vpos;
20406 }
20407
20408 /* Find the right column within BEST_ROW. */
20409 lastcol = 0;
20410 current_x = best_row->x;
20411 for (i = 0; i < best_row->used[TEXT_AREA]; i++)
20412 {
20413 struct glyph *glyph = best_row->glyphs[TEXT_AREA] + i;
20414 int charpos = glyph->charpos;
20415
20416 if (BUFFERP (glyph->object))
20417 {
20418 if (charpos == pos)
20419 {
20420 *hpos = i;
20421 *vpos = best_row_vpos;
20422 *x = current_x;
20423 *y = best_row->y;
20424 return 1;
20425 }
20426 else if (charpos > pos)
20427 break;
20428 }
20429 else if (EQ (glyph->object, stop))
20430 break;
20431
20432 if (charpos > 0)
20433 lastcol = i;
20434 current_x += glyph->pixel_width;
20435 }
20436
20437 /* If we're looking for the end of the buffer,
20438 and we didn't find it in the line we scanned,
20439 use the start of the following line. */
20440 if (maybe_next_line_p)
20441 {
20442 ++best_row;
20443 ++best_row_vpos;
20444 lastcol = 0;
20445 current_x = best_row->x;
20446 }
20447
20448 *vpos = best_row_vpos;
20449 *hpos = lastcol + 1;
20450 *x = current_x;
20451 *y = best_row->y;
20452 return 0;
20453 }
20454
20455 #endif /* not 1 */
20456
20457
20458 /* Find the position of the glyph for position POS in OBJECT in
20459 window W's current matrix, and return in *X, *Y the pixel
20460 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
20461
20462 RIGHT_P non-zero means return the position of the right edge of the
20463 glyph, RIGHT_P zero means return the left edge position.
20464
20465 If no glyph for POS exists in the matrix, return the position of
20466 the glyph with the next smaller position that is in the matrix, if
20467 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
20468 exists in the matrix, return the position of the glyph with the
20469 next larger position in OBJECT.
20470
20471 Value is non-zero if a glyph was found. */
20472
20473 static int
20474 fast_find_string_pos (w, pos, object, hpos, vpos, x, y, right_p)
20475 struct window *w;
20476 int pos;
20477 Lisp_Object object;
20478 int *hpos, *vpos, *x, *y;
20479 int right_p;
20480 {
20481 int yb = window_text_bottom_y (w);
20482 struct glyph_row *r;
20483 struct glyph *best_glyph = NULL;
20484 struct glyph_row *best_row = NULL;
20485 int best_x = 0;
20486
20487 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
20488 r->enabled_p && r->y < yb;
20489 ++r)
20490 {
20491 struct glyph *g = r->glyphs[TEXT_AREA];
20492 struct glyph *e = g + r->used[TEXT_AREA];
20493 int gx;
20494
20495 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
20496 if (EQ (g->object, object))
20497 {
20498 if (g->charpos == pos)
20499 {
20500 best_glyph = g;
20501 best_x = gx;
20502 best_row = r;
20503 goto found;
20504 }
20505 else if (best_glyph == NULL
20506 || ((abs (g->charpos - pos)
20507 < abs (best_glyph->charpos - pos))
20508 && (right_p
20509 ? g->charpos < pos
20510 : g->charpos > pos)))
20511 {
20512 best_glyph = g;
20513 best_x = gx;
20514 best_row = r;
20515 }
20516 }
20517 }
20518
20519 found:
20520
20521 if (best_glyph)
20522 {
20523 *x = best_x;
20524 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
20525
20526 if (right_p)
20527 {
20528 *x += best_glyph->pixel_width;
20529 ++*hpos;
20530 }
20531
20532 *y = best_row->y;
20533 *vpos = best_row - w->current_matrix->rows;
20534 }
20535
20536 return best_glyph != NULL;
20537 }
20538
20539
20540 /* See if position X, Y is within a hot-spot of an image. */
20541
20542 static int
20543 on_hot_spot_p (hot_spot, x, y)
20544 Lisp_Object hot_spot;
20545 int x, y;
20546 {
20547 if (!CONSP (hot_spot))
20548 return 0;
20549
20550 if (EQ (XCAR (hot_spot), Qrect))
20551 {
20552 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
20553 Lisp_Object rect = XCDR (hot_spot);
20554 Lisp_Object tem;
20555 if (!CONSP (rect))
20556 return 0;
20557 if (!CONSP (XCAR (rect)))
20558 return 0;
20559 if (!CONSP (XCDR (rect)))
20560 return 0;
20561 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
20562 return 0;
20563 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
20564 return 0;
20565 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
20566 return 0;
20567 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
20568 return 0;
20569 return 1;
20570 }
20571 else if (EQ (XCAR (hot_spot), Qcircle))
20572 {
20573 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
20574 Lisp_Object circ = XCDR (hot_spot);
20575 Lisp_Object lr, lx0, ly0;
20576 if (CONSP (circ)
20577 && CONSP (XCAR (circ))
20578 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
20579 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
20580 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
20581 {
20582 double r = XFLOATINT (lr);
20583 double dx = XINT (lx0) - x;
20584 double dy = XINT (ly0) - y;
20585 return (dx * dx + dy * dy <= r * r);
20586 }
20587 }
20588 else if (EQ (XCAR (hot_spot), Qpoly))
20589 {
20590 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
20591 if (VECTORP (XCDR (hot_spot)))
20592 {
20593 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
20594 Lisp_Object *poly = v->contents;
20595 int n = v->size;
20596 int i;
20597 int inside = 0;
20598 Lisp_Object lx, ly;
20599 int x0, y0;
20600
20601 /* Need an even number of coordinates, and at least 3 edges. */
20602 if (n < 6 || n & 1)
20603 return 0;
20604
20605 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
20606 If count is odd, we are inside polygon. Pixels on edges
20607 may or may not be included depending on actual geometry of the
20608 polygon. */
20609 if ((lx = poly[n-2], !INTEGERP (lx))
20610 || (ly = poly[n-1], !INTEGERP (lx)))
20611 return 0;
20612 x0 = XINT (lx), y0 = XINT (ly);
20613 for (i = 0; i < n; i += 2)
20614 {
20615 int x1 = x0, y1 = y0;
20616 if ((lx = poly[i], !INTEGERP (lx))
20617 || (ly = poly[i+1], !INTEGERP (ly)))
20618 return 0;
20619 x0 = XINT (lx), y0 = XINT (ly);
20620
20621 /* Does this segment cross the X line? */
20622 if (x0 >= x)
20623 {
20624 if (x1 >= x)
20625 continue;
20626 }
20627 else if (x1 < x)
20628 continue;
20629 if (y > y0 && y > y1)
20630 continue;
20631 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
20632 inside = !inside;
20633 }
20634 return inside;
20635 }
20636 }
20637 /* If we don't understand the format, pretend we're not in the hot-spot. */
20638 return 0;
20639 }
20640
20641 Lisp_Object
20642 find_hot_spot (map, x, y)
20643 Lisp_Object map;
20644 int x, y;
20645 {
20646 while (CONSP (map))
20647 {
20648 if (CONSP (XCAR (map))
20649 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
20650 return XCAR (map);
20651 map = XCDR (map);
20652 }
20653
20654 return Qnil;
20655 }
20656
20657 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
20658 3, 3, 0,
20659 doc: /* Lookup in image map MAP coordinates X and Y.
20660 An image map is an alist where each element has the format (AREA ID PLIST).
20661 An AREA is specified as either a rectangle, a circle, or a polygon:
20662 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
20663 pixel coordinates of the upper left and bottom right corners.
20664 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
20665 and the radius of the circle; r may be a float or integer.
20666 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
20667 vector describes one corner in the polygon.
20668 Returns the alist element for the first matching AREA in MAP. */)
20669 (map, x, y)
20670 Lisp_Object map;
20671 Lisp_Object x, y;
20672 {
20673 if (NILP (map))
20674 return Qnil;
20675
20676 CHECK_NUMBER (x);
20677 CHECK_NUMBER (y);
20678
20679 return find_hot_spot (map, XINT (x), XINT (y));
20680 }
20681
20682
20683 /* Display frame CURSOR, optionally using shape defined by POINTER. */
20684 static void
20685 define_frame_cursor1 (f, cursor, pointer)
20686 struct frame *f;
20687 Cursor cursor;
20688 Lisp_Object pointer;
20689 {
20690 if (!NILP (pointer))
20691 {
20692 if (EQ (pointer, Qarrow))
20693 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
20694 else if (EQ (pointer, Qhand))
20695 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
20696 else if (EQ (pointer, Qtext))
20697 cursor = FRAME_X_OUTPUT (f)->text_cursor;
20698 else if (EQ (pointer, intern ("hdrag")))
20699 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
20700 #ifdef HAVE_X_WINDOWS
20701 else if (EQ (pointer, intern ("vdrag")))
20702 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
20703 #endif
20704 else if (EQ (pointer, intern ("hourglass")))
20705 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
20706 else if (EQ (pointer, Qmodeline))
20707 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
20708 else
20709 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
20710 }
20711
20712 if (cursor != No_Cursor)
20713 rif->define_frame_cursor (f, cursor);
20714 }
20715
20716 /* Take proper action when mouse has moved to the mode or header line
20717 or marginal area AREA of window W, x-position X and y-position Y.
20718 X is relative to the start of the text display area of W, so the
20719 width of bitmap areas and scroll bars must be subtracted to get a
20720 position relative to the start of the mode line. */
20721
20722 static void
20723 note_mode_line_or_margin_highlight (w, x, y, area)
20724 struct window *w;
20725 int x, y;
20726 enum window_part area;
20727 {
20728 struct frame *f = XFRAME (w->frame);
20729 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
20730 Cursor cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
20731 Lisp_Object pointer = Qnil;
20732 int charpos, dx, dy, width, height;
20733 Lisp_Object string, object = Qnil;
20734 Lisp_Object pos, help;
20735
20736 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
20737 string = mode_line_string (w, area, &x, &y, &charpos,
20738 &object, &dx, &dy, &width, &height);
20739 else
20740 {
20741 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
20742 string = marginal_area_string (w, area, &x, &y, &charpos,
20743 &object, &dx, &dy, &width, &height);
20744 }
20745
20746 help = Qnil;
20747
20748 if (IMAGEP (object))
20749 {
20750 Lisp_Object image_map, hotspot;
20751 if ((image_map = Fplist_get (XCDR (object), QCmap),
20752 !NILP (image_map))
20753 && (hotspot = find_hot_spot (image_map, dx, dy),
20754 CONSP (hotspot))
20755 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
20756 {
20757 Lisp_Object area_id, plist;
20758
20759 area_id = XCAR (hotspot);
20760 /* Could check AREA_ID to see if we enter/leave this hot-spot.
20761 If so, we could look for mouse-enter, mouse-leave
20762 properties in PLIST (and do something...). */
20763 if ((plist = XCDR (hotspot), CONSP (plist)))
20764 {
20765 pointer = Fplist_get (plist, Qpointer);
20766 if (NILP (pointer))
20767 pointer = Qhand;
20768 help = Fplist_get (plist, Qhelp_echo);
20769 if (!NILP (help))
20770 {
20771 help_echo_string = help;
20772 /* Is this correct? ++kfs */
20773 XSETWINDOW (help_echo_window, w);
20774 help_echo_object = w->buffer;
20775 help_echo_pos = charpos;
20776 }
20777 }
20778 if (NILP (pointer))
20779 pointer = Fplist_get (XCDR (object), QCpointer);
20780 }
20781 }
20782
20783 if (STRINGP (string))
20784 {
20785 pos = make_number (charpos);
20786 /* If we're on a string with `help-echo' text property, arrange
20787 for the help to be displayed. This is done by setting the
20788 global variable help_echo_string to the help string. */
20789 help = Fget_text_property (pos, Qhelp_echo, string);
20790 if (!NILP (help))
20791 {
20792 help_echo_string = help;
20793 XSETWINDOW (help_echo_window, w);
20794 help_echo_object = string;
20795 help_echo_pos = charpos;
20796 }
20797
20798 if (NILP (pointer))
20799 pointer = Fget_text_property (pos, Qpointer, string);
20800
20801 /* Change the mouse pointer according to what is under X/Y. */
20802 if (NILP (pointer) && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
20803 {
20804 Lisp_Object map;
20805 map = Fget_text_property (pos, Qlocal_map, string);
20806 if (!KEYMAPP (map))
20807 map = Fget_text_property (pos, Qkeymap, string);
20808 if (!KEYMAPP (map))
20809 cursor = dpyinfo->vertical_scroll_bar_cursor;
20810 }
20811 }
20812
20813 define_frame_cursor1 (f, cursor, pointer);
20814 }
20815
20816
20817 /* EXPORT:
20818 Take proper action when the mouse has moved to position X, Y on
20819 frame F as regards highlighting characters that have mouse-face
20820 properties. Also de-highlighting chars where the mouse was before.
20821 X and Y can be negative or out of range. */
20822
20823 void
20824 note_mouse_highlight (f, x, y)
20825 struct frame *f;
20826 int x, y;
20827 {
20828 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
20829 enum window_part part;
20830 Lisp_Object window;
20831 struct window *w;
20832 Cursor cursor = No_Cursor;
20833 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
20834 struct buffer *b;
20835
20836 /* When a menu is active, don't highlight because this looks odd. */
20837 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NTGUI)
20838 if (popup_activated ())
20839 return;
20840 #endif
20841
20842 if (NILP (Vmouse_highlight)
20843 || !f->glyphs_initialized_p)
20844 return;
20845
20846 dpyinfo->mouse_face_mouse_x = x;
20847 dpyinfo->mouse_face_mouse_y = y;
20848 dpyinfo->mouse_face_mouse_frame = f;
20849
20850 if (dpyinfo->mouse_face_defer)
20851 return;
20852
20853 if (gc_in_progress)
20854 {
20855 dpyinfo->mouse_face_deferred_gc = 1;
20856 return;
20857 }
20858
20859 /* Which window is that in? */
20860 window = window_from_coordinates (f, x, y, &part, 0, 0, 1);
20861
20862 /* If we were displaying active text in another window, clear that. */
20863 if (! EQ (window, dpyinfo->mouse_face_window))
20864 clear_mouse_face (dpyinfo);
20865
20866 /* Not on a window -> return. */
20867 if (!WINDOWP (window))
20868 return;
20869
20870 /* Reset help_echo_string. It will get recomputed below. */
20871 help_echo_string = Qnil;
20872
20873 /* Convert to window-relative pixel coordinates. */
20874 w = XWINDOW (window);
20875 frame_to_window_pixel_xy (w, &x, &y);
20876
20877 /* Handle tool-bar window differently since it doesn't display a
20878 buffer. */
20879 if (EQ (window, f->tool_bar_window))
20880 {
20881 note_tool_bar_highlight (f, x, y);
20882 return;
20883 }
20884
20885 /* Mouse is on the mode, header line or margin? */
20886 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
20887 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
20888 {
20889 note_mode_line_or_margin_highlight (w, x, y, part);
20890 return;
20891 }
20892
20893 if (part == ON_VERTICAL_BORDER)
20894 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
20895 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE)
20896 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
20897 else
20898 cursor = FRAME_X_OUTPUT (f)->text_cursor;
20899
20900 /* Are we in a window whose display is up to date?
20901 And verify the buffer's text has not changed. */
20902 b = XBUFFER (w->buffer);
20903 if (part == ON_TEXT
20904 && EQ (w->window_end_valid, w->buffer)
20905 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
20906 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
20907 {
20908 int hpos, vpos, pos, i, dx, dy, area;
20909 struct glyph *glyph;
20910 Lisp_Object object;
20911 Lisp_Object mouse_face = Qnil, overlay = Qnil, position;
20912 Lisp_Object *overlay_vec = NULL;
20913 int noverlays;
20914 struct buffer *obuf;
20915 int obegv, ozv, same_region;
20916
20917 /* Find the glyph under X/Y. */
20918 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
20919
20920 /* Look for :pointer property on image. */
20921 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
20922 {
20923 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
20924 if (img != NULL && IMAGEP (img->spec))
20925 {
20926 Lisp_Object image_map, hotspot;
20927 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
20928 !NILP (image_map))
20929 && (hotspot = find_hot_spot (image_map,
20930 glyph->slice.x + dx,
20931 glyph->slice.y + dy),
20932 CONSP (hotspot))
20933 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
20934 {
20935 Lisp_Object area_id, plist;
20936
20937 area_id = XCAR (hotspot);
20938 /* Could check AREA_ID to see if we enter/leave this hot-spot.
20939 If so, we could look for mouse-enter, mouse-leave
20940 properties in PLIST (and do something...). */
20941 if ((plist = XCDR (hotspot), CONSP (plist)))
20942 {
20943 pointer = Fplist_get (plist, Qpointer);
20944 if (NILP (pointer))
20945 pointer = Qhand;
20946 help_echo_string = Fplist_get (plist, Qhelp_echo);
20947 if (!NILP (help_echo_string))
20948 {
20949 help_echo_window = window;
20950 help_echo_object = glyph->object;
20951 help_echo_pos = glyph->charpos;
20952 }
20953 }
20954 }
20955 if (NILP (pointer))
20956 pointer = Fplist_get (XCDR (img->spec), QCpointer);
20957 }
20958 }
20959
20960 /* Clear mouse face if X/Y not over text. */
20961 if (glyph == NULL
20962 || area != TEXT_AREA
20963 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p)
20964 {
20965 if (clear_mouse_face (dpyinfo))
20966 cursor = No_Cursor;
20967 if (NILP (pointer))
20968 {
20969 if (area != TEXT_AREA)
20970 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
20971 else
20972 pointer = Vvoid_text_area_pointer;
20973 }
20974 goto set_cursor;
20975 }
20976
20977 pos = glyph->charpos;
20978 object = glyph->object;
20979 if (!STRINGP (object) && !BUFFERP (object))
20980 goto set_cursor;
20981
20982 /* If we get an out-of-range value, return now; avoid an error. */
20983 if (BUFFERP (object) && pos > BUF_Z (b))
20984 goto set_cursor;
20985
20986 /* Make the window's buffer temporarily current for
20987 overlays_at and compute_char_face. */
20988 obuf = current_buffer;
20989 current_buffer = b;
20990 obegv = BEGV;
20991 ozv = ZV;
20992 BEGV = BEG;
20993 ZV = Z;
20994
20995 /* Is this char mouse-active or does it have help-echo? */
20996 position = make_number (pos);
20997
20998 if (BUFFERP (object))
20999 {
21000 /* Put all the overlays we want in a vector in overlay_vec. */
21001 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
21002 /* Sort overlays into increasing priority order. */
21003 noverlays = sort_overlays (overlay_vec, noverlays, w);
21004 }
21005 else
21006 noverlays = 0;
21007
21008 same_region = (EQ (window, dpyinfo->mouse_face_window)
21009 && vpos >= dpyinfo->mouse_face_beg_row
21010 && vpos <= dpyinfo->mouse_face_end_row
21011 && (vpos > dpyinfo->mouse_face_beg_row
21012 || hpos >= dpyinfo->mouse_face_beg_col)
21013 && (vpos < dpyinfo->mouse_face_end_row
21014 || hpos < dpyinfo->mouse_face_end_col
21015 || dpyinfo->mouse_face_past_end));
21016
21017 if (same_region)
21018 cursor = No_Cursor;
21019
21020 /* Check mouse-face highlighting. */
21021 if (! same_region
21022 /* If there exists an overlay with mouse-face overlapping
21023 the one we are currently highlighting, we have to
21024 check if we enter the overlapping overlay, and then
21025 highlight only that. */
21026 || (OVERLAYP (dpyinfo->mouse_face_overlay)
21027 && mouse_face_overlay_overlaps (dpyinfo->mouse_face_overlay)))
21028 {
21029 /* Find the highest priority overlay that has a mouse-face
21030 property. */
21031 overlay = Qnil;
21032 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
21033 {
21034 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
21035 if (!NILP (mouse_face))
21036 overlay = overlay_vec[i];
21037 }
21038
21039 /* If we're actually highlighting the same overlay as
21040 before, there's no need to do that again. */
21041 if (!NILP (overlay)
21042 && EQ (overlay, dpyinfo->mouse_face_overlay))
21043 goto check_help_echo;
21044
21045 dpyinfo->mouse_face_overlay = overlay;
21046
21047 /* Clear the display of the old active region, if any. */
21048 if (clear_mouse_face (dpyinfo))
21049 cursor = No_Cursor;
21050
21051 /* If no overlay applies, get a text property. */
21052 if (NILP (overlay))
21053 mouse_face = Fget_text_property (position, Qmouse_face, object);
21054
21055 /* Handle the overlay case. */
21056 if (!NILP (overlay))
21057 {
21058 /* Find the range of text around this char that
21059 should be active. */
21060 Lisp_Object before, after;
21061 int ignore;
21062
21063 before = Foverlay_start (overlay);
21064 after = Foverlay_end (overlay);
21065 /* Record this as the current active region. */
21066 fast_find_position (w, XFASTINT (before),
21067 &dpyinfo->mouse_face_beg_col,
21068 &dpyinfo->mouse_face_beg_row,
21069 &dpyinfo->mouse_face_beg_x,
21070 &dpyinfo->mouse_face_beg_y, Qnil);
21071
21072 dpyinfo->mouse_face_past_end
21073 = !fast_find_position (w, XFASTINT (after),
21074 &dpyinfo->mouse_face_end_col,
21075 &dpyinfo->mouse_face_end_row,
21076 &dpyinfo->mouse_face_end_x,
21077 &dpyinfo->mouse_face_end_y, Qnil);
21078 dpyinfo->mouse_face_window = window;
21079
21080 dpyinfo->mouse_face_face_id
21081 = face_at_buffer_position (w, pos, 0, 0,
21082 &ignore, pos + 1,
21083 !dpyinfo->mouse_face_hidden);
21084
21085 /* Display it as active. */
21086 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
21087 cursor = No_Cursor;
21088 }
21089 /* Handle the text property case. */
21090 else if (!NILP (mouse_face) && BUFFERP (object))
21091 {
21092 /* Find the range of text around this char that
21093 should be active. */
21094 Lisp_Object before, after, beginning, end;
21095 int ignore;
21096
21097 beginning = Fmarker_position (w->start);
21098 end = make_number (BUF_Z (XBUFFER (object))
21099 - XFASTINT (w->window_end_pos));
21100 before
21101 = Fprevious_single_property_change (make_number (pos + 1),
21102 Qmouse_face,
21103 object, beginning);
21104 after
21105 = Fnext_single_property_change (position, Qmouse_face,
21106 object, end);
21107
21108 /* Record this as the current active region. */
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, Qnil);
21114 dpyinfo->mouse_face_past_end
21115 = !fast_find_position (w, XFASTINT (after),
21116 &dpyinfo->mouse_face_end_col,
21117 &dpyinfo->mouse_face_end_row,
21118 &dpyinfo->mouse_face_end_x,
21119 &dpyinfo->mouse_face_end_y, Qnil);
21120 dpyinfo->mouse_face_window = window;
21121
21122 if (BUFFERP (object))
21123 dpyinfo->mouse_face_face_id
21124 = face_at_buffer_position (w, pos, 0, 0,
21125 &ignore, pos + 1,
21126 !dpyinfo->mouse_face_hidden);
21127
21128 /* Display it as active. */
21129 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
21130 cursor = No_Cursor;
21131 }
21132 else if (!NILP (mouse_face) && STRINGP (object))
21133 {
21134 Lisp_Object b, e;
21135 int ignore;
21136
21137 b = Fprevious_single_property_change (make_number (pos + 1),
21138 Qmouse_face,
21139 object, Qnil);
21140 e = Fnext_single_property_change (position, Qmouse_face,
21141 object, Qnil);
21142 if (NILP (b))
21143 b = make_number (0);
21144 if (NILP (e))
21145 e = make_number (SCHARS (object) - 1);
21146 fast_find_string_pos (w, XINT (b), object,
21147 &dpyinfo->mouse_face_beg_col,
21148 &dpyinfo->mouse_face_beg_row,
21149 &dpyinfo->mouse_face_beg_x,
21150 &dpyinfo->mouse_face_beg_y, 0);
21151 fast_find_string_pos (w, XINT (e), object,
21152 &dpyinfo->mouse_face_end_col,
21153 &dpyinfo->mouse_face_end_row,
21154 &dpyinfo->mouse_face_end_x,
21155 &dpyinfo->mouse_face_end_y, 1);
21156 dpyinfo->mouse_face_past_end = 0;
21157 dpyinfo->mouse_face_window = window;
21158 dpyinfo->mouse_face_face_id
21159 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
21160 glyph->face_id, 1);
21161 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
21162 cursor = No_Cursor;
21163 }
21164 else if (STRINGP (object) && NILP (mouse_face))
21165 {
21166 /* A string which doesn't have mouse-face, but
21167 the text ``under'' it might have. */
21168 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
21169 int start = MATRIX_ROW_START_CHARPOS (r);
21170
21171 pos = string_buffer_position (w, object, start);
21172 if (pos > 0)
21173 mouse_face = get_char_property_and_overlay (make_number (pos),
21174 Qmouse_face,
21175 w->buffer,
21176 &overlay);
21177 if (!NILP (mouse_face) && !NILP (overlay))
21178 {
21179 Lisp_Object before = Foverlay_start (overlay);
21180 Lisp_Object after = Foverlay_end (overlay);
21181 int ignore;
21182
21183 /* Note that we might not be able to find position
21184 BEFORE in the glyph matrix if the overlay is
21185 entirely covered by a `display' property. In
21186 this case, we overshoot. So let's stop in
21187 the glyph matrix before glyphs for OBJECT. */
21188 fast_find_position (w, XFASTINT (before),
21189 &dpyinfo->mouse_face_beg_col,
21190 &dpyinfo->mouse_face_beg_row,
21191 &dpyinfo->mouse_face_beg_x,
21192 &dpyinfo->mouse_face_beg_y,
21193 object);
21194
21195 dpyinfo->mouse_face_past_end
21196 = !fast_find_position (w, XFASTINT (after),
21197 &dpyinfo->mouse_face_end_col,
21198 &dpyinfo->mouse_face_end_row,
21199 &dpyinfo->mouse_face_end_x,
21200 &dpyinfo->mouse_face_end_y,
21201 Qnil);
21202 dpyinfo->mouse_face_window = window;
21203 dpyinfo->mouse_face_face_id
21204 = face_at_buffer_position (w, pos, 0, 0,
21205 &ignore, pos + 1,
21206 !dpyinfo->mouse_face_hidden);
21207
21208 /* Display it as active. */
21209 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
21210 cursor = No_Cursor;
21211 }
21212 }
21213 }
21214
21215 check_help_echo:
21216
21217 /* Look for a `help-echo' property. */
21218 if (NILP (help_echo_string)) {
21219 Lisp_Object help, overlay;
21220
21221 /* Check overlays first. */
21222 help = overlay = Qnil;
21223 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
21224 {
21225 overlay = overlay_vec[i];
21226 help = Foverlay_get (overlay, Qhelp_echo);
21227 }
21228
21229 if (!NILP (help))
21230 {
21231 help_echo_string = help;
21232 help_echo_window = window;
21233 help_echo_object = overlay;
21234 help_echo_pos = pos;
21235 }
21236 else
21237 {
21238 Lisp_Object object = glyph->object;
21239 int charpos = glyph->charpos;
21240
21241 /* Try text properties. */
21242 if (STRINGP (object)
21243 && charpos >= 0
21244 && charpos < SCHARS (object))
21245 {
21246 help = Fget_text_property (make_number (charpos),
21247 Qhelp_echo, object);
21248 if (NILP (help))
21249 {
21250 /* If the string itself doesn't specify a help-echo,
21251 see if the buffer text ``under'' it does. */
21252 struct glyph_row *r
21253 = MATRIX_ROW (w->current_matrix, vpos);
21254 int start = MATRIX_ROW_START_CHARPOS (r);
21255 int pos = string_buffer_position (w, object, start);
21256 if (pos > 0)
21257 {
21258 help = Fget_char_property (make_number (pos),
21259 Qhelp_echo, w->buffer);
21260 if (!NILP (help))
21261 {
21262 charpos = pos;
21263 object = w->buffer;
21264 }
21265 }
21266 }
21267 }
21268 else if (BUFFERP (object)
21269 && charpos >= BEGV
21270 && charpos < ZV)
21271 help = Fget_text_property (make_number (charpos), Qhelp_echo,
21272 object);
21273
21274 if (!NILP (help))
21275 {
21276 help_echo_string = help;
21277 help_echo_window = window;
21278 help_echo_object = object;
21279 help_echo_pos = charpos;
21280 }
21281 }
21282 }
21283
21284 /* Look for a `pointer' property. */
21285 if (NILP (pointer))
21286 {
21287 /* Check overlays first. */
21288 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
21289 pointer = Foverlay_get (overlay_vec[i], Qpointer);
21290
21291 if (NILP (pointer))
21292 {
21293 Lisp_Object object = glyph->object;
21294 int charpos = glyph->charpos;
21295
21296 /* Try text properties. */
21297 if (STRINGP (object)
21298 && charpos >= 0
21299 && charpos < SCHARS (object))
21300 {
21301 pointer = Fget_text_property (make_number (charpos),
21302 Qpointer, object);
21303 if (NILP (pointer))
21304 {
21305 /* If the string itself doesn't specify a pointer,
21306 see if the buffer text ``under'' it does. */
21307 struct glyph_row *r
21308 = MATRIX_ROW (w->current_matrix, vpos);
21309 int start = MATRIX_ROW_START_CHARPOS (r);
21310 int pos = string_buffer_position (w, object, start);
21311 if (pos > 0)
21312 pointer = Fget_char_property (make_number (pos),
21313 Qpointer, w->buffer);
21314 }
21315 }
21316 else if (BUFFERP (object)
21317 && charpos >= BEGV
21318 && charpos < ZV)
21319 pointer = Fget_text_property (make_number (charpos),
21320 Qpointer, object);
21321 }
21322 }
21323
21324 BEGV = obegv;
21325 ZV = ozv;
21326 current_buffer = obuf;
21327 }
21328
21329 set_cursor:
21330
21331 define_frame_cursor1 (f, cursor, pointer);
21332 }
21333
21334
21335 /* EXPORT for RIF:
21336 Clear any mouse-face on window W. This function is part of the
21337 redisplay interface, and is called from try_window_id and similar
21338 functions to ensure the mouse-highlight is off. */
21339
21340 void
21341 x_clear_window_mouse_face (w)
21342 struct window *w;
21343 {
21344 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
21345 Lisp_Object window;
21346
21347 BLOCK_INPUT;
21348 XSETWINDOW (window, w);
21349 if (EQ (window, dpyinfo->mouse_face_window))
21350 clear_mouse_face (dpyinfo);
21351 UNBLOCK_INPUT;
21352 }
21353
21354
21355 /* EXPORT:
21356 Just discard the mouse face information for frame F, if any.
21357 This is used when the size of F is changed. */
21358
21359 void
21360 cancel_mouse_face (f)
21361 struct frame *f;
21362 {
21363 Lisp_Object window;
21364 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
21365
21366 window = dpyinfo->mouse_face_window;
21367 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
21368 {
21369 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
21370 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
21371 dpyinfo->mouse_face_window = Qnil;
21372 }
21373 }
21374
21375
21376 #endif /* HAVE_WINDOW_SYSTEM */
21377
21378 \f
21379 /***********************************************************************
21380 Exposure Events
21381 ***********************************************************************/
21382
21383 #ifdef HAVE_WINDOW_SYSTEM
21384
21385 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
21386 which intersects rectangle R. R is in window-relative coordinates. */
21387
21388 static void
21389 expose_area (w, row, r, area)
21390 struct window *w;
21391 struct glyph_row *row;
21392 XRectangle *r;
21393 enum glyph_row_area area;
21394 {
21395 struct glyph *first = row->glyphs[area];
21396 struct glyph *end = row->glyphs[area] + row->used[area];
21397 struct glyph *last;
21398 int first_x, start_x, x;
21399
21400 if (area == TEXT_AREA && row->fill_line_p)
21401 /* If row extends face to end of line write the whole line. */
21402 draw_glyphs (w, 0, row, area,
21403 0, row->used[area],
21404 DRAW_NORMAL_TEXT, 0);
21405 else
21406 {
21407 /* Set START_X to the window-relative start position for drawing glyphs of
21408 AREA. The first glyph of the text area can be partially visible.
21409 The first glyphs of other areas cannot. */
21410 start_x = window_box_left_offset (w, area);
21411 x = start_x;
21412 if (area == TEXT_AREA)
21413 x += row->x;
21414
21415 /* Find the first glyph that must be redrawn. */
21416 while (first < end
21417 && x + first->pixel_width < r->x)
21418 {
21419 x += first->pixel_width;
21420 ++first;
21421 }
21422
21423 /* Find the last one. */
21424 last = first;
21425 first_x = x;
21426 while (last < end
21427 && x < r->x + r->width)
21428 {
21429 x += last->pixel_width;
21430 ++last;
21431 }
21432
21433 /* Repaint. */
21434 if (last > first)
21435 draw_glyphs (w, first_x - start_x, row, area,
21436 first - row->glyphs[area], last - row->glyphs[area],
21437 DRAW_NORMAL_TEXT, 0);
21438 }
21439 }
21440
21441
21442 /* Redraw the parts of the glyph row ROW on window W intersecting
21443 rectangle R. R is in window-relative coordinates. Value is
21444 non-zero if mouse-face was overwritten. */
21445
21446 static int
21447 expose_line (w, row, r)
21448 struct window *w;
21449 struct glyph_row *row;
21450 XRectangle *r;
21451 {
21452 xassert (row->enabled_p);
21453
21454 if (row->mode_line_p || w->pseudo_window_p)
21455 draw_glyphs (w, 0, row, TEXT_AREA,
21456 0, row->used[TEXT_AREA],
21457 DRAW_NORMAL_TEXT, 0);
21458 else
21459 {
21460 if (row->used[LEFT_MARGIN_AREA])
21461 expose_area (w, row, r, LEFT_MARGIN_AREA);
21462 if (row->used[TEXT_AREA])
21463 expose_area (w, row, r, TEXT_AREA);
21464 if (row->used[RIGHT_MARGIN_AREA])
21465 expose_area (w, row, r, RIGHT_MARGIN_AREA);
21466 draw_row_fringe_bitmaps (w, row);
21467 }
21468
21469 return row->mouse_face_p;
21470 }
21471
21472
21473 /* Redraw those parts of glyphs rows during expose event handling that
21474 overlap other rows. Redrawing of an exposed line writes over parts
21475 of lines overlapping that exposed line; this function fixes that.
21476
21477 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
21478 row in W's current matrix that is exposed and overlaps other rows.
21479 LAST_OVERLAPPING_ROW is the last such row. */
21480
21481 static void
21482 expose_overlaps (w, first_overlapping_row, last_overlapping_row)
21483 struct window *w;
21484 struct glyph_row *first_overlapping_row;
21485 struct glyph_row *last_overlapping_row;
21486 {
21487 struct glyph_row *row;
21488
21489 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
21490 if (row->overlapping_p)
21491 {
21492 xassert (row->enabled_p && !row->mode_line_p);
21493
21494 if (row->used[LEFT_MARGIN_AREA])
21495 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA);
21496
21497 if (row->used[TEXT_AREA])
21498 x_fix_overlapping_area (w, row, TEXT_AREA);
21499
21500 if (row->used[RIGHT_MARGIN_AREA])
21501 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA);
21502 }
21503 }
21504
21505
21506 /* Return non-zero if W's cursor intersects rectangle R. */
21507
21508 static int
21509 phys_cursor_in_rect_p (w, r)
21510 struct window *w;
21511 XRectangle *r;
21512 {
21513 XRectangle cr, result;
21514 struct glyph *cursor_glyph;
21515
21516 cursor_glyph = get_phys_cursor_glyph (w);
21517 if (cursor_glyph)
21518 {
21519 /* r is relative to W's box, but w->phys_cursor.x is relative
21520 to left edge of W's TEXT area. Adjust it. */
21521 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
21522 cr.y = w->phys_cursor.y;
21523 cr.width = cursor_glyph->pixel_width;
21524 cr.height = w->phys_cursor_height;
21525 /* ++KFS: W32 version used W32-specific IntersectRect here, but
21526 I assume the effect is the same -- and this is portable. */
21527 return x_intersect_rectangles (&cr, r, &result);
21528 }
21529 else
21530 return 0;
21531 }
21532
21533
21534 /* EXPORT:
21535 Draw a vertical window border to the right of window W if W doesn't
21536 have vertical scroll bars. */
21537
21538 void
21539 x_draw_vertical_border (w)
21540 struct window *w;
21541 {
21542 /* We could do better, if we knew what type of scroll-bar the adjacent
21543 windows (on either side) have... But we don't :-(
21544 However, I think this works ok. ++KFS 2003-04-25 */
21545
21546 /* Redraw borders between horizontally adjacent windows. Don't
21547 do it for frames with vertical scroll bars because either the
21548 right scroll bar of a window, or the left scroll bar of its
21549 neighbor will suffice as a border. */
21550 if (!WINDOW_RIGHTMOST_P (w)
21551 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
21552 {
21553 int x0, x1, y0, y1;
21554
21555 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
21556 y1 -= 1;
21557
21558 rif->draw_vertical_window_border (w, x1, y0, y1);
21559 }
21560 else if (!WINDOW_LEFTMOST_P (w)
21561 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
21562 {
21563 int x0, x1, y0, y1;
21564
21565 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
21566 y1 -= 1;
21567
21568 rif->draw_vertical_window_border (w, x0, y0, y1);
21569 }
21570 }
21571
21572
21573 /* Redraw the part of window W intersection rectangle FR. Pixel
21574 coordinates in FR are frame-relative. Call this function with
21575 input blocked. Value is non-zero if the exposure overwrites
21576 mouse-face. */
21577
21578 static int
21579 expose_window (w, fr)
21580 struct window *w;
21581 XRectangle *fr;
21582 {
21583 struct frame *f = XFRAME (w->frame);
21584 XRectangle wr, r;
21585 int mouse_face_overwritten_p = 0;
21586
21587 /* If window is not yet fully initialized, do nothing. This can
21588 happen when toolkit scroll bars are used and a window is split.
21589 Reconfiguring the scroll bar will generate an expose for a newly
21590 created window. */
21591 if (w->current_matrix == NULL)
21592 return 0;
21593
21594 /* When we're currently updating the window, display and current
21595 matrix usually don't agree. Arrange for a thorough display
21596 later. */
21597 if (w == updated_window)
21598 {
21599 SET_FRAME_GARBAGED (f);
21600 return 0;
21601 }
21602
21603 /* Frame-relative pixel rectangle of W. */
21604 wr.x = WINDOW_LEFT_EDGE_X (w);
21605 wr.y = WINDOW_TOP_EDGE_Y (w);
21606 wr.width = WINDOW_TOTAL_WIDTH (w);
21607 wr.height = WINDOW_TOTAL_HEIGHT (w);
21608
21609 if (x_intersect_rectangles (fr, &wr, &r))
21610 {
21611 int yb = window_text_bottom_y (w);
21612 struct glyph_row *row;
21613 int cursor_cleared_p;
21614 struct glyph_row *first_overlapping_row, *last_overlapping_row;
21615
21616 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
21617 r.x, r.y, r.width, r.height));
21618
21619 /* Convert to window coordinates. */
21620 r.x -= WINDOW_LEFT_EDGE_X (w);
21621 r.y -= WINDOW_TOP_EDGE_Y (w);
21622
21623 /* Turn off the cursor. */
21624 if (!w->pseudo_window_p
21625 && phys_cursor_in_rect_p (w, &r))
21626 {
21627 x_clear_cursor (w);
21628 cursor_cleared_p = 1;
21629 }
21630 else
21631 cursor_cleared_p = 0;
21632
21633 /* Update lines intersecting rectangle R. */
21634 first_overlapping_row = last_overlapping_row = NULL;
21635 for (row = w->current_matrix->rows;
21636 row->enabled_p;
21637 ++row)
21638 {
21639 int y0 = row->y;
21640 int y1 = MATRIX_ROW_BOTTOM_Y (row);
21641
21642 if ((y0 >= r.y && y0 < r.y + r.height)
21643 || (y1 > r.y && y1 < r.y + r.height)
21644 || (r.y >= y0 && r.y < y1)
21645 || (r.y + r.height > y0 && r.y + r.height < y1))
21646 {
21647 if (row->overlapping_p)
21648 {
21649 if (first_overlapping_row == NULL)
21650 first_overlapping_row = row;
21651 last_overlapping_row = row;
21652 }
21653
21654 if (expose_line (w, row, &r))
21655 mouse_face_overwritten_p = 1;
21656 }
21657
21658 if (y1 >= yb)
21659 break;
21660 }
21661
21662 /* Display the mode line if there is one. */
21663 if (WINDOW_WANTS_MODELINE_P (w)
21664 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
21665 row->enabled_p)
21666 && row->y < r.y + r.height)
21667 {
21668 if (expose_line (w, row, &r))
21669 mouse_face_overwritten_p = 1;
21670 }
21671
21672 if (!w->pseudo_window_p)
21673 {
21674 /* Fix the display of overlapping rows. */
21675 if (first_overlapping_row)
21676 expose_overlaps (w, first_overlapping_row, last_overlapping_row);
21677
21678 /* Draw border between windows. */
21679 x_draw_vertical_border (w);
21680
21681 /* Turn the cursor on again. */
21682 if (cursor_cleared_p)
21683 update_window_cursor (w, 1);
21684 }
21685 }
21686
21687 #ifdef HAVE_CARBON
21688 /* Display scroll bar for this window. */
21689 if (!NILP (w->vertical_scroll_bar))
21690 {
21691 /* ++KFS:
21692 If this doesn't work here (maybe some header files are missing),
21693 make a function in macterm.c and call it to do the job! */
21694 ControlHandle ch
21695 = SCROLL_BAR_CONTROL_HANDLE (XSCROLL_BAR (w->vertical_scroll_bar));
21696
21697 Draw1Control (ch);
21698 }
21699 #endif
21700
21701 return mouse_face_overwritten_p;
21702 }
21703
21704
21705
21706 /* Redraw (parts) of all windows in the window tree rooted at W that
21707 intersect R. R contains frame pixel coordinates. Value is
21708 non-zero if the exposure overwrites mouse-face. */
21709
21710 static int
21711 expose_window_tree (w, r)
21712 struct window *w;
21713 XRectangle *r;
21714 {
21715 struct frame *f = XFRAME (w->frame);
21716 int mouse_face_overwritten_p = 0;
21717
21718 while (w && !FRAME_GARBAGED_P (f))
21719 {
21720 if (!NILP (w->hchild))
21721 mouse_face_overwritten_p
21722 |= expose_window_tree (XWINDOW (w->hchild), r);
21723 else if (!NILP (w->vchild))
21724 mouse_face_overwritten_p
21725 |= expose_window_tree (XWINDOW (w->vchild), r);
21726 else
21727 mouse_face_overwritten_p |= expose_window (w, r);
21728
21729 w = NILP (w->next) ? NULL : XWINDOW (w->next);
21730 }
21731
21732 return mouse_face_overwritten_p;
21733 }
21734
21735
21736 /* EXPORT:
21737 Redisplay an exposed area of frame F. X and Y are the upper-left
21738 corner of the exposed rectangle. W and H are width and height of
21739 the exposed area. All are pixel values. W or H zero means redraw
21740 the entire frame. */
21741
21742 void
21743 expose_frame (f, x, y, w, h)
21744 struct frame *f;
21745 int x, y, w, h;
21746 {
21747 XRectangle r;
21748 int mouse_face_overwritten_p = 0;
21749
21750 TRACE ((stderr, "expose_frame "));
21751
21752 /* No need to redraw if frame will be redrawn soon. */
21753 if (FRAME_GARBAGED_P (f))
21754 {
21755 TRACE ((stderr, " garbaged\n"));
21756 return;
21757 }
21758
21759 #ifdef HAVE_CARBON
21760 /* MAC_TODO: this is a kludge, but if scroll bars are not activated
21761 or deactivated here, for unknown reasons, activated scroll bars
21762 are shown in deactivated frames in some instances. */
21763 if (f == FRAME_MAC_DISPLAY_INFO (f)->x_focus_frame)
21764 activate_scroll_bars (f);
21765 else
21766 deactivate_scroll_bars (f);
21767 #endif
21768
21769 /* If basic faces haven't been realized yet, there is no point in
21770 trying to redraw anything. This can happen when we get an expose
21771 event while Emacs is starting, e.g. by moving another window. */
21772 if (FRAME_FACE_CACHE (f) == NULL
21773 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
21774 {
21775 TRACE ((stderr, " no faces\n"));
21776 return;
21777 }
21778
21779 if (w == 0 || h == 0)
21780 {
21781 r.x = r.y = 0;
21782 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
21783 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
21784 }
21785 else
21786 {
21787 r.x = x;
21788 r.y = y;
21789 r.width = w;
21790 r.height = h;
21791 }
21792
21793 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
21794 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
21795
21796 if (WINDOWP (f->tool_bar_window))
21797 mouse_face_overwritten_p
21798 |= expose_window (XWINDOW (f->tool_bar_window), &r);
21799
21800 #ifdef HAVE_X_WINDOWS
21801 #ifndef MSDOS
21802 #ifndef USE_X_TOOLKIT
21803 if (WINDOWP (f->menu_bar_window))
21804 mouse_face_overwritten_p
21805 |= expose_window (XWINDOW (f->menu_bar_window), &r);
21806 #endif /* not USE_X_TOOLKIT */
21807 #endif
21808 #endif
21809
21810 /* Some window managers support a focus-follows-mouse style with
21811 delayed raising of frames. Imagine a partially obscured frame,
21812 and moving the mouse into partially obscured mouse-face on that
21813 frame. The visible part of the mouse-face will be highlighted,
21814 then the WM raises the obscured frame. With at least one WM, KDE
21815 2.1, Emacs is not getting any event for the raising of the frame
21816 (even tried with SubstructureRedirectMask), only Expose events.
21817 These expose events will draw text normally, i.e. not
21818 highlighted. Which means we must redo the highlight here.
21819 Subsume it under ``we love X''. --gerd 2001-08-15 */
21820 /* Included in Windows version because Windows most likely does not
21821 do the right thing if any third party tool offers
21822 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
21823 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
21824 {
21825 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
21826 if (f == dpyinfo->mouse_face_mouse_frame)
21827 {
21828 int x = dpyinfo->mouse_face_mouse_x;
21829 int y = dpyinfo->mouse_face_mouse_y;
21830 clear_mouse_face (dpyinfo);
21831 note_mouse_highlight (f, x, y);
21832 }
21833 }
21834 }
21835
21836
21837 /* EXPORT:
21838 Determine the intersection of two rectangles R1 and R2. Return
21839 the intersection in *RESULT. Value is non-zero if RESULT is not
21840 empty. */
21841
21842 int
21843 x_intersect_rectangles (r1, r2, result)
21844 XRectangle *r1, *r2, *result;
21845 {
21846 XRectangle *left, *right;
21847 XRectangle *upper, *lower;
21848 int intersection_p = 0;
21849
21850 /* Rearrange so that R1 is the left-most rectangle. */
21851 if (r1->x < r2->x)
21852 left = r1, right = r2;
21853 else
21854 left = r2, right = r1;
21855
21856 /* X0 of the intersection is right.x0, if this is inside R1,
21857 otherwise there is no intersection. */
21858 if (right->x <= left->x + left->width)
21859 {
21860 result->x = right->x;
21861
21862 /* The right end of the intersection is the minimum of the
21863 the right ends of left and right. */
21864 result->width = (min (left->x + left->width, right->x + right->width)
21865 - result->x);
21866
21867 /* Same game for Y. */
21868 if (r1->y < r2->y)
21869 upper = r1, lower = r2;
21870 else
21871 upper = r2, lower = r1;
21872
21873 /* The upper end of the intersection is lower.y0, if this is inside
21874 of upper. Otherwise, there is no intersection. */
21875 if (lower->y <= upper->y + upper->height)
21876 {
21877 result->y = lower->y;
21878
21879 /* The lower end of the intersection is the minimum of the lower
21880 ends of upper and lower. */
21881 result->height = (min (lower->y + lower->height,
21882 upper->y + upper->height)
21883 - result->y);
21884 intersection_p = 1;
21885 }
21886 }
21887
21888 return intersection_p;
21889 }
21890
21891 #endif /* HAVE_WINDOW_SYSTEM */
21892
21893 \f
21894 /***********************************************************************
21895 Initialization
21896 ***********************************************************************/
21897
21898 void
21899 syms_of_xdisp ()
21900 {
21901 Vwith_echo_area_save_vector = Qnil;
21902 staticpro (&Vwith_echo_area_save_vector);
21903
21904 Vmessage_stack = Qnil;
21905 staticpro (&Vmessage_stack);
21906
21907 Qinhibit_redisplay = intern ("inhibit-redisplay");
21908 staticpro (&Qinhibit_redisplay);
21909
21910 message_dolog_marker1 = Fmake_marker ();
21911 staticpro (&message_dolog_marker1);
21912 message_dolog_marker2 = Fmake_marker ();
21913 staticpro (&message_dolog_marker2);
21914 message_dolog_marker3 = Fmake_marker ();
21915 staticpro (&message_dolog_marker3);
21916
21917 #if GLYPH_DEBUG
21918 defsubr (&Sdump_frame_glyph_matrix);
21919 defsubr (&Sdump_glyph_matrix);
21920 defsubr (&Sdump_glyph_row);
21921 defsubr (&Sdump_tool_bar_row);
21922 defsubr (&Strace_redisplay);
21923 defsubr (&Strace_to_stderr);
21924 #endif
21925 #ifdef HAVE_WINDOW_SYSTEM
21926 defsubr (&Stool_bar_lines_needed);
21927 defsubr (&Slookup_image_map);
21928 #endif
21929 defsubr (&Sformat_mode_line);
21930
21931 staticpro (&Qmenu_bar_update_hook);
21932 Qmenu_bar_update_hook = intern ("menu-bar-update-hook");
21933
21934 staticpro (&Qoverriding_terminal_local_map);
21935 Qoverriding_terminal_local_map = intern ("overriding-terminal-local-map");
21936
21937 staticpro (&Qoverriding_local_map);
21938 Qoverriding_local_map = intern ("overriding-local-map");
21939
21940 staticpro (&Qwindow_scroll_functions);
21941 Qwindow_scroll_functions = intern ("window-scroll-functions");
21942
21943 staticpro (&Qredisplay_end_trigger_functions);
21944 Qredisplay_end_trigger_functions = intern ("redisplay-end-trigger-functions");
21945
21946 staticpro (&Qinhibit_point_motion_hooks);
21947 Qinhibit_point_motion_hooks = intern ("inhibit-point-motion-hooks");
21948
21949 QCdata = intern (":data");
21950 staticpro (&QCdata);
21951 Qdisplay = intern ("display");
21952 staticpro (&Qdisplay);
21953 Qspace_width = intern ("space-width");
21954 staticpro (&Qspace_width);
21955 Qraise = intern ("raise");
21956 staticpro (&Qraise);
21957 Qslice = intern ("slice");
21958 staticpro (&Qslice);
21959 Qspace = intern ("space");
21960 staticpro (&Qspace);
21961 Qmargin = intern ("margin");
21962 staticpro (&Qmargin);
21963 Qpointer = intern ("pointer");
21964 staticpro (&Qpointer);
21965 Qleft_margin = intern ("left-margin");
21966 staticpro (&Qleft_margin);
21967 Qright_margin = intern ("right-margin");
21968 staticpro (&Qright_margin);
21969 Qcenter = intern ("center");
21970 staticpro (&Qcenter);
21971 Qline_height = intern ("line-height");
21972 staticpro (&Qline_height);
21973 Qtotal = intern ("total");
21974 staticpro (&Qtotal);
21975 QCalign_to = intern (":align-to");
21976 staticpro (&QCalign_to);
21977 QCrelative_width = intern (":relative-width");
21978 staticpro (&QCrelative_width);
21979 QCrelative_height = intern (":relative-height");
21980 staticpro (&QCrelative_height);
21981 QCeval = intern (":eval");
21982 staticpro (&QCeval);
21983 QCpropertize = intern (":propertize");
21984 staticpro (&QCpropertize);
21985 QCfile = intern (":file");
21986 staticpro (&QCfile);
21987 Qfontified = intern ("fontified");
21988 staticpro (&Qfontified);
21989 Qfontification_functions = intern ("fontification-functions");
21990 staticpro (&Qfontification_functions);
21991 Qtrailing_whitespace = intern ("trailing-whitespace");
21992 staticpro (&Qtrailing_whitespace);
21993 Qimage = intern ("image");
21994 staticpro (&Qimage);
21995 QCmap = intern (":map");
21996 staticpro (&QCmap);
21997 QCpointer = intern (":pointer");
21998 staticpro (&QCpointer);
21999 Qrect = intern ("rect");
22000 staticpro (&Qrect);
22001 Qcircle = intern ("circle");
22002 staticpro (&Qcircle);
22003 Qpoly = intern ("poly");
22004 staticpro (&Qpoly);
22005 Qmessage_truncate_lines = intern ("message-truncate-lines");
22006 staticpro (&Qmessage_truncate_lines);
22007 Qcursor_in_non_selected_windows = intern ("cursor-in-non-selected-windows");
22008 staticpro (&Qcursor_in_non_selected_windows);
22009 Qgrow_only = intern ("grow-only");
22010 staticpro (&Qgrow_only);
22011 Qinhibit_menubar_update = intern ("inhibit-menubar-update");
22012 staticpro (&Qinhibit_menubar_update);
22013 Qinhibit_eval_during_redisplay = intern ("inhibit-eval-during-redisplay");
22014 staticpro (&Qinhibit_eval_during_redisplay);
22015 Qposition = intern ("position");
22016 staticpro (&Qposition);
22017 Qbuffer_position = intern ("buffer-position");
22018 staticpro (&Qbuffer_position);
22019 Qobject = intern ("object");
22020 staticpro (&Qobject);
22021 Qbar = intern ("bar");
22022 staticpro (&Qbar);
22023 Qhbar = intern ("hbar");
22024 staticpro (&Qhbar);
22025 Qbox = intern ("box");
22026 staticpro (&Qbox);
22027 Qhollow = intern ("hollow");
22028 staticpro (&Qhollow);
22029 Qhand = intern ("hand");
22030 staticpro (&Qhand);
22031 Qarrow = intern ("arrow");
22032 staticpro (&Qarrow);
22033 Qtext = intern ("text");
22034 staticpro (&Qtext);
22035 Qrisky_local_variable = intern ("risky-local-variable");
22036 staticpro (&Qrisky_local_variable);
22037 Qinhibit_free_realized_faces = intern ("inhibit-free-realized-faces");
22038 staticpro (&Qinhibit_free_realized_faces);
22039
22040 list_of_error = Fcons (Fcons (intern ("error"),
22041 Fcons (intern ("void-variable"), Qnil)),
22042 Qnil);
22043 staticpro (&list_of_error);
22044
22045 Qlast_arrow_position = intern ("last-arrow-position");
22046 staticpro (&Qlast_arrow_position);
22047 Qlast_arrow_string = intern ("last-arrow-string");
22048 staticpro (&Qlast_arrow_string);
22049
22050 Qoverlay_arrow_string = intern ("overlay-arrow-string");
22051 staticpro (&Qoverlay_arrow_string);
22052 Qoverlay_arrow_bitmap = intern ("overlay-arrow-bitmap");
22053 staticpro (&Qoverlay_arrow_bitmap);
22054
22055 echo_buffer[0] = echo_buffer[1] = Qnil;
22056 staticpro (&echo_buffer[0]);
22057 staticpro (&echo_buffer[1]);
22058
22059 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
22060 staticpro (&echo_area_buffer[0]);
22061 staticpro (&echo_area_buffer[1]);
22062
22063 Vmessages_buffer_name = build_string ("*Messages*");
22064 staticpro (&Vmessages_buffer_name);
22065
22066 mode_line_proptrans_alist = Qnil;
22067 staticpro (&mode_line_proptrans_alist);
22068
22069 mode_line_string_list = Qnil;
22070 staticpro (&mode_line_string_list);
22071
22072 help_echo_string = Qnil;
22073 staticpro (&help_echo_string);
22074 help_echo_object = Qnil;
22075 staticpro (&help_echo_object);
22076 help_echo_window = Qnil;
22077 staticpro (&help_echo_window);
22078 previous_help_echo_string = Qnil;
22079 staticpro (&previous_help_echo_string);
22080 help_echo_pos = -1;
22081
22082 #ifdef HAVE_WINDOW_SYSTEM
22083 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
22084 doc: /* *Non-nil means draw block cursor as wide as the glyph under it.
22085 For example, if a block cursor is over a tab, it will be drawn as
22086 wide as that tab on the display. */);
22087 x_stretch_cursor_p = 0;
22088 #endif
22089
22090 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
22091 doc: /* *Non-nil means highlight trailing whitespace.
22092 The face used for trailing whitespace is `trailing-whitespace'. */);
22093 Vshow_trailing_whitespace = Qnil;
22094
22095 DEFVAR_LISP ("void-text-area-pointer", &Vvoid_text_area_pointer,
22096 doc: /* *The pointer shape to show in void text areas.
22097 Nil means to show the text pointer. Other options are `arrow', `text',
22098 `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
22099 Vvoid_text_area_pointer = Qarrow;
22100
22101 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
22102 doc: /* Non-nil means don't actually do any redisplay.
22103 This is used for internal purposes. */);
22104 Vinhibit_redisplay = Qnil;
22105
22106 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
22107 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
22108 Vglobal_mode_string = Qnil;
22109
22110 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
22111 doc: /* Marker for where to display an arrow on top of the buffer text.
22112 This must be the beginning of a line in order to work.
22113 See also `overlay-arrow-string'. */);
22114 Voverlay_arrow_position = Qnil;
22115
22116 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
22117 doc: /* String to display as an arrow in non-window frames.
22118 See also `overlay-arrow-position'. */);
22119 Voverlay_arrow_string = Qnil;
22120
22121 DEFVAR_LISP ("overlay-arrow-variable-list", &Voverlay_arrow_variable_list,
22122 doc: /* List of variables (symbols) which hold markers for overlay arrows.
22123 The symbols on this list are examined during redisplay to determine
22124 where to display overlay arrows. */);
22125 Voverlay_arrow_variable_list
22126 = Fcons (intern ("overlay-arrow-position"), Qnil);
22127
22128 DEFVAR_INT ("scroll-step", &scroll_step,
22129 doc: /* *The number of lines to try scrolling a window by when point moves out.
22130 If that fails to bring point back on frame, point is centered instead.
22131 If this is zero, point is always centered after it moves off frame.
22132 If you want scrolling to always be a line at a time, you should set
22133 `scroll-conservatively' to a large value rather than set this to 1. */);
22134
22135 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
22136 doc: /* *Scroll up to this many lines, to bring point back on screen.
22137 A value of zero means to scroll the text to center point vertically
22138 in the window. */);
22139 scroll_conservatively = 0;
22140
22141 DEFVAR_INT ("scroll-margin", &scroll_margin,
22142 doc: /* *Number of lines of margin at the top and bottom of a window.
22143 Recenter the window whenever point gets within this many lines
22144 of the top or bottom of the window. */);
22145 scroll_margin = 0;
22146
22147 DEFVAR_LISP ("display-pixels-per-inch", &Vdisplay_pixels_per_inch,
22148 doc: /* Pixels per inch on current display.
22149 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
22150 Vdisplay_pixels_per_inch = make_float (72.0);
22151
22152 #if GLYPH_DEBUG
22153 DEFVAR_INT ("debug-end-pos", &debug_end_pos, doc: /* Don't ask. */);
22154 #endif
22155
22156 DEFVAR_BOOL ("truncate-partial-width-windows",
22157 &truncate_partial_width_windows,
22158 doc: /* *Non-nil means truncate lines in all windows less than full frame wide. */);
22159 truncate_partial_width_windows = 1;
22160
22161 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
22162 doc: /* nil means display the mode-line/header-line/menu-bar in the default face.
22163 Any other value means to use the appropriate face, `mode-line',
22164 `header-line', or `menu' respectively. */);
22165 mode_line_inverse_video = 1;
22166
22167 DEFVAR_LISP ("line-number-display-limit", &Vline_number_display_limit,
22168 doc: /* *Maximum buffer size for which line number should be displayed.
22169 If the buffer is bigger than this, the line number does not appear
22170 in the mode line. A value of nil means no limit. */);
22171 Vline_number_display_limit = Qnil;
22172
22173 DEFVAR_INT ("line-number-display-limit-width",
22174 &line_number_display_limit_width,
22175 doc: /* *Maximum line width (in characters) for line number display.
22176 If the average length of the lines near point is bigger than this, then the
22177 line number may be omitted from the mode line. */);
22178 line_number_display_limit_width = 200;
22179
22180 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
22181 doc: /* *Non-nil means highlight region even in nonselected windows. */);
22182 highlight_nonselected_windows = 0;
22183
22184 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
22185 doc: /* Non-nil if more than one frame is visible on this display.
22186 Minibuffer-only frames don't count, but iconified frames do.
22187 This variable is not guaranteed to be accurate except while processing
22188 `frame-title-format' and `icon-title-format'. */);
22189
22190 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
22191 doc: /* Template for displaying the title bar of visible frames.
22192 \(Assuming the window manager supports this feature.)
22193 This variable has the same structure as `mode-line-format' (which see),
22194 and is used only on frames for which no explicit name has been set
22195 \(see `modify-frame-parameters'). */);
22196
22197 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
22198 doc: /* Template for displaying the title bar of an iconified frame.
22199 \(Assuming the window manager supports this feature.)
22200 This variable has the same structure as `mode-line-format' (which see),
22201 and is used only on frames for which no explicit name has been set
22202 \(see `modify-frame-parameters'). */);
22203 Vicon_title_format
22204 = Vframe_title_format
22205 = Fcons (intern ("multiple-frames"),
22206 Fcons (build_string ("%b"),
22207 Fcons (Fcons (empty_string,
22208 Fcons (intern ("invocation-name"),
22209 Fcons (build_string ("@"),
22210 Fcons (intern ("system-name"),
22211 Qnil)))),
22212 Qnil)));
22213
22214 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
22215 doc: /* Maximum number of lines to keep in the message log buffer.
22216 If nil, disable message logging. If t, log messages but don't truncate
22217 the buffer when it becomes large. */);
22218 Vmessage_log_max = make_number (50);
22219
22220 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
22221 doc: /* Functions called before redisplay, if window sizes have changed.
22222 The value should be a list of functions that take one argument.
22223 Just before redisplay, for each frame, if any of its windows have changed
22224 size since the last redisplay, or have been split or deleted,
22225 all the functions in the list are called, with the frame as argument. */);
22226 Vwindow_size_change_functions = Qnil;
22227
22228 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
22229 doc: /* List of functions to call before redisplaying a window with scrolling.
22230 Each function is called with two arguments, the window
22231 and its new display-start position. Note that the value of `window-end'
22232 is not valid when these functions are called. */);
22233 Vwindow_scroll_functions = Qnil;
22234
22235 DEFVAR_BOOL ("mouse-autoselect-window", &mouse_autoselect_window,
22236 doc: /* *Non-nil means autoselect window with mouse pointer. */);
22237 mouse_autoselect_window = 0;
22238
22239 DEFVAR_BOOL ("auto-resize-tool-bars", &auto_resize_tool_bars_p,
22240 doc: /* *Non-nil means automatically resize tool-bars.
22241 This increases a tool-bar's height if not all tool-bar items are visible.
22242 It decreases a tool-bar's height when it would display blank lines
22243 otherwise. */);
22244 auto_resize_tool_bars_p = 1;
22245
22246 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
22247 doc: /* *Non-nil means raise tool-bar buttons when the mouse moves over them. */);
22248 auto_raise_tool_bar_buttons_p = 1;
22249
22250 DEFVAR_LISP ("tool-bar-button-margin", &Vtool_bar_button_margin,
22251 doc: /* *Margin around tool-bar buttons in pixels.
22252 If an integer, use that for both horizontal and vertical margins.
22253 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
22254 HORZ specifying the horizontal margin, and VERT specifying the
22255 vertical margin. */);
22256 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
22257
22258 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
22259 doc: /* *Relief thickness of tool-bar buttons. */);
22260 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
22261
22262 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
22263 doc: /* List of functions to call to fontify regions of text.
22264 Each function is called with one argument POS. Functions must
22265 fontify a region starting at POS in the current buffer, and give
22266 fontified regions the property `fontified'. */);
22267 Vfontification_functions = Qnil;
22268 Fmake_variable_buffer_local (Qfontification_functions);
22269
22270 DEFVAR_BOOL ("unibyte-display-via-language-environment",
22271 &unibyte_display_via_language_environment,
22272 doc: /* *Non-nil means display unibyte text according to language environment.
22273 Specifically this means that unibyte non-ASCII characters
22274 are displayed by converting them to the equivalent multibyte characters
22275 according to the current language environment. As a result, they are
22276 displayed according to the current fontset. */);
22277 unibyte_display_via_language_environment = 0;
22278
22279 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
22280 doc: /* *Maximum height for resizing mini-windows.
22281 If a float, it specifies a fraction of the mini-window frame's height.
22282 If an integer, it specifies a number of lines. */);
22283 Vmax_mini_window_height = make_float (0.25);
22284
22285 DEFVAR_LISP ("resize-mini-windows", &Vresize_mini_windows,
22286 doc: /* *How to resize mini-windows.
22287 A value of nil means don't automatically resize mini-windows.
22288 A value of t means resize them to fit the text displayed in them.
22289 A value of `grow-only', the default, means let mini-windows grow
22290 only, until their display becomes empty, at which point the windows
22291 go back to their normal size. */);
22292 Vresize_mini_windows = Qgrow_only;
22293
22294 DEFVAR_LISP ("cursor-in-non-selected-windows",
22295 &Vcursor_in_non_selected_windows,
22296 doc: /* *Cursor type to display in non-selected windows.
22297 t means to use hollow box cursor. See `cursor-type' for other values. */);
22298 Vcursor_in_non_selected_windows = Qt;
22299
22300 DEFVAR_LISP ("blink-cursor-alist", &Vblink_cursor_alist,
22301 doc: /* Alist specifying how to blink the cursor off.
22302 Each element has the form (ON-STATE . OFF-STATE). Whenever the
22303 `cursor-type' frame-parameter or variable equals ON-STATE,
22304 comparing using `equal', Emacs uses OFF-STATE to specify
22305 how to blink it off. */);
22306 Vblink_cursor_alist = Qnil;
22307
22308 DEFVAR_BOOL ("auto-hscroll-mode", &automatic_hscrolling_p,
22309 doc: /* *Non-nil means scroll the display automatically to make point visible. */);
22310 automatic_hscrolling_p = 1;
22311
22312 DEFVAR_INT ("hscroll-margin", &hscroll_margin,
22313 doc: /* *How many columns away from the window edge point is allowed to get
22314 before automatic hscrolling will horizontally scroll the window. */);
22315 hscroll_margin = 5;
22316
22317 DEFVAR_LISP ("hscroll-step", &Vhscroll_step,
22318 doc: /* *How many columns to scroll the window when point gets too close to the edge.
22319 When point is less than `automatic-hscroll-margin' columns from the window
22320 edge, automatic hscrolling will scroll the window by the amount of columns
22321 determined by this variable. If its value is a positive integer, scroll that
22322 many columns. If it's a positive floating-point number, it specifies the
22323 fraction of the window's width to scroll. If it's nil or zero, point will be
22324 centered horizontally after the scroll. Any other value, including negative
22325 numbers, are treated as if the value were zero.
22326
22327 Automatic hscrolling always moves point outside the scroll margin, so if
22328 point was more than scroll step columns inside the margin, the window will
22329 scroll more than the value given by the scroll step.
22330
22331 Note that the lower bound for automatic hscrolling specified by `scroll-left'
22332 and `scroll-right' overrides this variable's effect. */);
22333 Vhscroll_step = make_number (0);
22334
22335 DEFVAR_BOOL ("message-truncate-lines", &message_truncate_lines,
22336 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
22337 Bind this around calls to `message' to let it take effect. */);
22338 message_truncate_lines = 0;
22339
22340 DEFVAR_LISP ("menu-bar-update-hook", &Vmenu_bar_update_hook,
22341 doc: /* Normal hook run for clicks on menu bar, before displaying a submenu.
22342 Can be used to update submenus whose contents should vary. */);
22343 Vmenu_bar_update_hook = Qnil;
22344
22345 DEFVAR_BOOL ("inhibit-menubar-update", &inhibit_menubar_update,
22346 doc: /* Non-nil means don't update menu bars. Internal use only. */);
22347 inhibit_menubar_update = 0;
22348
22349 DEFVAR_BOOL ("inhibit-eval-during-redisplay", &inhibit_eval_during_redisplay,
22350 doc: /* Non-nil means don't eval Lisp during redisplay. */);
22351 inhibit_eval_during_redisplay = 0;
22352
22353 DEFVAR_BOOL ("inhibit-free-realized-faces", &inhibit_free_realized_faces,
22354 doc: /* Non-nil means don't free realized faces. Internal use only. */);
22355 inhibit_free_realized_faces = 0;
22356
22357 #if GLYPH_DEBUG
22358 DEFVAR_BOOL ("inhibit-try-window-id", &inhibit_try_window_id,
22359 doc: /* Inhibit try_window_id display optimization. */);
22360 inhibit_try_window_id = 0;
22361
22362 DEFVAR_BOOL ("inhibit-try-window-reusing", &inhibit_try_window_reusing,
22363 doc: /* Inhibit try_window_reusing display optimization. */);
22364 inhibit_try_window_reusing = 0;
22365
22366 DEFVAR_BOOL ("inhibit-try-cursor-movement", &inhibit_try_cursor_movement,
22367 doc: /* Inhibit try_cursor_movement display optimization. */);
22368 inhibit_try_cursor_movement = 0;
22369 #endif /* GLYPH_DEBUG */
22370 }
22371
22372
22373 /* Initialize this module when Emacs starts. */
22374
22375 void
22376 init_xdisp ()
22377 {
22378 Lisp_Object root_window;
22379 struct window *mini_w;
22380
22381 current_header_line_height = current_mode_line_height = -1;
22382
22383 CHARPOS (this_line_start_pos) = 0;
22384
22385 mini_w = XWINDOW (minibuf_window);
22386 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
22387
22388 if (!noninteractive)
22389 {
22390 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
22391 int i;
22392
22393 XWINDOW (root_window)->top_line = make_number (FRAME_TOP_MARGIN (f));
22394 set_window_height (root_window,
22395 FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f),
22396 0);
22397 mini_w->top_line = make_number (FRAME_LINES (f) - 1);
22398 set_window_height (minibuf_window, 1, 0);
22399
22400 XWINDOW (root_window)->total_cols = make_number (FRAME_COLS (f));
22401 mini_w->total_cols = make_number (FRAME_COLS (f));
22402
22403 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
22404 scratch_glyph_row.glyphs[TEXT_AREA + 1]
22405 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
22406
22407 /* The default ellipsis glyphs `...'. */
22408 for (i = 0; i < 3; ++i)
22409 default_invis_vector[i] = make_number ('.');
22410 }
22411
22412 {
22413 /* Allocate the buffer for frame titles.
22414 Also used for `format-mode-line'. */
22415 int size = 100;
22416 frame_title_buf = (char *) xmalloc (size);
22417 frame_title_buf_end = frame_title_buf + size;
22418 frame_title_ptr = NULL;
22419 }
22420
22421 help_echo_showing_p = 0;
22422 }
22423
22424
22425 /* arch-tag: eacc864d-bb6a-4b74-894a-1a4399a1358b
22426 (do not change this comment) */