Merged in changes from CVS trunk.
[bpt/emacs.git] / src / xdisp.c
1 /* Display generation from window structure and buffer text.
2 Copyright (C) 1985,86,87,88,93,94,95,97,98,99,2000,01,02,03,04
3 Free Software Foundation, Inc.
4
5 This file is part of GNU Emacs.
6
7 GNU Emacs is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
21
22 /* New redisplay written by Gerd Moellmann <gerd@gnu.org>.
23
24 Redisplay.
25
26 Emacs separates the task of updating the display from code
27 modifying global state, e.g. buffer text. This way functions
28 operating on buffers don't also have to be concerned with updating
29 the display.
30
31 Updating the display is triggered by the Lisp interpreter when it
32 decides it's time to do it. This is done either automatically for
33 you as part of the interpreter's command loop or as the result of
34 calling Lisp functions like `sit-for'. The C function `redisplay'
35 in xdisp.c is the only entry into the inner redisplay code. (Or,
36 let's say almost---see the description of direct update
37 operations, below.)
38
39 The following diagram shows how redisplay code is invoked. As you
40 can see, Lisp calls redisplay and vice versa. Under window systems
41 like X, some portions of the redisplay code are also called
42 asynchronously during mouse movement or expose events. It is very
43 important that these code parts do NOT use the C library (malloc,
44 free) because many C libraries under Unix are not reentrant. They
45 may also NOT call functions of the Lisp interpreter which could
46 change the interpreter's state. If you don't follow these rules,
47 you will encounter bugs which are very hard to explain.
48
49 (Direct functions, see below)
50 direct_output_for_insert,
51 direct_forward_char (dispnew.c)
52 +---------------------------------+
53 | |
54 | V
55 +--------------+ redisplay +----------------+
56 | Lisp machine |---------------->| Redisplay code |<--+
57 +--------------+ (xdisp.c) +----------------+ |
58 ^ | |
59 +----------------------------------+ |
60 Don't use this path when called |
61 asynchronously! |
62 |
63 expose_window (asynchronous) |
64 |
65 X expose events -----+
66
67 What does redisplay do? Obviously, it has to figure out somehow what
68 has been changed since the last time the display has been updated,
69 and to make these changes visible. Preferably it would do that in
70 a moderately intelligent way, i.e. fast.
71
72 Changes in buffer text can be deduced from window and buffer
73 structures, and from some global variables like `beg_unchanged' and
74 `end_unchanged'. The contents of the display are additionally
75 recorded in a `glyph matrix', a two-dimensional matrix of glyph
76 structures. Each row in such a matrix corresponds to a line on the
77 display, and each glyph in a row corresponds to a column displaying
78 a character, an image, or what else. This matrix is called the
79 `current glyph matrix' or `current matrix' in redisplay
80 terminology.
81
82 For buffer parts that have been changed since the last update, a
83 second glyph matrix is constructed, the so called `desired glyph
84 matrix' or short `desired matrix'. Current and desired matrix are
85 then compared to find a cheap way to update the display, e.g. by
86 reusing part of the display by scrolling lines.
87
88
89 Direct operations.
90
91 You will find a lot of redisplay optimizations when you start
92 looking at the innards of redisplay. The overall goal of all these
93 optimizations is to make redisplay fast because it is done
94 frequently.
95
96 Two optimizations are not found in xdisp.c. These are the direct
97 operations mentioned above. As the name suggests they follow a
98 different principle than the rest of redisplay. Instead of
99 building a desired matrix and then comparing it with the current
100 display, they perform their actions directly on the display and on
101 the current matrix.
102
103 One direct operation updates the display after one character has
104 been entered. The other one moves the cursor by one position
105 forward or backward. You find these functions under the names
106 `direct_output_for_insert' and `direct_output_forward_char' in
107 dispnew.c.
108
109
110 Desired matrices.
111
112 Desired matrices are always built per Emacs window. The function
113 `display_line' is the central function to look at if you are
114 interested. It constructs one row in a desired matrix given an
115 iterator structure containing both a buffer position and a
116 description of the environment in which the text is to be
117 displayed. But this is too early, read on.
118
119 Characters and pixmaps displayed for a range of buffer text depend
120 on various settings of buffers and windows, on overlays and text
121 properties, on display tables, on selective display. The good news
122 is that all this hairy stuff is hidden behind a small set of
123 interface functions taking an iterator structure (struct it)
124 argument.
125
126 Iteration over things to be displayed is then simple. It is
127 started by initializing an iterator with a call to init_iterator.
128 Calls to get_next_display_element fill the iterator structure with
129 relevant information about the next thing to display. Calls to
130 set_iterator_to_next move the iterator to the next thing.
131
132 Besides this, an iterator also contains information about the
133 display environment in which glyphs for display elements are to be
134 produced. It has fields for the width and height of the display,
135 the information whether long lines are truncated or continued, a
136 current X and Y position, and lots of other stuff you can better
137 see in dispextern.h.
138
139 Glyphs in a desired matrix are normally constructed in a loop
140 calling get_next_display_element and then produce_glyphs. The call
141 to produce_glyphs will fill the iterator structure with pixel
142 information about the element being displayed and at the same time
143 produce glyphs for it. If the display element fits on the line
144 being displayed, set_iterator_to_next is called next, otherwise the
145 glyphs produced are discarded.
146
147
148 Frame matrices.
149
150 That just couldn't be all, could it? What about terminal types not
151 supporting operations on sub-windows of the screen? To update the
152 display on such a terminal, window-based glyph matrices are not
153 well suited. To be able to reuse part of the display (scrolling
154 lines up and down), we must instead have a view of the whole
155 screen. This is what `frame matrices' are for. They are a trick.
156
157 Frames on terminals like above have a glyph pool. Windows on such
158 a frame sub-allocate their glyph memory from their frame's glyph
159 pool. The frame itself is given its own glyph matrices. By
160 coincidence---or maybe something else---rows in window glyph
161 matrices are slices of corresponding rows in frame matrices. Thus
162 writing to window matrices implicitly updates a frame matrix which
163 provides us with the view of the whole screen that we originally
164 wanted to have without having to move many bytes around. To be
165 honest, there is a little bit more done, but not much more. If you
166 plan to extend that code, take a look at dispnew.c. The function
167 build_frame_matrix is a good starting point. */
168
169 #include <config.h>
170 #include <stdio.h>
171
172 #include "lisp.h"
173 #include "keyboard.h"
174 #include "frame.h"
175 #include "window.h"
176 #include "termchar.h"
177 #include "dispextern.h"
178 #include "buffer.h"
179 #include "charset.h"
180 #include "indent.h"
181 #include "commands.h"
182 #include "keymap.h"
183 #include "macros.h"
184 #include "disptab.h"
185 #include "termhooks.h"
186 #include "intervals.h"
187 #include "coding.h"
188 #include "process.h"
189 #include "region-cache.h"
190 #include "fontset.h"
191 #include "blockinput.h"
192
193 #ifdef HAVE_X_WINDOWS
194 #include "xterm.h"
195 #endif
196 #ifdef WINDOWSNT
197 #include "w32term.h"
198 #endif
199 #ifdef MAC_OS
200 #include "macterm.h"
201 #endif
202
203 #ifndef FRAME_X_OUTPUT
204 #define FRAME_X_OUTPUT(f) ((f)->output_data.x)
205 #endif
206
207 #define INFINITY 10000000
208
209 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
210 || defined (USE_GTK)
211 extern void set_frame_menubar P_ ((struct frame *f, int, int));
212 extern int pending_menu_activation;
213 #endif
214
215 extern int interrupt_input;
216 extern int command_loop_level;
217
218 extern Lisp_Object do_mouse_tracking;
219
220 extern int minibuffer_auto_raise;
221 extern Lisp_Object Vminibuffer_list;
222
223 extern Lisp_Object Qface;
224 extern Lisp_Object Qmode_line, Qmode_line_inactive, Qheader_line;
225
226 extern Lisp_Object Voverriding_local_map;
227 extern Lisp_Object Voverriding_local_map_menu_flag;
228 extern Lisp_Object Qmenu_item;
229 extern Lisp_Object Qwhen;
230 extern Lisp_Object Qhelp_echo;
231
232 Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
233 Lisp_Object Qwindow_scroll_functions, Vwindow_scroll_functions;
234 Lisp_Object Qredisplay_end_trigger_functions;
235 Lisp_Object Qinhibit_point_motion_hooks;
236 Lisp_Object QCeval, QCfile, QCdata, QCpropertize;
237 Lisp_Object Qfontified;
238 Lisp_Object Qgrow_only;
239 Lisp_Object Qinhibit_eval_during_redisplay;
240 Lisp_Object Qbuffer_position, Qposition, Qobject;
241
242 /* Cursor shapes */
243 Lisp_Object Qbar, Qhbar, Qbox, Qhollow;
244
245 /* Pointer shapes */
246 Lisp_Object Qarrow, Qhand, Qtext;
247
248 Lisp_Object Qrisky_local_variable;
249
250 /* Holds the list (error). */
251 Lisp_Object list_of_error;
252
253 /* Functions called to fontify regions of text. */
254
255 Lisp_Object Vfontification_functions;
256 Lisp_Object Qfontification_functions;
257
258 /* Non-zero means automatically select any window when the mouse
259 cursor moves into it. */
260 int mouse_autoselect_window;
261
262 /* Non-zero means draw tool bar buttons raised when the mouse moves
263 over them. */
264
265 int auto_raise_tool_bar_buttons_p;
266
267 /* Non-zero means to reposition window if cursor line is only partially visible. */
268
269 int make_cursor_line_fully_visible_p;
270
271 /* Margin around tool bar buttons in pixels. */
272
273 Lisp_Object Vtool_bar_button_margin;
274
275 /* Thickness of shadow to draw around tool bar buttons. */
276
277 EMACS_INT tool_bar_button_relief;
278
279 /* Non-zero means automatically resize tool-bars so that all tool-bar
280 items are visible, and no blank lines remain. */
281
282 int auto_resize_tool_bars_p;
283
284 /* Non-zero means draw block and hollow cursor as wide as the glyph
285 under it. For example, if a block cursor is over a tab, it will be
286 drawn as wide as that tab on the display. */
287
288 int x_stretch_cursor_p;
289
290 /* Non-nil means don't actually do any redisplay. */
291
292 Lisp_Object Vinhibit_redisplay, Qinhibit_redisplay;
293
294 /* Non-zero means Lisp evaluation during redisplay is inhibited. */
295
296 int inhibit_eval_during_redisplay;
297
298 /* Names of text properties relevant for redisplay. */
299
300 Lisp_Object Qdisplay;
301 extern Lisp_Object Qface, Qinvisible, Qwidth;
302
303 /* Symbols used in text property values. */
304
305 Lisp_Object Vdisplay_pixels_per_inch;
306 Lisp_Object Qspace, QCalign_to, QCrelative_width, QCrelative_height;
307 Lisp_Object Qleft_margin, Qright_margin, Qspace_width, Qraise;
308 Lisp_Object Qslice;
309 Lisp_Object Qcenter;
310 Lisp_Object Qmargin, Qpointer;
311 Lisp_Object Qline_height, Qtotal;
312 extern Lisp_Object Qheight;
313 extern Lisp_Object QCwidth, QCheight, QCascent;
314 extern Lisp_Object Qscroll_bar;
315 extern Lisp_Object Qcursor;
316
317 /* Non-nil means highlight trailing whitespace. */
318
319 Lisp_Object Vshow_trailing_whitespace;
320
321 #ifdef HAVE_WINDOW_SYSTEM
322 extern Lisp_Object Voverflow_newline_into_fringe;
323
324 /* Test if overflow newline into fringe. Called with iterator IT
325 at or past right window margin, and with IT->current_x set. */
326
327 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(it) \
328 (!NILP (Voverflow_newline_into_fringe) \
329 && FRAME_WINDOW_P (it->f) \
330 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) > 0 \
331 && it->current_x == it->last_visible_x)
332
333 #endif /* HAVE_WINDOW_SYSTEM */
334
335 /* Non-nil means show the text cursor in void text areas
336 i.e. in blank areas after eol and eob. This used to be
337 the default in 21.3. */
338
339 Lisp_Object Vvoid_text_area_pointer;
340
341 /* Name of the face used to highlight trailing whitespace. */
342
343 Lisp_Object Qtrailing_whitespace;
344
345 /* Name and number of the face used to highlight escape glyphs. */
346
347 Lisp_Object Qescape_glyph;
348 int escape_glyph_face;
349
350 /* The symbol `image' which is the car of the lists used to represent
351 images in Lisp. */
352
353 Lisp_Object Qimage;
354
355 /* The image map types. */
356 Lisp_Object QCmap, QCpointer;
357 Lisp_Object Qrect, Qcircle, Qpoly;
358
359 /* Non-zero means print newline to stdout before next mini-buffer
360 message. */
361
362 int noninteractive_need_newline;
363
364 /* Non-zero means print newline to message log before next message. */
365
366 static int message_log_need_newline;
367
368 /* Three markers that message_dolog uses.
369 It could allocate them itself, but that causes trouble
370 in handling memory-full errors. */
371 static Lisp_Object message_dolog_marker1;
372 static Lisp_Object message_dolog_marker2;
373 static Lisp_Object message_dolog_marker3;
374 \f
375 /* The buffer position of the first character appearing entirely or
376 partially on the line of the selected window which contains the
377 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
378 redisplay optimization in redisplay_internal. */
379
380 static struct text_pos this_line_start_pos;
381
382 /* Number of characters past the end of the line above, including the
383 terminating newline. */
384
385 static struct text_pos this_line_end_pos;
386
387 /* The vertical positions and the height of this line. */
388
389 static int this_line_vpos;
390 static int this_line_y;
391 static int this_line_pixel_height;
392
393 /* X position at which this display line starts. Usually zero;
394 negative if first character is partially visible. */
395
396 static int this_line_start_x;
397
398 /* Buffer that this_line_.* variables are referring to. */
399
400 static struct buffer *this_line_buffer;
401
402 /* Nonzero means truncate lines in all windows less wide than the
403 frame. */
404
405 int truncate_partial_width_windows;
406
407 /* A flag to control how to display unibyte 8-bit character. */
408
409 int unibyte_display_via_language_environment;
410
411 /* Nonzero means we have more than one non-mini-buffer-only frame.
412 Not guaranteed to be accurate except while parsing
413 frame-title-format. */
414
415 int multiple_frames;
416
417 Lisp_Object Vglobal_mode_string;
418
419
420 /* List of variables (symbols) which hold markers for overlay arrows.
421 The symbols on this list are examined during redisplay to determine
422 where to display overlay arrows. */
423
424 Lisp_Object Voverlay_arrow_variable_list;
425
426 /* Marker for where to display an arrow on top of the buffer text. */
427
428 Lisp_Object Voverlay_arrow_position;
429
430 /* String to display for the arrow. Only used on terminal frames. */
431
432 Lisp_Object Voverlay_arrow_string;
433
434 /* Values of those variables at last redisplay are stored as
435 properties on `overlay-arrow-position' symbol. However, if
436 Voverlay_arrow_position is a marker, last-arrow-position is its
437 numerical position. */
438
439 Lisp_Object Qlast_arrow_position, Qlast_arrow_string;
440
441 /* Alternative overlay-arrow-string and overlay-arrow-bitmap
442 properties on a symbol in overlay-arrow-variable-list. */
443
444 Lisp_Object Qoverlay_arrow_string, Qoverlay_arrow_bitmap;
445
446 /* Like mode-line-format, but for the title bar on a visible frame. */
447
448 Lisp_Object Vframe_title_format;
449
450 /* Like mode-line-format, but for the title bar on an iconified frame. */
451
452 Lisp_Object Vicon_title_format;
453
454 /* List of functions to call when a window's size changes. These
455 functions get one arg, a frame on which one or more windows' sizes
456 have changed. */
457
458 static Lisp_Object Vwindow_size_change_functions;
459
460 Lisp_Object Qmenu_bar_update_hook, Vmenu_bar_update_hook;
461
462 /* Nonzero if overlay arrow has been displayed once in this window. */
463
464 static int overlay_arrow_seen;
465
466 /* Nonzero means highlight the region even in nonselected windows. */
467
468 int highlight_nonselected_windows;
469
470 /* If cursor motion alone moves point off frame, try scrolling this
471 many lines up or down if that will bring it back. */
472
473 static EMACS_INT scroll_step;
474
475 /* Nonzero means scroll just far enough to bring point back on the
476 screen, when appropriate. */
477
478 static EMACS_INT scroll_conservatively;
479
480 /* Recenter the window whenever point gets within this many lines of
481 the top or bottom of the window. This value is translated into a
482 pixel value by multiplying it with FRAME_LINE_HEIGHT, which means
483 that there is really a fixed pixel height scroll margin. */
484
485 EMACS_INT scroll_margin;
486
487 /* Number of windows showing the buffer of the selected window (or
488 another buffer with the same base buffer). keyboard.c refers to
489 this. */
490
491 int buffer_shared;
492
493 /* Vector containing glyphs for an ellipsis `...'. */
494
495 static Lisp_Object default_invis_vector[3];
496
497 /* Zero means display the mode-line/header-line/menu-bar in the default face
498 (this slightly odd definition is for compatibility with previous versions
499 of emacs), non-zero means display them using their respective faces.
500
501 This variable is deprecated. */
502
503 int mode_line_inverse_video;
504
505 /* Prompt to display in front of the mini-buffer contents. */
506
507 Lisp_Object minibuf_prompt;
508
509 /* Width of current mini-buffer prompt. Only set after display_line
510 of the line that contains the prompt. */
511
512 int minibuf_prompt_width;
513
514 /* This is the window where the echo area message was displayed. It
515 is always a mini-buffer window, but it may not be the same window
516 currently active as a mini-buffer. */
517
518 Lisp_Object echo_area_window;
519
520 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
521 pushes the current message and the value of
522 message_enable_multibyte on the stack, the function restore_message
523 pops the stack and displays MESSAGE again. */
524
525 Lisp_Object Vmessage_stack;
526
527 /* Nonzero means multibyte characters were enabled when the echo area
528 message was specified. */
529
530 int message_enable_multibyte;
531
532 /* Nonzero if we should redraw the mode lines on the next redisplay. */
533
534 int update_mode_lines;
535
536 /* Nonzero if window sizes or contents have changed since last
537 redisplay that finished. */
538
539 int windows_or_buffers_changed;
540
541 /* Nonzero means a frame's cursor type has been changed. */
542
543 int cursor_type_changed;
544
545 /* Nonzero after display_mode_line if %l was used and it displayed a
546 line number. */
547
548 int line_number_displayed;
549
550 /* Maximum buffer size for which to display line numbers. */
551
552 Lisp_Object Vline_number_display_limit;
553
554 /* Line width to consider when repositioning for line number display. */
555
556 static EMACS_INT line_number_display_limit_width;
557
558 /* Number of lines to keep in the message log buffer. t means
559 infinite. nil means don't log at all. */
560
561 Lisp_Object Vmessage_log_max;
562
563 /* The name of the *Messages* buffer, a string. */
564
565 static Lisp_Object Vmessages_buffer_name;
566
567 /* Current, index 0, and last displayed echo area message. Either
568 buffers from echo_buffers, or nil to indicate no message. */
569
570 Lisp_Object echo_area_buffer[2];
571
572 /* The buffers referenced from echo_area_buffer. */
573
574 static Lisp_Object echo_buffer[2];
575
576 /* A vector saved used in with_area_buffer to reduce consing. */
577
578 static Lisp_Object Vwith_echo_area_save_vector;
579
580 /* Non-zero means display_echo_area should display the last echo area
581 message again. Set by redisplay_preserve_echo_area. */
582
583 static int display_last_displayed_message_p;
584
585 /* Nonzero if echo area is being used by print; zero if being used by
586 message. */
587
588 int message_buf_print;
589
590 /* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
591
592 Lisp_Object Qinhibit_menubar_update;
593 int inhibit_menubar_update;
594
595 /* Maximum height for resizing mini-windows. Either a float
596 specifying a fraction of the available height, or an integer
597 specifying a number of lines. */
598
599 Lisp_Object Vmax_mini_window_height;
600
601 /* Non-zero means messages should be displayed with truncated
602 lines instead of being continued. */
603
604 int message_truncate_lines;
605 Lisp_Object Qmessage_truncate_lines;
606
607 /* Set to 1 in clear_message to make redisplay_internal aware
608 of an emptied echo area. */
609
610 static int message_cleared_p;
611
612 /* Non-zero means we want a hollow cursor in windows that are not
613 selected. Zero means there's no cursor in such windows. */
614
615 Lisp_Object Vcursor_in_non_selected_windows;
616 Lisp_Object Qcursor_in_non_selected_windows;
617
618 /* How to blink the default frame cursor off. */
619 Lisp_Object Vblink_cursor_alist;
620
621 /* A scratch glyph row with contents used for generating truncation
622 glyphs. Also used in direct_output_for_insert. */
623
624 #define MAX_SCRATCH_GLYPHS 100
625 struct glyph_row scratch_glyph_row;
626 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
627
628 /* Ascent and height of the last line processed by move_it_to. */
629
630 static int last_max_ascent, last_height;
631
632 /* Non-zero if there's a help-echo in the echo area. */
633
634 int help_echo_showing_p;
635
636 /* If >= 0, computed, exact values of mode-line and header-line height
637 to use in the macros CURRENT_MODE_LINE_HEIGHT and
638 CURRENT_HEADER_LINE_HEIGHT. */
639
640 int current_mode_line_height, current_header_line_height;
641
642 /* The maximum distance to look ahead for text properties. Values
643 that are too small let us call compute_char_face and similar
644 functions too often which is expensive. Values that are too large
645 let us call compute_char_face and alike too often because we
646 might not be interested in text properties that far away. */
647
648 #define TEXT_PROP_DISTANCE_LIMIT 100
649
650 #if GLYPH_DEBUG
651
652 /* Variables to turn off display optimizations from Lisp. */
653
654 int inhibit_try_window_id, inhibit_try_window_reusing;
655 int inhibit_try_cursor_movement;
656
657 /* Non-zero means print traces of redisplay if compiled with
658 GLYPH_DEBUG != 0. */
659
660 int trace_redisplay_p;
661
662 #endif /* GLYPH_DEBUG */
663
664 #ifdef DEBUG_TRACE_MOVE
665 /* Non-zero means trace with TRACE_MOVE to stderr. */
666 int trace_move;
667
668 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
669 #else
670 #define TRACE_MOVE(x) (void) 0
671 #endif
672
673 /* Non-zero means automatically scroll windows horizontally to make
674 point visible. */
675
676 int automatic_hscrolling_p;
677
678 /* How close to the margin can point get before the window is scrolled
679 horizontally. */
680 EMACS_INT hscroll_margin;
681
682 /* How much to scroll horizontally when point is inside the above margin. */
683 Lisp_Object Vhscroll_step;
684
685 /* The variable `resize-mini-windows'. If nil, don't resize
686 mini-windows. If t, always resize them to fit the text they
687 display. If `grow-only', let mini-windows grow only until they
688 become empty. */
689
690 Lisp_Object Vresize_mini_windows;
691
692 /* Buffer being redisplayed -- for redisplay_window_error. */
693
694 struct buffer *displayed_buffer;
695
696 /* Value returned from text property handlers (see below). */
697
698 enum prop_handled
699 {
700 HANDLED_NORMALLY,
701 HANDLED_RECOMPUTE_PROPS,
702 HANDLED_OVERLAY_STRING_CONSUMED,
703 HANDLED_RETURN
704 };
705
706 /* A description of text properties that redisplay is interested
707 in. */
708
709 struct props
710 {
711 /* The name of the property. */
712 Lisp_Object *name;
713
714 /* A unique index for the property. */
715 enum prop_idx idx;
716
717 /* A handler function called to set up iterator IT from the property
718 at IT's current position. Value is used to steer handle_stop. */
719 enum prop_handled (*handler) P_ ((struct it *it));
720 };
721
722 static enum prop_handled handle_face_prop P_ ((struct it *));
723 static enum prop_handled handle_invisible_prop P_ ((struct it *));
724 static enum prop_handled handle_display_prop P_ ((struct it *));
725 static enum prop_handled handle_composition_prop P_ ((struct it *));
726 static enum prop_handled handle_overlay_change P_ ((struct it *));
727 static enum prop_handled handle_fontified_prop P_ ((struct it *));
728
729 /* Properties handled by iterators. */
730
731 static struct props it_props[] =
732 {
733 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
734 /* Handle `face' before `display' because some sub-properties of
735 `display' need to know the face. */
736 {&Qface, FACE_PROP_IDX, handle_face_prop},
737 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
738 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
739 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
740 {NULL, 0, NULL}
741 };
742
743 /* Value is the position described by X. If X is a marker, value is
744 the marker_position of X. Otherwise, value is X. */
745
746 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
747
748 /* Enumeration returned by some move_it_.* functions internally. */
749
750 enum move_it_result
751 {
752 /* Not used. Undefined value. */
753 MOVE_UNDEFINED,
754
755 /* Move ended at the requested buffer position or ZV. */
756 MOVE_POS_MATCH_OR_ZV,
757
758 /* Move ended at the requested X pixel position. */
759 MOVE_X_REACHED,
760
761 /* Move within a line ended at the end of a line that must be
762 continued. */
763 MOVE_LINE_CONTINUED,
764
765 /* Move within a line ended at the end of a line that would
766 be displayed truncated. */
767 MOVE_LINE_TRUNCATED,
768
769 /* Move within a line ended at a line end. */
770 MOVE_NEWLINE_OR_CR
771 };
772
773 /* This counter is used to clear the face cache every once in a while
774 in redisplay_internal. It is incremented for each redisplay.
775 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
776 cleared. */
777
778 #define CLEAR_FACE_CACHE_COUNT 500
779 static int clear_face_cache_count;
780
781 /* Non-zero while redisplay_internal is in progress. */
782
783 int redisplaying_p;
784
785 /* Non-zero means don't free realized faces. Bound while freeing
786 realized faces is dangerous because glyph matrices might still
787 reference them. */
788
789 int inhibit_free_realized_faces;
790 Lisp_Object Qinhibit_free_realized_faces;
791
792 /* If a string, XTread_socket generates an event to display that string.
793 (The display is done in read_char.) */
794
795 Lisp_Object help_echo_string;
796 Lisp_Object help_echo_window;
797 Lisp_Object help_echo_object;
798 int help_echo_pos;
799
800 /* Temporary variable for XTread_socket. */
801
802 Lisp_Object previous_help_echo_string;
803
804 /* Null glyph slice */
805
806 static struct glyph_slice null_glyph_slice = { 0, 0, 0, 0 };
807
808 \f
809 /* Function prototypes. */
810
811 static void setup_for_ellipsis P_ ((struct it *, int));
812 static void mark_window_display_accurate_1 P_ ((struct window *, int));
813 static int single_display_prop_string_p P_ ((Lisp_Object, Lisp_Object));
814 static int display_prop_string_p P_ ((Lisp_Object, Lisp_Object));
815 static int cursor_row_p P_ ((struct window *, struct glyph_row *));
816 static int redisplay_mode_lines P_ ((Lisp_Object, int));
817 static char *decode_mode_spec_coding P_ ((Lisp_Object, char *, int));
818
819 #if 0
820 static int invisible_text_between_p P_ ((struct it *, int, int));
821 #endif
822
823 static int next_element_from_ellipsis P_ ((struct it *));
824 static void pint2str P_ ((char *, int, int));
825 static void pint2hrstr P_ ((char *, int, int));
826 static struct text_pos run_window_scroll_functions P_ ((Lisp_Object,
827 struct text_pos));
828 static void reconsider_clip_changes P_ ((struct window *, struct buffer *));
829 static int text_outside_line_unchanged_p P_ ((struct window *, int, int));
830 static void store_frame_title_char P_ ((char));
831 static int store_frame_title P_ ((const unsigned char *, int, int));
832 static void x_consider_frame_title P_ ((Lisp_Object));
833 static void handle_stop P_ ((struct it *));
834 static int tool_bar_lines_needed P_ ((struct frame *));
835 static int single_display_prop_intangible_p P_ ((Lisp_Object));
836 static void ensure_echo_area_buffers P_ ((void));
837 static Lisp_Object unwind_with_echo_area_buffer P_ ((Lisp_Object));
838 static Lisp_Object with_echo_area_buffer_unwind_data P_ ((struct window *));
839 static int with_echo_area_buffer P_ ((struct window *, int,
840 int (*) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
841 EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
842 static void clear_garbaged_frames P_ ((void));
843 static int current_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
844 static int truncate_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
845 static int set_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
846 static int display_echo_area P_ ((struct window *));
847 static int display_echo_area_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
848 static int resize_mini_window_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
849 static Lisp_Object unwind_redisplay P_ ((Lisp_Object));
850 static int string_char_and_length P_ ((const unsigned char *, int, int *));
851 static struct text_pos display_prop_end P_ ((struct it *, Lisp_Object,
852 struct text_pos));
853 static int compute_window_start_on_continuation_line P_ ((struct window *));
854 static Lisp_Object safe_eval_handler P_ ((Lisp_Object));
855 static void insert_left_trunc_glyphs P_ ((struct it *));
856 static struct glyph_row *get_overlay_arrow_glyph_row P_ ((struct window *,
857 Lisp_Object));
858 static void extend_face_to_end_of_line P_ ((struct it *));
859 static int append_space_for_newline P_ ((struct it *, int));
860 static int make_cursor_line_fully_visible P_ ((struct window *, int));
861 static int try_scrolling P_ ((Lisp_Object, int, EMACS_INT, EMACS_INT, int, int));
862 static int try_cursor_movement P_ ((Lisp_Object, struct text_pos, int *));
863 static int trailing_whitespace_p P_ ((int));
864 static int message_log_check_duplicate P_ ((int, int, int, int));
865 static void push_it P_ ((struct it *));
866 static void pop_it P_ ((struct it *));
867 static void sync_frame_with_window_matrix_rows P_ ((struct window *));
868 static void select_frame_for_redisplay P_ ((Lisp_Object));
869 static void redisplay_internal P_ ((int));
870 static int echo_area_display P_ ((int));
871 static void redisplay_windows P_ ((Lisp_Object));
872 static void redisplay_window P_ ((Lisp_Object, int));
873 static Lisp_Object redisplay_window_error ();
874 static Lisp_Object redisplay_window_0 P_ ((Lisp_Object));
875 static Lisp_Object redisplay_window_1 P_ ((Lisp_Object));
876 static void update_menu_bar P_ ((struct frame *, int));
877 static int try_window_reusing_current_matrix P_ ((struct window *));
878 static int try_window_id P_ ((struct window *));
879 static int display_line P_ ((struct it *));
880 static int display_mode_lines P_ ((struct window *));
881 static int display_mode_line P_ ((struct window *, enum face_id, Lisp_Object));
882 static int display_mode_element P_ ((struct it *, int, int, int, Lisp_Object, Lisp_Object, int));
883 static int store_mode_line_string P_ ((char *, Lisp_Object, int, int, int, Lisp_Object));
884 static char *decode_mode_spec P_ ((struct window *, int, int, int, int *));
885 static void display_menu_bar P_ ((struct window *));
886 static int display_count_lines P_ ((int, int, int, int, int *));
887 static int display_string P_ ((unsigned char *, Lisp_Object, Lisp_Object,
888 int, int, struct it *, int, int, int, int));
889 static void compute_line_metrics P_ ((struct it *));
890 static void run_redisplay_end_trigger_hook P_ ((struct it *));
891 static int get_overlay_strings P_ ((struct it *, int));
892 static void next_overlay_string P_ ((struct it *));
893 static void reseat P_ ((struct it *, struct text_pos, int));
894 static void reseat_1 P_ ((struct it *, struct text_pos, int));
895 static void back_to_previous_visible_line_start P_ ((struct it *));
896 void reseat_at_previous_visible_line_start P_ ((struct it *));
897 static void reseat_at_next_visible_line_start P_ ((struct it *, int));
898 static int next_element_from_display_vector P_ ((struct it *));
899 static int next_element_from_string P_ ((struct it *));
900 static int next_element_from_c_string P_ ((struct it *));
901 static int next_element_from_buffer P_ ((struct it *));
902 static int next_element_from_composition P_ ((struct it *));
903 static int next_element_from_image P_ ((struct it *));
904 static int next_element_from_stretch P_ ((struct it *));
905 static void load_overlay_strings P_ ((struct it *, int));
906 static int init_from_display_pos P_ ((struct it *, struct window *,
907 struct display_pos *));
908 static void reseat_to_string P_ ((struct it *, unsigned char *,
909 Lisp_Object, int, int, int, int));
910 static enum move_it_result move_it_in_display_line_to P_ ((struct it *,
911 int, int, int));
912 void move_it_vertically_backward P_ ((struct it *, int));
913 static void init_to_row_start P_ ((struct it *, struct window *,
914 struct glyph_row *));
915 static int init_to_row_end P_ ((struct it *, struct window *,
916 struct glyph_row *));
917 static void back_to_previous_line_start P_ ((struct it *));
918 static int forward_to_next_line_start P_ ((struct it *, int *));
919 static struct text_pos string_pos_nchars_ahead P_ ((struct text_pos,
920 Lisp_Object, int));
921 static struct text_pos string_pos P_ ((int, Lisp_Object));
922 static struct text_pos c_string_pos P_ ((int, unsigned char *, int));
923 static int number_of_chars P_ ((unsigned char *, int));
924 static void compute_stop_pos P_ ((struct it *));
925 static void compute_string_pos P_ ((struct text_pos *, struct text_pos,
926 Lisp_Object));
927 static int face_before_or_after_it_pos P_ ((struct it *, int));
928 static int next_overlay_change P_ ((int));
929 static int handle_single_display_prop P_ ((struct it *, Lisp_Object,
930 Lisp_Object, struct text_pos *,
931 int));
932 static int underlying_face_id P_ ((struct it *));
933 static int in_ellipses_for_invisible_text_p P_ ((struct display_pos *,
934 struct window *));
935
936 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
937 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
938
939 #ifdef HAVE_WINDOW_SYSTEM
940
941 static void update_tool_bar P_ ((struct frame *, int));
942 static void build_desired_tool_bar_string P_ ((struct frame *f));
943 static int redisplay_tool_bar P_ ((struct frame *));
944 static void display_tool_bar_line P_ ((struct it *));
945 static void notice_overwritten_cursor P_ ((struct window *,
946 enum glyph_row_area,
947 int, int, int, int));
948
949
950
951 #endif /* HAVE_WINDOW_SYSTEM */
952
953 \f
954 /***********************************************************************
955 Window display dimensions
956 ***********************************************************************/
957
958 /* Return the bottom boundary y-position for text lines in window W.
959 This is the first y position at which a line cannot start.
960 It is relative to the top of the window.
961
962 This is the height of W minus the height of a mode line, if any. */
963
964 INLINE int
965 window_text_bottom_y (w)
966 struct window *w;
967 {
968 int height = WINDOW_TOTAL_HEIGHT (w);
969
970 if (WINDOW_WANTS_MODELINE_P (w))
971 height -= CURRENT_MODE_LINE_HEIGHT (w);
972 return height;
973 }
974
975 /* Return the pixel width of display area AREA of window W. AREA < 0
976 means return the total width of W, not including fringes to
977 the left and right of the window. */
978
979 INLINE int
980 window_box_width (w, area)
981 struct window *w;
982 int area;
983 {
984 int cols = XFASTINT (w->total_cols);
985 int pixels = 0;
986
987 if (!w->pseudo_window_p)
988 {
989 cols -= WINDOW_SCROLL_BAR_COLS (w);
990
991 if (area == TEXT_AREA)
992 {
993 if (INTEGERP (w->left_margin_cols))
994 cols -= XFASTINT (w->left_margin_cols);
995 if (INTEGERP (w->right_margin_cols))
996 cols -= XFASTINT (w->right_margin_cols);
997 pixels = -WINDOW_TOTAL_FRINGE_WIDTH (w);
998 }
999 else if (area == LEFT_MARGIN_AREA)
1000 {
1001 cols = (INTEGERP (w->left_margin_cols)
1002 ? XFASTINT (w->left_margin_cols) : 0);
1003 pixels = 0;
1004 }
1005 else if (area == RIGHT_MARGIN_AREA)
1006 {
1007 cols = (INTEGERP (w->right_margin_cols)
1008 ? XFASTINT (w->right_margin_cols) : 0);
1009 pixels = 0;
1010 }
1011 }
1012
1013 return cols * WINDOW_FRAME_COLUMN_WIDTH (w) + pixels;
1014 }
1015
1016
1017 /* Return the pixel height of the display area of window W, not
1018 including mode lines of W, if any. */
1019
1020 INLINE int
1021 window_box_height (w)
1022 struct window *w;
1023 {
1024 struct frame *f = XFRAME (w->frame);
1025 int height = WINDOW_TOTAL_HEIGHT (w);
1026
1027 xassert (height >= 0);
1028
1029 /* Note: the code below that determines the mode-line/header-line
1030 height is essentially the same as that contained in the macro
1031 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
1032 the appropriate glyph row has its `mode_line_p' flag set,
1033 and if it doesn't, uses estimate_mode_line_height instead. */
1034
1035 if (WINDOW_WANTS_MODELINE_P (w))
1036 {
1037 struct glyph_row *ml_row
1038 = (w->current_matrix && w->current_matrix->rows
1039 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
1040 : 0);
1041 if (ml_row && ml_row->mode_line_p)
1042 height -= ml_row->height;
1043 else
1044 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
1045 }
1046
1047 if (WINDOW_WANTS_HEADER_LINE_P (w))
1048 {
1049 struct glyph_row *hl_row
1050 = (w->current_matrix && w->current_matrix->rows
1051 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1052 : 0);
1053 if (hl_row && hl_row->mode_line_p)
1054 height -= hl_row->height;
1055 else
1056 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1057 }
1058
1059 /* With a very small font and a mode-line that's taller than
1060 default, we might end up with a negative height. */
1061 return max (0, height);
1062 }
1063
1064 /* Return the window-relative coordinate of the left edge of display
1065 area AREA of window W. AREA < 0 means return the left edge of the
1066 whole window, to the right of the left fringe of W. */
1067
1068 INLINE int
1069 window_box_left_offset (w, area)
1070 struct window *w;
1071 int area;
1072 {
1073 int x;
1074
1075 if (w->pseudo_window_p)
1076 return 0;
1077
1078 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1079
1080 if (area == TEXT_AREA)
1081 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1082 + window_box_width (w, LEFT_MARGIN_AREA));
1083 else if (area == RIGHT_MARGIN_AREA)
1084 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1085 + window_box_width (w, LEFT_MARGIN_AREA)
1086 + window_box_width (w, TEXT_AREA)
1087 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1088 ? 0
1089 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1090 else if (area == LEFT_MARGIN_AREA
1091 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1092 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1093
1094 return x;
1095 }
1096
1097
1098 /* Return the window-relative coordinate of the right edge of display
1099 area AREA of window W. AREA < 0 means return the left edge of the
1100 whole window, to the left of the right fringe of W. */
1101
1102 INLINE int
1103 window_box_right_offset (w, area)
1104 struct window *w;
1105 int area;
1106 {
1107 return window_box_left_offset (w, area) + window_box_width (w, area);
1108 }
1109
1110 /* Return the frame-relative coordinate of the left edge of display
1111 area AREA of window W. AREA < 0 means return the left edge of the
1112 whole window, to the right of the left fringe of W. */
1113
1114 INLINE int
1115 window_box_left (w, area)
1116 struct window *w;
1117 int area;
1118 {
1119 struct frame *f = XFRAME (w->frame);
1120 int x;
1121
1122 if (w->pseudo_window_p)
1123 return FRAME_INTERNAL_BORDER_WIDTH (f);
1124
1125 x = (WINDOW_LEFT_EDGE_X (w)
1126 + window_box_left_offset (w, area));
1127
1128 return x;
1129 }
1130
1131
1132 /* Return the frame-relative coordinate of the right edge of display
1133 area AREA of window W. AREA < 0 means return the left edge of the
1134 whole window, to the left of the right fringe of W. */
1135
1136 INLINE int
1137 window_box_right (w, area)
1138 struct window *w;
1139 int area;
1140 {
1141 return window_box_left (w, area) + window_box_width (w, area);
1142 }
1143
1144 /* Get the bounding box of the display area AREA of window W, without
1145 mode lines, in frame-relative coordinates. AREA < 0 means the
1146 whole window, not including the left and right fringes of
1147 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1148 coordinates of the upper-left corner of the box. Return in
1149 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1150
1151 INLINE void
1152 window_box (w, area, box_x, box_y, box_width, box_height)
1153 struct window *w;
1154 int area;
1155 int *box_x, *box_y, *box_width, *box_height;
1156 {
1157 if (box_width)
1158 *box_width = window_box_width (w, area);
1159 if (box_height)
1160 *box_height = window_box_height (w);
1161 if (box_x)
1162 *box_x = window_box_left (w, area);
1163 if (box_y)
1164 {
1165 *box_y = WINDOW_TOP_EDGE_Y (w);
1166 if (WINDOW_WANTS_HEADER_LINE_P (w))
1167 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1168 }
1169 }
1170
1171
1172 /* Get the bounding box of the display area AREA of window W, without
1173 mode lines. AREA < 0 means the whole window, not including the
1174 left and right fringe of the window. Return in *TOP_LEFT_X
1175 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1176 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1177 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1178 box. */
1179
1180 INLINE void
1181 window_box_edges (w, area, top_left_x, top_left_y,
1182 bottom_right_x, bottom_right_y)
1183 struct window *w;
1184 int area;
1185 int *top_left_x, *top_left_y, *bottom_right_x, *bottom_right_y;
1186 {
1187 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
1188 bottom_right_y);
1189 *bottom_right_x += *top_left_x;
1190 *bottom_right_y += *top_left_y;
1191 }
1192
1193
1194 \f
1195 /***********************************************************************
1196 Utilities
1197 ***********************************************************************/
1198
1199 /* Return the bottom y-position of the line the iterator IT is in.
1200 This can modify IT's settings. */
1201
1202 int
1203 line_bottom_y (it)
1204 struct it *it;
1205 {
1206 int line_height = it->max_ascent + it->max_descent;
1207 int line_top_y = it->current_y;
1208
1209 if (line_height == 0)
1210 {
1211 if (last_height)
1212 line_height = last_height;
1213 else if (IT_CHARPOS (*it) < ZV)
1214 {
1215 move_it_by_lines (it, 1, 1);
1216 line_height = (it->max_ascent || it->max_descent
1217 ? it->max_ascent + it->max_descent
1218 : last_height);
1219 }
1220 else
1221 {
1222 struct glyph_row *row = it->glyph_row;
1223
1224 /* Use the default character height. */
1225 it->glyph_row = NULL;
1226 it->what = IT_CHARACTER;
1227 it->c = ' ';
1228 it->len = 1;
1229 PRODUCE_GLYPHS (it);
1230 line_height = it->ascent + it->descent;
1231 it->glyph_row = row;
1232 }
1233 }
1234
1235 return line_top_y + line_height;
1236 }
1237
1238
1239 /* Return 1 if position CHARPOS is visible in window W. Set *FULLY to
1240 1 if POS is visible and the line containing POS is fully visible.
1241 EXACT_MODE_LINE_HEIGHTS_P non-zero means compute exact mode-line
1242 and header-lines heights. */
1243
1244 int
1245 pos_visible_p (w, charpos, fully, x, y, exact_mode_line_heights_p)
1246 struct window *w;
1247 int charpos, *fully, *x, *y, exact_mode_line_heights_p;
1248 {
1249 struct it it;
1250 struct text_pos top;
1251 int visible_p;
1252 struct buffer *old_buffer = NULL;
1253
1254 if (XBUFFER (w->buffer) != current_buffer)
1255 {
1256 old_buffer = current_buffer;
1257 set_buffer_internal_1 (XBUFFER (w->buffer));
1258 }
1259
1260 *fully = visible_p = 0;
1261 SET_TEXT_POS_FROM_MARKER (top, w->start);
1262
1263 /* Compute exact mode line heights, if requested. */
1264 if (exact_mode_line_heights_p)
1265 {
1266 if (WINDOW_WANTS_MODELINE_P (w))
1267 current_mode_line_height
1268 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1269 current_buffer->mode_line_format);
1270
1271 if (WINDOW_WANTS_HEADER_LINE_P (w))
1272 current_header_line_height
1273 = display_mode_line (w, HEADER_LINE_FACE_ID,
1274 current_buffer->header_line_format);
1275 }
1276
1277 start_display (&it, w, top);
1278 move_it_to (&it, charpos, 0, it.last_visible_y, -1,
1279 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
1280
1281 /* Note that we may overshoot because of invisible text. */
1282 if (IT_CHARPOS (it) >= charpos)
1283 {
1284 int top_y = it.current_y;
1285 int bottom_y = line_bottom_y (&it);
1286 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1287
1288 if (top_y < window_top_y)
1289 visible_p = bottom_y > window_top_y;
1290 else if (top_y < it.last_visible_y)
1291 {
1292 visible_p = 1;
1293 *fully = bottom_y <= it.last_visible_y;
1294 }
1295 if (visible_p && x)
1296 {
1297 *x = it.current_x;
1298 *y = max (top_y + it.max_ascent - it.ascent, window_top_y);
1299 }
1300 }
1301 else if (it.current_y + it.max_ascent + it.max_descent > it.last_visible_y)
1302 {
1303 struct it it2;
1304
1305 it2 = it;
1306 move_it_by_lines (&it, 1, 0);
1307 if (charpos < IT_CHARPOS (it))
1308 {
1309 visible_p = 1;
1310 if (x)
1311 {
1312 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1313 *x = it2.current_x;
1314 *y = it2.current_y + it2.max_ascent - it2.ascent;
1315 }
1316 }
1317 }
1318
1319 if (old_buffer)
1320 set_buffer_internal_1 (old_buffer);
1321
1322 current_header_line_height = current_mode_line_height = -1;
1323
1324 return visible_p;
1325 }
1326
1327
1328 /* Return the next character from STR which is MAXLEN bytes long.
1329 Return in *LEN the length of the character. This is like
1330 STRING_CHAR_AND_LENGTH but never returns an invalid character. If
1331 we find one, we return a `?', but with the length of the invalid
1332 character. */
1333
1334 static INLINE int
1335 string_char_and_length (str, maxlen, len)
1336 const unsigned char *str;
1337 int maxlen, *len;
1338 {
1339 int c;
1340
1341 c = STRING_CHAR_AND_LENGTH (str, maxlen, *len);
1342 if (!CHAR_VALID_P (c, 1))
1343 /* We may not change the length here because other places in Emacs
1344 don't use this function, i.e. they silently accept invalid
1345 characters. */
1346 c = '?';
1347
1348 return c;
1349 }
1350
1351
1352
1353 /* Given a position POS containing a valid character and byte position
1354 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1355
1356 static struct text_pos
1357 string_pos_nchars_ahead (pos, string, nchars)
1358 struct text_pos pos;
1359 Lisp_Object string;
1360 int nchars;
1361 {
1362 xassert (STRINGP (string) && nchars >= 0);
1363
1364 if (STRING_MULTIBYTE (string))
1365 {
1366 int rest = SBYTES (string) - BYTEPOS (pos);
1367 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1368 int len;
1369
1370 while (nchars--)
1371 {
1372 string_char_and_length (p, rest, &len);
1373 p += len, rest -= len;
1374 xassert (rest >= 0);
1375 CHARPOS (pos) += 1;
1376 BYTEPOS (pos) += len;
1377 }
1378 }
1379 else
1380 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1381
1382 return pos;
1383 }
1384
1385
1386 /* Value is the text position, i.e. character and byte position,
1387 for character position CHARPOS in STRING. */
1388
1389 static INLINE struct text_pos
1390 string_pos (charpos, string)
1391 int charpos;
1392 Lisp_Object string;
1393 {
1394 struct text_pos pos;
1395 xassert (STRINGP (string));
1396 xassert (charpos >= 0);
1397 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1398 return pos;
1399 }
1400
1401
1402 /* Value is a text position, i.e. character and byte position, for
1403 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1404 means recognize multibyte characters. */
1405
1406 static struct text_pos
1407 c_string_pos (charpos, s, multibyte_p)
1408 int charpos;
1409 unsigned char *s;
1410 int multibyte_p;
1411 {
1412 struct text_pos pos;
1413
1414 xassert (s != NULL);
1415 xassert (charpos >= 0);
1416
1417 if (multibyte_p)
1418 {
1419 int rest = strlen (s), len;
1420
1421 SET_TEXT_POS (pos, 0, 0);
1422 while (charpos--)
1423 {
1424 string_char_and_length (s, rest, &len);
1425 s += len, rest -= len;
1426 xassert (rest >= 0);
1427 CHARPOS (pos) += 1;
1428 BYTEPOS (pos) += len;
1429 }
1430 }
1431 else
1432 SET_TEXT_POS (pos, charpos, charpos);
1433
1434 return pos;
1435 }
1436
1437
1438 /* Value is the number of characters in C string S. MULTIBYTE_P
1439 non-zero means recognize multibyte characters. */
1440
1441 static int
1442 number_of_chars (s, multibyte_p)
1443 unsigned char *s;
1444 int multibyte_p;
1445 {
1446 int nchars;
1447
1448 if (multibyte_p)
1449 {
1450 int rest = strlen (s), len;
1451 unsigned char *p = (unsigned char *) s;
1452
1453 for (nchars = 0; rest > 0; ++nchars)
1454 {
1455 string_char_and_length (p, rest, &len);
1456 rest -= len, p += len;
1457 }
1458 }
1459 else
1460 nchars = strlen (s);
1461
1462 return nchars;
1463 }
1464
1465
1466 /* Compute byte position NEWPOS->bytepos corresponding to
1467 NEWPOS->charpos. POS is a known position in string STRING.
1468 NEWPOS->charpos must be >= POS.charpos. */
1469
1470 static void
1471 compute_string_pos (newpos, pos, string)
1472 struct text_pos *newpos, pos;
1473 Lisp_Object string;
1474 {
1475 xassert (STRINGP (string));
1476 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1477
1478 if (STRING_MULTIBYTE (string))
1479 *newpos = string_pos_nchars_ahead (pos, string,
1480 CHARPOS (*newpos) - CHARPOS (pos));
1481 else
1482 BYTEPOS (*newpos) = CHARPOS (*newpos);
1483 }
1484
1485 /* EXPORT:
1486 Return an estimation of the pixel height of mode or top lines on
1487 frame F. FACE_ID specifies what line's height to estimate. */
1488
1489 int
1490 estimate_mode_line_height (f, face_id)
1491 struct frame *f;
1492 enum face_id face_id;
1493 {
1494 #ifdef HAVE_WINDOW_SYSTEM
1495 if (FRAME_WINDOW_P (f))
1496 {
1497 int height = FONT_HEIGHT (FRAME_FONT (f));
1498
1499 /* This function is called so early when Emacs starts that the face
1500 cache and mode line face are not yet initialized. */
1501 if (FRAME_FACE_CACHE (f))
1502 {
1503 struct face *face = FACE_FROM_ID (f, face_id);
1504 if (face)
1505 {
1506 if (face->font)
1507 height = FONT_HEIGHT (face->font);
1508 if (face->box_line_width > 0)
1509 height += 2 * face->box_line_width;
1510 }
1511 }
1512
1513 return height;
1514 }
1515 #endif
1516
1517 return 1;
1518 }
1519
1520 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1521 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1522 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1523 not force the value into range. */
1524
1525 void
1526 pixel_to_glyph_coords (f, pix_x, pix_y, x, y, bounds, noclip)
1527 FRAME_PTR f;
1528 register int pix_x, pix_y;
1529 int *x, *y;
1530 NativeRectangle *bounds;
1531 int noclip;
1532 {
1533
1534 #ifdef HAVE_WINDOW_SYSTEM
1535 if (FRAME_WINDOW_P (f))
1536 {
1537 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1538 even for negative values. */
1539 if (pix_x < 0)
1540 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1541 if (pix_y < 0)
1542 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1543
1544 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1545 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1546
1547 if (bounds)
1548 STORE_NATIVE_RECT (*bounds,
1549 FRAME_COL_TO_PIXEL_X (f, pix_x),
1550 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1551 FRAME_COLUMN_WIDTH (f) - 1,
1552 FRAME_LINE_HEIGHT (f) - 1);
1553
1554 if (!noclip)
1555 {
1556 if (pix_x < 0)
1557 pix_x = 0;
1558 else if (pix_x > FRAME_TOTAL_COLS (f))
1559 pix_x = FRAME_TOTAL_COLS (f);
1560
1561 if (pix_y < 0)
1562 pix_y = 0;
1563 else if (pix_y > FRAME_LINES (f))
1564 pix_y = FRAME_LINES (f);
1565 }
1566 }
1567 #endif
1568
1569 *x = pix_x;
1570 *y = pix_y;
1571 }
1572
1573
1574 /* Given HPOS/VPOS in the current matrix of W, return corresponding
1575 frame-relative pixel positions in *FRAME_X and *FRAME_Y. If we
1576 can't tell the positions because W's display is not up to date,
1577 return 0. */
1578
1579 int
1580 glyph_to_pixel_coords (w, hpos, vpos, frame_x, frame_y)
1581 struct window *w;
1582 int hpos, vpos;
1583 int *frame_x, *frame_y;
1584 {
1585 #ifdef HAVE_WINDOW_SYSTEM
1586 if (FRAME_WINDOW_P (XFRAME (WINDOW_FRAME (w))))
1587 {
1588 int success_p;
1589
1590 xassert (hpos >= 0 && hpos < w->current_matrix->matrix_w);
1591 xassert (vpos >= 0 && vpos < w->current_matrix->matrix_h);
1592
1593 if (display_completed)
1594 {
1595 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
1596 struct glyph *glyph = row->glyphs[TEXT_AREA];
1597 struct glyph *end = glyph + min (hpos, row->used[TEXT_AREA]);
1598
1599 hpos = row->x;
1600 vpos = row->y;
1601 while (glyph < end)
1602 {
1603 hpos += glyph->pixel_width;
1604 ++glyph;
1605 }
1606
1607 /* If first glyph is partially visible, its first visible position is still 0. */
1608 if (hpos < 0)
1609 hpos = 0;
1610
1611 success_p = 1;
1612 }
1613 else
1614 {
1615 hpos = vpos = 0;
1616 success_p = 0;
1617 }
1618
1619 *frame_x = WINDOW_TO_FRAME_PIXEL_X (w, hpos);
1620 *frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, vpos);
1621 return success_p;
1622 }
1623 #endif
1624
1625 *frame_x = hpos;
1626 *frame_y = vpos;
1627 return 1;
1628 }
1629
1630
1631 #ifdef HAVE_WINDOW_SYSTEM
1632
1633 /* Find the glyph under window-relative coordinates X/Y in window W.
1634 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1635 strings. Return in *HPOS and *VPOS the row and column number of
1636 the glyph found. Return in *AREA the glyph area containing X.
1637 Value is a pointer to the glyph found or null if X/Y is not on
1638 text, or we can't tell because W's current matrix is not up to
1639 date. */
1640
1641 static struct glyph *
1642 x_y_to_hpos_vpos (w, x, y, hpos, vpos, dx, dy, area)
1643 struct window *w;
1644 int x, y;
1645 int *hpos, *vpos, *dx, *dy, *area;
1646 {
1647 struct glyph *glyph, *end;
1648 struct glyph_row *row = NULL;
1649 int x0, i;
1650
1651 /* Find row containing Y. Give up if some row is not enabled. */
1652 for (i = 0; i < w->current_matrix->nrows; ++i)
1653 {
1654 row = MATRIX_ROW (w->current_matrix, i);
1655 if (!row->enabled_p)
1656 return NULL;
1657 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1658 break;
1659 }
1660
1661 *vpos = i;
1662 *hpos = 0;
1663
1664 /* Give up if Y is not in the window. */
1665 if (i == w->current_matrix->nrows)
1666 return NULL;
1667
1668 /* Get the glyph area containing X. */
1669 if (w->pseudo_window_p)
1670 {
1671 *area = TEXT_AREA;
1672 x0 = 0;
1673 }
1674 else
1675 {
1676 if (x < window_box_left_offset (w, TEXT_AREA))
1677 {
1678 *area = LEFT_MARGIN_AREA;
1679 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1680 }
1681 else if (x < window_box_right_offset (w, TEXT_AREA))
1682 {
1683 *area = TEXT_AREA;
1684 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1685 }
1686 else
1687 {
1688 *area = RIGHT_MARGIN_AREA;
1689 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1690 }
1691 }
1692
1693 /* Find glyph containing X. */
1694 glyph = row->glyphs[*area];
1695 end = glyph + row->used[*area];
1696 x -= x0;
1697 while (glyph < end && x >= glyph->pixel_width)
1698 {
1699 x -= glyph->pixel_width;
1700 ++glyph;
1701 }
1702
1703 if (glyph == end)
1704 return NULL;
1705
1706 if (dx)
1707 {
1708 *dx = x;
1709 *dy = y - (row->y + row->ascent - glyph->ascent);
1710 }
1711
1712 *hpos = glyph - row->glyphs[*area];
1713 return glyph;
1714 }
1715
1716
1717 /* EXPORT:
1718 Convert frame-relative x/y to coordinates relative to window W.
1719 Takes pseudo-windows into account. */
1720
1721 void
1722 frame_to_window_pixel_xy (w, x, y)
1723 struct window *w;
1724 int *x, *y;
1725 {
1726 if (w->pseudo_window_p)
1727 {
1728 /* A pseudo-window is always full-width, and starts at the
1729 left edge of the frame, plus a frame border. */
1730 struct frame *f = XFRAME (w->frame);
1731 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1732 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1733 }
1734 else
1735 {
1736 *x -= WINDOW_LEFT_EDGE_X (w);
1737 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1738 }
1739 }
1740
1741 /* EXPORT:
1742 Return in *R the clipping rectangle for glyph string S. */
1743
1744 void
1745 get_glyph_string_clip_rect (s, nr)
1746 struct glyph_string *s;
1747 NativeRectangle *nr;
1748 {
1749 XRectangle r;
1750
1751 if (s->row->full_width_p)
1752 {
1753 /* Draw full-width. X coordinates are relative to S->w->left_col. */
1754 r.x = WINDOW_LEFT_EDGE_X (s->w);
1755 r.width = WINDOW_TOTAL_WIDTH (s->w);
1756
1757 /* Unless displaying a mode or menu bar line, which are always
1758 fully visible, clip to the visible part of the row. */
1759 if (s->w->pseudo_window_p)
1760 r.height = s->row->visible_height;
1761 else
1762 r.height = s->height;
1763 }
1764 else
1765 {
1766 /* This is a text line that may be partially visible. */
1767 r.x = window_box_left (s->w, s->area);
1768 r.width = window_box_width (s->w, s->area);
1769 r.height = s->row->visible_height;
1770 }
1771
1772 /* If S draws overlapping rows, it's sufficient to use the top and
1773 bottom of the window for clipping because this glyph string
1774 intentionally draws over other lines. */
1775 if (s->for_overlaps_p)
1776 {
1777 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1778 r.height = window_text_bottom_y (s->w) - r.y;
1779 }
1780 else
1781 {
1782 /* Don't use S->y for clipping because it doesn't take partially
1783 visible lines into account. For example, it can be negative for
1784 partially visible lines at the top of a window. */
1785 if (!s->row->full_width_p
1786 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
1787 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1788 else
1789 r.y = max (0, s->row->y);
1790
1791 /* If drawing a tool-bar window, draw it over the internal border
1792 at the top of the window. */
1793 if (WINDOWP (s->f->tool_bar_window)
1794 && s->w == XWINDOW (s->f->tool_bar_window))
1795 r.y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
1796 }
1797
1798 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
1799
1800 /* If drawing the cursor, don't let glyph draw outside its
1801 advertised boundaries. Cleartype does this under some circumstances. */
1802 if (s->hl == DRAW_CURSOR)
1803 {
1804 struct glyph *glyph = s->first_glyph;
1805 int height;
1806
1807 if (s->x > r.x)
1808 {
1809 r.width -= s->x - r.x;
1810 r.x = s->x;
1811 }
1812 r.width = min (r.width, glyph->pixel_width);
1813
1814 /* Don't draw cursor glyph taller than our actual glyph. */
1815 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
1816 if (height < r.height)
1817 {
1818 int max_y = r.y + r.height;
1819 r.y = min (max_y, s->ybase + glyph->descent - height);
1820 r.height = min (max_y - r.y, height);
1821 }
1822 }
1823
1824 #ifdef CONVERT_FROM_XRECT
1825 CONVERT_FROM_XRECT (r, *nr);
1826 #else
1827 *nr = r;
1828 #endif
1829 }
1830
1831 #endif /* HAVE_WINDOW_SYSTEM */
1832
1833 \f
1834 /***********************************************************************
1835 Lisp form evaluation
1836 ***********************************************************************/
1837
1838 /* Error handler for safe_eval and safe_call. */
1839
1840 static Lisp_Object
1841 safe_eval_handler (arg)
1842 Lisp_Object arg;
1843 {
1844 add_to_log ("Error during redisplay: %s", arg, Qnil);
1845 return Qnil;
1846 }
1847
1848
1849 /* Evaluate SEXPR and return the result, or nil if something went
1850 wrong. Prevent redisplay during the evaluation. */
1851
1852 Lisp_Object
1853 safe_eval (sexpr)
1854 Lisp_Object sexpr;
1855 {
1856 Lisp_Object val;
1857
1858 if (inhibit_eval_during_redisplay)
1859 val = Qnil;
1860 else
1861 {
1862 int count = SPECPDL_INDEX ();
1863 struct gcpro gcpro1;
1864
1865 GCPRO1 (sexpr);
1866 specbind (Qinhibit_redisplay, Qt);
1867 /* Use Qt to ensure debugger does not run,
1868 so there is no possibility of wanting to redisplay. */
1869 val = internal_condition_case_1 (Feval, sexpr, Qt,
1870 safe_eval_handler);
1871 UNGCPRO;
1872 val = unbind_to (count, val);
1873 }
1874
1875 return val;
1876 }
1877
1878
1879 /* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
1880 Return the result, or nil if something went wrong. Prevent
1881 redisplay during the evaluation. */
1882
1883 Lisp_Object
1884 safe_call (nargs, args)
1885 int nargs;
1886 Lisp_Object *args;
1887 {
1888 Lisp_Object val;
1889
1890 if (inhibit_eval_during_redisplay)
1891 val = Qnil;
1892 else
1893 {
1894 int count = SPECPDL_INDEX ();
1895 struct gcpro gcpro1;
1896
1897 GCPRO1 (args[0]);
1898 gcpro1.nvars = nargs;
1899 specbind (Qinhibit_redisplay, Qt);
1900 /* Use Qt to ensure debugger does not run,
1901 so there is no possibility of wanting to redisplay. */
1902 val = internal_condition_case_2 (Ffuncall, nargs, args, Qt,
1903 safe_eval_handler);
1904 UNGCPRO;
1905 val = unbind_to (count, val);
1906 }
1907
1908 return val;
1909 }
1910
1911
1912 /* Call function FN with one argument ARG.
1913 Return the result, or nil if something went wrong. */
1914
1915 Lisp_Object
1916 safe_call1 (fn, arg)
1917 Lisp_Object fn, arg;
1918 {
1919 Lisp_Object args[2];
1920 args[0] = fn;
1921 args[1] = arg;
1922 return safe_call (2, args);
1923 }
1924
1925
1926 \f
1927 /***********************************************************************
1928 Debugging
1929 ***********************************************************************/
1930
1931 #if 0
1932
1933 /* Define CHECK_IT to perform sanity checks on iterators.
1934 This is for debugging. It is too slow to do unconditionally. */
1935
1936 static void
1937 check_it (it)
1938 struct it *it;
1939 {
1940 if (it->method == next_element_from_string)
1941 {
1942 xassert (STRINGP (it->string));
1943 xassert (IT_STRING_CHARPOS (*it) >= 0);
1944 }
1945 else
1946 {
1947 xassert (IT_STRING_CHARPOS (*it) < 0);
1948 if (it->method == next_element_from_buffer)
1949 {
1950 /* Check that character and byte positions agree. */
1951 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
1952 }
1953 }
1954
1955 if (it->dpvec)
1956 xassert (it->current.dpvec_index >= 0);
1957 else
1958 xassert (it->current.dpvec_index < 0);
1959 }
1960
1961 #define CHECK_IT(IT) check_it ((IT))
1962
1963 #else /* not 0 */
1964
1965 #define CHECK_IT(IT) (void) 0
1966
1967 #endif /* not 0 */
1968
1969
1970 #if GLYPH_DEBUG
1971
1972 /* Check that the window end of window W is what we expect it
1973 to be---the last row in the current matrix displaying text. */
1974
1975 static void
1976 check_window_end (w)
1977 struct window *w;
1978 {
1979 if (!MINI_WINDOW_P (w)
1980 && !NILP (w->window_end_valid))
1981 {
1982 struct glyph_row *row;
1983 xassert ((row = MATRIX_ROW (w->current_matrix,
1984 XFASTINT (w->window_end_vpos)),
1985 !row->enabled_p
1986 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
1987 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
1988 }
1989 }
1990
1991 #define CHECK_WINDOW_END(W) check_window_end ((W))
1992
1993 #else /* not GLYPH_DEBUG */
1994
1995 #define CHECK_WINDOW_END(W) (void) 0
1996
1997 #endif /* not GLYPH_DEBUG */
1998
1999
2000 \f
2001 /***********************************************************************
2002 Iterator initialization
2003 ***********************************************************************/
2004
2005 /* Initialize IT for displaying current_buffer in window W, starting
2006 at character position CHARPOS. CHARPOS < 0 means that no buffer
2007 position is specified which is useful when the iterator is assigned
2008 a position later. BYTEPOS is the byte position corresponding to
2009 CHARPOS. BYTEPOS < 0 means compute it from CHARPOS.
2010
2011 If ROW is not null, calls to produce_glyphs with IT as parameter
2012 will produce glyphs in that row.
2013
2014 BASE_FACE_ID is the id of a base face to use. It must be one of
2015 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2016 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2017 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2018
2019 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2020 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2021 will be initialized to use the corresponding mode line glyph row of
2022 the desired matrix of W. */
2023
2024 void
2025 init_iterator (it, w, charpos, bytepos, row, base_face_id)
2026 struct it *it;
2027 struct window *w;
2028 int charpos, bytepos;
2029 struct glyph_row *row;
2030 enum face_id base_face_id;
2031 {
2032 int highlight_region_p;
2033
2034 /* Some precondition checks. */
2035 xassert (w != NULL && it != NULL);
2036 xassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2037 && charpos <= ZV));
2038
2039 /* If face attributes have been changed since the last redisplay,
2040 free realized faces now because they depend on face definitions
2041 that might have changed. Don't free faces while there might be
2042 desired matrices pending which reference these faces. */
2043 if (face_change_count && !inhibit_free_realized_faces)
2044 {
2045 face_change_count = 0;
2046 free_all_realized_faces (Qnil);
2047 }
2048
2049 /* Use one of the mode line rows of W's desired matrix if
2050 appropriate. */
2051 if (row == NULL)
2052 {
2053 if (base_face_id == MODE_LINE_FACE_ID
2054 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2055 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2056 else if (base_face_id == HEADER_LINE_FACE_ID)
2057 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2058 }
2059
2060 /* Clear IT. */
2061 bzero (it, sizeof *it);
2062 it->current.overlay_string_index = -1;
2063 it->current.dpvec_index = -1;
2064 it->base_face_id = base_face_id;
2065 it->string = Qnil;
2066 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2067
2068 /* The window in which we iterate over current_buffer: */
2069 XSETWINDOW (it->window, w);
2070 it->w = w;
2071 it->f = XFRAME (w->frame);
2072
2073 /* Extra space between lines (on window systems only). */
2074 if (base_face_id == DEFAULT_FACE_ID
2075 && FRAME_WINDOW_P (it->f))
2076 {
2077 if (NATNUMP (current_buffer->extra_line_spacing))
2078 it->extra_line_spacing = XFASTINT (current_buffer->extra_line_spacing);
2079 else if (FLOATP (current_buffer->extra_line_spacing))
2080 it->extra_line_spacing = (XFLOAT_DATA (current_buffer->extra_line_spacing)
2081 * FRAME_LINE_HEIGHT (it->f));
2082 else if (it->f->extra_line_spacing > 0)
2083 it->extra_line_spacing = it->f->extra_line_spacing;
2084 it->max_extra_line_spacing = 0;
2085 }
2086
2087 /* If realized faces have been removed, e.g. because of face
2088 attribute changes of named faces, recompute them. When running
2089 in batch mode, the face cache of the initial frame is null. If
2090 we happen to get called, make a dummy face cache. */
2091 if (FRAME_FACE_CACHE (it->f) == NULL)
2092 init_frame_faces (it->f);
2093 if (FRAME_FACE_CACHE (it->f)->used == 0)
2094 recompute_basic_faces (it->f);
2095
2096 /* Current value of the `slice', `space-width', and 'height' properties. */
2097 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2098 it->space_width = Qnil;
2099 it->font_height = Qnil;
2100 it->override_ascent = -1;
2101
2102 /* Are control characters displayed as `^C'? */
2103 it->ctl_arrow_p = !NILP (current_buffer->ctl_arrow);
2104
2105 /* -1 means everything between a CR and the following line end
2106 is invisible. >0 means lines indented more than this value are
2107 invisible. */
2108 it->selective = (INTEGERP (current_buffer->selective_display)
2109 ? XFASTINT (current_buffer->selective_display)
2110 : (!NILP (current_buffer->selective_display)
2111 ? -1 : 0));
2112 it->selective_display_ellipsis_p
2113 = !NILP (current_buffer->selective_display_ellipses);
2114
2115 /* Display table to use. */
2116 it->dp = window_display_table (w);
2117
2118 /* Are multibyte characters enabled in current_buffer? */
2119 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
2120
2121 /* Non-zero if we should highlight the region. */
2122 highlight_region_p
2123 = (!NILP (Vtransient_mark_mode)
2124 && !NILP (current_buffer->mark_active)
2125 && XMARKER (current_buffer->mark)->buffer != 0);
2126
2127 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
2128 start and end of a visible region in window IT->w. Set both to
2129 -1 to indicate no region. */
2130 if (highlight_region_p
2131 /* Maybe highlight only in selected window. */
2132 && (/* Either show region everywhere. */
2133 highlight_nonselected_windows
2134 /* Or show region in the selected window. */
2135 || w == XWINDOW (selected_window)
2136 /* Or show the region if we are in the mini-buffer and W is
2137 the window the mini-buffer refers to. */
2138 || (MINI_WINDOW_P (XWINDOW (selected_window))
2139 && WINDOWP (minibuf_selected_window)
2140 && w == XWINDOW (minibuf_selected_window))))
2141 {
2142 int charpos = marker_position (current_buffer->mark);
2143 it->region_beg_charpos = min (PT, charpos);
2144 it->region_end_charpos = max (PT, charpos);
2145 }
2146 else
2147 it->region_beg_charpos = it->region_end_charpos = -1;
2148
2149 /* Get the position at which the redisplay_end_trigger hook should
2150 be run, if it is to be run at all. */
2151 if (MARKERP (w->redisplay_end_trigger)
2152 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2153 it->redisplay_end_trigger_charpos
2154 = marker_position (w->redisplay_end_trigger);
2155 else if (INTEGERP (w->redisplay_end_trigger))
2156 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
2157
2158 /* Correct bogus values of tab_width. */
2159 it->tab_width = XINT (current_buffer->tab_width);
2160 if (it->tab_width <= 0 || it->tab_width > 1000)
2161 it->tab_width = 8;
2162
2163 /* Are lines in the display truncated? */
2164 it->truncate_lines_p
2165 = (base_face_id != DEFAULT_FACE_ID
2166 || XINT (it->w->hscroll)
2167 || (truncate_partial_width_windows
2168 && !WINDOW_FULL_WIDTH_P (it->w))
2169 || !NILP (current_buffer->truncate_lines));
2170
2171 /* Get dimensions of truncation and continuation glyphs. These are
2172 displayed as fringe bitmaps under X, so we don't need them for such
2173 frames. */
2174 if (!FRAME_WINDOW_P (it->f))
2175 {
2176 if (it->truncate_lines_p)
2177 {
2178 /* We will need the truncation glyph. */
2179 xassert (it->glyph_row == NULL);
2180 produce_special_glyphs (it, IT_TRUNCATION);
2181 it->truncation_pixel_width = it->pixel_width;
2182 }
2183 else
2184 {
2185 /* We will need the continuation glyph. */
2186 xassert (it->glyph_row == NULL);
2187 produce_special_glyphs (it, IT_CONTINUATION);
2188 it->continuation_pixel_width = it->pixel_width;
2189 }
2190
2191 /* Reset these values to zero because the produce_special_glyphs
2192 above has changed them. */
2193 it->pixel_width = it->ascent = it->descent = 0;
2194 it->phys_ascent = it->phys_descent = 0;
2195 }
2196
2197 /* Set this after getting the dimensions of truncation and
2198 continuation glyphs, so that we don't produce glyphs when calling
2199 produce_special_glyphs, above. */
2200 it->glyph_row = row;
2201 it->area = TEXT_AREA;
2202
2203 /* Get the dimensions of the display area. The display area
2204 consists of the visible window area plus a horizontally scrolled
2205 part to the left of the window. All x-values are relative to the
2206 start of this total display area. */
2207 if (base_face_id != DEFAULT_FACE_ID)
2208 {
2209 /* Mode lines, menu bar in terminal frames. */
2210 it->first_visible_x = 0;
2211 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
2212 }
2213 else
2214 {
2215 it->first_visible_x
2216 = XFASTINT (it->w->hscroll) * FRAME_COLUMN_WIDTH (it->f);
2217 it->last_visible_x = (it->first_visible_x
2218 + window_box_width (w, TEXT_AREA));
2219
2220 /* If we truncate lines, leave room for the truncator glyph(s) at
2221 the right margin. Otherwise, leave room for the continuation
2222 glyph(s). Truncation and continuation glyphs are not inserted
2223 for window-based redisplay. */
2224 if (!FRAME_WINDOW_P (it->f))
2225 {
2226 if (it->truncate_lines_p)
2227 it->last_visible_x -= it->truncation_pixel_width;
2228 else
2229 it->last_visible_x -= it->continuation_pixel_width;
2230 }
2231
2232 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2233 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2234 }
2235
2236 /* Leave room for a border glyph. */
2237 if (!FRAME_WINDOW_P (it->f)
2238 && !WINDOW_RIGHTMOST_P (it->w))
2239 it->last_visible_x -= 1;
2240
2241 it->last_visible_y = window_text_bottom_y (w);
2242
2243 /* For mode lines and alike, arrange for the first glyph having a
2244 left box line if the face specifies a box. */
2245 if (base_face_id != DEFAULT_FACE_ID)
2246 {
2247 struct face *face;
2248
2249 it->face_id = base_face_id;
2250
2251 /* If we have a boxed mode line, make the first character appear
2252 with a left box line. */
2253 face = FACE_FROM_ID (it->f, base_face_id);
2254 if (face->box != FACE_NO_BOX)
2255 it->start_of_box_run_p = 1;
2256 }
2257
2258 /* If a buffer position was specified, set the iterator there,
2259 getting overlays and face properties from that position. */
2260 if (charpos >= BUF_BEG (current_buffer))
2261 {
2262 it->end_charpos = ZV;
2263 it->face_id = -1;
2264 IT_CHARPOS (*it) = charpos;
2265
2266 /* Compute byte position if not specified. */
2267 if (bytepos < charpos)
2268 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
2269 else
2270 IT_BYTEPOS (*it) = bytepos;
2271
2272 it->start = it->current;
2273
2274 /* Compute faces etc. */
2275 reseat (it, it->current.pos, 1);
2276 }
2277
2278 CHECK_IT (it);
2279 }
2280
2281
2282 /* Initialize IT for the display of window W with window start POS. */
2283
2284 void
2285 start_display (it, w, pos)
2286 struct it *it;
2287 struct window *w;
2288 struct text_pos pos;
2289 {
2290 struct glyph_row *row;
2291 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2292
2293 row = w->desired_matrix->rows + first_vpos;
2294 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2295 it->first_vpos = first_vpos;
2296
2297 if (!it->truncate_lines_p)
2298 {
2299 int start_at_line_beg_p;
2300 int first_y = it->current_y;
2301
2302 /* If window start is not at a line start, skip forward to POS to
2303 get the correct continuation lines width. */
2304 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2305 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2306 if (!start_at_line_beg_p)
2307 {
2308 int new_x;
2309
2310 reseat_at_previous_visible_line_start (it);
2311 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
2312
2313 new_x = it->current_x + it->pixel_width;
2314
2315 /* If lines are continued, this line may end in the middle
2316 of a multi-glyph character (e.g. a control character
2317 displayed as \003, or in the middle of an overlay
2318 string). In this case move_it_to above will not have
2319 taken us to the start of the continuation line but to the
2320 end of the continued line. */
2321 if (it->current_x > 0
2322 && !it->truncate_lines_p /* Lines are continued. */
2323 && (/* And glyph doesn't fit on the line. */
2324 new_x > it->last_visible_x
2325 /* Or it fits exactly and we're on a window
2326 system frame. */
2327 || (new_x == it->last_visible_x
2328 && FRAME_WINDOW_P (it->f))))
2329 {
2330 if (it->current.dpvec_index >= 0
2331 || it->current.overlay_string_index >= 0)
2332 {
2333 set_iterator_to_next (it, 1);
2334 move_it_in_display_line_to (it, -1, -1, 0);
2335 }
2336
2337 it->continuation_lines_width += it->current_x;
2338 }
2339
2340 /* We're starting a new display line, not affected by the
2341 height of the continued line, so clear the appropriate
2342 fields in the iterator structure. */
2343 it->max_ascent = it->max_descent = 0;
2344 it->max_phys_ascent = it->max_phys_descent = 0;
2345
2346 it->current_y = first_y;
2347 it->vpos = 0;
2348 it->current_x = it->hpos = 0;
2349 }
2350 }
2351
2352 #if 0 /* Don't assert the following because start_display is sometimes
2353 called intentionally with a window start that is not at a
2354 line start. Please leave this code in as a comment. */
2355
2356 /* Window start should be on a line start, now. */
2357 xassert (it->continuation_lines_width
2358 || IT_CHARPOS (it) == BEGV
2359 || FETCH_BYTE (IT_BYTEPOS (it) - 1) == '\n');
2360 #endif /* 0 */
2361 }
2362
2363
2364 /* Return 1 if POS is a position in ellipses displayed for invisible
2365 text. W is the window we display, for text property lookup. */
2366
2367 static int
2368 in_ellipses_for_invisible_text_p (pos, w)
2369 struct display_pos *pos;
2370 struct window *w;
2371 {
2372 Lisp_Object prop, window;
2373 int ellipses_p = 0;
2374 int charpos = CHARPOS (pos->pos);
2375
2376 /* If POS specifies a position in a display vector, this might
2377 be for an ellipsis displayed for invisible text. We won't
2378 get the iterator set up for delivering that ellipsis unless
2379 we make sure that it gets aware of the invisible text. */
2380 if (pos->dpvec_index >= 0
2381 && pos->overlay_string_index < 0
2382 && CHARPOS (pos->string_pos) < 0
2383 && charpos > BEGV
2384 && (XSETWINDOW (window, w),
2385 prop = Fget_char_property (make_number (charpos),
2386 Qinvisible, window),
2387 !TEXT_PROP_MEANS_INVISIBLE (prop)))
2388 {
2389 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
2390 window);
2391 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
2392 }
2393
2394 return ellipses_p;
2395 }
2396
2397
2398 /* Initialize IT for stepping through current_buffer in window W,
2399 starting at position POS that includes overlay string and display
2400 vector/ control character translation position information. Value
2401 is zero if there are overlay strings with newlines at POS. */
2402
2403 static int
2404 init_from_display_pos (it, w, pos)
2405 struct it *it;
2406 struct window *w;
2407 struct display_pos *pos;
2408 {
2409 int charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
2410 int i, overlay_strings_with_newlines = 0;
2411
2412 /* If POS specifies a position in a display vector, this might
2413 be for an ellipsis displayed for invisible text. We won't
2414 get the iterator set up for delivering that ellipsis unless
2415 we make sure that it gets aware of the invisible text. */
2416 if (in_ellipses_for_invisible_text_p (pos, w))
2417 {
2418 --charpos;
2419 bytepos = 0;
2420 }
2421
2422 /* Keep in mind: the call to reseat in init_iterator skips invisible
2423 text, so we might end up at a position different from POS. This
2424 is only a problem when POS is a row start after a newline and an
2425 overlay starts there with an after-string, and the overlay has an
2426 invisible property. Since we don't skip invisible text in
2427 display_line and elsewhere immediately after consuming the
2428 newline before the row start, such a POS will not be in a string,
2429 but the call to init_iterator below will move us to the
2430 after-string. */
2431 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
2432
2433 for (i = 0; i < it->n_overlay_strings; ++i)
2434 {
2435 const char *s = SDATA (it->overlay_strings[i]);
2436 const char *e = s + SBYTES (it->overlay_strings[i]);
2437
2438 while (s < e && *s != '\n')
2439 ++s;
2440
2441 if (s < e)
2442 {
2443 overlay_strings_with_newlines = 1;
2444 break;
2445 }
2446 }
2447
2448 /* If position is within an overlay string, set up IT to the right
2449 overlay string. */
2450 if (pos->overlay_string_index >= 0)
2451 {
2452 int relative_index;
2453
2454 /* If the first overlay string happens to have a `display'
2455 property for an image, the iterator will be set up for that
2456 image, and we have to undo that setup first before we can
2457 correct the overlay string index. */
2458 if (it->method == next_element_from_image)
2459 pop_it (it);
2460
2461 /* We already have the first chunk of overlay strings in
2462 IT->overlay_strings. Load more until the one for
2463 pos->overlay_string_index is in IT->overlay_strings. */
2464 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
2465 {
2466 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
2467 it->current.overlay_string_index = 0;
2468 while (n--)
2469 {
2470 load_overlay_strings (it, 0);
2471 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
2472 }
2473 }
2474
2475 it->current.overlay_string_index = pos->overlay_string_index;
2476 relative_index = (it->current.overlay_string_index
2477 % OVERLAY_STRING_CHUNK_SIZE);
2478 it->string = it->overlay_strings[relative_index];
2479 xassert (STRINGP (it->string));
2480 it->current.string_pos = pos->string_pos;
2481 it->method = next_element_from_string;
2482 }
2483
2484 #if 0 /* This is bogus because POS not having an overlay string
2485 position does not mean it's after the string. Example: A
2486 line starting with a before-string and initialization of IT
2487 to the previous row's end position. */
2488 else if (it->current.overlay_string_index >= 0)
2489 {
2490 /* If POS says we're already after an overlay string ending at
2491 POS, make sure to pop the iterator because it will be in
2492 front of that overlay string. When POS is ZV, we've thereby
2493 also ``processed'' overlay strings at ZV. */
2494 while (it->sp)
2495 pop_it (it);
2496 it->current.overlay_string_index = -1;
2497 it->method = next_element_from_buffer;
2498 if (CHARPOS (pos->pos) == ZV)
2499 it->overlay_strings_at_end_processed_p = 1;
2500 }
2501 #endif /* 0 */
2502
2503 if (CHARPOS (pos->string_pos) >= 0)
2504 {
2505 /* Recorded position is not in an overlay string, but in another
2506 string. This can only be a string from a `display' property.
2507 IT should already be filled with that string. */
2508 it->current.string_pos = pos->string_pos;
2509 xassert (STRINGP (it->string));
2510 }
2511
2512 /* Restore position in display vector translations, control
2513 character translations or ellipses. */
2514 if (pos->dpvec_index >= 0)
2515 {
2516 if (it->dpvec == NULL)
2517 get_next_display_element (it);
2518 xassert (it->dpvec && it->current.dpvec_index == 0);
2519 it->current.dpvec_index = pos->dpvec_index;
2520 }
2521
2522 CHECK_IT (it);
2523 return !overlay_strings_with_newlines;
2524 }
2525
2526
2527 /* Initialize IT for stepping through current_buffer in window W
2528 starting at ROW->start. */
2529
2530 static void
2531 init_to_row_start (it, w, row)
2532 struct it *it;
2533 struct window *w;
2534 struct glyph_row *row;
2535 {
2536 init_from_display_pos (it, w, &row->start);
2537 it->start = row->start;
2538 it->continuation_lines_width = row->continuation_lines_width;
2539 CHECK_IT (it);
2540 }
2541
2542
2543 /* Initialize IT for stepping through current_buffer in window W
2544 starting in the line following ROW, i.e. starting at ROW->end.
2545 Value is zero if there are overlay strings with newlines at ROW's
2546 end position. */
2547
2548 static int
2549 init_to_row_end (it, w, row)
2550 struct it *it;
2551 struct window *w;
2552 struct glyph_row *row;
2553 {
2554 int success = 0;
2555
2556 if (init_from_display_pos (it, w, &row->end))
2557 {
2558 if (row->continued_p)
2559 it->continuation_lines_width
2560 = row->continuation_lines_width + row->pixel_width;
2561 CHECK_IT (it);
2562 success = 1;
2563 }
2564
2565 return success;
2566 }
2567
2568
2569
2570 \f
2571 /***********************************************************************
2572 Text properties
2573 ***********************************************************************/
2574
2575 /* Called when IT reaches IT->stop_charpos. Handle text property and
2576 overlay changes. Set IT->stop_charpos to the next position where
2577 to stop. */
2578
2579 static void
2580 handle_stop (it)
2581 struct it *it;
2582 {
2583 enum prop_handled handled;
2584 int handle_overlay_change_p = 1;
2585 struct props *p;
2586
2587 it->dpvec = NULL;
2588 it->current.dpvec_index = -1;
2589
2590 do
2591 {
2592 handled = HANDLED_NORMALLY;
2593
2594 /* Call text property handlers. */
2595 for (p = it_props; p->handler; ++p)
2596 {
2597 handled = p->handler (it);
2598
2599 if (handled == HANDLED_RECOMPUTE_PROPS)
2600 break;
2601 else if (handled == HANDLED_RETURN)
2602 return;
2603 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
2604 handle_overlay_change_p = 0;
2605 }
2606
2607 if (handled != HANDLED_RECOMPUTE_PROPS)
2608 {
2609 /* Don't check for overlay strings below when set to deliver
2610 characters from a display vector. */
2611 if (it->method == next_element_from_display_vector)
2612 handle_overlay_change_p = 0;
2613
2614 /* Handle overlay changes. */
2615 if (handle_overlay_change_p)
2616 handled = handle_overlay_change (it);
2617
2618 /* Determine where to stop next. */
2619 if (handled == HANDLED_NORMALLY)
2620 compute_stop_pos (it);
2621 }
2622 }
2623 while (handled == HANDLED_RECOMPUTE_PROPS);
2624 }
2625
2626
2627 /* Compute IT->stop_charpos from text property and overlay change
2628 information for IT's current position. */
2629
2630 static void
2631 compute_stop_pos (it)
2632 struct it *it;
2633 {
2634 register INTERVAL iv, next_iv;
2635 Lisp_Object object, limit, position;
2636
2637 /* If nowhere else, stop at the end. */
2638 it->stop_charpos = it->end_charpos;
2639
2640 if (STRINGP (it->string))
2641 {
2642 /* Strings are usually short, so don't limit the search for
2643 properties. */
2644 object = it->string;
2645 limit = Qnil;
2646 position = make_number (IT_STRING_CHARPOS (*it));
2647 }
2648 else
2649 {
2650 int charpos;
2651
2652 /* If next overlay change is in front of the current stop pos
2653 (which is IT->end_charpos), stop there. Note: value of
2654 next_overlay_change is point-max if no overlay change
2655 follows. */
2656 charpos = next_overlay_change (IT_CHARPOS (*it));
2657 if (charpos < it->stop_charpos)
2658 it->stop_charpos = charpos;
2659
2660 /* If showing the region, we have to stop at the region
2661 start or end because the face might change there. */
2662 if (it->region_beg_charpos > 0)
2663 {
2664 if (IT_CHARPOS (*it) < it->region_beg_charpos)
2665 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
2666 else if (IT_CHARPOS (*it) < it->region_end_charpos)
2667 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
2668 }
2669
2670 /* Set up variables for computing the stop position from text
2671 property changes. */
2672 XSETBUFFER (object, current_buffer);
2673 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
2674 position = make_number (IT_CHARPOS (*it));
2675
2676 }
2677
2678 /* Get the interval containing IT's position. Value is a null
2679 interval if there isn't such an interval. */
2680 iv = validate_interval_range (object, &position, &position, 0);
2681 if (!NULL_INTERVAL_P (iv))
2682 {
2683 Lisp_Object values_here[LAST_PROP_IDX];
2684 struct props *p;
2685
2686 /* Get properties here. */
2687 for (p = it_props; p->handler; ++p)
2688 values_here[p->idx] = textget (iv->plist, *p->name);
2689
2690 /* Look for an interval following iv that has different
2691 properties. */
2692 for (next_iv = next_interval (iv);
2693 (!NULL_INTERVAL_P (next_iv)
2694 && (NILP (limit)
2695 || XFASTINT (limit) > next_iv->position));
2696 next_iv = next_interval (next_iv))
2697 {
2698 for (p = it_props; p->handler; ++p)
2699 {
2700 Lisp_Object new_value;
2701
2702 new_value = textget (next_iv->plist, *p->name);
2703 if (!EQ (values_here[p->idx], new_value))
2704 break;
2705 }
2706
2707 if (p->handler)
2708 break;
2709 }
2710
2711 if (!NULL_INTERVAL_P (next_iv))
2712 {
2713 if (INTEGERP (limit)
2714 && next_iv->position >= XFASTINT (limit))
2715 /* No text property change up to limit. */
2716 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
2717 else
2718 /* Text properties change in next_iv. */
2719 it->stop_charpos = min (it->stop_charpos, next_iv->position);
2720 }
2721 }
2722
2723 xassert (STRINGP (it->string)
2724 || (it->stop_charpos >= BEGV
2725 && it->stop_charpos >= IT_CHARPOS (*it)));
2726 }
2727
2728
2729 /* Return the position of the next overlay change after POS in
2730 current_buffer. Value is point-max if no overlay change
2731 follows. This is like `next-overlay-change' but doesn't use
2732 xmalloc. */
2733
2734 static int
2735 next_overlay_change (pos)
2736 int pos;
2737 {
2738 int noverlays;
2739 int endpos;
2740 Lisp_Object *overlays;
2741 int i;
2742
2743 /* Get all overlays at the given position. */
2744 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
2745
2746 /* If any of these overlays ends before endpos,
2747 use its ending point instead. */
2748 for (i = 0; i < noverlays; ++i)
2749 {
2750 Lisp_Object oend;
2751 int oendpos;
2752
2753 oend = OVERLAY_END (overlays[i]);
2754 oendpos = OVERLAY_POSITION (oend);
2755 endpos = min (endpos, oendpos);
2756 }
2757
2758 return endpos;
2759 }
2760
2761
2762 \f
2763 /***********************************************************************
2764 Fontification
2765 ***********************************************************************/
2766
2767 /* Handle changes in the `fontified' property of the current buffer by
2768 calling hook functions from Qfontification_functions to fontify
2769 regions of text. */
2770
2771 static enum prop_handled
2772 handle_fontified_prop (it)
2773 struct it *it;
2774 {
2775 Lisp_Object prop, pos;
2776 enum prop_handled handled = HANDLED_NORMALLY;
2777
2778 /* Get the value of the `fontified' property at IT's current buffer
2779 position. (The `fontified' property doesn't have a special
2780 meaning in strings.) If the value is nil, call functions from
2781 Qfontification_functions. */
2782 if (!STRINGP (it->string)
2783 && it->s == NULL
2784 && !NILP (Vfontification_functions)
2785 && !NILP (Vrun_hooks)
2786 && (pos = make_number (IT_CHARPOS (*it)),
2787 prop = Fget_char_property (pos, Qfontified, Qnil),
2788 NILP (prop)))
2789 {
2790 int count = SPECPDL_INDEX ();
2791 Lisp_Object val;
2792
2793 val = Vfontification_functions;
2794 specbind (Qfontification_functions, Qnil);
2795
2796 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
2797 safe_call1 (val, pos);
2798 else
2799 {
2800 Lisp_Object globals, fn;
2801 struct gcpro gcpro1, gcpro2;
2802
2803 globals = Qnil;
2804 GCPRO2 (val, globals);
2805
2806 for (; CONSP (val); val = XCDR (val))
2807 {
2808 fn = XCAR (val);
2809
2810 if (EQ (fn, Qt))
2811 {
2812 /* A value of t indicates this hook has a local
2813 binding; it means to run the global binding too.
2814 In a global value, t should not occur. If it
2815 does, we must ignore it to avoid an endless
2816 loop. */
2817 for (globals = Fdefault_value (Qfontification_functions);
2818 CONSP (globals);
2819 globals = XCDR (globals))
2820 {
2821 fn = XCAR (globals);
2822 if (!EQ (fn, Qt))
2823 safe_call1 (fn, pos);
2824 }
2825 }
2826 else
2827 safe_call1 (fn, pos);
2828 }
2829
2830 UNGCPRO;
2831 }
2832
2833 unbind_to (count, Qnil);
2834
2835 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
2836 something. This avoids an endless loop if they failed to
2837 fontify the text for which reason ever. */
2838 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
2839 handled = HANDLED_RECOMPUTE_PROPS;
2840 }
2841
2842 return handled;
2843 }
2844
2845
2846 \f
2847 /***********************************************************************
2848 Faces
2849 ***********************************************************************/
2850
2851 /* Set up iterator IT from face properties at its current position.
2852 Called from handle_stop. */
2853
2854 static enum prop_handled
2855 handle_face_prop (it)
2856 struct it *it;
2857 {
2858 int new_face_id, next_stop;
2859
2860 if (!STRINGP (it->string))
2861 {
2862 new_face_id
2863 = face_at_buffer_position (it->w,
2864 IT_CHARPOS (*it),
2865 it->region_beg_charpos,
2866 it->region_end_charpos,
2867 &next_stop,
2868 (IT_CHARPOS (*it)
2869 + TEXT_PROP_DISTANCE_LIMIT),
2870 0);
2871
2872 /* Is this a start of a run of characters with box face?
2873 Caveat: this can be called for a freshly initialized
2874 iterator; face_id is -1 in this case. We know that the new
2875 face will not change until limit, i.e. if the new face has a
2876 box, all characters up to limit will have one. But, as
2877 usual, we don't know whether limit is really the end. */
2878 if (new_face_id != it->face_id)
2879 {
2880 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
2881
2882 /* If new face has a box but old face has not, this is
2883 the start of a run of characters with box, i.e. it has
2884 a shadow on the left side. The value of face_id of the
2885 iterator will be -1 if this is the initial call that gets
2886 the face. In this case, we have to look in front of IT's
2887 position and see whether there is a face != new_face_id. */
2888 it->start_of_box_run_p
2889 = (new_face->box != FACE_NO_BOX
2890 && (it->face_id >= 0
2891 || IT_CHARPOS (*it) == BEG
2892 || new_face_id != face_before_it_pos (it)));
2893 it->face_box_p = new_face->box != FACE_NO_BOX;
2894 }
2895 }
2896 else
2897 {
2898 int base_face_id, bufpos;
2899
2900 if (it->current.overlay_string_index >= 0)
2901 bufpos = IT_CHARPOS (*it);
2902 else
2903 bufpos = 0;
2904
2905 /* For strings from a buffer, i.e. overlay strings or strings
2906 from a `display' property, use the face at IT's current
2907 buffer position as the base face to merge with, so that
2908 overlay strings appear in the same face as surrounding
2909 text, unless they specify their own faces. */
2910 base_face_id = underlying_face_id (it);
2911
2912 new_face_id = face_at_string_position (it->w,
2913 it->string,
2914 IT_STRING_CHARPOS (*it),
2915 bufpos,
2916 it->region_beg_charpos,
2917 it->region_end_charpos,
2918 &next_stop,
2919 base_face_id, 0);
2920
2921 #if 0 /* This shouldn't be neccessary. Let's check it. */
2922 /* If IT is used to display a mode line we would really like to
2923 use the mode line face instead of the frame's default face. */
2924 if (it->glyph_row == MATRIX_MODE_LINE_ROW (it->w->desired_matrix)
2925 && new_face_id == DEFAULT_FACE_ID)
2926 new_face_id = CURRENT_MODE_LINE_FACE_ID (it->w);
2927 #endif
2928
2929 /* Is this a start of a run of characters with box? Caveat:
2930 this can be called for a freshly allocated iterator; face_id
2931 is -1 is this case. We know that the new face will not
2932 change until the next check pos, i.e. if the new face has a
2933 box, all characters up to that position will have a
2934 box. But, as usual, we don't know whether that position
2935 is really the end. */
2936 if (new_face_id != it->face_id)
2937 {
2938 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
2939 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
2940
2941 /* If new face has a box but old face hasn't, this is the
2942 start of a run of characters with box, i.e. it has a
2943 shadow on the left side. */
2944 it->start_of_box_run_p
2945 = new_face->box && (old_face == NULL || !old_face->box);
2946 it->face_box_p = new_face->box != FACE_NO_BOX;
2947 }
2948 }
2949
2950 it->face_id = new_face_id;
2951 return HANDLED_NORMALLY;
2952 }
2953
2954
2955 /* Return the ID of the face ``underlying'' IT's current position,
2956 which is in a string. If the iterator is associated with a
2957 buffer, return the face at IT's current buffer position.
2958 Otherwise, use the iterator's base_face_id. */
2959
2960 static int
2961 underlying_face_id (it)
2962 struct it *it;
2963 {
2964 int face_id = it->base_face_id, i;
2965
2966 xassert (STRINGP (it->string));
2967
2968 for (i = it->sp - 1; i >= 0; --i)
2969 if (NILP (it->stack[i].string))
2970 face_id = it->stack[i].face_id;
2971
2972 return face_id;
2973 }
2974
2975
2976 /* Compute the face one character before or after the current position
2977 of IT. BEFORE_P non-zero means get the face in front of IT's
2978 position. Value is the id of the face. */
2979
2980 static int
2981 face_before_or_after_it_pos (it, before_p)
2982 struct it *it;
2983 int before_p;
2984 {
2985 int face_id, limit;
2986 int next_check_charpos;
2987 struct text_pos pos;
2988
2989 xassert (it->s == NULL);
2990
2991 if (STRINGP (it->string))
2992 {
2993 int bufpos, base_face_id;
2994
2995 /* No face change past the end of the string (for the case
2996 we are padding with spaces). No face change before the
2997 string start. */
2998 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
2999 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
3000 return it->face_id;
3001
3002 /* Set pos to the position before or after IT's current position. */
3003 if (before_p)
3004 pos = string_pos (IT_STRING_CHARPOS (*it) - 1, it->string);
3005 else
3006 /* For composition, we must check the character after the
3007 composition. */
3008 pos = (it->what == IT_COMPOSITION
3009 ? string_pos (IT_STRING_CHARPOS (*it) + it->cmp_len, it->string)
3010 : string_pos (IT_STRING_CHARPOS (*it) + 1, it->string));
3011
3012 if (it->current.overlay_string_index >= 0)
3013 bufpos = IT_CHARPOS (*it);
3014 else
3015 bufpos = 0;
3016
3017 base_face_id = underlying_face_id (it);
3018
3019 /* Get the face for ASCII, or unibyte. */
3020 face_id = face_at_string_position (it->w,
3021 it->string,
3022 CHARPOS (pos),
3023 bufpos,
3024 it->region_beg_charpos,
3025 it->region_end_charpos,
3026 &next_check_charpos,
3027 base_face_id, 0);
3028
3029 /* Correct the face for charsets different from ASCII. Do it
3030 for the multibyte case only. The face returned above is
3031 suitable for unibyte text if IT->string is unibyte. */
3032 if (STRING_MULTIBYTE (it->string))
3033 {
3034 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos);
3035 int rest = SBYTES (it->string) - BYTEPOS (pos);
3036 int c, len;
3037 struct face *face = FACE_FROM_ID (it->f, face_id);
3038
3039 c = string_char_and_length (p, rest, &len);
3040 face_id = FACE_FOR_CHAR (it->f, face, c);
3041 }
3042 }
3043 else
3044 {
3045 if ((IT_CHARPOS (*it) >= ZV && !before_p)
3046 || (IT_CHARPOS (*it) <= BEGV && before_p))
3047 return it->face_id;
3048
3049 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
3050 pos = it->current.pos;
3051
3052 if (before_p)
3053 DEC_TEXT_POS (pos, it->multibyte_p);
3054 else
3055 {
3056 if (it->what == IT_COMPOSITION)
3057 /* For composition, we must check the position after the
3058 composition. */
3059 pos.charpos += it->cmp_len, pos.bytepos += it->len;
3060 else
3061 INC_TEXT_POS (pos, it->multibyte_p);
3062 }
3063
3064 /* Determine face for CHARSET_ASCII, or unibyte. */
3065 face_id = face_at_buffer_position (it->w,
3066 CHARPOS (pos),
3067 it->region_beg_charpos,
3068 it->region_end_charpos,
3069 &next_check_charpos,
3070 limit, 0);
3071
3072 /* Correct the face for charsets different from ASCII. Do it
3073 for the multibyte case only. The face returned above is
3074 suitable for unibyte text if current_buffer is unibyte. */
3075 if (it->multibyte_p)
3076 {
3077 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
3078 struct face *face = FACE_FROM_ID (it->f, face_id);
3079 face_id = FACE_FOR_CHAR (it->f, face, c);
3080 }
3081 }
3082
3083 return face_id;
3084 }
3085
3086
3087 \f
3088 /***********************************************************************
3089 Invisible text
3090 ***********************************************************************/
3091
3092 /* Set up iterator IT from invisible properties at its current
3093 position. Called from handle_stop. */
3094
3095 static enum prop_handled
3096 handle_invisible_prop (it)
3097 struct it *it;
3098 {
3099 enum prop_handled handled = HANDLED_NORMALLY;
3100
3101 if (STRINGP (it->string))
3102 {
3103 extern Lisp_Object Qinvisible;
3104 Lisp_Object prop, end_charpos, limit, charpos;
3105
3106 /* Get the value of the invisible text property at the
3107 current position. Value will be nil if there is no such
3108 property. */
3109 charpos = make_number (IT_STRING_CHARPOS (*it));
3110 prop = Fget_text_property (charpos, Qinvisible, it->string);
3111
3112 if (!NILP (prop)
3113 && IT_STRING_CHARPOS (*it) < it->end_charpos)
3114 {
3115 handled = HANDLED_RECOMPUTE_PROPS;
3116
3117 /* Get the position at which the next change of the
3118 invisible text property can be found in IT->string.
3119 Value will be nil if the property value is the same for
3120 all the rest of IT->string. */
3121 XSETINT (limit, SCHARS (it->string));
3122 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
3123 it->string, limit);
3124
3125 /* Text at current position is invisible. The next
3126 change in the property is at position end_charpos.
3127 Move IT's current position to that position. */
3128 if (INTEGERP (end_charpos)
3129 && XFASTINT (end_charpos) < XFASTINT (limit))
3130 {
3131 struct text_pos old;
3132 old = it->current.string_pos;
3133 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
3134 compute_string_pos (&it->current.string_pos, old, it->string);
3135 }
3136 else
3137 {
3138 /* The rest of the string is invisible. If this is an
3139 overlay string, proceed with the next overlay string
3140 or whatever comes and return a character from there. */
3141 if (it->current.overlay_string_index >= 0)
3142 {
3143 next_overlay_string (it);
3144 /* Don't check for overlay strings when we just
3145 finished processing them. */
3146 handled = HANDLED_OVERLAY_STRING_CONSUMED;
3147 }
3148 else
3149 {
3150 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
3151 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
3152 }
3153 }
3154 }
3155 }
3156 else
3157 {
3158 int invis_p, newpos, next_stop, start_charpos;
3159 Lisp_Object pos, prop, overlay;
3160
3161 /* First of all, is there invisible text at this position? */
3162 start_charpos = IT_CHARPOS (*it);
3163 pos = make_number (IT_CHARPOS (*it));
3164 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
3165 &overlay);
3166 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3167
3168 /* If we are on invisible text, skip over it. */
3169 if (invis_p && IT_CHARPOS (*it) < it->end_charpos)
3170 {
3171 /* Record whether we have to display an ellipsis for the
3172 invisible text. */
3173 int display_ellipsis_p = invis_p == 2;
3174
3175 handled = HANDLED_RECOMPUTE_PROPS;
3176
3177 /* Loop skipping over invisible text. The loop is left at
3178 ZV or with IT on the first char being visible again. */
3179 do
3180 {
3181 /* Try to skip some invisible text. Return value is the
3182 position reached which can be equal to IT's position
3183 if there is nothing invisible here. This skips both
3184 over invisible text properties and overlays with
3185 invisible property. */
3186 newpos = skip_invisible (IT_CHARPOS (*it),
3187 &next_stop, ZV, it->window);
3188
3189 /* If we skipped nothing at all we weren't at invisible
3190 text in the first place. If everything to the end of
3191 the buffer was skipped, end the loop. */
3192 if (newpos == IT_CHARPOS (*it) || newpos >= ZV)
3193 invis_p = 0;
3194 else
3195 {
3196 /* We skipped some characters but not necessarily
3197 all there are. Check if we ended up on visible
3198 text. Fget_char_property returns the property of
3199 the char before the given position, i.e. if we
3200 get invis_p = 0, this means that the char at
3201 newpos is visible. */
3202 pos = make_number (newpos);
3203 prop = Fget_char_property (pos, Qinvisible, it->window);
3204 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3205 }
3206
3207 /* If we ended up on invisible text, proceed to
3208 skip starting with next_stop. */
3209 if (invis_p)
3210 IT_CHARPOS (*it) = next_stop;
3211 }
3212 while (invis_p);
3213
3214 /* The position newpos is now either ZV or on visible text. */
3215 IT_CHARPOS (*it) = newpos;
3216 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
3217
3218 /* If there are before-strings at the start of invisible
3219 text, and the text is invisible because of a text
3220 property, arrange to show before-strings because 20.x did
3221 it that way. (If the text is invisible because of an
3222 overlay property instead of a text property, this is
3223 already handled in the overlay code.) */
3224 if (NILP (overlay)
3225 && get_overlay_strings (it, start_charpos))
3226 {
3227 handled = HANDLED_RECOMPUTE_PROPS;
3228 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
3229 }
3230 else if (display_ellipsis_p)
3231 setup_for_ellipsis (it, 0);
3232 }
3233 }
3234
3235 return handled;
3236 }
3237
3238
3239 /* Make iterator IT return `...' next.
3240 Replaces LEN characters from buffer. */
3241
3242 static void
3243 setup_for_ellipsis (it, len)
3244 struct it *it;
3245 int len;
3246 {
3247 /* Use the display table definition for `...'. Invalid glyphs
3248 will be handled by the method returning elements from dpvec. */
3249 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
3250 {
3251 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
3252 it->dpvec = v->contents;
3253 it->dpend = v->contents + v->size;
3254 }
3255 else
3256 {
3257 /* Default `...'. */
3258 it->dpvec = default_invis_vector;
3259 it->dpend = default_invis_vector + 3;
3260 }
3261
3262 it->dpvec_char_len = len;
3263 it->current.dpvec_index = 0;
3264
3265 /* Remember the current face id in case glyphs specify faces.
3266 IT's face is restored in set_iterator_to_next. */
3267 it->saved_face_id = it->face_id;
3268 it->method = next_element_from_display_vector;
3269 }
3270
3271
3272 \f
3273 /***********************************************************************
3274 'display' property
3275 ***********************************************************************/
3276
3277 /* Set up iterator IT from `display' property at its current position.
3278 Called from handle_stop. */
3279
3280 static enum prop_handled
3281 handle_display_prop (it)
3282 struct it *it;
3283 {
3284 Lisp_Object prop, object;
3285 struct text_pos *position;
3286 int display_replaced_p = 0;
3287
3288 if (STRINGP (it->string))
3289 {
3290 object = it->string;
3291 position = &it->current.string_pos;
3292 }
3293 else
3294 {
3295 object = it->w->buffer;
3296 position = &it->current.pos;
3297 }
3298
3299 /* Reset those iterator values set from display property values. */
3300 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
3301 it->space_width = Qnil;
3302 it->font_height = Qnil;
3303 it->voffset = 0;
3304
3305 /* We don't support recursive `display' properties, i.e. string
3306 values that have a string `display' property, that have a string
3307 `display' property etc. */
3308 if (!it->string_from_display_prop_p)
3309 it->area = TEXT_AREA;
3310
3311 prop = Fget_char_property (make_number (position->charpos),
3312 Qdisplay, object);
3313 if (NILP (prop))
3314 return HANDLED_NORMALLY;
3315
3316 if (CONSP (prop)
3317 /* Simple properties. */
3318 && !EQ (XCAR (prop), Qimage)
3319 && !EQ (XCAR (prop), Qspace)
3320 && !EQ (XCAR (prop), Qwhen)
3321 && !EQ (XCAR (prop), Qslice)
3322 && !EQ (XCAR (prop), Qspace_width)
3323 && !EQ (XCAR (prop), Qheight)
3324 && !EQ (XCAR (prop), Qraise)
3325 /* Marginal area specifications. */
3326 && !(CONSP (XCAR (prop)) && EQ (XCAR (XCAR (prop)), Qmargin))
3327 && !EQ (XCAR (prop), Qleft_fringe)
3328 && !EQ (XCAR (prop), Qright_fringe)
3329 && !NILP (XCAR (prop)))
3330 {
3331 for (; CONSP (prop); prop = XCDR (prop))
3332 {
3333 if (handle_single_display_prop (it, XCAR (prop), object,
3334 position, display_replaced_p))
3335 display_replaced_p = 1;
3336 }
3337 }
3338 else if (VECTORP (prop))
3339 {
3340 int i;
3341 for (i = 0; i < ASIZE (prop); ++i)
3342 if (handle_single_display_prop (it, AREF (prop, i), object,
3343 position, display_replaced_p))
3344 display_replaced_p = 1;
3345 }
3346 else
3347 {
3348 if (handle_single_display_prop (it, prop, object, position, 0))
3349 display_replaced_p = 1;
3350 }
3351
3352 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
3353 }
3354
3355
3356 /* Value is the position of the end of the `display' property starting
3357 at START_POS in OBJECT. */
3358
3359 static struct text_pos
3360 display_prop_end (it, object, start_pos)
3361 struct it *it;
3362 Lisp_Object object;
3363 struct text_pos start_pos;
3364 {
3365 Lisp_Object end;
3366 struct text_pos end_pos;
3367
3368 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
3369 Qdisplay, object, Qnil);
3370 CHARPOS (end_pos) = XFASTINT (end);
3371 if (STRINGP (object))
3372 compute_string_pos (&end_pos, start_pos, it->string);
3373 else
3374 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
3375
3376 return end_pos;
3377 }
3378
3379
3380 /* Set up IT from a single `display' sub-property value PROP. OBJECT
3381 is the object in which the `display' property was found. *POSITION
3382 is the position at which it was found. DISPLAY_REPLACED_P non-zero
3383 means that we previously saw a display sub-property which already
3384 replaced text display with something else, for example an image;
3385 ignore such properties after the first one has been processed.
3386
3387 If PROP is a `space' or `image' sub-property, set *POSITION to the
3388 end position of the `display' property.
3389
3390 Value is non-zero if something was found which replaces the display
3391 of buffer or string text. */
3392
3393 static int
3394 handle_single_display_prop (it, prop, object, position,
3395 display_replaced_before_p)
3396 struct it *it;
3397 Lisp_Object prop;
3398 Lisp_Object object;
3399 struct text_pos *position;
3400 int display_replaced_before_p;
3401 {
3402 Lisp_Object value;
3403 int replaces_text_display_p = 0;
3404 Lisp_Object form;
3405
3406 /* If PROP is a list of the form `(when FORM . VALUE)', FORM is
3407 evaluated. If the result is nil, VALUE is ignored. */
3408 form = Qt;
3409 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
3410 {
3411 prop = XCDR (prop);
3412 if (!CONSP (prop))
3413 return 0;
3414 form = XCAR (prop);
3415 prop = XCDR (prop);
3416 }
3417
3418 if (!NILP (form) && !EQ (form, Qt))
3419 {
3420 int count = SPECPDL_INDEX ();
3421 struct gcpro gcpro1;
3422
3423 /* Bind `object' to the object having the `display' property, a
3424 buffer or string. Bind `position' to the position in the
3425 object where the property was found, and `buffer-position'
3426 to the current position in the buffer. */
3427 specbind (Qobject, object);
3428 specbind (Qposition, make_number (CHARPOS (*position)));
3429 specbind (Qbuffer_position,
3430 make_number (STRINGP (object)
3431 ? IT_CHARPOS (*it) : CHARPOS (*position)));
3432 GCPRO1 (form);
3433 form = safe_eval (form);
3434 UNGCPRO;
3435 unbind_to (count, Qnil);
3436 }
3437
3438 if (NILP (form))
3439 return 0;
3440
3441 if (CONSP (prop)
3442 && EQ (XCAR (prop), Qheight)
3443 && CONSP (XCDR (prop)))
3444 {
3445 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3446 return 0;
3447
3448 /* `(height HEIGHT)'. */
3449 it->font_height = XCAR (XCDR (prop));
3450 if (!NILP (it->font_height))
3451 {
3452 struct face *face = FACE_FROM_ID (it->f, it->face_id);
3453 int new_height = -1;
3454
3455 if (CONSP (it->font_height)
3456 && (EQ (XCAR (it->font_height), Qplus)
3457 || EQ (XCAR (it->font_height), Qminus))
3458 && CONSP (XCDR (it->font_height))
3459 && INTEGERP (XCAR (XCDR (it->font_height))))
3460 {
3461 /* `(+ N)' or `(- N)' where N is an integer. */
3462 int steps = XINT (XCAR (XCDR (it->font_height)));
3463 if (EQ (XCAR (it->font_height), Qplus))
3464 steps = - steps;
3465 it->face_id = smaller_face (it->f, it->face_id, steps);
3466 }
3467 else if (FUNCTIONP (it->font_height))
3468 {
3469 /* Call function with current height as argument.
3470 Value is the new height. */
3471 Lisp_Object height;
3472 height = safe_call1 (it->font_height,
3473 face->lface[LFACE_HEIGHT_INDEX]);
3474 if (NUMBERP (height))
3475 new_height = XFLOATINT (height);
3476 }
3477 else if (NUMBERP (it->font_height))
3478 {
3479 /* Value is a multiple of the canonical char height. */
3480 struct face *face;
3481
3482 face = FACE_FROM_ID (it->f, DEFAULT_FACE_ID);
3483 new_height = (XFLOATINT (it->font_height)
3484 * XINT (face->lface[LFACE_HEIGHT_INDEX]));
3485 }
3486 else
3487 {
3488 /* Evaluate IT->font_height with `height' bound to the
3489 current specified height to get the new height. */
3490 Lisp_Object value;
3491 int count = SPECPDL_INDEX ();
3492
3493 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
3494 value = safe_eval (it->font_height);
3495 unbind_to (count, Qnil);
3496
3497 if (NUMBERP (value))
3498 new_height = XFLOATINT (value);
3499 }
3500
3501 if (new_height > 0)
3502 it->face_id = face_with_height (it->f, it->face_id, new_height);
3503 }
3504 }
3505 else if (CONSP (prop)
3506 && EQ (XCAR (prop), Qspace_width)
3507 && CONSP (XCDR (prop)))
3508 {
3509 /* `(space_width WIDTH)'. */
3510 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3511 return 0;
3512
3513 value = XCAR (XCDR (prop));
3514 if (NUMBERP (value) && XFLOATINT (value) > 0)
3515 it->space_width = value;
3516 }
3517 else if (CONSP (prop)
3518 && EQ (XCAR (prop), Qslice))
3519 {
3520 /* `(slice X Y WIDTH HEIGHT)'. */
3521 Lisp_Object tem;
3522
3523 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3524 return 0;
3525
3526 if (tem = XCDR (prop), CONSP (tem))
3527 {
3528 it->slice.x = XCAR (tem);
3529 if (tem = XCDR (tem), CONSP (tem))
3530 {
3531 it->slice.y = XCAR (tem);
3532 if (tem = XCDR (tem), CONSP (tem))
3533 {
3534 it->slice.width = XCAR (tem);
3535 if (tem = XCDR (tem), CONSP (tem))
3536 it->slice.height = XCAR (tem);
3537 }
3538 }
3539 }
3540 }
3541 else if (CONSP (prop)
3542 && EQ (XCAR (prop), Qraise)
3543 && CONSP (XCDR (prop)))
3544 {
3545 /* `(raise FACTOR)'. */
3546 if (!FRAME_WINDOW_P (it->f))
3547 return 0;
3548
3549 #ifdef HAVE_WINDOW_SYSTEM
3550 value = XCAR (XCDR (prop));
3551 if (NUMBERP (value))
3552 {
3553 struct face *face = FACE_FROM_ID (it->f, it->face_id);
3554 it->voffset = - (XFLOATINT (value)
3555 * (FONT_HEIGHT (face->font)));
3556 }
3557 #endif /* HAVE_WINDOW_SYSTEM */
3558 }
3559 else if (!it->string_from_display_prop_p)
3560 {
3561 /* `((margin left-margin) VALUE)' or `((margin right-margin)
3562 VALUE) or `((margin nil) VALUE)' or VALUE. */
3563 Lisp_Object location, value;
3564 struct text_pos start_pos;
3565 int valid_p;
3566
3567 /* Characters having this form of property are not displayed, so
3568 we have to find the end of the property. */
3569 start_pos = *position;
3570 *position = display_prop_end (it, object, start_pos);
3571 value = Qnil;
3572
3573 /* Let's stop at the new position and assume that all
3574 text properties change there. */
3575 it->stop_charpos = position->charpos;
3576
3577 if (CONSP (prop)
3578 && (EQ (XCAR (prop), Qleft_fringe)
3579 || EQ (XCAR (prop), Qright_fringe))
3580 && CONSP (XCDR (prop)))
3581 {
3582 int face_id = DEFAULT_FACE_ID;
3583 int fringe_bitmap;
3584
3585 /* Save current settings of IT so that we can restore them
3586 when we are finished with the glyph property value. */
3587
3588 /* `(left-fringe BITMAP FACE)'. */
3589 if (!FRAME_WINDOW_P (it->f))
3590 return 0;
3591
3592 #ifdef HAVE_WINDOW_SYSTEM
3593 value = XCAR (XCDR (prop));
3594 if (!SYMBOLP (value)
3595 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
3596 return 0;
3597
3598 if (CONSP (XCDR (XCDR (prop))))
3599 {
3600 Lisp_Object face_name = XCAR (XCDR (XCDR (prop)));
3601 int face_id2 = lookup_named_face (it->f, face_name, 'A', 0);
3602 if (face_id2 >= 0)
3603 face_id = face_id2;
3604 }
3605
3606 push_it (it);
3607
3608 it->area = TEXT_AREA;
3609 it->what = IT_IMAGE;
3610 it->image_id = -1; /* no image */
3611 it->position = start_pos;
3612 it->object = NILP (object) ? it->w->buffer : object;
3613 it->method = next_element_from_image;
3614 it->face_id = face_id;
3615
3616 /* Say that we haven't consumed the characters with
3617 `display' property yet. The call to pop_it in
3618 set_iterator_to_next will clean this up. */
3619 *position = start_pos;
3620
3621 if (EQ (XCAR (prop), Qleft_fringe))
3622 {
3623 it->left_user_fringe_bitmap = fringe_bitmap;
3624 it->left_user_fringe_face_id = face_id;
3625 }
3626 else
3627 {
3628 it->right_user_fringe_bitmap = fringe_bitmap;
3629 it->right_user_fringe_face_id = face_id;
3630 }
3631 #endif /* HAVE_WINDOW_SYSTEM */
3632 return 1;
3633 }
3634
3635 location = Qunbound;
3636 if (CONSP (prop) && CONSP (XCAR (prop)))
3637 {
3638 Lisp_Object tem;
3639
3640 value = XCDR (prop);
3641 if (CONSP (value))
3642 value = XCAR (value);
3643
3644 tem = XCAR (prop);
3645 if (EQ (XCAR (tem), Qmargin)
3646 && (tem = XCDR (tem),
3647 tem = CONSP (tem) ? XCAR (tem) : Qnil,
3648 (NILP (tem)
3649 || EQ (tem, Qleft_margin)
3650 || EQ (tem, Qright_margin))))
3651 location = tem;
3652 }
3653
3654 if (EQ (location, Qunbound))
3655 {
3656 location = Qnil;
3657 value = prop;
3658 }
3659
3660 valid_p = (STRINGP (value)
3661 #ifdef HAVE_WINDOW_SYSTEM
3662 || (FRAME_WINDOW_P (it->f) && valid_image_p (value))
3663 #endif /* not HAVE_WINDOW_SYSTEM */
3664 || (CONSP (value) && EQ (XCAR (value), Qspace)));
3665
3666 if ((EQ (location, Qleft_margin)
3667 || EQ (location, Qright_margin)
3668 || NILP (location))
3669 && valid_p
3670 && !display_replaced_before_p)
3671 {
3672 replaces_text_display_p = 1;
3673
3674 /* Save current settings of IT so that we can restore them
3675 when we are finished with the glyph property value. */
3676 push_it (it);
3677
3678 if (NILP (location))
3679 it->area = TEXT_AREA;
3680 else if (EQ (location, Qleft_margin))
3681 it->area = LEFT_MARGIN_AREA;
3682 else
3683 it->area = RIGHT_MARGIN_AREA;
3684
3685 if (STRINGP (value))
3686 {
3687 it->string = value;
3688 it->multibyte_p = STRING_MULTIBYTE (it->string);
3689 it->current.overlay_string_index = -1;
3690 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
3691 it->end_charpos = it->string_nchars = SCHARS (it->string);
3692 it->method = next_element_from_string;
3693 it->stop_charpos = 0;
3694 it->string_from_display_prop_p = 1;
3695 /* Say that we haven't consumed the characters with
3696 `display' property yet. The call to pop_it in
3697 set_iterator_to_next will clean this up. */
3698 *position = start_pos;
3699 }
3700 else if (CONSP (value) && EQ (XCAR (value), Qspace))
3701 {
3702 it->method = next_element_from_stretch;
3703 it->object = value;
3704 it->current.pos = it->position = start_pos;
3705 }
3706 #ifdef HAVE_WINDOW_SYSTEM
3707 else
3708 {
3709 if (FRAME_WINDOW_P (it->f))
3710 {
3711 it->what = IT_IMAGE;
3712 it->image_id = lookup_image (it->f, value);
3713 it->position = start_pos;
3714 it->object = NILP (object) ? it->w->buffer : object;
3715 it->method = next_element_from_image;
3716 }
3717
3718 /* Say that we haven't consumed the characters with
3719 `display' property yet. The call to pop_it in
3720 set_iterator_to_next will clean this up. */
3721 *position = start_pos;
3722 }
3723 #endif /* HAVE_WINDOW_SYSTEM */
3724 }
3725 else
3726 /* Invalid property or property not supported. Restore
3727 the position to what it was before. */
3728 *position = start_pos;
3729 }
3730
3731 return replaces_text_display_p;
3732 }
3733
3734
3735 /* Check if PROP is a display sub-property value whose text should be
3736 treated as intangible. */
3737
3738 static int
3739 single_display_prop_intangible_p (prop)
3740 Lisp_Object prop;
3741 {
3742 /* Skip over `when FORM'. */
3743 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
3744 {
3745 prop = XCDR (prop);
3746 if (!CONSP (prop))
3747 return 0;
3748 prop = XCDR (prop);
3749 }
3750
3751 if (STRINGP (prop))
3752 return 1;
3753
3754 if (!CONSP (prop))
3755 return 0;
3756
3757 /* Skip over `margin LOCATION'. If LOCATION is in the margins,
3758 we don't need to treat text as intangible. */
3759 if (EQ (XCAR (prop), Qmargin))
3760 {
3761 prop = XCDR (prop);
3762 if (!CONSP (prop))
3763 return 0;
3764
3765 prop = XCDR (prop);
3766 if (!CONSP (prop)
3767 || EQ (XCAR (prop), Qleft_margin)
3768 || EQ (XCAR (prop), Qright_margin))
3769 return 0;
3770 }
3771
3772 return (CONSP (prop)
3773 && (EQ (XCAR (prop), Qimage)
3774 || EQ (XCAR (prop), Qspace)));
3775 }
3776
3777
3778 /* Check if PROP is a display property value whose text should be
3779 treated as intangible. */
3780
3781 int
3782 display_prop_intangible_p (prop)
3783 Lisp_Object prop;
3784 {
3785 if (CONSP (prop)
3786 && CONSP (XCAR (prop))
3787 && !EQ (Qmargin, XCAR (XCAR (prop))))
3788 {
3789 /* A list of sub-properties. */
3790 while (CONSP (prop))
3791 {
3792 if (single_display_prop_intangible_p (XCAR (prop)))
3793 return 1;
3794 prop = XCDR (prop);
3795 }
3796 }
3797 else if (VECTORP (prop))
3798 {
3799 /* A vector of sub-properties. */
3800 int i;
3801 for (i = 0; i < ASIZE (prop); ++i)
3802 if (single_display_prop_intangible_p (AREF (prop, i)))
3803 return 1;
3804 }
3805 else
3806 return single_display_prop_intangible_p (prop);
3807
3808 return 0;
3809 }
3810
3811
3812 /* Return 1 if PROP is a display sub-property value containing STRING. */
3813
3814 static int
3815 single_display_prop_string_p (prop, string)
3816 Lisp_Object prop, string;
3817 {
3818 if (EQ (string, prop))
3819 return 1;
3820
3821 /* Skip over `when FORM'. */
3822 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
3823 {
3824 prop = XCDR (prop);
3825 if (!CONSP (prop))
3826 return 0;
3827 prop = XCDR (prop);
3828 }
3829
3830 if (CONSP (prop))
3831 /* Skip over `margin LOCATION'. */
3832 if (EQ (XCAR (prop), Qmargin))
3833 {
3834 prop = XCDR (prop);
3835 if (!CONSP (prop))
3836 return 0;
3837
3838 prop = XCDR (prop);
3839 if (!CONSP (prop))
3840 return 0;
3841 }
3842
3843 return CONSP (prop) && EQ (XCAR (prop), string);
3844 }
3845
3846
3847 /* Return 1 if STRING appears in the `display' property PROP. */
3848
3849 static int
3850 display_prop_string_p (prop, string)
3851 Lisp_Object prop, string;
3852 {
3853 if (CONSP (prop)
3854 && CONSP (XCAR (prop))
3855 && !EQ (Qmargin, XCAR (XCAR (prop))))
3856 {
3857 /* A list of sub-properties. */
3858 while (CONSP (prop))
3859 {
3860 if (single_display_prop_string_p (XCAR (prop), string))
3861 return 1;
3862 prop = XCDR (prop);
3863 }
3864 }
3865 else if (VECTORP (prop))
3866 {
3867 /* A vector of sub-properties. */
3868 int i;
3869 for (i = 0; i < ASIZE (prop); ++i)
3870 if (single_display_prop_string_p (AREF (prop, i), string))
3871 return 1;
3872 }
3873 else
3874 return single_display_prop_string_p (prop, string);
3875
3876 return 0;
3877 }
3878
3879
3880 /* Determine from which buffer position in W's buffer STRING comes
3881 from. AROUND_CHARPOS is an approximate position where it could
3882 be from. Value is the buffer position or 0 if it couldn't be
3883 determined.
3884
3885 W's buffer must be current.
3886
3887 This function is necessary because we don't record buffer positions
3888 in glyphs generated from strings (to keep struct glyph small).
3889 This function may only use code that doesn't eval because it is
3890 called asynchronously from note_mouse_highlight. */
3891
3892 int
3893 string_buffer_position (w, string, around_charpos)
3894 struct window *w;
3895 Lisp_Object string;
3896 int around_charpos;
3897 {
3898 Lisp_Object limit, prop, pos;
3899 const int MAX_DISTANCE = 1000;
3900 int found = 0;
3901
3902 pos = make_number (around_charpos);
3903 limit = make_number (min (XINT (pos) + MAX_DISTANCE, ZV));
3904 while (!found && !EQ (pos, limit))
3905 {
3906 prop = Fget_char_property (pos, Qdisplay, Qnil);
3907 if (!NILP (prop) && display_prop_string_p (prop, string))
3908 found = 1;
3909 else
3910 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil, limit);
3911 }
3912
3913 if (!found)
3914 {
3915 pos = make_number (around_charpos);
3916 limit = make_number (max (XINT (pos) - MAX_DISTANCE, BEGV));
3917 while (!found && !EQ (pos, limit))
3918 {
3919 prop = Fget_char_property (pos, Qdisplay, Qnil);
3920 if (!NILP (prop) && display_prop_string_p (prop, string))
3921 found = 1;
3922 else
3923 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
3924 limit);
3925 }
3926 }
3927
3928 return found ? XINT (pos) : 0;
3929 }
3930
3931
3932 \f
3933 /***********************************************************************
3934 `composition' property
3935 ***********************************************************************/
3936
3937 /* Set up iterator IT from `composition' property at its current
3938 position. Called from handle_stop. */
3939
3940 static enum prop_handled
3941 handle_composition_prop (it)
3942 struct it *it;
3943 {
3944 Lisp_Object prop, string;
3945 int pos, pos_byte, end;
3946 enum prop_handled handled = HANDLED_NORMALLY;
3947
3948 if (STRINGP (it->string))
3949 {
3950 pos = IT_STRING_CHARPOS (*it);
3951 pos_byte = IT_STRING_BYTEPOS (*it);
3952 string = it->string;
3953 }
3954 else
3955 {
3956 pos = IT_CHARPOS (*it);
3957 pos_byte = IT_BYTEPOS (*it);
3958 string = Qnil;
3959 }
3960
3961 /* If there's a valid composition and point is not inside of the
3962 composition (in the case that the composition is from the current
3963 buffer), draw a glyph composed from the composition components. */
3964 if (find_composition (pos, -1, &pos, &end, &prop, string)
3965 && COMPOSITION_VALID_P (pos, end, prop)
3966 && (STRINGP (it->string) || (PT <= pos || PT >= end)))
3967 {
3968 int id = get_composition_id (pos, pos_byte, end - pos, prop, string);
3969
3970 if (id >= 0)
3971 {
3972 it->method = next_element_from_composition;
3973 it->cmp_id = id;
3974 it->cmp_len = COMPOSITION_LENGTH (prop);
3975 /* For a terminal, draw only the first character of the
3976 components. */
3977 it->c = COMPOSITION_GLYPH (composition_table[id], 0);
3978 it->len = (STRINGP (it->string)
3979 ? string_char_to_byte (it->string, end)
3980 : CHAR_TO_BYTE (end)) - pos_byte;
3981 it->stop_charpos = end;
3982 handled = HANDLED_RETURN;
3983 }
3984 }
3985
3986 return handled;
3987 }
3988
3989
3990 \f
3991 /***********************************************************************
3992 Overlay strings
3993 ***********************************************************************/
3994
3995 /* The following structure is used to record overlay strings for
3996 later sorting in load_overlay_strings. */
3997
3998 struct overlay_entry
3999 {
4000 Lisp_Object overlay;
4001 Lisp_Object string;
4002 int priority;
4003 int after_string_p;
4004 };
4005
4006
4007 /* Set up iterator IT from overlay strings at its current position.
4008 Called from handle_stop. */
4009
4010 static enum prop_handled
4011 handle_overlay_change (it)
4012 struct it *it;
4013 {
4014 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
4015 return HANDLED_RECOMPUTE_PROPS;
4016 else
4017 return HANDLED_NORMALLY;
4018 }
4019
4020
4021 /* Set up the next overlay string for delivery by IT, if there is an
4022 overlay string to deliver. Called by set_iterator_to_next when the
4023 end of the current overlay string is reached. If there are more
4024 overlay strings to display, IT->string and
4025 IT->current.overlay_string_index are set appropriately here.
4026 Otherwise IT->string is set to nil. */
4027
4028 static void
4029 next_overlay_string (it)
4030 struct it *it;
4031 {
4032 ++it->current.overlay_string_index;
4033 if (it->current.overlay_string_index == it->n_overlay_strings)
4034 {
4035 /* No more overlay strings. Restore IT's settings to what
4036 they were before overlay strings were processed, and
4037 continue to deliver from current_buffer. */
4038 int display_ellipsis_p = it->stack[it->sp - 1].display_ellipsis_p;
4039
4040 pop_it (it);
4041 xassert (it->stop_charpos >= BEGV
4042 && it->stop_charpos <= it->end_charpos);
4043 it->string = Qnil;
4044 it->current.overlay_string_index = -1;
4045 SET_TEXT_POS (it->current.string_pos, -1, -1);
4046 it->n_overlay_strings = 0;
4047 it->method = next_element_from_buffer;
4048
4049 /* If we're at the end of the buffer, record that we have
4050 processed the overlay strings there already, so that
4051 next_element_from_buffer doesn't try it again. */
4052 if (IT_CHARPOS (*it) >= it->end_charpos)
4053 it->overlay_strings_at_end_processed_p = 1;
4054
4055 /* If we have to display `...' for invisible text, set
4056 the iterator up for that. */
4057 if (display_ellipsis_p)
4058 setup_for_ellipsis (it, 0);
4059 }
4060 else
4061 {
4062 /* There are more overlay strings to process. If
4063 IT->current.overlay_string_index has advanced to a position
4064 where we must load IT->overlay_strings with more strings, do
4065 it. */
4066 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
4067
4068 if (it->current.overlay_string_index && i == 0)
4069 load_overlay_strings (it, 0);
4070
4071 /* Initialize IT to deliver display elements from the overlay
4072 string. */
4073 it->string = it->overlay_strings[i];
4074 it->multibyte_p = STRING_MULTIBYTE (it->string);
4075 SET_TEXT_POS (it->current.string_pos, 0, 0);
4076 it->method = next_element_from_string;
4077 it->stop_charpos = 0;
4078 }
4079
4080 CHECK_IT (it);
4081 }
4082
4083
4084 /* Compare two overlay_entry structures E1 and E2. Used as a
4085 comparison function for qsort in load_overlay_strings. Overlay
4086 strings for the same position are sorted so that
4087
4088 1. All after-strings come in front of before-strings, except
4089 when they come from the same overlay.
4090
4091 2. Within after-strings, strings are sorted so that overlay strings
4092 from overlays with higher priorities come first.
4093
4094 2. Within before-strings, strings are sorted so that overlay
4095 strings from overlays with higher priorities come last.
4096
4097 Value is analogous to strcmp. */
4098
4099
4100 static int
4101 compare_overlay_entries (e1, e2)
4102 void *e1, *e2;
4103 {
4104 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
4105 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
4106 int result;
4107
4108 if (entry1->after_string_p != entry2->after_string_p)
4109 {
4110 /* Let after-strings appear in front of before-strings if
4111 they come from different overlays. */
4112 if (EQ (entry1->overlay, entry2->overlay))
4113 result = entry1->after_string_p ? 1 : -1;
4114 else
4115 result = entry1->after_string_p ? -1 : 1;
4116 }
4117 else if (entry1->after_string_p)
4118 /* After-strings sorted in order of decreasing priority. */
4119 result = entry2->priority - entry1->priority;
4120 else
4121 /* Before-strings sorted in order of increasing priority. */
4122 result = entry1->priority - entry2->priority;
4123
4124 return result;
4125 }
4126
4127
4128 /* Load the vector IT->overlay_strings with overlay strings from IT's
4129 current buffer position, or from CHARPOS if that is > 0. Set
4130 IT->n_overlays to the total number of overlay strings found.
4131
4132 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
4133 a time. On entry into load_overlay_strings,
4134 IT->current.overlay_string_index gives the number of overlay
4135 strings that have already been loaded by previous calls to this
4136 function.
4137
4138 IT->add_overlay_start contains an additional overlay start
4139 position to consider for taking overlay strings from, if non-zero.
4140 This position comes into play when the overlay has an `invisible'
4141 property, and both before and after-strings. When we've skipped to
4142 the end of the overlay, because of its `invisible' property, we
4143 nevertheless want its before-string to appear.
4144 IT->add_overlay_start will contain the overlay start position
4145 in this case.
4146
4147 Overlay strings are sorted so that after-string strings come in
4148 front of before-string strings. Within before and after-strings,
4149 strings are sorted by overlay priority. See also function
4150 compare_overlay_entries. */
4151
4152 static void
4153 load_overlay_strings (it, charpos)
4154 struct it *it;
4155 int charpos;
4156 {
4157 extern Lisp_Object Qafter_string, Qbefore_string, Qwindow, Qpriority;
4158 Lisp_Object overlay, window, str, invisible;
4159 struct Lisp_Overlay *ov;
4160 int start, end;
4161 int size = 20;
4162 int n = 0, i, j, invis_p;
4163 struct overlay_entry *entries
4164 = (struct overlay_entry *) alloca (size * sizeof *entries);
4165
4166 if (charpos <= 0)
4167 charpos = IT_CHARPOS (*it);
4168
4169 /* Append the overlay string STRING of overlay OVERLAY to vector
4170 `entries' which has size `size' and currently contains `n'
4171 elements. AFTER_P non-zero means STRING is an after-string of
4172 OVERLAY. */
4173 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
4174 do \
4175 { \
4176 Lisp_Object priority; \
4177 \
4178 if (n == size) \
4179 { \
4180 int new_size = 2 * size; \
4181 struct overlay_entry *old = entries; \
4182 entries = \
4183 (struct overlay_entry *) alloca (new_size \
4184 * sizeof *entries); \
4185 bcopy (old, entries, size * sizeof *entries); \
4186 size = new_size; \
4187 } \
4188 \
4189 entries[n].string = (STRING); \
4190 entries[n].overlay = (OVERLAY); \
4191 priority = Foverlay_get ((OVERLAY), Qpriority); \
4192 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
4193 entries[n].after_string_p = (AFTER_P); \
4194 ++n; \
4195 } \
4196 while (0)
4197
4198 /* Process overlay before the overlay center. */
4199 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
4200 {
4201 XSETMISC (overlay, ov);
4202 xassert (OVERLAYP (overlay));
4203 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4204 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4205
4206 if (end < charpos)
4207 break;
4208
4209 /* Skip this overlay if it doesn't start or end at IT's current
4210 position. */
4211 if (end != charpos && start != charpos)
4212 continue;
4213
4214 /* Skip this overlay if it doesn't apply to IT->w. */
4215 window = Foverlay_get (overlay, Qwindow);
4216 if (WINDOWP (window) && XWINDOW (window) != it->w)
4217 continue;
4218
4219 /* If the text ``under'' the overlay is invisible, both before-
4220 and after-strings from this overlay are visible; start and
4221 end position are indistinguishable. */
4222 invisible = Foverlay_get (overlay, Qinvisible);
4223 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4224
4225 /* If overlay has a non-empty before-string, record it. */
4226 if ((start == charpos || (end == charpos && invis_p))
4227 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4228 && SCHARS (str))
4229 RECORD_OVERLAY_STRING (overlay, str, 0);
4230
4231 /* If overlay has a non-empty after-string, record it. */
4232 if ((end == charpos || (start == charpos && invis_p))
4233 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4234 && SCHARS (str))
4235 RECORD_OVERLAY_STRING (overlay, str, 1);
4236 }
4237
4238 /* Process overlays after the overlay center. */
4239 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
4240 {
4241 XSETMISC (overlay, ov);
4242 xassert (OVERLAYP (overlay));
4243 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4244 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4245
4246 if (start > charpos)
4247 break;
4248
4249 /* Skip this overlay if it doesn't start or end at IT's current
4250 position. */
4251 if (end != charpos && start != charpos)
4252 continue;
4253
4254 /* Skip this overlay if it doesn't apply to IT->w. */
4255 window = Foverlay_get (overlay, Qwindow);
4256 if (WINDOWP (window) && XWINDOW (window) != it->w)
4257 continue;
4258
4259 /* If the text ``under'' the overlay is invisible, it has a zero
4260 dimension, and both before- and after-strings apply. */
4261 invisible = Foverlay_get (overlay, Qinvisible);
4262 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4263
4264 /* If overlay has a non-empty before-string, record it. */
4265 if ((start == charpos || (end == charpos && invis_p))
4266 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4267 && SCHARS (str))
4268 RECORD_OVERLAY_STRING (overlay, str, 0);
4269
4270 /* If overlay has a non-empty after-string, record it. */
4271 if ((end == charpos || (start == charpos && invis_p))
4272 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4273 && SCHARS (str))
4274 RECORD_OVERLAY_STRING (overlay, str, 1);
4275 }
4276
4277 #undef RECORD_OVERLAY_STRING
4278
4279 /* Sort entries. */
4280 if (n > 1)
4281 qsort (entries, n, sizeof *entries, compare_overlay_entries);
4282
4283 /* Record the total number of strings to process. */
4284 it->n_overlay_strings = n;
4285
4286 /* IT->current.overlay_string_index is the number of overlay strings
4287 that have already been consumed by IT. Copy some of the
4288 remaining overlay strings to IT->overlay_strings. */
4289 i = 0;
4290 j = it->current.overlay_string_index;
4291 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
4292 it->overlay_strings[i++] = entries[j++].string;
4293
4294 CHECK_IT (it);
4295 }
4296
4297
4298 /* Get the first chunk of overlay strings at IT's current buffer
4299 position, or at CHARPOS if that is > 0. Value is non-zero if at
4300 least one overlay string was found. */
4301
4302 static int
4303 get_overlay_strings (it, charpos)
4304 struct it *it;
4305 int charpos;
4306 {
4307 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
4308 process. This fills IT->overlay_strings with strings, and sets
4309 IT->n_overlay_strings to the total number of strings to process.
4310 IT->pos.overlay_string_index has to be set temporarily to zero
4311 because load_overlay_strings needs this; it must be set to -1
4312 when no overlay strings are found because a zero value would
4313 indicate a position in the first overlay string. */
4314 it->current.overlay_string_index = 0;
4315 load_overlay_strings (it, charpos);
4316
4317 /* If we found overlay strings, set up IT to deliver display
4318 elements from the first one. Otherwise set up IT to deliver
4319 from current_buffer. */
4320 if (it->n_overlay_strings)
4321 {
4322 /* Make sure we know settings in current_buffer, so that we can
4323 restore meaningful values when we're done with the overlay
4324 strings. */
4325 compute_stop_pos (it);
4326 xassert (it->face_id >= 0);
4327
4328 /* Save IT's settings. They are restored after all overlay
4329 strings have been processed. */
4330 xassert (it->sp == 0);
4331 push_it (it);
4332
4333 /* Set up IT to deliver display elements from the first overlay
4334 string. */
4335 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4336 it->string = it->overlay_strings[0];
4337 it->stop_charpos = 0;
4338 xassert (STRINGP (it->string));
4339 it->end_charpos = SCHARS (it->string);
4340 it->multibyte_p = STRING_MULTIBYTE (it->string);
4341 it->method = next_element_from_string;
4342 }
4343 else
4344 {
4345 it->string = Qnil;
4346 it->current.overlay_string_index = -1;
4347 it->method = next_element_from_buffer;
4348 }
4349
4350 CHECK_IT (it);
4351
4352 /* Value is non-zero if we found at least one overlay string. */
4353 return STRINGP (it->string);
4354 }
4355
4356
4357 \f
4358 /***********************************************************************
4359 Saving and restoring state
4360 ***********************************************************************/
4361
4362 /* Save current settings of IT on IT->stack. Called, for example,
4363 before setting up IT for an overlay string, to be able to restore
4364 IT's settings to what they were after the overlay string has been
4365 processed. */
4366
4367 static void
4368 push_it (it)
4369 struct it *it;
4370 {
4371 struct iterator_stack_entry *p;
4372
4373 xassert (it->sp < 2);
4374 p = it->stack + it->sp;
4375
4376 p->stop_charpos = it->stop_charpos;
4377 xassert (it->face_id >= 0);
4378 p->face_id = it->face_id;
4379 p->string = it->string;
4380 p->pos = it->current;
4381 p->end_charpos = it->end_charpos;
4382 p->string_nchars = it->string_nchars;
4383 p->area = it->area;
4384 p->multibyte_p = it->multibyte_p;
4385 p->slice = it->slice;
4386 p->space_width = it->space_width;
4387 p->font_height = it->font_height;
4388 p->voffset = it->voffset;
4389 p->string_from_display_prop_p = it->string_from_display_prop_p;
4390 p->display_ellipsis_p = 0;
4391 ++it->sp;
4392 }
4393
4394
4395 /* Restore IT's settings from IT->stack. Called, for example, when no
4396 more overlay strings must be processed, and we return to delivering
4397 display elements from a buffer, or when the end of a string from a
4398 `display' property is reached and we return to delivering display
4399 elements from an overlay string, or from a buffer. */
4400
4401 static void
4402 pop_it (it)
4403 struct it *it;
4404 {
4405 struct iterator_stack_entry *p;
4406
4407 xassert (it->sp > 0);
4408 --it->sp;
4409 p = it->stack + it->sp;
4410 it->stop_charpos = p->stop_charpos;
4411 it->face_id = p->face_id;
4412 it->string = p->string;
4413 it->current = p->pos;
4414 it->end_charpos = p->end_charpos;
4415 it->string_nchars = p->string_nchars;
4416 it->area = p->area;
4417 it->multibyte_p = p->multibyte_p;
4418 it->slice = p->slice;
4419 it->space_width = p->space_width;
4420 it->font_height = p->font_height;
4421 it->voffset = p->voffset;
4422 it->string_from_display_prop_p = p->string_from_display_prop_p;
4423 }
4424
4425
4426 \f
4427 /***********************************************************************
4428 Moving over lines
4429 ***********************************************************************/
4430
4431 /* Set IT's current position to the previous line start. */
4432
4433 static void
4434 back_to_previous_line_start (it)
4435 struct it *it;
4436 {
4437 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
4438 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
4439 }
4440
4441
4442 /* Move IT to the next line start.
4443
4444 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
4445 we skipped over part of the text (as opposed to moving the iterator
4446 continuously over the text). Otherwise, don't change the value
4447 of *SKIPPED_P.
4448
4449 Newlines may come from buffer text, overlay strings, or strings
4450 displayed via the `display' property. That's the reason we can't
4451 simply use find_next_newline_no_quit.
4452
4453 Note that this function may not skip over invisible text that is so
4454 because of text properties and immediately follows a newline. If
4455 it would, function reseat_at_next_visible_line_start, when called
4456 from set_iterator_to_next, would effectively make invisible
4457 characters following a newline part of the wrong glyph row, which
4458 leads to wrong cursor motion. */
4459
4460 static int
4461 forward_to_next_line_start (it, skipped_p)
4462 struct it *it;
4463 int *skipped_p;
4464 {
4465 int old_selective, newline_found_p, n;
4466 const int MAX_NEWLINE_DISTANCE = 500;
4467
4468 /* If already on a newline, just consume it to avoid unintended
4469 skipping over invisible text below. */
4470 if (it->what == IT_CHARACTER
4471 && it->c == '\n'
4472 && CHARPOS (it->position) == IT_CHARPOS (*it))
4473 {
4474 set_iterator_to_next (it, 0);
4475 it->c = 0;
4476 return 1;
4477 }
4478
4479 /* Don't handle selective display in the following. It's (a)
4480 unnecessary because it's done by the caller, and (b) leads to an
4481 infinite recursion because next_element_from_ellipsis indirectly
4482 calls this function. */
4483 old_selective = it->selective;
4484 it->selective = 0;
4485
4486 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
4487 from buffer text. */
4488 for (n = newline_found_p = 0;
4489 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
4490 n += STRINGP (it->string) ? 0 : 1)
4491 {
4492 if (!get_next_display_element (it))
4493 return 0;
4494 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
4495 set_iterator_to_next (it, 0);
4496 }
4497
4498 /* If we didn't find a newline near enough, see if we can use a
4499 short-cut. */
4500 if (!newline_found_p)
4501 {
4502 int start = IT_CHARPOS (*it);
4503 int limit = find_next_newline_no_quit (start, 1);
4504 Lisp_Object pos;
4505
4506 xassert (!STRINGP (it->string));
4507
4508 /* If there isn't any `display' property in sight, and no
4509 overlays, we can just use the position of the newline in
4510 buffer text. */
4511 if (it->stop_charpos >= limit
4512 || ((pos = Fnext_single_property_change (make_number (start),
4513 Qdisplay,
4514 Qnil, make_number (limit)),
4515 NILP (pos))
4516 && next_overlay_change (start) == ZV))
4517 {
4518 IT_CHARPOS (*it) = limit;
4519 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
4520 *skipped_p = newline_found_p = 1;
4521 }
4522 else
4523 {
4524 while (get_next_display_element (it)
4525 && !newline_found_p)
4526 {
4527 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
4528 set_iterator_to_next (it, 0);
4529 }
4530 }
4531 }
4532
4533 it->selective = old_selective;
4534 return newline_found_p;
4535 }
4536
4537
4538 /* Set IT's current position to the previous visible line start. Skip
4539 invisible text that is so either due to text properties or due to
4540 selective display. Caution: this does not change IT->current_x and
4541 IT->hpos. */
4542
4543 static void
4544 back_to_previous_visible_line_start (it)
4545 struct it *it;
4546 {
4547 int visible_p = 0;
4548
4549 /* Go back one newline if not on BEGV already. */
4550 if (IT_CHARPOS (*it) > BEGV)
4551 back_to_previous_line_start (it);
4552
4553 /* Move over lines that are invisible because of selective display
4554 or text properties. */
4555 while (IT_CHARPOS (*it) > BEGV
4556 && !visible_p)
4557 {
4558 visible_p = 1;
4559
4560 /* If selective > 0, then lines indented more than that values
4561 are invisible. */
4562 if (it->selective > 0
4563 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
4564 (double) it->selective)) /* iftc */
4565 visible_p = 0;
4566 else
4567 {
4568 Lisp_Object prop;
4569
4570 /* Check the newline before point for invisibility. */
4571 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
4572 Qinvisible, it->window);
4573 if (TEXT_PROP_MEANS_INVISIBLE (prop))
4574 visible_p = 0;
4575 }
4576
4577 if (visible_p)
4578 {
4579 struct it it2 = *it;
4580
4581 if (handle_display_prop (&it2) == HANDLED_RETURN)
4582 visible_p = 0;
4583 }
4584
4585 /* Back one more newline if the current one is invisible. */
4586 if (!visible_p)
4587 back_to_previous_line_start (it);
4588 }
4589
4590 xassert (IT_CHARPOS (*it) >= BEGV);
4591 xassert (IT_CHARPOS (*it) == BEGV
4592 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
4593 CHECK_IT (it);
4594 }
4595
4596
4597 /* Reseat iterator IT at the previous visible line start. Skip
4598 invisible text that is so either due to text properties or due to
4599 selective display. At the end, update IT's overlay information,
4600 face information etc. */
4601
4602 void
4603 reseat_at_previous_visible_line_start (it)
4604 struct it *it;
4605 {
4606 back_to_previous_visible_line_start (it);
4607 reseat (it, it->current.pos, 1);
4608 CHECK_IT (it);
4609 }
4610
4611
4612 /* Reseat iterator IT on the next visible line start in the current
4613 buffer. ON_NEWLINE_P non-zero means position IT on the newline
4614 preceding the line start. Skip over invisible text that is so
4615 because of selective display. Compute faces, overlays etc at the
4616 new position. Note that this function does not skip over text that
4617 is invisible because of text properties. */
4618
4619 static void
4620 reseat_at_next_visible_line_start (it, on_newline_p)
4621 struct it *it;
4622 int on_newline_p;
4623 {
4624 int newline_found_p, skipped_p = 0;
4625
4626 newline_found_p = forward_to_next_line_start (it, &skipped_p);
4627
4628 /* Skip over lines that are invisible because they are indented
4629 more than the value of IT->selective. */
4630 if (it->selective > 0)
4631 while (IT_CHARPOS (*it) < ZV
4632 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
4633 (double) it->selective)) /* iftc */
4634 {
4635 xassert (FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
4636 newline_found_p = forward_to_next_line_start (it, &skipped_p);
4637 }
4638
4639 /* Position on the newline if that's what's requested. */
4640 if (on_newline_p && newline_found_p)
4641 {
4642 if (STRINGP (it->string))
4643 {
4644 if (IT_STRING_CHARPOS (*it) > 0)
4645 {
4646 --IT_STRING_CHARPOS (*it);
4647 --IT_STRING_BYTEPOS (*it);
4648 }
4649 }
4650 else if (IT_CHARPOS (*it) > BEGV)
4651 {
4652 --IT_CHARPOS (*it);
4653 --IT_BYTEPOS (*it);
4654 reseat (it, it->current.pos, 0);
4655 }
4656 }
4657 else if (skipped_p)
4658 reseat (it, it->current.pos, 0);
4659
4660 CHECK_IT (it);
4661 }
4662
4663
4664 \f
4665 /***********************************************************************
4666 Changing an iterator's position
4667 ***********************************************************************/
4668
4669 /* Change IT's current position to POS in current_buffer. If FORCE_P
4670 is non-zero, always check for text properties at the new position.
4671 Otherwise, text properties are only looked up if POS >=
4672 IT->check_charpos of a property. */
4673
4674 static void
4675 reseat (it, pos, force_p)
4676 struct it *it;
4677 struct text_pos pos;
4678 int force_p;
4679 {
4680 int original_pos = IT_CHARPOS (*it);
4681
4682 reseat_1 (it, pos, 0);
4683
4684 /* Determine where to check text properties. Avoid doing it
4685 where possible because text property lookup is very expensive. */
4686 if (force_p
4687 || CHARPOS (pos) > it->stop_charpos
4688 || CHARPOS (pos) < original_pos)
4689 handle_stop (it);
4690
4691 CHECK_IT (it);
4692 }
4693
4694
4695 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
4696 IT->stop_pos to POS, also. */
4697
4698 static void
4699 reseat_1 (it, pos, set_stop_p)
4700 struct it *it;
4701 struct text_pos pos;
4702 int set_stop_p;
4703 {
4704 /* Don't call this function when scanning a C string. */
4705 xassert (it->s == NULL);
4706
4707 /* POS must be a reasonable value. */
4708 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
4709
4710 it->current.pos = it->position = pos;
4711 XSETBUFFER (it->object, current_buffer);
4712 it->end_charpos = ZV;
4713 it->dpvec = NULL;
4714 it->current.dpvec_index = -1;
4715 it->current.overlay_string_index = -1;
4716 IT_STRING_CHARPOS (*it) = -1;
4717 IT_STRING_BYTEPOS (*it) = -1;
4718 it->string = Qnil;
4719 it->method = next_element_from_buffer;
4720 /* RMS: I added this to fix a bug in move_it_vertically_backward
4721 where it->area continued to relate to the starting point
4722 for the backward motion. Bug report from
4723 Nick Roberts <nick@nick.uklinux.net> on 19 May 2003.
4724 However, I am not sure whether reseat still does the right thing
4725 in general after this change. */
4726 it->area = TEXT_AREA;
4727 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
4728 it->sp = 0;
4729 it->face_before_selective_p = 0;
4730
4731 if (set_stop_p)
4732 it->stop_charpos = CHARPOS (pos);
4733 }
4734
4735
4736 /* Set up IT for displaying a string, starting at CHARPOS in window W.
4737 If S is non-null, it is a C string to iterate over. Otherwise,
4738 STRING gives a Lisp string to iterate over.
4739
4740 If PRECISION > 0, don't return more then PRECISION number of
4741 characters from the string.
4742
4743 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
4744 characters have been returned. FIELD_WIDTH < 0 means an infinite
4745 field width.
4746
4747 MULTIBYTE = 0 means disable processing of multibyte characters,
4748 MULTIBYTE > 0 means enable it,
4749 MULTIBYTE < 0 means use IT->multibyte_p.
4750
4751 IT must be initialized via a prior call to init_iterator before
4752 calling this function. */
4753
4754 static void
4755 reseat_to_string (it, s, string, charpos, precision, field_width, multibyte)
4756 struct it *it;
4757 unsigned char *s;
4758 Lisp_Object string;
4759 int charpos;
4760 int precision, field_width, multibyte;
4761 {
4762 /* No region in strings. */
4763 it->region_beg_charpos = it->region_end_charpos = -1;
4764
4765 /* No text property checks performed by default, but see below. */
4766 it->stop_charpos = -1;
4767
4768 /* Set iterator position and end position. */
4769 bzero (&it->current, sizeof it->current);
4770 it->current.overlay_string_index = -1;
4771 it->current.dpvec_index = -1;
4772 xassert (charpos >= 0);
4773
4774 /* If STRING is specified, use its multibyteness, otherwise use the
4775 setting of MULTIBYTE, if specified. */
4776 if (multibyte >= 0)
4777 it->multibyte_p = multibyte > 0;
4778
4779 if (s == NULL)
4780 {
4781 xassert (STRINGP (string));
4782 it->string = string;
4783 it->s = NULL;
4784 it->end_charpos = it->string_nchars = SCHARS (string);
4785 it->method = next_element_from_string;
4786 it->current.string_pos = string_pos (charpos, string);
4787 }
4788 else
4789 {
4790 it->s = s;
4791 it->string = Qnil;
4792
4793 /* Note that we use IT->current.pos, not it->current.string_pos,
4794 for displaying C strings. */
4795 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
4796 if (it->multibyte_p)
4797 {
4798 it->current.pos = c_string_pos (charpos, s, 1);
4799 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
4800 }
4801 else
4802 {
4803 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
4804 it->end_charpos = it->string_nchars = strlen (s);
4805 }
4806
4807 it->method = next_element_from_c_string;
4808 }
4809
4810 /* PRECISION > 0 means don't return more than PRECISION characters
4811 from the string. */
4812 if (precision > 0 && it->end_charpos - charpos > precision)
4813 it->end_charpos = it->string_nchars = charpos + precision;
4814
4815 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
4816 characters have been returned. FIELD_WIDTH == 0 means don't pad,
4817 FIELD_WIDTH < 0 means infinite field width. This is useful for
4818 padding with `-' at the end of a mode line. */
4819 if (field_width < 0)
4820 field_width = INFINITY;
4821 if (field_width > it->end_charpos - charpos)
4822 it->end_charpos = charpos + field_width;
4823
4824 /* Use the standard display table for displaying strings. */
4825 if (DISP_TABLE_P (Vstandard_display_table))
4826 it->dp = XCHAR_TABLE (Vstandard_display_table);
4827
4828 it->stop_charpos = charpos;
4829 CHECK_IT (it);
4830 }
4831
4832
4833 \f
4834 /***********************************************************************
4835 Iteration
4836 ***********************************************************************/
4837
4838 /* Load IT's display element fields with information about the next
4839 display element from the current position of IT. Value is zero if
4840 end of buffer (or C string) is reached. */
4841
4842 int
4843 get_next_display_element (it)
4844 struct it *it;
4845 {
4846 /* Non-zero means that we found a display element. Zero means that
4847 we hit the end of what we iterate over. Performance note: the
4848 function pointer `method' used here turns out to be faster than
4849 using a sequence of if-statements. */
4850 int success_p;
4851
4852 get_next:
4853 success_p = (*it->method) (it);
4854
4855 if (it->what == IT_CHARACTER)
4856 {
4857 /* Map via display table or translate control characters.
4858 IT->c, IT->len etc. have been set to the next character by
4859 the function call above. If we have a display table, and it
4860 contains an entry for IT->c, translate it. Don't do this if
4861 IT->c itself comes from a display table, otherwise we could
4862 end up in an infinite recursion. (An alternative could be to
4863 count the recursion depth of this function and signal an
4864 error when a certain maximum depth is reached.) Is it worth
4865 it? */
4866 if (success_p && it->dpvec == NULL)
4867 {
4868 Lisp_Object dv;
4869
4870 if (it->dp
4871 && (dv = DISP_CHAR_VECTOR (it->dp, it->c),
4872 VECTORP (dv)))
4873 {
4874 struct Lisp_Vector *v = XVECTOR (dv);
4875
4876 /* Return the first character from the display table
4877 entry, if not empty. If empty, don't display the
4878 current character. */
4879 if (v->size)
4880 {
4881 it->dpvec_char_len = it->len;
4882 it->dpvec = v->contents;
4883 it->dpend = v->contents + v->size;
4884 it->current.dpvec_index = 0;
4885 it->saved_face_id = it->face_id;
4886 it->method = next_element_from_display_vector;
4887 }
4888 else
4889 {
4890 set_iterator_to_next (it, 0);
4891 }
4892 goto get_next;
4893 }
4894
4895 /* Translate control characters into `\003' or `^C' form.
4896 Control characters coming from a display table entry are
4897 currently not translated because we use IT->dpvec to hold
4898 the translation. This could easily be changed but I
4899 don't believe that it is worth doing.
4900
4901 If it->multibyte_p is nonzero, eight-bit characters and
4902 non-printable multibyte characters are also translated to
4903 octal form.
4904
4905 If it->multibyte_p is zero, eight-bit characters that
4906 don't have corresponding multibyte char code are also
4907 translated to octal form. */
4908 else if ((it->c < ' '
4909 && (it->area != TEXT_AREA
4910 /* In mode line, treat \n like other crl chars. */
4911 || (it->c != '\n'
4912 && it->glyph_row && it->glyph_row->mode_line_p)
4913 || (it->c != '\n' && it->c != '\t')))
4914 || (it->multibyte_p
4915 ? ((it->c >= 127
4916 && it->len == 1)
4917 || !CHAR_PRINTABLE_P (it->c)
4918 || it->c == 0x8ad
4919 || it->c == 0x8a0)
4920 : (it->c >= 127
4921 && (!unibyte_display_via_language_environment
4922 || it->c == unibyte_char_to_multibyte (it->c)))))
4923 {
4924 /* IT->c is a control character which must be displayed
4925 either as '\003' or as `^C' where the '\\' and '^'
4926 can be defined in the display table. Fill
4927 IT->ctl_chars with glyphs for what we have to
4928 display. Then, set IT->dpvec to these glyphs. */
4929 GLYPH g;
4930 int ctl_len;
4931 int face_id = escape_glyph_face;
4932
4933 /* Find the face id if `escape-glyph' unless we recently did. */
4934 if (face_id < 0)
4935 {
4936 Lisp_Object tem = Fget (Qescape_glyph, Qface);
4937 if (INTEGERP (tem))
4938 face_id = XINT (tem);
4939 else
4940 face_id = 0;
4941 /* If there's overflow, use 0 instead. */
4942 if (FAST_GLYPH_FACE (FAST_MAKE_GLYPH (0, face_id)) != face_id)
4943 face_id = 0;
4944 escape_glyph_face = face_id;
4945 }
4946
4947 if (it->c < 128 && it->ctl_arrow_p)
4948 {
4949 /* Set IT->ctl_chars[0] to the glyph for `^'. */
4950 if (it->dp
4951 && INTEGERP (DISP_CTRL_GLYPH (it->dp))
4952 && GLYPH_CHAR_VALID_P (XINT (DISP_CTRL_GLYPH (it->dp))))
4953 g = XINT (DISP_CTRL_GLYPH (it->dp));
4954 else
4955 g = FAST_MAKE_GLYPH ('^', face_id);
4956 XSETINT (it->ctl_chars[0], g);
4957
4958 g = FAST_MAKE_GLYPH (it->c ^ 0100, face_id);
4959 XSETINT (it->ctl_chars[1], g);
4960 ctl_len = 2;
4961 }
4962 else if (it->c == 0x8a0 || it->c == 0x8ad)
4963 {
4964 /* Set IT->ctl_chars[0] to the glyph for `\\'. */
4965 if (it->dp
4966 && INTEGERP (DISP_ESCAPE_GLYPH (it->dp))
4967 && GLYPH_CHAR_VALID_P (XINT (DISP_ESCAPE_GLYPH (it->dp))))
4968 g = XINT (DISP_ESCAPE_GLYPH (it->dp));
4969 else
4970 g = FAST_MAKE_GLYPH ('\\', face_id);
4971 XSETINT (it->ctl_chars[0], g);
4972
4973 g = FAST_MAKE_GLYPH (it->c == 0x8ad ? '-' : ' ', face_id);
4974 XSETINT (it->ctl_chars[1], g);
4975 ctl_len = 2;
4976 }
4977 else
4978 {
4979 unsigned char str[MAX_MULTIBYTE_LENGTH];
4980 int len;
4981 int i;
4982 GLYPH escape_glyph;
4983
4984 /* Set IT->ctl_chars[0] to the glyph for `\\'. */
4985 if (it->dp
4986 && INTEGERP (DISP_ESCAPE_GLYPH (it->dp))
4987 && GLYPH_CHAR_VALID_P (XFASTINT (DISP_ESCAPE_GLYPH (it->dp))))
4988 escape_glyph = XFASTINT (DISP_ESCAPE_GLYPH (it->dp));
4989 else
4990 escape_glyph = FAST_MAKE_GLYPH ('\\', face_id);
4991
4992 if (SINGLE_BYTE_CHAR_P (it->c))
4993 str[0] = it->c, len = 1;
4994 else
4995 {
4996 len = CHAR_STRING_NO_SIGNAL (it->c, str);
4997 if (len < 0)
4998 {
4999 /* It's an invalid character, which
5000 shouldn't happen actually, but due to
5001 bugs it may happen. Let's print the char
5002 as is, there's not much meaningful we can
5003 do with it. */
5004 str[0] = it->c;
5005 str[1] = it->c >> 8;
5006 str[2] = it->c >> 16;
5007 str[3] = it->c >> 24;
5008 len = 4;
5009 }
5010 }
5011
5012 for (i = 0; i < len; i++)
5013 {
5014 XSETINT (it->ctl_chars[i * 4], escape_glyph);
5015 /* Insert three more glyphs into IT->ctl_chars for
5016 the octal display of the character. */
5017 g = FAST_MAKE_GLYPH (((str[i] >> 6) & 7) + '0',
5018 face_id);
5019 XSETINT (it->ctl_chars[i * 4 + 1], g);
5020 g = FAST_MAKE_GLYPH (((str[i] >> 3) & 7) + '0',
5021 face_id);
5022 XSETINT (it->ctl_chars[i * 4 + 2], g);
5023 g = FAST_MAKE_GLYPH ((str[i] & 7) + '0',
5024 face_id);
5025 XSETINT (it->ctl_chars[i * 4 + 3], g);
5026 }
5027 ctl_len = len * 4;
5028 }
5029
5030 /* Set up IT->dpvec and return first character from it. */
5031 it->dpvec_char_len = it->len;
5032 it->dpvec = it->ctl_chars;
5033 it->dpend = it->dpvec + ctl_len;
5034 it->current.dpvec_index = 0;
5035 it->saved_face_id = it->face_id;
5036 it->method = next_element_from_display_vector;
5037 goto get_next;
5038 }
5039 }
5040
5041 /* Adjust face id for a multibyte character. There are no
5042 multibyte character in unibyte text. */
5043 if (it->multibyte_p
5044 && success_p
5045 && FRAME_WINDOW_P (it->f))
5046 {
5047 struct face *face = FACE_FROM_ID (it->f, it->face_id);
5048 it->face_id = FACE_FOR_CHAR (it->f, face, it->c);
5049 }
5050 }
5051
5052 /* Is this character the last one of a run of characters with
5053 box? If yes, set IT->end_of_box_run_p to 1. */
5054 if (it->face_box_p
5055 && it->s == NULL)
5056 {
5057 int face_id;
5058 struct face *face;
5059
5060 it->end_of_box_run_p
5061 = ((face_id = face_after_it_pos (it),
5062 face_id != it->face_id)
5063 && (face = FACE_FROM_ID (it->f, face_id),
5064 face->box == FACE_NO_BOX));
5065 }
5066
5067 /* Value is 0 if end of buffer or string reached. */
5068 return success_p;
5069 }
5070
5071
5072 /* Move IT to the next display element.
5073
5074 RESEAT_P non-zero means if called on a newline in buffer text,
5075 skip to the next visible line start.
5076
5077 Functions get_next_display_element and set_iterator_to_next are
5078 separate because I find this arrangement easier to handle than a
5079 get_next_display_element function that also increments IT's
5080 position. The way it is we can first look at an iterator's current
5081 display element, decide whether it fits on a line, and if it does,
5082 increment the iterator position. The other way around we probably
5083 would either need a flag indicating whether the iterator has to be
5084 incremented the next time, or we would have to implement a
5085 decrement position function which would not be easy to write. */
5086
5087 void
5088 set_iterator_to_next (it, reseat_p)
5089 struct it *it;
5090 int reseat_p;
5091 {
5092 /* Reset flags indicating start and end of a sequence of characters
5093 with box. Reset them at the start of this function because
5094 moving the iterator to a new position might set them. */
5095 it->start_of_box_run_p = it->end_of_box_run_p = 0;
5096
5097 if (it->method == next_element_from_buffer)
5098 {
5099 /* The current display element of IT is a character from
5100 current_buffer. Advance in the buffer, and maybe skip over
5101 invisible lines that are so because of selective display. */
5102 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
5103 reseat_at_next_visible_line_start (it, 0);
5104 else
5105 {
5106 xassert (it->len != 0);
5107 IT_BYTEPOS (*it) += it->len;
5108 IT_CHARPOS (*it) += 1;
5109 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
5110 }
5111 }
5112 else if (it->method == next_element_from_composition)
5113 {
5114 xassert (it->cmp_id >= 0 && it ->cmp_id < n_compositions);
5115 if (STRINGP (it->string))
5116 {
5117 IT_STRING_BYTEPOS (*it) += it->len;
5118 IT_STRING_CHARPOS (*it) += it->cmp_len;
5119 it->method = next_element_from_string;
5120 goto consider_string_end;
5121 }
5122 else
5123 {
5124 IT_BYTEPOS (*it) += it->len;
5125 IT_CHARPOS (*it) += it->cmp_len;
5126 it->method = next_element_from_buffer;
5127 }
5128 }
5129 else if (it->method == next_element_from_c_string)
5130 {
5131 /* Current display element of IT is from a C string. */
5132 IT_BYTEPOS (*it) += it->len;
5133 IT_CHARPOS (*it) += 1;
5134 }
5135 else if (it->method == next_element_from_display_vector)
5136 {
5137 /* Current display element of IT is from a display table entry.
5138 Advance in the display table definition. Reset it to null if
5139 end reached, and continue with characters from buffers/
5140 strings. */
5141 ++it->current.dpvec_index;
5142
5143 /* Restore face of the iterator to what they were before the
5144 display vector entry (these entries may contain faces). */
5145 it->face_id = it->saved_face_id;
5146
5147 if (it->dpvec + it->current.dpvec_index == it->dpend)
5148 {
5149 if (it->s)
5150 it->method = next_element_from_c_string;
5151 else if (STRINGP (it->string))
5152 it->method = next_element_from_string;
5153 else
5154 it->method = next_element_from_buffer;
5155
5156 it->dpvec = NULL;
5157 it->current.dpvec_index = -1;
5158
5159 /* Recheck faces after display vector */
5160 it->stop_charpos = 0;
5161
5162 /* Skip over characters which were displayed via IT->dpvec. */
5163 if (it->dpvec_char_len < 0)
5164 reseat_at_next_visible_line_start (it, 1);
5165 else if (it->dpvec_char_len > 0)
5166 {
5167 it->len = it->dpvec_char_len;
5168 set_iterator_to_next (it, reseat_p);
5169 }
5170 }
5171 }
5172 else if (it->method == next_element_from_string)
5173 {
5174 /* Current display element is a character from a Lisp string. */
5175 xassert (it->s == NULL && STRINGP (it->string));
5176 IT_STRING_BYTEPOS (*it) += it->len;
5177 IT_STRING_CHARPOS (*it) += 1;
5178
5179 consider_string_end:
5180
5181 if (it->current.overlay_string_index >= 0)
5182 {
5183 /* IT->string is an overlay string. Advance to the
5184 next, if there is one. */
5185 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
5186 next_overlay_string (it);
5187 }
5188 else
5189 {
5190 /* IT->string is not an overlay string. If we reached
5191 its end, and there is something on IT->stack, proceed
5192 with what is on the stack. This can be either another
5193 string, this time an overlay string, or a buffer. */
5194 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
5195 && it->sp > 0)
5196 {
5197 pop_it (it);
5198 if (!STRINGP (it->string))
5199 it->method = next_element_from_buffer;
5200 else
5201 goto consider_string_end;
5202 }
5203 }
5204 }
5205 else if (it->method == next_element_from_image
5206 || it->method == next_element_from_stretch)
5207 {
5208 /* The position etc with which we have to proceed are on
5209 the stack. The position may be at the end of a string,
5210 if the `display' property takes up the whole string. */
5211 pop_it (it);
5212 it->image_id = 0;
5213 if (STRINGP (it->string))
5214 {
5215 it->method = next_element_from_string;
5216 goto consider_string_end;
5217 }
5218 else
5219 it->method = next_element_from_buffer;
5220 }
5221 else
5222 /* There are no other methods defined, so this should be a bug. */
5223 abort ();
5224
5225 xassert (it->method != next_element_from_string
5226 || (STRINGP (it->string)
5227 && IT_STRING_CHARPOS (*it) >= 0));
5228 }
5229
5230 /* Load IT's display element fields with information about the next
5231 display element which comes from a display table entry or from the
5232 result of translating a control character to one of the forms `^C'
5233 or `\003'.
5234
5235 IT->dpvec holds the glyphs to return as characters.
5236 IT->saved_face_id holds the face id before the display vector--
5237 it is restored into IT->face_idin set_iterator_to_next. */
5238
5239 static int
5240 next_element_from_display_vector (it)
5241 struct it *it;
5242 {
5243 /* Precondition. */
5244 xassert (it->dpvec && it->current.dpvec_index >= 0);
5245
5246 if (INTEGERP (*it->dpvec)
5247 && GLYPH_CHAR_VALID_P (XFASTINT (*it->dpvec)))
5248 {
5249 int lface_id;
5250 GLYPH g;
5251
5252 g = XFASTINT (it->dpvec[it->current.dpvec_index]);
5253 it->c = FAST_GLYPH_CHAR (g);
5254 it->len = CHAR_BYTES (it->c);
5255
5256 /* The entry may contain a face id to use. Such a face id is
5257 the id of a Lisp face, not a realized face. A face id of
5258 zero means no face is specified. */
5259 lface_id = FAST_GLYPH_FACE (g);
5260 if (lface_id)
5261 {
5262 /* The function returns -1 if lface_id is invalid. */
5263 int face_id = ascii_face_of_lisp_face (it->f, lface_id);
5264 if (face_id >= 0)
5265 it->face_id = face_id;
5266 }
5267 }
5268 else
5269 /* Display table entry is invalid. Return a space. */
5270 it->c = ' ', it->len = 1;
5271
5272 /* Don't change position and object of the iterator here. They are
5273 still the values of the character that had this display table
5274 entry or was translated, and that's what we want. */
5275 it->what = IT_CHARACTER;
5276 return 1;
5277 }
5278
5279
5280 /* Load IT with the next display element from Lisp string IT->string.
5281 IT->current.string_pos is the current position within the string.
5282 If IT->current.overlay_string_index >= 0, the Lisp string is an
5283 overlay string. */
5284
5285 static int
5286 next_element_from_string (it)
5287 struct it *it;
5288 {
5289 struct text_pos position;
5290
5291 xassert (STRINGP (it->string));
5292 xassert (IT_STRING_CHARPOS (*it) >= 0);
5293 position = it->current.string_pos;
5294
5295 /* Time to check for invisible text? */
5296 if (IT_STRING_CHARPOS (*it) < it->end_charpos
5297 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
5298 {
5299 handle_stop (it);
5300
5301 /* Since a handler may have changed IT->method, we must
5302 recurse here. */
5303 return get_next_display_element (it);
5304 }
5305
5306 if (it->current.overlay_string_index >= 0)
5307 {
5308 /* Get the next character from an overlay string. In overlay
5309 strings, There is no field width or padding with spaces to
5310 do. */
5311 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
5312 {
5313 it->what = IT_EOB;
5314 return 0;
5315 }
5316 else if (STRING_MULTIBYTE (it->string))
5317 {
5318 int remaining = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
5319 const unsigned char *s = (SDATA (it->string)
5320 + IT_STRING_BYTEPOS (*it));
5321 it->c = string_char_and_length (s, remaining, &it->len);
5322 }
5323 else
5324 {
5325 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
5326 it->len = 1;
5327 }
5328 }
5329 else
5330 {
5331 /* Get the next character from a Lisp string that is not an
5332 overlay string. Such strings come from the mode line, for
5333 example. We may have to pad with spaces, or truncate the
5334 string. See also next_element_from_c_string. */
5335 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
5336 {
5337 it->what = IT_EOB;
5338 return 0;
5339 }
5340 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
5341 {
5342 /* Pad with spaces. */
5343 it->c = ' ', it->len = 1;
5344 CHARPOS (position) = BYTEPOS (position) = -1;
5345 }
5346 else if (STRING_MULTIBYTE (it->string))
5347 {
5348 int maxlen = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
5349 const unsigned char *s = (SDATA (it->string)
5350 + IT_STRING_BYTEPOS (*it));
5351 it->c = string_char_and_length (s, maxlen, &it->len);
5352 }
5353 else
5354 {
5355 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
5356 it->len = 1;
5357 }
5358 }
5359
5360 /* Record what we have and where it came from. Note that we store a
5361 buffer position in IT->position although it could arguably be a
5362 string position. */
5363 it->what = IT_CHARACTER;
5364 it->object = it->string;
5365 it->position = position;
5366 return 1;
5367 }
5368
5369
5370 /* Load IT with next display element from C string IT->s.
5371 IT->string_nchars is the maximum number of characters to return
5372 from the string. IT->end_charpos may be greater than
5373 IT->string_nchars when this function is called, in which case we
5374 may have to return padding spaces. Value is zero if end of string
5375 reached, including padding spaces. */
5376
5377 static int
5378 next_element_from_c_string (it)
5379 struct it *it;
5380 {
5381 int success_p = 1;
5382
5383 xassert (it->s);
5384 it->what = IT_CHARACTER;
5385 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
5386 it->object = Qnil;
5387
5388 /* IT's position can be greater IT->string_nchars in case a field
5389 width or precision has been specified when the iterator was
5390 initialized. */
5391 if (IT_CHARPOS (*it) >= it->end_charpos)
5392 {
5393 /* End of the game. */
5394 it->what = IT_EOB;
5395 success_p = 0;
5396 }
5397 else if (IT_CHARPOS (*it) >= it->string_nchars)
5398 {
5399 /* Pad with spaces. */
5400 it->c = ' ', it->len = 1;
5401 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
5402 }
5403 else if (it->multibyte_p)
5404 {
5405 /* Implementation note: The calls to strlen apparently aren't a
5406 performance problem because there is no noticeable performance
5407 difference between Emacs running in unibyte or multibyte mode. */
5408 int maxlen = strlen (it->s) - IT_BYTEPOS (*it);
5409 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it),
5410 maxlen, &it->len);
5411 }
5412 else
5413 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
5414
5415 return success_p;
5416 }
5417
5418
5419 /* Set up IT to return characters from an ellipsis, if appropriate.
5420 The definition of the ellipsis glyphs may come from a display table
5421 entry. This function Fills IT with the first glyph from the
5422 ellipsis if an ellipsis is to be displayed. */
5423
5424 static int
5425 next_element_from_ellipsis (it)
5426 struct it *it;
5427 {
5428 if (it->selective_display_ellipsis_p)
5429 setup_for_ellipsis (it, it->len);
5430 else
5431 {
5432 /* The face at the current position may be different from the
5433 face we find after the invisible text. Remember what it
5434 was in IT->saved_face_id, and signal that it's there by
5435 setting face_before_selective_p. */
5436 it->saved_face_id = it->face_id;
5437 it->method = next_element_from_buffer;
5438 reseat_at_next_visible_line_start (it, 1);
5439 it->face_before_selective_p = 1;
5440 }
5441
5442 return get_next_display_element (it);
5443 }
5444
5445
5446 /* Deliver an image display element. The iterator IT is already
5447 filled with image information (done in handle_display_prop). Value
5448 is always 1. */
5449
5450
5451 static int
5452 next_element_from_image (it)
5453 struct it *it;
5454 {
5455 it->what = IT_IMAGE;
5456 return 1;
5457 }
5458
5459
5460 /* Fill iterator IT with next display element from a stretch glyph
5461 property. IT->object is the value of the text property. Value is
5462 always 1. */
5463
5464 static int
5465 next_element_from_stretch (it)
5466 struct it *it;
5467 {
5468 it->what = IT_STRETCH;
5469 return 1;
5470 }
5471
5472
5473 /* Load IT with the next display element from current_buffer. Value
5474 is zero if end of buffer reached. IT->stop_charpos is the next
5475 position at which to stop and check for text properties or buffer
5476 end. */
5477
5478 static int
5479 next_element_from_buffer (it)
5480 struct it *it;
5481 {
5482 int success_p = 1;
5483
5484 /* Check this assumption, otherwise, we would never enter the
5485 if-statement, below. */
5486 xassert (IT_CHARPOS (*it) >= BEGV
5487 && IT_CHARPOS (*it) <= it->stop_charpos);
5488
5489 if (IT_CHARPOS (*it) >= it->stop_charpos)
5490 {
5491 if (IT_CHARPOS (*it) >= it->end_charpos)
5492 {
5493 int overlay_strings_follow_p;
5494
5495 /* End of the game, except when overlay strings follow that
5496 haven't been returned yet. */
5497 if (it->overlay_strings_at_end_processed_p)
5498 overlay_strings_follow_p = 0;
5499 else
5500 {
5501 it->overlay_strings_at_end_processed_p = 1;
5502 overlay_strings_follow_p = get_overlay_strings (it, 0);
5503 }
5504
5505 if (overlay_strings_follow_p)
5506 success_p = get_next_display_element (it);
5507 else
5508 {
5509 it->what = IT_EOB;
5510 it->position = it->current.pos;
5511 success_p = 0;
5512 }
5513 }
5514 else
5515 {
5516 handle_stop (it);
5517 return get_next_display_element (it);
5518 }
5519 }
5520 else
5521 {
5522 /* No face changes, overlays etc. in sight, so just return a
5523 character from current_buffer. */
5524 unsigned char *p;
5525
5526 /* Maybe run the redisplay end trigger hook. Performance note:
5527 This doesn't seem to cost measurable time. */
5528 if (it->redisplay_end_trigger_charpos
5529 && it->glyph_row
5530 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
5531 run_redisplay_end_trigger_hook (it);
5532
5533 /* Get the next character, maybe multibyte. */
5534 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
5535 if (it->multibyte_p && !ASCII_BYTE_P (*p))
5536 {
5537 int maxlen = ((IT_BYTEPOS (*it) >= GPT_BYTE ? ZV_BYTE : GPT_BYTE)
5538 - IT_BYTEPOS (*it));
5539 it->c = string_char_and_length (p, maxlen, &it->len);
5540 }
5541 else
5542 it->c = *p, it->len = 1;
5543
5544 /* Record what we have and where it came from. */
5545 it->what = IT_CHARACTER;;
5546 it->object = it->w->buffer;
5547 it->position = it->current.pos;
5548
5549 /* Normally we return the character found above, except when we
5550 really want to return an ellipsis for selective display. */
5551 if (it->selective)
5552 {
5553 if (it->c == '\n')
5554 {
5555 /* A value of selective > 0 means hide lines indented more
5556 than that number of columns. */
5557 if (it->selective > 0
5558 && IT_CHARPOS (*it) + 1 < ZV
5559 && indented_beyond_p (IT_CHARPOS (*it) + 1,
5560 IT_BYTEPOS (*it) + 1,
5561 (double) it->selective)) /* iftc */
5562 {
5563 success_p = next_element_from_ellipsis (it);
5564 it->dpvec_char_len = -1;
5565 }
5566 }
5567 else if (it->c == '\r' && it->selective == -1)
5568 {
5569 /* A value of selective == -1 means that everything from the
5570 CR to the end of the line is invisible, with maybe an
5571 ellipsis displayed for it. */
5572 success_p = next_element_from_ellipsis (it);
5573 it->dpvec_char_len = -1;
5574 }
5575 }
5576 }
5577
5578 /* Value is zero if end of buffer reached. */
5579 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
5580 return success_p;
5581 }
5582
5583
5584 /* Run the redisplay end trigger hook for IT. */
5585
5586 static void
5587 run_redisplay_end_trigger_hook (it)
5588 struct it *it;
5589 {
5590 Lisp_Object args[3];
5591
5592 /* IT->glyph_row should be non-null, i.e. we should be actually
5593 displaying something, or otherwise we should not run the hook. */
5594 xassert (it->glyph_row);
5595
5596 /* Set up hook arguments. */
5597 args[0] = Qredisplay_end_trigger_functions;
5598 args[1] = it->window;
5599 XSETINT (args[2], it->redisplay_end_trigger_charpos);
5600 it->redisplay_end_trigger_charpos = 0;
5601
5602 /* Since we are *trying* to run these functions, don't try to run
5603 them again, even if they get an error. */
5604 it->w->redisplay_end_trigger = Qnil;
5605 Frun_hook_with_args (3, args);
5606
5607 /* Notice if it changed the face of the character we are on. */
5608 handle_face_prop (it);
5609 }
5610
5611
5612 /* Deliver a composition display element. The iterator IT is already
5613 filled with composition information (done in
5614 handle_composition_prop). Value is always 1. */
5615
5616 static int
5617 next_element_from_composition (it)
5618 struct it *it;
5619 {
5620 it->what = IT_COMPOSITION;
5621 it->position = (STRINGP (it->string)
5622 ? it->current.string_pos
5623 : it->current.pos);
5624 return 1;
5625 }
5626
5627
5628 \f
5629 /***********************************************************************
5630 Moving an iterator without producing glyphs
5631 ***********************************************************************/
5632
5633 /* Move iterator IT to a specified buffer or X position within one
5634 line on the display without producing glyphs.
5635
5636 OP should be a bit mask including some or all of these bits:
5637 MOVE_TO_X: Stop on reaching x-position TO_X.
5638 MOVE_TO_POS: Stop on reaching buffer or string position TO_CHARPOS.
5639 Regardless of OP's value, stop in reaching the end of the display line.
5640
5641 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
5642 This means, in particular, that TO_X includes window's horizontal
5643 scroll amount.
5644
5645 The return value has several possible values that
5646 say what condition caused the scan to stop:
5647
5648 MOVE_POS_MATCH_OR_ZV
5649 - when TO_POS or ZV was reached.
5650
5651 MOVE_X_REACHED
5652 -when TO_X was reached before TO_POS or ZV were reached.
5653
5654 MOVE_LINE_CONTINUED
5655 - when we reached the end of the display area and the line must
5656 be continued.
5657
5658 MOVE_LINE_TRUNCATED
5659 - when we reached the end of the display area and the line is
5660 truncated.
5661
5662 MOVE_NEWLINE_OR_CR
5663 - when we stopped at a line end, i.e. a newline or a CR and selective
5664 display is on. */
5665
5666 static enum move_it_result
5667 move_it_in_display_line_to (it, to_charpos, to_x, op)
5668 struct it *it;
5669 int to_charpos, to_x, op;
5670 {
5671 enum move_it_result result = MOVE_UNDEFINED;
5672 struct glyph_row *saved_glyph_row;
5673
5674 /* Don't produce glyphs in produce_glyphs. */
5675 saved_glyph_row = it->glyph_row;
5676 it->glyph_row = NULL;
5677
5678 #define BUFFER_POS_REACHED_P() \
5679 ((op & MOVE_TO_POS) != 0 \
5680 && BUFFERP (it->object) \
5681 && IT_CHARPOS (*it) >= to_charpos)
5682
5683 while (1)
5684 {
5685 int x, i, ascent = 0, descent = 0;
5686
5687 /* Stop when ZV reached.
5688 We used to stop here when TO_CHARPOS reached as well, but that is
5689 too soon if this glyph does not fit on this line. So we handle it
5690 explicitly below. */
5691 if (!get_next_display_element (it)
5692 || (it->truncate_lines_p
5693 && BUFFER_POS_REACHED_P ()))
5694 {
5695 result = MOVE_POS_MATCH_OR_ZV;
5696 break;
5697 }
5698
5699 /* The call to produce_glyphs will get the metrics of the
5700 display element IT is loaded with. We record in x the
5701 x-position before this display element in case it does not
5702 fit on the line. */
5703 x = it->current_x;
5704
5705 /* Remember the line height so far in case the next element doesn't
5706 fit on the line. */
5707 if (!it->truncate_lines_p)
5708 {
5709 ascent = it->max_ascent;
5710 descent = it->max_descent;
5711 }
5712
5713 PRODUCE_GLYPHS (it);
5714
5715 if (it->area != TEXT_AREA)
5716 {
5717 set_iterator_to_next (it, 1);
5718 continue;
5719 }
5720
5721 /* The number of glyphs we get back in IT->nglyphs will normally
5722 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
5723 character on a terminal frame, or (iii) a line end. For the
5724 second case, IT->nglyphs - 1 padding glyphs will be present
5725 (on X frames, there is only one glyph produced for a
5726 composite character.
5727
5728 The behavior implemented below means, for continuation lines,
5729 that as many spaces of a TAB as fit on the current line are
5730 displayed there. For terminal frames, as many glyphs of a
5731 multi-glyph character are displayed in the current line, too.
5732 This is what the old redisplay code did, and we keep it that
5733 way. Under X, the whole shape of a complex character must
5734 fit on the line or it will be completely displayed in the
5735 next line.
5736
5737 Note that both for tabs and padding glyphs, all glyphs have
5738 the same width. */
5739 if (it->nglyphs)
5740 {
5741 /* More than one glyph or glyph doesn't fit on line. All
5742 glyphs have the same width. */
5743 int single_glyph_width = it->pixel_width / it->nglyphs;
5744 int new_x;
5745
5746 for (i = 0; i < it->nglyphs; ++i, x = new_x)
5747 {
5748 new_x = x + single_glyph_width;
5749
5750 /* We want to leave anything reaching TO_X to the caller. */
5751 if ((op & MOVE_TO_X) && new_x > to_x)
5752 {
5753 if (BUFFER_POS_REACHED_P ())
5754 goto buffer_pos_reached;
5755 it->current_x = x;
5756 result = MOVE_X_REACHED;
5757 break;
5758 }
5759 else if (/* Lines are continued. */
5760 !it->truncate_lines_p
5761 && (/* And glyph doesn't fit on the line. */
5762 new_x > it->last_visible_x
5763 /* Or it fits exactly and we're on a window
5764 system frame. */
5765 || (new_x == it->last_visible_x
5766 && FRAME_WINDOW_P (it->f))))
5767 {
5768 if (/* IT->hpos == 0 means the very first glyph
5769 doesn't fit on the line, e.g. a wide image. */
5770 it->hpos == 0
5771 || (new_x == it->last_visible_x
5772 && FRAME_WINDOW_P (it->f)))
5773 {
5774 ++it->hpos;
5775 it->current_x = new_x;
5776 if (i == it->nglyphs - 1)
5777 {
5778 set_iterator_to_next (it, 1);
5779 #ifdef HAVE_WINDOW_SYSTEM
5780 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
5781 {
5782 if (!get_next_display_element (it))
5783 {
5784 result = MOVE_POS_MATCH_OR_ZV;
5785 break;
5786 }
5787 if (BUFFER_POS_REACHED_P ())
5788 {
5789 if (ITERATOR_AT_END_OF_LINE_P (it))
5790 result = MOVE_POS_MATCH_OR_ZV;
5791 else
5792 result = MOVE_LINE_CONTINUED;
5793 break;
5794 }
5795 if (ITERATOR_AT_END_OF_LINE_P (it))
5796 {
5797 result = MOVE_NEWLINE_OR_CR;
5798 break;
5799 }
5800 }
5801 #endif /* HAVE_WINDOW_SYSTEM */
5802 }
5803 }
5804 else
5805 {
5806 it->current_x = x;
5807 it->max_ascent = ascent;
5808 it->max_descent = descent;
5809 }
5810
5811 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
5812 IT_CHARPOS (*it)));
5813 result = MOVE_LINE_CONTINUED;
5814 break;
5815 }
5816 else if (BUFFER_POS_REACHED_P ())
5817 goto buffer_pos_reached;
5818 else if (new_x > it->first_visible_x)
5819 {
5820 /* Glyph is visible. Increment number of glyphs that
5821 would be displayed. */
5822 ++it->hpos;
5823 }
5824 else
5825 {
5826 /* Glyph is completely off the left margin of the display
5827 area. Nothing to do. */
5828 }
5829 }
5830
5831 if (result != MOVE_UNDEFINED)
5832 break;
5833 }
5834 else if (BUFFER_POS_REACHED_P ())
5835 {
5836 buffer_pos_reached:
5837 it->current_x = x;
5838 it->max_ascent = ascent;
5839 it->max_descent = descent;
5840 result = MOVE_POS_MATCH_OR_ZV;
5841 break;
5842 }
5843 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
5844 {
5845 /* Stop when TO_X specified and reached. This check is
5846 necessary here because of lines consisting of a line end,
5847 only. The line end will not produce any glyphs and we
5848 would never get MOVE_X_REACHED. */
5849 xassert (it->nglyphs == 0);
5850 result = MOVE_X_REACHED;
5851 break;
5852 }
5853
5854 /* Is this a line end? If yes, we're done. */
5855 if (ITERATOR_AT_END_OF_LINE_P (it))
5856 {
5857 result = MOVE_NEWLINE_OR_CR;
5858 break;
5859 }
5860
5861 /* The current display element has been consumed. Advance
5862 to the next. */
5863 set_iterator_to_next (it, 1);
5864
5865 /* Stop if lines are truncated and IT's current x-position is
5866 past the right edge of the window now. */
5867 if (it->truncate_lines_p
5868 && it->current_x >= it->last_visible_x)
5869 {
5870 #ifdef HAVE_WINDOW_SYSTEM
5871 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
5872 {
5873 if (!get_next_display_element (it)
5874 || BUFFER_POS_REACHED_P ())
5875 {
5876 result = MOVE_POS_MATCH_OR_ZV;
5877 break;
5878 }
5879 if (ITERATOR_AT_END_OF_LINE_P (it))
5880 {
5881 result = MOVE_NEWLINE_OR_CR;
5882 break;
5883 }
5884 }
5885 #endif /* HAVE_WINDOW_SYSTEM */
5886 result = MOVE_LINE_TRUNCATED;
5887 break;
5888 }
5889 }
5890
5891 #undef BUFFER_POS_REACHED_P
5892
5893 /* Restore the iterator settings altered at the beginning of this
5894 function. */
5895 it->glyph_row = saved_glyph_row;
5896 return result;
5897 }
5898
5899
5900 /* Move IT forward until it satisfies one or more of the criteria in
5901 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
5902
5903 OP is a bit-mask that specifies where to stop, and in particular,
5904 which of those four position arguments makes a difference. See the
5905 description of enum move_operation_enum.
5906
5907 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
5908 screen line, this function will set IT to the next position >
5909 TO_CHARPOS. */
5910
5911 void
5912 move_it_to (it, to_charpos, to_x, to_y, to_vpos, op)
5913 struct it *it;
5914 int to_charpos, to_x, to_y, to_vpos;
5915 int op;
5916 {
5917 enum move_it_result skip, skip2 = MOVE_X_REACHED;
5918 int line_height;
5919 int reached = 0;
5920
5921 for (;;)
5922 {
5923 if (op & MOVE_TO_VPOS)
5924 {
5925 /* If no TO_CHARPOS and no TO_X specified, stop at the
5926 start of the line TO_VPOS. */
5927 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
5928 {
5929 if (it->vpos == to_vpos)
5930 {
5931 reached = 1;
5932 break;
5933 }
5934 else
5935 skip = move_it_in_display_line_to (it, -1, -1, 0);
5936 }
5937 else
5938 {
5939 /* TO_VPOS >= 0 means stop at TO_X in the line at
5940 TO_VPOS, or at TO_POS, whichever comes first. */
5941 if (it->vpos == to_vpos)
5942 {
5943 reached = 2;
5944 break;
5945 }
5946
5947 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
5948
5949 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
5950 {
5951 reached = 3;
5952 break;
5953 }
5954 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
5955 {
5956 /* We have reached TO_X but not in the line we want. */
5957 skip = move_it_in_display_line_to (it, to_charpos,
5958 -1, MOVE_TO_POS);
5959 if (skip == MOVE_POS_MATCH_OR_ZV)
5960 {
5961 reached = 4;
5962 break;
5963 }
5964 }
5965 }
5966 }
5967 else if (op & MOVE_TO_Y)
5968 {
5969 struct it it_backup;
5970
5971 /* TO_Y specified means stop at TO_X in the line containing
5972 TO_Y---or at TO_CHARPOS if this is reached first. The
5973 problem is that we can't really tell whether the line
5974 contains TO_Y before we have completely scanned it, and
5975 this may skip past TO_X. What we do is to first scan to
5976 TO_X.
5977
5978 If TO_X is not specified, use a TO_X of zero. The reason
5979 is to make the outcome of this function more predictable.
5980 If we didn't use TO_X == 0, we would stop at the end of
5981 the line which is probably not what a caller would expect
5982 to happen. */
5983 skip = move_it_in_display_line_to (it, to_charpos,
5984 ((op & MOVE_TO_X)
5985 ? to_x : 0),
5986 (MOVE_TO_X
5987 | (op & MOVE_TO_POS)));
5988
5989 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
5990 if (skip == MOVE_POS_MATCH_OR_ZV)
5991 {
5992 reached = 5;
5993 break;
5994 }
5995
5996 /* If TO_X was reached, we would like to know whether TO_Y
5997 is in the line. This can only be said if we know the
5998 total line height which requires us to scan the rest of
5999 the line. */
6000 if (skip == MOVE_X_REACHED)
6001 {
6002 it_backup = *it;
6003 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
6004 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
6005 op & MOVE_TO_POS);
6006 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
6007 }
6008
6009 /* Now, decide whether TO_Y is in this line. */
6010 line_height = it->max_ascent + it->max_descent;
6011 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
6012
6013 if (to_y >= it->current_y
6014 && to_y < it->current_y + line_height)
6015 {
6016 if (skip == MOVE_X_REACHED)
6017 /* If TO_Y is in this line and TO_X was reached above,
6018 we scanned too far. We have to restore IT's settings
6019 to the ones before skipping. */
6020 *it = it_backup;
6021 reached = 6;
6022 }
6023 else if (skip == MOVE_X_REACHED)
6024 {
6025 skip = skip2;
6026 if (skip == MOVE_POS_MATCH_OR_ZV)
6027 reached = 7;
6028 }
6029
6030 if (reached)
6031 break;
6032 }
6033 else
6034 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
6035
6036 switch (skip)
6037 {
6038 case MOVE_POS_MATCH_OR_ZV:
6039 reached = 8;
6040 goto out;
6041
6042 case MOVE_NEWLINE_OR_CR:
6043 set_iterator_to_next (it, 1);
6044 it->continuation_lines_width = 0;
6045 break;
6046
6047 case MOVE_LINE_TRUNCATED:
6048 it->continuation_lines_width = 0;
6049 reseat_at_next_visible_line_start (it, 0);
6050 if ((op & MOVE_TO_POS) != 0
6051 && IT_CHARPOS (*it) > to_charpos)
6052 {
6053 reached = 9;
6054 goto out;
6055 }
6056 break;
6057
6058 case MOVE_LINE_CONTINUED:
6059 it->continuation_lines_width += it->current_x;
6060 break;
6061
6062 default:
6063 abort ();
6064 }
6065
6066 /* Reset/increment for the next run. */
6067 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
6068 it->current_x = it->hpos = 0;
6069 it->current_y += it->max_ascent + it->max_descent;
6070 ++it->vpos;
6071 last_height = it->max_ascent + it->max_descent;
6072 last_max_ascent = it->max_ascent;
6073 it->max_ascent = it->max_descent = 0;
6074 }
6075
6076 out:
6077
6078 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
6079 }
6080
6081
6082 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
6083
6084 If DY > 0, move IT backward at least that many pixels. DY = 0
6085 means move IT backward to the preceding line start or BEGV. This
6086 function may move over more than DY pixels if IT->current_y - DY
6087 ends up in the middle of a line; in this case IT->current_y will be
6088 set to the top of the line moved to. */
6089
6090 void
6091 move_it_vertically_backward (it, dy)
6092 struct it *it;
6093 int dy;
6094 {
6095 int nlines, h;
6096 struct it it2, it3;
6097 int start_pos;
6098
6099 move_further_back:
6100 xassert (dy >= 0);
6101
6102 start_pos = IT_CHARPOS (*it);
6103
6104 /* Estimate how many newlines we must move back. */
6105 nlines = max (1, dy / FRAME_LINE_HEIGHT (it->f));
6106
6107 /* Set the iterator's position that many lines back. */
6108 while (nlines-- && IT_CHARPOS (*it) > BEGV)
6109 back_to_previous_visible_line_start (it);
6110
6111 /* Reseat the iterator here. When moving backward, we don't want
6112 reseat to skip forward over invisible text, set up the iterator
6113 to deliver from overlay strings at the new position etc. So,
6114 use reseat_1 here. */
6115 reseat_1 (it, it->current.pos, 1);
6116
6117 /* We are now surely at a line start. */
6118 it->current_x = it->hpos = 0;
6119 it->continuation_lines_width = 0;
6120
6121 /* Move forward and see what y-distance we moved. First move to the
6122 start of the next line so that we get its height. We need this
6123 height to be able to tell whether we reached the specified
6124 y-distance. */
6125 it2 = *it;
6126 it2.max_ascent = it2.max_descent = 0;
6127 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
6128 MOVE_TO_POS | MOVE_TO_VPOS);
6129 xassert (IT_CHARPOS (*it) >= BEGV);
6130 it3 = it2;
6131
6132 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
6133 xassert (IT_CHARPOS (*it) >= BEGV);
6134 /* H is the actual vertical distance from the position in *IT
6135 and the starting position. */
6136 h = it2.current_y - it->current_y;
6137 /* NLINES is the distance in number of lines. */
6138 nlines = it2.vpos - it->vpos;
6139
6140 /* Correct IT's y and vpos position
6141 so that they are relative to the starting point. */
6142 it->vpos -= nlines;
6143 it->current_y -= h;
6144
6145 if (dy == 0)
6146 {
6147 /* DY == 0 means move to the start of the screen line. The
6148 value of nlines is > 0 if continuation lines were involved. */
6149 if (nlines > 0)
6150 move_it_by_lines (it, nlines, 1);
6151 xassert (IT_CHARPOS (*it) <= start_pos);
6152 }
6153 else
6154 {
6155 /* The y-position we try to reach, relative to *IT.
6156 Note that H has been subtracted in front of the if-statement. */
6157 int target_y = it->current_y + h - dy;
6158 int y0 = it3.current_y;
6159 int y1 = line_bottom_y (&it3);
6160 int line_height = y1 - y0;
6161
6162 /* If we did not reach target_y, try to move further backward if
6163 we can. If we moved too far backward, try to move forward. */
6164 if (target_y < it->current_y
6165 /* This is heuristic. In a window that's 3 lines high, with
6166 a line height of 13 pixels each, recentering with point
6167 on the bottom line will try to move -39/2 = 19 pixels
6168 backward. Try to avoid moving into the first line. */
6169 && it->current_y - target_y > line_height * 2 / 3
6170 && IT_CHARPOS (*it) > BEGV)
6171 {
6172 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
6173 target_y - it->current_y));
6174 dy = it->current_y - target_y;
6175 goto move_further_back;
6176 }
6177 else if (target_y >= it->current_y + line_height
6178 && IT_CHARPOS (*it) < ZV)
6179 {
6180 /* Should move forward by at least one line, maybe more.
6181
6182 Note: Calling move_it_by_lines can be expensive on
6183 terminal frames, where compute_motion is used (via
6184 vmotion) to do the job, when there are very long lines
6185 and truncate-lines is nil. That's the reason for
6186 treating terminal frames specially here. */
6187
6188 if (!FRAME_WINDOW_P (it->f))
6189 move_it_vertically (it, target_y - (it->current_y + line_height));
6190 else
6191 {
6192 do
6193 {
6194 move_it_by_lines (it, 1, 1);
6195 }
6196 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
6197 }
6198
6199 xassert (IT_CHARPOS (*it) >= BEGV);
6200 }
6201 }
6202 }
6203
6204
6205 /* Move IT by a specified amount of pixel lines DY. DY negative means
6206 move backwards. DY = 0 means move to start of screen line. At the
6207 end, IT will be on the start of a screen line. */
6208
6209 void
6210 move_it_vertically (it, dy)
6211 struct it *it;
6212 int dy;
6213 {
6214 if (dy <= 0)
6215 move_it_vertically_backward (it, -dy);
6216 else
6217 {
6218 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
6219 move_it_to (it, ZV, -1, it->current_y + dy, -1,
6220 MOVE_TO_POS | MOVE_TO_Y);
6221 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
6222
6223 /* If buffer ends in ZV without a newline, move to the start of
6224 the line to satisfy the post-condition. */
6225 if (IT_CHARPOS (*it) == ZV
6226 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
6227 move_it_by_lines (it, 0, 0);
6228 }
6229 }
6230
6231
6232 /* Move iterator IT past the end of the text line it is in. */
6233
6234 void
6235 move_it_past_eol (it)
6236 struct it *it;
6237 {
6238 enum move_it_result rc;
6239
6240 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
6241 if (rc == MOVE_NEWLINE_OR_CR)
6242 set_iterator_to_next (it, 0);
6243 }
6244
6245
6246 #if 0 /* Currently not used. */
6247
6248 /* Return non-zero if some text between buffer positions START_CHARPOS
6249 and END_CHARPOS is invisible. IT->window is the window for text
6250 property lookup. */
6251
6252 static int
6253 invisible_text_between_p (it, start_charpos, end_charpos)
6254 struct it *it;
6255 int start_charpos, end_charpos;
6256 {
6257 Lisp_Object prop, limit;
6258 int invisible_found_p;
6259
6260 xassert (it != NULL && start_charpos <= end_charpos);
6261
6262 /* Is text at START invisible? */
6263 prop = Fget_char_property (make_number (start_charpos), Qinvisible,
6264 it->window);
6265 if (TEXT_PROP_MEANS_INVISIBLE (prop))
6266 invisible_found_p = 1;
6267 else
6268 {
6269 limit = Fnext_single_char_property_change (make_number (start_charpos),
6270 Qinvisible, Qnil,
6271 make_number (end_charpos));
6272 invisible_found_p = XFASTINT (limit) < end_charpos;
6273 }
6274
6275 return invisible_found_p;
6276 }
6277
6278 #endif /* 0 */
6279
6280
6281 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
6282 negative means move up. DVPOS == 0 means move to the start of the
6283 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
6284 NEED_Y_P is zero, IT->current_y will be left unchanged.
6285
6286 Further optimization ideas: If we would know that IT->f doesn't use
6287 a face with proportional font, we could be faster for
6288 truncate-lines nil. */
6289
6290 void
6291 move_it_by_lines (it, dvpos, need_y_p)
6292 struct it *it;
6293 int dvpos, need_y_p;
6294 {
6295 struct position pos;
6296
6297 if (!FRAME_WINDOW_P (it->f))
6298 {
6299 struct text_pos textpos;
6300
6301 /* We can use vmotion on frames without proportional fonts. */
6302 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
6303 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
6304 reseat (it, textpos, 1);
6305 it->vpos += pos.vpos;
6306 it->current_y += pos.vpos;
6307 }
6308 else if (dvpos == 0)
6309 {
6310 /* DVPOS == 0 means move to the start of the screen line. */
6311 move_it_vertically_backward (it, 0);
6312 xassert (it->current_x == 0 && it->hpos == 0);
6313 /* Let next call to line_bottom_y calculate real line height */
6314 last_height = 0;
6315 }
6316 else if (dvpos > 0)
6317 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
6318 else
6319 {
6320 struct it it2;
6321 int start_charpos, i;
6322
6323 /* Start at the beginning of the screen line containing IT's
6324 position. */
6325 move_it_vertically_backward (it, 0);
6326
6327 /* Go back -DVPOS visible lines and reseat the iterator there. */
6328 start_charpos = IT_CHARPOS (*it);
6329 for (i = -dvpos; i && IT_CHARPOS (*it) > BEGV; --i)
6330 back_to_previous_visible_line_start (it);
6331 reseat (it, it->current.pos, 1);
6332 it->current_x = it->hpos = 0;
6333
6334 /* Above call may have moved too far if continuation lines
6335 are involved. Scan forward and see if it did. */
6336 it2 = *it;
6337 it2.vpos = it2.current_y = 0;
6338 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
6339 it->vpos -= it2.vpos;
6340 it->current_y -= it2.current_y;
6341 it->current_x = it->hpos = 0;
6342
6343 /* If we moved too far, move IT some lines forward. */
6344 if (it2.vpos > -dvpos)
6345 {
6346 int delta = it2.vpos + dvpos;
6347 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
6348 }
6349 }
6350 }
6351
6352 /* Return 1 if IT points into the middle of a display vector. */
6353
6354 int
6355 in_display_vector_p (it)
6356 struct it *it;
6357 {
6358 return (it->method == next_element_from_display_vector
6359 && it->current.dpvec_index > 0
6360 && it->dpvec + it->current.dpvec_index != it->dpend);
6361 }
6362
6363 \f
6364 /***********************************************************************
6365 Messages
6366 ***********************************************************************/
6367
6368
6369 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
6370 to *Messages*. */
6371
6372 void
6373 add_to_log (format, arg1, arg2)
6374 char *format;
6375 Lisp_Object arg1, arg2;
6376 {
6377 Lisp_Object args[3];
6378 Lisp_Object msg, fmt;
6379 char *buffer;
6380 int len;
6381 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
6382 USE_SAFE_ALLOCA;
6383
6384 /* Do nothing if called asynchronously. Inserting text into
6385 a buffer may call after-change-functions and alike and
6386 that would means running Lisp asynchronously. */
6387 if (handling_signal)
6388 return;
6389
6390 fmt = msg = Qnil;
6391 GCPRO4 (fmt, msg, arg1, arg2);
6392
6393 args[0] = fmt = build_string (format);
6394 args[1] = arg1;
6395 args[2] = arg2;
6396 msg = Fformat (3, args);
6397
6398 len = SBYTES (msg) + 1;
6399 SAFE_ALLOCA (buffer, char *, len);
6400 bcopy (SDATA (msg), buffer, len);
6401
6402 message_dolog (buffer, len - 1, 1, 0);
6403 SAFE_FREE ();
6404
6405 UNGCPRO;
6406 }
6407
6408
6409 /* Output a newline in the *Messages* buffer if "needs" one. */
6410
6411 void
6412 message_log_maybe_newline ()
6413 {
6414 if (message_log_need_newline)
6415 message_dolog ("", 0, 1, 0);
6416 }
6417
6418
6419 /* Add a string M of length NBYTES to the message log, optionally
6420 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
6421 nonzero, means interpret the contents of M as multibyte. This
6422 function calls low-level routines in order to bypass text property
6423 hooks, etc. which might not be safe to run. */
6424
6425 void
6426 message_dolog (m, nbytes, nlflag, multibyte)
6427 const char *m;
6428 int nbytes, nlflag, multibyte;
6429 {
6430 if (!NILP (Vmemory_full))
6431 return;
6432
6433 if (!NILP (Vmessage_log_max))
6434 {
6435 struct buffer *oldbuf;
6436 Lisp_Object oldpoint, oldbegv, oldzv;
6437 int old_windows_or_buffers_changed = windows_or_buffers_changed;
6438 int point_at_end = 0;
6439 int zv_at_end = 0;
6440 Lisp_Object old_deactivate_mark, tem;
6441 struct gcpro gcpro1;
6442
6443 old_deactivate_mark = Vdeactivate_mark;
6444 oldbuf = current_buffer;
6445 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
6446 current_buffer->undo_list = Qt;
6447
6448 oldpoint = message_dolog_marker1;
6449 set_marker_restricted (oldpoint, make_number (PT), Qnil);
6450 oldbegv = message_dolog_marker2;
6451 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
6452 oldzv = message_dolog_marker3;
6453 set_marker_restricted (oldzv, make_number (ZV), Qnil);
6454 GCPRO1 (old_deactivate_mark);
6455
6456 if (PT == Z)
6457 point_at_end = 1;
6458 if (ZV == Z)
6459 zv_at_end = 1;
6460
6461 BEGV = BEG;
6462 BEGV_BYTE = BEG_BYTE;
6463 ZV = Z;
6464 ZV_BYTE = Z_BYTE;
6465 TEMP_SET_PT_BOTH (Z, Z_BYTE);
6466
6467 /* Insert the string--maybe converting multibyte to single byte
6468 or vice versa, so that all the text fits the buffer. */
6469 if (multibyte
6470 && NILP (current_buffer->enable_multibyte_characters))
6471 {
6472 int i, c, char_bytes;
6473 unsigned char work[1];
6474
6475 /* Convert a multibyte string to single-byte
6476 for the *Message* buffer. */
6477 for (i = 0; i < nbytes; i += char_bytes)
6478 {
6479 c = string_char_and_length (m + i, nbytes - i, &char_bytes);
6480 work[0] = (SINGLE_BYTE_CHAR_P (c)
6481 ? c
6482 : multibyte_char_to_unibyte (c, Qnil));
6483 insert_1_both (work, 1, 1, 1, 0, 0);
6484 }
6485 }
6486 else if (! multibyte
6487 && ! NILP (current_buffer->enable_multibyte_characters))
6488 {
6489 int i, c, char_bytes;
6490 unsigned char *msg = (unsigned char *) m;
6491 unsigned char str[MAX_MULTIBYTE_LENGTH];
6492 /* Convert a single-byte string to multibyte
6493 for the *Message* buffer. */
6494 for (i = 0; i < nbytes; i++)
6495 {
6496 c = unibyte_char_to_multibyte (msg[i]);
6497 char_bytes = CHAR_STRING (c, str);
6498 insert_1_both (str, 1, char_bytes, 1, 0, 0);
6499 }
6500 }
6501 else if (nbytes)
6502 insert_1 (m, nbytes, 1, 0, 0);
6503
6504 if (nlflag)
6505 {
6506 int this_bol, this_bol_byte, prev_bol, prev_bol_byte, dup;
6507 insert_1 ("\n", 1, 1, 0, 0);
6508
6509 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
6510 this_bol = PT;
6511 this_bol_byte = PT_BYTE;
6512
6513 /* See if this line duplicates the previous one.
6514 If so, combine duplicates. */
6515 if (this_bol > BEG)
6516 {
6517 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
6518 prev_bol = PT;
6519 prev_bol_byte = PT_BYTE;
6520
6521 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
6522 this_bol, this_bol_byte);
6523 if (dup)
6524 {
6525 del_range_both (prev_bol, prev_bol_byte,
6526 this_bol, this_bol_byte, 0);
6527 if (dup > 1)
6528 {
6529 char dupstr[40];
6530 int duplen;
6531
6532 /* If you change this format, don't forget to also
6533 change message_log_check_duplicate. */
6534 sprintf (dupstr, " [%d times]", dup);
6535 duplen = strlen (dupstr);
6536 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
6537 insert_1 (dupstr, duplen, 1, 0, 1);
6538 }
6539 }
6540 }
6541
6542 /* If we have more than the desired maximum number of lines
6543 in the *Messages* buffer now, delete the oldest ones.
6544 This is safe because we don't have undo in this buffer. */
6545
6546 if (NATNUMP (Vmessage_log_max))
6547 {
6548 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
6549 -XFASTINT (Vmessage_log_max) - 1, 0);
6550 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
6551 }
6552 }
6553 BEGV = XMARKER (oldbegv)->charpos;
6554 BEGV_BYTE = marker_byte_position (oldbegv);
6555
6556 if (zv_at_end)
6557 {
6558 ZV = Z;
6559 ZV_BYTE = Z_BYTE;
6560 }
6561 else
6562 {
6563 ZV = XMARKER (oldzv)->charpos;
6564 ZV_BYTE = marker_byte_position (oldzv);
6565 }
6566
6567 if (point_at_end)
6568 TEMP_SET_PT_BOTH (Z, Z_BYTE);
6569 else
6570 /* We can't do Fgoto_char (oldpoint) because it will run some
6571 Lisp code. */
6572 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
6573 XMARKER (oldpoint)->bytepos);
6574
6575 UNGCPRO;
6576 unchain_marker (XMARKER (oldpoint));
6577 unchain_marker (XMARKER (oldbegv));
6578 unchain_marker (XMARKER (oldzv));
6579
6580 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
6581 set_buffer_internal (oldbuf);
6582 if (NILP (tem))
6583 windows_or_buffers_changed = old_windows_or_buffers_changed;
6584 message_log_need_newline = !nlflag;
6585 Vdeactivate_mark = old_deactivate_mark;
6586 }
6587 }
6588
6589
6590 /* We are at the end of the buffer after just having inserted a newline.
6591 (Note: We depend on the fact we won't be crossing the gap.)
6592 Check to see if the most recent message looks a lot like the previous one.
6593 Return 0 if different, 1 if the new one should just replace it, or a
6594 value N > 1 if we should also append " [N times]". */
6595
6596 static int
6597 message_log_check_duplicate (prev_bol, prev_bol_byte, this_bol, this_bol_byte)
6598 int prev_bol, this_bol;
6599 int prev_bol_byte, this_bol_byte;
6600 {
6601 int i;
6602 int len = Z_BYTE - 1 - this_bol_byte;
6603 int seen_dots = 0;
6604 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
6605 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
6606
6607 for (i = 0; i < len; i++)
6608 {
6609 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
6610 seen_dots = 1;
6611 if (p1[i] != p2[i])
6612 return seen_dots;
6613 }
6614 p1 += len;
6615 if (*p1 == '\n')
6616 return 2;
6617 if (*p1++ == ' ' && *p1++ == '[')
6618 {
6619 int n = 0;
6620 while (*p1 >= '0' && *p1 <= '9')
6621 n = n * 10 + *p1++ - '0';
6622 if (strncmp (p1, " times]\n", 8) == 0)
6623 return n+1;
6624 }
6625 return 0;
6626 }
6627
6628
6629 /* Display an echo area message M with a specified length of NBYTES
6630 bytes. The string may include null characters. If M is 0, clear
6631 out any existing message, and let the mini-buffer text show
6632 through.
6633
6634 The buffer M must continue to exist until after the echo area gets
6635 cleared or some other message gets displayed there. This means do
6636 not pass text that is stored in a Lisp string; do not pass text in
6637 a buffer that was alloca'd. */
6638
6639 void
6640 message2 (m, nbytes, multibyte)
6641 const char *m;
6642 int nbytes;
6643 int multibyte;
6644 {
6645 /* First flush out any partial line written with print. */
6646 message_log_maybe_newline ();
6647 if (m)
6648 message_dolog (m, nbytes, 1, multibyte);
6649 message2_nolog (m, nbytes, multibyte);
6650 }
6651
6652
6653 /* The non-logging counterpart of message2. */
6654
6655 void
6656 message2_nolog (m, nbytes, multibyte)
6657 const char *m;
6658 int nbytes, multibyte;
6659 {
6660 struct frame *sf = SELECTED_FRAME ();
6661 message_enable_multibyte = multibyte;
6662
6663 if (noninteractive)
6664 {
6665 if (noninteractive_need_newline)
6666 putc ('\n', stderr);
6667 noninteractive_need_newline = 0;
6668 if (m)
6669 fwrite (m, nbytes, 1, stderr);
6670 if (cursor_in_echo_area == 0)
6671 fprintf (stderr, "\n");
6672 fflush (stderr);
6673 }
6674 /* A null message buffer means that the frame hasn't really been
6675 initialized yet. Error messages get reported properly by
6676 cmd_error, so this must be just an informative message; toss it. */
6677 else if (INTERACTIVE
6678 && sf->glyphs_initialized_p
6679 && FRAME_MESSAGE_BUF (sf))
6680 {
6681 Lisp_Object mini_window;
6682 struct frame *f;
6683
6684 /* Get the frame containing the mini-buffer
6685 that the selected frame is using. */
6686 mini_window = FRAME_MINIBUF_WINDOW (sf);
6687 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
6688
6689 FRAME_SAMPLE_VISIBILITY (f);
6690 if (FRAME_VISIBLE_P (sf)
6691 && ! FRAME_VISIBLE_P (f))
6692 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
6693
6694 if (m)
6695 {
6696 set_message (m, Qnil, nbytes, multibyte);
6697 if (minibuffer_auto_raise)
6698 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
6699 }
6700 else
6701 clear_message (1, 1);
6702
6703 do_pending_window_change (0);
6704 echo_area_display (1);
6705 do_pending_window_change (0);
6706 if (FRAME_DISPLAY (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
6707 (*FRAME_DISPLAY (f)->frame_up_to_date_hook) (f);
6708 }
6709 }
6710
6711
6712 /* Display an echo area message M with a specified length of NBYTES
6713 bytes. The string may include null characters. If M is not a
6714 string, clear out any existing message, and let the mini-buffer
6715 text show through. */
6716
6717 void
6718 message3 (m, nbytes, multibyte)
6719 Lisp_Object m;
6720 int nbytes;
6721 int multibyte;
6722 {
6723 struct gcpro gcpro1;
6724
6725 GCPRO1 (m);
6726 clear_message (1,1);
6727
6728 /* First flush out any partial line written with print. */
6729 message_log_maybe_newline ();
6730 if (STRINGP (m))
6731 message_dolog (SDATA (m), nbytes, 1, multibyte);
6732 message3_nolog (m, nbytes, multibyte);
6733
6734 UNGCPRO;
6735 }
6736
6737
6738 /* The non-logging version of message3. */
6739
6740 void
6741 message3_nolog (m, nbytes, multibyte)
6742 Lisp_Object m;
6743 int nbytes, multibyte;
6744 {
6745 struct frame *sf = SELECTED_FRAME ();
6746 message_enable_multibyte = multibyte;
6747
6748 if (noninteractive)
6749 {
6750 if (noninteractive_need_newline)
6751 putc ('\n', stderr);
6752 noninteractive_need_newline = 0;
6753 if (STRINGP (m))
6754 fwrite (SDATA (m), nbytes, 1, stderr);
6755 if (cursor_in_echo_area == 0)
6756 fprintf (stderr, "\n");
6757 fflush (stderr);
6758 }
6759 /* A null message buffer means that the frame hasn't really been
6760 initialized yet. Error messages get reported properly by
6761 cmd_error, so this must be just an informative message; toss it. */
6762 else if (INTERACTIVE
6763 && sf->glyphs_initialized_p
6764 && FRAME_MESSAGE_BUF (sf))
6765 {
6766 Lisp_Object mini_window;
6767 Lisp_Object frame;
6768 struct frame *f;
6769
6770 /* Get the frame containing the mini-buffer
6771 that the selected frame is using. */
6772 mini_window = FRAME_MINIBUF_WINDOW (sf);
6773 frame = XWINDOW (mini_window)->frame;
6774 f = XFRAME (frame);
6775
6776 FRAME_SAMPLE_VISIBILITY (f);
6777 if (FRAME_VISIBLE_P (sf)
6778 && !FRAME_VISIBLE_P (f))
6779 Fmake_frame_visible (frame);
6780
6781 if (STRINGP (m) && SCHARS (m) > 0)
6782 {
6783 set_message (NULL, m, nbytes, multibyte);
6784 if (minibuffer_auto_raise)
6785 Fraise_frame (frame);
6786 }
6787 else
6788 clear_message (1, 1);
6789
6790 do_pending_window_change (0);
6791 echo_area_display (1);
6792 do_pending_window_change (0);
6793 if (FRAME_DISPLAY (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
6794 (*FRAME_DISPLAY (f)->frame_up_to_date_hook) (f);
6795 }
6796 }
6797
6798
6799 /* Display a null-terminated echo area message M. If M is 0, clear
6800 out any existing message, and let the mini-buffer text show through.
6801
6802 The buffer M must continue to exist until after the echo area gets
6803 cleared or some other message gets displayed there. Do not pass
6804 text that is stored in a Lisp string. Do not pass text in a buffer
6805 that was alloca'd. */
6806
6807 void
6808 message1 (m)
6809 char *m;
6810 {
6811 message2 (m, (m ? strlen (m) : 0), 0);
6812 }
6813
6814
6815 /* The non-logging counterpart of message1. */
6816
6817 void
6818 message1_nolog (m)
6819 char *m;
6820 {
6821 message2_nolog (m, (m ? strlen (m) : 0), 0);
6822 }
6823
6824 /* Display a message M which contains a single %s
6825 which gets replaced with STRING. */
6826
6827 void
6828 message_with_string (m, string, log)
6829 char *m;
6830 Lisp_Object string;
6831 int log;
6832 {
6833 CHECK_STRING (string);
6834
6835 if (noninteractive)
6836 {
6837 if (m)
6838 {
6839 if (noninteractive_need_newline)
6840 putc ('\n', stderr);
6841 noninteractive_need_newline = 0;
6842 fprintf (stderr, m, SDATA (string));
6843 if (cursor_in_echo_area == 0)
6844 fprintf (stderr, "\n");
6845 fflush (stderr);
6846 }
6847 }
6848 else if (INTERACTIVE)
6849 {
6850 /* The frame whose minibuffer we're going to display the message on.
6851 It may be larger than the selected frame, so we need
6852 to use its buffer, not the selected frame's buffer. */
6853 Lisp_Object mini_window;
6854 struct frame *f, *sf = SELECTED_FRAME ();
6855
6856 /* Get the frame containing the minibuffer
6857 that the selected frame is using. */
6858 mini_window = FRAME_MINIBUF_WINDOW (sf);
6859 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
6860
6861 /* A null message buffer means that the frame hasn't really been
6862 initialized yet. Error messages get reported properly by
6863 cmd_error, so this must be just an informative message; toss it. */
6864 if (FRAME_MESSAGE_BUF (f))
6865 {
6866 Lisp_Object args[2], message;
6867 struct gcpro gcpro1, gcpro2;
6868
6869 args[0] = build_string (m);
6870 args[1] = message = string;
6871 GCPRO2 (args[0], message);
6872 gcpro1.nvars = 2;
6873
6874 message = Fformat (2, args);
6875
6876 if (log)
6877 message3 (message, SBYTES (message), STRING_MULTIBYTE (message));
6878 else
6879 message3_nolog (message, SBYTES (message), STRING_MULTIBYTE (message));
6880
6881 UNGCPRO;
6882
6883 /* Print should start at the beginning of the message
6884 buffer next time. */
6885 message_buf_print = 0;
6886 }
6887 }
6888 }
6889
6890
6891 /* Dump an informative message to the minibuf. If M is 0, clear out
6892 any existing message, and let the mini-buffer text show through. */
6893
6894 /* VARARGS 1 */
6895 void
6896 message (m, a1, a2, a3)
6897 char *m;
6898 EMACS_INT a1, a2, a3;
6899 {
6900 if (noninteractive)
6901 {
6902 if (m)
6903 {
6904 if (noninteractive_need_newline)
6905 putc ('\n', stderr);
6906 noninteractive_need_newline = 0;
6907 fprintf (stderr, m, a1, a2, a3);
6908 if (cursor_in_echo_area == 0)
6909 fprintf (stderr, "\n");
6910 fflush (stderr);
6911 }
6912 }
6913 else if (INTERACTIVE)
6914 {
6915 /* The frame whose mini-buffer we're going to display the message
6916 on. It may be larger than the selected frame, so we need to
6917 use its buffer, not the selected frame's buffer. */
6918 Lisp_Object mini_window;
6919 struct frame *f, *sf = SELECTED_FRAME ();
6920
6921 /* Get the frame containing the mini-buffer
6922 that the selected frame is using. */
6923 mini_window = FRAME_MINIBUF_WINDOW (sf);
6924 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
6925
6926 /* A null message buffer means that the frame hasn't really been
6927 initialized yet. Error messages get reported properly by
6928 cmd_error, so this must be just an informative message; toss
6929 it. */
6930 if (FRAME_MESSAGE_BUF (f))
6931 {
6932 if (m)
6933 {
6934 int len;
6935 #ifdef NO_ARG_ARRAY
6936 char *a[3];
6937 a[0] = (char *) a1;
6938 a[1] = (char *) a2;
6939 a[2] = (char *) a3;
6940
6941 len = doprnt (FRAME_MESSAGE_BUF (f),
6942 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
6943 #else
6944 len = doprnt (FRAME_MESSAGE_BUF (f),
6945 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3,
6946 (char **) &a1);
6947 #endif /* NO_ARG_ARRAY */
6948
6949 message2 (FRAME_MESSAGE_BUF (f), len, 0);
6950 }
6951 else
6952 message1 (0);
6953
6954 /* Print should start at the beginning of the message
6955 buffer next time. */
6956 message_buf_print = 0;
6957 }
6958 }
6959 }
6960
6961
6962 /* The non-logging version of message. */
6963
6964 void
6965 message_nolog (m, a1, a2, a3)
6966 char *m;
6967 EMACS_INT a1, a2, a3;
6968 {
6969 Lisp_Object old_log_max;
6970 old_log_max = Vmessage_log_max;
6971 Vmessage_log_max = Qnil;
6972 message (m, a1, a2, a3);
6973 Vmessage_log_max = old_log_max;
6974 }
6975
6976
6977 /* Display the current message in the current mini-buffer. This is
6978 only called from error handlers in process.c, and is not time
6979 critical. */
6980
6981 void
6982 update_echo_area ()
6983 {
6984 if (!NILP (echo_area_buffer[0]))
6985 {
6986 Lisp_Object string;
6987 string = Fcurrent_message ();
6988 message3 (string, SBYTES (string),
6989 !NILP (current_buffer->enable_multibyte_characters));
6990 }
6991 }
6992
6993
6994 /* Make sure echo area buffers in `echo_buffers' are live.
6995 If they aren't, make new ones. */
6996
6997 static void
6998 ensure_echo_area_buffers ()
6999 {
7000 int i;
7001
7002 for (i = 0; i < 2; ++i)
7003 if (!BUFFERP (echo_buffer[i])
7004 || NILP (XBUFFER (echo_buffer[i])->name))
7005 {
7006 char name[30];
7007 Lisp_Object old_buffer;
7008 int j;
7009
7010 old_buffer = echo_buffer[i];
7011 sprintf (name, " *Echo Area %d*", i);
7012 echo_buffer[i] = Fget_buffer_create (build_string (name));
7013 XBUFFER (echo_buffer[i])->truncate_lines = Qnil;
7014
7015 for (j = 0; j < 2; ++j)
7016 if (EQ (old_buffer, echo_area_buffer[j]))
7017 echo_area_buffer[j] = echo_buffer[i];
7018 }
7019 }
7020
7021
7022 /* Call FN with args A1..A4 with either the current or last displayed
7023 echo_area_buffer as current buffer.
7024
7025 WHICH zero means use the current message buffer
7026 echo_area_buffer[0]. If that is nil, choose a suitable buffer
7027 from echo_buffer[] and clear it.
7028
7029 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
7030 suitable buffer from echo_buffer[] and clear it.
7031
7032 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
7033 that the current message becomes the last displayed one, make
7034 choose a suitable buffer for echo_area_buffer[0], and clear it.
7035
7036 Value is what FN returns. */
7037
7038 static int
7039 with_echo_area_buffer (w, which, fn, a1, a2, a3, a4)
7040 struct window *w;
7041 int which;
7042 int (*fn) P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
7043 EMACS_INT a1;
7044 Lisp_Object a2;
7045 EMACS_INT a3, a4;
7046 {
7047 Lisp_Object buffer;
7048 int this_one, the_other, clear_buffer_p, rc;
7049 int count = SPECPDL_INDEX ();
7050
7051 /* If buffers aren't live, make new ones. */
7052 ensure_echo_area_buffers ();
7053
7054 clear_buffer_p = 0;
7055
7056 if (which == 0)
7057 this_one = 0, the_other = 1;
7058 else if (which > 0)
7059 this_one = 1, the_other = 0;
7060 else
7061 {
7062 this_one = 0, the_other = 1;
7063 clear_buffer_p = 1;
7064
7065 /* We need a fresh one in case the current echo buffer equals
7066 the one containing the last displayed echo area message. */
7067 if (!NILP (echo_area_buffer[this_one])
7068 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
7069 echo_area_buffer[this_one] = Qnil;
7070 }
7071
7072 /* Choose a suitable buffer from echo_buffer[] is we don't
7073 have one. */
7074 if (NILP (echo_area_buffer[this_one]))
7075 {
7076 echo_area_buffer[this_one]
7077 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
7078 ? echo_buffer[the_other]
7079 : echo_buffer[this_one]);
7080 clear_buffer_p = 1;
7081 }
7082
7083 buffer = echo_area_buffer[this_one];
7084
7085 /* Don't get confused by reusing the buffer used for echoing
7086 for a different purpose. */
7087 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
7088 cancel_echoing ();
7089
7090 record_unwind_protect (unwind_with_echo_area_buffer,
7091 with_echo_area_buffer_unwind_data (w));
7092
7093 /* Make the echo area buffer current. Note that for display
7094 purposes, it is not necessary that the displayed window's buffer
7095 == current_buffer, except for text property lookup. So, let's
7096 only set that buffer temporarily here without doing a full
7097 Fset_window_buffer. We must also change w->pointm, though,
7098 because otherwise an assertions in unshow_buffer fails, and Emacs
7099 aborts. */
7100 set_buffer_internal_1 (XBUFFER (buffer));
7101 if (w)
7102 {
7103 w->buffer = buffer;
7104 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
7105 }
7106
7107 current_buffer->undo_list = Qt;
7108 current_buffer->read_only = Qnil;
7109 specbind (Qinhibit_read_only, Qt);
7110 specbind (Qinhibit_modification_hooks, Qt);
7111
7112 if (clear_buffer_p && Z > BEG)
7113 del_range (BEG, Z);
7114
7115 xassert (BEGV >= BEG);
7116 xassert (ZV <= Z && ZV >= BEGV);
7117
7118 rc = fn (a1, a2, a3, a4);
7119
7120 xassert (BEGV >= BEG);
7121 xassert (ZV <= Z && ZV >= BEGV);
7122
7123 unbind_to (count, Qnil);
7124 return rc;
7125 }
7126
7127
7128 /* Save state that should be preserved around the call to the function
7129 FN called in with_echo_area_buffer. */
7130
7131 static Lisp_Object
7132 with_echo_area_buffer_unwind_data (w)
7133 struct window *w;
7134 {
7135 int i = 0;
7136 Lisp_Object vector;
7137
7138 /* Reduce consing by keeping one vector in
7139 Vwith_echo_area_save_vector. */
7140 vector = Vwith_echo_area_save_vector;
7141 Vwith_echo_area_save_vector = Qnil;
7142
7143 if (NILP (vector))
7144 vector = Fmake_vector (make_number (7), Qnil);
7145
7146 XSETBUFFER (AREF (vector, i), current_buffer); ++i;
7147 AREF (vector, i) = Vdeactivate_mark, ++i;
7148 AREF (vector, i) = make_number (windows_or_buffers_changed), ++i;
7149
7150 if (w)
7151 {
7152 XSETWINDOW (AREF (vector, i), w); ++i;
7153 AREF (vector, i) = w->buffer; ++i;
7154 AREF (vector, i) = make_number (XMARKER (w->pointm)->charpos); ++i;
7155 AREF (vector, i) = make_number (XMARKER (w->pointm)->bytepos); ++i;
7156 }
7157 else
7158 {
7159 int end = i + 4;
7160 for (; i < end; ++i)
7161 AREF (vector, i) = Qnil;
7162 }
7163
7164 xassert (i == ASIZE (vector));
7165 return vector;
7166 }
7167
7168
7169 /* Restore global state from VECTOR which was created by
7170 with_echo_area_buffer_unwind_data. */
7171
7172 static Lisp_Object
7173 unwind_with_echo_area_buffer (vector)
7174 Lisp_Object vector;
7175 {
7176 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
7177 Vdeactivate_mark = AREF (vector, 1);
7178 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
7179
7180 if (WINDOWP (AREF (vector, 3)))
7181 {
7182 struct window *w;
7183 Lisp_Object buffer, charpos, bytepos;
7184
7185 w = XWINDOW (AREF (vector, 3));
7186 buffer = AREF (vector, 4);
7187 charpos = AREF (vector, 5);
7188 bytepos = AREF (vector, 6);
7189
7190 w->buffer = buffer;
7191 set_marker_both (w->pointm, buffer,
7192 XFASTINT (charpos), XFASTINT (bytepos));
7193 }
7194
7195 Vwith_echo_area_save_vector = vector;
7196 return Qnil;
7197 }
7198
7199
7200 /* Set up the echo area for use by print functions. MULTIBYTE_P
7201 non-zero means we will print multibyte. */
7202
7203 void
7204 setup_echo_area_for_printing (multibyte_p)
7205 int multibyte_p;
7206 {
7207 /* If we can't find an echo area any more, exit. */
7208 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
7209 Fkill_emacs (Qnil);
7210
7211 ensure_echo_area_buffers ();
7212
7213 if (!message_buf_print)
7214 {
7215 /* A message has been output since the last time we printed.
7216 Choose a fresh echo area buffer. */
7217 if (EQ (echo_area_buffer[1], echo_buffer[0]))
7218 echo_area_buffer[0] = echo_buffer[1];
7219 else
7220 echo_area_buffer[0] = echo_buffer[0];
7221
7222 /* Switch to that buffer and clear it. */
7223 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
7224 current_buffer->truncate_lines = Qnil;
7225
7226 if (Z > BEG)
7227 {
7228 int count = SPECPDL_INDEX ();
7229 specbind (Qinhibit_read_only, Qt);
7230 /* Note that undo recording is always disabled. */
7231 del_range (BEG, Z);
7232 unbind_to (count, Qnil);
7233 }
7234 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
7235
7236 /* Set up the buffer for the multibyteness we need. */
7237 if (multibyte_p
7238 != !NILP (current_buffer->enable_multibyte_characters))
7239 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
7240
7241 /* Raise the frame containing the echo area. */
7242 if (minibuffer_auto_raise)
7243 {
7244 struct frame *sf = SELECTED_FRAME ();
7245 Lisp_Object mini_window;
7246 mini_window = FRAME_MINIBUF_WINDOW (sf);
7247 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
7248 }
7249
7250 message_log_maybe_newline ();
7251 message_buf_print = 1;
7252 }
7253 else
7254 {
7255 if (NILP (echo_area_buffer[0]))
7256 {
7257 if (EQ (echo_area_buffer[1], echo_buffer[0]))
7258 echo_area_buffer[0] = echo_buffer[1];
7259 else
7260 echo_area_buffer[0] = echo_buffer[0];
7261 }
7262
7263 if (current_buffer != XBUFFER (echo_area_buffer[0]))
7264 {
7265 /* Someone switched buffers between print requests. */
7266 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
7267 current_buffer->truncate_lines = Qnil;
7268 }
7269 }
7270 }
7271
7272
7273 /* Display an echo area message in window W. Value is non-zero if W's
7274 height is changed. If display_last_displayed_message_p is
7275 non-zero, display the message that was last displayed, otherwise
7276 display the current message. */
7277
7278 static int
7279 display_echo_area (w)
7280 struct window *w;
7281 {
7282 int i, no_message_p, window_height_changed_p, count;
7283
7284 /* Temporarily disable garbage collections while displaying the echo
7285 area. This is done because a GC can print a message itself.
7286 That message would modify the echo area buffer's contents while a
7287 redisplay of the buffer is going on, and seriously confuse
7288 redisplay. */
7289 count = inhibit_garbage_collection ();
7290
7291 /* If there is no message, we must call display_echo_area_1
7292 nevertheless because it resizes the window. But we will have to
7293 reset the echo_area_buffer in question to nil at the end because
7294 with_echo_area_buffer will sets it to an empty buffer. */
7295 i = display_last_displayed_message_p ? 1 : 0;
7296 no_message_p = NILP (echo_area_buffer[i]);
7297
7298 window_height_changed_p
7299 = with_echo_area_buffer (w, display_last_displayed_message_p,
7300 display_echo_area_1,
7301 (EMACS_INT) w, Qnil, 0, 0);
7302
7303 if (no_message_p)
7304 echo_area_buffer[i] = Qnil;
7305
7306 unbind_to (count, Qnil);
7307 return window_height_changed_p;
7308 }
7309
7310
7311 /* Helper for display_echo_area. Display the current buffer which
7312 contains the current echo area message in window W, a mini-window,
7313 a pointer to which is passed in A1. A2..A4 are currently not used.
7314 Change the height of W so that all of the message is displayed.
7315 Value is non-zero if height of W was changed. */
7316
7317 static int
7318 display_echo_area_1 (a1, a2, a3, a4)
7319 EMACS_INT a1;
7320 Lisp_Object a2;
7321 EMACS_INT a3, a4;
7322 {
7323 struct window *w = (struct window *) a1;
7324 Lisp_Object window;
7325 struct text_pos start;
7326 int window_height_changed_p = 0;
7327
7328 /* Do this before displaying, so that we have a large enough glyph
7329 matrix for the display. */
7330 window_height_changed_p = resize_mini_window (w, 0);
7331
7332 /* Display. */
7333 clear_glyph_matrix (w->desired_matrix);
7334 XSETWINDOW (window, w);
7335 SET_TEXT_POS (start, BEG, BEG_BYTE);
7336 try_window (window, start);
7337
7338 return window_height_changed_p;
7339 }
7340
7341
7342 /* Resize the echo area window to exactly the size needed for the
7343 currently displayed message, if there is one. If a mini-buffer
7344 is active, don't shrink it. */
7345
7346 void
7347 resize_echo_area_exactly ()
7348 {
7349 if (BUFFERP (echo_area_buffer[0])
7350 && WINDOWP (echo_area_window))
7351 {
7352 struct window *w = XWINDOW (echo_area_window);
7353 int resized_p;
7354 Lisp_Object resize_exactly;
7355
7356 if (minibuf_level == 0)
7357 resize_exactly = Qt;
7358 else
7359 resize_exactly = Qnil;
7360
7361 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
7362 (EMACS_INT) w, resize_exactly, 0, 0);
7363 if (resized_p)
7364 {
7365 ++windows_or_buffers_changed;
7366 ++update_mode_lines;
7367 redisplay_internal (0);
7368 }
7369 }
7370 }
7371
7372
7373 /* Callback function for with_echo_area_buffer, when used from
7374 resize_echo_area_exactly. A1 contains a pointer to the window to
7375 resize, EXACTLY non-nil means resize the mini-window exactly to the
7376 size of the text displayed. A3 and A4 are not used. Value is what
7377 resize_mini_window returns. */
7378
7379 static int
7380 resize_mini_window_1 (a1, exactly, a3, a4)
7381 EMACS_INT a1;
7382 Lisp_Object exactly;
7383 EMACS_INT a3, a4;
7384 {
7385 return resize_mini_window ((struct window *) a1, !NILP (exactly));
7386 }
7387
7388
7389 /* Resize mini-window W to fit the size of its contents. EXACT:P
7390 means size the window exactly to the size needed. Otherwise, it's
7391 only enlarged until W's buffer is empty. Value is non-zero if
7392 the window height has been changed. */
7393
7394 int
7395 resize_mini_window (w, exact_p)
7396 struct window *w;
7397 int exact_p;
7398 {
7399 struct frame *f = XFRAME (w->frame);
7400 int window_height_changed_p = 0;
7401
7402 xassert (MINI_WINDOW_P (w));
7403
7404 /* Don't resize windows while redisplaying a window; it would
7405 confuse redisplay functions when the size of the window they are
7406 displaying changes from under them. Such a resizing can happen,
7407 for instance, when which-func prints a long message while
7408 we are running fontification-functions. We're running these
7409 functions with safe_call which binds inhibit-redisplay to t. */
7410 if (!NILP (Vinhibit_redisplay))
7411 return 0;
7412
7413 /* Nil means don't try to resize. */
7414 if (NILP (Vresize_mini_windows)
7415 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
7416 return 0;
7417
7418 if (!FRAME_MINIBUF_ONLY_P (f))
7419 {
7420 struct it it;
7421 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
7422 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
7423 int height, max_height;
7424 int unit = FRAME_LINE_HEIGHT (f);
7425 struct text_pos start;
7426 struct buffer *old_current_buffer = NULL;
7427
7428 if (current_buffer != XBUFFER (w->buffer))
7429 {
7430 old_current_buffer = current_buffer;
7431 set_buffer_internal (XBUFFER (w->buffer));
7432 }
7433
7434 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
7435
7436 /* Compute the max. number of lines specified by the user. */
7437 if (FLOATP (Vmax_mini_window_height))
7438 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
7439 else if (INTEGERP (Vmax_mini_window_height))
7440 max_height = XINT (Vmax_mini_window_height);
7441 else
7442 max_height = total_height / 4;
7443
7444 /* Correct that max. height if it's bogus. */
7445 max_height = max (1, max_height);
7446 max_height = min (total_height, max_height);
7447
7448 /* Find out the height of the text in the window. */
7449 if (it.truncate_lines_p)
7450 height = 1;
7451 else
7452 {
7453 last_height = 0;
7454 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
7455 if (it.max_ascent == 0 && it.max_descent == 0)
7456 height = it.current_y + last_height;
7457 else
7458 height = it.current_y + it.max_ascent + it.max_descent;
7459 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
7460 height = (height + unit - 1) / unit;
7461 }
7462
7463 /* Compute a suitable window start. */
7464 if (height > max_height)
7465 {
7466 height = max_height;
7467 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
7468 move_it_vertically_backward (&it, (height - 1) * unit);
7469 start = it.current.pos;
7470 }
7471 else
7472 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
7473 SET_MARKER_FROM_TEXT_POS (w->start, start);
7474
7475 if (EQ (Vresize_mini_windows, Qgrow_only))
7476 {
7477 /* Let it grow only, until we display an empty message, in which
7478 case the window shrinks again. */
7479 if (height > WINDOW_TOTAL_LINES (w))
7480 {
7481 int old_height = WINDOW_TOTAL_LINES (w);
7482 freeze_window_starts (f, 1);
7483 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
7484 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
7485 }
7486 else if (height < WINDOW_TOTAL_LINES (w)
7487 && (exact_p || BEGV == ZV))
7488 {
7489 int old_height = WINDOW_TOTAL_LINES (w);
7490 freeze_window_starts (f, 0);
7491 shrink_mini_window (w);
7492 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
7493 }
7494 }
7495 else
7496 {
7497 /* Always resize to exact size needed. */
7498 if (height > WINDOW_TOTAL_LINES (w))
7499 {
7500 int old_height = WINDOW_TOTAL_LINES (w);
7501 freeze_window_starts (f, 1);
7502 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
7503 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
7504 }
7505 else if (height < WINDOW_TOTAL_LINES (w))
7506 {
7507 int old_height = WINDOW_TOTAL_LINES (w);
7508 freeze_window_starts (f, 0);
7509 shrink_mini_window (w);
7510
7511 if (height)
7512 {
7513 freeze_window_starts (f, 1);
7514 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
7515 }
7516
7517 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
7518 }
7519 }
7520
7521 if (old_current_buffer)
7522 set_buffer_internal (old_current_buffer);
7523 }
7524
7525 return window_height_changed_p;
7526 }
7527
7528
7529 /* Value is the current message, a string, or nil if there is no
7530 current message. */
7531
7532 Lisp_Object
7533 current_message ()
7534 {
7535 Lisp_Object msg;
7536
7537 if (NILP (echo_area_buffer[0]))
7538 msg = Qnil;
7539 else
7540 {
7541 with_echo_area_buffer (0, 0, current_message_1,
7542 (EMACS_INT) &msg, Qnil, 0, 0);
7543 if (NILP (msg))
7544 echo_area_buffer[0] = Qnil;
7545 }
7546
7547 return msg;
7548 }
7549
7550
7551 static int
7552 current_message_1 (a1, a2, a3, a4)
7553 EMACS_INT a1;
7554 Lisp_Object a2;
7555 EMACS_INT a3, a4;
7556 {
7557 Lisp_Object *msg = (Lisp_Object *) a1;
7558
7559 if (Z > BEG)
7560 *msg = make_buffer_string (BEG, Z, 1);
7561 else
7562 *msg = Qnil;
7563 return 0;
7564 }
7565
7566
7567 /* Push the current message on Vmessage_stack for later restauration
7568 by restore_message. Value is non-zero if the current message isn't
7569 empty. This is a relatively infrequent operation, so it's not
7570 worth optimizing. */
7571
7572 int
7573 push_message ()
7574 {
7575 Lisp_Object msg;
7576 msg = current_message ();
7577 Vmessage_stack = Fcons (msg, Vmessage_stack);
7578 return STRINGP (msg);
7579 }
7580
7581
7582 /* Restore message display from the top of Vmessage_stack. */
7583
7584 void
7585 restore_message ()
7586 {
7587 Lisp_Object msg;
7588
7589 xassert (CONSP (Vmessage_stack));
7590 msg = XCAR (Vmessage_stack);
7591 if (STRINGP (msg))
7592 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
7593 else
7594 message3_nolog (msg, 0, 0);
7595 }
7596
7597
7598 /* Handler for record_unwind_protect calling pop_message. */
7599
7600 Lisp_Object
7601 pop_message_unwind (dummy)
7602 Lisp_Object dummy;
7603 {
7604 pop_message ();
7605 return Qnil;
7606 }
7607
7608 /* Pop the top-most entry off Vmessage_stack. */
7609
7610 void
7611 pop_message ()
7612 {
7613 xassert (CONSP (Vmessage_stack));
7614 Vmessage_stack = XCDR (Vmessage_stack);
7615 }
7616
7617
7618 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
7619 exits. If the stack is not empty, we have a missing pop_message
7620 somewhere. */
7621
7622 void
7623 check_message_stack ()
7624 {
7625 if (!NILP (Vmessage_stack))
7626 abort ();
7627 }
7628
7629
7630 /* Truncate to NCHARS what will be displayed in the echo area the next
7631 time we display it---but don't redisplay it now. */
7632
7633 void
7634 truncate_echo_area (nchars)
7635 int nchars;
7636 {
7637 if (nchars == 0)
7638 echo_area_buffer[0] = Qnil;
7639 /* A null message buffer means that the frame hasn't really been
7640 initialized yet. Error messages get reported properly by
7641 cmd_error, so this must be just an informative message; toss it. */
7642 else if (!noninteractive
7643 && INTERACTIVE
7644 && !NILP (echo_area_buffer[0]))
7645 {
7646 struct frame *sf = SELECTED_FRAME ();
7647 if (FRAME_MESSAGE_BUF (sf))
7648 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
7649 }
7650 }
7651
7652
7653 /* Helper function for truncate_echo_area. Truncate the current
7654 message to at most NCHARS characters. */
7655
7656 static int
7657 truncate_message_1 (nchars, a2, a3, a4)
7658 EMACS_INT nchars;
7659 Lisp_Object a2;
7660 EMACS_INT a3, a4;
7661 {
7662 if (BEG + nchars < Z)
7663 del_range (BEG + nchars, Z);
7664 if (Z == BEG)
7665 echo_area_buffer[0] = Qnil;
7666 return 0;
7667 }
7668
7669
7670 /* Set the current message to a substring of S or STRING.
7671
7672 If STRING is a Lisp string, set the message to the first NBYTES
7673 bytes from STRING. NBYTES zero means use the whole string. If
7674 STRING is multibyte, the message will be displayed multibyte.
7675
7676 If S is not null, set the message to the first LEN bytes of S. LEN
7677 zero means use the whole string. MULTIBYTE_P non-zero means S is
7678 multibyte. Display the message multibyte in that case. */
7679
7680 void
7681 set_message (s, string, nbytes, multibyte_p)
7682 const char *s;
7683 Lisp_Object string;
7684 int nbytes, multibyte_p;
7685 {
7686 message_enable_multibyte
7687 = ((s && multibyte_p)
7688 || (STRINGP (string) && STRING_MULTIBYTE (string)));
7689
7690 with_echo_area_buffer (0, -1, set_message_1,
7691 (EMACS_INT) s, string, nbytes, multibyte_p);
7692 message_buf_print = 0;
7693 help_echo_showing_p = 0;
7694 }
7695
7696
7697 /* Helper function for set_message. Arguments have the same meaning
7698 as there, with A1 corresponding to S and A2 corresponding to STRING
7699 This function is called with the echo area buffer being
7700 current. */
7701
7702 static int
7703 set_message_1 (a1, a2, nbytes, multibyte_p)
7704 EMACS_INT a1;
7705 Lisp_Object a2;
7706 EMACS_INT nbytes, multibyte_p;
7707 {
7708 const char *s = (const char *) a1;
7709 Lisp_Object string = a2;
7710
7711 xassert (BEG == Z);
7712
7713 /* Change multibyteness of the echo buffer appropriately. */
7714 if (message_enable_multibyte
7715 != !NILP (current_buffer->enable_multibyte_characters))
7716 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
7717
7718 current_buffer->truncate_lines = message_truncate_lines ? Qt : Qnil;
7719
7720 /* Insert new message at BEG. */
7721 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
7722
7723 if (STRINGP (string))
7724 {
7725 int nchars;
7726
7727 if (nbytes == 0)
7728 nbytes = SBYTES (string);
7729 nchars = string_byte_to_char (string, nbytes);
7730
7731 /* This function takes care of single/multibyte conversion. We
7732 just have to ensure that the echo area buffer has the right
7733 setting of enable_multibyte_characters. */
7734 insert_from_string (string, 0, 0, nchars, nbytes, 1);
7735 }
7736 else if (s)
7737 {
7738 if (nbytes == 0)
7739 nbytes = strlen (s);
7740
7741 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
7742 {
7743 /* Convert from multi-byte to single-byte. */
7744 int i, c, n;
7745 unsigned char work[1];
7746
7747 /* Convert a multibyte string to single-byte. */
7748 for (i = 0; i < nbytes; i += n)
7749 {
7750 c = string_char_and_length (s + i, nbytes - i, &n);
7751 work[0] = (SINGLE_BYTE_CHAR_P (c)
7752 ? c
7753 : multibyte_char_to_unibyte (c, Qnil));
7754 insert_1_both (work, 1, 1, 1, 0, 0);
7755 }
7756 }
7757 else if (!multibyte_p
7758 && !NILP (current_buffer->enable_multibyte_characters))
7759 {
7760 /* Convert from single-byte to multi-byte. */
7761 int i, c, n;
7762 const unsigned char *msg = (const unsigned char *) s;
7763 unsigned char str[MAX_MULTIBYTE_LENGTH];
7764
7765 /* Convert a single-byte string to multibyte. */
7766 for (i = 0; i < nbytes; i++)
7767 {
7768 c = unibyte_char_to_multibyte (msg[i]);
7769 n = CHAR_STRING (c, str);
7770 insert_1_both (str, 1, n, 1, 0, 0);
7771 }
7772 }
7773 else
7774 insert_1 (s, nbytes, 1, 0, 0);
7775 }
7776
7777 return 0;
7778 }
7779
7780
7781 /* Clear messages. CURRENT_P non-zero means clear the current
7782 message. LAST_DISPLAYED_P non-zero means clear the message
7783 last displayed. */
7784
7785 void
7786 clear_message (current_p, last_displayed_p)
7787 int current_p, last_displayed_p;
7788 {
7789 if (current_p)
7790 {
7791 echo_area_buffer[0] = Qnil;
7792 message_cleared_p = 1;
7793 }
7794
7795 if (last_displayed_p)
7796 echo_area_buffer[1] = Qnil;
7797
7798 message_buf_print = 0;
7799 }
7800
7801 /* Clear garbaged frames.
7802
7803 This function is used where the old redisplay called
7804 redraw_garbaged_frames which in turn called redraw_frame which in
7805 turn called clear_frame. The call to clear_frame was a source of
7806 flickering. I believe a clear_frame is not necessary. It should
7807 suffice in the new redisplay to invalidate all current matrices,
7808 and ensure a complete redisplay of all windows. */
7809
7810 static void
7811 clear_garbaged_frames ()
7812 {
7813 if (frame_garbaged)
7814 {
7815 Lisp_Object tail, frame;
7816 int changed_count = 0;
7817
7818 FOR_EACH_FRAME (tail, frame)
7819 {
7820 struct frame *f = XFRAME (frame);
7821
7822 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
7823 {
7824 if (f->resized_p)
7825 {
7826 Fredraw_frame (frame);
7827 f->force_flush_display_p = 1;
7828 }
7829 clear_current_matrices (f);
7830 changed_count++;
7831 f->garbaged = 0;
7832 f->resized_p = 0;
7833 }
7834 }
7835
7836 frame_garbaged = 0;
7837 if (changed_count)
7838 ++windows_or_buffers_changed;
7839 }
7840 }
7841
7842
7843 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
7844 is non-zero update selected_frame. Value is non-zero if the
7845 mini-windows height has been changed. */
7846
7847 static int
7848 echo_area_display (update_frame_p)
7849 int update_frame_p;
7850 {
7851 Lisp_Object mini_window;
7852 struct window *w;
7853 struct frame *f;
7854 int window_height_changed_p = 0;
7855 struct frame *sf = SELECTED_FRAME ();
7856
7857 mini_window = FRAME_MINIBUF_WINDOW (sf);
7858 w = XWINDOW (mini_window);
7859 f = XFRAME (WINDOW_FRAME (w));
7860
7861 /* Don't display if frame is invisible or not yet initialized. */
7862 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
7863 return 0;
7864
7865 /* The terminal frame is used as the first Emacs frame on the Mac OS. */
7866 #ifndef MAC_OS8
7867 #ifdef HAVE_WINDOW_SYSTEM
7868 /* When Emacs starts, selected_frame may be the initial terminal
7869 frame. If we let this through, a message would be displayed on
7870 the terminal. */
7871 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
7872 return 0;
7873 #endif /* HAVE_WINDOW_SYSTEM */
7874 #endif
7875
7876 /* Redraw garbaged frames. */
7877 if (frame_garbaged)
7878 clear_garbaged_frames ();
7879
7880 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
7881 {
7882 echo_area_window = mini_window;
7883 window_height_changed_p = display_echo_area (w);
7884 w->must_be_updated_p = 1;
7885
7886 /* Update the display, unless called from redisplay_internal.
7887 Also don't update the screen during redisplay itself. The
7888 update will happen at the end of redisplay, and an update
7889 here could cause confusion. */
7890 if (update_frame_p && !redisplaying_p)
7891 {
7892 int n = 0;
7893
7894 /* If the display update has been interrupted by pending
7895 input, update mode lines in the frame. Due to the
7896 pending input, it might have been that redisplay hasn't
7897 been called, so that mode lines above the echo area are
7898 garbaged. This looks odd, so we prevent it here. */
7899 if (!display_completed)
7900 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
7901
7902 if (window_height_changed_p
7903 /* Don't do this if Emacs is shutting down. Redisplay
7904 needs to run hooks. */
7905 && !NILP (Vrun_hooks))
7906 {
7907 /* Must update other windows. Likewise as in other
7908 cases, don't let this update be interrupted by
7909 pending input. */
7910 int count = SPECPDL_INDEX ();
7911 specbind (Qredisplay_dont_pause, Qt);
7912 windows_or_buffers_changed = 1;
7913 redisplay_internal (0);
7914 unbind_to (count, Qnil);
7915 }
7916 else if (FRAME_WINDOW_P (f) && n == 0)
7917 {
7918 /* Window configuration is the same as before.
7919 Can do with a display update of the echo area,
7920 unless we displayed some mode lines. */
7921 update_single_window (w, 1);
7922 FRAME_RIF (f)->flush_display (f);
7923 }
7924 else
7925 update_frame (f, 1, 1);
7926
7927 /* If cursor is in the echo area, make sure that the next
7928 redisplay displays the minibuffer, so that the cursor will
7929 be replaced with what the minibuffer wants. */
7930 if (cursor_in_echo_area)
7931 ++windows_or_buffers_changed;
7932 }
7933 }
7934 else if (!EQ (mini_window, selected_window))
7935 windows_or_buffers_changed++;
7936
7937 /* Last displayed message is now the current message. */
7938 echo_area_buffer[1] = echo_area_buffer[0];
7939
7940 /* Prevent redisplay optimization in redisplay_internal by resetting
7941 this_line_start_pos. This is done because the mini-buffer now
7942 displays the message instead of its buffer text. */
7943 if (EQ (mini_window, selected_window))
7944 CHARPOS (this_line_start_pos) = 0;
7945
7946 return window_height_changed_p;
7947 }
7948
7949
7950 \f
7951 /***********************************************************************
7952 Frame Titles
7953 ***********************************************************************/
7954
7955
7956 /* The frame title buffering code is also used by Fformat_mode_line.
7957 So it is not conditioned by HAVE_WINDOW_SYSTEM. */
7958
7959 /* A buffer for constructing frame titles in it; allocated from the
7960 heap in init_xdisp and resized as needed in store_frame_title_char. */
7961
7962 static char *frame_title_buf;
7963
7964 /* The buffer's end, and a current output position in it. */
7965
7966 static char *frame_title_buf_end;
7967 static char *frame_title_ptr;
7968
7969
7970 /* Store a single character C for the frame title in frame_title_buf.
7971 Re-allocate frame_title_buf if necessary. */
7972
7973 static void
7974 #ifdef PROTOTYPES
7975 store_frame_title_char (char c)
7976 #else
7977 store_frame_title_char (c)
7978 char c;
7979 #endif
7980 {
7981 /* If output position has reached the end of the allocated buffer,
7982 double the buffer's size. */
7983 if (frame_title_ptr == frame_title_buf_end)
7984 {
7985 int len = frame_title_ptr - frame_title_buf;
7986 int new_size = 2 * len * sizeof *frame_title_buf;
7987 frame_title_buf = (char *) xrealloc (frame_title_buf, new_size);
7988 frame_title_buf_end = frame_title_buf + new_size;
7989 frame_title_ptr = frame_title_buf + len;
7990 }
7991
7992 *frame_title_ptr++ = c;
7993 }
7994
7995
7996 /* Store part of a frame title in frame_title_buf, beginning at
7997 frame_title_ptr. STR is the string to store. Do not copy
7998 characters that yield more columns than PRECISION; PRECISION <= 0
7999 means copy the whole string. Pad with spaces until FIELD_WIDTH
8000 number of characters have been copied; FIELD_WIDTH <= 0 means don't
8001 pad. Called from display_mode_element when it is used to build a
8002 frame title. */
8003
8004 static int
8005 store_frame_title (str, field_width, precision)
8006 const unsigned char *str;
8007 int field_width, precision;
8008 {
8009 int n = 0;
8010 int dummy, nbytes;
8011
8012 /* Copy at most PRECISION chars from STR. */
8013 nbytes = strlen (str);
8014 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
8015 while (nbytes--)
8016 store_frame_title_char (*str++);
8017
8018 /* Fill up with spaces until FIELD_WIDTH reached. */
8019 while (field_width > 0
8020 && n < field_width)
8021 {
8022 store_frame_title_char (' ');
8023 ++n;
8024 }
8025
8026 return n;
8027 }
8028
8029 #ifdef HAVE_WINDOW_SYSTEM
8030
8031 /* Set the title of FRAME, if it has changed. The title format is
8032 Vicon_title_format if FRAME is iconified, otherwise it is
8033 frame_title_format. */
8034
8035 static void
8036 x_consider_frame_title (frame)
8037 Lisp_Object frame;
8038 {
8039 struct frame *f = XFRAME (frame);
8040
8041 if (FRAME_WINDOW_P (f)
8042 || FRAME_MINIBUF_ONLY_P (f)
8043 || f->explicit_name)
8044 {
8045 /* Do we have more than one visible frame on this X display? */
8046 Lisp_Object tail;
8047 Lisp_Object fmt;
8048 struct buffer *obuf;
8049 int len;
8050 struct it it;
8051
8052 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
8053 {
8054 Lisp_Object other_frame = XCAR (tail);
8055 struct frame *tf = XFRAME (other_frame);
8056
8057 if (tf != f
8058 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
8059 && !FRAME_MINIBUF_ONLY_P (tf)
8060 && !EQ (other_frame, tip_frame)
8061 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
8062 break;
8063 }
8064
8065 /* Set global variable indicating that multiple frames exist. */
8066 multiple_frames = CONSP (tail);
8067
8068 /* Switch to the buffer of selected window of the frame. Set up
8069 frame_title_ptr so that display_mode_element will output into it;
8070 then display the title. */
8071 obuf = current_buffer;
8072 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
8073 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
8074 frame_title_ptr = frame_title_buf;
8075 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
8076 NULL, DEFAULT_FACE_ID);
8077 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
8078 len = frame_title_ptr - frame_title_buf;
8079 frame_title_ptr = NULL;
8080 set_buffer_internal_1 (obuf);
8081
8082 /* Set the title only if it's changed. This avoids consing in
8083 the common case where it hasn't. (If it turns out that we've
8084 already wasted too much time by walking through the list with
8085 display_mode_element, then we might need to optimize at a
8086 higher level than this.) */
8087 if (! STRINGP (f->name)
8088 || SBYTES (f->name) != len
8089 || bcmp (frame_title_buf, SDATA (f->name), len) != 0)
8090 x_implicitly_set_name (f, make_string (frame_title_buf, len), Qnil);
8091 }
8092 }
8093
8094 #endif /* not HAVE_WINDOW_SYSTEM */
8095
8096
8097
8098 \f
8099 /***********************************************************************
8100 Menu Bars
8101 ***********************************************************************/
8102
8103
8104 /* Prepare for redisplay by updating menu-bar item lists when
8105 appropriate. This can call eval. */
8106
8107 void
8108 prepare_menu_bars ()
8109 {
8110 int all_windows;
8111 struct gcpro gcpro1, gcpro2;
8112 struct frame *f;
8113 Lisp_Object tooltip_frame;
8114
8115 #ifdef HAVE_WINDOW_SYSTEM
8116 tooltip_frame = tip_frame;
8117 #else
8118 tooltip_frame = Qnil;
8119 #endif
8120
8121 /* Update all frame titles based on their buffer names, etc. We do
8122 this before the menu bars so that the buffer-menu will show the
8123 up-to-date frame titles. */
8124 #ifdef HAVE_WINDOW_SYSTEM
8125 if (windows_or_buffers_changed || update_mode_lines)
8126 {
8127 Lisp_Object tail, frame;
8128
8129 FOR_EACH_FRAME (tail, frame)
8130 {
8131 f = XFRAME (frame);
8132 if (!EQ (frame, tooltip_frame)
8133 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
8134 x_consider_frame_title (frame);
8135 }
8136 }
8137 #endif /* HAVE_WINDOW_SYSTEM */
8138
8139 /* Update the menu bar item lists, if appropriate. This has to be
8140 done before any actual redisplay or generation of display lines. */
8141 all_windows = (update_mode_lines
8142 || buffer_shared > 1
8143 || windows_or_buffers_changed);
8144 if (all_windows)
8145 {
8146 Lisp_Object tail, frame;
8147 int count = SPECPDL_INDEX ();
8148
8149 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
8150
8151 FOR_EACH_FRAME (tail, frame)
8152 {
8153 f = XFRAME (frame);
8154
8155 /* Ignore tooltip frame. */
8156 if (EQ (frame, tooltip_frame))
8157 continue;
8158
8159 /* If a window on this frame changed size, report that to
8160 the user and clear the size-change flag. */
8161 if (FRAME_WINDOW_SIZES_CHANGED (f))
8162 {
8163 Lisp_Object functions;
8164
8165 /* Clear flag first in case we get an error below. */
8166 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
8167 functions = Vwindow_size_change_functions;
8168 GCPRO2 (tail, functions);
8169
8170 while (CONSP (functions))
8171 {
8172 call1 (XCAR (functions), frame);
8173 functions = XCDR (functions);
8174 }
8175 UNGCPRO;
8176 }
8177
8178 GCPRO1 (tail);
8179 update_menu_bar (f, 0);
8180 #ifdef HAVE_WINDOW_SYSTEM
8181 update_tool_bar (f, 0);
8182 #endif
8183 UNGCPRO;
8184 }
8185
8186 unbind_to (count, Qnil);
8187 }
8188 else
8189 {
8190 struct frame *sf = SELECTED_FRAME ();
8191 update_menu_bar (sf, 1);
8192 #ifdef HAVE_WINDOW_SYSTEM
8193 update_tool_bar (sf, 1);
8194 #endif
8195 }
8196
8197 /* Motif needs this. See comment in xmenu.c. Turn it off when
8198 pending_menu_activation is not defined. */
8199 #ifdef USE_X_TOOLKIT
8200 pending_menu_activation = 0;
8201 #endif
8202 }
8203
8204
8205 /* Update the menu bar item list for frame F. This has to be done
8206 before we start to fill in any display lines, because it can call
8207 eval.
8208
8209 If SAVE_MATCH_DATA is non-zero, we must save and restore it here. */
8210
8211 static void
8212 update_menu_bar (f, save_match_data)
8213 struct frame *f;
8214 int save_match_data;
8215 {
8216 Lisp_Object window;
8217 register struct window *w;
8218
8219 /* If called recursively during a menu update, do nothing. This can
8220 happen when, for instance, an activate-menubar-hook causes a
8221 redisplay. */
8222 if (inhibit_menubar_update)
8223 return;
8224
8225 window = FRAME_SELECTED_WINDOW (f);
8226 w = XWINDOW (window);
8227
8228 #if 0 /* The if statement below this if statement used to include the
8229 condition !NILP (w->update_mode_line), rather than using
8230 update_mode_lines directly, and this if statement may have
8231 been added to make that condition work. Now the if
8232 statement below matches its comment, this isn't needed. */
8233 if (update_mode_lines)
8234 w->update_mode_line = Qt;
8235 #endif
8236
8237 if (FRAME_WINDOW_P (f)
8238 ?
8239 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
8240 || defined (USE_GTK)
8241 FRAME_EXTERNAL_MENU_BAR (f)
8242 #else
8243 FRAME_MENU_BAR_LINES (f) > 0
8244 #endif
8245 : FRAME_MENU_BAR_LINES (f) > 0)
8246 {
8247 /* If the user has switched buffers or windows, we need to
8248 recompute to reflect the new bindings. But we'll
8249 recompute when update_mode_lines is set too; that means
8250 that people can use force-mode-line-update to request
8251 that the menu bar be recomputed. The adverse effect on
8252 the rest of the redisplay algorithm is about the same as
8253 windows_or_buffers_changed anyway. */
8254 if (windows_or_buffers_changed
8255 /* This used to test w->update_mode_line, but we believe
8256 there is no need to recompute the menu in that case. */
8257 || update_mode_lines
8258 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
8259 < BUF_MODIFF (XBUFFER (w->buffer)))
8260 != !NILP (w->last_had_star))
8261 || ((!NILP (Vtransient_mark_mode)
8262 && !NILP (XBUFFER (w->buffer)->mark_active))
8263 != !NILP (w->region_showing)))
8264 {
8265 struct buffer *prev = current_buffer;
8266 int count = SPECPDL_INDEX ();
8267
8268 specbind (Qinhibit_menubar_update, Qt);
8269
8270 set_buffer_internal_1 (XBUFFER (w->buffer));
8271 if (save_match_data)
8272 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
8273 if (NILP (Voverriding_local_map_menu_flag))
8274 {
8275 specbind (Qoverriding_terminal_local_map, Qnil);
8276 specbind (Qoverriding_local_map, Qnil);
8277 }
8278
8279 /* Run the Lucid hook. */
8280 safe_run_hooks (Qactivate_menubar_hook);
8281
8282 /* If it has changed current-menubar from previous value,
8283 really recompute the menu-bar from the value. */
8284 if (! NILP (Vlucid_menu_bar_dirty_flag))
8285 call0 (Qrecompute_lucid_menubar);
8286
8287 safe_run_hooks (Qmenu_bar_update_hook);
8288 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
8289
8290 /* Redisplay the menu bar in case we changed it. */
8291 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
8292 || defined (USE_GTK)
8293 if (FRAME_WINDOW_P (f)
8294 #if defined (MAC_OS)
8295 /* All frames on Mac OS share the same menubar. So only the
8296 selected frame should be allowed to set it. */
8297 && f == SELECTED_FRAME ()
8298 #endif
8299 )
8300 set_frame_menubar (f, 0, 0);
8301 else
8302 /* On a terminal screen, the menu bar is an ordinary screen
8303 line, and this makes it get updated. */
8304 w->update_mode_line = Qt;
8305 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
8306 /* In the non-toolkit version, the menu bar is an ordinary screen
8307 line, and this makes it get updated. */
8308 w->update_mode_line = Qt;
8309 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
8310
8311 unbind_to (count, Qnil);
8312 set_buffer_internal_1 (prev);
8313 }
8314 }
8315 }
8316
8317
8318 \f
8319 /***********************************************************************
8320 Output Cursor
8321 ***********************************************************************/
8322
8323 #ifdef HAVE_WINDOW_SYSTEM
8324
8325 /* EXPORT:
8326 Nominal cursor position -- where to draw output.
8327 HPOS and VPOS are window relative glyph matrix coordinates.
8328 X and Y are window relative pixel coordinates. */
8329
8330 struct cursor_pos output_cursor;
8331
8332
8333 /* EXPORT:
8334 Set the global variable output_cursor to CURSOR. All cursor
8335 positions are relative to updated_window. */
8336
8337 void
8338 set_output_cursor (cursor)
8339 struct cursor_pos *cursor;
8340 {
8341 output_cursor.hpos = cursor->hpos;
8342 output_cursor.vpos = cursor->vpos;
8343 output_cursor.x = cursor->x;
8344 output_cursor.y = cursor->y;
8345 }
8346
8347
8348 /* EXPORT for RIF:
8349 Set a nominal cursor position.
8350
8351 HPOS and VPOS are column/row positions in a window glyph matrix. X
8352 and Y are window text area relative pixel positions.
8353
8354 If this is done during an update, updated_window will contain the
8355 window that is being updated and the position is the future output
8356 cursor position for that window. If updated_window is null, use
8357 selected_window and display the cursor at the given position. */
8358
8359 void
8360 x_cursor_to (vpos, hpos, y, x)
8361 int vpos, hpos, y, x;
8362 {
8363 struct window *w;
8364
8365 /* If updated_window is not set, work on selected_window. */
8366 if (updated_window)
8367 w = updated_window;
8368 else
8369 w = XWINDOW (selected_window);
8370
8371 /* Set the output cursor. */
8372 output_cursor.hpos = hpos;
8373 output_cursor.vpos = vpos;
8374 output_cursor.x = x;
8375 output_cursor.y = y;
8376
8377 /* If not called as part of an update, really display the cursor.
8378 This will also set the cursor position of W. */
8379 if (updated_window == NULL)
8380 {
8381 BLOCK_INPUT;
8382 display_and_set_cursor (w, 1, hpos, vpos, x, y);
8383 if (FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
8384 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (SELECTED_FRAME ());
8385 UNBLOCK_INPUT;
8386 }
8387 }
8388
8389 #endif /* HAVE_WINDOW_SYSTEM */
8390
8391 \f
8392 /***********************************************************************
8393 Tool-bars
8394 ***********************************************************************/
8395
8396 #ifdef HAVE_WINDOW_SYSTEM
8397
8398 /* Where the mouse was last time we reported a mouse event. */
8399
8400 FRAME_PTR last_mouse_frame;
8401
8402 /* Tool-bar item index of the item on which a mouse button was pressed
8403 or -1. */
8404
8405 int last_tool_bar_item;
8406
8407
8408 /* Update the tool-bar item list for frame F. This has to be done
8409 before we start to fill in any display lines. Called from
8410 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
8411 and restore it here. */
8412
8413 static void
8414 update_tool_bar (f, save_match_data)
8415 struct frame *f;
8416 int save_match_data;
8417 {
8418 #ifdef USE_GTK
8419 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
8420 #else
8421 int do_update = WINDOWP (f->tool_bar_window)
8422 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
8423 #endif
8424
8425 if (do_update)
8426 {
8427 Lisp_Object window;
8428 struct window *w;
8429
8430 window = FRAME_SELECTED_WINDOW (f);
8431 w = XWINDOW (window);
8432
8433 /* If the user has switched buffers or windows, we need to
8434 recompute to reflect the new bindings. But we'll
8435 recompute when update_mode_lines is set too; that means
8436 that people can use force-mode-line-update to request
8437 that the menu bar be recomputed. The adverse effect on
8438 the rest of the redisplay algorithm is about the same as
8439 windows_or_buffers_changed anyway. */
8440 if (windows_or_buffers_changed
8441 || !NILP (w->update_mode_line)
8442 || update_mode_lines
8443 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
8444 < BUF_MODIFF (XBUFFER (w->buffer)))
8445 != !NILP (w->last_had_star))
8446 || ((!NILP (Vtransient_mark_mode)
8447 && !NILP (XBUFFER (w->buffer)->mark_active))
8448 != !NILP (w->region_showing)))
8449 {
8450 struct buffer *prev = current_buffer;
8451 int count = SPECPDL_INDEX ();
8452 Lisp_Object new_tool_bar;
8453 int new_n_tool_bar;
8454 struct gcpro gcpro1;
8455
8456 /* Set current_buffer to the buffer of the selected
8457 window of the frame, so that we get the right local
8458 keymaps. */
8459 set_buffer_internal_1 (XBUFFER (w->buffer));
8460
8461 /* Save match data, if we must. */
8462 if (save_match_data)
8463 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
8464
8465 /* Make sure that we don't accidentally use bogus keymaps. */
8466 if (NILP (Voverriding_local_map_menu_flag))
8467 {
8468 specbind (Qoverriding_terminal_local_map, Qnil);
8469 specbind (Qoverriding_local_map, Qnil);
8470 }
8471
8472 GCPRO1 (new_tool_bar);
8473
8474 /* Build desired tool-bar items from keymaps. */
8475 new_tool_bar = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
8476 &new_n_tool_bar);
8477
8478 /* Redisplay the tool-bar if we changed it. */
8479 if (NILP (Fequal (new_tool_bar, f->tool_bar_items)))
8480 {
8481 /* Redisplay that happens asynchronously due to an expose event
8482 may access f->tool_bar_items. Make sure we update both
8483 variables within BLOCK_INPUT so no such event interrupts. */
8484 BLOCK_INPUT;
8485 f->tool_bar_items = new_tool_bar;
8486 f->n_tool_bar_items = new_n_tool_bar;
8487 w->update_mode_line = Qt;
8488 UNBLOCK_INPUT;
8489 }
8490
8491 UNGCPRO;
8492
8493 unbind_to (count, Qnil);
8494 set_buffer_internal_1 (prev);
8495 }
8496 }
8497 }
8498
8499
8500 /* Set F->desired_tool_bar_string to a Lisp string representing frame
8501 F's desired tool-bar contents. F->tool_bar_items must have
8502 been set up previously by calling prepare_menu_bars. */
8503
8504 static void
8505 build_desired_tool_bar_string (f)
8506 struct frame *f;
8507 {
8508 int i, size, size_needed;
8509 struct gcpro gcpro1, gcpro2, gcpro3;
8510 Lisp_Object image, plist, props;
8511
8512 image = plist = props = Qnil;
8513 GCPRO3 (image, plist, props);
8514
8515 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
8516 Otherwise, make a new string. */
8517
8518 /* The size of the string we might be able to reuse. */
8519 size = (STRINGP (f->desired_tool_bar_string)
8520 ? SCHARS (f->desired_tool_bar_string)
8521 : 0);
8522
8523 /* We need one space in the string for each image. */
8524 size_needed = f->n_tool_bar_items;
8525
8526 /* Reuse f->desired_tool_bar_string, if possible. */
8527 if (size < size_needed || NILP (f->desired_tool_bar_string))
8528 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
8529 make_number (' '));
8530 else
8531 {
8532 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
8533 Fremove_text_properties (make_number (0), make_number (size),
8534 props, f->desired_tool_bar_string);
8535 }
8536
8537 /* Put a `display' property on the string for the images to display,
8538 put a `menu_item' property on tool-bar items with a value that
8539 is the index of the item in F's tool-bar item vector. */
8540 for (i = 0; i < f->n_tool_bar_items; ++i)
8541 {
8542 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
8543
8544 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
8545 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
8546 int hmargin, vmargin, relief, idx, end;
8547 extern Lisp_Object QCrelief, QCmargin, QCconversion;
8548
8549 /* If image is a vector, choose the image according to the
8550 button state. */
8551 image = PROP (TOOL_BAR_ITEM_IMAGES);
8552 if (VECTORP (image))
8553 {
8554 if (enabled_p)
8555 idx = (selected_p
8556 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
8557 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
8558 else
8559 idx = (selected_p
8560 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
8561 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
8562
8563 xassert (ASIZE (image) >= idx);
8564 image = AREF (image, idx);
8565 }
8566 else
8567 idx = -1;
8568
8569 /* Ignore invalid image specifications. */
8570 if (!valid_image_p (image))
8571 continue;
8572
8573 /* Display the tool-bar button pressed, or depressed. */
8574 plist = Fcopy_sequence (XCDR (image));
8575
8576 /* Compute margin and relief to draw. */
8577 relief = (tool_bar_button_relief >= 0
8578 ? tool_bar_button_relief
8579 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
8580 hmargin = vmargin = relief;
8581
8582 if (INTEGERP (Vtool_bar_button_margin)
8583 && XINT (Vtool_bar_button_margin) > 0)
8584 {
8585 hmargin += XFASTINT (Vtool_bar_button_margin);
8586 vmargin += XFASTINT (Vtool_bar_button_margin);
8587 }
8588 else if (CONSP (Vtool_bar_button_margin))
8589 {
8590 if (INTEGERP (XCAR (Vtool_bar_button_margin))
8591 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
8592 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
8593
8594 if (INTEGERP (XCDR (Vtool_bar_button_margin))
8595 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
8596 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
8597 }
8598
8599 if (auto_raise_tool_bar_buttons_p)
8600 {
8601 /* Add a `:relief' property to the image spec if the item is
8602 selected. */
8603 if (selected_p)
8604 {
8605 plist = Fplist_put (plist, QCrelief, make_number (-relief));
8606 hmargin -= relief;
8607 vmargin -= relief;
8608 }
8609 }
8610 else
8611 {
8612 /* If image is selected, display it pressed, i.e. with a
8613 negative relief. If it's not selected, display it with a
8614 raised relief. */
8615 plist = Fplist_put (plist, QCrelief,
8616 (selected_p
8617 ? make_number (-relief)
8618 : make_number (relief)));
8619 hmargin -= relief;
8620 vmargin -= relief;
8621 }
8622
8623 /* Put a margin around the image. */
8624 if (hmargin || vmargin)
8625 {
8626 if (hmargin == vmargin)
8627 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
8628 else
8629 plist = Fplist_put (plist, QCmargin,
8630 Fcons (make_number (hmargin),
8631 make_number (vmargin)));
8632 }
8633
8634 /* If button is not enabled, and we don't have special images
8635 for the disabled state, make the image appear disabled by
8636 applying an appropriate algorithm to it. */
8637 if (!enabled_p && idx < 0)
8638 plist = Fplist_put (plist, QCconversion, Qdisabled);
8639
8640 /* Put a `display' text property on the string for the image to
8641 display. Put a `menu-item' property on the string that gives
8642 the start of this item's properties in the tool-bar items
8643 vector. */
8644 image = Fcons (Qimage, plist);
8645 props = list4 (Qdisplay, image,
8646 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
8647
8648 /* Let the last image hide all remaining spaces in the tool bar
8649 string. The string can be longer than needed when we reuse a
8650 previous string. */
8651 if (i + 1 == f->n_tool_bar_items)
8652 end = SCHARS (f->desired_tool_bar_string);
8653 else
8654 end = i + 1;
8655 Fadd_text_properties (make_number (i), make_number (end),
8656 props, f->desired_tool_bar_string);
8657 #undef PROP
8658 }
8659
8660 UNGCPRO;
8661 }
8662
8663
8664 /* Display one line of the tool-bar of frame IT->f. */
8665
8666 static void
8667 display_tool_bar_line (it)
8668 struct it *it;
8669 {
8670 struct glyph_row *row = it->glyph_row;
8671 int max_x = it->last_visible_x;
8672 struct glyph *last;
8673
8674 prepare_desired_row (row);
8675 row->y = it->current_y;
8676
8677 /* Note that this isn't made use of if the face hasn't a box,
8678 so there's no need to check the face here. */
8679 it->start_of_box_run_p = 1;
8680
8681 while (it->current_x < max_x)
8682 {
8683 int x_before, x, n_glyphs_before, i, nglyphs;
8684
8685 /* Get the next display element. */
8686 if (!get_next_display_element (it))
8687 break;
8688
8689 /* Produce glyphs. */
8690 x_before = it->current_x;
8691 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
8692 PRODUCE_GLYPHS (it);
8693
8694 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
8695 i = 0;
8696 x = x_before;
8697 while (i < nglyphs)
8698 {
8699 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
8700
8701 if (x + glyph->pixel_width > max_x)
8702 {
8703 /* Glyph doesn't fit on line. */
8704 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
8705 it->current_x = x;
8706 goto out;
8707 }
8708
8709 ++it->hpos;
8710 x += glyph->pixel_width;
8711 ++i;
8712 }
8713
8714 /* Stop at line ends. */
8715 if (ITERATOR_AT_END_OF_LINE_P (it))
8716 break;
8717
8718 set_iterator_to_next (it, 1);
8719 }
8720
8721 out:;
8722
8723 row->displays_text_p = row->used[TEXT_AREA] != 0;
8724 extend_face_to_end_of_line (it);
8725 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
8726 last->right_box_line_p = 1;
8727 if (last == row->glyphs[TEXT_AREA])
8728 last->left_box_line_p = 1;
8729 compute_line_metrics (it);
8730
8731 /* If line is empty, make it occupy the rest of the tool-bar. */
8732 if (!row->displays_text_p)
8733 {
8734 row->height = row->phys_height = it->last_visible_y - row->y;
8735 row->ascent = row->phys_ascent = 0;
8736 row->extra_line_spacing = 0;
8737 }
8738
8739 row->full_width_p = 1;
8740 row->continued_p = 0;
8741 row->truncated_on_left_p = 0;
8742 row->truncated_on_right_p = 0;
8743
8744 it->current_x = it->hpos = 0;
8745 it->current_y += row->height;
8746 ++it->vpos;
8747 ++it->glyph_row;
8748 }
8749
8750
8751 /* Value is the number of screen lines needed to make all tool-bar
8752 items of frame F visible. */
8753
8754 static int
8755 tool_bar_lines_needed (f)
8756 struct frame *f;
8757 {
8758 struct window *w = XWINDOW (f->tool_bar_window);
8759 struct it it;
8760
8761 /* Initialize an iterator for iteration over
8762 F->desired_tool_bar_string in the tool-bar window of frame F. */
8763 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
8764 it.first_visible_x = 0;
8765 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
8766 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
8767
8768 while (!ITERATOR_AT_END_P (&it))
8769 {
8770 it.glyph_row = w->desired_matrix->rows;
8771 clear_glyph_row (it.glyph_row);
8772 display_tool_bar_line (&it);
8773 }
8774
8775 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
8776 }
8777
8778
8779 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
8780 0, 1, 0,
8781 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
8782 (frame)
8783 Lisp_Object frame;
8784 {
8785 struct frame *f;
8786 struct window *w;
8787 int nlines = 0;
8788
8789 if (NILP (frame))
8790 frame = selected_frame;
8791 else
8792 CHECK_FRAME (frame);
8793 f = XFRAME (frame);
8794
8795 if (WINDOWP (f->tool_bar_window)
8796 || (w = XWINDOW (f->tool_bar_window),
8797 WINDOW_TOTAL_LINES (w) > 0))
8798 {
8799 update_tool_bar (f, 1);
8800 if (f->n_tool_bar_items)
8801 {
8802 build_desired_tool_bar_string (f);
8803 nlines = tool_bar_lines_needed (f);
8804 }
8805 }
8806
8807 return make_number (nlines);
8808 }
8809
8810
8811 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
8812 height should be changed. */
8813
8814 static int
8815 redisplay_tool_bar (f)
8816 struct frame *f;
8817 {
8818 struct window *w;
8819 struct it it;
8820 struct glyph_row *row;
8821 int change_height_p = 0;
8822
8823 #ifdef USE_GTK
8824 if (FRAME_EXTERNAL_TOOL_BAR (f))
8825 update_frame_tool_bar (f);
8826 return 0;
8827 #endif
8828
8829 /* If frame hasn't a tool-bar window or if it is zero-height, don't
8830 do anything. This means you must start with tool-bar-lines
8831 non-zero to get the auto-sizing effect. Or in other words, you
8832 can turn off tool-bars by specifying tool-bar-lines zero. */
8833 if (!WINDOWP (f->tool_bar_window)
8834 || (w = XWINDOW (f->tool_bar_window),
8835 WINDOW_TOTAL_LINES (w) == 0))
8836 return 0;
8837
8838 /* Set up an iterator for the tool-bar window. */
8839 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
8840 it.first_visible_x = 0;
8841 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
8842 row = it.glyph_row;
8843
8844 /* Build a string that represents the contents of the tool-bar. */
8845 build_desired_tool_bar_string (f);
8846 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
8847
8848 /* Display as many lines as needed to display all tool-bar items. */
8849 while (it.current_y < it.last_visible_y)
8850 display_tool_bar_line (&it);
8851
8852 /* It doesn't make much sense to try scrolling in the tool-bar
8853 window, so don't do it. */
8854 w->desired_matrix->no_scrolling_p = 1;
8855 w->must_be_updated_p = 1;
8856
8857 if (auto_resize_tool_bars_p)
8858 {
8859 int nlines;
8860
8861 /* If we couldn't display everything, change the tool-bar's
8862 height. */
8863 if (IT_STRING_CHARPOS (it) < it.end_charpos)
8864 change_height_p = 1;
8865
8866 /* If there are blank lines at the end, except for a partially
8867 visible blank line at the end that is smaller than
8868 FRAME_LINE_HEIGHT, change the tool-bar's height. */
8869 row = it.glyph_row - 1;
8870 if (!row->displays_text_p
8871 && row->height >= FRAME_LINE_HEIGHT (f))
8872 change_height_p = 1;
8873
8874 /* If row displays tool-bar items, but is partially visible,
8875 change the tool-bar's height. */
8876 if (row->displays_text_p
8877 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y)
8878 change_height_p = 1;
8879
8880 /* Resize windows as needed by changing the `tool-bar-lines'
8881 frame parameter. */
8882 if (change_height_p
8883 && (nlines = tool_bar_lines_needed (f),
8884 nlines != WINDOW_TOTAL_LINES (w)))
8885 {
8886 extern Lisp_Object Qtool_bar_lines;
8887 Lisp_Object frame;
8888 int old_height = WINDOW_TOTAL_LINES (w);
8889
8890 XSETFRAME (frame, f);
8891 clear_glyph_matrix (w->desired_matrix);
8892 Fmodify_frame_parameters (frame,
8893 Fcons (Fcons (Qtool_bar_lines,
8894 make_number (nlines)),
8895 Qnil));
8896 if (WINDOW_TOTAL_LINES (w) != old_height)
8897 fonts_changed_p = 1;
8898 }
8899 }
8900
8901 return change_height_p;
8902 }
8903
8904
8905 /* Get information about the tool-bar item which is displayed in GLYPH
8906 on frame F. Return in *PROP_IDX the index where tool-bar item
8907 properties start in F->tool_bar_items. Value is zero if
8908 GLYPH doesn't display a tool-bar item. */
8909
8910 static int
8911 tool_bar_item_info (f, glyph, prop_idx)
8912 struct frame *f;
8913 struct glyph *glyph;
8914 int *prop_idx;
8915 {
8916 Lisp_Object prop;
8917 int success_p;
8918 int charpos;
8919
8920 /* This function can be called asynchronously, which means we must
8921 exclude any possibility that Fget_text_property signals an
8922 error. */
8923 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
8924 charpos = max (0, charpos);
8925
8926 /* Get the text property `menu-item' at pos. The value of that
8927 property is the start index of this item's properties in
8928 F->tool_bar_items. */
8929 prop = Fget_text_property (make_number (charpos),
8930 Qmenu_item, f->current_tool_bar_string);
8931 if (INTEGERP (prop))
8932 {
8933 *prop_idx = XINT (prop);
8934 success_p = 1;
8935 }
8936 else
8937 success_p = 0;
8938
8939 return success_p;
8940 }
8941
8942 \f
8943 /* Get information about the tool-bar item at position X/Y on frame F.
8944 Return in *GLYPH a pointer to the glyph of the tool-bar item in
8945 the current matrix of the tool-bar window of F, or NULL if not
8946 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
8947 item in F->tool_bar_items. Value is
8948
8949 -1 if X/Y is not on a tool-bar item
8950 0 if X/Y is on the same item that was highlighted before.
8951 1 otherwise. */
8952
8953 static int
8954 get_tool_bar_item (f, x, y, glyph, hpos, vpos, prop_idx)
8955 struct frame *f;
8956 int x, y;
8957 struct glyph **glyph;
8958 int *hpos, *vpos, *prop_idx;
8959 {
8960 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
8961 struct window *w = XWINDOW (f->tool_bar_window);
8962 int area;
8963
8964 /* Find the glyph under X/Y. */
8965 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
8966 if (*glyph == NULL)
8967 return -1;
8968
8969 /* Get the start of this tool-bar item's properties in
8970 f->tool_bar_items. */
8971 if (!tool_bar_item_info (f, *glyph, prop_idx))
8972 return -1;
8973
8974 /* Is mouse on the highlighted item? */
8975 if (EQ (f->tool_bar_window, dpyinfo->mouse_face_window)
8976 && *vpos >= dpyinfo->mouse_face_beg_row
8977 && *vpos <= dpyinfo->mouse_face_end_row
8978 && (*vpos > dpyinfo->mouse_face_beg_row
8979 || *hpos >= dpyinfo->mouse_face_beg_col)
8980 && (*vpos < dpyinfo->mouse_face_end_row
8981 || *hpos < dpyinfo->mouse_face_end_col
8982 || dpyinfo->mouse_face_past_end))
8983 return 0;
8984
8985 return 1;
8986 }
8987
8988
8989 /* EXPORT:
8990 Handle mouse button event on the tool-bar of frame F, at
8991 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
8992 0 for button release. MODIFIERS is event modifiers for button
8993 release. */
8994
8995 void
8996 handle_tool_bar_click (f, x, y, down_p, modifiers)
8997 struct frame *f;
8998 int x, y, down_p;
8999 unsigned int modifiers;
9000 {
9001 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9002 struct window *w = XWINDOW (f->tool_bar_window);
9003 int hpos, vpos, prop_idx;
9004 struct glyph *glyph;
9005 Lisp_Object enabled_p;
9006
9007 /* If not on the highlighted tool-bar item, return. */
9008 frame_to_window_pixel_xy (w, &x, &y);
9009 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
9010 return;
9011
9012 /* If item is disabled, do nothing. */
9013 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
9014 if (NILP (enabled_p))
9015 return;
9016
9017 if (down_p)
9018 {
9019 /* Show item in pressed state. */
9020 show_mouse_face (dpyinfo, DRAW_IMAGE_SUNKEN);
9021 dpyinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
9022 last_tool_bar_item = prop_idx;
9023 }
9024 else
9025 {
9026 Lisp_Object key, frame;
9027 struct input_event event;
9028 EVENT_INIT (event);
9029
9030 /* Show item in released state. */
9031 show_mouse_face (dpyinfo, DRAW_IMAGE_RAISED);
9032 dpyinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
9033
9034 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
9035
9036 XSETFRAME (frame, f);
9037 event.kind = TOOL_BAR_EVENT;
9038 event.frame_or_window = frame;
9039 event.arg = frame;
9040 kbd_buffer_store_event (&event);
9041
9042 event.kind = TOOL_BAR_EVENT;
9043 event.frame_or_window = frame;
9044 event.arg = key;
9045 event.modifiers = modifiers;
9046 kbd_buffer_store_event (&event);
9047 last_tool_bar_item = -1;
9048 }
9049 }
9050
9051
9052 /* Possibly highlight a tool-bar item on frame F when mouse moves to
9053 tool-bar window-relative coordinates X/Y. Called from
9054 note_mouse_highlight. */
9055
9056 static void
9057 note_tool_bar_highlight (f, x, y)
9058 struct frame *f;
9059 int x, y;
9060 {
9061 Lisp_Object window = f->tool_bar_window;
9062 struct window *w = XWINDOW (window);
9063 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9064 int hpos, vpos;
9065 struct glyph *glyph;
9066 struct glyph_row *row;
9067 int i;
9068 Lisp_Object enabled_p;
9069 int prop_idx;
9070 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
9071 int mouse_down_p, rc;
9072
9073 /* Function note_mouse_highlight is called with negative x(y
9074 values when mouse moves outside of the frame. */
9075 if (x <= 0 || y <= 0)
9076 {
9077 clear_mouse_face (dpyinfo);
9078 return;
9079 }
9080
9081 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
9082 if (rc < 0)
9083 {
9084 /* Not on tool-bar item. */
9085 clear_mouse_face (dpyinfo);
9086 return;
9087 }
9088 else if (rc == 0)
9089 /* On same tool-bar item as before. */
9090 goto set_help_echo;
9091
9092 clear_mouse_face (dpyinfo);
9093
9094 /* Mouse is down, but on different tool-bar item? */
9095 mouse_down_p = (dpyinfo->grabbed
9096 && f == last_mouse_frame
9097 && FRAME_LIVE_P (f));
9098 if (mouse_down_p
9099 && last_tool_bar_item != prop_idx)
9100 return;
9101
9102 dpyinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
9103 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
9104
9105 /* If tool-bar item is not enabled, don't highlight it. */
9106 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
9107 if (!NILP (enabled_p))
9108 {
9109 /* Compute the x-position of the glyph. In front and past the
9110 image is a space. We include this in the highlighted area. */
9111 row = MATRIX_ROW (w->current_matrix, vpos);
9112 for (i = x = 0; i < hpos; ++i)
9113 x += row->glyphs[TEXT_AREA][i].pixel_width;
9114
9115 /* Record this as the current active region. */
9116 dpyinfo->mouse_face_beg_col = hpos;
9117 dpyinfo->mouse_face_beg_row = vpos;
9118 dpyinfo->mouse_face_beg_x = x;
9119 dpyinfo->mouse_face_beg_y = row->y;
9120 dpyinfo->mouse_face_past_end = 0;
9121
9122 dpyinfo->mouse_face_end_col = hpos + 1;
9123 dpyinfo->mouse_face_end_row = vpos;
9124 dpyinfo->mouse_face_end_x = x + glyph->pixel_width;
9125 dpyinfo->mouse_face_end_y = row->y;
9126 dpyinfo->mouse_face_window = window;
9127 dpyinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
9128
9129 /* Display it as active. */
9130 show_mouse_face (dpyinfo, draw);
9131 dpyinfo->mouse_face_image_state = draw;
9132 }
9133
9134 set_help_echo:
9135
9136 /* Set help_echo_string to a help string to display for this tool-bar item.
9137 XTread_socket does the rest. */
9138 help_echo_object = help_echo_window = Qnil;
9139 help_echo_pos = -1;
9140 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
9141 if (NILP (help_echo_string))
9142 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
9143 }
9144
9145 #endif /* HAVE_WINDOW_SYSTEM */
9146
9147
9148 \f
9149 /************************************************************************
9150 Horizontal scrolling
9151 ************************************************************************/
9152
9153 static int hscroll_window_tree P_ ((Lisp_Object));
9154 static int hscroll_windows P_ ((Lisp_Object));
9155
9156 /* For all leaf windows in the window tree rooted at WINDOW, set their
9157 hscroll value so that PT is (i) visible in the window, and (ii) so
9158 that it is not within a certain margin at the window's left and
9159 right border. Value is non-zero if any window's hscroll has been
9160 changed. */
9161
9162 static int
9163 hscroll_window_tree (window)
9164 Lisp_Object window;
9165 {
9166 int hscrolled_p = 0;
9167 int hscroll_relative_p = FLOATP (Vhscroll_step);
9168 int hscroll_step_abs = 0;
9169 double hscroll_step_rel = 0;
9170
9171 if (hscroll_relative_p)
9172 {
9173 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
9174 if (hscroll_step_rel < 0)
9175 {
9176 hscroll_relative_p = 0;
9177 hscroll_step_abs = 0;
9178 }
9179 }
9180 else if (INTEGERP (Vhscroll_step))
9181 {
9182 hscroll_step_abs = XINT (Vhscroll_step);
9183 if (hscroll_step_abs < 0)
9184 hscroll_step_abs = 0;
9185 }
9186 else
9187 hscroll_step_abs = 0;
9188
9189 while (WINDOWP (window))
9190 {
9191 struct window *w = XWINDOW (window);
9192
9193 if (WINDOWP (w->hchild))
9194 hscrolled_p |= hscroll_window_tree (w->hchild);
9195 else if (WINDOWP (w->vchild))
9196 hscrolled_p |= hscroll_window_tree (w->vchild);
9197 else if (w->cursor.vpos >= 0)
9198 {
9199 int h_margin;
9200 int text_area_width;
9201 struct glyph_row *current_cursor_row
9202 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
9203 struct glyph_row *desired_cursor_row
9204 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
9205 struct glyph_row *cursor_row
9206 = (desired_cursor_row->enabled_p
9207 ? desired_cursor_row
9208 : current_cursor_row);
9209
9210 text_area_width = window_box_width (w, TEXT_AREA);
9211
9212 /* Scroll when cursor is inside this scroll margin. */
9213 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
9214
9215 if ((XFASTINT (w->hscroll)
9216 && w->cursor.x <= h_margin)
9217 || (cursor_row->enabled_p
9218 && cursor_row->truncated_on_right_p
9219 && (w->cursor.x >= text_area_width - h_margin)))
9220 {
9221 struct it it;
9222 int hscroll;
9223 struct buffer *saved_current_buffer;
9224 int pt;
9225 int wanted_x;
9226
9227 /* Find point in a display of infinite width. */
9228 saved_current_buffer = current_buffer;
9229 current_buffer = XBUFFER (w->buffer);
9230
9231 if (w == XWINDOW (selected_window))
9232 pt = BUF_PT (current_buffer);
9233 else
9234 {
9235 pt = marker_position (w->pointm);
9236 pt = max (BEGV, pt);
9237 pt = min (ZV, pt);
9238 }
9239
9240 /* Move iterator to pt starting at cursor_row->start in
9241 a line with infinite width. */
9242 init_to_row_start (&it, w, cursor_row);
9243 it.last_visible_x = INFINITY;
9244 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
9245 current_buffer = saved_current_buffer;
9246
9247 /* Position cursor in window. */
9248 if (!hscroll_relative_p && hscroll_step_abs == 0)
9249 hscroll = max (0, (it.current_x
9250 - (ITERATOR_AT_END_OF_LINE_P (&it)
9251 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
9252 : (text_area_width / 2))))
9253 / FRAME_COLUMN_WIDTH (it.f);
9254 else if (w->cursor.x >= text_area_width - h_margin)
9255 {
9256 if (hscroll_relative_p)
9257 wanted_x = text_area_width * (1 - hscroll_step_rel)
9258 - h_margin;
9259 else
9260 wanted_x = text_area_width
9261 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
9262 - h_margin;
9263 hscroll
9264 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
9265 }
9266 else
9267 {
9268 if (hscroll_relative_p)
9269 wanted_x = text_area_width * hscroll_step_rel
9270 + h_margin;
9271 else
9272 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
9273 + h_margin;
9274 hscroll
9275 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
9276 }
9277 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
9278
9279 /* Don't call Fset_window_hscroll if value hasn't
9280 changed because it will prevent redisplay
9281 optimizations. */
9282 if (XFASTINT (w->hscroll) != hscroll)
9283 {
9284 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
9285 w->hscroll = make_number (hscroll);
9286 hscrolled_p = 1;
9287 }
9288 }
9289 }
9290
9291 window = w->next;
9292 }
9293
9294 /* Value is non-zero if hscroll of any leaf window has been changed. */
9295 return hscrolled_p;
9296 }
9297
9298
9299 /* Set hscroll so that cursor is visible and not inside horizontal
9300 scroll margins for all windows in the tree rooted at WINDOW. See
9301 also hscroll_window_tree above. Value is non-zero if any window's
9302 hscroll has been changed. If it has, desired matrices on the frame
9303 of WINDOW are cleared. */
9304
9305 static int
9306 hscroll_windows (window)
9307 Lisp_Object window;
9308 {
9309 int hscrolled_p;
9310
9311 if (automatic_hscrolling_p)
9312 {
9313 hscrolled_p = hscroll_window_tree (window);
9314 if (hscrolled_p)
9315 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
9316 }
9317 else
9318 hscrolled_p = 0;
9319 return hscrolled_p;
9320 }
9321
9322
9323 \f
9324 /************************************************************************
9325 Redisplay
9326 ************************************************************************/
9327
9328 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
9329 to a non-zero value. This is sometimes handy to have in a debugger
9330 session. */
9331
9332 #if GLYPH_DEBUG
9333
9334 /* First and last unchanged row for try_window_id. */
9335
9336 int debug_first_unchanged_at_end_vpos;
9337 int debug_last_unchanged_at_beg_vpos;
9338
9339 /* Delta vpos and y. */
9340
9341 int debug_dvpos, debug_dy;
9342
9343 /* Delta in characters and bytes for try_window_id. */
9344
9345 int debug_delta, debug_delta_bytes;
9346
9347 /* Values of window_end_pos and window_end_vpos at the end of
9348 try_window_id. */
9349
9350 EMACS_INT debug_end_pos, debug_end_vpos;
9351
9352 /* Append a string to W->desired_matrix->method. FMT is a printf
9353 format string. A1...A9 are a supplement for a variable-length
9354 argument list. If trace_redisplay_p is non-zero also printf the
9355 resulting string to stderr. */
9356
9357 static void
9358 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
9359 struct window *w;
9360 char *fmt;
9361 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
9362 {
9363 char buffer[512];
9364 char *method = w->desired_matrix->method;
9365 int len = strlen (method);
9366 int size = sizeof w->desired_matrix->method;
9367 int remaining = size - len - 1;
9368
9369 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
9370 if (len && remaining)
9371 {
9372 method[len] = '|';
9373 --remaining, ++len;
9374 }
9375
9376 strncpy (method + len, buffer, remaining);
9377
9378 if (trace_redisplay_p)
9379 fprintf (stderr, "%p (%s): %s\n",
9380 w,
9381 ((BUFFERP (w->buffer)
9382 && STRINGP (XBUFFER (w->buffer)->name))
9383 ? (char *) SDATA (XBUFFER (w->buffer)->name)
9384 : "no buffer"),
9385 buffer);
9386 }
9387
9388 #endif /* GLYPH_DEBUG */
9389
9390
9391 /* Value is non-zero if all changes in window W, which displays
9392 current_buffer, are in the text between START and END. START is a
9393 buffer position, END is given as a distance from Z. Used in
9394 redisplay_internal for display optimization. */
9395
9396 static INLINE int
9397 text_outside_line_unchanged_p (w, start, end)
9398 struct window *w;
9399 int start, end;
9400 {
9401 int unchanged_p = 1;
9402
9403 /* If text or overlays have changed, see where. */
9404 if (XFASTINT (w->last_modified) < MODIFF
9405 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
9406 {
9407 /* Gap in the line? */
9408 if (GPT < start || Z - GPT < end)
9409 unchanged_p = 0;
9410
9411 /* Changes start in front of the line, or end after it? */
9412 if (unchanged_p
9413 && (BEG_UNCHANGED < start - 1
9414 || END_UNCHANGED < end))
9415 unchanged_p = 0;
9416
9417 /* If selective display, can't optimize if changes start at the
9418 beginning of the line. */
9419 if (unchanged_p
9420 && INTEGERP (current_buffer->selective_display)
9421 && XINT (current_buffer->selective_display) > 0
9422 && (BEG_UNCHANGED < start || GPT <= start))
9423 unchanged_p = 0;
9424
9425 /* If there are overlays at the start or end of the line, these
9426 may have overlay strings with newlines in them. A change at
9427 START, for instance, may actually concern the display of such
9428 overlay strings as well, and they are displayed on different
9429 lines. So, quickly rule out this case. (For the future, it
9430 might be desirable to implement something more telling than
9431 just BEG/END_UNCHANGED.) */
9432 if (unchanged_p)
9433 {
9434 if (BEG + BEG_UNCHANGED == start
9435 && overlay_touches_p (start))
9436 unchanged_p = 0;
9437 if (END_UNCHANGED == end
9438 && overlay_touches_p (Z - end))
9439 unchanged_p = 0;
9440 }
9441 }
9442
9443 return unchanged_p;
9444 }
9445
9446
9447 /* Do a frame update, taking possible shortcuts into account. This is
9448 the main external entry point for redisplay.
9449
9450 If the last redisplay displayed an echo area message and that message
9451 is no longer requested, we clear the echo area or bring back the
9452 mini-buffer if that is in use. */
9453
9454 void
9455 redisplay ()
9456 {
9457 redisplay_internal (0);
9458 }
9459
9460
9461 static Lisp_Object
9462 overlay_arrow_string_or_property (var, pbitmap)
9463 Lisp_Object var;
9464 int *pbitmap;
9465 {
9466 Lisp_Object pstr = Fget (var, Qoverlay_arrow_string);
9467 Lisp_Object bitmap;
9468
9469 if (pbitmap)
9470 {
9471 *pbitmap = 0;
9472 if (bitmap = Fget (var, Qoverlay_arrow_bitmap), INTEGERP (bitmap))
9473 *pbitmap = XINT (bitmap);
9474 }
9475
9476 if (!NILP (pstr))
9477 return pstr;
9478 return Voverlay_arrow_string;
9479 }
9480
9481 /* Return 1 if there are any overlay-arrows in current_buffer. */
9482 static int
9483 overlay_arrow_in_current_buffer_p ()
9484 {
9485 Lisp_Object vlist;
9486
9487 for (vlist = Voverlay_arrow_variable_list;
9488 CONSP (vlist);
9489 vlist = XCDR (vlist))
9490 {
9491 Lisp_Object var = XCAR (vlist);
9492 Lisp_Object val;
9493
9494 if (!SYMBOLP (var))
9495 continue;
9496 val = find_symbol_value (var);
9497 if (MARKERP (val)
9498 && current_buffer == XMARKER (val)->buffer)
9499 return 1;
9500 }
9501 return 0;
9502 }
9503
9504
9505 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
9506 has changed. */
9507
9508 static int
9509 overlay_arrows_changed_p ()
9510 {
9511 Lisp_Object vlist;
9512
9513 for (vlist = Voverlay_arrow_variable_list;
9514 CONSP (vlist);
9515 vlist = XCDR (vlist))
9516 {
9517 Lisp_Object var = XCAR (vlist);
9518 Lisp_Object val, pstr;
9519
9520 if (!SYMBOLP (var))
9521 continue;
9522 val = find_symbol_value (var);
9523 if (!MARKERP (val))
9524 continue;
9525 if (! EQ (COERCE_MARKER (val),
9526 Fget (var, Qlast_arrow_position))
9527 || ! (pstr = overlay_arrow_string_or_property (var, 0),
9528 EQ (pstr, Fget (var, Qlast_arrow_string))))
9529 return 1;
9530 }
9531 return 0;
9532 }
9533
9534 /* Mark overlay arrows to be updated on next redisplay. */
9535
9536 static void
9537 update_overlay_arrows (up_to_date)
9538 int up_to_date;
9539 {
9540 Lisp_Object vlist;
9541
9542 for (vlist = Voverlay_arrow_variable_list;
9543 CONSP (vlist);
9544 vlist = XCDR (vlist))
9545 {
9546 Lisp_Object var = XCAR (vlist);
9547
9548 if (!SYMBOLP (var))
9549 continue;
9550
9551 if (up_to_date > 0)
9552 {
9553 Lisp_Object val = find_symbol_value (var);
9554 Fput (var, Qlast_arrow_position,
9555 COERCE_MARKER (val));
9556 Fput (var, Qlast_arrow_string,
9557 overlay_arrow_string_or_property (var, 0));
9558 }
9559 else if (up_to_date < 0
9560 || !NILP (Fget (var, Qlast_arrow_position)))
9561 {
9562 Fput (var, Qlast_arrow_position, Qt);
9563 Fput (var, Qlast_arrow_string, Qt);
9564 }
9565 }
9566 }
9567
9568
9569 /* Return overlay arrow string to display at row.
9570 Return t if display as bitmap in left fringe.
9571 Return nil if no overlay arrow. */
9572
9573 static Lisp_Object
9574 overlay_arrow_at_row (it, row, pbitmap)
9575 struct it *it;
9576 struct glyph_row *row;
9577 int *pbitmap;
9578 {
9579 Lisp_Object vlist;
9580
9581 for (vlist = Voverlay_arrow_variable_list;
9582 CONSP (vlist);
9583 vlist = XCDR (vlist))
9584 {
9585 Lisp_Object var = XCAR (vlist);
9586 Lisp_Object val;
9587
9588 if (!SYMBOLP (var))
9589 continue;
9590
9591 val = find_symbol_value (var);
9592
9593 if (MARKERP (val)
9594 && current_buffer == XMARKER (val)->buffer
9595 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
9596 {
9597 val = overlay_arrow_string_or_property (var, pbitmap);
9598 if (FRAME_WINDOW_P (it->f)
9599 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
9600 return Qt;
9601 if (STRINGP (val))
9602 return val;
9603 break;
9604 }
9605 }
9606
9607 *pbitmap = 0;
9608 return Qnil;
9609 }
9610
9611 /* Return 1 if point moved out of or into a composition. Otherwise
9612 return 0. PREV_BUF and PREV_PT are the last point buffer and
9613 position. BUF and PT are the current point buffer and position. */
9614
9615 int
9616 check_point_in_composition (prev_buf, prev_pt, buf, pt)
9617 struct buffer *prev_buf, *buf;
9618 int prev_pt, pt;
9619 {
9620 int start, end;
9621 Lisp_Object prop;
9622 Lisp_Object buffer;
9623
9624 XSETBUFFER (buffer, buf);
9625 /* Check a composition at the last point if point moved within the
9626 same buffer. */
9627 if (prev_buf == buf)
9628 {
9629 if (prev_pt == pt)
9630 /* Point didn't move. */
9631 return 0;
9632
9633 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
9634 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
9635 && COMPOSITION_VALID_P (start, end, prop)
9636 && start < prev_pt && end > prev_pt)
9637 /* The last point was within the composition. Return 1 iff
9638 point moved out of the composition. */
9639 return (pt <= start || pt >= end);
9640 }
9641
9642 /* Check a composition at the current point. */
9643 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
9644 && find_composition (pt, -1, &start, &end, &prop, buffer)
9645 && COMPOSITION_VALID_P (start, end, prop)
9646 && start < pt && end > pt);
9647 }
9648
9649
9650 /* Reconsider the setting of B->clip_changed which is displayed
9651 in window W. */
9652
9653 static INLINE void
9654 reconsider_clip_changes (w, b)
9655 struct window *w;
9656 struct buffer *b;
9657 {
9658 if (b->clip_changed
9659 && !NILP (w->window_end_valid)
9660 && w->current_matrix->buffer == b
9661 && w->current_matrix->zv == BUF_ZV (b)
9662 && w->current_matrix->begv == BUF_BEGV (b))
9663 b->clip_changed = 0;
9664
9665 /* If display wasn't paused, and W is not a tool bar window, see if
9666 point has been moved into or out of a composition. In that case,
9667 we set b->clip_changed to 1 to force updating the screen. If
9668 b->clip_changed has already been set to 1, we can skip this
9669 check. */
9670 if (!b->clip_changed
9671 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
9672 {
9673 int pt;
9674
9675 if (w == XWINDOW (selected_window))
9676 pt = BUF_PT (current_buffer);
9677 else
9678 pt = marker_position (w->pointm);
9679
9680 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
9681 || pt != XINT (w->last_point))
9682 && check_point_in_composition (w->current_matrix->buffer,
9683 XINT (w->last_point),
9684 XBUFFER (w->buffer), pt))
9685 b->clip_changed = 1;
9686 }
9687 }
9688 \f
9689
9690 /* Select FRAME to forward the values of frame-local variables into C
9691 variables so that the redisplay routines can access those values
9692 directly. */
9693
9694 static void
9695 select_frame_for_redisplay (frame)
9696 Lisp_Object frame;
9697 {
9698 Lisp_Object tail, sym, val;
9699 Lisp_Object old = selected_frame;
9700
9701 selected_frame = frame;
9702
9703 for (tail = XFRAME (frame)->param_alist; CONSP (tail); tail = XCDR (tail))
9704 if (CONSP (XCAR (tail))
9705 && (sym = XCAR (XCAR (tail)),
9706 SYMBOLP (sym))
9707 && (sym = indirect_variable (sym),
9708 val = SYMBOL_VALUE (sym),
9709 (BUFFER_LOCAL_VALUEP (val)
9710 || SOME_BUFFER_LOCAL_VALUEP (val)))
9711 && XBUFFER_LOCAL_VALUE (val)->check_frame)
9712 Fsymbol_value (sym);
9713
9714 for (tail = XFRAME (old)->param_alist; CONSP (tail); tail = XCDR (tail))
9715 if (CONSP (XCAR (tail))
9716 && (sym = XCAR (XCAR (tail)),
9717 SYMBOLP (sym))
9718 && (sym = indirect_variable (sym),
9719 val = SYMBOL_VALUE (sym),
9720 (BUFFER_LOCAL_VALUEP (val)
9721 || SOME_BUFFER_LOCAL_VALUEP (val)))
9722 && XBUFFER_LOCAL_VALUE (val)->check_frame)
9723 Fsymbol_value (sym);
9724 }
9725
9726
9727 #define STOP_POLLING \
9728 do { if (! polling_stopped_here) stop_polling (); \
9729 polling_stopped_here = 1; } while (0)
9730
9731 #define RESUME_POLLING \
9732 do { if (polling_stopped_here) start_polling (); \
9733 polling_stopped_here = 0; } while (0)
9734
9735
9736 /* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
9737 response to any user action; therefore, we should preserve the echo
9738 area. (Actually, our caller does that job.) Perhaps in the future
9739 avoid recentering windows if it is not necessary; currently that
9740 causes some problems. */
9741
9742 static void
9743 redisplay_internal (preserve_echo_area)
9744 int preserve_echo_area;
9745 {
9746 struct window *w = XWINDOW (selected_window);
9747 struct frame *f = XFRAME (w->frame);
9748 int pause;
9749 int must_finish = 0;
9750 struct text_pos tlbufpos, tlendpos;
9751 int number_of_visible_frames;
9752 int count;
9753 struct frame *sf = SELECTED_FRAME ();
9754 int polling_stopped_here = 0;
9755
9756 /* Non-zero means redisplay has to consider all windows on all
9757 frames. Zero means, only selected_window is considered. */
9758 int consider_all_windows_p;
9759
9760 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
9761
9762 /* No redisplay if running in batch mode or frame is not yet fully
9763 initialized, or redisplay is explicitly turned off by setting
9764 Vinhibit_redisplay. */
9765 if (noninteractive
9766 || !NILP (Vinhibit_redisplay)
9767 || !f->glyphs_initialized_p)
9768 return;
9769
9770 /* The flag redisplay_performed_directly_p is set by
9771 direct_output_for_insert when it already did the whole screen
9772 update necessary. */
9773 if (redisplay_performed_directly_p)
9774 {
9775 redisplay_performed_directly_p = 0;
9776 if (!hscroll_windows (selected_window))
9777 return;
9778 }
9779
9780 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
9781 if (popup_activated ())
9782 return;
9783 #endif
9784
9785 /* I don't think this happens but let's be paranoid. */
9786 if (redisplaying_p)
9787 return;
9788
9789 /* Record a function that resets redisplaying_p to its old value
9790 when we leave this function. */
9791 count = SPECPDL_INDEX ();
9792 record_unwind_protect (unwind_redisplay,
9793 Fcons (make_number (redisplaying_p), selected_frame));
9794 ++redisplaying_p;
9795 specbind (Qinhibit_free_realized_faces, Qnil);
9796
9797 retry:
9798 pause = 0;
9799 reconsider_clip_changes (w, current_buffer);
9800
9801 /* If new fonts have been loaded that make a glyph matrix adjustment
9802 necessary, do it. */
9803 if (fonts_changed_p)
9804 {
9805 adjust_glyphs (NULL);
9806 ++windows_or_buffers_changed;
9807 fonts_changed_p = 0;
9808 }
9809
9810 /* If face_change_count is non-zero, init_iterator will free all
9811 realized faces, which includes the faces referenced from current
9812 matrices. So, we can't reuse current matrices in this case. */
9813 if (face_change_count)
9814 ++windows_or_buffers_changed;
9815
9816 if (FRAME_TERMCAP_P (sf)
9817 && FRAME_TTY (sf)->previous_terminal_frame != sf)
9818 {
9819 /* Since frames on a single ASCII terminal share the same
9820 display area, displaying a different frame means redisplay
9821 the whole thing. */
9822 windows_or_buffers_changed++;
9823 SET_FRAME_GARBAGED (sf);
9824 FRAME_TTY (sf)->previous_terminal_frame = sf;
9825 }
9826
9827 /* Set the visible flags for all frames. Do this before checking
9828 for resized or garbaged frames; they want to know if their frames
9829 are visible. See the comment in frame.h for
9830 FRAME_SAMPLE_VISIBILITY. */
9831 {
9832 Lisp_Object tail, frame;
9833
9834 number_of_visible_frames = 0;
9835
9836 FOR_EACH_FRAME (tail, frame)
9837 {
9838 struct frame *f = XFRAME (frame);
9839
9840 FRAME_SAMPLE_VISIBILITY (f);
9841 if (FRAME_VISIBLE_P (f))
9842 ++number_of_visible_frames;
9843 clear_desired_matrices (f);
9844 }
9845 }
9846
9847
9848 /* Notice any pending interrupt request to change frame size. */
9849 do_pending_window_change (1);
9850
9851 /* Clear frames marked as garbaged. */
9852 if (frame_garbaged)
9853 clear_garbaged_frames ();
9854
9855 /* Build menubar and tool-bar items. */
9856 prepare_menu_bars ();
9857
9858 if (windows_or_buffers_changed)
9859 update_mode_lines++;
9860
9861 /* Detect case that we need to write or remove a star in the mode line. */
9862 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
9863 {
9864 w->update_mode_line = Qt;
9865 if (buffer_shared > 1)
9866 update_mode_lines++;
9867 }
9868
9869 /* If %c is in the mode line, update it if needed. */
9870 if (!NILP (w->column_number_displayed)
9871 /* This alternative quickly identifies a common case
9872 where no change is needed. */
9873 && !(PT == XFASTINT (w->last_point)
9874 && XFASTINT (w->last_modified) >= MODIFF
9875 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
9876 && (XFASTINT (w->column_number_displayed)
9877 != (int) current_column ())) /* iftc */
9878 w->update_mode_line = Qt;
9879
9880 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
9881
9882 /* The variable buffer_shared is set in redisplay_window and
9883 indicates that we redisplay a buffer in different windows. See
9884 there. */
9885 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
9886 || cursor_type_changed);
9887
9888 /* If specs for an arrow have changed, do thorough redisplay
9889 to ensure we remove any arrow that should no longer exist. */
9890 if (overlay_arrows_changed_p ())
9891 consider_all_windows_p = windows_or_buffers_changed = 1;
9892
9893 /* Normally the message* functions will have already displayed and
9894 updated the echo area, but the frame may have been trashed, or
9895 the update may have been preempted, so display the echo area
9896 again here. Checking message_cleared_p captures the case that
9897 the echo area should be cleared. */
9898 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
9899 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
9900 || (message_cleared_p
9901 && minibuf_level == 0
9902 /* If the mini-window is currently selected, this means the
9903 echo-area doesn't show through. */
9904 && !MINI_WINDOW_P (XWINDOW (selected_window))))
9905 {
9906 int window_height_changed_p = echo_area_display (0);
9907 must_finish = 1;
9908
9909 /* If we don't display the current message, don't clear the
9910 message_cleared_p flag, because, if we did, we wouldn't clear
9911 the echo area in the next redisplay which doesn't preserve
9912 the echo area. */
9913 if (!display_last_displayed_message_p)
9914 message_cleared_p = 0;
9915
9916 if (fonts_changed_p)
9917 goto retry;
9918 else if (window_height_changed_p)
9919 {
9920 consider_all_windows_p = 1;
9921 ++update_mode_lines;
9922 ++windows_or_buffers_changed;
9923
9924 /* If window configuration was changed, frames may have been
9925 marked garbaged. Clear them or we will experience
9926 surprises wrt scrolling. */
9927 if (frame_garbaged)
9928 clear_garbaged_frames ();
9929 }
9930 }
9931 else if (EQ (selected_window, minibuf_window)
9932 && (current_buffer->clip_changed
9933 || XFASTINT (w->last_modified) < MODIFF
9934 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
9935 && resize_mini_window (w, 0))
9936 {
9937 /* Resized active mini-window to fit the size of what it is
9938 showing if its contents might have changed. */
9939 must_finish = 1;
9940 consider_all_windows_p = 1;
9941 ++windows_or_buffers_changed;
9942 ++update_mode_lines;
9943
9944 /* If window configuration was changed, frames may have been
9945 marked garbaged. Clear them or we will experience
9946 surprises wrt scrolling. */
9947 if (frame_garbaged)
9948 clear_garbaged_frames ();
9949 }
9950
9951
9952 /* If showing the region, and mark has changed, we must redisplay
9953 the whole window. The assignment to this_line_start_pos prevents
9954 the optimization directly below this if-statement. */
9955 if (((!NILP (Vtransient_mark_mode)
9956 && !NILP (XBUFFER (w->buffer)->mark_active))
9957 != !NILP (w->region_showing))
9958 || (!NILP (w->region_showing)
9959 && !EQ (w->region_showing,
9960 Fmarker_position (XBUFFER (w->buffer)->mark))))
9961 CHARPOS (this_line_start_pos) = 0;
9962
9963 /* Optimize the case that only the line containing the cursor in the
9964 selected window has changed. Variables starting with this_ are
9965 set in display_line and record information about the line
9966 containing the cursor. */
9967 tlbufpos = this_line_start_pos;
9968 tlendpos = this_line_end_pos;
9969 if (!consider_all_windows_p
9970 && CHARPOS (tlbufpos) > 0
9971 && NILP (w->update_mode_line)
9972 && !current_buffer->clip_changed
9973 && !current_buffer->prevent_redisplay_optimizations_p
9974 && FRAME_VISIBLE_P (XFRAME (w->frame))
9975 && !FRAME_OBSCURED_P (XFRAME (w->frame))
9976 /* Make sure recorded data applies to current buffer, etc. */
9977 && this_line_buffer == current_buffer
9978 && current_buffer == XBUFFER (w->buffer)
9979 && NILP (w->force_start)
9980 && NILP (w->optional_new_start)
9981 /* Point must be on the line that we have info recorded about. */
9982 && PT >= CHARPOS (tlbufpos)
9983 && PT <= Z - CHARPOS (tlendpos)
9984 /* All text outside that line, including its final newline,
9985 must be unchanged */
9986 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
9987 CHARPOS (tlendpos)))
9988 {
9989 if (CHARPOS (tlbufpos) > BEGV
9990 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
9991 && (CHARPOS (tlbufpos) == ZV
9992 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
9993 /* Former continuation line has disappeared by becoming empty */
9994 goto cancel;
9995 else if (XFASTINT (w->last_modified) < MODIFF
9996 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
9997 || MINI_WINDOW_P (w))
9998 {
9999 /* We have to handle the case of continuation around a
10000 wide-column character (See the comment in indent.c around
10001 line 885).
10002
10003 For instance, in the following case:
10004
10005 -------- Insert --------
10006 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
10007 J_I_ ==> J_I_ `^^' are cursors.
10008 ^^ ^^
10009 -------- --------
10010
10011 As we have to redraw the line above, we should goto cancel. */
10012
10013 struct it it;
10014 int line_height_before = this_line_pixel_height;
10015
10016 /* Note that start_display will handle the case that the
10017 line starting at tlbufpos is a continuation lines. */
10018 start_display (&it, w, tlbufpos);
10019
10020 /* Implementation note: It this still necessary? */
10021 if (it.current_x != this_line_start_x)
10022 goto cancel;
10023
10024 TRACE ((stderr, "trying display optimization 1\n"));
10025 w->cursor.vpos = -1;
10026 overlay_arrow_seen = 0;
10027 it.vpos = this_line_vpos;
10028 it.current_y = this_line_y;
10029 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
10030 display_line (&it);
10031
10032 /* If line contains point, is not continued,
10033 and ends at same distance from eob as before, we win */
10034 if (w->cursor.vpos >= 0
10035 /* Line is not continued, otherwise this_line_start_pos
10036 would have been set to 0 in display_line. */
10037 && CHARPOS (this_line_start_pos)
10038 /* Line ends as before. */
10039 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
10040 /* Line has same height as before. Otherwise other lines
10041 would have to be shifted up or down. */
10042 && this_line_pixel_height == line_height_before)
10043 {
10044 /* If this is not the window's last line, we must adjust
10045 the charstarts of the lines below. */
10046 if (it.current_y < it.last_visible_y)
10047 {
10048 struct glyph_row *row
10049 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
10050 int delta, delta_bytes;
10051
10052 if (Z - CHARPOS (tlendpos) == ZV)
10053 {
10054 /* This line ends at end of (accessible part of)
10055 buffer. There is no newline to count. */
10056 delta = (Z
10057 - CHARPOS (tlendpos)
10058 - MATRIX_ROW_START_CHARPOS (row));
10059 delta_bytes = (Z_BYTE
10060 - BYTEPOS (tlendpos)
10061 - MATRIX_ROW_START_BYTEPOS (row));
10062 }
10063 else
10064 {
10065 /* This line ends in a newline. Must take
10066 account of the newline and the rest of the
10067 text that follows. */
10068 delta = (Z
10069 - CHARPOS (tlendpos)
10070 - MATRIX_ROW_START_CHARPOS (row));
10071 delta_bytes = (Z_BYTE
10072 - BYTEPOS (tlendpos)
10073 - MATRIX_ROW_START_BYTEPOS (row));
10074 }
10075
10076 increment_matrix_positions (w->current_matrix,
10077 this_line_vpos + 1,
10078 w->current_matrix->nrows,
10079 delta, delta_bytes);
10080 }
10081
10082 /* If this row displays text now but previously didn't,
10083 or vice versa, w->window_end_vpos may have to be
10084 adjusted. */
10085 if ((it.glyph_row - 1)->displays_text_p)
10086 {
10087 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
10088 XSETINT (w->window_end_vpos, this_line_vpos);
10089 }
10090 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
10091 && this_line_vpos > 0)
10092 XSETINT (w->window_end_vpos, this_line_vpos - 1);
10093 w->window_end_valid = Qnil;
10094
10095 /* Update hint: No need to try to scroll in update_window. */
10096 w->desired_matrix->no_scrolling_p = 1;
10097
10098 #if GLYPH_DEBUG
10099 *w->desired_matrix->method = 0;
10100 debug_method_add (w, "optimization 1");
10101 #endif
10102 #ifdef HAVE_WINDOW_SYSTEM
10103 update_window_fringes (w, 0);
10104 #endif
10105 goto update;
10106 }
10107 else
10108 goto cancel;
10109 }
10110 else if (/* Cursor position hasn't changed. */
10111 PT == XFASTINT (w->last_point)
10112 /* Make sure the cursor was last displayed
10113 in this window. Otherwise we have to reposition it. */
10114 && 0 <= w->cursor.vpos
10115 && WINDOW_TOTAL_LINES (w) > w->cursor.vpos)
10116 {
10117 if (!must_finish)
10118 {
10119 do_pending_window_change (1);
10120
10121 /* We used to always goto end_of_redisplay here, but this
10122 isn't enough if we have a blinking cursor. */
10123 if (w->cursor_off_p == w->last_cursor_off_p)
10124 goto end_of_redisplay;
10125 }
10126 goto update;
10127 }
10128 /* If highlighting the region, or if the cursor is in the echo area,
10129 then we can't just move the cursor. */
10130 else if (! (!NILP (Vtransient_mark_mode)
10131 && !NILP (current_buffer->mark_active))
10132 && (EQ (selected_window, current_buffer->last_selected_window)
10133 || highlight_nonselected_windows)
10134 && NILP (w->region_showing)
10135 && NILP (Vshow_trailing_whitespace)
10136 && !cursor_in_echo_area)
10137 {
10138 struct it it;
10139 struct glyph_row *row;
10140
10141 /* Skip from tlbufpos to PT and see where it is. Note that
10142 PT may be in invisible text. If so, we will end at the
10143 next visible position. */
10144 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
10145 NULL, DEFAULT_FACE_ID);
10146 it.current_x = this_line_start_x;
10147 it.current_y = this_line_y;
10148 it.vpos = this_line_vpos;
10149
10150 /* The call to move_it_to stops in front of PT, but
10151 moves over before-strings. */
10152 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
10153
10154 if (it.vpos == this_line_vpos
10155 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
10156 row->enabled_p))
10157 {
10158 xassert (this_line_vpos == it.vpos);
10159 xassert (this_line_y == it.current_y);
10160 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
10161 #if GLYPH_DEBUG
10162 *w->desired_matrix->method = 0;
10163 debug_method_add (w, "optimization 3");
10164 #endif
10165 goto update;
10166 }
10167 else
10168 goto cancel;
10169 }
10170
10171 cancel:
10172 /* Text changed drastically or point moved off of line. */
10173 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
10174 }
10175
10176 CHARPOS (this_line_start_pos) = 0;
10177 consider_all_windows_p |= buffer_shared > 1;
10178 ++clear_face_cache_count;
10179
10180
10181 /* Build desired matrices, and update the display. If
10182 consider_all_windows_p is non-zero, do it for all windows on all
10183 frames. Otherwise do it for selected_window, only. */
10184
10185 if (consider_all_windows_p)
10186 {
10187 Lisp_Object tail, frame;
10188 int i, n = 0, size = 50;
10189 struct frame **updated
10190 = (struct frame **) alloca (size * sizeof *updated);
10191
10192 /* Clear the face cache eventually. */
10193 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
10194 {
10195 clear_face_cache (0);
10196 clear_face_cache_count = 0;
10197 }
10198
10199 /* Recompute # windows showing selected buffer. This will be
10200 incremented each time such a window is displayed. */
10201 buffer_shared = 0;
10202
10203 FOR_EACH_FRAME (tail, frame)
10204 {
10205 struct frame *f = XFRAME (frame);
10206
10207 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
10208 {
10209 if (! EQ (frame, selected_frame))
10210 /* Select the frame, for the sake of frame-local
10211 variables. */
10212 select_frame_for_redisplay (frame);
10213
10214 #ifdef HAVE_WINDOW_SYSTEM
10215 if (clear_face_cache_count % 50 == 0
10216 && FRAME_WINDOW_P (f))
10217 clear_image_cache (f, 0);
10218 #endif /* HAVE_WINDOW_SYSTEM */
10219
10220 /* Mark all the scroll bars to be removed; we'll redeem
10221 the ones we want when we redisplay their windows. */
10222 if (FRAME_DISPLAY (f)->condemn_scroll_bars_hook)
10223 FRAME_DISPLAY (f)->condemn_scroll_bars_hook (f);
10224
10225 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
10226 redisplay_windows (FRAME_ROOT_WINDOW (f));
10227
10228 /* Any scroll bars which redisplay_windows should have
10229 nuked should now go away. */
10230 if (FRAME_DISPLAY (f)->judge_scroll_bars_hook)
10231 FRAME_DISPLAY (f)->judge_scroll_bars_hook (f);
10232
10233 /* If fonts changed, display again. */
10234 /* ??? rms: I suspect it is a mistake to jump all the way
10235 back to retry here. It should just retry this frame. */
10236 if (fonts_changed_p)
10237 goto retry;
10238
10239 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
10240 {
10241 /* See if we have to hscroll. */
10242 if (hscroll_windows (f->root_window))
10243 goto retry;
10244
10245 /* Prevent various kinds of signals during display
10246 update. stdio is not robust about handling
10247 signals, which can cause an apparent I/O
10248 error. */
10249 if (interrupt_input)
10250 unrequest_sigio ();
10251 STOP_POLLING;
10252
10253 /* Update the display. */
10254 set_window_update_flags (XWINDOW (f->root_window), 1);
10255 pause |= update_frame (f, 0, 0);
10256 #if 0 /* Exiting the loop can leave the wrong value for buffer_shared. */
10257 if (pause)
10258 break;
10259 #endif
10260
10261 if (n == size)
10262 {
10263 int nbytes = size * sizeof *updated;
10264 struct frame **p = (struct frame **) alloca (2 * nbytes);
10265 bcopy (updated, p, nbytes);
10266 size *= 2;
10267 }
10268
10269 updated[n++] = f;
10270 }
10271 }
10272 }
10273
10274 if (!pause)
10275 {
10276 /* Do the mark_window_display_accurate after all windows have
10277 been redisplayed because this call resets flags in buffers
10278 which are needed for proper redisplay. */
10279 for (i = 0; i < n; ++i)
10280 {
10281 struct frame *f = updated[i];
10282 mark_window_display_accurate (f->root_window, 1);
10283 if (FRAME_DISPLAY (f)->frame_up_to_date_hook)
10284 FRAME_DISPLAY (f)->frame_up_to_date_hook (f);
10285 }
10286 }
10287 }
10288 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
10289 {
10290 Lisp_Object mini_window;
10291 struct frame *mini_frame;
10292
10293 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
10294 /* Use list_of_error, not Qerror, so that
10295 we catch only errors and don't run the debugger. */
10296 internal_condition_case_1 (redisplay_window_1, selected_window,
10297 list_of_error,
10298 redisplay_window_error);
10299
10300 /* Compare desired and current matrices, perform output. */
10301
10302 update:
10303 /* If fonts changed, display again. */
10304 if (fonts_changed_p)
10305 goto retry;
10306
10307 /* Prevent various kinds of signals during display update.
10308 stdio is not robust about handling signals,
10309 which can cause an apparent I/O error. */
10310 if (interrupt_input)
10311 unrequest_sigio ();
10312 STOP_POLLING;
10313
10314 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
10315 {
10316 if (hscroll_windows (selected_window))
10317 goto retry;
10318
10319 XWINDOW (selected_window)->must_be_updated_p = 1;
10320 pause = update_frame (sf, 0, 0);
10321 }
10322
10323 /* We may have called echo_area_display at the top of this
10324 function. If the echo area is on another frame, that may
10325 have put text on a frame other than the selected one, so the
10326 above call to update_frame would not have caught it. Catch
10327 it here. */
10328 mini_window = FRAME_MINIBUF_WINDOW (sf);
10329 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
10330
10331 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
10332 {
10333 XWINDOW (mini_window)->must_be_updated_p = 1;
10334 pause |= update_frame (mini_frame, 0, 0);
10335 if (!pause && hscroll_windows (mini_window))
10336 goto retry;
10337 }
10338 }
10339
10340 /* If display was paused because of pending input, make sure we do a
10341 thorough update the next time. */
10342 if (pause)
10343 {
10344 /* Prevent the optimization at the beginning of
10345 redisplay_internal that tries a single-line update of the
10346 line containing the cursor in the selected window. */
10347 CHARPOS (this_line_start_pos) = 0;
10348
10349 /* Let the overlay arrow be updated the next time. */
10350 update_overlay_arrows (0);
10351
10352 /* If we pause after scrolling, some rows in the current
10353 matrices of some windows are not valid. */
10354 if (!WINDOW_FULL_WIDTH_P (w)
10355 && !FRAME_WINDOW_P (XFRAME (w->frame)))
10356 update_mode_lines = 1;
10357 }
10358 else
10359 {
10360 if (!consider_all_windows_p)
10361 {
10362 /* This has already been done above if
10363 consider_all_windows_p is set. */
10364 mark_window_display_accurate_1 (w, 1);
10365
10366 /* Say overlay arrows are up to date. */
10367 update_overlay_arrows (1);
10368
10369 if (FRAME_DISPLAY (sf)->frame_up_to_date_hook != 0)
10370 FRAME_DISPLAY (sf)->frame_up_to_date_hook (sf);
10371 }
10372
10373 update_mode_lines = 0;
10374 windows_or_buffers_changed = 0;
10375 cursor_type_changed = 0;
10376 }
10377
10378 /* Start SIGIO interrupts coming again. Having them off during the
10379 code above makes it less likely one will discard output, but not
10380 impossible, since there might be stuff in the system buffer here.
10381 But it is much hairier to try to do anything about that. */
10382 if (interrupt_input)
10383 request_sigio ();
10384 RESUME_POLLING;
10385
10386 /* If a frame has become visible which was not before, redisplay
10387 again, so that we display it. Expose events for such a frame
10388 (which it gets when becoming visible) don't call the parts of
10389 redisplay constructing glyphs, so simply exposing a frame won't
10390 display anything in this case. So, we have to display these
10391 frames here explicitly. */
10392 if (!pause)
10393 {
10394 Lisp_Object tail, frame;
10395 int new_count = 0;
10396
10397 FOR_EACH_FRAME (tail, frame)
10398 {
10399 int this_is_visible = 0;
10400
10401 if (XFRAME (frame)->visible)
10402 this_is_visible = 1;
10403 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
10404 if (XFRAME (frame)->visible)
10405 this_is_visible = 1;
10406
10407 if (this_is_visible)
10408 new_count++;
10409 }
10410
10411 if (new_count != number_of_visible_frames)
10412 windows_or_buffers_changed++;
10413 }
10414
10415 /* Change frame size now if a change is pending. */
10416 do_pending_window_change (1);
10417
10418 /* If we just did a pending size change, or have additional
10419 visible frames, redisplay again. */
10420 if (windows_or_buffers_changed && !pause)
10421 goto retry;
10422
10423 end_of_redisplay:
10424 unbind_to (count, Qnil);
10425 RESUME_POLLING;
10426 }
10427
10428
10429 /* Redisplay, but leave alone any recent echo area message unless
10430 another message has been requested in its place.
10431
10432 This is useful in situations where you need to redisplay but no
10433 user action has occurred, making it inappropriate for the message
10434 area to be cleared. See tracking_off and
10435 wait_reading_process_output for examples of these situations.
10436
10437 FROM_WHERE is an integer saying from where this function was
10438 called. This is useful for debugging. */
10439
10440 void
10441 redisplay_preserve_echo_area (from_where)
10442 int from_where;
10443 {
10444 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
10445
10446 if (!NILP (echo_area_buffer[1]))
10447 {
10448 /* We have a previously displayed message, but no current
10449 message. Redisplay the previous message. */
10450 display_last_displayed_message_p = 1;
10451 redisplay_internal (1);
10452 display_last_displayed_message_p = 0;
10453 }
10454 else
10455 redisplay_internal (1);
10456
10457 if (FRAME_RIF (SELECTED_FRAME ()) != NULL
10458 && FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
10459 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (NULL);
10460 }
10461
10462
10463 /* Function registered with record_unwind_protect in
10464 redisplay_internal. Reset redisplaying_p to the value it had
10465 before redisplay_internal was called, and clear
10466 prevent_freeing_realized_faces_p. It also selects the previously
10467 selected frame. */
10468
10469 static Lisp_Object
10470 unwind_redisplay (val)
10471 Lisp_Object val;
10472 {
10473 Lisp_Object old_redisplaying_p, old_frame;
10474
10475 old_redisplaying_p = XCAR (val);
10476 redisplaying_p = XFASTINT (old_redisplaying_p);
10477 old_frame = XCDR (val);
10478 if (! EQ (old_frame, selected_frame))
10479 select_frame_for_redisplay (old_frame);
10480 return Qnil;
10481 }
10482
10483
10484 /* Mark the display of window W as accurate or inaccurate. If
10485 ACCURATE_P is non-zero mark display of W as accurate. If
10486 ACCURATE_P is zero, arrange for W to be redisplayed the next time
10487 redisplay_internal is called. */
10488
10489 static void
10490 mark_window_display_accurate_1 (w, accurate_p)
10491 struct window *w;
10492 int accurate_p;
10493 {
10494 if (BUFFERP (w->buffer))
10495 {
10496 struct buffer *b = XBUFFER (w->buffer);
10497
10498 w->last_modified
10499 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
10500 w->last_overlay_modified
10501 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
10502 w->last_had_star
10503 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
10504
10505 if (accurate_p)
10506 {
10507 b->clip_changed = 0;
10508 b->prevent_redisplay_optimizations_p = 0;
10509
10510 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
10511 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
10512 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
10513 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
10514
10515 w->current_matrix->buffer = b;
10516 w->current_matrix->begv = BUF_BEGV (b);
10517 w->current_matrix->zv = BUF_ZV (b);
10518
10519 w->last_cursor = w->cursor;
10520 w->last_cursor_off_p = w->cursor_off_p;
10521
10522 if (w == XWINDOW (selected_window))
10523 w->last_point = make_number (BUF_PT (b));
10524 else
10525 w->last_point = make_number (XMARKER (w->pointm)->charpos);
10526 }
10527 }
10528
10529 if (accurate_p)
10530 {
10531 w->window_end_valid = w->buffer;
10532 #if 0 /* This is incorrect with variable-height lines. */
10533 xassert (XINT (w->window_end_vpos)
10534 < (WINDOW_TOTAL_LINES (w)
10535 - (WINDOW_WANTS_MODELINE_P (w) ? 1 : 0)));
10536 #endif
10537 w->update_mode_line = Qnil;
10538 }
10539 }
10540
10541
10542 /* Mark the display of windows in the window tree rooted at WINDOW as
10543 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
10544 windows as accurate. If ACCURATE_P is zero, arrange for windows to
10545 be redisplayed the next time redisplay_internal is called. */
10546
10547 void
10548 mark_window_display_accurate (window, accurate_p)
10549 Lisp_Object window;
10550 int accurate_p;
10551 {
10552 struct window *w;
10553
10554 for (; !NILP (window); window = w->next)
10555 {
10556 w = XWINDOW (window);
10557 mark_window_display_accurate_1 (w, accurate_p);
10558
10559 if (!NILP (w->vchild))
10560 mark_window_display_accurate (w->vchild, accurate_p);
10561 if (!NILP (w->hchild))
10562 mark_window_display_accurate (w->hchild, accurate_p);
10563 }
10564
10565 if (accurate_p)
10566 {
10567 update_overlay_arrows (1);
10568 }
10569 else
10570 {
10571 /* Force a thorough redisplay the next time by setting
10572 last_arrow_position and last_arrow_string to t, which is
10573 unequal to any useful value of Voverlay_arrow_... */
10574 update_overlay_arrows (-1);
10575 }
10576 }
10577
10578
10579 /* Return value in display table DP (Lisp_Char_Table *) for character
10580 C. Since a display table doesn't have any parent, we don't have to
10581 follow parent. Do not call this function directly but use the
10582 macro DISP_CHAR_VECTOR. */
10583
10584 Lisp_Object
10585 disp_char_vector (dp, c)
10586 struct Lisp_Char_Table *dp;
10587 int c;
10588 {
10589 int code[4], i;
10590 Lisp_Object val;
10591
10592 if (SINGLE_BYTE_CHAR_P (c))
10593 return (dp->contents[c]);
10594
10595 SPLIT_CHAR (c, code[0], code[1], code[2]);
10596 if (code[1] < 32)
10597 code[1] = -1;
10598 else if (code[2] < 32)
10599 code[2] = -1;
10600
10601 /* Here, the possible range of code[0] (== charset ID) is
10602 128..max_charset. Since the top level char table contains data
10603 for multibyte characters after 256th element, we must increment
10604 code[0] by 128 to get a correct index. */
10605 code[0] += 128;
10606 code[3] = -1; /* anchor */
10607
10608 for (i = 0; code[i] >= 0; i++, dp = XCHAR_TABLE (val))
10609 {
10610 val = dp->contents[code[i]];
10611 if (!SUB_CHAR_TABLE_P (val))
10612 return (NILP (val) ? dp->defalt : val);
10613 }
10614
10615 /* Here, val is a sub char table. We return the default value of
10616 it. */
10617 return (dp->defalt);
10618 }
10619
10620
10621 \f
10622 /***********************************************************************
10623 Window Redisplay
10624 ***********************************************************************/
10625
10626 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
10627
10628 static void
10629 redisplay_windows (window)
10630 Lisp_Object window;
10631 {
10632 while (!NILP (window))
10633 {
10634 struct window *w = XWINDOW (window);
10635
10636 if (!NILP (w->hchild))
10637 redisplay_windows (w->hchild);
10638 else if (!NILP (w->vchild))
10639 redisplay_windows (w->vchild);
10640 else
10641 {
10642 displayed_buffer = XBUFFER (w->buffer);
10643 /* Use list_of_error, not Qerror, so that
10644 we catch only errors and don't run the debugger. */
10645 internal_condition_case_1 (redisplay_window_0, window,
10646 list_of_error,
10647 redisplay_window_error);
10648 }
10649
10650 window = w->next;
10651 }
10652 }
10653
10654 static Lisp_Object
10655 redisplay_window_error ()
10656 {
10657 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
10658 return Qnil;
10659 }
10660
10661 static Lisp_Object
10662 redisplay_window_0 (window)
10663 Lisp_Object window;
10664 {
10665 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
10666 redisplay_window (window, 0);
10667 return Qnil;
10668 }
10669
10670 static Lisp_Object
10671 redisplay_window_1 (window)
10672 Lisp_Object window;
10673 {
10674 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
10675 redisplay_window (window, 1);
10676 return Qnil;
10677 }
10678 \f
10679
10680 /* Increment GLYPH until it reaches END or CONDITION fails while
10681 adding (GLYPH)->pixel_width to X. */
10682
10683 #define SKIP_GLYPHS(glyph, end, x, condition) \
10684 do \
10685 { \
10686 (x) += (glyph)->pixel_width; \
10687 ++(glyph); \
10688 } \
10689 while ((glyph) < (end) && (condition))
10690
10691
10692 /* Set cursor position of W. PT is assumed to be displayed in ROW.
10693 DELTA is the number of bytes by which positions recorded in ROW
10694 differ from current buffer positions. */
10695
10696 void
10697 set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
10698 struct window *w;
10699 struct glyph_row *row;
10700 struct glyph_matrix *matrix;
10701 int delta, delta_bytes, dy, dvpos;
10702 {
10703 struct glyph *glyph = row->glyphs[TEXT_AREA];
10704 struct glyph *end = glyph + row->used[TEXT_AREA];
10705 struct glyph *cursor = NULL;
10706 /* The first glyph that starts a sequence of glyphs from string. */
10707 struct glyph *string_start;
10708 /* The X coordinate of string_start. */
10709 int string_start_x;
10710 /* The last known character position. */
10711 int last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
10712 /* The last known character position before string_start. */
10713 int string_before_pos;
10714 int x = row->x;
10715 int cursor_x = x;
10716 int cursor_from_overlay_pos = 0;
10717 int pt_old = PT - delta;
10718
10719 /* Skip over glyphs not having an object at the start of the row.
10720 These are special glyphs like truncation marks on terminal
10721 frames. */
10722 if (row->displays_text_p)
10723 while (glyph < end
10724 && INTEGERP (glyph->object)
10725 && glyph->charpos < 0)
10726 {
10727 x += glyph->pixel_width;
10728 ++glyph;
10729 }
10730
10731 string_start = NULL;
10732 while (glyph < end
10733 && !INTEGERP (glyph->object)
10734 && (!BUFFERP (glyph->object)
10735 || (last_pos = glyph->charpos) < pt_old))
10736 {
10737 if (! STRINGP (glyph->object))
10738 {
10739 string_start = NULL;
10740 x += glyph->pixel_width;
10741 ++glyph;
10742 if (cursor_from_overlay_pos
10743 && last_pos > cursor_from_overlay_pos)
10744 {
10745 cursor_from_overlay_pos = 0;
10746 cursor = 0;
10747 }
10748 }
10749 else
10750 {
10751 string_before_pos = last_pos;
10752 string_start = glyph;
10753 string_start_x = x;
10754 /* Skip all glyphs from string. */
10755 do
10756 {
10757 int pos;
10758 if ((cursor == NULL || glyph > cursor)
10759 && !NILP (Fget_char_property (make_number ((glyph)->charpos),
10760 Qcursor, (glyph)->object))
10761 && (pos = string_buffer_position (w, glyph->object,
10762 string_before_pos),
10763 (pos == 0 /* From overlay */
10764 || pos == pt_old)))
10765 {
10766 /* Estimate overlay buffer position from the buffer
10767 positions of the glyphs before and after the overlay.
10768 Add 1 to last_pos so that if point corresponds to the
10769 glyph right after the overlay, we still use a 'cursor'
10770 property found in that overlay. */
10771 cursor_from_overlay_pos = pos == 0 ? last_pos+1 : 0;
10772 cursor = glyph;
10773 cursor_x = x;
10774 }
10775 x += glyph->pixel_width;
10776 ++glyph;
10777 }
10778 while (glyph < end && STRINGP (glyph->object));
10779 }
10780 }
10781
10782 if (cursor != NULL)
10783 {
10784 glyph = cursor;
10785 x = cursor_x;
10786 }
10787 else if (string_start
10788 && (glyph == end || !BUFFERP (glyph->object) || last_pos > pt_old))
10789 {
10790 /* We may have skipped over point because the previous glyphs
10791 are from string. As there's no easy way to know the
10792 character position of the current glyph, find the correct
10793 glyph on point by scanning from string_start again. */
10794 Lisp_Object limit;
10795 Lisp_Object string;
10796 int pos;
10797
10798 limit = make_number (pt_old + 1);
10799 end = glyph;
10800 glyph = string_start;
10801 x = string_start_x;
10802 string = glyph->object;
10803 pos = string_buffer_position (w, string, string_before_pos);
10804 /* If STRING is from overlay, LAST_POS == 0. We skip such glyphs
10805 because we always put cursor after overlay strings. */
10806 while (pos == 0 && glyph < end)
10807 {
10808 string = glyph->object;
10809 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
10810 if (glyph < end)
10811 pos = string_buffer_position (w, glyph->object, string_before_pos);
10812 }
10813
10814 while (glyph < end)
10815 {
10816 pos = XINT (Fnext_single_char_property_change
10817 (make_number (pos), Qdisplay, Qnil, limit));
10818 if (pos > pt_old)
10819 break;
10820 /* Skip glyphs from the same string. */
10821 string = glyph->object;
10822 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
10823 /* Skip glyphs from an overlay. */
10824 while (glyph < end
10825 && ! string_buffer_position (w, glyph->object, pos))
10826 {
10827 string = glyph->object;
10828 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
10829 }
10830 }
10831 }
10832
10833 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
10834 w->cursor.x = x;
10835 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
10836 w->cursor.y = row->y + dy;
10837
10838 if (w == XWINDOW (selected_window))
10839 {
10840 if (!row->continued_p
10841 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
10842 && row->x == 0)
10843 {
10844 this_line_buffer = XBUFFER (w->buffer);
10845
10846 CHARPOS (this_line_start_pos)
10847 = MATRIX_ROW_START_CHARPOS (row) + delta;
10848 BYTEPOS (this_line_start_pos)
10849 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
10850
10851 CHARPOS (this_line_end_pos)
10852 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
10853 BYTEPOS (this_line_end_pos)
10854 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
10855
10856 this_line_y = w->cursor.y;
10857 this_line_pixel_height = row->height;
10858 this_line_vpos = w->cursor.vpos;
10859 this_line_start_x = row->x;
10860 }
10861 else
10862 CHARPOS (this_line_start_pos) = 0;
10863 }
10864 }
10865
10866
10867 /* Run window scroll functions, if any, for WINDOW with new window
10868 start STARTP. Sets the window start of WINDOW to that position.
10869
10870 We assume that the window's buffer is really current. */
10871
10872 static INLINE struct text_pos
10873 run_window_scroll_functions (window, startp)
10874 Lisp_Object window;
10875 struct text_pos startp;
10876 {
10877 struct window *w = XWINDOW (window);
10878 SET_MARKER_FROM_TEXT_POS (w->start, startp);
10879
10880 if (current_buffer != XBUFFER (w->buffer))
10881 abort ();
10882
10883 if (!NILP (Vwindow_scroll_functions))
10884 {
10885 run_hook_with_args_2 (Qwindow_scroll_functions, window,
10886 make_number (CHARPOS (startp)));
10887 SET_TEXT_POS_FROM_MARKER (startp, w->start);
10888 /* In case the hook functions switch buffers. */
10889 if (current_buffer != XBUFFER (w->buffer))
10890 set_buffer_internal_1 (XBUFFER (w->buffer));
10891 }
10892
10893 return startp;
10894 }
10895
10896
10897 /* Make sure the line containing the cursor is fully visible.
10898 A value of 1 means there is nothing to be done.
10899 (Either the line is fully visible, or it cannot be made so,
10900 or we cannot tell.)
10901
10902 If FORCE_P is non-zero, return 0 even if partial visible cursor row
10903 is higher than window.
10904
10905 A value of 0 means the caller should do scrolling
10906 as if point had gone off the screen. */
10907
10908 static int
10909 make_cursor_line_fully_visible (w, force_p)
10910 struct window *w;
10911 int force_p;
10912 {
10913 struct glyph_matrix *matrix;
10914 struct glyph_row *row;
10915 int window_height;
10916
10917 if (!make_cursor_line_fully_visible_p)
10918 return 1;
10919
10920 /* It's not always possible to find the cursor, e.g, when a window
10921 is full of overlay strings. Don't do anything in that case. */
10922 if (w->cursor.vpos < 0)
10923 return 1;
10924
10925 matrix = w->desired_matrix;
10926 row = MATRIX_ROW (matrix, w->cursor.vpos);
10927
10928 /* If the cursor row is not partially visible, there's nothing to do. */
10929 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
10930 return 1;
10931
10932 /* If the row the cursor is in is taller than the window's height,
10933 it's not clear what to do, so do nothing. */
10934 window_height = window_box_height (w);
10935 if (row->height >= window_height)
10936 {
10937 if (!force_p || w->vscroll)
10938 return 1;
10939 }
10940 return 0;
10941
10942 #if 0
10943 /* This code used to try to scroll the window just enough to make
10944 the line visible. It returned 0 to say that the caller should
10945 allocate larger glyph matrices. */
10946
10947 if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (w, row))
10948 {
10949 int dy = row->height - row->visible_height;
10950 w->vscroll = 0;
10951 w->cursor.y += dy;
10952 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
10953 }
10954 else /* MATRIX_ROW_PARTIALLY_VISIBLE_AT_BOTTOM_P (w, row)) */
10955 {
10956 int dy = - (row->height - row->visible_height);
10957 w->vscroll = dy;
10958 w->cursor.y += dy;
10959 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
10960 }
10961
10962 /* When we change the cursor y-position of the selected window,
10963 change this_line_y as well so that the display optimization for
10964 the cursor line of the selected window in redisplay_internal uses
10965 the correct y-position. */
10966 if (w == XWINDOW (selected_window))
10967 this_line_y = w->cursor.y;
10968
10969 /* If vscrolling requires a larger glyph matrix, arrange for a fresh
10970 redisplay with larger matrices. */
10971 if (matrix->nrows < required_matrix_height (w))
10972 {
10973 fonts_changed_p = 1;
10974 return 0;
10975 }
10976
10977 return 1;
10978 #endif /* 0 */
10979 }
10980
10981
10982 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
10983 non-zero means only WINDOW is redisplayed in redisplay_internal.
10984 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
10985 in redisplay_window to bring a partially visible line into view in
10986 the case that only the cursor has moved.
10987
10988 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
10989 last screen line's vertical height extends past the end of the screen.
10990
10991 Value is
10992
10993 1 if scrolling succeeded
10994
10995 0 if scrolling didn't find point.
10996
10997 -1 if new fonts have been loaded so that we must interrupt
10998 redisplay, adjust glyph matrices, and try again. */
10999
11000 enum
11001 {
11002 SCROLLING_SUCCESS,
11003 SCROLLING_FAILED,
11004 SCROLLING_NEED_LARGER_MATRICES
11005 };
11006
11007 static int
11008 try_scrolling (window, just_this_one_p, scroll_conservatively,
11009 scroll_step, temp_scroll_step, last_line_misfit)
11010 Lisp_Object window;
11011 int just_this_one_p;
11012 EMACS_INT scroll_conservatively, scroll_step;
11013 int temp_scroll_step;
11014 int last_line_misfit;
11015 {
11016 struct window *w = XWINDOW (window);
11017 struct frame *f = XFRAME (w->frame);
11018 struct text_pos scroll_margin_pos;
11019 struct text_pos pos;
11020 struct text_pos startp;
11021 struct it it;
11022 Lisp_Object window_end;
11023 int this_scroll_margin;
11024 int dy = 0;
11025 int scroll_max;
11026 int rc;
11027 int amount_to_scroll = 0;
11028 Lisp_Object aggressive;
11029 int height;
11030 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
11031
11032 #if GLYPH_DEBUG
11033 debug_method_add (w, "try_scrolling");
11034 #endif
11035
11036 SET_TEXT_POS_FROM_MARKER (startp, w->start);
11037
11038 /* Compute scroll margin height in pixels. We scroll when point is
11039 within this distance from the top or bottom of the window. */
11040 if (scroll_margin > 0)
11041 {
11042 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
11043 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
11044 }
11045 else
11046 this_scroll_margin = 0;
11047
11048 /* Force scroll_conservatively to have a reasonable value so it doesn't
11049 cause an overflow while computing how much to scroll. */
11050 if (scroll_conservatively)
11051 scroll_conservatively = min (scroll_conservatively,
11052 MOST_POSITIVE_FIXNUM / FRAME_LINE_HEIGHT (f));
11053
11054 /* Compute how much we should try to scroll maximally to bring point
11055 into view. */
11056 if (scroll_step || scroll_conservatively || temp_scroll_step)
11057 scroll_max = max (scroll_step,
11058 max (scroll_conservatively, temp_scroll_step));
11059 else if (NUMBERP (current_buffer->scroll_down_aggressively)
11060 || NUMBERP (current_buffer->scroll_up_aggressively))
11061 /* We're trying to scroll because of aggressive scrolling
11062 but no scroll_step is set. Choose an arbitrary one. Maybe
11063 there should be a variable for this. */
11064 scroll_max = 10;
11065 else
11066 scroll_max = 0;
11067 scroll_max *= FRAME_LINE_HEIGHT (f);
11068
11069 /* Decide whether we have to scroll down. Start at the window end
11070 and move this_scroll_margin up to find the position of the scroll
11071 margin. */
11072 window_end = Fwindow_end (window, Qt);
11073
11074 too_near_end:
11075
11076 CHARPOS (scroll_margin_pos) = XINT (window_end);
11077 BYTEPOS (scroll_margin_pos) = CHAR_TO_BYTE (CHARPOS (scroll_margin_pos));
11078
11079 if (this_scroll_margin || extra_scroll_margin_lines)
11080 {
11081 start_display (&it, w, scroll_margin_pos);
11082 if (this_scroll_margin)
11083 move_it_vertically_backward (&it, this_scroll_margin);
11084 if (extra_scroll_margin_lines)
11085 move_it_by_lines (&it, - extra_scroll_margin_lines, 0);
11086 scroll_margin_pos = it.current.pos;
11087 }
11088
11089 if (PT >= CHARPOS (scroll_margin_pos))
11090 {
11091 int y0;
11092
11093 /* Point is in the scroll margin at the bottom of the window, or
11094 below. Compute a new window start that makes point visible. */
11095
11096 /* Compute the distance from the scroll margin to PT.
11097 Give up if the distance is greater than scroll_max. */
11098 start_display (&it, w, scroll_margin_pos);
11099 y0 = it.current_y;
11100 move_it_to (&it, PT, 0, it.last_visible_y, -1,
11101 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
11102
11103 /* To make point visible, we have to move the window start
11104 down so that the line the cursor is in is visible, which
11105 means we have to add in the height of the cursor line. */
11106 dy = line_bottom_y (&it) - y0;
11107
11108 if (dy > scroll_max)
11109 return SCROLLING_FAILED;
11110
11111 /* Move the window start down. If scrolling conservatively,
11112 move it just enough down to make point visible. If
11113 scroll_step is set, move it down by scroll_step. */
11114 start_display (&it, w, startp);
11115
11116 if (scroll_conservatively)
11117 /* Set AMOUNT_TO_SCROLL to at least one line,
11118 and at most scroll_conservatively lines. */
11119 amount_to_scroll
11120 = min (max (dy, FRAME_LINE_HEIGHT (f)),
11121 FRAME_LINE_HEIGHT (f) * scroll_conservatively);
11122 else if (scroll_step || temp_scroll_step)
11123 amount_to_scroll = scroll_max;
11124 else
11125 {
11126 aggressive = current_buffer->scroll_up_aggressively;
11127 height = WINDOW_BOX_TEXT_HEIGHT (w);
11128 if (NUMBERP (aggressive))
11129 {
11130 double float_amount = XFLOATINT (aggressive) * height;
11131 amount_to_scroll = float_amount;
11132 if (amount_to_scroll == 0 && float_amount > 0)
11133 amount_to_scroll = 1;
11134 }
11135 }
11136
11137 if (amount_to_scroll <= 0)
11138 return SCROLLING_FAILED;
11139
11140 /* If moving by amount_to_scroll leaves STARTP unchanged,
11141 move it down one screen line. */
11142
11143 move_it_vertically (&it, amount_to_scroll);
11144 if (CHARPOS (it.current.pos) == CHARPOS (startp))
11145 move_it_by_lines (&it, 1, 1);
11146 startp = it.current.pos;
11147 }
11148 else
11149 {
11150 /* See if point is inside the scroll margin at the top of the
11151 window. */
11152 scroll_margin_pos = startp;
11153 if (this_scroll_margin)
11154 {
11155 start_display (&it, w, startp);
11156 move_it_vertically (&it, this_scroll_margin);
11157 scroll_margin_pos = it.current.pos;
11158 }
11159
11160 if (PT < CHARPOS (scroll_margin_pos))
11161 {
11162 /* Point is in the scroll margin at the top of the window or
11163 above what is displayed in the window. */
11164 int y0;
11165
11166 /* Compute the vertical distance from PT to the scroll
11167 margin position. Give up if distance is greater than
11168 scroll_max. */
11169 SET_TEXT_POS (pos, PT, PT_BYTE);
11170 start_display (&it, w, pos);
11171 y0 = it.current_y;
11172 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
11173 it.last_visible_y, -1,
11174 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
11175 dy = it.current_y - y0;
11176 if (dy > scroll_max)
11177 return SCROLLING_FAILED;
11178
11179 /* Compute new window start. */
11180 start_display (&it, w, startp);
11181
11182 if (scroll_conservatively)
11183 amount_to_scroll
11184 = max (dy, FRAME_LINE_HEIGHT (f) * max (scroll_step, temp_scroll_step));
11185 else if (scroll_step || temp_scroll_step)
11186 amount_to_scroll = scroll_max;
11187 else
11188 {
11189 aggressive = current_buffer->scroll_down_aggressively;
11190 height = WINDOW_BOX_TEXT_HEIGHT (w);
11191 if (NUMBERP (aggressive))
11192 {
11193 double float_amount = XFLOATINT (aggressive) * height;
11194 amount_to_scroll = float_amount;
11195 if (amount_to_scroll == 0 && float_amount > 0)
11196 amount_to_scroll = 1;
11197 }
11198 }
11199
11200 if (amount_to_scroll <= 0)
11201 return SCROLLING_FAILED;
11202
11203 move_it_vertically_backward (&it, amount_to_scroll);
11204 startp = it.current.pos;
11205 }
11206 }
11207
11208 /* Run window scroll functions. */
11209 startp = run_window_scroll_functions (window, startp);
11210
11211 /* Display the window. Give up if new fonts are loaded, or if point
11212 doesn't appear. */
11213 if (!try_window (window, startp))
11214 rc = SCROLLING_NEED_LARGER_MATRICES;
11215 else if (w->cursor.vpos < 0)
11216 {
11217 clear_glyph_matrix (w->desired_matrix);
11218 rc = SCROLLING_FAILED;
11219 }
11220 else
11221 {
11222 /* Maybe forget recorded base line for line number display. */
11223 if (!just_this_one_p
11224 || current_buffer->clip_changed
11225 || BEG_UNCHANGED < CHARPOS (startp))
11226 w->base_line_number = Qnil;
11227
11228 /* If cursor ends up on a partially visible line,
11229 treat that as being off the bottom of the screen. */
11230 if (! make_cursor_line_fully_visible (w, extra_scroll_margin_lines <= 1))
11231 {
11232 clear_glyph_matrix (w->desired_matrix);
11233 ++extra_scroll_margin_lines;
11234 goto too_near_end;
11235 }
11236 rc = SCROLLING_SUCCESS;
11237 }
11238
11239 return rc;
11240 }
11241
11242
11243 /* Compute a suitable window start for window W if display of W starts
11244 on a continuation line. Value is non-zero if a new window start
11245 was computed.
11246
11247 The new window start will be computed, based on W's width, starting
11248 from the start of the continued line. It is the start of the
11249 screen line with the minimum distance from the old start W->start. */
11250
11251 static int
11252 compute_window_start_on_continuation_line (w)
11253 struct window *w;
11254 {
11255 struct text_pos pos, start_pos;
11256 int window_start_changed_p = 0;
11257
11258 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
11259
11260 /* If window start is on a continuation line... Window start may be
11261 < BEGV in case there's invisible text at the start of the
11262 buffer (M-x rmail, for example). */
11263 if (CHARPOS (start_pos) > BEGV
11264 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
11265 {
11266 struct it it;
11267 struct glyph_row *row;
11268
11269 /* Handle the case that the window start is out of range. */
11270 if (CHARPOS (start_pos) < BEGV)
11271 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
11272 else if (CHARPOS (start_pos) > ZV)
11273 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
11274
11275 /* Find the start of the continued line. This should be fast
11276 because scan_buffer is fast (newline cache). */
11277 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
11278 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
11279 row, DEFAULT_FACE_ID);
11280 reseat_at_previous_visible_line_start (&it);
11281
11282 /* If the line start is "too far" away from the window start,
11283 say it takes too much time to compute a new window start. */
11284 if (CHARPOS (start_pos) - IT_CHARPOS (it)
11285 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
11286 {
11287 int min_distance, distance;
11288
11289 /* Move forward by display lines to find the new window
11290 start. If window width was enlarged, the new start can
11291 be expected to be > the old start. If window width was
11292 decreased, the new window start will be < the old start.
11293 So, we're looking for the display line start with the
11294 minimum distance from the old window start. */
11295 pos = it.current.pos;
11296 min_distance = INFINITY;
11297 while ((distance = abs (CHARPOS (start_pos) - IT_CHARPOS (it))),
11298 distance < min_distance)
11299 {
11300 min_distance = distance;
11301 pos = it.current.pos;
11302 move_it_by_lines (&it, 1, 0);
11303 }
11304
11305 /* Set the window start there. */
11306 SET_MARKER_FROM_TEXT_POS (w->start, pos);
11307 window_start_changed_p = 1;
11308 }
11309 }
11310
11311 return window_start_changed_p;
11312 }
11313
11314
11315 /* Try cursor movement in case text has not changed in window WINDOW,
11316 with window start STARTP. Value is
11317
11318 CURSOR_MOVEMENT_SUCCESS if successful
11319
11320 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
11321
11322 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
11323 display. *SCROLL_STEP is set to 1, under certain circumstances, if
11324 we want to scroll as if scroll-step were set to 1. See the code.
11325
11326 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
11327 which case we have to abort this redisplay, and adjust matrices
11328 first. */
11329
11330 enum
11331 {
11332 CURSOR_MOVEMENT_SUCCESS,
11333 CURSOR_MOVEMENT_CANNOT_BE_USED,
11334 CURSOR_MOVEMENT_MUST_SCROLL,
11335 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
11336 };
11337
11338 static int
11339 try_cursor_movement (window, startp, scroll_step)
11340 Lisp_Object window;
11341 struct text_pos startp;
11342 int *scroll_step;
11343 {
11344 struct window *w = XWINDOW (window);
11345 struct frame *f = XFRAME (w->frame);
11346 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
11347
11348 #if GLYPH_DEBUG
11349 if (inhibit_try_cursor_movement)
11350 return rc;
11351 #endif
11352
11353 /* Handle case where text has not changed, only point, and it has
11354 not moved off the frame. */
11355 if (/* Point may be in this window. */
11356 PT >= CHARPOS (startp)
11357 /* Selective display hasn't changed. */
11358 && !current_buffer->clip_changed
11359 /* Function force-mode-line-update is used to force a thorough
11360 redisplay. It sets either windows_or_buffers_changed or
11361 update_mode_lines. So don't take a shortcut here for these
11362 cases. */
11363 && !update_mode_lines
11364 && !windows_or_buffers_changed
11365 && !cursor_type_changed
11366 /* Can't use this case if highlighting a region. When a
11367 region exists, cursor movement has to do more than just
11368 set the cursor. */
11369 && !(!NILP (Vtransient_mark_mode)
11370 && !NILP (current_buffer->mark_active))
11371 && NILP (w->region_showing)
11372 && NILP (Vshow_trailing_whitespace)
11373 /* Right after splitting windows, last_point may be nil. */
11374 && INTEGERP (w->last_point)
11375 /* This code is not used for mini-buffer for the sake of the case
11376 of redisplaying to replace an echo area message; since in
11377 that case the mini-buffer contents per se are usually
11378 unchanged. This code is of no real use in the mini-buffer
11379 since the handling of this_line_start_pos, etc., in redisplay
11380 handles the same cases. */
11381 && !EQ (window, minibuf_window)
11382 /* When splitting windows or for new windows, it happens that
11383 redisplay is called with a nil window_end_vpos or one being
11384 larger than the window. This should really be fixed in
11385 window.c. I don't have this on my list, now, so we do
11386 approximately the same as the old redisplay code. --gerd. */
11387 && INTEGERP (w->window_end_vpos)
11388 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
11389 && (FRAME_WINDOW_P (f)
11390 || !overlay_arrow_in_current_buffer_p ()))
11391 {
11392 int this_scroll_margin, top_scroll_margin;
11393 struct glyph_row *row = NULL;
11394
11395 #if GLYPH_DEBUG
11396 debug_method_add (w, "cursor movement");
11397 #endif
11398
11399 /* Scroll if point within this distance from the top or bottom
11400 of the window. This is a pixel value. */
11401 this_scroll_margin = max (0, scroll_margin);
11402 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
11403 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
11404
11405 top_scroll_margin = this_scroll_margin;
11406 if (WINDOW_WANTS_HEADER_LINE_P (w))
11407 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
11408
11409 /* Start with the row the cursor was displayed during the last
11410 not paused redisplay. Give up if that row is not valid. */
11411 if (w->last_cursor.vpos < 0
11412 || w->last_cursor.vpos >= w->current_matrix->nrows)
11413 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11414 else
11415 {
11416 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
11417 if (row->mode_line_p)
11418 ++row;
11419 if (!row->enabled_p)
11420 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11421 }
11422
11423 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
11424 {
11425 int scroll_p = 0;
11426 int last_y = window_text_bottom_y (w) - this_scroll_margin;
11427
11428 if (PT > XFASTINT (w->last_point))
11429 {
11430 /* Point has moved forward. */
11431 while (MATRIX_ROW_END_CHARPOS (row) < PT
11432 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
11433 {
11434 xassert (row->enabled_p);
11435 ++row;
11436 }
11437
11438 /* The end position of a row equals the start position
11439 of the next row. If PT is there, we would rather
11440 display it in the next line. */
11441 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
11442 && MATRIX_ROW_END_CHARPOS (row) == PT
11443 && !cursor_row_p (w, row))
11444 ++row;
11445
11446 /* If within the scroll margin, scroll. Note that
11447 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
11448 the next line would be drawn, and that
11449 this_scroll_margin can be zero. */
11450 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
11451 || PT > MATRIX_ROW_END_CHARPOS (row)
11452 /* Line is completely visible last line in window
11453 and PT is to be set in the next line. */
11454 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
11455 && PT == MATRIX_ROW_END_CHARPOS (row)
11456 && !row->ends_at_zv_p
11457 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
11458 scroll_p = 1;
11459 }
11460 else if (PT < XFASTINT (w->last_point))
11461 {
11462 /* Cursor has to be moved backward. Note that PT >=
11463 CHARPOS (startp) because of the outer if-statement. */
11464 while (!row->mode_line_p
11465 && (MATRIX_ROW_START_CHARPOS (row) > PT
11466 || (MATRIX_ROW_START_CHARPOS (row) == PT
11467 && MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)))
11468 && (row->y > top_scroll_margin
11469 || CHARPOS (startp) == BEGV))
11470 {
11471 xassert (row->enabled_p);
11472 --row;
11473 }
11474
11475 /* Consider the following case: Window starts at BEGV,
11476 there is invisible, intangible text at BEGV, so that
11477 display starts at some point START > BEGV. It can
11478 happen that we are called with PT somewhere between
11479 BEGV and START. Try to handle that case. */
11480 if (row < w->current_matrix->rows
11481 || row->mode_line_p)
11482 {
11483 row = w->current_matrix->rows;
11484 if (row->mode_line_p)
11485 ++row;
11486 }
11487
11488 /* Due to newlines in overlay strings, we may have to
11489 skip forward over overlay strings. */
11490 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
11491 && MATRIX_ROW_END_CHARPOS (row) == PT
11492 && !cursor_row_p (w, row))
11493 ++row;
11494
11495 /* If within the scroll margin, scroll. */
11496 if (row->y < top_scroll_margin
11497 && CHARPOS (startp) != BEGV)
11498 scroll_p = 1;
11499 }
11500
11501 if (PT < MATRIX_ROW_START_CHARPOS (row)
11502 || PT > MATRIX_ROW_END_CHARPOS (row))
11503 {
11504 /* if PT is not in the glyph row, give up. */
11505 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11506 }
11507 else if (MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
11508 && make_cursor_line_fully_visible_p)
11509 {
11510 if (PT == MATRIX_ROW_END_CHARPOS (row)
11511 && !row->ends_at_zv_p
11512 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
11513 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11514 else if (row->height > window_box_height (w))
11515 {
11516 /* If we end up in a partially visible line, let's
11517 make it fully visible, except when it's taller
11518 than the window, in which case we can't do much
11519 about it. */
11520 *scroll_step = 1;
11521 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11522 }
11523 else
11524 {
11525 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11526 if (!make_cursor_line_fully_visible (w, 0))
11527 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11528 else
11529 rc = CURSOR_MOVEMENT_SUCCESS;
11530 }
11531 }
11532 else if (scroll_p)
11533 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11534 else
11535 {
11536 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11537 rc = CURSOR_MOVEMENT_SUCCESS;
11538 }
11539 }
11540 }
11541
11542 return rc;
11543 }
11544
11545 void
11546 set_vertical_scroll_bar (w)
11547 struct window *w;
11548 {
11549 int start, end, whole;
11550
11551 /* Calculate the start and end positions for the current window.
11552 At some point, it would be nice to choose between scrollbars
11553 which reflect the whole buffer size, with special markers
11554 indicating narrowing, and scrollbars which reflect only the
11555 visible region.
11556
11557 Note that mini-buffers sometimes aren't displaying any text. */
11558 if (!MINI_WINDOW_P (w)
11559 || (w == XWINDOW (minibuf_window)
11560 && NILP (echo_area_buffer[0])))
11561 {
11562 struct buffer *buf = XBUFFER (w->buffer);
11563 whole = BUF_ZV (buf) - BUF_BEGV (buf);
11564 start = marker_position (w->start) - BUF_BEGV (buf);
11565 /* I don't think this is guaranteed to be right. For the
11566 moment, we'll pretend it is. */
11567 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
11568
11569 if (end < start)
11570 end = start;
11571 if (whole < (end - start))
11572 whole = end - start;
11573 }
11574 else
11575 start = end = whole = 0;
11576
11577 /* Indicate what this scroll bar ought to be displaying now. */
11578 if (FRAME_DISPLAY (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
11579 (*FRAME_DISPLAY (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
11580 (w, end - start, whole, start);
11581 }
11582
11583
11584 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
11585 selected_window is redisplayed.
11586
11587 We can return without actually redisplaying the window if
11588 fonts_changed_p is nonzero. In that case, redisplay_internal will
11589 retry. */
11590
11591 static void
11592 redisplay_window (window, just_this_one_p)
11593 Lisp_Object window;
11594 int just_this_one_p;
11595 {
11596 struct window *w = XWINDOW (window);
11597 struct frame *f = XFRAME (w->frame);
11598 struct buffer *buffer = XBUFFER (w->buffer);
11599 struct buffer *old = current_buffer;
11600 struct text_pos lpoint, opoint, startp;
11601 int update_mode_line;
11602 int tem;
11603 struct it it;
11604 /* Record it now because it's overwritten. */
11605 int current_matrix_up_to_date_p = 0;
11606 int used_current_matrix_p = 0;
11607 /* This is less strict than current_matrix_up_to_date_p.
11608 It indictes that the buffer contents and narrowing are unchanged. */
11609 int buffer_unchanged_p = 0;
11610 int temp_scroll_step = 0;
11611 int count = SPECPDL_INDEX ();
11612 int rc;
11613 int centering_position;
11614 int last_line_misfit = 0;
11615
11616 SET_TEXT_POS (lpoint, PT, PT_BYTE);
11617 opoint = lpoint;
11618
11619 /* W must be a leaf window here. */
11620 xassert (!NILP (w->buffer));
11621 #if GLYPH_DEBUG
11622 *w->desired_matrix->method = 0;
11623 #endif
11624
11625 /* Force this to be looked up again for each redisp of each window. */
11626 escape_glyph_face = -1;
11627
11628 specbind (Qinhibit_point_motion_hooks, Qt);
11629
11630 reconsider_clip_changes (w, buffer);
11631
11632 /* Has the mode line to be updated? */
11633 update_mode_line = (!NILP (w->update_mode_line)
11634 || update_mode_lines
11635 || buffer->clip_changed
11636 || buffer->prevent_redisplay_optimizations_p);
11637
11638 if (MINI_WINDOW_P (w))
11639 {
11640 if (w == XWINDOW (echo_area_window)
11641 && !NILP (echo_area_buffer[0]))
11642 {
11643 if (update_mode_line)
11644 /* We may have to update a tty frame's menu bar or a
11645 tool-bar. Example `M-x C-h C-h C-g'. */
11646 goto finish_menu_bars;
11647 else
11648 /* We've already displayed the echo area glyphs in this window. */
11649 goto finish_scroll_bars;
11650 }
11651 else if ((w != XWINDOW (minibuf_window)
11652 || minibuf_level == 0)
11653 /* When buffer is nonempty, redisplay window normally. */
11654 && BUF_Z (XBUFFER (w->buffer)) == BUF_BEG (XBUFFER (w->buffer))
11655 /* Quail displays non-mini buffers in minibuffer window.
11656 In that case, redisplay the window normally. */
11657 && !NILP (Fmemq (w->buffer, Vminibuffer_list)))
11658 {
11659 /* W is a mini-buffer window, but it's not active, so clear
11660 it. */
11661 int yb = window_text_bottom_y (w);
11662 struct glyph_row *row;
11663 int y;
11664
11665 for (y = 0, row = w->desired_matrix->rows;
11666 y < yb;
11667 y += row->height, ++row)
11668 blank_row (w, row, y);
11669 goto finish_scroll_bars;
11670 }
11671
11672 clear_glyph_matrix (w->desired_matrix);
11673 }
11674
11675 /* Otherwise set up data on this window; select its buffer and point
11676 value. */
11677 /* Really select the buffer, for the sake of buffer-local
11678 variables. */
11679 set_buffer_internal_1 (XBUFFER (w->buffer));
11680 SET_TEXT_POS (opoint, PT, PT_BYTE);
11681
11682 current_matrix_up_to_date_p
11683 = (!NILP (w->window_end_valid)
11684 && !current_buffer->clip_changed
11685 && !current_buffer->prevent_redisplay_optimizations_p
11686 && XFASTINT (w->last_modified) >= MODIFF
11687 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
11688
11689 buffer_unchanged_p
11690 = (!NILP (w->window_end_valid)
11691 && !current_buffer->clip_changed
11692 && XFASTINT (w->last_modified) >= MODIFF
11693 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
11694
11695 /* When windows_or_buffers_changed is non-zero, we can't rely on
11696 the window end being valid, so set it to nil there. */
11697 if (windows_or_buffers_changed)
11698 {
11699 /* If window starts on a continuation line, maybe adjust the
11700 window start in case the window's width changed. */
11701 if (XMARKER (w->start)->buffer == current_buffer)
11702 compute_window_start_on_continuation_line (w);
11703
11704 w->window_end_valid = Qnil;
11705 }
11706
11707 /* Some sanity checks. */
11708 CHECK_WINDOW_END (w);
11709 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
11710 abort ();
11711 if (BYTEPOS (opoint) < CHARPOS (opoint))
11712 abort ();
11713
11714 /* If %c is in mode line, update it if needed. */
11715 if (!NILP (w->column_number_displayed)
11716 /* This alternative quickly identifies a common case
11717 where no change is needed. */
11718 && !(PT == XFASTINT (w->last_point)
11719 && XFASTINT (w->last_modified) >= MODIFF
11720 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
11721 && (XFASTINT (w->column_number_displayed)
11722 != (int) current_column ())) /* iftc */
11723 update_mode_line = 1;
11724
11725 /* Count number of windows showing the selected buffer. An indirect
11726 buffer counts as its base buffer. */
11727 if (!just_this_one_p)
11728 {
11729 struct buffer *current_base, *window_base;
11730 current_base = current_buffer;
11731 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
11732 if (current_base->base_buffer)
11733 current_base = current_base->base_buffer;
11734 if (window_base->base_buffer)
11735 window_base = window_base->base_buffer;
11736 if (current_base == window_base)
11737 buffer_shared++;
11738 }
11739
11740 /* Point refers normally to the selected window. For any other
11741 window, set up appropriate value. */
11742 if (!EQ (window, selected_window))
11743 {
11744 int new_pt = XMARKER (w->pointm)->charpos;
11745 int new_pt_byte = marker_byte_position (w->pointm);
11746 if (new_pt < BEGV)
11747 {
11748 new_pt = BEGV;
11749 new_pt_byte = BEGV_BYTE;
11750 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
11751 }
11752 else if (new_pt > (ZV - 1))
11753 {
11754 new_pt = ZV;
11755 new_pt_byte = ZV_BYTE;
11756 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
11757 }
11758
11759 /* We don't use SET_PT so that the point-motion hooks don't run. */
11760 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
11761 }
11762
11763 /* If any of the character widths specified in the display table
11764 have changed, invalidate the width run cache. It's true that
11765 this may be a bit late to catch such changes, but the rest of
11766 redisplay goes (non-fatally) haywire when the display table is
11767 changed, so why should we worry about doing any better? */
11768 if (current_buffer->width_run_cache)
11769 {
11770 struct Lisp_Char_Table *disptab = buffer_display_table ();
11771
11772 if (! disptab_matches_widthtab (disptab,
11773 XVECTOR (current_buffer->width_table)))
11774 {
11775 invalidate_region_cache (current_buffer,
11776 current_buffer->width_run_cache,
11777 BEG, Z);
11778 recompute_width_table (current_buffer, disptab);
11779 }
11780 }
11781
11782 /* If window-start is screwed up, choose a new one. */
11783 if (XMARKER (w->start)->buffer != current_buffer)
11784 goto recenter;
11785
11786 SET_TEXT_POS_FROM_MARKER (startp, w->start);
11787
11788 /* If someone specified a new starting point but did not insist,
11789 check whether it can be used. */
11790 if (!NILP (w->optional_new_start)
11791 && CHARPOS (startp) >= BEGV
11792 && CHARPOS (startp) <= ZV)
11793 {
11794 w->optional_new_start = Qnil;
11795 start_display (&it, w, startp);
11796 move_it_to (&it, PT, 0, it.last_visible_y, -1,
11797 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
11798 if (IT_CHARPOS (it) == PT)
11799 w->force_start = Qt;
11800 /* IT may overshoot PT if text at PT is invisible. */
11801 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
11802 w->force_start = Qt;
11803
11804
11805 }
11806
11807 /* Handle case where place to start displaying has been specified,
11808 unless the specified location is outside the accessible range. */
11809 if (!NILP (w->force_start)
11810 || w->frozen_window_start_p)
11811 {
11812 /* We set this later on if we have to adjust point. */
11813 int new_vpos = -1;
11814
11815 w->force_start = Qnil;
11816 w->vscroll = 0;
11817 w->window_end_valid = Qnil;
11818
11819 /* Forget any recorded base line for line number display. */
11820 if (!buffer_unchanged_p)
11821 w->base_line_number = Qnil;
11822
11823 /* Redisplay the mode line. Select the buffer properly for that.
11824 Also, run the hook window-scroll-functions
11825 because we have scrolled. */
11826 /* Note, we do this after clearing force_start because
11827 if there's an error, it is better to forget about force_start
11828 than to get into an infinite loop calling the hook functions
11829 and having them get more errors. */
11830 if (!update_mode_line
11831 || ! NILP (Vwindow_scroll_functions))
11832 {
11833 update_mode_line = 1;
11834 w->update_mode_line = Qt;
11835 startp = run_window_scroll_functions (window, startp);
11836 }
11837
11838 w->last_modified = make_number (0);
11839 w->last_overlay_modified = make_number (0);
11840 if (CHARPOS (startp) < BEGV)
11841 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
11842 else if (CHARPOS (startp) > ZV)
11843 SET_TEXT_POS (startp, ZV, ZV_BYTE);
11844
11845 /* Redisplay, then check if cursor has been set during the
11846 redisplay. Give up if new fonts were loaded. */
11847 if (!try_window (window, startp))
11848 {
11849 w->force_start = Qt;
11850 clear_glyph_matrix (w->desired_matrix);
11851 goto need_larger_matrices;
11852 }
11853
11854 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
11855 {
11856 /* If point does not appear, try to move point so it does
11857 appear. The desired matrix has been built above, so we
11858 can use it here. */
11859 new_vpos = window_box_height (w) / 2;
11860 }
11861
11862 if (!make_cursor_line_fully_visible (w, 0))
11863 {
11864 /* Point does appear, but on a line partly visible at end of window.
11865 Move it back to a fully-visible line. */
11866 new_vpos = window_box_height (w);
11867 }
11868
11869 /* If we need to move point for either of the above reasons,
11870 now actually do it. */
11871 if (new_vpos >= 0)
11872 {
11873 struct glyph_row *row;
11874
11875 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
11876 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
11877 ++row;
11878
11879 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
11880 MATRIX_ROW_START_BYTEPOS (row));
11881
11882 if (w != XWINDOW (selected_window))
11883 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
11884 else if (current_buffer == old)
11885 SET_TEXT_POS (lpoint, PT, PT_BYTE);
11886
11887 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
11888
11889 /* If we are highlighting the region, then we just changed
11890 the region, so redisplay to show it. */
11891 if (!NILP (Vtransient_mark_mode)
11892 && !NILP (current_buffer->mark_active))
11893 {
11894 clear_glyph_matrix (w->desired_matrix);
11895 if (!try_window (window, startp))
11896 goto need_larger_matrices;
11897 }
11898 }
11899
11900 #if GLYPH_DEBUG
11901 debug_method_add (w, "forced window start");
11902 #endif
11903 goto done;
11904 }
11905
11906 /* Handle case where text has not changed, only point, and it has
11907 not moved off the frame, and we are not retrying after hscroll.
11908 (current_matrix_up_to_date_p is nonzero when retrying.) */
11909 if (current_matrix_up_to_date_p
11910 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
11911 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
11912 {
11913 switch (rc)
11914 {
11915 case CURSOR_MOVEMENT_SUCCESS:
11916 used_current_matrix_p = 1;
11917 goto done;
11918
11919 #if 0 /* try_cursor_movement never returns this value. */
11920 case CURSOR_MOVEMENT_NEED_LARGER_MATRICES:
11921 goto need_larger_matrices;
11922 #endif
11923
11924 case CURSOR_MOVEMENT_MUST_SCROLL:
11925 goto try_to_scroll;
11926
11927 default:
11928 abort ();
11929 }
11930 }
11931 /* If current starting point was originally the beginning of a line
11932 but no longer is, find a new starting point. */
11933 else if (!NILP (w->start_at_line_beg)
11934 && !(CHARPOS (startp) <= BEGV
11935 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
11936 {
11937 #if GLYPH_DEBUG
11938 debug_method_add (w, "recenter 1");
11939 #endif
11940 goto recenter;
11941 }
11942
11943 /* Try scrolling with try_window_id. Value is > 0 if update has
11944 been done, it is -1 if we know that the same window start will
11945 not work. It is 0 if unsuccessful for some other reason. */
11946 else if ((tem = try_window_id (w)) != 0)
11947 {
11948 #if GLYPH_DEBUG
11949 debug_method_add (w, "try_window_id %d", tem);
11950 #endif
11951
11952 if (fonts_changed_p)
11953 goto need_larger_matrices;
11954 if (tem > 0)
11955 goto done;
11956
11957 /* Otherwise try_window_id has returned -1 which means that we
11958 don't want the alternative below this comment to execute. */
11959 }
11960 else if (CHARPOS (startp) >= BEGV
11961 && CHARPOS (startp) <= ZV
11962 && PT >= CHARPOS (startp)
11963 && (CHARPOS (startp) < ZV
11964 /* Avoid starting at end of buffer. */
11965 || CHARPOS (startp) == BEGV
11966 || (XFASTINT (w->last_modified) >= MODIFF
11967 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
11968 {
11969 #if GLYPH_DEBUG
11970 debug_method_add (w, "same window start");
11971 #endif
11972
11973 /* Try to redisplay starting at same place as before.
11974 If point has not moved off frame, accept the results. */
11975 if (!current_matrix_up_to_date_p
11976 /* Don't use try_window_reusing_current_matrix in this case
11977 because a window scroll function can have changed the
11978 buffer. */
11979 || !NILP (Vwindow_scroll_functions)
11980 || MINI_WINDOW_P (w)
11981 || !(used_current_matrix_p
11982 = try_window_reusing_current_matrix (w)))
11983 {
11984 IF_DEBUG (debug_method_add (w, "1"));
11985 try_window (window, startp);
11986 }
11987
11988 if (fonts_changed_p)
11989 goto need_larger_matrices;
11990
11991 if (w->cursor.vpos >= 0)
11992 {
11993 if (!just_this_one_p
11994 || current_buffer->clip_changed
11995 || BEG_UNCHANGED < CHARPOS (startp))
11996 /* Forget any recorded base line for line number display. */
11997 w->base_line_number = Qnil;
11998
11999 if (!make_cursor_line_fully_visible (w, 1))
12000 {
12001 clear_glyph_matrix (w->desired_matrix);
12002 last_line_misfit = 1;
12003 }
12004 /* Drop through and scroll. */
12005 else
12006 goto done;
12007 }
12008 else
12009 clear_glyph_matrix (w->desired_matrix);
12010 }
12011
12012 try_to_scroll:
12013
12014 w->last_modified = make_number (0);
12015 w->last_overlay_modified = make_number (0);
12016
12017 /* Redisplay the mode line. Select the buffer properly for that. */
12018 if (!update_mode_line)
12019 {
12020 update_mode_line = 1;
12021 w->update_mode_line = Qt;
12022 }
12023
12024 /* Try to scroll by specified few lines. */
12025 if ((scroll_conservatively
12026 || scroll_step
12027 || temp_scroll_step
12028 || NUMBERP (current_buffer->scroll_up_aggressively)
12029 || NUMBERP (current_buffer->scroll_down_aggressively))
12030 && !current_buffer->clip_changed
12031 && CHARPOS (startp) >= BEGV
12032 && CHARPOS (startp) <= ZV)
12033 {
12034 /* The function returns -1 if new fonts were loaded, 1 if
12035 successful, 0 if not successful. */
12036 int rc = try_scrolling (window, just_this_one_p,
12037 scroll_conservatively,
12038 scroll_step,
12039 temp_scroll_step, last_line_misfit);
12040 switch (rc)
12041 {
12042 case SCROLLING_SUCCESS:
12043 goto done;
12044
12045 case SCROLLING_NEED_LARGER_MATRICES:
12046 goto need_larger_matrices;
12047
12048 case SCROLLING_FAILED:
12049 break;
12050
12051 default:
12052 abort ();
12053 }
12054 }
12055
12056 /* Finally, just choose place to start which centers point */
12057
12058 recenter:
12059 centering_position = window_box_height (w) / 2;
12060
12061 point_at_top:
12062 /* Jump here with centering_position already set to 0. */
12063
12064 #if GLYPH_DEBUG
12065 debug_method_add (w, "recenter");
12066 #endif
12067
12068 /* w->vscroll = 0; */
12069
12070 /* Forget any previously recorded base line for line number display. */
12071 if (!buffer_unchanged_p)
12072 w->base_line_number = Qnil;
12073
12074 /* Move backward half the height of the window. */
12075 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
12076 it.current_y = it.last_visible_y;
12077 move_it_vertically_backward (&it, centering_position);
12078 xassert (IT_CHARPOS (it) >= BEGV);
12079
12080 /* The function move_it_vertically_backward may move over more
12081 than the specified y-distance. If it->w is small, e.g. a
12082 mini-buffer window, we may end up in front of the window's
12083 display area. Start displaying at the start of the line
12084 containing PT in this case. */
12085 if (it.current_y <= 0)
12086 {
12087 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
12088 move_it_vertically_backward (&it, 0);
12089 xassert (IT_CHARPOS (it) <= PT);
12090 it.current_y = 0;
12091 }
12092
12093 it.current_x = it.hpos = 0;
12094
12095 /* Set startp here explicitly in case that helps avoid an infinite loop
12096 in case the window-scroll-functions functions get errors. */
12097 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
12098
12099 /* Run scroll hooks. */
12100 startp = run_window_scroll_functions (window, it.current.pos);
12101
12102 /* Redisplay the window. */
12103 if (!current_matrix_up_to_date_p
12104 || windows_or_buffers_changed
12105 || cursor_type_changed
12106 /* Don't use try_window_reusing_current_matrix in this case
12107 because it can have changed the buffer. */
12108 || !NILP (Vwindow_scroll_functions)
12109 || !just_this_one_p
12110 || MINI_WINDOW_P (w)
12111 || !(used_current_matrix_p
12112 = try_window_reusing_current_matrix (w)))
12113 try_window (window, startp);
12114
12115 /* If new fonts have been loaded (due to fontsets), give up. We
12116 have to start a new redisplay since we need to re-adjust glyph
12117 matrices. */
12118 if (fonts_changed_p)
12119 goto need_larger_matrices;
12120
12121 /* If cursor did not appear assume that the middle of the window is
12122 in the first line of the window. Do it again with the next line.
12123 (Imagine a window of height 100, displaying two lines of height
12124 60. Moving back 50 from it->last_visible_y will end in the first
12125 line.) */
12126 if (w->cursor.vpos < 0)
12127 {
12128 if (!NILP (w->window_end_valid)
12129 && PT >= Z - XFASTINT (w->window_end_pos))
12130 {
12131 clear_glyph_matrix (w->desired_matrix);
12132 move_it_by_lines (&it, 1, 0);
12133 try_window (window, it.current.pos);
12134 }
12135 else if (PT < IT_CHARPOS (it))
12136 {
12137 clear_glyph_matrix (w->desired_matrix);
12138 move_it_by_lines (&it, -1, 0);
12139 try_window (window, it.current.pos);
12140 }
12141 else
12142 {
12143 /* Not much we can do about it. */
12144 }
12145 }
12146
12147 /* Consider the following case: Window starts at BEGV, there is
12148 invisible, intangible text at BEGV, so that display starts at
12149 some point START > BEGV. It can happen that we are called with
12150 PT somewhere between BEGV and START. Try to handle that case. */
12151 if (w->cursor.vpos < 0)
12152 {
12153 struct glyph_row *row = w->current_matrix->rows;
12154 if (row->mode_line_p)
12155 ++row;
12156 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
12157 }
12158
12159 if (!make_cursor_line_fully_visible (w, centering_position > 0))
12160 {
12161 /* If vscroll is enabled, disable it and try again. */
12162 if (w->vscroll)
12163 {
12164 w->vscroll = 0;
12165 clear_glyph_matrix (w->desired_matrix);
12166 goto recenter;
12167 }
12168
12169 /* If centering point failed to make the whole line visible,
12170 put point at the top instead. That has to make the whole line
12171 visible, if it can be done. */
12172 clear_glyph_matrix (w->desired_matrix);
12173 centering_position = 0;
12174 goto point_at_top;
12175 }
12176
12177 done:
12178
12179 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12180 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
12181 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
12182 ? Qt : Qnil);
12183
12184 /* Display the mode line, if we must. */
12185 if ((update_mode_line
12186 /* If window not full width, must redo its mode line
12187 if (a) the window to its side is being redone and
12188 (b) we do a frame-based redisplay. This is a consequence
12189 of how inverted lines are drawn in frame-based redisplay. */
12190 || (!just_this_one_p
12191 && !FRAME_WINDOW_P (f)
12192 && !WINDOW_FULL_WIDTH_P (w))
12193 /* Line number to display. */
12194 || INTEGERP (w->base_line_pos)
12195 /* Column number is displayed and different from the one displayed. */
12196 || (!NILP (w->column_number_displayed)
12197 && (XFASTINT (w->column_number_displayed)
12198 != (int) current_column ()))) /* iftc */
12199 /* This means that the window has a mode line. */
12200 && (WINDOW_WANTS_MODELINE_P (w)
12201 || WINDOW_WANTS_HEADER_LINE_P (w)))
12202 {
12203 display_mode_lines (w);
12204
12205 /* If mode line height has changed, arrange for a thorough
12206 immediate redisplay using the correct mode line height. */
12207 if (WINDOW_WANTS_MODELINE_P (w)
12208 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
12209 {
12210 fonts_changed_p = 1;
12211 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
12212 = DESIRED_MODE_LINE_HEIGHT (w);
12213 }
12214
12215 /* If top line height has changed, arrange for a thorough
12216 immediate redisplay using the correct mode line height. */
12217 if (WINDOW_WANTS_HEADER_LINE_P (w)
12218 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
12219 {
12220 fonts_changed_p = 1;
12221 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
12222 = DESIRED_HEADER_LINE_HEIGHT (w);
12223 }
12224
12225 if (fonts_changed_p)
12226 goto need_larger_matrices;
12227 }
12228
12229 if (!line_number_displayed
12230 && !BUFFERP (w->base_line_pos))
12231 {
12232 w->base_line_pos = Qnil;
12233 w->base_line_number = Qnil;
12234 }
12235
12236 finish_menu_bars:
12237
12238 /* When we reach a frame's selected window, redo the frame's menu bar. */
12239 if (update_mode_line
12240 && EQ (FRAME_SELECTED_WINDOW (f), window))
12241 {
12242 int redisplay_menu_p = 0;
12243 int redisplay_tool_bar_p = 0;
12244
12245 if (FRAME_WINDOW_P (f))
12246 {
12247 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
12248 || defined (USE_GTK)
12249 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
12250 #else
12251 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
12252 #endif
12253 }
12254 else
12255 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
12256
12257 if (redisplay_menu_p)
12258 display_menu_bar (w);
12259
12260 #ifdef HAVE_WINDOW_SYSTEM
12261 if (FRAME_WINDOW_P (f))
12262 {
12263 #ifdef USE_GTK
12264 redisplay_tool_bar_p = FRAME_EXTERNAL_TOOL_BAR (f);
12265 #else
12266 redisplay_tool_bar_p = WINDOWP (f->tool_bar_window)
12267 && (FRAME_TOOL_BAR_LINES (f) > 0
12268 || auto_resize_tool_bars_p);
12269 #endif
12270
12271 if (redisplay_tool_bar_p)
12272 redisplay_tool_bar (f);
12273 }
12274 #endif
12275 }
12276
12277 #ifdef HAVE_WINDOW_SYSTEM
12278 if (FRAME_WINDOW_P (f)
12279 && update_window_fringes (w, 0)
12280 && !just_this_one_p
12281 && (used_current_matrix_p || overlay_arrow_seen)
12282 && !w->pseudo_window_p)
12283 {
12284 update_begin (f);
12285 BLOCK_INPUT;
12286 if (draw_window_fringes (w, 1))
12287 x_draw_vertical_border (w);
12288 UNBLOCK_INPUT;
12289 update_end (f);
12290 }
12291 #endif /* HAVE_WINDOW_SYSTEM */
12292
12293 /* We go to this label, with fonts_changed_p nonzero,
12294 if it is necessary to try again using larger glyph matrices.
12295 We have to redeem the scroll bar even in this case,
12296 because the loop in redisplay_internal expects that. */
12297 need_larger_matrices:
12298 ;
12299 finish_scroll_bars:
12300
12301 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
12302 {
12303 /* Set the thumb's position and size. */
12304 set_vertical_scroll_bar (w);
12305
12306 /* Note that we actually used the scroll bar attached to this
12307 window, so it shouldn't be deleted at the end of redisplay. */
12308 if (FRAME_DISPLAY (f)->redeem_scroll_bar_hook)
12309 (*FRAME_DISPLAY (f)->redeem_scroll_bar_hook) (w);
12310 }
12311
12312 /* Restore current_buffer and value of point in it. */
12313 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
12314 set_buffer_internal_1 (old);
12315 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
12316
12317 unbind_to (count, Qnil);
12318 }
12319
12320
12321 /* Build the complete desired matrix of WINDOW with a window start
12322 buffer position POS. Value is non-zero if successful. It is zero
12323 if fonts were loaded during redisplay which makes re-adjusting
12324 glyph matrices necessary. */
12325
12326 int
12327 try_window (window, pos)
12328 Lisp_Object window;
12329 struct text_pos pos;
12330 {
12331 struct window *w = XWINDOW (window);
12332 struct it it;
12333 struct glyph_row *last_text_row = NULL;
12334
12335 /* Make POS the new window start. */
12336 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
12337
12338 /* Mark cursor position as unknown. No overlay arrow seen. */
12339 w->cursor.vpos = -1;
12340 overlay_arrow_seen = 0;
12341
12342 /* Initialize iterator and info to start at POS. */
12343 start_display (&it, w, pos);
12344
12345 /* Display all lines of W. */
12346 while (it.current_y < it.last_visible_y)
12347 {
12348 if (display_line (&it))
12349 last_text_row = it.glyph_row - 1;
12350 if (fonts_changed_p)
12351 return 0;
12352 }
12353
12354 /* If bottom moved off end of frame, change mode line percentage. */
12355 if (XFASTINT (w->window_end_pos) <= 0
12356 && Z != IT_CHARPOS (it))
12357 w->update_mode_line = Qt;
12358
12359 /* Set window_end_pos to the offset of the last character displayed
12360 on the window from the end of current_buffer. Set
12361 window_end_vpos to its row number. */
12362 if (last_text_row)
12363 {
12364 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
12365 w->window_end_bytepos
12366 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
12367 w->window_end_pos
12368 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
12369 w->window_end_vpos
12370 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
12371 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
12372 ->displays_text_p);
12373 }
12374 else
12375 {
12376 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
12377 w->window_end_pos = make_number (Z - ZV);
12378 w->window_end_vpos = make_number (0);
12379 }
12380
12381 /* But that is not valid info until redisplay finishes. */
12382 w->window_end_valid = Qnil;
12383 return 1;
12384 }
12385
12386
12387 \f
12388 /************************************************************************
12389 Window redisplay reusing current matrix when buffer has not changed
12390 ************************************************************************/
12391
12392 /* Try redisplay of window W showing an unchanged buffer with a
12393 different window start than the last time it was displayed by
12394 reusing its current matrix. Value is non-zero if successful.
12395 W->start is the new window start. */
12396
12397 static int
12398 try_window_reusing_current_matrix (w)
12399 struct window *w;
12400 {
12401 struct frame *f = XFRAME (w->frame);
12402 struct glyph_row *row, *bottom_row;
12403 struct it it;
12404 struct run run;
12405 struct text_pos start, new_start;
12406 int nrows_scrolled, i;
12407 struct glyph_row *last_text_row;
12408 struct glyph_row *last_reused_text_row;
12409 struct glyph_row *start_row;
12410 int start_vpos, min_y, max_y;
12411
12412 #if GLYPH_DEBUG
12413 if (inhibit_try_window_reusing)
12414 return 0;
12415 #endif
12416
12417 if (/* This function doesn't handle terminal frames. */
12418 !FRAME_WINDOW_P (f)
12419 /* Don't try to reuse the display if windows have been split
12420 or such. */
12421 || windows_or_buffers_changed
12422 || cursor_type_changed)
12423 return 0;
12424
12425 /* Can't do this if region may have changed. */
12426 if ((!NILP (Vtransient_mark_mode)
12427 && !NILP (current_buffer->mark_active))
12428 || !NILP (w->region_showing)
12429 || !NILP (Vshow_trailing_whitespace))
12430 return 0;
12431
12432 /* If top-line visibility has changed, give up. */
12433 if (WINDOW_WANTS_HEADER_LINE_P (w)
12434 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
12435 return 0;
12436
12437 /* Give up if old or new display is scrolled vertically. We could
12438 make this function handle this, but right now it doesn't. */
12439 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
12440 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
12441 return 0;
12442
12443 /* The variable new_start now holds the new window start. The old
12444 start `start' can be determined from the current matrix. */
12445 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
12446 start = start_row->start.pos;
12447 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
12448
12449 /* Clear the desired matrix for the display below. */
12450 clear_glyph_matrix (w->desired_matrix);
12451
12452 if (CHARPOS (new_start) <= CHARPOS (start))
12453 {
12454 int first_row_y;
12455
12456 /* Don't use this method if the display starts with an ellipsis
12457 displayed for invisible text. It's not easy to handle that case
12458 below, and it's certainly not worth the effort since this is
12459 not a frequent case. */
12460 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
12461 return 0;
12462
12463 IF_DEBUG (debug_method_add (w, "twu1"));
12464
12465 /* Display up to a row that can be reused. The variable
12466 last_text_row is set to the last row displayed that displays
12467 text. Note that it.vpos == 0 if or if not there is a
12468 header-line; it's not the same as the MATRIX_ROW_VPOS! */
12469 start_display (&it, w, new_start);
12470 first_row_y = it.current_y;
12471 w->cursor.vpos = -1;
12472 last_text_row = last_reused_text_row = NULL;
12473
12474 while (it.current_y < it.last_visible_y
12475 && !fonts_changed_p)
12476 {
12477 /* If we have reached into the characters in the START row,
12478 that means the line boundaries have changed. So we
12479 can't start copying with the row START. Maybe it will
12480 work to start copying with the following row. */
12481 while (IT_CHARPOS (it) > CHARPOS (start))
12482 {
12483 /* Advance to the next row as the "start". */
12484 start_row++;
12485 start = start_row->start.pos;
12486 /* If there are no more rows to try, or just one, give up. */
12487 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
12488 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
12489 || CHARPOS (start) == ZV)
12490 {
12491 clear_glyph_matrix (w->desired_matrix);
12492 return 0;
12493 }
12494
12495 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
12496 }
12497 /* If we have reached alignment,
12498 we can copy the rest of the rows. */
12499 if (IT_CHARPOS (it) == CHARPOS (start))
12500 break;
12501
12502 if (display_line (&it))
12503 last_text_row = it.glyph_row - 1;
12504 }
12505
12506 /* A value of current_y < last_visible_y means that we stopped
12507 at the previous window start, which in turn means that we
12508 have at least one reusable row. */
12509 if (it.current_y < it.last_visible_y)
12510 {
12511 /* IT.vpos always starts from 0; it counts text lines. */
12512 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
12513
12514 /* Find PT if not already found in the lines displayed. */
12515 if (w->cursor.vpos < 0)
12516 {
12517 int dy = it.current_y - start_row->y;
12518
12519 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
12520 row = row_containing_pos (w, PT, row, NULL, dy);
12521 if (row)
12522 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
12523 dy, nrows_scrolled);
12524 else
12525 {
12526 clear_glyph_matrix (w->desired_matrix);
12527 return 0;
12528 }
12529 }
12530
12531 /* Scroll the display. Do it before the current matrix is
12532 changed. The problem here is that update has not yet
12533 run, i.e. part of the current matrix is not up to date.
12534 scroll_run_hook will clear the cursor, and use the
12535 current matrix to get the height of the row the cursor is
12536 in. */
12537 run.current_y = start_row->y;
12538 run.desired_y = it.current_y;
12539 run.height = it.last_visible_y - it.current_y;
12540
12541 if (run.height > 0 && run.current_y != run.desired_y)
12542 {
12543 update_begin (f);
12544 FRAME_RIF (f)->update_window_begin_hook (w);
12545 FRAME_RIF (f)->clear_window_mouse_face (w);
12546 FRAME_RIF (f)->scroll_run_hook (w, &run);
12547 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
12548 update_end (f);
12549 }
12550
12551 /* Shift current matrix down by nrows_scrolled lines. */
12552 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
12553 rotate_matrix (w->current_matrix,
12554 start_vpos,
12555 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
12556 nrows_scrolled);
12557
12558 /* Disable lines that must be updated. */
12559 for (i = 0; i < it.vpos; ++i)
12560 (start_row + i)->enabled_p = 0;
12561
12562 /* Re-compute Y positions. */
12563 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
12564 max_y = it.last_visible_y;
12565 for (row = start_row + nrows_scrolled;
12566 row < bottom_row;
12567 ++row)
12568 {
12569 row->y = it.current_y;
12570 row->visible_height = row->height;
12571
12572 if (row->y < min_y)
12573 row->visible_height -= min_y - row->y;
12574 if (row->y + row->height > max_y)
12575 row->visible_height -= row->y + row->height - max_y;
12576 row->redraw_fringe_bitmaps_p = 1;
12577
12578 it.current_y += row->height;
12579
12580 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
12581 last_reused_text_row = row;
12582 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
12583 break;
12584 }
12585
12586 /* Disable lines in the current matrix which are now
12587 below the window. */
12588 for (++row; row < bottom_row; ++row)
12589 row->enabled_p = 0;
12590 }
12591
12592 /* Update window_end_pos etc.; last_reused_text_row is the last
12593 reused row from the current matrix containing text, if any.
12594 The value of last_text_row is the last displayed line
12595 containing text. */
12596 if (last_reused_text_row)
12597 {
12598 w->window_end_bytepos
12599 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
12600 w->window_end_pos
12601 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
12602 w->window_end_vpos
12603 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
12604 w->current_matrix));
12605 }
12606 else if (last_text_row)
12607 {
12608 w->window_end_bytepos
12609 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
12610 w->window_end_pos
12611 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
12612 w->window_end_vpos
12613 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
12614 }
12615 else
12616 {
12617 /* This window must be completely empty. */
12618 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
12619 w->window_end_pos = make_number (Z - ZV);
12620 w->window_end_vpos = make_number (0);
12621 }
12622 w->window_end_valid = Qnil;
12623
12624 /* Update hint: don't try scrolling again in update_window. */
12625 w->desired_matrix->no_scrolling_p = 1;
12626
12627 #if GLYPH_DEBUG
12628 debug_method_add (w, "try_window_reusing_current_matrix 1");
12629 #endif
12630 return 1;
12631 }
12632 else if (CHARPOS (new_start) > CHARPOS (start))
12633 {
12634 struct glyph_row *pt_row, *row;
12635 struct glyph_row *first_reusable_row;
12636 struct glyph_row *first_row_to_display;
12637 int dy;
12638 int yb = window_text_bottom_y (w);
12639
12640 /* Find the row starting at new_start, if there is one. Don't
12641 reuse a partially visible line at the end. */
12642 first_reusable_row = start_row;
12643 while (first_reusable_row->enabled_p
12644 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
12645 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
12646 < CHARPOS (new_start)))
12647 ++first_reusable_row;
12648
12649 /* Give up if there is no row to reuse. */
12650 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
12651 || !first_reusable_row->enabled_p
12652 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
12653 != CHARPOS (new_start)))
12654 return 0;
12655
12656 /* We can reuse fully visible rows beginning with
12657 first_reusable_row to the end of the window. Set
12658 first_row_to_display to the first row that cannot be reused.
12659 Set pt_row to the row containing point, if there is any. */
12660 pt_row = NULL;
12661 for (first_row_to_display = first_reusable_row;
12662 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
12663 ++first_row_to_display)
12664 {
12665 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
12666 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
12667 pt_row = first_row_to_display;
12668 }
12669
12670 /* Start displaying at the start of first_row_to_display. */
12671 xassert (first_row_to_display->y < yb);
12672 init_to_row_start (&it, w, first_row_to_display);
12673
12674 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
12675 - start_vpos);
12676 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
12677 - nrows_scrolled);
12678 it.current_y = (first_row_to_display->y - first_reusable_row->y
12679 + WINDOW_HEADER_LINE_HEIGHT (w));
12680
12681 /* Display lines beginning with first_row_to_display in the
12682 desired matrix. Set last_text_row to the last row displayed
12683 that displays text. */
12684 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
12685 if (pt_row == NULL)
12686 w->cursor.vpos = -1;
12687 last_text_row = NULL;
12688 while (it.current_y < it.last_visible_y && !fonts_changed_p)
12689 if (display_line (&it))
12690 last_text_row = it.glyph_row - 1;
12691
12692 /* Give up If point isn't in a row displayed or reused. */
12693 if (w->cursor.vpos < 0)
12694 {
12695 clear_glyph_matrix (w->desired_matrix);
12696 return 0;
12697 }
12698
12699 /* If point is in a reused row, adjust y and vpos of the cursor
12700 position. */
12701 if (pt_row)
12702 {
12703 w->cursor.vpos -= nrows_scrolled;
12704 w->cursor.y -= first_reusable_row->y - start_row->y;
12705 }
12706
12707 /* Scroll the display. */
12708 run.current_y = first_reusable_row->y;
12709 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
12710 run.height = it.last_visible_y - run.current_y;
12711 dy = run.current_y - run.desired_y;
12712
12713 if (run.height)
12714 {
12715 update_begin (f);
12716 FRAME_RIF (f)->update_window_begin_hook (w);
12717 FRAME_RIF (f)->clear_window_mouse_face (w);
12718 FRAME_RIF (f)->scroll_run_hook (w, &run);
12719 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
12720 update_end (f);
12721 }
12722
12723 /* Adjust Y positions of reused rows. */
12724 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
12725 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
12726 max_y = it.last_visible_y;
12727 for (row = first_reusable_row; row < first_row_to_display; ++row)
12728 {
12729 row->y -= dy;
12730 row->visible_height = row->height;
12731 if (row->y < min_y)
12732 row->visible_height -= min_y - row->y;
12733 if (row->y + row->height > max_y)
12734 row->visible_height -= row->y + row->height - max_y;
12735 row->redraw_fringe_bitmaps_p = 1;
12736 }
12737
12738 /* Scroll the current matrix. */
12739 xassert (nrows_scrolled > 0);
12740 rotate_matrix (w->current_matrix,
12741 start_vpos,
12742 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
12743 -nrows_scrolled);
12744
12745 /* Disable rows not reused. */
12746 for (row -= nrows_scrolled; row < bottom_row; ++row)
12747 row->enabled_p = 0;
12748
12749 /* Point may have moved to a different line, so we cannot assume that
12750 the previous cursor position is valid; locate the correct row. */
12751 if (pt_row)
12752 {
12753 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
12754 row < bottom_row && PT >= MATRIX_ROW_END_CHARPOS (row);
12755 row++)
12756 {
12757 w->cursor.vpos++;
12758 w->cursor.y = row->y;
12759 }
12760 if (row < bottom_row)
12761 {
12762 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
12763 while (glyph->charpos < PT)
12764 {
12765 w->cursor.hpos++;
12766 w->cursor.x += glyph->pixel_width;
12767 glyph++;
12768 }
12769 }
12770 }
12771
12772 /* Adjust window end. A null value of last_text_row means that
12773 the window end is in reused rows which in turn means that
12774 only its vpos can have changed. */
12775 if (last_text_row)
12776 {
12777 w->window_end_bytepos
12778 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
12779 w->window_end_pos
12780 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
12781 w->window_end_vpos
12782 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
12783 }
12784 else
12785 {
12786 w->window_end_vpos
12787 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
12788 }
12789
12790 w->window_end_valid = Qnil;
12791 w->desired_matrix->no_scrolling_p = 1;
12792
12793 #if GLYPH_DEBUG
12794 debug_method_add (w, "try_window_reusing_current_matrix 2");
12795 #endif
12796 return 1;
12797 }
12798
12799 return 0;
12800 }
12801
12802
12803 \f
12804 /************************************************************************
12805 Window redisplay reusing current matrix when buffer has changed
12806 ************************************************************************/
12807
12808 static struct glyph_row *find_last_unchanged_at_beg_row P_ ((struct window *));
12809 static struct glyph_row *find_first_unchanged_at_end_row P_ ((struct window *,
12810 int *, int *));
12811 static struct glyph_row *
12812 find_last_row_displaying_text P_ ((struct glyph_matrix *, struct it *,
12813 struct glyph_row *));
12814
12815
12816 /* Return the last row in MATRIX displaying text. If row START is
12817 non-null, start searching with that row. IT gives the dimensions
12818 of the display. Value is null if matrix is empty; otherwise it is
12819 a pointer to the row found. */
12820
12821 static struct glyph_row *
12822 find_last_row_displaying_text (matrix, it, start)
12823 struct glyph_matrix *matrix;
12824 struct it *it;
12825 struct glyph_row *start;
12826 {
12827 struct glyph_row *row, *row_found;
12828
12829 /* Set row_found to the last row in IT->w's current matrix
12830 displaying text. The loop looks funny but think of partially
12831 visible lines. */
12832 row_found = NULL;
12833 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
12834 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
12835 {
12836 xassert (row->enabled_p);
12837 row_found = row;
12838 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
12839 break;
12840 ++row;
12841 }
12842
12843 return row_found;
12844 }
12845
12846
12847 /* Return the last row in the current matrix of W that is not affected
12848 by changes at the start of current_buffer that occurred since W's
12849 current matrix was built. Value is null if no such row exists.
12850
12851 BEG_UNCHANGED us the number of characters unchanged at the start of
12852 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
12853 first changed character in current_buffer. Characters at positions <
12854 BEG + BEG_UNCHANGED are at the same buffer positions as they were
12855 when the current matrix was built. */
12856
12857 static struct glyph_row *
12858 find_last_unchanged_at_beg_row (w)
12859 struct window *w;
12860 {
12861 int first_changed_pos = BEG + BEG_UNCHANGED;
12862 struct glyph_row *row;
12863 struct glyph_row *row_found = NULL;
12864 int yb = window_text_bottom_y (w);
12865
12866 /* Find the last row displaying unchanged text. */
12867 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
12868 while (MATRIX_ROW_DISPLAYS_TEXT_P (row)
12869 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos)
12870 {
12871 if (/* If row ends before first_changed_pos, it is unchanged,
12872 except in some case. */
12873 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
12874 /* When row ends in ZV and we write at ZV it is not
12875 unchanged. */
12876 && !row->ends_at_zv_p
12877 /* When first_changed_pos is the end of a continued line,
12878 row is not unchanged because it may be no longer
12879 continued. */
12880 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
12881 && (row->continued_p
12882 || row->exact_window_width_line_p)))
12883 row_found = row;
12884
12885 /* Stop if last visible row. */
12886 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
12887 break;
12888
12889 ++row;
12890 }
12891
12892 return row_found;
12893 }
12894
12895
12896 /* Find the first glyph row in the current matrix of W that is not
12897 affected by changes at the end of current_buffer since the
12898 time W's current matrix was built.
12899
12900 Return in *DELTA the number of chars by which buffer positions in
12901 unchanged text at the end of current_buffer must be adjusted.
12902
12903 Return in *DELTA_BYTES the corresponding number of bytes.
12904
12905 Value is null if no such row exists, i.e. all rows are affected by
12906 changes. */
12907
12908 static struct glyph_row *
12909 find_first_unchanged_at_end_row (w, delta, delta_bytes)
12910 struct window *w;
12911 int *delta, *delta_bytes;
12912 {
12913 struct glyph_row *row;
12914 struct glyph_row *row_found = NULL;
12915
12916 *delta = *delta_bytes = 0;
12917
12918 /* Display must not have been paused, otherwise the current matrix
12919 is not up to date. */
12920 if (NILP (w->window_end_valid))
12921 abort ();
12922
12923 /* A value of window_end_pos >= END_UNCHANGED means that the window
12924 end is in the range of changed text. If so, there is no
12925 unchanged row at the end of W's current matrix. */
12926 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
12927 return NULL;
12928
12929 /* Set row to the last row in W's current matrix displaying text. */
12930 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
12931
12932 /* If matrix is entirely empty, no unchanged row exists. */
12933 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
12934 {
12935 /* The value of row is the last glyph row in the matrix having a
12936 meaningful buffer position in it. The end position of row
12937 corresponds to window_end_pos. This allows us to translate
12938 buffer positions in the current matrix to current buffer
12939 positions for characters not in changed text. */
12940 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
12941 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
12942 int last_unchanged_pos, last_unchanged_pos_old;
12943 struct glyph_row *first_text_row
12944 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
12945
12946 *delta = Z - Z_old;
12947 *delta_bytes = Z_BYTE - Z_BYTE_old;
12948
12949 /* Set last_unchanged_pos to the buffer position of the last
12950 character in the buffer that has not been changed. Z is the
12951 index + 1 of the last character in current_buffer, i.e. by
12952 subtracting END_UNCHANGED we get the index of the last
12953 unchanged character, and we have to add BEG to get its buffer
12954 position. */
12955 last_unchanged_pos = Z - END_UNCHANGED + BEG;
12956 last_unchanged_pos_old = last_unchanged_pos - *delta;
12957
12958 /* Search backward from ROW for a row displaying a line that
12959 starts at a minimum position >= last_unchanged_pos_old. */
12960 for (; row > first_text_row; --row)
12961 {
12962 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
12963 abort ();
12964
12965 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
12966 row_found = row;
12967 }
12968 }
12969
12970 if (row_found && !MATRIX_ROW_DISPLAYS_TEXT_P (row_found))
12971 abort ();
12972
12973 return row_found;
12974 }
12975
12976
12977 /* Make sure that glyph rows in the current matrix of window W
12978 reference the same glyph memory as corresponding rows in the
12979 frame's frame matrix. This function is called after scrolling W's
12980 current matrix on a terminal frame in try_window_id and
12981 try_window_reusing_current_matrix. */
12982
12983 static void
12984 sync_frame_with_window_matrix_rows (w)
12985 struct window *w;
12986 {
12987 struct frame *f = XFRAME (w->frame);
12988 struct glyph_row *window_row, *window_row_end, *frame_row;
12989
12990 /* Preconditions: W must be a leaf window and full-width. Its frame
12991 must have a frame matrix. */
12992 xassert (NILP (w->hchild) && NILP (w->vchild));
12993 xassert (WINDOW_FULL_WIDTH_P (w));
12994 xassert (!FRAME_WINDOW_P (f));
12995
12996 /* If W is a full-width window, glyph pointers in W's current matrix
12997 have, by definition, to be the same as glyph pointers in the
12998 corresponding frame matrix. Note that frame matrices have no
12999 marginal areas (see build_frame_matrix). */
13000 window_row = w->current_matrix->rows;
13001 window_row_end = window_row + w->current_matrix->nrows;
13002 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
13003 while (window_row < window_row_end)
13004 {
13005 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
13006 struct glyph *end = window_row->glyphs[LAST_AREA];
13007
13008 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
13009 frame_row->glyphs[TEXT_AREA] = start;
13010 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
13011 frame_row->glyphs[LAST_AREA] = end;
13012
13013 /* Disable frame rows whose corresponding window rows have
13014 been disabled in try_window_id. */
13015 if (!window_row->enabled_p)
13016 frame_row->enabled_p = 0;
13017
13018 ++window_row, ++frame_row;
13019 }
13020 }
13021
13022
13023 /* Find the glyph row in window W containing CHARPOS. Consider all
13024 rows between START and END (not inclusive). END null means search
13025 all rows to the end of the display area of W. Value is the row
13026 containing CHARPOS or null. */
13027
13028 struct glyph_row *
13029 row_containing_pos (w, charpos, start, end, dy)
13030 struct window *w;
13031 int charpos;
13032 struct glyph_row *start, *end;
13033 int dy;
13034 {
13035 struct glyph_row *row = start;
13036 int last_y;
13037
13038 /* If we happen to start on a header-line, skip that. */
13039 if (row->mode_line_p)
13040 ++row;
13041
13042 if ((end && row >= end) || !row->enabled_p)
13043 return NULL;
13044
13045 last_y = window_text_bottom_y (w) - dy;
13046
13047 while (1)
13048 {
13049 /* Give up if we have gone too far. */
13050 if (end && row >= end)
13051 return NULL;
13052 /* This formerly returned if they were equal.
13053 I think that both quantities are of a "last plus one" type;
13054 if so, when they are equal, the row is within the screen. -- rms. */
13055 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
13056 return NULL;
13057
13058 /* If it is in this row, return this row. */
13059 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
13060 || (MATRIX_ROW_END_CHARPOS (row) == charpos
13061 /* The end position of a row equals the start
13062 position of the next row. If CHARPOS is there, we
13063 would rather display it in the next line, except
13064 when this line ends in ZV. */
13065 && !row->ends_at_zv_p
13066 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
13067 && charpos >= MATRIX_ROW_START_CHARPOS (row))
13068 return row;
13069 ++row;
13070 }
13071 }
13072
13073
13074 /* Try to redisplay window W by reusing its existing display. W's
13075 current matrix must be up to date when this function is called,
13076 i.e. window_end_valid must not be nil.
13077
13078 Value is
13079
13080 1 if display has been updated
13081 0 if otherwise unsuccessful
13082 -1 if redisplay with same window start is known not to succeed
13083
13084 The following steps are performed:
13085
13086 1. Find the last row in the current matrix of W that is not
13087 affected by changes at the start of current_buffer. If no such row
13088 is found, give up.
13089
13090 2. Find the first row in W's current matrix that is not affected by
13091 changes at the end of current_buffer. Maybe there is no such row.
13092
13093 3. Display lines beginning with the row + 1 found in step 1 to the
13094 row found in step 2 or, if step 2 didn't find a row, to the end of
13095 the window.
13096
13097 4. If cursor is not known to appear on the window, give up.
13098
13099 5. If display stopped at the row found in step 2, scroll the
13100 display and current matrix as needed.
13101
13102 6. Maybe display some lines at the end of W, if we must. This can
13103 happen under various circumstances, like a partially visible line
13104 becoming fully visible, or because newly displayed lines are displayed
13105 in smaller font sizes.
13106
13107 7. Update W's window end information. */
13108
13109 static int
13110 try_window_id (w)
13111 struct window *w;
13112 {
13113 struct frame *f = XFRAME (w->frame);
13114 struct glyph_matrix *current_matrix = w->current_matrix;
13115 struct glyph_matrix *desired_matrix = w->desired_matrix;
13116 struct glyph_row *last_unchanged_at_beg_row;
13117 struct glyph_row *first_unchanged_at_end_row;
13118 struct glyph_row *row;
13119 struct glyph_row *bottom_row;
13120 int bottom_vpos;
13121 struct it it;
13122 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
13123 struct text_pos start_pos;
13124 struct run run;
13125 int first_unchanged_at_end_vpos = 0;
13126 struct glyph_row *last_text_row, *last_text_row_at_end;
13127 struct text_pos start;
13128 int first_changed_charpos, last_changed_charpos;
13129
13130 #if GLYPH_DEBUG
13131 if (inhibit_try_window_id)
13132 return 0;
13133 #endif
13134
13135 /* This is handy for debugging. */
13136 #if 0
13137 #define GIVE_UP(X) \
13138 do { \
13139 fprintf (stderr, "try_window_id give up %d\n", (X)); \
13140 return 0; \
13141 } while (0)
13142 #else
13143 #define GIVE_UP(X) return 0
13144 #endif
13145
13146 SET_TEXT_POS_FROM_MARKER (start, w->start);
13147
13148 /* Don't use this for mini-windows because these can show
13149 messages and mini-buffers, and we don't handle that here. */
13150 if (MINI_WINDOW_P (w))
13151 GIVE_UP (1);
13152
13153 /* This flag is used to prevent redisplay optimizations. */
13154 if (windows_or_buffers_changed || cursor_type_changed)
13155 GIVE_UP (2);
13156
13157 /* Verify that narrowing has not changed.
13158 Also verify that we were not told to prevent redisplay optimizations.
13159 It would be nice to further
13160 reduce the number of cases where this prevents try_window_id. */
13161 if (current_buffer->clip_changed
13162 || current_buffer->prevent_redisplay_optimizations_p)
13163 GIVE_UP (3);
13164
13165 /* Window must either use window-based redisplay or be full width. */
13166 if (!FRAME_WINDOW_P (f)
13167 && (!FRAME_LINE_INS_DEL_OK (f)
13168 || !WINDOW_FULL_WIDTH_P (w)))
13169 GIVE_UP (4);
13170
13171 /* Give up if point is not known NOT to appear in W. */
13172 if (PT < CHARPOS (start))
13173 GIVE_UP (5);
13174
13175 /* Another way to prevent redisplay optimizations. */
13176 if (XFASTINT (w->last_modified) == 0)
13177 GIVE_UP (6);
13178
13179 /* Verify that window is not hscrolled. */
13180 if (XFASTINT (w->hscroll) != 0)
13181 GIVE_UP (7);
13182
13183 /* Verify that display wasn't paused. */
13184 if (NILP (w->window_end_valid))
13185 GIVE_UP (8);
13186
13187 /* Can't use this if highlighting a region because a cursor movement
13188 will do more than just set the cursor. */
13189 if (!NILP (Vtransient_mark_mode)
13190 && !NILP (current_buffer->mark_active))
13191 GIVE_UP (9);
13192
13193 /* Likewise if highlighting trailing whitespace. */
13194 if (!NILP (Vshow_trailing_whitespace))
13195 GIVE_UP (11);
13196
13197 /* Likewise if showing a region. */
13198 if (!NILP (w->region_showing))
13199 GIVE_UP (10);
13200
13201 /* Can use this if overlay arrow position and or string have changed. */
13202 if (overlay_arrows_changed_p ())
13203 GIVE_UP (12);
13204
13205
13206 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
13207 only if buffer has really changed. The reason is that the gap is
13208 initially at Z for freshly visited files. The code below would
13209 set end_unchanged to 0 in that case. */
13210 if (MODIFF > SAVE_MODIFF
13211 /* This seems to happen sometimes after saving a buffer. */
13212 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
13213 {
13214 if (GPT - BEG < BEG_UNCHANGED)
13215 BEG_UNCHANGED = GPT - BEG;
13216 if (Z - GPT < END_UNCHANGED)
13217 END_UNCHANGED = Z - GPT;
13218 }
13219
13220 /* The position of the first and last character that has been changed. */
13221 first_changed_charpos = BEG + BEG_UNCHANGED;
13222 last_changed_charpos = Z - END_UNCHANGED;
13223
13224 /* If window starts after a line end, and the last change is in
13225 front of that newline, then changes don't affect the display.
13226 This case happens with stealth-fontification. Note that although
13227 the display is unchanged, glyph positions in the matrix have to
13228 be adjusted, of course. */
13229 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
13230 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
13231 && ((last_changed_charpos < CHARPOS (start)
13232 && CHARPOS (start) == BEGV)
13233 || (last_changed_charpos < CHARPOS (start) - 1
13234 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
13235 {
13236 int Z_old, delta, Z_BYTE_old, delta_bytes;
13237 struct glyph_row *r0;
13238
13239 /* Compute how many chars/bytes have been added to or removed
13240 from the buffer. */
13241 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
13242 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
13243 delta = Z - Z_old;
13244 delta_bytes = Z_BYTE - Z_BYTE_old;
13245
13246 /* Give up if PT is not in the window. Note that it already has
13247 been checked at the start of try_window_id that PT is not in
13248 front of the window start. */
13249 if (PT >= MATRIX_ROW_END_CHARPOS (row) + delta)
13250 GIVE_UP (13);
13251
13252 /* If window start is unchanged, we can reuse the whole matrix
13253 as is, after adjusting glyph positions. No need to compute
13254 the window end again, since its offset from Z hasn't changed. */
13255 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
13256 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + delta
13257 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + delta_bytes
13258 /* PT must not be in a partially visible line. */
13259 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + delta
13260 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
13261 {
13262 /* Adjust positions in the glyph matrix. */
13263 if (delta || delta_bytes)
13264 {
13265 struct glyph_row *r1
13266 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
13267 increment_matrix_positions (w->current_matrix,
13268 MATRIX_ROW_VPOS (r0, current_matrix),
13269 MATRIX_ROW_VPOS (r1, current_matrix),
13270 delta, delta_bytes);
13271 }
13272
13273 /* Set the cursor. */
13274 row = row_containing_pos (w, PT, r0, NULL, 0);
13275 if (row)
13276 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
13277 else
13278 abort ();
13279 return 1;
13280 }
13281 }
13282
13283 /* Handle the case that changes are all below what is displayed in
13284 the window, and that PT is in the window. This shortcut cannot
13285 be taken if ZV is visible in the window, and text has been added
13286 there that is visible in the window. */
13287 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
13288 /* ZV is not visible in the window, or there are no
13289 changes at ZV, actually. */
13290 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
13291 || first_changed_charpos == last_changed_charpos))
13292 {
13293 struct glyph_row *r0;
13294
13295 /* Give up if PT is not in the window. Note that it already has
13296 been checked at the start of try_window_id that PT is not in
13297 front of the window start. */
13298 if (PT >= MATRIX_ROW_END_CHARPOS (row))
13299 GIVE_UP (14);
13300
13301 /* If window start is unchanged, we can reuse the whole matrix
13302 as is, without changing glyph positions since no text has
13303 been added/removed in front of the window end. */
13304 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
13305 if (TEXT_POS_EQUAL_P (start, r0->start.pos)
13306 /* PT must not be in a partially visible line. */
13307 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
13308 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
13309 {
13310 /* We have to compute the window end anew since text
13311 can have been added/removed after it. */
13312 w->window_end_pos
13313 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
13314 w->window_end_bytepos
13315 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
13316
13317 /* Set the cursor. */
13318 row = row_containing_pos (w, PT, r0, NULL, 0);
13319 if (row)
13320 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
13321 else
13322 abort ();
13323 return 2;
13324 }
13325 }
13326
13327 /* Give up if window start is in the changed area.
13328
13329 The condition used to read
13330
13331 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
13332
13333 but why that was tested escapes me at the moment. */
13334 if (CHARPOS (start) >= first_changed_charpos
13335 && CHARPOS (start) <= last_changed_charpos)
13336 GIVE_UP (15);
13337
13338 /* Check that window start agrees with the start of the first glyph
13339 row in its current matrix. Check this after we know the window
13340 start is not in changed text, otherwise positions would not be
13341 comparable. */
13342 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
13343 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
13344 GIVE_UP (16);
13345
13346 /* Give up if the window ends in strings. Overlay strings
13347 at the end are difficult to handle, so don't try. */
13348 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
13349 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
13350 GIVE_UP (20);
13351
13352 /* Compute the position at which we have to start displaying new
13353 lines. Some of the lines at the top of the window might be
13354 reusable because they are not displaying changed text. Find the
13355 last row in W's current matrix not affected by changes at the
13356 start of current_buffer. Value is null if changes start in the
13357 first line of window. */
13358 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
13359 if (last_unchanged_at_beg_row)
13360 {
13361 /* Avoid starting to display in the moddle of a character, a TAB
13362 for instance. This is easier than to set up the iterator
13363 exactly, and it's not a frequent case, so the additional
13364 effort wouldn't really pay off. */
13365 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
13366 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
13367 && last_unchanged_at_beg_row > w->current_matrix->rows)
13368 --last_unchanged_at_beg_row;
13369
13370 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
13371 GIVE_UP (17);
13372
13373 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
13374 GIVE_UP (18);
13375 start_pos = it.current.pos;
13376
13377 /* Start displaying new lines in the desired matrix at the same
13378 vpos we would use in the current matrix, i.e. below
13379 last_unchanged_at_beg_row. */
13380 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
13381 current_matrix);
13382 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
13383 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
13384
13385 xassert (it.hpos == 0 && it.current_x == 0);
13386 }
13387 else
13388 {
13389 /* There are no reusable lines at the start of the window.
13390 Start displaying in the first text line. */
13391 start_display (&it, w, start);
13392 it.vpos = it.first_vpos;
13393 start_pos = it.current.pos;
13394 }
13395
13396 /* Find the first row that is not affected by changes at the end of
13397 the buffer. Value will be null if there is no unchanged row, in
13398 which case we must redisplay to the end of the window. delta
13399 will be set to the value by which buffer positions beginning with
13400 first_unchanged_at_end_row have to be adjusted due to text
13401 changes. */
13402 first_unchanged_at_end_row
13403 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
13404 IF_DEBUG (debug_delta = delta);
13405 IF_DEBUG (debug_delta_bytes = delta_bytes);
13406
13407 /* Set stop_pos to the buffer position up to which we will have to
13408 display new lines. If first_unchanged_at_end_row != NULL, this
13409 is the buffer position of the start of the line displayed in that
13410 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
13411 that we don't stop at a buffer position. */
13412 stop_pos = 0;
13413 if (first_unchanged_at_end_row)
13414 {
13415 xassert (last_unchanged_at_beg_row == NULL
13416 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
13417
13418 /* If this is a continuation line, move forward to the next one
13419 that isn't. Changes in lines above affect this line.
13420 Caution: this may move first_unchanged_at_end_row to a row
13421 not displaying text. */
13422 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
13423 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
13424 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
13425 < it.last_visible_y))
13426 ++first_unchanged_at_end_row;
13427
13428 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
13429 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
13430 >= it.last_visible_y))
13431 first_unchanged_at_end_row = NULL;
13432 else
13433 {
13434 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
13435 + delta);
13436 first_unchanged_at_end_vpos
13437 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
13438 xassert (stop_pos >= Z - END_UNCHANGED);
13439 }
13440 }
13441 else if (last_unchanged_at_beg_row == NULL)
13442 GIVE_UP (19);
13443
13444
13445 #if GLYPH_DEBUG
13446
13447 /* Either there is no unchanged row at the end, or the one we have
13448 now displays text. This is a necessary condition for the window
13449 end pos calculation at the end of this function. */
13450 xassert (first_unchanged_at_end_row == NULL
13451 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
13452
13453 debug_last_unchanged_at_beg_vpos
13454 = (last_unchanged_at_beg_row
13455 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
13456 : -1);
13457 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
13458
13459 #endif /* GLYPH_DEBUG != 0 */
13460
13461
13462 /* Display new lines. Set last_text_row to the last new line
13463 displayed which has text on it, i.e. might end up as being the
13464 line where the window_end_vpos is. */
13465 w->cursor.vpos = -1;
13466 last_text_row = NULL;
13467 overlay_arrow_seen = 0;
13468 while (it.current_y < it.last_visible_y
13469 && !fonts_changed_p
13470 && (first_unchanged_at_end_row == NULL
13471 || IT_CHARPOS (it) < stop_pos))
13472 {
13473 if (display_line (&it))
13474 last_text_row = it.glyph_row - 1;
13475 }
13476
13477 if (fonts_changed_p)
13478 return -1;
13479
13480
13481 /* Compute differences in buffer positions, y-positions etc. for
13482 lines reused at the bottom of the window. Compute what we can
13483 scroll. */
13484 if (first_unchanged_at_end_row
13485 /* No lines reused because we displayed everything up to the
13486 bottom of the window. */
13487 && it.current_y < it.last_visible_y)
13488 {
13489 dvpos = (it.vpos
13490 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
13491 current_matrix));
13492 dy = it.current_y - first_unchanged_at_end_row->y;
13493 run.current_y = first_unchanged_at_end_row->y;
13494 run.desired_y = run.current_y + dy;
13495 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
13496 }
13497 else
13498 {
13499 delta = dvpos = dy = run.current_y = run.desired_y = run.height = 0;
13500 first_unchanged_at_end_row = NULL;
13501 }
13502 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
13503
13504
13505 /* Find the cursor if not already found. We have to decide whether
13506 PT will appear on this window (it sometimes doesn't, but this is
13507 not a very frequent case.) This decision has to be made before
13508 the current matrix is altered. A value of cursor.vpos < 0 means
13509 that PT is either in one of the lines beginning at
13510 first_unchanged_at_end_row or below the window. Don't care for
13511 lines that might be displayed later at the window end; as
13512 mentioned, this is not a frequent case. */
13513 if (w->cursor.vpos < 0)
13514 {
13515 /* Cursor in unchanged rows at the top? */
13516 if (PT < CHARPOS (start_pos)
13517 && last_unchanged_at_beg_row)
13518 {
13519 row = row_containing_pos (w, PT,
13520 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
13521 last_unchanged_at_beg_row + 1, 0);
13522 if (row)
13523 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13524 }
13525
13526 /* Start from first_unchanged_at_end_row looking for PT. */
13527 else if (first_unchanged_at_end_row)
13528 {
13529 row = row_containing_pos (w, PT - delta,
13530 first_unchanged_at_end_row, NULL, 0);
13531 if (row)
13532 set_cursor_from_row (w, row, w->current_matrix, delta,
13533 delta_bytes, dy, dvpos);
13534 }
13535
13536 /* Give up if cursor was not found. */
13537 if (w->cursor.vpos < 0)
13538 {
13539 clear_glyph_matrix (w->desired_matrix);
13540 return -1;
13541 }
13542 }
13543
13544 /* Don't let the cursor end in the scroll margins. */
13545 {
13546 int this_scroll_margin, cursor_height;
13547
13548 this_scroll_margin = max (0, scroll_margin);
13549 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
13550 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
13551 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
13552
13553 if ((w->cursor.y < this_scroll_margin
13554 && CHARPOS (start) > BEGV)
13555 /* Old redisplay didn't take scroll margin into account at the bottom,
13556 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
13557 || (w->cursor.y + (make_cursor_line_fully_visible_p
13558 ? cursor_height + this_scroll_margin
13559 : 1)) > it.last_visible_y)
13560 {
13561 w->cursor.vpos = -1;
13562 clear_glyph_matrix (w->desired_matrix);
13563 return -1;
13564 }
13565 }
13566
13567 /* Scroll the display. Do it before changing the current matrix so
13568 that xterm.c doesn't get confused about where the cursor glyph is
13569 found. */
13570 if (dy && run.height)
13571 {
13572 update_begin (f);
13573
13574 if (FRAME_WINDOW_P (f))
13575 {
13576 FRAME_RIF (f)->update_window_begin_hook (w);
13577 FRAME_RIF (f)->clear_window_mouse_face (w);
13578 FRAME_RIF (f)->scroll_run_hook (w, &run);
13579 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
13580 }
13581 else
13582 {
13583 /* Terminal frame. In this case, dvpos gives the number of
13584 lines to scroll by; dvpos < 0 means scroll up. */
13585 int first_unchanged_at_end_vpos
13586 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
13587 int from = WINDOW_TOP_EDGE_LINE (w) + first_unchanged_at_end_vpos;
13588 int end = (WINDOW_TOP_EDGE_LINE (w)
13589 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
13590 + window_internal_height (w));
13591
13592 /* Perform the operation on the screen. */
13593 if (dvpos > 0)
13594 {
13595 /* Scroll last_unchanged_at_beg_row to the end of the
13596 window down dvpos lines. */
13597 set_terminal_window (f, end);
13598
13599 /* On dumb terminals delete dvpos lines at the end
13600 before inserting dvpos empty lines. */
13601 if (!FRAME_SCROLL_REGION_OK (f))
13602 ins_del_lines (f, end - dvpos, -dvpos);
13603
13604 /* Insert dvpos empty lines in front of
13605 last_unchanged_at_beg_row. */
13606 ins_del_lines (f, from, dvpos);
13607 }
13608 else if (dvpos < 0)
13609 {
13610 /* Scroll up last_unchanged_at_beg_vpos to the end of
13611 the window to last_unchanged_at_beg_vpos - |dvpos|. */
13612 set_terminal_window (f, end);
13613
13614 /* Delete dvpos lines in front of
13615 last_unchanged_at_beg_vpos. ins_del_lines will set
13616 the cursor to the given vpos and emit |dvpos| delete
13617 line sequences. */
13618 ins_del_lines (f, from + dvpos, dvpos);
13619
13620 /* On a dumb terminal insert dvpos empty lines at the
13621 end. */
13622 if (!FRAME_SCROLL_REGION_OK (f))
13623 ins_del_lines (f, end + dvpos, -dvpos);
13624 }
13625
13626 set_terminal_window (f, 0);
13627 }
13628
13629 update_end (f);
13630 }
13631
13632 /* Shift reused rows of the current matrix to the right position.
13633 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
13634 text. */
13635 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
13636 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
13637 if (dvpos < 0)
13638 {
13639 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
13640 bottom_vpos, dvpos);
13641 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
13642 bottom_vpos, 0);
13643 }
13644 else if (dvpos > 0)
13645 {
13646 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
13647 bottom_vpos, dvpos);
13648 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
13649 first_unchanged_at_end_vpos + dvpos, 0);
13650 }
13651
13652 /* For frame-based redisplay, make sure that current frame and window
13653 matrix are in sync with respect to glyph memory. */
13654 if (!FRAME_WINDOW_P (f))
13655 sync_frame_with_window_matrix_rows (w);
13656
13657 /* Adjust buffer positions in reused rows. */
13658 if (delta)
13659 increment_matrix_positions (current_matrix,
13660 first_unchanged_at_end_vpos + dvpos,
13661 bottom_vpos, delta, delta_bytes);
13662
13663 /* Adjust Y positions. */
13664 if (dy)
13665 shift_glyph_matrix (w, current_matrix,
13666 first_unchanged_at_end_vpos + dvpos,
13667 bottom_vpos, dy);
13668
13669 if (first_unchanged_at_end_row)
13670 first_unchanged_at_end_row += dvpos;
13671
13672 /* If scrolling up, there may be some lines to display at the end of
13673 the window. */
13674 last_text_row_at_end = NULL;
13675 if (dy < 0)
13676 {
13677 /* Scrolling up can leave for example a partially visible line
13678 at the end of the window to be redisplayed. */
13679 /* Set last_row to the glyph row in the current matrix where the
13680 window end line is found. It has been moved up or down in
13681 the matrix by dvpos. */
13682 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
13683 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
13684
13685 /* If last_row is the window end line, it should display text. */
13686 xassert (last_row->displays_text_p);
13687
13688 /* If window end line was partially visible before, begin
13689 displaying at that line. Otherwise begin displaying with the
13690 line following it. */
13691 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
13692 {
13693 init_to_row_start (&it, w, last_row);
13694 it.vpos = last_vpos;
13695 it.current_y = last_row->y;
13696 }
13697 else
13698 {
13699 init_to_row_end (&it, w, last_row);
13700 it.vpos = 1 + last_vpos;
13701 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
13702 ++last_row;
13703 }
13704
13705 /* We may start in a continuation line. If so, we have to
13706 get the right continuation_lines_width and current_x. */
13707 it.continuation_lines_width = last_row->continuation_lines_width;
13708 it.hpos = it.current_x = 0;
13709
13710 /* Display the rest of the lines at the window end. */
13711 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
13712 while (it.current_y < it.last_visible_y
13713 && !fonts_changed_p)
13714 {
13715 /* Is it always sure that the display agrees with lines in
13716 the current matrix? I don't think so, so we mark rows
13717 displayed invalid in the current matrix by setting their
13718 enabled_p flag to zero. */
13719 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
13720 if (display_line (&it))
13721 last_text_row_at_end = it.glyph_row - 1;
13722 }
13723 }
13724
13725 /* Update window_end_pos and window_end_vpos. */
13726 if (first_unchanged_at_end_row
13727 && first_unchanged_at_end_row->y < it.last_visible_y
13728 && !last_text_row_at_end)
13729 {
13730 /* Window end line if one of the preserved rows from the current
13731 matrix. Set row to the last row displaying text in current
13732 matrix starting at first_unchanged_at_end_row, after
13733 scrolling. */
13734 xassert (first_unchanged_at_end_row->displays_text_p);
13735 row = find_last_row_displaying_text (w->current_matrix, &it,
13736 first_unchanged_at_end_row);
13737 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
13738
13739 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
13740 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
13741 w->window_end_vpos
13742 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
13743 xassert (w->window_end_bytepos >= 0);
13744 IF_DEBUG (debug_method_add (w, "A"));
13745 }
13746 else if (last_text_row_at_end)
13747 {
13748 w->window_end_pos
13749 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
13750 w->window_end_bytepos
13751 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
13752 w->window_end_vpos
13753 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
13754 xassert (w->window_end_bytepos >= 0);
13755 IF_DEBUG (debug_method_add (w, "B"));
13756 }
13757 else if (last_text_row)
13758 {
13759 /* We have displayed either to the end of the window or at the
13760 end of the window, i.e. the last row with text is to be found
13761 in the desired matrix. */
13762 w->window_end_pos
13763 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
13764 w->window_end_bytepos
13765 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
13766 w->window_end_vpos
13767 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
13768 xassert (w->window_end_bytepos >= 0);
13769 }
13770 else if (first_unchanged_at_end_row == NULL
13771 && last_text_row == NULL
13772 && last_text_row_at_end == NULL)
13773 {
13774 /* Displayed to end of window, but no line containing text was
13775 displayed. Lines were deleted at the end of the window. */
13776 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
13777 int vpos = XFASTINT (w->window_end_vpos);
13778 struct glyph_row *current_row = current_matrix->rows + vpos;
13779 struct glyph_row *desired_row = desired_matrix->rows + vpos;
13780
13781 for (row = NULL;
13782 row == NULL && vpos >= first_vpos;
13783 --vpos, --current_row, --desired_row)
13784 {
13785 if (desired_row->enabled_p)
13786 {
13787 if (desired_row->displays_text_p)
13788 row = desired_row;
13789 }
13790 else if (current_row->displays_text_p)
13791 row = current_row;
13792 }
13793
13794 xassert (row != NULL);
13795 w->window_end_vpos = make_number (vpos + 1);
13796 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
13797 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
13798 xassert (w->window_end_bytepos >= 0);
13799 IF_DEBUG (debug_method_add (w, "C"));
13800 }
13801 else
13802 abort ();
13803
13804 #if 0 /* This leads to problems, for instance when the cursor is
13805 at ZV, and the cursor line displays no text. */
13806 /* Disable rows below what's displayed in the window. This makes
13807 debugging easier. */
13808 enable_glyph_matrix_rows (current_matrix,
13809 XFASTINT (w->window_end_vpos) + 1,
13810 bottom_vpos, 0);
13811 #endif
13812
13813 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
13814 debug_end_vpos = XFASTINT (w->window_end_vpos));
13815
13816 /* Record that display has not been completed. */
13817 w->window_end_valid = Qnil;
13818 w->desired_matrix->no_scrolling_p = 1;
13819 return 3;
13820
13821 #undef GIVE_UP
13822 }
13823
13824
13825 \f
13826 /***********************************************************************
13827 More debugging support
13828 ***********************************************************************/
13829
13830 #if GLYPH_DEBUG
13831
13832 void dump_glyph_row P_ ((struct glyph_row *, int, int));
13833 void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
13834 void dump_glyph P_ ((struct glyph_row *, struct glyph *, int));
13835
13836
13837 /* Dump the contents of glyph matrix MATRIX on stderr.
13838
13839 GLYPHS 0 means don't show glyph contents.
13840 GLYPHS 1 means show glyphs in short form
13841 GLYPHS > 1 means show glyphs in long form. */
13842
13843 void
13844 dump_glyph_matrix (matrix, glyphs)
13845 struct glyph_matrix *matrix;
13846 int glyphs;
13847 {
13848 int i;
13849 for (i = 0; i < matrix->nrows; ++i)
13850 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
13851 }
13852
13853
13854 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
13855 the glyph row and area where the glyph comes from. */
13856
13857 void
13858 dump_glyph (row, glyph, area)
13859 struct glyph_row *row;
13860 struct glyph *glyph;
13861 int area;
13862 {
13863 if (glyph->type == CHAR_GLYPH)
13864 {
13865 fprintf (stderr,
13866 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
13867 glyph - row->glyphs[TEXT_AREA],
13868 'C',
13869 glyph->charpos,
13870 (BUFFERP (glyph->object)
13871 ? 'B'
13872 : (STRINGP (glyph->object)
13873 ? 'S'
13874 : '-')),
13875 glyph->pixel_width,
13876 glyph->u.ch,
13877 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
13878 ? glyph->u.ch
13879 : '.'),
13880 glyph->face_id,
13881 glyph->left_box_line_p,
13882 glyph->right_box_line_p);
13883 }
13884 else if (glyph->type == STRETCH_GLYPH)
13885 {
13886 fprintf (stderr,
13887 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
13888 glyph - row->glyphs[TEXT_AREA],
13889 'S',
13890 glyph->charpos,
13891 (BUFFERP (glyph->object)
13892 ? 'B'
13893 : (STRINGP (glyph->object)
13894 ? 'S'
13895 : '-')),
13896 glyph->pixel_width,
13897 0,
13898 '.',
13899 glyph->face_id,
13900 glyph->left_box_line_p,
13901 glyph->right_box_line_p);
13902 }
13903 else if (glyph->type == IMAGE_GLYPH)
13904 {
13905 fprintf (stderr,
13906 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
13907 glyph - row->glyphs[TEXT_AREA],
13908 'I',
13909 glyph->charpos,
13910 (BUFFERP (glyph->object)
13911 ? 'B'
13912 : (STRINGP (glyph->object)
13913 ? 'S'
13914 : '-')),
13915 glyph->pixel_width,
13916 glyph->u.img_id,
13917 '.',
13918 glyph->face_id,
13919 glyph->left_box_line_p,
13920 glyph->right_box_line_p);
13921 }
13922 }
13923
13924
13925 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
13926 GLYPHS 0 means don't show glyph contents.
13927 GLYPHS 1 means show glyphs in short form
13928 GLYPHS > 1 means show glyphs in long form. */
13929
13930 void
13931 dump_glyph_row (row, vpos, glyphs)
13932 struct glyph_row *row;
13933 int vpos, glyphs;
13934 {
13935 if (glyphs != 1)
13936 {
13937 fprintf (stderr, "Row Start End Used oEI><O\\CTZFesm X Y W H V A P\n");
13938 fprintf (stderr, "=======================================================================\n");
13939
13940 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d%1.1d\
13941 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
13942 vpos,
13943 MATRIX_ROW_START_CHARPOS (row),
13944 MATRIX_ROW_END_CHARPOS (row),
13945 row->used[TEXT_AREA],
13946 row->contains_overlapping_glyphs_p,
13947 row->enabled_p,
13948 row->truncated_on_left_p,
13949 row->truncated_on_right_p,
13950 row->overlay_arrow_p,
13951 row->continued_p,
13952 MATRIX_ROW_CONTINUATION_LINE_P (row),
13953 row->displays_text_p,
13954 row->ends_at_zv_p,
13955 row->fill_line_p,
13956 row->ends_in_middle_of_char_p,
13957 row->starts_in_middle_of_char_p,
13958 row->mouse_face_p,
13959 row->x,
13960 row->y,
13961 row->pixel_width,
13962 row->height,
13963 row->visible_height,
13964 row->ascent,
13965 row->phys_ascent);
13966 fprintf (stderr, "%9d %5d\t%5d\n", row->start.overlay_string_index,
13967 row->end.overlay_string_index,
13968 row->continuation_lines_width);
13969 fprintf (stderr, "%9d %5d\n",
13970 CHARPOS (row->start.string_pos),
13971 CHARPOS (row->end.string_pos));
13972 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
13973 row->end.dpvec_index);
13974 }
13975
13976 if (glyphs > 1)
13977 {
13978 int area;
13979
13980 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
13981 {
13982 struct glyph *glyph = row->glyphs[area];
13983 struct glyph *glyph_end = glyph + row->used[area];
13984
13985 /* Glyph for a line end in text. */
13986 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
13987 ++glyph_end;
13988
13989 if (glyph < glyph_end)
13990 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
13991
13992 for (; glyph < glyph_end; ++glyph)
13993 dump_glyph (row, glyph, area);
13994 }
13995 }
13996 else if (glyphs == 1)
13997 {
13998 int area;
13999
14000 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
14001 {
14002 char *s = (char *) alloca (row->used[area] + 1);
14003 int i;
14004
14005 for (i = 0; i < row->used[area]; ++i)
14006 {
14007 struct glyph *glyph = row->glyphs[area] + i;
14008 if (glyph->type == CHAR_GLYPH
14009 && glyph->u.ch < 0x80
14010 && glyph->u.ch >= ' ')
14011 s[i] = glyph->u.ch;
14012 else
14013 s[i] = '.';
14014 }
14015
14016 s[i] = '\0';
14017 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
14018 }
14019 }
14020 }
14021
14022
14023 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
14024 Sdump_glyph_matrix, 0, 1, "p",
14025 doc: /* Dump the current matrix of the selected window to stderr.
14026 Shows contents of glyph row structures. With non-nil
14027 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
14028 glyphs in short form, otherwise show glyphs in long form. */)
14029 (glyphs)
14030 Lisp_Object glyphs;
14031 {
14032 struct window *w = XWINDOW (selected_window);
14033 struct buffer *buffer = XBUFFER (w->buffer);
14034
14035 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
14036 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
14037 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
14038 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
14039 fprintf (stderr, "=============================================\n");
14040 dump_glyph_matrix (w->current_matrix,
14041 NILP (glyphs) ? 0 : XINT (glyphs));
14042 return Qnil;
14043 }
14044
14045
14046 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
14047 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
14048 ()
14049 {
14050 struct frame *f = XFRAME (selected_frame);
14051 dump_glyph_matrix (f->current_matrix, 1);
14052 return Qnil;
14053 }
14054
14055
14056 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
14057 doc: /* Dump glyph row ROW to stderr.
14058 GLYPH 0 means don't dump glyphs.
14059 GLYPH 1 means dump glyphs in short form.
14060 GLYPH > 1 or omitted means dump glyphs in long form. */)
14061 (row, glyphs)
14062 Lisp_Object row, glyphs;
14063 {
14064 struct glyph_matrix *matrix;
14065 int vpos;
14066
14067 CHECK_NUMBER (row);
14068 matrix = XWINDOW (selected_window)->current_matrix;
14069 vpos = XINT (row);
14070 if (vpos >= 0 && vpos < matrix->nrows)
14071 dump_glyph_row (MATRIX_ROW (matrix, vpos),
14072 vpos,
14073 INTEGERP (glyphs) ? XINT (glyphs) : 2);
14074 return Qnil;
14075 }
14076
14077
14078 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
14079 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
14080 GLYPH 0 means don't dump glyphs.
14081 GLYPH 1 means dump glyphs in short form.
14082 GLYPH > 1 or omitted means dump glyphs in long form. */)
14083 (row, glyphs)
14084 Lisp_Object row, glyphs;
14085 {
14086 struct frame *sf = SELECTED_FRAME ();
14087 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
14088 int vpos;
14089
14090 CHECK_NUMBER (row);
14091 vpos = XINT (row);
14092 if (vpos >= 0 && vpos < m->nrows)
14093 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
14094 INTEGERP (glyphs) ? XINT (glyphs) : 2);
14095 return Qnil;
14096 }
14097
14098
14099 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
14100 doc: /* Toggle tracing of redisplay.
14101 With ARG, turn tracing on if and only if ARG is positive. */)
14102 (arg)
14103 Lisp_Object arg;
14104 {
14105 if (NILP (arg))
14106 trace_redisplay_p = !trace_redisplay_p;
14107 else
14108 {
14109 arg = Fprefix_numeric_value (arg);
14110 trace_redisplay_p = XINT (arg) > 0;
14111 }
14112
14113 return Qnil;
14114 }
14115
14116
14117 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
14118 doc: /* Like `format', but print result to stderr.
14119 usage: (trace-to-stderr STRING &rest OBJECTS) */)
14120 (nargs, args)
14121 int nargs;
14122 Lisp_Object *args;
14123 {
14124 Lisp_Object s = Fformat (nargs, args);
14125 fprintf (stderr, "%s", SDATA (s));
14126 return Qnil;
14127 }
14128
14129 #endif /* GLYPH_DEBUG */
14130
14131
14132 \f
14133 /***********************************************************************
14134 Building Desired Matrix Rows
14135 ***********************************************************************/
14136
14137 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
14138 Used for non-window-redisplay windows, and for windows w/o left fringe. */
14139
14140 static struct glyph_row *
14141 get_overlay_arrow_glyph_row (w, overlay_arrow_string)
14142 struct window *w;
14143 Lisp_Object overlay_arrow_string;
14144 {
14145 struct frame *f = XFRAME (WINDOW_FRAME (w));
14146 struct buffer *buffer = XBUFFER (w->buffer);
14147 struct buffer *old = current_buffer;
14148 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
14149 int arrow_len = SCHARS (overlay_arrow_string);
14150 const unsigned char *arrow_end = arrow_string + arrow_len;
14151 const unsigned char *p;
14152 struct it it;
14153 int multibyte_p;
14154 int n_glyphs_before;
14155
14156 set_buffer_temp (buffer);
14157 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
14158 it.glyph_row->used[TEXT_AREA] = 0;
14159 SET_TEXT_POS (it.position, 0, 0);
14160
14161 multibyte_p = !NILP (buffer->enable_multibyte_characters);
14162 p = arrow_string;
14163 while (p < arrow_end)
14164 {
14165 Lisp_Object face, ilisp;
14166
14167 /* Get the next character. */
14168 if (multibyte_p)
14169 it.c = string_char_and_length (p, arrow_len, &it.len);
14170 else
14171 it.c = *p, it.len = 1;
14172 p += it.len;
14173
14174 /* Get its face. */
14175 ilisp = make_number (p - arrow_string);
14176 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
14177 it.face_id = compute_char_face (f, it.c, face);
14178
14179 /* Compute its width, get its glyphs. */
14180 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
14181 SET_TEXT_POS (it.position, -1, -1);
14182 PRODUCE_GLYPHS (&it);
14183
14184 /* If this character doesn't fit any more in the line, we have
14185 to remove some glyphs. */
14186 if (it.current_x > it.last_visible_x)
14187 {
14188 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
14189 break;
14190 }
14191 }
14192
14193 set_buffer_temp (old);
14194 return it.glyph_row;
14195 }
14196
14197
14198 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
14199 glyphs are only inserted for terminal frames since we can't really
14200 win with truncation glyphs when partially visible glyphs are
14201 involved. Which glyphs to insert is determined by
14202 produce_special_glyphs. */
14203
14204 static void
14205 insert_left_trunc_glyphs (it)
14206 struct it *it;
14207 {
14208 struct it truncate_it;
14209 struct glyph *from, *end, *to, *toend;
14210
14211 xassert (!FRAME_WINDOW_P (it->f));
14212
14213 /* Get the truncation glyphs. */
14214 truncate_it = *it;
14215 truncate_it.current_x = 0;
14216 truncate_it.face_id = DEFAULT_FACE_ID;
14217 truncate_it.glyph_row = &scratch_glyph_row;
14218 truncate_it.glyph_row->used[TEXT_AREA] = 0;
14219 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
14220 truncate_it.object = make_number (0);
14221 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
14222
14223 /* Overwrite glyphs from IT with truncation glyphs. */
14224 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
14225 end = from + truncate_it.glyph_row->used[TEXT_AREA];
14226 to = it->glyph_row->glyphs[TEXT_AREA];
14227 toend = to + it->glyph_row->used[TEXT_AREA];
14228
14229 while (from < end)
14230 *to++ = *from++;
14231
14232 /* There may be padding glyphs left over. Overwrite them too. */
14233 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
14234 {
14235 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
14236 while (from < end)
14237 *to++ = *from++;
14238 }
14239
14240 if (to > toend)
14241 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
14242 }
14243
14244
14245 /* Compute the pixel height and width of IT->glyph_row.
14246
14247 Most of the time, ascent and height of a display line will be equal
14248 to the max_ascent and max_height values of the display iterator
14249 structure. This is not the case if
14250
14251 1. We hit ZV without displaying anything. In this case, max_ascent
14252 and max_height will be zero.
14253
14254 2. We have some glyphs that don't contribute to the line height.
14255 (The glyph row flag contributes_to_line_height_p is for future
14256 pixmap extensions).
14257
14258 The first case is easily covered by using default values because in
14259 these cases, the line height does not really matter, except that it
14260 must not be zero. */
14261
14262 static void
14263 compute_line_metrics (it)
14264 struct it *it;
14265 {
14266 struct glyph_row *row = it->glyph_row;
14267 int area, i;
14268
14269 if (FRAME_WINDOW_P (it->f))
14270 {
14271 int i, min_y, max_y;
14272
14273 /* The line may consist of one space only, that was added to
14274 place the cursor on it. If so, the row's height hasn't been
14275 computed yet. */
14276 if (row->height == 0)
14277 {
14278 if (it->max_ascent + it->max_descent == 0)
14279 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
14280 row->ascent = it->max_ascent;
14281 row->height = it->max_ascent + it->max_descent;
14282 row->phys_ascent = it->max_phys_ascent;
14283 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
14284 row->extra_line_spacing = it->max_extra_line_spacing;
14285 }
14286
14287 /* Compute the width of this line. */
14288 row->pixel_width = row->x;
14289 for (i = 0; i < row->used[TEXT_AREA]; ++i)
14290 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
14291
14292 xassert (row->pixel_width >= 0);
14293 xassert (row->ascent >= 0 && row->height > 0);
14294
14295 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
14296 || MATRIX_ROW_OVERLAPS_PRED_P (row));
14297
14298 /* If first line's physical ascent is larger than its logical
14299 ascent, use the physical ascent, and make the row taller.
14300 This makes accented characters fully visible. */
14301 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
14302 && row->phys_ascent > row->ascent)
14303 {
14304 row->height += row->phys_ascent - row->ascent;
14305 row->ascent = row->phys_ascent;
14306 }
14307
14308 /* Compute how much of the line is visible. */
14309 row->visible_height = row->height;
14310
14311 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
14312 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
14313
14314 if (row->y < min_y)
14315 row->visible_height -= min_y - row->y;
14316 if (row->y + row->height > max_y)
14317 row->visible_height -= row->y + row->height - max_y;
14318 }
14319 else
14320 {
14321 row->pixel_width = row->used[TEXT_AREA];
14322 if (row->continued_p)
14323 row->pixel_width -= it->continuation_pixel_width;
14324 else if (row->truncated_on_right_p)
14325 row->pixel_width -= it->truncation_pixel_width;
14326 row->ascent = row->phys_ascent = 0;
14327 row->height = row->phys_height = row->visible_height = 1;
14328 row->extra_line_spacing = 0;
14329 }
14330
14331 /* Compute a hash code for this row. */
14332 row->hash = 0;
14333 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
14334 for (i = 0; i < row->used[area]; ++i)
14335 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
14336 + row->glyphs[area][i].u.val
14337 + row->glyphs[area][i].face_id
14338 + row->glyphs[area][i].padding_p
14339 + (row->glyphs[area][i].type << 2));
14340
14341 it->max_ascent = it->max_descent = 0;
14342 it->max_phys_ascent = it->max_phys_descent = 0;
14343 }
14344
14345
14346 /* Append one space to the glyph row of iterator IT if doing a
14347 window-based redisplay. The space has the same face as
14348 IT->face_id. Value is non-zero if a space was added.
14349
14350 This function is called to make sure that there is always one glyph
14351 at the end of a glyph row that the cursor can be set on under
14352 window-systems. (If there weren't such a glyph we would not know
14353 how wide and tall a box cursor should be displayed).
14354
14355 At the same time this space let's a nicely handle clearing to the
14356 end of the line if the row ends in italic text. */
14357
14358 static int
14359 append_space_for_newline (it, default_face_p)
14360 struct it *it;
14361 int default_face_p;
14362 {
14363 if (FRAME_WINDOW_P (it->f))
14364 {
14365 int n = it->glyph_row->used[TEXT_AREA];
14366
14367 if (it->glyph_row->glyphs[TEXT_AREA] + n
14368 < it->glyph_row->glyphs[1 + TEXT_AREA])
14369 {
14370 /* Save some values that must not be changed.
14371 Must save IT->c and IT->len because otherwise
14372 ITERATOR_AT_END_P wouldn't work anymore after
14373 append_space_for_newline has been called. */
14374 enum display_element_type saved_what = it->what;
14375 int saved_c = it->c, saved_len = it->len;
14376 int saved_x = it->current_x;
14377 int saved_face_id = it->face_id;
14378 struct text_pos saved_pos;
14379 Lisp_Object saved_object;
14380 struct face *face;
14381
14382 saved_object = it->object;
14383 saved_pos = it->position;
14384
14385 it->what = IT_CHARACTER;
14386 bzero (&it->position, sizeof it->position);
14387 it->object = make_number (0);
14388 it->c = ' ';
14389 it->len = 1;
14390
14391 if (default_face_p)
14392 it->face_id = DEFAULT_FACE_ID;
14393 else if (it->face_before_selective_p)
14394 it->face_id = it->saved_face_id;
14395 face = FACE_FROM_ID (it->f, it->face_id);
14396 it->face_id = FACE_FOR_CHAR (it->f, face, 0);
14397
14398 PRODUCE_GLYPHS (it);
14399
14400 it->override_ascent = -1;
14401 it->constrain_row_ascent_descent_p = 0;
14402 it->current_x = saved_x;
14403 it->object = saved_object;
14404 it->position = saved_pos;
14405 it->what = saved_what;
14406 it->face_id = saved_face_id;
14407 it->len = saved_len;
14408 it->c = saved_c;
14409 return 1;
14410 }
14411 }
14412
14413 return 0;
14414 }
14415
14416
14417 /* Extend the face of the last glyph in the text area of IT->glyph_row
14418 to the end of the display line. Called from display_line.
14419 If the glyph row is empty, add a space glyph to it so that we
14420 know the face to draw. Set the glyph row flag fill_line_p. */
14421
14422 static void
14423 extend_face_to_end_of_line (it)
14424 struct it *it;
14425 {
14426 struct face *face;
14427 struct frame *f = it->f;
14428
14429 /* If line is already filled, do nothing. */
14430 if (it->current_x >= it->last_visible_x)
14431 return;
14432
14433 /* Face extension extends the background and box of IT->face_id
14434 to the end of the line. If the background equals the background
14435 of the frame, we don't have to do anything. */
14436 if (it->face_before_selective_p)
14437 face = FACE_FROM_ID (it->f, it->saved_face_id);
14438 else
14439 face = FACE_FROM_ID (f, it->face_id);
14440
14441 if (FRAME_WINDOW_P (f)
14442 && face->box == FACE_NO_BOX
14443 && face->background == FRAME_BACKGROUND_PIXEL (f)
14444 && !face->stipple)
14445 return;
14446
14447 /* Set the glyph row flag indicating that the face of the last glyph
14448 in the text area has to be drawn to the end of the text area. */
14449 it->glyph_row->fill_line_p = 1;
14450
14451 /* If current character of IT is not ASCII, make sure we have the
14452 ASCII face. This will be automatically undone the next time
14453 get_next_display_element returns a multibyte character. Note
14454 that the character will always be single byte in unibyte text. */
14455 if (!SINGLE_BYTE_CHAR_P (it->c))
14456 {
14457 it->face_id = FACE_FOR_CHAR (f, face, 0);
14458 }
14459
14460 if (FRAME_WINDOW_P (f))
14461 {
14462 /* If the row is empty, add a space with the current face of IT,
14463 so that we know which face to draw. */
14464 if (it->glyph_row->used[TEXT_AREA] == 0)
14465 {
14466 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
14467 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
14468 it->glyph_row->used[TEXT_AREA] = 1;
14469 }
14470 }
14471 else
14472 {
14473 /* Save some values that must not be changed. */
14474 int saved_x = it->current_x;
14475 struct text_pos saved_pos;
14476 Lisp_Object saved_object;
14477 enum display_element_type saved_what = it->what;
14478 int saved_face_id = it->face_id;
14479
14480 saved_object = it->object;
14481 saved_pos = it->position;
14482
14483 it->what = IT_CHARACTER;
14484 bzero (&it->position, sizeof it->position);
14485 it->object = make_number (0);
14486 it->c = ' ';
14487 it->len = 1;
14488 it->face_id = face->id;
14489
14490 PRODUCE_GLYPHS (it);
14491
14492 while (it->current_x <= it->last_visible_x)
14493 PRODUCE_GLYPHS (it);
14494
14495 /* Don't count these blanks really. It would let us insert a left
14496 truncation glyph below and make us set the cursor on them, maybe. */
14497 it->current_x = saved_x;
14498 it->object = saved_object;
14499 it->position = saved_pos;
14500 it->what = saved_what;
14501 it->face_id = saved_face_id;
14502 }
14503 }
14504
14505
14506 /* Value is non-zero if text starting at CHARPOS in current_buffer is
14507 trailing whitespace. */
14508
14509 static int
14510 trailing_whitespace_p (charpos)
14511 int charpos;
14512 {
14513 int bytepos = CHAR_TO_BYTE (charpos);
14514 int c = 0;
14515
14516 while (bytepos < ZV_BYTE
14517 && (c = FETCH_CHAR (bytepos),
14518 c == ' ' || c == '\t'))
14519 ++bytepos;
14520
14521 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
14522 {
14523 if (bytepos != PT_BYTE)
14524 return 1;
14525 }
14526 return 0;
14527 }
14528
14529
14530 /* Highlight trailing whitespace, if any, in ROW. */
14531
14532 void
14533 highlight_trailing_whitespace (f, row)
14534 struct frame *f;
14535 struct glyph_row *row;
14536 {
14537 int used = row->used[TEXT_AREA];
14538
14539 if (used)
14540 {
14541 struct glyph *start = row->glyphs[TEXT_AREA];
14542 struct glyph *glyph = start + used - 1;
14543
14544 /* Skip over glyphs inserted to display the cursor at the
14545 end of a line, for extending the face of the last glyph
14546 to the end of the line on terminals, and for truncation
14547 and continuation glyphs. */
14548 while (glyph >= start
14549 && glyph->type == CHAR_GLYPH
14550 && INTEGERP (glyph->object))
14551 --glyph;
14552
14553 /* If last glyph is a space or stretch, and it's trailing
14554 whitespace, set the face of all trailing whitespace glyphs in
14555 IT->glyph_row to `trailing-whitespace'. */
14556 if (glyph >= start
14557 && BUFFERP (glyph->object)
14558 && (glyph->type == STRETCH_GLYPH
14559 || (glyph->type == CHAR_GLYPH
14560 && glyph->u.ch == ' '))
14561 && trailing_whitespace_p (glyph->charpos))
14562 {
14563 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0, 0);
14564 if (face_id < 0)
14565 return;
14566
14567 while (glyph >= start
14568 && BUFFERP (glyph->object)
14569 && (glyph->type == STRETCH_GLYPH
14570 || (glyph->type == CHAR_GLYPH
14571 && glyph->u.ch == ' ')))
14572 (glyph--)->face_id = face_id;
14573 }
14574 }
14575 }
14576
14577
14578 /* Value is non-zero if glyph row ROW in window W should be
14579 used to hold the cursor. */
14580
14581 static int
14582 cursor_row_p (w, row)
14583 struct window *w;
14584 struct glyph_row *row;
14585 {
14586 int cursor_row_p = 1;
14587
14588 if (PT == MATRIX_ROW_END_CHARPOS (row))
14589 {
14590 /* If the row ends with a newline from a string, we don't want
14591 the cursor there (if the row is continued it doesn't end in a
14592 newline). */
14593 if (CHARPOS (row->end.string_pos) >= 0
14594 || MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
14595 cursor_row_p = row->continued_p;
14596
14597 /* If the row ends at ZV, display the cursor at the end of that
14598 row instead of at the start of the row below. */
14599 else if (row->ends_at_zv_p)
14600 cursor_row_p = 1;
14601 else
14602 cursor_row_p = 0;
14603 }
14604
14605 return cursor_row_p;
14606 }
14607
14608
14609 /* Construct the glyph row IT->glyph_row in the desired matrix of
14610 IT->w from text at the current position of IT. See dispextern.h
14611 for an overview of struct it. Value is non-zero if
14612 IT->glyph_row displays text, as opposed to a line displaying ZV
14613 only. */
14614
14615 static int
14616 display_line (it)
14617 struct it *it;
14618 {
14619 struct glyph_row *row = it->glyph_row;
14620 int overlay_arrow_bitmap;
14621 Lisp_Object overlay_arrow_string;
14622
14623 /* We always start displaying at hpos zero even if hscrolled. */
14624 xassert (it->hpos == 0 && it->current_x == 0);
14625
14626 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
14627 >= it->w->desired_matrix->nrows)
14628 {
14629 it->w->nrows_scale_factor++;
14630 fonts_changed_p = 1;
14631 return 0;
14632 }
14633
14634 /* Is IT->w showing the region? */
14635 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
14636
14637 /* Clear the result glyph row and enable it. */
14638 prepare_desired_row (row);
14639
14640 row->y = it->current_y;
14641 row->start = it->start;
14642 row->continuation_lines_width = it->continuation_lines_width;
14643 row->displays_text_p = 1;
14644 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
14645 it->starts_in_middle_of_char_p = 0;
14646
14647 /* Arrange the overlays nicely for our purposes. Usually, we call
14648 display_line on only one line at a time, in which case this
14649 can't really hurt too much, or we call it on lines which appear
14650 one after another in the buffer, in which case all calls to
14651 recenter_overlay_lists but the first will be pretty cheap. */
14652 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
14653
14654 /* Move over display elements that are not visible because we are
14655 hscrolled. This may stop at an x-position < IT->first_visible_x
14656 if the first glyph is partially visible or if we hit a line end. */
14657 if (it->current_x < it->first_visible_x)
14658 {
14659 move_it_in_display_line_to (it, ZV, it->first_visible_x,
14660 MOVE_TO_POS | MOVE_TO_X);
14661 }
14662
14663 /* Get the initial row height. This is either the height of the
14664 text hscrolled, if there is any, or zero. */
14665 row->ascent = it->max_ascent;
14666 row->height = it->max_ascent + it->max_descent;
14667 row->phys_ascent = it->max_phys_ascent;
14668 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
14669 row->extra_line_spacing = it->max_extra_line_spacing;
14670
14671 /* Loop generating characters. The loop is left with IT on the next
14672 character to display. */
14673 while (1)
14674 {
14675 int n_glyphs_before, hpos_before, x_before;
14676 int x, i, nglyphs;
14677 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
14678
14679 /* Retrieve the next thing to display. Value is zero if end of
14680 buffer reached. */
14681 if (!get_next_display_element (it))
14682 {
14683 /* Maybe add a space at the end of this line that is used to
14684 display the cursor there under X. Set the charpos of the
14685 first glyph of blank lines not corresponding to any text
14686 to -1. */
14687 #ifdef HAVE_WINDOW_SYSTEM
14688 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
14689 row->exact_window_width_line_p = 1;
14690 else
14691 #endif /* HAVE_WINDOW_SYSTEM */
14692 if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
14693 || row->used[TEXT_AREA] == 0)
14694 {
14695 row->glyphs[TEXT_AREA]->charpos = -1;
14696 row->displays_text_p = 0;
14697
14698 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines)
14699 && (!MINI_WINDOW_P (it->w)
14700 || (minibuf_level && EQ (it->window, minibuf_window))))
14701 row->indicate_empty_line_p = 1;
14702 }
14703
14704 it->continuation_lines_width = 0;
14705 row->ends_at_zv_p = 1;
14706 break;
14707 }
14708
14709 /* Now, get the metrics of what we want to display. This also
14710 generates glyphs in `row' (which is IT->glyph_row). */
14711 n_glyphs_before = row->used[TEXT_AREA];
14712 x = it->current_x;
14713
14714 /* Remember the line height so far in case the next element doesn't
14715 fit on the line. */
14716 if (!it->truncate_lines_p)
14717 {
14718 ascent = it->max_ascent;
14719 descent = it->max_descent;
14720 phys_ascent = it->max_phys_ascent;
14721 phys_descent = it->max_phys_descent;
14722 }
14723
14724 PRODUCE_GLYPHS (it);
14725
14726 /* If this display element was in marginal areas, continue with
14727 the next one. */
14728 if (it->area != TEXT_AREA)
14729 {
14730 row->ascent = max (row->ascent, it->max_ascent);
14731 row->height = max (row->height, it->max_ascent + it->max_descent);
14732 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
14733 row->phys_height = max (row->phys_height,
14734 it->max_phys_ascent + it->max_phys_descent);
14735 row->extra_line_spacing = max (row->extra_line_spacing,
14736 it->max_extra_line_spacing);
14737 set_iterator_to_next (it, 1);
14738 continue;
14739 }
14740
14741 /* Does the display element fit on the line? If we truncate
14742 lines, we should draw past the right edge of the window. If
14743 we don't truncate, we want to stop so that we can display the
14744 continuation glyph before the right margin. If lines are
14745 continued, there are two possible strategies for characters
14746 resulting in more than 1 glyph (e.g. tabs): Display as many
14747 glyphs as possible in this line and leave the rest for the
14748 continuation line, or display the whole element in the next
14749 line. Original redisplay did the former, so we do it also. */
14750 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
14751 hpos_before = it->hpos;
14752 x_before = x;
14753
14754 if (/* Not a newline. */
14755 nglyphs > 0
14756 /* Glyphs produced fit entirely in the line. */
14757 && it->current_x < it->last_visible_x)
14758 {
14759 it->hpos += nglyphs;
14760 row->ascent = max (row->ascent, it->max_ascent);
14761 row->height = max (row->height, it->max_ascent + it->max_descent);
14762 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
14763 row->phys_height = max (row->phys_height,
14764 it->max_phys_ascent + it->max_phys_descent);
14765 row->extra_line_spacing = max (row->extra_line_spacing,
14766 it->max_extra_line_spacing);
14767 if (it->current_x - it->pixel_width < it->first_visible_x)
14768 row->x = x - it->first_visible_x;
14769 }
14770 else
14771 {
14772 int new_x;
14773 struct glyph *glyph;
14774
14775 for (i = 0; i < nglyphs; ++i, x = new_x)
14776 {
14777 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
14778 new_x = x + glyph->pixel_width;
14779
14780 if (/* Lines are continued. */
14781 !it->truncate_lines_p
14782 && (/* Glyph doesn't fit on the line. */
14783 new_x > it->last_visible_x
14784 /* Or it fits exactly on a window system frame. */
14785 || (new_x == it->last_visible_x
14786 && FRAME_WINDOW_P (it->f))))
14787 {
14788 /* End of a continued line. */
14789
14790 if (it->hpos == 0
14791 || (new_x == it->last_visible_x
14792 && FRAME_WINDOW_P (it->f)))
14793 {
14794 /* Current glyph is the only one on the line or
14795 fits exactly on the line. We must continue
14796 the line because we can't draw the cursor
14797 after the glyph. */
14798 row->continued_p = 1;
14799 it->current_x = new_x;
14800 it->continuation_lines_width += new_x;
14801 ++it->hpos;
14802 if (i == nglyphs - 1)
14803 {
14804 set_iterator_to_next (it, 1);
14805 #ifdef HAVE_WINDOW_SYSTEM
14806 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
14807 {
14808 if (!get_next_display_element (it))
14809 {
14810 row->exact_window_width_line_p = 1;
14811 it->continuation_lines_width = 0;
14812 row->continued_p = 0;
14813 row->ends_at_zv_p = 1;
14814 }
14815 else if (ITERATOR_AT_END_OF_LINE_P (it))
14816 {
14817 row->continued_p = 0;
14818 row->exact_window_width_line_p = 1;
14819 }
14820 }
14821 #endif /* HAVE_WINDOW_SYSTEM */
14822 }
14823 }
14824 else if (CHAR_GLYPH_PADDING_P (*glyph)
14825 && !FRAME_WINDOW_P (it->f))
14826 {
14827 /* A padding glyph that doesn't fit on this line.
14828 This means the whole character doesn't fit
14829 on the line. */
14830 row->used[TEXT_AREA] = n_glyphs_before;
14831
14832 /* Fill the rest of the row with continuation
14833 glyphs like in 20.x. */
14834 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
14835 < row->glyphs[1 + TEXT_AREA])
14836 produce_special_glyphs (it, IT_CONTINUATION);
14837
14838 row->continued_p = 1;
14839 it->current_x = x_before;
14840 it->continuation_lines_width += x_before;
14841
14842 /* Restore the height to what it was before the
14843 element not fitting on the line. */
14844 it->max_ascent = ascent;
14845 it->max_descent = descent;
14846 it->max_phys_ascent = phys_ascent;
14847 it->max_phys_descent = phys_descent;
14848 }
14849 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
14850 {
14851 /* A TAB that extends past the right edge of the
14852 window. This produces a single glyph on
14853 window system frames. We leave the glyph in
14854 this row and let it fill the row, but don't
14855 consume the TAB. */
14856 it->continuation_lines_width += it->last_visible_x;
14857 row->ends_in_middle_of_char_p = 1;
14858 row->continued_p = 1;
14859 glyph->pixel_width = it->last_visible_x - x;
14860 it->starts_in_middle_of_char_p = 1;
14861 }
14862 else
14863 {
14864 /* Something other than a TAB that draws past
14865 the right edge of the window. Restore
14866 positions to values before the element. */
14867 row->used[TEXT_AREA] = n_glyphs_before + i;
14868
14869 /* Display continuation glyphs. */
14870 if (!FRAME_WINDOW_P (it->f))
14871 produce_special_glyphs (it, IT_CONTINUATION);
14872 row->continued_p = 1;
14873
14874 it->continuation_lines_width += x;
14875
14876 if (nglyphs > 1 && i > 0)
14877 {
14878 row->ends_in_middle_of_char_p = 1;
14879 it->starts_in_middle_of_char_p = 1;
14880 }
14881
14882 /* Restore the height to what it was before the
14883 element not fitting on the line. */
14884 it->max_ascent = ascent;
14885 it->max_descent = descent;
14886 it->max_phys_ascent = phys_ascent;
14887 it->max_phys_descent = phys_descent;
14888 }
14889
14890 break;
14891 }
14892 else if (new_x > it->first_visible_x)
14893 {
14894 /* Increment number of glyphs actually displayed. */
14895 ++it->hpos;
14896
14897 if (x < it->first_visible_x)
14898 /* Glyph is partially visible, i.e. row starts at
14899 negative X position. */
14900 row->x = x - it->first_visible_x;
14901 }
14902 else
14903 {
14904 /* Glyph is completely off the left margin of the
14905 window. This should not happen because of the
14906 move_it_in_display_line at the start of this
14907 function, unless the text display area of the
14908 window is empty. */
14909 xassert (it->first_visible_x <= it->last_visible_x);
14910 }
14911 }
14912
14913 row->ascent = max (row->ascent, it->max_ascent);
14914 row->height = max (row->height, it->max_ascent + it->max_descent);
14915 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
14916 row->phys_height = max (row->phys_height,
14917 it->max_phys_ascent + it->max_phys_descent);
14918 row->extra_line_spacing = max (row->extra_line_spacing,
14919 it->max_extra_line_spacing);
14920
14921 /* End of this display line if row is continued. */
14922 if (row->continued_p || row->ends_at_zv_p)
14923 break;
14924 }
14925
14926 at_end_of_line:
14927 /* Is this a line end? If yes, we're also done, after making
14928 sure that a non-default face is extended up to the right
14929 margin of the window. */
14930 if (ITERATOR_AT_END_OF_LINE_P (it))
14931 {
14932 int used_before = row->used[TEXT_AREA];
14933
14934 row->ends_in_newline_from_string_p = STRINGP (it->object);
14935
14936 #ifdef HAVE_WINDOW_SYSTEM
14937 /* Add a space at the end of the line that is used to
14938 display the cursor there. */
14939 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
14940 append_space_for_newline (it, 0);
14941 #endif /* HAVE_WINDOW_SYSTEM */
14942
14943 /* Extend the face to the end of the line. */
14944 extend_face_to_end_of_line (it);
14945
14946 /* Make sure we have the position. */
14947 if (used_before == 0)
14948 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
14949
14950 /* Consume the line end. This skips over invisible lines. */
14951 set_iterator_to_next (it, 1);
14952 it->continuation_lines_width = 0;
14953 break;
14954 }
14955
14956 /* Proceed with next display element. Note that this skips
14957 over lines invisible because of selective display. */
14958 set_iterator_to_next (it, 1);
14959
14960 /* If we truncate lines, we are done when the last displayed
14961 glyphs reach past the right margin of the window. */
14962 if (it->truncate_lines_p
14963 && (FRAME_WINDOW_P (it->f)
14964 ? (it->current_x >= it->last_visible_x)
14965 : (it->current_x > it->last_visible_x)))
14966 {
14967 /* Maybe add truncation glyphs. */
14968 if (!FRAME_WINDOW_P (it->f))
14969 {
14970 int i, n;
14971
14972 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
14973 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
14974 break;
14975
14976 for (n = row->used[TEXT_AREA]; i < n; ++i)
14977 {
14978 row->used[TEXT_AREA] = i;
14979 produce_special_glyphs (it, IT_TRUNCATION);
14980 }
14981 }
14982 #ifdef HAVE_WINDOW_SYSTEM
14983 else
14984 {
14985 /* Don't truncate if we can overflow newline into fringe. */
14986 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
14987 {
14988 if (!get_next_display_element (it))
14989 {
14990 it->continuation_lines_width = 0;
14991 row->ends_at_zv_p = 1;
14992 row->exact_window_width_line_p = 1;
14993 break;
14994 }
14995 if (ITERATOR_AT_END_OF_LINE_P (it))
14996 {
14997 row->exact_window_width_line_p = 1;
14998 goto at_end_of_line;
14999 }
15000 }
15001 }
15002 #endif /* HAVE_WINDOW_SYSTEM */
15003
15004 row->truncated_on_right_p = 1;
15005 it->continuation_lines_width = 0;
15006 reseat_at_next_visible_line_start (it, 0);
15007 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
15008 it->hpos = hpos_before;
15009 it->current_x = x_before;
15010 break;
15011 }
15012 }
15013
15014 /* If line is not empty and hscrolled, maybe insert truncation glyphs
15015 at the left window margin. */
15016 if (it->first_visible_x
15017 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
15018 {
15019 if (!FRAME_WINDOW_P (it->f))
15020 insert_left_trunc_glyphs (it);
15021 row->truncated_on_left_p = 1;
15022 }
15023
15024 /* If the start of this line is the overlay arrow-position, then
15025 mark this glyph row as the one containing the overlay arrow.
15026 This is clearly a mess with variable size fonts. It would be
15027 better to let it be displayed like cursors under X. */
15028 if (! overlay_arrow_seen
15029 && (overlay_arrow_string
15030 = overlay_arrow_at_row (it, row, &overlay_arrow_bitmap),
15031 !NILP (overlay_arrow_string)))
15032 {
15033 /* Overlay arrow in window redisplay is a fringe bitmap. */
15034 if (STRINGP (overlay_arrow_string))
15035 {
15036 struct glyph_row *arrow_row
15037 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
15038 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
15039 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
15040 struct glyph *p = row->glyphs[TEXT_AREA];
15041 struct glyph *p2, *end;
15042
15043 /* Copy the arrow glyphs. */
15044 while (glyph < arrow_end)
15045 *p++ = *glyph++;
15046
15047 /* Throw away padding glyphs. */
15048 p2 = p;
15049 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
15050 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
15051 ++p2;
15052 if (p2 > p)
15053 {
15054 while (p2 < end)
15055 *p++ = *p2++;
15056 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
15057 }
15058 }
15059 else
15060 {
15061 it->w->overlay_arrow_bitmap = overlay_arrow_bitmap;
15062 row->overlay_arrow_p = 1;
15063 }
15064 overlay_arrow_seen = 1;
15065 }
15066
15067 /* Compute pixel dimensions of this line. */
15068 compute_line_metrics (it);
15069
15070 /* Remember the position at which this line ends. */
15071 row->end = it->current;
15072
15073 /* Save fringe bitmaps in this row. */
15074 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
15075 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
15076 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
15077 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
15078
15079 it->left_user_fringe_bitmap = 0;
15080 it->left_user_fringe_face_id = 0;
15081 it->right_user_fringe_bitmap = 0;
15082 it->right_user_fringe_face_id = 0;
15083
15084 /* Maybe set the cursor. */
15085 if (it->w->cursor.vpos < 0
15086 && PT >= MATRIX_ROW_START_CHARPOS (row)
15087 && PT <= MATRIX_ROW_END_CHARPOS (row)
15088 && cursor_row_p (it->w, row))
15089 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
15090
15091 /* Highlight trailing whitespace. */
15092 if (!NILP (Vshow_trailing_whitespace))
15093 highlight_trailing_whitespace (it->f, it->glyph_row);
15094
15095 /* Prepare for the next line. This line starts horizontally at (X
15096 HPOS) = (0 0). Vertical positions are incremented. As a
15097 convenience for the caller, IT->glyph_row is set to the next
15098 row to be used. */
15099 it->current_x = it->hpos = 0;
15100 it->current_y += row->height;
15101 ++it->vpos;
15102 ++it->glyph_row;
15103 it->start = it->current;
15104 return row->displays_text_p;
15105 }
15106
15107
15108 \f
15109 /***********************************************************************
15110 Menu Bar
15111 ***********************************************************************/
15112
15113 /* Redisplay the menu bar in the frame for window W.
15114
15115 The menu bar of X frames that don't have X toolkit support is
15116 displayed in a special window W->frame->menu_bar_window.
15117
15118 The menu bar of terminal frames is treated specially as far as
15119 glyph matrices are concerned. Menu bar lines are not part of
15120 windows, so the update is done directly on the frame matrix rows
15121 for the menu bar. */
15122
15123 static void
15124 display_menu_bar (w)
15125 struct window *w;
15126 {
15127 struct frame *f = XFRAME (WINDOW_FRAME (w));
15128 struct it it;
15129 Lisp_Object items;
15130 int i;
15131
15132 /* Don't do all this for graphical frames. */
15133 #ifdef HAVE_NTGUI
15134 if (!NILP (Vwindow_system))
15135 return;
15136 #endif
15137 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
15138 if (FRAME_X_P (f))
15139 return;
15140 #endif
15141 #ifdef MAC_OS
15142 if (FRAME_MAC_P (f))
15143 return;
15144 #endif
15145
15146 #ifdef USE_X_TOOLKIT
15147 xassert (!FRAME_WINDOW_P (f));
15148 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
15149 it.first_visible_x = 0;
15150 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
15151 #else /* not USE_X_TOOLKIT */
15152 if (FRAME_WINDOW_P (f))
15153 {
15154 /* Menu bar lines are displayed in the desired matrix of the
15155 dummy window menu_bar_window. */
15156 struct window *menu_w;
15157 xassert (WINDOWP (f->menu_bar_window));
15158 menu_w = XWINDOW (f->menu_bar_window);
15159 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
15160 MENU_FACE_ID);
15161 it.first_visible_x = 0;
15162 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
15163 }
15164 else
15165 {
15166 /* This is a TTY frame, i.e. character hpos/vpos are used as
15167 pixel x/y. */
15168 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
15169 MENU_FACE_ID);
15170 it.first_visible_x = 0;
15171 it.last_visible_x = FRAME_COLS (f);
15172 }
15173 #endif /* not USE_X_TOOLKIT */
15174
15175 if (! mode_line_inverse_video)
15176 /* Force the menu-bar to be displayed in the default face. */
15177 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
15178
15179 /* Clear all rows of the menu bar. */
15180 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
15181 {
15182 struct glyph_row *row = it.glyph_row + i;
15183 clear_glyph_row (row);
15184 row->enabled_p = 1;
15185 row->full_width_p = 1;
15186 }
15187
15188 /* Display all items of the menu bar. */
15189 items = FRAME_MENU_BAR_ITEMS (it.f);
15190 for (i = 0; i < XVECTOR (items)->size; i += 4)
15191 {
15192 Lisp_Object string;
15193
15194 /* Stop at nil string. */
15195 string = AREF (items, i + 1);
15196 if (NILP (string))
15197 break;
15198
15199 /* Remember where item was displayed. */
15200 AREF (items, i + 3) = make_number (it.hpos);
15201
15202 /* Display the item, pad with one space. */
15203 if (it.current_x < it.last_visible_x)
15204 display_string (NULL, string, Qnil, 0, 0, &it,
15205 SCHARS (string) + 1, 0, 0, -1);
15206 }
15207
15208 /* Fill out the line with spaces. */
15209 if (it.current_x < it.last_visible_x)
15210 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
15211
15212 /* Compute the total height of the lines. */
15213 compute_line_metrics (&it);
15214 }
15215
15216
15217 \f
15218 /***********************************************************************
15219 Mode Line
15220 ***********************************************************************/
15221
15222 /* Redisplay mode lines in the window tree whose root is WINDOW. If
15223 FORCE is non-zero, redisplay mode lines unconditionally.
15224 Otherwise, redisplay only mode lines that are garbaged. Value is
15225 the number of windows whose mode lines were redisplayed. */
15226
15227 static int
15228 redisplay_mode_lines (window, force)
15229 Lisp_Object window;
15230 int force;
15231 {
15232 int nwindows = 0;
15233
15234 while (!NILP (window))
15235 {
15236 struct window *w = XWINDOW (window);
15237
15238 if (WINDOWP (w->hchild))
15239 nwindows += redisplay_mode_lines (w->hchild, force);
15240 else if (WINDOWP (w->vchild))
15241 nwindows += redisplay_mode_lines (w->vchild, force);
15242 else if (force
15243 || FRAME_GARBAGED_P (XFRAME (w->frame))
15244 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
15245 {
15246 struct text_pos lpoint;
15247 struct buffer *old = current_buffer;
15248
15249 /* Set the window's buffer for the mode line display. */
15250 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15251 set_buffer_internal_1 (XBUFFER (w->buffer));
15252
15253 /* Point refers normally to the selected window. For any
15254 other window, set up appropriate value. */
15255 if (!EQ (window, selected_window))
15256 {
15257 struct text_pos pt;
15258
15259 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
15260 if (CHARPOS (pt) < BEGV)
15261 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
15262 else if (CHARPOS (pt) > (ZV - 1))
15263 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
15264 else
15265 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
15266 }
15267
15268 /* Display mode lines. */
15269 clear_glyph_matrix (w->desired_matrix);
15270 if (display_mode_lines (w))
15271 {
15272 ++nwindows;
15273 w->must_be_updated_p = 1;
15274 }
15275
15276 /* Restore old settings. */
15277 set_buffer_internal_1 (old);
15278 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
15279 }
15280
15281 window = w->next;
15282 }
15283
15284 return nwindows;
15285 }
15286
15287
15288 /* Display the mode and/or top line of window W. Value is the number
15289 of mode lines displayed. */
15290
15291 static int
15292 display_mode_lines (w)
15293 struct window *w;
15294 {
15295 Lisp_Object old_selected_window, old_selected_frame;
15296 int n = 0;
15297
15298 old_selected_frame = selected_frame;
15299 selected_frame = w->frame;
15300 old_selected_window = selected_window;
15301 XSETWINDOW (selected_window, w);
15302
15303 /* These will be set while the mode line specs are processed. */
15304 line_number_displayed = 0;
15305 w->column_number_displayed = Qnil;
15306
15307 if (WINDOW_WANTS_MODELINE_P (w))
15308 {
15309 struct window *sel_w = XWINDOW (old_selected_window);
15310
15311 /* Select mode line face based on the real selected window. */
15312 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
15313 current_buffer->mode_line_format);
15314 ++n;
15315 }
15316
15317 if (WINDOW_WANTS_HEADER_LINE_P (w))
15318 {
15319 display_mode_line (w, HEADER_LINE_FACE_ID,
15320 current_buffer->header_line_format);
15321 ++n;
15322 }
15323
15324 selected_frame = old_selected_frame;
15325 selected_window = old_selected_window;
15326 return n;
15327 }
15328
15329
15330 /* Display mode or top line of window W. FACE_ID specifies which line
15331 to display; it is either MODE_LINE_FACE_ID or HEADER_LINE_FACE_ID.
15332 FORMAT is the mode line format to display. Value is the pixel
15333 height of the mode line displayed. */
15334
15335 static int
15336 display_mode_line (w, face_id, format)
15337 struct window *w;
15338 enum face_id face_id;
15339 Lisp_Object format;
15340 {
15341 struct it it;
15342 struct face *face;
15343
15344 init_iterator (&it, w, -1, -1, NULL, face_id);
15345 prepare_desired_row (it.glyph_row);
15346
15347 it.glyph_row->mode_line_p = 1;
15348
15349 if (! mode_line_inverse_video)
15350 /* Force the mode-line to be displayed in the default face. */
15351 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
15352
15353 /* Temporarily make frame's keyboard the current kboard so that
15354 kboard-local variables in the mode_line_format will get the right
15355 values. */
15356 push_frame_kboard (it.f);
15357 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
15358 pop_frame_kboard ();
15359
15360 /* Fill up with spaces. */
15361 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
15362
15363 compute_line_metrics (&it);
15364 it.glyph_row->full_width_p = 1;
15365 it.glyph_row->continued_p = 0;
15366 it.glyph_row->truncated_on_left_p = 0;
15367 it.glyph_row->truncated_on_right_p = 0;
15368
15369 /* Make a 3D mode-line have a shadow at its right end. */
15370 face = FACE_FROM_ID (it.f, face_id);
15371 extend_face_to_end_of_line (&it);
15372 if (face->box != FACE_NO_BOX)
15373 {
15374 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
15375 + it.glyph_row->used[TEXT_AREA] - 1);
15376 last->right_box_line_p = 1;
15377 }
15378
15379 return it.glyph_row->height;
15380 }
15381
15382 /* Alist that caches the results of :propertize.
15383 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
15384 Lisp_Object mode_line_proptrans_alist;
15385
15386 /* List of strings making up the mode-line. */
15387 Lisp_Object mode_line_string_list;
15388
15389 /* Base face property when building propertized mode line string. */
15390 static Lisp_Object mode_line_string_face;
15391 static Lisp_Object mode_line_string_face_prop;
15392
15393
15394 /* Contribute ELT to the mode line for window IT->w. How it
15395 translates into text depends on its data type.
15396
15397 IT describes the display environment in which we display, as usual.
15398
15399 DEPTH is the depth in recursion. It is used to prevent
15400 infinite recursion here.
15401
15402 FIELD_WIDTH is the number of characters the display of ELT should
15403 occupy in the mode line, and PRECISION is the maximum number of
15404 characters to display from ELT's representation. See
15405 display_string for details.
15406
15407 Returns the hpos of the end of the text generated by ELT.
15408
15409 PROPS is a property list to add to any string we encounter.
15410
15411 If RISKY is nonzero, remove (disregard) any properties in any string
15412 we encounter, and ignore :eval and :propertize.
15413
15414 If the global variable `frame_title_ptr' is non-NULL, then the output
15415 is passed to `store_frame_title' instead of `display_string'. */
15416
15417 static int
15418 display_mode_element (it, depth, field_width, precision, elt, props, risky)
15419 struct it *it;
15420 int depth;
15421 int field_width, precision;
15422 Lisp_Object elt, props;
15423 int risky;
15424 {
15425 int n = 0, field, prec;
15426 int literal = 0;
15427
15428 tail_recurse:
15429 if (depth > 100)
15430 elt = build_string ("*too-deep*");
15431
15432 depth++;
15433
15434 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
15435 {
15436 case Lisp_String:
15437 {
15438 /* A string: output it and check for %-constructs within it. */
15439 unsigned char c;
15440 const unsigned char *this, *lisp_string;
15441
15442 if (!NILP (props) || risky)
15443 {
15444 Lisp_Object oprops, aelt;
15445 oprops = Ftext_properties_at (make_number (0), elt);
15446
15447 /* If the starting string's properties are not what
15448 we want, translate the string. Also, if the string
15449 is risky, do that anyway. */
15450
15451 if (NILP (Fequal (props, oprops)) || risky)
15452 {
15453 /* If the starting string has properties,
15454 merge the specified ones onto the existing ones. */
15455 if (! NILP (oprops) && !risky)
15456 {
15457 Lisp_Object tem;
15458
15459 oprops = Fcopy_sequence (oprops);
15460 tem = props;
15461 while (CONSP (tem))
15462 {
15463 oprops = Fplist_put (oprops, XCAR (tem),
15464 XCAR (XCDR (tem)));
15465 tem = XCDR (XCDR (tem));
15466 }
15467 props = oprops;
15468 }
15469
15470 aelt = Fassoc (elt, mode_line_proptrans_alist);
15471 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
15472 {
15473 mode_line_proptrans_alist
15474 = Fcons (aelt, Fdelq (aelt, mode_line_proptrans_alist));
15475 elt = XCAR (aelt);
15476 }
15477 else
15478 {
15479 Lisp_Object tem;
15480
15481 elt = Fcopy_sequence (elt);
15482 Fset_text_properties (make_number (0), Flength (elt),
15483 props, elt);
15484 /* Add this item to mode_line_proptrans_alist. */
15485 mode_line_proptrans_alist
15486 = Fcons (Fcons (elt, props),
15487 mode_line_proptrans_alist);
15488 /* Truncate mode_line_proptrans_alist
15489 to at most 50 elements. */
15490 tem = Fnthcdr (make_number (50),
15491 mode_line_proptrans_alist);
15492 if (! NILP (tem))
15493 XSETCDR (tem, Qnil);
15494 }
15495 }
15496 }
15497
15498 this = SDATA (elt);
15499 lisp_string = this;
15500
15501 if (literal)
15502 {
15503 prec = precision - n;
15504 if (frame_title_ptr)
15505 n += store_frame_title (SDATA (elt), -1, prec);
15506 else if (!NILP (mode_line_string_list))
15507 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
15508 else
15509 n += display_string (NULL, elt, Qnil, 0, 0, it,
15510 0, prec, 0, STRING_MULTIBYTE (elt));
15511
15512 break;
15513 }
15514
15515 while ((precision <= 0 || n < precision)
15516 && *this
15517 && (frame_title_ptr
15518 || !NILP (mode_line_string_list)
15519 || it->current_x < it->last_visible_x))
15520 {
15521 const unsigned char *last = this;
15522
15523 /* Advance to end of string or next format specifier. */
15524 while ((c = *this++) != '\0' && c != '%')
15525 ;
15526
15527 if (this - 1 != last)
15528 {
15529 int nchars, nbytes;
15530
15531 /* Output to end of string or up to '%'. Field width
15532 is length of string. Don't output more than
15533 PRECISION allows us. */
15534 --this;
15535
15536 prec = c_string_width (last, this - last, precision - n,
15537 &nchars, &nbytes);
15538
15539 if (frame_title_ptr)
15540 n += store_frame_title (last, 0, prec);
15541 else if (!NILP (mode_line_string_list))
15542 {
15543 int bytepos = last - lisp_string;
15544 int charpos = string_byte_to_char (elt, bytepos);
15545 int endpos = (precision <= 0
15546 ? string_byte_to_char (elt,
15547 this - lisp_string)
15548 : charpos + nchars);
15549
15550 n += store_mode_line_string (NULL,
15551 Fsubstring (elt, make_number (charpos),
15552 make_number (endpos)),
15553 0, 0, 0, Qnil);
15554 }
15555 else
15556 {
15557 int bytepos = last - lisp_string;
15558 int charpos = string_byte_to_char (elt, bytepos);
15559 n += display_string (NULL, elt, Qnil, 0, charpos,
15560 it, 0, prec, 0,
15561 STRING_MULTIBYTE (elt));
15562 }
15563 }
15564 else /* c == '%' */
15565 {
15566 const unsigned char *percent_position = this;
15567
15568 /* Get the specified minimum width. Zero means
15569 don't pad. */
15570 field = 0;
15571 while ((c = *this++) >= '0' && c <= '9')
15572 field = field * 10 + c - '0';
15573
15574 /* Don't pad beyond the total padding allowed. */
15575 if (field_width - n > 0 && field > field_width - n)
15576 field = field_width - n;
15577
15578 /* Note that either PRECISION <= 0 or N < PRECISION. */
15579 prec = precision - n;
15580
15581 if (c == 'M')
15582 n += display_mode_element (it, depth, field, prec,
15583 Vglobal_mode_string, props,
15584 risky);
15585 else if (c != 0)
15586 {
15587 int multibyte;
15588 int bytepos, charpos;
15589 unsigned char *spec;
15590
15591 bytepos = percent_position - lisp_string;
15592 charpos = (STRING_MULTIBYTE (elt)
15593 ? string_byte_to_char (elt, bytepos)
15594 : bytepos);
15595
15596 spec
15597 = decode_mode_spec (it->w, c, field, prec, &multibyte);
15598
15599 if (frame_title_ptr)
15600 n += store_frame_title (spec, field, prec);
15601 else if (!NILP (mode_line_string_list))
15602 {
15603 int len = strlen (spec);
15604 Lisp_Object tem = make_string (spec, len);
15605 props = Ftext_properties_at (make_number (charpos), elt);
15606 /* Should only keep face property in props */
15607 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
15608 }
15609 else
15610 {
15611 int nglyphs_before, nwritten;
15612
15613 nglyphs_before = it->glyph_row->used[TEXT_AREA];
15614 nwritten = display_string (spec, Qnil, elt,
15615 charpos, 0, it,
15616 field, prec, 0,
15617 multibyte);
15618
15619 /* Assign to the glyphs written above the
15620 string where the `%x' came from, position
15621 of the `%'. */
15622 if (nwritten > 0)
15623 {
15624 struct glyph *glyph
15625 = (it->glyph_row->glyphs[TEXT_AREA]
15626 + nglyphs_before);
15627 int i;
15628
15629 for (i = 0; i < nwritten; ++i)
15630 {
15631 glyph[i].object = elt;
15632 glyph[i].charpos = charpos;
15633 }
15634
15635 n += nwritten;
15636 }
15637 }
15638 }
15639 else /* c == 0 */
15640 break;
15641 }
15642 }
15643 }
15644 break;
15645
15646 case Lisp_Symbol:
15647 /* A symbol: process the value of the symbol recursively
15648 as if it appeared here directly. Avoid error if symbol void.
15649 Special case: if value of symbol is a string, output the string
15650 literally. */
15651 {
15652 register Lisp_Object tem;
15653
15654 /* If the variable is not marked as risky to set
15655 then its contents are risky to use. */
15656 if (NILP (Fget (elt, Qrisky_local_variable)))
15657 risky = 1;
15658
15659 tem = Fboundp (elt);
15660 if (!NILP (tem))
15661 {
15662 tem = Fsymbol_value (elt);
15663 /* If value is a string, output that string literally:
15664 don't check for % within it. */
15665 if (STRINGP (tem))
15666 literal = 1;
15667
15668 if (!EQ (tem, elt))
15669 {
15670 /* Give up right away for nil or t. */
15671 elt = tem;
15672 goto tail_recurse;
15673 }
15674 }
15675 }
15676 break;
15677
15678 case Lisp_Cons:
15679 {
15680 register Lisp_Object car, tem;
15681
15682 /* A cons cell: five distinct cases.
15683 If first element is :eval or :propertize, do something special.
15684 If first element is a string or a cons, process all the elements
15685 and effectively concatenate them.
15686 If first element is a negative number, truncate displaying cdr to
15687 at most that many characters. If positive, pad (with spaces)
15688 to at least that many characters.
15689 If first element is a symbol, process the cadr or caddr recursively
15690 according to whether the symbol's value is non-nil or nil. */
15691 car = XCAR (elt);
15692 if (EQ (car, QCeval))
15693 {
15694 /* An element of the form (:eval FORM) means evaluate FORM
15695 and use the result as mode line elements. */
15696
15697 if (risky)
15698 break;
15699
15700 if (CONSP (XCDR (elt)))
15701 {
15702 Lisp_Object spec;
15703 spec = safe_eval (XCAR (XCDR (elt)));
15704 n += display_mode_element (it, depth, field_width - n,
15705 precision - n, spec, props,
15706 risky);
15707 }
15708 }
15709 else if (EQ (car, QCpropertize))
15710 {
15711 /* An element of the form (:propertize ELT PROPS...)
15712 means display ELT but applying properties PROPS. */
15713
15714 if (risky)
15715 break;
15716
15717 if (CONSP (XCDR (elt)))
15718 n += display_mode_element (it, depth, field_width - n,
15719 precision - n, XCAR (XCDR (elt)),
15720 XCDR (XCDR (elt)), risky);
15721 }
15722 else if (SYMBOLP (car))
15723 {
15724 tem = Fboundp (car);
15725 elt = XCDR (elt);
15726 if (!CONSP (elt))
15727 goto invalid;
15728 /* elt is now the cdr, and we know it is a cons cell.
15729 Use its car if CAR has a non-nil value. */
15730 if (!NILP (tem))
15731 {
15732 tem = Fsymbol_value (car);
15733 if (!NILP (tem))
15734 {
15735 elt = XCAR (elt);
15736 goto tail_recurse;
15737 }
15738 }
15739 /* Symbol's value is nil (or symbol is unbound)
15740 Get the cddr of the original list
15741 and if possible find the caddr and use that. */
15742 elt = XCDR (elt);
15743 if (NILP (elt))
15744 break;
15745 else if (!CONSP (elt))
15746 goto invalid;
15747 elt = XCAR (elt);
15748 goto tail_recurse;
15749 }
15750 else if (INTEGERP (car))
15751 {
15752 register int lim = XINT (car);
15753 elt = XCDR (elt);
15754 if (lim < 0)
15755 {
15756 /* Negative int means reduce maximum width. */
15757 if (precision <= 0)
15758 precision = -lim;
15759 else
15760 precision = min (precision, -lim);
15761 }
15762 else if (lim > 0)
15763 {
15764 /* Padding specified. Don't let it be more than
15765 current maximum. */
15766 if (precision > 0)
15767 lim = min (precision, lim);
15768
15769 /* If that's more padding than already wanted, queue it.
15770 But don't reduce padding already specified even if
15771 that is beyond the current truncation point. */
15772 field_width = max (lim, field_width);
15773 }
15774 goto tail_recurse;
15775 }
15776 else if (STRINGP (car) || CONSP (car))
15777 {
15778 register int limit = 50;
15779 /* Limit is to protect against circular lists. */
15780 while (CONSP (elt)
15781 && --limit > 0
15782 && (precision <= 0 || n < precision))
15783 {
15784 n += display_mode_element (it, depth, field_width - n,
15785 precision - n, XCAR (elt),
15786 props, risky);
15787 elt = XCDR (elt);
15788 }
15789 }
15790 }
15791 break;
15792
15793 default:
15794 invalid:
15795 elt = build_string ("*invalid*");
15796 goto tail_recurse;
15797 }
15798
15799 /* Pad to FIELD_WIDTH. */
15800 if (field_width > 0 && n < field_width)
15801 {
15802 if (frame_title_ptr)
15803 n += store_frame_title ("", field_width - n, 0);
15804 else if (!NILP (mode_line_string_list))
15805 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
15806 else
15807 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
15808 0, 0, 0);
15809 }
15810
15811 return n;
15812 }
15813
15814 /* Store a mode-line string element in mode_line_string_list.
15815
15816 If STRING is non-null, display that C string. Otherwise, the Lisp
15817 string LISP_STRING is displayed.
15818
15819 FIELD_WIDTH is the minimum number of output glyphs to produce.
15820 If STRING has fewer characters than FIELD_WIDTH, pad to the right
15821 with spaces. FIELD_WIDTH <= 0 means don't pad.
15822
15823 PRECISION is the maximum number of characters to output from
15824 STRING. PRECISION <= 0 means don't truncate the string.
15825
15826 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
15827 properties to the string.
15828
15829 PROPS are the properties to add to the string.
15830 The mode_line_string_face face property is always added to the string.
15831 */
15832
15833 static int
15834 store_mode_line_string (string, lisp_string, copy_string, field_width, precision, props)
15835 char *string;
15836 Lisp_Object lisp_string;
15837 int copy_string;
15838 int field_width;
15839 int precision;
15840 Lisp_Object props;
15841 {
15842 int len;
15843 int n = 0;
15844
15845 if (string != NULL)
15846 {
15847 len = strlen (string);
15848 if (precision > 0 && len > precision)
15849 len = precision;
15850 lisp_string = make_string (string, len);
15851 if (NILP (props))
15852 props = mode_line_string_face_prop;
15853 else if (!NILP (mode_line_string_face))
15854 {
15855 Lisp_Object face = Fsafe_plist_get (props, Qface);
15856 props = Fcopy_sequence (props);
15857 if (NILP (face))
15858 face = mode_line_string_face;
15859 else
15860 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
15861 props = Fplist_put (props, Qface, face);
15862 }
15863 Fadd_text_properties (make_number (0), make_number (len),
15864 props, lisp_string);
15865 }
15866 else
15867 {
15868 len = XFASTINT (Flength (lisp_string));
15869 if (precision > 0 && len > precision)
15870 {
15871 len = precision;
15872 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
15873 precision = -1;
15874 }
15875 if (!NILP (mode_line_string_face))
15876 {
15877 Lisp_Object face;
15878 if (NILP (props))
15879 props = Ftext_properties_at (make_number (0), lisp_string);
15880 face = Fsafe_plist_get (props, Qface);
15881 if (NILP (face))
15882 face = mode_line_string_face;
15883 else
15884 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
15885 props = Fcons (Qface, Fcons (face, Qnil));
15886 if (copy_string)
15887 lisp_string = Fcopy_sequence (lisp_string);
15888 }
15889 if (!NILP (props))
15890 Fadd_text_properties (make_number (0), make_number (len),
15891 props, lisp_string);
15892 }
15893
15894 if (len > 0)
15895 {
15896 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
15897 n += len;
15898 }
15899
15900 if (field_width > len)
15901 {
15902 field_width -= len;
15903 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
15904 if (!NILP (props))
15905 Fadd_text_properties (make_number (0), make_number (field_width),
15906 props, lisp_string);
15907 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
15908 n += field_width;
15909 }
15910
15911 return n;
15912 }
15913
15914
15915 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
15916 0, 4, 0,
15917 doc: /* Return the mode-line of selected window as a string.
15918 First optional arg FORMAT specifies a different format string (see
15919 `mode-line-format' for details) to use. If FORMAT is t, return
15920 the buffer's header-line. Second optional arg WINDOW specifies a
15921 different window to use as the context for the formatting.
15922 If third optional arg NO-PROPS is non-nil, string is not propertized.
15923 Fourth optional arg BUFFER specifies which buffer to use. */)
15924 (format, window, no_props, buffer)
15925 Lisp_Object format, window, no_props, buffer;
15926 {
15927 struct it it;
15928 int len;
15929 struct window *w;
15930 struct buffer *old_buffer = NULL;
15931 enum face_id face_id = DEFAULT_FACE_ID;
15932
15933 if (NILP (window))
15934 window = selected_window;
15935 CHECK_WINDOW (window);
15936 w = XWINDOW (window);
15937
15938 if (NILP (buffer))
15939 buffer = w->buffer;
15940
15941 CHECK_BUFFER (buffer);
15942
15943 if (XBUFFER (buffer) != current_buffer)
15944 {
15945 old_buffer = current_buffer;
15946 set_buffer_internal_1 (XBUFFER (buffer));
15947 }
15948
15949 if (NILP (format) || EQ (format, Qt))
15950 {
15951 face_id = (NILP (format)
15952 ? CURRENT_MODE_LINE_FACE_ID (w)
15953 : HEADER_LINE_FACE_ID);
15954 format = (NILP (format)
15955 ? current_buffer->mode_line_format
15956 : current_buffer->header_line_format);
15957 }
15958
15959 init_iterator (&it, w, -1, -1, NULL, face_id);
15960
15961 if (NILP (no_props))
15962 {
15963 mode_line_string_face
15964 = (face_id == MODE_LINE_FACE_ID ? Qmode_line
15965 : face_id == MODE_LINE_INACTIVE_FACE_ID ? Qmode_line_inactive
15966 : face_id == HEADER_LINE_FACE_ID ? Qheader_line : Qnil);
15967
15968 mode_line_string_face_prop
15969 = (NILP (mode_line_string_face) ? Qnil
15970 : Fcons (Qface, Fcons (mode_line_string_face, Qnil)));
15971
15972 /* We need a dummy last element in mode_line_string_list to
15973 indicate we are building the propertized mode-line string.
15974 Using mode_line_string_face_prop here GC protects it. */
15975 mode_line_string_list
15976 = Fcons (mode_line_string_face_prop, Qnil);
15977 frame_title_ptr = NULL;
15978 }
15979 else
15980 {
15981 mode_line_string_face_prop = Qnil;
15982 mode_line_string_list = Qnil;
15983 frame_title_ptr = frame_title_buf;
15984 }
15985
15986 push_frame_kboard (it.f);
15987 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
15988 pop_frame_kboard ();
15989
15990 if (old_buffer)
15991 set_buffer_internal_1 (old_buffer);
15992
15993 if (NILP (no_props))
15994 {
15995 Lisp_Object str;
15996 mode_line_string_list = Fnreverse (mode_line_string_list);
15997 str = Fmapconcat (intern ("identity"), XCDR (mode_line_string_list),
15998 make_string ("", 0));
15999 mode_line_string_face_prop = Qnil;
16000 mode_line_string_list = Qnil;
16001 return str;
16002 }
16003
16004 len = frame_title_ptr - frame_title_buf;
16005 if (len > 0 && frame_title_ptr[-1] == '-')
16006 {
16007 /* Mode lines typically ends with numerous dashes; reduce to two dashes. */
16008 while (frame_title_ptr > frame_title_buf && *--frame_title_ptr == '-')
16009 ;
16010 frame_title_ptr += 3; /* restore last non-dash + two dashes */
16011 if (len > frame_title_ptr - frame_title_buf)
16012 len = frame_title_ptr - frame_title_buf;
16013 }
16014
16015 frame_title_ptr = NULL;
16016 return make_string (frame_title_buf, len);
16017 }
16018
16019 /* Write a null-terminated, right justified decimal representation of
16020 the positive integer D to BUF using a minimal field width WIDTH. */
16021
16022 static void
16023 pint2str (buf, width, d)
16024 register char *buf;
16025 register int width;
16026 register int d;
16027 {
16028 register char *p = buf;
16029
16030 if (d <= 0)
16031 *p++ = '0';
16032 else
16033 {
16034 while (d > 0)
16035 {
16036 *p++ = d % 10 + '0';
16037 d /= 10;
16038 }
16039 }
16040
16041 for (width -= (int) (p - buf); width > 0; --width)
16042 *p++ = ' ';
16043 *p-- = '\0';
16044 while (p > buf)
16045 {
16046 d = *buf;
16047 *buf++ = *p;
16048 *p-- = d;
16049 }
16050 }
16051
16052 /* Write a null-terminated, right justified decimal and "human
16053 readable" representation of the nonnegative integer D to BUF using
16054 a minimal field width WIDTH. D should be smaller than 999.5e24. */
16055
16056 static const char power_letter[] =
16057 {
16058 0, /* not used */
16059 'k', /* kilo */
16060 'M', /* mega */
16061 'G', /* giga */
16062 'T', /* tera */
16063 'P', /* peta */
16064 'E', /* exa */
16065 'Z', /* zetta */
16066 'Y' /* yotta */
16067 };
16068
16069 static void
16070 pint2hrstr (buf, width, d)
16071 char *buf;
16072 int width;
16073 int d;
16074 {
16075 /* We aim to represent the nonnegative integer D as
16076 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
16077 int quotient = d;
16078 int remainder = 0;
16079 /* -1 means: do not use TENTHS. */
16080 int tenths = -1;
16081 int exponent = 0;
16082
16083 /* Length of QUOTIENT.TENTHS as a string. */
16084 int length;
16085
16086 char * psuffix;
16087 char * p;
16088
16089 if (1000 <= quotient)
16090 {
16091 /* Scale to the appropriate EXPONENT. */
16092 do
16093 {
16094 remainder = quotient % 1000;
16095 quotient /= 1000;
16096 exponent++;
16097 }
16098 while (1000 <= quotient);
16099
16100 /* Round to nearest and decide whether to use TENTHS or not. */
16101 if (quotient <= 9)
16102 {
16103 tenths = remainder / 100;
16104 if (50 <= remainder % 100)
16105 {
16106 if (tenths < 9)
16107 tenths++;
16108 else
16109 {
16110 quotient++;
16111 if (quotient == 10)
16112 tenths = -1;
16113 else
16114 tenths = 0;
16115 }
16116 }
16117 }
16118 else
16119 if (500 <= remainder)
16120 {
16121 if (quotient < 999)
16122 quotient++;
16123 else
16124 {
16125 quotient = 1;
16126 exponent++;
16127 tenths = 0;
16128 }
16129 }
16130 }
16131
16132 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
16133 if (tenths == -1 && quotient <= 99)
16134 if (quotient <= 9)
16135 length = 1;
16136 else
16137 length = 2;
16138 else
16139 length = 3;
16140 p = psuffix = buf + max (width, length);
16141
16142 /* Print EXPONENT. */
16143 if (exponent)
16144 *psuffix++ = power_letter[exponent];
16145 *psuffix = '\0';
16146
16147 /* Print TENTHS. */
16148 if (tenths >= 0)
16149 {
16150 *--p = '0' + tenths;
16151 *--p = '.';
16152 }
16153
16154 /* Print QUOTIENT. */
16155 do
16156 {
16157 int digit = quotient % 10;
16158 *--p = '0' + digit;
16159 }
16160 while ((quotient /= 10) != 0);
16161
16162 /* Print leading spaces. */
16163 while (buf < p)
16164 *--p = ' ';
16165 }
16166
16167 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
16168 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
16169 type of CODING_SYSTEM. Return updated pointer into BUF. */
16170
16171 static unsigned char invalid_eol_type[] = "(*invalid*)";
16172
16173 static char *
16174 decode_mode_spec_coding (coding_system, buf, eol_flag)
16175 Lisp_Object coding_system;
16176 register char *buf;
16177 int eol_flag;
16178 {
16179 Lisp_Object val;
16180 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
16181 const unsigned char *eol_str;
16182 int eol_str_len;
16183 /* The EOL conversion we are using. */
16184 Lisp_Object eoltype;
16185
16186 val = Fget (coding_system, Qcoding_system);
16187 eoltype = Qnil;
16188
16189 if (!VECTORP (val)) /* Not yet decided. */
16190 {
16191 if (multibyte)
16192 *buf++ = '-';
16193 if (eol_flag)
16194 eoltype = eol_mnemonic_undecided;
16195 /* Don't mention EOL conversion if it isn't decided. */
16196 }
16197 else
16198 {
16199 Lisp_Object eolvalue;
16200
16201 eolvalue = Fget (coding_system, Qeol_type);
16202
16203 if (multibyte)
16204 *buf++ = XFASTINT (AREF (val, 1));
16205
16206 if (eol_flag)
16207 {
16208 /* The EOL conversion that is normal on this system. */
16209
16210 if (NILP (eolvalue)) /* Not yet decided. */
16211 eoltype = eol_mnemonic_undecided;
16212 else if (VECTORP (eolvalue)) /* Not yet decided. */
16213 eoltype = eol_mnemonic_undecided;
16214 else /* INTEGERP (eolvalue) -- 0:LF, 1:CRLF, 2:CR */
16215 eoltype = (XFASTINT (eolvalue) == 0
16216 ? eol_mnemonic_unix
16217 : (XFASTINT (eolvalue) == 1
16218 ? eol_mnemonic_dos : eol_mnemonic_mac));
16219 }
16220 }
16221
16222 if (eol_flag)
16223 {
16224 /* Mention the EOL conversion if it is not the usual one. */
16225 if (STRINGP (eoltype))
16226 {
16227 eol_str = SDATA (eoltype);
16228 eol_str_len = SBYTES (eoltype);
16229 }
16230 else if (INTEGERP (eoltype)
16231 && CHAR_VALID_P (XINT (eoltype), 0))
16232 {
16233 unsigned char *tmp = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
16234 eol_str_len = CHAR_STRING (XINT (eoltype), tmp);
16235 eol_str = tmp;
16236 }
16237 else
16238 {
16239 eol_str = invalid_eol_type;
16240 eol_str_len = sizeof (invalid_eol_type) - 1;
16241 }
16242 bcopy (eol_str, buf, eol_str_len);
16243 buf += eol_str_len;
16244 }
16245
16246 return buf;
16247 }
16248
16249 /* Return a string for the output of a mode line %-spec for window W,
16250 generated by character C. PRECISION >= 0 means don't return a
16251 string longer than that value. FIELD_WIDTH > 0 means pad the
16252 string returned with spaces to that value. Return 1 in *MULTIBYTE
16253 if the result is multibyte text.
16254
16255 Note we operate on the current buffer for most purposes,
16256 the exception being w->base_line_pos. */
16257
16258 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
16259
16260 static char *
16261 decode_mode_spec (w, c, field_width, precision, multibyte)
16262 struct window *w;
16263 register int c;
16264 int field_width, precision;
16265 int *multibyte;
16266 {
16267 Lisp_Object obj;
16268 struct frame *f = XFRAME (WINDOW_FRAME (w));
16269 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
16270 struct buffer *b = current_buffer;
16271
16272 obj = Qnil;
16273 *multibyte = 0;
16274
16275 switch (c)
16276 {
16277 case '*':
16278 if (!NILP (b->read_only))
16279 return "%";
16280 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
16281 return "*";
16282 return "-";
16283
16284 case '+':
16285 /* This differs from %* only for a modified read-only buffer. */
16286 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
16287 return "*";
16288 if (!NILP (b->read_only))
16289 return "%";
16290 return "-";
16291
16292 case '&':
16293 /* This differs from %* in ignoring read-only-ness. */
16294 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
16295 return "*";
16296 return "-";
16297
16298 case '%':
16299 return "%";
16300
16301 case '[':
16302 {
16303 int i;
16304 char *p;
16305
16306 if (command_loop_level > 5)
16307 return "[[[... ";
16308 p = decode_mode_spec_buf;
16309 for (i = 0; i < command_loop_level; i++)
16310 *p++ = '[';
16311 *p = 0;
16312 return decode_mode_spec_buf;
16313 }
16314
16315 case ']':
16316 {
16317 int i;
16318 char *p;
16319
16320 if (command_loop_level > 5)
16321 return " ...]]]";
16322 p = decode_mode_spec_buf;
16323 for (i = 0; i < command_loop_level; i++)
16324 *p++ = ']';
16325 *p = 0;
16326 return decode_mode_spec_buf;
16327 }
16328
16329 case '-':
16330 {
16331 register int i;
16332
16333 /* Let lots_of_dashes be a string of infinite length. */
16334 if (!NILP (mode_line_string_list))
16335 return "--";
16336 if (field_width <= 0
16337 || field_width > sizeof (lots_of_dashes))
16338 {
16339 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
16340 decode_mode_spec_buf[i] = '-';
16341 decode_mode_spec_buf[i] = '\0';
16342 return decode_mode_spec_buf;
16343 }
16344 else
16345 return lots_of_dashes;
16346 }
16347
16348 case 'b':
16349 obj = b->name;
16350 break;
16351
16352 case 'c':
16353 {
16354 int col = (int) current_column (); /* iftc */
16355 w->column_number_displayed = make_number (col);
16356 pint2str (decode_mode_spec_buf, field_width, col);
16357 return decode_mode_spec_buf;
16358 }
16359
16360 case 'F':
16361 /* %F displays the frame name. */
16362 if (!NILP (f->title))
16363 return (char *) SDATA (f->title);
16364 if (f->explicit_name || ! FRAME_WINDOW_P (f))
16365 return (char *) SDATA (f->name);
16366 return "Emacs";
16367
16368 case 'f':
16369 obj = b->filename;
16370 break;
16371
16372 case 'i':
16373 {
16374 int size = ZV - BEGV;
16375 pint2str (decode_mode_spec_buf, field_width, size);
16376 return decode_mode_spec_buf;
16377 }
16378
16379 case 'I':
16380 {
16381 int size = ZV - BEGV;
16382 pint2hrstr (decode_mode_spec_buf, field_width, size);
16383 return decode_mode_spec_buf;
16384 }
16385
16386 case 'l':
16387 {
16388 int startpos = XMARKER (w->start)->charpos;
16389 int startpos_byte = marker_byte_position (w->start);
16390 int line, linepos, linepos_byte, topline;
16391 int nlines, junk;
16392 int height = WINDOW_TOTAL_LINES (w);
16393
16394 /* If we decided that this buffer isn't suitable for line numbers,
16395 don't forget that too fast. */
16396 if (EQ (w->base_line_pos, w->buffer))
16397 goto no_value;
16398 /* But do forget it, if the window shows a different buffer now. */
16399 else if (BUFFERP (w->base_line_pos))
16400 w->base_line_pos = Qnil;
16401
16402 /* If the buffer is very big, don't waste time. */
16403 if (INTEGERP (Vline_number_display_limit)
16404 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
16405 {
16406 w->base_line_pos = Qnil;
16407 w->base_line_number = Qnil;
16408 goto no_value;
16409 }
16410
16411 if (!NILP (w->base_line_number)
16412 && !NILP (w->base_line_pos)
16413 && XFASTINT (w->base_line_pos) <= startpos)
16414 {
16415 line = XFASTINT (w->base_line_number);
16416 linepos = XFASTINT (w->base_line_pos);
16417 linepos_byte = buf_charpos_to_bytepos (b, linepos);
16418 }
16419 else
16420 {
16421 line = 1;
16422 linepos = BUF_BEGV (b);
16423 linepos_byte = BUF_BEGV_BYTE (b);
16424 }
16425
16426 /* Count lines from base line to window start position. */
16427 nlines = display_count_lines (linepos, linepos_byte,
16428 startpos_byte,
16429 startpos, &junk);
16430
16431 topline = nlines + line;
16432
16433 /* Determine a new base line, if the old one is too close
16434 or too far away, or if we did not have one.
16435 "Too close" means it's plausible a scroll-down would
16436 go back past it. */
16437 if (startpos == BUF_BEGV (b))
16438 {
16439 w->base_line_number = make_number (topline);
16440 w->base_line_pos = make_number (BUF_BEGV (b));
16441 }
16442 else if (nlines < height + 25 || nlines > height * 3 + 50
16443 || linepos == BUF_BEGV (b))
16444 {
16445 int limit = BUF_BEGV (b);
16446 int limit_byte = BUF_BEGV_BYTE (b);
16447 int position;
16448 int distance = (height * 2 + 30) * line_number_display_limit_width;
16449
16450 if (startpos - distance > limit)
16451 {
16452 limit = startpos - distance;
16453 limit_byte = CHAR_TO_BYTE (limit);
16454 }
16455
16456 nlines = display_count_lines (startpos, startpos_byte,
16457 limit_byte,
16458 - (height * 2 + 30),
16459 &position);
16460 /* If we couldn't find the lines we wanted within
16461 line_number_display_limit_width chars per line,
16462 give up on line numbers for this window. */
16463 if (position == limit_byte && limit == startpos - distance)
16464 {
16465 w->base_line_pos = w->buffer;
16466 w->base_line_number = Qnil;
16467 goto no_value;
16468 }
16469
16470 w->base_line_number = make_number (topline - nlines);
16471 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
16472 }
16473
16474 /* Now count lines from the start pos to point. */
16475 nlines = display_count_lines (startpos, startpos_byte,
16476 PT_BYTE, PT, &junk);
16477
16478 /* Record that we did display the line number. */
16479 line_number_displayed = 1;
16480
16481 /* Make the string to show. */
16482 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
16483 return decode_mode_spec_buf;
16484 no_value:
16485 {
16486 char* p = decode_mode_spec_buf;
16487 int pad = field_width - 2;
16488 while (pad-- > 0)
16489 *p++ = ' ';
16490 *p++ = '?';
16491 *p++ = '?';
16492 *p = '\0';
16493 return decode_mode_spec_buf;
16494 }
16495 }
16496 break;
16497
16498 case 'm':
16499 obj = b->mode_name;
16500 break;
16501
16502 case 'n':
16503 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
16504 return " Narrow";
16505 break;
16506
16507 case 'p':
16508 {
16509 int pos = marker_position (w->start);
16510 int total = BUF_ZV (b) - BUF_BEGV (b);
16511
16512 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
16513 {
16514 if (pos <= BUF_BEGV (b))
16515 return "All";
16516 else
16517 return "Bottom";
16518 }
16519 else if (pos <= BUF_BEGV (b))
16520 return "Top";
16521 else
16522 {
16523 if (total > 1000000)
16524 /* Do it differently for a large value, to avoid overflow. */
16525 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
16526 else
16527 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
16528 /* We can't normally display a 3-digit number,
16529 so get us a 2-digit number that is close. */
16530 if (total == 100)
16531 total = 99;
16532 sprintf (decode_mode_spec_buf, "%2d%%", total);
16533 return decode_mode_spec_buf;
16534 }
16535 }
16536
16537 /* Display percentage of size above the bottom of the screen. */
16538 case 'P':
16539 {
16540 int toppos = marker_position (w->start);
16541 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
16542 int total = BUF_ZV (b) - BUF_BEGV (b);
16543
16544 if (botpos >= BUF_ZV (b))
16545 {
16546 if (toppos <= BUF_BEGV (b))
16547 return "All";
16548 else
16549 return "Bottom";
16550 }
16551 else
16552 {
16553 if (total > 1000000)
16554 /* Do it differently for a large value, to avoid overflow. */
16555 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
16556 else
16557 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
16558 /* We can't normally display a 3-digit number,
16559 so get us a 2-digit number that is close. */
16560 if (total == 100)
16561 total = 99;
16562 if (toppos <= BUF_BEGV (b))
16563 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
16564 else
16565 sprintf (decode_mode_spec_buf, "%2d%%", total);
16566 return decode_mode_spec_buf;
16567 }
16568 }
16569
16570 case 's':
16571 /* status of process */
16572 obj = Fget_buffer_process (Fcurrent_buffer ());
16573 if (NILP (obj))
16574 return "no process";
16575 #ifdef subprocesses
16576 obj = Fsymbol_name (Fprocess_status (obj));
16577 #endif
16578 break;
16579
16580 case 't': /* indicate TEXT or BINARY */
16581 #ifdef MODE_LINE_BINARY_TEXT
16582 return MODE_LINE_BINARY_TEXT (b);
16583 #else
16584 return "T";
16585 #endif
16586
16587 case 'z':
16588 /* coding-system (not including end-of-line format) */
16589 case 'Z':
16590 /* coding-system (including end-of-line type) */
16591 {
16592 int eol_flag = (c == 'Z');
16593 char *p = decode_mode_spec_buf;
16594
16595 if (! FRAME_WINDOW_P (f))
16596 {
16597 /* No need to mention EOL here--the terminal never needs
16598 to do EOL conversion. */
16599 p = decode_mode_spec_coding (FRAME_KEYBOARD_CODING (f)->symbol, p, 0);
16600 p = decode_mode_spec_coding (FRAME_TERMINAL_CODING (f)->symbol, p, 0);
16601 }
16602 p = decode_mode_spec_coding (b->buffer_file_coding_system,
16603 p, eol_flag);
16604
16605 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
16606 #ifdef subprocesses
16607 obj = Fget_buffer_process (Fcurrent_buffer ());
16608 if (PROCESSP (obj))
16609 {
16610 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
16611 p, eol_flag);
16612 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
16613 p, eol_flag);
16614 }
16615 #endif /* subprocesses */
16616 #endif /* 0 */
16617 *p = 0;
16618 return decode_mode_spec_buf;
16619 }
16620 }
16621
16622 if (STRINGP (obj))
16623 {
16624 *multibyte = STRING_MULTIBYTE (obj);
16625 return (char *) SDATA (obj);
16626 }
16627 else
16628 return "";
16629 }
16630
16631
16632 /* Count up to COUNT lines starting from START / START_BYTE.
16633 But don't go beyond LIMIT_BYTE.
16634 Return the number of lines thus found (always nonnegative).
16635
16636 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
16637
16638 static int
16639 display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
16640 int start, start_byte, limit_byte, count;
16641 int *byte_pos_ptr;
16642 {
16643 register unsigned char *cursor;
16644 unsigned char *base;
16645
16646 register int ceiling;
16647 register unsigned char *ceiling_addr;
16648 int orig_count = count;
16649
16650 /* If we are not in selective display mode,
16651 check only for newlines. */
16652 int selective_display = (!NILP (current_buffer->selective_display)
16653 && !INTEGERP (current_buffer->selective_display));
16654
16655 if (count > 0)
16656 {
16657 while (start_byte < limit_byte)
16658 {
16659 ceiling = BUFFER_CEILING_OF (start_byte);
16660 ceiling = min (limit_byte - 1, ceiling);
16661 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
16662 base = (cursor = BYTE_POS_ADDR (start_byte));
16663 while (1)
16664 {
16665 if (selective_display)
16666 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
16667 ;
16668 else
16669 while (*cursor != '\n' && ++cursor != ceiling_addr)
16670 ;
16671
16672 if (cursor != ceiling_addr)
16673 {
16674 if (--count == 0)
16675 {
16676 start_byte += cursor - base + 1;
16677 *byte_pos_ptr = start_byte;
16678 return orig_count;
16679 }
16680 else
16681 if (++cursor == ceiling_addr)
16682 break;
16683 }
16684 else
16685 break;
16686 }
16687 start_byte += cursor - base;
16688 }
16689 }
16690 else
16691 {
16692 while (start_byte > limit_byte)
16693 {
16694 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
16695 ceiling = max (limit_byte, ceiling);
16696 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
16697 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
16698 while (1)
16699 {
16700 if (selective_display)
16701 while (--cursor != ceiling_addr
16702 && *cursor != '\n' && *cursor != 015)
16703 ;
16704 else
16705 while (--cursor != ceiling_addr && *cursor != '\n')
16706 ;
16707
16708 if (cursor != ceiling_addr)
16709 {
16710 if (++count == 0)
16711 {
16712 start_byte += cursor - base + 1;
16713 *byte_pos_ptr = start_byte;
16714 /* When scanning backwards, we should
16715 not count the newline posterior to which we stop. */
16716 return - orig_count - 1;
16717 }
16718 }
16719 else
16720 break;
16721 }
16722 /* Here we add 1 to compensate for the last decrement
16723 of CURSOR, which took it past the valid range. */
16724 start_byte += cursor - base + 1;
16725 }
16726 }
16727
16728 *byte_pos_ptr = limit_byte;
16729
16730 if (count < 0)
16731 return - orig_count + count;
16732 return orig_count - count;
16733
16734 }
16735
16736
16737 \f
16738 /***********************************************************************
16739 Displaying strings
16740 ***********************************************************************/
16741
16742 /* Display a NUL-terminated string, starting with index START.
16743
16744 If STRING is non-null, display that C string. Otherwise, the Lisp
16745 string LISP_STRING is displayed.
16746
16747 If FACE_STRING is not nil, FACE_STRING_POS is a position in
16748 FACE_STRING. Display STRING or LISP_STRING with the face at
16749 FACE_STRING_POS in FACE_STRING:
16750
16751 Display the string in the environment given by IT, but use the
16752 standard display table, temporarily.
16753
16754 FIELD_WIDTH is the minimum number of output glyphs to produce.
16755 If STRING has fewer characters than FIELD_WIDTH, pad to the right
16756 with spaces. If STRING has more characters, more than FIELD_WIDTH
16757 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
16758
16759 PRECISION is the maximum number of characters to output from
16760 STRING. PRECISION < 0 means don't truncate the string.
16761
16762 This is roughly equivalent to printf format specifiers:
16763
16764 FIELD_WIDTH PRECISION PRINTF
16765 ----------------------------------------
16766 -1 -1 %s
16767 -1 10 %.10s
16768 10 -1 %10s
16769 20 10 %20.10s
16770
16771 MULTIBYTE zero means do not display multibyte chars, > 0 means do
16772 display them, and < 0 means obey the current buffer's value of
16773 enable_multibyte_characters.
16774
16775 Value is the number of glyphs produced. */
16776
16777 static int
16778 display_string (string, lisp_string, face_string, face_string_pos,
16779 start, it, field_width, precision, max_x, multibyte)
16780 unsigned char *string;
16781 Lisp_Object lisp_string;
16782 Lisp_Object face_string;
16783 int face_string_pos;
16784 int start;
16785 struct it *it;
16786 int field_width, precision, max_x;
16787 int multibyte;
16788 {
16789 int hpos_at_start = it->hpos;
16790 int saved_face_id = it->face_id;
16791 struct glyph_row *row = it->glyph_row;
16792
16793 /* Initialize the iterator IT for iteration over STRING beginning
16794 with index START. */
16795 reseat_to_string (it, string, lisp_string, start,
16796 precision, field_width, multibyte);
16797
16798 /* If displaying STRING, set up the face of the iterator
16799 from LISP_STRING, if that's given. */
16800 if (STRINGP (face_string))
16801 {
16802 int endptr;
16803 struct face *face;
16804
16805 it->face_id
16806 = face_at_string_position (it->w, face_string, face_string_pos,
16807 0, it->region_beg_charpos,
16808 it->region_end_charpos,
16809 &endptr, it->base_face_id, 0);
16810 face = FACE_FROM_ID (it->f, it->face_id);
16811 it->face_box_p = face->box != FACE_NO_BOX;
16812 }
16813
16814 /* Set max_x to the maximum allowed X position. Don't let it go
16815 beyond the right edge of the window. */
16816 if (max_x <= 0)
16817 max_x = it->last_visible_x;
16818 else
16819 max_x = min (max_x, it->last_visible_x);
16820
16821 /* Skip over display elements that are not visible. because IT->w is
16822 hscrolled. */
16823 if (it->current_x < it->first_visible_x)
16824 move_it_in_display_line_to (it, 100000, it->first_visible_x,
16825 MOVE_TO_POS | MOVE_TO_X);
16826
16827 row->ascent = it->max_ascent;
16828 row->height = it->max_ascent + it->max_descent;
16829 row->phys_ascent = it->max_phys_ascent;
16830 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
16831 row->extra_line_spacing = it->max_extra_line_spacing;
16832
16833 /* This condition is for the case that we are called with current_x
16834 past last_visible_x. */
16835 while (it->current_x < max_x)
16836 {
16837 int x_before, x, n_glyphs_before, i, nglyphs;
16838
16839 /* Get the next display element. */
16840 if (!get_next_display_element (it))
16841 break;
16842
16843 /* Produce glyphs. */
16844 x_before = it->current_x;
16845 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
16846 PRODUCE_GLYPHS (it);
16847
16848 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
16849 i = 0;
16850 x = x_before;
16851 while (i < nglyphs)
16852 {
16853 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
16854
16855 if (!it->truncate_lines_p
16856 && x + glyph->pixel_width > max_x)
16857 {
16858 /* End of continued line or max_x reached. */
16859 if (CHAR_GLYPH_PADDING_P (*glyph))
16860 {
16861 /* A wide character is unbreakable. */
16862 it->glyph_row->used[TEXT_AREA] = n_glyphs_before;
16863 it->current_x = x_before;
16864 }
16865 else
16866 {
16867 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
16868 it->current_x = x;
16869 }
16870 break;
16871 }
16872 else if (x + glyph->pixel_width > it->first_visible_x)
16873 {
16874 /* Glyph is at least partially visible. */
16875 ++it->hpos;
16876 if (x < it->first_visible_x)
16877 it->glyph_row->x = x - it->first_visible_x;
16878 }
16879 else
16880 {
16881 /* Glyph is off the left margin of the display area.
16882 Should not happen. */
16883 abort ();
16884 }
16885
16886 row->ascent = max (row->ascent, it->max_ascent);
16887 row->height = max (row->height, it->max_ascent + it->max_descent);
16888 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
16889 row->phys_height = max (row->phys_height,
16890 it->max_phys_ascent + it->max_phys_descent);
16891 row->extra_line_spacing = max (row->extra_line_spacing,
16892 it->max_extra_line_spacing);
16893 x += glyph->pixel_width;
16894 ++i;
16895 }
16896
16897 /* Stop if max_x reached. */
16898 if (i < nglyphs)
16899 break;
16900
16901 /* Stop at line ends. */
16902 if (ITERATOR_AT_END_OF_LINE_P (it))
16903 {
16904 it->continuation_lines_width = 0;
16905 break;
16906 }
16907
16908 set_iterator_to_next (it, 1);
16909
16910 /* Stop if truncating at the right edge. */
16911 if (it->truncate_lines_p
16912 && it->current_x >= it->last_visible_x)
16913 {
16914 /* Add truncation mark, but don't do it if the line is
16915 truncated at a padding space. */
16916 if (IT_CHARPOS (*it) < it->string_nchars)
16917 {
16918 if (!FRAME_WINDOW_P (it->f))
16919 {
16920 int i, n;
16921
16922 if (it->current_x > it->last_visible_x)
16923 {
16924 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
16925 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
16926 break;
16927 for (n = row->used[TEXT_AREA]; i < n; ++i)
16928 {
16929 row->used[TEXT_AREA] = i;
16930 produce_special_glyphs (it, IT_TRUNCATION);
16931 }
16932 }
16933 produce_special_glyphs (it, IT_TRUNCATION);
16934 }
16935 it->glyph_row->truncated_on_right_p = 1;
16936 }
16937 break;
16938 }
16939 }
16940
16941 /* Maybe insert a truncation at the left. */
16942 if (it->first_visible_x
16943 && IT_CHARPOS (*it) > 0)
16944 {
16945 if (!FRAME_WINDOW_P (it->f))
16946 insert_left_trunc_glyphs (it);
16947 it->glyph_row->truncated_on_left_p = 1;
16948 }
16949
16950 it->face_id = saved_face_id;
16951
16952 /* Value is number of columns displayed. */
16953 return it->hpos - hpos_at_start;
16954 }
16955
16956
16957 \f
16958 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
16959 appears as an element of LIST or as the car of an element of LIST.
16960 If PROPVAL is a list, compare each element against LIST in that
16961 way, and return 1/2 if any element of PROPVAL is found in LIST.
16962 Otherwise return 0. This function cannot quit.
16963 The return value is 2 if the text is invisible but with an ellipsis
16964 and 1 if it's invisible and without an ellipsis. */
16965
16966 int
16967 invisible_p (propval, list)
16968 register Lisp_Object propval;
16969 Lisp_Object list;
16970 {
16971 register Lisp_Object tail, proptail;
16972
16973 for (tail = list; CONSP (tail); tail = XCDR (tail))
16974 {
16975 register Lisp_Object tem;
16976 tem = XCAR (tail);
16977 if (EQ (propval, tem))
16978 return 1;
16979 if (CONSP (tem) && EQ (propval, XCAR (tem)))
16980 return NILP (XCDR (tem)) ? 1 : 2;
16981 }
16982
16983 if (CONSP (propval))
16984 {
16985 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
16986 {
16987 Lisp_Object propelt;
16988 propelt = XCAR (proptail);
16989 for (tail = list; CONSP (tail); tail = XCDR (tail))
16990 {
16991 register Lisp_Object tem;
16992 tem = XCAR (tail);
16993 if (EQ (propelt, tem))
16994 return 1;
16995 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
16996 return NILP (XCDR (tem)) ? 1 : 2;
16997 }
16998 }
16999 }
17000
17001 return 0;
17002 }
17003
17004 /* Calculate a width or height in pixels from a specification using
17005 the following elements:
17006
17007 SPEC ::=
17008 NUM - a (fractional) multiple of the default font width/height
17009 (NUM) - specifies exactly NUM pixels
17010 UNIT - a fixed number of pixels, see below.
17011 ELEMENT - size of a display element in pixels, see below.
17012 (NUM . SPEC) - equals NUM * SPEC
17013 (+ SPEC SPEC ...) - add pixel values
17014 (- SPEC SPEC ...) - subtract pixel values
17015 (- SPEC) - negate pixel value
17016
17017 NUM ::=
17018 INT or FLOAT - a number constant
17019 SYMBOL - use symbol's (buffer local) variable binding.
17020
17021 UNIT ::=
17022 in - pixels per inch *)
17023 mm - pixels per 1/1000 meter *)
17024 cm - pixels per 1/100 meter *)
17025 width - width of current font in pixels.
17026 height - height of current font in pixels.
17027
17028 *) using the ratio(s) defined in display-pixels-per-inch.
17029
17030 ELEMENT ::=
17031
17032 left-fringe - left fringe width in pixels
17033 right-fringe - right fringe width in pixels
17034
17035 left-margin - left margin width in pixels
17036 right-margin - right margin width in pixels
17037
17038 scroll-bar - scroll-bar area width in pixels
17039
17040 Examples:
17041
17042 Pixels corresponding to 5 inches:
17043 (5 . in)
17044
17045 Total width of non-text areas on left side of window (if scroll-bar is on left):
17046 '(space :width (+ left-fringe left-margin scroll-bar))
17047
17048 Align to first text column (in header line):
17049 '(space :align-to 0)
17050
17051 Align to middle of text area minus half the width of variable `my-image'
17052 containing a loaded image:
17053 '(space :align-to (0.5 . (- text my-image)))
17054
17055 Width of left margin minus width of 1 character in the default font:
17056 '(space :width (- left-margin 1))
17057
17058 Width of left margin minus width of 2 characters in the current font:
17059 '(space :width (- left-margin (2 . width)))
17060
17061 Center 1 character over left-margin (in header line):
17062 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
17063
17064 Different ways to express width of left fringe plus left margin minus one pixel:
17065 '(space :width (- (+ left-fringe left-margin) (1)))
17066 '(space :width (+ left-fringe left-margin (- (1))))
17067 '(space :width (+ left-fringe left-margin (-1)))
17068
17069 */
17070
17071 #define NUMVAL(X) \
17072 ((INTEGERP (X) || FLOATP (X)) \
17073 ? XFLOATINT (X) \
17074 : - 1)
17075
17076 int
17077 calc_pixel_width_or_height (res, it, prop, font, width_p, align_to)
17078 double *res;
17079 struct it *it;
17080 Lisp_Object prop;
17081 void *font;
17082 int width_p, *align_to;
17083 {
17084 double pixels;
17085
17086 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
17087 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
17088
17089 if (NILP (prop))
17090 return OK_PIXELS (0);
17091
17092 if (SYMBOLP (prop))
17093 {
17094 if (SCHARS (SYMBOL_NAME (prop)) == 2)
17095 {
17096 char *unit = SDATA (SYMBOL_NAME (prop));
17097
17098 if (unit[0] == 'i' && unit[1] == 'n')
17099 pixels = 1.0;
17100 else if (unit[0] == 'm' && unit[1] == 'm')
17101 pixels = 25.4;
17102 else if (unit[0] == 'c' && unit[1] == 'm')
17103 pixels = 2.54;
17104 else
17105 pixels = 0;
17106 if (pixels > 0)
17107 {
17108 double ppi;
17109 if ((ppi = NUMVAL (Vdisplay_pixels_per_inch), ppi > 0)
17110 || (CONSP (Vdisplay_pixels_per_inch)
17111 && (ppi = (width_p
17112 ? NUMVAL (XCAR (Vdisplay_pixels_per_inch))
17113 : NUMVAL (XCDR (Vdisplay_pixels_per_inch))),
17114 ppi > 0)))
17115 return OK_PIXELS (ppi / pixels);
17116
17117 return 0;
17118 }
17119 }
17120
17121 #ifdef HAVE_WINDOW_SYSTEM
17122 if (EQ (prop, Qheight))
17123 return OK_PIXELS (font ? FONT_HEIGHT ((XFontStruct *)font) : FRAME_LINE_HEIGHT (it->f));
17124 if (EQ (prop, Qwidth))
17125 return OK_PIXELS (font ? FONT_WIDTH ((XFontStruct *)font) : FRAME_COLUMN_WIDTH (it->f));
17126 #else
17127 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
17128 return OK_PIXELS (1);
17129 #endif
17130
17131 if (EQ (prop, Qtext))
17132 return OK_PIXELS (width_p
17133 ? window_box_width (it->w, TEXT_AREA)
17134 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
17135
17136 if (align_to && *align_to < 0)
17137 {
17138 *res = 0;
17139 if (EQ (prop, Qleft))
17140 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
17141 if (EQ (prop, Qright))
17142 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
17143 if (EQ (prop, Qcenter))
17144 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
17145 + window_box_width (it->w, TEXT_AREA) / 2);
17146 if (EQ (prop, Qleft_fringe))
17147 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
17148 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
17149 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
17150 if (EQ (prop, Qright_fringe))
17151 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
17152 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
17153 : window_box_right_offset (it->w, TEXT_AREA));
17154 if (EQ (prop, Qleft_margin))
17155 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
17156 if (EQ (prop, Qright_margin))
17157 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
17158 if (EQ (prop, Qscroll_bar))
17159 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
17160 ? 0
17161 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
17162 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
17163 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
17164 : 0)));
17165 }
17166 else
17167 {
17168 if (EQ (prop, Qleft_fringe))
17169 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
17170 if (EQ (prop, Qright_fringe))
17171 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
17172 if (EQ (prop, Qleft_margin))
17173 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
17174 if (EQ (prop, Qright_margin))
17175 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
17176 if (EQ (prop, Qscroll_bar))
17177 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
17178 }
17179
17180 prop = Fbuffer_local_value (prop, it->w->buffer);
17181 }
17182
17183 if (INTEGERP (prop) || FLOATP (prop))
17184 {
17185 int base_unit = (width_p
17186 ? FRAME_COLUMN_WIDTH (it->f)
17187 : FRAME_LINE_HEIGHT (it->f));
17188 return OK_PIXELS (XFLOATINT (prop) * base_unit);
17189 }
17190
17191 if (CONSP (prop))
17192 {
17193 Lisp_Object car = XCAR (prop);
17194 Lisp_Object cdr = XCDR (prop);
17195
17196 if (SYMBOLP (car))
17197 {
17198 #ifdef HAVE_WINDOW_SYSTEM
17199 if (valid_image_p (prop))
17200 {
17201 int id = lookup_image (it->f, prop);
17202 struct image *img = IMAGE_FROM_ID (it->f, id);
17203
17204 return OK_PIXELS (width_p ? img->width : img->height);
17205 }
17206 #endif
17207 if (EQ (car, Qplus) || EQ (car, Qminus))
17208 {
17209 int first = 1;
17210 double px;
17211
17212 pixels = 0;
17213 while (CONSP (cdr))
17214 {
17215 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
17216 font, width_p, align_to))
17217 return 0;
17218 if (first)
17219 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
17220 else
17221 pixels += px;
17222 cdr = XCDR (cdr);
17223 }
17224 if (EQ (car, Qminus))
17225 pixels = -pixels;
17226 return OK_PIXELS (pixels);
17227 }
17228
17229 car = Fbuffer_local_value (car, it->w->buffer);
17230 }
17231
17232 if (INTEGERP (car) || FLOATP (car))
17233 {
17234 double fact;
17235 pixels = XFLOATINT (car);
17236 if (NILP (cdr))
17237 return OK_PIXELS (pixels);
17238 if (calc_pixel_width_or_height (&fact, it, cdr,
17239 font, width_p, align_to))
17240 return OK_PIXELS (pixels * fact);
17241 return 0;
17242 }
17243
17244 return 0;
17245 }
17246
17247 return 0;
17248 }
17249
17250 \f
17251 /***********************************************************************
17252 Glyph Display
17253 ***********************************************************************/
17254
17255 #ifdef HAVE_WINDOW_SYSTEM
17256
17257 #if GLYPH_DEBUG
17258
17259 void
17260 dump_glyph_string (s)
17261 struct glyph_string *s;
17262 {
17263 fprintf (stderr, "glyph string\n");
17264 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
17265 s->x, s->y, s->width, s->height);
17266 fprintf (stderr, " ybase = %d\n", s->ybase);
17267 fprintf (stderr, " hl = %d\n", s->hl);
17268 fprintf (stderr, " left overhang = %d, right = %d\n",
17269 s->left_overhang, s->right_overhang);
17270 fprintf (stderr, " nchars = %d\n", s->nchars);
17271 fprintf (stderr, " extends to end of line = %d\n",
17272 s->extends_to_end_of_line_p);
17273 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
17274 fprintf (stderr, " bg width = %d\n", s->background_width);
17275 }
17276
17277 #endif /* GLYPH_DEBUG */
17278
17279 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
17280 of XChar2b structures for S; it can't be allocated in
17281 init_glyph_string because it must be allocated via `alloca'. W
17282 is the window on which S is drawn. ROW and AREA are the glyph row
17283 and area within the row from which S is constructed. START is the
17284 index of the first glyph structure covered by S. HL is a
17285 face-override for drawing S. */
17286
17287 #ifdef HAVE_NTGUI
17288 #define OPTIONAL_HDC(hdc) hdc,
17289 #define DECLARE_HDC(hdc) HDC hdc;
17290 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
17291 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
17292 #endif
17293
17294 #ifndef OPTIONAL_HDC
17295 #define OPTIONAL_HDC(hdc)
17296 #define DECLARE_HDC(hdc)
17297 #define ALLOCATE_HDC(hdc, f)
17298 #define RELEASE_HDC(hdc, f)
17299 #endif
17300
17301 static void
17302 init_glyph_string (s, OPTIONAL_HDC (hdc) char2b, w, row, area, start, hl)
17303 struct glyph_string *s;
17304 DECLARE_HDC (hdc)
17305 XChar2b *char2b;
17306 struct window *w;
17307 struct glyph_row *row;
17308 enum glyph_row_area area;
17309 int start;
17310 enum draw_glyphs_face hl;
17311 {
17312 bzero (s, sizeof *s);
17313 s->w = w;
17314 s->f = XFRAME (w->frame);
17315 #ifdef HAVE_NTGUI
17316 s->hdc = hdc;
17317 #endif
17318 s->display = FRAME_X_DISPLAY (s->f);
17319 s->window = FRAME_X_WINDOW (s->f);
17320 s->char2b = char2b;
17321 s->hl = hl;
17322 s->row = row;
17323 s->area = area;
17324 s->first_glyph = row->glyphs[area] + start;
17325 s->height = row->height;
17326 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
17327
17328 /* Display the internal border below the tool-bar window. */
17329 if (WINDOWP (s->f->tool_bar_window)
17330 && s->w == XWINDOW (s->f->tool_bar_window))
17331 s->y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
17332
17333 s->ybase = s->y + row->ascent;
17334 }
17335
17336
17337 /* Append the list of glyph strings with head H and tail T to the list
17338 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
17339
17340 static INLINE void
17341 append_glyph_string_lists (head, tail, h, t)
17342 struct glyph_string **head, **tail;
17343 struct glyph_string *h, *t;
17344 {
17345 if (h)
17346 {
17347 if (*head)
17348 (*tail)->next = h;
17349 else
17350 *head = h;
17351 h->prev = *tail;
17352 *tail = t;
17353 }
17354 }
17355
17356
17357 /* Prepend the list of glyph strings with head H and tail T to the
17358 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
17359 result. */
17360
17361 static INLINE void
17362 prepend_glyph_string_lists (head, tail, h, t)
17363 struct glyph_string **head, **tail;
17364 struct glyph_string *h, *t;
17365 {
17366 if (h)
17367 {
17368 if (*head)
17369 (*head)->prev = t;
17370 else
17371 *tail = t;
17372 t->next = *head;
17373 *head = h;
17374 }
17375 }
17376
17377
17378 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
17379 Set *HEAD and *TAIL to the resulting list. */
17380
17381 static INLINE void
17382 append_glyph_string (head, tail, s)
17383 struct glyph_string **head, **tail;
17384 struct glyph_string *s;
17385 {
17386 s->next = s->prev = NULL;
17387 append_glyph_string_lists (head, tail, s, s);
17388 }
17389
17390
17391 /* Get face and two-byte form of character glyph GLYPH on frame F.
17392 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
17393 a pointer to a realized face that is ready for display. */
17394
17395 static INLINE struct face *
17396 get_glyph_face_and_encoding (f, glyph, char2b, two_byte_p)
17397 struct frame *f;
17398 struct glyph *glyph;
17399 XChar2b *char2b;
17400 int *two_byte_p;
17401 {
17402 struct face *face;
17403
17404 xassert (glyph->type == CHAR_GLYPH);
17405 face = FACE_FROM_ID (f, glyph->face_id);
17406
17407 if (two_byte_p)
17408 *two_byte_p = 0;
17409
17410 if (!glyph->multibyte_p)
17411 {
17412 /* Unibyte case. We don't have to encode, but we have to make
17413 sure to use a face suitable for unibyte. */
17414 STORE_XCHAR2B (char2b, 0, glyph->u.ch);
17415 }
17416 else if (glyph->u.ch < 128
17417 && glyph->face_id < BASIC_FACE_ID_SENTINEL)
17418 {
17419 /* Case of ASCII in a face known to fit ASCII. */
17420 STORE_XCHAR2B (char2b, 0, glyph->u.ch);
17421 }
17422 else
17423 {
17424 int c1, c2, charset;
17425
17426 /* Split characters into bytes. If c2 is -1 afterwards, C is
17427 really a one-byte character so that byte1 is zero. */
17428 SPLIT_CHAR (glyph->u.ch, charset, c1, c2);
17429 if (c2 > 0)
17430 STORE_XCHAR2B (char2b, c1, c2);
17431 else
17432 STORE_XCHAR2B (char2b, 0, c1);
17433
17434 /* Maybe encode the character in *CHAR2B. */
17435 if (charset != CHARSET_ASCII)
17436 {
17437 struct font_info *font_info
17438 = FONT_INFO_FROM_ID (f, face->font_info_id);
17439 if (font_info)
17440 glyph->font_type
17441 = FRAME_RIF (f)->encode_char (glyph->u.ch, char2b, font_info, two_byte_p);
17442 }
17443 }
17444
17445 /* Make sure X resources of the face are allocated. */
17446 xassert (face != NULL);
17447 PREPARE_FACE_FOR_DISPLAY (f, face);
17448 return face;
17449 }
17450
17451
17452 /* Fill glyph string S with composition components specified by S->cmp.
17453
17454 FACES is an array of faces for all components of this composition.
17455 S->gidx is the index of the first component for S.
17456 OVERLAPS_P non-zero means S should draw the foreground only, and
17457 use its physical height for clipping.
17458
17459 Value is the index of a component not in S. */
17460
17461 static int
17462 fill_composite_glyph_string (s, faces, overlaps_p)
17463 struct glyph_string *s;
17464 struct face **faces;
17465 int overlaps_p;
17466 {
17467 int i;
17468
17469 xassert (s);
17470
17471 s->for_overlaps_p = overlaps_p;
17472
17473 s->face = faces[s->gidx];
17474 s->font = s->face->font;
17475 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
17476
17477 /* For all glyphs of this composition, starting at the offset
17478 S->gidx, until we reach the end of the definition or encounter a
17479 glyph that requires the different face, add it to S. */
17480 ++s->nchars;
17481 for (i = s->gidx + 1; i < s->cmp->glyph_len && faces[i] == s->face; ++i)
17482 ++s->nchars;
17483
17484 /* All glyph strings for the same composition has the same width,
17485 i.e. the width set for the first component of the composition. */
17486
17487 s->width = s->first_glyph->pixel_width;
17488
17489 /* If the specified font could not be loaded, use the frame's
17490 default font, but record the fact that we couldn't load it in
17491 the glyph string so that we can draw rectangles for the
17492 characters of the glyph string. */
17493 if (s->font == NULL)
17494 {
17495 s->font_not_found_p = 1;
17496 s->font = FRAME_FONT (s->f);
17497 }
17498
17499 /* Adjust base line for subscript/superscript text. */
17500 s->ybase += s->first_glyph->voffset;
17501
17502 xassert (s->face && s->face->gc);
17503
17504 /* This glyph string must always be drawn with 16-bit functions. */
17505 s->two_byte_p = 1;
17506
17507 return s->gidx + s->nchars;
17508 }
17509
17510
17511 /* Fill glyph string S from a sequence of character glyphs.
17512
17513 FACE_ID is the face id of the string. START is the index of the
17514 first glyph to consider, END is the index of the last + 1.
17515 OVERLAPS_P non-zero means S should draw the foreground only, and
17516 use its physical height for clipping.
17517
17518 Value is the index of the first glyph not in S. */
17519
17520 static int
17521 fill_glyph_string (s, face_id, start, end, overlaps_p)
17522 struct glyph_string *s;
17523 int face_id;
17524 int start, end, overlaps_p;
17525 {
17526 struct glyph *glyph, *last;
17527 int voffset;
17528 int glyph_not_available_p;
17529
17530 xassert (s->f == XFRAME (s->w->frame));
17531 xassert (s->nchars == 0);
17532 xassert (start >= 0 && end > start);
17533
17534 s->for_overlaps_p = overlaps_p,
17535 glyph = s->row->glyphs[s->area] + start;
17536 last = s->row->glyphs[s->area] + end;
17537 voffset = glyph->voffset;
17538
17539 glyph_not_available_p = glyph->glyph_not_available_p;
17540
17541 while (glyph < last
17542 && glyph->type == CHAR_GLYPH
17543 && glyph->voffset == voffset
17544 /* Same face id implies same font, nowadays. */
17545 && glyph->face_id == face_id
17546 && glyph->glyph_not_available_p == glyph_not_available_p)
17547 {
17548 int two_byte_p;
17549
17550 s->face = get_glyph_face_and_encoding (s->f, glyph,
17551 s->char2b + s->nchars,
17552 &two_byte_p);
17553 s->two_byte_p = two_byte_p;
17554 ++s->nchars;
17555 xassert (s->nchars <= end - start);
17556 s->width += glyph->pixel_width;
17557 ++glyph;
17558 }
17559
17560 s->font = s->face->font;
17561 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
17562
17563 /* If the specified font could not be loaded, use the frame's font,
17564 but record the fact that we couldn't load it in
17565 S->font_not_found_p so that we can draw rectangles for the
17566 characters of the glyph string. */
17567 if (s->font == NULL || glyph_not_available_p)
17568 {
17569 s->font_not_found_p = 1;
17570 s->font = FRAME_FONT (s->f);
17571 }
17572
17573 /* Adjust base line for subscript/superscript text. */
17574 s->ybase += voffset;
17575
17576 xassert (s->face && s->face->gc);
17577 return glyph - s->row->glyphs[s->area];
17578 }
17579
17580
17581 /* Fill glyph string S from image glyph S->first_glyph. */
17582
17583 static void
17584 fill_image_glyph_string (s)
17585 struct glyph_string *s;
17586 {
17587 xassert (s->first_glyph->type == IMAGE_GLYPH);
17588 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
17589 xassert (s->img);
17590 s->slice = s->first_glyph->slice;
17591 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
17592 s->font = s->face->font;
17593 s->width = s->first_glyph->pixel_width;
17594
17595 /* Adjust base line for subscript/superscript text. */
17596 s->ybase += s->first_glyph->voffset;
17597 }
17598
17599
17600 /* Fill glyph string S from a sequence of stretch glyphs.
17601
17602 ROW is the glyph row in which the glyphs are found, AREA is the
17603 area within the row. START is the index of the first glyph to
17604 consider, END is the index of the last + 1.
17605
17606 Value is the index of the first glyph not in S. */
17607
17608 static int
17609 fill_stretch_glyph_string (s, row, area, start, end)
17610 struct glyph_string *s;
17611 struct glyph_row *row;
17612 enum glyph_row_area area;
17613 int start, end;
17614 {
17615 struct glyph *glyph, *last;
17616 int voffset, face_id;
17617
17618 xassert (s->first_glyph->type == STRETCH_GLYPH);
17619
17620 glyph = s->row->glyphs[s->area] + start;
17621 last = s->row->glyphs[s->area] + end;
17622 face_id = glyph->face_id;
17623 s->face = FACE_FROM_ID (s->f, face_id);
17624 s->font = s->face->font;
17625 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
17626 s->width = glyph->pixel_width;
17627 voffset = glyph->voffset;
17628
17629 for (++glyph;
17630 (glyph < last
17631 && glyph->type == STRETCH_GLYPH
17632 && glyph->voffset == voffset
17633 && glyph->face_id == face_id);
17634 ++glyph)
17635 s->width += glyph->pixel_width;
17636
17637 /* Adjust base line for subscript/superscript text. */
17638 s->ybase += voffset;
17639
17640 /* The case that face->gc == 0 is handled when drawing the glyph
17641 string by calling PREPARE_FACE_FOR_DISPLAY. */
17642 xassert (s->face);
17643 return glyph - s->row->glyphs[s->area];
17644 }
17645
17646
17647 /* EXPORT for RIF:
17648 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
17649 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
17650 assumed to be zero. */
17651
17652 void
17653 x_get_glyph_overhangs (glyph, f, left, right)
17654 struct glyph *glyph;
17655 struct frame *f;
17656 int *left, *right;
17657 {
17658 *left = *right = 0;
17659
17660 if (glyph->type == CHAR_GLYPH)
17661 {
17662 XFontStruct *font;
17663 struct face *face;
17664 struct font_info *font_info;
17665 XChar2b char2b;
17666 XCharStruct *pcm;
17667
17668 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
17669 font = face->font;
17670 font_info = FONT_INFO_FROM_ID (f, face->font_info_id);
17671 if (font /* ++KFS: Should this be font_info ? */
17672 && (pcm = FRAME_RIF (f)->per_char_metric (font, &char2b, glyph->font_type)))
17673 {
17674 if (pcm->rbearing > pcm->width)
17675 *right = pcm->rbearing - pcm->width;
17676 if (pcm->lbearing < 0)
17677 *left = -pcm->lbearing;
17678 }
17679 }
17680 }
17681
17682
17683 /* Return the index of the first glyph preceding glyph string S that
17684 is overwritten by S because of S's left overhang. Value is -1
17685 if no glyphs are overwritten. */
17686
17687 static int
17688 left_overwritten (s)
17689 struct glyph_string *s;
17690 {
17691 int k;
17692
17693 if (s->left_overhang)
17694 {
17695 int x = 0, i;
17696 struct glyph *glyphs = s->row->glyphs[s->area];
17697 int first = s->first_glyph - glyphs;
17698
17699 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
17700 x -= glyphs[i].pixel_width;
17701
17702 k = i + 1;
17703 }
17704 else
17705 k = -1;
17706
17707 return k;
17708 }
17709
17710
17711 /* Return the index of the first glyph preceding glyph string S that
17712 is overwriting S because of its right overhang. Value is -1 if no
17713 glyph in front of S overwrites S. */
17714
17715 static int
17716 left_overwriting (s)
17717 struct glyph_string *s;
17718 {
17719 int i, k, x;
17720 struct glyph *glyphs = s->row->glyphs[s->area];
17721 int first = s->first_glyph - glyphs;
17722
17723 k = -1;
17724 x = 0;
17725 for (i = first - 1; i >= 0; --i)
17726 {
17727 int left, right;
17728 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
17729 if (x + right > 0)
17730 k = i;
17731 x -= glyphs[i].pixel_width;
17732 }
17733
17734 return k;
17735 }
17736
17737
17738 /* Return the index of the last glyph following glyph string S that is
17739 not overwritten by S because of S's right overhang. Value is -1 if
17740 no such glyph is found. */
17741
17742 static int
17743 right_overwritten (s)
17744 struct glyph_string *s;
17745 {
17746 int k = -1;
17747
17748 if (s->right_overhang)
17749 {
17750 int x = 0, i;
17751 struct glyph *glyphs = s->row->glyphs[s->area];
17752 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
17753 int end = s->row->used[s->area];
17754
17755 for (i = first; i < end && s->right_overhang > x; ++i)
17756 x += glyphs[i].pixel_width;
17757
17758 k = i;
17759 }
17760
17761 return k;
17762 }
17763
17764
17765 /* Return the index of the last glyph following glyph string S that
17766 overwrites S because of its left overhang. Value is negative
17767 if no such glyph is found. */
17768
17769 static int
17770 right_overwriting (s)
17771 struct glyph_string *s;
17772 {
17773 int i, k, x;
17774 int end = s->row->used[s->area];
17775 struct glyph *glyphs = s->row->glyphs[s->area];
17776 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
17777
17778 k = -1;
17779 x = 0;
17780 for (i = first; i < end; ++i)
17781 {
17782 int left, right;
17783 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
17784 if (x - left < 0)
17785 k = i;
17786 x += glyphs[i].pixel_width;
17787 }
17788
17789 return k;
17790 }
17791
17792
17793 /* Get face and two-byte form of character C in face FACE_ID on frame
17794 F. The encoding of C is returned in *CHAR2B. MULTIBYTE_P non-zero
17795 means we want to display multibyte text. DISPLAY_P non-zero means
17796 make sure that X resources for the face returned are allocated.
17797 Value is a pointer to a realized face that is ready for display if
17798 DISPLAY_P is non-zero. */
17799
17800 static INLINE struct face *
17801 get_char_face_and_encoding (f, c, face_id, char2b, multibyte_p, display_p)
17802 struct frame *f;
17803 int c, face_id;
17804 XChar2b *char2b;
17805 int multibyte_p, display_p;
17806 {
17807 struct face *face = FACE_FROM_ID (f, face_id);
17808
17809 if (!multibyte_p)
17810 {
17811 /* Unibyte case. We don't have to encode, but we have to make
17812 sure to use a face suitable for unibyte. */
17813 STORE_XCHAR2B (char2b, 0, c);
17814 face_id = FACE_FOR_CHAR (f, face, c);
17815 face = FACE_FROM_ID (f, face_id);
17816 }
17817 else if (c < 128 && face_id < BASIC_FACE_ID_SENTINEL)
17818 {
17819 /* Case of ASCII in a face known to fit ASCII. */
17820 STORE_XCHAR2B (char2b, 0, c);
17821 }
17822 else
17823 {
17824 int c1, c2, charset;
17825
17826 /* Split characters into bytes. If c2 is -1 afterwards, C is
17827 really a one-byte character so that byte1 is zero. */
17828 SPLIT_CHAR (c, charset, c1, c2);
17829 if (c2 > 0)
17830 STORE_XCHAR2B (char2b, c1, c2);
17831 else
17832 STORE_XCHAR2B (char2b, 0, c1);
17833
17834 /* Maybe encode the character in *CHAR2B. */
17835 if (face->font != NULL)
17836 {
17837 struct font_info *font_info
17838 = FONT_INFO_FROM_ID (f, face->font_info_id);
17839 if (font_info)
17840 FRAME_RIF (f)->encode_char (c, char2b, font_info, 0);
17841 }
17842 }
17843
17844 /* Make sure X resources of the face are allocated. */
17845 #ifdef HAVE_X_WINDOWS
17846 if (display_p)
17847 #endif
17848 {
17849 xassert (face != NULL);
17850 PREPARE_FACE_FOR_DISPLAY (f, face);
17851 }
17852
17853 return face;
17854 }
17855
17856
17857 /* Set background width of glyph string S. START is the index of the
17858 first glyph following S. LAST_X is the right-most x-position + 1
17859 in the drawing area. */
17860
17861 static INLINE void
17862 set_glyph_string_background_width (s, start, last_x)
17863 struct glyph_string *s;
17864 int start;
17865 int last_x;
17866 {
17867 /* If the face of this glyph string has to be drawn to the end of
17868 the drawing area, set S->extends_to_end_of_line_p. */
17869 struct face *default_face = FACE_FROM_ID (s->f, DEFAULT_FACE_ID);
17870
17871 if (start == s->row->used[s->area]
17872 && s->area == TEXT_AREA
17873 && ((s->hl == DRAW_NORMAL_TEXT
17874 && (s->row->fill_line_p
17875 || s->face->background != default_face->background
17876 || s->face->stipple != default_face->stipple
17877 || s->row->mouse_face_p))
17878 || s->hl == DRAW_MOUSE_FACE
17879 || ((s->hl == DRAW_IMAGE_RAISED || s->hl == DRAW_IMAGE_SUNKEN)
17880 && s->row->fill_line_p)))
17881 s->extends_to_end_of_line_p = 1;
17882
17883 /* If S extends its face to the end of the line, set its
17884 background_width to the distance to the right edge of the drawing
17885 area. */
17886 if (s->extends_to_end_of_line_p)
17887 s->background_width = last_x - s->x + 1;
17888 else
17889 s->background_width = s->width;
17890 }
17891
17892
17893 /* Compute overhangs and x-positions for glyph string S and its
17894 predecessors, or successors. X is the starting x-position for S.
17895 BACKWARD_P non-zero means process predecessors. */
17896
17897 static void
17898 compute_overhangs_and_x (s, x, backward_p)
17899 struct glyph_string *s;
17900 int x;
17901 int backward_p;
17902 {
17903 if (backward_p)
17904 {
17905 while (s)
17906 {
17907 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
17908 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
17909 x -= s->width;
17910 s->x = x;
17911 s = s->prev;
17912 }
17913 }
17914 else
17915 {
17916 while (s)
17917 {
17918 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
17919 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
17920 s->x = x;
17921 x += s->width;
17922 s = s->next;
17923 }
17924 }
17925 }
17926
17927
17928
17929 /* The following macros are only called from draw_glyphs below.
17930 They reference the following parameters of that function directly:
17931 `w', `row', `area', and `overlap_p'
17932 as well as the following local variables:
17933 `s', `f', and `hdc' (in W32) */
17934
17935 #ifdef HAVE_NTGUI
17936 /* On W32, silently add local `hdc' variable to argument list of
17937 init_glyph_string. */
17938 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
17939 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
17940 #else
17941 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
17942 init_glyph_string (s, char2b, w, row, area, start, hl)
17943 #endif
17944
17945 /* Add a glyph string for a stretch glyph to the list of strings
17946 between HEAD and TAIL. START is the index of the stretch glyph in
17947 row area AREA of glyph row ROW. END is the index of the last glyph
17948 in that glyph row area. X is the current output position assigned
17949 to the new glyph string constructed. HL overrides that face of the
17950 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
17951 is the right-most x-position of the drawing area. */
17952
17953 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
17954 and below -- keep them on one line. */
17955 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
17956 do \
17957 { \
17958 s = (struct glyph_string *) alloca (sizeof *s); \
17959 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
17960 START = fill_stretch_glyph_string (s, row, area, START, END); \
17961 append_glyph_string (&HEAD, &TAIL, s); \
17962 s->x = (X); \
17963 } \
17964 while (0)
17965
17966
17967 /* Add a glyph string for an image glyph to the list of strings
17968 between HEAD and TAIL. START is the index of the image glyph in
17969 row area AREA of glyph row ROW. END is the index of the last glyph
17970 in that glyph row area. X is the current output position assigned
17971 to the new glyph string constructed. HL overrides that face of the
17972 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
17973 is the right-most x-position of the drawing area. */
17974
17975 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
17976 do \
17977 { \
17978 s = (struct glyph_string *) alloca (sizeof *s); \
17979 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
17980 fill_image_glyph_string (s); \
17981 append_glyph_string (&HEAD, &TAIL, s); \
17982 ++START; \
17983 s->x = (X); \
17984 } \
17985 while (0)
17986
17987
17988 /* Add a glyph string for a sequence of character glyphs to the list
17989 of strings between HEAD and TAIL. START is the index of the first
17990 glyph in row area AREA of glyph row ROW that is part of the new
17991 glyph string. END is the index of the last glyph in that glyph row
17992 area. X is the current output position assigned to the new glyph
17993 string constructed. HL overrides that face of the glyph; e.g. it
17994 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
17995 right-most x-position of the drawing area. */
17996
17997 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
17998 do \
17999 { \
18000 int c, face_id; \
18001 XChar2b *char2b; \
18002 \
18003 c = (row)->glyphs[area][START].u.ch; \
18004 face_id = (row)->glyphs[area][START].face_id; \
18005 \
18006 s = (struct glyph_string *) alloca (sizeof *s); \
18007 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
18008 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
18009 append_glyph_string (&HEAD, &TAIL, s); \
18010 s->x = (X); \
18011 START = fill_glyph_string (s, face_id, START, END, overlaps_p); \
18012 } \
18013 while (0)
18014
18015
18016 /* Add a glyph string for a composite sequence to the list of strings
18017 between HEAD and TAIL. START is the index of the first glyph in
18018 row area AREA of glyph row ROW that is part of the new glyph
18019 string. END is the index of the last glyph in that glyph row area.
18020 X is the current output position assigned to the new glyph string
18021 constructed. HL overrides that face of the glyph; e.g. it is
18022 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
18023 x-position of the drawing area. */
18024
18025 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
18026 do { \
18027 int cmp_id = (row)->glyphs[area][START].u.cmp_id; \
18028 int face_id = (row)->glyphs[area][START].face_id; \
18029 struct face *base_face = FACE_FROM_ID (f, face_id); \
18030 struct composition *cmp = composition_table[cmp_id]; \
18031 int glyph_len = cmp->glyph_len; \
18032 XChar2b *char2b; \
18033 struct face **faces; \
18034 struct glyph_string *first_s = NULL; \
18035 int n; \
18036 \
18037 base_face = base_face->ascii_face; \
18038 char2b = (XChar2b *) alloca ((sizeof *char2b) * glyph_len); \
18039 faces = (struct face **) alloca ((sizeof *faces) * glyph_len); \
18040 /* At first, fill in `char2b' and `faces'. */ \
18041 for (n = 0; n < glyph_len; n++) \
18042 { \
18043 int c = COMPOSITION_GLYPH (cmp, n); \
18044 int this_face_id = FACE_FOR_CHAR (f, base_face, c); \
18045 faces[n] = FACE_FROM_ID (f, this_face_id); \
18046 get_char_face_and_encoding (f, c, this_face_id, \
18047 char2b + n, 1, 1); \
18048 } \
18049 \
18050 /* Make glyph_strings for each glyph sequence that is drawable by \
18051 the same face, and append them to HEAD/TAIL. */ \
18052 for (n = 0; n < cmp->glyph_len;) \
18053 { \
18054 s = (struct glyph_string *) alloca (sizeof *s); \
18055 INIT_GLYPH_STRING (s, char2b + n, w, row, area, START, HL); \
18056 append_glyph_string (&(HEAD), &(TAIL), s); \
18057 s->cmp = cmp; \
18058 s->gidx = n; \
18059 s->x = (X); \
18060 \
18061 if (n == 0) \
18062 first_s = s; \
18063 \
18064 n = fill_composite_glyph_string (s, faces, overlaps_p); \
18065 } \
18066 \
18067 ++START; \
18068 s = first_s; \
18069 } while (0)
18070
18071
18072 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
18073 of AREA of glyph row ROW on window W between indices START and END.
18074 HL overrides the face for drawing glyph strings, e.g. it is
18075 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
18076 x-positions of the drawing area.
18077
18078 This is an ugly monster macro construct because we must use alloca
18079 to allocate glyph strings (because draw_glyphs can be called
18080 asynchronously). */
18081
18082 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
18083 do \
18084 { \
18085 HEAD = TAIL = NULL; \
18086 while (START < END) \
18087 { \
18088 struct glyph *first_glyph = (row)->glyphs[area] + START; \
18089 switch (first_glyph->type) \
18090 { \
18091 case CHAR_GLYPH: \
18092 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
18093 HL, X, LAST_X); \
18094 break; \
18095 \
18096 case COMPOSITE_GLYPH: \
18097 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
18098 HL, X, LAST_X); \
18099 break; \
18100 \
18101 case STRETCH_GLYPH: \
18102 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
18103 HL, X, LAST_X); \
18104 break; \
18105 \
18106 case IMAGE_GLYPH: \
18107 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
18108 HL, X, LAST_X); \
18109 break; \
18110 \
18111 default: \
18112 abort (); \
18113 } \
18114 \
18115 set_glyph_string_background_width (s, START, LAST_X); \
18116 (X) += s->width; \
18117 } \
18118 } \
18119 while (0)
18120
18121
18122 /* Draw glyphs between START and END in AREA of ROW on window W,
18123 starting at x-position X. X is relative to AREA in W. HL is a
18124 face-override with the following meaning:
18125
18126 DRAW_NORMAL_TEXT draw normally
18127 DRAW_CURSOR draw in cursor face
18128 DRAW_MOUSE_FACE draw in mouse face.
18129 DRAW_INVERSE_VIDEO draw in mode line face
18130 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
18131 DRAW_IMAGE_RAISED draw an image with a raised relief around it
18132
18133 If OVERLAPS_P is non-zero, draw only the foreground of characters
18134 and clip to the physical height of ROW.
18135
18136 Value is the x-position reached, relative to AREA of W. */
18137
18138 static int
18139 draw_glyphs (w, x, row, area, start, end, hl, overlaps_p)
18140 struct window *w;
18141 int x;
18142 struct glyph_row *row;
18143 enum glyph_row_area area;
18144 int start, end;
18145 enum draw_glyphs_face hl;
18146 int overlaps_p;
18147 {
18148 struct glyph_string *head, *tail;
18149 struct glyph_string *s;
18150 int last_x, area_width;
18151 int x_reached;
18152 int i, j;
18153 struct frame *f = XFRAME (WINDOW_FRAME (w));
18154 DECLARE_HDC (hdc);
18155
18156 ALLOCATE_HDC (hdc, f);
18157
18158 /* Let's rather be paranoid than getting a SEGV. */
18159 end = min (end, row->used[area]);
18160 start = max (0, start);
18161 start = min (end, start);
18162
18163 /* Translate X to frame coordinates. Set last_x to the right
18164 end of the drawing area. */
18165 if (row->full_width_p)
18166 {
18167 /* X is relative to the left edge of W, without scroll bars
18168 or fringes. */
18169 x += WINDOW_LEFT_EDGE_X (w);
18170 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
18171 }
18172 else
18173 {
18174 int area_left = window_box_left (w, area);
18175 x += area_left;
18176 area_width = window_box_width (w, area);
18177 last_x = area_left + area_width;
18178 }
18179
18180 /* Build a doubly-linked list of glyph_string structures between
18181 head and tail from what we have to draw. Note that the macro
18182 BUILD_GLYPH_STRINGS will modify its start parameter. That's
18183 the reason we use a separate variable `i'. */
18184 i = start;
18185 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
18186 if (tail)
18187 x_reached = tail->x + tail->background_width;
18188 else
18189 x_reached = x;
18190
18191 /* If there are any glyphs with lbearing < 0 or rbearing > width in
18192 the row, redraw some glyphs in front or following the glyph
18193 strings built above. */
18194 if (head && !overlaps_p && row->contains_overlapping_glyphs_p)
18195 {
18196 int dummy_x = 0;
18197 struct glyph_string *h, *t;
18198
18199 /* Compute overhangs for all glyph strings. */
18200 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
18201 for (s = head; s; s = s->next)
18202 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
18203
18204 /* Prepend glyph strings for glyphs in front of the first glyph
18205 string that are overwritten because of the first glyph
18206 string's left overhang. The background of all strings
18207 prepended must be drawn because the first glyph string
18208 draws over it. */
18209 i = left_overwritten (head);
18210 if (i >= 0)
18211 {
18212 j = i;
18213 BUILD_GLYPH_STRINGS (j, start, h, t,
18214 DRAW_NORMAL_TEXT, dummy_x, last_x);
18215 start = i;
18216 compute_overhangs_and_x (t, head->x, 1);
18217 prepend_glyph_string_lists (&head, &tail, h, t);
18218 }
18219
18220 /* Prepend glyph strings for glyphs in front of the first glyph
18221 string that overwrite that glyph string because of their
18222 right overhang. For these strings, only the foreground must
18223 be drawn, because it draws over the glyph string at `head'.
18224 The background must not be drawn because this would overwrite
18225 right overhangs of preceding glyphs for which no glyph
18226 strings exist. */
18227 i = left_overwriting (head);
18228 if (i >= 0)
18229 {
18230 BUILD_GLYPH_STRINGS (i, start, h, t,
18231 DRAW_NORMAL_TEXT, dummy_x, last_x);
18232 for (s = h; s; s = s->next)
18233 s->background_filled_p = 1;
18234 compute_overhangs_and_x (t, head->x, 1);
18235 prepend_glyph_string_lists (&head, &tail, h, t);
18236 }
18237
18238 /* Append glyphs strings for glyphs following the last glyph
18239 string tail that are overwritten by tail. The background of
18240 these strings has to be drawn because tail's foreground draws
18241 over it. */
18242 i = right_overwritten (tail);
18243 if (i >= 0)
18244 {
18245 BUILD_GLYPH_STRINGS (end, i, h, t,
18246 DRAW_NORMAL_TEXT, x, last_x);
18247 compute_overhangs_and_x (h, tail->x + tail->width, 0);
18248 append_glyph_string_lists (&head, &tail, h, t);
18249 }
18250
18251 /* Append glyph strings for glyphs following the last glyph
18252 string tail that overwrite tail. The foreground of such
18253 glyphs has to be drawn because it writes into the background
18254 of tail. The background must not be drawn because it could
18255 paint over the foreground of following glyphs. */
18256 i = right_overwriting (tail);
18257 if (i >= 0)
18258 {
18259 BUILD_GLYPH_STRINGS (end, i, h, t,
18260 DRAW_NORMAL_TEXT, x, last_x);
18261 for (s = h; s; s = s->next)
18262 s->background_filled_p = 1;
18263 compute_overhangs_and_x (h, tail->x + tail->width, 0);
18264 append_glyph_string_lists (&head, &tail, h, t);
18265 }
18266 }
18267
18268 /* Draw all strings. */
18269 for (s = head; s; s = s->next)
18270 FRAME_RIF (f)->draw_glyph_string (s);
18271
18272 if (area == TEXT_AREA
18273 && !row->full_width_p
18274 /* When drawing overlapping rows, only the glyph strings'
18275 foreground is drawn, which doesn't erase a cursor
18276 completely. */
18277 && !overlaps_p)
18278 {
18279 int x0 = head ? head->x : x;
18280 int x1 = tail ? tail->x + tail->background_width : x;
18281
18282 int text_left = window_box_left (w, TEXT_AREA);
18283 x0 -= text_left;
18284 x1 -= text_left;
18285
18286 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
18287 row->y, MATRIX_ROW_BOTTOM_Y (row));
18288 }
18289
18290 /* Value is the x-position up to which drawn, relative to AREA of W.
18291 This doesn't include parts drawn because of overhangs. */
18292 if (row->full_width_p)
18293 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
18294 else
18295 x_reached -= window_box_left (w, area);
18296
18297 RELEASE_HDC (hdc, f);
18298
18299 return x_reached;
18300 }
18301
18302 /* Expand row matrix if too narrow. Don't expand if area
18303 is not present. */
18304
18305 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
18306 { \
18307 if (!fonts_changed_p \
18308 && (it->glyph_row->glyphs[area] \
18309 < it->glyph_row->glyphs[area + 1])) \
18310 { \
18311 it->w->ncols_scale_factor++; \
18312 fonts_changed_p = 1; \
18313 } \
18314 }
18315
18316 /* Store one glyph for IT->char_to_display in IT->glyph_row.
18317 Called from x_produce_glyphs when IT->glyph_row is non-null. */
18318
18319 static INLINE void
18320 append_glyph (it)
18321 struct it *it;
18322 {
18323 struct glyph *glyph;
18324 enum glyph_row_area area = it->area;
18325
18326 xassert (it->glyph_row);
18327 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
18328
18329 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
18330 if (glyph < it->glyph_row->glyphs[area + 1])
18331 {
18332 glyph->charpos = CHARPOS (it->position);
18333 glyph->object = it->object;
18334 glyph->pixel_width = it->pixel_width;
18335 glyph->ascent = it->ascent;
18336 glyph->descent = it->descent;
18337 glyph->voffset = it->voffset;
18338 glyph->type = CHAR_GLYPH;
18339 glyph->multibyte_p = it->multibyte_p;
18340 glyph->left_box_line_p = it->start_of_box_run_p;
18341 glyph->right_box_line_p = it->end_of_box_run_p;
18342 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
18343 || it->phys_descent > it->descent);
18344 glyph->padding_p = 0;
18345 glyph->glyph_not_available_p = it->glyph_not_available_p;
18346 glyph->face_id = it->face_id;
18347 glyph->u.ch = it->char_to_display;
18348 glyph->slice = null_glyph_slice;
18349 glyph->font_type = FONT_TYPE_UNKNOWN;
18350 ++it->glyph_row->used[area];
18351 }
18352 else
18353 IT_EXPAND_MATRIX_WIDTH (it, area);
18354 }
18355
18356 /* Store one glyph for the composition IT->cmp_id in IT->glyph_row.
18357 Called from x_produce_glyphs when IT->glyph_row is non-null. */
18358
18359 static INLINE void
18360 append_composite_glyph (it)
18361 struct it *it;
18362 {
18363 struct glyph *glyph;
18364 enum glyph_row_area area = it->area;
18365
18366 xassert (it->glyph_row);
18367
18368 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
18369 if (glyph < it->glyph_row->glyphs[area + 1])
18370 {
18371 glyph->charpos = CHARPOS (it->position);
18372 glyph->object = it->object;
18373 glyph->pixel_width = it->pixel_width;
18374 glyph->ascent = it->ascent;
18375 glyph->descent = it->descent;
18376 glyph->voffset = it->voffset;
18377 glyph->type = COMPOSITE_GLYPH;
18378 glyph->multibyte_p = it->multibyte_p;
18379 glyph->left_box_line_p = it->start_of_box_run_p;
18380 glyph->right_box_line_p = it->end_of_box_run_p;
18381 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
18382 || it->phys_descent > it->descent);
18383 glyph->padding_p = 0;
18384 glyph->glyph_not_available_p = 0;
18385 glyph->face_id = it->face_id;
18386 glyph->u.cmp_id = it->cmp_id;
18387 glyph->slice = null_glyph_slice;
18388 glyph->font_type = FONT_TYPE_UNKNOWN;
18389 ++it->glyph_row->used[area];
18390 }
18391 else
18392 IT_EXPAND_MATRIX_WIDTH (it, area);
18393 }
18394
18395
18396 /* Change IT->ascent and IT->height according to the setting of
18397 IT->voffset. */
18398
18399 static INLINE void
18400 take_vertical_position_into_account (it)
18401 struct it *it;
18402 {
18403 if (it->voffset)
18404 {
18405 if (it->voffset < 0)
18406 /* Increase the ascent so that we can display the text higher
18407 in the line. */
18408 it->ascent -= it->voffset;
18409 else
18410 /* Increase the descent so that we can display the text lower
18411 in the line. */
18412 it->descent += it->voffset;
18413 }
18414 }
18415
18416
18417 /* Produce glyphs/get display metrics for the image IT is loaded with.
18418 See the description of struct display_iterator in dispextern.h for
18419 an overview of struct display_iterator. */
18420
18421 static void
18422 produce_image_glyph (it)
18423 struct it *it;
18424 {
18425 struct image *img;
18426 struct face *face;
18427 int glyph_ascent;
18428 struct glyph_slice slice;
18429
18430 xassert (it->what == IT_IMAGE);
18431
18432 face = FACE_FROM_ID (it->f, it->face_id);
18433 xassert (face);
18434 /* Make sure X resources of the face is loaded. */
18435 PREPARE_FACE_FOR_DISPLAY (it->f, face);
18436
18437 if (it->image_id < 0)
18438 {
18439 /* Fringe bitmap. */
18440 it->ascent = it->phys_ascent = 0;
18441 it->descent = it->phys_descent = 0;
18442 it->pixel_width = 0;
18443 it->nglyphs = 0;
18444 return;
18445 }
18446
18447 img = IMAGE_FROM_ID (it->f, it->image_id);
18448 xassert (img);
18449 /* Make sure X resources of the image is loaded. */
18450 prepare_image_for_display (it->f, img);
18451
18452 slice.x = slice.y = 0;
18453 slice.width = img->width;
18454 slice.height = img->height;
18455
18456 if (INTEGERP (it->slice.x))
18457 slice.x = XINT (it->slice.x);
18458 else if (FLOATP (it->slice.x))
18459 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
18460
18461 if (INTEGERP (it->slice.y))
18462 slice.y = XINT (it->slice.y);
18463 else if (FLOATP (it->slice.y))
18464 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
18465
18466 if (INTEGERP (it->slice.width))
18467 slice.width = XINT (it->slice.width);
18468 else if (FLOATP (it->slice.width))
18469 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
18470
18471 if (INTEGERP (it->slice.height))
18472 slice.height = XINT (it->slice.height);
18473 else if (FLOATP (it->slice.height))
18474 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
18475
18476 if (slice.x >= img->width)
18477 slice.x = img->width;
18478 if (slice.y >= img->height)
18479 slice.y = img->height;
18480 if (slice.x + slice.width >= img->width)
18481 slice.width = img->width - slice.x;
18482 if (slice.y + slice.height > img->height)
18483 slice.height = img->height - slice.y;
18484
18485 if (slice.width == 0 || slice.height == 0)
18486 return;
18487
18488 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
18489
18490 it->descent = slice.height - glyph_ascent;
18491 if (slice.y == 0)
18492 it->descent += img->vmargin;
18493 if (slice.y + slice.height == img->height)
18494 it->descent += img->vmargin;
18495 it->phys_descent = it->descent;
18496
18497 it->pixel_width = slice.width;
18498 if (slice.x == 0)
18499 it->pixel_width += img->hmargin;
18500 if (slice.x + slice.width == img->width)
18501 it->pixel_width += img->hmargin;
18502
18503 /* It's quite possible for images to have an ascent greater than
18504 their height, so don't get confused in that case. */
18505 if (it->descent < 0)
18506 it->descent = 0;
18507
18508 #if 0 /* this breaks image tiling */
18509 /* If this glyph is alone on the last line, adjust it.ascent to minimum row ascent. */
18510 int face_ascent = face->font ? FONT_BASE (face->font) : FRAME_BASELINE_OFFSET (it->f);
18511 if (face_ascent > it->ascent)
18512 it->ascent = it->phys_ascent = face_ascent;
18513 #endif
18514
18515 it->nglyphs = 1;
18516
18517 if (face->box != FACE_NO_BOX)
18518 {
18519 if (face->box_line_width > 0)
18520 {
18521 if (slice.y == 0)
18522 it->ascent += face->box_line_width;
18523 if (slice.y + slice.height == img->height)
18524 it->descent += face->box_line_width;
18525 }
18526
18527 if (it->start_of_box_run_p && slice.x == 0)
18528 it->pixel_width += abs (face->box_line_width);
18529 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
18530 it->pixel_width += abs (face->box_line_width);
18531 }
18532
18533 take_vertical_position_into_account (it);
18534
18535 if (it->glyph_row)
18536 {
18537 struct glyph *glyph;
18538 enum glyph_row_area area = it->area;
18539
18540 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
18541 if (glyph < it->glyph_row->glyphs[area + 1])
18542 {
18543 glyph->charpos = CHARPOS (it->position);
18544 glyph->object = it->object;
18545 glyph->pixel_width = it->pixel_width;
18546 glyph->ascent = glyph_ascent;
18547 glyph->descent = it->descent;
18548 glyph->voffset = it->voffset;
18549 glyph->type = IMAGE_GLYPH;
18550 glyph->multibyte_p = it->multibyte_p;
18551 glyph->left_box_line_p = it->start_of_box_run_p;
18552 glyph->right_box_line_p = it->end_of_box_run_p;
18553 glyph->overlaps_vertically_p = 0;
18554 glyph->padding_p = 0;
18555 glyph->glyph_not_available_p = 0;
18556 glyph->face_id = it->face_id;
18557 glyph->u.img_id = img->id;
18558 glyph->slice = slice;
18559 glyph->font_type = FONT_TYPE_UNKNOWN;
18560 ++it->glyph_row->used[area];
18561 }
18562 else
18563 IT_EXPAND_MATRIX_WIDTH (it, area);
18564 }
18565 }
18566
18567
18568 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
18569 of the glyph, WIDTH and HEIGHT are the width and height of the
18570 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
18571
18572 static void
18573 append_stretch_glyph (it, object, width, height, ascent)
18574 struct it *it;
18575 Lisp_Object object;
18576 int width, height;
18577 int ascent;
18578 {
18579 struct glyph *glyph;
18580 enum glyph_row_area area = it->area;
18581
18582 xassert (ascent >= 0 && ascent <= height);
18583
18584 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
18585 if (glyph < it->glyph_row->glyphs[area + 1])
18586 {
18587 glyph->charpos = CHARPOS (it->position);
18588 glyph->object = object;
18589 glyph->pixel_width = width;
18590 glyph->ascent = ascent;
18591 glyph->descent = height - ascent;
18592 glyph->voffset = it->voffset;
18593 glyph->type = STRETCH_GLYPH;
18594 glyph->multibyte_p = it->multibyte_p;
18595 glyph->left_box_line_p = it->start_of_box_run_p;
18596 glyph->right_box_line_p = it->end_of_box_run_p;
18597 glyph->overlaps_vertically_p = 0;
18598 glyph->padding_p = 0;
18599 glyph->glyph_not_available_p = 0;
18600 glyph->face_id = it->face_id;
18601 glyph->u.stretch.ascent = ascent;
18602 glyph->u.stretch.height = height;
18603 glyph->slice = null_glyph_slice;
18604 glyph->font_type = FONT_TYPE_UNKNOWN;
18605 ++it->glyph_row->used[area];
18606 }
18607 else
18608 IT_EXPAND_MATRIX_WIDTH (it, area);
18609 }
18610
18611
18612 /* Produce a stretch glyph for iterator IT. IT->object is the value
18613 of the glyph property displayed. The value must be a list
18614 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
18615 being recognized:
18616
18617 1. `:width WIDTH' specifies that the space should be WIDTH *
18618 canonical char width wide. WIDTH may be an integer or floating
18619 point number.
18620
18621 2. `:relative-width FACTOR' specifies that the width of the stretch
18622 should be computed from the width of the first character having the
18623 `glyph' property, and should be FACTOR times that width.
18624
18625 3. `:align-to HPOS' specifies that the space should be wide enough
18626 to reach HPOS, a value in canonical character units.
18627
18628 Exactly one of the above pairs must be present.
18629
18630 4. `:height HEIGHT' specifies that the height of the stretch produced
18631 should be HEIGHT, measured in canonical character units.
18632
18633 5. `:relative-height FACTOR' specifies that the height of the
18634 stretch should be FACTOR times the height of the characters having
18635 the glyph property.
18636
18637 Either none or exactly one of 4 or 5 must be present.
18638
18639 6. `:ascent ASCENT' specifies that ASCENT percent of the height
18640 of the stretch should be used for the ascent of the stretch.
18641 ASCENT must be in the range 0 <= ASCENT <= 100. */
18642
18643 static void
18644 produce_stretch_glyph (it)
18645 struct it *it;
18646 {
18647 /* (space :width WIDTH :height HEIGHT ...) */
18648 Lisp_Object prop, plist;
18649 int width = 0, height = 0, align_to = -1;
18650 int zero_width_ok_p = 0, zero_height_ok_p = 0;
18651 int ascent = 0;
18652 double tem;
18653 struct face *face = FACE_FROM_ID (it->f, it->face_id);
18654 XFontStruct *font = face->font ? face->font : FRAME_FONT (it->f);
18655
18656 PREPARE_FACE_FOR_DISPLAY (it->f, face);
18657
18658 /* List should start with `space'. */
18659 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
18660 plist = XCDR (it->object);
18661
18662 /* Compute the width of the stretch. */
18663 if ((prop = Fsafe_plist_get (plist, QCwidth), !NILP (prop))
18664 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
18665 {
18666 /* Absolute width `:width WIDTH' specified and valid. */
18667 zero_width_ok_p = 1;
18668 width = (int)tem;
18669 }
18670 else if (prop = Fsafe_plist_get (plist, QCrelative_width),
18671 NUMVAL (prop) > 0)
18672 {
18673 /* Relative width `:relative-width FACTOR' specified and valid.
18674 Compute the width of the characters having the `glyph'
18675 property. */
18676 struct it it2;
18677 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
18678
18679 it2 = *it;
18680 if (it->multibyte_p)
18681 {
18682 int maxlen = ((IT_BYTEPOS (*it) >= GPT ? ZV : GPT)
18683 - IT_BYTEPOS (*it));
18684 it2.c = STRING_CHAR_AND_LENGTH (p, maxlen, it2.len);
18685 }
18686 else
18687 it2.c = *p, it2.len = 1;
18688
18689 it2.glyph_row = NULL;
18690 it2.what = IT_CHARACTER;
18691 x_produce_glyphs (&it2);
18692 width = NUMVAL (prop) * it2.pixel_width;
18693 }
18694 else if ((prop = Fsafe_plist_get (plist, QCalign_to), !NILP (prop))
18695 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
18696 {
18697 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
18698 align_to = (align_to < 0
18699 ? 0
18700 : align_to - window_box_left_offset (it->w, TEXT_AREA));
18701 else if (align_to < 0)
18702 align_to = window_box_left_offset (it->w, TEXT_AREA);
18703 width = max (0, (int)tem + align_to - it->current_x);
18704 zero_width_ok_p = 1;
18705 }
18706 else
18707 /* Nothing specified -> width defaults to canonical char width. */
18708 width = FRAME_COLUMN_WIDTH (it->f);
18709
18710 if (width <= 0 && (width < 0 || !zero_width_ok_p))
18711 width = 1;
18712
18713 /* Compute height. */
18714 if ((prop = Fsafe_plist_get (plist, QCheight), !NILP (prop))
18715 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
18716 {
18717 height = (int)tem;
18718 zero_height_ok_p = 1;
18719 }
18720 else if (prop = Fsafe_plist_get (plist, QCrelative_height),
18721 NUMVAL (prop) > 0)
18722 height = FONT_HEIGHT (font) * NUMVAL (prop);
18723 else
18724 height = FONT_HEIGHT (font);
18725
18726 if (height <= 0 && (height < 0 || !zero_height_ok_p))
18727 height = 1;
18728
18729 /* Compute percentage of height used for ascent. If
18730 `:ascent ASCENT' is present and valid, use that. Otherwise,
18731 derive the ascent from the font in use. */
18732 if (prop = Fsafe_plist_get (plist, QCascent),
18733 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
18734 ascent = height * NUMVAL (prop) / 100.0;
18735 else if (!NILP (prop)
18736 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
18737 ascent = min (max (0, (int)tem), height);
18738 else
18739 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
18740
18741 if (width > 0 && height > 0 && it->glyph_row)
18742 {
18743 Lisp_Object object = it->stack[it->sp - 1].string;
18744 if (!STRINGP (object))
18745 object = it->w->buffer;
18746 append_stretch_glyph (it, object, width, height, ascent);
18747 }
18748
18749 it->pixel_width = width;
18750 it->ascent = it->phys_ascent = ascent;
18751 it->descent = it->phys_descent = height - it->ascent;
18752 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
18753
18754 if (width > 0 && height > 0 && face->box != FACE_NO_BOX)
18755 {
18756 if (face->box_line_width > 0)
18757 {
18758 it->ascent += face->box_line_width;
18759 it->descent += face->box_line_width;
18760 }
18761
18762 if (it->start_of_box_run_p)
18763 it->pixel_width += abs (face->box_line_width);
18764 if (it->end_of_box_run_p)
18765 it->pixel_width += abs (face->box_line_width);
18766 }
18767
18768 take_vertical_position_into_account (it);
18769 }
18770
18771 /* Calculate line-height and line-spacing properties.
18772 An integer value specifies explicit pixel value.
18773 A float value specifies relative value to current face height.
18774 A cons (float . face-name) specifies relative value to
18775 height of specified face font.
18776
18777 Returns height in pixels, or nil. */
18778
18779 static Lisp_Object
18780 calc_line_height_property (it, prop, font, boff, total)
18781 struct it *it;
18782 Lisp_Object prop;
18783 XFontStruct *font;
18784 int boff, *total;
18785 {
18786 Lisp_Object position, val;
18787 Lisp_Object face_name = Qnil;
18788 int ascent, descent, height, override;
18789
18790 if (STRINGP (it->object))
18791 position = make_number (IT_STRING_CHARPOS (*it));
18792 else if (BUFFERP (it->object))
18793 position = make_number (IT_CHARPOS (*it));
18794 else
18795 return Qnil;
18796
18797 val = Fget_char_property (position, prop, it->object);
18798
18799 if (NILP (val))
18800 return val;
18801
18802 if (total && CONSP (val) && EQ (XCAR (val), Qtotal))
18803 {
18804 *total = 1;
18805 val = XCDR (val);
18806 }
18807
18808 if (INTEGERP (val))
18809 return val;
18810
18811 if (CONSP (val))
18812 {
18813 face_name = XCDR (val);
18814 val = XCAR (val);
18815 }
18816 else if (SYMBOLP (val))
18817 {
18818 face_name = val;
18819 val = Qnil;
18820 }
18821
18822 override = EQ (prop, Qline_height);
18823
18824 if (NILP (face_name))
18825 {
18826 font = FRAME_FONT (it->f);
18827 boff = FRAME_BASELINE_OFFSET (it->f);
18828 }
18829 else if (EQ (face_name, Qt))
18830 {
18831 override = 0;
18832 }
18833 else
18834 {
18835 int face_id;
18836 struct face *face;
18837 struct font_info *font_info;
18838
18839 face_id = lookup_named_face (it->f, face_name, ' ', 0);
18840 if (face_id < 0)
18841 return make_number (-1);
18842
18843 face = FACE_FROM_ID (it->f, face_id);
18844 font = face->font;
18845 if (font == NULL)
18846 return make_number (-1);
18847
18848 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
18849 boff = font_info->baseline_offset;
18850 if (font_info->vertical_centering)
18851 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
18852 }
18853
18854 ascent = FONT_BASE (font) + boff;
18855 descent = FONT_DESCENT (font) - boff;
18856
18857 if (override)
18858 {
18859 it->override_ascent = ascent;
18860 it->override_descent = descent;
18861 it->override_boff = boff;
18862 }
18863
18864 height = ascent + descent;
18865 if (FLOATP (val))
18866 height = (int)(XFLOAT_DATA (val) * height);
18867 else if (INTEGERP (val))
18868 height *= XINT (val);
18869
18870 return make_number (height);
18871 }
18872
18873
18874 /* RIF:
18875 Produce glyphs/get display metrics for the display element IT is
18876 loaded with. See the description of struct display_iterator in
18877 dispextern.h for an overview of struct display_iterator. */
18878
18879 void
18880 x_produce_glyphs (it)
18881 struct it *it;
18882 {
18883 int extra_line_spacing = it->extra_line_spacing;
18884
18885 it->glyph_not_available_p = 0;
18886
18887 if (it->what == IT_CHARACTER)
18888 {
18889 XChar2b char2b;
18890 XFontStruct *font;
18891 struct face *face = FACE_FROM_ID (it->f, it->face_id);
18892 XCharStruct *pcm;
18893 int font_not_found_p;
18894 struct font_info *font_info;
18895 int boff; /* baseline offset */
18896 /* We may change it->multibyte_p upon unibyte<->multibyte
18897 conversion. So, save the current value now and restore it
18898 later.
18899
18900 Note: It seems that we don't have to record multibyte_p in
18901 struct glyph because the character code itself tells if or
18902 not the character is multibyte. Thus, in the future, we must
18903 consider eliminating the field `multibyte_p' in the struct
18904 glyph. */
18905 int saved_multibyte_p = it->multibyte_p;
18906
18907 /* Maybe translate single-byte characters to multibyte, or the
18908 other way. */
18909 it->char_to_display = it->c;
18910 if (!ASCII_BYTE_P (it->c))
18911 {
18912 if (unibyte_display_via_language_environment
18913 && SINGLE_BYTE_CHAR_P (it->c)
18914 && (it->c >= 0240
18915 || !NILP (Vnonascii_translation_table)))
18916 {
18917 it->char_to_display = unibyte_char_to_multibyte (it->c);
18918 it->multibyte_p = 1;
18919 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
18920 face = FACE_FROM_ID (it->f, it->face_id);
18921 }
18922 else if (!SINGLE_BYTE_CHAR_P (it->c)
18923 && !it->multibyte_p)
18924 {
18925 it->multibyte_p = 1;
18926 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
18927 face = FACE_FROM_ID (it->f, it->face_id);
18928 }
18929 }
18930
18931 /* Get font to use. Encode IT->char_to_display. */
18932 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
18933 &char2b, it->multibyte_p, 0);
18934 font = face->font;
18935
18936 /* When no suitable font found, use the default font. */
18937 font_not_found_p = font == NULL;
18938 if (font_not_found_p)
18939 {
18940 font = FRAME_FONT (it->f);
18941 boff = FRAME_BASELINE_OFFSET (it->f);
18942 font_info = NULL;
18943 }
18944 else
18945 {
18946 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
18947 boff = font_info->baseline_offset;
18948 if (font_info->vertical_centering)
18949 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
18950 }
18951
18952 if (it->char_to_display >= ' '
18953 && (!it->multibyte_p || it->char_to_display < 128))
18954 {
18955 /* Either unibyte or ASCII. */
18956 int stretched_p;
18957
18958 it->nglyphs = 1;
18959
18960 pcm = FRAME_RIF (it->f)->per_char_metric
18961 (font, &char2b, FONT_TYPE_FOR_UNIBYTE (font, it->char_to_display));
18962
18963 if (it->override_ascent >= 0)
18964 {
18965 it->ascent = it->override_ascent;
18966 it->descent = it->override_descent;
18967 boff = it->override_boff;
18968 }
18969 else
18970 {
18971 it->ascent = FONT_BASE (font) + boff;
18972 it->descent = FONT_DESCENT (font) - boff;
18973 }
18974
18975 if (pcm)
18976 {
18977 it->phys_ascent = pcm->ascent + boff;
18978 it->phys_descent = pcm->descent - boff;
18979 it->pixel_width = pcm->width;
18980 }
18981 else
18982 {
18983 it->glyph_not_available_p = 1;
18984 it->phys_ascent = it->ascent;
18985 it->phys_descent = it->descent;
18986 it->pixel_width = FONT_WIDTH (font);
18987 }
18988
18989 if (it->constrain_row_ascent_descent_p)
18990 {
18991 if (it->descent > it->max_descent)
18992 {
18993 it->ascent += it->descent - it->max_descent;
18994 it->descent = it->max_descent;
18995 }
18996 if (it->ascent > it->max_ascent)
18997 {
18998 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
18999 it->ascent = it->max_ascent;
19000 }
19001 it->phys_ascent = min (it->phys_ascent, it->ascent);
19002 it->phys_descent = min (it->phys_descent, it->descent);
19003 extra_line_spacing = 0;
19004 }
19005
19006 /* If this is a space inside a region of text with
19007 `space-width' property, change its width. */
19008 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
19009 if (stretched_p)
19010 it->pixel_width *= XFLOATINT (it->space_width);
19011
19012 /* If face has a box, add the box thickness to the character
19013 height. If character has a box line to the left and/or
19014 right, add the box line width to the character's width. */
19015 if (face->box != FACE_NO_BOX)
19016 {
19017 int thick = face->box_line_width;
19018
19019 if (thick > 0)
19020 {
19021 it->ascent += thick;
19022 it->descent += thick;
19023 }
19024 else
19025 thick = -thick;
19026
19027 if (it->start_of_box_run_p)
19028 it->pixel_width += thick;
19029 if (it->end_of_box_run_p)
19030 it->pixel_width += thick;
19031 }
19032
19033 /* If face has an overline, add the height of the overline
19034 (1 pixel) and a 1 pixel margin to the character height. */
19035 if (face->overline_p)
19036 it->ascent += 2;
19037
19038 if (it->constrain_row_ascent_descent_p)
19039 {
19040 if (it->ascent > it->max_ascent)
19041 it->ascent = it->max_ascent;
19042 if (it->descent > it->max_descent)
19043 it->descent = it->max_descent;
19044 }
19045
19046 take_vertical_position_into_account (it);
19047
19048 /* If we have to actually produce glyphs, do it. */
19049 if (it->glyph_row)
19050 {
19051 if (stretched_p)
19052 {
19053 /* Translate a space with a `space-width' property
19054 into a stretch glyph. */
19055 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
19056 / FONT_HEIGHT (font));
19057 append_stretch_glyph (it, it->object, it->pixel_width,
19058 it->ascent + it->descent, ascent);
19059 }
19060 else
19061 append_glyph (it);
19062
19063 /* If characters with lbearing or rbearing are displayed
19064 in this line, record that fact in a flag of the
19065 glyph row. This is used to optimize X output code. */
19066 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
19067 it->glyph_row->contains_overlapping_glyphs_p = 1;
19068 }
19069 }
19070 else if (it->char_to_display == '\n')
19071 {
19072 /* A newline has no width but we need the height of the line.
19073 But if previous part of the line set a height, don't
19074 increase that height */
19075
19076 Lisp_Object height;
19077
19078 it->override_ascent = -1;
19079 it->pixel_width = 0;
19080 it->nglyphs = 0;
19081
19082 height = calc_line_height_property(it, Qline_height, font, boff, 0);
19083
19084 if (it->override_ascent >= 0)
19085 {
19086 it->ascent = it->override_ascent;
19087 it->descent = it->override_descent;
19088 boff = it->override_boff;
19089 }
19090 else
19091 {
19092 it->ascent = FONT_BASE (font) + boff;
19093 it->descent = FONT_DESCENT (font) - boff;
19094 }
19095
19096 if (EQ (height, make_number(0)))
19097 {
19098 if (it->descent > it->max_descent)
19099 {
19100 it->ascent += it->descent - it->max_descent;
19101 it->descent = it->max_descent;
19102 }
19103 if (it->ascent > it->max_ascent)
19104 {
19105 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
19106 it->ascent = it->max_ascent;
19107 }
19108 it->phys_ascent = min (it->phys_ascent, it->ascent);
19109 it->phys_descent = min (it->phys_descent, it->descent);
19110 it->constrain_row_ascent_descent_p = 1;
19111 extra_line_spacing = 0;
19112 }
19113 else
19114 {
19115 Lisp_Object spacing;
19116 int total = 0;
19117
19118 it->phys_ascent = it->ascent;
19119 it->phys_descent = it->descent;
19120
19121 if ((it->max_ascent > 0 || it->max_descent > 0)
19122 && face->box != FACE_NO_BOX
19123 && face->box_line_width > 0)
19124 {
19125 it->ascent += face->box_line_width;
19126 it->descent += face->box_line_width;
19127 }
19128 if (!NILP (height)
19129 && XINT (height) > it->ascent + it->descent)
19130 it->ascent = XINT (height) - it->descent;
19131
19132 spacing = calc_line_height_property(it, Qline_spacing, font, boff, &total);
19133 if (INTEGERP (spacing))
19134 {
19135 extra_line_spacing = XINT (spacing);
19136 if (total)
19137 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
19138 }
19139 }
19140 }
19141 else if (it->char_to_display == '\t')
19142 {
19143 int tab_width = it->tab_width * FRAME_COLUMN_WIDTH (it->f);
19144 int x = it->current_x + it->continuation_lines_width;
19145 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
19146
19147 /* If the distance from the current position to the next tab
19148 stop is less than a canonical character width, use the
19149 tab stop after that. */
19150 if (next_tab_x - x < FRAME_COLUMN_WIDTH (it->f))
19151 next_tab_x += tab_width;
19152
19153 it->pixel_width = next_tab_x - x;
19154 it->nglyphs = 1;
19155 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
19156 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
19157
19158 if (it->glyph_row)
19159 {
19160 append_stretch_glyph (it, it->object, it->pixel_width,
19161 it->ascent + it->descent, it->ascent);
19162 }
19163 }
19164 else
19165 {
19166 /* A multi-byte character. Assume that the display width of the
19167 character is the width of the character multiplied by the
19168 width of the font. */
19169
19170 /* If we found a font, this font should give us the right
19171 metrics. If we didn't find a font, use the frame's
19172 default font and calculate the width of the character
19173 from the charset width; this is what old redisplay code
19174 did. */
19175
19176 pcm = FRAME_RIF (it->f)->per_char_metric (font, &char2b,
19177 FONT_TYPE_FOR_MULTIBYTE (font, it->c));
19178
19179 if (font_not_found_p || !pcm)
19180 {
19181 int charset = CHAR_CHARSET (it->char_to_display);
19182
19183 it->glyph_not_available_p = 1;
19184 it->pixel_width = (FRAME_COLUMN_WIDTH (it->f)
19185 * CHARSET_WIDTH (charset));
19186 it->phys_ascent = FONT_BASE (font) + boff;
19187 it->phys_descent = FONT_DESCENT (font) - boff;
19188 }
19189 else
19190 {
19191 it->pixel_width = pcm->width;
19192 it->phys_ascent = pcm->ascent + boff;
19193 it->phys_descent = pcm->descent - boff;
19194 if (it->glyph_row
19195 && (pcm->lbearing < 0
19196 || pcm->rbearing > pcm->width))
19197 it->glyph_row->contains_overlapping_glyphs_p = 1;
19198 }
19199 it->nglyphs = 1;
19200 it->ascent = FONT_BASE (font) + boff;
19201 it->descent = FONT_DESCENT (font) - boff;
19202 if (face->box != FACE_NO_BOX)
19203 {
19204 int thick = face->box_line_width;
19205
19206 if (thick > 0)
19207 {
19208 it->ascent += thick;
19209 it->descent += thick;
19210 }
19211 else
19212 thick = - thick;
19213
19214 if (it->start_of_box_run_p)
19215 it->pixel_width += thick;
19216 if (it->end_of_box_run_p)
19217 it->pixel_width += thick;
19218 }
19219
19220 /* If face has an overline, add the height of the overline
19221 (1 pixel) and a 1 pixel margin to the character height. */
19222 if (face->overline_p)
19223 it->ascent += 2;
19224
19225 take_vertical_position_into_account (it);
19226
19227 if (it->glyph_row)
19228 append_glyph (it);
19229 }
19230 it->multibyte_p = saved_multibyte_p;
19231 }
19232 else if (it->what == IT_COMPOSITION)
19233 {
19234 /* Note: A composition is represented as one glyph in the
19235 glyph matrix. There are no padding glyphs. */
19236 XChar2b char2b;
19237 XFontStruct *font;
19238 struct face *face = FACE_FROM_ID (it->f, it->face_id);
19239 XCharStruct *pcm;
19240 int font_not_found_p;
19241 struct font_info *font_info;
19242 int boff; /* baseline offset */
19243 struct composition *cmp = composition_table[it->cmp_id];
19244
19245 /* Maybe translate single-byte characters to multibyte. */
19246 it->char_to_display = it->c;
19247 if (unibyte_display_via_language_environment
19248 && SINGLE_BYTE_CHAR_P (it->c)
19249 && (it->c >= 0240
19250 || (it->c >= 0200
19251 && !NILP (Vnonascii_translation_table))))
19252 {
19253 it->char_to_display = unibyte_char_to_multibyte (it->c);
19254 }
19255
19256 /* Get face and font to use. Encode IT->char_to_display. */
19257 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
19258 face = FACE_FROM_ID (it->f, it->face_id);
19259 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
19260 &char2b, it->multibyte_p, 0);
19261 font = face->font;
19262
19263 /* When no suitable font found, use the default font. */
19264 font_not_found_p = font == NULL;
19265 if (font_not_found_p)
19266 {
19267 font = FRAME_FONT (it->f);
19268 boff = FRAME_BASELINE_OFFSET (it->f);
19269 font_info = NULL;
19270 }
19271 else
19272 {
19273 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
19274 boff = font_info->baseline_offset;
19275 if (font_info->vertical_centering)
19276 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
19277 }
19278
19279 /* There are no padding glyphs, so there is only one glyph to
19280 produce for the composition. Important is that pixel_width,
19281 ascent and descent are the values of what is drawn by
19282 draw_glyphs (i.e. the values of the overall glyphs composed). */
19283 it->nglyphs = 1;
19284
19285 /* If we have not yet calculated pixel size data of glyphs of
19286 the composition for the current face font, calculate them
19287 now. Theoretically, we have to check all fonts for the
19288 glyphs, but that requires much time and memory space. So,
19289 here we check only the font of the first glyph. This leads
19290 to incorrect display very rarely, and C-l (recenter) can
19291 correct the display anyway. */
19292 if (cmp->font != (void *) font)
19293 {
19294 /* Ascent and descent of the font of the first character of
19295 this composition (adjusted by baseline offset). Ascent
19296 and descent of overall glyphs should not be less than
19297 them respectively. */
19298 int font_ascent = FONT_BASE (font) + boff;
19299 int font_descent = FONT_DESCENT (font) - boff;
19300 /* Bounding box of the overall glyphs. */
19301 int leftmost, rightmost, lowest, highest;
19302 int i, width, ascent, descent;
19303
19304 cmp->font = (void *) font;
19305
19306 /* Initialize the bounding box. */
19307 if (font_info
19308 && (pcm = FRAME_RIF (it->f)->per_char_metric (font, &char2b,
19309 FONT_TYPE_FOR_MULTIBYTE (font, it->c))))
19310 {
19311 width = pcm->width;
19312 ascent = pcm->ascent;
19313 descent = pcm->descent;
19314 }
19315 else
19316 {
19317 width = FONT_WIDTH (font);
19318 ascent = FONT_BASE (font);
19319 descent = FONT_DESCENT (font);
19320 }
19321
19322 rightmost = width;
19323 lowest = - descent + boff;
19324 highest = ascent + boff;
19325 leftmost = 0;
19326
19327 if (font_info
19328 && font_info->default_ascent
19329 && CHAR_TABLE_P (Vuse_default_ascent)
19330 && !NILP (Faref (Vuse_default_ascent,
19331 make_number (it->char_to_display))))
19332 highest = font_info->default_ascent + boff;
19333
19334 /* Draw the first glyph at the normal position. It may be
19335 shifted to right later if some other glyphs are drawn at
19336 the left. */
19337 cmp->offsets[0] = 0;
19338 cmp->offsets[1] = boff;
19339
19340 /* Set cmp->offsets for the remaining glyphs. */
19341 for (i = 1; i < cmp->glyph_len; i++)
19342 {
19343 int left, right, btm, top;
19344 int ch = COMPOSITION_GLYPH (cmp, i);
19345 int face_id = FACE_FOR_CHAR (it->f, face, ch);
19346
19347 face = FACE_FROM_ID (it->f, face_id);
19348 get_char_face_and_encoding (it->f, ch, face->id,
19349 &char2b, it->multibyte_p, 0);
19350 font = face->font;
19351 if (font == NULL)
19352 {
19353 font = FRAME_FONT (it->f);
19354 boff = FRAME_BASELINE_OFFSET (it->f);
19355 font_info = NULL;
19356 }
19357 else
19358 {
19359 font_info
19360 = FONT_INFO_FROM_ID (it->f, face->font_info_id);
19361 boff = font_info->baseline_offset;
19362 if (font_info->vertical_centering)
19363 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
19364 }
19365
19366 if (font_info
19367 && (pcm = FRAME_RIF (it->f)->per_char_metric (font, &char2b,
19368 FONT_TYPE_FOR_MULTIBYTE (font, ch))))
19369 {
19370 width = pcm->width;
19371 ascent = pcm->ascent;
19372 descent = pcm->descent;
19373 }
19374 else
19375 {
19376 width = FONT_WIDTH (font);
19377 ascent = 1;
19378 descent = 0;
19379 }
19380
19381 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
19382 {
19383 /* Relative composition with or without
19384 alternate chars. */
19385 left = (leftmost + rightmost - width) / 2;
19386 btm = - descent + boff;
19387 if (font_info && font_info->relative_compose
19388 && (! CHAR_TABLE_P (Vignore_relative_composition)
19389 || NILP (Faref (Vignore_relative_composition,
19390 make_number (ch)))))
19391 {
19392
19393 if (- descent >= font_info->relative_compose)
19394 /* One extra pixel between two glyphs. */
19395 btm = highest + 1;
19396 else if (ascent <= 0)
19397 /* One extra pixel between two glyphs. */
19398 btm = lowest - 1 - ascent - descent;
19399 }
19400 }
19401 else
19402 {
19403 /* A composition rule is specified by an integer
19404 value that encodes global and new reference
19405 points (GREF and NREF). GREF and NREF are
19406 specified by numbers as below:
19407
19408 0---1---2 -- ascent
19409 | |
19410 | |
19411 | |
19412 9--10--11 -- center
19413 | |
19414 ---3---4---5--- baseline
19415 | |
19416 6---7---8 -- descent
19417 */
19418 int rule = COMPOSITION_RULE (cmp, i);
19419 int gref, nref, grefx, grefy, nrefx, nrefy;
19420
19421 COMPOSITION_DECODE_RULE (rule, gref, nref);
19422 grefx = gref % 3, nrefx = nref % 3;
19423 grefy = gref / 3, nrefy = nref / 3;
19424
19425 left = (leftmost
19426 + grefx * (rightmost - leftmost) / 2
19427 - nrefx * width / 2);
19428 btm = ((grefy == 0 ? highest
19429 : grefy == 1 ? 0
19430 : grefy == 2 ? lowest
19431 : (highest + lowest) / 2)
19432 - (nrefy == 0 ? ascent + descent
19433 : nrefy == 1 ? descent - boff
19434 : nrefy == 2 ? 0
19435 : (ascent + descent) / 2));
19436 }
19437
19438 cmp->offsets[i * 2] = left;
19439 cmp->offsets[i * 2 + 1] = btm + descent;
19440
19441 /* Update the bounding box of the overall glyphs. */
19442 right = left + width;
19443 top = btm + descent + ascent;
19444 if (left < leftmost)
19445 leftmost = left;
19446 if (right > rightmost)
19447 rightmost = right;
19448 if (top > highest)
19449 highest = top;
19450 if (btm < lowest)
19451 lowest = btm;
19452 }
19453
19454 /* If there are glyphs whose x-offsets are negative,
19455 shift all glyphs to the right and make all x-offsets
19456 non-negative. */
19457 if (leftmost < 0)
19458 {
19459 for (i = 0; i < cmp->glyph_len; i++)
19460 cmp->offsets[i * 2] -= leftmost;
19461 rightmost -= leftmost;
19462 }
19463
19464 cmp->pixel_width = rightmost;
19465 cmp->ascent = highest;
19466 cmp->descent = - lowest;
19467 if (cmp->ascent < font_ascent)
19468 cmp->ascent = font_ascent;
19469 if (cmp->descent < font_descent)
19470 cmp->descent = font_descent;
19471 }
19472
19473 it->pixel_width = cmp->pixel_width;
19474 it->ascent = it->phys_ascent = cmp->ascent;
19475 it->descent = it->phys_descent = cmp->descent;
19476
19477 if (face->box != FACE_NO_BOX)
19478 {
19479 int thick = face->box_line_width;
19480
19481 if (thick > 0)
19482 {
19483 it->ascent += thick;
19484 it->descent += thick;
19485 }
19486 else
19487 thick = - thick;
19488
19489 if (it->start_of_box_run_p)
19490 it->pixel_width += thick;
19491 if (it->end_of_box_run_p)
19492 it->pixel_width += thick;
19493 }
19494
19495 /* If face has an overline, add the height of the overline
19496 (1 pixel) and a 1 pixel margin to the character height. */
19497 if (face->overline_p)
19498 it->ascent += 2;
19499
19500 take_vertical_position_into_account (it);
19501
19502 if (it->glyph_row)
19503 append_composite_glyph (it);
19504 }
19505 else if (it->what == IT_IMAGE)
19506 produce_image_glyph (it);
19507 else if (it->what == IT_STRETCH)
19508 produce_stretch_glyph (it);
19509
19510 /* Accumulate dimensions. Note: can't assume that it->descent > 0
19511 because this isn't true for images with `:ascent 100'. */
19512 xassert (it->ascent >= 0 && it->descent >= 0);
19513 if (it->area == TEXT_AREA)
19514 it->current_x += it->pixel_width;
19515
19516 if (extra_line_spacing > 0)
19517 {
19518 it->descent += extra_line_spacing;
19519 if (extra_line_spacing > it->max_extra_line_spacing)
19520 it->max_extra_line_spacing = extra_line_spacing;
19521 }
19522
19523 it->max_ascent = max (it->max_ascent, it->ascent);
19524 it->max_descent = max (it->max_descent, it->descent);
19525 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
19526 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
19527 }
19528
19529 /* EXPORT for RIF:
19530 Output LEN glyphs starting at START at the nominal cursor position.
19531 Advance the nominal cursor over the text. The global variable
19532 updated_window contains the window being updated, updated_row is
19533 the glyph row being updated, and updated_area is the area of that
19534 row being updated. */
19535
19536 void
19537 x_write_glyphs (start, len)
19538 struct glyph *start;
19539 int len;
19540 {
19541 int x, hpos;
19542
19543 xassert (updated_window && updated_row);
19544 BLOCK_INPUT;
19545
19546 /* Write glyphs. */
19547
19548 hpos = start - updated_row->glyphs[updated_area];
19549 x = draw_glyphs (updated_window, output_cursor.x,
19550 updated_row, updated_area,
19551 hpos, hpos + len,
19552 DRAW_NORMAL_TEXT, 0);
19553
19554 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
19555 if (updated_area == TEXT_AREA
19556 && updated_window->phys_cursor_on_p
19557 && updated_window->phys_cursor.vpos == output_cursor.vpos
19558 && updated_window->phys_cursor.hpos >= hpos
19559 && updated_window->phys_cursor.hpos < hpos + len)
19560 updated_window->phys_cursor_on_p = 0;
19561
19562 UNBLOCK_INPUT;
19563
19564 /* Advance the output cursor. */
19565 output_cursor.hpos += len;
19566 output_cursor.x = x;
19567 }
19568
19569
19570 /* EXPORT for RIF:
19571 Insert LEN glyphs from START at the nominal cursor position. */
19572
19573 void
19574 x_insert_glyphs (start, len)
19575 struct glyph *start;
19576 int len;
19577 {
19578 struct frame *f;
19579 struct window *w;
19580 int line_height, shift_by_width, shifted_region_width;
19581 struct glyph_row *row;
19582 struct glyph *glyph;
19583 int frame_x, frame_y, hpos;
19584
19585 xassert (updated_window && updated_row);
19586 BLOCK_INPUT;
19587 w = updated_window;
19588 f = XFRAME (WINDOW_FRAME (w));
19589
19590 /* Get the height of the line we are in. */
19591 row = updated_row;
19592 line_height = row->height;
19593
19594 /* Get the width of the glyphs to insert. */
19595 shift_by_width = 0;
19596 for (glyph = start; glyph < start + len; ++glyph)
19597 shift_by_width += glyph->pixel_width;
19598
19599 /* Get the width of the region to shift right. */
19600 shifted_region_width = (window_box_width (w, updated_area)
19601 - output_cursor.x
19602 - shift_by_width);
19603
19604 /* Shift right. */
19605 frame_x = window_box_left (w, updated_area) + output_cursor.x;
19606 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
19607
19608 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
19609 line_height, shift_by_width);
19610
19611 /* Write the glyphs. */
19612 hpos = start - row->glyphs[updated_area];
19613 draw_glyphs (w, output_cursor.x, row, updated_area,
19614 hpos, hpos + len,
19615 DRAW_NORMAL_TEXT, 0);
19616
19617 /* Advance the output cursor. */
19618 output_cursor.hpos += len;
19619 output_cursor.x += shift_by_width;
19620 UNBLOCK_INPUT;
19621 }
19622
19623
19624 /* EXPORT for RIF:
19625 Erase the current text line from the nominal cursor position
19626 (inclusive) to pixel column TO_X (exclusive). The idea is that
19627 everything from TO_X onward is already erased.
19628
19629 TO_X is a pixel position relative to updated_area of
19630 updated_window. TO_X == -1 means clear to the end of this area. */
19631
19632 void
19633 x_clear_end_of_line (to_x)
19634 int to_x;
19635 {
19636 struct frame *f;
19637 struct window *w = updated_window;
19638 int max_x, min_y, max_y;
19639 int from_x, from_y, to_y;
19640
19641 xassert (updated_window && updated_row);
19642 f = XFRAME (w->frame);
19643
19644 if (updated_row->full_width_p)
19645 max_x = WINDOW_TOTAL_WIDTH (w);
19646 else
19647 max_x = window_box_width (w, updated_area);
19648 max_y = window_text_bottom_y (w);
19649
19650 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
19651 of window. For TO_X > 0, truncate to end of drawing area. */
19652 if (to_x == 0)
19653 return;
19654 else if (to_x < 0)
19655 to_x = max_x;
19656 else
19657 to_x = min (to_x, max_x);
19658
19659 to_y = min (max_y, output_cursor.y + updated_row->height);
19660
19661 /* Notice if the cursor will be cleared by this operation. */
19662 if (!updated_row->full_width_p)
19663 notice_overwritten_cursor (w, updated_area,
19664 output_cursor.x, -1,
19665 updated_row->y,
19666 MATRIX_ROW_BOTTOM_Y (updated_row));
19667
19668 from_x = output_cursor.x;
19669
19670 /* Translate to frame coordinates. */
19671 if (updated_row->full_width_p)
19672 {
19673 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
19674 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
19675 }
19676 else
19677 {
19678 int area_left = window_box_left (w, updated_area);
19679 from_x += area_left;
19680 to_x += area_left;
19681 }
19682
19683 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
19684 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
19685 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
19686
19687 /* Prevent inadvertently clearing to end of the X window. */
19688 if (to_x > from_x && to_y > from_y)
19689 {
19690 BLOCK_INPUT;
19691 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
19692 to_x - from_x, to_y - from_y);
19693 UNBLOCK_INPUT;
19694 }
19695 }
19696
19697 #endif /* HAVE_WINDOW_SYSTEM */
19698
19699
19700 \f
19701 /***********************************************************************
19702 Cursor types
19703 ***********************************************************************/
19704
19705 /* Value is the internal representation of the specified cursor type
19706 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
19707 of the bar cursor. */
19708
19709 static enum text_cursor_kinds
19710 get_specified_cursor_type (arg, width)
19711 Lisp_Object arg;
19712 int *width;
19713 {
19714 enum text_cursor_kinds type;
19715
19716 if (NILP (arg))
19717 return NO_CURSOR;
19718
19719 if (EQ (arg, Qbox))
19720 return FILLED_BOX_CURSOR;
19721
19722 if (EQ (arg, Qhollow))
19723 return HOLLOW_BOX_CURSOR;
19724
19725 if (EQ (arg, Qbar))
19726 {
19727 *width = 2;
19728 return BAR_CURSOR;
19729 }
19730
19731 if (CONSP (arg)
19732 && EQ (XCAR (arg), Qbar)
19733 && INTEGERP (XCDR (arg))
19734 && XINT (XCDR (arg)) >= 0)
19735 {
19736 *width = XINT (XCDR (arg));
19737 return BAR_CURSOR;
19738 }
19739
19740 if (EQ (arg, Qhbar))
19741 {
19742 *width = 2;
19743 return HBAR_CURSOR;
19744 }
19745
19746 if (CONSP (arg)
19747 && EQ (XCAR (arg), Qhbar)
19748 && INTEGERP (XCDR (arg))
19749 && XINT (XCDR (arg)) >= 0)
19750 {
19751 *width = XINT (XCDR (arg));
19752 return HBAR_CURSOR;
19753 }
19754
19755 /* Treat anything unknown as "hollow box cursor".
19756 It was bad to signal an error; people have trouble fixing
19757 .Xdefaults with Emacs, when it has something bad in it. */
19758 type = HOLLOW_BOX_CURSOR;
19759
19760 return type;
19761 }
19762
19763 /* Set the default cursor types for specified frame. */
19764 void
19765 set_frame_cursor_types (f, arg)
19766 struct frame *f;
19767 Lisp_Object arg;
19768 {
19769 int width;
19770 Lisp_Object tem;
19771
19772 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
19773 FRAME_CURSOR_WIDTH (f) = width;
19774
19775 /* By default, set up the blink-off state depending on the on-state. */
19776
19777 tem = Fassoc (arg, Vblink_cursor_alist);
19778 if (!NILP (tem))
19779 {
19780 FRAME_BLINK_OFF_CURSOR (f)
19781 = get_specified_cursor_type (XCDR (tem), &width);
19782 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
19783 }
19784 else
19785 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
19786 }
19787
19788
19789 /* Return the cursor we want to be displayed in window W. Return
19790 width of bar/hbar cursor through WIDTH arg. Return with
19791 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
19792 (i.e. if the `system caret' should track this cursor).
19793
19794 In a mini-buffer window, we want the cursor only to appear if we
19795 are reading input from this window. For the selected window, we
19796 want the cursor type given by the frame parameter or buffer local
19797 setting of cursor-type. If explicitly marked off, draw no cursor.
19798 In all other cases, we want a hollow box cursor. */
19799
19800 static enum text_cursor_kinds
19801 get_window_cursor_type (w, glyph, width, active_cursor)
19802 struct window *w;
19803 struct glyph *glyph;
19804 int *width;
19805 int *active_cursor;
19806 {
19807 struct frame *f = XFRAME (w->frame);
19808 struct buffer *b = XBUFFER (w->buffer);
19809 int cursor_type = DEFAULT_CURSOR;
19810 Lisp_Object alt_cursor;
19811 int non_selected = 0;
19812
19813 *active_cursor = 1;
19814
19815 /* Echo area */
19816 if (cursor_in_echo_area
19817 && FRAME_HAS_MINIBUF_P (f)
19818 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
19819 {
19820 if (w == XWINDOW (echo_area_window))
19821 {
19822 *width = FRAME_CURSOR_WIDTH (f);
19823 return FRAME_DESIRED_CURSOR (f);
19824 }
19825
19826 *active_cursor = 0;
19827 non_selected = 1;
19828 }
19829
19830 /* Nonselected window or nonselected frame. */
19831 else if (w != XWINDOW (f->selected_window)
19832 #ifdef HAVE_WINDOW_SYSTEM
19833 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame
19834 #endif
19835 )
19836 {
19837 *active_cursor = 0;
19838
19839 if (MINI_WINDOW_P (w) && minibuf_level == 0)
19840 return NO_CURSOR;
19841
19842 non_selected = 1;
19843 }
19844
19845 /* Never display a cursor in a window in which cursor-type is nil. */
19846 if (NILP (b->cursor_type))
19847 return NO_CURSOR;
19848
19849 /* Use cursor-in-non-selected-windows for non-selected window or frame. */
19850 if (non_selected)
19851 {
19852 alt_cursor = Fbuffer_local_value (Qcursor_in_non_selected_windows, w->buffer);
19853 return get_specified_cursor_type (alt_cursor, width);
19854 }
19855
19856 /* Get the normal cursor type for this window. */
19857 if (EQ (b->cursor_type, Qt))
19858 {
19859 cursor_type = FRAME_DESIRED_CURSOR (f);
19860 *width = FRAME_CURSOR_WIDTH (f);
19861 }
19862 else
19863 cursor_type = get_specified_cursor_type (b->cursor_type, width);
19864
19865 /* Use normal cursor if not blinked off. */
19866 if (!w->cursor_off_p)
19867 {
19868 if (glyph != NULL && glyph->type == IMAGE_GLYPH) {
19869 if (cursor_type == FILLED_BOX_CURSOR)
19870 cursor_type = HOLLOW_BOX_CURSOR;
19871 }
19872 return cursor_type;
19873 }
19874
19875 /* Cursor is blinked off, so determine how to "toggle" it. */
19876
19877 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
19878 if ((alt_cursor = Fassoc (b->cursor_type, Vblink_cursor_alist), !NILP (alt_cursor)))
19879 return get_specified_cursor_type (XCDR (alt_cursor), width);
19880
19881 /* Then see if frame has specified a specific blink off cursor type. */
19882 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
19883 {
19884 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
19885 return FRAME_BLINK_OFF_CURSOR (f);
19886 }
19887
19888 #if 0
19889 /* Some people liked having a permanently visible blinking cursor,
19890 while others had very strong opinions against it. So it was
19891 decided to remove it. KFS 2003-09-03 */
19892
19893 /* Finally perform built-in cursor blinking:
19894 filled box <-> hollow box
19895 wide [h]bar <-> narrow [h]bar
19896 narrow [h]bar <-> no cursor
19897 other type <-> no cursor */
19898
19899 if (cursor_type == FILLED_BOX_CURSOR)
19900 return HOLLOW_BOX_CURSOR;
19901
19902 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
19903 {
19904 *width = 1;
19905 return cursor_type;
19906 }
19907 #endif
19908
19909 return NO_CURSOR;
19910 }
19911
19912
19913 #ifdef HAVE_WINDOW_SYSTEM
19914
19915 /* Notice when the text cursor of window W has been completely
19916 overwritten by a drawing operation that outputs glyphs in AREA
19917 starting at X0 and ending at X1 in the line starting at Y0 and
19918 ending at Y1. X coordinates are area-relative. X1 < 0 means all
19919 the rest of the line after X0 has been written. Y coordinates
19920 are window-relative. */
19921
19922 static void
19923 notice_overwritten_cursor (w, area, x0, x1, y0, y1)
19924 struct window *w;
19925 enum glyph_row_area area;
19926 int x0, y0, x1, y1;
19927 {
19928 int cx0, cx1, cy0, cy1;
19929 struct glyph_row *row;
19930
19931 if (!w->phys_cursor_on_p)
19932 return;
19933 if (area != TEXT_AREA)
19934 return;
19935
19936 row = w->current_matrix->rows + w->phys_cursor.vpos;
19937 if (!row->displays_text_p)
19938 return;
19939
19940 if (row->cursor_in_fringe_p)
19941 {
19942 row->cursor_in_fringe_p = 0;
19943 draw_fringe_bitmap (w, row, 0);
19944 w->phys_cursor_on_p = 0;
19945 return;
19946 }
19947
19948 cx0 = w->phys_cursor.x;
19949 cx1 = cx0 + w->phys_cursor_width;
19950 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
19951 return;
19952
19953 /* The cursor image will be completely removed from the
19954 screen if the output area intersects the cursor area in
19955 y-direction. When we draw in [y0 y1[, and some part of
19956 the cursor is at y < y0, that part must have been drawn
19957 before. When scrolling, the cursor is erased before
19958 actually scrolling, so we don't come here. When not
19959 scrolling, the rows above the old cursor row must have
19960 changed, and in this case these rows must have written
19961 over the cursor image.
19962
19963 Likewise if part of the cursor is below y1, with the
19964 exception of the cursor being in the first blank row at
19965 the buffer and window end because update_text_area
19966 doesn't draw that row. (Except when it does, but
19967 that's handled in update_text_area.) */
19968
19969 cy0 = w->phys_cursor.y;
19970 cy1 = cy0 + w->phys_cursor_height;
19971 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
19972 return;
19973
19974 w->phys_cursor_on_p = 0;
19975 }
19976
19977 #endif /* HAVE_WINDOW_SYSTEM */
19978
19979 \f
19980 /************************************************************************
19981 Mouse Face
19982 ************************************************************************/
19983
19984 #ifdef HAVE_WINDOW_SYSTEM
19985
19986 /* EXPORT for RIF:
19987 Fix the display of area AREA of overlapping row ROW in window W. */
19988
19989 void
19990 x_fix_overlapping_area (w, row, area)
19991 struct window *w;
19992 struct glyph_row *row;
19993 enum glyph_row_area area;
19994 {
19995 int i, x;
19996
19997 BLOCK_INPUT;
19998
19999 x = 0;
20000 for (i = 0; i < row->used[area];)
20001 {
20002 if (row->glyphs[area][i].overlaps_vertically_p)
20003 {
20004 int start = i, start_x = x;
20005
20006 do
20007 {
20008 x += row->glyphs[area][i].pixel_width;
20009 ++i;
20010 }
20011 while (i < row->used[area]
20012 && row->glyphs[area][i].overlaps_vertically_p);
20013
20014 draw_glyphs (w, start_x, row, area,
20015 start, i,
20016 DRAW_NORMAL_TEXT, 1);
20017 }
20018 else
20019 {
20020 x += row->glyphs[area][i].pixel_width;
20021 ++i;
20022 }
20023 }
20024
20025 UNBLOCK_INPUT;
20026 }
20027
20028
20029 /* EXPORT:
20030 Draw the cursor glyph of window W in glyph row ROW. See the
20031 comment of draw_glyphs for the meaning of HL. */
20032
20033 void
20034 draw_phys_cursor_glyph (w, row, hl)
20035 struct window *w;
20036 struct glyph_row *row;
20037 enum draw_glyphs_face hl;
20038 {
20039 /* If cursor hpos is out of bounds, don't draw garbage. This can
20040 happen in mini-buffer windows when switching between echo area
20041 glyphs and mini-buffer. */
20042 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
20043 {
20044 int on_p = w->phys_cursor_on_p;
20045 int x1;
20046 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
20047 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
20048 hl, 0);
20049 w->phys_cursor_on_p = on_p;
20050
20051 if (hl == DRAW_CURSOR)
20052 w->phys_cursor_width = x1 - w->phys_cursor.x;
20053 /* When we erase the cursor, and ROW is overlapped by other
20054 rows, make sure that these overlapping parts of other rows
20055 are redrawn. */
20056 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
20057 {
20058 if (row > w->current_matrix->rows
20059 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
20060 x_fix_overlapping_area (w, row - 1, TEXT_AREA);
20061
20062 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
20063 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
20064 x_fix_overlapping_area (w, row + 1, TEXT_AREA);
20065 }
20066 }
20067 }
20068
20069
20070 /* EXPORT:
20071 Erase the image of a cursor of window W from the screen. */
20072
20073 void
20074 erase_phys_cursor (w)
20075 struct window *w;
20076 {
20077 struct frame *f = XFRAME (w->frame);
20078 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
20079 int hpos = w->phys_cursor.hpos;
20080 int vpos = w->phys_cursor.vpos;
20081 int mouse_face_here_p = 0;
20082 struct glyph_matrix *active_glyphs = w->current_matrix;
20083 struct glyph_row *cursor_row;
20084 struct glyph *cursor_glyph;
20085 enum draw_glyphs_face hl;
20086
20087 /* No cursor displayed or row invalidated => nothing to do on the
20088 screen. */
20089 if (w->phys_cursor_type == NO_CURSOR)
20090 goto mark_cursor_off;
20091
20092 /* VPOS >= active_glyphs->nrows means that window has been resized.
20093 Don't bother to erase the cursor. */
20094 if (vpos >= active_glyphs->nrows)
20095 goto mark_cursor_off;
20096
20097 /* If row containing cursor is marked invalid, there is nothing we
20098 can do. */
20099 cursor_row = MATRIX_ROW (active_glyphs, vpos);
20100 if (!cursor_row->enabled_p)
20101 goto mark_cursor_off;
20102
20103 /* If line spacing is > 0, old cursor may only be partially visible in
20104 window after split-window. So adjust visible height. */
20105 cursor_row->visible_height = min (cursor_row->visible_height,
20106 window_text_bottom_y (w) - cursor_row->y);
20107
20108 /* If row is completely invisible, don't attempt to delete a cursor which
20109 isn't there. This can happen if cursor is at top of a window, and
20110 we switch to a buffer with a header line in that window. */
20111 if (cursor_row->visible_height <= 0)
20112 goto mark_cursor_off;
20113
20114 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
20115 if (cursor_row->cursor_in_fringe_p)
20116 {
20117 cursor_row->cursor_in_fringe_p = 0;
20118 draw_fringe_bitmap (w, cursor_row, 0);
20119 goto mark_cursor_off;
20120 }
20121
20122 /* This can happen when the new row is shorter than the old one.
20123 In this case, either draw_glyphs or clear_end_of_line
20124 should have cleared the cursor. Note that we wouldn't be
20125 able to erase the cursor in this case because we don't have a
20126 cursor glyph at hand. */
20127 if (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])
20128 goto mark_cursor_off;
20129
20130 /* If the cursor is in the mouse face area, redisplay that when
20131 we clear the cursor. */
20132 if (! NILP (dpyinfo->mouse_face_window)
20133 && w == XWINDOW (dpyinfo->mouse_face_window)
20134 && (vpos > dpyinfo->mouse_face_beg_row
20135 || (vpos == dpyinfo->mouse_face_beg_row
20136 && hpos >= dpyinfo->mouse_face_beg_col))
20137 && (vpos < dpyinfo->mouse_face_end_row
20138 || (vpos == dpyinfo->mouse_face_end_row
20139 && hpos < dpyinfo->mouse_face_end_col))
20140 /* Don't redraw the cursor's spot in mouse face if it is at the
20141 end of a line (on a newline). The cursor appears there, but
20142 mouse highlighting does not. */
20143 && cursor_row->used[TEXT_AREA] > hpos)
20144 mouse_face_here_p = 1;
20145
20146 /* Maybe clear the display under the cursor. */
20147 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
20148 {
20149 int x, y;
20150 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
20151 int width;
20152
20153 cursor_glyph = get_phys_cursor_glyph (w);
20154 if (cursor_glyph == NULL)
20155 goto mark_cursor_off;
20156
20157 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
20158 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
20159 width = min (cursor_glyph->pixel_width,
20160 window_box_width (w, TEXT_AREA) - w->phys_cursor.x);
20161
20162 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
20163 }
20164
20165 /* Erase the cursor by redrawing the character underneath it. */
20166 if (mouse_face_here_p)
20167 hl = DRAW_MOUSE_FACE;
20168 else
20169 hl = DRAW_NORMAL_TEXT;
20170 draw_phys_cursor_glyph (w, cursor_row, hl);
20171
20172 mark_cursor_off:
20173 w->phys_cursor_on_p = 0;
20174 w->phys_cursor_type = NO_CURSOR;
20175 }
20176
20177
20178 /* EXPORT:
20179 Display or clear cursor of window W. If ON is zero, clear the
20180 cursor. If it is non-zero, display the cursor. If ON is nonzero,
20181 where to put the cursor is specified by HPOS, VPOS, X and Y. */
20182
20183 void
20184 display_and_set_cursor (w, on, hpos, vpos, x, y)
20185 struct window *w;
20186 int on, hpos, vpos, x, y;
20187 {
20188 struct frame *f = XFRAME (w->frame);
20189 int new_cursor_type;
20190 int new_cursor_width;
20191 int active_cursor;
20192 struct glyph_row *glyph_row;
20193 struct glyph *glyph;
20194
20195 /* This is pointless on invisible frames, and dangerous on garbaged
20196 windows and frames; in the latter case, the frame or window may
20197 be in the midst of changing its size, and x and y may be off the
20198 window. */
20199 if (! FRAME_VISIBLE_P (f)
20200 || FRAME_GARBAGED_P (f)
20201 || vpos >= w->current_matrix->nrows
20202 || hpos >= w->current_matrix->matrix_w)
20203 return;
20204
20205 /* If cursor is off and we want it off, return quickly. */
20206 if (!on && !w->phys_cursor_on_p)
20207 return;
20208
20209 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
20210 /* If cursor row is not enabled, we don't really know where to
20211 display the cursor. */
20212 if (!glyph_row->enabled_p)
20213 {
20214 w->phys_cursor_on_p = 0;
20215 return;
20216 }
20217
20218 glyph = NULL;
20219 if (!glyph_row->exact_window_width_line_p
20220 || hpos < glyph_row->used[TEXT_AREA])
20221 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
20222
20223 xassert (interrupt_input_blocked);
20224
20225 /* Set new_cursor_type to the cursor we want to be displayed. */
20226 new_cursor_type = get_window_cursor_type (w, glyph,
20227 &new_cursor_width, &active_cursor);
20228
20229 /* If cursor is currently being shown and we don't want it to be or
20230 it is in the wrong place, or the cursor type is not what we want,
20231 erase it. */
20232 if (w->phys_cursor_on_p
20233 && (!on
20234 || w->phys_cursor.x != x
20235 || w->phys_cursor.y != y
20236 || new_cursor_type != w->phys_cursor_type
20237 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
20238 && new_cursor_width != w->phys_cursor_width)))
20239 erase_phys_cursor (w);
20240
20241 /* Don't check phys_cursor_on_p here because that flag is only set
20242 to zero in some cases where we know that the cursor has been
20243 completely erased, to avoid the extra work of erasing the cursor
20244 twice. In other words, phys_cursor_on_p can be 1 and the cursor
20245 still not be visible, or it has only been partly erased. */
20246 if (on)
20247 {
20248 w->phys_cursor_ascent = glyph_row->ascent;
20249 w->phys_cursor_height = glyph_row->height;
20250
20251 /* Set phys_cursor_.* before x_draw_.* is called because some
20252 of them may need the information. */
20253 w->phys_cursor.x = x;
20254 w->phys_cursor.y = glyph_row->y;
20255 w->phys_cursor.hpos = hpos;
20256 w->phys_cursor.vpos = vpos;
20257 }
20258
20259 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
20260 new_cursor_type, new_cursor_width,
20261 on, active_cursor);
20262 }
20263
20264
20265 /* Switch the display of W's cursor on or off, according to the value
20266 of ON. */
20267
20268 static void
20269 update_window_cursor (w, on)
20270 struct window *w;
20271 int on;
20272 {
20273 /* Don't update cursor in windows whose frame is in the process
20274 of being deleted. */
20275 if (w->current_matrix)
20276 {
20277 BLOCK_INPUT;
20278 display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
20279 w->phys_cursor.x, w->phys_cursor.y);
20280 UNBLOCK_INPUT;
20281 }
20282 }
20283
20284
20285 /* Call update_window_cursor with parameter ON_P on all leaf windows
20286 in the window tree rooted at W. */
20287
20288 static void
20289 update_cursor_in_window_tree (w, on_p)
20290 struct window *w;
20291 int on_p;
20292 {
20293 while (w)
20294 {
20295 if (!NILP (w->hchild))
20296 update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
20297 else if (!NILP (w->vchild))
20298 update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
20299 else
20300 update_window_cursor (w, on_p);
20301
20302 w = NILP (w->next) ? 0 : XWINDOW (w->next);
20303 }
20304 }
20305
20306
20307 /* EXPORT:
20308 Display the cursor on window W, or clear it, according to ON_P.
20309 Don't change the cursor's position. */
20310
20311 void
20312 x_update_cursor (f, on_p)
20313 struct frame *f;
20314 int on_p;
20315 {
20316 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
20317 }
20318
20319
20320 /* EXPORT:
20321 Clear the cursor of window W to background color, and mark the
20322 cursor as not shown. This is used when the text where the cursor
20323 is is about to be rewritten. */
20324
20325 void
20326 x_clear_cursor (w)
20327 struct window *w;
20328 {
20329 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
20330 update_window_cursor (w, 0);
20331 }
20332
20333
20334 /* EXPORT:
20335 Display the active region described by mouse_face_* according to DRAW. */
20336
20337 void
20338 show_mouse_face (dpyinfo, draw)
20339 Display_Info *dpyinfo;
20340 enum draw_glyphs_face draw;
20341 {
20342 struct window *w = XWINDOW (dpyinfo->mouse_face_window);
20343 struct frame *f = XFRAME (WINDOW_FRAME (w));
20344
20345 if (/* If window is in the process of being destroyed, don't bother
20346 to do anything. */
20347 w->current_matrix != NULL
20348 /* Don't update mouse highlight if hidden */
20349 && (draw != DRAW_MOUSE_FACE || !dpyinfo->mouse_face_hidden)
20350 /* Recognize when we are called to operate on rows that don't exist
20351 anymore. This can happen when a window is split. */
20352 && dpyinfo->mouse_face_end_row < w->current_matrix->nrows)
20353 {
20354 int phys_cursor_on_p = w->phys_cursor_on_p;
20355 struct glyph_row *row, *first, *last;
20356
20357 first = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_beg_row);
20358 last = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_end_row);
20359
20360 for (row = first; row <= last && row->enabled_p; ++row)
20361 {
20362 int start_hpos, end_hpos, start_x;
20363
20364 /* For all but the first row, the highlight starts at column 0. */
20365 if (row == first)
20366 {
20367 start_hpos = dpyinfo->mouse_face_beg_col;
20368 start_x = dpyinfo->mouse_face_beg_x;
20369 }
20370 else
20371 {
20372 start_hpos = 0;
20373 start_x = 0;
20374 }
20375
20376 if (row == last)
20377 end_hpos = dpyinfo->mouse_face_end_col;
20378 else
20379 end_hpos = row->used[TEXT_AREA];
20380
20381 if (end_hpos > start_hpos)
20382 {
20383 draw_glyphs (w, start_x, row, TEXT_AREA,
20384 start_hpos, end_hpos,
20385 draw, 0);
20386
20387 row->mouse_face_p
20388 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
20389 }
20390 }
20391
20392 /* When we've written over the cursor, arrange for it to
20393 be displayed again. */
20394 if (phys_cursor_on_p && !w->phys_cursor_on_p)
20395 {
20396 BLOCK_INPUT;
20397 display_and_set_cursor (w, 1,
20398 w->phys_cursor.hpos, w->phys_cursor.vpos,
20399 w->phys_cursor.x, w->phys_cursor.y);
20400 UNBLOCK_INPUT;
20401 }
20402 }
20403
20404 /* Change the mouse cursor. */
20405 if (draw == DRAW_NORMAL_TEXT)
20406 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
20407 else if (draw == DRAW_MOUSE_FACE)
20408 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
20409 else
20410 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
20411 }
20412
20413 /* EXPORT:
20414 Clear out the mouse-highlighted active region.
20415 Redraw it un-highlighted first. Value is non-zero if mouse
20416 face was actually drawn unhighlighted. */
20417
20418 int
20419 clear_mouse_face (dpyinfo)
20420 Display_Info *dpyinfo;
20421 {
20422 int cleared = 0;
20423
20424 if (!dpyinfo->mouse_face_hidden && !NILP (dpyinfo->mouse_face_window))
20425 {
20426 show_mouse_face (dpyinfo, DRAW_NORMAL_TEXT);
20427 cleared = 1;
20428 }
20429
20430 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
20431 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
20432 dpyinfo->mouse_face_window = Qnil;
20433 dpyinfo->mouse_face_overlay = Qnil;
20434 return cleared;
20435 }
20436
20437
20438 /* EXPORT:
20439 Non-zero if physical cursor of window W is within mouse face. */
20440
20441 int
20442 cursor_in_mouse_face_p (w)
20443 struct window *w;
20444 {
20445 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
20446 int in_mouse_face = 0;
20447
20448 if (WINDOWP (dpyinfo->mouse_face_window)
20449 && XWINDOW (dpyinfo->mouse_face_window) == w)
20450 {
20451 int hpos = w->phys_cursor.hpos;
20452 int vpos = w->phys_cursor.vpos;
20453
20454 if (vpos >= dpyinfo->mouse_face_beg_row
20455 && vpos <= dpyinfo->mouse_face_end_row
20456 && (vpos > dpyinfo->mouse_face_beg_row
20457 || hpos >= dpyinfo->mouse_face_beg_col)
20458 && (vpos < dpyinfo->mouse_face_end_row
20459 || hpos < dpyinfo->mouse_face_end_col
20460 || dpyinfo->mouse_face_past_end))
20461 in_mouse_face = 1;
20462 }
20463
20464 return in_mouse_face;
20465 }
20466
20467
20468
20469 \f
20470 /* Find the glyph matrix position of buffer position CHARPOS in window
20471 *W. HPOS, *VPOS, *X, and *Y are set to the positions found. W's
20472 current glyphs must be up to date. If CHARPOS is above window
20473 start return (0, 0, 0, 0). If CHARPOS is after end of W, return end
20474 of last line in W. In the row containing CHARPOS, stop before glyphs
20475 having STOP as object. */
20476
20477 #if 1 /* This is a version of fast_find_position that's more correct
20478 in the presence of hscrolling, for example. I didn't install
20479 it right away because the problem fixed is minor, it failed
20480 in 20.x as well, and I think it's too risky to install
20481 so near the release of 21.1. 2001-09-25 gerd. */
20482
20483 static int
20484 fast_find_position (w, charpos, hpos, vpos, x, y, stop)
20485 struct window *w;
20486 int charpos;
20487 int *hpos, *vpos, *x, *y;
20488 Lisp_Object stop;
20489 {
20490 struct glyph_row *row, *first;
20491 struct glyph *glyph, *end;
20492 int past_end = 0;
20493
20494 first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
20495 if (charpos < MATRIX_ROW_START_CHARPOS (first))
20496 {
20497 *x = first->x;
20498 *y = first->y;
20499 *hpos = 0;
20500 *vpos = MATRIX_ROW_VPOS (first, w->current_matrix);
20501 return 1;
20502 }
20503
20504 row = row_containing_pos (w, charpos, first, NULL, 0);
20505 if (row == NULL)
20506 {
20507 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
20508 past_end = 1;
20509 }
20510
20511 *x = row->x;
20512 *y = row->y;
20513 *vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
20514
20515 glyph = row->glyphs[TEXT_AREA];
20516 end = glyph + row->used[TEXT_AREA];
20517
20518 /* Skip over glyphs not having an object at the start of the row.
20519 These are special glyphs like truncation marks on terminal
20520 frames. */
20521 if (row->displays_text_p)
20522 while (glyph < end
20523 && INTEGERP (glyph->object)
20524 && !EQ (stop, glyph->object)
20525 && glyph->charpos < 0)
20526 {
20527 *x += glyph->pixel_width;
20528 ++glyph;
20529 }
20530
20531 while (glyph < end
20532 && !INTEGERP (glyph->object)
20533 && !EQ (stop, glyph->object)
20534 && (!BUFFERP (glyph->object)
20535 || glyph->charpos < charpos))
20536 {
20537 *x += glyph->pixel_width;
20538 ++glyph;
20539 }
20540
20541 *hpos = glyph - row->glyphs[TEXT_AREA];
20542 return !past_end;
20543 }
20544
20545 #else /* not 1 */
20546
20547 static int
20548 fast_find_position (w, pos, hpos, vpos, x, y, stop)
20549 struct window *w;
20550 int pos;
20551 int *hpos, *vpos, *x, *y;
20552 Lisp_Object stop;
20553 {
20554 int i;
20555 int lastcol;
20556 int maybe_next_line_p = 0;
20557 int line_start_position;
20558 int yb = window_text_bottom_y (w);
20559 struct glyph_row *row, *best_row;
20560 int row_vpos, best_row_vpos;
20561 int current_x;
20562
20563 row = best_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
20564 row_vpos = best_row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
20565
20566 while (row->y < yb)
20567 {
20568 if (row->used[TEXT_AREA])
20569 line_start_position = row->glyphs[TEXT_AREA]->charpos;
20570 else
20571 line_start_position = 0;
20572
20573 if (line_start_position > pos)
20574 break;
20575 /* If the position sought is the end of the buffer,
20576 don't include the blank lines at the bottom of the window. */
20577 else if (line_start_position == pos
20578 && pos == BUF_ZV (XBUFFER (w->buffer)))
20579 {
20580 maybe_next_line_p = 1;
20581 break;
20582 }
20583 else if (line_start_position > 0)
20584 {
20585 best_row = row;
20586 best_row_vpos = row_vpos;
20587 }
20588
20589 if (row->y + row->height >= yb)
20590 break;
20591
20592 ++row;
20593 ++row_vpos;
20594 }
20595
20596 /* Find the right column within BEST_ROW. */
20597 lastcol = 0;
20598 current_x = best_row->x;
20599 for (i = 0; i < best_row->used[TEXT_AREA]; i++)
20600 {
20601 struct glyph *glyph = best_row->glyphs[TEXT_AREA] + i;
20602 int charpos = glyph->charpos;
20603
20604 if (BUFFERP (glyph->object))
20605 {
20606 if (charpos == pos)
20607 {
20608 *hpos = i;
20609 *vpos = best_row_vpos;
20610 *x = current_x;
20611 *y = best_row->y;
20612 return 1;
20613 }
20614 else if (charpos > pos)
20615 break;
20616 }
20617 else if (EQ (glyph->object, stop))
20618 break;
20619
20620 if (charpos > 0)
20621 lastcol = i;
20622 current_x += glyph->pixel_width;
20623 }
20624
20625 /* If we're looking for the end of the buffer,
20626 and we didn't find it in the line we scanned,
20627 use the start of the following line. */
20628 if (maybe_next_line_p)
20629 {
20630 ++best_row;
20631 ++best_row_vpos;
20632 lastcol = 0;
20633 current_x = best_row->x;
20634 }
20635
20636 *vpos = best_row_vpos;
20637 *hpos = lastcol + 1;
20638 *x = current_x;
20639 *y = best_row->y;
20640 return 0;
20641 }
20642
20643 #endif /* not 1 */
20644
20645
20646 /* Find the position of the glyph for position POS in OBJECT in
20647 window W's current matrix, and return in *X, *Y the pixel
20648 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
20649
20650 RIGHT_P non-zero means return the position of the right edge of the
20651 glyph, RIGHT_P zero means return the left edge position.
20652
20653 If no glyph for POS exists in the matrix, return the position of
20654 the glyph with the next smaller position that is in the matrix, if
20655 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
20656 exists in the matrix, return the position of the glyph with the
20657 next larger position in OBJECT.
20658
20659 Value is non-zero if a glyph was found. */
20660
20661 static int
20662 fast_find_string_pos (w, pos, object, hpos, vpos, x, y, right_p)
20663 struct window *w;
20664 int pos;
20665 Lisp_Object object;
20666 int *hpos, *vpos, *x, *y;
20667 int right_p;
20668 {
20669 int yb = window_text_bottom_y (w);
20670 struct glyph_row *r;
20671 struct glyph *best_glyph = NULL;
20672 struct glyph_row *best_row = NULL;
20673 int best_x = 0;
20674
20675 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
20676 r->enabled_p && r->y < yb;
20677 ++r)
20678 {
20679 struct glyph *g = r->glyphs[TEXT_AREA];
20680 struct glyph *e = g + r->used[TEXT_AREA];
20681 int gx;
20682
20683 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
20684 if (EQ (g->object, object))
20685 {
20686 if (g->charpos == pos)
20687 {
20688 best_glyph = g;
20689 best_x = gx;
20690 best_row = r;
20691 goto found;
20692 }
20693 else if (best_glyph == NULL
20694 || ((abs (g->charpos - pos)
20695 < abs (best_glyph->charpos - pos))
20696 && (right_p
20697 ? g->charpos < pos
20698 : g->charpos > pos)))
20699 {
20700 best_glyph = g;
20701 best_x = gx;
20702 best_row = r;
20703 }
20704 }
20705 }
20706
20707 found:
20708
20709 if (best_glyph)
20710 {
20711 *x = best_x;
20712 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
20713
20714 if (right_p)
20715 {
20716 *x += best_glyph->pixel_width;
20717 ++*hpos;
20718 }
20719
20720 *y = best_row->y;
20721 *vpos = best_row - w->current_matrix->rows;
20722 }
20723
20724 return best_glyph != NULL;
20725 }
20726
20727
20728 /* See if position X, Y is within a hot-spot of an image. */
20729
20730 static int
20731 on_hot_spot_p (hot_spot, x, y)
20732 Lisp_Object hot_spot;
20733 int x, y;
20734 {
20735 if (!CONSP (hot_spot))
20736 return 0;
20737
20738 if (EQ (XCAR (hot_spot), Qrect))
20739 {
20740 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
20741 Lisp_Object rect = XCDR (hot_spot);
20742 Lisp_Object tem;
20743 if (!CONSP (rect))
20744 return 0;
20745 if (!CONSP (XCAR (rect)))
20746 return 0;
20747 if (!CONSP (XCDR (rect)))
20748 return 0;
20749 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
20750 return 0;
20751 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
20752 return 0;
20753 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
20754 return 0;
20755 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
20756 return 0;
20757 return 1;
20758 }
20759 else if (EQ (XCAR (hot_spot), Qcircle))
20760 {
20761 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
20762 Lisp_Object circ = XCDR (hot_spot);
20763 Lisp_Object lr, lx0, ly0;
20764 if (CONSP (circ)
20765 && CONSP (XCAR (circ))
20766 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
20767 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
20768 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
20769 {
20770 double r = XFLOATINT (lr);
20771 double dx = XINT (lx0) - x;
20772 double dy = XINT (ly0) - y;
20773 return (dx * dx + dy * dy <= r * r);
20774 }
20775 }
20776 else if (EQ (XCAR (hot_spot), Qpoly))
20777 {
20778 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
20779 if (VECTORP (XCDR (hot_spot)))
20780 {
20781 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
20782 Lisp_Object *poly = v->contents;
20783 int n = v->size;
20784 int i;
20785 int inside = 0;
20786 Lisp_Object lx, ly;
20787 int x0, y0;
20788
20789 /* Need an even number of coordinates, and at least 3 edges. */
20790 if (n < 6 || n & 1)
20791 return 0;
20792
20793 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
20794 If count is odd, we are inside polygon. Pixels on edges
20795 may or may not be included depending on actual geometry of the
20796 polygon. */
20797 if ((lx = poly[n-2], !INTEGERP (lx))
20798 || (ly = poly[n-1], !INTEGERP (lx)))
20799 return 0;
20800 x0 = XINT (lx), y0 = XINT (ly);
20801 for (i = 0; i < n; i += 2)
20802 {
20803 int x1 = x0, y1 = y0;
20804 if ((lx = poly[i], !INTEGERP (lx))
20805 || (ly = poly[i+1], !INTEGERP (ly)))
20806 return 0;
20807 x0 = XINT (lx), y0 = XINT (ly);
20808
20809 /* Does this segment cross the X line? */
20810 if (x0 >= x)
20811 {
20812 if (x1 >= x)
20813 continue;
20814 }
20815 else if (x1 < x)
20816 continue;
20817 if (y > y0 && y > y1)
20818 continue;
20819 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
20820 inside = !inside;
20821 }
20822 return inside;
20823 }
20824 }
20825 return 0;
20826 }
20827
20828 Lisp_Object
20829 find_hot_spot (map, x, y)
20830 Lisp_Object map;
20831 int x, y;
20832 {
20833 while (CONSP (map))
20834 {
20835 if (CONSP (XCAR (map))
20836 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
20837 return XCAR (map);
20838 map = XCDR (map);
20839 }
20840
20841 return Qnil;
20842 }
20843
20844 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
20845 3, 3, 0,
20846 doc: /* Lookup in image map MAP coordinates X and Y.
20847 An image map is an alist where each element has the format (AREA ID PLIST).
20848 An AREA is specified as either a rectangle, a circle, or a polygon:
20849 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
20850 pixel coordinates of the upper left and bottom right corners.
20851 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
20852 and the radius of the circle; r may be a float or integer.
20853 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
20854 vector describes one corner in the polygon.
20855 Returns the alist element for the first matching AREA in MAP. */)
20856 (map, x, y)
20857 Lisp_Object map;
20858 Lisp_Object x, y;
20859 {
20860 if (NILP (map))
20861 return Qnil;
20862
20863 CHECK_NUMBER (x);
20864 CHECK_NUMBER (y);
20865
20866 return find_hot_spot (map, XINT (x), XINT (y));
20867 }
20868
20869
20870 /* Display frame CURSOR, optionally using shape defined by POINTER. */
20871 static void
20872 define_frame_cursor1 (f, cursor, pointer)
20873 struct frame *f;
20874 Cursor cursor;
20875 Lisp_Object pointer;
20876 {
20877 /* Do not change cursor shape while dragging mouse. */
20878 if (!NILP (do_mouse_tracking))
20879 return;
20880
20881 if (!NILP (pointer))
20882 {
20883 if (EQ (pointer, Qarrow))
20884 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
20885 else if (EQ (pointer, Qhand))
20886 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
20887 else if (EQ (pointer, Qtext))
20888 cursor = FRAME_X_OUTPUT (f)->text_cursor;
20889 else if (EQ (pointer, intern ("hdrag")))
20890 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
20891 #ifdef HAVE_X_WINDOWS
20892 else if (EQ (pointer, intern ("vdrag")))
20893 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
20894 #endif
20895 else if (EQ (pointer, intern ("hourglass")))
20896 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
20897 else if (EQ (pointer, Qmodeline))
20898 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
20899 else
20900 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
20901 }
20902
20903 if (cursor != No_Cursor)
20904 FRAME_RIF (f)->define_frame_cursor (f, cursor);
20905 }
20906
20907 /* Take proper action when mouse has moved to the mode or header line
20908 or marginal area AREA of window W, x-position X and y-position Y.
20909 X is relative to the start of the text display area of W, so the
20910 width of bitmap areas and scroll bars must be subtracted to get a
20911 position relative to the start of the mode line. */
20912
20913 static void
20914 note_mode_line_or_margin_highlight (w, x, y, area)
20915 struct window *w;
20916 int x, y;
20917 enum window_part area;
20918 {
20919 struct frame *f = XFRAME (w->frame);
20920 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
20921 Cursor cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
20922 Lisp_Object pointer = Qnil;
20923 int charpos, dx, dy, width, height;
20924 Lisp_Object string, object = Qnil;
20925 Lisp_Object pos, help;
20926
20927 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
20928 string = mode_line_string (w, area, &x, &y, &charpos,
20929 &object, &dx, &dy, &width, &height);
20930 else
20931 {
20932 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
20933 string = marginal_area_string (w, area, &x, &y, &charpos,
20934 &object, &dx, &dy, &width, &height);
20935 }
20936
20937 help = Qnil;
20938
20939 if (IMAGEP (object))
20940 {
20941 Lisp_Object image_map, hotspot;
20942 if ((image_map = Fsafe_plist_get (XCDR (object), QCmap),
20943 !NILP (image_map))
20944 && (hotspot = find_hot_spot (image_map, dx, dy),
20945 CONSP (hotspot))
20946 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
20947 {
20948 Lisp_Object area_id, plist;
20949
20950 area_id = XCAR (hotspot);
20951 /* Could check AREA_ID to see if we enter/leave this hot-spot.
20952 If so, we could look for mouse-enter, mouse-leave
20953 properties in PLIST (and do something...). */
20954 hotspot = XCDR (hotspot);
20955 if (CONSP (hotspot)
20956 && (plist = XCAR (hotspot), CONSP (plist)))
20957 {
20958 pointer = Fsafe_plist_get (plist, Qpointer);
20959 if (NILP (pointer))
20960 pointer = Qhand;
20961 help = Fsafe_plist_get (plist, Qhelp_echo);
20962 if (!NILP (help))
20963 {
20964 help_echo_string = help;
20965 /* Is this correct? ++kfs */
20966 XSETWINDOW (help_echo_window, w);
20967 help_echo_object = w->buffer;
20968 help_echo_pos = charpos;
20969 }
20970 }
20971 if (NILP (pointer))
20972 pointer = Fsafe_plist_get (XCDR (object), QCpointer);
20973 }
20974 }
20975
20976 if (STRINGP (string))
20977 {
20978 pos = make_number (charpos);
20979 /* If we're on a string with `help-echo' text property, arrange
20980 for the help to be displayed. This is done by setting the
20981 global variable help_echo_string to the help string. */
20982 if (NILP (help))
20983 {
20984 help = Fget_text_property (pos, Qhelp_echo, string);
20985 if (!NILP (help))
20986 {
20987 help_echo_string = help;
20988 XSETWINDOW (help_echo_window, w);
20989 help_echo_object = string;
20990 help_echo_pos = charpos;
20991 }
20992 }
20993
20994 if (NILP (pointer))
20995 pointer = Fget_text_property (pos, Qpointer, string);
20996
20997 /* Change the mouse pointer according to what is under X/Y. */
20998 if (NILP (pointer) && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
20999 {
21000 Lisp_Object map;
21001 map = Fget_text_property (pos, Qlocal_map, string);
21002 if (!KEYMAPP (map))
21003 map = Fget_text_property (pos, Qkeymap, string);
21004 if (!KEYMAPP (map))
21005 cursor = dpyinfo->vertical_scroll_bar_cursor;
21006 }
21007 }
21008
21009 define_frame_cursor1 (f, cursor, pointer);
21010 }
21011
21012
21013 /* EXPORT:
21014 Take proper action when the mouse has moved to position X, Y on
21015 frame F as regards highlighting characters that have mouse-face
21016 properties. Also de-highlighting chars where the mouse was before.
21017 X and Y can be negative or out of range. */
21018
21019 void
21020 note_mouse_highlight (f, x, y)
21021 struct frame *f;
21022 int x, y;
21023 {
21024 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
21025 enum window_part part;
21026 Lisp_Object window;
21027 struct window *w;
21028 Cursor cursor = No_Cursor;
21029 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
21030 struct buffer *b;
21031
21032 /* When a menu is active, don't highlight because this looks odd. */
21033 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NTGUI)
21034 if (popup_activated ())
21035 return;
21036 #endif
21037
21038 if (NILP (Vmouse_highlight)
21039 || !f->glyphs_initialized_p)
21040 return;
21041
21042 dpyinfo->mouse_face_mouse_x = x;
21043 dpyinfo->mouse_face_mouse_y = y;
21044 dpyinfo->mouse_face_mouse_frame = f;
21045
21046 if (dpyinfo->mouse_face_defer)
21047 return;
21048
21049 if (gc_in_progress)
21050 {
21051 dpyinfo->mouse_face_deferred_gc = 1;
21052 return;
21053 }
21054
21055 /* Which window is that in? */
21056 window = window_from_coordinates (f, x, y, &part, 0, 0, 1);
21057
21058 /* If we were displaying active text in another window, clear that.
21059 Also clear if we move out of text area in same window. */
21060 if (! EQ (window, dpyinfo->mouse_face_window)
21061 || (part != ON_TEXT && !NILP (dpyinfo->mouse_face_window)))
21062 clear_mouse_face (dpyinfo);
21063
21064 /* Not on a window -> return. */
21065 if (!WINDOWP (window))
21066 return;
21067
21068 /* Reset help_echo_string. It will get recomputed below. */
21069 help_echo_string = Qnil;
21070
21071 /* Convert to window-relative pixel coordinates. */
21072 w = XWINDOW (window);
21073 frame_to_window_pixel_xy (w, &x, &y);
21074
21075 /* Handle tool-bar window differently since it doesn't display a
21076 buffer. */
21077 if (EQ (window, f->tool_bar_window))
21078 {
21079 note_tool_bar_highlight (f, x, y);
21080 return;
21081 }
21082
21083 /* Mouse is on the mode, header line or margin? */
21084 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
21085 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
21086 {
21087 note_mode_line_or_margin_highlight (w, x, y, part);
21088 return;
21089 }
21090
21091 if (part == ON_VERTICAL_BORDER)
21092 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
21093 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
21094 || part == ON_SCROLL_BAR)
21095 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
21096 else
21097 cursor = FRAME_X_OUTPUT (f)->text_cursor;
21098
21099 /* Are we in a window whose display is up to date?
21100 And verify the buffer's text has not changed. */
21101 b = XBUFFER (w->buffer);
21102 if (part == ON_TEXT
21103 && EQ (w->window_end_valid, w->buffer)
21104 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
21105 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
21106 {
21107 int hpos, vpos, pos, i, dx, dy, area;
21108 struct glyph *glyph;
21109 Lisp_Object object;
21110 Lisp_Object mouse_face = Qnil, overlay = Qnil, position;
21111 Lisp_Object *overlay_vec = NULL;
21112 int noverlays;
21113 struct buffer *obuf;
21114 int obegv, ozv, same_region;
21115
21116 /* Find the glyph under X/Y. */
21117 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
21118
21119 /* Look for :pointer property on image. */
21120 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
21121 {
21122 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
21123 if (img != NULL && IMAGEP (img->spec))
21124 {
21125 Lisp_Object image_map, hotspot;
21126 if ((image_map = Fsafe_plist_get (XCDR (img->spec), QCmap),
21127 !NILP (image_map))
21128 && (hotspot = find_hot_spot (image_map,
21129 glyph->slice.x + dx,
21130 glyph->slice.y + dy),
21131 CONSP (hotspot))
21132 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
21133 {
21134 Lisp_Object area_id, plist;
21135
21136 area_id = XCAR (hotspot);
21137 /* Could check AREA_ID to see if we enter/leave this hot-spot.
21138 If so, we could look for mouse-enter, mouse-leave
21139 properties in PLIST (and do something...). */
21140 hotspot = XCDR (hotspot);
21141 if (CONSP (hotspot)
21142 && (plist = XCAR (hotspot), CONSP (plist)))
21143 {
21144 pointer = Fsafe_plist_get (plist, Qpointer);
21145 if (NILP (pointer))
21146 pointer = Qhand;
21147 help_echo_string = Fsafe_plist_get (plist, Qhelp_echo);
21148 if (!NILP (help_echo_string))
21149 {
21150 help_echo_window = window;
21151 help_echo_object = glyph->object;
21152 help_echo_pos = glyph->charpos;
21153 }
21154 }
21155 }
21156 if (NILP (pointer))
21157 pointer = Fsafe_plist_get (XCDR (img->spec), QCpointer);
21158 }
21159 }
21160
21161 /* Clear mouse face if X/Y not over text. */
21162 if (glyph == NULL
21163 || area != TEXT_AREA
21164 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p)
21165 {
21166 if (clear_mouse_face (dpyinfo))
21167 cursor = No_Cursor;
21168 if (NILP (pointer))
21169 {
21170 if (area != TEXT_AREA)
21171 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
21172 else
21173 pointer = Vvoid_text_area_pointer;
21174 }
21175 goto set_cursor;
21176 }
21177
21178 pos = glyph->charpos;
21179 object = glyph->object;
21180 if (!STRINGP (object) && !BUFFERP (object))
21181 goto set_cursor;
21182
21183 /* If we get an out-of-range value, return now; avoid an error. */
21184 if (BUFFERP (object) && pos > BUF_Z (b))
21185 goto set_cursor;
21186
21187 /* Make the window's buffer temporarily current for
21188 overlays_at and compute_char_face. */
21189 obuf = current_buffer;
21190 current_buffer = b;
21191 obegv = BEGV;
21192 ozv = ZV;
21193 BEGV = BEG;
21194 ZV = Z;
21195
21196 /* Is this char mouse-active or does it have help-echo? */
21197 position = make_number (pos);
21198
21199 if (BUFFERP (object))
21200 {
21201 /* Put all the overlays we want in a vector in overlay_vec. */
21202 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
21203 /* Sort overlays into increasing priority order. */
21204 noverlays = sort_overlays (overlay_vec, noverlays, w);
21205 }
21206 else
21207 noverlays = 0;
21208
21209 same_region = (EQ (window, dpyinfo->mouse_face_window)
21210 && vpos >= dpyinfo->mouse_face_beg_row
21211 && vpos <= dpyinfo->mouse_face_end_row
21212 && (vpos > dpyinfo->mouse_face_beg_row
21213 || hpos >= dpyinfo->mouse_face_beg_col)
21214 && (vpos < dpyinfo->mouse_face_end_row
21215 || hpos < dpyinfo->mouse_face_end_col
21216 || dpyinfo->mouse_face_past_end));
21217
21218 if (same_region)
21219 cursor = No_Cursor;
21220
21221 /* Check mouse-face highlighting. */
21222 if (! same_region
21223 /* If there exists an overlay with mouse-face overlapping
21224 the one we are currently highlighting, we have to
21225 check if we enter the overlapping overlay, and then
21226 highlight only that. */
21227 || (OVERLAYP (dpyinfo->mouse_face_overlay)
21228 && mouse_face_overlay_overlaps (dpyinfo->mouse_face_overlay)))
21229 {
21230 /* Find the highest priority overlay that has a mouse-face
21231 property. */
21232 overlay = Qnil;
21233 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
21234 {
21235 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
21236 if (!NILP (mouse_face))
21237 overlay = overlay_vec[i];
21238 }
21239
21240 /* If we're actually highlighting the same overlay as
21241 before, there's no need to do that again. */
21242 if (!NILP (overlay)
21243 && EQ (overlay, dpyinfo->mouse_face_overlay))
21244 goto check_help_echo;
21245
21246 dpyinfo->mouse_face_overlay = overlay;
21247
21248 /* Clear the display of the old active region, if any. */
21249 if (clear_mouse_face (dpyinfo))
21250 cursor = No_Cursor;
21251
21252 /* If no overlay applies, get a text property. */
21253 if (NILP (overlay))
21254 mouse_face = Fget_text_property (position, Qmouse_face, object);
21255
21256 /* Handle the overlay case. */
21257 if (!NILP (overlay))
21258 {
21259 /* Find the range of text around this char that
21260 should be active. */
21261 Lisp_Object before, after;
21262 int ignore;
21263
21264 before = Foverlay_start (overlay);
21265 after = Foverlay_end (overlay);
21266 /* Record this as the current active region. */
21267 fast_find_position (w, XFASTINT (before),
21268 &dpyinfo->mouse_face_beg_col,
21269 &dpyinfo->mouse_face_beg_row,
21270 &dpyinfo->mouse_face_beg_x,
21271 &dpyinfo->mouse_face_beg_y, Qnil);
21272
21273 dpyinfo->mouse_face_past_end
21274 = !fast_find_position (w, XFASTINT (after),
21275 &dpyinfo->mouse_face_end_col,
21276 &dpyinfo->mouse_face_end_row,
21277 &dpyinfo->mouse_face_end_x,
21278 &dpyinfo->mouse_face_end_y, Qnil);
21279 dpyinfo->mouse_face_window = window;
21280
21281 dpyinfo->mouse_face_face_id
21282 = face_at_buffer_position (w, pos, 0, 0,
21283 &ignore, pos + 1,
21284 !dpyinfo->mouse_face_hidden);
21285
21286 /* Display it as active. */
21287 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
21288 cursor = No_Cursor;
21289 }
21290 /* Handle the text property case. */
21291 else if (!NILP (mouse_face) && BUFFERP (object))
21292 {
21293 /* Find the range of text around this char that
21294 should be active. */
21295 Lisp_Object before, after, beginning, end;
21296 int ignore;
21297
21298 beginning = Fmarker_position (w->start);
21299 end = make_number (BUF_Z (XBUFFER (object))
21300 - XFASTINT (w->window_end_pos));
21301 before
21302 = Fprevious_single_property_change (make_number (pos + 1),
21303 Qmouse_face,
21304 object, beginning);
21305 after
21306 = Fnext_single_property_change (position, Qmouse_face,
21307 object, end);
21308
21309 /* Record this as the current active region. */
21310 fast_find_position (w, XFASTINT (before),
21311 &dpyinfo->mouse_face_beg_col,
21312 &dpyinfo->mouse_face_beg_row,
21313 &dpyinfo->mouse_face_beg_x,
21314 &dpyinfo->mouse_face_beg_y, Qnil);
21315 dpyinfo->mouse_face_past_end
21316 = !fast_find_position (w, XFASTINT (after),
21317 &dpyinfo->mouse_face_end_col,
21318 &dpyinfo->mouse_face_end_row,
21319 &dpyinfo->mouse_face_end_x,
21320 &dpyinfo->mouse_face_end_y, Qnil);
21321 dpyinfo->mouse_face_window = window;
21322
21323 if (BUFFERP (object))
21324 dpyinfo->mouse_face_face_id
21325 = face_at_buffer_position (w, pos, 0, 0,
21326 &ignore, pos + 1,
21327 !dpyinfo->mouse_face_hidden);
21328
21329 /* Display it as active. */
21330 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
21331 cursor = No_Cursor;
21332 }
21333 else if (!NILP (mouse_face) && STRINGP (object))
21334 {
21335 Lisp_Object b, e;
21336 int ignore;
21337
21338 b = Fprevious_single_property_change (make_number (pos + 1),
21339 Qmouse_face,
21340 object, Qnil);
21341 e = Fnext_single_property_change (position, Qmouse_face,
21342 object, Qnil);
21343 if (NILP (b))
21344 b = make_number (0);
21345 if (NILP (e))
21346 e = make_number (SCHARS (object) - 1);
21347 fast_find_string_pos (w, XINT (b), object,
21348 &dpyinfo->mouse_face_beg_col,
21349 &dpyinfo->mouse_face_beg_row,
21350 &dpyinfo->mouse_face_beg_x,
21351 &dpyinfo->mouse_face_beg_y, 0);
21352 fast_find_string_pos (w, XINT (e), object,
21353 &dpyinfo->mouse_face_end_col,
21354 &dpyinfo->mouse_face_end_row,
21355 &dpyinfo->mouse_face_end_x,
21356 &dpyinfo->mouse_face_end_y, 1);
21357 dpyinfo->mouse_face_past_end = 0;
21358 dpyinfo->mouse_face_window = window;
21359 dpyinfo->mouse_face_face_id
21360 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
21361 glyph->face_id, 1);
21362 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
21363 cursor = No_Cursor;
21364 }
21365 else if (STRINGP (object) && NILP (mouse_face))
21366 {
21367 /* A string which doesn't have mouse-face, but
21368 the text ``under'' it might have. */
21369 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
21370 int start = MATRIX_ROW_START_CHARPOS (r);
21371
21372 pos = string_buffer_position (w, object, start);
21373 if (pos > 0)
21374 mouse_face = get_char_property_and_overlay (make_number (pos),
21375 Qmouse_face,
21376 w->buffer,
21377 &overlay);
21378 if (!NILP (mouse_face) && !NILP (overlay))
21379 {
21380 Lisp_Object before = Foverlay_start (overlay);
21381 Lisp_Object after = Foverlay_end (overlay);
21382 int ignore;
21383
21384 /* Note that we might not be able to find position
21385 BEFORE in the glyph matrix if the overlay is
21386 entirely covered by a `display' property. In
21387 this case, we overshoot. So let's stop in
21388 the glyph matrix before glyphs for OBJECT. */
21389 fast_find_position (w, XFASTINT (before),
21390 &dpyinfo->mouse_face_beg_col,
21391 &dpyinfo->mouse_face_beg_row,
21392 &dpyinfo->mouse_face_beg_x,
21393 &dpyinfo->mouse_face_beg_y,
21394 object);
21395
21396 dpyinfo->mouse_face_past_end
21397 = !fast_find_position (w, XFASTINT (after),
21398 &dpyinfo->mouse_face_end_col,
21399 &dpyinfo->mouse_face_end_row,
21400 &dpyinfo->mouse_face_end_x,
21401 &dpyinfo->mouse_face_end_y,
21402 Qnil);
21403 dpyinfo->mouse_face_window = window;
21404 dpyinfo->mouse_face_face_id
21405 = face_at_buffer_position (w, pos, 0, 0,
21406 &ignore, pos + 1,
21407 !dpyinfo->mouse_face_hidden);
21408
21409 /* Display it as active. */
21410 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
21411 cursor = No_Cursor;
21412 }
21413 }
21414 }
21415
21416 check_help_echo:
21417
21418 /* Look for a `help-echo' property. */
21419 if (NILP (help_echo_string)) {
21420 Lisp_Object help, overlay;
21421
21422 /* Check overlays first. */
21423 help = overlay = Qnil;
21424 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
21425 {
21426 overlay = overlay_vec[i];
21427 help = Foverlay_get (overlay, Qhelp_echo);
21428 }
21429
21430 if (!NILP (help))
21431 {
21432 help_echo_string = help;
21433 help_echo_window = window;
21434 help_echo_object = overlay;
21435 help_echo_pos = pos;
21436 }
21437 else
21438 {
21439 Lisp_Object object = glyph->object;
21440 int charpos = glyph->charpos;
21441
21442 /* Try text properties. */
21443 if (STRINGP (object)
21444 && charpos >= 0
21445 && charpos < SCHARS (object))
21446 {
21447 help = Fget_text_property (make_number (charpos),
21448 Qhelp_echo, object);
21449 if (NILP (help))
21450 {
21451 /* If the string itself doesn't specify a help-echo,
21452 see if the buffer text ``under'' it does. */
21453 struct glyph_row *r
21454 = MATRIX_ROW (w->current_matrix, vpos);
21455 int start = MATRIX_ROW_START_CHARPOS (r);
21456 int pos = string_buffer_position (w, object, start);
21457 if (pos > 0)
21458 {
21459 help = Fget_char_property (make_number (pos),
21460 Qhelp_echo, w->buffer);
21461 if (!NILP (help))
21462 {
21463 charpos = pos;
21464 object = w->buffer;
21465 }
21466 }
21467 }
21468 }
21469 else if (BUFFERP (object)
21470 && charpos >= BEGV
21471 && charpos < ZV)
21472 help = Fget_text_property (make_number (charpos), Qhelp_echo,
21473 object);
21474
21475 if (!NILP (help))
21476 {
21477 help_echo_string = help;
21478 help_echo_window = window;
21479 help_echo_object = object;
21480 help_echo_pos = charpos;
21481 }
21482 }
21483 }
21484
21485 /* Look for a `pointer' property. */
21486 if (NILP (pointer))
21487 {
21488 /* Check overlays first. */
21489 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
21490 pointer = Foverlay_get (overlay_vec[i], Qpointer);
21491
21492 if (NILP (pointer))
21493 {
21494 Lisp_Object object = glyph->object;
21495 int charpos = glyph->charpos;
21496
21497 /* Try text properties. */
21498 if (STRINGP (object)
21499 && charpos >= 0
21500 && charpos < SCHARS (object))
21501 {
21502 pointer = Fget_text_property (make_number (charpos),
21503 Qpointer, object);
21504 if (NILP (pointer))
21505 {
21506 /* If the string itself doesn't specify a pointer,
21507 see if the buffer text ``under'' it does. */
21508 struct glyph_row *r
21509 = MATRIX_ROW (w->current_matrix, vpos);
21510 int start = MATRIX_ROW_START_CHARPOS (r);
21511 int pos = string_buffer_position (w, object, start);
21512 if (pos > 0)
21513 pointer = Fget_char_property (make_number (pos),
21514 Qpointer, w->buffer);
21515 }
21516 }
21517 else if (BUFFERP (object)
21518 && charpos >= BEGV
21519 && charpos < ZV)
21520 pointer = Fget_text_property (make_number (charpos),
21521 Qpointer, object);
21522 }
21523 }
21524
21525 BEGV = obegv;
21526 ZV = ozv;
21527 current_buffer = obuf;
21528 }
21529
21530 set_cursor:
21531
21532 define_frame_cursor1 (f, cursor, pointer);
21533 }
21534
21535
21536 /* EXPORT for RIF:
21537 Clear any mouse-face on window W. This function is part of the
21538 redisplay interface, and is called from try_window_id and similar
21539 functions to ensure the mouse-highlight is off. */
21540
21541 void
21542 x_clear_window_mouse_face (w)
21543 struct window *w;
21544 {
21545 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
21546 Lisp_Object window;
21547
21548 BLOCK_INPUT;
21549 XSETWINDOW (window, w);
21550 if (EQ (window, dpyinfo->mouse_face_window))
21551 clear_mouse_face (dpyinfo);
21552 UNBLOCK_INPUT;
21553 }
21554
21555
21556 /* EXPORT:
21557 Just discard the mouse face information for frame F, if any.
21558 This is used when the size of F is changed. */
21559
21560 void
21561 cancel_mouse_face (f)
21562 struct frame *f;
21563 {
21564 Lisp_Object window;
21565 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
21566
21567 window = dpyinfo->mouse_face_window;
21568 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
21569 {
21570 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
21571 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
21572 dpyinfo->mouse_face_window = Qnil;
21573 }
21574 }
21575
21576
21577 #endif /* HAVE_WINDOW_SYSTEM */
21578
21579 \f
21580 /***********************************************************************
21581 Exposure Events
21582 ***********************************************************************/
21583
21584 #ifdef HAVE_WINDOW_SYSTEM
21585
21586 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
21587 which intersects rectangle R. R is in window-relative coordinates. */
21588
21589 static void
21590 expose_area (w, row, r, area)
21591 struct window *w;
21592 struct glyph_row *row;
21593 XRectangle *r;
21594 enum glyph_row_area area;
21595 {
21596 struct glyph *first = row->glyphs[area];
21597 struct glyph *end = row->glyphs[area] + row->used[area];
21598 struct glyph *last;
21599 int first_x, start_x, x;
21600
21601 if (area == TEXT_AREA && row->fill_line_p)
21602 /* If row extends face to end of line write the whole line. */
21603 draw_glyphs (w, 0, row, area,
21604 0, row->used[area],
21605 DRAW_NORMAL_TEXT, 0);
21606 else
21607 {
21608 /* Set START_X to the window-relative start position for drawing glyphs of
21609 AREA. The first glyph of the text area can be partially visible.
21610 The first glyphs of other areas cannot. */
21611 start_x = window_box_left_offset (w, area);
21612 x = start_x;
21613 if (area == TEXT_AREA)
21614 x += row->x;
21615
21616 /* Find the first glyph that must be redrawn. */
21617 while (first < end
21618 && x + first->pixel_width < r->x)
21619 {
21620 x += first->pixel_width;
21621 ++first;
21622 }
21623
21624 /* Find the last one. */
21625 last = first;
21626 first_x = x;
21627 while (last < end
21628 && x < r->x + r->width)
21629 {
21630 x += last->pixel_width;
21631 ++last;
21632 }
21633
21634 /* Repaint. */
21635 if (last > first)
21636 draw_glyphs (w, first_x - start_x, row, area,
21637 first - row->glyphs[area], last - row->glyphs[area],
21638 DRAW_NORMAL_TEXT, 0);
21639 }
21640 }
21641
21642
21643 /* Redraw the parts of the glyph row ROW on window W intersecting
21644 rectangle R. R is in window-relative coordinates. Value is
21645 non-zero if mouse-face was overwritten. */
21646
21647 static int
21648 expose_line (w, row, r)
21649 struct window *w;
21650 struct glyph_row *row;
21651 XRectangle *r;
21652 {
21653 xassert (row->enabled_p);
21654
21655 if (row->mode_line_p || w->pseudo_window_p)
21656 draw_glyphs (w, 0, row, TEXT_AREA,
21657 0, row->used[TEXT_AREA],
21658 DRAW_NORMAL_TEXT, 0);
21659 else
21660 {
21661 if (row->used[LEFT_MARGIN_AREA])
21662 expose_area (w, row, r, LEFT_MARGIN_AREA);
21663 if (row->used[TEXT_AREA])
21664 expose_area (w, row, r, TEXT_AREA);
21665 if (row->used[RIGHT_MARGIN_AREA])
21666 expose_area (w, row, r, RIGHT_MARGIN_AREA);
21667 draw_row_fringe_bitmaps (w, row);
21668 }
21669
21670 return row->mouse_face_p;
21671 }
21672
21673
21674 /* Redraw those parts of glyphs rows during expose event handling that
21675 overlap other rows. Redrawing of an exposed line writes over parts
21676 of lines overlapping that exposed line; this function fixes that.
21677
21678 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
21679 row in W's current matrix that is exposed and overlaps other rows.
21680 LAST_OVERLAPPING_ROW is the last such row. */
21681
21682 static void
21683 expose_overlaps (w, first_overlapping_row, last_overlapping_row)
21684 struct window *w;
21685 struct glyph_row *first_overlapping_row;
21686 struct glyph_row *last_overlapping_row;
21687 {
21688 struct glyph_row *row;
21689
21690 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
21691 if (row->overlapping_p)
21692 {
21693 xassert (row->enabled_p && !row->mode_line_p);
21694
21695 if (row->used[LEFT_MARGIN_AREA])
21696 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA);
21697
21698 if (row->used[TEXT_AREA])
21699 x_fix_overlapping_area (w, row, TEXT_AREA);
21700
21701 if (row->used[RIGHT_MARGIN_AREA])
21702 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA);
21703 }
21704 }
21705
21706
21707 /* Return non-zero if W's cursor intersects rectangle R. */
21708
21709 static int
21710 phys_cursor_in_rect_p (w, r)
21711 struct window *w;
21712 XRectangle *r;
21713 {
21714 XRectangle cr, result;
21715 struct glyph *cursor_glyph;
21716
21717 cursor_glyph = get_phys_cursor_glyph (w);
21718 if (cursor_glyph)
21719 {
21720 /* r is relative to W's box, but w->phys_cursor.x is relative
21721 to left edge of W's TEXT area. Adjust it. */
21722 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
21723 cr.y = w->phys_cursor.y;
21724 cr.width = cursor_glyph->pixel_width;
21725 cr.height = w->phys_cursor_height;
21726 /* ++KFS: W32 version used W32-specific IntersectRect here, but
21727 I assume the effect is the same -- and this is portable. */
21728 return x_intersect_rectangles (&cr, r, &result);
21729 }
21730 /* If we don't understand the format, pretend we're not in the hot-spot. */
21731 return 0;
21732 }
21733
21734
21735 /* EXPORT:
21736 Draw a vertical window border to the right of window W if W doesn't
21737 have vertical scroll bars. */
21738
21739 void
21740 x_draw_vertical_border (w)
21741 struct window *w;
21742 {
21743 struct frame *f = XFRAME (WINDOW_FRAME (w));
21744
21745 /* We could do better, if we knew what type of scroll-bar the adjacent
21746 windows (on either side) have... But we don't :-(
21747 However, I think this works ok. ++KFS 2003-04-25 */
21748
21749 /* Redraw borders between horizontally adjacent windows. Don't
21750 do it for frames with vertical scroll bars because either the
21751 right scroll bar of a window, or the left scroll bar of its
21752 neighbor will suffice as a border. */
21753 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
21754 return;
21755
21756 if (!WINDOW_RIGHTMOST_P (w)
21757 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
21758 {
21759 int x0, x1, y0, y1;
21760
21761 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
21762 y1 -= 1;
21763
21764 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
21765 }
21766 else if (!WINDOW_LEFTMOST_P (w)
21767 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
21768 {
21769 int x0, x1, y0, y1;
21770
21771 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
21772 y1 -= 1;
21773
21774 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
21775 }
21776 }
21777
21778
21779 /* Redraw the part of window W intersection rectangle FR. Pixel
21780 coordinates in FR are frame-relative. Call this function with
21781 input blocked. Value is non-zero if the exposure overwrites
21782 mouse-face. */
21783
21784 static int
21785 expose_window (w, fr)
21786 struct window *w;
21787 XRectangle *fr;
21788 {
21789 struct frame *f = XFRAME (w->frame);
21790 XRectangle wr, r;
21791 int mouse_face_overwritten_p = 0;
21792
21793 /* If window is not yet fully initialized, do nothing. This can
21794 happen when toolkit scroll bars are used and a window is split.
21795 Reconfiguring the scroll bar will generate an expose for a newly
21796 created window. */
21797 if (w->current_matrix == NULL)
21798 return 0;
21799
21800 /* When we're currently updating the window, display and current
21801 matrix usually don't agree. Arrange for a thorough display
21802 later. */
21803 if (w == updated_window)
21804 {
21805 SET_FRAME_GARBAGED (f);
21806 return 0;
21807 }
21808
21809 /* Frame-relative pixel rectangle of W. */
21810 wr.x = WINDOW_LEFT_EDGE_X (w);
21811 wr.y = WINDOW_TOP_EDGE_Y (w);
21812 wr.width = WINDOW_TOTAL_WIDTH (w);
21813 wr.height = WINDOW_TOTAL_HEIGHT (w);
21814
21815 if (x_intersect_rectangles (fr, &wr, &r))
21816 {
21817 int yb = window_text_bottom_y (w);
21818 struct glyph_row *row;
21819 int cursor_cleared_p;
21820 struct glyph_row *first_overlapping_row, *last_overlapping_row;
21821
21822 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
21823 r.x, r.y, r.width, r.height));
21824
21825 /* Convert to window coordinates. */
21826 r.x -= WINDOW_LEFT_EDGE_X (w);
21827 r.y -= WINDOW_TOP_EDGE_Y (w);
21828
21829 /* Turn off the cursor. */
21830 if (!w->pseudo_window_p
21831 && phys_cursor_in_rect_p (w, &r))
21832 {
21833 x_clear_cursor (w);
21834 cursor_cleared_p = 1;
21835 }
21836 else
21837 cursor_cleared_p = 0;
21838
21839 /* Update lines intersecting rectangle R. */
21840 first_overlapping_row = last_overlapping_row = NULL;
21841 for (row = w->current_matrix->rows;
21842 row->enabled_p;
21843 ++row)
21844 {
21845 int y0 = row->y;
21846 int y1 = MATRIX_ROW_BOTTOM_Y (row);
21847
21848 if ((y0 >= r.y && y0 < r.y + r.height)
21849 || (y1 > r.y && y1 < r.y + r.height)
21850 || (r.y >= y0 && r.y < y1)
21851 || (r.y + r.height > y0 && r.y + r.height < y1))
21852 {
21853 if (row->overlapping_p)
21854 {
21855 if (first_overlapping_row == NULL)
21856 first_overlapping_row = row;
21857 last_overlapping_row = row;
21858 }
21859
21860 if (expose_line (w, row, &r))
21861 mouse_face_overwritten_p = 1;
21862 }
21863
21864 if (y1 >= yb)
21865 break;
21866 }
21867
21868 /* Display the mode line if there is one. */
21869 if (WINDOW_WANTS_MODELINE_P (w)
21870 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
21871 row->enabled_p)
21872 && row->y < r.y + r.height)
21873 {
21874 if (expose_line (w, row, &r))
21875 mouse_face_overwritten_p = 1;
21876 }
21877
21878 if (!w->pseudo_window_p)
21879 {
21880 /* Fix the display of overlapping rows. */
21881 if (first_overlapping_row)
21882 expose_overlaps (w, first_overlapping_row, last_overlapping_row);
21883
21884 /* Draw border between windows. */
21885 x_draw_vertical_border (w);
21886
21887 /* Turn the cursor on again. */
21888 if (cursor_cleared_p)
21889 update_window_cursor (w, 1);
21890 }
21891 }
21892
21893 #ifdef HAVE_CARBON
21894 /* Display scroll bar for this window. */
21895 if (!NILP (w->vertical_scroll_bar))
21896 {
21897 /* ++KFS:
21898 If this doesn't work here (maybe some header files are missing),
21899 make a function in macterm.c and call it to do the job! */
21900 ControlHandle ch
21901 = SCROLL_BAR_CONTROL_HANDLE (XSCROLL_BAR (w->vertical_scroll_bar));
21902
21903 Draw1Control (ch);
21904 }
21905 #endif
21906
21907 return mouse_face_overwritten_p;
21908 }
21909
21910
21911
21912 /* Redraw (parts) of all windows in the window tree rooted at W that
21913 intersect R. R contains frame pixel coordinates. Value is
21914 non-zero if the exposure overwrites mouse-face. */
21915
21916 static int
21917 expose_window_tree (w, r)
21918 struct window *w;
21919 XRectangle *r;
21920 {
21921 struct frame *f = XFRAME (w->frame);
21922 int mouse_face_overwritten_p = 0;
21923
21924 while (w && !FRAME_GARBAGED_P (f))
21925 {
21926 if (!NILP (w->hchild))
21927 mouse_face_overwritten_p
21928 |= expose_window_tree (XWINDOW (w->hchild), r);
21929 else if (!NILP (w->vchild))
21930 mouse_face_overwritten_p
21931 |= expose_window_tree (XWINDOW (w->vchild), r);
21932 else
21933 mouse_face_overwritten_p |= expose_window (w, r);
21934
21935 w = NILP (w->next) ? NULL : XWINDOW (w->next);
21936 }
21937
21938 return mouse_face_overwritten_p;
21939 }
21940
21941
21942 /* EXPORT:
21943 Redisplay an exposed area of frame F. X and Y are the upper-left
21944 corner of the exposed rectangle. W and H are width and height of
21945 the exposed area. All are pixel values. W or H zero means redraw
21946 the entire frame. */
21947
21948 void
21949 expose_frame (f, x, y, w, h)
21950 struct frame *f;
21951 int x, y, w, h;
21952 {
21953 XRectangle r;
21954 int mouse_face_overwritten_p = 0;
21955
21956 TRACE ((stderr, "expose_frame "));
21957
21958 /* No need to redraw if frame will be redrawn soon. */
21959 if (FRAME_GARBAGED_P (f))
21960 {
21961 TRACE ((stderr, " garbaged\n"));
21962 return;
21963 }
21964
21965 #ifdef HAVE_CARBON
21966 /* MAC_TODO: this is a kludge, but if scroll bars are not activated
21967 or deactivated here, for unknown reasons, activated scroll bars
21968 are shown in deactivated frames in some instances. */
21969 if (f == FRAME_MAC_DISPLAY_INFO (f)->x_focus_frame)
21970 activate_scroll_bars (f);
21971 else
21972 deactivate_scroll_bars (f);
21973 #endif
21974
21975 /* If basic faces haven't been realized yet, there is no point in
21976 trying to redraw anything. This can happen when we get an expose
21977 event while Emacs is starting, e.g. by moving another window. */
21978 if (FRAME_FACE_CACHE (f) == NULL
21979 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
21980 {
21981 TRACE ((stderr, " no faces\n"));
21982 return;
21983 }
21984
21985 if (w == 0 || h == 0)
21986 {
21987 r.x = r.y = 0;
21988 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
21989 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
21990 }
21991 else
21992 {
21993 r.x = x;
21994 r.y = y;
21995 r.width = w;
21996 r.height = h;
21997 }
21998
21999 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
22000 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
22001
22002 if (WINDOWP (f->tool_bar_window))
22003 mouse_face_overwritten_p
22004 |= expose_window (XWINDOW (f->tool_bar_window), &r);
22005
22006 #ifdef HAVE_X_WINDOWS
22007 #ifndef MSDOS
22008 #ifndef USE_X_TOOLKIT
22009 if (WINDOWP (f->menu_bar_window))
22010 mouse_face_overwritten_p
22011 |= expose_window (XWINDOW (f->menu_bar_window), &r);
22012 #endif /* not USE_X_TOOLKIT */
22013 #endif
22014 #endif
22015
22016 /* Some window managers support a focus-follows-mouse style with
22017 delayed raising of frames. Imagine a partially obscured frame,
22018 and moving the mouse into partially obscured mouse-face on that
22019 frame. The visible part of the mouse-face will be highlighted,
22020 then the WM raises the obscured frame. With at least one WM, KDE
22021 2.1, Emacs is not getting any event for the raising of the frame
22022 (even tried with SubstructureRedirectMask), only Expose events.
22023 These expose events will draw text normally, i.e. not
22024 highlighted. Which means we must redo the highlight here.
22025 Subsume it under ``we love X''. --gerd 2001-08-15 */
22026 /* Included in Windows version because Windows most likely does not
22027 do the right thing if any third party tool offers
22028 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
22029 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
22030 {
22031 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
22032 if (f == dpyinfo->mouse_face_mouse_frame)
22033 {
22034 int x = dpyinfo->mouse_face_mouse_x;
22035 int y = dpyinfo->mouse_face_mouse_y;
22036 clear_mouse_face (dpyinfo);
22037 note_mouse_highlight (f, x, y);
22038 }
22039 }
22040 }
22041
22042
22043 /* EXPORT:
22044 Determine the intersection of two rectangles R1 and R2. Return
22045 the intersection in *RESULT. Value is non-zero if RESULT is not
22046 empty. */
22047
22048 int
22049 x_intersect_rectangles (r1, r2, result)
22050 XRectangle *r1, *r2, *result;
22051 {
22052 XRectangle *left, *right;
22053 XRectangle *upper, *lower;
22054 int intersection_p = 0;
22055
22056 /* Rearrange so that R1 is the left-most rectangle. */
22057 if (r1->x < r2->x)
22058 left = r1, right = r2;
22059 else
22060 left = r2, right = r1;
22061
22062 /* X0 of the intersection is right.x0, if this is inside R1,
22063 otherwise there is no intersection. */
22064 if (right->x <= left->x + left->width)
22065 {
22066 result->x = right->x;
22067
22068 /* The right end of the intersection is the minimum of the
22069 the right ends of left and right. */
22070 result->width = (min (left->x + left->width, right->x + right->width)
22071 - result->x);
22072
22073 /* Same game for Y. */
22074 if (r1->y < r2->y)
22075 upper = r1, lower = r2;
22076 else
22077 upper = r2, lower = r1;
22078
22079 /* The upper end of the intersection is lower.y0, if this is inside
22080 of upper. Otherwise, there is no intersection. */
22081 if (lower->y <= upper->y + upper->height)
22082 {
22083 result->y = lower->y;
22084
22085 /* The lower end of the intersection is the minimum of the lower
22086 ends of upper and lower. */
22087 result->height = (min (lower->y + lower->height,
22088 upper->y + upper->height)
22089 - result->y);
22090 intersection_p = 1;
22091 }
22092 }
22093
22094 return intersection_p;
22095 }
22096
22097 #endif /* HAVE_WINDOW_SYSTEM */
22098
22099 \f
22100 /***********************************************************************
22101 Initialization
22102 ***********************************************************************/
22103
22104 void
22105 syms_of_xdisp ()
22106 {
22107 Vwith_echo_area_save_vector = Qnil;
22108 staticpro (&Vwith_echo_area_save_vector);
22109
22110 Vmessage_stack = Qnil;
22111 staticpro (&Vmessage_stack);
22112
22113 Qinhibit_redisplay = intern ("inhibit-redisplay");
22114 staticpro (&Qinhibit_redisplay);
22115
22116 message_dolog_marker1 = Fmake_marker ();
22117 staticpro (&message_dolog_marker1);
22118 message_dolog_marker2 = Fmake_marker ();
22119 staticpro (&message_dolog_marker2);
22120 message_dolog_marker3 = Fmake_marker ();
22121 staticpro (&message_dolog_marker3);
22122
22123 #if GLYPH_DEBUG
22124 defsubr (&Sdump_frame_glyph_matrix);
22125 defsubr (&Sdump_glyph_matrix);
22126 defsubr (&Sdump_glyph_row);
22127 defsubr (&Sdump_tool_bar_row);
22128 defsubr (&Strace_redisplay);
22129 defsubr (&Strace_to_stderr);
22130 #endif
22131 #ifdef HAVE_WINDOW_SYSTEM
22132 defsubr (&Stool_bar_lines_needed);
22133 defsubr (&Slookup_image_map);
22134 #endif
22135 defsubr (&Sformat_mode_line);
22136
22137 staticpro (&Qmenu_bar_update_hook);
22138 Qmenu_bar_update_hook = intern ("menu-bar-update-hook");
22139
22140 staticpro (&Qoverriding_terminal_local_map);
22141 Qoverriding_terminal_local_map = intern ("overriding-terminal-local-map");
22142
22143 staticpro (&Qoverriding_local_map);
22144 Qoverriding_local_map = intern ("overriding-local-map");
22145
22146 staticpro (&Qwindow_scroll_functions);
22147 Qwindow_scroll_functions = intern ("window-scroll-functions");
22148
22149 staticpro (&Qredisplay_end_trigger_functions);
22150 Qredisplay_end_trigger_functions = intern ("redisplay-end-trigger-functions");
22151
22152 staticpro (&Qinhibit_point_motion_hooks);
22153 Qinhibit_point_motion_hooks = intern ("inhibit-point-motion-hooks");
22154
22155 QCdata = intern (":data");
22156 staticpro (&QCdata);
22157 Qdisplay = intern ("display");
22158 staticpro (&Qdisplay);
22159 Qspace_width = intern ("space-width");
22160 staticpro (&Qspace_width);
22161 Qraise = intern ("raise");
22162 staticpro (&Qraise);
22163 Qslice = intern ("slice");
22164 staticpro (&Qslice);
22165 Qspace = intern ("space");
22166 staticpro (&Qspace);
22167 Qmargin = intern ("margin");
22168 staticpro (&Qmargin);
22169 Qpointer = intern ("pointer");
22170 staticpro (&Qpointer);
22171 Qleft_margin = intern ("left-margin");
22172 staticpro (&Qleft_margin);
22173 Qright_margin = intern ("right-margin");
22174 staticpro (&Qright_margin);
22175 Qcenter = intern ("center");
22176 staticpro (&Qcenter);
22177 Qline_height = intern ("line-height");
22178 staticpro (&Qline_height);
22179 Qtotal = intern ("total");
22180 staticpro (&Qtotal);
22181 QCalign_to = intern (":align-to");
22182 staticpro (&QCalign_to);
22183 QCrelative_width = intern (":relative-width");
22184 staticpro (&QCrelative_width);
22185 QCrelative_height = intern (":relative-height");
22186 staticpro (&QCrelative_height);
22187 QCeval = intern (":eval");
22188 staticpro (&QCeval);
22189 QCpropertize = intern (":propertize");
22190 staticpro (&QCpropertize);
22191 QCfile = intern (":file");
22192 staticpro (&QCfile);
22193 Qfontified = intern ("fontified");
22194 staticpro (&Qfontified);
22195 Qfontification_functions = intern ("fontification-functions");
22196 staticpro (&Qfontification_functions);
22197 Qtrailing_whitespace = intern ("trailing-whitespace");
22198 staticpro (&Qtrailing_whitespace);
22199 Qescape_glyph = intern ("escape-glyph");
22200 staticpro (&Qescape_glyph);
22201 Qimage = intern ("image");
22202 staticpro (&Qimage);
22203 QCmap = intern (":map");
22204 staticpro (&QCmap);
22205 QCpointer = intern (":pointer");
22206 staticpro (&QCpointer);
22207 Qrect = intern ("rect");
22208 staticpro (&Qrect);
22209 Qcircle = intern ("circle");
22210 staticpro (&Qcircle);
22211 Qpoly = intern ("poly");
22212 staticpro (&Qpoly);
22213 Qmessage_truncate_lines = intern ("message-truncate-lines");
22214 staticpro (&Qmessage_truncate_lines);
22215 Qcursor_in_non_selected_windows = intern ("cursor-in-non-selected-windows");
22216 staticpro (&Qcursor_in_non_selected_windows);
22217 Qgrow_only = intern ("grow-only");
22218 staticpro (&Qgrow_only);
22219 Qinhibit_menubar_update = intern ("inhibit-menubar-update");
22220 staticpro (&Qinhibit_menubar_update);
22221 Qinhibit_eval_during_redisplay = intern ("inhibit-eval-during-redisplay");
22222 staticpro (&Qinhibit_eval_during_redisplay);
22223 Qposition = intern ("position");
22224 staticpro (&Qposition);
22225 Qbuffer_position = intern ("buffer-position");
22226 staticpro (&Qbuffer_position);
22227 Qobject = intern ("object");
22228 staticpro (&Qobject);
22229 Qbar = intern ("bar");
22230 staticpro (&Qbar);
22231 Qhbar = intern ("hbar");
22232 staticpro (&Qhbar);
22233 Qbox = intern ("box");
22234 staticpro (&Qbox);
22235 Qhollow = intern ("hollow");
22236 staticpro (&Qhollow);
22237 Qhand = intern ("hand");
22238 staticpro (&Qhand);
22239 Qarrow = intern ("arrow");
22240 staticpro (&Qarrow);
22241 Qtext = intern ("text");
22242 staticpro (&Qtext);
22243 Qrisky_local_variable = intern ("risky-local-variable");
22244 staticpro (&Qrisky_local_variable);
22245 Qinhibit_free_realized_faces = intern ("inhibit-free-realized-faces");
22246 staticpro (&Qinhibit_free_realized_faces);
22247
22248 list_of_error = Fcons (Fcons (intern ("error"),
22249 Fcons (intern ("void-variable"), Qnil)),
22250 Qnil);
22251 staticpro (&list_of_error);
22252
22253 Qlast_arrow_position = intern ("last-arrow-position");
22254 staticpro (&Qlast_arrow_position);
22255 Qlast_arrow_string = intern ("last-arrow-string");
22256 staticpro (&Qlast_arrow_string);
22257
22258 Qoverlay_arrow_string = intern ("overlay-arrow-string");
22259 staticpro (&Qoverlay_arrow_string);
22260 Qoverlay_arrow_bitmap = intern ("overlay-arrow-bitmap");
22261 staticpro (&Qoverlay_arrow_bitmap);
22262
22263 echo_buffer[0] = echo_buffer[1] = Qnil;
22264 staticpro (&echo_buffer[0]);
22265 staticpro (&echo_buffer[1]);
22266
22267 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
22268 staticpro (&echo_area_buffer[0]);
22269 staticpro (&echo_area_buffer[1]);
22270
22271 Vmessages_buffer_name = build_string ("*Messages*");
22272 staticpro (&Vmessages_buffer_name);
22273
22274 mode_line_proptrans_alist = Qnil;
22275 staticpro (&mode_line_proptrans_alist);
22276
22277 mode_line_string_list = Qnil;
22278 staticpro (&mode_line_string_list);
22279
22280 help_echo_string = Qnil;
22281 staticpro (&help_echo_string);
22282 help_echo_object = Qnil;
22283 staticpro (&help_echo_object);
22284 help_echo_window = Qnil;
22285 staticpro (&help_echo_window);
22286 previous_help_echo_string = Qnil;
22287 staticpro (&previous_help_echo_string);
22288 help_echo_pos = -1;
22289
22290 #ifdef HAVE_WINDOW_SYSTEM
22291 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
22292 doc: /* *Non-nil means draw block cursor as wide as the glyph under it.
22293 For example, if a block cursor is over a tab, it will be drawn as
22294 wide as that tab on the display. */);
22295 x_stretch_cursor_p = 0;
22296 #endif
22297
22298 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
22299 doc: /* *Non-nil means highlight trailing whitespace.
22300 The face used for trailing whitespace is `trailing-whitespace'. */);
22301 Vshow_trailing_whitespace = Qnil;
22302
22303 DEFVAR_LISP ("void-text-area-pointer", &Vvoid_text_area_pointer,
22304 doc: /* *The pointer shape to show in void text areas.
22305 Nil means to show the text pointer. Other options are `arrow', `text',
22306 `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
22307 Vvoid_text_area_pointer = Qarrow;
22308
22309 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
22310 doc: /* Non-nil means don't actually do any redisplay.
22311 This is used for internal purposes. */);
22312 Vinhibit_redisplay = Qnil;
22313
22314 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
22315 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
22316 Vglobal_mode_string = Qnil;
22317
22318 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
22319 doc: /* Marker for where to display an arrow on top of the buffer text.
22320 This must be the beginning of a line in order to work.
22321 See also `overlay-arrow-string'. */);
22322 Voverlay_arrow_position = Qnil;
22323
22324 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
22325 doc: /* String to display as an arrow in non-window frames.
22326 See also `overlay-arrow-position'. */);
22327 Voverlay_arrow_string = Qnil;
22328
22329 DEFVAR_LISP ("overlay-arrow-variable-list", &Voverlay_arrow_variable_list,
22330 doc: /* List of variables (symbols) which hold markers for overlay arrows.
22331 The symbols on this list are examined during redisplay to determine
22332 where to display overlay arrows. */);
22333 Voverlay_arrow_variable_list
22334 = Fcons (intern ("overlay-arrow-position"), Qnil);
22335
22336 DEFVAR_INT ("scroll-step", &scroll_step,
22337 doc: /* *The number of lines to try scrolling a window by when point moves out.
22338 If that fails to bring point back on frame, point is centered instead.
22339 If this is zero, point is always centered after it moves off frame.
22340 If you want scrolling to always be a line at a time, you should set
22341 `scroll-conservatively' to a large value rather than set this to 1. */);
22342
22343 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
22344 doc: /* *Scroll up to this many lines, to bring point back on screen.
22345 A value of zero means to scroll the text to center point vertically
22346 in the window. */);
22347 scroll_conservatively = 0;
22348
22349 DEFVAR_INT ("scroll-margin", &scroll_margin,
22350 doc: /* *Number of lines of margin at the top and bottom of a window.
22351 Recenter the window whenever point gets within this many lines
22352 of the top or bottom of the window. */);
22353 scroll_margin = 0;
22354
22355 DEFVAR_LISP ("display-pixels-per-inch", &Vdisplay_pixels_per_inch,
22356 doc: /* Pixels per inch on current display.
22357 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
22358 Vdisplay_pixels_per_inch = make_float (72.0);
22359
22360 #if GLYPH_DEBUG
22361 DEFVAR_INT ("debug-end-pos", &debug_end_pos, doc: /* Don't ask. */);
22362 #endif
22363
22364 DEFVAR_BOOL ("truncate-partial-width-windows",
22365 &truncate_partial_width_windows,
22366 doc: /* *Non-nil means truncate lines in all windows less than full frame wide. */);
22367 truncate_partial_width_windows = 1;
22368
22369 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
22370 doc: /* nil means display the mode-line/header-line/menu-bar in the default face.
22371 Any other value means to use the appropriate face, `mode-line',
22372 `header-line', or `menu' respectively. */);
22373 mode_line_inverse_video = 1;
22374
22375 DEFVAR_LISP ("line-number-display-limit", &Vline_number_display_limit,
22376 doc: /* *Maximum buffer size for which line number should be displayed.
22377 If the buffer is bigger than this, the line number does not appear
22378 in the mode line. A value of nil means no limit. */);
22379 Vline_number_display_limit = Qnil;
22380
22381 DEFVAR_INT ("line-number-display-limit-width",
22382 &line_number_display_limit_width,
22383 doc: /* *Maximum line width (in characters) for line number display.
22384 If the average length of the lines near point is bigger than this, then the
22385 line number may be omitted from the mode line. */);
22386 line_number_display_limit_width = 200;
22387
22388 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
22389 doc: /* *Non-nil means highlight region even in nonselected windows. */);
22390 highlight_nonselected_windows = 0;
22391
22392 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
22393 doc: /* Non-nil if more than one frame is visible on this display.
22394 Minibuffer-only frames don't count, but iconified frames do.
22395 This variable is not guaranteed to be accurate except while processing
22396 `frame-title-format' and `icon-title-format'. */);
22397
22398 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
22399 doc: /* Template for displaying the title bar of visible frames.
22400 \(Assuming the window manager supports this feature.)
22401 This variable has the same structure as `mode-line-format' (which see),
22402 and is used only on frames for which no explicit name has been set
22403 \(see `modify-frame-parameters'). */);
22404
22405 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
22406 doc: /* Template for displaying the title bar of an iconified frame.
22407 \(Assuming the window manager supports this feature.)
22408 This variable has the same structure as `mode-line-format' (which see),
22409 and is used only on frames for which no explicit name has been set
22410 \(see `modify-frame-parameters'). */);
22411 Vicon_title_format
22412 = Vframe_title_format
22413 = Fcons (intern ("multiple-frames"),
22414 Fcons (build_string ("%b"),
22415 Fcons (Fcons (empty_string,
22416 Fcons (intern ("invocation-name"),
22417 Fcons (build_string ("@"),
22418 Fcons (intern ("system-name"),
22419 Qnil)))),
22420 Qnil)));
22421
22422 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
22423 doc: /* Maximum number of lines to keep in the message log buffer.
22424 If nil, disable message logging. If t, log messages but don't truncate
22425 the buffer when it becomes large. */);
22426 Vmessage_log_max = make_number (50);
22427
22428 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
22429 doc: /* Functions called before redisplay, if window sizes have changed.
22430 The value should be a list of functions that take one argument.
22431 Just before redisplay, for each frame, if any of its windows have changed
22432 size since the last redisplay, or have been split or deleted,
22433 all the functions in the list are called, with the frame as argument. */);
22434 Vwindow_size_change_functions = Qnil;
22435
22436 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
22437 doc: /* List of functions to call before redisplaying a window with scrolling.
22438 Each function is called with two arguments, the window
22439 and its new display-start position. Note that the value of `window-end'
22440 is not valid when these functions are called. */);
22441 Vwindow_scroll_functions = Qnil;
22442
22443 DEFVAR_BOOL ("mouse-autoselect-window", &mouse_autoselect_window,
22444 doc: /* *Non-nil means autoselect window with mouse pointer. */);
22445 mouse_autoselect_window = 0;
22446
22447 DEFVAR_BOOL ("auto-resize-tool-bars", &auto_resize_tool_bars_p,
22448 doc: /* *Non-nil means automatically resize tool-bars.
22449 This increases a tool-bar's height if not all tool-bar items are visible.
22450 It decreases a tool-bar's height when it would display blank lines
22451 otherwise. */);
22452 auto_resize_tool_bars_p = 1;
22453
22454 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
22455 doc: /* *Non-nil means raise tool-bar buttons when the mouse moves over them. */);
22456 auto_raise_tool_bar_buttons_p = 1;
22457
22458 DEFVAR_BOOL ("make-cursor-line-fully-visible", &make_cursor_line_fully_visible_p,
22459 doc: /* *Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
22460 make_cursor_line_fully_visible_p = 1;
22461
22462 DEFVAR_LISP ("tool-bar-button-margin", &Vtool_bar_button_margin,
22463 doc: /* *Margin around tool-bar buttons in pixels.
22464 If an integer, use that for both horizontal and vertical margins.
22465 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
22466 HORZ specifying the horizontal margin, and VERT specifying the
22467 vertical margin. */);
22468 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
22469
22470 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
22471 doc: /* *Relief thickness of tool-bar buttons. */);
22472 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
22473
22474 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
22475 doc: /* List of functions to call to fontify regions of text.
22476 Each function is called with one argument POS. Functions must
22477 fontify a region starting at POS in the current buffer, and give
22478 fontified regions the property `fontified'. */);
22479 Vfontification_functions = Qnil;
22480 Fmake_variable_buffer_local (Qfontification_functions);
22481
22482 DEFVAR_BOOL ("unibyte-display-via-language-environment",
22483 &unibyte_display_via_language_environment,
22484 doc: /* *Non-nil means display unibyte text according to language environment.
22485 Specifically this means that unibyte non-ASCII characters
22486 are displayed by converting them to the equivalent multibyte characters
22487 according to the current language environment. As a result, they are
22488 displayed according to the current fontset. */);
22489 unibyte_display_via_language_environment = 0;
22490
22491 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
22492 doc: /* *Maximum height for resizing mini-windows.
22493 If a float, it specifies a fraction of the mini-window frame's height.
22494 If an integer, it specifies a number of lines. */);
22495 Vmax_mini_window_height = make_float (0.25);
22496
22497 DEFVAR_LISP ("resize-mini-windows", &Vresize_mini_windows,
22498 doc: /* *How to resize mini-windows.
22499 A value of nil means don't automatically resize mini-windows.
22500 A value of t means resize them to fit the text displayed in them.
22501 A value of `grow-only', the default, means let mini-windows grow
22502 only, until their display becomes empty, at which point the windows
22503 go back to their normal size. */);
22504 Vresize_mini_windows = Qgrow_only;
22505
22506 DEFVAR_LISP ("cursor-in-non-selected-windows",
22507 &Vcursor_in_non_selected_windows,
22508 doc: /* *Cursor type to display in non-selected windows.
22509 t means to use hollow box cursor. See `cursor-type' for other values. */);
22510 Vcursor_in_non_selected_windows = Qt;
22511
22512 DEFVAR_LISP ("blink-cursor-alist", &Vblink_cursor_alist,
22513 doc: /* Alist specifying how to blink the cursor off.
22514 Each element has the form (ON-STATE . OFF-STATE). Whenever the
22515 `cursor-type' frame-parameter or variable equals ON-STATE,
22516 comparing using `equal', Emacs uses OFF-STATE to specify
22517 how to blink it off. */);
22518 Vblink_cursor_alist = Qnil;
22519
22520 DEFVAR_BOOL ("auto-hscroll-mode", &automatic_hscrolling_p,
22521 doc: /* *Non-nil means scroll the display automatically to make point visible. */);
22522 automatic_hscrolling_p = 1;
22523
22524 DEFVAR_INT ("hscroll-margin", &hscroll_margin,
22525 doc: /* *How many columns away from the window edge point is allowed to get
22526 before automatic hscrolling will horizontally scroll the window. */);
22527 hscroll_margin = 5;
22528
22529 DEFVAR_LISP ("hscroll-step", &Vhscroll_step,
22530 doc: /* *How many columns to scroll the window when point gets too close to the edge.
22531 When point is less than `automatic-hscroll-margin' columns from the window
22532 edge, automatic hscrolling will scroll the window by the amount of columns
22533 determined by this variable. If its value is a positive integer, scroll that
22534 many columns. If it's a positive floating-point number, it specifies the
22535 fraction of the window's width to scroll. If it's nil or zero, point will be
22536 centered horizontally after the scroll. Any other value, including negative
22537 numbers, are treated as if the value were zero.
22538
22539 Automatic hscrolling always moves point outside the scroll margin, so if
22540 point was more than scroll step columns inside the margin, the window will
22541 scroll more than the value given by the scroll step.
22542
22543 Note that the lower bound for automatic hscrolling specified by `scroll-left'
22544 and `scroll-right' overrides this variable's effect. */);
22545 Vhscroll_step = make_number (0);
22546
22547 DEFVAR_BOOL ("message-truncate-lines", &message_truncate_lines,
22548 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
22549 Bind this around calls to `message' to let it take effect. */);
22550 message_truncate_lines = 0;
22551
22552 DEFVAR_LISP ("menu-bar-update-hook", &Vmenu_bar_update_hook,
22553 doc: /* Normal hook run for clicks on menu bar, before displaying a submenu.
22554 Can be used to update submenus whose contents should vary. */);
22555 Vmenu_bar_update_hook = Qnil;
22556
22557 DEFVAR_BOOL ("inhibit-menubar-update", &inhibit_menubar_update,
22558 doc: /* Non-nil means don't update menu bars. Internal use only. */);
22559 inhibit_menubar_update = 0;
22560
22561 DEFVAR_BOOL ("inhibit-eval-during-redisplay", &inhibit_eval_during_redisplay,
22562 doc: /* Non-nil means don't eval Lisp during redisplay. */);
22563 inhibit_eval_during_redisplay = 0;
22564
22565 DEFVAR_BOOL ("inhibit-free-realized-faces", &inhibit_free_realized_faces,
22566 doc: /* Non-nil means don't free realized faces. Internal use only. */);
22567 inhibit_free_realized_faces = 0;
22568
22569 #if GLYPH_DEBUG
22570 DEFVAR_BOOL ("inhibit-try-window-id", &inhibit_try_window_id,
22571 doc: /* Inhibit try_window_id display optimization. */);
22572 inhibit_try_window_id = 0;
22573
22574 DEFVAR_BOOL ("inhibit-try-window-reusing", &inhibit_try_window_reusing,
22575 doc: /* Inhibit try_window_reusing display optimization. */);
22576 inhibit_try_window_reusing = 0;
22577
22578 DEFVAR_BOOL ("inhibit-try-cursor-movement", &inhibit_try_cursor_movement,
22579 doc: /* Inhibit try_cursor_movement display optimization. */);
22580 inhibit_try_cursor_movement = 0;
22581 #endif /* GLYPH_DEBUG */
22582 }
22583
22584
22585 /* Initialize this module when Emacs starts. */
22586
22587 void
22588 init_xdisp ()
22589 {
22590 Lisp_Object root_window;
22591 struct window *mini_w;
22592
22593 current_header_line_height = current_mode_line_height = -1;
22594
22595 CHARPOS (this_line_start_pos) = 0;
22596
22597 mini_w = XWINDOW (minibuf_window);
22598 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
22599
22600 if (!noninteractive)
22601 {
22602 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
22603 int i;
22604
22605 XWINDOW (root_window)->top_line = make_number (FRAME_TOP_MARGIN (f));
22606 set_window_height (root_window,
22607 FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f),
22608 0);
22609 mini_w->top_line = make_number (FRAME_LINES (f) - 1);
22610 set_window_height (minibuf_window, 1, 0);
22611
22612 XWINDOW (root_window)->total_cols = make_number (FRAME_COLS (f));
22613 mini_w->total_cols = make_number (FRAME_COLS (f));
22614
22615 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
22616 scratch_glyph_row.glyphs[TEXT_AREA + 1]
22617 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
22618
22619 /* The default ellipsis glyphs `...'. */
22620 for (i = 0; i < 3; ++i)
22621 default_invis_vector[i] = make_number ('.');
22622 }
22623
22624 {
22625 /* Allocate the buffer for frame titles.
22626 Also used for `format-mode-line'. */
22627 int size = 100;
22628 frame_title_buf = (char *) xmalloc (size);
22629 frame_title_buf_end = frame_title_buf + size;
22630 frame_title_ptr = NULL;
22631 }
22632
22633 help_echo_showing_p = 0;
22634 }
22635
22636
22637 /* arch-tag: eacc864d-bb6a-4b74-894a-1a4399a1358b
22638 (do not change this comment) */