]> code.delx.au - gnu-emacs/blob - src/xdisp.c
Merge from emacs--devo--0
[gnu-emacs] / src / xdisp.c
1 /* Display generation from window structure and buffer text.
2 Copyright (C) 1985, 1986, 1987, 1988, 1993, 1994, 1995,
3 1997, 1998, 1999, 2000, 2001, 2002, 2003,
4 2004, 2005, 2006, 2007 Free Software Foundation, Inc.
5
6 This file is part of GNU Emacs.
7
8 GNU Emacs is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
12
13 GNU Emacs is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GNU Emacs; see the file COPYING. If not, write to
20 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 Boston, MA 02110-1301, USA. */
22
23 /* New redisplay written by Gerd Moellmann <gerd@gnu.org>.
24
25 Redisplay.
26
27 Emacs separates the task of updating the display from code
28 modifying global state, e.g. buffer text. This way functions
29 operating on buffers don't also have to be concerned with updating
30 the display.
31
32 Updating the display is triggered by the Lisp interpreter when it
33 decides it's time to do it. This is done either automatically for
34 you as part of the interpreter's command loop or as the result of
35 calling Lisp functions like `sit-for'. The C function `redisplay'
36 in xdisp.c is the only entry into the inner redisplay code. (Or,
37 let's say almost---see the description of direct update
38 operations, below.)
39
40 The following diagram shows how redisplay code is invoked. As you
41 can see, Lisp calls redisplay and vice versa. Under window systems
42 like X, some portions of the redisplay code are also called
43 asynchronously during mouse movement or expose events. It is very
44 important that these code parts do NOT use the C library (malloc,
45 free) because many C libraries under Unix are not reentrant. They
46 may also NOT call functions of the Lisp interpreter which could
47 change the interpreter's state. If you don't follow these rules,
48 you will encounter bugs which are very hard to explain.
49
50 (Direct functions, see below)
51 direct_output_for_insert,
52 direct_forward_char (dispnew.c)
53 +---------------------------------+
54 | |
55 | V
56 +--------------+ redisplay +----------------+
57 | Lisp machine |---------------->| Redisplay code |<--+
58 +--------------+ (xdisp.c) +----------------+ |
59 ^ | |
60 +----------------------------------+ |
61 Don't use this path when called |
62 asynchronously! |
63 |
64 expose_window (asynchronous) |
65 |
66 X expose events -----+
67
68 What does redisplay do? Obviously, it has to figure out somehow what
69 has been changed since the last time the display has been updated,
70 and to make these changes visible. Preferably it would do that in
71 a moderately intelligent way, i.e. fast.
72
73 Changes in buffer text can be deduced from window and buffer
74 structures, and from some global variables like `beg_unchanged' and
75 `end_unchanged'. The contents of the display are additionally
76 recorded in a `glyph matrix', a two-dimensional matrix of glyph
77 structures. Each row in such a matrix corresponds to a line on the
78 display, and each glyph in a row corresponds to a column displaying
79 a character, an image, or what else. This matrix is called the
80 `current glyph matrix' or `current matrix' in redisplay
81 terminology.
82
83 For buffer parts that have been changed since the last update, a
84 second glyph matrix is constructed, the so called `desired glyph
85 matrix' or short `desired matrix'. Current and desired matrix are
86 then compared to find a cheap way to update the display, e.g. by
87 reusing part of the display by scrolling lines.
88
89
90 Direct operations.
91
92 You will find a lot of redisplay optimizations when you start
93 looking at the innards of redisplay. The overall goal of all these
94 optimizations is to make redisplay fast because it is done
95 frequently.
96
97 Two optimizations are not found in xdisp.c. These are the direct
98 operations mentioned above. As the name suggests they follow a
99 different principle than the rest of redisplay. Instead of
100 building a desired matrix and then comparing it with the current
101 display, they perform their actions directly on the display and on
102 the current matrix.
103
104 One direct operation updates the display after one character has
105 been entered. The other one moves the cursor by one position
106 forward or backward. You find these functions under the names
107 `direct_output_for_insert' and `direct_output_forward_char' in
108 dispnew.c.
109
110
111 Desired matrices.
112
113 Desired matrices are always built per Emacs window. The function
114 `display_line' is the central function to look at if you are
115 interested. It constructs one row in a desired matrix given an
116 iterator structure containing both a buffer position and a
117 description of the environment in which the text is to be
118 displayed. But this is too early, read on.
119
120 Characters and pixmaps displayed for a range of buffer text depend
121 on various settings of buffers and windows, on overlays and text
122 properties, on display tables, on selective display. The good news
123 is that all this hairy stuff is hidden behind a small set of
124 interface functions taking an iterator structure (struct it)
125 argument.
126
127 Iteration over things to be displayed is then simple. It is
128 started by initializing an iterator with a call to init_iterator.
129 Calls to get_next_display_element fill the iterator structure with
130 relevant information about the next thing to display. Calls to
131 set_iterator_to_next move the iterator to the next thing.
132
133 Besides this, an iterator also contains information about the
134 display environment in which glyphs for display elements are to be
135 produced. It has fields for the width and height of the display,
136 the information whether long lines are truncated or continued, a
137 current X and Y position, and lots of other stuff you can better
138 see in dispextern.h.
139
140 Glyphs in a desired matrix are normally constructed in a loop
141 calling get_next_display_element and then produce_glyphs. The call
142 to produce_glyphs will fill the iterator structure with pixel
143 information about the element being displayed and at the same time
144 produce glyphs for it. If the display element fits on the line
145 being displayed, set_iterator_to_next is called next, otherwise the
146 glyphs produced are discarded.
147
148
149 Frame matrices.
150
151 That just couldn't be all, could it? What about terminal types not
152 supporting operations on sub-windows of the screen? To update the
153 display on such a terminal, window-based glyph matrices are not
154 well suited. To be able to reuse part of the display (scrolling
155 lines up and down), we must instead have a view of the whole
156 screen. This is what `frame matrices' are for. They are a trick.
157
158 Frames on terminals like above have a glyph pool. Windows on such
159 a frame sub-allocate their glyph memory from their frame's glyph
160 pool. The frame itself is given its own glyph matrices. By
161 coincidence---or maybe something else---rows in window glyph
162 matrices are slices of corresponding rows in frame matrices. Thus
163 writing to window matrices implicitly updates a frame matrix which
164 provides us with the view of the whole screen that we originally
165 wanted to have without having to move many bytes around. To be
166 honest, there is a little bit more done, but not much more. If you
167 plan to extend that code, take a look at dispnew.c. The function
168 build_frame_matrix is a good starting point. */
169
170 #include <config.h>
171 #include <stdio.h>
172
173 #include "lisp.h"
174 #include "keyboard.h"
175 #include "frame.h"
176 #include "window.h"
177 #include "termchar.h"
178 #include "dispextern.h"
179 #include "buffer.h"
180 #include "character.h"
181 #include "charset.h"
182 #include "indent.h"
183 #include "commands.h"
184 #include "keymap.h"
185 #include "macros.h"
186 #include "disptab.h"
187 #include "termhooks.h"
188 #include "intervals.h"
189 #include "coding.h"
190 #include "process.h"
191 #include "region-cache.h"
192 #include "fontset.h"
193 #include "blockinput.h"
194
195 #ifdef HAVE_X_WINDOWS
196 #include "xterm.h"
197 #endif
198 #ifdef WINDOWSNT
199 #include "w32term.h"
200 #endif
201 #ifdef MAC_OS
202 #include "macterm.h"
203 #endif
204
205 #ifdef HAVE_WINDOW_SYSTEM
206 #ifdef USE_FONT_BACKEND
207 #include "font.h"
208 #endif /* USE_FONT_BACKEND */
209 #endif /* HAVE_WINDOW_SYSTEM */
210
211 #ifndef FRAME_X_OUTPUT
212 #define FRAME_X_OUTPUT(f) ((f)->output_data.x)
213 #endif
214
215 #define INFINITY 10000000
216
217 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
218 || defined (USE_GTK)
219 extern void set_frame_menubar P_ ((struct frame *f, int, int));
220 extern int pending_menu_activation;
221 #endif
222
223 extern int interrupt_input;
224 extern int command_loop_level;
225
226 extern Lisp_Object do_mouse_tracking;
227
228 extern int minibuffer_auto_raise;
229 extern Lisp_Object Vminibuffer_list;
230
231 extern Lisp_Object Qface;
232 extern Lisp_Object Qmode_line, Qmode_line_inactive, Qheader_line;
233
234 extern Lisp_Object Voverriding_local_map;
235 extern Lisp_Object Voverriding_local_map_menu_flag;
236 extern Lisp_Object Qmenu_item;
237 extern Lisp_Object Qwhen;
238 extern Lisp_Object Qhelp_echo;
239
240 Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
241 Lisp_Object Qwindow_scroll_functions, Vwindow_scroll_functions;
242 Lisp_Object Qredisplay_end_trigger_functions, Vredisplay_end_trigger_functions;
243 Lisp_Object Qinhibit_point_motion_hooks;
244 Lisp_Object QCeval, QCfile, QCdata, QCpropertize;
245 Lisp_Object Qfontified;
246 Lisp_Object Qgrow_only;
247 Lisp_Object Qinhibit_eval_during_redisplay;
248 Lisp_Object Qbuffer_position, Qposition, Qobject;
249
250 /* Cursor shapes */
251 Lisp_Object Qbar, Qhbar, Qbox, Qhollow;
252
253 /* Pointer shapes */
254 Lisp_Object Qarrow, Qhand, Qtext;
255
256 Lisp_Object Qrisky_local_variable;
257
258 /* Holds the list (error). */
259 Lisp_Object list_of_error;
260
261 /* Functions called to fontify regions of text. */
262
263 Lisp_Object Vfontification_functions;
264 Lisp_Object Qfontification_functions;
265
266 /* Non-nil means automatically select any window when the mouse
267 cursor moves into it. */
268 Lisp_Object Vmouse_autoselect_window;
269
270 /* Non-zero means draw tool bar buttons raised when the mouse moves
271 over them. */
272
273 int auto_raise_tool_bar_buttons_p;
274
275 /* Non-zero means to reposition window if cursor line is only partially visible. */
276
277 int make_cursor_line_fully_visible_p;
278
279 /* Margin below tool bar in pixels. 0 or nil means no margin.
280 If value is `internal-border-width' or `border-width',
281 the corresponding frame parameter is used. */
282
283 Lisp_Object Vtool_bar_border;
284
285 /* Margin around tool bar buttons in pixels. */
286
287 Lisp_Object Vtool_bar_button_margin;
288
289 /* Thickness of shadow to draw around tool bar buttons. */
290
291 EMACS_INT tool_bar_button_relief;
292
293 /* Non-nil means automatically resize tool-bars so that all tool-bar
294 items are visible, and no blank lines remain.
295
296 If value is `grow-only', only make tool-bar bigger. */
297
298 Lisp_Object Vauto_resize_tool_bars;
299
300 /* Non-zero means draw block and hollow cursor as wide as the glyph
301 under it. For example, if a block cursor is over a tab, it will be
302 drawn as wide as that tab on the display. */
303
304 int x_stretch_cursor_p;
305
306 /* Non-nil means don't actually do any redisplay. */
307
308 Lisp_Object Vinhibit_redisplay, Qinhibit_redisplay;
309
310 /* Non-zero means Lisp evaluation during redisplay is inhibited. */
311
312 int inhibit_eval_during_redisplay;
313
314 /* Names of text properties relevant for redisplay. */
315
316 Lisp_Object Qdisplay;
317 extern Lisp_Object Qface, Qinvisible, Qwidth;
318
319 /* Symbols used in text property values. */
320
321 Lisp_Object Vdisplay_pixels_per_inch;
322 Lisp_Object Qspace, QCalign_to, QCrelative_width, QCrelative_height;
323 Lisp_Object Qleft_margin, Qright_margin, Qspace_width, Qraise;
324 Lisp_Object Qslice;
325 Lisp_Object Qcenter;
326 Lisp_Object Qmargin, Qpointer;
327 Lisp_Object Qline_height;
328 extern Lisp_Object Qheight;
329 extern Lisp_Object QCwidth, QCheight, QCascent;
330 extern Lisp_Object Qscroll_bar;
331 extern Lisp_Object Qcursor;
332
333 /* Non-nil means highlight trailing whitespace. */
334
335 Lisp_Object Vshow_trailing_whitespace;
336
337 /* Non-nil means escape non-break space and hyphens. */
338
339 Lisp_Object Vnobreak_char_display;
340
341 #ifdef HAVE_WINDOW_SYSTEM
342 extern Lisp_Object Voverflow_newline_into_fringe;
343
344 /* Test if overflow newline into fringe. Called with iterator IT
345 at or past right window margin, and with IT->current_x set. */
346
347 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(it) \
348 (!NILP (Voverflow_newline_into_fringe) \
349 && FRAME_WINDOW_P (it->f) \
350 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) > 0 \
351 && it->current_x == it->last_visible_x)
352
353 #endif /* HAVE_WINDOW_SYSTEM */
354
355 /* Non-nil means show the text cursor in void text areas
356 i.e. in blank areas after eol and eob. This used to be
357 the default in 21.3. */
358
359 Lisp_Object Vvoid_text_area_pointer;
360
361 /* Name of the face used to highlight trailing whitespace. */
362
363 Lisp_Object Qtrailing_whitespace;
364
365 /* Name and number of the face used to highlight escape glyphs. */
366
367 Lisp_Object Qescape_glyph;
368
369 /* Name and number of the face used to highlight non-breaking spaces. */
370
371 Lisp_Object Qnobreak_space;
372
373 /* The symbol `image' which is the car of the lists used to represent
374 images in Lisp. */
375
376 Lisp_Object Qimage;
377
378 /* The image map types. */
379 Lisp_Object QCmap, QCpointer;
380 Lisp_Object Qrect, Qcircle, Qpoly;
381
382 /* Non-zero means print newline to stdout before next mini-buffer
383 message. */
384
385 int noninteractive_need_newline;
386
387 /* Non-zero means print newline to message log before next message. */
388
389 static int message_log_need_newline;
390
391 /* Three markers that message_dolog uses.
392 It could allocate them itself, but that causes trouble
393 in handling memory-full errors. */
394 static Lisp_Object message_dolog_marker1;
395 static Lisp_Object message_dolog_marker2;
396 static Lisp_Object message_dolog_marker3;
397 \f
398 /* The buffer position of the first character appearing entirely or
399 partially on the line of the selected window which contains the
400 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
401 redisplay optimization in redisplay_internal. */
402
403 static struct text_pos this_line_start_pos;
404
405 /* Number of characters past the end of the line above, including the
406 terminating newline. */
407
408 static struct text_pos this_line_end_pos;
409
410 /* The vertical positions and the height of this line. */
411
412 static int this_line_vpos;
413 static int this_line_y;
414 static int this_line_pixel_height;
415
416 /* X position at which this display line starts. Usually zero;
417 negative if first character is partially visible. */
418
419 static int this_line_start_x;
420
421 /* Buffer that this_line_.* variables are referring to. */
422
423 static struct buffer *this_line_buffer;
424
425 /* Nonzero means truncate lines in all windows less wide than the
426 frame. */
427
428 int truncate_partial_width_windows;
429
430 /* A flag to control how to display unibyte 8-bit character. */
431
432 int unibyte_display_via_language_environment;
433
434 /* Nonzero means we have more than one non-mini-buffer-only frame.
435 Not guaranteed to be accurate except while parsing
436 frame-title-format. */
437
438 int multiple_frames;
439
440 Lisp_Object Vglobal_mode_string;
441
442
443 /* List of variables (symbols) which hold markers for overlay arrows.
444 The symbols on this list are examined during redisplay to determine
445 where to display overlay arrows. */
446
447 Lisp_Object Voverlay_arrow_variable_list;
448
449 /* Marker for where to display an arrow on top of the buffer text. */
450
451 Lisp_Object Voverlay_arrow_position;
452
453 /* String to display for the arrow. Only used on terminal frames. */
454
455 Lisp_Object Voverlay_arrow_string;
456
457 /* Values of those variables at last redisplay are stored as
458 properties on `overlay-arrow-position' symbol. However, if
459 Voverlay_arrow_position is a marker, last-arrow-position is its
460 numerical position. */
461
462 Lisp_Object Qlast_arrow_position, Qlast_arrow_string;
463
464 /* Alternative overlay-arrow-string and overlay-arrow-bitmap
465 properties on a symbol in overlay-arrow-variable-list. */
466
467 Lisp_Object Qoverlay_arrow_string, Qoverlay_arrow_bitmap;
468
469 /* Like mode-line-format, but for the title bar on a visible frame. */
470
471 Lisp_Object Vframe_title_format;
472
473 /* Like mode-line-format, but for the title bar on an iconified frame. */
474
475 Lisp_Object Vicon_title_format;
476
477 /* List of functions to call when a window's size changes. These
478 functions get one arg, a frame on which one or more windows' sizes
479 have changed. */
480
481 static Lisp_Object Vwindow_size_change_functions;
482
483 Lisp_Object Qmenu_bar_update_hook, Vmenu_bar_update_hook;
484
485 /* Nonzero if an overlay arrow has been displayed in this window. */
486
487 static int overlay_arrow_seen;
488
489 /* Nonzero means highlight the region even in nonselected windows. */
490
491 int highlight_nonselected_windows;
492
493 /* If cursor motion alone moves point off frame, try scrolling this
494 many lines up or down if that will bring it back. */
495
496 static EMACS_INT scroll_step;
497
498 /* Nonzero means scroll just far enough to bring point back on the
499 screen, when appropriate. */
500
501 static EMACS_INT scroll_conservatively;
502
503 /* Recenter the window whenever point gets within this many lines of
504 the top or bottom of the window. This value is translated into a
505 pixel value by multiplying it with FRAME_LINE_HEIGHT, which means
506 that there is really a fixed pixel height scroll margin. */
507
508 EMACS_INT scroll_margin;
509
510 /* Number of windows showing the buffer of the selected window (or
511 another buffer with the same base buffer). keyboard.c refers to
512 this. */
513
514 int buffer_shared;
515
516 /* Vector containing glyphs for an ellipsis `...'. */
517
518 static Lisp_Object default_invis_vector[3];
519
520 /* Zero means display the mode-line/header-line/menu-bar in the default face
521 (this slightly odd definition is for compatibility with previous versions
522 of emacs), non-zero means display them using their respective faces.
523
524 This variable is deprecated. */
525
526 int mode_line_inverse_video;
527
528 /* Prompt to display in front of the mini-buffer contents. */
529
530 Lisp_Object minibuf_prompt;
531
532 /* Width of current mini-buffer prompt. Only set after display_line
533 of the line that contains the prompt. */
534
535 int minibuf_prompt_width;
536
537 /* This is the window where the echo area message was displayed. It
538 is always a mini-buffer window, but it may not be the same window
539 currently active as a mini-buffer. */
540
541 Lisp_Object echo_area_window;
542
543 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
544 pushes the current message and the value of
545 message_enable_multibyte on the stack, the function restore_message
546 pops the stack and displays MESSAGE again. */
547
548 Lisp_Object Vmessage_stack;
549
550 /* Nonzero means multibyte characters were enabled when the echo area
551 message was specified. */
552
553 int message_enable_multibyte;
554
555 /* Nonzero if we should redraw the mode lines on the next redisplay. */
556
557 int update_mode_lines;
558
559 /* Nonzero if window sizes or contents have changed since last
560 redisplay that finished. */
561
562 int windows_or_buffers_changed;
563
564 /* Nonzero means a frame's cursor type has been changed. */
565
566 int cursor_type_changed;
567
568 /* Nonzero after display_mode_line if %l was used and it displayed a
569 line number. */
570
571 int line_number_displayed;
572
573 /* Maximum buffer size for which to display line numbers. */
574
575 Lisp_Object Vline_number_display_limit;
576
577 /* Line width to consider when repositioning for line number display. */
578
579 static EMACS_INT line_number_display_limit_width;
580
581 /* Number of lines to keep in the message log buffer. t means
582 infinite. nil means don't log at all. */
583
584 Lisp_Object Vmessage_log_max;
585
586 /* The name of the *Messages* buffer, a string. */
587
588 static Lisp_Object Vmessages_buffer_name;
589
590 /* Current, index 0, and last displayed echo area message. Either
591 buffers from echo_buffers, or nil to indicate no message. */
592
593 Lisp_Object echo_area_buffer[2];
594
595 /* The buffers referenced from echo_area_buffer. */
596
597 static Lisp_Object echo_buffer[2];
598
599 /* A vector saved used in with_area_buffer to reduce consing. */
600
601 static Lisp_Object Vwith_echo_area_save_vector;
602
603 /* Non-zero means display_echo_area should display the last echo area
604 message again. Set by redisplay_preserve_echo_area. */
605
606 static int display_last_displayed_message_p;
607
608 /* Nonzero if echo area is being used by print; zero if being used by
609 message. */
610
611 int message_buf_print;
612
613 /* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
614
615 Lisp_Object Qinhibit_menubar_update;
616 int inhibit_menubar_update;
617
618 /* When evaluating expressions from menu bar items (enable conditions,
619 for instance), this is the frame they are being processed for. */
620
621 Lisp_Object Vmenu_updating_frame;
622
623 /* Maximum height for resizing mini-windows. Either a float
624 specifying a fraction of the available height, or an integer
625 specifying a number of lines. */
626
627 Lisp_Object Vmax_mini_window_height;
628
629 /* Non-zero means messages should be displayed with truncated
630 lines instead of being continued. */
631
632 int message_truncate_lines;
633 Lisp_Object Qmessage_truncate_lines;
634
635 /* Set to 1 in clear_message to make redisplay_internal aware
636 of an emptied echo area. */
637
638 static int message_cleared_p;
639
640 /* How to blink the default frame cursor off. */
641 Lisp_Object Vblink_cursor_alist;
642
643 /* A scratch glyph row with contents used for generating truncation
644 glyphs. Also used in direct_output_for_insert. */
645
646 #define MAX_SCRATCH_GLYPHS 100
647 struct glyph_row scratch_glyph_row;
648 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
649
650 /* Ascent and height of the last line processed by move_it_to. */
651
652 static int last_max_ascent, last_height;
653
654 /* Non-zero if there's a help-echo in the echo area. */
655
656 int help_echo_showing_p;
657
658 /* If >= 0, computed, exact values of mode-line and header-line height
659 to use in the macros CURRENT_MODE_LINE_HEIGHT and
660 CURRENT_HEADER_LINE_HEIGHT. */
661
662 int current_mode_line_height, current_header_line_height;
663
664 /* The maximum distance to look ahead for text properties. Values
665 that are too small let us call compute_char_face and similar
666 functions too often which is expensive. Values that are too large
667 let us call compute_char_face and alike too often because we
668 might not be interested in text properties that far away. */
669
670 #define TEXT_PROP_DISTANCE_LIMIT 100
671
672 #if GLYPH_DEBUG
673
674 /* Variables to turn off display optimizations from Lisp. */
675
676 int inhibit_try_window_id, inhibit_try_window_reusing;
677 int inhibit_try_cursor_movement;
678
679 /* Non-zero means print traces of redisplay if compiled with
680 GLYPH_DEBUG != 0. */
681
682 int trace_redisplay_p;
683
684 #endif /* GLYPH_DEBUG */
685
686 #ifdef DEBUG_TRACE_MOVE
687 /* Non-zero means trace with TRACE_MOVE to stderr. */
688 int trace_move;
689
690 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
691 #else
692 #define TRACE_MOVE(x) (void) 0
693 #endif
694
695 /* Non-zero means automatically scroll windows horizontally to make
696 point visible. */
697
698 int automatic_hscrolling_p;
699
700 /* How close to the margin can point get before the window is scrolled
701 horizontally. */
702 EMACS_INT hscroll_margin;
703
704 /* How much to scroll horizontally when point is inside the above margin. */
705 Lisp_Object Vhscroll_step;
706
707 /* The variable `resize-mini-windows'. If nil, don't resize
708 mini-windows. If t, always resize them to fit the text they
709 display. If `grow-only', let mini-windows grow only until they
710 become empty. */
711
712 Lisp_Object Vresize_mini_windows;
713
714 /* Buffer being redisplayed -- for redisplay_window_error. */
715
716 struct buffer *displayed_buffer;
717
718 /* Space between overline and text. */
719
720 EMACS_INT overline_margin;
721
722 /* Value returned from text property handlers (see below). */
723
724 enum prop_handled
725 {
726 HANDLED_NORMALLY,
727 HANDLED_RECOMPUTE_PROPS,
728 HANDLED_OVERLAY_STRING_CONSUMED,
729 HANDLED_RETURN
730 };
731
732 /* A description of text properties that redisplay is interested
733 in. */
734
735 struct props
736 {
737 /* The name of the property. */
738 Lisp_Object *name;
739
740 /* A unique index for the property. */
741 enum prop_idx idx;
742
743 /* A handler function called to set up iterator IT from the property
744 at IT's current position. Value is used to steer handle_stop. */
745 enum prop_handled (*handler) P_ ((struct it *it));
746 };
747
748 static enum prop_handled handle_face_prop P_ ((struct it *));
749 static enum prop_handled handle_invisible_prop P_ ((struct it *));
750 static enum prop_handled handle_display_prop P_ ((struct it *));
751 static enum prop_handled handle_composition_prop P_ ((struct it *));
752 static enum prop_handled handle_overlay_change P_ ((struct it *));
753 static enum prop_handled handle_fontified_prop P_ ((struct it *));
754 static enum prop_handled handle_auto_composed_prop P_ ((struct it *));
755
756 /* Properties handled by iterators. */
757
758 static struct props it_props[] =
759 {
760 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
761 /* Handle `face' before `display' because some sub-properties of
762 `display' need to know the face. */
763 {&Qface, FACE_PROP_IDX, handle_face_prop},
764 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
765 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
766 {&Qauto_composed, AUTO_COMPOSED_PROP_IDX, handle_auto_composed_prop},
767 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
768 {NULL, 0, NULL}
769 };
770
771 /* Value is the position described by X. If X is a marker, value is
772 the marker_position of X. Otherwise, value is X. */
773
774 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
775
776 /* Enumeration returned by some move_it_.* functions internally. */
777
778 enum move_it_result
779 {
780 /* Not used. Undefined value. */
781 MOVE_UNDEFINED,
782
783 /* Move ended at the requested buffer position or ZV. */
784 MOVE_POS_MATCH_OR_ZV,
785
786 /* Move ended at the requested X pixel position. */
787 MOVE_X_REACHED,
788
789 /* Move within a line ended at the end of a line that must be
790 continued. */
791 MOVE_LINE_CONTINUED,
792
793 /* Move within a line ended at the end of a line that would
794 be displayed truncated. */
795 MOVE_LINE_TRUNCATED,
796
797 /* Move within a line ended at a line end. */
798 MOVE_NEWLINE_OR_CR
799 };
800
801 /* This counter is used to clear the face cache every once in a while
802 in redisplay_internal. It is incremented for each redisplay.
803 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
804 cleared. */
805
806 #define CLEAR_FACE_CACHE_COUNT 500
807 static int clear_face_cache_count;
808
809 /* Similarly for the image cache. */
810
811 #ifdef HAVE_WINDOW_SYSTEM
812 #define CLEAR_IMAGE_CACHE_COUNT 101
813 static int clear_image_cache_count;
814 #endif
815
816 /* Non-zero while redisplay_internal is in progress. */
817
818 int redisplaying_p;
819
820 /* Non-zero means don't free realized faces. Bound while freeing
821 realized faces is dangerous because glyph matrices might still
822 reference them. */
823
824 int inhibit_free_realized_faces;
825 Lisp_Object Qinhibit_free_realized_faces;
826
827 /* If a string, XTread_socket generates an event to display that string.
828 (The display is done in read_char.) */
829
830 Lisp_Object help_echo_string;
831 Lisp_Object help_echo_window;
832 Lisp_Object help_echo_object;
833 int help_echo_pos;
834
835 /* Temporary variable for XTread_socket. */
836
837 Lisp_Object previous_help_echo_string;
838
839 /* Null glyph slice */
840
841 static struct glyph_slice null_glyph_slice = { 0, 0, 0, 0 };
842
843 \f
844 /* Function prototypes. */
845
846 static void setup_for_ellipsis P_ ((struct it *, int));
847 static void mark_window_display_accurate_1 P_ ((struct window *, int));
848 static int single_display_spec_string_p P_ ((Lisp_Object, Lisp_Object));
849 static int display_prop_string_p P_ ((Lisp_Object, Lisp_Object));
850 static int cursor_row_p P_ ((struct window *, struct glyph_row *));
851 static int redisplay_mode_lines P_ ((Lisp_Object, int));
852 static char *decode_mode_spec_coding P_ ((Lisp_Object, char *, int));
853
854 #if 0
855 static int invisible_text_between_p P_ ((struct it *, int, int));
856 #endif
857
858 static void pint2str P_ ((char *, int, int));
859 static void pint2hrstr P_ ((char *, int, int));
860 static struct text_pos run_window_scroll_functions P_ ((Lisp_Object,
861 struct text_pos));
862 static void reconsider_clip_changes P_ ((struct window *, struct buffer *));
863 static int text_outside_line_unchanged_p P_ ((struct window *, int, int));
864 static void store_mode_line_noprop_char P_ ((char));
865 static int store_mode_line_noprop P_ ((const unsigned char *, int, int));
866 static void x_consider_frame_title P_ ((Lisp_Object));
867 static void handle_stop P_ ((struct it *));
868 static int tool_bar_lines_needed P_ ((struct frame *, int *));
869 static int single_display_spec_intangible_p P_ ((Lisp_Object));
870 static void ensure_echo_area_buffers P_ ((void));
871 static Lisp_Object unwind_with_echo_area_buffer P_ ((Lisp_Object));
872 static Lisp_Object with_echo_area_buffer_unwind_data P_ ((struct window *));
873 static int with_echo_area_buffer P_ ((struct window *, int,
874 int (*) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
875 EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
876 static void clear_garbaged_frames P_ ((void));
877 static int current_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
878 static int truncate_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
879 static int set_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
880 static int display_echo_area P_ ((struct window *));
881 static int display_echo_area_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
882 static int resize_mini_window_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
883 static Lisp_Object unwind_redisplay P_ ((Lisp_Object));
884 static int string_char_and_length P_ ((const unsigned char *, int, int *));
885 static struct text_pos display_prop_end P_ ((struct it *, Lisp_Object,
886 struct text_pos));
887 static int compute_window_start_on_continuation_line P_ ((struct window *));
888 static Lisp_Object safe_eval_handler P_ ((Lisp_Object));
889 static void insert_left_trunc_glyphs P_ ((struct it *));
890 static struct glyph_row *get_overlay_arrow_glyph_row P_ ((struct window *,
891 Lisp_Object));
892 static void extend_face_to_end_of_line P_ ((struct it *));
893 static int append_space_for_newline P_ ((struct it *, int));
894 static int cursor_row_fully_visible_p P_ ((struct window *, int, int));
895 static int try_scrolling P_ ((Lisp_Object, int, EMACS_INT, EMACS_INT, int, int));
896 static int try_cursor_movement P_ ((Lisp_Object, struct text_pos, int *));
897 static int trailing_whitespace_p P_ ((int));
898 static int message_log_check_duplicate P_ ((int, int, int, int));
899 static void push_it P_ ((struct it *));
900 static void pop_it P_ ((struct it *));
901 static void sync_frame_with_window_matrix_rows P_ ((struct window *));
902 static void select_frame_for_redisplay P_ ((Lisp_Object));
903 static void redisplay_internal P_ ((int));
904 static int echo_area_display P_ ((int));
905 static void redisplay_windows P_ ((Lisp_Object));
906 static void redisplay_window P_ ((Lisp_Object, int));
907 static Lisp_Object redisplay_window_error ();
908 static Lisp_Object redisplay_window_0 P_ ((Lisp_Object));
909 static Lisp_Object redisplay_window_1 P_ ((Lisp_Object));
910 static int update_menu_bar P_ ((struct frame *, int, int));
911 static int try_window_reusing_current_matrix P_ ((struct window *));
912 static int try_window_id P_ ((struct window *));
913 static int display_line P_ ((struct it *));
914 static int display_mode_lines P_ ((struct window *));
915 static int display_mode_line P_ ((struct window *, enum face_id, Lisp_Object));
916 static int display_mode_element P_ ((struct it *, int, int, int, Lisp_Object, Lisp_Object, int));
917 static int store_mode_line_string P_ ((char *, Lisp_Object, int, int, int, Lisp_Object));
918 static char *decode_mode_spec P_ ((struct window *, int, int, int, int *));
919 static void display_menu_bar P_ ((struct window *));
920 static int display_count_lines P_ ((int, int, int, int, int *));
921 static int display_string P_ ((unsigned char *, Lisp_Object, Lisp_Object,
922 int, int, struct it *, int, int, int, int));
923 static void compute_line_metrics P_ ((struct it *));
924 static void run_redisplay_end_trigger_hook P_ ((struct it *));
925 static int get_overlay_strings P_ ((struct it *, int));
926 static int get_overlay_strings_1 P_ ((struct it *, int, int));
927 static void next_overlay_string P_ ((struct it *));
928 static void reseat P_ ((struct it *, struct text_pos, int));
929 static void reseat_1 P_ ((struct it *, struct text_pos, int));
930 static void back_to_previous_visible_line_start P_ ((struct it *));
931 void reseat_at_previous_visible_line_start P_ ((struct it *));
932 static void reseat_at_next_visible_line_start P_ ((struct it *, int));
933 static int next_element_from_ellipsis P_ ((struct it *));
934 static int next_element_from_display_vector P_ ((struct it *));
935 static int next_element_from_string P_ ((struct it *));
936 static int next_element_from_c_string P_ ((struct it *));
937 static int next_element_from_buffer P_ ((struct it *));
938 static int next_element_from_composition P_ ((struct it *));
939 static int next_element_from_image P_ ((struct it *));
940 static int next_element_from_stretch P_ ((struct it *));
941 static void load_overlay_strings P_ ((struct it *, int));
942 static int init_from_display_pos P_ ((struct it *, struct window *,
943 struct display_pos *));
944 static void reseat_to_string P_ ((struct it *, unsigned char *,
945 Lisp_Object, int, int, int, int));
946 static enum move_it_result move_it_in_display_line_to P_ ((struct it *,
947 int, int, int));
948 void move_it_vertically_backward P_ ((struct it *, int));
949 static void init_to_row_start P_ ((struct it *, struct window *,
950 struct glyph_row *));
951 static int init_to_row_end P_ ((struct it *, struct window *,
952 struct glyph_row *));
953 static void back_to_previous_line_start P_ ((struct it *));
954 static int forward_to_next_line_start P_ ((struct it *, int *));
955 static struct text_pos string_pos_nchars_ahead P_ ((struct text_pos,
956 Lisp_Object, int));
957 static struct text_pos string_pos P_ ((int, Lisp_Object));
958 static struct text_pos c_string_pos P_ ((int, unsigned char *, int));
959 static int number_of_chars P_ ((unsigned char *, int));
960 static void compute_stop_pos P_ ((struct it *));
961 static void compute_string_pos P_ ((struct text_pos *, struct text_pos,
962 Lisp_Object));
963 static int face_before_or_after_it_pos P_ ((struct it *, int));
964 static int next_overlay_change P_ ((int));
965 static int handle_single_display_spec P_ ((struct it *, Lisp_Object,
966 Lisp_Object, struct text_pos *,
967 int));
968 static int underlying_face_id P_ ((struct it *));
969 static int in_ellipses_for_invisible_text_p P_ ((struct display_pos *,
970 struct window *));
971
972 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
973 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
974
975 #ifdef HAVE_WINDOW_SYSTEM
976
977 static void update_tool_bar P_ ((struct frame *, int));
978 static void build_desired_tool_bar_string P_ ((struct frame *f));
979 static int redisplay_tool_bar P_ ((struct frame *));
980 static void display_tool_bar_line P_ ((struct it *, int));
981 static void notice_overwritten_cursor P_ ((struct window *,
982 enum glyph_row_area,
983 int, int, int, int));
984
985
986
987 #endif /* HAVE_WINDOW_SYSTEM */
988
989 \f
990 /***********************************************************************
991 Window display dimensions
992 ***********************************************************************/
993
994 /* Return the bottom boundary y-position for text lines in window W.
995 This is the first y position at which a line cannot start.
996 It is relative to the top of the window.
997
998 This is the height of W minus the height of a mode line, if any. */
999
1000 INLINE int
1001 window_text_bottom_y (w)
1002 struct window *w;
1003 {
1004 int height = WINDOW_TOTAL_HEIGHT (w);
1005
1006 if (WINDOW_WANTS_MODELINE_P (w))
1007 height -= CURRENT_MODE_LINE_HEIGHT (w);
1008 return height;
1009 }
1010
1011 /* Return the pixel width of display area AREA of window W. AREA < 0
1012 means return the total width of W, not including fringes to
1013 the left and right of the window. */
1014
1015 INLINE int
1016 window_box_width (w, area)
1017 struct window *w;
1018 int area;
1019 {
1020 int cols = XFASTINT (w->total_cols);
1021 int pixels = 0;
1022
1023 if (!w->pseudo_window_p)
1024 {
1025 cols -= WINDOW_SCROLL_BAR_COLS (w);
1026
1027 if (area == TEXT_AREA)
1028 {
1029 if (INTEGERP (w->left_margin_cols))
1030 cols -= XFASTINT (w->left_margin_cols);
1031 if (INTEGERP (w->right_margin_cols))
1032 cols -= XFASTINT (w->right_margin_cols);
1033 pixels = -WINDOW_TOTAL_FRINGE_WIDTH (w);
1034 }
1035 else if (area == LEFT_MARGIN_AREA)
1036 {
1037 cols = (INTEGERP (w->left_margin_cols)
1038 ? XFASTINT (w->left_margin_cols) : 0);
1039 pixels = 0;
1040 }
1041 else if (area == RIGHT_MARGIN_AREA)
1042 {
1043 cols = (INTEGERP (w->right_margin_cols)
1044 ? XFASTINT (w->right_margin_cols) : 0);
1045 pixels = 0;
1046 }
1047 }
1048
1049 return cols * WINDOW_FRAME_COLUMN_WIDTH (w) + pixels;
1050 }
1051
1052
1053 /* Return the pixel height of the display area of window W, not
1054 including mode lines of W, if any. */
1055
1056 INLINE int
1057 window_box_height (w)
1058 struct window *w;
1059 {
1060 struct frame *f = XFRAME (w->frame);
1061 int height = WINDOW_TOTAL_HEIGHT (w);
1062
1063 xassert (height >= 0);
1064
1065 /* Note: the code below that determines the mode-line/header-line
1066 height is essentially the same as that contained in the macro
1067 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
1068 the appropriate glyph row has its `mode_line_p' flag set,
1069 and if it doesn't, uses estimate_mode_line_height instead. */
1070
1071 if (WINDOW_WANTS_MODELINE_P (w))
1072 {
1073 struct glyph_row *ml_row
1074 = (w->current_matrix && w->current_matrix->rows
1075 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
1076 : 0);
1077 if (ml_row && ml_row->mode_line_p)
1078 height -= ml_row->height;
1079 else
1080 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
1081 }
1082
1083 if (WINDOW_WANTS_HEADER_LINE_P (w))
1084 {
1085 struct glyph_row *hl_row
1086 = (w->current_matrix && w->current_matrix->rows
1087 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1088 : 0);
1089 if (hl_row && hl_row->mode_line_p)
1090 height -= hl_row->height;
1091 else
1092 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1093 }
1094
1095 /* With a very small font and a mode-line that's taller than
1096 default, we might end up with a negative height. */
1097 return max (0, height);
1098 }
1099
1100 /* Return the window-relative coordinate of the left edge of display
1101 area AREA of window W. AREA < 0 means return the left edge of the
1102 whole window, to the right of the left fringe of W. */
1103
1104 INLINE int
1105 window_box_left_offset (w, area)
1106 struct window *w;
1107 int area;
1108 {
1109 int x;
1110
1111 if (w->pseudo_window_p)
1112 return 0;
1113
1114 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1115
1116 if (area == TEXT_AREA)
1117 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1118 + window_box_width (w, LEFT_MARGIN_AREA));
1119 else if (area == RIGHT_MARGIN_AREA)
1120 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1121 + window_box_width (w, LEFT_MARGIN_AREA)
1122 + window_box_width (w, TEXT_AREA)
1123 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1124 ? 0
1125 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1126 else if (area == LEFT_MARGIN_AREA
1127 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1128 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1129
1130 return x;
1131 }
1132
1133
1134 /* Return the window-relative coordinate of the right edge of display
1135 area AREA of window W. AREA < 0 means return the left edge of the
1136 whole window, to the left of the right fringe of W. */
1137
1138 INLINE int
1139 window_box_right_offset (w, area)
1140 struct window *w;
1141 int area;
1142 {
1143 return window_box_left_offset (w, area) + window_box_width (w, area);
1144 }
1145
1146 /* Return the frame-relative coordinate of the left edge of display
1147 area AREA of window W. AREA < 0 means return the left edge of the
1148 whole window, to the right of the left fringe of W. */
1149
1150 INLINE int
1151 window_box_left (w, area)
1152 struct window *w;
1153 int area;
1154 {
1155 struct frame *f = XFRAME (w->frame);
1156 int x;
1157
1158 if (w->pseudo_window_p)
1159 return FRAME_INTERNAL_BORDER_WIDTH (f);
1160
1161 x = (WINDOW_LEFT_EDGE_X (w)
1162 + window_box_left_offset (w, area));
1163
1164 return x;
1165 }
1166
1167
1168 /* Return the frame-relative coordinate of the right edge of display
1169 area AREA of window W. AREA < 0 means return the left edge of the
1170 whole window, to the left of the right fringe of W. */
1171
1172 INLINE int
1173 window_box_right (w, area)
1174 struct window *w;
1175 int area;
1176 {
1177 return window_box_left (w, area) + window_box_width (w, area);
1178 }
1179
1180 /* Get the bounding box of the display area AREA of window W, without
1181 mode lines, in frame-relative coordinates. AREA < 0 means the
1182 whole window, not including the left and right fringes of
1183 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1184 coordinates of the upper-left corner of the box. Return in
1185 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1186
1187 INLINE void
1188 window_box (w, area, box_x, box_y, box_width, box_height)
1189 struct window *w;
1190 int area;
1191 int *box_x, *box_y, *box_width, *box_height;
1192 {
1193 if (box_width)
1194 *box_width = window_box_width (w, area);
1195 if (box_height)
1196 *box_height = window_box_height (w);
1197 if (box_x)
1198 *box_x = window_box_left (w, area);
1199 if (box_y)
1200 {
1201 *box_y = WINDOW_TOP_EDGE_Y (w);
1202 if (WINDOW_WANTS_HEADER_LINE_P (w))
1203 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1204 }
1205 }
1206
1207
1208 /* Get the bounding box of the display area AREA of window W, without
1209 mode lines. AREA < 0 means the whole window, not including the
1210 left and right fringe of the window. Return in *TOP_LEFT_X
1211 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1212 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1213 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1214 box. */
1215
1216 INLINE void
1217 window_box_edges (w, area, top_left_x, top_left_y,
1218 bottom_right_x, bottom_right_y)
1219 struct window *w;
1220 int area;
1221 int *top_left_x, *top_left_y, *bottom_right_x, *bottom_right_y;
1222 {
1223 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
1224 bottom_right_y);
1225 *bottom_right_x += *top_left_x;
1226 *bottom_right_y += *top_left_y;
1227 }
1228
1229
1230 \f
1231 /***********************************************************************
1232 Utilities
1233 ***********************************************************************/
1234
1235 /* Return the bottom y-position of the line the iterator IT is in.
1236 This can modify IT's settings. */
1237
1238 int
1239 line_bottom_y (it)
1240 struct it *it;
1241 {
1242 int line_height = it->max_ascent + it->max_descent;
1243 int line_top_y = it->current_y;
1244
1245 if (line_height == 0)
1246 {
1247 if (last_height)
1248 line_height = last_height;
1249 else if (IT_CHARPOS (*it) < ZV)
1250 {
1251 move_it_by_lines (it, 1, 1);
1252 line_height = (it->max_ascent || it->max_descent
1253 ? it->max_ascent + it->max_descent
1254 : last_height);
1255 }
1256 else
1257 {
1258 struct glyph_row *row = it->glyph_row;
1259
1260 /* Use the default character height. */
1261 it->glyph_row = NULL;
1262 it->what = IT_CHARACTER;
1263 it->c = ' ';
1264 it->len = 1;
1265 PRODUCE_GLYPHS (it);
1266 line_height = it->ascent + it->descent;
1267 it->glyph_row = row;
1268 }
1269 }
1270
1271 return line_top_y + line_height;
1272 }
1273
1274
1275 /* Return 1 if position CHARPOS is visible in window W.
1276 CHARPOS < 0 means return info about WINDOW_END position.
1277 If visible, set *X and *Y to pixel coordinates of top left corner.
1278 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1279 Set *ROWH and *VPOS to row's visible height and VPOS (row number). */
1280
1281 int
1282 pos_visible_p (w, charpos, x, y, rtop, rbot, rowh, vpos)
1283 struct window *w;
1284 int charpos, *x, *y, *rtop, *rbot, *rowh, *vpos;
1285 {
1286 struct it it;
1287 struct text_pos top;
1288 int visible_p = 0;
1289 struct buffer *old_buffer = NULL;
1290
1291 if (noninteractive)
1292 return visible_p;
1293
1294 if (XBUFFER (w->buffer) != current_buffer)
1295 {
1296 old_buffer = current_buffer;
1297 set_buffer_internal_1 (XBUFFER (w->buffer));
1298 }
1299
1300 SET_TEXT_POS_FROM_MARKER (top, w->start);
1301
1302 /* Compute exact mode line heights. */
1303 if (WINDOW_WANTS_MODELINE_P (w))
1304 current_mode_line_height
1305 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1306 current_buffer->mode_line_format);
1307
1308 if (WINDOW_WANTS_HEADER_LINE_P (w))
1309 current_header_line_height
1310 = display_mode_line (w, HEADER_LINE_FACE_ID,
1311 current_buffer->header_line_format);
1312
1313 start_display (&it, w, top);
1314 move_it_to (&it, charpos, -1, it.last_visible_y-1, -1,
1315 (charpos >= 0 ? MOVE_TO_POS : 0) | MOVE_TO_Y);
1316
1317 /* Note that we may overshoot because of invisible text. */
1318 if (charpos >= 0 && IT_CHARPOS (it) >= charpos)
1319 {
1320 int top_x = it.current_x;
1321 int top_y = it.current_y;
1322 int bottom_y = (last_height = 0, line_bottom_y (&it));
1323 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1324
1325 if (top_y < window_top_y)
1326 visible_p = bottom_y > window_top_y;
1327 else if (top_y < it.last_visible_y)
1328 visible_p = 1;
1329 if (visible_p)
1330 {
1331 *x = top_x;
1332 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1333 *rtop = max (0, window_top_y - top_y);
1334 *rbot = max (0, bottom_y - it.last_visible_y);
1335 *rowh = max (0, (min (bottom_y, it.last_visible_y)
1336 - max (top_y, window_top_y)));
1337 *vpos = it.vpos;
1338 }
1339 }
1340 else
1341 {
1342 struct it it2;
1343
1344 it2 = it;
1345 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1346 move_it_by_lines (&it, 1, 0);
1347 if (charpos < IT_CHARPOS (it)
1348 || (it.what == IT_EOB && charpos == IT_CHARPOS (it)))
1349 {
1350 visible_p = 1;
1351 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1352 *x = it2.current_x;
1353 *y = it2.current_y + it2.max_ascent - it2.ascent;
1354 *rtop = max (0, -it2.current_y);
1355 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1356 - it.last_visible_y));
1357 *rowh = max (0, (min (it2.current_y + it2.max_ascent + it2.max_descent,
1358 it.last_visible_y)
1359 - max (it2.current_y,
1360 WINDOW_HEADER_LINE_HEIGHT (w))));
1361 *vpos = it2.vpos;
1362 }
1363 }
1364
1365 if (old_buffer)
1366 set_buffer_internal_1 (old_buffer);
1367
1368 current_header_line_height = current_mode_line_height = -1;
1369
1370 if (visible_p && XFASTINT (w->hscroll) > 0)
1371 *x -= XFASTINT (w->hscroll) * WINDOW_FRAME_COLUMN_WIDTH (w);
1372
1373 #if 0
1374 /* Debugging code. */
1375 if (visible_p)
1376 fprintf (stderr, "+pv pt=%d vs=%d --> x=%d y=%d rt=%d rb=%d rh=%d vp=%d\n",
1377 charpos, w->vscroll, *x, *y, *rtop, *rbot, *rowh, *vpos);
1378 else
1379 fprintf (stderr, "-pv pt=%d vs=%d\n", charpos, w->vscroll);
1380 #endif
1381
1382 return visible_p;
1383 }
1384
1385
1386 /* Return the next character from STR which is MAXLEN bytes long.
1387 Return in *LEN the length of the character. This is like
1388 STRING_CHAR_AND_LENGTH but never returns an invalid character. If
1389 we find one, we return a `?', but with the length of the invalid
1390 character. */
1391
1392 static INLINE int
1393 string_char_and_length (str, maxlen, len)
1394 const unsigned char *str;
1395 int maxlen, *len;
1396 {
1397 int c;
1398
1399 c = STRING_CHAR_AND_LENGTH (str, maxlen, *len);
1400 if (!CHAR_VALID_P (c, 1))
1401 /* We may not change the length here because other places in Emacs
1402 don't use this function, i.e. they silently accept invalid
1403 characters. */
1404 c = '?';
1405
1406 return c;
1407 }
1408
1409
1410
1411 /* Given a position POS containing a valid character and byte position
1412 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1413
1414 static struct text_pos
1415 string_pos_nchars_ahead (pos, string, nchars)
1416 struct text_pos pos;
1417 Lisp_Object string;
1418 int nchars;
1419 {
1420 xassert (STRINGP (string) && nchars >= 0);
1421
1422 if (STRING_MULTIBYTE (string))
1423 {
1424 int rest = SBYTES (string) - BYTEPOS (pos);
1425 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1426 int len;
1427
1428 while (nchars--)
1429 {
1430 string_char_and_length (p, rest, &len);
1431 p += len, rest -= len;
1432 xassert (rest >= 0);
1433 CHARPOS (pos) += 1;
1434 BYTEPOS (pos) += len;
1435 }
1436 }
1437 else
1438 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1439
1440 return pos;
1441 }
1442
1443
1444 /* Value is the text position, i.e. character and byte position,
1445 for character position CHARPOS in STRING. */
1446
1447 static INLINE struct text_pos
1448 string_pos (charpos, string)
1449 int charpos;
1450 Lisp_Object string;
1451 {
1452 struct text_pos pos;
1453 xassert (STRINGP (string));
1454 xassert (charpos >= 0);
1455 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1456 return pos;
1457 }
1458
1459
1460 /* Value is a text position, i.e. character and byte position, for
1461 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1462 means recognize multibyte characters. */
1463
1464 static struct text_pos
1465 c_string_pos (charpos, s, multibyte_p)
1466 int charpos;
1467 unsigned char *s;
1468 int multibyte_p;
1469 {
1470 struct text_pos pos;
1471
1472 xassert (s != NULL);
1473 xassert (charpos >= 0);
1474
1475 if (multibyte_p)
1476 {
1477 int rest = strlen (s), len;
1478
1479 SET_TEXT_POS (pos, 0, 0);
1480 while (charpos--)
1481 {
1482 string_char_and_length (s, rest, &len);
1483 s += len, rest -= len;
1484 xassert (rest >= 0);
1485 CHARPOS (pos) += 1;
1486 BYTEPOS (pos) += len;
1487 }
1488 }
1489 else
1490 SET_TEXT_POS (pos, charpos, charpos);
1491
1492 return pos;
1493 }
1494
1495
1496 /* Value is the number of characters in C string S. MULTIBYTE_P
1497 non-zero means recognize multibyte characters. */
1498
1499 static int
1500 number_of_chars (s, multibyte_p)
1501 unsigned char *s;
1502 int multibyte_p;
1503 {
1504 int nchars;
1505
1506 if (multibyte_p)
1507 {
1508 int rest = strlen (s), len;
1509 unsigned char *p = (unsigned char *) s;
1510
1511 for (nchars = 0; rest > 0; ++nchars)
1512 {
1513 string_char_and_length (p, rest, &len);
1514 rest -= len, p += len;
1515 }
1516 }
1517 else
1518 nchars = strlen (s);
1519
1520 return nchars;
1521 }
1522
1523
1524 /* Compute byte position NEWPOS->bytepos corresponding to
1525 NEWPOS->charpos. POS is a known position in string STRING.
1526 NEWPOS->charpos must be >= POS.charpos. */
1527
1528 static void
1529 compute_string_pos (newpos, pos, string)
1530 struct text_pos *newpos, pos;
1531 Lisp_Object string;
1532 {
1533 xassert (STRINGP (string));
1534 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1535
1536 if (STRING_MULTIBYTE (string))
1537 *newpos = string_pos_nchars_ahead (pos, string,
1538 CHARPOS (*newpos) - CHARPOS (pos));
1539 else
1540 BYTEPOS (*newpos) = CHARPOS (*newpos);
1541 }
1542
1543 /* EXPORT:
1544 Return an estimation of the pixel height of mode or top lines on
1545 frame F. FACE_ID specifies what line's height to estimate. */
1546
1547 int
1548 estimate_mode_line_height (f, face_id)
1549 struct frame *f;
1550 enum face_id face_id;
1551 {
1552 #ifdef HAVE_WINDOW_SYSTEM
1553 if (FRAME_WINDOW_P (f))
1554 {
1555 int height = FONT_HEIGHT (FRAME_FONT (f));
1556
1557 /* This function is called so early when Emacs starts that the face
1558 cache and mode line face are not yet initialized. */
1559 if (FRAME_FACE_CACHE (f))
1560 {
1561 struct face *face = FACE_FROM_ID (f, face_id);
1562 if (face)
1563 {
1564 if (face->font)
1565 height = FONT_HEIGHT (face->font);
1566 if (face->box_line_width > 0)
1567 height += 2 * face->box_line_width;
1568 }
1569 }
1570
1571 return height;
1572 }
1573 #endif
1574
1575 return 1;
1576 }
1577
1578 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1579 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1580 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1581 not force the value into range. */
1582
1583 void
1584 pixel_to_glyph_coords (f, pix_x, pix_y, x, y, bounds, noclip)
1585 FRAME_PTR f;
1586 register int pix_x, pix_y;
1587 int *x, *y;
1588 NativeRectangle *bounds;
1589 int noclip;
1590 {
1591
1592 #ifdef HAVE_WINDOW_SYSTEM
1593 if (FRAME_WINDOW_P (f))
1594 {
1595 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1596 even for negative values. */
1597 if (pix_x < 0)
1598 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1599 if (pix_y < 0)
1600 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1601
1602 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1603 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1604
1605 if (bounds)
1606 STORE_NATIVE_RECT (*bounds,
1607 FRAME_COL_TO_PIXEL_X (f, pix_x),
1608 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1609 FRAME_COLUMN_WIDTH (f) - 1,
1610 FRAME_LINE_HEIGHT (f) - 1);
1611
1612 if (!noclip)
1613 {
1614 if (pix_x < 0)
1615 pix_x = 0;
1616 else if (pix_x > FRAME_TOTAL_COLS (f))
1617 pix_x = FRAME_TOTAL_COLS (f);
1618
1619 if (pix_y < 0)
1620 pix_y = 0;
1621 else if (pix_y > FRAME_LINES (f))
1622 pix_y = FRAME_LINES (f);
1623 }
1624 }
1625 #endif
1626
1627 *x = pix_x;
1628 *y = pix_y;
1629 }
1630
1631
1632 /* Given HPOS/VPOS in the current matrix of W, return corresponding
1633 frame-relative pixel positions in *FRAME_X and *FRAME_Y. If we
1634 can't tell the positions because W's display is not up to date,
1635 return 0. */
1636
1637 int
1638 glyph_to_pixel_coords (w, hpos, vpos, frame_x, frame_y)
1639 struct window *w;
1640 int hpos, vpos;
1641 int *frame_x, *frame_y;
1642 {
1643 #ifdef HAVE_WINDOW_SYSTEM
1644 if (FRAME_WINDOW_P (XFRAME (WINDOW_FRAME (w))))
1645 {
1646 int success_p;
1647
1648 xassert (hpos >= 0 && hpos < w->current_matrix->matrix_w);
1649 xassert (vpos >= 0 && vpos < w->current_matrix->matrix_h);
1650
1651 if (display_completed)
1652 {
1653 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
1654 struct glyph *glyph = row->glyphs[TEXT_AREA];
1655 struct glyph *end = glyph + min (hpos, row->used[TEXT_AREA]);
1656
1657 hpos = row->x;
1658 vpos = row->y;
1659 while (glyph < end)
1660 {
1661 hpos += glyph->pixel_width;
1662 ++glyph;
1663 }
1664
1665 /* If first glyph is partially visible, its first visible position is still 0. */
1666 if (hpos < 0)
1667 hpos = 0;
1668
1669 success_p = 1;
1670 }
1671 else
1672 {
1673 hpos = vpos = 0;
1674 success_p = 0;
1675 }
1676
1677 *frame_x = WINDOW_TO_FRAME_PIXEL_X (w, hpos);
1678 *frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, vpos);
1679 return success_p;
1680 }
1681 #endif
1682
1683 *frame_x = hpos;
1684 *frame_y = vpos;
1685 return 1;
1686 }
1687
1688
1689 #ifdef HAVE_WINDOW_SYSTEM
1690
1691 /* Find the glyph under window-relative coordinates X/Y in window W.
1692 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1693 strings. Return in *HPOS and *VPOS the row and column number of
1694 the glyph found. Return in *AREA the glyph area containing X.
1695 Value is a pointer to the glyph found or null if X/Y is not on
1696 text, or we can't tell because W's current matrix is not up to
1697 date. */
1698
1699 static struct glyph *
1700 x_y_to_hpos_vpos (w, x, y, hpos, vpos, dx, dy, area)
1701 struct window *w;
1702 int x, y;
1703 int *hpos, *vpos, *dx, *dy, *area;
1704 {
1705 struct glyph *glyph, *end;
1706 struct glyph_row *row = NULL;
1707 int x0, i;
1708
1709 /* Find row containing Y. Give up if some row is not enabled. */
1710 for (i = 0; i < w->current_matrix->nrows; ++i)
1711 {
1712 row = MATRIX_ROW (w->current_matrix, i);
1713 if (!row->enabled_p)
1714 return NULL;
1715 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1716 break;
1717 }
1718
1719 *vpos = i;
1720 *hpos = 0;
1721
1722 /* Give up if Y is not in the window. */
1723 if (i == w->current_matrix->nrows)
1724 return NULL;
1725
1726 /* Get the glyph area containing X. */
1727 if (w->pseudo_window_p)
1728 {
1729 *area = TEXT_AREA;
1730 x0 = 0;
1731 }
1732 else
1733 {
1734 if (x < window_box_left_offset (w, TEXT_AREA))
1735 {
1736 *area = LEFT_MARGIN_AREA;
1737 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1738 }
1739 else if (x < window_box_right_offset (w, TEXT_AREA))
1740 {
1741 *area = TEXT_AREA;
1742 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1743 }
1744 else
1745 {
1746 *area = RIGHT_MARGIN_AREA;
1747 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1748 }
1749 }
1750
1751 /* Find glyph containing X. */
1752 glyph = row->glyphs[*area];
1753 end = glyph + row->used[*area];
1754 x -= x0;
1755 while (glyph < end && x >= glyph->pixel_width)
1756 {
1757 x -= glyph->pixel_width;
1758 ++glyph;
1759 }
1760
1761 if (glyph == end)
1762 return NULL;
1763
1764 if (dx)
1765 {
1766 *dx = x;
1767 *dy = y - (row->y + row->ascent - glyph->ascent);
1768 }
1769
1770 *hpos = glyph - row->glyphs[*area];
1771 return glyph;
1772 }
1773
1774
1775 /* EXPORT:
1776 Convert frame-relative x/y to coordinates relative to window W.
1777 Takes pseudo-windows into account. */
1778
1779 void
1780 frame_to_window_pixel_xy (w, x, y)
1781 struct window *w;
1782 int *x, *y;
1783 {
1784 if (w->pseudo_window_p)
1785 {
1786 /* A pseudo-window is always full-width, and starts at the
1787 left edge of the frame, plus a frame border. */
1788 struct frame *f = XFRAME (w->frame);
1789 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1790 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1791 }
1792 else
1793 {
1794 *x -= WINDOW_LEFT_EDGE_X (w);
1795 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1796 }
1797 }
1798
1799 /* EXPORT:
1800 Return in RECTS[] at most N clipping rectangles for glyph string S.
1801 Return the number of stored rectangles. */
1802
1803 int
1804 get_glyph_string_clip_rects (s, rects, n)
1805 struct glyph_string *s;
1806 NativeRectangle *rects;
1807 int n;
1808 {
1809 XRectangle r;
1810
1811 if (n <= 0)
1812 return 0;
1813
1814 if (s->row->full_width_p)
1815 {
1816 /* Draw full-width. X coordinates are relative to S->w->left_col. */
1817 r.x = WINDOW_LEFT_EDGE_X (s->w);
1818 r.width = WINDOW_TOTAL_WIDTH (s->w);
1819
1820 /* Unless displaying a mode or menu bar line, which are always
1821 fully visible, clip to the visible part of the row. */
1822 if (s->w->pseudo_window_p)
1823 r.height = s->row->visible_height;
1824 else
1825 r.height = s->height;
1826 }
1827 else
1828 {
1829 /* This is a text line that may be partially visible. */
1830 r.x = window_box_left (s->w, s->area);
1831 r.width = window_box_width (s->w, s->area);
1832 r.height = s->row->visible_height;
1833 }
1834
1835 if (s->clip_head)
1836 if (r.x < s->clip_head->x)
1837 {
1838 if (r.width >= s->clip_head->x - r.x)
1839 r.width -= s->clip_head->x - r.x;
1840 else
1841 r.width = 0;
1842 r.x = s->clip_head->x;
1843 }
1844 if (s->clip_tail)
1845 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
1846 {
1847 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
1848 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
1849 else
1850 r.width = 0;
1851 }
1852
1853 /* If S draws overlapping rows, it's sufficient to use the top and
1854 bottom of the window for clipping because this glyph string
1855 intentionally draws over other lines. */
1856 if (s->for_overlaps)
1857 {
1858 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1859 r.height = window_text_bottom_y (s->w) - r.y;
1860
1861 /* Alas, the above simple strategy does not work for the
1862 environments with anti-aliased text: if the same text is
1863 drawn onto the same place multiple times, it gets thicker.
1864 If the overlap we are processing is for the erased cursor, we
1865 take the intersection with the rectagle of the cursor. */
1866 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
1867 {
1868 XRectangle rc, r_save = r;
1869
1870 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
1871 rc.y = s->w->phys_cursor.y;
1872 rc.width = s->w->phys_cursor_width;
1873 rc.height = s->w->phys_cursor_height;
1874
1875 x_intersect_rectangles (&r_save, &rc, &r);
1876 }
1877 }
1878 else
1879 {
1880 /* Don't use S->y for clipping because it doesn't take partially
1881 visible lines into account. For example, it can be negative for
1882 partially visible lines at the top of a window. */
1883 if (!s->row->full_width_p
1884 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
1885 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1886 else
1887 r.y = max (0, s->row->y);
1888
1889 /* If drawing a tool-bar window, draw it over the internal border
1890 at the top of the window. */
1891 if (WINDOWP (s->f->tool_bar_window)
1892 && s->w == XWINDOW (s->f->tool_bar_window))
1893 r.y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
1894 }
1895
1896 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
1897
1898 /* If drawing the cursor, don't let glyph draw outside its
1899 advertised boundaries. Cleartype does this under some circumstances. */
1900 if (s->hl == DRAW_CURSOR)
1901 {
1902 struct glyph *glyph = s->first_glyph;
1903 int height, max_y;
1904
1905 if (s->x > r.x)
1906 {
1907 r.width -= s->x - r.x;
1908 r.x = s->x;
1909 }
1910 r.width = min (r.width, glyph->pixel_width);
1911
1912 /* If r.y is below window bottom, ensure that we still see a cursor. */
1913 height = min (glyph->ascent + glyph->descent,
1914 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
1915 max_y = window_text_bottom_y (s->w) - height;
1916 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
1917 if (s->ybase - glyph->ascent > max_y)
1918 {
1919 r.y = max_y;
1920 r.height = height;
1921 }
1922 else
1923 {
1924 /* Don't draw cursor glyph taller than our actual glyph. */
1925 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
1926 if (height < r.height)
1927 {
1928 max_y = r.y + r.height;
1929 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
1930 r.height = min (max_y - r.y, height);
1931 }
1932 }
1933 }
1934
1935 if (s->row->clip)
1936 {
1937 XRectangle r_save = r;
1938
1939 if (! x_intersect_rectangles (&r_save, s->row->clip, &r))
1940 r.width = 0;
1941 }
1942
1943 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
1944 || ((s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1))
1945 {
1946 #ifdef CONVERT_FROM_XRECT
1947 CONVERT_FROM_XRECT (r, *rects);
1948 #else
1949 *rects = r;
1950 #endif
1951 return 1;
1952 }
1953 else
1954 {
1955 /* If we are processing overlapping and allowed to return
1956 multiple clipping rectangles, we exclude the row of the glyph
1957 string from the clipping rectangle. This is to avoid drawing
1958 the same text on the environment with anti-aliasing. */
1959 #ifdef CONVERT_FROM_XRECT
1960 XRectangle rs[2];
1961 #else
1962 XRectangle *rs = rects;
1963 #endif
1964 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
1965
1966 if (s->for_overlaps & OVERLAPS_PRED)
1967 {
1968 rs[i] = r;
1969 if (r.y + r.height > row_y)
1970 {
1971 if (r.y < row_y)
1972 rs[i].height = row_y - r.y;
1973 else
1974 rs[i].height = 0;
1975 }
1976 i++;
1977 }
1978 if (s->for_overlaps & OVERLAPS_SUCC)
1979 {
1980 rs[i] = r;
1981 if (r.y < row_y + s->row->visible_height)
1982 {
1983 if (r.y + r.height > row_y + s->row->visible_height)
1984 {
1985 rs[i].y = row_y + s->row->visible_height;
1986 rs[i].height = r.y + r.height - rs[i].y;
1987 }
1988 else
1989 rs[i].height = 0;
1990 }
1991 i++;
1992 }
1993
1994 n = i;
1995 #ifdef CONVERT_FROM_XRECT
1996 for (i = 0; i < n; i++)
1997 CONVERT_FROM_XRECT (rs[i], rects[i]);
1998 #endif
1999 return n;
2000 }
2001 }
2002
2003 /* EXPORT:
2004 Return in *NR the clipping rectangle for glyph string S. */
2005
2006 void
2007 get_glyph_string_clip_rect (s, nr)
2008 struct glyph_string *s;
2009 NativeRectangle *nr;
2010 {
2011 get_glyph_string_clip_rects (s, nr, 1);
2012 }
2013
2014
2015 /* EXPORT:
2016 Return the position and height of the phys cursor in window W.
2017 Set w->phys_cursor_width to width of phys cursor.
2018 */
2019
2020 void
2021 get_phys_cursor_geometry (w, row, glyph, xp, yp, heightp)
2022 struct window *w;
2023 struct glyph_row *row;
2024 struct glyph *glyph;
2025 int *xp, *yp, *heightp;
2026 {
2027 struct frame *f = XFRAME (WINDOW_FRAME (w));
2028 int x, y, wd, h, h0, y0;
2029
2030 /* Compute the width of the rectangle to draw. If on a stretch
2031 glyph, and `x-stretch-block-cursor' is nil, don't draw a
2032 rectangle as wide as the glyph, but use a canonical character
2033 width instead. */
2034 wd = glyph->pixel_width - 1;
2035 #ifdef HAVE_NTGUI
2036 wd++; /* Why? */
2037 #endif
2038
2039 x = w->phys_cursor.x;
2040 if (x < 0)
2041 {
2042 wd += x;
2043 x = 0;
2044 }
2045
2046 if (glyph->type == STRETCH_GLYPH
2047 && !x_stretch_cursor_p)
2048 wd = min (FRAME_COLUMN_WIDTH (f), wd);
2049 w->phys_cursor_width = wd;
2050
2051 y = w->phys_cursor.y + row->ascent - glyph->ascent;
2052
2053 /* If y is below window bottom, ensure that we still see a cursor. */
2054 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
2055
2056 h = max (h0, glyph->ascent + glyph->descent);
2057 h0 = min (h0, glyph->ascent + glyph->descent);
2058
2059 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
2060 if (y < y0)
2061 {
2062 h = max (h - (y0 - y) + 1, h0);
2063 y = y0 - 1;
2064 }
2065 else
2066 {
2067 y0 = window_text_bottom_y (w) - h0;
2068 if (y > y0)
2069 {
2070 h += y - y0;
2071 y = y0;
2072 }
2073 }
2074
2075 *xp = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
2076 *yp = WINDOW_TO_FRAME_PIXEL_Y (w, y);
2077 *heightp = h;
2078 }
2079
2080 /*
2081 * Remember which glyph the mouse is over.
2082 */
2083
2084 void
2085 remember_mouse_glyph (f, gx, gy, rect)
2086 struct frame *f;
2087 int gx, gy;
2088 NativeRectangle *rect;
2089 {
2090 Lisp_Object window;
2091 struct window *w;
2092 struct glyph_row *r, *gr, *end_row;
2093 enum window_part part;
2094 enum glyph_row_area area;
2095 int x, y, width, height;
2096
2097 /* Try to determine frame pixel position and size of the glyph under
2098 frame pixel coordinates X/Y on frame F. */
2099
2100 if (!f->glyphs_initialized_p
2101 || (window = window_from_coordinates (f, gx, gy, &part, &x, &y, 0),
2102 NILP (window)))
2103 {
2104 width = FRAME_SMALLEST_CHAR_WIDTH (f);
2105 height = FRAME_SMALLEST_FONT_HEIGHT (f);
2106 goto virtual_glyph;
2107 }
2108
2109 w = XWINDOW (window);
2110 width = WINDOW_FRAME_COLUMN_WIDTH (w);
2111 height = WINDOW_FRAME_LINE_HEIGHT (w);
2112
2113 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
2114 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
2115
2116 if (w->pseudo_window_p)
2117 {
2118 area = TEXT_AREA;
2119 part = ON_MODE_LINE; /* Don't adjust margin. */
2120 goto text_glyph;
2121 }
2122
2123 switch (part)
2124 {
2125 case ON_LEFT_MARGIN:
2126 area = LEFT_MARGIN_AREA;
2127 goto text_glyph;
2128
2129 case ON_RIGHT_MARGIN:
2130 area = RIGHT_MARGIN_AREA;
2131 goto text_glyph;
2132
2133 case ON_HEADER_LINE:
2134 case ON_MODE_LINE:
2135 gr = (part == ON_HEADER_LINE
2136 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
2137 : MATRIX_MODE_LINE_ROW (w->current_matrix));
2138 gy = gr->y;
2139 area = TEXT_AREA;
2140 goto text_glyph_row_found;
2141
2142 case ON_TEXT:
2143 area = TEXT_AREA;
2144
2145 text_glyph:
2146 gr = 0; gy = 0;
2147 for (; r <= end_row && r->enabled_p; ++r)
2148 if (r->y + r->height > y)
2149 {
2150 gr = r; gy = r->y;
2151 break;
2152 }
2153
2154 text_glyph_row_found:
2155 if (gr && gy <= y)
2156 {
2157 struct glyph *g = gr->glyphs[area];
2158 struct glyph *end = g + gr->used[area];
2159
2160 height = gr->height;
2161 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2162 if (gx + g->pixel_width > x)
2163 break;
2164
2165 if (g < end)
2166 {
2167 if (g->type == IMAGE_GLYPH)
2168 {
2169 /* Don't remember when mouse is over image, as
2170 image may have hot-spots. */
2171 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2172 return;
2173 }
2174 width = g->pixel_width;
2175 }
2176 else
2177 {
2178 /* Use nominal char spacing at end of line. */
2179 x -= gx;
2180 gx += (x / width) * width;
2181 }
2182
2183 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2184 gx += window_box_left_offset (w, area);
2185 }
2186 else
2187 {
2188 /* Use nominal line height at end of window. */
2189 gx = (x / width) * width;
2190 y -= gy;
2191 gy += (y / height) * height;
2192 }
2193 break;
2194
2195 case ON_LEFT_FRINGE:
2196 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2197 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2198 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2199 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2200 goto row_glyph;
2201
2202 case ON_RIGHT_FRINGE:
2203 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2204 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2205 : window_box_right_offset (w, TEXT_AREA));
2206 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2207 goto row_glyph;
2208
2209 case ON_SCROLL_BAR:
2210 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2211 ? 0
2212 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2213 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2214 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2215 : 0)));
2216 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2217
2218 row_glyph:
2219 gr = 0, gy = 0;
2220 for (; r <= end_row && r->enabled_p; ++r)
2221 if (r->y + r->height > y)
2222 {
2223 gr = r; gy = r->y;
2224 break;
2225 }
2226
2227 if (gr && gy <= y)
2228 height = gr->height;
2229 else
2230 {
2231 /* Use nominal line height at end of window. */
2232 y -= gy;
2233 gy += (y / height) * height;
2234 }
2235 break;
2236
2237 default:
2238 ;
2239 virtual_glyph:
2240 /* If there is no glyph under the mouse, then we divide the screen
2241 into a grid of the smallest glyph in the frame, and use that
2242 as our "glyph". */
2243
2244 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2245 round down even for negative values. */
2246 if (gx < 0)
2247 gx -= width - 1;
2248 if (gy < 0)
2249 gy -= height - 1;
2250
2251 gx = (gx / width) * width;
2252 gy = (gy / height) * height;
2253
2254 goto store_rect;
2255 }
2256
2257 gx += WINDOW_LEFT_EDGE_X (w);
2258 gy += WINDOW_TOP_EDGE_Y (w);
2259
2260 store_rect:
2261 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2262
2263 /* Visible feedback for debugging. */
2264 #if 0
2265 #if HAVE_X_WINDOWS
2266 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2267 f->output_data.x->normal_gc,
2268 gx, gy, width, height);
2269 #endif
2270 #endif
2271 }
2272
2273
2274 #endif /* HAVE_WINDOW_SYSTEM */
2275
2276 \f
2277 /***********************************************************************
2278 Lisp form evaluation
2279 ***********************************************************************/
2280
2281 /* Error handler for safe_eval and safe_call. */
2282
2283 static Lisp_Object
2284 safe_eval_handler (arg)
2285 Lisp_Object arg;
2286 {
2287 add_to_log ("Error during redisplay: %s", arg, Qnil);
2288 return Qnil;
2289 }
2290
2291
2292 /* Evaluate SEXPR and return the result, or nil if something went
2293 wrong. Prevent redisplay during the evaluation. */
2294
2295 Lisp_Object
2296 safe_eval (sexpr)
2297 Lisp_Object sexpr;
2298 {
2299 Lisp_Object val;
2300
2301 if (inhibit_eval_during_redisplay)
2302 val = Qnil;
2303 else
2304 {
2305 int count = SPECPDL_INDEX ();
2306 struct gcpro gcpro1;
2307
2308 GCPRO1 (sexpr);
2309 specbind (Qinhibit_redisplay, Qt);
2310 /* Use Qt to ensure debugger does not run,
2311 so there is no possibility of wanting to redisplay. */
2312 val = internal_condition_case_1 (Feval, sexpr, Qt,
2313 safe_eval_handler);
2314 UNGCPRO;
2315 val = unbind_to (count, val);
2316 }
2317
2318 return val;
2319 }
2320
2321
2322 /* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
2323 Return the result, or nil if something went wrong. Prevent
2324 redisplay during the evaluation. */
2325
2326 Lisp_Object
2327 safe_call (nargs, args)
2328 int nargs;
2329 Lisp_Object *args;
2330 {
2331 Lisp_Object val;
2332
2333 if (inhibit_eval_during_redisplay)
2334 val = Qnil;
2335 else
2336 {
2337 int count = SPECPDL_INDEX ();
2338 struct gcpro gcpro1;
2339
2340 GCPRO1 (args[0]);
2341 gcpro1.nvars = nargs;
2342 specbind (Qinhibit_redisplay, Qt);
2343 /* Use Qt to ensure debugger does not run,
2344 so there is no possibility of wanting to redisplay. */
2345 val = internal_condition_case_2 (Ffuncall, nargs, args, Qt,
2346 safe_eval_handler);
2347 UNGCPRO;
2348 val = unbind_to (count, val);
2349 }
2350
2351 return val;
2352 }
2353
2354
2355 /* Call function FN with one argument ARG.
2356 Return the result, or nil if something went wrong. */
2357
2358 Lisp_Object
2359 safe_call1 (fn, arg)
2360 Lisp_Object fn, arg;
2361 {
2362 Lisp_Object args[2];
2363 args[0] = fn;
2364 args[1] = arg;
2365 return safe_call (2, args);
2366 }
2367
2368
2369 \f
2370 /***********************************************************************
2371 Debugging
2372 ***********************************************************************/
2373
2374 #if 0
2375
2376 /* Define CHECK_IT to perform sanity checks on iterators.
2377 This is for debugging. It is too slow to do unconditionally. */
2378
2379 static void
2380 check_it (it)
2381 struct it *it;
2382 {
2383 if (it->method == GET_FROM_STRING)
2384 {
2385 xassert (STRINGP (it->string));
2386 xassert (IT_STRING_CHARPOS (*it) >= 0);
2387 }
2388 else
2389 {
2390 xassert (IT_STRING_CHARPOS (*it) < 0);
2391 if (it->method == GET_FROM_BUFFER)
2392 {
2393 /* Check that character and byte positions agree. */
2394 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2395 }
2396 }
2397
2398 if (it->dpvec)
2399 xassert (it->current.dpvec_index >= 0);
2400 else
2401 xassert (it->current.dpvec_index < 0);
2402 }
2403
2404 #define CHECK_IT(IT) check_it ((IT))
2405
2406 #else /* not 0 */
2407
2408 #define CHECK_IT(IT) (void) 0
2409
2410 #endif /* not 0 */
2411
2412
2413 #if GLYPH_DEBUG
2414
2415 /* Check that the window end of window W is what we expect it
2416 to be---the last row in the current matrix displaying text. */
2417
2418 static void
2419 check_window_end (w)
2420 struct window *w;
2421 {
2422 if (!MINI_WINDOW_P (w)
2423 && !NILP (w->window_end_valid))
2424 {
2425 struct glyph_row *row;
2426 xassert ((row = MATRIX_ROW (w->current_matrix,
2427 XFASTINT (w->window_end_vpos)),
2428 !row->enabled_p
2429 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2430 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2431 }
2432 }
2433
2434 #define CHECK_WINDOW_END(W) check_window_end ((W))
2435
2436 #else /* not GLYPH_DEBUG */
2437
2438 #define CHECK_WINDOW_END(W) (void) 0
2439
2440 #endif /* not GLYPH_DEBUG */
2441
2442
2443 \f
2444 /***********************************************************************
2445 Iterator initialization
2446 ***********************************************************************/
2447
2448 /* Initialize IT for displaying current_buffer in window W, starting
2449 at character position CHARPOS. CHARPOS < 0 means that no buffer
2450 position is specified which is useful when the iterator is assigned
2451 a position later. BYTEPOS is the byte position corresponding to
2452 CHARPOS. BYTEPOS < 0 means compute it from CHARPOS.
2453
2454 If ROW is not null, calls to produce_glyphs with IT as parameter
2455 will produce glyphs in that row.
2456
2457 BASE_FACE_ID is the id of a base face to use. It must be one of
2458 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2459 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2460 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2461
2462 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2463 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2464 will be initialized to use the corresponding mode line glyph row of
2465 the desired matrix of W. */
2466
2467 void
2468 init_iterator (it, w, charpos, bytepos, row, base_face_id)
2469 struct it *it;
2470 struct window *w;
2471 int charpos, bytepos;
2472 struct glyph_row *row;
2473 enum face_id base_face_id;
2474 {
2475 int highlight_region_p;
2476
2477 /* Some precondition checks. */
2478 xassert (w != NULL && it != NULL);
2479 xassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2480 && charpos <= ZV));
2481
2482 /* If face attributes have been changed since the last redisplay,
2483 free realized faces now because they depend on face definitions
2484 that might have changed. Don't free faces while there might be
2485 desired matrices pending which reference these faces. */
2486 if (face_change_count && !inhibit_free_realized_faces)
2487 {
2488 face_change_count = 0;
2489 free_all_realized_faces (Qnil);
2490 }
2491
2492 /* Use one of the mode line rows of W's desired matrix if
2493 appropriate. */
2494 if (row == NULL)
2495 {
2496 if (base_face_id == MODE_LINE_FACE_ID
2497 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2498 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2499 else if (base_face_id == HEADER_LINE_FACE_ID)
2500 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2501 }
2502
2503 /* Clear IT. */
2504 bzero (it, sizeof *it);
2505 it->current.overlay_string_index = -1;
2506 it->current.dpvec_index = -1;
2507 it->base_face_id = base_face_id;
2508 it->string = Qnil;
2509 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2510
2511 /* The window in which we iterate over current_buffer: */
2512 XSETWINDOW (it->window, w);
2513 it->w = w;
2514 it->f = XFRAME (w->frame);
2515
2516 /* Extra space between lines (on window systems only). */
2517 if (base_face_id == DEFAULT_FACE_ID
2518 && FRAME_WINDOW_P (it->f))
2519 {
2520 if (NATNUMP (current_buffer->extra_line_spacing))
2521 it->extra_line_spacing = XFASTINT (current_buffer->extra_line_spacing);
2522 else if (FLOATP (current_buffer->extra_line_spacing))
2523 it->extra_line_spacing = (XFLOAT_DATA (current_buffer->extra_line_spacing)
2524 * FRAME_LINE_HEIGHT (it->f));
2525 else if (it->f->extra_line_spacing > 0)
2526 it->extra_line_spacing = it->f->extra_line_spacing;
2527 it->max_extra_line_spacing = 0;
2528 }
2529
2530 /* If realized faces have been removed, e.g. because of face
2531 attribute changes of named faces, recompute them. When running
2532 in batch mode, the face cache of the initial frame is null. If
2533 we happen to get called, make a dummy face cache. */
2534 if (FRAME_FACE_CACHE (it->f) == NULL)
2535 init_frame_faces (it->f);
2536 if (FRAME_FACE_CACHE (it->f)->used == 0)
2537 recompute_basic_faces (it->f);
2538
2539 /* Current value of the `slice', `space-width', and 'height' properties. */
2540 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2541 it->space_width = Qnil;
2542 it->font_height = Qnil;
2543 it->override_ascent = -1;
2544
2545 /* Are control characters displayed as `^C'? */
2546 it->ctl_arrow_p = !NILP (current_buffer->ctl_arrow);
2547
2548 /* -1 means everything between a CR and the following line end
2549 is invisible. >0 means lines indented more than this value are
2550 invisible. */
2551 it->selective = (INTEGERP (current_buffer->selective_display)
2552 ? XFASTINT (current_buffer->selective_display)
2553 : (!NILP (current_buffer->selective_display)
2554 ? -1 : 0));
2555 it->selective_display_ellipsis_p
2556 = !NILP (current_buffer->selective_display_ellipses);
2557
2558 /* Display table to use. */
2559 it->dp = window_display_table (w);
2560
2561 /* Are multibyte characters enabled in current_buffer? */
2562 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
2563
2564 /* Non-zero if we should highlight the region. */
2565 highlight_region_p
2566 = (!NILP (Vtransient_mark_mode)
2567 && !NILP (current_buffer->mark_active)
2568 && XMARKER (current_buffer->mark)->buffer != 0);
2569
2570 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
2571 start and end of a visible region in window IT->w. Set both to
2572 -1 to indicate no region. */
2573 if (highlight_region_p
2574 /* Maybe highlight only in selected window. */
2575 && (/* Either show region everywhere. */
2576 highlight_nonselected_windows
2577 /* Or show region in the selected window. */
2578 || w == XWINDOW (selected_window)
2579 /* Or show the region if we are in the mini-buffer and W is
2580 the window the mini-buffer refers to. */
2581 || (MINI_WINDOW_P (XWINDOW (selected_window))
2582 && WINDOWP (minibuf_selected_window)
2583 && w == XWINDOW (minibuf_selected_window))))
2584 {
2585 int charpos = marker_position (current_buffer->mark);
2586 it->region_beg_charpos = min (PT, charpos);
2587 it->region_end_charpos = max (PT, charpos);
2588 }
2589 else
2590 it->region_beg_charpos = it->region_end_charpos = -1;
2591
2592 /* Get the position at which the redisplay_end_trigger hook should
2593 be run, if it is to be run at all. */
2594 if (MARKERP (w->redisplay_end_trigger)
2595 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2596 it->redisplay_end_trigger_charpos
2597 = marker_position (w->redisplay_end_trigger);
2598 else if (INTEGERP (w->redisplay_end_trigger))
2599 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
2600
2601 /* Correct bogus values of tab_width. */
2602 it->tab_width = XINT (current_buffer->tab_width);
2603 if (it->tab_width <= 0 || it->tab_width > 1000)
2604 it->tab_width = 8;
2605
2606 /* Are lines in the display truncated? */
2607 it->truncate_lines_p
2608 = (base_face_id != DEFAULT_FACE_ID
2609 || XINT (it->w->hscroll)
2610 || (truncate_partial_width_windows
2611 && !WINDOW_FULL_WIDTH_P (it->w))
2612 || !NILP (current_buffer->truncate_lines));
2613
2614 /* Get dimensions of truncation and continuation glyphs. These are
2615 displayed as fringe bitmaps under X, so we don't need them for such
2616 frames. */
2617 if (!FRAME_WINDOW_P (it->f))
2618 {
2619 if (it->truncate_lines_p)
2620 {
2621 /* We will need the truncation glyph. */
2622 xassert (it->glyph_row == NULL);
2623 produce_special_glyphs (it, IT_TRUNCATION);
2624 it->truncation_pixel_width = it->pixel_width;
2625 }
2626 else
2627 {
2628 /* We will need the continuation glyph. */
2629 xassert (it->glyph_row == NULL);
2630 produce_special_glyphs (it, IT_CONTINUATION);
2631 it->continuation_pixel_width = it->pixel_width;
2632 }
2633
2634 /* Reset these values to zero because the produce_special_glyphs
2635 above has changed them. */
2636 it->pixel_width = it->ascent = it->descent = 0;
2637 it->phys_ascent = it->phys_descent = 0;
2638 }
2639
2640 /* Set this after getting the dimensions of truncation and
2641 continuation glyphs, so that we don't produce glyphs when calling
2642 produce_special_glyphs, above. */
2643 it->glyph_row = row;
2644 it->area = TEXT_AREA;
2645
2646 /* Get the dimensions of the display area. The display area
2647 consists of the visible window area plus a horizontally scrolled
2648 part to the left of the window. All x-values are relative to the
2649 start of this total display area. */
2650 if (base_face_id != DEFAULT_FACE_ID)
2651 {
2652 /* Mode lines, menu bar in terminal frames. */
2653 it->first_visible_x = 0;
2654 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
2655 }
2656 else
2657 {
2658 it->first_visible_x
2659 = XFASTINT (it->w->hscroll) * FRAME_COLUMN_WIDTH (it->f);
2660 it->last_visible_x = (it->first_visible_x
2661 + window_box_width (w, TEXT_AREA));
2662
2663 /* If we truncate lines, leave room for the truncator glyph(s) at
2664 the right margin. Otherwise, leave room for the continuation
2665 glyph(s). Truncation and continuation glyphs are not inserted
2666 for window-based redisplay. */
2667 if (!FRAME_WINDOW_P (it->f))
2668 {
2669 if (it->truncate_lines_p)
2670 it->last_visible_x -= it->truncation_pixel_width;
2671 else
2672 it->last_visible_x -= it->continuation_pixel_width;
2673 }
2674
2675 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2676 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2677 }
2678
2679 /* Leave room for a border glyph. */
2680 if (!FRAME_WINDOW_P (it->f)
2681 && !WINDOW_RIGHTMOST_P (it->w))
2682 it->last_visible_x -= 1;
2683
2684 it->last_visible_y = window_text_bottom_y (w);
2685
2686 /* For mode lines and alike, arrange for the first glyph having a
2687 left box line if the face specifies a box. */
2688 if (base_face_id != DEFAULT_FACE_ID)
2689 {
2690 struct face *face;
2691
2692 it->face_id = base_face_id;
2693
2694 /* If we have a boxed mode line, make the first character appear
2695 with a left box line. */
2696 face = FACE_FROM_ID (it->f, base_face_id);
2697 if (face->box != FACE_NO_BOX)
2698 it->start_of_box_run_p = 1;
2699 }
2700
2701 /* If a buffer position was specified, set the iterator there,
2702 getting overlays and face properties from that position. */
2703 if (charpos >= BUF_BEG (current_buffer))
2704 {
2705 it->end_charpos = ZV;
2706 it->face_id = -1;
2707 IT_CHARPOS (*it) = charpos;
2708
2709 /* Compute byte position if not specified. */
2710 if (bytepos < charpos)
2711 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
2712 else
2713 IT_BYTEPOS (*it) = bytepos;
2714
2715 it->start = it->current;
2716
2717 /* Compute faces etc. */
2718 reseat (it, it->current.pos, 1);
2719 }
2720
2721 CHECK_IT (it);
2722 }
2723
2724
2725 /* Initialize IT for the display of window W with window start POS. */
2726
2727 void
2728 start_display (it, w, pos)
2729 struct it *it;
2730 struct window *w;
2731 struct text_pos pos;
2732 {
2733 struct glyph_row *row;
2734 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2735
2736 row = w->desired_matrix->rows + first_vpos;
2737 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2738 it->first_vpos = first_vpos;
2739
2740 /* Don't reseat to previous visible line start if current start
2741 position is in a string or image. */
2742 if (it->method == GET_FROM_BUFFER && !it->truncate_lines_p)
2743 {
2744 int start_at_line_beg_p;
2745 int first_y = it->current_y;
2746
2747 /* If window start is not at a line start, skip forward to POS to
2748 get the correct continuation lines width. */
2749 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2750 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2751 if (!start_at_line_beg_p)
2752 {
2753 int new_x;
2754
2755 reseat_at_previous_visible_line_start (it);
2756 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
2757
2758 new_x = it->current_x + it->pixel_width;
2759
2760 /* If lines are continued, this line may end in the middle
2761 of a multi-glyph character (e.g. a control character
2762 displayed as \003, or in the middle of an overlay
2763 string). In this case move_it_to above will not have
2764 taken us to the start of the continuation line but to the
2765 end of the continued line. */
2766 if (it->current_x > 0
2767 && !it->truncate_lines_p /* Lines are continued. */
2768 && (/* And glyph doesn't fit on the line. */
2769 new_x > it->last_visible_x
2770 /* Or it fits exactly and we're on a window
2771 system frame. */
2772 || (new_x == it->last_visible_x
2773 && FRAME_WINDOW_P (it->f))))
2774 {
2775 if (it->current.dpvec_index >= 0
2776 || it->current.overlay_string_index >= 0)
2777 {
2778 set_iterator_to_next (it, 1);
2779 move_it_in_display_line_to (it, -1, -1, 0);
2780 }
2781
2782 it->continuation_lines_width += it->current_x;
2783 }
2784
2785 /* We're starting a new display line, not affected by the
2786 height of the continued line, so clear the appropriate
2787 fields in the iterator structure. */
2788 it->max_ascent = it->max_descent = 0;
2789 it->max_phys_ascent = it->max_phys_descent = 0;
2790
2791 it->current_y = first_y;
2792 it->vpos = 0;
2793 it->current_x = it->hpos = 0;
2794 }
2795 }
2796
2797 #if 0 /* Don't assert the following because start_display is sometimes
2798 called intentionally with a window start that is not at a
2799 line start. Please leave this code in as a comment. */
2800
2801 /* Window start should be on a line start, now. */
2802 xassert (it->continuation_lines_width
2803 || IT_CHARPOS (it) == BEGV
2804 || FETCH_BYTE (IT_BYTEPOS (it) - 1) == '\n');
2805 #endif /* 0 */
2806 }
2807
2808
2809 /* Return 1 if POS is a position in ellipses displayed for invisible
2810 text. W is the window we display, for text property lookup. */
2811
2812 static int
2813 in_ellipses_for_invisible_text_p (pos, w)
2814 struct display_pos *pos;
2815 struct window *w;
2816 {
2817 Lisp_Object prop, window;
2818 int ellipses_p = 0;
2819 int charpos = CHARPOS (pos->pos);
2820
2821 /* If POS specifies a position in a display vector, this might
2822 be for an ellipsis displayed for invisible text. We won't
2823 get the iterator set up for delivering that ellipsis unless
2824 we make sure that it gets aware of the invisible text. */
2825 if (pos->dpvec_index >= 0
2826 && pos->overlay_string_index < 0
2827 && CHARPOS (pos->string_pos) < 0
2828 && charpos > BEGV
2829 && (XSETWINDOW (window, w),
2830 prop = Fget_char_property (make_number (charpos),
2831 Qinvisible, window),
2832 !TEXT_PROP_MEANS_INVISIBLE (prop)))
2833 {
2834 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
2835 window);
2836 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
2837 }
2838
2839 return ellipses_p;
2840 }
2841
2842
2843 /* Initialize IT for stepping through current_buffer in window W,
2844 starting at position POS that includes overlay string and display
2845 vector/ control character translation position information. Value
2846 is zero if there are overlay strings with newlines at POS. */
2847
2848 static int
2849 init_from_display_pos (it, w, pos)
2850 struct it *it;
2851 struct window *w;
2852 struct display_pos *pos;
2853 {
2854 int charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
2855 int i, overlay_strings_with_newlines = 0;
2856
2857 /* If POS specifies a position in a display vector, this might
2858 be for an ellipsis displayed for invisible text. We won't
2859 get the iterator set up for delivering that ellipsis unless
2860 we make sure that it gets aware of the invisible text. */
2861 if (in_ellipses_for_invisible_text_p (pos, w))
2862 {
2863 --charpos;
2864 bytepos = 0;
2865 }
2866
2867 /* Keep in mind: the call to reseat in init_iterator skips invisible
2868 text, so we might end up at a position different from POS. This
2869 is only a problem when POS is a row start after a newline and an
2870 overlay starts there with an after-string, and the overlay has an
2871 invisible property. Since we don't skip invisible text in
2872 display_line and elsewhere immediately after consuming the
2873 newline before the row start, such a POS will not be in a string,
2874 but the call to init_iterator below will move us to the
2875 after-string. */
2876 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
2877
2878 /* This only scans the current chunk -- it should scan all chunks.
2879 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
2880 to 16 in 22.1 to make this a lesser problem. */
2881 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
2882 {
2883 const char *s = SDATA (it->overlay_strings[i]);
2884 const char *e = s + SBYTES (it->overlay_strings[i]);
2885
2886 while (s < e && *s != '\n')
2887 ++s;
2888
2889 if (s < e)
2890 {
2891 overlay_strings_with_newlines = 1;
2892 break;
2893 }
2894 }
2895
2896 /* If position is within an overlay string, set up IT to the right
2897 overlay string. */
2898 if (pos->overlay_string_index >= 0)
2899 {
2900 int relative_index;
2901
2902 /* If the first overlay string happens to have a `display'
2903 property for an image, the iterator will be set up for that
2904 image, and we have to undo that setup first before we can
2905 correct the overlay string index. */
2906 if (it->method == GET_FROM_IMAGE)
2907 pop_it (it);
2908
2909 /* We already have the first chunk of overlay strings in
2910 IT->overlay_strings. Load more until the one for
2911 pos->overlay_string_index is in IT->overlay_strings. */
2912 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
2913 {
2914 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
2915 it->current.overlay_string_index = 0;
2916 while (n--)
2917 {
2918 load_overlay_strings (it, 0);
2919 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
2920 }
2921 }
2922
2923 it->current.overlay_string_index = pos->overlay_string_index;
2924 relative_index = (it->current.overlay_string_index
2925 % OVERLAY_STRING_CHUNK_SIZE);
2926 it->string = it->overlay_strings[relative_index];
2927 xassert (STRINGP (it->string));
2928 it->current.string_pos = pos->string_pos;
2929 it->method = GET_FROM_STRING;
2930 }
2931
2932 #if 0 /* This is bogus because POS not having an overlay string
2933 position does not mean it's after the string. Example: A
2934 line starting with a before-string and initialization of IT
2935 to the previous row's end position. */
2936 else if (it->current.overlay_string_index >= 0)
2937 {
2938 /* If POS says we're already after an overlay string ending at
2939 POS, make sure to pop the iterator because it will be in
2940 front of that overlay string. When POS is ZV, we've thereby
2941 also ``processed'' overlay strings at ZV. */
2942 while (it->sp)
2943 pop_it (it);
2944 xassert (it->current.overlay_string_index == -1);
2945 xassert (it->method == GET_FROM_BUFFER);
2946 if (CHARPOS (pos->pos) == ZV)
2947 it->overlay_strings_at_end_processed_p = 1;
2948 }
2949 #endif /* 0 */
2950
2951 if (CHARPOS (pos->string_pos) >= 0)
2952 {
2953 /* Recorded position is not in an overlay string, but in another
2954 string. This can only be a string from a `display' property.
2955 IT should already be filled with that string. */
2956 it->current.string_pos = pos->string_pos;
2957 xassert (STRINGP (it->string));
2958 }
2959
2960 /* Restore position in display vector translations, control
2961 character translations or ellipses. */
2962 if (pos->dpvec_index >= 0)
2963 {
2964 if (it->dpvec == NULL)
2965 get_next_display_element (it);
2966 xassert (it->dpvec && it->current.dpvec_index == 0);
2967 it->current.dpvec_index = pos->dpvec_index;
2968 }
2969
2970 CHECK_IT (it);
2971 return !overlay_strings_with_newlines;
2972 }
2973
2974
2975 /* Initialize IT for stepping through current_buffer in window W
2976 starting at ROW->start. */
2977
2978 static void
2979 init_to_row_start (it, w, row)
2980 struct it *it;
2981 struct window *w;
2982 struct glyph_row *row;
2983 {
2984 init_from_display_pos (it, w, &row->start);
2985 it->start = row->start;
2986 it->continuation_lines_width = row->continuation_lines_width;
2987 CHECK_IT (it);
2988 }
2989
2990
2991 /* Initialize IT for stepping through current_buffer in window W
2992 starting in the line following ROW, i.e. starting at ROW->end.
2993 Value is zero if there are overlay strings with newlines at ROW's
2994 end position. */
2995
2996 static int
2997 init_to_row_end (it, w, row)
2998 struct it *it;
2999 struct window *w;
3000 struct glyph_row *row;
3001 {
3002 int success = 0;
3003
3004 if (init_from_display_pos (it, w, &row->end))
3005 {
3006 if (row->continued_p)
3007 it->continuation_lines_width
3008 = row->continuation_lines_width + row->pixel_width;
3009 CHECK_IT (it);
3010 success = 1;
3011 }
3012
3013 return success;
3014 }
3015
3016
3017
3018 \f
3019 /***********************************************************************
3020 Text properties
3021 ***********************************************************************/
3022
3023 /* Called when IT reaches IT->stop_charpos. Handle text property and
3024 overlay changes. Set IT->stop_charpos to the next position where
3025 to stop. */
3026
3027 static void
3028 handle_stop (it)
3029 struct it *it;
3030 {
3031 enum prop_handled handled;
3032 int handle_overlay_change_p;
3033 struct props *p;
3034
3035 it->dpvec = NULL;
3036 it->current.dpvec_index = -1;
3037 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
3038 it->ignore_overlay_strings_at_pos_p = 0;
3039
3040 /* Use face of preceding text for ellipsis (if invisible) */
3041 if (it->selective_display_ellipsis_p)
3042 it->saved_face_id = it->face_id;
3043
3044 do
3045 {
3046 handled = HANDLED_NORMALLY;
3047
3048 /* Call text property handlers. */
3049 for (p = it_props; p->handler; ++p)
3050 {
3051 handled = p->handler (it);
3052
3053 if (handled == HANDLED_RECOMPUTE_PROPS)
3054 break;
3055 else if (handled == HANDLED_RETURN)
3056 {
3057 /* We still want to show before and after strings from
3058 overlays even if the actual buffer text is replaced. */
3059 if (!handle_overlay_change_p || it->sp > 1)
3060 return;
3061 if (!get_overlay_strings_1 (it, 0, 0))
3062 return;
3063 it->ignore_overlay_strings_at_pos_p = 1;
3064 it->string_from_display_prop_p = 0;
3065 handle_overlay_change_p = 0;
3066 handled = HANDLED_RECOMPUTE_PROPS;
3067 break;
3068 }
3069 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
3070 handle_overlay_change_p = 0;
3071 }
3072
3073 if (handled != HANDLED_RECOMPUTE_PROPS)
3074 {
3075 /* Don't check for overlay strings below when set to deliver
3076 characters from a display vector. */
3077 if (it->method == GET_FROM_DISPLAY_VECTOR)
3078 handle_overlay_change_p = 0;
3079
3080 /* Handle overlay changes. */
3081 if (handle_overlay_change_p)
3082 handled = handle_overlay_change (it);
3083
3084 /* Determine where to stop next. */
3085 if (handled == HANDLED_NORMALLY)
3086 compute_stop_pos (it);
3087 }
3088 }
3089 while (handled == HANDLED_RECOMPUTE_PROPS);
3090 }
3091
3092
3093 /* Compute IT->stop_charpos from text property and overlay change
3094 information for IT's current position. */
3095
3096 static void
3097 compute_stop_pos (it)
3098 struct it *it;
3099 {
3100 register INTERVAL iv, next_iv;
3101 Lisp_Object object, limit, position;
3102
3103 /* If nowhere else, stop at the end. */
3104 it->stop_charpos = it->end_charpos;
3105
3106 if (STRINGP (it->string))
3107 {
3108 /* Strings are usually short, so don't limit the search for
3109 properties. */
3110 object = it->string;
3111 limit = Qnil;
3112 position = make_number (IT_STRING_CHARPOS (*it));
3113 }
3114 else
3115 {
3116 int charpos;
3117
3118 /* If next overlay change is in front of the current stop pos
3119 (which is IT->end_charpos), stop there. Note: value of
3120 next_overlay_change is point-max if no overlay change
3121 follows. */
3122 charpos = next_overlay_change (IT_CHARPOS (*it));
3123 if (charpos < it->stop_charpos)
3124 it->stop_charpos = charpos;
3125
3126 /* If showing the region, we have to stop at the region
3127 start or end because the face might change there. */
3128 if (it->region_beg_charpos > 0)
3129 {
3130 if (IT_CHARPOS (*it) < it->region_beg_charpos)
3131 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
3132 else if (IT_CHARPOS (*it) < it->region_end_charpos)
3133 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
3134 }
3135
3136 /* Set up variables for computing the stop position from text
3137 property changes. */
3138 XSETBUFFER (object, current_buffer);
3139 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3140 position = make_number (IT_CHARPOS (*it));
3141
3142 }
3143
3144 /* Get the interval containing IT's position. Value is a null
3145 interval if there isn't such an interval. */
3146 iv = validate_interval_range (object, &position, &position, 0);
3147 if (!NULL_INTERVAL_P (iv))
3148 {
3149 Lisp_Object values_here[LAST_PROP_IDX];
3150 struct props *p;
3151
3152 /* Get properties here. */
3153 for (p = it_props; p->handler; ++p)
3154 values_here[p->idx] = textget (iv->plist, *p->name);
3155
3156 /* Look for an interval following iv that has different
3157 properties. */
3158 for (next_iv = next_interval (iv);
3159 (!NULL_INTERVAL_P (next_iv)
3160 && (NILP (limit)
3161 || XFASTINT (limit) > next_iv->position));
3162 next_iv = next_interval (next_iv))
3163 {
3164 for (p = it_props; p->handler; ++p)
3165 {
3166 Lisp_Object new_value;
3167
3168 new_value = textget (next_iv->plist, *p->name);
3169 if (!EQ (values_here[p->idx], new_value))
3170 break;
3171 }
3172
3173 if (p->handler)
3174 break;
3175 }
3176
3177 if (!NULL_INTERVAL_P (next_iv))
3178 {
3179 if (INTEGERP (limit)
3180 && next_iv->position >= XFASTINT (limit))
3181 /* No text property change up to limit. */
3182 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3183 else
3184 /* Text properties change in next_iv. */
3185 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3186 }
3187 }
3188
3189 xassert (STRINGP (it->string)
3190 || (it->stop_charpos >= BEGV
3191 && it->stop_charpos >= IT_CHARPOS (*it)));
3192 }
3193
3194
3195 /* Return the position of the next overlay change after POS in
3196 current_buffer. Value is point-max if no overlay change
3197 follows. This is like `next-overlay-change' but doesn't use
3198 xmalloc. */
3199
3200 static int
3201 next_overlay_change (pos)
3202 int pos;
3203 {
3204 int noverlays;
3205 int endpos;
3206 Lisp_Object *overlays;
3207 int i;
3208
3209 /* Get all overlays at the given position. */
3210 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
3211
3212 /* If any of these overlays ends before endpos,
3213 use its ending point instead. */
3214 for (i = 0; i < noverlays; ++i)
3215 {
3216 Lisp_Object oend;
3217 int oendpos;
3218
3219 oend = OVERLAY_END (overlays[i]);
3220 oendpos = OVERLAY_POSITION (oend);
3221 endpos = min (endpos, oendpos);
3222 }
3223
3224 return endpos;
3225 }
3226
3227
3228 \f
3229 /***********************************************************************
3230 Fontification
3231 ***********************************************************************/
3232
3233 /* Handle changes in the `fontified' property of the current buffer by
3234 calling hook functions from Qfontification_functions to fontify
3235 regions of text. */
3236
3237 static enum prop_handled
3238 handle_fontified_prop (it)
3239 struct it *it;
3240 {
3241 Lisp_Object prop, pos;
3242 enum prop_handled handled = HANDLED_NORMALLY;
3243
3244 if (!NILP (Vmemory_full))
3245 return handled;
3246
3247 /* Get the value of the `fontified' property at IT's current buffer
3248 position. (The `fontified' property doesn't have a special
3249 meaning in strings.) If the value is nil, call functions from
3250 Qfontification_functions. */
3251 if (!STRINGP (it->string)
3252 && it->s == NULL
3253 && !NILP (Vfontification_functions)
3254 && !NILP (Vrun_hooks)
3255 && (pos = make_number (IT_CHARPOS (*it)),
3256 prop = Fget_char_property (pos, Qfontified, Qnil),
3257 /* Ignore the special cased nil value always present at EOB since
3258 no amount of fontifying will be able to change it. */
3259 NILP (prop) && IT_CHARPOS (*it) < Z))
3260 {
3261 int count = SPECPDL_INDEX ();
3262 Lisp_Object val;
3263
3264 val = Vfontification_functions;
3265 specbind (Qfontification_functions, Qnil);
3266
3267 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3268 safe_call1 (val, pos);
3269 else
3270 {
3271 Lisp_Object globals, fn;
3272 struct gcpro gcpro1, gcpro2;
3273
3274 globals = Qnil;
3275 GCPRO2 (val, globals);
3276
3277 for (; CONSP (val); val = XCDR (val))
3278 {
3279 fn = XCAR (val);
3280
3281 if (EQ (fn, Qt))
3282 {
3283 /* A value of t indicates this hook has a local
3284 binding; it means to run the global binding too.
3285 In a global value, t should not occur. If it
3286 does, we must ignore it to avoid an endless
3287 loop. */
3288 for (globals = Fdefault_value (Qfontification_functions);
3289 CONSP (globals);
3290 globals = XCDR (globals))
3291 {
3292 fn = XCAR (globals);
3293 if (!EQ (fn, Qt))
3294 safe_call1 (fn, pos);
3295 }
3296 }
3297 else
3298 safe_call1 (fn, pos);
3299 }
3300
3301 UNGCPRO;
3302 }
3303
3304 unbind_to (count, Qnil);
3305
3306 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3307 something. This avoids an endless loop if they failed to
3308 fontify the text for which reason ever. */
3309 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3310 handled = HANDLED_RECOMPUTE_PROPS;
3311 }
3312
3313 return handled;
3314 }
3315
3316
3317 \f
3318 /***********************************************************************
3319 Faces
3320 ***********************************************************************/
3321
3322 /* Set up iterator IT from face properties at its current position.
3323 Called from handle_stop. */
3324
3325 static enum prop_handled
3326 handle_face_prop (it)
3327 struct it *it;
3328 {
3329 int new_face_id, next_stop;
3330
3331 if (!STRINGP (it->string))
3332 {
3333 new_face_id
3334 = face_at_buffer_position (it->w,
3335 IT_CHARPOS (*it),
3336 it->region_beg_charpos,
3337 it->region_end_charpos,
3338 &next_stop,
3339 (IT_CHARPOS (*it)
3340 + TEXT_PROP_DISTANCE_LIMIT),
3341 0);
3342
3343 /* Is this a start of a run of characters with box face?
3344 Caveat: this can be called for a freshly initialized
3345 iterator; face_id is -1 in this case. We know that the new
3346 face will not change until limit, i.e. if the new face has a
3347 box, all characters up to limit will have one. But, as
3348 usual, we don't know whether limit is really the end. */
3349 if (new_face_id != it->face_id)
3350 {
3351 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3352
3353 /* If new face has a box but old face has not, this is
3354 the start of a run of characters with box, i.e. it has
3355 a shadow on the left side. The value of face_id of the
3356 iterator will be -1 if this is the initial call that gets
3357 the face. In this case, we have to look in front of IT's
3358 position and see whether there is a face != new_face_id. */
3359 it->start_of_box_run_p
3360 = (new_face->box != FACE_NO_BOX
3361 && (it->face_id >= 0
3362 || IT_CHARPOS (*it) == BEG
3363 || new_face_id != face_before_it_pos (it)));
3364 it->face_box_p = new_face->box != FACE_NO_BOX;
3365 }
3366 }
3367 else
3368 {
3369 int base_face_id, bufpos;
3370
3371 if (it->current.overlay_string_index >= 0)
3372 bufpos = IT_CHARPOS (*it);
3373 else
3374 bufpos = 0;
3375
3376 /* For strings from a buffer, i.e. overlay strings or strings
3377 from a `display' property, use the face at IT's current
3378 buffer position as the base face to merge with, so that
3379 overlay strings appear in the same face as surrounding
3380 text, unless they specify their own faces. */
3381 base_face_id = underlying_face_id (it);
3382
3383 new_face_id = face_at_string_position (it->w,
3384 it->string,
3385 IT_STRING_CHARPOS (*it),
3386 bufpos,
3387 it->region_beg_charpos,
3388 it->region_end_charpos,
3389 &next_stop,
3390 base_face_id, 0);
3391
3392 #if 0 /* This shouldn't be neccessary. Let's check it. */
3393 /* If IT is used to display a mode line we would really like to
3394 use the mode line face instead of the frame's default face. */
3395 if (it->glyph_row == MATRIX_MODE_LINE_ROW (it->w->desired_matrix)
3396 && new_face_id == DEFAULT_FACE_ID)
3397 new_face_id = CURRENT_MODE_LINE_FACE_ID (it->w);
3398 #endif
3399
3400 /* Is this a start of a run of characters with box? Caveat:
3401 this can be called for a freshly allocated iterator; face_id
3402 is -1 is this case. We know that the new face will not
3403 change until the next check pos, i.e. if the new face has a
3404 box, all characters up to that position will have a
3405 box. But, as usual, we don't know whether that position
3406 is really the end. */
3407 if (new_face_id != it->face_id)
3408 {
3409 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3410 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3411
3412 /* If new face has a box but old face hasn't, this is the
3413 start of a run of characters with box, i.e. it has a
3414 shadow on the left side. */
3415 it->start_of_box_run_p
3416 = new_face->box && (old_face == NULL || !old_face->box);
3417 it->face_box_p = new_face->box != FACE_NO_BOX;
3418 }
3419 }
3420
3421 it->face_id = new_face_id;
3422 return HANDLED_NORMALLY;
3423 }
3424
3425
3426 /* Return the ID of the face ``underlying'' IT's current position,
3427 which is in a string. If the iterator is associated with a
3428 buffer, return the face at IT's current buffer position.
3429 Otherwise, use the iterator's base_face_id. */
3430
3431 static int
3432 underlying_face_id (it)
3433 struct it *it;
3434 {
3435 int face_id = it->base_face_id, i;
3436
3437 xassert (STRINGP (it->string));
3438
3439 for (i = it->sp - 1; i >= 0; --i)
3440 if (NILP (it->stack[i].string))
3441 face_id = it->stack[i].face_id;
3442
3443 return face_id;
3444 }
3445
3446
3447 /* Compute the face one character before or after the current position
3448 of IT. BEFORE_P non-zero means get the face in front of IT's
3449 position. Value is the id of the face. */
3450
3451 static int
3452 face_before_or_after_it_pos (it, before_p)
3453 struct it *it;
3454 int before_p;
3455 {
3456 int face_id, limit;
3457 int next_check_charpos;
3458 struct text_pos pos;
3459
3460 xassert (it->s == NULL);
3461
3462 if (STRINGP (it->string))
3463 {
3464 int bufpos, base_face_id;
3465
3466 /* No face change past the end of the string (for the case
3467 we are padding with spaces). No face change before the
3468 string start. */
3469 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
3470 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
3471 return it->face_id;
3472
3473 /* Set pos to the position before or after IT's current position. */
3474 if (before_p)
3475 pos = string_pos (IT_STRING_CHARPOS (*it) - 1, it->string);
3476 else
3477 /* For composition, we must check the character after the
3478 composition. */
3479 pos = (it->what == IT_COMPOSITION
3480 ? string_pos (IT_STRING_CHARPOS (*it) + it->cmp_len, it->string)
3481 : string_pos (IT_STRING_CHARPOS (*it) + 1, it->string));
3482
3483 if (it->current.overlay_string_index >= 0)
3484 bufpos = IT_CHARPOS (*it);
3485 else
3486 bufpos = 0;
3487
3488 base_face_id = underlying_face_id (it);
3489
3490 /* Get the face for ASCII, or unibyte. */
3491 face_id = face_at_string_position (it->w,
3492 it->string,
3493 CHARPOS (pos),
3494 bufpos,
3495 it->region_beg_charpos,
3496 it->region_end_charpos,
3497 &next_check_charpos,
3498 base_face_id, 0);
3499
3500 /* Correct the face for charsets different from ASCII. Do it
3501 for the multibyte case only. The face returned above is
3502 suitable for unibyte text if IT->string is unibyte. */
3503 if (STRING_MULTIBYTE (it->string))
3504 {
3505 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos);
3506 int rest = SBYTES (it->string) - BYTEPOS (pos);
3507 int c, len;
3508 struct face *face = FACE_FROM_ID (it->f, face_id);
3509
3510 c = string_char_and_length (p, rest, &len);
3511 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), it->string);
3512 }
3513 }
3514 else
3515 {
3516 if ((IT_CHARPOS (*it) >= ZV && !before_p)
3517 || (IT_CHARPOS (*it) <= BEGV && before_p))
3518 return it->face_id;
3519
3520 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
3521 pos = it->current.pos;
3522
3523 if (before_p)
3524 DEC_TEXT_POS (pos, it->multibyte_p);
3525 else
3526 {
3527 if (it->what == IT_COMPOSITION)
3528 /* For composition, we must check the position after the
3529 composition. */
3530 pos.charpos += it->cmp_len, pos.bytepos += it->len;
3531 else
3532 INC_TEXT_POS (pos, it->multibyte_p);
3533 }
3534
3535 /* Determine face for CHARSET_ASCII, or unibyte. */
3536 face_id = face_at_buffer_position (it->w,
3537 CHARPOS (pos),
3538 it->region_beg_charpos,
3539 it->region_end_charpos,
3540 &next_check_charpos,
3541 limit, 0);
3542
3543 /* Correct the face for charsets different from ASCII. Do it
3544 for the multibyte case only. The face returned above is
3545 suitable for unibyte text if current_buffer is unibyte. */
3546 if (it->multibyte_p)
3547 {
3548 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
3549 struct face *face = FACE_FROM_ID (it->f, face_id);
3550 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), Qnil);
3551 }
3552 }
3553
3554 return face_id;
3555 }
3556
3557
3558 \f
3559 /***********************************************************************
3560 Invisible text
3561 ***********************************************************************/
3562
3563 /* Set up iterator IT from invisible properties at its current
3564 position. Called from handle_stop. */
3565
3566 static enum prop_handled
3567 handle_invisible_prop (it)
3568 struct it *it;
3569 {
3570 enum prop_handled handled = HANDLED_NORMALLY;
3571
3572 if (STRINGP (it->string))
3573 {
3574 extern Lisp_Object Qinvisible;
3575 Lisp_Object prop, end_charpos, limit, charpos;
3576
3577 /* Get the value of the invisible text property at the
3578 current position. Value will be nil if there is no such
3579 property. */
3580 charpos = make_number (IT_STRING_CHARPOS (*it));
3581 prop = Fget_text_property (charpos, Qinvisible, it->string);
3582
3583 if (!NILP (prop)
3584 && IT_STRING_CHARPOS (*it) < it->end_charpos)
3585 {
3586 handled = HANDLED_RECOMPUTE_PROPS;
3587
3588 /* Get the position at which the next change of the
3589 invisible text property can be found in IT->string.
3590 Value will be nil if the property value is the same for
3591 all the rest of IT->string. */
3592 XSETINT (limit, SCHARS (it->string));
3593 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
3594 it->string, limit);
3595
3596 /* Text at current position is invisible. The next
3597 change in the property is at position end_charpos.
3598 Move IT's current position to that position. */
3599 if (INTEGERP (end_charpos)
3600 && XFASTINT (end_charpos) < XFASTINT (limit))
3601 {
3602 struct text_pos old;
3603 old = it->current.string_pos;
3604 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
3605 compute_string_pos (&it->current.string_pos, old, it->string);
3606 }
3607 else
3608 {
3609 /* The rest of the string is invisible. If this is an
3610 overlay string, proceed with the next overlay string
3611 or whatever comes and return a character from there. */
3612 if (it->current.overlay_string_index >= 0)
3613 {
3614 next_overlay_string (it);
3615 /* Don't check for overlay strings when we just
3616 finished processing them. */
3617 handled = HANDLED_OVERLAY_STRING_CONSUMED;
3618 }
3619 else
3620 {
3621 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
3622 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
3623 }
3624 }
3625 }
3626 }
3627 else
3628 {
3629 int invis_p;
3630 EMACS_INT newpos, next_stop, start_charpos;
3631 Lisp_Object pos, prop, overlay;
3632
3633 /* First of all, is there invisible text at this position? */
3634 start_charpos = IT_CHARPOS (*it);
3635 pos = make_number (IT_CHARPOS (*it));
3636 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
3637 &overlay);
3638 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3639
3640 /* If we are on invisible text, skip over it. */
3641 if (invis_p && IT_CHARPOS (*it) < it->end_charpos)
3642 {
3643 /* Record whether we have to display an ellipsis for the
3644 invisible text. */
3645 int display_ellipsis_p = invis_p == 2;
3646
3647 handled = HANDLED_RECOMPUTE_PROPS;
3648
3649 /* Loop skipping over invisible text. The loop is left at
3650 ZV or with IT on the first char being visible again. */
3651 do
3652 {
3653 /* Try to skip some invisible text. Return value is the
3654 position reached which can be equal to IT's position
3655 if there is nothing invisible here. This skips both
3656 over invisible text properties and overlays with
3657 invisible property. */
3658 newpos = skip_invisible (IT_CHARPOS (*it),
3659 &next_stop, ZV, it->window);
3660
3661 /* If we skipped nothing at all we weren't at invisible
3662 text in the first place. If everything to the end of
3663 the buffer was skipped, end the loop. */
3664 if (newpos == IT_CHARPOS (*it) || newpos >= ZV)
3665 invis_p = 0;
3666 else
3667 {
3668 /* We skipped some characters but not necessarily
3669 all there are. Check if we ended up on visible
3670 text. Fget_char_property returns the property of
3671 the char before the given position, i.e. if we
3672 get invis_p = 0, this means that the char at
3673 newpos is visible. */
3674 pos = make_number (newpos);
3675 prop = Fget_char_property (pos, Qinvisible, it->window);
3676 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3677 }
3678
3679 /* If we ended up on invisible text, proceed to
3680 skip starting with next_stop. */
3681 if (invis_p)
3682 IT_CHARPOS (*it) = next_stop;
3683
3684 /* If there are adjacent invisible texts, don't lose the
3685 second one's ellipsis. */
3686 if (invis_p == 2)
3687 display_ellipsis_p = 1;
3688 }
3689 while (invis_p);
3690
3691 /* The position newpos is now either ZV or on visible text. */
3692 IT_CHARPOS (*it) = newpos;
3693 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
3694
3695 /* If there are before-strings at the start of invisible
3696 text, and the text is invisible because of a text
3697 property, arrange to show before-strings because 20.x did
3698 it that way. (If the text is invisible because of an
3699 overlay property instead of a text property, this is
3700 already handled in the overlay code.) */
3701 if (NILP (overlay)
3702 && get_overlay_strings (it, start_charpos))
3703 {
3704 handled = HANDLED_RECOMPUTE_PROPS;
3705 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
3706 }
3707 else if (display_ellipsis_p)
3708 {
3709 /* Make sure that the glyphs of the ellipsis will get
3710 correct `charpos' values. If we would not update
3711 it->position here, the glyphs would belong to the
3712 last visible character _before_ the invisible
3713 text, which confuses `set_cursor_from_row'.
3714
3715 We use the last invisible position instead of the
3716 first because this way the cursor is always drawn on
3717 the first "." of the ellipsis, whenever PT is inside
3718 the invisible text. Otherwise the cursor would be
3719 placed _after_ the ellipsis when the point is after the
3720 first invisible character. */
3721 if (!STRINGP (it->object))
3722 {
3723 it->position.charpos = IT_CHARPOS (*it) - 1;
3724 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
3725 }
3726 setup_for_ellipsis (it, 0);
3727 /* Let the ellipsis display before
3728 considering any properties of the following char.
3729 Fixes jasonr@gnu.org 01 Oct 07 bug. */
3730 handled = HANDLED_RETURN;
3731 }
3732 }
3733 }
3734
3735 return handled;
3736 }
3737
3738
3739 /* Make iterator IT return `...' next.
3740 Replaces LEN characters from buffer. */
3741
3742 static void
3743 setup_for_ellipsis (it, len)
3744 struct it *it;
3745 int len;
3746 {
3747 /* Use the display table definition for `...'. Invalid glyphs
3748 will be handled by the method returning elements from dpvec. */
3749 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
3750 {
3751 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
3752 it->dpvec = v->contents;
3753 it->dpend = v->contents + v->size;
3754 }
3755 else
3756 {
3757 /* Default `...'. */
3758 it->dpvec = default_invis_vector;
3759 it->dpend = default_invis_vector + 3;
3760 }
3761
3762 it->dpvec_char_len = len;
3763 it->current.dpvec_index = 0;
3764 it->dpvec_face_id = -1;
3765
3766 /* Remember the current face id in case glyphs specify faces.
3767 IT's face is restored in set_iterator_to_next.
3768 saved_face_id was set to preceding char's face in handle_stop. */
3769 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
3770 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
3771
3772 it->method = GET_FROM_DISPLAY_VECTOR;
3773 it->ellipsis_p = 1;
3774 }
3775
3776
3777 \f
3778 /***********************************************************************
3779 'display' property
3780 ***********************************************************************/
3781
3782 /* Set up iterator IT from `display' property at its current position.
3783 Called from handle_stop.
3784 We return HANDLED_RETURN if some part of the display property
3785 overrides the display of the buffer text itself.
3786 Otherwise we return HANDLED_NORMALLY. */
3787
3788 static enum prop_handled
3789 handle_display_prop (it)
3790 struct it *it;
3791 {
3792 Lisp_Object prop, object;
3793 struct text_pos *position;
3794 /* Nonzero if some property replaces the display of the text itself. */
3795 int display_replaced_p = 0;
3796
3797 if (STRINGP (it->string))
3798 {
3799 object = it->string;
3800 position = &it->current.string_pos;
3801 }
3802 else
3803 {
3804 XSETWINDOW (object, it->w);
3805 position = &it->current.pos;
3806 }
3807
3808 /* Reset those iterator values set from display property values. */
3809 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
3810 it->space_width = Qnil;
3811 it->font_height = Qnil;
3812 it->voffset = 0;
3813
3814 /* We don't support recursive `display' properties, i.e. string
3815 values that have a string `display' property, that have a string
3816 `display' property etc. */
3817 if (!it->string_from_display_prop_p)
3818 it->area = TEXT_AREA;
3819
3820 prop = Fget_char_property (make_number (position->charpos),
3821 Qdisplay, object);
3822 if (NILP (prop))
3823 return HANDLED_NORMALLY;
3824
3825 if (!STRINGP (it->string))
3826 object = it->w->buffer;
3827
3828 if (CONSP (prop)
3829 /* Simple properties. */
3830 && !EQ (XCAR (prop), Qimage)
3831 && !EQ (XCAR (prop), Qspace)
3832 && !EQ (XCAR (prop), Qwhen)
3833 && !EQ (XCAR (prop), Qslice)
3834 && !EQ (XCAR (prop), Qspace_width)
3835 && !EQ (XCAR (prop), Qheight)
3836 && !EQ (XCAR (prop), Qraise)
3837 /* Marginal area specifications. */
3838 && !(CONSP (XCAR (prop)) && EQ (XCAR (XCAR (prop)), Qmargin))
3839 && !EQ (XCAR (prop), Qleft_fringe)
3840 && !EQ (XCAR (prop), Qright_fringe)
3841 && !NILP (XCAR (prop)))
3842 {
3843 for (; CONSP (prop); prop = XCDR (prop))
3844 {
3845 if (handle_single_display_spec (it, XCAR (prop), object,
3846 position, display_replaced_p))
3847 {
3848 display_replaced_p = 1;
3849 /* If some text in a string is replaced, `position' no
3850 longer points to the position of `object'. */
3851 if (STRINGP (object))
3852 break;
3853 }
3854 }
3855 }
3856 else if (VECTORP (prop))
3857 {
3858 int i;
3859 for (i = 0; i < ASIZE (prop); ++i)
3860 if (handle_single_display_spec (it, AREF (prop, i), object,
3861 position, display_replaced_p))
3862 {
3863 display_replaced_p = 1;
3864 /* If some text in a string is replaced, `position' no
3865 longer points to the position of `object'. */
3866 if (STRINGP (object))
3867 break;
3868 }
3869 }
3870 else
3871 {
3872 int ret = handle_single_display_spec (it, prop, object, position, 0);
3873 if (ret < 0) /* Replaced by "", i.e. nothing. */
3874 return HANDLED_RECOMPUTE_PROPS;
3875 if (ret)
3876 display_replaced_p = 1;
3877 }
3878
3879 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
3880 }
3881
3882
3883 /* Value is the position of the end of the `display' property starting
3884 at START_POS in OBJECT. */
3885
3886 static struct text_pos
3887 display_prop_end (it, object, start_pos)
3888 struct it *it;
3889 Lisp_Object object;
3890 struct text_pos start_pos;
3891 {
3892 Lisp_Object end;
3893 struct text_pos end_pos;
3894
3895 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
3896 Qdisplay, object, Qnil);
3897 CHARPOS (end_pos) = XFASTINT (end);
3898 if (STRINGP (object))
3899 compute_string_pos (&end_pos, start_pos, it->string);
3900 else
3901 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
3902
3903 return end_pos;
3904 }
3905
3906
3907 /* Set up IT from a single `display' specification PROP. OBJECT
3908 is the object in which the `display' property was found. *POSITION
3909 is the position at which it was found. DISPLAY_REPLACED_P non-zero
3910 means that we previously saw a display specification which already
3911 replaced text display with something else, for example an image;
3912 we ignore such properties after the first one has been processed.
3913
3914 If PROP is a `space' or `image' specification, and in some other
3915 cases too, set *POSITION to the position where the `display'
3916 property ends.
3917
3918 Value is non-zero if something was found which replaces the display
3919 of buffer or string text. Specifically, the value is -1 if that
3920 "something" is "nothing". */
3921
3922 static int
3923 handle_single_display_spec (it, spec, object, position,
3924 display_replaced_before_p)
3925 struct it *it;
3926 Lisp_Object spec;
3927 Lisp_Object object;
3928 struct text_pos *position;
3929 int display_replaced_before_p;
3930 {
3931 Lisp_Object form;
3932 Lisp_Object location, value;
3933 struct text_pos start_pos, save_pos;
3934 int valid_p;
3935
3936 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
3937 If the result is non-nil, use VALUE instead of SPEC. */
3938 form = Qt;
3939 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
3940 {
3941 spec = XCDR (spec);
3942 if (!CONSP (spec))
3943 return 0;
3944 form = XCAR (spec);
3945 spec = XCDR (spec);
3946 }
3947
3948 if (!NILP (form) && !EQ (form, Qt))
3949 {
3950 int count = SPECPDL_INDEX ();
3951 struct gcpro gcpro1;
3952
3953 /* Bind `object' to the object having the `display' property, a
3954 buffer or string. Bind `position' to the position in the
3955 object where the property was found, and `buffer-position'
3956 to the current position in the buffer. */
3957 specbind (Qobject, object);
3958 specbind (Qposition, make_number (CHARPOS (*position)));
3959 specbind (Qbuffer_position,
3960 make_number (STRINGP (object)
3961 ? IT_CHARPOS (*it) : CHARPOS (*position)));
3962 GCPRO1 (form);
3963 form = safe_eval (form);
3964 UNGCPRO;
3965 unbind_to (count, Qnil);
3966 }
3967
3968 if (NILP (form))
3969 return 0;
3970
3971 /* Handle `(height HEIGHT)' specifications. */
3972 if (CONSP (spec)
3973 && EQ (XCAR (spec), Qheight)
3974 && CONSP (XCDR (spec)))
3975 {
3976 if (!FRAME_WINDOW_P (it->f))
3977 return 0;
3978
3979 it->font_height = XCAR (XCDR (spec));
3980 if (!NILP (it->font_height))
3981 {
3982 struct face *face = FACE_FROM_ID (it->f, it->face_id);
3983 int new_height = -1;
3984
3985 if (CONSP (it->font_height)
3986 && (EQ (XCAR (it->font_height), Qplus)
3987 || EQ (XCAR (it->font_height), Qminus))
3988 && CONSP (XCDR (it->font_height))
3989 && INTEGERP (XCAR (XCDR (it->font_height))))
3990 {
3991 /* `(+ N)' or `(- N)' where N is an integer. */
3992 int steps = XINT (XCAR (XCDR (it->font_height)));
3993 if (EQ (XCAR (it->font_height), Qplus))
3994 steps = - steps;
3995 it->face_id = smaller_face (it->f, it->face_id, steps);
3996 }
3997 else if (FUNCTIONP (it->font_height))
3998 {
3999 /* Call function with current height as argument.
4000 Value is the new height. */
4001 Lisp_Object height;
4002 height = safe_call1 (it->font_height,
4003 face->lface[LFACE_HEIGHT_INDEX]);
4004 if (NUMBERP (height))
4005 new_height = XFLOATINT (height);
4006 }
4007 else if (NUMBERP (it->font_height))
4008 {
4009 /* Value is a multiple of the canonical char height. */
4010 struct face *face;
4011
4012 face = FACE_FROM_ID (it->f, DEFAULT_FACE_ID);
4013 new_height = (XFLOATINT (it->font_height)
4014 * XINT (face->lface[LFACE_HEIGHT_INDEX]));
4015 }
4016 else
4017 {
4018 /* Evaluate IT->font_height with `height' bound to the
4019 current specified height to get the new height. */
4020 int count = SPECPDL_INDEX ();
4021
4022 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4023 value = safe_eval (it->font_height);
4024 unbind_to (count, Qnil);
4025
4026 if (NUMBERP (value))
4027 new_height = XFLOATINT (value);
4028 }
4029
4030 if (new_height > 0)
4031 it->face_id = face_with_height (it->f, it->face_id, new_height);
4032 }
4033
4034 return 0;
4035 }
4036
4037 /* Handle `(space_width WIDTH)'. */
4038 if (CONSP (spec)
4039 && EQ (XCAR (spec), Qspace_width)
4040 && CONSP (XCDR (spec)))
4041 {
4042 if (!FRAME_WINDOW_P (it->f))
4043 return 0;
4044
4045 value = XCAR (XCDR (spec));
4046 if (NUMBERP (value) && XFLOATINT (value) > 0)
4047 it->space_width = value;
4048
4049 return 0;
4050 }
4051
4052 /* Handle `(slice X Y WIDTH HEIGHT)'. */
4053 if (CONSP (spec)
4054 && EQ (XCAR (spec), Qslice))
4055 {
4056 Lisp_Object tem;
4057
4058 if (!FRAME_WINDOW_P (it->f))
4059 return 0;
4060
4061 if (tem = XCDR (spec), CONSP (tem))
4062 {
4063 it->slice.x = XCAR (tem);
4064 if (tem = XCDR (tem), CONSP (tem))
4065 {
4066 it->slice.y = XCAR (tem);
4067 if (tem = XCDR (tem), CONSP (tem))
4068 {
4069 it->slice.width = XCAR (tem);
4070 if (tem = XCDR (tem), CONSP (tem))
4071 it->slice.height = XCAR (tem);
4072 }
4073 }
4074 }
4075
4076 return 0;
4077 }
4078
4079 /* Handle `(raise FACTOR)'. */
4080 if (CONSP (spec)
4081 && EQ (XCAR (spec), Qraise)
4082 && CONSP (XCDR (spec)))
4083 {
4084 if (!FRAME_WINDOW_P (it->f))
4085 return 0;
4086
4087 #ifdef HAVE_WINDOW_SYSTEM
4088 value = XCAR (XCDR (spec));
4089 if (NUMBERP (value))
4090 {
4091 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4092 it->voffset = - (XFLOATINT (value)
4093 * (FONT_HEIGHT (face->font)));
4094 }
4095 #endif /* HAVE_WINDOW_SYSTEM */
4096
4097 return 0;
4098 }
4099
4100 /* Don't handle the other kinds of display specifications
4101 inside a string that we got from a `display' property. */
4102 if (it->string_from_display_prop_p)
4103 return 0;
4104
4105 /* Characters having this form of property are not displayed, so
4106 we have to find the end of the property. */
4107 start_pos = *position;
4108 *position = display_prop_end (it, object, start_pos);
4109 value = Qnil;
4110
4111 /* Stop the scan at that end position--we assume that all
4112 text properties change there. */
4113 it->stop_charpos = position->charpos;
4114
4115 /* Handle `(left-fringe BITMAP [FACE])'
4116 and `(right-fringe BITMAP [FACE])'. */
4117 if (CONSP (spec)
4118 && (EQ (XCAR (spec), Qleft_fringe)
4119 || EQ (XCAR (spec), Qright_fringe))
4120 && CONSP (XCDR (spec)))
4121 {
4122 int face_id = DEFAULT_FACE_ID;
4123 int fringe_bitmap;
4124
4125 if (!FRAME_WINDOW_P (it->f))
4126 /* If we return here, POSITION has been advanced
4127 across the text with this property. */
4128 return 0;
4129
4130 #ifdef HAVE_WINDOW_SYSTEM
4131 value = XCAR (XCDR (spec));
4132 if (!SYMBOLP (value)
4133 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
4134 /* If we return here, POSITION has been advanced
4135 across the text with this property. */
4136 return 0;
4137
4138 if (CONSP (XCDR (XCDR (spec))))
4139 {
4140 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
4141 int face_id2 = lookup_derived_face (it->f, face_name,
4142 FRINGE_FACE_ID, 0);
4143 if (face_id2 >= 0)
4144 face_id = face_id2;
4145 }
4146
4147 /* Save current settings of IT so that we can restore them
4148 when we are finished with the glyph property value. */
4149
4150 save_pos = it->position;
4151 it->position = *position;
4152 push_it (it);
4153 it->position = save_pos;
4154
4155 it->area = TEXT_AREA;
4156 it->what = IT_IMAGE;
4157 it->image_id = -1; /* no image */
4158 it->position = start_pos;
4159 it->object = NILP (object) ? it->w->buffer : object;
4160 it->method = GET_FROM_IMAGE;
4161 it->face_id = face_id;
4162
4163 /* Say that we haven't consumed the characters with
4164 `display' property yet. The call to pop_it in
4165 set_iterator_to_next will clean this up. */
4166 *position = start_pos;
4167
4168 if (EQ (XCAR (spec), Qleft_fringe))
4169 {
4170 it->left_user_fringe_bitmap = fringe_bitmap;
4171 it->left_user_fringe_face_id = face_id;
4172 }
4173 else
4174 {
4175 it->right_user_fringe_bitmap = fringe_bitmap;
4176 it->right_user_fringe_face_id = face_id;
4177 }
4178 #endif /* HAVE_WINDOW_SYSTEM */
4179 return 1;
4180 }
4181
4182 /* Prepare to handle `((margin left-margin) ...)',
4183 `((margin right-margin) ...)' and `((margin nil) ...)'
4184 prefixes for display specifications. */
4185 location = Qunbound;
4186 if (CONSP (spec) && CONSP (XCAR (spec)))
4187 {
4188 Lisp_Object tem;
4189
4190 value = XCDR (spec);
4191 if (CONSP (value))
4192 value = XCAR (value);
4193
4194 tem = XCAR (spec);
4195 if (EQ (XCAR (tem), Qmargin)
4196 && (tem = XCDR (tem),
4197 tem = CONSP (tem) ? XCAR (tem) : Qnil,
4198 (NILP (tem)
4199 || EQ (tem, Qleft_margin)
4200 || EQ (tem, Qright_margin))))
4201 location = tem;
4202 }
4203
4204 if (EQ (location, Qunbound))
4205 {
4206 location = Qnil;
4207 value = spec;
4208 }
4209
4210 /* After this point, VALUE is the property after any
4211 margin prefix has been stripped. It must be a string,
4212 an image specification, or `(space ...)'.
4213
4214 LOCATION specifies where to display: `left-margin',
4215 `right-margin' or nil. */
4216
4217 valid_p = (STRINGP (value)
4218 #ifdef HAVE_WINDOW_SYSTEM
4219 || (FRAME_WINDOW_P (it->f) && valid_image_p (value))
4220 #endif /* not HAVE_WINDOW_SYSTEM */
4221 || (CONSP (value) && EQ (XCAR (value), Qspace)));
4222
4223 if (valid_p && !display_replaced_before_p)
4224 {
4225 /* Save current settings of IT so that we can restore them
4226 when we are finished with the glyph property value. */
4227 save_pos = it->position;
4228 it->position = *position;
4229 push_it (it);
4230 it->position = save_pos;
4231
4232 if (NILP (location))
4233 it->area = TEXT_AREA;
4234 else if (EQ (location, Qleft_margin))
4235 it->area = LEFT_MARGIN_AREA;
4236 else
4237 it->area = RIGHT_MARGIN_AREA;
4238
4239 if (STRINGP (value))
4240 {
4241 if (SCHARS (value) == 0)
4242 {
4243 pop_it (it);
4244 return -1; /* Replaced by "", i.e. nothing. */
4245 }
4246 it->string = value;
4247 it->multibyte_p = STRING_MULTIBYTE (it->string);
4248 it->current.overlay_string_index = -1;
4249 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4250 it->end_charpos = it->string_nchars = SCHARS (it->string);
4251 it->method = GET_FROM_STRING;
4252 it->stop_charpos = 0;
4253 it->string_from_display_prop_p = 1;
4254 /* Say that we haven't consumed the characters with
4255 `display' property yet. The call to pop_it in
4256 set_iterator_to_next will clean this up. */
4257 if (BUFFERP (object))
4258 it->current.pos = start_pos;
4259 }
4260 else if (CONSP (value) && EQ (XCAR (value), Qspace))
4261 {
4262 it->method = GET_FROM_STRETCH;
4263 it->object = value;
4264 it->position = start_pos;
4265 if (BUFFERP (object))
4266 it->current.pos = start_pos;
4267 }
4268 #ifdef HAVE_WINDOW_SYSTEM
4269 else
4270 {
4271 it->what = IT_IMAGE;
4272 it->image_id = lookup_image (it->f, value);
4273 it->position = start_pos;
4274 it->object = NILP (object) ? it->w->buffer : object;
4275 it->method = GET_FROM_IMAGE;
4276
4277 /* Say that we haven't consumed the characters with
4278 `display' property yet. The call to pop_it in
4279 set_iterator_to_next will clean this up. */
4280 if (BUFFERP (object))
4281 it->current.pos = start_pos;
4282 }
4283 #endif /* HAVE_WINDOW_SYSTEM */
4284
4285 return 1;
4286 }
4287
4288 /* Invalid property or property not supported. Restore
4289 POSITION to what it was before. */
4290 *position = start_pos;
4291 return 0;
4292 }
4293
4294
4295 /* Check if SPEC is a display sub-property value whose text should be
4296 treated as intangible. */
4297
4298 static int
4299 single_display_spec_intangible_p (prop)
4300 Lisp_Object prop;
4301 {
4302 /* Skip over `when FORM'. */
4303 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
4304 {
4305 prop = XCDR (prop);
4306 if (!CONSP (prop))
4307 return 0;
4308 prop = XCDR (prop);
4309 }
4310
4311 if (STRINGP (prop))
4312 return 1;
4313
4314 if (!CONSP (prop))
4315 return 0;
4316
4317 /* Skip over `margin LOCATION'. If LOCATION is in the margins,
4318 we don't need to treat text as intangible. */
4319 if (EQ (XCAR (prop), Qmargin))
4320 {
4321 prop = XCDR (prop);
4322 if (!CONSP (prop))
4323 return 0;
4324
4325 prop = XCDR (prop);
4326 if (!CONSP (prop)
4327 || EQ (XCAR (prop), Qleft_margin)
4328 || EQ (XCAR (prop), Qright_margin))
4329 return 0;
4330 }
4331
4332 return (CONSP (prop)
4333 && (EQ (XCAR (prop), Qimage)
4334 || EQ (XCAR (prop), Qspace)));
4335 }
4336
4337
4338 /* Check if PROP is a display property value whose text should be
4339 treated as intangible. */
4340
4341 int
4342 display_prop_intangible_p (prop)
4343 Lisp_Object prop;
4344 {
4345 if (CONSP (prop)
4346 && CONSP (XCAR (prop))
4347 && !EQ (Qmargin, XCAR (XCAR (prop))))
4348 {
4349 /* A list of sub-properties. */
4350 while (CONSP (prop))
4351 {
4352 if (single_display_spec_intangible_p (XCAR (prop)))
4353 return 1;
4354 prop = XCDR (prop);
4355 }
4356 }
4357 else if (VECTORP (prop))
4358 {
4359 /* A vector of sub-properties. */
4360 int i;
4361 for (i = 0; i < ASIZE (prop); ++i)
4362 if (single_display_spec_intangible_p (AREF (prop, i)))
4363 return 1;
4364 }
4365 else
4366 return single_display_spec_intangible_p (prop);
4367
4368 return 0;
4369 }
4370
4371
4372 /* Return 1 if PROP is a display sub-property value containing STRING. */
4373
4374 static int
4375 single_display_spec_string_p (prop, string)
4376 Lisp_Object prop, string;
4377 {
4378 if (EQ (string, prop))
4379 return 1;
4380
4381 /* Skip over `when FORM'. */
4382 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
4383 {
4384 prop = XCDR (prop);
4385 if (!CONSP (prop))
4386 return 0;
4387 prop = XCDR (prop);
4388 }
4389
4390 if (CONSP (prop))
4391 /* Skip over `margin LOCATION'. */
4392 if (EQ (XCAR (prop), Qmargin))
4393 {
4394 prop = XCDR (prop);
4395 if (!CONSP (prop))
4396 return 0;
4397
4398 prop = XCDR (prop);
4399 if (!CONSP (prop))
4400 return 0;
4401 }
4402
4403 return CONSP (prop) && EQ (XCAR (prop), string);
4404 }
4405
4406
4407 /* Return 1 if STRING appears in the `display' property PROP. */
4408
4409 static int
4410 display_prop_string_p (prop, string)
4411 Lisp_Object prop, string;
4412 {
4413 if (CONSP (prop)
4414 && CONSP (XCAR (prop))
4415 && !EQ (Qmargin, XCAR (XCAR (prop))))
4416 {
4417 /* A list of sub-properties. */
4418 while (CONSP (prop))
4419 {
4420 if (single_display_spec_string_p (XCAR (prop), string))
4421 return 1;
4422 prop = XCDR (prop);
4423 }
4424 }
4425 else if (VECTORP (prop))
4426 {
4427 /* A vector of sub-properties. */
4428 int i;
4429 for (i = 0; i < ASIZE (prop); ++i)
4430 if (single_display_spec_string_p (AREF (prop, i), string))
4431 return 1;
4432 }
4433 else
4434 return single_display_spec_string_p (prop, string);
4435
4436 return 0;
4437 }
4438
4439
4440 /* Determine from which buffer position in W's buffer STRING comes
4441 from. AROUND_CHARPOS is an approximate position where it could
4442 be from. Value is the buffer position or 0 if it couldn't be
4443 determined.
4444
4445 W's buffer must be current.
4446
4447 This function is necessary because we don't record buffer positions
4448 in glyphs generated from strings (to keep struct glyph small).
4449 This function may only use code that doesn't eval because it is
4450 called asynchronously from note_mouse_highlight. */
4451
4452 int
4453 string_buffer_position (w, string, around_charpos)
4454 struct window *w;
4455 Lisp_Object string;
4456 int around_charpos;
4457 {
4458 Lisp_Object limit, prop, pos;
4459 const int MAX_DISTANCE = 1000;
4460 int found = 0;
4461
4462 pos = make_number (around_charpos);
4463 limit = make_number (min (XINT (pos) + MAX_DISTANCE, ZV));
4464 while (!found && !EQ (pos, limit))
4465 {
4466 prop = Fget_char_property (pos, Qdisplay, Qnil);
4467 if (!NILP (prop) && display_prop_string_p (prop, string))
4468 found = 1;
4469 else
4470 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil, limit);
4471 }
4472
4473 if (!found)
4474 {
4475 pos = make_number (around_charpos);
4476 limit = make_number (max (XINT (pos) - MAX_DISTANCE, BEGV));
4477 while (!found && !EQ (pos, limit))
4478 {
4479 prop = Fget_char_property (pos, Qdisplay, Qnil);
4480 if (!NILP (prop) && display_prop_string_p (prop, string))
4481 found = 1;
4482 else
4483 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
4484 limit);
4485 }
4486 }
4487
4488 return found ? XINT (pos) : 0;
4489 }
4490
4491
4492 \f
4493 /***********************************************************************
4494 `composition' property
4495 ***********************************************************************/
4496
4497 static enum prop_handled
4498 handle_auto_composed_prop (it)
4499 struct it *it;
4500 {
4501 enum prop_handled handled = HANDLED_NORMALLY;
4502
4503 if (FUNCTIONP (Vauto_composition_function))
4504 {
4505 Lisp_Object val;
4506 EMACS_INT pos, this_pos;
4507
4508 if (STRINGP (it->string))
4509 pos = IT_STRING_CHARPOS (*it);
4510 else
4511 pos = IT_CHARPOS (*it);
4512 this_pos = pos;
4513
4514 val =Fget_char_property (make_number (pos), Qauto_composed, it->string);
4515 if (! NILP (val))
4516 {
4517 Lisp_Object limit = Qnil, next;
4518
4519 /* As Fnext_single_char_property_change is very slow, we
4520 limit the search to the current line. */
4521 if (STRINGP (it->string))
4522 limit = make_number (SCHARS (it->string));
4523 else
4524 limit = make_number (find_next_newline_no_quit (pos, 1));
4525
4526 next = (Fnext_single_property_change
4527 (make_number (pos), Qauto_composed, it->string, limit));
4528 if (XINT (next) < XINT (limit))
4529 {
4530 /* The current point is auto-composed, but there exist
4531 characters not yet composed beyond the auto-composed
4532 region. There's a possiblity that the last
4533 characters in the region may be newly composed. */
4534 int charpos = XINT (next) - 1, bytepos, c;
4535
4536 if (STRINGP (it->string))
4537 {
4538 bytepos = string_char_to_byte (it->string, charpos);
4539 c = SDATA (it->string)[bytepos];
4540 }
4541 else
4542 {
4543 bytepos = CHAR_TO_BYTE (charpos);
4544 c = FETCH_BYTE (bytepos);
4545 }
4546 if (c != '\n')
4547 /* If the last character is not newline, it may be
4548 composed with the following characters. */
4549 val = Qnil, pos = charpos + 1;
4550 }
4551 }
4552 if (NILP (val))
4553 {
4554 int count = SPECPDL_INDEX ();
4555 Lisp_Object args[4];
4556
4557 args[0] = Vauto_composition_function;
4558 specbind (Qauto_composition_function, Qnil);
4559 args[1] = make_number (pos);
4560 args[2] = it->string;
4561 #ifdef USE_FONT_BACKEND
4562 if (enable_font_backend)
4563 {
4564 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4565 int c;
4566
4567 if (STRINGP (it->string))
4568 {
4569 EMACS_INT pos_byte = IT_STRING_BYTEPOS (*it);
4570 const unsigned char *s = SDATA (it->string) + pos_byte;
4571
4572 if (STRING_MULTIBYTE (it->string))
4573 it->c = STRING_CHAR (s, 0);
4574 else
4575 it->c = *s;
4576 }
4577 else
4578 {
4579 EMACS_INT pos_byte = IT_BYTEPOS (*it);
4580
4581 it->c = FETCH_CHAR (pos_byte);
4582 }
4583 args[3] = font_at (it->c, this_pos, face, it->w, it->string);
4584 }
4585 else
4586 #endif /* USE_FONT_BACKEND */
4587 args[3] = Qnil;
4588 safe_call (4, args);
4589 unbind_to (count, Qnil);
4590
4591 if (this_pos == pos)
4592 {
4593 val = Fget_char_property (args[1], Qauto_composed, it->string);
4594 /* Return HANDLED_RECOMPUTE_PROPS only if function composed
4595 something. This avoids an endless loop if they failed to
4596 fontify the text for which reason ever. */
4597 if (! NILP (val))
4598 handled = HANDLED_RECOMPUTE_PROPS;
4599 }
4600 else
4601 handled = HANDLED_RECOMPUTE_PROPS;
4602 }
4603 }
4604
4605 return handled;
4606 }
4607
4608 /* Set up iterator IT from `composition' property at its current
4609 position. Called from handle_stop. */
4610
4611 static enum prop_handled
4612 handle_composition_prop (it)
4613 struct it *it;
4614 {
4615 Lisp_Object prop, string;
4616 EMACS_INT pos, pos_byte, start, end;
4617 enum prop_handled handled = HANDLED_NORMALLY;
4618
4619 if (STRINGP (it->string))
4620 {
4621 pos = IT_STRING_CHARPOS (*it);
4622 pos_byte = IT_STRING_BYTEPOS (*it);
4623 string = it->string;
4624 }
4625 else
4626 {
4627 pos = IT_CHARPOS (*it);
4628 pos_byte = IT_BYTEPOS (*it);
4629 string = Qnil;
4630 }
4631
4632 /* If there's a valid composition and point is not inside of the
4633 composition (in the case that the composition is from the current
4634 buffer), draw a glyph composed from the composition components. */
4635 if (find_composition (pos, -1, &start, &end, &prop, string)
4636 && COMPOSITION_VALID_P (start, end, prop)
4637 && (STRINGP (it->string) || (PT <= start || PT >= end)))
4638 {
4639 int id;
4640
4641 if (start != pos)
4642 {
4643 if (STRINGP (it->string))
4644 pos_byte = string_char_to_byte (it->string, start);
4645 else
4646 pos_byte = CHAR_TO_BYTE (start);
4647 }
4648 id = get_composition_id (start, pos_byte, end - start, prop, string);
4649
4650 if (id >= 0)
4651 {
4652 struct composition *cmp = composition_table[id];
4653
4654 if (cmp->glyph_len == 0)
4655 {
4656 /* No glyph. */
4657 if (STRINGP (it->string))
4658 {
4659 IT_STRING_CHARPOS (*it) = end;
4660 IT_STRING_BYTEPOS (*it) = string_char_to_byte (it->string,
4661 end);
4662 }
4663 else
4664 {
4665 IT_CHARPOS (*it) = end;
4666 IT_BYTEPOS (*it) = CHAR_TO_BYTE (end);
4667 }
4668 return HANDLED_RECOMPUTE_PROPS;
4669 }
4670
4671 it->stop_charpos = end;
4672 push_it (it);
4673
4674 it->method = GET_FROM_COMPOSITION;
4675 it->cmp_id = id;
4676 it->cmp_len = COMPOSITION_LENGTH (prop);
4677 /* For a terminal, draw only the first (non-TAB) character
4678 of the components. */
4679 #ifdef USE_FONT_BACKEND
4680 if (composition_table[id]->method == COMPOSITION_WITH_GLYPH_STRING)
4681 {
4682 Lisp_Object lgstring = AREF (XHASH_TABLE (composition_hash_table)
4683 ->key_and_value,
4684 cmp->hash_index * 2);
4685
4686 it->c = XINT (LGLYPH_CHAR (LGSTRING_GLYPH (lgstring, 0)));
4687 }
4688 else
4689 #endif /* USE_FONT_BACKEND */
4690 {
4691 int i;
4692
4693 for (i = 0; i < cmp->glyph_len; i++)
4694 if ((it->c = COMPOSITION_GLYPH (composition_table[id], i))
4695 != '\t')
4696 break;
4697 }
4698 if (it->c == '\t')
4699 it->c = ' ';
4700 it->len = (STRINGP (it->string)
4701 ? string_char_to_byte (it->string, end)
4702 : CHAR_TO_BYTE (end)) - pos_byte;
4703 handled = HANDLED_RETURN;
4704 }
4705 }
4706
4707 return handled;
4708 }
4709
4710
4711 \f
4712 /***********************************************************************
4713 Overlay strings
4714 ***********************************************************************/
4715
4716 /* The following structure is used to record overlay strings for
4717 later sorting in load_overlay_strings. */
4718
4719 struct overlay_entry
4720 {
4721 Lisp_Object overlay;
4722 Lisp_Object string;
4723 int priority;
4724 int after_string_p;
4725 };
4726
4727
4728 /* Set up iterator IT from overlay strings at its current position.
4729 Called from handle_stop. */
4730
4731 static enum prop_handled
4732 handle_overlay_change (it)
4733 struct it *it;
4734 {
4735 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
4736 return HANDLED_RECOMPUTE_PROPS;
4737 else
4738 return HANDLED_NORMALLY;
4739 }
4740
4741
4742 /* Set up the next overlay string for delivery by IT, if there is an
4743 overlay string to deliver. Called by set_iterator_to_next when the
4744 end of the current overlay string is reached. If there are more
4745 overlay strings to display, IT->string and
4746 IT->current.overlay_string_index are set appropriately here.
4747 Otherwise IT->string is set to nil. */
4748
4749 static void
4750 next_overlay_string (it)
4751 struct it *it;
4752 {
4753 ++it->current.overlay_string_index;
4754 if (it->current.overlay_string_index == it->n_overlay_strings)
4755 {
4756 /* No more overlay strings. Restore IT's settings to what
4757 they were before overlay strings were processed, and
4758 continue to deliver from current_buffer. */
4759 int display_ellipsis_p = it->stack[it->sp - 1].display_ellipsis_p;
4760
4761 pop_it (it);
4762 xassert (it->sp > 0
4763 || it->method == GET_FROM_COMPOSITION
4764 || (NILP (it->string)
4765 && it->method == GET_FROM_BUFFER
4766 && it->stop_charpos >= BEGV
4767 && it->stop_charpos <= it->end_charpos));
4768 it->current.overlay_string_index = -1;
4769 it->n_overlay_strings = 0;
4770
4771 /* If we're at the end of the buffer, record that we have
4772 processed the overlay strings there already, so that
4773 next_element_from_buffer doesn't try it again. */
4774 if (IT_CHARPOS (*it) >= it->end_charpos)
4775 it->overlay_strings_at_end_processed_p = 1;
4776
4777 /* If we have to display `...' for invisible text, set
4778 the iterator up for that. */
4779 if (display_ellipsis_p)
4780 setup_for_ellipsis (it, 0);
4781 }
4782 else
4783 {
4784 /* There are more overlay strings to process. If
4785 IT->current.overlay_string_index has advanced to a position
4786 where we must load IT->overlay_strings with more strings, do
4787 it. */
4788 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
4789
4790 if (it->current.overlay_string_index && i == 0)
4791 load_overlay_strings (it, 0);
4792
4793 /* Initialize IT to deliver display elements from the overlay
4794 string. */
4795 it->string = it->overlay_strings[i];
4796 it->multibyte_p = STRING_MULTIBYTE (it->string);
4797 SET_TEXT_POS (it->current.string_pos, 0, 0);
4798 it->method = GET_FROM_STRING;
4799 it->stop_charpos = 0;
4800 }
4801
4802 CHECK_IT (it);
4803 }
4804
4805
4806 /* Compare two overlay_entry structures E1 and E2. Used as a
4807 comparison function for qsort in load_overlay_strings. Overlay
4808 strings for the same position are sorted so that
4809
4810 1. All after-strings come in front of before-strings, except
4811 when they come from the same overlay.
4812
4813 2. Within after-strings, strings are sorted so that overlay strings
4814 from overlays with higher priorities come first.
4815
4816 2. Within before-strings, strings are sorted so that overlay
4817 strings from overlays with higher priorities come last.
4818
4819 Value is analogous to strcmp. */
4820
4821
4822 static int
4823 compare_overlay_entries (e1, e2)
4824 void *e1, *e2;
4825 {
4826 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
4827 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
4828 int result;
4829
4830 if (entry1->after_string_p != entry2->after_string_p)
4831 {
4832 /* Let after-strings appear in front of before-strings if
4833 they come from different overlays. */
4834 if (EQ (entry1->overlay, entry2->overlay))
4835 result = entry1->after_string_p ? 1 : -1;
4836 else
4837 result = entry1->after_string_p ? -1 : 1;
4838 }
4839 else if (entry1->after_string_p)
4840 /* After-strings sorted in order of decreasing priority. */
4841 result = entry2->priority - entry1->priority;
4842 else
4843 /* Before-strings sorted in order of increasing priority. */
4844 result = entry1->priority - entry2->priority;
4845
4846 return result;
4847 }
4848
4849
4850 /* Load the vector IT->overlay_strings with overlay strings from IT's
4851 current buffer position, or from CHARPOS if that is > 0. Set
4852 IT->n_overlays to the total number of overlay strings found.
4853
4854 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
4855 a time. On entry into load_overlay_strings,
4856 IT->current.overlay_string_index gives the number of overlay
4857 strings that have already been loaded by previous calls to this
4858 function.
4859
4860 IT->add_overlay_start contains an additional overlay start
4861 position to consider for taking overlay strings from, if non-zero.
4862 This position comes into play when the overlay has an `invisible'
4863 property, and both before and after-strings. When we've skipped to
4864 the end of the overlay, because of its `invisible' property, we
4865 nevertheless want its before-string to appear.
4866 IT->add_overlay_start will contain the overlay start position
4867 in this case.
4868
4869 Overlay strings are sorted so that after-string strings come in
4870 front of before-string strings. Within before and after-strings,
4871 strings are sorted by overlay priority. See also function
4872 compare_overlay_entries. */
4873
4874 static void
4875 load_overlay_strings (it, charpos)
4876 struct it *it;
4877 int charpos;
4878 {
4879 extern Lisp_Object Qafter_string, Qbefore_string, Qwindow, Qpriority;
4880 Lisp_Object overlay, window, str, invisible;
4881 struct Lisp_Overlay *ov;
4882 int start, end;
4883 int size = 20;
4884 int n = 0, i, j, invis_p;
4885 struct overlay_entry *entries
4886 = (struct overlay_entry *) alloca (size * sizeof *entries);
4887
4888 if (charpos <= 0)
4889 charpos = IT_CHARPOS (*it);
4890
4891 /* Append the overlay string STRING of overlay OVERLAY to vector
4892 `entries' which has size `size' and currently contains `n'
4893 elements. AFTER_P non-zero means STRING is an after-string of
4894 OVERLAY. */
4895 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
4896 do \
4897 { \
4898 Lisp_Object priority; \
4899 \
4900 if (n == size) \
4901 { \
4902 int new_size = 2 * size; \
4903 struct overlay_entry *old = entries; \
4904 entries = \
4905 (struct overlay_entry *) alloca (new_size \
4906 * sizeof *entries); \
4907 bcopy (old, entries, size * sizeof *entries); \
4908 size = new_size; \
4909 } \
4910 \
4911 entries[n].string = (STRING); \
4912 entries[n].overlay = (OVERLAY); \
4913 priority = Foverlay_get ((OVERLAY), Qpriority); \
4914 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
4915 entries[n].after_string_p = (AFTER_P); \
4916 ++n; \
4917 } \
4918 while (0)
4919
4920 /* Process overlay before the overlay center. */
4921 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
4922 {
4923 XSETMISC (overlay, ov);
4924 xassert (OVERLAYP (overlay));
4925 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4926 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4927
4928 if (end < charpos)
4929 break;
4930
4931 /* Skip this overlay if it doesn't start or end at IT's current
4932 position. */
4933 if (end != charpos && start != charpos)
4934 continue;
4935
4936 /* Skip this overlay if it doesn't apply to IT->w. */
4937 window = Foverlay_get (overlay, Qwindow);
4938 if (WINDOWP (window) && XWINDOW (window) != it->w)
4939 continue;
4940
4941 /* If the text ``under'' the overlay is invisible, both before-
4942 and after-strings from this overlay are visible; start and
4943 end position are indistinguishable. */
4944 invisible = Foverlay_get (overlay, Qinvisible);
4945 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4946
4947 /* If overlay has a non-empty before-string, record it. */
4948 if ((start == charpos || (end == charpos && invis_p))
4949 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4950 && SCHARS (str))
4951 RECORD_OVERLAY_STRING (overlay, str, 0);
4952
4953 /* If overlay has a non-empty after-string, record it. */
4954 if ((end == charpos || (start == charpos && invis_p))
4955 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4956 && SCHARS (str))
4957 RECORD_OVERLAY_STRING (overlay, str, 1);
4958 }
4959
4960 /* Process overlays after the overlay center. */
4961 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
4962 {
4963 XSETMISC (overlay, ov);
4964 xassert (OVERLAYP (overlay));
4965 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4966 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4967
4968 if (start > charpos)
4969 break;
4970
4971 /* Skip this overlay if it doesn't start or end at IT's current
4972 position. */
4973 if (end != charpos && start != charpos)
4974 continue;
4975
4976 /* Skip this overlay if it doesn't apply to IT->w. */
4977 window = Foverlay_get (overlay, Qwindow);
4978 if (WINDOWP (window) && XWINDOW (window) != it->w)
4979 continue;
4980
4981 /* If the text ``under'' the overlay is invisible, it has a zero
4982 dimension, and both before- and after-strings apply. */
4983 invisible = Foverlay_get (overlay, Qinvisible);
4984 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4985
4986 /* If overlay has a non-empty before-string, record it. */
4987 if ((start == charpos || (end == charpos && invis_p))
4988 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4989 && SCHARS (str))
4990 RECORD_OVERLAY_STRING (overlay, str, 0);
4991
4992 /* If overlay has a non-empty after-string, record it. */
4993 if ((end == charpos || (start == charpos && invis_p))
4994 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4995 && SCHARS (str))
4996 RECORD_OVERLAY_STRING (overlay, str, 1);
4997 }
4998
4999 #undef RECORD_OVERLAY_STRING
5000
5001 /* Sort entries. */
5002 if (n > 1)
5003 qsort (entries, n, sizeof *entries, compare_overlay_entries);
5004
5005 /* Record the total number of strings to process. */
5006 it->n_overlay_strings = n;
5007
5008 /* IT->current.overlay_string_index is the number of overlay strings
5009 that have already been consumed by IT. Copy some of the
5010 remaining overlay strings to IT->overlay_strings. */
5011 i = 0;
5012 j = it->current.overlay_string_index;
5013 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
5014 it->overlay_strings[i++] = entries[j++].string;
5015
5016 CHECK_IT (it);
5017 }
5018
5019
5020 /* Get the first chunk of overlay strings at IT's current buffer
5021 position, or at CHARPOS if that is > 0. Value is non-zero if at
5022 least one overlay string was found. */
5023
5024 static int
5025 get_overlay_strings_1 (it, charpos, compute_stop_p)
5026 struct it *it;
5027 int charpos;
5028 {
5029 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
5030 process. This fills IT->overlay_strings with strings, and sets
5031 IT->n_overlay_strings to the total number of strings to process.
5032 IT->pos.overlay_string_index has to be set temporarily to zero
5033 because load_overlay_strings needs this; it must be set to -1
5034 when no overlay strings are found because a zero value would
5035 indicate a position in the first overlay string. */
5036 it->current.overlay_string_index = 0;
5037 load_overlay_strings (it, charpos);
5038
5039 /* If we found overlay strings, set up IT to deliver display
5040 elements from the first one. Otherwise set up IT to deliver
5041 from current_buffer. */
5042 if (it->n_overlay_strings)
5043 {
5044 /* Make sure we know settings in current_buffer, so that we can
5045 restore meaningful values when we're done with the overlay
5046 strings. */
5047 if (compute_stop_p)
5048 compute_stop_pos (it);
5049 xassert (it->face_id >= 0);
5050
5051 /* Save IT's settings. They are restored after all overlay
5052 strings have been processed. */
5053 xassert (!compute_stop_p || it->sp == 0);
5054 push_it (it);
5055
5056 /* Set up IT to deliver display elements from the first overlay
5057 string. */
5058 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5059 it->string = it->overlay_strings[0];
5060 it->stop_charpos = 0;
5061 xassert (STRINGP (it->string));
5062 it->end_charpos = SCHARS (it->string);
5063 it->multibyte_p = STRING_MULTIBYTE (it->string);
5064 it->method = GET_FROM_STRING;
5065 return 1;
5066 }
5067
5068 it->current.overlay_string_index = -1;
5069 return 0;
5070 }
5071
5072 static int
5073 get_overlay_strings (it, charpos)
5074 struct it *it;
5075 int charpos;
5076 {
5077 it->string = Qnil;
5078 it->method = GET_FROM_BUFFER;
5079
5080 (void) get_overlay_strings_1 (it, charpos, 1);
5081
5082 CHECK_IT (it);
5083
5084 /* Value is non-zero if we found at least one overlay string. */
5085 return STRINGP (it->string);
5086 }
5087
5088
5089 \f
5090 /***********************************************************************
5091 Saving and restoring state
5092 ***********************************************************************/
5093
5094 /* Save current settings of IT on IT->stack. Called, for example,
5095 before setting up IT for an overlay string, to be able to restore
5096 IT's settings to what they were after the overlay string has been
5097 processed. */
5098
5099 static void
5100 push_it (it)
5101 struct it *it;
5102 {
5103 struct iterator_stack_entry *p;
5104
5105 xassert (it->sp < IT_STACK_SIZE);
5106 p = it->stack + it->sp;
5107
5108 p->stop_charpos = it->stop_charpos;
5109 xassert (it->face_id >= 0);
5110 p->face_id = it->face_id;
5111 p->string = it->string;
5112 p->method = it->method;
5113 switch (p->method)
5114 {
5115 case GET_FROM_IMAGE:
5116 p->u.image.object = it->object;
5117 p->u.image.image_id = it->image_id;
5118 p->u.image.slice = it->slice;
5119 break;
5120 case GET_FROM_COMPOSITION:
5121 p->u.comp.object = it->object;
5122 p->u.comp.c = it->c;
5123 p->u.comp.len = it->len;
5124 p->u.comp.cmp_id = it->cmp_id;
5125 p->u.comp.cmp_len = it->cmp_len;
5126 break;
5127 case GET_FROM_STRETCH:
5128 p->u.stretch.object = it->object;
5129 break;
5130 }
5131 p->position = it->position;
5132 p->current = it->current;
5133 p->end_charpos = it->end_charpos;
5134 p->string_nchars = it->string_nchars;
5135 p->area = it->area;
5136 p->multibyte_p = it->multibyte_p;
5137 p->space_width = it->space_width;
5138 p->font_height = it->font_height;
5139 p->voffset = it->voffset;
5140 p->string_from_display_prop_p = it->string_from_display_prop_p;
5141 p->display_ellipsis_p = 0;
5142 ++it->sp;
5143 }
5144
5145
5146 /* Restore IT's settings from IT->stack. Called, for example, when no
5147 more overlay strings must be processed, and we return to delivering
5148 display elements from a buffer, or when the end of a string from a
5149 `display' property is reached and we return to delivering display
5150 elements from an overlay string, or from a buffer. */
5151
5152 static void
5153 pop_it (it)
5154 struct it *it;
5155 {
5156 struct iterator_stack_entry *p;
5157
5158 xassert (it->sp > 0);
5159 --it->sp;
5160 p = it->stack + it->sp;
5161 it->stop_charpos = p->stop_charpos;
5162 it->face_id = p->face_id;
5163 it->current = p->current;
5164 it->position = p->position;
5165 it->string = p->string;
5166 if (NILP (it->string))
5167 SET_TEXT_POS (it->current.string_pos, -1, -1);
5168 it->method = p->method;
5169 switch (it->method)
5170 {
5171 case GET_FROM_IMAGE:
5172 it->image_id = p->u.image.image_id;
5173 it->object = p->u.image.object;
5174 it->slice = p->u.image.slice;
5175 break;
5176 case GET_FROM_COMPOSITION:
5177 it->object = p->u.comp.object;
5178 it->c = p->u.comp.c;
5179 it->len = p->u.comp.len;
5180 it->cmp_id = p->u.comp.cmp_id;
5181 it->cmp_len = p->u.comp.cmp_len;
5182 break;
5183 case GET_FROM_STRETCH:
5184 it->object = p->u.comp.object;
5185 break;
5186 case GET_FROM_BUFFER:
5187 it->object = it->w->buffer;
5188 break;
5189 case GET_FROM_STRING:
5190 it->object = it->string;
5191 break;
5192 }
5193 it->end_charpos = p->end_charpos;
5194 it->string_nchars = p->string_nchars;
5195 it->area = p->area;
5196 it->multibyte_p = p->multibyte_p;
5197 it->space_width = p->space_width;
5198 it->font_height = p->font_height;
5199 it->voffset = p->voffset;
5200 it->string_from_display_prop_p = p->string_from_display_prop_p;
5201 }
5202
5203
5204 \f
5205 /***********************************************************************
5206 Moving over lines
5207 ***********************************************************************/
5208
5209 /* Set IT's current position to the previous line start. */
5210
5211 static void
5212 back_to_previous_line_start (it)
5213 struct it *it;
5214 {
5215 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
5216 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
5217 }
5218
5219
5220 /* Move IT to the next line start.
5221
5222 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
5223 we skipped over part of the text (as opposed to moving the iterator
5224 continuously over the text). Otherwise, don't change the value
5225 of *SKIPPED_P.
5226
5227 Newlines may come from buffer text, overlay strings, or strings
5228 displayed via the `display' property. That's the reason we can't
5229 simply use find_next_newline_no_quit.
5230
5231 Note that this function may not skip over invisible text that is so
5232 because of text properties and immediately follows a newline. If
5233 it would, function reseat_at_next_visible_line_start, when called
5234 from set_iterator_to_next, would effectively make invisible
5235 characters following a newline part of the wrong glyph row, which
5236 leads to wrong cursor motion. */
5237
5238 static int
5239 forward_to_next_line_start (it, skipped_p)
5240 struct it *it;
5241 int *skipped_p;
5242 {
5243 int old_selective, newline_found_p, n;
5244 const int MAX_NEWLINE_DISTANCE = 500;
5245
5246 /* If already on a newline, just consume it to avoid unintended
5247 skipping over invisible text below. */
5248 if (it->what == IT_CHARACTER
5249 && it->c == '\n'
5250 && CHARPOS (it->position) == IT_CHARPOS (*it))
5251 {
5252 set_iterator_to_next (it, 0);
5253 it->c = 0;
5254 return 1;
5255 }
5256
5257 /* Don't handle selective display in the following. It's (a)
5258 unnecessary because it's done by the caller, and (b) leads to an
5259 infinite recursion because next_element_from_ellipsis indirectly
5260 calls this function. */
5261 old_selective = it->selective;
5262 it->selective = 0;
5263
5264 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
5265 from buffer text. */
5266 for (n = newline_found_p = 0;
5267 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
5268 n += STRINGP (it->string) ? 0 : 1)
5269 {
5270 if (!get_next_display_element (it))
5271 return 0;
5272 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
5273 set_iterator_to_next (it, 0);
5274 }
5275
5276 /* If we didn't find a newline near enough, see if we can use a
5277 short-cut. */
5278 if (!newline_found_p)
5279 {
5280 int start = IT_CHARPOS (*it);
5281 int limit = find_next_newline_no_quit (start, 1);
5282 Lisp_Object pos;
5283
5284 xassert (!STRINGP (it->string));
5285
5286 /* If there isn't any `display' property in sight, and no
5287 overlays, we can just use the position of the newline in
5288 buffer text. */
5289 if (it->stop_charpos >= limit
5290 || ((pos = Fnext_single_property_change (make_number (start),
5291 Qdisplay,
5292 Qnil, make_number (limit)),
5293 NILP (pos))
5294 && next_overlay_change (start) == ZV))
5295 {
5296 IT_CHARPOS (*it) = limit;
5297 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
5298 *skipped_p = newline_found_p = 1;
5299 }
5300 else
5301 {
5302 while (get_next_display_element (it)
5303 && !newline_found_p)
5304 {
5305 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
5306 set_iterator_to_next (it, 0);
5307 }
5308 }
5309 }
5310
5311 it->selective = old_selective;
5312 return newline_found_p;
5313 }
5314
5315
5316 /* Set IT's current position to the previous visible line start. Skip
5317 invisible text that is so either due to text properties or due to
5318 selective display. Caution: this does not change IT->current_x and
5319 IT->hpos. */
5320
5321 static void
5322 back_to_previous_visible_line_start (it)
5323 struct it *it;
5324 {
5325 while (IT_CHARPOS (*it) > BEGV)
5326 {
5327 back_to_previous_line_start (it);
5328
5329 if (IT_CHARPOS (*it) <= BEGV)
5330 break;
5331
5332 /* If selective > 0, then lines indented more than that values
5333 are invisible. */
5334 if (it->selective > 0
5335 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5336 (double) it->selective)) /* iftc */
5337 continue;
5338
5339 /* Check the newline before point for invisibility. */
5340 {
5341 Lisp_Object prop;
5342 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
5343 Qinvisible, it->window);
5344 if (TEXT_PROP_MEANS_INVISIBLE (prop))
5345 continue;
5346 }
5347
5348 if (IT_CHARPOS (*it) <= BEGV)
5349 break;
5350
5351 {
5352 struct it it2;
5353 int pos;
5354 int beg, end;
5355 Lisp_Object val, overlay;
5356
5357 /* If newline is part of a composition, continue from start of composition */
5358 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
5359 && beg < IT_CHARPOS (*it))
5360 goto replaced;
5361
5362 /* If newline is replaced by a display property, find start of overlay
5363 or interval and continue search from that point. */
5364 it2 = *it;
5365 pos = --IT_CHARPOS (it2);
5366 --IT_BYTEPOS (it2);
5367 it2.sp = 0;
5368 if (handle_display_prop (&it2) == HANDLED_RETURN
5369 && !NILP (val = get_char_property_and_overlay
5370 (make_number (pos), Qdisplay, Qnil, &overlay))
5371 && (OVERLAYP (overlay)
5372 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
5373 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
5374 goto replaced;
5375
5376 /* Newline is not replaced by anything -- so we are done. */
5377 break;
5378
5379 replaced:
5380 if (beg < BEGV)
5381 beg = BEGV;
5382 IT_CHARPOS (*it) = beg;
5383 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
5384 }
5385 }
5386
5387 it->continuation_lines_width = 0;
5388
5389 xassert (IT_CHARPOS (*it) >= BEGV);
5390 xassert (IT_CHARPOS (*it) == BEGV
5391 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5392 CHECK_IT (it);
5393 }
5394
5395
5396 /* Reseat iterator IT at the previous visible line start. Skip
5397 invisible text that is so either due to text properties or due to
5398 selective display. At the end, update IT's overlay information,
5399 face information etc. */
5400
5401 void
5402 reseat_at_previous_visible_line_start (it)
5403 struct it *it;
5404 {
5405 back_to_previous_visible_line_start (it);
5406 reseat (it, it->current.pos, 1);
5407 CHECK_IT (it);
5408 }
5409
5410
5411 /* Reseat iterator IT on the next visible line start in the current
5412 buffer. ON_NEWLINE_P non-zero means position IT on the newline
5413 preceding the line start. Skip over invisible text that is so
5414 because of selective display. Compute faces, overlays etc at the
5415 new position. Note that this function does not skip over text that
5416 is invisible because of text properties. */
5417
5418 static void
5419 reseat_at_next_visible_line_start (it, on_newline_p)
5420 struct it *it;
5421 int on_newline_p;
5422 {
5423 int newline_found_p, skipped_p = 0;
5424
5425 newline_found_p = forward_to_next_line_start (it, &skipped_p);
5426
5427 /* Skip over lines that are invisible because they are indented
5428 more than the value of IT->selective. */
5429 if (it->selective > 0)
5430 while (IT_CHARPOS (*it) < ZV
5431 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5432 (double) it->selective)) /* iftc */
5433 {
5434 xassert (IT_BYTEPOS (*it) == BEGV
5435 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5436 newline_found_p = forward_to_next_line_start (it, &skipped_p);
5437 }
5438
5439 /* Position on the newline if that's what's requested. */
5440 if (on_newline_p && newline_found_p)
5441 {
5442 if (STRINGP (it->string))
5443 {
5444 if (IT_STRING_CHARPOS (*it) > 0)
5445 {
5446 --IT_STRING_CHARPOS (*it);
5447 --IT_STRING_BYTEPOS (*it);
5448 }
5449 }
5450 else if (IT_CHARPOS (*it) > BEGV)
5451 {
5452 --IT_CHARPOS (*it);
5453 --IT_BYTEPOS (*it);
5454 reseat (it, it->current.pos, 0);
5455 }
5456 }
5457 else if (skipped_p)
5458 reseat (it, it->current.pos, 0);
5459
5460 CHECK_IT (it);
5461 }
5462
5463
5464 \f
5465 /***********************************************************************
5466 Changing an iterator's position
5467 ***********************************************************************/
5468
5469 /* Change IT's current position to POS in current_buffer. If FORCE_P
5470 is non-zero, always check for text properties at the new position.
5471 Otherwise, text properties are only looked up if POS >=
5472 IT->check_charpos of a property. */
5473
5474 static void
5475 reseat (it, pos, force_p)
5476 struct it *it;
5477 struct text_pos pos;
5478 int force_p;
5479 {
5480 int original_pos = IT_CHARPOS (*it);
5481
5482 reseat_1 (it, pos, 0);
5483
5484 /* Determine where to check text properties. Avoid doing it
5485 where possible because text property lookup is very expensive. */
5486 if (force_p
5487 || CHARPOS (pos) > it->stop_charpos
5488 || CHARPOS (pos) < original_pos)
5489 handle_stop (it);
5490
5491 CHECK_IT (it);
5492 }
5493
5494
5495 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
5496 IT->stop_pos to POS, also. */
5497
5498 static void
5499 reseat_1 (it, pos, set_stop_p)
5500 struct it *it;
5501 struct text_pos pos;
5502 int set_stop_p;
5503 {
5504 /* Don't call this function when scanning a C string. */
5505 xassert (it->s == NULL);
5506
5507 /* POS must be a reasonable value. */
5508 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
5509
5510 it->current.pos = it->position = pos;
5511 it->end_charpos = ZV;
5512 it->dpvec = NULL;
5513 it->current.dpvec_index = -1;
5514 it->current.overlay_string_index = -1;
5515 IT_STRING_CHARPOS (*it) = -1;
5516 IT_STRING_BYTEPOS (*it) = -1;
5517 it->string = Qnil;
5518 it->method = GET_FROM_BUFFER;
5519 it->object = it->w->buffer;
5520 it->area = TEXT_AREA;
5521 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
5522 it->sp = 0;
5523 it->string_from_display_prop_p = 0;
5524 it->face_before_selective_p = 0;
5525
5526 if (set_stop_p)
5527 it->stop_charpos = CHARPOS (pos);
5528 }
5529
5530
5531 /* Set up IT for displaying a string, starting at CHARPOS in window W.
5532 If S is non-null, it is a C string to iterate over. Otherwise,
5533 STRING gives a Lisp string to iterate over.
5534
5535 If PRECISION > 0, don't return more then PRECISION number of
5536 characters from the string.
5537
5538 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
5539 characters have been returned. FIELD_WIDTH < 0 means an infinite
5540 field width.
5541
5542 MULTIBYTE = 0 means disable processing of multibyte characters,
5543 MULTIBYTE > 0 means enable it,
5544 MULTIBYTE < 0 means use IT->multibyte_p.
5545
5546 IT must be initialized via a prior call to init_iterator before
5547 calling this function. */
5548
5549 static void
5550 reseat_to_string (it, s, string, charpos, precision, field_width, multibyte)
5551 struct it *it;
5552 unsigned char *s;
5553 Lisp_Object string;
5554 int charpos;
5555 int precision, field_width, multibyte;
5556 {
5557 /* No region in strings. */
5558 it->region_beg_charpos = it->region_end_charpos = -1;
5559
5560 /* No text property checks performed by default, but see below. */
5561 it->stop_charpos = -1;
5562
5563 /* Set iterator position and end position. */
5564 bzero (&it->current, sizeof it->current);
5565 it->current.overlay_string_index = -1;
5566 it->current.dpvec_index = -1;
5567 xassert (charpos >= 0);
5568
5569 /* If STRING is specified, use its multibyteness, otherwise use the
5570 setting of MULTIBYTE, if specified. */
5571 if (multibyte >= 0)
5572 it->multibyte_p = multibyte > 0;
5573
5574 if (s == NULL)
5575 {
5576 xassert (STRINGP (string));
5577 it->string = string;
5578 it->s = NULL;
5579 it->end_charpos = it->string_nchars = SCHARS (string);
5580 it->method = GET_FROM_STRING;
5581 it->current.string_pos = string_pos (charpos, string);
5582 }
5583 else
5584 {
5585 it->s = s;
5586 it->string = Qnil;
5587
5588 /* Note that we use IT->current.pos, not it->current.string_pos,
5589 for displaying C strings. */
5590 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
5591 if (it->multibyte_p)
5592 {
5593 it->current.pos = c_string_pos (charpos, s, 1);
5594 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
5595 }
5596 else
5597 {
5598 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
5599 it->end_charpos = it->string_nchars = strlen (s);
5600 }
5601
5602 it->method = GET_FROM_C_STRING;
5603 }
5604
5605 /* PRECISION > 0 means don't return more than PRECISION characters
5606 from the string. */
5607 if (precision > 0 && it->end_charpos - charpos > precision)
5608 it->end_charpos = it->string_nchars = charpos + precision;
5609
5610 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
5611 characters have been returned. FIELD_WIDTH == 0 means don't pad,
5612 FIELD_WIDTH < 0 means infinite field width. This is useful for
5613 padding with `-' at the end of a mode line. */
5614 if (field_width < 0)
5615 field_width = INFINITY;
5616 if (field_width > it->end_charpos - charpos)
5617 it->end_charpos = charpos + field_width;
5618
5619 /* Use the standard display table for displaying strings. */
5620 if (DISP_TABLE_P (Vstandard_display_table))
5621 it->dp = XCHAR_TABLE (Vstandard_display_table);
5622
5623 it->stop_charpos = charpos;
5624 CHECK_IT (it);
5625 }
5626
5627
5628 \f
5629 /***********************************************************************
5630 Iteration
5631 ***********************************************************************/
5632
5633 /* Map enum it_method value to corresponding next_element_from_* function. */
5634
5635 static int (* get_next_element[NUM_IT_METHODS]) P_ ((struct it *it)) =
5636 {
5637 next_element_from_buffer,
5638 next_element_from_display_vector,
5639 next_element_from_composition,
5640 next_element_from_string,
5641 next_element_from_c_string,
5642 next_element_from_image,
5643 next_element_from_stretch
5644 };
5645
5646
5647 /* Load IT's display element fields with information about the next
5648 display element from the current position of IT. Value is zero if
5649 end of buffer (or C string) is reached. */
5650
5651 static struct frame *last_escape_glyph_frame = NULL;
5652 static unsigned last_escape_glyph_face_id = (1 << FACE_ID_BITS);
5653 static int last_escape_glyph_merged_face_id = 0;
5654
5655 int
5656 get_next_display_element (it)
5657 struct it *it;
5658 {
5659 /* Non-zero means that we found a display element. Zero means that
5660 we hit the end of what we iterate over. Performance note: the
5661 function pointer `method' used here turns out to be faster than
5662 using a sequence of if-statements. */
5663 int success_p;
5664
5665 get_next:
5666 success_p = (*get_next_element[it->method]) (it);
5667
5668 if (it->what == IT_CHARACTER)
5669 {
5670 /* Map via display table or translate control characters.
5671 IT->c, IT->len etc. have been set to the next character by
5672 the function call above. If we have a display table, and it
5673 contains an entry for IT->c, translate it. Don't do this if
5674 IT->c itself comes from a display table, otherwise we could
5675 end up in an infinite recursion. (An alternative could be to
5676 count the recursion depth of this function and signal an
5677 error when a certain maximum depth is reached.) Is it worth
5678 it? */
5679 if (success_p && it->dpvec == NULL)
5680 {
5681 Lisp_Object dv;
5682
5683 if (it->dp
5684 && (dv = DISP_CHAR_VECTOR (it->dp, it->c),
5685 VECTORP (dv)))
5686 {
5687 struct Lisp_Vector *v = XVECTOR (dv);
5688
5689 /* Return the first character from the display table
5690 entry, if not empty. If empty, don't display the
5691 current character. */
5692 if (v->size)
5693 {
5694 it->dpvec_char_len = it->len;
5695 it->dpvec = v->contents;
5696 it->dpend = v->contents + v->size;
5697 it->current.dpvec_index = 0;
5698 it->dpvec_face_id = -1;
5699 it->saved_face_id = it->face_id;
5700 it->method = GET_FROM_DISPLAY_VECTOR;
5701 it->ellipsis_p = 0;
5702 }
5703 else
5704 {
5705 set_iterator_to_next (it, 0);
5706 }
5707 goto get_next;
5708 }
5709
5710 /* Translate control characters into `\003' or `^C' form.
5711 Control characters coming from a display table entry are
5712 currently not translated because we use IT->dpvec to hold
5713 the translation. This could easily be changed but I
5714 don't believe that it is worth doing.
5715
5716 If it->multibyte_p is nonzero, non-printable non-ASCII
5717 characters are also translated to octal form.
5718
5719 If it->multibyte_p is zero, eight-bit characters that
5720 don't have corresponding multibyte char code are also
5721 translated to octal form. */
5722 else if ((it->c < ' '
5723 ? (it->area != TEXT_AREA
5724 /* In mode line, treat \n, \t like other crl chars. */
5725 || (it->c != '\t'
5726 && it->glyph_row && it->glyph_row->mode_line_p)
5727 || (it->c != '\n' && it->c != '\t'))
5728 : (it->multibyte_p
5729 ? (!CHAR_PRINTABLE_P (it->c)
5730 || (!NILP (Vnobreak_char_display)
5731 && (it->c == 0xA0 /* NO-BREAK SPACE */
5732 || it->c == 0xAD /* SOFT HYPHEN */)))
5733 : (it->c >= 127
5734 && (! unibyte_display_via_language_environment
5735 || (UNIBYTE_CHAR_HAS_MULTIBYTE_P (it->c)))))))
5736 {
5737 /* IT->c is a control character which must be displayed
5738 either as '\003' or as `^C' where the '\\' and '^'
5739 can be defined in the display table. Fill
5740 IT->ctl_chars with glyphs for what we have to
5741 display. Then, set IT->dpvec to these glyphs. */
5742 GLYPH g;
5743 int ctl_len;
5744 int face_id, lface_id = 0 ;
5745 GLYPH escape_glyph;
5746
5747 /* Handle control characters with ^. */
5748
5749 if (it->c < 128 && it->ctl_arrow_p)
5750 {
5751 g = '^'; /* default glyph for Control */
5752 /* Set IT->ctl_chars[0] to the glyph for `^'. */
5753 if (it->dp
5754 && INTEGERP (DISP_CTRL_GLYPH (it->dp))
5755 && GLYPH_CHAR_VALID_P (XINT (DISP_CTRL_GLYPH (it->dp))))
5756 {
5757 g = XINT (DISP_CTRL_GLYPH (it->dp));
5758 lface_id = FAST_GLYPH_FACE (g);
5759 }
5760 if (lface_id)
5761 {
5762 g = FAST_GLYPH_CHAR (g);
5763 face_id = merge_faces (it->f, Qt, lface_id,
5764 it->face_id);
5765 }
5766 else if (it->f == last_escape_glyph_frame
5767 && it->face_id == last_escape_glyph_face_id)
5768 {
5769 face_id = last_escape_glyph_merged_face_id;
5770 }
5771 else
5772 {
5773 /* Merge the escape-glyph face into the current face. */
5774 face_id = merge_faces (it->f, Qescape_glyph, 0,
5775 it->face_id);
5776 last_escape_glyph_frame = it->f;
5777 last_escape_glyph_face_id = it->face_id;
5778 last_escape_glyph_merged_face_id = face_id;
5779 }
5780
5781 XSETINT (it->ctl_chars[0], g);
5782 g = it->c ^ 0100;
5783 XSETINT (it->ctl_chars[1], g);
5784 ctl_len = 2;
5785 goto display_control;
5786 }
5787
5788 /* Handle non-break space in the mode where it only gets
5789 highlighting. */
5790
5791 if (EQ (Vnobreak_char_display, Qt)
5792 && it->c == 0xA0)
5793 {
5794 /* Merge the no-break-space face into the current face. */
5795 face_id = merge_faces (it->f, Qnobreak_space, 0,
5796 it->face_id);
5797
5798 g = it->c = ' ';
5799 XSETINT (it->ctl_chars[0], g);
5800 ctl_len = 1;
5801 goto display_control;
5802 }
5803
5804 /* Handle sequences that start with the "escape glyph". */
5805
5806 /* the default escape glyph is \. */
5807 escape_glyph = '\\';
5808
5809 if (it->dp
5810 && INTEGERP (DISP_ESCAPE_GLYPH (it->dp))
5811 && GLYPH_CHAR_VALID_P (XFASTINT (DISP_ESCAPE_GLYPH (it->dp))))
5812 {
5813 escape_glyph = XFASTINT (DISP_ESCAPE_GLYPH (it->dp));
5814 lface_id = FAST_GLYPH_FACE (escape_glyph);
5815 }
5816 if (lface_id)
5817 {
5818 /* The display table specified a face.
5819 Merge it into face_id and also into escape_glyph. */
5820 escape_glyph = FAST_GLYPH_CHAR (escape_glyph);
5821 face_id = merge_faces (it->f, Qt, lface_id,
5822 it->face_id);
5823 }
5824 else if (it->f == last_escape_glyph_frame
5825 && it->face_id == last_escape_glyph_face_id)
5826 {
5827 face_id = last_escape_glyph_merged_face_id;
5828 }
5829 else
5830 {
5831 /* Merge the escape-glyph face into the current face. */
5832 face_id = merge_faces (it->f, Qescape_glyph, 0,
5833 it->face_id);
5834 last_escape_glyph_frame = it->f;
5835 last_escape_glyph_face_id = it->face_id;
5836 last_escape_glyph_merged_face_id = face_id;
5837 }
5838
5839 /* Handle soft hyphens in the mode where they only get
5840 highlighting. */
5841
5842 if (EQ (Vnobreak_char_display, Qt)
5843 && it->c == 0xAD)
5844 {
5845 g = it->c = '-';
5846 XSETINT (it->ctl_chars[0], g);
5847 ctl_len = 1;
5848 goto display_control;
5849 }
5850
5851 /* Handle non-break space and soft hyphen
5852 with the escape glyph. */
5853
5854 if (it->c == 0xA0 || it->c == 0xAD)
5855 {
5856 XSETINT (it->ctl_chars[0], escape_glyph);
5857 g = it->c = (it->c == 0xA0 ? ' ' : '-');
5858 XSETINT (it->ctl_chars[1], g);
5859 ctl_len = 2;
5860 goto display_control;
5861 }
5862
5863 {
5864 unsigned char str[MAX_MULTIBYTE_LENGTH];
5865 int len;
5866 int i;
5867
5868 /* Set IT->ctl_chars[0] to the glyph for `\\'. */
5869 if (CHAR_BYTE8_P (it->c))
5870 {
5871 str[0] = CHAR_TO_BYTE8 (it->c);
5872 len = 1;
5873 }
5874 else if (it->c < 256)
5875 {
5876 str[0] = it->c;
5877 len = 1;
5878 }
5879 else
5880 {
5881 /* It's an invalid character, which shouldn't
5882 happen actually, but due to bugs it may
5883 happen. Let's print the char as is, there's
5884 not much meaningful we can do with it. */
5885 str[0] = it->c;
5886 str[1] = it->c >> 8;
5887 str[2] = it->c >> 16;
5888 str[3] = it->c >> 24;
5889 len = 4;
5890 }
5891
5892 for (i = 0; i < len; i++)
5893 {
5894 XSETINT (it->ctl_chars[i * 4], escape_glyph);
5895 /* Insert three more glyphs into IT->ctl_chars for
5896 the octal display of the character. */
5897 g = ((str[i] >> 6) & 7) + '0';
5898 XSETINT (it->ctl_chars[i * 4 + 1], g);
5899 g = ((str[i] >> 3) & 7) + '0';
5900 XSETINT (it->ctl_chars[i * 4 + 2], g);
5901 g = (str[i] & 7) + '0';
5902 XSETINT (it->ctl_chars[i * 4 + 3], g);
5903 }
5904 ctl_len = len * 4;
5905 }
5906
5907 display_control:
5908 /* Set up IT->dpvec and return first character from it. */
5909 it->dpvec_char_len = it->len;
5910 it->dpvec = it->ctl_chars;
5911 it->dpend = it->dpvec + ctl_len;
5912 it->current.dpvec_index = 0;
5913 it->dpvec_face_id = face_id;
5914 it->saved_face_id = it->face_id;
5915 it->method = GET_FROM_DISPLAY_VECTOR;
5916 it->ellipsis_p = 0;
5917 goto get_next;
5918 }
5919 }
5920 }
5921
5922 /* Adjust face id for a multibyte character. There are no multibyte
5923 character in unibyte text. */
5924 if ((it->what == IT_CHARACTER || it->what == IT_COMPOSITION)
5925 && it->multibyte_p
5926 && success_p
5927 && FRAME_WINDOW_P (it->f))
5928 {
5929 struct face *face = FACE_FROM_ID (it->f, it->face_id);
5930 int pos = (it->s ? -1
5931 : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
5932 : IT_CHARPOS (*it));
5933
5934 it->face_id = FACE_FOR_CHAR (it->f, face, it->c, pos, it->string);
5935 }
5936
5937 /* Is this character the last one of a run of characters with
5938 box? If yes, set IT->end_of_box_run_p to 1. */
5939 if (it->face_box_p
5940 && it->s == NULL)
5941 {
5942 int face_id;
5943 struct face *face;
5944
5945 it->end_of_box_run_p
5946 = ((face_id = face_after_it_pos (it),
5947 face_id != it->face_id)
5948 && (face = FACE_FROM_ID (it->f, face_id),
5949 face->box == FACE_NO_BOX));
5950 }
5951
5952 /* Value is 0 if end of buffer or string reached. */
5953 return success_p;
5954 }
5955
5956
5957 /* Move IT to the next display element.
5958
5959 RESEAT_P non-zero means if called on a newline in buffer text,
5960 skip to the next visible line start.
5961
5962 Functions get_next_display_element and set_iterator_to_next are
5963 separate because I find this arrangement easier to handle than a
5964 get_next_display_element function that also increments IT's
5965 position. The way it is we can first look at an iterator's current
5966 display element, decide whether it fits on a line, and if it does,
5967 increment the iterator position. The other way around we probably
5968 would either need a flag indicating whether the iterator has to be
5969 incremented the next time, or we would have to implement a
5970 decrement position function which would not be easy to write. */
5971
5972 void
5973 set_iterator_to_next (it, reseat_p)
5974 struct it *it;
5975 int reseat_p;
5976 {
5977 /* Reset flags indicating start and end of a sequence of characters
5978 with box. Reset them at the start of this function because
5979 moving the iterator to a new position might set them. */
5980 it->start_of_box_run_p = it->end_of_box_run_p = 0;
5981
5982 switch (it->method)
5983 {
5984 case GET_FROM_BUFFER:
5985 /* The current display element of IT is a character from
5986 current_buffer. Advance in the buffer, and maybe skip over
5987 invisible lines that are so because of selective display. */
5988 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
5989 reseat_at_next_visible_line_start (it, 0);
5990 else
5991 {
5992 xassert (it->len != 0);
5993 IT_BYTEPOS (*it) += it->len;
5994 IT_CHARPOS (*it) += 1;
5995 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
5996 }
5997 break;
5998
5999 case GET_FROM_COMPOSITION:
6000 xassert (it->cmp_id >= 0 && it->cmp_id < n_compositions);
6001 xassert (it->sp > 0);
6002 pop_it (it);
6003 if (it->method == GET_FROM_STRING)
6004 {
6005 IT_STRING_BYTEPOS (*it) += it->len;
6006 IT_STRING_CHARPOS (*it) += it->cmp_len;
6007 goto consider_string_end;
6008 }
6009 else if (it->method == GET_FROM_BUFFER)
6010 {
6011 IT_BYTEPOS (*it) += it->len;
6012 IT_CHARPOS (*it) += it->cmp_len;
6013 }
6014 break;
6015
6016 case GET_FROM_C_STRING:
6017 /* Current display element of IT is from a C string. */
6018 IT_BYTEPOS (*it) += it->len;
6019 IT_CHARPOS (*it) += 1;
6020 break;
6021
6022 case GET_FROM_DISPLAY_VECTOR:
6023 /* Current display element of IT is from a display table entry.
6024 Advance in the display table definition. Reset it to null if
6025 end reached, and continue with characters from buffers/
6026 strings. */
6027 ++it->current.dpvec_index;
6028
6029 /* Restore face of the iterator to what they were before the
6030 display vector entry (these entries may contain faces). */
6031 it->face_id = it->saved_face_id;
6032
6033 if (it->dpvec + it->current.dpvec_index == it->dpend)
6034 {
6035 int recheck_faces = it->ellipsis_p;
6036
6037 if (it->s)
6038 it->method = GET_FROM_C_STRING;
6039 else if (STRINGP (it->string))
6040 it->method = GET_FROM_STRING;
6041 else
6042 {
6043 it->method = GET_FROM_BUFFER;
6044 it->object = it->w->buffer;
6045 }
6046
6047 it->dpvec = NULL;
6048 it->current.dpvec_index = -1;
6049
6050 /* Skip over characters which were displayed via IT->dpvec. */
6051 if (it->dpvec_char_len < 0)
6052 reseat_at_next_visible_line_start (it, 1);
6053 else if (it->dpvec_char_len > 0)
6054 {
6055 if (it->method == GET_FROM_STRING
6056 && it->n_overlay_strings > 0)
6057 it->ignore_overlay_strings_at_pos_p = 1;
6058 it->len = it->dpvec_char_len;
6059 set_iterator_to_next (it, reseat_p);
6060 }
6061
6062 /* Maybe recheck faces after display vector */
6063 if (recheck_faces)
6064 it->stop_charpos = IT_CHARPOS (*it);
6065 }
6066 break;
6067
6068 case GET_FROM_STRING:
6069 /* Current display element is a character from a Lisp string. */
6070 xassert (it->s == NULL && STRINGP (it->string));
6071 IT_STRING_BYTEPOS (*it) += it->len;
6072 IT_STRING_CHARPOS (*it) += 1;
6073
6074 consider_string_end:
6075
6076 if (it->current.overlay_string_index >= 0)
6077 {
6078 /* IT->string is an overlay string. Advance to the
6079 next, if there is one. */
6080 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
6081 next_overlay_string (it);
6082 }
6083 else
6084 {
6085 /* IT->string is not an overlay string. If we reached
6086 its end, and there is something on IT->stack, proceed
6087 with what is on the stack. This can be either another
6088 string, this time an overlay string, or a buffer. */
6089 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
6090 && it->sp > 0)
6091 {
6092 pop_it (it);
6093 if (it->method == GET_FROM_STRING)
6094 goto consider_string_end;
6095 }
6096 }
6097 break;
6098
6099 case GET_FROM_IMAGE:
6100 case GET_FROM_STRETCH:
6101 /* The position etc with which we have to proceed are on
6102 the stack. The position may be at the end of a string,
6103 if the `display' property takes up the whole string. */
6104 xassert (it->sp > 0);
6105 pop_it (it);
6106 if (it->method == GET_FROM_STRING)
6107 goto consider_string_end;
6108 break;
6109
6110 default:
6111 /* There are no other methods defined, so this should be a bug. */
6112 abort ();
6113 }
6114
6115 xassert (it->method != GET_FROM_STRING
6116 || (STRINGP (it->string)
6117 && IT_STRING_CHARPOS (*it) >= 0));
6118 }
6119
6120 /* Load IT's display element fields with information about the next
6121 display element which comes from a display table entry or from the
6122 result of translating a control character to one of the forms `^C'
6123 or `\003'.
6124
6125 IT->dpvec holds the glyphs to return as characters.
6126 IT->saved_face_id holds the face id before the display vector--
6127 it is restored into IT->face_idin set_iterator_to_next. */
6128
6129 static int
6130 next_element_from_display_vector (it)
6131 struct it *it;
6132 {
6133 /* Precondition. */
6134 xassert (it->dpvec && it->current.dpvec_index >= 0);
6135
6136 it->face_id = it->saved_face_id;
6137
6138 if (INTEGERP (*it->dpvec)
6139 && GLYPH_CHAR_VALID_P (XFASTINT (*it->dpvec)))
6140 {
6141 GLYPH g;
6142
6143 g = XFASTINT (it->dpvec[it->current.dpvec_index]);
6144 it->c = FAST_GLYPH_CHAR (g);
6145 it->len = CHAR_BYTES (it->c);
6146
6147 /* The entry may contain a face id to use. Such a face id is
6148 the id of a Lisp face, not a realized face. A face id of
6149 zero means no face is specified. */
6150 if (it->dpvec_face_id >= 0)
6151 it->face_id = it->dpvec_face_id;
6152 else
6153 {
6154 int lface_id = FAST_GLYPH_FACE (g);
6155 if (lface_id > 0)
6156 it->face_id = merge_faces (it->f, Qt, lface_id,
6157 it->saved_face_id);
6158 }
6159 }
6160 else
6161 /* Display table entry is invalid. Return a space. */
6162 it->c = ' ', it->len = 1;
6163
6164 /* Don't change position and object of the iterator here. They are
6165 still the values of the character that had this display table
6166 entry or was translated, and that's what we want. */
6167 it->what = IT_CHARACTER;
6168 return 1;
6169 }
6170
6171
6172 /* Load IT with the next display element from Lisp string IT->string.
6173 IT->current.string_pos is the current position within the string.
6174 If IT->current.overlay_string_index >= 0, the Lisp string is an
6175 overlay string. */
6176
6177 static int
6178 next_element_from_string (it)
6179 struct it *it;
6180 {
6181 struct text_pos position;
6182
6183 xassert (STRINGP (it->string));
6184 xassert (IT_STRING_CHARPOS (*it) >= 0);
6185 position = it->current.string_pos;
6186
6187 /* Time to check for invisible text? */
6188 if (IT_STRING_CHARPOS (*it) < it->end_charpos
6189 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
6190 {
6191 handle_stop (it);
6192
6193 /* Since a handler may have changed IT->method, we must
6194 recurse here. */
6195 return get_next_display_element (it);
6196 }
6197
6198 if (it->current.overlay_string_index >= 0)
6199 {
6200 /* Get the next character from an overlay string. In overlay
6201 strings, There is no field width or padding with spaces to
6202 do. */
6203 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
6204 {
6205 it->what = IT_EOB;
6206 return 0;
6207 }
6208 else if (STRING_MULTIBYTE (it->string))
6209 {
6210 int remaining = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
6211 const unsigned char *s = (SDATA (it->string)
6212 + IT_STRING_BYTEPOS (*it));
6213 it->c = string_char_and_length (s, remaining, &it->len);
6214 }
6215 else
6216 {
6217 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
6218 it->len = 1;
6219 }
6220 }
6221 else
6222 {
6223 /* Get the next character from a Lisp string that is not an
6224 overlay string. Such strings come from the mode line, for
6225 example. We may have to pad with spaces, or truncate the
6226 string. See also next_element_from_c_string. */
6227 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
6228 {
6229 it->what = IT_EOB;
6230 return 0;
6231 }
6232 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
6233 {
6234 /* Pad with spaces. */
6235 it->c = ' ', it->len = 1;
6236 CHARPOS (position) = BYTEPOS (position) = -1;
6237 }
6238 else if (STRING_MULTIBYTE (it->string))
6239 {
6240 int maxlen = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
6241 const unsigned char *s = (SDATA (it->string)
6242 + IT_STRING_BYTEPOS (*it));
6243 it->c = string_char_and_length (s, maxlen, &it->len);
6244 }
6245 else
6246 {
6247 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
6248 it->len = 1;
6249 }
6250 }
6251
6252 /* Record what we have and where it came from. */
6253 it->what = IT_CHARACTER;
6254 it->object = it->string;
6255 it->position = position;
6256 return 1;
6257 }
6258
6259
6260 /* Load IT with next display element from C string IT->s.
6261 IT->string_nchars is the maximum number of characters to return
6262 from the string. IT->end_charpos may be greater than
6263 IT->string_nchars when this function is called, in which case we
6264 may have to return padding spaces. Value is zero if end of string
6265 reached, including padding spaces. */
6266
6267 static int
6268 next_element_from_c_string (it)
6269 struct it *it;
6270 {
6271 int success_p = 1;
6272
6273 xassert (it->s);
6274 it->what = IT_CHARACTER;
6275 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
6276 it->object = Qnil;
6277
6278 /* IT's position can be greater IT->string_nchars in case a field
6279 width or precision has been specified when the iterator was
6280 initialized. */
6281 if (IT_CHARPOS (*it) >= it->end_charpos)
6282 {
6283 /* End of the game. */
6284 it->what = IT_EOB;
6285 success_p = 0;
6286 }
6287 else if (IT_CHARPOS (*it) >= it->string_nchars)
6288 {
6289 /* Pad with spaces. */
6290 it->c = ' ', it->len = 1;
6291 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
6292 }
6293 else if (it->multibyte_p)
6294 {
6295 /* Implementation note: The calls to strlen apparently aren't a
6296 performance problem because there is no noticeable performance
6297 difference between Emacs running in unibyte or multibyte mode. */
6298 int maxlen = strlen (it->s) - IT_BYTEPOS (*it);
6299 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it),
6300 maxlen, &it->len);
6301 }
6302 else
6303 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
6304
6305 return success_p;
6306 }
6307
6308
6309 /* Set up IT to return characters from an ellipsis, if appropriate.
6310 The definition of the ellipsis glyphs may come from a display table
6311 entry. This function Fills IT with the first glyph from the
6312 ellipsis if an ellipsis is to be displayed. */
6313
6314 static int
6315 next_element_from_ellipsis (it)
6316 struct it *it;
6317 {
6318 if (it->selective_display_ellipsis_p)
6319 setup_for_ellipsis (it, it->len);
6320 else
6321 {
6322 /* The face at the current position may be different from the
6323 face we find after the invisible text. Remember what it
6324 was in IT->saved_face_id, and signal that it's there by
6325 setting face_before_selective_p. */
6326 it->saved_face_id = it->face_id;
6327 it->method = GET_FROM_BUFFER;
6328 it->object = it->w->buffer;
6329 reseat_at_next_visible_line_start (it, 1);
6330 it->face_before_selective_p = 1;
6331 }
6332
6333 return get_next_display_element (it);
6334 }
6335
6336
6337 /* Deliver an image display element. The iterator IT is already
6338 filled with image information (done in handle_display_prop). Value
6339 is always 1. */
6340
6341
6342 static int
6343 next_element_from_image (it)
6344 struct it *it;
6345 {
6346 it->what = IT_IMAGE;
6347 return 1;
6348 }
6349
6350
6351 /* Fill iterator IT with next display element from a stretch glyph
6352 property. IT->object is the value of the text property. Value is
6353 always 1. */
6354
6355 static int
6356 next_element_from_stretch (it)
6357 struct it *it;
6358 {
6359 it->what = IT_STRETCH;
6360 return 1;
6361 }
6362
6363
6364 /* Load IT with the next display element from current_buffer. Value
6365 is zero if end of buffer reached. IT->stop_charpos is the next
6366 position at which to stop and check for text properties or buffer
6367 end. */
6368
6369 static int
6370 next_element_from_buffer (it)
6371 struct it *it;
6372 {
6373 int success_p = 1;
6374
6375 /* Check this assumption, otherwise, we would never enter the
6376 if-statement, below. */
6377 xassert (IT_CHARPOS (*it) >= BEGV
6378 && IT_CHARPOS (*it) <= it->stop_charpos);
6379
6380 if (IT_CHARPOS (*it) >= it->stop_charpos)
6381 {
6382 if (IT_CHARPOS (*it) >= it->end_charpos)
6383 {
6384 int overlay_strings_follow_p;
6385
6386 /* End of the game, except when overlay strings follow that
6387 haven't been returned yet. */
6388 if (it->overlay_strings_at_end_processed_p)
6389 overlay_strings_follow_p = 0;
6390 else
6391 {
6392 it->overlay_strings_at_end_processed_p = 1;
6393 overlay_strings_follow_p = get_overlay_strings (it, 0);
6394 }
6395
6396 if (overlay_strings_follow_p)
6397 success_p = get_next_display_element (it);
6398 else
6399 {
6400 it->what = IT_EOB;
6401 it->position = it->current.pos;
6402 success_p = 0;
6403 }
6404 }
6405 else
6406 {
6407 handle_stop (it);
6408 return get_next_display_element (it);
6409 }
6410 }
6411 else
6412 {
6413 /* No face changes, overlays etc. in sight, so just return a
6414 character from current_buffer. */
6415 unsigned char *p;
6416
6417 /* Maybe run the redisplay end trigger hook. Performance note:
6418 This doesn't seem to cost measurable time. */
6419 if (it->redisplay_end_trigger_charpos
6420 && it->glyph_row
6421 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
6422 run_redisplay_end_trigger_hook (it);
6423
6424 /* Get the next character, maybe multibyte. */
6425 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
6426 if (it->multibyte_p && !ASCII_BYTE_P (*p))
6427 {
6428 int maxlen = ((IT_BYTEPOS (*it) >= GPT_BYTE ? ZV_BYTE : GPT_BYTE)
6429 - IT_BYTEPOS (*it));
6430 it->c = string_char_and_length (p, maxlen, &it->len);
6431 }
6432 else
6433 it->c = *p, it->len = 1;
6434
6435 /* Record what we have and where it came from. */
6436 it->what = IT_CHARACTER;
6437 it->object = it->w->buffer;
6438 it->position = it->current.pos;
6439
6440 /* Normally we return the character found above, except when we
6441 really want to return an ellipsis for selective display. */
6442 if (it->selective)
6443 {
6444 if (it->c == '\n')
6445 {
6446 /* A value of selective > 0 means hide lines indented more
6447 than that number of columns. */
6448 if (it->selective > 0
6449 && IT_CHARPOS (*it) + 1 < ZV
6450 && indented_beyond_p (IT_CHARPOS (*it) + 1,
6451 IT_BYTEPOS (*it) + 1,
6452 (double) it->selective)) /* iftc */
6453 {
6454 success_p = next_element_from_ellipsis (it);
6455 it->dpvec_char_len = -1;
6456 }
6457 }
6458 else if (it->c == '\r' && it->selective == -1)
6459 {
6460 /* A value of selective == -1 means that everything from the
6461 CR to the end of the line is invisible, with maybe an
6462 ellipsis displayed for it. */
6463 success_p = next_element_from_ellipsis (it);
6464 it->dpvec_char_len = -1;
6465 }
6466 }
6467 }
6468
6469 /* Value is zero if end of buffer reached. */
6470 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
6471 return success_p;
6472 }
6473
6474
6475 /* Run the redisplay end trigger hook for IT. */
6476
6477 static void
6478 run_redisplay_end_trigger_hook (it)
6479 struct it *it;
6480 {
6481 Lisp_Object args[3];
6482
6483 /* IT->glyph_row should be non-null, i.e. we should be actually
6484 displaying something, or otherwise we should not run the hook. */
6485 xassert (it->glyph_row);
6486
6487 /* Set up hook arguments. */
6488 args[0] = Qredisplay_end_trigger_functions;
6489 args[1] = it->window;
6490 XSETINT (args[2], it->redisplay_end_trigger_charpos);
6491 it->redisplay_end_trigger_charpos = 0;
6492
6493 /* Since we are *trying* to run these functions, don't try to run
6494 them again, even if they get an error. */
6495 it->w->redisplay_end_trigger = Qnil;
6496 Frun_hook_with_args (3, args);
6497
6498 /* Notice if it changed the face of the character we are on. */
6499 handle_face_prop (it);
6500 }
6501
6502
6503 /* Deliver a composition display element. The iterator IT is already
6504 filled with composition information (done in
6505 handle_composition_prop). Value is always 1. */
6506
6507 static int
6508 next_element_from_composition (it)
6509 struct it *it;
6510 {
6511 it->what = IT_COMPOSITION;
6512 it->position = (STRINGP (it->string)
6513 ? it->current.string_pos
6514 : it->current.pos);
6515 if (STRINGP (it->string))
6516 it->object = it->string;
6517 else
6518 it->object = it->w->buffer;
6519 return 1;
6520 }
6521
6522
6523 \f
6524 /***********************************************************************
6525 Moving an iterator without producing glyphs
6526 ***********************************************************************/
6527
6528 /* Check if iterator is at a position corresponding to a valid buffer
6529 position after some move_it_ call. */
6530
6531 #define IT_POS_VALID_AFTER_MOVE_P(it) \
6532 ((it)->method == GET_FROM_STRING \
6533 ? IT_STRING_CHARPOS (*it) == 0 \
6534 : 1)
6535
6536
6537 /* Move iterator IT to a specified buffer or X position within one
6538 line on the display without producing glyphs.
6539
6540 OP should be a bit mask including some or all of these bits:
6541 MOVE_TO_X: Stop on reaching x-position TO_X.
6542 MOVE_TO_POS: Stop on reaching buffer or string position TO_CHARPOS.
6543 Regardless of OP's value, stop in reaching the end of the display line.
6544
6545 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
6546 This means, in particular, that TO_X includes window's horizontal
6547 scroll amount.
6548
6549 The return value has several possible values that
6550 say what condition caused the scan to stop:
6551
6552 MOVE_POS_MATCH_OR_ZV
6553 - when TO_POS or ZV was reached.
6554
6555 MOVE_X_REACHED
6556 -when TO_X was reached before TO_POS or ZV were reached.
6557
6558 MOVE_LINE_CONTINUED
6559 - when we reached the end of the display area and the line must
6560 be continued.
6561
6562 MOVE_LINE_TRUNCATED
6563 - when we reached the end of the display area and the line is
6564 truncated.
6565
6566 MOVE_NEWLINE_OR_CR
6567 - when we stopped at a line end, i.e. a newline or a CR and selective
6568 display is on. */
6569
6570 static enum move_it_result
6571 move_it_in_display_line_to (it, to_charpos, to_x, op)
6572 struct it *it;
6573 int to_charpos, to_x, op;
6574 {
6575 enum move_it_result result = MOVE_UNDEFINED;
6576 struct glyph_row *saved_glyph_row;
6577
6578 /* Don't produce glyphs in produce_glyphs. */
6579 saved_glyph_row = it->glyph_row;
6580 it->glyph_row = NULL;
6581
6582 #define BUFFER_POS_REACHED_P() \
6583 ((op & MOVE_TO_POS) != 0 \
6584 && BUFFERP (it->object) \
6585 && IT_CHARPOS (*it) >= to_charpos \
6586 && (it->method == GET_FROM_BUFFER \
6587 || (it->method == GET_FROM_DISPLAY_VECTOR \
6588 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
6589
6590
6591 while (1)
6592 {
6593 int x, i, ascent = 0, descent = 0;
6594
6595 /* Stop if we move beyond TO_CHARPOS (after an image or stretch glyph). */
6596 if ((op & MOVE_TO_POS) != 0
6597 && BUFFERP (it->object)
6598 && it->method == GET_FROM_BUFFER
6599 && IT_CHARPOS (*it) > to_charpos)
6600 {
6601 result = MOVE_POS_MATCH_OR_ZV;
6602 break;
6603 }
6604
6605 /* Stop when ZV reached.
6606 We used to stop here when TO_CHARPOS reached as well, but that is
6607 too soon if this glyph does not fit on this line. So we handle it
6608 explicitly below. */
6609 if (!get_next_display_element (it)
6610 || (it->truncate_lines_p
6611 && BUFFER_POS_REACHED_P ()))
6612 {
6613 result = MOVE_POS_MATCH_OR_ZV;
6614 break;
6615 }
6616
6617 /* The call to produce_glyphs will get the metrics of the
6618 display element IT is loaded with. We record in x the
6619 x-position before this display element in case it does not
6620 fit on the line. */
6621 x = it->current_x;
6622
6623 /* Remember the line height so far in case the next element doesn't
6624 fit on the line. */
6625 if (!it->truncate_lines_p)
6626 {
6627 ascent = it->max_ascent;
6628 descent = it->max_descent;
6629 }
6630
6631 PRODUCE_GLYPHS (it);
6632
6633 if (it->area != TEXT_AREA)
6634 {
6635 set_iterator_to_next (it, 1);
6636 continue;
6637 }
6638
6639 /* The number of glyphs we get back in IT->nglyphs will normally
6640 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
6641 character on a terminal frame, or (iii) a line end. For the
6642 second case, IT->nglyphs - 1 padding glyphs will be present
6643 (on X frames, there is only one glyph produced for a
6644 composite character.
6645
6646 The behavior implemented below means, for continuation lines,
6647 that as many spaces of a TAB as fit on the current line are
6648 displayed there. For terminal frames, as many glyphs of a
6649 multi-glyph character are displayed in the current line, too.
6650 This is what the old redisplay code did, and we keep it that
6651 way. Under X, the whole shape of a complex character must
6652 fit on the line or it will be completely displayed in the
6653 next line.
6654
6655 Note that both for tabs and padding glyphs, all glyphs have
6656 the same width. */
6657 if (it->nglyphs)
6658 {
6659 /* More than one glyph or glyph doesn't fit on line. All
6660 glyphs have the same width. */
6661 int single_glyph_width = it->pixel_width / it->nglyphs;
6662 int new_x;
6663 int x_before_this_char = x;
6664 int hpos_before_this_char = it->hpos;
6665
6666 for (i = 0; i < it->nglyphs; ++i, x = new_x)
6667 {
6668 new_x = x + single_glyph_width;
6669
6670 /* We want to leave anything reaching TO_X to the caller. */
6671 if ((op & MOVE_TO_X) && new_x > to_x)
6672 {
6673 if (BUFFER_POS_REACHED_P ())
6674 goto buffer_pos_reached;
6675 it->current_x = x;
6676 result = MOVE_X_REACHED;
6677 break;
6678 }
6679 else if (/* Lines are continued. */
6680 !it->truncate_lines_p
6681 && (/* And glyph doesn't fit on the line. */
6682 new_x > it->last_visible_x
6683 /* Or it fits exactly and we're on a window
6684 system frame. */
6685 || (new_x == it->last_visible_x
6686 && FRAME_WINDOW_P (it->f))))
6687 {
6688 if (/* IT->hpos == 0 means the very first glyph
6689 doesn't fit on the line, e.g. a wide image. */
6690 it->hpos == 0
6691 || (new_x == it->last_visible_x
6692 && FRAME_WINDOW_P (it->f)))
6693 {
6694 ++it->hpos;
6695 it->current_x = new_x;
6696
6697 /* The character's last glyph just barely fits
6698 in this row. */
6699 if (i == it->nglyphs - 1)
6700 {
6701 /* If this is the destination position,
6702 return a position *before* it in this row,
6703 now that we know it fits in this row. */
6704 if (BUFFER_POS_REACHED_P ())
6705 {
6706 it->hpos = hpos_before_this_char;
6707 it->current_x = x_before_this_char;
6708 result = MOVE_POS_MATCH_OR_ZV;
6709 break;
6710 }
6711
6712 set_iterator_to_next (it, 1);
6713 #ifdef HAVE_WINDOW_SYSTEM
6714 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
6715 {
6716 if (!get_next_display_element (it))
6717 {
6718 result = MOVE_POS_MATCH_OR_ZV;
6719 break;
6720 }
6721 if (BUFFER_POS_REACHED_P ())
6722 {
6723 if (ITERATOR_AT_END_OF_LINE_P (it))
6724 result = MOVE_POS_MATCH_OR_ZV;
6725 else
6726 result = MOVE_LINE_CONTINUED;
6727 break;
6728 }
6729 if (ITERATOR_AT_END_OF_LINE_P (it))
6730 {
6731 result = MOVE_NEWLINE_OR_CR;
6732 break;
6733 }
6734 }
6735 #endif /* HAVE_WINDOW_SYSTEM */
6736 }
6737 }
6738 else
6739 {
6740 it->current_x = x;
6741 it->max_ascent = ascent;
6742 it->max_descent = descent;
6743 }
6744
6745 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
6746 IT_CHARPOS (*it)));
6747 result = MOVE_LINE_CONTINUED;
6748 break;
6749 }
6750 else if (BUFFER_POS_REACHED_P ())
6751 goto buffer_pos_reached;
6752 else if (new_x > it->first_visible_x)
6753 {
6754 /* Glyph is visible. Increment number of glyphs that
6755 would be displayed. */
6756 ++it->hpos;
6757 }
6758 else
6759 {
6760 /* Glyph is completely off the left margin of the display
6761 area. Nothing to do. */
6762 }
6763 }
6764
6765 if (result != MOVE_UNDEFINED)
6766 break;
6767 }
6768 else if (BUFFER_POS_REACHED_P ())
6769 {
6770 buffer_pos_reached:
6771 it->current_x = x;
6772 it->max_ascent = ascent;
6773 it->max_descent = descent;
6774 result = MOVE_POS_MATCH_OR_ZV;
6775 break;
6776 }
6777 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
6778 {
6779 /* Stop when TO_X specified and reached. This check is
6780 necessary here because of lines consisting of a line end,
6781 only. The line end will not produce any glyphs and we
6782 would never get MOVE_X_REACHED. */
6783 xassert (it->nglyphs == 0);
6784 result = MOVE_X_REACHED;
6785 break;
6786 }
6787
6788 /* Is this a line end? If yes, we're done. */
6789 if (ITERATOR_AT_END_OF_LINE_P (it))
6790 {
6791 result = MOVE_NEWLINE_OR_CR;
6792 break;
6793 }
6794
6795 /* The current display element has been consumed. Advance
6796 to the next. */
6797 set_iterator_to_next (it, 1);
6798
6799 /* Stop if lines are truncated and IT's current x-position is
6800 past the right edge of the window now. */
6801 if (it->truncate_lines_p
6802 && it->current_x >= it->last_visible_x)
6803 {
6804 #ifdef HAVE_WINDOW_SYSTEM
6805 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
6806 {
6807 if (!get_next_display_element (it)
6808 || BUFFER_POS_REACHED_P ())
6809 {
6810 result = MOVE_POS_MATCH_OR_ZV;
6811 break;
6812 }
6813 if (ITERATOR_AT_END_OF_LINE_P (it))
6814 {
6815 result = MOVE_NEWLINE_OR_CR;
6816 break;
6817 }
6818 }
6819 #endif /* HAVE_WINDOW_SYSTEM */
6820 result = MOVE_LINE_TRUNCATED;
6821 break;
6822 }
6823 }
6824
6825 #undef BUFFER_POS_REACHED_P
6826
6827 /* Restore the iterator settings altered at the beginning of this
6828 function. */
6829 it->glyph_row = saved_glyph_row;
6830 return result;
6831 }
6832
6833
6834 /* Move IT forward until it satisfies one or more of the criteria in
6835 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
6836
6837 OP is a bit-mask that specifies where to stop, and in particular,
6838 which of those four position arguments makes a difference. See the
6839 description of enum move_operation_enum.
6840
6841 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
6842 screen line, this function will set IT to the next position >
6843 TO_CHARPOS. */
6844
6845 void
6846 move_it_to (it, to_charpos, to_x, to_y, to_vpos, op)
6847 struct it *it;
6848 int to_charpos, to_x, to_y, to_vpos;
6849 int op;
6850 {
6851 enum move_it_result skip, skip2 = MOVE_X_REACHED;
6852 int line_height;
6853 int reached = 0;
6854
6855 for (;;)
6856 {
6857 if (op & MOVE_TO_VPOS)
6858 {
6859 /* If no TO_CHARPOS and no TO_X specified, stop at the
6860 start of the line TO_VPOS. */
6861 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
6862 {
6863 if (it->vpos == to_vpos)
6864 {
6865 reached = 1;
6866 break;
6867 }
6868 else
6869 skip = move_it_in_display_line_to (it, -1, -1, 0);
6870 }
6871 else
6872 {
6873 /* TO_VPOS >= 0 means stop at TO_X in the line at
6874 TO_VPOS, or at TO_POS, whichever comes first. */
6875 if (it->vpos == to_vpos)
6876 {
6877 reached = 2;
6878 break;
6879 }
6880
6881 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
6882
6883 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
6884 {
6885 reached = 3;
6886 break;
6887 }
6888 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
6889 {
6890 /* We have reached TO_X but not in the line we want. */
6891 skip = move_it_in_display_line_to (it, to_charpos,
6892 -1, MOVE_TO_POS);
6893 if (skip == MOVE_POS_MATCH_OR_ZV)
6894 {
6895 reached = 4;
6896 break;
6897 }
6898 }
6899 }
6900 }
6901 else if (op & MOVE_TO_Y)
6902 {
6903 struct it it_backup;
6904
6905 /* TO_Y specified means stop at TO_X in the line containing
6906 TO_Y---or at TO_CHARPOS if this is reached first. The
6907 problem is that we can't really tell whether the line
6908 contains TO_Y before we have completely scanned it, and
6909 this may skip past TO_X. What we do is to first scan to
6910 TO_X.
6911
6912 If TO_X is not specified, use a TO_X of zero. The reason
6913 is to make the outcome of this function more predictable.
6914 If we didn't use TO_X == 0, we would stop at the end of
6915 the line which is probably not what a caller would expect
6916 to happen. */
6917 skip = move_it_in_display_line_to (it, to_charpos,
6918 ((op & MOVE_TO_X)
6919 ? to_x : 0),
6920 (MOVE_TO_X
6921 | (op & MOVE_TO_POS)));
6922
6923 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
6924 if (skip == MOVE_POS_MATCH_OR_ZV)
6925 {
6926 reached = 5;
6927 break;
6928 }
6929
6930 /* If TO_X was reached, we would like to know whether TO_Y
6931 is in the line. This can only be said if we know the
6932 total line height which requires us to scan the rest of
6933 the line. */
6934 if (skip == MOVE_X_REACHED)
6935 {
6936 /* Wait! We can conclude that TO_Y is in the line if
6937 the already scanned glyphs make the line tall enough
6938 because further scanning doesn't make it shorter. */
6939 line_height = it->max_ascent + it->max_descent;
6940 if (to_y >= it->current_y
6941 && to_y < it->current_y + line_height)
6942 {
6943 reached = 6;
6944 break;
6945 }
6946 it_backup = *it;
6947 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
6948 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
6949 op & MOVE_TO_POS);
6950 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
6951 }
6952
6953 /* Now, decide whether TO_Y is in this line. */
6954 line_height = it->max_ascent + it->max_descent;
6955 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
6956
6957 if (to_y >= it->current_y
6958 && to_y < it->current_y + line_height)
6959 {
6960 if (skip == MOVE_X_REACHED)
6961 /* If TO_Y is in this line and TO_X was reached above,
6962 we scanned too far. We have to restore IT's settings
6963 to the ones before skipping. */
6964 *it = it_backup;
6965 reached = 6;
6966 }
6967 else if (skip == MOVE_X_REACHED)
6968 {
6969 skip = skip2;
6970 if (skip == MOVE_POS_MATCH_OR_ZV)
6971 reached = 7;
6972 }
6973
6974 if (reached)
6975 break;
6976 }
6977 else if (BUFFERP (it->object)
6978 && it->method == GET_FROM_BUFFER
6979 && IT_CHARPOS (*it) >= to_charpos)
6980 skip = MOVE_POS_MATCH_OR_ZV;
6981 else
6982 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
6983
6984 switch (skip)
6985 {
6986 case MOVE_POS_MATCH_OR_ZV:
6987 reached = 8;
6988 goto out;
6989
6990 case MOVE_NEWLINE_OR_CR:
6991 set_iterator_to_next (it, 1);
6992 it->continuation_lines_width = 0;
6993 break;
6994
6995 case MOVE_LINE_TRUNCATED:
6996 it->continuation_lines_width = 0;
6997 reseat_at_next_visible_line_start (it, 0);
6998 if ((op & MOVE_TO_POS) != 0
6999 && IT_CHARPOS (*it) > to_charpos)
7000 {
7001 reached = 9;
7002 goto out;
7003 }
7004 break;
7005
7006 case MOVE_LINE_CONTINUED:
7007 /* For continued lines ending in a tab, some of the glyphs
7008 associated with the tab are displayed on the current
7009 line. Since it->current_x does not include these glyphs,
7010 we use it->last_visible_x instead. */
7011 it->continuation_lines_width +=
7012 (it->c == '\t') ? it->last_visible_x : it->current_x;
7013 break;
7014
7015 default:
7016 abort ();
7017 }
7018
7019 /* Reset/increment for the next run. */
7020 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
7021 it->current_x = it->hpos = 0;
7022 it->current_y += it->max_ascent + it->max_descent;
7023 ++it->vpos;
7024 last_height = it->max_ascent + it->max_descent;
7025 last_max_ascent = it->max_ascent;
7026 it->max_ascent = it->max_descent = 0;
7027 }
7028
7029 out:
7030
7031 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
7032 }
7033
7034
7035 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
7036
7037 If DY > 0, move IT backward at least that many pixels. DY = 0
7038 means move IT backward to the preceding line start or BEGV. This
7039 function may move over more than DY pixels if IT->current_y - DY
7040 ends up in the middle of a line; in this case IT->current_y will be
7041 set to the top of the line moved to. */
7042
7043 void
7044 move_it_vertically_backward (it, dy)
7045 struct it *it;
7046 int dy;
7047 {
7048 int nlines, h;
7049 struct it it2, it3;
7050 int start_pos;
7051
7052 move_further_back:
7053 xassert (dy >= 0);
7054
7055 start_pos = IT_CHARPOS (*it);
7056
7057 /* Estimate how many newlines we must move back. */
7058 nlines = max (1, dy / FRAME_LINE_HEIGHT (it->f));
7059
7060 /* Set the iterator's position that many lines back. */
7061 while (nlines-- && IT_CHARPOS (*it) > BEGV)
7062 back_to_previous_visible_line_start (it);
7063
7064 /* Reseat the iterator here. When moving backward, we don't want
7065 reseat to skip forward over invisible text, set up the iterator
7066 to deliver from overlay strings at the new position etc. So,
7067 use reseat_1 here. */
7068 reseat_1 (it, it->current.pos, 1);
7069
7070 /* We are now surely at a line start. */
7071 it->current_x = it->hpos = 0;
7072 it->continuation_lines_width = 0;
7073
7074 /* Move forward and see what y-distance we moved. First move to the
7075 start of the next line so that we get its height. We need this
7076 height to be able to tell whether we reached the specified
7077 y-distance. */
7078 it2 = *it;
7079 it2.max_ascent = it2.max_descent = 0;
7080 do
7081 {
7082 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
7083 MOVE_TO_POS | MOVE_TO_VPOS);
7084 }
7085 while (!IT_POS_VALID_AFTER_MOVE_P (&it2));
7086 xassert (IT_CHARPOS (*it) >= BEGV);
7087 it3 = it2;
7088
7089 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
7090 xassert (IT_CHARPOS (*it) >= BEGV);
7091 /* H is the actual vertical distance from the position in *IT
7092 and the starting position. */
7093 h = it2.current_y - it->current_y;
7094 /* NLINES is the distance in number of lines. */
7095 nlines = it2.vpos - it->vpos;
7096
7097 /* Correct IT's y and vpos position
7098 so that they are relative to the starting point. */
7099 it->vpos -= nlines;
7100 it->current_y -= h;
7101
7102 if (dy == 0)
7103 {
7104 /* DY == 0 means move to the start of the screen line. The
7105 value of nlines is > 0 if continuation lines were involved. */
7106 if (nlines > 0)
7107 move_it_by_lines (it, nlines, 1);
7108 #if 0
7109 /* I think this assert is bogus if buffer contains
7110 invisible text or images. KFS. */
7111 xassert (IT_CHARPOS (*it) <= start_pos);
7112 #endif
7113 }
7114 else
7115 {
7116 /* The y-position we try to reach, relative to *IT.
7117 Note that H has been subtracted in front of the if-statement. */
7118 int target_y = it->current_y + h - dy;
7119 int y0 = it3.current_y;
7120 int y1 = line_bottom_y (&it3);
7121 int line_height = y1 - y0;
7122
7123 /* If we did not reach target_y, try to move further backward if
7124 we can. If we moved too far backward, try to move forward. */
7125 if (target_y < it->current_y
7126 /* This is heuristic. In a window that's 3 lines high, with
7127 a line height of 13 pixels each, recentering with point
7128 on the bottom line will try to move -39/2 = 19 pixels
7129 backward. Try to avoid moving into the first line. */
7130 && (it->current_y - target_y
7131 > min (window_box_height (it->w), line_height * 2 / 3))
7132 && IT_CHARPOS (*it) > BEGV)
7133 {
7134 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
7135 target_y - it->current_y));
7136 dy = it->current_y - target_y;
7137 goto move_further_back;
7138 }
7139 else if (target_y >= it->current_y + line_height
7140 && IT_CHARPOS (*it) < ZV)
7141 {
7142 /* Should move forward by at least one line, maybe more.
7143
7144 Note: Calling move_it_by_lines can be expensive on
7145 terminal frames, where compute_motion is used (via
7146 vmotion) to do the job, when there are very long lines
7147 and truncate-lines is nil. That's the reason for
7148 treating terminal frames specially here. */
7149
7150 if (!FRAME_WINDOW_P (it->f))
7151 move_it_vertically (it, target_y - (it->current_y + line_height));
7152 else
7153 {
7154 do
7155 {
7156 move_it_by_lines (it, 1, 1);
7157 }
7158 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
7159 }
7160
7161 #if 0
7162 /* I think this assert is bogus if buffer contains
7163 invisible text or images. KFS. */
7164 xassert (IT_CHARPOS (*it) >= BEGV);
7165 #endif
7166 }
7167 }
7168 }
7169
7170
7171 /* Move IT by a specified amount of pixel lines DY. DY negative means
7172 move backwards. DY = 0 means move to start of screen line. At the
7173 end, IT will be on the start of a screen line. */
7174
7175 void
7176 move_it_vertically (it, dy)
7177 struct it *it;
7178 int dy;
7179 {
7180 if (dy <= 0)
7181 move_it_vertically_backward (it, -dy);
7182 else
7183 {
7184 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
7185 move_it_to (it, ZV, -1, it->current_y + dy, -1,
7186 MOVE_TO_POS | MOVE_TO_Y);
7187 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
7188
7189 /* If buffer ends in ZV without a newline, move to the start of
7190 the line to satisfy the post-condition. */
7191 if (IT_CHARPOS (*it) == ZV
7192 && ZV > BEGV
7193 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
7194 move_it_by_lines (it, 0, 0);
7195 }
7196 }
7197
7198
7199 /* Move iterator IT past the end of the text line it is in. */
7200
7201 void
7202 move_it_past_eol (it)
7203 struct it *it;
7204 {
7205 enum move_it_result rc;
7206
7207 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
7208 if (rc == MOVE_NEWLINE_OR_CR)
7209 set_iterator_to_next (it, 0);
7210 }
7211
7212
7213 #if 0 /* Currently not used. */
7214
7215 /* Return non-zero if some text between buffer positions START_CHARPOS
7216 and END_CHARPOS is invisible. IT->window is the window for text
7217 property lookup. */
7218
7219 static int
7220 invisible_text_between_p (it, start_charpos, end_charpos)
7221 struct it *it;
7222 int start_charpos, end_charpos;
7223 {
7224 Lisp_Object prop, limit;
7225 int invisible_found_p;
7226
7227 xassert (it != NULL && start_charpos <= end_charpos);
7228
7229 /* Is text at START invisible? */
7230 prop = Fget_char_property (make_number (start_charpos), Qinvisible,
7231 it->window);
7232 if (TEXT_PROP_MEANS_INVISIBLE (prop))
7233 invisible_found_p = 1;
7234 else
7235 {
7236 limit = Fnext_single_char_property_change (make_number (start_charpos),
7237 Qinvisible, Qnil,
7238 make_number (end_charpos));
7239 invisible_found_p = XFASTINT (limit) < end_charpos;
7240 }
7241
7242 return invisible_found_p;
7243 }
7244
7245 #endif /* 0 */
7246
7247
7248 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
7249 negative means move up. DVPOS == 0 means move to the start of the
7250 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
7251 NEED_Y_P is zero, IT->current_y will be left unchanged.
7252
7253 Further optimization ideas: If we would know that IT->f doesn't use
7254 a face with proportional font, we could be faster for
7255 truncate-lines nil. */
7256
7257 void
7258 move_it_by_lines (it, dvpos, need_y_p)
7259 struct it *it;
7260 int dvpos, need_y_p;
7261 {
7262 struct position pos;
7263
7264 /* The commented-out optimization uses vmotion on terminals. This
7265 gives bad results, because elements like it->what, on which
7266 callers such as pos_visible_p rely, aren't updated. */
7267 /* if (!FRAME_WINDOW_P (it->f))
7268 {
7269 struct text_pos textpos;
7270
7271 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
7272 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
7273 reseat (it, textpos, 1);
7274 it->vpos += pos.vpos;
7275 it->current_y += pos.vpos;
7276 }
7277 else */
7278
7279 if (dvpos == 0)
7280 {
7281 /* DVPOS == 0 means move to the start of the screen line. */
7282 move_it_vertically_backward (it, 0);
7283 xassert (it->current_x == 0 && it->hpos == 0);
7284 /* Let next call to line_bottom_y calculate real line height */
7285 last_height = 0;
7286 }
7287 else if (dvpos > 0)
7288 {
7289 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
7290 if (!IT_POS_VALID_AFTER_MOVE_P (it))
7291 move_it_to (it, IT_CHARPOS (*it) + 1, -1, -1, -1, MOVE_TO_POS);
7292 }
7293 else
7294 {
7295 struct it it2;
7296 int start_charpos, i;
7297
7298 /* Start at the beginning of the screen line containing IT's
7299 position. This may actually move vertically backwards,
7300 in case of overlays, so adjust dvpos accordingly. */
7301 dvpos += it->vpos;
7302 move_it_vertically_backward (it, 0);
7303 dvpos -= it->vpos;
7304
7305 /* Go back -DVPOS visible lines and reseat the iterator there. */
7306 start_charpos = IT_CHARPOS (*it);
7307 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > BEGV; --i)
7308 back_to_previous_visible_line_start (it);
7309 reseat (it, it->current.pos, 1);
7310
7311 /* Move further back if we end up in a string or an image. */
7312 while (!IT_POS_VALID_AFTER_MOVE_P (it))
7313 {
7314 /* First try to move to start of display line. */
7315 dvpos += it->vpos;
7316 move_it_vertically_backward (it, 0);
7317 dvpos -= it->vpos;
7318 if (IT_POS_VALID_AFTER_MOVE_P (it))
7319 break;
7320 /* If start of line is still in string or image,
7321 move further back. */
7322 back_to_previous_visible_line_start (it);
7323 reseat (it, it->current.pos, 1);
7324 dvpos--;
7325 }
7326
7327 it->current_x = it->hpos = 0;
7328
7329 /* Above call may have moved too far if continuation lines
7330 are involved. Scan forward and see if it did. */
7331 it2 = *it;
7332 it2.vpos = it2.current_y = 0;
7333 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
7334 it->vpos -= it2.vpos;
7335 it->current_y -= it2.current_y;
7336 it->current_x = it->hpos = 0;
7337
7338 /* If we moved too far back, move IT some lines forward. */
7339 if (it2.vpos > -dvpos)
7340 {
7341 int delta = it2.vpos + dvpos;
7342 it2 = *it;
7343 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
7344 /* Move back again if we got too far ahead. */
7345 if (IT_CHARPOS (*it) >= start_charpos)
7346 *it = it2;
7347 }
7348 }
7349 }
7350
7351 /* Return 1 if IT points into the middle of a display vector. */
7352
7353 int
7354 in_display_vector_p (it)
7355 struct it *it;
7356 {
7357 return (it->method == GET_FROM_DISPLAY_VECTOR
7358 && it->current.dpvec_index > 0
7359 && it->dpvec + it->current.dpvec_index != it->dpend);
7360 }
7361
7362 \f
7363 /***********************************************************************
7364 Messages
7365 ***********************************************************************/
7366
7367
7368 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
7369 to *Messages*. */
7370
7371 void
7372 add_to_log (format, arg1, arg2)
7373 char *format;
7374 Lisp_Object arg1, arg2;
7375 {
7376 Lisp_Object args[3];
7377 Lisp_Object msg, fmt;
7378 char *buffer;
7379 int len;
7380 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
7381 USE_SAFE_ALLOCA;
7382
7383 /* Do nothing if called asynchronously. Inserting text into
7384 a buffer may call after-change-functions and alike and
7385 that would means running Lisp asynchronously. */
7386 if (handling_signal)
7387 return;
7388
7389 fmt = msg = Qnil;
7390 GCPRO4 (fmt, msg, arg1, arg2);
7391
7392 args[0] = fmt = build_string (format);
7393 args[1] = arg1;
7394 args[2] = arg2;
7395 msg = Fformat (3, args);
7396
7397 len = SBYTES (msg) + 1;
7398 SAFE_ALLOCA (buffer, char *, len);
7399 bcopy (SDATA (msg), buffer, len);
7400
7401 message_dolog (buffer, len - 1, 1, 0);
7402 SAFE_FREE ();
7403
7404 UNGCPRO;
7405 }
7406
7407
7408 /* Output a newline in the *Messages* buffer if "needs" one. */
7409
7410 void
7411 message_log_maybe_newline ()
7412 {
7413 if (message_log_need_newline)
7414 message_dolog ("", 0, 1, 0);
7415 }
7416
7417
7418 /* Add a string M of length NBYTES to the message log, optionally
7419 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
7420 nonzero, means interpret the contents of M as multibyte. This
7421 function calls low-level routines in order to bypass text property
7422 hooks, etc. which might not be safe to run.
7423
7424 This may GC (insert may run before/after change hooks),
7425 so the buffer M must NOT point to a Lisp string. */
7426
7427 void
7428 message_dolog (m, nbytes, nlflag, multibyte)
7429 const char *m;
7430 int nbytes, nlflag, multibyte;
7431 {
7432 if (!NILP (Vmemory_full))
7433 return;
7434
7435 if (!NILP (Vmessage_log_max))
7436 {
7437 struct buffer *oldbuf;
7438 Lisp_Object oldpoint, oldbegv, oldzv;
7439 int old_windows_or_buffers_changed = windows_or_buffers_changed;
7440 int point_at_end = 0;
7441 int zv_at_end = 0;
7442 Lisp_Object old_deactivate_mark, tem;
7443 struct gcpro gcpro1;
7444
7445 old_deactivate_mark = Vdeactivate_mark;
7446 oldbuf = current_buffer;
7447 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
7448 current_buffer->undo_list = Qt;
7449
7450 oldpoint = message_dolog_marker1;
7451 set_marker_restricted (oldpoint, make_number (PT), Qnil);
7452 oldbegv = message_dolog_marker2;
7453 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
7454 oldzv = message_dolog_marker3;
7455 set_marker_restricted (oldzv, make_number (ZV), Qnil);
7456 GCPRO1 (old_deactivate_mark);
7457
7458 if (PT == Z)
7459 point_at_end = 1;
7460 if (ZV == Z)
7461 zv_at_end = 1;
7462
7463 BEGV = BEG;
7464 BEGV_BYTE = BEG_BYTE;
7465 ZV = Z;
7466 ZV_BYTE = Z_BYTE;
7467 TEMP_SET_PT_BOTH (Z, Z_BYTE);
7468
7469 /* Insert the string--maybe converting multibyte to single byte
7470 or vice versa, so that all the text fits the buffer. */
7471 if (multibyte
7472 && NILP (current_buffer->enable_multibyte_characters))
7473 {
7474 int i, c, char_bytes;
7475 unsigned char work[1];
7476
7477 /* Convert a multibyte string to single-byte
7478 for the *Message* buffer. */
7479 for (i = 0; i < nbytes; i += char_bytes)
7480 {
7481 c = string_char_and_length (m + i, nbytes - i, &char_bytes);
7482 work[0] = (ASCII_CHAR_P (c)
7483 ? c
7484 : multibyte_char_to_unibyte (c, Qnil));
7485 insert_1_both (work, 1, 1, 1, 0, 0);
7486 }
7487 }
7488 else if (! multibyte
7489 && ! NILP (current_buffer->enable_multibyte_characters))
7490 {
7491 int i, c, char_bytes;
7492 unsigned char *msg = (unsigned char *) m;
7493 unsigned char str[MAX_MULTIBYTE_LENGTH];
7494 /* Convert a single-byte string to multibyte
7495 for the *Message* buffer. */
7496 for (i = 0; i < nbytes; i++)
7497 {
7498 c = msg[i];
7499 c = unibyte_char_to_multibyte (c);
7500 char_bytes = CHAR_STRING (c, str);
7501 insert_1_both (str, 1, char_bytes, 1, 0, 0);
7502 }
7503 }
7504 else if (nbytes)
7505 insert_1 (m, nbytes, 1, 0, 0);
7506
7507 if (nlflag)
7508 {
7509 int this_bol, this_bol_byte, prev_bol, prev_bol_byte, dup;
7510 insert_1 ("\n", 1, 1, 0, 0);
7511
7512 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
7513 this_bol = PT;
7514 this_bol_byte = PT_BYTE;
7515
7516 /* See if this line duplicates the previous one.
7517 If so, combine duplicates. */
7518 if (this_bol > BEG)
7519 {
7520 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
7521 prev_bol = PT;
7522 prev_bol_byte = PT_BYTE;
7523
7524 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
7525 this_bol, this_bol_byte);
7526 if (dup)
7527 {
7528 del_range_both (prev_bol, prev_bol_byte,
7529 this_bol, this_bol_byte, 0);
7530 if (dup > 1)
7531 {
7532 char dupstr[40];
7533 int duplen;
7534
7535 /* If you change this format, don't forget to also
7536 change message_log_check_duplicate. */
7537 sprintf (dupstr, " [%d times]", dup);
7538 duplen = strlen (dupstr);
7539 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
7540 insert_1 (dupstr, duplen, 1, 0, 1);
7541 }
7542 }
7543 }
7544
7545 /* If we have more than the desired maximum number of lines
7546 in the *Messages* buffer now, delete the oldest ones.
7547 This is safe because we don't have undo in this buffer. */
7548
7549 if (NATNUMP (Vmessage_log_max))
7550 {
7551 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
7552 -XFASTINT (Vmessage_log_max) - 1, 0);
7553 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
7554 }
7555 }
7556 BEGV = XMARKER (oldbegv)->charpos;
7557 BEGV_BYTE = marker_byte_position (oldbegv);
7558
7559 if (zv_at_end)
7560 {
7561 ZV = Z;
7562 ZV_BYTE = Z_BYTE;
7563 }
7564 else
7565 {
7566 ZV = XMARKER (oldzv)->charpos;
7567 ZV_BYTE = marker_byte_position (oldzv);
7568 }
7569
7570 if (point_at_end)
7571 TEMP_SET_PT_BOTH (Z, Z_BYTE);
7572 else
7573 /* We can't do Fgoto_char (oldpoint) because it will run some
7574 Lisp code. */
7575 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
7576 XMARKER (oldpoint)->bytepos);
7577
7578 UNGCPRO;
7579 unchain_marker (XMARKER (oldpoint));
7580 unchain_marker (XMARKER (oldbegv));
7581 unchain_marker (XMARKER (oldzv));
7582
7583 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
7584 set_buffer_internal (oldbuf);
7585 if (NILP (tem))
7586 windows_or_buffers_changed = old_windows_or_buffers_changed;
7587 message_log_need_newline = !nlflag;
7588 Vdeactivate_mark = old_deactivate_mark;
7589 }
7590 }
7591
7592
7593 /* We are at the end of the buffer after just having inserted a newline.
7594 (Note: We depend on the fact we won't be crossing the gap.)
7595 Check to see if the most recent message looks a lot like the previous one.
7596 Return 0 if different, 1 if the new one should just replace it, or a
7597 value N > 1 if we should also append " [N times]". */
7598
7599 static int
7600 message_log_check_duplicate (prev_bol, prev_bol_byte, this_bol, this_bol_byte)
7601 int prev_bol, this_bol;
7602 int prev_bol_byte, this_bol_byte;
7603 {
7604 int i;
7605 int len = Z_BYTE - 1 - this_bol_byte;
7606 int seen_dots = 0;
7607 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
7608 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
7609
7610 for (i = 0; i < len; i++)
7611 {
7612 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
7613 seen_dots = 1;
7614 if (p1[i] != p2[i])
7615 return seen_dots;
7616 }
7617 p1 += len;
7618 if (*p1 == '\n')
7619 return 2;
7620 if (*p1++ == ' ' && *p1++ == '[')
7621 {
7622 int n = 0;
7623 while (*p1 >= '0' && *p1 <= '9')
7624 n = n * 10 + *p1++ - '0';
7625 if (strncmp (p1, " times]\n", 8) == 0)
7626 return n+1;
7627 }
7628 return 0;
7629 }
7630 \f
7631
7632 /* Display an echo area message M with a specified length of NBYTES
7633 bytes. The string may include null characters. If M is 0, clear
7634 out any existing message, and let the mini-buffer text show
7635 through.
7636
7637 This may GC, so the buffer M must NOT point to a Lisp string. */
7638
7639 void
7640 message2 (m, nbytes, multibyte)
7641 const char *m;
7642 int nbytes;
7643 int multibyte;
7644 {
7645 /* First flush out any partial line written with print. */
7646 message_log_maybe_newline ();
7647 if (m)
7648 message_dolog (m, nbytes, 1, multibyte);
7649 message2_nolog (m, nbytes, multibyte);
7650 }
7651
7652
7653 /* The non-logging counterpart of message2. */
7654
7655 void
7656 message2_nolog (m, nbytes, multibyte)
7657 const char *m;
7658 int nbytes, multibyte;
7659 {
7660 struct frame *sf = SELECTED_FRAME ();
7661 message_enable_multibyte = multibyte;
7662
7663 if (noninteractive)
7664 {
7665 if (noninteractive_need_newline)
7666 putc ('\n', stderr);
7667 noninteractive_need_newline = 0;
7668 if (m)
7669 fwrite (m, nbytes, 1, stderr);
7670 if (cursor_in_echo_area == 0)
7671 fprintf (stderr, "\n");
7672 fflush (stderr);
7673 }
7674 /* A null message buffer means that the frame hasn't really been
7675 initialized yet. Error messages get reported properly by
7676 cmd_error, so this must be just an informative message; toss it. */
7677 else if (INTERACTIVE
7678 && sf->glyphs_initialized_p
7679 && FRAME_MESSAGE_BUF (sf))
7680 {
7681 Lisp_Object mini_window;
7682 struct frame *f;
7683
7684 /* Get the frame containing the mini-buffer
7685 that the selected frame is using. */
7686 mini_window = FRAME_MINIBUF_WINDOW (sf);
7687 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7688
7689 FRAME_SAMPLE_VISIBILITY (f);
7690 if (FRAME_VISIBLE_P (sf)
7691 && ! FRAME_VISIBLE_P (f))
7692 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
7693
7694 if (m)
7695 {
7696 set_message (m, Qnil, nbytes, multibyte);
7697 if (minibuffer_auto_raise)
7698 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
7699 }
7700 else
7701 clear_message (1, 1);
7702
7703 do_pending_window_change (0);
7704 echo_area_display (1);
7705 do_pending_window_change (0);
7706 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
7707 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
7708 }
7709 }
7710
7711
7712 /* Display an echo area message M with a specified length of NBYTES
7713 bytes. The string may include null characters. If M is not a
7714 string, clear out any existing message, and let the mini-buffer
7715 text show through.
7716
7717 This function cancels echoing. */
7718
7719 void
7720 message3 (m, nbytes, multibyte)
7721 Lisp_Object m;
7722 int nbytes;
7723 int multibyte;
7724 {
7725 struct gcpro gcpro1;
7726
7727 GCPRO1 (m);
7728 clear_message (1,1);
7729 cancel_echoing ();
7730
7731 /* First flush out any partial line written with print. */
7732 message_log_maybe_newline ();
7733 if (STRINGP (m))
7734 {
7735 char *buffer;
7736 USE_SAFE_ALLOCA;
7737
7738 SAFE_ALLOCA (buffer, char *, nbytes);
7739 bcopy (SDATA (m), buffer, nbytes);
7740 message_dolog (buffer, nbytes, 1, multibyte);
7741 SAFE_FREE ();
7742 }
7743 message3_nolog (m, nbytes, multibyte);
7744
7745 UNGCPRO;
7746 }
7747
7748
7749 /* The non-logging version of message3.
7750 This does not cancel echoing, because it is used for echoing.
7751 Perhaps we need to make a separate function for echoing
7752 and make this cancel echoing. */
7753
7754 void
7755 message3_nolog (m, nbytes, multibyte)
7756 Lisp_Object m;
7757 int nbytes, multibyte;
7758 {
7759 struct frame *sf = SELECTED_FRAME ();
7760 message_enable_multibyte = multibyte;
7761
7762 if (noninteractive)
7763 {
7764 if (noninteractive_need_newline)
7765 putc ('\n', stderr);
7766 noninteractive_need_newline = 0;
7767 if (STRINGP (m))
7768 fwrite (SDATA (m), nbytes, 1, stderr);
7769 if (cursor_in_echo_area == 0)
7770 fprintf (stderr, "\n");
7771 fflush (stderr);
7772 }
7773 /* A null message buffer means that the frame hasn't really been
7774 initialized yet. Error messages get reported properly by
7775 cmd_error, so this must be just an informative message; toss it. */
7776 else if (INTERACTIVE
7777 && sf->glyphs_initialized_p
7778 && FRAME_MESSAGE_BUF (sf))
7779 {
7780 Lisp_Object mini_window;
7781 Lisp_Object frame;
7782 struct frame *f;
7783
7784 /* Get the frame containing the mini-buffer
7785 that the selected frame is using. */
7786 mini_window = FRAME_MINIBUF_WINDOW (sf);
7787 frame = XWINDOW (mini_window)->frame;
7788 f = XFRAME (frame);
7789
7790 FRAME_SAMPLE_VISIBILITY (f);
7791 if (FRAME_VISIBLE_P (sf)
7792 && !FRAME_VISIBLE_P (f))
7793 Fmake_frame_visible (frame);
7794
7795 if (STRINGP (m) && SCHARS (m) > 0)
7796 {
7797 set_message (NULL, m, nbytes, multibyte);
7798 if (minibuffer_auto_raise)
7799 Fraise_frame (frame);
7800 /* Assume we are not echoing.
7801 (If we are, echo_now will override this.) */
7802 echo_message_buffer = Qnil;
7803 }
7804 else
7805 clear_message (1, 1);
7806
7807 do_pending_window_change (0);
7808 echo_area_display (1);
7809 do_pending_window_change (0);
7810 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
7811 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
7812 }
7813 }
7814
7815
7816 /* Display a null-terminated echo area message M. If M is 0, clear
7817 out any existing message, and let the mini-buffer text show through.
7818
7819 The buffer M must continue to exist until after the echo area gets
7820 cleared or some other message gets displayed there. Do not pass
7821 text that is stored in a Lisp string. Do not pass text in a buffer
7822 that was alloca'd. */
7823
7824 void
7825 message1 (m)
7826 char *m;
7827 {
7828 message2 (m, (m ? strlen (m) : 0), 0);
7829 }
7830
7831
7832 /* The non-logging counterpart of message1. */
7833
7834 void
7835 message1_nolog (m)
7836 char *m;
7837 {
7838 message2_nolog (m, (m ? strlen (m) : 0), 0);
7839 }
7840
7841 /* Display a message M which contains a single %s
7842 which gets replaced with STRING. */
7843
7844 void
7845 message_with_string (m, string, log)
7846 char *m;
7847 Lisp_Object string;
7848 int log;
7849 {
7850 CHECK_STRING (string);
7851
7852 if (noninteractive)
7853 {
7854 if (m)
7855 {
7856 if (noninteractive_need_newline)
7857 putc ('\n', stderr);
7858 noninteractive_need_newline = 0;
7859 fprintf (stderr, m, SDATA (string));
7860 if (cursor_in_echo_area == 0)
7861 fprintf (stderr, "\n");
7862 fflush (stderr);
7863 }
7864 }
7865 else if (INTERACTIVE)
7866 {
7867 /* The frame whose minibuffer we're going to display the message on.
7868 It may be larger than the selected frame, so we need
7869 to use its buffer, not the selected frame's buffer. */
7870 Lisp_Object mini_window;
7871 struct frame *f, *sf = SELECTED_FRAME ();
7872
7873 /* Get the frame containing the minibuffer
7874 that the selected frame is using. */
7875 mini_window = FRAME_MINIBUF_WINDOW (sf);
7876 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7877
7878 /* A null message buffer means that the frame hasn't really been
7879 initialized yet. Error messages get reported properly by
7880 cmd_error, so this must be just an informative message; toss it. */
7881 if (FRAME_MESSAGE_BUF (f))
7882 {
7883 Lisp_Object args[2], message;
7884 struct gcpro gcpro1, gcpro2;
7885
7886 args[0] = build_string (m);
7887 args[1] = message = string;
7888 GCPRO2 (args[0], message);
7889 gcpro1.nvars = 2;
7890
7891 message = Fformat (2, args);
7892
7893 if (log)
7894 message3 (message, SBYTES (message), STRING_MULTIBYTE (message));
7895 else
7896 message3_nolog (message, SBYTES (message), STRING_MULTIBYTE (message));
7897
7898 UNGCPRO;
7899
7900 /* Print should start at the beginning of the message
7901 buffer next time. */
7902 message_buf_print = 0;
7903 }
7904 }
7905 }
7906
7907
7908 /* Dump an informative message to the minibuf. If M is 0, clear out
7909 any existing message, and let the mini-buffer text show through. */
7910
7911 /* VARARGS 1 */
7912 void
7913 message (m, a1, a2, a3)
7914 char *m;
7915 EMACS_INT a1, a2, a3;
7916 {
7917 if (noninteractive)
7918 {
7919 if (m)
7920 {
7921 if (noninteractive_need_newline)
7922 putc ('\n', stderr);
7923 noninteractive_need_newline = 0;
7924 fprintf (stderr, m, a1, a2, a3);
7925 if (cursor_in_echo_area == 0)
7926 fprintf (stderr, "\n");
7927 fflush (stderr);
7928 }
7929 }
7930 else if (INTERACTIVE)
7931 {
7932 /* The frame whose mini-buffer we're going to display the message
7933 on. It may be larger than the selected frame, so we need to
7934 use its buffer, not the selected frame's buffer. */
7935 Lisp_Object mini_window;
7936 struct frame *f, *sf = SELECTED_FRAME ();
7937
7938 /* Get the frame containing the mini-buffer
7939 that the selected frame is using. */
7940 mini_window = FRAME_MINIBUF_WINDOW (sf);
7941 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7942
7943 /* A null message buffer means that the frame hasn't really been
7944 initialized yet. Error messages get reported properly by
7945 cmd_error, so this must be just an informative message; toss
7946 it. */
7947 if (FRAME_MESSAGE_BUF (f))
7948 {
7949 if (m)
7950 {
7951 int len;
7952 #ifdef NO_ARG_ARRAY
7953 char *a[3];
7954 a[0] = (char *) a1;
7955 a[1] = (char *) a2;
7956 a[2] = (char *) a3;
7957
7958 len = doprnt (FRAME_MESSAGE_BUF (f),
7959 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
7960 #else
7961 len = doprnt (FRAME_MESSAGE_BUF (f),
7962 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3,
7963 (char **) &a1);
7964 #endif /* NO_ARG_ARRAY */
7965
7966 message2 (FRAME_MESSAGE_BUF (f), len, 0);
7967 }
7968 else
7969 message1 (0);
7970
7971 /* Print should start at the beginning of the message
7972 buffer next time. */
7973 message_buf_print = 0;
7974 }
7975 }
7976 }
7977
7978
7979 /* The non-logging version of message. */
7980
7981 void
7982 message_nolog (m, a1, a2, a3)
7983 char *m;
7984 EMACS_INT a1, a2, a3;
7985 {
7986 Lisp_Object old_log_max;
7987 old_log_max = Vmessage_log_max;
7988 Vmessage_log_max = Qnil;
7989 message (m, a1, a2, a3);
7990 Vmessage_log_max = old_log_max;
7991 }
7992
7993
7994 /* Display the current message in the current mini-buffer. This is
7995 only called from error handlers in process.c, and is not time
7996 critical. */
7997
7998 void
7999 update_echo_area ()
8000 {
8001 if (!NILP (echo_area_buffer[0]))
8002 {
8003 Lisp_Object string;
8004 string = Fcurrent_message ();
8005 message3 (string, SBYTES (string),
8006 !NILP (current_buffer->enable_multibyte_characters));
8007 }
8008 }
8009
8010
8011 /* Make sure echo area buffers in `echo_buffers' are live.
8012 If they aren't, make new ones. */
8013
8014 static void
8015 ensure_echo_area_buffers ()
8016 {
8017 int i;
8018
8019 for (i = 0; i < 2; ++i)
8020 if (!BUFFERP (echo_buffer[i])
8021 || NILP (XBUFFER (echo_buffer[i])->name))
8022 {
8023 char name[30];
8024 Lisp_Object old_buffer;
8025 int j;
8026
8027 old_buffer = echo_buffer[i];
8028 sprintf (name, " *Echo Area %d*", i);
8029 echo_buffer[i] = Fget_buffer_create (build_string (name));
8030 XBUFFER (echo_buffer[i])->truncate_lines = Qnil;
8031
8032 for (j = 0; j < 2; ++j)
8033 if (EQ (old_buffer, echo_area_buffer[j]))
8034 echo_area_buffer[j] = echo_buffer[i];
8035 }
8036 }
8037
8038
8039 /* Call FN with args A1..A4 with either the current or last displayed
8040 echo_area_buffer as current buffer.
8041
8042 WHICH zero means use the current message buffer
8043 echo_area_buffer[0]. If that is nil, choose a suitable buffer
8044 from echo_buffer[] and clear it.
8045
8046 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
8047 suitable buffer from echo_buffer[] and clear it.
8048
8049 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
8050 that the current message becomes the last displayed one, make
8051 choose a suitable buffer for echo_area_buffer[0], and clear it.
8052
8053 Value is what FN returns. */
8054
8055 static int
8056 with_echo_area_buffer (w, which, fn, a1, a2, a3, a4)
8057 struct window *w;
8058 int which;
8059 int (*fn) P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
8060 EMACS_INT a1;
8061 Lisp_Object a2;
8062 EMACS_INT a3, a4;
8063 {
8064 Lisp_Object buffer;
8065 int this_one, the_other, clear_buffer_p, rc;
8066 int count = SPECPDL_INDEX ();
8067
8068 /* If buffers aren't live, make new ones. */
8069 ensure_echo_area_buffers ();
8070
8071 clear_buffer_p = 0;
8072
8073 if (which == 0)
8074 this_one = 0, the_other = 1;
8075 else if (which > 0)
8076 this_one = 1, the_other = 0;
8077 else
8078 {
8079 this_one = 0, the_other = 1;
8080 clear_buffer_p = 1;
8081
8082 /* We need a fresh one in case the current echo buffer equals
8083 the one containing the last displayed echo area message. */
8084 if (!NILP (echo_area_buffer[this_one])
8085 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
8086 echo_area_buffer[this_one] = Qnil;
8087 }
8088
8089 /* Choose a suitable buffer from echo_buffer[] is we don't
8090 have one. */
8091 if (NILP (echo_area_buffer[this_one]))
8092 {
8093 echo_area_buffer[this_one]
8094 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
8095 ? echo_buffer[the_other]
8096 : echo_buffer[this_one]);
8097 clear_buffer_p = 1;
8098 }
8099
8100 buffer = echo_area_buffer[this_one];
8101
8102 /* Don't get confused by reusing the buffer used for echoing
8103 for a different purpose. */
8104 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
8105 cancel_echoing ();
8106
8107 record_unwind_protect (unwind_with_echo_area_buffer,
8108 with_echo_area_buffer_unwind_data (w));
8109
8110 /* Make the echo area buffer current. Note that for display
8111 purposes, it is not necessary that the displayed window's buffer
8112 == current_buffer, except for text property lookup. So, let's
8113 only set that buffer temporarily here without doing a full
8114 Fset_window_buffer. We must also change w->pointm, though,
8115 because otherwise an assertions in unshow_buffer fails, and Emacs
8116 aborts. */
8117 set_buffer_internal_1 (XBUFFER (buffer));
8118 if (w)
8119 {
8120 w->buffer = buffer;
8121 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
8122 }
8123
8124 current_buffer->undo_list = Qt;
8125 current_buffer->read_only = Qnil;
8126 specbind (Qinhibit_read_only, Qt);
8127 specbind (Qinhibit_modification_hooks, Qt);
8128
8129 if (clear_buffer_p && Z > BEG)
8130 del_range (BEG, Z);
8131
8132 xassert (BEGV >= BEG);
8133 xassert (ZV <= Z && ZV >= BEGV);
8134
8135 rc = fn (a1, a2, a3, a4);
8136
8137 xassert (BEGV >= BEG);
8138 xassert (ZV <= Z && ZV >= BEGV);
8139
8140 unbind_to (count, Qnil);
8141 return rc;
8142 }
8143
8144
8145 /* Save state that should be preserved around the call to the function
8146 FN called in with_echo_area_buffer. */
8147
8148 static Lisp_Object
8149 with_echo_area_buffer_unwind_data (w)
8150 struct window *w;
8151 {
8152 int i = 0;
8153 Lisp_Object vector;
8154
8155 /* Reduce consing by keeping one vector in
8156 Vwith_echo_area_save_vector. */
8157 vector = Vwith_echo_area_save_vector;
8158 Vwith_echo_area_save_vector = Qnil;
8159
8160 if (NILP (vector))
8161 vector = Fmake_vector (make_number (7), Qnil);
8162
8163 XSETBUFFER (AREF (vector, i), current_buffer); ++i;
8164 AREF (vector, i) = Vdeactivate_mark, ++i;
8165 AREF (vector, i) = make_number (windows_or_buffers_changed), ++i;
8166
8167 if (w)
8168 {
8169 XSETWINDOW (AREF (vector, i), w); ++i;
8170 AREF (vector, i) = w->buffer; ++i;
8171 AREF (vector, i) = make_number (XMARKER (w->pointm)->charpos); ++i;
8172 AREF (vector, i) = make_number (XMARKER (w->pointm)->bytepos); ++i;
8173 }
8174 else
8175 {
8176 int end = i + 4;
8177 for (; i < end; ++i)
8178 AREF (vector, i) = Qnil;
8179 }
8180
8181 xassert (i == ASIZE (vector));
8182 return vector;
8183 }
8184
8185
8186 /* Restore global state from VECTOR which was created by
8187 with_echo_area_buffer_unwind_data. */
8188
8189 static Lisp_Object
8190 unwind_with_echo_area_buffer (vector)
8191 Lisp_Object vector;
8192 {
8193 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
8194 Vdeactivate_mark = AREF (vector, 1);
8195 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
8196
8197 if (WINDOWP (AREF (vector, 3)))
8198 {
8199 struct window *w;
8200 Lisp_Object buffer, charpos, bytepos;
8201
8202 w = XWINDOW (AREF (vector, 3));
8203 buffer = AREF (vector, 4);
8204 charpos = AREF (vector, 5);
8205 bytepos = AREF (vector, 6);
8206
8207 w->buffer = buffer;
8208 set_marker_both (w->pointm, buffer,
8209 XFASTINT (charpos), XFASTINT (bytepos));
8210 }
8211
8212 Vwith_echo_area_save_vector = vector;
8213 return Qnil;
8214 }
8215
8216
8217 /* Set up the echo area for use by print functions. MULTIBYTE_P
8218 non-zero means we will print multibyte. */
8219
8220 void
8221 setup_echo_area_for_printing (multibyte_p)
8222 int multibyte_p;
8223 {
8224 /* If we can't find an echo area any more, exit. */
8225 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
8226 Fkill_emacs (Qnil);
8227
8228 ensure_echo_area_buffers ();
8229
8230 if (!message_buf_print)
8231 {
8232 /* A message has been output since the last time we printed.
8233 Choose a fresh echo area buffer. */
8234 if (EQ (echo_area_buffer[1], echo_buffer[0]))
8235 echo_area_buffer[0] = echo_buffer[1];
8236 else
8237 echo_area_buffer[0] = echo_buffer[0];
8238
8239 /* Switch to that buffer and clear it. */
8240 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
8241 current_buffer->truncate_lines = Qnil;
8242
8243 if (Z > BEG)
8244 {
8245 int count = SPECPDL_INDEX ();
8246 specbind (Qinhibit_read_only, Qt);
8247 /* Note that undo recording is always disabled. */
8248 del_range (BEG, Z);
8249 unbind_to (count, Qnil);
8250 }
8251 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
8252
8253 /* Set up the buffer for the multibyteness we need. */
8254 if (multibyte_p
8255 != !NILP (current_buffer->enable_multibyte_characters))
8256 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
8257
8258 /* Raise the frame containing the echo area. */
8259 if (minibuffer_auto_raise)
8260 {
8261 struct frame *sf = SELECTED_FRAME ();
8262 Lisp_Object mini_window;
8263 mini_window = FRAME_MINIBUF_WINDOW (sf);
8264 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
8265 }
8266
8267 message_log_maybe_newline ();
8268 message_buf_print = 1;
8269 }
8270 else
8271 {
8272 if (NILP (echo_area_buffer[0]))
8273 {
8274 if (EQ (echo_area_buffer[1], echo_buffer[0]))
8275 echo_area_buffer[0] = echo_buffer[1];
8276 else
8277 echo_area_buffer[0] = echo_buffer[0];
8278 }
8279
8280 if (current_buffer != XBUFFER (echo_area_buffer[0]))
8281 {
8282 /* Someone switched buffers between print requests. */
8283 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
8284 current_buffer->truncate_lines = Qnil;
8285 }
8286 }
8287 }
8288
8289
8290 /* Display an echo area message in window W. Value is non-zero if W's
8291 height is changed. If display_last_displayed_message_p is
8292 non-zero, display the message that was last displayed, otherwise
8293 display the current message. */
8294
8295 static int
8296 display_echo_area (w)
8297 struct window *w;
8298 {
8299 int i, no_message_p, window_height_changed_p, count;
8300
8301 /* Temporarily disable garbage collections while displaying the echo
8302 area. This is done because a GC can print a message itself.
8303 That message would modify the echo area buffer's contents while a
8304 redisplay of the buffer is going on, and seriously confuse
8305 redisplay. */
8306 count = inhibit_garbage_collection ();
8307
8308 /* If there is no message, we must call display_echo_area_1
8309 nevertheless because it resizes the window. But we will have to
8310 reset the echo_area_buffer in question to nil at the end because
8311 with_echo_area_buffer will sets it to an empty buffer. */
8312 i = display_last_displayed_message_p ? 1 : 0;
8313 no_message_p = NILP (echo_area_buffer[i]);
8314
8315 window_height_changed_p
8316 = with_echo_area_buffer (w, display_last_displayed_message_p,
8317 display_echo_area_1,
8318 (EMACS_INT) w, Qnil, 0, 0);
8319
8320 if (no_message_p)
8321 echo_area_buffer[i] = Qnil;
8322
8323 unbind_to (count, Qnil);
8324 return window_height_changed_p;
8325 }
8326
8327
8328 /* Helper for display_echo_area. Display the current buffer which
8329 contains the current echo area message in window W, a mini-window,
8330 a pointer to which is passed in A1. A2..A4 are currently not used.
8331 Change the height of W so that all of the message is displayed.
8332 Value is non-zero if height of W was changed. */
8333
8334 static int
8335 display_echo_area_1 (a1, a2, a3, a4)
8336 EMACS_INT a1;
8337 Lisp_Object a2;
8338 EMACS_INT a3, a4;
8339 {
8340 struct window *w = (struct window *) a1;
8341 Lisp_Object window;
8342 struct text_pos start;
8343 int window_height_changed_p = 0;
8344
8345 /* Do this before displaying, so that we have a large enough glyph
8346 matrix for the display. If we can't get enough space for the
8347 whole text, display the last N lines. That works by setting w->start. */
8348 window_height_changed_p = resize_mini_window (w, 0);
8349
8350 /* Use the starting position chosen by resize_mini_window. */
8351 SET_TEXT_POS_FROM_MARKER (start, w->start);
8352
8353 /* Display. */
8354 clear_glyph_matrix (w->desired_matrix);
8355 XSETWINDOW (window, w);
8356 try_window (window, start, 0);
8357
8358 return window_height_changed_p;
8359 }
8360
8361
8362 /* Resize the echo area window to exactly the size needed for the
8363 currently displayed message, if there is one. If a mini-buffer
8364 is active, don't shrink it. */
8365
8366 void
8367 resize_echo_area_exactly ()
8368 {
8369 if (BUFFERP (echo_area_buffer[0])
8370 && WINDOWP (echo_area_window))
8371 {
8372 struct window *w = XWINDOW (echo_area_window);
8373 int resized_p;
8374 Lisp_Object resize_exactly;
8375
8376 if (minibuf_level == 0)
8377 resize_exactly = Qt;
8378 else
8379 resize_exactly = Qnil;
8380
8381 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
8382 (EMACS_INT) w, resize_exactly, 0, 0);
8383 if (resized_p)
8384 {
8385 ++windows_or_buffers_changed;
8386 ++update_mode_lines;
8387 redisplay_internal (0);
8388 }
8389 }
8390 }
8391
8392
8393 /* Callback function for with_echo_area_buffer, when used from
8394 resize_echo_area_exactly. A1 contains a pointer to the window to
8395 resize, EXACTLY non-nil means resize the mini-window exactly to the
8396 size of the text displayed. A3 and A4 are not used. Value is what
8397 resize_mini_window returns. */
8398
8399 static int
8400 resize_mini_window_1 (a1, exactly, a3, a4)
8401 EMACS_INT a1;
8402 Lisp_Object exactly;
8403 EMACS_INT a3, a4;
8404 {
8405 return resize_mini_window ((struct window *) a1, !NILP (exactly));
8406 }
8407
8408
8409 /* Resize mini-window W to fit the size of its contents. EXACT:P
8410 means size the window exactly to the size needed. Otherwise, it's
8411 only enlarged until W's buffer is empty.
8412
8413 Set W->start to the right place to begin display. If the whole
8414 contents fit, start at the beginning. Otherwise, start so as
8415 to make the end of the contents appear. This is particularly
8416 important for y-or-n-p, but seems desirable generally.
8417
8418 Value is non-zero if the window height has been changed. */
8419
8420 int
8421 resize_mini_window (w, exact_p)
8422 struct window *w;
8423 int exact_p;
8424 {
8425 struct frame *f = XFRAME (w->frame);
8426 int window_height_changed_p = 0;
8427
8428 xassert (MINI_WINDOW_P (w));
8429
8430 /* By default, start display at the beginning. */
8431 set_marker_both (w->start, w->buffer,
8432 BUF_BEGV (XBUFFER (w->buffer)),
8433 BUF_BEGV_BYTE (XBUFFER (w->buffer)));
8434
8435 /* Don't resize windows while redisplaying a window; it would
8436 confuse redisplay functions when the size of the window they are
8437 displaying changes from under them. Such a resizing can happen,
8438 for instance, when which-func prints a long message while
8439 we are running fontification-functions. We're running these
8440 functions with safe_call which binds inhibit-redisplay to t. */
8441 if (!NILP (Vinhibit_redisplay))
8442 return 0;
8443
8444 /* Nil means don't try to resize. */
8445 if (NILP (Vresize_mini_windows)
8446 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
8447 return 0;
8448
8449 if (!FRAME_MINIBUF_ONLY_P (f))
8450 {
8451 struct it it;
8452 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
8453 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
8454 int height, max_height;
8455 int unit = FRAME_LINE_HEIGHT (f);
8456 struct text_pos start;
8457 struct buffer *old_current_buffer = NULL;
8458
8459 if (current_buffer != XBUFFER (w->buffer))
8460 {
8461 old_current_buffer = current_buffer;
8462 set_buffer_internal (XBUFFER (w->buffer));
8463 }
8464
8465 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
8466
8467 /* Compute the max. number of lines specified by the user. */
8468 if (FLOATP (Vmax_mini_window_height))
8469 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
8470 else if (INTEGERP (Vmax_mini_window_height))
8471 max_height = XINT (Vmax_mini_window_height);
8472 else
8473 max_height = total_height / 4;
8474
8475 /* Correct that max. height if it's bogus. */
8476 max_height = max (1, max_height);
8477 max_height = min (total_height, max_height);
8478
8479 /* Find out the height of the text in the window. */
8480 if (it.truncate_lines_p)
8481 height = 1;
8482 else
8483 {
8484 last_height = 0;
8485 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
8486 if (it.max_ascent == 0 && it.max_descent == 0)
8487 height = it.current_y + last_height;
8488 else
8489 height = it.current_y + it.max_ascent + it.max_descent;
8490 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
8491 height = (height + unit - 1) / unit;
8492 }
8493
8494 /* Compute a suitable window start. */
8495 if (height > max_height)
8496 {
8497 height = max_height;
8498 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
8499 move_it_vertically_backward (&it, (height - 1) * unit);
8500 start = it.current.pos;
8501 }
8502 else
8503 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
8504 SET_MARKER_FROM_TEXT_POS (w->start, start);
8505
8506 if (EQ (Vresize_mini_windows, Qgrow_only))
8507 {
8508 /* Let it grow only, until we display an empty message, in which
8509 case the window shrinks again. */
8510 if (height > WINDOW_TOTAL_LINES (w))
8511 {
8512 int old_height = WINDOW_TOTAL_LINES (w);
8513 freeze_window_starts (f, 1);
8514 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8515 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8516 }
8517 else if (height < WINDOW_TOTAL_LINES (w)
8518 && (exact_p || BEGV == ZV))
8519 {
8520 int old_height = WINDOW_TOTAL_LINES (w);
8521 freeze_window_starts (f, 0);
8522 shrink_mini_window (w);
8523 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8524 }
8525 }
8526 else
8527 {
8528 /* Always resize to exact size needed. */
8529 if (height > WINDOW_TOTAL_LINES (w))
8530 {
8531 int old_height = WINDOW_TOTAL_LINES (w);
8532 freeze_window_starts (f, 1);
8533 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8534 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8535 }
8536 else if (height < WINDOW_TOTAL_LINES (w))
8537 {
8538 int old_height = WINDOW_TOTAL_LINES (w);
8539 freeze_window_starts (f, 0);
8540 shrink_mini_window (w);
8541
8542 if (height)
8543 {
8544 freeze_window_starts (f, 1);
8545 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8546 }
8547
8548 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8549 }
8550 }
8551
8552 if (old_current_buffer)
8553 set_buffer_internal (old_current_buffer);
8554 }
8555
8556 return window_height_changed_p;
8557 }
8558
8559
8560 /* Value is the current message, a string, or nil if there is no
8561 current message. */
8562
8563 Lisp_Object
8564 current_message ()
8565 {
8566 Lisp_Object msg;
8567
8568 if (NILP (echo_area_buffer[0]))
8569 msg = Qnil;
8570 else
8571 {
8572 with_echo_area_buffer (0, 0, current_message_1,
8573 (EMACS_INT) &msg, Qnil, 0, 0);
8574 if (NILP (msg))
8575 echo_area_buffer[0] = Qnil;
8576 }
8577
8578 return msg;
8579 }
8580
8581
8582 static int
8583 current_message_1 (a1, a2, a3, a4)
8584 EMACS_INT a1;
8585 Lisp_Object a2;
8586 EMACS_INT a3, a4;
8587 {
8588 Lisp_Object *msg = (Lisp_Object *) a1;
8589
8590 if (Z > BEG)
8591 *msg = make_buffer_string (BEG, Z, 1);
8592 else
8593 *msg = Qnil;
8594 return 0;
8595 }
8596
8597
8598 /* Push the current message on Vmessage_stack for later restauration
8599 by restore_message. Value is non-zero if the current message isn't
8600 empty. This is a relatively infrequent operation, so it's not
8601 worth optimizing. */
8602
8603 int
8604 push_message ()
8605 {
8606 Lisp_Object msg;
8607 msg = current_message ();
8608 Vmessage_stack = Fcons (msg, Vmessage_stack);
8609 return STRINGP (msg);
8610 }
8611
8612
8613 /* Restore message display from the top of Vmessage_stack. */
8614
8615 void
8616 restore_message ()
8617 {
8618 Lisp_Object msg;
8619
8620 xassert (CONSP (Vmessage_stack));
8621 msg = XCAR (Vmessage_stack);
8622 if (STRINGP (msg))
8623 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
8624 else
8625 message3_nolog (msg, 0, 0);
8626 }
8627
8628
8629 /* Handler for record_unwind_protect calling pop_message. */
8630
8631 Lisp_Object
8632 pop_message_unwind (dummy)
8633 Lisp_Object dummy;
8634 {
8635 pop_message ();
8636 return Qnil;
8637 }
8638
8639 /* Pop the top-most entry off Vmessage_stack. */
8640
8641 void
8642 pop_message ()
8643 {
8644 xassert (CONSP (Vmessage_stack));
8645 Vmessage_stack = XCDR (Vmessage_stack);
8646 }
8647
8648
8649 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
8650 exits. If the stack is not empty, we have a missing pop_message
8651 somewhere. */
8652
8653 void
8654 check_message_stack ()
8655 {
8656 if (!NILP (Vmessage_stack))
8657 abort ();
8658 }
8659
8660
8661 /* Truncate to NCHARS what will be displayed in the echo area the next
8662 time we display it---but don't redisplay it now. */
8663
8664 void
8665 truncate_echo_area (nchars)
8666 int nchars;
8667 {
8668 if (nchars == 0)
8669 echo_area_buffer[0] = Qnil;
8670 /* A null message buffer means that the frame hasn't really been
8671 initialized yet. Error messages get reported properly by
8672 cmd_error, so this must be just an informative message; toss it. */
8673 else if (!noninteractive
8674 && INTERACTIVE
8675 && !NILP (echo_area_buffer[0]))
8676 {
8677 struct frame *sf = SELECTED_FRAME ();
8678 if (FRAME_MESSAGE_BUF (sf))
8679 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
8680 }
8681 }
8682
8683
8684 /* Helper function for truncate_echo_area. Truncate the current
8685 message to at most NCHARS characters. */
8686
8687 static int
8688 truncate_message_1 (nchars, a2, a3, a4)
8689 EMACS_INT nchars;
8690 Lisp_Object a2;
8691 EMACS_INT a3, a4;
8692 {
8693 if (BEG + nchars < Z)
8694 del_range (BEG + nchars, Z);
8695 if (Z == BEG)
8696 echo_area_buffer[0] = Qnil;
8697 return 0;
8698 }
8699
8700
8701 /* Set the current message to a substring of S or STRING.
8702
8703 If STRING is a Lisp string, set the message to the first NBYTES
8704 bytes from STRING. NBYTES zero means use the whole string. If
8705 STRING is multibyte, the message will be displayed multibyte.
8706
8707 If S is not null, set the message to the first LEN bytes of S. LEN
8708 zero means use the whole string. MULTIBYTE_P non-zero means S is
8709 multibyte. Display the message multibyte in that case.
8710
8711 Doesn't GC, as with_echo_area_buffer binds Qinhibit_modification_hooks
8712 to t before calling set_message_1 (which calls insert).
8713 */
8714
8715 void
8716 set_message (s, string, nbytes, multibyte_p)
8717 const char *s;
8718 Lisp_Object string;
8719 int nbytes, multibyte_p;
8720 {
8721 message_enable_multibyte
8722 = ((s && multibyte_p)
8723 || (STRINGP (string) && STRING_MULTIBYTE (string)));
8724
8725 with_echo_area_buffer (0, -1, set_message_1,
8726 (EMACS_INT) s, string, nbytes, multibyte_p);
8727 message_buf_print = 0;
8728 help_echo_showing_p = 0;
8729 }
8730
8731
8732 /* Helper function for set_message. Arguments have the same meaning
8733 as there, with A1 corresponding to S and A2 corresponding to STRING
8734 This function is called with the echo area buffer being
8735 current. */
8736
8737 static int
8738 set_message_1 (a1, a2, nbytes, multibyte_p)
8739 EMACS_INT a1;
8740 Lisp_Object a2;
8741 EMACS_INT nbytes, multibyte_p;
8742 {
8743 const char *s = (const char *) a1;
8744 Lisp_Object string = a2;
8745
8746 /* Change multibyteness of the echo buffer appropriately. */
8747 if (message_enable_multibyte
8748 != !NILP (current_buffer->enable_multibyte_characters))
8749 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
8750
8751 current_buffer->truncate_lines = message_truncate_lines ? Qt : Qnil;
8752
8753 /* Insert new message at BEG. */
8754 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
8755
8756 if (STRINGP (string))
8757 {
8758 int nchars;
8759
8760 if (nbytes == 0)
8761 nbytes = SBYTES (string);
8762 nchars = string_byte_to_char (string, nbytes);
8763
8764 /* This function takes care of single/multibyte conversion. We
8765 just have to ensure that the echo area buffer has the right
8766 setting of enable_multibyte_characters. */
8767 insert_from_string (string, 0, 0, nchars, nbytes, 1);
8768 }
8769 else if (s)
8770 {
8771 if (nbytes == 0)
8772 nbytes = strlen (s);
8773
8774 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
8775 {
8776 /* Convert from multi-byte to single-byte. */
8777 int i, c, n;
8778 unsigned char work[1];
8779
8780 /* Convert a multibyte string to single-byte. */
8781 for (i = 0; i < nbytes; i += n)
8782 {
8783 c = string_char_and_length (s + i, nbytes - i, &n);
8784 work[0] = (ASCII_CHAR_P (c)
8785 ? c
8786 : multibyte_char_to_unibyte (c, Qnil));
8787 insert_1_both (work, 1, 1, 1, 0, 0);
8788 }
8789 }
8790 else if (!multibyte_p
8791 && !NILP (current_buffer->enable_multibyte_characters))
8792 {
8793 /* Convert from single-byte to multi-byte. */
8794 int i, c, n;
8795 const unsigned char *msg = (const unsigned char *) s;
8796 unsigned char str[MAX_MULTIBYTE_LENGTH];
8797
8798 /* Convert a single-byte string to multibyte. */
8799 for (i = 0; i < nbytes; i++)
8800 {
8801 c = msg[i];
8802 c = unibyte_char_to_multibyte (c);
8803 n = CHAR_STRING (c, str);
8804 insert_1_both (str, 1, n, 1, 0, 0);
8805 }
8806 }
8807 else
8808 insert_1 (s, nbytes, 1, 0, 0);
8809 }
8810
8811 return 0;
8812 }
8813
8814
8815 /* Clear messages. CURRENT_P non-zero means clear the current
8816 message. LAST_DISPLAYED_P non-zero means clear the message
8817 last displayed. */
8818
8819 void
8820 clear_message (current_p, last_displayed_p)
8821 int current_p, last_displayed_p;
8822 {
8823 if (current_p)
8824 {
8825 echo_area_buffer[0] = Qnil;
8826 message_cleared_p = 1;
8827 }
8828
8829 if (last_displayed_p)
8830 echo_area_buffer[1] = Qnil;
8831
8832 message_buf_print = 0;
8833 }
8834
8835 /* Clear garbaged frames.
8836
8837 This function is used where the old redisplay called
8838 redraw_garbaged_frames which in turn called redraw_frame which in
8839 turn called clear_frame. The call to clear_frame was a source of
8840 flickering. I believe a clear_frame is not necessary. It should
8841 suffice in the new redisplay to invalidate all current matrices,
8842 and ensure a complete redisplay of all windows. */
8843
8844 static void
8845 clear_garbaged_frames ()
8846 {
8847 if (frame_garbaged)
8848 {
8849 Lisp_Object tail, frame;
8850 int changed_count = 0;
8851
8852 FOR_EACH_FRAME (tail, frame)
8853 {
8854 struct frame *f = XFRAME (frame);
8855
8856 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
8857 {
8858 if (f->resized_p)
8859 {
8860 Fredraw_frame (frame);
8861 f->force_flush_display_p = 1;
8862 }
8863 clear_current_matrices (f);
8864 changed_count++;
8865 f->garbaged = 0;
8866 f->resized_p = 0;
8867 }
8868 }
8869
8870 frame_garbaged = 0;
8871 if (changed_count)
8872 ++windows_or_buffers_changed;
8873 }
8874 }
8875
8876
8877 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
8878 is non-zero update selected_frame. Value is non-zero if the
8879 mini-windows height has been changed. */
8880
8881 static int
8882 echo_area_display (update_frame_p)
8883 int update_frame_p;
8884 {
8885 Lisp_Object mini_window;
8886 struct window *w;
8887 struct frame *f;
8888 int window_height_changed_p = 0;
8889 struct frame *sf = SELECTED_FRAME ();
8890
8891 mini_window = FRAME_MINIBUF_WINDOW (sf);
8892 w = XWINDOW (mini_window);
8893 f = XFRAME (WINDOW_FRAME (w));
8894
8895 /* Don't display if frame is invisible or not yet initialized. */
8896 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
8897 return 0;
8898
8899 /* The terminal frame is used as the first Emacs frame on the Mac OS. */
8900 #ifndef MAC_OS8
8901 #ifdef HAVE_WINDOW_SYSTEM
8902 /* When Emacs starts, selected_frame may be the initial terminal
8903 frame. If we let this through, a message would be displayed on
8904 the terminal. */
8905 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
8906 return 0;
8907 #endif /* HAVE_WINDOW_SYSTEM */
8908 #endif
8909
8910 /* Redraw garbaged frames. */
8911 if (frame_garbaged)
8912 clear_garbaged_frames ();
8913
8914 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
8915 {
8916 echo_area_window = mini_window;
8917 window_height_changed_p = display_echo_area (w);
8918 w->must_be_updated_p = 1;
8919
8920 /* Update the display, unless called from redisplay_internal.
8921 Also don't update the screen during redisplay itself. The
8922 update will happen at the end of redisplay, and an update
8923 here could cause confusion. */
8924 if (update_frame_p && !redisplaying_p)
8925 {
8926 int n = 0;
8927
8928 /* If the display update has been interrupted by pending
8929 input, update mode lines in the frame. Due to the
8930 pending input, it might have been that redisplay hasn't
8931 been called, so that mode lines above the echo area are
8932 garbaged. This looks odd, so we prevent it here. */
8933 if (!display_completed)
8934 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
8935
8936 if (window_height_changed_p
8937 /* Don't do this if Emacs is shutting down. Redisplay
8938 needs to run hooks. */
8939 && !NILP (Vrun_hooks))
8940 {
8941 /* Must update other windows. Likewise as in other
8942 cases, don't let this update be interrupted by
8943 pending input. */
8944 int count = SPECPDL_INDEX ();
8945 specbind (Qredisplay_dont_pause, Qt);
8946 windows_or_buffers_changed = 1;
8947 redisplay_internal (0);
8948 unbind_to (count, Qnil);
8949 }
8950 else if (FRAME_WINDOW_P (f) && n == 0)
8951 {
8952 /* Window configuration is the same as before.
8953 Can do with a display update of the echo area,
8954 unless we displayed some mode lines. */
8955 update_single_window (w, 1);
8956 FRAME_RIF (f)->flush_display (f);
8957 }
8958 else
8959 update_frame (f, 1, 1);
8960
8961 /* If cursor is in the echo area, make sure that the next
8962 redisplay displays the minibuffer, so that the cursor will
8963 be replaced with what the minibuffer wants. */
8964 if (cursor_in_echo_area)
8965 ++windows_or_buffers_changed;
8966 }
8967 }
8968 else if (!EQ (mini_window, selected_window))
8969 windows_or_buffers_changed++;
8970
8971 /* Last displayed message is now the current message. */
8972 echo_area_buffer[1] = echo_area_buffer[0];
8973 /* Inform read_char that we're not echoing. */
8974 echo_message_buffer = Qnil;
8975
8976 /* Prevent redisplay optimization in redisplay_internal by resetting
8977 this_line_start_pos. This is done because the mini-buffer now
8978 displays the message instead of its buffer text. */
8979 if (EQ (mini_window, selected_window))
8980 CHARPOS (this_line_start_pos) = 0;
8981
8982 return window_height_changed_p;
8983 }
8984
8985
8986 \f
8987 /***********************************************************************
8988 Mode Lines and Frame Titles
8989 ***********************************************************************/
8990
8991 /* A buffer for constructing non-propertized mode-line strings and
8992 frame titles in it; allocated from the heap in init_xdisp and
8993 resized as needed in store_mode_line_noprop_char. */
8994
8995 static char *mode_line_noprop_buf;
8996
8997 /* The buffer's end, and a current output position in it. */
8998
8999 static char *mode_line_noprop_buf_end;
9000 static char *mode_line_noprop_ptr;
9001
9002 #define MODE_LINE_NOPROP_LEN(start) \
9003 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
9004
9005 static enum {
9006 MODE_LINE_DISPLAY = 0,
9007 MODE_LINE_TITLE,
9008 MODE_LINE_NOPROP,
9009 MODE_LINE_STRING
9010 } mode_line_target;
9011
9012 /* Alist that caches the results of :propertize.
9013 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
9014 static Lisp_Object mode_line_proptrans_alist;
9015
9016 /* List of strings making up the mode-line. */
9017 static Lisp_Object mode_line_string_list;
9018
9019 /* Base face property when building propertized mode line string. */
9020 static Lisp_Object mode_line_string_face;
9021 static Lisp_Object mode_line_string_face_prop;
9022
9023
9024 /* Unwind data for mode line strings */
9025
9026 static Lisp_Object Vmode_line_unwind_vector;
9027
9028 static Lisp_Object
9029 format_mode_line_unwind_data (obuf, save_proptrans)
9030 struct buffer *obuf;
9031 {
9032 Lisp_Object vector;
9033
9034 /* Reduce consing by keeping one vector in
9035 Vwith_echo_area_save_vector. */
9036 vector = Vmode_line_unwind_vector;
9037 Vmode_line_unwind_vector = Qnil;
9038
9039 if (NILP (vector))
9040 vector = Fmake_vector (make_number (7), Qnil);
9041
9042 AREF (vector, 0) = make_number (mode_line_target);
9043 AREF (vector, 1) = make_number (MODE_LINE_NOPROP_LEN (0));
9044 AREF (vector, 2) = mode_line_string_list;
9045 AREF (vector, 3) = (save_proptrans ? mode_line_proptrans_alist : Qt);
9046 AREF (vector, 4) = mode_line_string_face;
9047 AREF (vector, 5) = mode_line_string_face_prop;
9048
9049 if (obuf)
9050 XSETBUFFER (AREF (vector, 6), obuf);
9051 else
9052 AREF (vector, 6) = Qnil;
9053
9054 return vector;
9055 }
9056
9057 static Lisp_Object
9058 unwind_format_mode_line (vector)
9059 Lisp_Object vector;
9060 {
9061 mode_line_target = XINT (AREF (vector, 0));
9062 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
9063 mode_line_string_list = AREF (vector, 2);
9064 if (! EQ (AREF (vector, 3), Qt))
9065 mode_line_proptrans_alist = AREF (vector, 3);
9066 mode_line_string_face = AREF (vector, 4);
9067 mode_line_string_face_prop = AREF (vector, 5);
9068
9069 if (!NILP (AREF (vector, 6)))
9070 {
9071 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
9072 AREF (vector, 6) = Qnil;
9073 }
9074
9075 Vmode_line_unwind_vector = vector;
9076 return Qnil;
9077 }
9078
9079
9080 /* Store a single character C for the frame title in mode_line_noprop_buf.
9081 Re-allocate mode_line_noprop_buf if necessary. */
9082
9083 static void
9084 #ifdef PROTOTYPES
9085 store_mode_line_noprop_char (char c)
9086 #else
9087 store_mode_line_noprop_char (c)
9088 char c;
9089 #endif
9090 {
9091 /* If output position has reached the end of the allocated buffer,
9092 double the buffer's size. */
9093 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
9094 {
9095 int len = MODE_LINE_NOPROP_LEN (0);
9096 int new_size = 2 * len * sizeof *mode_line_noprop_buf;
9097 mode_line_noprop_buf = (char *) xrealloc (mode_line_noprop_buf, new_size);
9098 mode_line_noprop_buf_end = mode_line_noprop_buf + new_size;
9099 mode_line_noprop_ptr = mode_line_noprop_buf + len;
9100 }
9101
9102 *mode_line_noprop_ptr++ = c;
9103 }
9104
9105
9106 /* Store part of a frame title in mode_line_noprop_buf, beginning at
9107 mode_line_noprop_ptr. STR is the string to store. Do not copy
9108 characters that yield more columns than PRECISION; PRECISION <= 0
9109 means copy the whole string. Pad with spaces until FIELD_WIDTH
9110 number of characters have been copied; FIELD_WIDTH <= 0 means don't
9111 pad. Called from display_mode_element when it is used to build a
9112 frame title. */
9113
9114 static int
9115 store_mode_line_noprop (str, field_width, precision)
9116 const unsigned char *str;
9117 int field_width, precision;
9118 {
9119 int n = 0;
9120 int dummy, nbytes;
9121
9122 /* Copy at most PRECISION chars from STR. */
9123 nbytes = strlen (str);
9124 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
9125 while (nbytes--)
9126 store_mode_line_noprop_char (*str++);
9127
9128 /* Fill up with spaces until FIELD_WIDTH reached. */
9129 while (field_width > 0
9130 && n < field_width)
9131 {
9132 store_mode_line_noprop_char (' ');
9133 ++n;
9134 }
9135
9136 return n;
9137 }
9138
9139 /***********************************************************************
9140 Frame Titles
9141 ***********************************************************************/
9142
9143 #ifdef HAVE_WINDOW_SYSTEM
9144
9145 /* Set the title of FRAME, if it has changed. The title format is
9146 Vicon_title_format if FRAME is iconified, otherwise it is
9147 frame_title_format. */
9148
9149 static void
9150 x_consider_frame_title (frame)
9151 Lisp_Object frame;
9152 {
9153 struct frame *f = XFRAME (frame);
9154
9155 if (FRAME_WINDOW_P (f)
9156 || FRAME_MINIBUF_ONLY_P (f)
9157 || f->explicit_name)
9158 {
9159 /* Do we have more than one visible frame on this X display? */
9160 Lisp_Object tail;
9161 Lisp_Object fmt;
9162 int title_start;
9163 char *title;
9164 int len;
9165 struct it it;
9166 int count = SPECPDL_INDEX ();
9167
9168 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
9169 {
9170 Lisp_Object other_frame = XCAR (tail);
9171 struct frame *tf = XFRAME (other_frame);
9172
9173 if (tf != f
9174 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
9175 && !FRAME_MINIBUF_ONLY_P (tf)
9176 && !EQ (other_frame, tip_frame)
9177 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
9178 break;
9179 }
9180
9181 /* Set global variable indicating that multiple frames exist. */
9182 multiple_frames = CONSP (tail);
9183
9184 /* Switch to the buffer of selected window of the frame. Set up
9185 mode_line_target so that display_mode_element will output into
9186 mode_line_noprop_buf; then display the title. */
9187 record_unwind_protect (unwind_format_mode_line,
9188 format_mode_line_unwind_data (current_buffer, 0));
9189
9190 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
9191 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
9192
9193 mode_line_target = MODE_LINE_TITLE;
9194 title_start = MODE_LINE_NOPROP_LEN (0);
9195 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
9196 NULL, DEFAULT_FACE_ID);
9197 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
9198 len = MODE_LINE_NOPROP_LEN (title_start);
9199 title = mode_line_noprop_buf + title_start;
9200 unbind_to (count, Qnil);
9201
9202 /* Set the title only if it's changed. This avoids consing in
9203 the common case where it hasn't. (If it turns out that we've
9204 already wasted too much time by walking through the list with
9205 display_mode_element, then we might need to optimize at a
9206 higher level than this.) */
9207 if (! STRINGP (f->name)
9208 || SBYTES (f->name) != len
9209 || bcmp (title, SDATA (f->name), len) != 0)
9210 x_implicitly_set_name (f, make_string (title, len), Qnil);
9211 }
9212 }
9213
9214 #endif /* not HAVE_WINDOW_SYSTEM */
9215
9216
9217
9218 \f
9219 /***********************************************************************
9220 Menu Bars
9221 ***********************************************************************/
9222
9223
9224 /* Prepare for redisplay by updating menu-bar item lists when
9225 appropriate. This can call eval. */
9226
9227 void
9228 prepare_menu_bars ()
9229 {
9230 int all_windows;
9231 struct gcpro gcpro1, gcpro2;
9232 struct frame *f;
9233 Lisp_Object tooltip_frame;
9234
9235 #ifdef HAVE_WINDOW_SYSTEM
9236 tooltip_frame = tip_frame;
9237 #else
9238 tooltip_frame = Qnil;
9239 #endif
9240
9241 /* Update all frame titles based on their buffer names, etc. We do
9242 this before the menu bars so that the buffer-menu will show the
9243 up-to-date frame titles. */
9244 #ifdef HAVE_WINDOW_SYSTEM
9245 if (windows_or_buffers_changed || update_mode_lines)
9246 {
9247 Lisp_Object tail, frame;
9248
9249 FOR_EACH_FRAME (tail, frame)
9250 {
9251 f = XFRAME (frame);
9252 if (!EQ (frame, tooltip_frame)
9253 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
9254 x_consider_frame_title (frame);
9255 }
9256 }
9257 #endif /* HAVE_WINDOW_SYSTEM */
9258
9259 /* Update the menu bar item lists, if appropriate. This has to be
9260 done before any actual redisplay or generation of display lines. */
9261 all_windows = (update_mode_lines
9262 || buffer_shared > 1
9263 || windows_or_buffers_changed);
9264 if (all_windows)
9265 {
9266 Lisp_Object tail, frame;
9267 int count = SPECPDL_INDEX ();
9268 /* 1 means that update_menu_bar has run its hooks
9269 so any further calls to update_menu_bar shouldn't do so again. */
9270 int menu_bar_hooks_run = 0;
9271
9272 record_unwind_save_match_data ();
9273
9274 FOR_EACH_FRAME (tail, frame)
9275 {
9276 f = XFRAME (frame);
9277
9278 /* Ignore tooltip frame. */
9279 if (EQ (frame, tooltip_frame))
9280 continue;
9281
9282 /* If a window on this frame changed size, report that to
9283 the user and clear the size-change flag. */
9284 if (FRAME_WINDOW_SIZES_CHANGED (f))
9285 {
9286 Lisp_Object functions;
9287
9288 /* Clear flag first in case we get an error below. */
9289 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
9290 functions = Vwindow_size_change_functions;
9291 GCPRO2 (tail, functions);
9292
9293 while (CONSP (functions))
9294 {
9295 call1 (XCAR (functions), frame);
9296 functions = XCDR (functions);
9297 }
9298 UNGCPRO;
9299 }
9300
9301 GCPRO1 (tail);
9302 menu_bar_hooks_run = update_menu_bar (f, 0, menu_bar_hooks_run);
9303 #ifdef HAVE_WINDOW_SYSTEM
9304 update_tool_bar (f, 0);
9305 #ifdef MAC_OS
9306 mac_update_title_bar (f, 0);
9307 #endif
9308 #endif
9309 UNGCPRO;
9310 }
9311
9312 unbind_to (count, Qnil);
9313 }
9314 else
9315 {
9316 struct frame *sf = SELECTED_FRAME ();
9317 update_menu_bar (sf, 1, 0);
9318 #ifdef HAVE_WINDOW_SYSTEM
9319 update_tool_bar (sf, 1);
9320 #ifdef MAC_OS
9321 mac_update_title_bar (sf, 1);
9322 #endif
9323 #endif
9324 }
9325
9326 /* Motif needs this. See comment in xmenu.c. Turn it off when
9327 pending_menu_activation is not defined. */
9328 #ifdef USE_X_TOOLKIT
9329 pending_menu_activation = 0;
9330 #endif
9331 }
9332
9333
9334 /* Update the menu bar item list for frame F. This has to be done
9335 before we start to fill in any display lines, because it can call
9336 eval.
9337
9338 If SAVE_MATCH_DATA is non-zero, we must save and restore it here.
9339
9340 If HOOKS_RUN is 1, that means a previous call to update_menu_bar
9341 already ran the menu bar hooks for this redisplay, so there
9342 is no need to run them again. The return value is the
9343 updated value of this flag, to pass to the next call. */
9344
9345 static int
9346 update_menu_bar (f, save_match_data, hooks_run)
9347 struct frame *f;
9348 int save_match_data;
9349 int hooks_run;
9350 {
9351 Lisp_Object window;
9352 register struct window *w;
9353
9354 /* If called recursively during a menu update, do nothing. This can
9355 happen when, for instance, an activate-menubar-hook causes a
9356 redisplay. */
9357 if (inhibit_menubar_update)
9358 return hooks_run;
9359
9360 window = FRAME_SELECTED_WINDOW (f);
9361 w = XWINDOW (window);
9362
9363 #if 0 /* The if statement below this if statement used to include the
9364 condition !NILP (w->update_mode_line), rather than using
9365 update_mode_lines directly, and this if statement may have
9366 been added to make that condition work. Now the if
9367 statement below matches its comment, this isn't needed. */
9368 if (update_mode_lines)
9369 w->update_mode_line = Qt;
9370 #endif
9371
9372 if (FRAME_WINDOW_P (f)
9373 ?
9374 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
9375 || defined (USE_GTK)
9376 FRAME_EXTERNAL_MENU_BAR (f)
9377 #else
9378 FRAME_MENU_BAR_LINES (f) > 0
9379 #endif
9380 : FRAME_MENU_BAR_LINES (f) > 0)
9381 {
9382 /* If the user has switched buffers or windows, we need to
9383 recompute to reflect the new bindings. But we'll
9384 recompute when update_mode_lines is set too; that means
9385 that people can use force-mode-line-update to request
9386 that the menu bar be recomputed. The adverse effect on
9387 the rest of the redisplay algorithm is about the same as
9388 windows_or_buffers_changed anyway. */
9389 if (windows_or_buffers_changed
9390 /* This used to test w->update_mode_line, but we believe
9391 there is no need to recompute the menu in that case. */
9392 || update_mode_lines
9393 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
9394 < BUF_MODIFF (XBUFFER (w->buffer)))
9395 != !NILP (w->last_had_star))
9396 || ((!NILP (Vtransient_mark_mode)
9397 && !NILP (XBUFFER (w->buffer)->mark_active))
9398 != !NILP (w->region_showing)))
9399 {
9400 struct buffer *prev = current_buffer;
9401 int count = SPECPDL_INDEX ();
9402
9403 specbind (Qinhibit_menubar_update, Qt);
9404
9405 set_buffer_internal_1 (XBUFFER (w->buffer));
9406 if (save_match_data)
9407 record_unwind_save_match_data ();
9408 if (NILP (Voverriding_local_map_menu_flag))
9409 {
9410 specbind (Qoverriding_terminal_local_map, Qnil);
9411 specbind (Qoverriding_local_map, Qnil);
9412 }
9413
9414 if (!hooks_run)
9415 {
9416 /* Run the Lucid hook. */
9417 safe_run_hooks (Qactivate_menubar_hook);
9418
9419 /* If it has changed current-menubar from previous value,
9420 really recompute the menu-bar from the value. */
9421 if (! NILP (Vlucid_menu_bar_dirty_flag))
9422 call0 (Qrecompute_lucid_menubar);
9423
9424 safe_run_hooks (Qmenu_bar_update_hook);
9425
9426 hooks_run = 1;
9427 }
9428
9429 XSETFRAME (Vmenu_updating_frame, f);
9430 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
9431
9432 /* Redisplay the menu bar in case we changed it. */
9433 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
9434 || defined (USE_GTK)
9435 if (FRAME_WINDOW_P (f))
9436 {
9437 #ifdef MAC_OS
9438 /* All frames on Mac OS share the same menubar. So only
9439 the selected frame should be allowed to set it. */
9440 if (f == SELECTED_FRAME ())
9441 #endif
9442 set_frame_menubar (f, 0, 0);
9443 }
9444 else
9445 /* On a terminal screen, the menu bar is an ordinary screen
9446 line, and this makes it get updated. */
9447 w->update_mode_line = Qt;
9448 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
9449 /* In the non-toolkit version, the menu bar is an ordinary screen
9450 line, and this makes it get updated. */
9451 w->update_mode_line = Qt;
9452 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
9453
9454 unbind_to (count, Qnil);
9455 set_buffer_internal_1 (prev);
9456 }
9457 }
9458
9459 return hooks_run;
9460 }
9461
9462
9463 \f
9464 /***********************************************************************
9465 Output Cursor
9466 ***********************************************************************/
9467
9468 #ifdef HAVE_WINDOW_SYSTEM
9469
9470 /* EXPORT:
9471 Nominal cursor position -- where to draw output.
9472 HPOS and VPOS are window relative glyph matrix coordinates.
9473 X and Y are window relative pixel coordinates. */
9474
9475 struct cursor_pos output_cursor;
9476
9477
9478 /* EXPORT:
9479 Set the global variable output_cursor to CURSOR. All cursor
9480 positions are relative to updated_window. */
9481
9482 void
9483 set_output_cursor (cursor)
9484 struct cursor_pos *cursor;
9485 {
9486 output_cursor.hpos = cursor->hpos;
9487 output_cursor.vpos = cursor->vpos;
9488 output_cursor.x = cursor->x;
9489 output_cursor.y = cursor->y;
9490 }
9491
9492
9493 /* EXPORT for RIF:
9494 Set a nominal cursor position.
9495
9496 HPOS and VPOS are column/row positions in a window glyph matrix. X
9497 and Y are window text area relative pixel positions.
9498
9499 If this is done during an update, updated_window will contain the
9500 window that is being updated and the position is the future output
9501 cursor position for that window. If updated_window is null, use
9502 selected_window and display the cursor at the given position. */
9503
9504 void
9505 x_cursor_to (vpos, hpos, y, x)
9506 int vpos, hpos, y, x;
9507 {
9508 struct window *w;
9509
9510 /* If updated_window is not set, work on selected_window. */
9511 if (updated_window)
9512 w = updated_window;
9513 else
9514 w = XWINDOW (selected_window);
9515
9516 /* Set the output cursor. */
9517 output_cursor.hpos = hpos;
9518 output_cursor.vpos = vpos;
9519 output_cursor.x = x;
9520 output_cursor.y = y;
9521
9522 /* If not called as part of an update, really display the cursor.
9523 This will also set the cursor position of W. */
9524 if (updated_window == NULL)
9525 {
9526 BLOCK_INPUT;
9527 display_and_set_cursor (w, 1, hpos, vpos, x, y);
9528 if (FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
9529 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (SELECTED_FRAME ());
9530 UNBLOCK_INPUT;
9531 }
9532 }
9533
9534 #endif /* HAVE_WINDOW_SYSTEM */
9535
9536 \f
9537 /***********************************************************************
9538 Tool-bars
9539 ***********************************************************************/
9540
9541 #ifdef HAVE_WINDOW_SYSTEM
9542
9543 /* Where the mouse was last time we reported a mouse event. */
9544
9545 FRAME_PTR last_mouse_frame;
9546
9547 /* Tool-bar item index of the item on which a mouse button was pressed
9548 or -1. */
9549
9550 int last_tool_bar_item;
9551
9552
9553 /* Update the tool-bar item list for frame F. This has to be done
9554 before we start to fill in any display lines. Called from
9555 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
9556 and restore it here. */
9557
9558 static void
9559 update_tool_bar (f, save_match_data)
9560 struct frame *f;
9561 int save_match_data;
9562 {
9563 #if defined (USE_GTK) || USE_MAC_TOOLBAR
9564 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
9565 #else
9566 int do_update = WINDOWP (f->tool_bar_window)
9567 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
9568 #endif
9569
9570 if (do_update)
9571 {
9572 Lisp_Object window;
9573 struct window *w;
9574
9575 window = FRAME_SELECTED_WINDOW (f);
9576 w = XWINDOW (window);
9577
9578 /* If the user has switched buffers or windows, we need to
9579 recompute to reflect the new bindings. But we'll
9580 recompute when update_mode_lines is set too; that means
9581 that people can use force-mode-line-update to request
9582 that the menu bar be recomputed. The adverse effect on
9583 the rest of the redisplay algorithm is about the same as
9584 windows_or_buffers_changed anyway. */
9585 if (windows_or_buffers_changed
9586 || !NILP (w->update_mode_line)
9587 || update_mode_lines
9588 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
9589 < BUF_MODIFF (XBUFFER (w->buffer)))
9590 != !NILP (w->last_had_star))
9591 || ((!NILP (Vtransient_mark_mode)
9592 && !NILP (XBUFFER (w->buffer)->mark_active))
9593 != !NILP (w->region_showing)))
9594 {
9595 struct buffer *prev = current_buffer;
9596 int count = SPECPDL_INDEX ();
9597 Lisp_Object new_tool_bar;
9598 int new_n_tool_bar;
9599 struct gcpro gcpro1;
9600
9601 /* Set current_buffer to the buffer of the selected
9602 window of the frame, so that we get the right local
9603 keymaps. */
9604 set_buffer_internal_1 (XBUFFER (w->buffer));
9605
9606 /* Save match data, if we must. */
9607 if (save_match_data)
9608 record_unwind_save_match_data ();
9609
9610 /* Make sure that we don't accidentally use bogus keymaps. */
9611 if (NILP (Voverriding_local_map_menu_flag))
9612 {
9613 specbind (Qoverriding_terminal_local_map, Qnil);
9614 specbind (Qoverriding_local_map, Qnil);
9615 }
9616
9617 GCPRO1 (new_tool_bar);
9618
9619 /* Build desired tool-bar items from keymaps. */
9620 new_tool_bar = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
9621 &new_n_tool_bar);
9622
9623 /* Redisplay the tool-bar if we changed it. */
9624 if (new_n_tool_bar != f->n_tool_bar_items
9625 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
9626 {
9627 /* Redisplay that happens asynchronously due to an expose event
9628 may access f->tool_bar_items. Make sure we update both
9629 variables within BLOCK_INPUT so no such event interrupts. */
9630 BLOCK_INPUT;
9631 f->tool_bar_items = new_tool_bar;
9632 f->n_tool_bar_items = new_n_tool_bar;
9633 w->update_mode_line = Qt;
9634 UNBLOCK_INPUT;
9635 }
9636
9637 UNGCPRO;
9638
9639 unbind_to (count, Qnil);
9640 set_buffer_internal_1 (prev);
9641 }
9642 }
9643 }
9644
9645
9646 /* Set F->desired_tool_bar_string to a Lisp string representing frame
9647 F's desired tool-bar contents. F->tool_bar_items must have
9648 been set up previously by calling prepare_menu_bars. */
9649
9650 static void
9651 build_desired_tool_bar_string (f)
9652 struct frame *f;
9653 {
9654 int i, size, size_needed;
9655 struct gcpro gcpro1, gcpro2, gcpro3;
9656 Lisp_Object image, plist, props;
9657
9658 image = plist = props = Qnil;
9659 GCPRO3 (image, plist, props);
9660
9661 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
9662 Otherwise, make a new string. */
9663
9664 /* The size of the string we might be able to reuse. */
9665 size = (STRINGP (f->desired_tool_bar_string)
9666 ? SCHARS (f->desired_tool_bar_string)
9667 : 0);
9668
9669 /* We need one space in the string for each image. */
9670 size_needed = f->n_tool_bar_items;
9671
9672 /* Reuse f->desired_tool_bar_string, if possible. */
9673 if (size < size_needed || NILP (f->desired_tool_bar_string))
9674 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
9675 make_number (' '));
9676 else
9677 {
9678 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
9679 Fremove_text_properties (make_number (0), make_number (size),
9680 props, f->desired_tool_bar_string);
9681 }
9682
9683 /* Put a `display' property on the string for the images to display,
9684 put a `menu_item' property on tool-bar items with a value that
9685 is the index of the item in F's tool-bar item vector. */
9686 for (i = 0; i < f->n_tool_bar_items; ++i)
9687 {
9688 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
9689
9690 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
9691 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
9692 int hmargin, vmargin, relief, idx, end;
9693 extern Lisp_Object QCrelief, QCmargin, QCconversion;
9694
9695 /* If image is a vector, choose the image according to the
9696 button state. */
9697 image = PROP (TOOL_BAR_ITEM_IMAGES);
9698 if (VECTORP (image))
9699 {
9700 if (enabled_p)
9701 idx = (selected_p
9702 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
9703 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
9704 else
9705 idx = (selected_p
9706 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
9707 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
9708
9709 xassert (ASIZE (image) >= idx);
9710 image = AREF (image, idx);
9711 }
9712 else
9713 idx = -1;
9714
9715 /* Ignore invalid image specifications. */
9716 if (!valid_image_p (image))
9717 continue;
9718
9719 /* Display the tool-bar button pressed, or depressed. */
9720 plist = Fcopy_sequence (XCDR (image));
9721
9722 /* Compute margin and relief to draw. */
9723 relief = (tool_bar_button_relief >= 0
9724 ? tool_bar_button_relief
9725 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
9726 hmargin = vmargin = relief;
9727
9728 if (INTEGERP (Vtool_bar_button_margin)
9729 && XINT (Vtool_bar_button_margin) > 0)
9730 {
9731 hmargin += XFASTINT (Vtool_bar_button_margin);
9732 vmargin += XFASTINT (Vtool_bar_button_margin);
9733 }
9734 else if (CONSP (Vtool_bar_button_margin))
9735 {
9736 if (INTEGERP (XCAR (Vtool_bar_button_margin))
9737 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
9738 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
9739
9740 if (INTEGERP (XCDR (Vtool_bar_button_margin))
9741 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
9742 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
9743 }
9744
9745 if (auto_raise_tool_bar_buttons_p)
9746 {
9747 /* Add a `:relief' property to the image spec if the item is
9748 selected. */
9749 if (selected_p)
9750 {
9751 plist = Fplist_put (plist, QCrelief, make_number (-relief));
9752 hmargin -= relief;
9753 vmargin -= relief;
9754 }
9755 }
9756 else
9757 {
9758 /* If image is selected, display it pressed, i.e. with a
9759 negative relief. If it's not selected, display it with a
9760 raised relief. */
9761 plist = Fplist_put (plist, QCrelief,
9762 (selected_p
9763 ? make_number (-relief)
9764 : make_number (relief)));
9765 hmargin -= relief;
9766 vmargin -= relief;
9767 }
9768
9769 /* Put a margin around the image. */
9770 if (hmargin || vmargin)
9771 {
9772 if (hmargin == vmargin)
9773 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
9774 else
9775 plist = Fplist_put (plist, QCmargin,
9776 Fcons (make_number (hmargin),
9777 make_number (vmargin)));
9778 }
9779
9780 /* If button is not enabled, and we don't have special images
9781 for the disabled state, make the image appear disabled by
9782 applying an appropriate algorithm to it. */
9783 if (!enabled_p && idx < 0)
9784 plist = Fplist_put (plist, QCconversion, Qdisabled);
9785
9786 /* Put a `display' text property on the string for the image to
9787 display. Put a `menu-item' property on the string that gives
9788 the start of this item's properties in the tool-bar items
9789 vector. */
9790 image = Fcons (Qimage, plist);
9791 props = list4 (Qdisplay, image,
9792 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
9793
9794 /* Let the last image hide all remaining spaces in the tool bar
9795 string. The string can be longer than needed when we reuse a
9796 previous string. */
9797 if (i + 1 == f->n_tool_bar_items)
9798 end = SCHARS (f->desired_tool_bar_string);
9799 else
9800 end = i + 1;
9801 Fadd_text_properties (make_number (i), make_number (end),
9802 props, f->desired_tool_bar_string);
9803 #undef PROP
9804 }
9805
9806 UNGCPRO;
9807 }
9808
9809
9810 /* Display one line of the tool-bar of frame IT->f.
9811
9812 HEIGHT specifies the desired height of the tool-bar line.
9813 If the actual height of the glyph row is less than HEIGHT, the
9814 row's height is increased to HEIGHT, and the icons are centered
9815 vertically in the new height.
9816
9817 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
9818 count a final empty row in case the tool-bar width exactly matches
9819 the window width.
9820 */
9821
9822 static void
9823 display_tool_bar_line (it, height)
9824 struct it *it;
9825 int height;
9826 {
9827 struct glyph_row *row = it->glyph_row;
9828 int max_x = it->last_visible_x;
9829 struct glyph *last;
9830
9831 prepare_desired_row (row);
9832 row->y = it->current_y;
9833
9834 /* Note that this isn't made use of if the face hasn't a box,
9835 so there's no need to check the face here. */
9836 it->start_of_box_run_p = 1;
9837
9838 while (it->current_x < max_x)
9839 {
9840 int x, n_glyphs_before, i, nglyphs;
9841 struct it it_before;
9842
9843 /* Get the next display element. */
9844 if (!get_next_display_element (it))
9845 {
9846 /* Don't count empty row if we are counting needed tool-bar lines. */
9847 if (height < 0 && !it->hpos)
9848 return;
9849 break;
9850 }
9851
9852 /* Produce glyphs. */
9853 n_glyphs_before = row->used[TEXT_AREA];
9854 it_before = *it;
9855
9856 PRODUCE_GLYPHS (it);
9857
9858 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
9859 i = 0;
9860 x = it_before.current_x;
9861 while (i < nglyphs)
9862 {
9863 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
9864
9865 if (x + glyph->pixel_width > max_x)
9866 {
9867 /* Glyph doesn't fit on line. Backtrack. */
9868 row->used[TEXT_AREA] = n_glyphs_before;
9869 *it = it_before;
9870 /* If this is the only glyph on this line, it will never fit on the
9871 toolbar, so skip it. But ensure there is at least one glyph,
9872 so we don't accidentally disable the tool-bar. */
9873 if (n_glyphs_before == 0
9874 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
9875 break;
9876 goto out;
9877 }
9878
9879 ++it->hpos;
9880 x += glyph->pixel_width;
9881 ++i;
9882 }
9883
9884 /* Stop at line ends. */
9885 if (ITERATOR_AT_END_OF_LINE_P (it))
9886 break;
9887
9888 set_iterator_to_next (it, 1);
9889 }
9890
9891 out:;
9892
9893 row->displays_text_p = row->used[TEXT_AREA] != 0;
9894
9895 /* Use default face for the border below the tool bar.
9896
9897 FIXME: When auto-resize-tool-bars is grow-only, there is
9898 no additional border below the possibly empty tool-bar lines.
9899 So to make the extra empty lines look "normal", we have to
9900 use the tool-bar face for the border too. */
9901 if (!row->displays_text_p && !EQ (Vauto_resize_tool_bars, Qgrow_only))
9902 it->face_id = DEFAULT_FACE_ID;
9903
9904 extend_face_to_end_of_line (it);
9905 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
9906 last->right_box_line_p = 1;
9907 if (last == row->glyphs[TEXT_AREA])
9908 last->left_box_line_p = 1;
9909
9910 /* Make line the desired height and center it vertically. */
9911 if ((height -= it->max_ascent + it->max_descent) > 0)
9912 {
9913 /* Don't add more than one line height. */
9914 height %= FRAME_LINE_HEIGHT (it->f);
9915 it->max_ascent += height / 2;
9916 it->max_descent += (height + 1) / 2;
9917 }
9918
9919 compute_line_metrics (it);
9920
9921 /* If line is empty, make it occupy the rest of the tool-bar. */
9922 if (!row->displays_text_p)
9923 {
9924 row->height = row->phys_height = it->last_visible_y - row->y;
9925 row->visible_height = row->height;
9926 row->ascent = row->phys_ascent = 0;
9927 row->extra_line_spacing = 0;
9928 }
9929
9930 row->full_width_p = 1;
9931 row->continued_p = 0;
9932 row->truncated_on_left_p = 0;
9933 row->truncated_on_right_p = 0;
9934
9935 it->current_x = it->hpos = 0;
9936 it->current_y += row->height;
9937 ++it->vpos;
9938 ++it->glyph_row;
9939 }
9940
9941
9942 /* Max tool-bar height. */
9943
9944 #define MAX_FRAME_TOOL_BAR_HEIGHT(f) \
9945 ((FRAME_LINE_HEIGHT (f) * FRAME_LINES (f)))
9946
9947 /* Value is the number of screen lines needed to make all tool-bar
9948 items of frame F visible. The number of actual rows needed is
9949 returned in *N_ROWS if non-NULL. */
9950
9951 static int
9952 tool_bar_lines_needed (f, n_rows)
9953 struct frame *f;
9954 int *n_rows;
9955 {
9956 struct window *w = XWINDOW (f->tool_bar_window);
9957 struct it it;
9958 /* tool_bar_lines_needed is called from redisplay_tool_bar after building
9959 the desired matrix, so use (unused) mode-line row as temporary row to
9960 avoid destroying the first tool-bar row. */
9961 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
9962
9963 /* Initialize an iterator for iteration over
9964 F->desired_tool_bar_string in the tool-bar window of frame F. */
9965 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
9966 it.first_visible_x = 0;
9967 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
9968 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
9969
9970 while (!ITERATOR_AT_END_P (&it))
9971 {
9972 clear_glyph_row (temp_row);
9973 it.glyph_row = temp_row;
9974 display_tool_bar_line (&it, -1);
9975 }
9976 clear_glyph_row (temp_row);
9977
9978 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
9979 if (n_rows)
9980 *n_rows = it.vpos > 0 ? it.vpos : -1;
9981
9982 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
9983 }
9984
9985
9986 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
9987 0, 1, 0,
9988 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
9989 (frame)
9990 Lisp_Object frame;
9991 {
9992 struct frame *f;
9993 struct window *w;
9994 int nlines = 0;
9995
9996 if (NILP (frame))
9997 frame = selected_frame;
9998 else
9999 CHECK_FRAME (frame);
10000 f = XFRAME (frame);
10001
10002 if (WINDOWP (f->tool_bar_window)
10003 || (w = XWINDOW (f->tool_bar_window),
10004 WINDOW_TOTAL_LINES (w) > 0))
10005 {
10006 update_tool_bar (f, 1);
10007 if (f->n_tool_bar_items)
10008 {
10009 build_desired_tool_bar_string (f);
10010 nlines = tool_bar_lines_needed (f, NULL);
10011 }
10012 }
10013
10014 return make_number (nlines);
10015 }
10016
10017
10018 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
10019 height should be changed. */
10020
10021 static int
10022 redisplay_tool_bar (f)
10023 struct frame *f;
10024 {
10025 struct window *w;
10026 struct it it;
10027 struct glyph_row *row;
10028
10029 #if defined (USE_GTK) || USE_MAC_TOOLBAR
10030 if (FRAME_EXTERNAL_TOOL_BAR (f))
10031 update_frame_tool_bar (f);
10032 return 0;
10033 #endif
10034
10035 /* If frame hasn't a tool-bar window or if it is zero-height, don't
10036 do anything. This means you must start with tool-bar-lines
10037 non-zero to get the auto-sizing effect. Or in other words, you
10038 can turn off tool-bars by specifying tool-bar-lines zero. */
10039 if (!WINDOWP (f->tool_bar_window)
10040 || (w = XWINDOW (f->tool_bar_window),
10041 WINDOW_TOTAL_LINES (w) == 0))
10042 return 0;
10043
10044 /* Set up an iterator for the tool-bar window. */
10045 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
10046 it.first_visible_x = 0;
10047 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
10048 row = it.glyph_row;
10049
10050 /* Build a string that represents the contents of the tool-bar. */
10051 build_desired_tool_bar_string (f);
10052 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
10053
10054 if (f->n_tool_bar_rows == 0)
10055 {
10056 int nlines;
10057
10058 if ((nlines = tool_bar_lines_needed (f, &f->n_tool_bar_rows),
10059 nlines != WINDOW_TOTAL_LINES (w)))
10060 {
10061 extern Lisp_Object Qtool_bar_lines;
10062 Lisp_Object frame;
10063 int old_height = WINDOW_TOTAL_LINES (w);
10064
10065 XSETFRAME (frame, f);
10066 Fmodify_frame_parameters (frame,
10067 Fcons (Fcons (Qtool_bar_lines,
10068 make_number (nlines)),
10069 Qnil));
10070 if (WINDOW_TOTAL_LINES (w) != old_height)
10071 {
10072 clear_glyph_matrix (w->desired_matrix);
10073 fonts_changed_p = 1;
10074 return 1;
10075 }
10076 }
10077 }
10078
10079 /* Display as many lines as needed to display all tool-bar items. */
10080
10081 if (f->n_tool_bar_rows > 0)
10082 {
10083 int border, rows, height, extra;
10084
10085 if (INTEGERP (Vtool_bar_border))
10086 border = XINT (Vtool_bar_border);
10087 else if (EQ (Vtool_bar_border, Qinternal_border_width))
10088 border = FRAME_INTERNAL_BORDER_WIDTH (f);
10089 else if (EQ (Vtool_bar_border, Qborder_width))
10090 border = f->border_width;
10091 else
10092 border = 0;
10093 if (border < 0)
10094 border = 0;
10095
10096 rows = f->n_tool_bar_rows;
10097 height = max (1, (it.last_visible_y - border) / rows);
10098 extra = it.last_visible_y - border - height * rows;
10099
10100 while (it.current_y < it.last_visible_y)
10101 {
10102 int h = 0;
10103 if (extra > 0 && rows-- > 0)
10104 {
10105 h = (extra + rows - 1) / rows;
10106 extra -= h;
10107 }
10108 display_tool_bar_line (&it, height + h);
10109 }
10110 }
10111 else
10112 {
10113 while (it.current_y < it.last_visible_y)
10114 display_tool_bar_line (&it, 0);
10115 }
10116
10117 /* It doesn't make much sense to try scrolling in the tool-bar
10118 window, so don't do it. */
10119 w->desired_matrix->no_scrolling_p = 1;
10120 w->must_be_updated_p = 1;
10121
10122 if (!NILP (Vauto_resize_tool_bars))
10123 {
10124 int max_tool_bar_height = MAX_FRAME_TOOL_BAR_HEIGHT (f);
10125 int change_height_p = 0;
10126
10127 /* If we couldn't display everything, change the tool-bar's
10128 height if there is room for more. */
10129 if (IT_STRING_CHARPOS (it) < it.end_charpos
10130 && it.current_y < max_tool_bar_height)
10131 change_height_p = 1;
10132
10133 row = it.glyph_row - 1;
10134
10135 /* If there are blank lines at the end, except for a partially
10136 visible blank line at the end that is smaller than
10137 FRAME_LINE_HEIGHT, change the tool-bar's height. */
10138 if (!row->displays_text_p
10139 && row->height >= FRAME_LINE_HEIGHT (f))
10140 change_height_p = 1;
10141
10142 /* If row displays tool-bar items, but is partially visible,
10143 change the tool-bar's height. */
10144 if (row->displays_text_p
10145 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y
10146 && MATRIX_ROW_BOTTOM_Y (row) < max_tool_bar_height)
10147 change_height_p = 1;
10148
10149 /* Resize windows as needed by changing the `tool-bar-lines'
10150 frame parameter. */
10151 if (change_height_p)
10152 {
10153 extern Lisp_Object Qtool_bar_lines;
10154 Lisp_Object frame;
10155 int old_height = WINDOW_TOTAL_LINES (w);
10156 int nrows;
10157 int nlines = tool_bar_lines_needed (f, &nrows);
10158
10159 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
10160 && !f->minimize_tool_bar_window_p)
10161 ? (nlines > old_height)
10162 : (nlines != old_height));
10163 f->minimize_tool_bar_window_p = 0;
10164
10165 if (change_height_p)
10166 {
10167 XSETFRAME (frame, f);
10168 Fmodify_frame_parameters (frame,
10169 Fcons (Fcons (Qtool_bar_lines,
10170 make_number (nlines)),
10171 Qnil));
10172 if (WINDOW_TOTAL_LINES (w) != old_height)
10173 {
10174 clear_glyph_matrix (w->desired_matrix);
10175 f->n_tool_bar_rows = nrows;
10176 fonts_changed_p = 1;
10177 return 1;
10178 }
10179 }
10180 }
10181 }
10182
10183 f->minimize_tool_bar_window_p = 0;
10184 return 0;
10185 }
10186
10187
10188 /* Get information about the tool-bar item which is displayed in GLYPH
10189 on frame F. Return in *PROP_IDX the index where tool-bar item
10190 properties start in F->tool_bar_items. Value is zero if
10191 GLYPH doesn't display a tool-bar item. */
10192
10193 static int
10194 tool_bar_item_info (f, glyph, prop_idx)
10195 struct frame *f;
10196 struct glyph *glyph;
10197 int *prop_idx;
10198 {
10199 Lisp_Object prop;
10200 int success_p;
10201 int charpos;
10202
10203 /* This function can be called asynchronously, which means we must
10204 exclude any possibility that Fget_text_property signals an
10205 error. */
10206 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
10207 charpos = max (0, charpos);
10208
10209 /* Get the text property `menu-item' at pos. The value of that
10210 property is the start index of this item's properties in
10211 F->tool_bar_items. */
10212 prop = Fget_text_property (make_number (charpos),
10213 Qmenu_item, f->current_tool_bar_string);
10214 if (INTEGERP (prop))
10215 {
10216 *prop_idx = XINT (prop);
10217 success_p = 1;
10218 }
10219 else
10220 success_p = 0;
10221
10222 return success_p;
10223 }
10224
10225 \f
10226 /* Get information about the tool-bar item at position X/Y on frame F.
10227 Return in *GLYPH a pointer to the glyph of the tool-bar item in
10228 the current matrix of the tool-bar window of F, or NULL if not
10229 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
10230 item in F->tool_bar_items. Value is
10231
10232 -1 if X/Y is not on a tool-bar item
10233 0 if X/Y is on the same item that was highlighted before.
10234 1 otherwise. */
10235
10236 static int
10237 get_tool_bar_item (f, x, y, glyph, hpos, vpos, prop_idx)
10238 struct frame *f;
10239 int x, y;
10240 struct glyph **glyph;
10241 int *hpos, *vpos, *prop_idx;
10242 {
10243 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10244 struct window *w = XWINDOW (f->tool_bar_window);
10245 int area;
10246
10247 /* Find the glyph under X/Y. */
10248 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
10249 if (*glyph == NULL)
10250 return -1;
10251
10252 /* Get the start of this tool-bar item's properties in
10253 f->tool_bar_items. */
10254 if (!tool_bar_item_info (f, *glyph, prop_idx))
10255 return -1;
10256
10257 /* Is mouse on the highlighted item? */
10258 if (EQ (f->tool_bar_window, dpyinfo->mouse_face_window)
10259 && *vpos >= dpyinfo->mouse_face_beg_row
10260 && *vpos <= dpyinfo->mouse_face_end_row
10261 && (*vpos > dpyinfo->mouse_face_beg_row
10262 || *hpos >= dpyinfo->mouse_face_beg_col)
10263 && (*vpos < dpyinfo->mouse_face_end_row
10264 || *hpos < dpyinfo->mouse_face_end_col
10265 || dpyinfo->mouse_face_past_end))
10266 return 0;
10267
10268 return 1;
10269 }
10270
10271
10272 /* EXPORT:
10273 Handle mouse button event on the tool-bar of frame F, at
10274 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
10275 0 for button release. MODIFIERS is event modifiers for button
10276 release. */
10277
10278 void
10279 handle_tool_bar_click (f, x, y, down_p, modifiers)
10280 struct frame *f;
10281 int x, y, down_p;
10282 unsigned int modifiers;
10283 {
10284 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10285 struct window *w = XWINDOW (f->tool_bar_window);
10286 int hpos, vpos, prop_idx;
10287 struct glyph *glyph;
10288 Lisp_Object enabled_p;
10289
10290 /* If not on the highlighted tool-bar item, return. */
10291 frame_to_window_pixel_xy (w, &x, &y);
10292 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
10293 return;
10294
10295 /* If item is disabled, do nothing. */
10296 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
10297 if (NILP (enabled_p))
10298 return;
10299
10300 if (down_p)
10301 {
10302 /* Show item in pressed state. */
10303 show_mouse_face (dpyinfo, DRAW_IMAGE_SUNKEN);
10304 dpyinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
10305 last_tool_bar_item = prop_idx;
10306 }
10307 else
10308 {
10309 Lisp_Object key, frame;
10310 struct input_event event;
10311 EVENT_INIT (event);
10312
10313 /* Show item in released state. */
10314 show_mouse_face (dpyinfo, DRAW_IMAGE_RAISED);
10315 dpyinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
10316
10317 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
10318
10319 XSETFRAME (frame, f);
10320 event.kind = TOOL_BAR_EVENT;
10321 event.frame_or_window = frame;
10322 event.arg = frame;
10323 kbd_buffer_store_event (&event);
10324
10325 event.kind = TOOL_BAR_EVENT;
10326 event.frame_or_window = frame;
10327 event.arg = key;
10328 event.modifiers = modifiers;
10329 kbd_buffer_store_event (&event);
10330 last_tool_bar_item = -1;
10331 }
10332 }
10333
10334
10335 /* Possibly highlight a tool-bar item on frame F when mouse moves to
10336 tool-bar window-relative coordinates X/Y. Called from
10337 note_mouse_highlight. */
10338
10339 static void
10340 note_tool_bar_highlight (f, x, y)
10341 struct frame *f;
10342 int x, y;
10343 {
10344 Lisp_Object window = f->tool_bar_window;
10345 struct window *w = XWINDOW (window);
10346 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10347 int hpos, vpos;
10348 struct glyph *glyph;
10349 struct glyph_row *row;
10350 int i;
10351 Lisp_Object enabled_p;
10352 int prop_idx;
10353 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
10354 int mouse_down_p, rc;
10355
10356 /* Function note_mouse_highlight is called with negative x(y
10357 values when mouse moves outside of the frame. */
10358 if (x <= 0 || y <= 0)
10359 {
10360 clear_mouse_face (dpyinfo);
10361 return;
10362 }
10363
10364 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
10365 if (rc < 0)
10366 {
10367 /* Not on tool-bar item. */
10368 clear_mouse_face (dpyinfo);
10369 return;
10370 }
10371 else if (rc == 0)
10372 /* On same tool-bar item as before. */
10373 goto set_help_echo;
10374
10375 clear_mouse_face (dpyinfo);
10376
10377 /* Mouse is down, but on different tool-bar item? */
10378 mouse_down_p = (dpyinfo->grabbed
10379 && f == last_mouse_frame
10380 && FRAME_LIVE_P (f));
10381 if (mouse_down_p
10382 && last_tool_bar_item != prop_idx)
10383 return;
10384
10385 dpyinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
10386 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
10387
10388 /* If tool-bar item is not enabled, don't highlight it. */
10389 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
10390 if (!NILP (enabled_p))
10391 {
10392 /* Compute the x-position of the glyph. In front and past the
10393 image is a space. We include this in the highlighted area. */
10394 row = MATRIX_ROW (w->current_matrix, vpos);
10395 for (i = x = 0; i < hpos; ++i)
10396 x += row->glyphs[TEXT_AREA][i].pixel_width;
10397
10398 /* Record this as the current active region. */
10399 dpyinfo->mouse_face_beg_col = hpos;
10400 dpyinfo->mouse_face_beg_row = vpos;
10401 dpyinfo->mouse_face_beg_x = x;
10402 dpyinfo->mouse_face_beg_y = row->y;
10403 dpyinfo->mouse_face_past_end = 0;
10404
10405 dpyinfo->mouse_face_end_col = hpos + 1;
10406 dpyinfo->mouse_face_end_row = vpos;
10407 dpyinfo->mouse_face_end_x = x + glyph->pixel_width;
10408 dpyinfo->mouse_face_end_y = row->y;
10409 dpyinfo->mouse_face_window = window;
10410 dpyinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
10411
10412 /* Display it as active. */
10413 show_mouse_face (dpyinfo, draw);
10414 dpyinfo->mouse_face_image_state = draw;
10415 }
10416
10417 set_help_echo:
10418
10419 /* Set help_echo_string to a help string to display for this tool-bar item.
10420 XTread_socket does the rest. */
10421 help_echo_object = help_echo_window = Qnil;
10422 help_echo_pos = -1;
10423 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
10424 if (NILP (help_echo_string))
10425 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
10426 }
10427
10428 #endif /* HAVE_WINDOW_SYSTEM */
10429
10430
10431 \f
10432 /************************************************************************
10433 Horizontal scrolling
10434 ************************************************************************/
10435
10436 static int hscroll_window_tree P_ ((Lisp_Object));
10437 static int hscroll_windows P_ ((Lisp_Object));
10438
10439 /* For all leaf windows in the window tree rooted at WINDOW, set their
10440 hscroll value so that PT is (i) visible in the window, and (ii) so
10441 that it is not within a certain margin at the window's left and
10442 right border. Value is non-zero if any window's hscroll has been
10443 changed. */
10444
10445 static int
10446 hscroll_window_tree (window)
10447 Lisp_Object window;
10448 {
10449 int hscrolled_p = 0;
10450 int hscroll_relative_p = FLOATP (Vhscroll_step);
10451 int hscroll_step_abs = 0;
10452 double hscroll_step_rel = 0;
10453
10454 if (hscroll_relative_p)
10455 {
10456 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
10457 if (hscroll_step_rel < 0)
10458 {
10459 hscroll_relative_p = 0;
10460 hscroll_step_abs = 0;
10461 }
10462 }
10463 else if (INTEGERP (Vhscroll_step))
10464 {
10465 hscroll_step_abs = XINT (Vhscroll_step);
10466 if (hscroll_step_abs < 0)
10467 hscroll_step_abs = 0;
10468 }
10469 else
10470 hscroll_step_abs = 0;
10471
10472 while (WINDOWP (window))
10473 {
10474 struct window *w = XWINDOW (window);
10475
10476 if (WINDOWP (w->hchild))
10477 hscrolled_p |= hscroll_window_tree (w->hchild);
10478 else if (WINDOWP (w->vchild))
10479 hscrolled_p |= hscroll_window_tree (w->vchild);
10480 else if (w->cursor.vpos >= 0)
10481 {
10482 int h_margin;
10483 int text_area_width;
10484 struct glyph_row *current_cursor_row
10485 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
10486 struct glyph_row *desired_cursor_row
10487 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
10488 struct glyph_row *cursor_row
10489 = (desired_cursor_row->enabled_p
10490 ? desired_cursor_row
10491 : current_cursor_row);
10492
10493 text_area_width = window_box_width (w, TEXT_AREA);
10494
10495 /* Scroll when cursor is inside this scroll margin. */
10496 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
10497
10498 if ((XFASTINT (w->hscroll)
10499 && w->cursor.x <= h_margin)
10500 || (cursor_row->enabled_p
10501 && cursor_row->truncated_on_right_p
10502 && (w->cursor.x >= text_area_width - h_margin)))
10503 {
10504 struct it it;
10505 int hscroll;
10506 struct buffer *saved_current_buffer;
10507 int pt;
10508 int wanted_x;
10509
10510 /* Find point in a display of infinite width. */
10511 saved_current_buffer = current_buffer;
10512 current_buffer = XBUFFER (w->buffer);
10513
10514 if (w == XWINDOW (selected_window))
10515 pt = BUF_PT (current_buffer);
10516 else
10517 {
10518 pt = marker_position (w->pointm);
10519 pt = max (BEGV, pt);
10520 pt = min (ZV, pt);
10521 }
10522
10523 /* Move iterator to pt starting at cursor_row->start in
10524 a line with infinite width. */
10525 init_to_row_start (&it, w, cursor_row);
10526 it.last_visible_x = INFINITY;
10527 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
10528 current_buffer = saved_current_buffer;
10529
10530 /* Position cursor in window. */
10531 if (!hscroll_relative_p && hscroll_step_abs == 0)
10532 hscroll = max (0, (it.current_x
10533 - (ITERATOR_AT_END_OF_LINE_P (&it)
10534 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
10535 : (text_area_width / 2))))
10536 / FRAME_COLUMN_WIDTH (it.f);
10537 else if (w->cursor.x >= text_area_width - h_margin)
10538 {
10539 if (hscroll_relative_p)
10540 wanted_x = text_area_width * (1 - hscroll_step_rel)
10541 - h_margin;
10542 else
10543 wanted_x = text_area_width
10544 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
10545 - h_margin;
10546 hscroll
10547 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
10548 }
10549 else
10550 {
10551 if (hscroll_relative_p)
10552 wanted_x = text_area_width * hscroll_step_rel
10553 + h_margin;
10554 else
10555 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
10556 + h_margin;
10557 hscroll
10558 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
10559 }
10560 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
10561
10562 /* Don't call Fset_window_hscroll if value hasn't
10563 changed because it will prevent redisplay
10564 optimizations. */
10565 if (XFASTINT (w->hscroll) != hscroll)
10566 {
10567 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
10568 w->hscroll = make_number (hscroll);
10569 hscrolled_p = 1;
10570 }
10571 }
10572 }
10573
10574 window = w->next;
10575 }
10576
10577 /* Value is non-zero if hscroll of any leaf window has been changed. */
10578 return hscrolled_p;
10579 }
10580
10581
10582 /* Set hscroll so that cursor is visible and not inside horizontal
10583 scroll margins for all windows in the tree rooted at WINDOW. See
10584 also hscroll_window_tree above. Value is non-zero if any window's
10585 hscroll has been changed. If it has, desired matrices on the frame
10586 of WINDOW are cleared. */
10587
10588 static int
10589 hscroll_windows (window)
10590 Lisp_Object window;
10591 {
10592 int hscrolled_p;
10593
10594 if (automatic_hscrolling_p)
10595 {
10596 hscrolled_p = hscroll_window_tree (window);
10597 if (hscrolled_p)
10598 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
10599 }
10600 else
10601 hscrolled_p = 0;
10602 return hscrolled_p;
10603 }
10604
10605
10606 \f
10607 /************************************************************************
10608 Redisplay
10609 ************************************************************************/
10610
10611 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
10612 to a non-zero value. This is sometimes handy to have in a debugger
10613 session. */
10614
10615 #if GLYPH_DEBUG
10616
10617 /* First and last unchanged row for try_window_id. */
10618
10619 int debug_first_unchanged_at_end_vpos;
10620 int debug_last_unchanged_at_beg_vpos;
10621
10622 /* Delta vpos and y. */
10623
10624 int debug_dvpos, debug_dy;
10625
10626 /* Delta in characters and bytes for try_window_id. */
10627
10628 int debug_delta, debug_delta_bytes;
10629
10630 /* Values of window_end_pos and window_end_vpos at the end of
10631 try_window_id. */
10632
10633 EMACS_INT debug_end_pos, debug_end_vpos;
10634
10635 /* Append a string to W->desired_matrix->method. FMT is a printf
10636 format string. A1...A9 are a supplement for a variable-length
10637 argument list. If trace_redisplay_p is non-zero also printf the
10638 resulting string to stderr. */
10639
10640 static void
10641 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
10642 struct window *w;
10643 char *fmt;
10644 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
10645 {
10646 char buffer[512];
10647 char *method = w->desired_matrix->method;
10648 int len = strlen (method);
10649 int size = sizeof w->desired_matrix->method;
10650 int remaining = size - len - 1;
10651
10652 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
10653 if (len && remaining)
10654 {
10655 method[len] = '|';
10656 --remaining, ++len;
10657 }
10658
10659 strncpy (method + len, buffer, remaining);
10660
10661 if (trace_redisplay_p)
10662 fprintf (stderr, "%p (%s): %s\n",
10663 w,
10664 ((BUFFERP (w->buffer)
10665 && STRINGP (XBUFFER (w->buffer)->name))
10666 ? (char *) SDATA (XBUFFER (w->buffer)->name)
10667 : "no buffer"),
10668 buffer);
10669 }
10670
10671 #endif /* GLYPH_DEBUG */
10672
10673
10674 /* Value is non-zero if all changes in window W, which displays
10675 current_buffer, are in the text between START and END. START is a
10676 buffer position, END is given as a distance from Z. Used in
10677 redisplay_internal for display optimization. */
10678
10679 static INLINE int
10680 text_outside_line_unchanged_p (w, start, end)
10681 struct window *w;
10682 int start, end;
10683 {
10684 int unchanged_p = 1;
10685
10686 /* If text or overlays have changed, see where. */
10687 if (XFASTINT (w->last_modified) < MODIFF
10688 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
10689 {
10690 /* Gap in the line? */
10691 if (GPT < start || Z - GPT < end)
10692 unchanged_p = 0;
10693
10694 /* Changes start in front of the line, or end after it? */
10695 if (unchanged_p
10696 && (BEG_UNCHANGED < start - 1
10697 || END_UNCHANGED < end))
10698 unchanged_p = 0;
10699
10700 /* If selective display, can't optimize if changes start at the
10701 beginning of the line. */
10702 if (unchanged_p
10703 && INTEGERP (current_buffer->selective_display)
10704 && XINT (current_buffer->selective_display) > 0
10705 && (BEG_UNCHANGED < start || GPT <= start))
10706 unchanged_p = 0;
10707
10708 /* If there are overlays at the start or end of the line, these
10709 may have overlay strings with newlines in them. A change at
10710 START, for instance, may actually concern the display of such
10711 overlay strings as well, and they are displayed on different
10712 lines. So, quickly rule out this case. (For the future, it
10713 might be desirable to implement something more telling than
10714 just BEG/END_UNCHANGED.) */
10715 if (unchanged_p)
10716 {
10717 if (BEG + BEG_UNCHANGED == start
10718 && overlay_touches_p (start))
10719 unchanged_p = 0;
10720 if (END_UNCHANGED == end
10721 && overlay_touches_p (Z - end))
10722 unchanged_p = 0;
10723 }
10724 }
10725
10726 return unchanged_p;
10727 }
10728
10729
10730 /* Do a frame update, taking possible shortcuts into account. This is
10731 the main external entry point for redisplay.
10732
10733 If the last redisplay displayed an echo area message and that message
10734 is no longer requested, we clear the echo area or bring back the
10735 mini-buffer if that is in use. */
10736
10737 void
10738 redisplay ()
10739 {
10740 redisplay_internal (0);
10741 }
10742
10743
10744 static Lisp_Object
10745 overlay_arrow_string_or_property (var)
10746 Lisp_Object var;
10747 {
10748 Lisp_Object val;
10749
10750 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
10751 return val;
10752
10753 return Voverlay_arrow_string;
10754 }
10755
10756 /* Return 1 if there are any overlay-arrows in current_buffer. */
10757 static int
10758 overlay_arrow_in_current_buffer_p ()
10759 {
10760 Lisp_Object vlist;
10761
10762 for (vlist = Voverlay_arrow_variable_list;
10763 CONSP (vlist);
10764 vlist = XCDR (vlist))
10765 {
10766 Lisp_Object var = XCAR (vlist);
10767 Lisp_Object val;
10768
10769 if (!SYMBOLP (var))
10770 continue;
10771 val = find_symbol_value (var);
10772 if (MARKERP (val)
10773 && current_buffer == XMARKER (val)->buffer)
10774 return 1;
10775 }
10776 return 0;
10777 }
10778
10779
10780 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
10781 has changed. */
10782
10783 static int
10784 overlay_arrows_changed_p ()
10785 {
10786 Lisp_Object vlist;
10787
10788 for (vlist = Voverlay_arrow_variable_list;
10789 CONSP (vlist);
10790 vlist = XCDR (vlist))
10791 {
10792 Lisp_Object var = XCAR (vlist);
10793 Lisp_Object val, pstr;
10794
10795 if (!SYMBOLP (var))
10796 continue;
10797 val = find_symbol_value (var);
10798 if (!MARKERP (val))
10799 continue;
10800 if (! EQ (COERCE_MARKER (val),
10801 Fget (var, Qlast_arrow_position))
10802 || ! (pstr = overlay_arrow_string_or_property (var),
10803 EQ (pstr, Fget (var, Qlast_arrow_string))))
10804 return 1;
10805 }
10806 return 0;
10807 }
10808
10809 /* Mark overlay arrows to be updated on next redisplay. */
10810
10811 static void
10812 update_overlay_arrows (up_to_date)
10813 int up_to_date;
10814 {
10815 Lisp_Object vlist;
10816
10817 for (vlist = Voverlay_arrow_variable_list;
10818 CONSP (vlist);
10819 vlist = XCDR (vlist))
10820 {
10821 Lisp_Object var = XCAR (vlist);
10822
10823 if (!SYMBOLP (var))
10824 continue;
10825
10826 if (up_to_date > 0)
10827 {
10828 Lisp_Object val = find_symbol_value (var);
10829 Fput (var, Qlast_arrow_position,
10830 COERCE_MARKER (val));
10831 Fput (var, Qlast_arrow_string,
10832 overlay_arrow_string_or_property (var));
10833 }
10834 else if (up_to_date < 0
10835 || !NILP (Fget (var, Qlast_arrow_position)))
10836 {
10837 Fput (var, Qlast_arrow_position, Qt);
10838 Fput (var, Qlast_arrow_string, Qt);
10839 }
10840 }
10841 }
10842
10843
10844 /* Return overlay arrow string to display at row.
10845 Return integer (bitmap number) for arrow bitmap in left fringe.
10846 Return nil if no overlay arrow. */
10847
10848 static Lisp_Object
10849 overlay_arrow_at_row (it, row)
10850 struct it *it;
10851 struct glyph_row *row;
10852 {
10853 Lisp_Object vlist;
10854
10855 for (vlist = Voverlay_arrow_variable_list;
10856 CONSP (vlist);
10857 vlist = XCDR (vlist))
10858 {
10859 Lisp_Object var = XCAR (vlist);
10860 Lisp_Object val;
10861
10862 if (!SYMBOLP (var))
10863 continue;
10864
10865 val = find_symbol_value (var);
10866
10867 if (MARKERP (val)
10868 && current_buffer == XMARKER (val)->buffer
10869 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
10870 {
10871 if (FRAME_WINDOW_P (it->f)
10872 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
10873 {
10874 #ifdef HAVE_WINDOW_SYSTEM
10875 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
10876 {
10877 int fringe_bitmap;
10878 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
10879 return make_number (fringe_bitmap);
10880 }
10881 #endif
10882 return make_number (-1); /* Use default arrow bitmap */
10883 }
10884 return overlay_arrow_string_or_property (var);
10885 }
10886 }
10887
10888 return Qnil;
10889 }
10890
10891 /* Return 1 if point moved out of or into a composition. Otherwise
10892 return 0. PREV_BUF and PREV_PT are the last point buffer and
10893 position. BUF and PT are the current point buffer and position. */
10894
10895 int
10896 check_point_in_composition (prev_buf, prev_pt, buf, pt)
10897 struct buffer *prev_buf, *buf;
10898 int prev_pt, pt;
10899 {
10900 EMACS_INT start, end;
10901 Lisp_Object prop;
10902 Lisp_Object buffer;
10903
10904 XSETBUFFER (buffer, buf);
10905 /* Check a composition at the last point if point moved within the
10906 same buffer. */
10907 if (prev_buf == buf)
10908 {
10909 if (prev_pt == pt)
10910 /* Point didn't move. */
10911 return 0;
10912
10913 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
10914 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
10915 && COMPOSITION_VALID_P (start, end, prop)
10916 && start < prev_pt && end > prev_pt)
10917 /* The last point was within the composition. Return 1 iff
10918 point moved out of the composition. */
10919 return (pt <= start || pt >= end);
10920 }
10921
10922 /* Check a composition at the current point. */
10923 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
10924 && find_composition (pt, -1, &start, &end, &prop, buffer)
10925 && COMPOSITION_VALID_P (start, end, prop)
10926 && start < pt && end > pt);
10927 }
10928
10929
10930 /* Reconsider the setting of B->clip_changed which is displayed
10931 in window W. */
10932
10933 static INLINE void
10934 reconsider_clip_changes (w, b)
10935 struct window *w;
10936 struct buffer *b;
10937 {
10938 if (b->clip_changed
10939 && !NILP (w->window_end_valid)
10940 && w->current_matrix->buffer == b
10941 && w->current_matrix->zv == BUF_ZV (b)
10942 && w->current_matrix->begv == BUF_BEGV (b))
10943 b->clip_changed = 0;
10944
10945 /* If display wasn't paused, and W is not a tool bar window, see if
10946 point has been moved into or out of a composition. In that case,
10947 we set b->clip_changed to 1 to force updating the screen. If
10948 b->clip_changed has already been set to 1, we can skip this
10949 check. */
10950 if (!b->clip_changed
10951 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
10952 {
10953 int pt;
10954
10955 if (w == XWINDOW (selected_window))
10956 pt = BUF_PT (current_buffer);
10957 else
10958 pt = marker_position (w->pointm);
10959
10960 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
10961 || pt != XINT (w->last_point))
10962 && check_point_in_composition (w->current_matrix->buffer,
10963 XINT (w->last_point),
10964 XBUFFER (w->buffer), pt))
10965 b->clip_changed = 1;
10966 }
10967 }
10968 \f
10969
10970 /* Select FRAME to forward the values of frame-local variables into C
10971 variables so that the redisplay routines can access those values
10972 directly. */
10973
10974 static void
10975 select_frame_for_redisplay (frame)
10976 Lisp_Object frame;
10977 {
10978 Lisp_Object tail, sym, val;
10979 Lisp_Object old = selected_frame;
10980
10981 xassert (FRAMEP (frame) && FRAME_LIVE_P (XFRAME (frame)));
10982
10983 selected_frame = frame;
10984
10985 for (tail = XFRAME (frame)->param_alist; CONSP (tail); tail = XCDR (tail))
10986 if (CONSP (XCAR (tail))
10987 && (sym = XCAR (XCAR (tail)),
10988 SYMBOLP (sym))
10989 && (sym = indirect_variable (sym),
10990 val = SYMBOL_VALUE (sym),
10991 (BUFFER_LOCAL_VALUEP (val)))
10992 && XBUFFER_LOCAL_VALUE (val)->check_frame)
10993 /* Use find_symbol_value rather than Fsymbol_value
10994 to avoid an error if it is void. */
10995 find_symbol_value (sym);
10996
10997 for (tail = XFRAME (old)->param_alist; CONSP (tail); tail = XCDR (tail))
10998 if (CONSP (XCAR (tail))
10999 && (sym = XCAR (XCAR (tail)),
11000 SYMBOLP (sym))
11001 && (sym = indirect_variable (sym),
11002 val = SYMBOL_VALUE (sym),
11003 (BUFFER_LOCAL_VALUEP (val)))
11004 && XBUFFER_LOCAL_VALUE (val)->check_frame)
11005 find_symbol_value (sym);
11006 }
11007
11008
11009 #define STOP_POLLING \
11010 do { if (! polling_stopped_here) stop_polling (); \
11011 polling_stopped_here = 1; } while (0)
11012
11013 #define RESUME_POLLING \
11014 do { if (polling_stopped_here) start_polling (); \
11015 polling_stopped_here = 0; } while (0)
11016
11017
11018 /* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
11019 response to any user action; therefore, we should preserve the echo
11020 area. (Actually, our caller does that job.) Perhaps in the future
11021 avoid recentering windows if it is not necessary; currently that
11022 causes some problems. */
11023
11024 static void
11025 redisplay_internal (preserve_echo_area)
11026 int preserve_echo_area;
11027 {
11028 struct window *w = XWINDOW (selected_window);
11029 struct frame *f;
11030 int pause;
11031 int must_finish = 0;
11032 struct text_pos tlbufpos, tlendpos;
11033 int number_of_visible_frames;
11034 int count, count1;
11035 struct frame *sf;
11036 int polling_stopped_here = 0;
11037 Lisp_Object old_frame = selected_frame;
11038
11039 /* Non-zero means redisplay has to consider all windows on all
11040 frames. Zero means, only selected_window is considered. */
11041 int consider_all_windows_p;
11042
11043 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
11044
11045 /* No redisplay if running in batch mode or frame is not yet fully
11046 initialized, or redisplay is explicitly turned off by setting
11047 Vinhibit_redisplay. */
11048 if (noninteractive
11049 || !NILP (Vinhibit_redisplay))
11050 return;
11051
11052 /* Don't examine these until after testing Vinhibit_redisplay.
11053 When Emacs is shutting down, perhaps because its connection to
11054 X has dropped, we should not look at them at all. */
11055 f = XFRAME (w->frame);
11056 sf = SELECTED_FRAME ();
11057
11058 if (!f->glyphs_initialized_p)
11059 return;
11060
11061 /* The flag redisplay_performed_directly_p is set by
11062 direct_output_for_insert when it already did the whole screen
11063 update necessary. */
11064 if (redisplay_performed_directly_p)
11065 {
11066 redisplay_performed_directly_p = 0;
11067 if (!hscroll_windows (selected_window))
11068 return;
11069 }
11070
11071 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (MAC_OS)
11072 if (popup_activated ())
11073 return;
11074 #endif
11075
11076 /* I don't think this happens but let's be paranoid. */
11077 if (redisplaying_p)
11078 return;
11079
11080 /* Record a function that resets redisplaying_p to its old value
11081 when we leave this function. */
11082 count = SPECPDL_INDEX ();
11083 record_unwind_protect (unwind_redisplay,
11084 Fcons (make_number (redisplaying_p), selected_frame));
11085 ++redisplaying_p;
11086 specbind (Qinhibit_free_realized_faces, Qnil);
11087
11088 {
11089 Lisp_Object tail, frame;
11090
11091 FOR_EACH_FRAME (tail, frame)
11092 {
11093 struct frame *f = XFRAME (frame);
11094 f->already_hscrolled_p = 0;
11095 }
11096 }
11097
11098 retry:
11099 if (!EQ (old_frame, selected_frame)
11100 && FRAME_LIVE_P (XFRAME (old_frame)))
11101 /* When running redisplay, we play a bit fast-and-loose and allow e.g.
11102 selected_frame and selected_window to be temporarily out-of-sync so
11103 when we come back here via `goto retry', we need to resync because we
11104 may need to run Elisp code (via prepare_menu_bars). */
11105 select_frame_for_redisplay (old_frame);
11106
11107 pause = 0;
11108 reconsider_clip_changes (w, current_buffer);
11109 last_escape_glyph_frame = NULL;
11110 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
11111
11112 /* If new fonts have been loaded that make a glyph matrix adjustment
11113 necessary, do it. */
11114 if (fonts_changed_p)
11115 {
11116 adjust_glyphs (NULL);
11117 ++windows_or_buffers_changed;
11118 fonts_changed_p = 0;
11119 }
11120
11121 /* If face_change_count is non-zero, init_iterator will free all
11122 realized faces, which includes the faces referenced from current
11123 matrices. So, we can't reuse current matrices in this case. */
11124 if (face_change_count)
11125 ++windows_or_buffers_changed;
11126
11127 if (FRAME_TERMCAP_P (sf)
11128 && FRAME_TTY (sf)->previous_frame != sf)
11129 {
11130 /* Since frames on a single ASCII terminal share the same
11131 display area, displaying a different frame means redisplay
11132 the whole thing. */
11133 windows_or_buffers_changed++;
11134 SET_FRAME_GARBAGED (sf);
11135 FRAME_TTY (sf)->previous_frame = sf;
11136 }
11137
11138 /* Set the visible flags for all frames. Do this before checking
11139 for resized or garbaged frames; they want to know if their frames
11140 are visible. See the comment in frame.h for
11141 FRAME_SAMPLE_VISIBILITY. */
11142 {
11143 Lisp_Object tail, frame;
11144
11145 number_of_visible_frames = 0;
11146
11147 FOR_EACH_FRAME (tail, frame)
11148 {
11149 struct frame *f = XFRAME (frame);
11150
11151 FRAME_SAMPLE_VISIBILITY (f);
11152 if (FRAME_VISIBLE_P (f))
11153 ++number_of_visible_frames;
11154 clear_desired_matrices (f);
11155 }
11156 }
11157
11158
11159 /* Notice any pending interrupt request to change frame size. */
11160 do_pending_window_change (1);
11161
11162 /* Clear frames marked as garbaged. */
11163 if (frame_garbaged)
11164 clear_garbaged_frames ();
11165
11166 /* Build menubar and tool-bar items. */
11167 if (NILP (Vmemory_full))
11168 prepare_menu_bars ();
11169
11170 if (windows_or_buffers_changed)
11171 update_mode_lines++;
11172
11173 /* Detect case that we need to write or remove a star in the mode line. */
11174 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
11175 {
11176 w->update_mode_line = Qt;
11177 if (buffer_shared > 1)
11178 update_mode_lines++;
11179 }
11180
11181 /* Avoid invocation of point motion hooks by `current_column' below. */
11182 count1 = SPECPDL_INDEX ();
11183 specbind (Qinhibit_point_motion_hooks, Qt);
11184
11185 /* If %c is in the mode line, update it if needed. */
11186 if (!NILP (w->column_number_displayed)
11187 /* This alternative quickly identifies a common case
11188 where no change is needed. */
11189 && !(PT == XFASTINT (w->last_point)
11190 && XFASTINT (w->last_modified) >= MODIFF
11191 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
11192 && (XFASTINT (w->column_number_displayed)
11193 != (int) current_column ())) /* iftc */
11194 w->update_mode_line = Qt;
11195
11196 unbind_to (count1, Qnil);
11197
11198 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
11199
11200 /* The variable buffer_shared is set in redisplay_window and
11201 indicates that we redisplay a buffer in different windows. See
11202 there. */
11203 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
11204 || cursor_type_changed);
11205
11206 /* If specs for an arrow have changed, do thorough redisplay
11207 to ensure we remove any arrow that should no longer exist. */
11208 if (overlay_arrows_changed_p ())
11209 consider_all_windows_p = windows_or_buffers_changed = 1;
11210
11211 /* Normally the message* functions will have already displayed and
11212 updated the echo area, but the frame may have been trashed, or
11213 the update may have been preempted, so display the echo area
11214 again here. Checking message_cleared_p captures the case that
11215 the echo area should be cleared. */
11216 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
11217 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
11218 || (message_cleared_p
11219 && minibuf_level == 0
11220 /* If the mini-window is currently selected, this means the
11221 echo-area doesn't show through. */
11222 && !MINI_WINDOW_P (XWINDOW (selected_window))))
11223 {
11224 int window_height_changed_p = echo_area_display (0);
11225 must_finish = 1;
11226
11227 /* If we don't display the current message, don't clear the
11228 message_cleared_p flag, because, if we did, we wouldn't clear
11229 the echo area in the next redisplay which doesn't preserve
11230 the echo area. */
11231 if (!display_last_displayed_message_p)
11232 message_cleared_p = 0;
11233
11234 if (fonts_changed_p)
11235 goto retry;
11236 else if (window_height_changed_p)
11237 {
11238 consider_all_windows_p = 1;
11239 ++update_mode_lines;
11240 ++windows_or_buffers_changed;
11241
11242 /* If window configuration was changed, frames may have been
11243 marked garbaged. Clear them or we will experience
11244 surprises wrt scrolling. */
11245 if (frame_garbaged)
11246 clear_garbaged_frames ();
11247 }
11248 }
11249 else if (EQ (selected_window, minibuf_window)
11250 && (current_buffer->clip_changed
11251 || XFASTINT (w->last_modified) < MODIFF
11252 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
11253 && resize_mini_window (w, 0))
11254 {
11255 /* Resized active mini-window to fit the size of what it is
11256 showing if its contents might have changed. */
11257 must_finish = 1;
11258 consider_all_windows_p = 1;
11259 ++windows_or_buffers_changed;
11260 ++update_mode_lines;
11261
11262 /* If window configuration was changed, frames may have been
11263 marked garbaged. Clear them or we will experience
11264 surprises wrt scrolling. */
11265 if (frame_garbaged)
11266 clear_garbaged_frames ();
11267 }
11268
11269
11270 /* If showing the region, and mark has changed, we must redisplay
11271 the whole window. The assignment to this_line_start_pos prevents
11272 the optimization directly below this if-statement. */
11273 if (((!NILP (Vtransient_mark_mode)
11274 && !NILP (XBUFFER (w->buffer)->mark_active))
11275 != !NILP (w->region_showing))
11276 || (!NILP (w->region_showing)
11277 && !EQ (w->region_showing,
11278 Fmarker_position (XBUFFER (w->buffer)->mark))))
11279 CHARPOS (this_line_start_pos) = 0;
11280
11281 /* Optimize the case that only the line containing the cursor in the
11282 selected window has changed. Variables starting with this_ are
11283 set in display_line and record information about the line
11284 containing the cursor. */
11285 tlbufpos = this_line_start_pos;
11286 tlendpos = this_line_end_pos;
11287 if (!consider_all_windows_p
11288 && CHARPOS (tlbufpos) > 0
11289 && NILP (w->update_mode_line)
11290 && !current_buffer->clip_changed
11291 && !current_buffer->prevent_redisplay_optimizations_p
11292 && FRAME_VISIBLE_P (XFRAME (w->frame))
11293 && !FRAME_OBSCURED_P (XFRAME (w->frame))
11294 /* Make sure recorded data applies to current buffer, etc. */
11295 && this_line_buffer == current_buffer
11296 && current_buffer == XBUFFER (w->buffer)
11297 && NILP (w->force_start)
11298 && NILP (w->optional_new_start)
11299 /* Point must be on the line that we have info recorded about. */
11300 && PT >= CHARPOS (tlbufpos)
11301 && PT <= Z - CHARPOS (tlendpos)
11302 /* All text outside that line, including its final newline,
11303 must be unchanged */
11304 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
11305 CHARPOS (tlendpos)))
11306 {
11307 if (CHARPOS (tlbufpos) > BEGV
11308 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
11309 && (CHARPOS (tlbufpos) == ZV
11310 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
11311 /* Former continuation line has disappeared by becoming empty */
11312 goto cancel;
11313 else if (XFASTINT (w->last_modified) < MODIFF
11314 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
11315 || MINI_WINDOW_P (w))
11316 {
11317 /* We have to handle the case of continuation around a
11318 wide-column character (See the comment in indent.c around
11319 line 885).
11320
11321 For instance, in the following case:
11322
11323 -------- Insert --------
11324 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
11325 J_I_ ==> J_I_ `^^' are cursors.
11326 ^^ ^^
11327 -------- --------
11328
11329 As we have to redraw the line above, we should goto cancel. */
11330
11331 struct it it;
11332 int line_height_before = this_line_pixel_height;
11333
11334 /* Note that start_display will handle the case that the
11335 line starting at tlbufpos is a continuation lines. */
11336 start_display (&it, w, tlbufpos);
11337
11338 /* Implementation note: It this still necessary? */
11339 if (it.current_x != this_line_start_x)
11340 goto cancel;
11341
11342 TRACE ((stderr, "trying display optimization 1\n"));
11343 w->cursor.vpos = -1;
11344 overlay_arrow_seen = 0;
11345 it.vpos = this_line_vpos;
11346 it.current_y = this_line_y;
11347 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
11348 display_line (&it);
11349
11350 /* If line contains point, is not continued,
11351 and ends at same distance from eob as before, we win */
11352 if (w->cursor.vpos >= 0
11353 /* Line is not continued, otherwise this_line_start_pos
11354 would have been set to 0 in display_line. */
11355 && CHARPOS (this_line_start_pos)
11356 /* Line ends as before. */
11357 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
11358 /* Line has same height as before. Otherwise other lines
11359 would have to be shifted up or down. */
11360 && this_line_pixel_height == line_height_before)
11361 {
11362 /* If this is not the window's last line, we must adjust
11363 the charstarts of the lines below. */
11364 if (it.current_y < it.last_visible_y)
11365 {
11366 struct glyph_row *row
11367 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
11368 int delta, delta_bytes;
11369
11370 if (Z - CHARPOS (tlendpos) == ZV)
11371 {
11372 /* This line ends at end of (accessible part of)
11373 buffer. There is no newline to count. */
11374 delta = (Z
11375 - CHARPOS (tlendpos)
11376 - MATRIX_ROW_START_CHARPOS (row));
11377 delta_bytes = (Z_BYTE
11378 - BYTEPOS (tlendpos)
11379 - MATRIX_ROW_START_BYTEPOS (row));
11380 }
11381 else
11382 {
11383 /* This line ends in a newline. Must take
11384 account of the newline and the rest of the
11385 text that follows. */
11386 delta = (Z
11387 - CHARPOS (tlendpos)
11388 - MATRIX_ROW_START_CHARPOS (row));
11389 delta_bytes = (Z_BYTE
11390 - BYTEPOS (tlendpos)
11391 - MATRIX_ROW_START_BYTEPOS (row));
11392 }
11393
11394 increment_matrix_positions (w->current_matrix,
11395 this_line_vpos + 1,
11396 w->current_matrix->nrows,
11397 delta, delta_bytes);
11398 }
11399
11400 /* If this row displays text now but previously didn't,
11401 or vice versa, w->window_end_vpos may have to be
11402 adjusted. */
11403 if ((it.glyph_row - 1)->displays_text_p)
11404 {
11405 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
11406 XSETINT (w->window_end_vpos, this_line_vpos);
11407 }
11408 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
11409 && this_line_vpos > 0)
11410 XSETINT (w->window_end_vpos, this_line_vpos - 1);
11411 w->window_end_valid = Qnil;
11412
11413 /* Update hint: No need to try to scroll in update_window. */
11414 w->desired_matrix->no_scrolling_p = 1;
11415
11416 #if GLYPH_DEBUG
11417 *w->desired_matrix->method = 0;
11418 debug_method_add (w, "optimization 1");
11419 #endif
11420 #ifdef HAVE_WINDOW_SYSTEM
11421 update_window_fringes (w, 0);
11422 #endif
11423 goto update;
11424 }
11425 else
11426 goto cancel;
11427 }
11428 else if (/* Cursor position hasn't changed. */
11429 PT == XFASTINT (w->last_point)
11430 /* Make sure the cursor was last displayed
11431 in this window. Otherwise we have to reposition it. */
11432 && 0 <= w->cursor.vpos
11433 && WINDOW_TOTAL_LINES (w) > w->cursor.vpos)
11434 {
11435 if (!must_finish)
11436 {
11437 do_pending_window_change (1);
11438
11439 /* We used to always goto end_of_redisplay here, but this
11440 isn't enough if we have a blinking cursor. */
11441 if (w->cursor_off_p == w->last_cursor_off_p)
11442 goto end_of_redisplay;
11443 }
11444 goto update;
11445 }
11446 /* If highlighting the region, or if the cursor is in the echo area,
11447 then we can't just move the cursor. */
11448 else if (! (!NILP (Vtransient_mark_mode)
11449 && !NILP (current_buffer->mark_active))
11450 && (EQ (selected_window, current_buffer->last_selected_window)
11451 || highlight_nonselected_windows)
11452 && NILP (w->region_showing)
11453 && NILP (Vshow_trailing_whitespace)
11454 && !cursor_in_echo_area)
11455 {
11456 struct it it;
11457 struct glyph_row *row;
11458
11459 /* Skip from tlbufpos to PT and see where it is. Note that
11460 PT may be in invisible text. If so, we will end at the
11461 next visible position. */
11462 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
11463 NULL, DEFAULT_FACE_ID);
11464 it.current_x = this_line_start_x;
11465 it.current_y = this_line_y;
11466 it.vpos = this_line_vpos;
11467
11468 /* The call to move_it_to stops in front of PT, but
11469 moves over before-strings. */
11470 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
11471
11472 if (it.vpos == this_line_vpos
11473 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
11474 row->enabled_p))
11475 {
11476 xassert (this_line_vpos == it.vpos);
11477 xassert (this_line_y == it.current_y);
11478 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11479 #if GLYPH_DEBUG
11480 *w->desired_matrix->method = 0;
11481 debug_method_add (w, "optimization 3");
11482 #endif
11483 goto update;
11484 }
11485 else
11486 goto cancel;
11487 }
11488
11489 cancel:
11490 /* Text changed drastically or point moved off of line. */
11491 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
11492 }
11493
11494 CHARPOS (this_line_start_pos) = 0;
11495 consider_all_windows_p |= buffer_shared > 1;
11496 ++clear_face_cache_count;
11497 #ifdef HAVE_WINDOW_SYSTEM
11498 ++clear_image_cache_count;
11499 #endif
11500
11501 /* Build desired matrices, and update the display. If
11502 consider_all_windows_p is non-zero, do it for all windows on all
11503 frames. Otherwise do it for selected_window, only. */
11504
11505 if (consider_all_windows_p)
11506 {
11507 Lisp_Object tail, frame;
11508
11509 FOR_EACH_FRAME (tail, frame)
11510 XFRAME (frame)->updated_p = 0;
11511
11512 /* Recompute # windows showing selected buffer. This will be
11513 incremented each time such a window is displayed. */
11514 buffer_shared = 0;
11515
11516 FOR_EACH_FRAME (tail, frame)
11517 {
11518 struct frame *f = XFRAME (frame);
11519
11520 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
11521 {
11522 if (! EQ (frame, selected_frame))
11523 /* Select the frame, for the sake of frame-local
11524 variables. */
11525 select_frame_for_redisplay (frame);
11526
11527 /* Mark all the scroll bars to be removed; we'll redeem
11528 the ones we want when we redisplay their windows. */
11529 if (FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
11530 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
11531
11532 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
11533 redisplay_windows (FRAME_ROOT_WINDOW (f));
11534
11535 /* Any scroll bars which redisplay_windows should have
11536 nuked should now go away. */
11537 if (FRAME_TERMINAL (f)->judge_scroll_bars_hook)
11538 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
11539
11540 /* If fonts changed, display again. */
11541 /* ??? rms: I suspect it is a mistake to jump all the way
11542 back to retry here. It should just retry this frame. */
11543 if (fonts_changed_p)
11544 goto retry;
11545
11546 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
11547 {
11548 /* See if we have to hscroll. */
11549 if (!f->already_hscrolled_p)
11550 {
11551 f->already_hscrolled_p = 1;
11552 if (hscroll_windows (f->root_window))
11553 goto retry;
11554 }
11555
11556 /* Prevent various kinds of signals during display
11557 update. stdio is not robust about handling
11558 signals, which can cause an apparent I/O
11559 error. */
11560 if (interrupt_input)
11561 unrequest_sigio ();
11562 STOP_POLLING;
11563
11564 /* Update the display. */
11565 set_window_update_flags (XWINDOW (f->root_window), 1);
11566 pause |= update_frame (f, 0, 0);
11567 #if 0 /* Exiting the loop can leave the wrong value for buffer_shared. */
11568 if (pause)
11569 break;
11570 #endif
11571
11572 f->updated_p = 1;
11573 }
11574 }
11575 }
11576
11577 if (!pause)
11578 {
11579 /* Do the mark_window_display_accurate after all windows have
11580 been redisplayed because this call resets flags in buffers
11581 which are needed for proper redisplay. */
11582 FOR_EACH_FRAME (tail, frame)
11583 {
11584 struct frame *f = XFRAME (frame);
11585 if (f->updated_p)
11586 {
11587 mark_window_display_accurate (f->root_window, 1);
11588 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
11589 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
11590 }
11591 }
11592 }
11593 }
11594 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
11595 {
11596 Lisp_Object mini_window;
11597 struct frame *mini_frame;
11598
11599 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
11600 /* Use list_of_error, not Qerror, so that
11601 we catch only errors and don't run the debugger. */
11602 internal_condition_case_1 (redisplay_window_1, selected_window,
11603 list_of_error,
11604 redisplay_window_error);
11605
11606 /* Compare desired and current matrices, perform output. */
11607
11608 update:
11609 /* If fonts changed, display again. */
11610 if (fonts_changed_p)
11611 goto retry;
11612
11613 /* Prevent various kinds of signals during display update.
11614 stdio is not robust about handling signals,
11615 which can cause an apparent I/O error. */
11616 if (interrupt_input)
11617 unrequest_sigio ();
11618 STOP_POLLING;
11619
11620 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
11621 {
11622 if (hscroll_windows (selected_window))
11623 goto retry;
11624
11625 XWINDOW (selected_window)->must_be_updated_p = 1;
11626 pause = update_frame (sf, 0, 0);
11627 }
11628
11629 /* We may have called echo_area_display at the top of this
11630 function. If the echo area is on another frame, that may
11631 have put text on a frame other than the selected one, so the
11632 above call to update_frame would not have caught it. Catch
11633 it here. */
11634 mini_window = FRAME_MINIBUF_WINDOW (sf);
11635 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
11636
11637 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
11638 {
11639 XWINDOW (mini_window)->must_be_updated_p = 1;
11640 pause |= update_frame (mini_frame, 0, 0);
11641 if (!pause && hscroll_windows (mini_window))
11642 goto retry;
11643 }
11644 }
11645
11646 /* If display was paused because of pending input, make sure we do a
11647 thorough update the next time. */
11648 if (pause)
11649 {
11650 /* Prevent the optimization at the beginning of
11651 redisplay_internal that tries a single-line update of the
11652 line containing the cursor in the selected window. */
11653 CHARPOS (this_line_start_pos) = 0;
11654
11655 /* Let the overlay arrow be updated the next time. */
11656 update_overlay_arrows (0);
11657
11658 /* If we pause after scrolling, some rows in the current
11659 matrices of some windows are not valid. */
11660 if (!WINDOW_FULL_WIDTH_P (w)
11661 && !FRAME_WINDOW_P (XFRAME (w->frame)))
11662 update_mode_lines = 1;
11663 }
11664 else
11665 {
11666 if (!consider_all_windows_p)
11667 {
11668 /* This has already been done above if
11669 consider_all_windows_p is set. */
11670 mark_window_display_accurate_1 (w, 1);
11671
11672 /* Say overlay arrows are up to date. */
11673 update_overlay_arrows (1);
11674
11675 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
11676 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
11677 }
11678
11679 update_mode_lines = 0;
11680 windows_or_buffers_changed = 0;
11681 cursor_type_changed = 0;
11682 }
11683
11684 /* Start SIGIO interrupts coming again. Having them off during the
11685 code above makes it less likely one will discard output, but not
11686 impossible, since there might be stuff in the system buffer here.
11687 But it is much hairier to try to do anything about that. */
11688 if (interrupt_input)
11689 request_sigio ();
11690 RESUME_POLLING;
11691
11692 /* If a frame has become visible which was not before, redisplay
11693 again, so that we display it. Expose events for such a frame
11694 (which it gets when becoming visible) don't call the parts of
11695 redisplay constructing glyphs, so simply exposing a frame won't
11696 display anything in this case. So, we have to display these
11697 frames here explicitly. */
11698 if (!pause)
11699 {
11700 Lisp_Object tail, frame;
11701 int new_count = 0;
11702
11703 FOR_EACH_FRAME (tail, frame)
11704 {
11705 int this_is_visible = 0;
11706
11707 if (XFRAME (frame)->visible)
11708 this_is_visible = 1;
11709 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
11710 if (XFRAME (frame)->visible)
11711 this_is_visible = 1;
11712
11713 if (this_is_visible)
11714 new_count++;
11715 }
11716
11717 if (new_count != number_of_visible_frames)
11718 windows_or_buffers_changed++;
11719 }
11720
11721 /* Change frame size now if a change is pending. */
11722 do_pending_window_change (1);
11723
11724 /* If we just did a pending size change, or have additional
11725 visible frames, redisplay again. */
11726 if (windows_or_buffers_changed && !pause)
11727 goto retry;
11728
11729 /* Clear the face cache eventually. */
11730 if (consider_all_windows_p)
11731 {
11732 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
11733 {
11734 clear_face_cache (0);
11735 clear_face_cache_count = 0;
11736 }
11737 #ifdef HAVE_WINDOW_SYSTEM
11738 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
11739 {
11740 Lisp_Object tail, frame;
11741 FOR_EACH_FRAME (tail, frame)
11742 {
11743 struct frame *f = XFRAME (frame);
11744 if (FRAME_WINDOW_P (f))
11745 clear_image_cache (f, 0);
11746 }
11747 clear_image_cache_count = 0;
11748 }
11749 #endif /* HAVE_WINDOW_SYSTEM */
11750 }
11751
11752 end_of_redisplay:
11753 unbind_to (count, Qnil);
11754 RESUME_POLLING;
11755 }
11756
11757
11758 /* Redisplay, but leave alone any recent echo area message unless
11759 another message has been requested in its place.
11760
11761 This is useful in situations where you need to redisplay but no
11762 user action has occurred, making it inappropriate for the message
11763 area to be cleared. See tracking_off and
11764 wait_reading_process_output for examples of these situations.
11765
11766 FROM_WHERE is an integer saying from where this function was
11767 called. This is useful for debugging. */
11768
11769 void
11770 redisplay_preserve_echo_area (from_where)
11771 int from_where;
11772 {
11773 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
11774
11775 if (!NILP (echo_area_buffer[1]))
11776 {
11777 /* We have a previously displayed message, but no current
11778 message. Redisplay the previous message. */
11779 display_last_displayed_message_p = 1;
11780 redisplay_internal (1);
11781 display_last_displayed_message_p = 0;
11782 }
11783 else
11784 redisplay_internal (1);
11785
11786 if (FRAME_RIF (SELECTED_FRAME ()) != NULL
11787 && FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
11788 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (NULL);
11789 }
11790
11791
11792 /* Function registered with record_unwind_protect in
11793 redisplay_internal. Reset redisplaying_p to the value it had
11794 before redisplay_internal was called, and clear
11795 prevent_freeing_realized_faces_p. It also selects the previously
11796 selected frame, unless it has been deleted (by an X connection
11797 failure during redisplay, for example). */
11798
11799 static Lisp_Object
11800 unwind_redisplay (val)
11801 Lisp_Object val;
11802 {
11803 Lisp_Object old_redisplaying_p, old_frame;
11804
11805 old_redisplaying_p = XCAR (val);
11806 redisplaying_p = XFASTINT (old_redisplaying_p);
11807 old_frame = XCDR (val);
11808 if (! EQ (old_frame, selected_frame)
11809 && FRAME_LIVE_P (XFRAME (old_frame)))
11810 select_frame_for_redisplay (old_frame);
11811 return Qnil;
11812 }
11813
11814
11815 /* Mark the display of window W as accurate or inaccurate. If
11816 ACCURATE_P is non-zero mark display of W as accurate. If
11817 ACCURATE_P is zero, arrange for W to be redisplayed the next time
11818 redisplay_internal is called. */
11819
11820 static void
11821 mark_window_display_accurate_1 (w, accurate_p)
11822 struct window *w;
11823 int accurate_p;
11824 {
11825 if (BUFFERP (w->buffer))
11826 {
11827 struct buffer *b = XBUFFER (w->buffer);
11828
11829 w->last_modified
11830 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
11831 w->last_overlay_modified
11832 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
11833 w->last_had_star
11834 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
11835
11836 if (accurate_p)
11837 {
11838 b->clip_changed = 0;
11839 b->prevent_redisplay_optimizations_p = 0;
11840
11841 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
11842 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
11843 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
11844 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
11845
11846 w->current_matrix->buffer = b;
11847 w->current_matrix->begv = BUF_BEGV (b);
11848 w->current_matrix->zv = BUF_ZV (b);
11849
11850 w->last_cursor = w->cursor;
11851 w->last_cursor_off_p = w->cursor_off_p;
11852
11853 if (w == XWINDOW (selected_window))
11854 w->last_point = make_number (BUF_PT (b));
11855 else
11856 w->last_point = make_number (XMARKER (w->pointm)->charpos);
11857 }
11858 }
11859
11860 if (accurate_p)
11861 {
11862 w->window_end_valid = w->buffer;
11863 #if 0 /* This is incorrect with variable-height lines. */
11864 xassert (XINT (w->window_end_vpos)
11865 < (WINDOW_TOTAL_LINES (w)
11866 - (WINDOW_WANTS_MODELINE_P (w) ? 1 : 0)));
11867 #endif
11868 w->update_mode_line = Qnil;
11869 }
11870 }
11871
11872
11873 /* Mark the display of windows in the window tree rooted at WINDOW as
11874 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
11875 windows as accurate. If ACCURATE_P is zero, arrange for windows to
11876 be redisplayed the next time redisplay_internal is called. */
11877
11878 void
11879 mark_window_display_accurate (window, accurate_p)
11880 Lisp_Object window;
11881 int accurate_p;
11882 {
11883 struct window *w;
11884
11885 for (; !NILP (window); window = w->next)
11886 {
11887 w = XWINDOW (window);
11888 mark_window_display_accurate_1 (w, accurate_p);
11889
11890 if (!NILP (w->vchild))
11891 mark_window_display_accurate (w->vchild, accurate_p);
11892 if (!NILP (w->hchild))
11893 mark_window_display_accurate (w->hchild, accurate_p);
11894 }
11895
11896 if (accurate_p)
11897 {
11898 update_overlay_arrows (1);
11899 }
11900 else
11901 {
11902 /* Force a thorough redisplay the next time by setting
11903 last_arrow_position and last_arrow_string to t, which is
11904 unequal to any useful value of Voverlay_arrow_... */
11905 update_overlay_arrows (-1);
11906 }
11907 }
11908
11909
11910 /* Return value in display table DP (Lisp_Char_Table *) for character
11911 C. Since a display table doesn't have any parent, we don't have to
11912 follow parent. Do not call this function directly but use the
11913 macro DISP_CHAR_VECTOR. */
11914
11915 Lisp_Object
11916 disp_char_vector (dp, c)
11917 struct Lisp_Char_Table *dp;
11918 int c;
11919 {
11920 Lisp_Object val;
11921
11922 if (ASCII_CHAR_P (c))
11923 {
11924 val = dp->ascii;
11925 if (SUB_CHAR_TABLE_P (val))
11926 val = XSUB_CHAR_TABLE (val)->contents[c];
11927 }
11928 else
11929 {
11930 Lisp_Object table;
11931
11932 XSETCHAR_TABLE (table, dp);
11933 val = char_table_ref (table, c);
11934 }
11935 if (NILP (val))
11936 val = dp->defalt;
11937 return val;
11938 }
11939
11940
11941 \f
11942 /***********************************************************************
11943 Window Redisplay
11944 ***********************************************************************/
11945
11946 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
11947
11948 static void
11949 redisplay_windows (window)
11950 Lisp_Object window;
11951 {
11952 while (!NILP (window))
11953 {
11954 struct window *w = XWINDOW (window);
11955
11956 if (!NILP (w->hchild))
11957 redisplay_windows (w->hchild);
11958 else if (!NILP (w->vchild))
11959 redisplay_windows (w->vchild);
11960 else
11961 {
11962 displayed_buffer = XBUFFER (w->buffer);
11963 /* Use list_of_error, not Qerror, so that
11964 we catch only errors and don't run the debugger. */
11965 internal_condition_case_1 (redisplay_window_0, window,
11966 list_of_error,
11967 redisplay_window_error);
11968 }
11969
11970 window = w->next;
11971 }
11972 }
11973
11974 static Lisp_Object
11975 redisplay_window_error ()
11976 {
11977 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
11978 return Qnil;
11979 }
11980
11981 static Lisp_Object
11982 redisplay_window_0 (window)
11983 Lisp_Object window;
11984 {
11985 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
11986 redisplay_window (window, 0);
11987 return Qnil;
11988 }
11989
11990 static Lisp_Object
11991 redisplay_window_1 (window)
11992 Lisp_Object window;
11993 {
11994 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
11995 redisplay_window (window, 1);
11996 return Qnil;
11997 }
11998 \f
11999
12000 /* Increment GLYPH until it reaches END or CONDITION fails while
12001 adding (GLYPH)->pixel_width to X. */
12002
12003 #define SKIP_GLYPHS(glyph, end, x, condition) \
12004 do \
12005 { \
12006 (x) += (glyph)->pixel_width; \
12007 ++(glyph); \
12008 } \
12009 while ((glyph) < (end) && (condition))
12010
12011
12012 /* Set cursor position of W. PT is assumed to be displayed in ROW.
12013 DELTA is the number of bytes by which positions recorded in ROW
12014 differ from current buffer positions.
12015
12016 Return 0 if cursor is not on this row. 1 otherwise. */
12017
12018 int
12019 set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
12020 struct window *w;
12021 struct glyph_row *row;
12022 struct glyph_matrix *matrix;
12023 int delta, delta_bytes, dy, dvpos;
12024 {
12025 struct glyph *glyph = row->glyphs[TEXT_AREA];
12026 struct glyph *end = glyph + row->used[TEXT_AREA];
12027 struct glyph *cursor = NULL;
12028 /* The first glyph that starts a sequence of glyphs from string. */
12029 struct glyph *string_start;
12030 /* The X coordinate of string_start. */
12031 int string_start_x;
12032 /* The last known character position. */
12033 int last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
12034 /* The last known character position before string_start. */
12035 int string_before_pos;
12036 int x = row->x;
12037 int cursor_x = x;
12038 int cursor_from_overlay_pos = 0;
12039 int pt_old = PT - delta;
12040
12041 /* Skip over glyphs not having an object at the start of the row.
12042 These are special glyphs like truncation marks on terminal
12043 frames. */
12044 if (row->displays_text_p)
12045 while (glyph < end
12046 && INTEGERP (glyph->object)
12047 && glyph->charpos < 0)
12048 {
12049 x += glyph->pixel_width;
12050 ++glyph;
12051 }
12052
12053 string_start = NULL;
12054 while (glyph < end
12055 && !INTEGERP (glyph->object)
12056 && (!BUFFERP (glyph->object)
12057 || (last_pos = glyph->charpos) < pt_old))
12058 {
12059 if (! STRINGP (glyph->object))
12060 {
12061 string_start = NULL;
12062 x += glyph->pixel_width;
12063 ++glyph;
12064 if (cursor_from_overlay_pos
12065 && last_pos >= cursor_from_overlay_pos)
12066 {
12067 cursor_from_overlay_pos = 0;
12068 cursor = 0;
12069 }
12070 }
12071 else
12072 {
12073 if (string_start == NULL)
12074 {
12075 string_before_pos = last_pos;
12076 string_start = glyph;
12077 string_start_x = x;
12078 }
12079 /* Skip all glyphs from string. */
12080 do
12081 {
12082 Lisp_Object cprop;
12083 int pos;
12084 if ((cursor == NULL || glyph > cursor)
12085 && (cprop = Fget_char_property (make_number ((glyph)->charpos),
12086 Qcursor, (glyph)->object),
12087 !NILP (cprop))
12088 && (pos = string_buffer_position (w, glyph->object,
12089 string_before_pos),
12090 (pos == 0 /* From overlay */
12091 || pos == pt_old)))
12092 {
12093 /* Estimate overlay buffer position from the buffer
12094 positions of the glyphs before and after the overlay.
12095 Add 1 to last_pos so that if point corresponds to the
12096 glyph right after the overlay, we still use a 'cursor'
12097 property found in that overlay. */
12098 cursor_from_overlay_pos = (pos ? 0 : last_pos
12099 + (INTEGERP (cprop) ? XINT (cprop) : 0));
12100 cursor = glyph;
12101 cursor_x = x;
12102 }
12103 x += glyph->pixel_width;
12104 ++glyph;
12105 }
12106 while (glyph < end && EQ (glyph->object, string_start->object));
12107 }
12108 }
12109
12110 if (cursor != NULL)
12111 {
12112 glyph = cursor;
12113 x = cursor_x;
12114 }
12115 else if (row->ends_in_ellipsis_p && glyph == end)
12116 {
12117 /* Scan back over the ellipsis glyphs, decrementing positions. */
12118 while (glyph > row->glyphs[TEXT_AREA]
12119 && (glyph - 1)->charpos == last_pos)
12120 glyph--, x -= glyph->pixel_width;
12121 /* That loop always goes one position too far,
12122 including the glyph before the ellipsis.
12123 So scan forward over that one. */
12124 x += glyph->pixel_width;
12125 glyph++;
12126 }
12127 else if (string_start
12128 && (glyph == end || !BUFFERP (glyph->object) || last_pos > pt_old))
12129 {
12130 /* We may have skipped over point because the previous glyphs
12131 are from string. As there's no easy way to know the
12132 character position of the current glyph, find the correct
12133 glyph on point by scanning from string_start again. */
12134 Lisp_Object limit;
12135 Lisp_Object string;
12136 struct glyph *stop = glyph;
12137 int pos;
12138
12139 limit = make_number (pt_old + 1);
12140 glyph = string_start;
12141 x = string_start_x;
12142 string = glyph->object;
12143 pos = string_buffer_position (w, string, string_before_pos);
12144 /* If STRING is from overlay, LAST_POS == 0. We skip such glyphs
12145 because we always put cursor after overlay strings. */
12146 while (pos == 0 && glyph < stop)
12147 {
12148 string = glyph->object;
12149 SKIP_GLYPHS (glyph, stop, x, EQ (glyph->object, string));
12150 if (glyph < stop)
12151 pos = string_buffer_position (w, glyph->object, string_before_pos);
12152 }
12153
12154 while (glyph < stop)
12155 {
12156 pos = XINT (Fnext_single_char_property_change
12157 (make_number (pos), Qdisplay, Qnil, limit));
12158 if (pos > pt_old)
12159 break;
12160 /* Skip glyphs from the same string. */
12161 string = glyph->object;
12162 SKIP_GLYPHS (glyph, stop, x, EQ (glyph->object, string));
12163 /* Skip glyphs from an overlay. */
12164 while (glyph < stop
12165 && ! string_buffer_position (w, glyph->object, pos))
12166 {
12167 string = glyph->object;
12168 SKIP_GLYPHS (glyph, stop, x, EQ (glyph->object, string));
12169 }
12170 }
12171
12172 /* If we reached the end of the line, and end was from a string,
12173 cursor is not on this line. */
12174 if (glyph == end && row->continued_p)
12175 return 0;
12176 }
12177
12178 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
12179 w->cursor.x = x;
12180 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
12181 w->cursor.y = row->y + dy;
12182
12183 if (w == XWINDOW (selected_window))
12184 {
12185 if (!row->continued_p
12186 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
12187 && row->x == 0)
12188 {
12189 this_line_buffer = XBUFFER (w->buffer);
12190
12191 CHARPOS (this_line_start_pos)
12192 = MATRIX_ROW_START_CHARPOS (row) + delta;
12193 BYTEPOS (this_line_start_pos)
12194 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
12195
12196 CHARPOS (this_line_end_pos)
12197 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
12198 BYTEPOS (this_line_end_pos)
12199 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
12200
12201 this_line_y = w->cursor.y;
12202 this_line_pixel_height = row->height;
12203 this_line_vpos = w->cursor.vpos;
12204 this_line_start_x = row->x;
12205 }
12206 else
12207 CHARPOS (this_line_start_pos) = 0;
12208 }
12209
12210 return 1;
12211 }
12212
12213
12214 /* Run window scroll functions, if any, for WINDOW with new window
12215 start STARTP. Sets the window start of WINDOW to that position.
12216
12217 We assume that the window's buffer is really current. */
12218
12219 static INLINE struct text_pos
12220 run_window_scroll_functions (window, startp)
12221 Lisp_Object window;
12222 struct text_pos startp;
12223 {
12224 struct window *w = XWINDOW (window);
12225 SET_MARKER_FROM_TEXT_POS (w->start, startp);
12226
12227 if (current_buffer != XBUFFER (w->buffer))
12228 abort ();
12229
12230 if (!NILP (Vwindow_scroll_functions))
12231 {
12232 run_hook_with_args_2 (Qwindow_scroll_functions, window,
12233 make_number (CHARPOS (startp)));
12234 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12235 /* In case the hook functions switch buffers. */
12236 if (current_buffer != XBUFFER (w->buffer))
12237 set_buffer_internal_1 (XBUFFER (w->buffer));
12238 }
12239
12240 return startp;
12241 }
12242
12243
12244 /* Make sure the line containing the cursor is fully visible.
12245 A value of 1 means there is nothing to be done.
12246 (Either the line is fully visible, or it cannot be made so,
12247 or we cannot tell.)
12248
12249 If FORCE_P is non-zero, return 0 even if partial visible cursor row
12250 is higher than window.
12251
12252 A value of 0 means the caller should do scrolling
12253 as if point had gone off the screen. */
12254
12255 static int
12256 cursor_row_fully_visible_p (w, force_p, current_matrix_p)
12257 struct window *w;
12258 int force_p;
12259 int current_matrix_p;
12260 {
12261 struct glyph_matrix *matrix;
12262 struct glyph_row *row;
12263 int window_height;
12264
12265 if (!make_cursor_line_fully_visible_p)
12266 return 1;
12267
12268 /* It's not always possible to find the cursor, e.g, when a window
12269 is full of overlay strings. Don't do anything in that case. */
12270 if (w->cursor.vpos < 0)
12271 return 1;
12272
12273 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
12274 row = MATRIX_ROW (matrix, w->cursor.vpos);
12275
12276 /* If the cursor row is not partially visible, there's nothing to do. */
12277 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
12278 return 1;
12279
12280 /* If the row the cursor is in is taller than the window's height,
12281 it's not clear what to do, so do nothing. */
12282 window_height = window_box_height (w);
12283 if (row->height >= window_height)
12284 {
12285 if (!force_p || MINI_WINDOW_P (w)
12286 || w->vscroll || w->cursor.vpos == 0)
12287 return 1;
12288 }
12289 return 0;
12290
12291 #if 0
12292 /* This code used to try to scroll the window just enough to make
12293 the line visible. It returned 0 to say that the caller should
12294 allocate larger glyph matrices. */
12295
12296 if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (w, row))
12297 {
12298 int dy = row->height - row->visible_height;
12299 w->vscroll = 0;
12300 w->cursor.y += dy;
12301 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
12302 }
12303 else /* MATRIX_ROW_PARTIALLY_VISIBLE_AT_BOTTOM_P (w, row)) */
12304 {
12305 int dy = - (row->height - row->visible_height);
12306 w->vscroll = dy;
12307 w->cursor.y += dy;
12308 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
12309 }
12310
12311 /* When we change the cursor y-position of the selected window,
12312 change this_line_y as well so that the display optimization for
12313 the cursor line of the selected window in redisplay_internal uses
12314 the correct y-position. */
12315 if (w == XWINDOW (selected_window))
12316 this_line_y = w->cursor.y;
12317
12318 /* If vscrolling requires a larger glyph matrix, arrange for a fresh
12319 redisplay with larger matrices. */
12320 if (matrix->nrows < required_matrix_height (w))
12321 {
12322 fonts_changed_p = 1;
12323 return 0;
12324 }
12325
12326 return 1;
12327 #endif /* 0 */
12328 }
12329
12330
12331 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
12332 non-zero means only WINDOW is redisplayed in redisplay_internal.
12333 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
12334 in redisplay_window to bring a partially visible line into view in
12335 the case that only the cursor has moved.
12336
12337 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
12338 last screen line's vertical height extends past the end of the screen.
12339
12340 Value is
12341
12342 1 if scrolling succeeded
12343
12344 0 if scrolling didn't find point.
12345
12346 -1 if new fonts have been loaded so that we must interrupt
12347 redisplay, adjust glyph matrices, and try again. */
12348
12349 enum
12350 {
12351 SCROLLING_SUCCESS,
12352 SCROLLING_FAILED,
12353 SCROLLING_NEED_LARGER_MATRICES
12354 };
12355
12356 static int
12357 try_scrolling (window, just_this_one_p, scroll_conservatively,
12358 scroll_step, temp_scroll_step, last_line_misfit)
12359 Lisp_Object window;
12360 int just_this_one_p;
12361 EMACS_INT scroll_conservatively, scroll_step;
12362 int temp_scroll_step;
12363 int last_line_misfit;
12364 {
12365 struct window *w = XWINDOW (window);
12366 struct frame *f = XFRAME (w->frame);
12367 struct text_pos scroll_margin_pos;
12368 struct text_pos pos;
12369 struct text_pos startp;
12370 struct it it;
12371 Lisp_Object window_end;
12372 int this_scroll_margin;
12373 int dy = 0;
12374 int scroll_max;
12375 int rc;
12376 int amount_to_scroll = 0;
12377 Lisp_Object aggressive;
12378 int height;
12379 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
12380
12381 #if GLYPH_DEBUG
12382 debug_method_add (w, "try_scrolling");
12383 #endif
12384
12385 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12386
12387 /* Compute scroll margin height in pixels. We scroll when point is
12388 within this distance from the top or bottom of the window. */
12389 if (scroll_margin > 0)
12390 {
12391 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
12392 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
12393 }
12394 else
12395 this_scroll_margin = 0;
12396
12397 /* Force scroll_conservatively to have a reasonable value so it doesn't
12398 cause an overflow while computing how much to scroll. */
12399 if (scroll_conservatively)
12400 scroll_conservatively = min (scroll_conservatively,
12401 MOST_POSITIVE_FIXNUM / FRAME_LINE_HEIGHT (f));
12402
12403 /* Compute how much we should try to scroll maximally to bring point
12404 into view. */
12405 if (scroll_step || scroll_conservatively || temp_scroll_step)
12406 scroll_max = max (scroll_step,
12407 max (scroll_conservatively, temp_scroll_step));
12408 else if (NUMBERP (current_buffer->scroll_down_aggressively)
12409 || NUMBERP (current_buffer->scroll_up_aggressively))
12410 /* We're trying to scroll because of aggressive scrolling
12411 but no scroll_step is set. Choose an arbitrary one. Maybe
12412 there should be a variable for this. */
12413 scroll_max = 10;
12414 else
12415 scroll_max = 0;
12416 scroll_max *= FRAME_LINE_HEIGHT (f);
12417
12418 /* Decide whether we have to scroll down. Start at the window end
12419 and move this_scroll_margin up to find the position of the scroll
12420 margin. */
12421 window_end = Fwindow_end (window, Qt);
12422
12423 too_near_end:
12424
12425 CHARPOS (scroll_margin_pos) = XINT (window_end);
12426 BYTEPOS (scroll_margin_pos) = CHAR_TO_BYTE (CHARPOS (scroll_margin_pos));
12427
12428 if (this_scroll_margin || extra_scroll_margin_lines)
12429 {
12430 start_display (&it, w, scroll_margin_pos);
12431 if (this_scroll_margin)
12432 move_it_vertically_backward (&it, this_scroll_margin);
12433 if (extra_scroll_margin_lines)
12434 move_it_by_lines (&it, - extra_scroll_margin_lines, 0);
12435 scroll_margin_pos = it.current.pos;
12436 }
12437
12438 if (PT >= CHARPOS (scroll_margin_pos))
12439 {
12440 int y0;
12441
12442 /* Point is in the scroll margin at the bottom of the window, or
12443 below. Compute a new window start that makes point visible. */
12444
12445 /* Compute the distance from the scroll margin to PT.
12446 Give up if the distance is greater than scroll_max. */
12447 start_display (&it, w, scroll_margin_pos);
12448 y0 = it.current_y;
12449 move_it_to (&it, PT, 0, it.last_visible_y, -1,
12450 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
12451
12452 /* To make point visible, we have to move the window start
12453 down so that the line the cursor is in is visible, which
12454 means we have to add in the height of the cursor line. */
12455 dy = line_bottom_y (&it) - y0;
12456
12457 if (dy > scroll_max)
12458 return SCROLLING_FAILED;
12459
12460 /* Move the window start down. If scrolling conservatively,
12461 move it just enough down to make point visible. If
12462 scroll_step is set, move it down by scroll_step. */
12463 start_display (&it, w, startp);
12464
12465 if (scroll_conservatively)
12466 /* Set AMOUNT_TO_SCROLL to at least one line,
12467 and at most scroll_conservatively lines. */
12468 amount_to_scroll
12469 = min (max (dy, FRAME_LINE_HEIGHT (f)),
12470 FRAME_LINE_HEIGHT (f) * scroll_conservatively);
12471 else if (scroll_step || temp_scroll_step)
12472 amount_to_scroll = scroll_max;
12473 else
12474 {
12475 aggressive = current_buffer->scroll_up_aggressively;
12476 height = WINDOW_BOX_TEXT_HEIGHT (w);
12477 if (NUMBERP (aggressive))
12478 {
12479 double float_amount = XFLOATINT (aggressive) * height;
12480 amount_to_scroll = float_amount;
12481 if (amount_to_scroll == 0 && float_amount > 0)
12482 amount_to_scroll = 1;
12483 }
12484 }
12485
12486 if (amount_to_scroll <= 0)
12487 return SCROLLING_FAILED;
12488
12489 /* If moving by amount_to_scroll leaves STARTP unchanged,
12490 move it down one screen line. */
12491
12492 move_it_vertically (&it, amount_to_scroll);
12493 if (CHARPOS (it.current.pos) == CHARPOS (startp))
12494 move_it_by_lines (&it, 1, 1);
12495 startp = it.current.pos;
12496 }
12497 else
12498 {
12499 /* See if point is inside the scroll margin at the top of the
12500 window. */
12501 scroll_margin_pos = startp;
12502 if (this_scroll_margin)
12503 {
12504 start_display (&it, w, startp);
12505 move_it_vertically (&it, this_scroll_margin);
12506 scroll_margin_pos = it.current.pos;
12507 }
12508
12509 if (PT < CHARPOS (scroll_margin_pos))
12510 {
12511 /* Point is in the scroll margin at the top of the window or
12512 above what is displayed in the window. */
12513 int y0;
12514
12515 /* Compute the vertical distance from PT to the scroll
12516 margin position. Give up if distance is greater than
12517 scroll_max. */
12518 SET_TEXT_POS (pos, PT, PT_BYTE);
12519 start_display (&it, w, pos);
12520 y0 = it.current_y;
12521 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
12522 it.last_visible_y, -1,
12523 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
12524 dy = it.current_y - y0;
12525 if (dy > scroll_max)
12526 return SCROLLING_FAILED;
12527
12528 /* Compute new window start. */
12529 start_display (&it, w, startp);
12530
12531 if (scroll_conservatively)
12532 amount_to_scroll
12533 = max (dy, FRAME_LINE_HEIGHT (f) * max (scroll_step, temp_scroll_step));
12534 else if (scroll_step || temp_scroll_step)
12535 amount_to_scroll = scroll_max;
12536 else
12537 {
12538 aggressive = current_buffer->scroll_down_aggressively;
12539 height = WINDOW_BOX_TEXT_HEIGHT (w);
12540 if (NUMBERP (aggressive))
12541 {
12542 double float_amount = XFLOATINT (aggressive) * height;
12543 amount_to_scroll = float_amount;
12544 if (amount_to_scroll == 0 && float_amount > 0)
12545 amount_to_scroll = 1;
12546 }
12547 }
12548
12549 if (amount_to_scroll <= 0)
12550 return SCROLLING_FAILED;
12551
12552 move_it_vertically_backward (&it, amount_to_scroll);
12553 startp = it.current.pos;
12554 }
12555 }
12556
12557 /* Run window scroll functions. */
12558 startp = run_window_scroll_functions (window, startp);
12559
12560 /* Display the window. Give up if new fonts are loaded, or if point
12561 doesn't appear. */
12562 if (!try_window (window, startp, 0))
12563 rc = SCROLLING_NEED_LARGER_MATRICES;
12564 else if (w->cursor.vpos < 0)
12565 {
12566 clear_glyph_matrix (w->desired_matrix);
12567 rc = SCROLLING_FAILED;
12568 }
12569 else
12570 {
12571 /* Maybe forget recorded base line for line number display. */
12572 if (!just_this_one_p
12573 || current_buffer->clip_changed
12574 || BEG_UNCHANGED < CHARPOS (startp))
12575 w->base_line_number = Qnil;
12576
12577 /* If cursor ends up on a partially visible line,
12578 treat that as being off the bottom of the screen. */
12579 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0))
12580 {
12581 clear_glyph_matrix (w->desired_matrix);
12582 ++extra_scroll_margin_lines;
12583 goto too_near_end;
12584 }
12585 rc = SCROLLING_SUCCESS;
12586 }
12587
12588 return rc;
12589 }
12590
12591
12592 /* Compute a suitable window start for window W if display of W starts
12593 on a continuation line. Value is non-zero if a new window start
12594 was computed.
12595
12596 The new window start will be computed, based on W's width, starting
12597 from the start of the continued line. It is the start of the
12598 screen line with the minimum distance from the old start W->start. */
12599
12600 static int
12601 compute_window_start_on_continuation_line (w)
12602 struct window *w;
12603 {
12604 struct text_pos pos, start_pos;
12605 int window_start_changed_p = 0;
12606
12607 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
12608
12609 /* If window start is on a continuation line... Window start may be
12610 < BEGV in case there's invisible text at the start of the
12611 buffer (M-x rmail, for example). */
12612 if (CHARPOS (start_pos) > BEGV
12613 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
12614 {
12615 struct it it;
12616 struct glyph_row *row;
12617
12618 /* Handle the case that the window start is out of range. */
12619 if (CHARPOS (start_pos) < BEGV)
12620 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
12621 else if (CHARPOS (start_pos) > ZV)
12622 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
12623
12624 /* Find the start of the continued line. This should be fast
12625 because scan_buffer is fast (newline cache). */
12626 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
12627 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
12628 row, DEFAULT_FACE_ID);
12629 reseat_at_previous_visible_line_start (&it);
12630
12631 /* If the line start is "too far" away from the window start,
12632 say it takes too much time to compute a new window start. */
12633 if (CHARPOS (start_pos) - IT_CHARPOS (it)
12634 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
12635 {
12636 int min_distance, distance;
12637
12638 /* Move forward by display lines to find the new window
12639 start. If window width was enlarged, the new start can
12640 be expected to be > the old start. If window width was
12641 decreased, the new window start will be < the old start.
12642 So, we're looking for the display line start with the
12643 minimum distance from the old window start. */
12644 pos = it.current.pos;
12645 min_distance = INFINITY;
12646 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
12647 distance < min_distance)
12648 {
12649 min_distance = distance;
12650 pos = it.current.pos;
12651 move_it_by_lines (&it, 1, 0);
12652 }
12653
12654 /* Set the window start there. */
12655 SET_MARKER_FROM_TEXT_POS (w->start, pos);
12656 window_start_changed_p = 1;
12657 }
12658 }
12659
12660 return window_start_changed_p;
12661 }
12662
12663
12664 /* Try cursor movement in case text has not changed in window WINDOW,
12665 with window start STARTP. Value is
12666
12667 CURSOR_MOVEMENT_SUCCESS if successful
12668
12669 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
12670
12671 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
12672 display. *SCROLL_STEP is set to 1, under certain circumstances, if
12673 we want to scroll as if scroll-step were set to 1. See the code.
12674
12675 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
12676 which case we have to abort this redisplay, and adjust matrices
12677 first. */
12678
12679 enum
12680 {
12681 CURSOR_MOVEMENT_SUCCESS,
12682 CURSOR_MOVEMENT_CANNOT_BE_USED,
12683 CURSOR_MOVEMENT_MUST_SCROLL,
12684 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
12685 };
12686
12687 static int
12688 try_cursor_movement (window, startp, scroll_step)
12689 Lisp_Object window;
12690 struct text_pos startp;
12691 int *scroll_step;
12692 {
12693 struct window *w = XWINDOW (window);
12694 struct frame *f = XFRAME (w->frame);
12695 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
12696
12697 #if GLYPH_DEBUG
12698 if (inhibit_try_cursor_movement)
12699 return rc;
12700 #endif
12701
12702 /* Handle case where text has not changed, only point, and it has
12703 not moved off the frame. */
12704 if (/* Point may be in this window. */
12705 PT >= CHARPOS (startp)
12706 /* Selective display hasn't changed. */
12707 && !current_buffer->clip_changed
12708 /* Function force-mode-line-update is used to force a thorough
12709 redisplay. It sets either windows_or_buffers_changed or
12710 update_mode_lines. So don't take a shortcut here for these
12711 cases. */
12712 && !update_mode_lines
12713 && !windows_or_buffers_changed
12714 && !cursor_type_changed
12715 /* Can't use this case if highlighting a region. When a
12716 region exists, cursor movement has to do more than just
12717 set the cursor. */
12718 && !(!NILP (Vtransient_mark_mode)
12719 && !NILP (current_buffer->mark_active))
12720 && NILP (w->region_showing)
12721 && NILP (Vshow_trailing_whitespace)
12722 /* Right after splitting windows, last_point may be nil. */
12723 && INTEGERP (w->last_point)
12724 /* This code is not used for mini-buffer for the sake of the case
12725 of redisplaying to replace an echo area message; since in
12726 that case the mini-buffer contents per se are usually
12727 unchanged. This code is of no real use in the mini-buffer
12728 since the handling of this_line_start_pos, etc., in redisplay
12729 handles the same cases. */
12730 && !EQ (window, minibuf_window)
12731 /* When splitting windows or for new windows, it happens that
12732 redisplay is called with a nil window_end_vpos or one being
12733 larger than the window. This should really be fixed in
12734 window.c. I don't have this on my list, now, so we do
12735 approximately the same as the old redisplay code. --gerd. */
12736 && INTEGERP (w->window_end_vpos)
12737 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
12738 && (FRAME_WINDOW_P (f)
12739 || !overlay_arrow_in_current_buffer_p ()))
12740 {
12741 int this_scroll_margin, top_scroll_margin;
12742 struct glyph_row *row = NULL;
12743
12744 #if GLYPH_DEBUG
12745 debug_method_add (w, "cursor movement");
12746 #endif
12747
12748 /* Scroll if point within this distance from the top or bottom
12749 of the window. This is a pixel value. */
12750 this_scroll_margin = max (0, scroll_margin);
12751 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
12752 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
12753
12754 top_scroll_margin = this_scroll_margin;
12755 if (WINDOW_WANTS_HEADER_LINE_P (w))
12756 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
12757
12758 /* Start with the row the cursor was displayed during the last
12759 not paused redisplay. Give up if that row is not valid. */
12760 if (w->last_cursor.vpos < 0
12761 || w->last_cursor.vpos >= w->current_matrix->nrows)
12762 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12763 else
12764 {
12765 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
12766 if (row->mode_line_p)
12767 ++row;
12768 if (!row->enabled_p)
12769 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12770 }
12771
12772 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
12773 {
12774 int scroll_p = 0;
12775 int last_y = window_text_bottom_y (w) - this_scroll_margin;
12776
12777 if (PT > XFASTINT (w->last_point))
12778 {
12779 /* Point has moved forward. */
12780 while (MATRIX_ROW_END_CHARPOS (row) < PT
12781 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
12782 {
12783 xassert (row->enabled_p);
12784 ++row;
12785 }
12786
12787 /* The end position of a row equals the start position
12788 of the next row. If PT is there, we would rather
12789 display it in the next line. */
12790 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
12791 && MATRIX_ROW_END_CHARPOS (row) == PT
12792 && !cursor_row_p (w, row))
12793 ++row;
12794
12795 /* If within the scroll margin, scroll. Note that
12796 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
12797 the next line would be drawn, and that
12798 this_scroll_margin can be zero. */
12799 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
12800 || PT > MATRIX_ROW_END_CHARPOS (row)
12801 /* Line is completely visible last line in window
12802 and PT is to be set in the next line. */
12803 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
12804 && PT == MATRIX_ROW_END_CHARPOS (row)
12805 && !row->ends_at_zv_p
12806 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
12807 scroll_p = 1;
12808 }
12809 else if (PT < XFASTINT (w->last_point))
12810 {
12811 /* Cursor has to be moved backward. Note that PT >=
12812 CHARPOS (startp) because of the outer if-statement. */
12813 while (!row->mode_line_p
12814 && (MATRIX_ROW_START_CHARPOS (row) > PT
12815 || (MATRIX_ROW_START_CHARPOS (row) == PT
12816 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
12817 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
12818 row > w->current_matrix->rows
12819 && (row-1)->ends_in_newline_from_string_p))))
12820 && (row->y > top_scroll_margin
12821 || CHARPOS (startp) == BEGV))
12822 {
12823 xassert (row->enabled_p);
12824 --row;
12825 }
12826
12827 /* Consider the following case: Window starts at BEGV,
12828 there is invisible, intangible text at BEGV, so that
12829 display starts at some point START > BEGV. It can
12830 happen that we are called with PT somewhere between
12831 BEGV and START. Try to handle that case. */
12832 if (row < w->current_matrix->rows
12833 || row->mode_line_p)
12834 {
12835 row = w->current_matrix->rows;
12836 if (row->mode_line_p)
12837 ++row;
12838 }
12839
12840 /* Due to newlines in overlay strings, we may have to
12841 skip forward over overlay strings. */
12842 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
12843 && MATRIX_ROW_END_CHARPOS (row) == PT
12844 && !cursor_row_p (w, row))
12845 ++row;
12846
12847 /* If within the scroll margin, scroll. */
12848 if (row->y < top_scroll_margin
12849 && CHARPOS (startp) != BEGV)
12850 scroll_p = 1;
12851 }
12852 else
12853 {
12854 /* Cursor did not move. So don't scroll even if cursor line
12855 is partially visible, as it was so before. */
12856 rc = CURSOR_MOVEMENT_SUCCESS;
12857 }
12858
12859 if (PT < MATRIX_ROW_START_CHARPOS (row)
12860 || PT > MATRIX_ROW_END_CHARPOS (row))
12861 {
12862 /* if PT is not in the glyph row, give up. */
12863 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12864 }
12865 else if (rc != CURSOR_MOVEMENT_SUCCESS
12866 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
12867 && make_cursor_line_fully_visible_p)
12868 {
12869 if (PT == MATRIX_ROW_END_CHARPOS (row)
12870 && !row->ends_at_zv_p
12871 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
12872 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12873 else if (row->height > window_box_height (w))
12874 {
12875 /* If we end up in a partially visible line, let's
12876 make it fully visible, except when it's taller
12877 than the window, in which case we can't do much
12878 about it. */
12879 *scroll_step = 1;
12880 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12881 }
12882 else
12883 {
12884 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
12885 if (!cursor_row_fully_visible_p (w, 0, 1))
12886 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12887 else
12888 rc = CURSOR_MOVEMENT_SUCCESS;
12889 }
12890 }
12891 else if (scroll_p)
12892 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12893 else
12894 {
12895 do
12896 {
12897 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
12898 {
12899 rc = CURSOR_MOVEMENT_SUCCESS;
12900 break;
12901 }
12902 ++row;
12903 }
12904 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
12905 && MATRIX_ROW_START_CHARPOS (row) == PT
12906 && cursor_row_p (w, row));
12907 }
12908 }
12909 }
12910
12911 return rc;
12912 }
12913
12914 void
12915 set_vertical_scroll_bar (w)
12916 struct window *w;
12917 {
12918 int start, end, whole;
12919
12920 /* Calculate the start and end positions for the current window.
12921 At some point, it would be nice to choose between scrollbars
12922 which reflect the whole buffer size, with special markers
12923 indicating narrowing, and scrollbars which reflect only the
12924 visible region.
12925
12926 Note that mini-buffers sometimes aren't displaying any text. */
12927 if (!MINI_WINDOW_P (w)
12928 || (w == XWINDOW (minibuf_window)
12929 && NILP (echo_area_buffer[0])))
12930 {
12931 struct buffer *buf = XBUFFER (w->buffer);
12932 whole = BUF_ZV (buf) - BUF_BEGV (buf);
12933 start = marker_position (w->start) - BUF_BEGV (buf);
12934 /* I don't think this is guaranteed to be right. For the
12935 moment, we'll pretend it is. */
12936 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
12937
12938 if (end < start)
12939 end = start;
12940 if (whole < (end - start))
12941 whole = end - start;
12942 }
12943 else
12944 start = end = whole = 0;
12945
12946 /* Indicate what this scroll bar ought to be displaying now. */
12947 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
12948 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
12949 (w, end - start, whole, start);
12950 }
12951
12952
12953 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
12954 selected_window is redisplayed.
12955
12956 We can return without actually redisplaying the window if
12957 fonts_changed_p is nonzero. In that case, redisplay_internal will
12958 retry. */
12959
12960 static void
12961 redisplay_window (window, just_this_one_p)
12962 Lisp_Object window;
12963 int just_this_one_p;
12964 {
12965 struct window *w = XWINDOW (window);
12966 struct frame *f = XFRAME (w->frame);
12967 struct buffer *buffer = XBUFFER (w->buffer);
12968 struct buffer *old = current_buffer;
12969 struct text_pos lpoint, opoint, startp;
12970 int update_mode_line;
12971 int tem;
12972 struct it it;
12973 /* Record it now because it's overwritten. */
12974 int current_matrix_up_to_date_p = 0;
12975 int used_current_matrix_p = 0;
12976 /* This is less strict than current_matrix_up_to_date_p.
12977 It indictes that the buffer contents and narrowing are unchanged. */
12978 int buffer_unchanged_p = 0;
12979 int temp_scroll_step = 0;
12980 int count = SPECPDL_INDEX ();
12981 int rc;
12982 int centering_position = -1;
12983 int last_line_misfit = 0;
12984 int beg_unchanged, end_unchanged;
12985
12986 SET_TEXT_POS (lpoint, PT, PT_BYTE);
12987 opoint = lpoint;
12988
12989 /* W must be a leaf window here. */
12990 xassert (!NILP (w->buffer));
12991 #if GLYPH_DEBUG
12992 *w->desired_matrix->method = 0;
12993 #endif
12994
12995 specbind (Qinhibit_point_motion_hooks, Qt);
12996
12997 reconsider_clip_changes (w, buffer);
12998
12999 /* Has the mode line to be updated? */
13000 update_mode_line = (!NILP (w->update_mode_line)
13001 || update_mode_lines
13002 || buffer->clip_changed
13003 || buffer->prevent_redisplay_optimizations_p);
13004
13005 if (MINI_WINDOW_P (w))
13006 {
13007 if (w == XWINDOW (echo_area_window)
13008 && !NILP (echo_area_buffer[0]))
13009 {
13010 if (update_mode_line)
13011 /* We may have to update a tty frame's menu bar or a
13012 tool-bar. Example `M-x C-h C-h C-g'. */
13013 goto finish_menu_bars;
13014 else
13015 /* We've already displayed the echo area glyphs in this window. */
13016 goto finish_scroll_bars;
13017 }
13018 else if ((w != XWINDOW (minibuf_window)
13019 || minibuf_level == 0)
13020 /* When buffer is nonempty, redisplay window normally. */
13021 && BUF_Z (XBUFFER (w->buffer)) == BUF_BEG (XBUFFER (w->buffer))
13022 /* Quail displays non-mini buffers in minibuffer window.
13023 In that case, redisplay the window normally. */
13024 && !NILP (Fmemq (w->buffer, Vminibuffer_list)))
13025 {
13026 /* W is a mini-buffer window, but it's not active, so clear
13027 it. */
13028 int yb = window_text_bottom_y (w);
13029 struct glyph_row *row;
13030 int y;
13031
13032 for (y = 0, row = w->desired_matrix->rows;
13033 y < yb;
13034 y += row->height, ++row)
13035 blank_row (w, row, y);
13036 goto finish_scroll_bars;
13037 }
13038
13039 clear_glyph_matrix (w->desired_matrix);
13040 }
13041
13042 /* Otherwise set up data on this window; select its buffer and point
13043 value. */
13044 /* Really select the buffer, for the sake of buffer-local
13045 variables. */
13046 set_buffer_internal_1 (XBUFFER (w->buffer));
13047 SET_TEXT_POS (opoint, PT, PT_BYTE);
13048
13049 beg_unchanged = BEG_UNCHANGED;
13050 end_unchanged = END_UNCHANGED;
13051
13052 current_matrix_up_to_date_p
13053 = (!NILP (w->window_end_valid)
13054 && !current_buffer->clip_changed
13055 && !current_buffer->prevent_redisplay_optimizations_p
13056 && XFASTINT (w->last_modified) >= MODIFF
13057 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
13058
13059 buffer_unchanged_p
13060 = (!NILP (w->window_end_valid)
13061 && !current_buffer->clip_changed
13062 && XFASTINT (w->last_modified) >= MODIFF
13063 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
13064
13065 /* When windows_or_buffers_changed is non-zero, we can't rely on
13066 the window end being valid, so set it to nil there. */
13067 if (windows_or_buffers_changed)
13068 {
13069 /* If window starts on a continuation line, maybe adjust the
13070 window start in case the window's width changed. */
13071 if (XMARKER (w->start)->buffer == current_buffer)
13072 compute_window_start_on_continuation_line (w);
13073
13074 w->window_end_valid = Qnil;
13075 }
13076
13077 /* Some sanity checks. */
13078 CHECK_WINDOW_END (w);
13079 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
13080 abort ();
13081 if (BYTEPOS (opoint) < CHARPOS (opoint))
13082 abort ();
13083
13084 /* If %c is in mode line, update it if needed. */
13085 if (!NILP (w->column_number_displayed)
13086 /* This alternative quickly identifies a common case
13087 where no change is needed. */
13088 && !(PT == XFASTINT (w->last_point)
13089 && XFASTINT (w->last_modified) >= MODIFF
13090 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
13091 && (XFASTINT (w->column_number_displayed)
13092 != (int) current_column ())) /* iftc */
13093 update_mode_line = 1;
13094
13095 /* Count number of windows showing the selected buffer. An indirect
13096 buffer counts as its base buffer. */
13097 if (!just_this_one_p)
13098 {
13099 struct buffer *current_base, *window_base;
13100 current_base = current_buffer;
13101 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
13102 if (current_base->base_buffer)
13103 current_base = current_base->base_buffer;
13104 if (window_base->base_buffer)
13105 window_base = window_base->base_buffer;
13106 if (current_base == window_base)
13107 buffer_shared++;
13108 }
13109
13110 /* Point refers normally to the selected window. For any other
13111 window, set up appropriate value. */
13112 if (!EQ (window, selected_window))
13113 {
13114 int new_pt = XMARKER (w->pointm)->charpos;
13115 int new_pt_byte = marker_byte_position (w->pointm);
13116 if (new_pt < BEGV)
13117 {
13118 new_pt = BEGV;
13119 new_pt_byte = BEGV_BYTE;
13120 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
13121 }
13122 else if (new_pt > (ZV - 1))
13123 {
13124 new_pt = ZV;
13125 new_pt_byte = ZV_BYTE;
13126 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
13127 }
13128
13129 /* We don't use SET_PT so that the point-motion hooks don't run. */
13130 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
13131 }
13132
13133 /* If any of the character widths specified in the display table
13134 have changed, invalidate the width run cache. It's true that
13135 this may be a bit late to catch such changes, but the rest of
13136 redisplay goes (non-fatally) haywire when the display table is
13137 changed, so why should we worry about doing any better? */
13138 if (current_buffer->width_run_cache)
13139 {
13140 struct Lisp_Char_Table *disptab = buffer_display_table ();
13141
13142 if (! disptab_matches_widthtab (disptab,
13143 XVECTOR (current_buffer->width_table)))
13144 {
13145 invalidate_region_cache (current_buffer,
13146 current_buffer->width_run_cache,
13147 BEG, Z);
13148 recompute_width_table (current_buffer, disptab);
13149 }
13150 }
13151
13152 /* If window-start is screwed up, choose a new one. */
13153 if (XMARKER (w->start)->buffer != current_buffer)
13154 goto recenter;
13155
13156 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13157
13158 /* If someone specified a new starting point but did not insist,
13159 check whether it can be used. */
13160 if (!NILP (w->optional_new_start)
13161 && CHARPOS (startp) >= BEGV
13162 && CHARPOS (startp) <= ZV)
13163 {
13164 w->optional_new_start = Qnil;
13165 start_display (&it, w, startp);
13166 move_it_to (&it, PT, 0, it.last_visible_y, -1,
13167 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
13168 if (IT_CHARPOS (it) == PT)
13169 w->force_start = Qt;
13170 /* IT may overshoot PT if text at PT is invisible. */
13171 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
13172 w->force_start = Qt;
13173 }
13174
13175 force_start:
13176
13177 /* Handle case where place to start displaying has been specified,
13178 unless the specified location is outside the accessible range. */
13179 if (!NILP (w->force_start)
13180 || w->frozen_window_start_p)
13181 {
13182 /* We set this later on if we have to adjust point. */
13183 int new_vpos = -1;
13184 int val;
13185
13186 w->force_start = Qnil;
13187 w->vscroll = 0;
13188 w->window_end_valid = Qnil;
13189
13190 /* Forget any recorded base line for line number display. */
13191 if (!buffer_unchanged_p)
13192 w->base_line_number = Qnil;
13193
13194 /* Redisplay the mode line. Select the buffer properly for that.
13195 Also, run the hook window-scroll-functions
13196 because we have scrolled. */
13197 /* Note, we do this after clearing force_start because
13198 if there's an error, it is better to forget about force_start
13199 than to get into an infinite loop calling the hook functions
13200 and having them get more errors. */
13201 if (!update_mode_line
13202 || ! NILP (Vwindow_scroll_functions))
13203 {
13204 update_mode_line = 1;
13205 w->update_mode_line = Qt;
13206 startp = run_window_scroll_functions (window, startp);
13207 }
13208
13209 w->last_modified = make_number (0);
13210 w->last_overlay_modified = make_number (0);
13211 if (CHARPOS (startp) < BEGV)
13212 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
13213 else if (CHARPOS (startp) > ZV)
13214 SET_TEXT_POS (startp, ZV, ZV_BYTE);
13215
13216 /* Redisplay, then check if cursor has been set during the
13217 redisplay. Give up if new fonts were loaded. */
13218 val = try_window (window, startp, 1);
13219 if (!val)
13220 {
13221 w->force_start = Qt;
13222 clear_glyph_matrix (w->desired_matrix);
13223 goto need_larger_matrices;
13224 }
13225 /* Point was outside the scroll margins. */
13226 if (val < 0)
13227 new_vpos = window_box_height (w) / 2;
13228
13229 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
13230 {
13231 /* If point does not appear, try to move point so it does
13232 appear. The desired matrix has been built above, so we
13233 can use it here. */
13234 new_vpos = window_box_height (w) / 2;
13235 }
13236
13237 if (!cursor_row_fully_visible_p (w, 0, 0))
13238 {
13239 /* Point does appear, but on a line partly visible at end of window.
13240 Move it back to a fully-visible line. */
13241 new_vpos = window_box_height (w);
13242 }
13243
13244 /* If we need to move point for either of the above reasons,
13245 now actually do it. */
13246 if (new_vpos >= 0)
13247 {
13248 struct glyph_row *row;
13249
13250 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
13251 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
13252 ++row;
13253
13254 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
13255 MATRIX_ROW_START_BYTEPOS (row));
13256
13257 if (w != XWINDOW (selected_window))
13258 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
13259 else if (current_buffer == old)
13260 SET_TEXT_POS (lpoint, PT, PT_BYTE);
13261
13262 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
13263
13264 /* If we are highlighting the region, then we just changed
13265 the region, so redisplay to show it. */
13266 if (!NILP (Vtransient_mark_mode)
13267 && !NILP (current_buffer->mark_active))
13268 {
13269 clear_glyph_matrix (w->desired_matrix);
13270 if (!try_window (window, startp, 0))
13271 goto need_larger_matrices;
13272 }
13273 }
13274
13275 #if GLYPH_DEBUG
13276 debug_method_add (w, "forced window start");
13277 #endif
13278 goto done;
13279 }
13280
13281 /* Handle case where text has not changed, only point, and it has
13282 not moved off the frame, and we are not retrying after hscroll.
13283 (current_matrix_up_to_date_p is nonzero when retrying.) */
13284 if (current_matrix_up_to_date_p
13285 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
13286 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
13287 {
13288 switch (rc)
13289 {
13290 case CURSOR_MOVEMENT_SUCCESS:
13291 used_current_matrix_p = 1;
13292 goto done;
13293
13294 #if 0 /* try_cursor_movement never returns this value. */
13295 case CURSOR_MOVEMENT_NEED_LARGER_MATRICES:
13296 goto need_larger_matrices;
13297 #endif
13298
13299 case CURSOR_MOVEMENT_MUST_SCROLL:
13300 goto try_to_scroll;
13301
13302 default:
13303 abort ();
13304 }
13305 }
13306 /* If current starting point was originally the beginning of a line
13307 but no longer is, find a new starting point. */
13308 else if (!NILP (w->start_at_line_beg)
13309 && !(CHARPOS (startp) <= BEGV
13310 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
13311 {
13312 #if GLYPH_DEBUG
13313 debug_method_add (w, "recenter 1");
13314 #endif
13315 goto recenter;
13316 }
13317
13318 /* Try scrolling with try_window_id. Value is > 0 if update has
13319 been done, it is -1 if we know that the same window start will
13320 not work. It is 0 if unsuccessful for some other reason. */
13321 else if ((tem = try_window_id (w)) != 0)
13322 {
13323 #if GLYPH_DEBUG
13324 debug_method_add (w, "try_window_id %d", tem);
13325 #endif
13326
13327 if (fonts_changed_p)
13328 goto need_larger_matrices;
13329 if (tem > 0)
13330 goto done;
13331
13332 /* Otherwise try_window_id has returned -1 which means that we
13333 don't want the alternative below this comment to execute. */
13334 }
13335 else if (CHARPOS (startp) >= BEGV
13336 && CHARPOS (startp) <= ZV
13337 && PT >= CHARPOS (startp)
13338 && (CHARPOS (startp) < ZV
13339 /* Avoid starting at end of buffer. */
13340 || CHARPOS (startp) == BEGV
13341 || (XFASTINT (w->last_modified) >= MODIFF
13342 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
13343 {
13344
13345 /* If first window line is a continuation line, and window start
13346 is inside the modified region, but the first change is before
13347 current window start, we must select a new window start.
13348
13349 However, if this is the result of a down-mouse event (e.g. by
13350 extending the mouse-drag-overlay), we don't want to select a
13351 new window start, since that would change the position under
13352 the mouse, resulting in an unwanted mouse-movement rather
13353 than a simple mouse-click. */
13354 if (NILP (w->start_at_line_beg)
13355 && NILP (do_mouse_tracking)
13356 && CHARPOS (startp) > BEGV
13357 && CHARPOS (startp) > BEG + beg_unchanged
13358 && CHARPOS (startp) <= Z - end_unchanged)
13359 {
13360 w->force_start = Qt;
13361 if (XMARKER (w->start)->buffer == current_buffer)
13362 compute_window_start_on_continuation_line (w);
13363 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13364 goto force_start;
13365 }
13366
13367 #if GLYPH_DEBUG
13368 debug_method_add (w, "same window start");
13369 #endif
13370
13371 /* Try to redisplay starting at same place as before.
13372 If point has not moved off frame, accept the results. */
13373 if (!current_matrix_up_to_date_p
13374 /* Don't use try_window_reusing_current_matrix in this case
13375 because a window scroll function can have changed the
13376 buffer. */
13377 || !NILP (Vwindow_scroll_functions)
13378 || MINI_WINDOW_P (w)
13379 || !(used_current_matrix_p
13380 = try_window_reusing_current_matrix (w)))
13381 {
13382 IF_DEBUG (debug_method_add (w, "1"));
13383 if (try_window (window, startp, 1) < 0)
13384 /* -1 means we need to scroll.
13385 0 means we need new matrices, but fonts_changed_p
13386 is set in that case, so we will detect it below. */
13387 goto try_to_scroll;
13388 }
13389
13390 if (fonts_changed_p)
13391 goto need_larger_matrices;
13392
13393 if (w->cursor.vpos >= 0)
13394 {
13395 if (!just_this_one_p
13396 || current_buffer->clip_changed
13397 || BEG_UNCHANGED < CHARPOS (startp))
13398 /* Forget any recorded base line for line number display. */
13399 w->base_line_number = Qnil;
13400
13401 if (!cursor_row_fully_visible_p (w, 1, 0))
13402 {
13403 clear_glyph_matrix (w->desired_matrix);
13404 last_line_misfit = 1;
13405 }
13406 /* Drop through and scroll. */
13407 else
13408 goto done;
13409 }
13410 else
13411 clear_glyph_matrix (w->desired_matrix);
13412 }
13413
13414 try_to_scroll:
13415
13416 w->last_modified = make_number (0);
13417 w->last_overlay_modified = make_number (0);
13418
13419 /* Redisplay the mode line. Select the buffer properly for that. */
13420 if (!update_mode_line)
13421 {
13422 update_mode_line = 1;
13423 w->update_mode_line = Qt;
13424 }
13425
13426 /* Try to scroll by specified few lines. */
13427 if ((scroll_conservatively
13428 || scroll_step
13429 || temp_scroll_step
13430 || NUMBERP (current_buffer->scroll_up_aggressively)
13431 || NUMBERP (current_buffer->scroll_down_aggressively))
13432 && !current_buffer->clip_changed
13433 && CHARPOS (startp) >= BEGV
13434 && CHARPOS (startp) <= ZV)
13435 {
13436 /* The function returns -1 if new fonts were loaded, 1 if
13437 successful, 0 if not successful. */
13438 int rc = try_scrolling (window, just_this_one_p,
13439 scroll_conservatively,
13440 scroll_step,
13441 temp_scroll_step, last_line_misfit);
13442 switch (rc)
13443 {
13444 case SCROLLING_SUCCESS:
13445 goto done;
13446
13447 case SCROLLING_NEED_LARGER_MATRICES:
13448 goto need_larger_matrices;
13449
13450 case SCROLLING_FAILED:
13451 break;
13452
13453 default:
13454 abort ();
13455 }
13456 }
13457
13458 /* Finally, just choose place to start which centers point */
13459
13460 recenter:
13461 if (centering_position < 0)
13462 centering_position = window_box_height (w) / 2;
13463
13464 #if GLYPH_DEBUG
13465 debug_method_add (w, "recenter");
13466 #endif
13467
13468 /* w->vscroll = 0; */
13469
13470 /* Forget any previously recorded base line for line number display. */
13471 if (!buffer_unchanged_p)
13472 w->base_line_number = Qnil;
13473
13474 /* Move backward half the height of the window. */
13475 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
13476 it.current_y = it.last_visible_y;
13477 move_it_vertically_backward (&it, centering_position);
13478 xassert (IT_CHARPOS (it) >= BEGV);
13479
13480 /* The function move_it_vertically_backward may move over more
13481 than the specified y-distance. If it->w is small, e.g. a
13482 mini-buffer window, we may end up in front of the window's
13483 display area. Start displaying at the start of the line
13484 containing PT in this case. */
13485 if (it.current_y <= 0)
13486 {
13487 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
13488 move_it_vertically_backward (&it, 0);
13489 #if 0
13490 /* I think this assert is bogus if buffer contains
13491 invisible text or images. KFS. */
13492 xassert (IT_CHARPOS (it) <= PT);
13493 #endif
13494 it.current_y = 0;
13495 }
13496
13497 it.current_x = it.hpos = 0;
13498
13499 /* Set startp here explicitly in case that helps avoid an infinite loop
13500 in case the window-scroll-functions functions get errors. */
13501 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
13502
13503 /* Run scroll hooks. */
13504 startp = run_window_scroll_functions (window, it.current.pos);
13505
13506 /* Redisplay the window. */
13507 if (!current_matrix_up_to_date_p
13508 || windows_or_buffers_changed
13509 || cursor_type_changed
13510 /* Don't use try_window_reusing_current_matrix in this case
13511 because it can have changed the buffer. */
13512 || !NILP (Vwindow_scroll_functions)
13513 || !just_this_one_p
13514 || MINI_WINDOW_P (w)
13515 || !(used_current_matrix_p
13516 = try_window_reusing_current_matrix (w)))
13517 try_window (window, startp, 0);
13518
13519 /* If new fonts have been loaded (due to fontsets), give up. We
13520 have to start a new redisplay since we need to re-adjust glyph
13521 matrices. */
13522 if (fonts_changed_p)
13523 goto need_larger_matrices;
13524
13525 /* If cursor did not appear assume that the middle of the window is
13526 in the first line of the window. Do it again with the next line.
13527 (Imagine a window of height 100, displaying two lines of height
13528 60. Moving back 50 from it->last_visible_y will end in the first
13529 line.) */
13530 if (w->cursor.vpos < 0)
13531 {
13532 if (!NILP (w->window_end_valid)
13533 && PT >= Z - XFASTINT (w->window_end_pos))
13534 {
13535 clear_glyph_matrix (w->desired_matrix);
13536 move_it_by_lines (&it, 1, 0);
13537 try_window (window, it.current.pos, 0);
13538 }
13539 else if (PT < IT_CHARPOS (it))
13540 {
13541 clear_glyph_matrix (w->desired_matrix);
13542 move_it_by_lines (&it, -1, 0);
13543 try_window (window, it.current.pos, 0);
13544 }
13545 else
13546 {
13547 /* Not much we can do about it. */
13548 }
13549 }
13550
13551 /* Consider the following case: Window starts at BEGV, there is
13552 invisible, intangible text at BEGV, so that display starts at
13553 some point START > BEGV. It can happen that we are called with
13554 PT somewhere between BEGV and START. Try to handle that case. */
13555 if (w->cursor.vpos < 0)
13556 {
13557 struct glyph_row *row = w->current_matrix->rows;
13558 if (row->mode_line_p)
13559 ++row;
13560 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13561 }
13562
13563 if (!cursor_row_fully_visible_p (w, 0, 0))
13564 {
13565 /* If vscroll is enabled, disable it and try again. */
13566 if (w->vscroll)
13567 {
13568 w->vscroll = 0;
13569 clear_glyph_matrix (w->desired_matrix);
13570 goto recenter;
13571 }
13572
13573 /* If centering point failed to make the whole line visible,
13574 put point at the top instead. That has to make the whole line
13575 visible, if it can be done. */
13576 if (centering_position == 0)
13577 goto done;
13578
13579 clear_glyph_matrix (w->desired_matrix);
13580 centering_position = 0;
13581 goto recenter;
13582 }
13583
13584 done:
13585
13586 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13587 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
13588 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
13589 ? Qt : Qnil);
13590
13591 /* Display the mode line, if we must. */
13592 if ((update_mode_line
13593 /* If window not full width, must redo its mode line
13594 if (a) the window to its side is being redone and
13595 (b) we do a frame-based redisplay. This is a consequence
13596 of how inverted lines are drawn in frame-based redisplay. */
13597 || (!just_this_one_p
13598 && !FRAME_WINDOW_P (f)
13599 && !WINDOW_FULL_WIDTH_P (w))
13600 /* Line number to display. */
13601 || INTEGERP (w->base_line_pos)
13602 /* Column number is displayed and different from the one displayed. */
13603 || (!NILP (w->column_number_displayed)
13604 && (XFASTINT (w->column_number_displayed)
13605 != (int) current_column ()))) /* iftc */
13606 /* This means that the window has a mode line. */
13607 && (WINDOW_WANTS_MODELINE_P (w)
13608 || WINDOW_WANTS_HEADER_LINE_P (w)))
13609 {
13610 display_mode_lines (w);
13611
13612 /* If mode line height has changed, arrange for a thorough
13613 immediate redisplay using the correct mode line height. */
13614 if (WINDOW_WANTS_MODELINE_P (w)
13615 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
13616 {
13617 fonts_changed_p = 1;
13618 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
13619 = DESIRED_MODE_LINE_HEIGHT (w);
13620 }
13621
13622 /* If top line height has changed, arrange for a thorough
13623 immediate redisplay using the correct mode line height. */
13624 if (WINDOW_WANTS_HEADER_LINE_P (w)
13625 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
13626 {
13627 fonts_changed_p = 1;
13628 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
13629 = DESIRED_HEADER_LINE_HEIGHT (w);
13630 }
13631
13632 if (fonts_changed_p)
13633 goto need_larger_matrices;
13634 }
13635
13636 if (!line_number_displayed
13637 && !BUFFERP (w->base_line_pos))
13638 {
13639 w->base_line_pos = Qnil;
13640 w->base_line_number = Qnil;
13641 }
13642
13643 finish_menu_bars:
13644
13645 /* When we reach a frame's selected window, redo the frame's menu bar. */
13646 if (update_mode_line
13647 && EQ (FRAME_SELECTED_WINDOW (f), window))
13648 {
13649 int redisplay_menu_p = 0;
13650 int redisplay_tool_bar_p = 0;
13651
13652 if (FRAME_WINDOW_P (f))
13653 {
13654 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
13655 || defined (USE_GTK)
13656 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
13657 #else
13658 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
13659 #endif
13660 }
13661 else
13662 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
13663
13664 if (redisplay_menu_p)
13665 display_menu_bar (w);
13666
13667 #ifdef HAVE_WINDOW_SYSTEM
13668 if (FRAME_WINDOW_P (f))
13669 {
13670 #if defined (USE_GTK) || USE_MAC_TOOLBAR
13671 redisplay_tool_bar_p = FRAME_EXTERNAL_TOOL_BAR (f);
13672 #else
13673 redisplay_tool_bar_p = WINDOWP (f->tool_bar_window)
13674 && (FRAME_TOOL_BAR_LINES (f) > 0
13675 || !NILP (Vauto_resize_tool_bars));
13676 #endif
13677
13678 if (redisplay_tool_bar_p && redisplay_tool_bar (f))
13679 {
13680 extern int ignore_mouse_drag_p;
13681 ignore_mouse_drag_p = 1;
13682 }
13683 }
13684 #endif
13685 }
13686
13687 #ifdef HAVE_WINDOW_SYSTEM
13688 if (FRAME_WINDOW_P (f)
13689 && update_window_fringes (w, (just_this_one_p
13690 || (!used_current_matrix_p && !overlay_arrow_seen)
13691 || w->pseudo_window_p)))
13692 {
13693 update_begin (f);
13694 BLOCK_INPUT;
13695 if (draw_window_fringes (w, 1))
13696 x_draw_vertical_border (w);
13697 UNBLOCK_INPUT;
13698 update_end (f);
13699 }
13700 #endif /* HAVE_WINDOW_SYSTEM */
13701
13702 /* We go to this label, with fonts_changed_p nonzero,
13703 if it is necessary to try again using larger glyph matrices.
13704 We have to redeem the scroll bar even in this case,
13705 because the loop in redisplay_internal expects that. */
13706 need_larger_matrices:
13707 ;
13708 finish_scroll_bars:
13709
13710 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
13711 {
13712 /* Set the thumb's position and size. */
13713 set_vertical_scroll_bar (w);
13714
13715 /* Note that we actually used the scroll bar attached to this
13716 window, so it shouldn't be deleted at the end of redisplay. */
13717 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
13718 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
13719 }
13720
13721 /* Restore current_buffer and value of point in it. */
13722 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
13723 set_buffer_internal_1 (old);
13724 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
13725 shorter. This can be caused by log truncation in *Messages*. */
13726 if (CHARPOS (lpoint) <= ZV)
13727 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
13728
13729 unbind_to (count, Qnil);
13730 }
13731
13732
13733 /* Build the complete desired matrix of WINDOW with a window start
13734 buffer position POS.
13735
13736 Value is 1 if successful. It is zero if fonts were loaded during
13737 redisplay which makes re-adjusting glyph matrices necessary, and -1
13738 if point would appear in the scroll margins.
13739 (We check that only if CHECK_MARGINS is nonzero. */
13740
13741 int
13742 try_window (window, pos, check_margins)
13743 Lisp_Object window;
13744 struct text_pos pos;
13745 int check_margins;
13746 {
13747 struct window *w = XWINDOW (window);
13748 struct it it;
13749 struct glyph_row *last_text_row = NULL;
13750 struct frame *f = XFRAME (w->frame);
13751
13752 /* Make POS the new window start. */
13753 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
13754
13755 /* Mark cursor position as unknown. No overlay arrow seen. */
13756 w->cursor.vpos = -1;
13757 overlay_arrow_seen = 0;
13758
13759 /* Initialize iterator and info to start at POS. */
13760 start_display (&it, w, pos);
13761
13762 /* Display all lines of W. */
13763 while (it.current_y < it.last_visible_y)
13764 {
13765 if (display_line (&it))
13766 last_text_row = it.glyph_row - 1;
13767 if (fonts_changed_p)
13768 return 0;
13769 }
13770
13771 /* Don't let the cursor end in the scroll margins. */
13772 if (check_margins
13773 && !MINI_WINDOW_P (w))
13774 {
13775 int this_scroll_margin;
13776
13777 this_scroll_margin = max (0, scroll_margin);
13778 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
13779 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
13780
13781 if ((w->cursor.y >= 0 /* not vscrolled */
13782 && w->cursor.y < this_scroll_margin
13783 && CHARPOS (pos) > BEGV
13784 && IT_CHARPOS (it) < ZV)
13785 /* rms: considering make_cursor_line_fully_visible_p here
13786 seems to give wrong results. We don't want to recenter
13787 when the last line is partly visible, we want to allow
13788 that case to be handled in the usual way. */
13789 || (w->cursor.y + 1) > it.last_visible_y)
13790 {
13791 w->cursor.vpos = -1;
13792 clear_glyph_matrix (w->desired_matrix);
13793 return -1;
13794 }
13795 }
13796
13797 /* If bottom moved off end of frame, change mode line percentage. */
13798 if (XFASTINT (w->window_end_pos) <= 0
13799 && Z != IT_CHARPOS (it))
13800 w->update_mode_line = Qt;
13801
13802 /* Set window_end_pos to the offset of the last character displayed
13803 on the window from the end of current_buffer. Set
13804 window_end_vpos to its row number. */
13805 if (last_text_row)
13806 {
13807 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
13808 w->window_end_bytepos
13809 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
13810 w->window_end_pos
13811 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
13812 w->window_end_vpos
13813 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
13814 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
13815 ->displays_text_p);
13816 }
13817 else
13818 {
13819 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
13820 w->window_end_pos = make_number (Z - ZV);
13821 w->window_end_vpos = make_number (0);
13822 }
13823
13824 /* But that is not valid info until redisplay finishes. */
13825 w->window_end_valid = Qnil;
13826 return 1;
13827 }
13828
13829
13830 \f
13831 /************************************************************************
13832 Window redisplay reusing current matrix when buffer has not changed
13833 ************************************************************************/
13834
13835 /* Try redisplay of window W showing an unchanged buffer with a
13836 different window start than the last time it was displayed by
13837 reusing its current matrix. Value is non-zero if successful.
13838 W->start is the new window start. */
13839
13840 static int
13841 try_window_reusing_current_matrix (w)
13842 struct window *w;
13843 {
13844 struct frame *f = XFRAME (w->frame);
13845 struct glyph_row *row, *bottom_row;
13846 struct it it;
13847 struct run run;
13848 struct text_pos start, new_start;
13849 int nrows_scrolled, i;
13850 struct glyph_row *last_text_row;
13851 struct glyph_row *last_reused_text_row;
13852 struct glyph_row *start_row;
13853 int start_vpos, min_y, max_y;
13854
13855 #if GLYPH_DEBUG
13856 if (inhibit_try_window_reusing)
13857 return 0;
13858 #endif
13859
13860 if (/* This function doesn't handle terminal frames. */
13861 !FRAME_WINDOW_P (f)
13862 /* Don't try to reuse the display if windows have been split
13863 or such. */
13864 || windows_or_buffers_changed
13865 || cursor_type_changed)
13866 return 0;
13867
13868 /* Can't do this if region may have changed. */
13869 if ((!NILP (Vtransient_mark_mode)
13870 && !NILP (current_buffer->mark_active))
13871 || !NILP (w->region_showing)
13872 || !NILP (Vshow_trailing_whitespace))
13873 return 0;
13874
13875 /* If top-line visibility has changed, give up. */
13876 if (WINDOW_WANTS_HEADER_LINE_P (w)
13877 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
13878 return 0;
13879
13880 /* Give up if old or new display is scrolled vertically. We could
13881 make this function handle this, but right now it doesn't. */
13882 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
13883 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
13884 return 0;
13885
13886 /* The variable new_start now holds the new window start. The old
13887 start `start' can be determined from the current matrix. */
13888 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
13889 start = start_row->start.pos;
13890 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
13891
13892 /* Clear the desired matrix for the display below. */
13893 clear_glyph_matrix (w->desired_matrix);
13894
13895 if (CHARPOS (new_start) <= CHARPOS (start))
13896 {
13897 int first_row_y;
13898
13899 /* Don't use this method if the display starts with an ellipsis
13900 displayed for invisible text. It's not easy to handle that case
13901 below, and it's certainly not worth the effort since this is
13902 not a frequent case. */
13903 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
13904 return 0;
13905
13906 IF_DEBUG (debug_method_add (w, "twu1"));
13907
13908 /* Display up to a row that can be reused. The variable
13909 last_text_row is set to the last row displayed that displays
13910 text. Note that it.vpos == 0 if or if not there is a
13911 header-line; it's not the same as the MATRIX_ROW_VPOS! */
13912 start_display (&it, w, new_start);
13913 first_row_y = it.current_y;
13914 w->cursor.vpos = -1;
13915 last_text_row = last_reused_text_row = NULL;
13916
13917 while (it.current_y < it.last_visible_y
13918 && !fonts_changed_p)
13919 {
13920 /* If we have reached into the characters in the START row,
13921 that means the line boundaries have changed. So we
13922 can't start copying with the row START. Maybe it will
13923 work to start copying with the following row. */
13924 while (IT_CHARPOS (it) > CHARPOS (start))
13925 {
13926 /* Advance to the next row as the "start". */
13927 start_row++;
13928 start = start_row->start.pos;
13929 /* If there are no more rows to try, or just one, give up. */
13930 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
13931 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
13932 || CHARPOS (start) == ZV)
13933 {
13934 clear_glyph_matrix (w->desired_matrix);
13935 return 0;
13936 }
13937
13938 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
13939 }
13940 /* If we have reached alignment,
13941 we can copy the rest of the rows. */
13942 if (IT_CHARPOS (it) == CHARPOS (start))
13943 break;
13944
13945 if (display_line (&it))
13946 last_text_row = it.glyph_row - 1;
13947 }
13948
13949 /* A value of current_y < last_visible_y means that we stopped
13950 at the previous window start, which in turn means that we
13951 have at least one reusable row. */
13952 if (it.current_y < it.last_visible_y)
13953 {
13954 /* IT.vpos always starts from 0; it counts text lines. */
13955 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
13956
13957 /* Find PT if not already found in the lines displayed. */
13958 if (w->cursor.vpos < 0)
13959 {
13960 int dy = it.current_y - start_row->y;
13961
13962 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
13963 row = row_containing_pos (w, PT, row, NULL, dy);
13964 if (row)
13965 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
13966 dy, nrows_scrolled);
13967 else
13968 {
13969 clear_glyph_matrix (w->desired_matrix);
13970 return 0;
13971 }
13972 }
13973
13974 /* Scroll the display. Do it before the current matrix is
13975 changed. The problem here is that update has not yet
13976 run, i.e. part of the current matrix is not up to date.
13977 scroll_run_hook will clear the cursor, and use the
13978 current matrix to get the height of the row the cursor is
13979 in. */
13980 run.current_y = start_row->y;
13981 run.desired_y = it.current_y;
13982 run.height = it.last_visible_y - it.current_y;
13983
13984 if (run.height > 0 && run.current_y != run.desired_y)
13985 {
13986 update_begin (f);
13987 FRAME_RIF (f)->update_window_begin_hook (w);
13988 FRAME_RIF (f)->clear_window_mouse_face (w);
13989 FRAME_RIF (f)->scroll_run_hook (w, &run);
13990 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
13991 update_end (f);
13992 }
13993
13994 /* Shift current matrix down by nrows_scrolled lines. */
13995 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
13996 rotate_matrix (w->current_matrix,
13997 start_vpos,
13998 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
13999 nrows_scrolled);
14000
14001 /* Disable lines that must be updated. */
14002 for (i = 0; i < nrows_scrolled; ++i)
14003 (start_row + i)->enabled_p = 0;
14004
14005 /* Re-compute Y positions. */
14006 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
14007 max_y = it.last_visible_y;
14008 for (row = start_row + nrows_scrolled;
14009 row < bottom_row;
14010 ++row)
14011 {
14012 row->y = it.current_y;
14013 row->visible_height = row->height;
14014
14015 if (row->y < min_y)
14016 row->visible_height -= min_y - row->y;
14017 if (row->y + row->height > max_y)
14018 row->visible_height -= row->y + row->height - max_y;
14019 row->redraw_fringe_bitmaps_p = 1;
14020
14021 it.current_y += row->height;
14022
14023 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14024 last_reused_text_row = row;
14025 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
14026 break;
14027 }
14028
14029 /* Disable lines in the current matrix which are now
14030 below the window. */
14031 for (++row; row < bottom_row; ++row)
14032 row->enabled_p = row->mode_line_p = 0;
14033 }
14034
14035 /* Update window_end_pos etc.; last_reused_text_row is the last
14036 reused row from the current matrix containing text, if any.
14037 The value of last_text_row is the last displayed line
14038 containing text. */
14039 if (last_reused_text_row)
14040 {
14041 w->window_end_bytepos
14042 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
14043 w->window_end_pos
14044 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
14045 w->window_end_vpos
14046 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
14047 w->current_matrix));
14048 }
14049 else if (last_text_row)
14050 {
14051 w->window_end_bytepos
14052 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
14053 w->window_end_pos
14054 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
14055 w->window_end_vpos
14056 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
14057 }
14058 else
14059 {
14060 /* This window must be completely empty. */
14061 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
14062 w->window_end_pos = make_number (Z - ZV);
14063 w->window_end_vpos = make_number (0);
14064 }
14065 w->window_end_valid = Qnil;
14066
14067 /* Update hint: don't try scrolling again in update_window. */
14068 w->desired_matrix->no_scrolling_p = 1;
14069
14070 #if GLYPH_DEBUG
14071 debug_method_add (w, "try_window_reusing_current_matrix 1");
14072 #endif
14073 return 1;
14074 }
14075 else if (CHARPOS (new_start) > CHARPOS (start))
14076 {
14077 struct glyph_row *pt_row, *row;
14078 struct glyph_row *first_reusable_row;
14079 struct glyph_row *first_row_to_display;
14080 int dy;
14081 int yb = window_text_bottom_y (w);
14082
14083 /* Find the row starting at new_start, if there is one. Don't
14084 reuse a partially visible line at the end. */
14085 first_reusable_row = start_row;
14086 while (first_reusable_row->enabled_p
14087 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
14088 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
14089 < CHARPOS (new_start)))
14090 ++first_reusable_row;
14091
14092 /* Give up if there is no row to reuse. */
14093 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
14094 || !first_reusable_row->enabled_p
14095 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
14096 != CHARPOS (new_start)))
14097 return 0;
14098
14099 /* We can reuse fully visible rows beginning with
14100 first_reusable_row to the end of the window. Set
14101 first_row_to_display to the first row that cannot be reused.
14102 Set pt_row to the row containing point, if there is any. */
14103 pt_row = NULL;
14104 for (first_row_to_display = first_reusable_row;
14105 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
14106 ++first_row_to_display)
14107 {
14108 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
14109 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
14110 pt_row = first_row_to_display;
14111 }
14112
14113 /* Start displaying at the start of first_row_to_display. */
14114 xassert (first_row_to_display->y < yb);
14115 init_to_row_start (&it, w, first_row_to_display);
14116
14117 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
14118 - start_vpos);
14119 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
14120 - nrows_scrolled);
14121 it.current_y = (first_row_to_display->y - first_reusable_row->y
14122 + WINDOW_HEADER_LINE_HEIGHT (w));
14123
14124 /* Display lines beginning with first_row_to_display in the
14125 desired matrix. Set last_text_row to the last row displayed
14126 that displays text. */
14127 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
14128 if (pt_row == NULL)
14129 w->cursor.vpos = -1;
14130 last_text_row = NULL;
14131 while (it.current_y < it.last_visible_y && !fonts_changed_p)
14132 if (display_line (&it))
14133 last_text_row = it.glyph_row - 1;
14134
14135 /* Give up If point isn't in a row displayed or reused. */
14136 if (w->cursor.vpos < 0)
14137 {
14138 clear_glyph_matrix (w->desired_matrix);
14139 return 0;
14140 }
14141
14142 /* If point is in a reused row, adjust y and vpos of the cursor
14143 position. */
14144 if (pt_row)
14145 {
14146 w->cursor.vpos -= nrows_scrolled;
14147 w->cursor.y -= first_reusable_row->y - start_row->y;
14148 }
14149
14150 /* Scroll the display. */
14151 run.current_y = first_reusable_row->y;
14152 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
14153 run.height = it.last_visible_y - run.current_y;
14154 dy = run.current_y - run.desired_y;
14155
14156 if (run.height)
14157 {
14158 update_begin (f);
14159 FRAME_RIF (f)->update_window_begin_hook (w);
14160 FRAME_RIF (f)->clear_window_mouse_face (w);
14161 FRAME_RIF (f)->scroll_run_hook (w, &run);
14162 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
14163 update_end (f);
14164 }
14165
14166 /* Adjust Y positions of reused rows. */
14167 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
14168 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
14169 max_y = it.last_visible_y;
14170 for (row = first_reusable_row; row < first_row_to_display; ++row)
14171 {
14172 row->y -= dy;
14173 row->visible_height = row->height;
14174 if (row->y < min_y)
14175 row->visible_height -= min_y - row->y;
14176 if (row->y + row->height > max_y)
14177 row->visible_height -= row->y + row->height - max_y;
14178 row->redraw_fringe_bitmaps_p = 1;
14179 }
14180
14181 /* Scroll the current matrix. */
14182 xassert (nrows_scrolled > 0);
14183 rotate_matrix (w->current_matrix,
14184 start_vpos,
14185 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
14186 -nrows_scrolled);
14187
14188 /* Disable rows not reused. */
14189 for (row -= nrows_scrolled; row < bottom_row; ++row)
14190 row->enabled_p = 0;
14191
14192 /* Point may have moved to a different line, so we cannot assume that
14193 the previous cursor position is valid; locate the correct row. */
14194 if (pt_row)
14195 {
14196 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
14197 row < bottom_row && PT >= MATRIX_ROW_END_CHARPOS (row);
14198 row++)
14199 {
14200 w->cursor.vpos++;
14201 w->cursor.y = row->y;
14202 }
14203 if (row < bottom_row)
14204 {
14205 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
14206 while (glyph->charpos < PT)
14207 {
14208 w->cursor.hpos++;
14209 w->cursor.x += glyph->pixel_width;
14210 glyph++;
14211 }
14212 }
14213 }
14214
14215 /* Adjust window end. A null value of last_text_row means that
14216 the window end is in reused rows which in turn means that
14217 only its vpos can have changed. */
14218 if (last_text_row)
14219 {
14220 w->window_end_bytepos
14221 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
14222 w->window_end_pos
14223 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
14224 w->window_end_vpos
14225 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
14226 }
14227 else
14228 {
14229 w->window_end_vpos
14230 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
14231 }
14232
14233 w->window_end_valid = Qnil;
14234 w->desired_matrix->no_scrolling_p = 1;
14235
14236 #if GLYPH_DEBUG
14237 debug_method_add (w, "try_window_reusing_current_matrix 2");
14238 #endif
14239 return 1;
14240 }
14241
14242 return 0;
14243 }
14244
14245
14246 \f
14247 /************************************************************************
14248 Window redisplay reusing current matrix when buffer has changed
14249 ************************************************************************/
14250
14251 static struct glyph_row *find_last_unchanged_at_beg_row P_ ((struct window *));
14252 static struct glyph_row *find_first_unchanged_at_end_row P_ ((struct window *,
14253 int *, int *));
14254 static struct glyph_row *
14255 find_last_row_displaying_text P_ ((struct glyph_matrix *, struct it *,
14256 struct glyph_row *));
14257
14258
14259 /* Return the last row in MATRIX displaying text. If row START is
14260 non-null, start searching with that row. IT gives the dimensions
14261 of the display. Value is null if matrix is empty; otherwise it is
14262 a pointer to the row found. */
14263
14264 static struct glyph_row *
14265 find_last_row_displaying_text (matrix, it, start)
14266 struct glyph_matrix *matrix;
14267 struct it *it;
14268 struct glyph_row *start;
14269 {
14270 struct glyph_row *row, *row_found;
14271
14272 /* Set row_found to the last row in IT->w's current matrix
14273 displaying text. The loop looks funny but think of partially
14274 visible lines. */
14275 row_found = NULL;
14276 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
14277 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14278 {
14279 xassert (row->enabled_p);
14280 row_found = row;
14281 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
14282 break;
14283 ++row;
14284 }
14285
14286 return row_found;
14287 }
14288
14289
14290 /* Return the last row in the current matrix of W that is not affected
14291 by changes at the start of current_buffer that occurred since W's
14292 current matrix was built. Value is null if no such row exists.
14293
14294 BEG_UNCHANGED us the number of characters unchanged at the start of
14295 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
14296 first changed character in current_buffer. Characters at positions <
14297 BEG + BEG_UNCHANGED are at the same buffer positions as they were
14298 when the current matrix was built. */
14299
14300 static struct glyph_row *
14301 find_last_unchanged_at_beg_row (w)
14302 struct window *w;
14303 {
14304 int first_changed_pos = BEG + BEG_UNCHANGED;
14305 struct glyph_row *row;
14306 struct glyph_row *row_found = NULL;
14307 int yb = window_text_bottom_y (w);
14308
14309 /* Find the last row displaying unchanged text. */
14310 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14311 while (MATRIX_ROW_DISPLAYS_TEXT_P (row)
14312 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos)
14313 {
14314 if (/* If row ends before first_changed_pos, it is unchanged,
14315 except in some case. */
14316 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
14317 /* When row ends in ZV and we write at ZV it is not
14318 unchanged. */
14319 && !row->ends_at_zv_p
14320 /* When first_changed_pos is the end of a continued line,
14321 row is not unchanged because it may be no longer
14322 continued. */
14323 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
14324 && (row->continued_p
14325 || row->exact_window_width_line_p)))
14326 row_found = row;
14327
14328 /* Stop if last visible row. */
14329 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
14330 break;
14331
14332 ++row;
14333 }
14334
14335 return row_found;
14336 }
14337
14338
14339 /* Find the first glyph row in the current matrix of W that is not
14340 affected by changes at the end of current_buffer since the
14341 time W's current matrix was built.
14342
14343 Return in *DELTA the number of chars by which buffer positions in
14344 unchanged text at the end of current_buffer must be adjusted.
14345
14346 Return in *DELTA_BYTES the corresponding number of bytes.
14347
14348 Value is null if no such row exists, i.e. all rows are affected by
14349 changes. */
14350
14351 static struct glyph_row *
14352 find_first_unchanged_at_end_row (w, delta, delta_bytes)
14353 struct window *w;
14354 int *delta, *delta_bytes;
14355 {
14356 struct glyph_row *row;
14357 struct glyph_row *row_found = NULL;
14358
14359 *delta = *delta_bytes = 0;
14360
14361 /* Display must not have been paused, otherwise the current matrix
14362 is not up to date. */
14363 if (NILP (w->window_end_valid))
14364 abort ();
14365
14366 /* A value of window_end_pos >= END_UNCHANGED means that the window
14367 end is in the range of changed text. If so, there is no
14368 unchanged row at the end of W's current matrix. */
14369 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
14370 return NULL;
14371
14372 /* Set row to the last row in W's current matrix displaying text. */
14373 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
14374
14375 /* If matrix is entirely empty, no unchanged row exists. */
14376 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14377 {
14378 /* The value of row is the last glyph row in the matrix having a
14379 meaningful buffer position in it. The end position of row
14380 corresponds to window_end_pos. This allows us to translate
14381 buffer positions in the current matrix to current buffer
14382 positions for characters not in changed text. */
14383 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
14384 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
14385 int last_unchanged_pos, last_unchanged_pos_old;
14386 struct glyph_row *first_text_row
14387 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14388
14389 *delta = Z - Z_old;
14390 *delta_bytes = Z_BYTE - Z_BYTE_old;
14391
14392 /* Set last_unchanged_pos to the buffer position of the last
14393 character in the buffer that has not been changed. Z is the
14394 index + 1 of the last character in current_buffer, i.e. by
14395 subtracting END_UNCHANGED we get the index of the last
14396 unchanged character, and we have to add BEG to get its buffer
14397 position. */
14398 last_unchanged_pos = Z - END_UNCHANGED + BEG;
14399 last_unchanged_pos_old = last_unchanged_pos - *delta;
14400
14401 /* Search backward from ROW for a row displaying a line that
14402 starts at a minimum position >= last_unchanged_pos_old. */
14403 for (; row > first_text_row; --row)
14404 {
14405 /* This used to abort, but it can happen.
14406 It is ok to just stop the search instead here. KFS. */
14407 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
14408 break;
14409
14410 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
14411 row_found = row;
14412 }
14413 }
14414
14415 if (row_found && !MATRIX_ROW_DISPLAYS_TEXT_P (row_found))
14416 abort ();
14417
14418 return row_found;
14419 }
14420
14421
14422 /* Make sure that glyph rows in the current matrix of window W
14423 reference the same glyph memory as corresponding rows in the
14424 frame's frame matrix. This function is called after scrolling W's
14425 current matrix on a terminal frame in try_window_id and
14426 try_window_reusing_current_matrix. */
14427
14428 static void
14429 sync_frame_with_window_matrix_rows (w)
14430 struct window *w;
14431 {
14432 struct frame *f = XFRAME (w->frame);
14433 struct glyph_row *window_row, *window_row_end, *frame_row;
14434
14435 /* Preconditions: W must be a leaf window and full-width. Its frame
14436 must have a frame matrix. */
14437 xassert (NILP (w->hchild) && NILP (w->vchild));
14438 xassert (WINDOW_FULL_WIDTH_P (w));
14439 xassert (!FRAME_WINDOW_P (f));
14440
14441 /* If W is a full-width window, glyph pointers in W's current matrix
14442 have, by definition, to be the same as glyph pointers in the
14443 corresponding frame matrix. Note that frame matrices have no
14444 marginal areas (see build_frame_matrix). */
14445 window_row = w->current_matrix->rows;
14446 window_row_end = window_row + w->current_matrix->nrows;
14447 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
14448 while (window_row < window_row_end)
14449 {
14450 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
14451 struct glyph *end = window_row->glyphs[LAST_AREA];
14452
14453 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
14454 frame_row->glyphs[TEXT_AREA] = start;
14455 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
14456 frame_row->glyphs[LAST_AREA] = end;
14457
14458 /* Disable frame rows whose corresponding window rows have
14459 been disabled in try_window_id. */
14460 if (!window_row->enabled_p)
14461 frame_row->enabled_p = 0;
14462
14463 ++window_row, ++frame_row;
14464 }
14465 }
14466
14467
14468 /* Find the glyph row in window W containing CHARPOS. Consider all
14469 rows between START and END (not inclusive). END null means search
14470 all rows to the end of the display area of W. Value is the row
14471 containing CHARPOS or null. */
14472
14473 struct glyph_row *
14474 row_containing_pos (w, charpos, start, end, dy)
14475 struct window *w;
14476 int charpos;
14477 struct glyph_row *start, *end;
14478 int dy;
14479 {
14480 struct glyph_row *row = start;
14481 int last_y;
14482
14483 /* If we happen to start on a header-line, skip that. */
14484 if (row->mode_line_p)
14485 ++row;
14486
14487 if ((end && row >= end) || !row->enabled_p)
14488 return NULL;
14489
14490 last_y = window_text_bottom_y (w) - dy;
14491
14492 while (1)
14493 {
14494 /* Give up if we have gone too far. */
14495 if (end && row >= end)
14496 return NULL;
14497 /* This formerly returned if they were equal.
14498 I think that both quantities are of a "last plus one" type;
14499 if so, when they are equal, the row is within the screen. -- rms. */
14500 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
14501 return NULL;
14502
14503 /* If it is in this row, return this row. */
14504 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
14505 || (MATRIX_ROW_END_CHARPOS (row) == charpos
14506 /* The end position of a row equals the start
14507 position of the next row. If CHARPOS is there, we
14508 would rather display it in the next line, except
14509 when this line ends in ZV. */
14510 && !row->ends_at_zv_p
14511 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
14512 && charpos >= MATRIX_ROW_START_CHARPOS (row))
14513 return row;
14514 ++row;
14515 }
14516 }
14517
14518
14519 /* Try to redisplay window W by reusing its existing display. W's
14520 current matrix must be up to date when this function is called,
14521 i.e. window_end_valid must not be nil.
14522
14523 Value is
14524
14525 1 if display has been updated
14526 0 if otherwise unsuccessful
14527 -1 if redisplay with same window start is known not to succeed
14528
14529 The following steps are performed:
14530
14531 1. Find the last row in the current matrix of W that is not
14532 affected by changes at the start of current_buffer. If no such row
14533 is found, give up.
14534
14535 2. Find the first row in W's current matrix that is not affected by
14536 changes at the end of current_buffer. Maybe there is no such row.
14537
14538 3. Display lines beginning with the row + 1 found in step 1 to the
14539 row found in step 2 or, if step 2 didn't find a row, to the end of
14540 the window.
14541
14542 4. If cursor is not known to appear on the window, give up.
14543
14544 5. If display stopped at the row found in step 2, scroll the
14545 display and current matrix as needed.
14546
14547 6. Maybe display some lines at the end of W, if we must. This can
14548 happen under various circumstances, like a partially visible line
14549 becoming fully visible, or because newly displayed lines are displayed
14550 in smaller font sizes.
14551
14552 7. Update W's window end information. */
14553
14554 static int
14555 try_window_id (w)
14556 struct window *w;
14557 {
14558 struct frame *f = XFRAME (w->frame);
14559 struct glyph_matrix *current_matrix = w->current_matrix;
14560 struct glyph_matrix *desired_matrix = w->desired_matrix;
14561 struct glyph_row *last_unchanged_at_beg_row;
14562 struct glyph_row *first_unchanged_at_end_row;
14563 struct glyph_row *row;
14564 struct glyph_row *bottom_row;
14565 int bottom_vpos;
14566 struct it it;
14567 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
14568 struct text_pos start_pos;
14569 struct run run;
14570 int first_unchanged_at_end_vpos = 0;
14571 struct glyph_row *last_text_row, *last_text_row_at_end;
14572 struct text_pos start;
14573 int first_changed_charpos, last_changed_charpos;
14574
14575 #if GLYPH_DEBUG
14576 if (inhibit_try_window_id)
14577 return 0;
14578 #endif
14579
14580 /* This is handy for debugging. */
14581 #if 0
14582 #define GIVE_UP(X) \
14583 do { \
14584 fprintf (stderr, "try_window_id give up %d\n", (X)); \
14585 return 0; \
14586 } while (0)
14587 #else
14588 #define GIVE_UP(X) return 0
14589 #endif
14590
14591 SET_TEXT_POS_FROM_MARKER (start, w->start);
14592
14593 /* Don't use this for mini-windows because these can show
14594 messages and mini-buffers, and we don't handle that here. */
14595 if (MINI_WINDOW_P (w))
14596 GIVE_UP (1);
14597
14598 /* This flag is used to prevent redisplay optimizations. */
14599 if (windows_or_buffers_changed || cursor_type_changed)
14600 GIVE_UP (2);
14601
14602 /* Verify that narrowing has not changed.
14603 Also verify that we were not told to prevent redisplay optimizations.
14604 It would be nice to further
14605 reduce the number of cases where this prevents try_window_id. */
14606 if (current_buffer->clip_changed
14607 || current_buffer->prevent_redisplay_optimizations_p)
14608 GIVE_UP (3);
14609
14610 /* Window must either use window-based redisplay or be full width. */
14611 if (!FRAME_WINDOW_P (f)
14612 && (!FRAME_LINE_INS_DEL_OK (f)
14613 || !WINDOW_FULL_WIDTH_P (w)))
14614 GIVE_UP (4);
14615
14616 /* Give up if point is not known NOT to appear in W. */
14617 if (PT < CHARPOS (start))
14618 GIVE_UP (5);
14619
14620 /* Another way to prevent redisplay optimizations. */
14621 if (XFASTINT (w->last_modified) == 0)
14622 GIVE_UP (6);
14623
14624 /* Verify that window is not hscrolled. */
14625 if (XFASTINT (w->hscroll) != 0)
14626 GIVE_UP (7);
14627
14628 /* Verify that display wasn't paused. */
14629 if (NILP (w->window_end_valid))
14630 GIVE_UP (8);
14631
14632 /* Can't use this if highlighting a region because a cursor movement
14633 will do more than just set the cursor. */
14634 if (!NILP (Vtransient_mark_mode)
14635 && !NILP (current_buffer->mark_active))
14636 GIVE_UP (9);
14637
14638 /* Likewise if highlighting trailing whitespace. */
14639 if (!NILP (Vshow_trailing_whitespace))
14640 GIVE_UP (11);
14641
14642 /* Likewise if showing a region. */
14643 if (!NILP (w->region_showing))
14644 GIVE_UP (10);
14645
14646 /* Can use this if overlay arrow position and or string have changed. */
14647 if (overlay_arrows_changed_p ())
14648 GIVE_UP (12);
14649
14650
14651 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
14652 only if buffer has really changed. The reason is that the gap is
14653 initially at Z for freshly visited files. The code below would
14654 set end_unchanged to 0 in that case. */
14655 if (MODIFF > SAVE_MODIFF
14656 /* This seems to happen sometimes after saving a buffer. */
14657 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
14658 {
14659 if (GPT - BEG < BEG_UNCHANGED)
14660 BEG_UNCHANGED = GPT - BEG;
14661 if (Z - GPT < END_UNCHANGED)
14662 END_UNCHANGED = Z - GPT;
14663 }
14664
14665 /* The position of the first and last character that has been changed. */
14666 first_changed_charpos = BEG + BEG_UNCHANGED;
14667 last_changed_charpos = Z - END_UNCHANGED;
14668
14669 /* If window starts after a line end, and the last change is in
14670 front of that newline, then changes don't affect the display.
14671 This case happens with stealth-fontification. Note that although
14672 the display is unchanged, glyph positions in the matrix have to
14673 be adjusted, of course. */
14674 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
14675 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
14676 && ((last_changed_charpos < CHARPOS (start)
14677 && CHARPOS (start) == BEGV)
14678 || (last_changed_charpos < CHARPOS (start) - 1
14679 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
14680 {
14681 int Z_old, delta, Z_BYTE_old, delta_bytes;
14682 struct glyph_row *r0;
14683
14684 /* Compute how many chars/bytes have been added to or removed
14685 from the buffer. */
14686 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
14687 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
14688 delta = Z - Z_old;
14689 delta_bytes = Z_BYTE - Z_BYTE_old;
14690
14691 /* Give up if PT is not in the window. Note that it already has
14692 been checked at the start of try_window_id that PT is not in
14693 front of the window start. */
14694 if (PT >= MATRIX_ROW_END_CHARPOS (row) + delta)
14695 GIVE_UP (13);
14696
14697 /* If window start is unchanged, we can reuse the whole matrix
14698 as is, after adjusting glyph positions. No need to compute
14699 the window end again, since its offset from Z hasn't changed. */
14700 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
14701 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + delta
14702 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + delta_bytes
14703 /* PT must not be in a partially visible line. */
14704 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + delta
14705 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
14706 {
14707 /* Adjust positions in the glyph matrix. */
14708 if (delta || delta_bytes)
14709 {
14710 struct glyph_row *r1
14711 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
14712 increment_matrix_positions (w->current_matrix,
14713 MATRIX_ROW_VPOS (r0, current_matrix),
14714 MATRIX_ROW_VPOS (r1, current_matrix),
14715 delta, delta_bytes);
14716 }
14717
14718 /* Set the cursor. */
14719 row = row_containing_pos (w, PT, r0, NULL, 0);
14720 if (row)
14721 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
14722 else
14723 abort ();
14724 return 1;
14725 }
14726 }
14727
14728 /* Handle the case that changes are all below what is displayed in
14729 the window, and that PT is in the window. This shortcut cannot
14730 be taken if ZV is visible in the window, and text has been added
14731 there that is visible in the window. */
14732 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
14733 /* ZV is not visible in the window, or there are no
14734 changes at ZV, actually. */
14735 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
14736 || first_changed_charpos == last_changed_charpos))
14737 {
14738 struct glyph_row *r0;
14739
14740 /* Give up if PT is not in the window. Note that it already has
14741 been checked at the start of try_window_id that PT is not in
14742 front of the window start. */
14743 if (PT >= MATRIX_ROW_END_CHARPOS (row))
14744 GIVE_UP (14);
14745
14746 /* If window start is unchanged, we can reuse the whole matrix
14747 as is, without changing glyph positions since no text has
14748 been added/removed in front of the window end. */
14749 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
14750 if (TEXT_POS_EQUAL_P (start, r0->start.pos)
14751 /* PT must not be in a partially visible line. */
14752 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
14753 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
14754 {
14755 /* We have to compute the window end anew since text
14756 can have been added/removed after it. */
14757 w->window_end_pos
14758 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
14759 w->window_end_bytepos
14760 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
14761
14762 /* Set the cursor. */
14763 row = row_containing_pos (w, PT, r0, NULL, 0);
14764 if (row)
14765 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
14766 else
14767 abort ();
14768 return 2;
14769 }
14770 }
14771
14772 /* Give up if window start is in the changed area.
14773
14774 The condition used to read
14775
14776 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
14777
14778 but why that was tested escapes me at the moment. */
14779 if (CHARPOS (start) >= first_changed_charpos
14780 && CHARPOS (start) <= last_changed_charpos)
14781 GIVE_UP (15);
14782
14783 /* Check that window start agrees with the start of the first glyph
14784 row in its current matrix. Check this after we know the window
14785 start is not in changed text, otherwise positions would not be
14786 comparable. */
14787 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
14788 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
14789 GIVE_UP (16);
14790
14791 /* Give up if the window ends in strings. Overlay strings
14792 at the end are difficult to handle, so don't try. */
14793 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
14794 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
14795 GIVE_UP (20);
14796
14797 /* Compute the position at which we have to start displaying new
14798 lines. Some of the lines at the top of the window might be
14799 reusable because they are not displaying changed text. Find the
14800 last row in W's current matrix not affected by changes at the
14801 start of current_buffer. Value is null if changes start in the
14802 first line of window. */
14803 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
14804 if (last_unchanged_at_beg_row)
14805 {
14806 /* Avoid starting to display in the moddle of a character, a TAB
14807 for instance. This is easier than to set up the iterator
14808 exactly, and it's not a frequent case, so the additional
14809 effort wouldn't really pay off. */
14810 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
14811 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
14812 && last_unchanged_at_beg_row > w->current_matrix->rows)
14813 --last_unchanged_at_beg_row;
14814
14815 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
14816 GIVE_UP (17);
14817
14818 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
14819 GIVE_UP (18);
14820 start_pos = it.current.pos;
14821
14822 /* Start displaying new lines in the desired matrix at the same
14823 vpos we would use in the current matrix, i.e. below
14824 last_unchanged_at_beg_row. */
14825 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
14826 current_matrix);
14827 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
14828 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
14829
14830 xassert (it.hpos == 0 && it.current_x == 0);
14831 }
14832 else
14833 {
14834 /* There are no reusable lines at the start of the window.
14835 Start displaying in the first text line. */
14836 start_display (&it, w, start);
14837 it.vpos = it.first_vpos;
14838 start_pos = it.current.pos;
14839 }
14840
14841 /* Find the first row that is not affected by changes at the end of
14842 the buffer. Value will be null if there is no unchanged row, in
14843 which case we must redisplay to the end of the window. delta
14844 will be set to the value by which buffer positions beginning with
14845 first_unchanged_at_end_row have to be adjusted due to text
14846 changes. */
14847 first_unchanged_at_end_row
14848 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
14849 IF_DEBUG (debug_delta = delta);
14850 IF_DEBUG (debug_delta_bytes = delta_bytes);
14851
14852 /* Set stop_pos to the buffer position up to which we will have to
14853 display new lines. If first_unchanged_at_end_row != NULL, this
14854 is the buffer position of the start of the line displayed in that
14855 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
14856 that we don't stop at a buffer position. */
14857 stop_pos = 0;
14858 if (first_unchanged_at_end_row)
14859 {
14860 xassert (last_unchanged_at_beg_row == NULL
14861 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
14862
14863 /* If this is a continuation line, move forward to the next one
14864 that isn't. Changes in lines above affect this line.
14865 Caution: this may move first_unchanged_at_end_row to a row
14866 not displaying text. */
14867 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
14868 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
14869 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
14870 < it.last_visible_y))
14871 ++first_unchanged_at_end_row;
14872
14873 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
14874 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
14875 >= it.last_visible_y))
14876 first_unchanged_at_end_row = NULL;
14877 else
14878 {
14879 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
14880 + delta);
14881 first_unchanged_at_end_vpos
14882 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
14883 xassert (stop_pos >= Z - END_UNCHANGED);
14884 }
14885 }
14886 else if (last_unchanged_at_beg_row == NULL)
14887 GIVE_UP (19);
14888
14889
14890 #if GLYPH_DEBUG
14891
14892 /* Either there is no unchanged row at the end, or the one we have
14893 now displays text. This is a necessary condition for the window
14894 end pos calculation at the end of this function. */
14895 xassert (first_unchanged_at_end_row == NULL
14896 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
14897
14898 debug_last_unchanged_at_beg_vpos
14899 = (last_unchanged_at_beg_row
14900 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
14901 : -1);
14902 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
14903
14904 #endif /* GLYPH_DEBUG != 0 */
14905
14906
14907 /* Display new lines. Set last_text_row to the last new line
14908 displayed which has text on it, i.e. might end up as being the
14909 line where the window_end_vpos is. */
14910 w->cursor.vpos = -1;
14911 last_text_row = NULL;
14912 overlay_arrow_seen = 0;
14913 while (it.current_y < it.last_visible_y
14914 && !fonts_changed_p
14915 && (first_unchanged_at_end_row == NULL
14916 || IT_CHARPOS (it) < stop_pos))
14917 {
14918 if (display_line (&it))
14919 last_text_row = it.glyph_row - 1;
14920 }
14921
14922 if (fonts_changed_p)
14923 return -1;
14924
14925
14926 /* Compute differences in buffer positions, y-positions etc. for
14927 lines reused at the bottom of the window. Compute what we can
14928 scroll. */
14929 if (first_unchanged_at_end_row
14930 /* No lines reused because we displayed everything up to the
14931 bottom of the window. */
14932 && it.current_y < it.last_visible_y)
14933 {
14934 dvpos = (it.vpos
14935 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
14936 current_matrix));
14937 dy = it.current_y - first_unchanged_at_end_row->y;
14938 run.current_y = first_unchanged_at_end_row->y;
14939 run.desired_y = run.current_y + dy;
14940 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
14941 }
14942 else
14943 {
14944 delta = dvpos = dy = run.current_y = run.desired_y = run.height = 0;
14945 first_unchanged_at_end_row = NULL;
14946 }
14947 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
14948
14949
14950 /* Find the cursor if not already found. We have to decide whether
14951 PT will appear on this window (it sometimes doesn't, but this is
14952 not a very frequent case.) This decision has to be made before
14953 the current matrix is altered. A value of cursor.vpos < 0 means
14954 that PT is either in one of the lines beginning at
14955 first_unchanged_at_end_row or below the window. Don't care for
14956 lines that might be displayed later at the window end; as
14957 mentioned, this is not a frequent case. */
14958 if (w->cursor.vpos < 0)
14959 {
14960 /* Cursor in unchanged rows at the top? */
14961 if (PT < CHARPOS (start_pos)
14962 && last_unchanged_at_beg_row)
14963 {
14964 row = row_containing_pos (w, PT,
14965 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
14966 last_unchanged_at_beg_row + 1, 0);
14967 if (row)
14968 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
14969 }
14970
14971 /* Start from first_unchanged_at_end_row looking for PT. */
14972 else if (first_unchanged_at_end_row)
14973 {
14974 row = row_containing_pos (w, PT - delta,
14975 first_unchanged_at_end_row, NULL, 0);
14976 if (row)
14977 set_cursor_from_row (w, row, w->current_matrix, delta,
14978 delta_bytes, dy, dvpos);
14979 }
14980
14981 /* Give up if cursor was not found. */
14982 if (w->cursor.vpos < 0)
14983 {
14984 clear_glyph_matrix (w->desired_matrix);
14985 return -1;
14986 }
14987 }
14988
14989 /* Don't let the cursor end in the scroll margins. */
14990 {
14991 int this_scroll_margin, cursor_height;
14992
14993 this_scroll_margin = max (0, scroll_margin);
14994 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
14995 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
14996 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
14997
14998 if ((w->cursor.y < this_scroll_margin
14999 && CHARPOS (start) > BEGV)
15000 /* Old redisplay didn't take scroll margin into account at the bottom,
15001 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
15002 || (w->cursor.y + (make_cursor_line_fully_visible_p
15003 ? cursor_height + this_scroll_margin
15004 : 1)) > it.last_visible_y)
15005 {
15006 w->cursor.vpos = -1;
15007 clear_glyph_matrix (w->desired_matrix);
15008 return -1;
15009 }
15010 }
15011
15012 /* Scroll the display. Do it before changing the current matrix so
15013 that xterm.c doesn't get confused about where the cursor glyph is
15014 found. */
15015 if (dy && run.height)
15016 {
15017 update_begin (f);
15018
15019 if (FRAME_WINDOW_P (f))
15020 {
15021 FRAME_RIF (f)->update_window_begin_hook (w);
15022 FRAME_RIF (f)->clear_window_mouse_face (w);
15023 FRAME_RIF (f)->scroll_run_hook (w, &run);
15024 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
15025 }
15026 else
15027 {
15028 /* Terminal frame. In this case, dvpos gives the number of
15029 lines to scroll by; dvpos < 0 means scroll up. */
15030 int first_unchanged_at_end_vpos
15031 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
15032 int from = WINDOW_TOP_EDGE_LINE (w) + first_unchanged_at_end_vpos;
15033 int end = (WINDOW_TOP_EDGE_LINE (w)
15034 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
15035 + window_internal_height (w));
15036
15037 /* Perform the operation on the screen. */
15038 if (dvpos > 0)
15039 {
15040 /* Scroll last_unchanged_at_beg_row to the end of the
15041 window down dvpos lines. */
15042 set_terminal_window (f, end);
15043
15044 /* On dumb terminals delete dvpos lines at the end
15045 before inserting dvpos empty lines. */
15046 if (!FRAME_SCROLL_REGION_OK (f))
15047 ins_del_lines (f, end - dvpos, -dvpos);
15048
15049 /* Insert dvpos empty lines in front of
15050 last_unchanged_at_beg_row. */
15051 ins_del_lines (f, from, dvpos);
15052 }
15053 else if (dvpos < 0)
15054 {
15055 /* Scroll up last_unchanged_at_beg_vpos to the end of
15056 the window to last_unchanged_at_beg_vpos - |dvpos|. */
15057 set_terminal_window (f, end);
15058
15059 /* Delete dvpos lines in front of
15060 last_unchanged_at_beg_vpos. ins_del_lines will set
15061 the cursor to the given vpos and emit |dvpos| delete
15062 line sequences. */
15063 ins_del_lines (f, from + dvpos, dvpos);
15064
15065 /* On a dumb terminal insert dvpos empty lines at the
15066 end. */
15067 if (!FRAME_SCROLL_REGION_OK (f))
15068 ins_del_lines (f, end + dvpos, -dvpos);
15069 }
15070
15071 set_terminal_window (f, 0);
15072 }
15073
15074 update_end (f);
15075 }
15076
15077 /* Shift reused rows of the current matrix to the right position.
15078 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
15079 text. */
15080 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
15081 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
15082 if (dvpos < 0)
15083 {
15084 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
15085 bottom_vpos, dvpos);
15086 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
15087 bottom_vpos, 0);
15088 }
15089 else if (dvpos > 0)
15090 {
15091 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
15092 bottom_vpos, dvpos);
15093 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
15094 first_unchanged_at_end_vpos + dvpos, 0);
15095 }
15096
15097 /* For frame-based redisplay, make sure that current frame and window
15098 matrix are in sync with respect to glyph memory. */
15099 if (!FRAME_WINDOW_P (f))
15100 sync_frame_with_window_matrix_rows (w);
15101
15102 /* Adjust buffer positions in reused rows. */
15103 if (delta || delta_bytes)
15104 increment_matrix_positions (current_matrix,
15105 first_unchanged_at_end_vpos + dvpos,
15106 bottom_vpos, delta, delta_bytes);
15107
15108 /* Adjust Y positions. */
15109 if (dy)
15110 shift_glyph_matrix (w, current_matrix,
15111 first_unchanged_at_end_vpos + dvpos,
15112 bottom_vpos, dy);
15113
15114 if (first_unchanged_at_end_row)
15115 {
15116 first_unchanged_at_end_row += dvpos;
15117 if (first_unchanged_at_end_row->y >= it.last_visible_y
15118 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
15119 first_unchanged_at_end_row = NULL;
15120 }
15121
15122 /* If scrolling up, there may be some lines to display at the end of
15123 the window. */
15124 last_text_row_at_end = NULL;
15125 if (dy < 0)
15126 {
15127 /* Scrolling up can leave for example a partially visible line
15128 at the end of the window to be redisplayed. */
15129 /* Set last_row to the glyph row in the current matrix where the
15130 window end line is found. It has been moved up or down in
15131 the matrix by dvpos. */
15132 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
15133 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
15134
15135 /* If last_row is the window end line, it should display text. */
15136 xassert (last_row->displays_text_p);
15137
15138 /* If window end line was partially visible before, begin
15139 displaying at that line. Otherwise begin displaying with the
15140 line following it. */
15141 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
15142 {
15143 init_to_row_start (&it, w, last_row);
15144 it.vpos = last_vpos;
15145 it.current_y = last_row->y;
15146 }
15147 else
15148 {
15149 init_to_row_end (&it, w, last_row);
15150 it.vpos = 1 + last_vpos;
15151 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
15152 ++last_row;
15153 }
15154
15155 /* We may start in a continuation line. If so, we have to
15156 get the right continuation_lines_width and current_x. */
15157 it.continuation_lines_width = last_row->continuation_lines_width;
15158 it.hpos = it.current_x = 0;
15159
15160 /* Display the rest of the lines at the window end. */
15161 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
15162 while (it.current_y < it.last_visible_y
15163 && !fonts_changed_p)
15164 {
15165 /* Is it always sure that the display agrees with lines in
15166 the current matrix? I don't think so, so we mark rows
15167 displayed invalid in the current matrix by setting their
15168 enabled_p flag to zero. */
15169 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
15170 if (display_line (&it))
15171 last_text_row_at_end = it.glyph_row - 1;
15172 }
15173 }
15174
15175 /* Update window_end_pos and window_end_vpos. */
15176 if (first_unchanged_at_end_row
15177 && !last_text_row_at_end)
15178 {
15179 /* Window end line if one of the preserved rows from the current
15180 matrix. Set row to the last row displaying text in current
15181 matrix starting at first_unchanged_at_end_row, after
15182 scrolling. */
15183 xassert (first_unchanged_at_end_row->displays_text_p);
15184 row = find_last_row_displaying_text (w->current_matrix, &it,
15185 first_unchanged_at_end_row);
15186 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
15187
15188 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
15189 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
15190 w->window_end_vpos
15191 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
15192 xassert (w->window_end_bytepos >= 0);
15193 IF_DEBUG (debug_method_add (w, "A"));
15194 }
15195 else if (last_text_row_at_end)
15196 {
15197 w->window_end_pos
15198 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
15199 w->window_end_bytepos
15200 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
15201 w->window_end_vpos
15202 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
15203 xassert (w->window_end_bytepos >= 0);
15204 IF_DEBUG (debug_method_add (w, "B"));
15205 }
15206 else if (last_text_row)
15207 {
15208 /* We have displayed either to the end of the window or at the
15209 end of the window, i.e. the last row with text is to be found
15210 in the desired matrix. */
15211 w->window_end_pos
15212 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
15213 w->window_end_bytepos
15214 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
15215 w->window_end_vpos
15216 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
15217 xassert (w->window_end_bytepos >= 0);
15218 }
15219 else if (first_unchanged_at_end_row == NULL
15220 && last_text_row == NULL
15221 && last_text_row_at_end == NULL)
15222 {
15223 /* Displayed to end of window, but no line containing text was
15224 displayed. Lines were deleted at the end of the window. */
15225 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
15226 int vpos = XFASTINT (w->window_end_vpos);
15227 struct glyph_row *current_row = current_matrix->rows + vpos;
15228 struct glyph_row *desired_row = desired_matrix->rows + vpos;
15229
15230 for (row = NULL;
15231 row == NULL && vpos >= first_vpos;
15232 --vpos, --current_row, --desired_row)
15233 {
15234 if (desired_row->enabled_p)
15235 {
15236 if (desired_row->displays_text_p)
15237 row = desired_row;
15238 }
15239 else if (current_row->displays_text_p)
15240 row = current_row;
15241 }
15242
15243 xassert (row != NULL);
15244 w->window_end_vpos = make_number (vpos + 1);
15245 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
15246 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
15247 xassert (w->window_end_bytepos >= 0);
15248 IF_DEBUG (debug_method_add (w, "C"));
15249 }
15250 else
15251 abort ();
15252
15253 #if 0 /* This leads to problems, for instance when the cursor is
15254 at ZV, and the cursor line displays no text. */
15255 /* Disable rows below what's displayed in the window. This makes
15256 debugging easier. */
15257 enable_glyph_matrix_rows (current_matrix,
15258 XFASTINT (w->window_end_vpos) + 1,
15259 bottom_vpos, 0);
15260 #endif
15261
15262 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
15263 debug_end_vpos = XFASTINT (w->window_end_vpos));
15264
15265 /* Record that display has not been completed. */
15266 w->window_end_valid = Qnil;
15267 w->desired_matrix->no_scrolling_p = 1;
15268 return 3;
15269
15270 #undef GIVE_UP
15271 }
15272
15273
15274 \f
15275 /***********************************************************************
15276 More debugging support
15277 ***********************************************************************/
15278
15279 #if GLYPH_DEBUG
15280
15281 void dump_glyph_row P_ ((struct glyph_row *, int, int));
15282 void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
15283 void dump_glyph P_ ((struct glyph_row *, struct glyph *, int));
15284
15285
15286 /* Dump the contents of glyph matrix MATRIX on stderr.
15287
15288 GLYPHS 0 means don't show glyph contents.
15289 GLYPHS 1 means show glyphs in short form
15290 GLYPHS > 1 means show glyphs in long form. */
15291
15292 void
15293 dump_glyph_matrix (matrix, glyphs)
15294 struct glyph_matrix *matrix;
15295 int glyphs;
15296 {
15297 int i;
15298 for (i = 0; i < matrix->nrows; ++i)
15299 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
15300 }
15301
15302
15303 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
15304 the glyph row and area where the glyph comes from. */
15305
15306 void
15307 dump_glyph (row, glyph, area)
15308 struct glyph_row *row;
15309 struct glyph *glyph;
15310 int area;
15311 {
15312 if (glyph->type == CHAR_GLYPH)
15313 {
15314 fprintf (stderr,
15315 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15316 glyph - row->glyphs[TEXT_AREA],
15317 'C',
15318 glyph->charpos,
15319 (BUFFERP (glyph->object)
15320 ? 'B'
15321 : (STRINGP (glyph->object)
15322 ? 'S'
15323 : '-')),
15324 glyph->pixel_width,
15325 glyph->u.ch,
15326 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
15327 ? glyph->u.ch
15328 : '.'),
15329 glyph->face_id,
15330 glyph->left_box_line_p,
15331 glyph->right_box_line_p);
15332 }
15333 else if (glyph->type == STRETCH_GLYPH)
15334 {
15335 fprintf (stderr,
15336 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15337 glyph - row->glyphs[TEXT_AREA],
15338 'S',
15339 glyph->charpos,
15340 (BUFFERP (glyph->object)
15341 ? 'B'
15342 : (STRINGP (glyph->object)
15343 ? 'S'
15344 : '-')),
15345 glyph->pixel_width,
15346 0,
15347 '.',
15348 glyph->face_id,
15349 glyph->left_box_line_p,
15350 glyph->right_box_line_p);
15351 }
15352 else if (glyph->type == IMAGE_GLYPH)
15353 {
15354 fprintf (stderr,
15355 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15356 glyph - row->glyphs[TEXT_AREA],
15357 'I',
15358 glyph->charpos,
15359 (BUFFERP (glyph->object)
15360 ? 'B'
15361 : (STRINGP (glyph->object)
15362 ? 'S'
15363 : '-')),
15364 glyph->pixel_width,
15365 glyph->u.img_id,
15366 '.',
15367 glyph->face_id,
15368 glyph->left_box_line_p,
15369 glyph->right_box_line_p);
15370 }
15371 else if (glyph->type == COMPOSITE_GLYPH)
15372 {
15373 fprintf (stderr,
15374 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15375 glyph - row->glyphs[TEXT_AREA],
15376 '+',
15377 glyph->charpos,
15378 (BUFFERP (glyph->object)
15379 ? 'B'
15380 : (STRINGP (glyph->object)
15381 ? 'S'
15382 : '-')),
15383 glyph->pixel_width,
15384 glyph->u.cmp_id,
15385 '.',
15386 glyph->face_id,
15387 glyph->left_box_line_p,
15388 glyph->right_box_line_p);
15389 }
15390 }
15391
15392
15393 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
15394 GLYPHS 0 means don't show glyph contents.
15395 GLYPHS 1 means show glyphs in short form
15396 GLYPHS > 1 means show glyphs in long form. */
15397
15398 void
15399 dump_glyph_row (row, vpos, glyphs)
15400 struct glyph_row *row;
15401 int vpos, glyphs;
15402 {
15403 if (glyphs != 1)
15404 {
15405 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
15406 fprintf (stderr, "======================================================================\n");
15407
15408 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d\
15409 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
15410 vpos,
15411 MATRIX_ROW_START_CHARPOS (row),
15412 MATRIX_ROW_END_CHARPOS (row),
15413 row->used[TEXT_AREA],
15414 row->contains_overlapping_glyphs_p,
15415 row->enabled_p,
15416 row->truncated_on_left_p,
15417 row->truncated_on_right_p,
15418 row->continued_p,
15419 MATRIX_ROW_CONTINUATION_LINE_P (row),
15420 row->displays_text_p,
15421 row->ends_at_zv_p,
15422 row->fill_line_p,
15423 row->ends_in_middle_of_char_p,
15424 row->starts_in_middle_of_char_p,
15425 row->mouse_face_p,
15426 row->x,
15427 row->y,
15428 row->pixel_width,
15429 row->height,
15430 row->visible_height,
15431 row->ascent,
15432 row->phys_ascent);
15433 fprintf (stderr, "%9d %5d\t%5d\n", row->start.overlay_string_index,
15434 row->end.overlay_string_index,
15435 row->continuation_lines_width);
15436 fprintf (stderr, "%9d %5d\n",
15437 CHARPOS (row->start.string_pos),
15438 CHARPOS (row->end.string_pos));
15439 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
15440 row->end.dpvec_index);
15441 }
15442
15443 if (glyphs > 1)
15444 {
15445 int area;
15446
15447 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
15448 {
15449 struct glyph *glyph = row->glyphs[area];
15450 struct glyph *glyph_end = glyph + row->used[area];
15451
15452 /* Glyph for a line end in text. */
15453 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
15454 ++glyph_end;
15455
15456 if (glyph < glyph_end)
15457 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
15458
15459 for (; glyph < glyph_end; ++glyph)
15460 dump_glyph (row, glyph, area);
15461 }
15462 }
15463 else if (glyphs == 1)
15464 {
15465 int area;
15466
15467 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
15468 {
15469 char *s = (char *) alloca (row->used[area] + 1);
15470 int i;
15471
15472 for (i = 0; i < row->used[area]; ++i)
15473 {
15474 struct glyph *glyph = row->glyphs[area] + i;
15475 if (glyph->type == CHAR_GLYPH
15476 && glyph->u.ch < 0x80
15477 && glyph->u.ch >= ' ')
15478 s[i] = glyph->u.ch;
15479 else
15480 s[i] = '.';
15481 }
15482
15483 s[i] = '\0';
15484 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
15485 }
15486 }
15487 }
15488
15489
15490 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
15491 Sdump_glyph_matrix, 0, 1, "p",
15492 doc: /* Dump the current matrix of the selected window to stderr.
15493 Shows contents of glyph row structures. With non-nil
15494 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
15495 glyphs in short form, otherwise show glyphs in long form. */)
15496 (glyphs)
15497 Lisp_Object glyphs;
15498 {
15499 struct window *w = XWINDOW (selected_window);
15500 struct buffer *buffer = XBUFFER (w->buffer);
15501
15502 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
15503 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
15504 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
15505 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
15506 fprintf (stderr, "=============================================\n");
15507 dump_glyph_matrix (w->current_matrix,
15508 NILP (glyphs) ? 0 : XINT (glyphs));
15509 return Qnil;
15510 }
15511
15512
15513 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
15514 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
15515 ()
15516 {
15517 struct frame *f = XFRAME (selected_frame);
15518 dump_glyph_matrix (f->current_matrix, 1);
15519 return Qnil;
15520 }
15521
15522
15523 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
15524 doc: /* Dump glyph row ROW to stderr.
15525 GLYPH 0 means don't dump glyphs.
15526 GLYPH 1 means dump glyphs in short form.
15527 GLYPH > 1 or omitted means dump glyphs in long form. */)
15528 (row, glyphs)
15529 Lisp_Object row, glyphs;
15530 {
15531 struct glyph_matrix *matrix;
15532 int vpos;
15533
15534 CHECK_NUMBER (row);
15535 matrix = XWINDOW (selected_window)->current_matrix;
15536 vpos = XINT (row);
15537 if (vpos >= 0 && vpos < matrix->nrows)
15538 dump_glyph_row (MATRIX_ROW (matrix, vpos),
15539 vpos,
15540 INTEGERP (glyphs) ? XINT (glyphs) : 2);
15541 return Qnil;
15542 }
15543
15544
15545 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
15546 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
15547 GLYPH 0 means don't dump glyphs.
15548 GLYPH 1 means dump glyphs in short form.
15549 GLYPH > 1 or omitted means dump glyphs in long form. */)
15550 (row, glyphs)
15551 Lisp_Object row, glyphs;
15552 {
15553 struct frame *sf = SELECTED_FRAME ();
15554 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
15555 int vpos;
15556
15557 CHECK_NUMBER (row);
15558 vpos = XINT (row);
15559 if (vpos >= 0 && vpos < m->nrows)
15560 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
15561 INTEGERP (glyphs) ? XINT (glyphs) : 2);
15562 return Qnil;
15563 }
15564
15565
15566 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
15567 doc: /* Toggle tracing of redisplay.
15568 With ARG, turn tracing on if and only if ARG is positive. */)
15569 (arg)
15570 Lisp_Object arg;
15571 {
15572 if (NILP (arg))
15573 trace_redisplay_p = !trace_redisplay_p;
15574 else
15575 {
15576 arg = Fprefix_numeric_value (arg);
15577 trace_redisplay_p = XINT (arg) > 0;
15578 }
15579
15580 return Qnil;
15581 }
15582
15583
15584 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
15585 doc: /* Like `format', but print result to stderr.
15586 usage: (trace-to-stderr STRING &rest OBJECTS) */)
15587 (nargs, args)
15588 int nargs;
15589 Lisp_Object *args;
15590 {
15591 Lisp_Object s = Fformat (nargs, args);
15592 fprintf (stderr, "%s", SDATA (s));
15593 return Qnil;
15594 }
15595
15596 #endif /* GLYPH_DEBUG */
15597
15598
15599 \f
15600 /***********************************************************************
15601 Building Desired Matrix Rows
15602 ***********************************************************************/
15603
15604 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
15605 Used for non-window-redisplay windows, and for windows w/o left fringe. */
15606
15607 static struct glyph_row *
15608 get_overlay_arrow_glyph_row (w, overlay_arrow_string)
15609 struct window *w;
15610 Lisp_Object overlay_arrow_string;
15611 {
15612 struct frame *f = XFRAME (WINDOW_FRAME (w));
15613 struct buffer *buffer = XBUFFER (w->buffer);
15614 struct buffer *old = current_buffer;
15615 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
15616 int arrow_len = SCHARS (overlay_arrow_string);
15617 const unsigned char *arrow_end = arrow_string + arrow_len;
15618 const unsigned char *p;
15619 struct it it;
15620 int multibyte_p;
15621 int n_glyphs_before;
15622
15623 set_buffer_temp (buffer);
15624 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
15625 it.glyph_row->used[TEXT_AREA] = 0;
15626 SET_TEXT_POS (it.position, 0, 0);
15627
15628 multibyte_p = !NILP (buffer->enable_multibyte_characters);
15629 p = arrow_string;
15630 while (p < arrow_end)
15631 {
15632 Lisp_Object face, ilisp;
15633
15634 /* Get the next character. */
15635 if (multibyte_p)
15636 it.c = string_char_and_length (p, arrow_len, &it.len);
15637 else
15638 it.c = *p, it.len = 1;
15639 p += it.len;
15640
15641 /* Get its face. */
15642 ilisp = make_number (p - arrow_string);
15643 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
15644 it.face_id = compute_char_face (f, it.c, face);
15645
15646 /* Compute its width, get its glyphs. */
15647 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
15648 SET_TEXT_POS (it.position, -1, -1);
15649 PRODUCE_GLYPHS (&it);
15650
15651 /* If this character doesn't fit any more in the line, we have
15652 to remove some glyphs. */
15653 if (it.current_x > it.last_visible_x)
15654 {
15655 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
15656 break;
15657 }
15658 }
15659
15660 set_buffer_temp (old);
15661 return it.glyph_row;
15662 }
15663
15664
15665 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
15666 glyphs are only inserted for terminal frames since we can't really
15667 win with truncation glyphs when partially visible glyphs are
15668 involved. Which glyphs to insert is determined by
15669 produce_special_glyphs. */
15670
15671 static void
15672 insert_left_trunc_glyphs (it)
15673 struct it *it;
15674 {
15675 struct it truncate_it;
15676 struct glyph *from, *end, *to, *toend;
15677
15678 xassert (!FRAME_WINDOW_P (it->f));
15679
15680 /* Get the truncation glyphs. */
15681 truncate_it = *it;
15682 truncate_it.current_x = 0;
15683 truncate_it.face_id = DEFAULT_FACE_ID;
15684 truncate_it.glyph_row = &scratch_glyph_row;
15685 truncate_it.glyph_row->used[TEXT_AREA] = 0;
15686 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
15687 truncate_it.object = make_number (0);
15688 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
15689
15690 /* Overwrite glyphs from IT with truncation glyphs. */
15691 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
15692 end = from + truncate_it.glyph_row->used[TEXT_AREA];
15693 to = it->glyph_row->glyphs[TEXT_AREA];
15694 toend = to + it->glyph_row->used[TEXT_AREA];
15695
15696 while (from < end)
15697 *to++ = *from++;
15698
15699 /* There may be padding glyphs left over. Overwrite them too. */
15700 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
15701 {
15702 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
15703 while (from < end)
15704 *to++ = *from++;
15705 }
15706
15707 if (to > toend)
15708 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
15709 }
15710
15711
15712 /* Compute the pixel height and width of IT->glyph_row.
15713
15714 Most of the time, ascent and height of a display line will be equal
15715 to the max_ascent and max_height values of the display iterator
15716 structure. This is not the case if
15717
15718 1. We hit ZV without displaying anything. In this case, max_ascent
15719 and max_height will be zero.
15720
15721 2. We have some glyphs that don't contribute to the line height.
15722 (The glyph row flag contributes_to_line_height_p is for future
15723 pixmap extensions).
15724
15725 The first case is easily covered by using default values because in
15726 these cases, the line height does not really matter, except that it
15727 must not be zero. */
15728
15729 static void
15730 compute_line_metrics (it)
15731 struct it *it;
15732 {
15733 struct glyph_row *row = it->glyph_row;
15734 int area, i;
15735
15736 if (FRAME_WINDOW_P (it->f))
15737 {
15738 int i, min_y, max_y;
15739
15740 /* The line may consist of one space only, that was added to
15741 place the cursor on it. If so, the row's height hasn't been
15742 computed yet. */
15743 if (row->height == 0)
15744 {
15745 if (it->max_ascent + it->max_descent == 0)
15746 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
15747 row->ascent = it->max_ascent;
15748 row->height = it->max_ascent + it->max_descent;
15749 row->phys_ascent = it->max_phys_ascent;
15750 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
15751 row->extra_line_spacing = it->max_extra_line_spacing;
15752 }
15753
15754 /* Compute the width of this line. */
15755 row->pixel_width = row->x;
15756 for (i = 0; i < row->used[TEXT_AREA]; ++i)
15757 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
15758
15759 xassert (row->pixel_width >= 0);
15760 xassert (row->ascent >= 0 && row->height > 0);
15761
15762 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
15763 || MATRIX_ROW_OVERLAPS_PRED_P (row));
15764
15765 /* If first line's physical ascent is larger than its logical
15766 ascent, use the physical ascent, and make the row taller.
15767 This makes accented characters fully visible. */
15768 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
15769 && row->phys_ascent > row->ascent)
15770 {
15771 row->height += row->phys_ascent - row->ascent;
15772 row->ascent = row->phys_ascent;
15773 }
15774
15775 /* Compute how much of the line is visible. */
15776 row->visible_height = row->height;
15777
15778 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
15779 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
15780
15781 if (row->y < min_y)
15782 row->visible_height -= min_y - row->y;
15783 if (row->y + row->height > max_y)
15784 row->visible_height -= row->y + row->height - max_y;
15785 }
15786 else
15787 {
15788 row->pixel_width = row->used[TEXT_AREA];
15789 if (row->continued_p)
15790 row->pixel_width -= it->continuation_pixel_width;
15791 else if (row->truncated_on_right_p)
15792 row->pixel_width -= it->truncation_pixel_width;
15793 row->ascent = row->phys_ascent = 0;
15794 row->height = row->phys_height = row->visible_height = 1;
15795 row->extra_line_spacing = 0;
15796 }
15797
15798 /* Compute a hash code for this row. */
15799 row->hash = 0;
15800 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
15801 for (i = 0; i < row->used[area]; ++i)
15802 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
15803 + row->glyphs[area][i].u.val
15804 + row->glyphs[area][i].face_id
15805 + row->glyphs[area][i].padding_p
15806 + (row->glyphs[area][i].type << 2));
15807
15808 it->max_ascent = it->max_descent = 0;
15809 it->max_phys_ascent = it->max_phys_descent = 0;
15810 }
15811
15812
15813 /* Append one space to the glyph row of iterator IT if doing a
15814 window-based redisplay. The space has the same face as
15815 IT->face_id. Value is non-zero if a space was added.
15816
15817 This function is called to make sure that there is always one glyph
15818 at the end of a glyph row that the cursor can be set on under
15819 window-systems. (If there weren't such a glyph we would not know
15820 how wide and tall a box cursor should be displayed).
15821
15822 At the same time this space let's a nicely handle clearing to the
15823 end of the line if the row ends in italic text. */
15824
15825 static int
15826 append_space_for_newline (it, default_face_p)
15827 struct it *it;
15828 int default_face_p;
15829 {
15830 if (FRAME_WINDOW_P (it->f))
15831 {
15832 int n = it->glyph_row->used[TEXT_AREA];
15833
15834 if (it->glyph_row->glyphs[TEXT_AREA] + n
15835 < it->glyph_row->glyphs[1 + TEXT_AREA])
15836 {
15837 /* Save some values that must not be changed.
15838 Must save IT->c and IT->len because otherwise
15839 ITERATOR_AT_END_P wouldn't work anymore after
15840 append_space_for_newline has been called. */
15841 enum display_element_type saved_what = it->what;
15842 int saved_c = it->c, saved_len = it->len;
15843 int saved_x = it->current_x;
15844 int saved_face_id = it->face_id;
15845 struct text_pos saved_pos;
15846 Lisp_Object saved_object;
15847 struct face *face;
15848
15849 saved_object = it->object;
15850 saved_pos = it->position;
15851
15852 it->what = IT_CHARACTER;
15853 bzero (&it->position, sizeof it->position);
15854 it->object = make_number (0);
15855 it->c = ' ';
15856 it->len = 1;
15857
15858 if (default_face_p)
15859 it->face_id = DEFAULT_FACE_ID;
15860 else if (it->face_before_selective_p)
15861 it->face_id = it->saved_face_id;
15862 face = FACE_FROM_ID (it->f, it->face_id);
15863 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
15864
15865 PRODUCE_GLYPHS (it);
15866
15867 it->override_ascent = -1;
15868 it->constrain_row_ascent_descent_p = 0;
15869 it->current_x = saved_x;
15870 it->object = saved_object;
15871 it->position = saved_pos;
15872 it->what = saved_what;
15873 it->face_id = saved_face_id;
15874 it->len = saved_len;
15875 it->c = saved_c;
15876 return 1;
15877 }
15878 }
15879
15880 return 0;
15881 }
15882
15883
15884 /* Extend the face of the last glyph in the text area of IT->glyph_row
15885 to the end of the display line. Called from display_line.
15886 If the glyph row is empty, add a space glyph to it so that we
15887 know the face to draw. Set the glyph row flag fill_line_p. */
15888
15889 static void
15890 extend_face_to_end_of_line (it)
15891 struct it *it;
15892 {
15893 struct face *face;
15894 struct frame *f = it->f;
15895
15896 /* If line is already filled, do nothing. */
15897 if (it->current_x >= it->last_visible_x)
15898 return;
15899
15900 /* Face extension extends the background and box of IT->face_id
15901 to the end of the line. If the background equals the background
15902 of the frame, we don't have to do anything. */
15903 if (it->face_before_selective_p)
15904 face = FACE_FROM_ID (it->f, it->saved_face_id);
15905 else
15906 face = FACE_FROM_ID (f, it->face_id);
15907
15908 if (FRAME_WINDOW_P (f)
15909 && it->glyph_row->displays_text_p
15910 && face->box == FACE_NO_BOX
15911 && face->background == FRAME_BACKGROUND_PIXEL (f)
15912 && !face->stipple)
15913 return;
15914
15915 /* Set the glyph row flag indicating that the face of the last glyph
15916 in the text area has to be drawn to the end of the text area. */
15917 it->glyph_row->fill_line_p = 1;
15918
15919 /* If current character of IT is not ASCII, make sure we have the
15920 ASCII face. This will be automatically undone the next time
15921 get_next_display_element returns a multibyte character. Note
15922 that the character will always be single byte in unibyte text. */
15923 if (!ASCII_CHAR_P (it->c))
15924 {
15925 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
15926 }
15927
15928 if (FRAME_WINDOW_P (f))
15929 {
15930 /* If the row is empty, add a space with the current face of IT,
15931 so that we know which face to draw. */
15932 if (it->glyph_row->used[TEXT_AREA] == 0)
15933 {
15934 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
15935 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
15936 it->glyph_row->used[TEXT_AREA] = 1;
15937 }
15938 }
15939 else
15940 {
15941 /* Save some values that must not be changed. */
15942 int saved_x = it->current_x;
15943 struct text_pos saved_pos;
15944 Lisp_Object saved_object;
15945 enum display_element_type saved_what = it->what;
15946 int saved_face_id = it->face_id;
15947
15948 saved_object = it->object;
15949 saved_pos = it->position;
15950
15951 it->what = IT_CHARACTER;
15952 bzero (&it->position, sizeof it->position);
15953 it->object = make_number (0);
15954 it->c = ' ';
15955 it->len = 1;
15956 it->face_id = face->id;
15957
15958 PRODUCE_GLYPHS (it);
15959
15960 while (it->current_x <= it->last_visible_x)
15961 PRODUCE_GLYPHS (it);
15962
15963 /* Don't count these blanks really. It would let us insert a left
15964 truncation glyph below and make us set the cursor on them, maybe. */
15965 it->current_x = saved_x;
15966 it->object = saved_object;
15967 it->position = saved_pos;
15968 it->what = saved_what;
15969 it->face_id = saved_face_id;
15970 }
15971 }
15972
15973
15974 /* Value is non-zero if text starting at CHARPOS in current_buffer is
15975 trailing whitespace. */
15976
15977 static int
15978 trailing_whitespace_p (charpos)
15979 int charpos;
15980 {
15981 int bytepos = CHAR_TO_BYTE (charpos);
15982 int c = 0;
15983
15984 while (bytepos < ZV_BYTE
15985 && (c = FETCH_CHAR (bytepos),
15986 c == ' ' || c == '\t'))
15987 ++bytepos;
15988
15989 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
15990 {
15991 if (bytepos != PT_BYTE)
15992 return 1;
15993 }
15994 return 0;
15995 }
15996
15997
15998 /* Highlight trailing whitespace, if any, in ROW. */
15999
16000 void
16001 highlight_trailing_whitespace (f, row)
16002 struct frame *f;
16003 struct glyph_row *row;
16004 {
16005 int used = row->used[TEXT_AREA];
16006
16007 if (used)
16008 {
16009 struct glyph *start = row->glyphs[TEXT_AREA];
16010 struct glyph *glyph = start + used - 1;
16011
16012 /* Skip over glyphs inserted to display the cursor at the
16013 end of a line, for extending the face of the last glyph
16014 to the end of the line on terminals, and for truncation
16015 and continuation glyphs. */
16016 while (glyph >= start
16017 && glyph->type == CHAR_GLYPH
16018 && INTEGERP (glyph->object))
16019 --glyph;
16020
16021 /* If last glyph is a space or stretch, and it's trailing
16022 whitespace, set the face of all trailing whitespace glyphs in
16023 IT->glyph_row to `trailing-whitespace'. */
16024 if (glyph >= start
16025 && BUFFERP (glyph->object)
16026 && (glyph->type == STRETCH_GLYPH
16027 || (glyph->type == CHAR_GLYPH
16028 && glyph->u.ch == ' '))
16029 && trailing_whitespace_p (glyph->charpos))
16030 {
16031 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
16032 if (face_id < 0)
16033 return;
16034
16035 while (glyph >= start
16036 && BUFFERP (glyph->object)
16037 && (glyph->type == STRETCH_GLYPH
16038 || (glyph->type == CHAR_GLYPH
16039 && glyph->u.ch == ' ')))
16040 (glyph--)->face_id = face_id;
16041 }
16042 }
16043 }
16044
16045
16046 /* Value is non-zero if glyph row ROW in window W should be
16047 used to hold the cursor. */
16048
16049 static int
16050 cursor_row_p (w, row)
16051 struct window *w;
16052 struct glyph_row *row;
16053 {
16054 int cursor_row_p = 1;
16055
16056 if (PT == MATRIX_ROW_END_CHARPOS (row))
16057 {
16058 /* Suppose the row ends on a string.
16059 Unless the row is continued, that means it ends on a newline
16060 in the string. If it's anything other than a display string
16061 (e.g. a before-string from an overlay), we don't want the
16062 cursor there. (This heuristic seems to give the optimal
16063 behavior for the various types of multi-line strings.) */
16064 if (CHARPOS (row->end.string_pos) >= 0)
16065 {
16066 if (row->continued_p)
16067 cursor_row_p = 1;
16068 else
16069 {
16070 /* Check for `display' property. */
16071 struct glyph *beg = row->glyphs[TEXT_AREA];
16072 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
16073 struct glyph *glyph;
16074
16075 cursor_row_p = 0;
16076 for (glyph = end; glyph >= beg; --glyph)
16077 if (STRINGP (glyph->object))
16078 {
16079 Lisp_Object prop
16080 = Fget_char_property (make_number (PT),
16081 Qdisplay, Qnil);
16082 cursor_row_p =
16083 (!NILP (prop)
16084 && display_prop_string_p (prop, glyph->object));
16085 break;
16086 }
16087 }
16088 }
16089 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
16090 {
16091 /* If the row ends in middle of a real character,
16092 and the line is continued, we want the cursor here.
16093 That's because MATRIX_ROW_END_CHARPOS would equal
16094 PT if PT is before the character. */
16095 if (!row->ends_in_ellipsis_p)
16096 cursor_row_p = row->continued_p;
16097 else
16098 /* If the row ends in an ellipsis, then
16099 MATRIX_ROW_END_CHARPOS will equal point after the invisible text.
16100 We want that position to be displayed after the ellipsis. */
16101 cursor_row_p = 0;
16102 }
16103 /* If the row ends at ZV, display the cursor at the end of that
16104 row instead of at the start of the row below. */
16105 else if (row->ends_at_zv_p)
16106 cursor_row_p = 1;
16107 else
16108 cursor_row_p = 0;
16109 }
16110
16111 return cursor_row_p;
16112 }
16113
16114
16115 /* Construct the glyph row IT->glyph_row in the desired matrix of
16116 IT->w from text at the current position of IT. See dispextern.h
16117 for an overview of struct it. Value is non-zero if
16118 IT->glyph_row displays text, as opposed to a line displaying ZV
16119 only. */
16120
16121 static int
16122 display_line (it)
16123 struct it *it;
16124 {
16125 struct glyph_row *row = it->glyph_row;
16126 Lisp_Object overlay_arrow_string;
16127
16128 /* We always start displaying at hpos zero even if hscrolled. */
16129 xassert (it->hpos == 0 && it->current_x == 0);
16130
16131 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
16132 >= it->w->desired_matrix->nrows)
16133 {
16134 it->w->nrows_scale_factor++;
16135 fonts_changed_p = 1;
16136 return 0;
16137 }
16138
16139 /* Is IT->w showing the region? */
16140 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
16141
16142 /* Clear the result glyph row and enable it. */
16143 prepare_desired_row (row);
16144
16145 row->y = it->current_y;
16146 row->start = it->start;
16147 row->continuation_lines_width = it->continuation_lines_width;
16148 row->displays_text_p = 1;
16149 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
16150 it->starts_in_middle_of_char_p = 0;
16151
16152 /* Arrange the overlays nicely for our purposes. Usually, we call
16153 display_line on only one line at a time, in which case this
16154 can't really hurt too much, or we call it on lines which appear
16155 one after another in the buffer, in which case all calls to
16156 recenter_overlay_lists but the first will be pretty cheap. */
16157 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
16158
16159 /* Move over display elements that are not visible because we are
16160 hscrolled. This may stop at an x-position < IT->first_visible_x
16161 if the first glyph is partially visible or if we hit a line end. */
16162 if (it->current_x < it->first_visible_x)
16163 {
16164 move_it_in_display_line_to (it, ZV, it->first_visible_x,
16165 MOVE_TO_POS | MOVE_TO_X);
16166 }
16167
16168 /* Get the initial row height. This is either the height of the
16169 text hscrolled, if there is any, or zero. */
16170 row->ascent = it->max_ascent;
16171 row->height = it->max_ascent + it->max_descent;
16172 row->phys_ascent = it->max_phys_ascent;
16173 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
16174 row->extra_line_spacing = it->max_extra_line_spacing;
16175
16176 /* Loop generating characters. The loop is left with IT on the next
16177 character to display. */
16178 while (1)
16179 {
16180 int n_glyphs_before, hpos_before, x_before;
16181 int x, i, nglyphs;
16182 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
16183
16184 /* Retrieve the next thing to display. Value is zero if end of
16185 buffer reached. */
16186 if (!get_next_display_element (it))
16187 {
16188 /* Maybe add a space at the end of this line that is used to
16189 display the cursor there under X. Set the charpos of the
16190 first glyph of blank lines not corresponding to any text
16191 to -1. */
16192 #ifdef HAVE_WINDOW_SYSTEM
16193 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16194 row->exact_window_width_line_p = 1;
16195 else
16196 #endif /* HAVE_WINDOW_SYSTEM */
16197 if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
16198 || row->used[TEXT_AREA] == 0)
16199 {
16200 row->glyphs[TEXT_AREA]->charpos = -1;
16201 row->displays_text_p = 0;
16202
16203 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines)
16204 && (!MINI_WINDOW_P (it->w)
16205 || (minibuf_level && EQ (it->window, minibuf_window))))
16206 row->indicate_empty_line_p = 1;
16207 }
16208
16209 it->continuation_lines_width = 0;
16210 row->ends_at_zv_p = 1;
16211 break;
16212 }
16213
16214 /* Now, get the metrics of what we want to display. This also
16215 generates glyphs in `row' (which is IT->glyph_row). */
16216 n_glyphs_before = row->used[TEXT_AREA];
16217 x = it->current_x;
16218
16219 /* Remember the line height so far in case the next element doesn't
16220 fit on the line. */
16221 if (!it->truncate_lines_p)
16222 {
16223 ascent = it->max_ascent;
16224 descent = it->max_descent;
16225 phys_ascent = it->max_phys_ascent;
16226 phys_descent = it->max_phys_descent;
16227 }
16228
16229 PRODUCE_GLYPHS (it);
16230
16231 /* If this display element was in marginal areas, continue with
16232 the next one. */
16233 if (it->area != TEXT_AREA)
16234 {
16235 row->ascent = max (row->ascent, it->max_ascent);
16236 row->height = max (row->height, it->max_ascent + it->max_descent);
16237 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
16238 row->phys_height = max (row->phys_height,
16239 it->max_phys_ascent + it->max_phys_descent);
16240 row->extra_line_spacing = max (row->extra_line_spacing,
16241 it->max_extra_line_spacing);
16242 set_iterator_to_next (it, 1);
16243 continue;
16244 }
16245
16246 /* Does the display element fit on the line? If we truncate
16247 lines, we should draw past the right edge of the window. If
16248 we don't truncate, we want to stop so that we can display the
16249 continuation glyph before the right margin. If lines are
16250 continued, there are two possible strategies for characters
16251 resulting in more than 1 glyph (e.g. tabs): Display as many
16252 glyphs as possible in this line and leave the rest for the
16253 continuation line, or display the whole element in the next
16254 line. Original redisplay did the former, so we do it also. */
16255 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
16256 hpos_before = it->hpos;
16257 x_before = x;
16258
16259 if (/* Not a newline. */
16260 nglyphs > 0
16261 /* Glyphs produced fit entirely in the line. */
16262 && it->current_x < it->last_visible_x)
16263 {
16264 it->hpos += nglyphs;
16265 row->ascent = max (row->ascent, it->max_ascent);
16266 row->height = max (row->height, it->max_ascent + it->max_descent);
16267 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
16268 row->phys_height = max (row->phys_height,
16269 it->max_phys_ascent + it->max_phys_descent);
16270 row->extra_line_spacing = max (row->extra_line_spacing,
16271 it->max_extra_line_spacing);
16272 if (it->current_x - it->pixel_width < it->first_visible_x)
16273 row->x = x - it->first_visible_x;
16274 }
16275 else
16276 {
16277 int new_x;
16278 struct glyph *glyph;
16279
16280 for (i = 0; i < nglyphs; ++i, x = new_x)
16281 {
16282 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
16283 new_x = x + glyph->pixel_width;
16284
16285 if (/* Lines are continued. */
16286 !it->truncate_lines_p
16287 && (/* Glyph doesn't fit on the line. */
16288 new_x > it->last_visible_x
16289 /* Or it fits exactly on a window system frame. */
16290 || (new_x == it->last_visible_x
16291 && FRAME_WINDOW_P (it->f))))
16292 {
16293 /* End of a continued line. */
16294
16295 if (it->hpos == 0
16296 || (new_x == it->last_visible_x
16297 && FRAME_WINDOW_P (it->f)))
16298 {
16299 /* Current glyph is the only one on the line or
16300 fits exactly on the line. We must continue
16301 the line because we can't draw the cursor
16302 after the glyph. */
16303 row->continued_p = 1;
16304 it->current_x = new_x;
16305 it->continuation_lines_width += new_x;
16306 ++it->hpos;
16307 if (i == nglyphs - 1)
16308 {
16309 set_iterator_to_next (it, 1);
16310 #ifdef HAVE_WINDOW_SYSTEM
16311 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16312 {
16313 if (!get_next_display_element (it))
16314 {
16315 row->exact_window_width_line_p = 1;
16316 it->continuation_lines_width = 0;
16317 row->continued_p = 0;
16318 row->ends_at_zv_p = 1;
16319 }
16320 else if (ITERATOR_AT_END_OF_LINE_P (it))
16321 {
16322 row->continued_p = 0;
16323 row->exact_window_width_line_p = 1;
16324 }
16325 }
16326 #endif /* HAVE_WINDOW_SYSTEM */
16327 }
16328 }
16329 else if (CHAR_GLYPH_PADDING_P (*glyph)
16330 && !FRAME_WINDOW_P (it->f))
16331 {
16332 /* A padding glyph that doesn't fit on this line.
16333 This means the whole character doesn't fit
16334 on the line. */
16335 row->used[TEXT_AREA] = n_glyphs_before;
16336
16337 /* Fill the rest of the row with continuation
16338 glyphs like in 20.x. */
16339 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
16340 < row->glyphs[1 + TEXT_AREA])
16341 produce_special_glyphs (it, IT_CONTINUATION);
16342
16343 row->continued_p = 1;
16344 it->current_x = x_before;
16345 it->continuation_lines_width += x_before;
16346
16347 /* Restore the height to what it was before the
16348 element not fitting on the line. */
16349 it->max_ascent = ascent;
16350 it->max_descent = descent;
16351 it->max_phys_ascent = phys_ascent;
16352 it->max_phys_descent = phys_descent;
16353 }
16354 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
16355 {
16356 /* A TAB that extends past the right edge of the
16357 window. This produces a single glyph on
16358 window system frames. We leave the glyph in
16359 this row and let it fill the row, but don't
16360 consume the TAB. */
16361 it->continuation_lines_width += it->last_visible_x;
16362 row->ends_in_middle_of_char_p = 1;
16363 row->continued_p = 1;
16364 glyph->pixel_width = it->last_visible_x - x;
16365 it->starts_in_middle_of_char_p = 1;
16366 }
16367 else
16368 {
16369 /* Something other than a TAB that draws past
16370 the right edge of the window. Restore
16371 positions to values before the element. */
16372 row->used[TEXT_AREA] = n_glyphs_before + i;
16373
16374 /* Display continuation glyphs. */
16375 if (!FRAME_WINDOW_P (it->f))
16376 produce_special_glyphs (it, IT_CONTINUATION);
16377 row->continued_p = 1;
16378
16379 it->current_x = x_before;
16380 it->continuation_lines_width += x;
16381 extend_face_to_end_of_line (it);
16382
16383 if (nglyphs > 1 && i > 0)
16384 {
16385 row->ends_in_middle_of_char_p = 1;
16386 it->starts_in_middle_of_char_p = 1;
16387 }
16388
16389 /* Restore the height to what it was before the
16390 element not fitting on the line. */
16391 it->max_ascent = ascent;
16392 it->max_descent = descent;
16393 it->max_phys_ascent = phys_ascent;
16394 it->max_phys_descent = phys_descent;
16395 }
16396
16397 break;
16398 }
16399 else if (new_x > it->first_visible_x)
16400 {
16401 /* Increment number of glyphs actually displayed. */
16402 ++it->hpos;
16403
16404 if (x < it->first_visible_x)
16405 /* Glyph is partially visible, i.e. row starts at
16406 negative X position. */
16407 row->x = x - it->first_visible_x;
16408 }
16409 else
16410 {
16411 /* Glyph is completely off the left margin of the
16412 window. This should not happen because of the
16413 move_it_in_display_line at the start of this
16414 function, unless the text display area of the
16415 window is empty. */
16416 xassert (it->first_visible_x <= it->last_visible_x);
16417 }
16418 }
16419
16420 row->ascent = max (row->ascent, it->max_ascent);
16421 row->height = max (row->height, it->max_ascent + it->max_descent);
16422 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
16423 row->phys_height = max (row->phys_height,
16424 it->max_phys_ascent + it->max_phys_descent);
16425 row->extra_line_spacing = max (row->extra_line_spacing,
16426 it->max_extra_line_spacing);
16427
16428 /* End of this display line if row is continued. */
16429 if (row->continued_p || row->ends_at_zv_p)
16430 break;
16431 }
16432
16433 at_end_of_line:
16434 /* Is this a line end? If yes, we're also done, after making
16435 sure that a non-default face is extended up to the right
16436 margin of the window. */
16437 if (ITERATOR_AT_END_OF_LINE_P (it))
16438 {
16439 int used_before = row->used[TEXT_AREA];
16440
16441 row->ends_in_newline_from_string_p = STRINGP (it->object);
16442
16443 #ifdef HAVE_WINDOW_SYSTEM
16444 /* Add a space at the end of the line that is used to
16445 display the cursor there. */
16446 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16447 append_space_for_newline (it, 0);
16448 #endif /* HAVE_WINDOW_SYSTEM */
16449
16450 /* Extend the face to the end of the line. */
16451 extend_face_to_end_of_line (it);
16452
16453 /* Make sure we have the position. */
16454 if (used_before == 0)
16455 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
16456
16457 /* Consume the line end. This skips over invisible lines. */
16458 set_iterator_to_next (it, 1);
16459 it->continuation_lines_width = 0;
16460 break;
16461 }
16462
16463 /* Proceed with next display element. Note that this skips
16464 over lines invisible because of selective display. */
16465 set_iterator_to_next (it, 1);
16466
16467 /* If we truncate lines, we are done when the last displayed
16468 glyphs reach past the right margin of the window. */
16469 if (it->truncate_lines_p
16470 && (FRAME_WINDOW_P (it->f)
16471 ? (it->current_x >= it->last_visible_x)
16472 : (it->current_x > it->last_visible_x)))
16473 {
16474 /* Maybe add truncation glyphs. */
16475 if (!FRAME_WINDOW_P (it->f))
16476 {
16477 int i, n;
16478
16479 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
16480 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
16481 break;
16482
16483 for (n = row->used[TEXT_AREA]; i < n; ++i)
16484 {
16485 row->used[TEXT_AREA] = i;
16486 produce_special_glyphs (it, IT_TRUNCATION);
16487 }
16488 }
16489 #ifdef HAVE_WINDOW_SYSTEM
16490 else
16491 {
16492 /* Don't truncate if we can overflow newline into fringe. */
16493 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16494 {
16495 if (!get_next_display_element (it))
16496 {
16497 it->continuation_lines_width = 0;
16498 row->ends_at_zv_p = 1;
16499 row->exact_window_width_line_p = 1;
16500 break;
16501 }
16502 if (ITERATOR_AT_END_OF_LINE_P (it))
16503 {
16504 row->exact_window_width_line_p = 1;
16505 goto at_end_of_line;
16506 }
16507 }
16508 }
16509 #endif /* HAVE_WINDOW_SYSTEM */
16510
16511 row->truncated_on_right_p = 1;
16512 it->continuation_lines_width = 0;
16513 reseat_at_next_visible_line_start (it, 0);
16514 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
16515 it->hpos = hpos_before;
16516 it->current_x = x_before;
16517 break;
16518 }
16519 }
16520
16521 /* If line is not empty and hscrolled, maybe insert truncation glyphs
16522 at the left window margin. */
16523 if (it->first_visible_x
16524 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
16525 {
16526 if (!FRAME_WINDOW_P (it->f))
16527 insert_left_trunc_glyphs (it);
16528 row->truncated_on_left_p = 1;
16529 }
16530
16531 /* If the start of this line is the overlay arrow-position, then
16532 mark this glyph row as the one containing the overlay arrow.
16533 This is clearly a mess with variable size fonts. It would be
16534 better to let it be displayed like cursors under X. */
16535 if ((row->displays_text_p || !overlay_arrow_seen)
16536 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
16537 !NILP (overlay_arrow_string)))
16538 {
16539 /* Overlay arrow in window redisplay is a fringe bitmap. */
16540 if (STRINGP (overlay_arrow_string))
16541 {
16542 struct glyph_row *arrow_row
16543 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
16544 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
16545 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
16546 struct glyph *p = row->glyphs[TEXT_AREA];
16547 struct glyph *p2, *end;
16548
16549 /* Copy the arrow glyphs. */
16550 while (glyph < arrow_end)
16551 *p++ = *glyph++;
16552
16553 /* Throw away padding glyphs. */
16554 p2 = p;
16555 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
16556 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
16557 ++p2;
16558 if (p2 > p)
16559 {
16560 while (p2 < end)
16561 *p++ = *p2++;
16562 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
16563 }
16564 }
16565 else
16566 {
16567 xassert (INTEGERP (overlay_arrow_string));
16568 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
16569 }
16570 overlay_arrow_seen = 1;
16571 }
16572
16573 /* Compute pixel dimensions of this line. */
16574 compute_line_metrics (it);
16575
16576 /* Remember the position at which this line ends. */
16577 row->end = it->current;
16578
16579 /* Record whether this row ends inside an ellipsis. */
16580 row->ends_in_ellipsis_p
16581 = (it->method == GET_FROM_DISPLAY_VECTOR
16582 && it->ellipsis_p);
16583
16584 /* Save fringe bitmaps in this row. */
16585 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
16586 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
16587 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
16588 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
16589
16590 it->left_user_fringe_bitmap = 0;
16591 it->left_user_fringe_face_id = 0;
16592 it->right_user_fringe_bitmap = 0;
16593 it->right_user_fringe_face_id = 0;
16594
16595 /* Maybe set the cursor. */
16596 if (it->w->cursor.vpos < 0
16597 && PT >= MATRIX_ROW_START_CHARPOS (row)
16598 && PT <= MATRIX_ROW_END_CHARPOS (row)
16599 && cursor_row_p (it->w, row))
16600 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
16601
16602 /* Highlight trailing whitespace. */
16603 if (!NILP (Vshow_trailing_whitespace))
16604 highlight_trailing_whitespace (it->f, it->glyph_row);
16605
16606 /* Prepare for the next line. This line starts horizontally at (X
16607 HPOS) = (0 0). Vertical positions are incremented. As a
16608 convenience for the caller, IT->glyph_row is set to the next
16609 row to be used. */
16610 it->current_x = it->hpos = 0;
16611 it->current_y += row->height;
16612 ++it->vpos;
16613 ++it->glyph_row;
16614 it->start = it->current;
16615 return row->displays_text_p;
16616 }
16617
16618
16619 \f
16620 /***********************************************************************
16621 Menu Bar
16622 ***********************************************************************/
16623
16624 /* Redisplay the menu bar in the frame for window W.
16625
16626 The menu bar of X frames that don't have X toolkit support is
16627 displayed in a special window W->frame->menu_bar_window.
16628
16629 The menu bar of terminal frames is treated specially as far as
16630 glyph matrices are concerned. Menu bar lines are not part of
16631 windows, so the update is done directly on the frame matrix rows
16632 for the menu bar. */
16633
16634 static void
16635 display_menu_bar (w)
16636 struct window *w;
16637 {
16638 struct frame *f = XFRAME (WINDOW_FRAME (w));
16639 struct it it;
16640 Lisp_Object items;
16641 int i;
16642
16643 /* Don't do all this for graphical frames. */
16644 #ifdef HAVE_NTGUI
16645 if (FRAME_W32_P (f))
16646 return;
16647 #endif
16648 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
16649 if (FRAME_X_P (f))
16650 return;
16651 #endif
16652 #ifdef MAC_OS
16653 if (FRAME_MAC_P (f))
16654 return;
16655 #endif
16656
16657 #ifdef USE_X_TOOLKIT
16658 xassert (!FRAME_WINDOW_P (f));
16659 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
16660 it.first_visible_x = 0;
16661 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
16662 #else /* not USE_X_TOOLKIT */
16663 if (FRAME_WINDOW_P (f))
16664 {
16665 /* Menu bar lines are displayed in the desired matrix of the
16666 dummy window menu_bar_window. */
16667 struct window *menu_w;
16668 xassert (WINDOWP (f->menu_bar_window));
16669 menu_w = XWINDOW (f->menu_bar_window);
16670 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
16671 MENU_FACE_ID);
16672 it.first_visible_x = 0;
16673 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
16674 }
16675 else
16676 {
16677 /* This is a TTY frame, i.e. character hpos/vpos are used as
16678 pixel x/y. */
16679 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
16680 MENU_FACE_ID);
16681 it.first_visible_x = 0;
16682 it.last_visible_x = FRAME_COLS (f);
16683 }
16684 #endif /* not USE_X_TOOLKIT */
16685
16686 if (! mode_line_inverse_video)
16687 /* Force the menu-bar to be displayed in the default face. */
16688 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
16689
16690 /* Clear all rows of the menu bar. */
16691 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
16692 {
16693 struct glyph_row *row = it.glyph_row + i;
16694 clear_glyph_row (row);
16695 row->enabled_p = 1;
16696 row->full_width_p = 1;
16697 }
16698
16699 /* Display all items of the menu bar. */
16700 items = FRAME_MENU_BAR_ITEMS (it.f);
16701 for (i = 0; i < XVECTOR (items)->size; i += 4)
16702 {
16703 Lisp_Object string;
16704
16705 /* Stop at nil string. */
16706 string = AREF (items, i + 1);
16707 if (NILP (string))
16708 break;
16709
16710 /* Remember where item was displayed. */
16711 AREF (items, i + 3) = make_number (it.hpos);
16712
16713 /* Display the item, pad with one space. */
16714 if (it.current_x < it.last_visible_x)
16715 display_string (NULL, string, Qnil, 0, 0, &it,
16716 SCHARS (string) + 1, 0, 0, -1);
16717 }
16718
16719 /* Fill out the line with spaces. */
16720 if (it.current_x < it.last_visible_x)
16721 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
16722
16723 /* Compute the total height of the lines. */
16724 compute_line_metrics (&it);
16725 }
16726
16727
16728 \f
16729 /***********************************************************************
16730 Mode Line
16731 ***********************************************************************/
16732
16733 /* Redisplay mode lines in the window tree whose root is WINDOW. If
16734 FORCE is non-zero, redisplay mode lines unconditionally.
16735 Otherwise, redisplay only mode lines that are garbaged. Value is
16736 the number of windows whose mode lines were redisplayed. */
16737
16738 static int
16739 redisplay_mode_lines (window, force)
16740 Lisp_Object window;
16741 int force;
16742 {
16743 int nwindows = 0;
16744
16745 while (!NILP (window))
16746 {
16747 struct window *w = XWINDOW (window);
16748
16749 if (WINDOWP (w->hchild))
16750 nwindows += redisplay_mode_lines (w->hchild, force);
16751 else if (WINDOWP (w->vchild))
16752 nwindows += redisplay_mode_lines (w->vchild, force);
16753 else if (force
16754 || FRAME_GARBAGED_P (XFRAME (w->frame))
16755 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
16756 {
16757 struct text_pos lpoint;
16758 struct buffer *old = current_buffer;
16759
16760 /* Set the window's buffer for the mode line display. */
16761 SET_TEXT_POS (lpoint, PT, PT_BYTE);
16762 set_buffer_internal_1 (XBUFFER (w->buffer));
16763
16764 /* Point refers normally to the selected window. For any
16765 other window, set up appropriate value. */
16766 if (!EQ (window, selected_window))
16767 {
16768 struct text_pos pt;
16769
16770 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
16771 if (CHARPOS (pt) < BEGV)
16772 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
16773 else if (CHARPOS (pt) > (ZV - 1))
16774 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
16775 else
16776 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
16777 }
16778
16779 /* Display mode lines. */
16780 clear_glyph_matrix (w->desired_matrix);
16781 if (display_mode_lines (w))
16782 {
16783 ++nwindows;
16784 w->must_be_updated_p = 1;
16785 }
16786
16787 /* Restore old settings. */
16788 set_buffer_internal_1 (old);
16789 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
16790 }
16791
16792 window = w->next;
16793 }
16794
16795 return nwindows;
16796 }
16797
16798
16799 /* Display the mode and/or top line of window W. Value is the number
16800 of mode lines displayed. */
16801
16802 static int
16803 display_mode_lines (w)
16804 struct window *w;
16805 {
16806 Lisp_Object old_selected_window, old_selected_frame;
16807 int n = 0;
16808
16809 old_selected_frame = selected_frame;
16810 selected_frame = w->frame;
16811 old_selected_window = selected_window;
16812 XSETWINDOW (selected_window, w);
16813
16814 /* These will be set while the mode line specs are processed. */
16815 line_number_displayed = 0;
16816 w->column_number_displayed = Qnil;
16817
16818 if (WINDOW_WANTS_MODELINE_P (w))
16819 {
16820 struct window *sel_w = XWINDOW (old_selected_window);
16821
16822 /* Select mode line face based on the real selected window. */
16823 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
16824 current_buffer->mode_line_format);
16825 ++n;
16826 }
16827
16828 if (WINDOW_WANTS_HEADER_LINE_P (w))
16829 {
16830 display_mode_line (w, HEADER_LINE_FACE_ID,
16831 current_buffer->header_line_format);
16832 ++n;
16833 }
16834
16835 selected_frame = old_selected_frame;
16836 selected_window = old_selected_window;
16837 return n;
16838 }
16839
16840
16841 /* Display mode or top line of window W. FACE_ID specifies which line
16842 to display; it is either MODE_LINE_FACE_ID or HEADER_LINE_FACE_ID.
16843 FORMAT is the mode line format to display. Value is the pixel
16844 height of the mode line displayed. */
16845
16846 static int
16847 display_mode_line (w, face_id, format)
16848 struct window *w;
16849 enum face_id face_id;
16850 Lisp_Object format;
16851 {
16852 struct it it;
16853 struct face *face;
16854 int count = SPECPDL_INDEX ();
16855
16856 init_iterator (&it, w, -1, -1, NULL, face_id);
16857 /* Don't extend on a previously drawn mode-line.
16858 This may happen if called from pos_visible_p. */
16859 it.glyph_row->enabled_p = 0;
16860 prepare_desired_row (it.glyph_row);
16861
16862 it.glyph_row->mode_line_p = 1;
16863
16864 if (! mode_line_inverse_video)
16865 /* Force the mode-line to be displayed in the default face. */
16866 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
16867
16868 record_unwind_protect (unwind_format_mode_line,
16869 format_mode_line_unwind_data (NULL, 0));
16870
16871 mode_line_target = MODE_LINE_DISPLAY;
16872
16873 /* Temporarily make frame's keyboard the current kboard so that
16874 kboard-local variables in the mode_line_format will get the right
16875 values. */
16876 push_kboard (FRAME_KBOARD (it.f));
16877 record_unwind_save_match_data ();
16878 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
16879 pop_kboard ();
16880
16881 unbind_to (count, Qnil);
16882
16883 /* Fill up with spaces. */
16884 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
16885
16886 compute_line_metrics (&it);
16887 it.glyph_row->full_width_p = 1;
16888 it.glyph_row->continued_p = 0;
16889 it.glyph_row->truncated_on_left_p = 0;
16890 it.glyph_row->truncated_on_right_p = 0;
16891
16892 /* Make a 3D mode-line have a shadow at its right end. */
16893 face = FACE_FROM_ID (it.f, face_id);
16894 extend_face_to_end_of_line (&it);
16895 if (face->box != FACE_NO_BOX)
16896 {
16897 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
16898 + it.glyph_row->used[TEXT_AREA] - 1);
16899 last->right_box_line_p = 1;
16900 }
16901
16902 return it.glyph_row->height;
16903 }
16904
16905 /* Move element ELT in LIST to the front of LIST.
16906 Return the updated list. */
16907
16908 static Lisp_Object
16909 move_elt_to_front (elt, list)
16910 Lisp_Object elt, list;
16911 {
16912 register Lisp_Object tail, prev;
16913 register Lisp_Object tem;
16914
16915 tail = list;
16916 prev = Qnil;
16917 while (CONSP (tail))
16918 {
16919 tem = XCAR (tail);
16920
16921 if (EQ (elt, tem))
16922 {
16923 /* Splice out the link TAIL. */
16924 if (NILP (prev))
16925 list = XCDR (tail);
16926 else
16927 Fsetcdr (prev, XCDR (tail));
16928
16929 /* Now make it the first. */
16930 Fsetcdr (tail, list);
16931 return tail;
16932 }
16933 else
16934 prev = tail;
16935 tail = XCDR (tail);
16936 QUIT;
16937 }
16938
16939 /* Not found--return unchanged LIST. */
16940 return list;
16941 }
16942
16943 /* Contribute ELT to the mode line for window IT->w. How it
16944 translates into text depends on its data type.
16945
16946 IT describes the display environment in which we display, as usual.
16947
16948 DEPTH is the depth in recursion. It is used to prevent
16949 infinite recursion here.
16950
16951 FIELD_WIDTH is the number of characters the display of ELT should
16952 occupy in the mode line, and PRECISION is the maximum number of
16953 characters to display from ELT's representation. See
16954 display_string for details.
16955
16956 Returns the hpos of the end of the text generated by ELT.
16957
16958 PROPS is a property list to add to any string we encounter.
16959
16960 If RISKY is nonzero, remove (disregard) any properties in any string
16961 we encounter, and ignore :eval and :propertize.
16962
16963 The global variable `mode_line_target' determines whether the
16964 output is passed to `store_mode_line_noprop',
16965 `store_mode_line_string', or `display_string'. */
16966
16967 static int
16968 display_mode_element (it, depth, field_width, precision, elt, props, risky)
16969 struct it *it;
16970 int depth;
16971 int field_width, precision;
16972 Lisp_Object elt, props;
16973 int risky;
16974 {
16975 int n = 0, field, prec;
16976 int literal = 0;
16977
16978 tail_recurse:
16979 if (depth > 100)
16980 elt = build_string ("*too-deep*");
16981
16982 depth++;
16983
16984 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
16985 {
16986 case Lisp_String:
16987 {
16988 /* A string: output it and check for %-constructs within it. */
16989 unsigned char c;
16990 int offset = 0;
16991
16992 if (SCHARS (elt) > 0
16993 && (!NILP (props) || risky))
16994 {
16995 Lisp_Object oprops, aelt;
16996 oprops = Ftext_properties_at (make_number (0), elt);
16997
16998 /* If the starting string's properties are not what
16999 we want, translate the string. Also, if the string
17000 is risky, do that anyway. */
17001
17002 if (NILP (Fequal (props, oprops)) || risky)
17003 {
17004 /* If the starting string has properties,
17005 merge the specified ones onto the existing ones. */
17006 if (! NILP (oprops) && !risky)
17007 {
17008 Lisp_Object tem;
17009
17010 oprops = Fcopy_sequence (oprops);
17011 tem = props;
17012 while (CONSP (tem))
17013 {
17014 oprops = Fplist_put (oprops, XCAR (tem),
17015 XCAR (XCDR (tem)));
17016 tem = XCDR (XCDR (tem));
17017 }
17018 props = oprops;
17019 }
17020
17021 aelt = Fassoc (elt, mode_line_proptrans_alist);
17022 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
17023 {
17024 /* AELT is what we want. Move it to the front
17025 without consing. */
17026 elt = XCAR (aelt);
17027 mode_line_proptrans_alist
17028 = move_elt_to_front (aelt, mode_line_proptrans_alist);
17029 }
17030 else
17031 {
17032 Lisp_Object tem;
17033
17034 /* If AELT has the wrong props, it is useless.
17035 so get rid of it. */
17036 if (! NILP (aelt))
17037 mode_line_proptrans_alist
17038 = Fdelq (aelt, mode_line_proptrans_alist);
17039
17040 elt = Fcopy_sequence (elt);
17041 Fset_text_properties (make_number (0), Flength (elt),
17042 props, elt);
17043 /* Add this item to mode_line_proptrans_alist. */
17044 mode_line_proptrans_alist
17045 = Fcons (Fcons (elt, props),
17046 mode_line_proptrans_alist);
17047 /* Truncate mode_line_proptrans_alist
17048 to at most 50 elements. */
17049 tem = Fnthcdr (make_number (50),
17050 mode_line_proptrans_alist);
17051 if (! NILP (tem))
17052 XSETCDR (tem, Qnil);
17053 }
17054 }
17055 }
17056
17057 offset = 0;
17058
17059 if (literal)
17060 {
17061 prec = precision - n;
17062 switch (mode_line_target)
17063 {
17064 case MODE_LINE_NOPROP:
17065 case MODE_LINE_TITLE:
17066 n += store_mode_line_noprop (SDATA (elt), -1, prec);
17067 break;
17068 case MODE_LINE_STRING:
17069 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
17070 break;
17071 case MODE_LINE_DISPLAY:
17072 n += display_string (NULL, elt, Qnil, 0, 0, it,
17073 0, prec, 0, STRING_MULTIBYTE (elt));
17074 break;
17075 }
17076
17077 break;
17078 }
17079
17080 /* Handle the non-literal case. */
17081
17082 while ((precision <= 0 || n < precision)
17083 && SREF (elt, offset) != 0
17084 && (mode_line_target != MODE_LINE_DISPLAY
17085 || it->current_x < it->last_visible_x))
17086 {
17087 int last_offset = offset;
17088
17089 /* Advance to end of string or next format specifier. */
17090 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
17091 ;
17092
17093 if (offset - 1 != last_offset)
17094 {
17095 int nchars, nbytes;
17096
17097 /* Output to end of string or up to '%'. Field width
17098 is length of string. Don't output more than
17099 PRECISION allows us. */
17100 offset--;
17101
17102 prec = c_string_width (SDATA (elt) + last_offset,
17103 offset - last_offset, precision - n,
17104 &nchars, &nbytes);
17105
17106 switch (mode_line_target)
17107 {
17108 case MODE_LINE_NOPROP:
17109 case MODE_LINE_TITLE:
17110 n += store_mode_line_noprop (SDATA (elt) + last_offset, 0, prec);
17111 break;
17112 case MODE_LINE_STRING:
17113 {
17114 int bytepos = last_offset;
17115 int charpos = string_byte_to_char (elt, bytepos);
17116 int endpos = (precision <= 0
17117 ? string_byte_to_char (elt, offset)
17118 : charpos + nchars);
17119
17120 n += store_mode_line_string (NULL,
17121 Fsubstring (elt, make_number (charpos),
17122 make_number (endpos)),
17123 0, 0, 0, Qnil);
17124 }
17125 break;
17126 case MODE_LINE_DISPLAY:
17127 {
17128 int bytepos = last_offset;
17129 int charpos = string_byte_to_char (elt, bytepos);
17130
17131 if (precision <= 0)
17132 nchars = string_byte_to_char (elt, offset) - charpos;
17133 n += display_string (NULL, elt, Qnil, 0, charpos,
17134 it, 0, nchars, 0,
17135 STRING_MULTIBYTE (elt));
17136 }
17137 break;
17138 }
17139 }
17140 else /* c == '%' */
17141 {
17142 int percent_position = offset;
17143
17144 /* Get the specified minimum width. Zero means
17145 don't pad. */
17146 field = 0;
17147 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
17148 field = field * 10 + c - '0';
17149
17150 /* Don't pad beyond the total padding allowed. */
17151 if (field_width - n > 0 && field > field_width - n)
17152 field = field_width - n;
17153
17154 /* Note that either PRECISION <= 0 or N < PRECISION. */
17155 prec = precision - n;
17156
17157 if (c == 'M')
17158 n += display_mode_element (it, depth, field, prec,
17159 Vglobal_mode_string, props,
17160 risky);
17161 else if (c != 0)
17162 {
17163 int multibyte;
17164 int bytepos, charpos;
17165 unsigned char *spec;
17166
17167 bytepos = percent_position;
17168 charpos = (STRING_MULTIBYTE (elt)
17169 ? string_byte_to_char (elt, bytepos)
17170 : bytepos);
17171
17172 spec
17173 = decode_mode_spec (it->w, c, field, prec, &multibyte);
17174
17175 switch (mode_line_target)
17176 {
17177 case MODE_LINE_NOPROP:
17178 case MODE_LINE_TITLE:
17179 n += store_mode_line_noprop (spec, field, prec);
17180 break;
17181 case MODE_LINE_STRING:
17182 {
17183 int len = strlen (spec);
17184 Lisp_Object tem = make_string (spec, len);
17185 props = Ftext_properties_at (make_number (charpos), elt);
17186 /* Should only keep face property in props */
17187 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
17188 }
17189 break;
17190 case MODE_LINE_DISPLAY:
17191 {
17192 int nglyphs_before, nwritten;
17193
17194 nglyphs_before = it->glyph_row->used[TEXT_AREA];
17195 nwritten = display_string (spec, Qnil, elt,
17196 charpos, 0, it,
17197 field, prec, 0,
17198 multibyte);
17199
17200 /* Assign to the glyphs written above the
17201 string where the `%x' came from, position
17202 of the `%'. */
17203 if (nwritten > 0)
17204 {
17205 struct glyph *glyph
17206 = (it->glyph_row->glyphs[TEXT_AREA]
17207 + nglyphs_before);
17208 int i;
17209
17210 for (i = 0; i < nwritten; ++i)
17211 {
17212 glyph[i].object = elt;
17213 glyph[i].charpos = charpos;
17214 }
17215
17216 n += nwritten;
17217 }
17218 }
17219 break;
17220 }
17221 }
17222 else /* c == 0 */
17223 break;
17224 }
17225 }
17226 }
17227 break;
17228
17229 case Lisp_Symbol:
17230 /* A symbol: process the value of the symbol recursively
17231 as if it appeared here directly. Avoid error if symbol void.
17232 Special case: if value of symbol is a string, output the string
17233 literally. */
17234 {
17235 register Lisp_Object tem;
17236
17237 /* If the variable is not marked as risky to set
17238 then its contents are risky to use. */
17239 if (NILP (Fget (elt, Qrisky_local_variable)))
17240 risky = 1;
17241
17242 tem = Fboundp (elt);
17243 if (!NILP (tem))
17244 {
17245 tem = Fsymbol_value (elt);
17246 /* If value is a string, output that string literally:
17247 don't check for % within it. */
17248 if (STRINGP (tem))
17249 literal = 1;
17250
17251 if (!EQ (tem, elt))
17252 {
17253 /* Give up right away for nil or t. */
17254 elt = tem;
17255 goto tail_recurse;
17256 }
17257 }
17258 }
17259 break;
17260
17261 case Lisp_Cons:
17262 {
17263 register Lisp_Object car, tem;
17264
17265 /* A cons cell: five distinct cases.
17266 If first element is :eval or :propertize, do something special.
17267 If first element is a string or a cons, process all the elements
17268 and effectively concatenate them.
17269 If first element is a negative number, truncate displaying cdr to
17270 at most that many characters. If positive, pad (with spaces)
17271 to at least that many characters.
17272 If first element is a symbol, process the cadr or caddr recursively
17273 according to whether the symbol's value is non-nil or nil. */
17274 car = XCAR (elt);
17275 if (EQ (car, QCeval))
17276 {
17277 /* An element of the form (:eval FORM) means evaluate FORM
17278 and use the result as mode line elements. */
17279
17280 if (risky)
17281 break;
17282
17283 if (CONSP (XCDR (elt)))
17284 {
17285 Lisp_Object spec;
17286 spec = safe_eval (XCAR (XCDR (elt)));
17287 n += display_mode_element (it, depth, field_width - n,
17288 precision - n, spec, props,
17289 risky);
17290 }
17291 }
17292 else if (EQ (car, QCpropertize))
17293 {
17294 /* An element of the form (:propertize ELT PROPS...)
17295 means display ELT but applying properties PROPS. */
17296
17297 if (risky)
17298 break;
17299
17300 if (CONSP (XCDR (elt)))
17301 n += display_mode_element (it, depth, field_width - n,
17302 precision - n, XCAR (XCDR (elt)),
17303 XCDR (XCDR (elt)), risky);
17304 }
17305 else if (SYMBOLP (car))
17306 {
17307 tem = Fboundp (car);
17308 elt = XCDR (elt);
17309 if (!CONSP (elt))
17310 goto invalid;
17311 /* elt is now the cdr, and we know it is a cons cell.
17312 Use its car if CAR has a non-nil value. */
17313 if (!NILP (tem))
17314 {
17315 tem = Fsymbol_value (car);
17316 if (!NILP (tem))
17317 {
17318 elt = XCAR (elt);
17319 goto tail_recurse;
17320 }
17321 }
17322 /* Symbol's value is nil (or symbol is unbound)
17323 Get the cddr of the original list
17324 and if possible find the caddr and use that. */
17325 elt = XCDR (elt);
17326 if (NILP (elt))
17327 break;
17328 else if (!CONSP (elt))
17329 goto invalid;
17330 elt = XCAR (elt);
17331 goto tail_recurse;
17332 }
17333 else if (INTEGERP (car))
17334 {
17335 register int lim = XINT (car);
17336 elt = XCDR (elt);
17337 if (lim < 0)
17338 {
17339 /* Negative int means reduce maximum width. */
17340 if (precision <= 0)
17341 precision = -lim;
17342 else
17343 precision = min (precision, -lim);
17344 }
17345 else if (lim > 0)
17346 {
17347 /* Padding specified. Don't let it be more than
17348 current maximum. */
17349 if (precision > 0)
17350 lim = min (precision, lim);
17351
17352 /* If that's more padding than already wanted, queue it.
17353 But don't reduce padding already specified even if
17354 that is beyond the current truncation point. */
17355 field_width = max (lim, field_width);
17356 }
17357 goto tail_recurse;
17358 }
17359 else if (STRINGP (car) || CONSP (car))
17360 {
17361 register int limit = 50;
17362 /* Limit is to protect against circular lists. */
17363 while (CONSP (elt)
17364 && --limit > 0
17365 && (precision <= 0 || n < precision))
17366 {
17367 n += display_mode_element (it, depth,
17368 /* Do padding only after the last
17369 element in the list. */
17370 (! CONSP (XCDR (elt))
17371 ? field_width - n
17372 : 0),
17373 precision - n, XCAR (elt),
17374 props, risky);
17375 elt = XCDR (elt);
17376 }
17377 }
17378 }
17379 break;
17380
17381 default:
17382 invalid:
17383 elt = build_string ("*invalid*");
17384 goto tail_recurse;
17385 }
17386
17387 /* Pad to FIELD_WIDTH. */
17388 if (field_width > 0 && n < field_width)
17389 {
17390 switch (mode_line_target)
17391 {
17392 case MODE_LINE_NOPROP:
17393 case MODE_LINE_TITLE:
17394 n += store_mode_line_noprop ("", field_width - n, 0);
17395 break;
17396 case MODE_LINE_STRING:
17397 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
17398 break;
17399 case MODE_LINE_DISPLAY:
17400 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
17401 0, 0, 0);
17402 break;
17403 }
17404 }
17405
17406 return n;
17407 }
17408
17409 /* Store a mode-line string element in mode_line_string_list.
17410
17411 If STRING is non-null, display that C string. Otherwise, the Lisp
17412 string LISP_STRING is displayed.
17413
17414 FIELD_WIDTH is the minimum number of output glyphs to produce.
17415 If STRING has fewer characters than FIELD_WIDTH, pad to the right
17416 with spaces. FIELD_WIDTH <= 0 means don't pad.
17417
17418 PRECISION is the maximum number of characters to output from
17419 STRING. PRECISION <= 0 means don't truncate the string.
17420
17421 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
17422 properties to the string.
17423
17424 PROPS are the properties to add to the string.
17425 The mode_line_string_face face property is always added to the string.
17426 */
17427
17428 static int
17429 store_mode_line_string (string, lisp_string, copy_string, field_width, precision, props)
17430 char *string;
17431 Lisp_Object lisp_string;
17432 int copy_string;
17433 int field_width;
17434 int precision;
17435 Lisp_Object props;
17436 {
17437 int len;
17438 int n = 0;
17439
17440 if (string != NULL)
17441 {
17442 len = strlen (string);
17443 if (precision > 0 && len > precision)
17444 len = precision;
17445 lisp_string = make_string (string, len);
17446 if (NILP (props))
17447 props = mode_line_string_face_prop;
17448 else if (!NILP (mode_line_string_face))
17449 {
17450 Lisp_Object face = Fplist_get (props, Qface);
17451 props = Fcopy_sequence (props);
17452 if (NILP (face))
17453 face = mode_line_string_face;
17454 else
17455 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
17456 props = Fplist_put (props, Qface, face);
17457 }
17458 Fadd_text_properties (make_number (0), make_number (len),
17459 props, lisp_string);
17460 }
17461 else
17462 {
17463 len = XFASTINT (Flength (lisp_string));
17464 if (precision > 0 && len > precision)
17465 {
17466 len = precision;
17467 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
17468 precision = -1;
17469 }
17470 if (!NILP (mode_line_string_face))
17471 {
17472 Lisp_Object face;
17473 if (NILP (props))
17474 props = Ftext_properties_at (make_number (0), lisp_string);
17475 face = Fplist_get (props, Qface);
17476 if (NILP (face))
17477 face = mode_line_string_face;
17478 else
17479 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
17480 props = Fcons (Qface, Fcons (face, Qnil));
17481 if (copy_string)
17482 lisp_string = Fcopy_sequence (lisp_string);
17483 }
17484 if (!NILP (props))
17485 Fadd_text_properties (make_number (0), make_number (len),
17486 props, lisp_string);
17487 }
17488
17489 if (len > 0)
17490 {
17491 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
17492 n += len;
17493 }
17494
17495 if (field_width > len)
17496 {
17497 field_width -= len;
17498 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
17499 if (!NILP (props))
17500 Fadd_text_properties (make_number (0), make_number (field_width),
17501 props, lisp_string);
17502 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
17503 n += field_width;
17504 }
17505
17506 return n;
17507 }
17508
17509
17510 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
17511 1, 4, 0,
17512 doc: /* Format a string out of a mode line format specification.
17513 First arg FORMAT specifies the mode line format (see `mode-line-format'
17514 for details) to use.
17515
17516 Optional second arg FACE specifies the face property to put
17517 on all characters for which no face is specified.
17518 The value t means whatever face the window's mode line currently uses
17519 \(either `mode-line' or `mode-line-inactive', depending).
17520 A value of nil means the default is no face property.
17521 If FACE is an integer, the value string has no text properties.
17522
17523 Optional third and fourth args WINDOW and BUFFER specify the window
17524 and buffer to use as the context for the formatting (defaults
17525 are the selected window and the window's buffer). */)
17526 (format, face, window, buffer)
17527 Lisp_Object format, face, window, buffer;
17528 {
17529 struct it it;
17530 int len;
17531 struct window *w;
17532 struct buffer *old_buffer = NULL;
17533 int face_id = -1;
17534 int no_props = INTEGERP (face);
17535 int count = SPECPDL_INDEX ();
17536 Lisp_Object str;
17537 int string_start = 0;
17538
17539 if (NILP (window))
17540 window = selected_window;
17541 CHECK_WINDOW (window);
17542 w = XWINDOW (window);
17543
17544 if (NILP (buffer))
17545 buffer = w->buffer;
17546 CHECK_BUFFER (buffer);
17547
17548 if (NILP (format))
17549 return empty_unibyte_string;
17550
17551 if (no_props)
17552 face = Qnil;
17553
17554 if (!NILP (face))
17555 {
17556 if (EQ (face, Qt))
17557 face = (EQ (window, selected_window) ? Qmode_line : Qmode_line_inactive);
17558 face_id = lookup_named_face (XFRAME (WINDOW_FRAME (w)), face, 0);
17559 }
17560
17561 if (face_id < 0)
17562 face_id = DEFAULT_FACE_ID;
17563
17564 if (XBUFFER (buffer) != current_buffer)
17565 old_buffer = current_buffer;
17566
17567 /* Save things including mode_line_proptrans_alist,
17568 and set that to nil so that we don't alter the outer value. */
17569 record_unwind_protect (unwind_format_mode_line,
17570 format_mode_line_unwind_data (old_buffer, 1));
17571 mode_line_proptrans_alist = Qnil;
17572
17573 if (old_buffer)
17574 set_buffer_internal_1 (XBUFFER (buffer));
17575
17576 init_iterator (&it, w, -1, -1, NULL, face_id);
17577
17578 if (no_props)
17579 {
17580 mode_line_target = MODE_LINE_NOPROP;
17581 mode_line_string_face_prop = Qnil;
17582 mode_line_string_list = Qnil;
17583 string_start = MODE_LINE_NOPROP_LEN (0);
17584 }
17585 else
17586 {
17587 mode_line_target = MODE_LINE_STRING;
17588 mode_line_string_list = Qnil;
17589 mode_line_string_face = face;
17590 mode_line_string_face_prop
17591 = (NILP (face) ? Qnil : Fcons (Qface, Fcons (face, Qnil)));
17592 }
17593
17594 push_kboard (FRAME_KBOARD (it.f));
17595 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
17596 pop_kboard ();
17597
17598 if (no_props)
17599 {
17600 len = MODE_LINE_NOPROP_LEN (string_start);
17601 str = make_string (mode_line_noprop_buf + string_start, len);
17602 }
17603 else
17604 {
17605 mode_line_string_list = Fnreverse (mode_line_string_list);
17606 str = Fmapconcat (intern ("identity"), mode_line_string_list,
17607 empty_unibyte_string);
17608 }
17609
17610 unbind_to (count, Qnil);
17611 return str;
17612 }
17613
17614 /* Write a null-terminated, right justified decimal representation of
17615 the positive integer D to BUF using a minimal field width WIDTH. */
17616
17617 static void
17618 pint2str (buf, width, d)
17619 register char *buf;
17620 register int width;
17621 register int d;
17622 {
17623 register char *p = buf;
17624
17625 if (d <= 0)
17626 *p++ = '0';
17627 else
17628 {
17629 while (d > 0)
17630 {
17631 *p++ = d % 10 + '0';
17632 d /= 10;
17633 }
17634 }
17635
17636 for (width -= (int) (p - buf); width > 0; --width)
17637 *p++ = ' ';
17638 *p-- = '\0';
17639 while (p > buf)
17640 {
17641 d = *buf;
17642 *buf++ = *p;
17643 *p-- = d;
17644 }
17645 }
17646
17647 /* Write a null-terminated, right justified decimal and "human
17648 readable" representation of the nonnegative integer D to BUF using
17649 a minimal field width WIDTH. D should be smaller than 999.5e24. */
17650
17651 static const char power_letter[] =
17652 {
17653 0, /* not used */
17654 'k', /* kilo */
17655 'M', /* mega */
17656 'G', /* giga */
17657 'T', /* tera */
17658 'P', /* peta */
17659 'E', /* exa */
17660 'Z', /* zetta */
17661 'Y' /* yotta */
17662 };
17663
17664 static void
17665 pint2hrstr (buf, width, d)
17666 char *buf;
17667 int width;
17668 int d;
17669 {
17670 /* We aim to represent the nonnegative integer D as
17671 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
17672 int quotient = d;
17673 int remainder = 0;
17674 /* -1 means: do not use TENTHS. */
17675 int tenths = -1;
17676 int exponent = 0;
17677
17678 /* Length of QUOTIENT.TENTHS as a string. */
17679 int length;
17680
17681 char * psuffix;
17682 char * p;
17683
17684 if (1000 <= quotient)
17685 {
17686 /* Scale to the appropriate EXPONENT. */
17687 do
17688 {
17689 remainder = quotient % 1000;
17690 quotient /= 1000;
17691 exponent++;
17692 }
17693 while (1000 <= quotient);
17694
17695 /* Round to nearest and decide whether to use TENTHS or not. */
17696 if (quotient <= 9)
17697 {
17698 tenths = remainder / 100;
17699 if (50 <= remainder % 100)
17700 {
17701 if (tenths < 9)
17702 tenths++;
17703 else
17704 {
17705 quotient++;
17706 if (quotient == 10)
17707 tenths = -1;
17708 else
17709 tenths = 0;
17710 }
17711 }
17712 }
17713 else
17714 if (500 <= remainder)
17715 {
17716 if (quotient < 999)
17717 quotient++;
17718 else
17719 {
17720 quotient = 1;
17721 exponent++;
17722 tenths = 0;
17723 }
17724 }
17725 }
17726
17727 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
17728 if (tenths == -1 && quotient <= 99)
17729 if (quotient <= 9)
17730 length = 1;
17731 else
17732 length = 2;
17733 else
17734 length = 3;
17735 p = psuffix = buf + max (width, length);
17736
17737 /* Print EXPONENT. */
17738 if (exponent)
17739 *psuffix++ = power_letter[exponent];
17740 *psuffix = '\0';
17741
17742 /* Print TENTHS. */
17743 if (tenths >= 0)
17744 {
17745 *--p = '0' + tenths;
17746 *--p = '.';
17747 }
17748
17749 /* Print QUOTIENT. */
17750 do
17751 {
17752 int digit = quotient % 10;
17753 *--p = '0' + digit;
17754 }
17755 while ((quotient /= 10) != 0);
17756
17757 /* Print leading spaces. */
17758 while (buf < p)
17759 *--p = ' ';
17760 }
17761
17762 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
17763 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
17764 type of CODING_SYSTEM. Return updated pointer into BUF. */
17765
17766 static unsigned char invalid_eol_type[] = "(*invalid*)";
17767
17768 static char *
17769 decode_mode_spec_coding (coding_system, buf, eol_flag)
17770 Lisp_Object coding_system;
17771 register char *buf;
17772 int eol_flag;
17773 {
17774 Lisp_Object val;
17775 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
17776 const unsigned char *eol_str;
17777 int eol_str_len;
17778 /* The EOL conversion we are using. */
17779 Lisp_Object eoltype;
17780
17781 val = CODING_SYSTEM_SPEC (coding_system);
17782 eoltype = Qnil;
17783
17784 if (!VECTORP (val)) /* Not yet decided. */
17785 {
17786 if (multibyte)
17787 *buf++ = '-';
17788 if (eol_flag)
17789 eoltype = eol_mnemonic_undecided;
17790 /* Don't mention EOL conversion if it isn't decided. */
17791 }
17792 else
17793 {
17794 Lisp_Object attrs;
17795 Lisp_Object eolvalue;
17796
17797 attrs = AREF (val, 0);
17798 eolvalue = AREF (val, 2);
17799
17800 if (multibyte)
17801 *buf++ = XFASTINT (CODING_ATTR_MNEMONIC (attrs));
17802
17803 if (eol_flag)
17804 {
17805 /* The EOL conversion that is normal on this system. */
17806
17807 if (NILP (eolvalue)) /* Not yet decided. */
17808 eoltype = eol_mnemonic_undecided;
17809 else if (VECTORP (eolvalue)) /* Not yet decided. */
17810 eoltype = eol_mnemonic_undecided;
17811 else /* eolvalue is Qunix, Qdos, or Qmac. */
17812 eoltype = (EQ (eolvalue, Qunix)
17813 ? eol_mnemonic_unix
17814 : (EQ (eolvalue, Qdos) == 1
17815 ? eol_mnemonic_dos : eol_mnemonic_mac));
17816 }
17817 }
17818
17819 if (eol_flag)
17820 {
17821 /* Mention the EOL conversion if it is not the usual one. */
17822 if (STRINGP (eoltype))
17823 {
17824 eol_str = SDATA (eoltype);
17825 eol_str_len = SBYTES (eoltype);
17826 }
17827 else if (CHARACTERP (eoltype))
17828 {
17829 unsigned char *tmp = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
17830 eol_str_len = CHAR_STRING (XINT (eoltype), tmp);
17831 eol_str = tmp;
17832 }
17833 else
17834 {
17835 eol_str = invalid_eol_type;
17836 eol_str_len = sizeof (invalid_eol_type) - 1;
17837 }
17838 bcopy (eol_str, buf, eol_str_len);
17839 buf += eol_str_len;
17840 }
17841
17842 return buf;
17843 }
17844
17845 /* Return a string for the output of a mode line %-spec for window W,
17846 generated by character C. PRECISION >= 0 means don't return a
17847 string longer than that value. FIELD_WIDTH > 0 means pad the
17848 string returned with spaces to that value. Return 1 in *MULTIBYTE
17849 if the result is multibyte text.
17850
17851 Note we operate on the current buffer for most purposes,
17852 the exception being w->base_line_pos. */
17853
17854 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
17855
17856 static char *
17857 decode_mode_spec (w, c, field_width, precision, multibyte)
17858 struct window *w;
17859 register int c;
17860 int field_width, precision;
17861 int *multibyte;
17862 {
17863 Lisp_Object obj;
17864 struct frame *f = XFRAME (WINDOW_FRAME (w));
17865 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
17866 struct buffer *b = current_buffer;
17867
17868 obj = Qnil;
17869 *multibyte = 0;
17870
17871 switch (c)
17872 {
17873 case '*':
17874 if (!NILP (b->read_only))
17875 return "%";
17876 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
17877 return "*";
17878 return "-";
17879
17880 case '+':
17881 /* This differs from %* only for a modified read-only buffer. */
17882 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
17883 return "*";
17884 if (!NILP (b->read_only))
17885 return "%";
17886 return "-";
17887
17888 case '&':
17889 /* This differs from %* in ignoring read-only-ness. */
17890 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
17891 return "*";
17892 return "-";
17893
17894 case '%':
17895 return "%";
17896
17897 case '[':
17898 {
17899 int i;
17900 char *p;
17901
17902 if (command_loop_level > 5)
17903 return "[[[... ";
17904 p = decode_mode_spec_buf;
17905 for (i = 0; i < command_loop_level; i++)
17906 *p++ = '[';
17907 *p = 0;
17908 return decode_mode_spec_buf;
17909 }
17910
17911 case ']':
17912 {
17913 int i;
17914 char *p;
17915
17916 if (command_loop_level > 5)
17917 return " ...]]]";
17918 p = decode_mode_spec_buf;
17919 for (i = 0; i < command_loop_level; i++)
17920 *p++ = ']';
17921 *p = 0;
17922 return decode_mode_spec_buf;
17923 }
17924
17925 case '-':
17926 {
17927 register int i;
17928
17929 /* Let lots_of_dashes be a string of infinite length. */
17930 if (mode_line_target == MODE_LINE_NOPROP ||
17931 mode_line_target == MODE_LINE_STRING)
17932 return "--";
17933 if (field_width <= 0
17934 || field_width > sizeof (lots_of_dashes))
17935 {
17936 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
17937 decode_mode_spec_buf[i] = '-';
17938 decode_mode_spec_buf[i] = '\0';
17939 return decode_mode_spec_buf;
17940 }
17941 else
17942 return lots_of_dashes;
17943 }
17944
17945 case 'b':
17946 obj = b->name;
17947 break;
17948
17949 case 'c':
17950 /* %c and %l are ignored in `frame-title-format'.
17951 (In redisplay_internal, the frame title is drawn _before_ the
17952 windows are updated, so the stuff which depends on actual
17953 window contents (such as %l) may fail to render properly, or
17954 even crash emacs.) */
17955 if (mode_line_target == MODE_LINE_TITLE)
17956 return "";
17957 else
17958 {
17959 int col = (int) current_column (); /* iftc */
17960 w->column_number_displayed = make_number (col);
17961 pint2str (decode_mode_spec_buf, field_width, col);
17962 return decode_mode_spec_buf;
17963 }
17964
17965 case 'e':
17966 #ifndef SYSTEM_MALLOC
17967 {
17968 if (NILP (Vmemory_full))
17969 return "";
17970 else
17971 return "!MEM FULL! ";
17972 }
17973 #else
17974 return "";
17975 #endif
17976
17977 case 'F':
17978 /* %F displays the frame name. */
17979 if (!NILP (f->title))
17980 return (char *) SDATA (f->title);
17981 if (f->explicit_name || ! FRAME_WINDOW_P (f))
17982 return (char *) SDATA (f->name);
17983 return "Emacs";
17984
17985 case 'f':
17986 obj = b->filename;
17987 break;
17988
17989 case 'i':
17990 {
17991 int size = ZV - BEGV;
17992 pint2str (decode_mode_spec_buf, field_width, size);
17993 return decode_mode_spec_buf;
17994 }
17995
17996 case 'I':
17997 {
17998 int size = ZV - BEGV;
17999 pint2hrstr (decode_mode_spec_buf, field_width, size);
18000 return decode_mode_spec_buf;
18001 }
18002
18003 case 'l':
18004 {
18005 int startpos, startpos_byte, line, linepos, linepos_byte;
18006 int topline, nlines, junk, height;
18007
18008 /* %c and %l are ignored in `frame-title-format'. */
18009 if (mode_line_target == MODE_LINE_TITLE)
18010 return "";
18011
18012 startpos = XMARKER (w->start)->charpos;
18013 startpos_byte = marker_byte_position (w->start);
18014 height = WINDOW_TOTAL_LINES (w);
18015
18016 /* If we decided that this buffer isn't suitable for line numbers,
18017 don't forget that too fast. */
18018 if (EQ (w->base_line_pos, w->buffer))
18019 goto no_value;
18020 /* But do forget it, if the window shows a different buffer now. */
18021 else if (BUFFERP (w->base_line_pos))
18022 w->base_line_pos = Qnil;
18023
18024 /* If the buffer is very big, don't waste time. */
18025 if (INTEGERP (Vline_number_display_limit)
18026 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
18027 {
18028 w->base_line_pos = Qnil;
18029 w->base_line_number = Qnil;
18030 goto no_value;
18031 }
18032
18033 if (!NILP (w->base_line_number)
18034 && !NILP (w->base_line_pos)
18035 && XFASTINT (w->base_line_pos) <= startpos)
18036 {
18037 line = XFASTINT (w->base_line_number);
18038 linepos = XFASTINT (w->base_line_pos);
18039 linepos_byte = buf_charpos_to_bytepos (b, linepos);
18040 }
18041 else
18042 {
18043 line = 1;
18044 linepos = BUF_BEGV (b);
18045 linepos_byte = BUF_BEGV_BYTE (b);
18046 }
18047
18048 /* Count lines from base line to window start position. */
18049 nlines = display_count_lines (linepos, linepos_byte,
18050 startpos_byte,
18051 startpos, &junk);
18052
18053 topline = nlines + line;
18054
18055 /* Determine a new base line, if the old one is too close
18056 or too far away, or if we did not have one.
18057 "Too close" means it's plausible a scroll-down would
18058 go back past it. */
18059 if (startpos == BUF_BEGV (b))
18060 {
18061 w->base_line_number = make_number (topline);
18062 w->base_line_pos = make_number (BUF_BEGV (b));
18063 }
18064 else if (nlines < height + 25 || nlines > height * 3 + 50
18065 || linepos == BUF_BEGV (b))
18066 {
18067 int limit = BUF_BEGV (b);
18068 int limit_byte = BUF_BEGV_BYTE (b);
18069 int position;
18070 int distance = (height * 2 + 30) * line_number_display_limit_width;
18071
18072 if (startpos - distance > limit)
18073 {
18074 limit = startpos - distance;
18075 limit_byte = CHAR_TO_BYTE (limit);
18076 }
18077
18078 nlines = display_count_lines (startpos, startpos_byte,
18079 limit_byte,
18080 - (height * 2 + 30),
18081 &position);
18082 /* If we couldn't find the lines we wanted within
18083 line_number_display_limit_width chars per line,
18084 give up on line numbers for this window. */
18085 if (position == limit_byte && limit == startpos - distance)
18086 {
18087 w->base_line_pos = w->buffer;
18088 w->base_line_number = Qnil;
18089 goto no_value;
18090 }
18091
18092 w->base_line_number = make_number (topline - nlines);
18093 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
18094 }
18095
18096 /* Now count lines from the start pos to point. */
18097 nlines = display_count_lines (startpos, startpos_byte,
18098 PT_BYTE, PT, &junk);
18099
18100 /* Record that we did display the line number. */
18101 line_number_displayed = 1;
18102
18103 /* Make the string to show. */
18104 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
18105 return decode_mode_spec_buf;
18106 no_value:
18107 {
18108 char* p = decode_mode_spec_buf;
18109 int pad = field_width - 2;
18110 while (pad-- > 0)
18111 *p++ = ' ';
18112 *p++ = '?';
18113 *p++ = '?';
18114 *p = '\0';
18115 return decode_mode_spec_buf;
18116 }
18117 }
18118 break;
18119
18120 case 'm':
18121 obj = b->mode_name;
18122 break;
18123
18124 case 'n':
18125 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
18126 return " Narrow";
18127 break;
18128
18129 case 'p':
18130 {
18131 int pos = marker_position (w->start);
18132 int total = BUF_ZV (b) - BUF_BEGV (b);
18133
18134 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
18135 {
18136 if (pos <= BUF_BEGV (b))
18137 return "All";
18138 else
18139 return "Bottom";
18140 }
18141 else if (pos <= BUF_BEGV (b))
18142 return "Top";
18143 else
18144 {
18145 if (total > 1000000)
18146 /* Do it differently for a large value, to avoid overflow. */
18147 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
18148 else
18149 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
18150 /* We can't normally display a 3-digit number,
18151 so get us a 2-digit number that is close. */
18152 if (total == 100)
18153 total = 99;
18154 sprintf (decode_mode_spec_buf, "%2d%%", total);
18155 return decode_mode_spec_buf;
18156 }
18157 }
18158
18159 /* Display percentage of size above the bottom of the screen. */
18160 case 'P':
18161 {
18162 int toppos = marker_position (w->start);
18163 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
18164 int total = BUF_ZV (b) - BUF_BEGV (b);
18165
18166 if (botpos >= BUF_ZV (b))
18167 {
18168 if (toppos <= BUF_BEGV (b))
18169 return "All";
18170 else
18171 return "Bottom";
18172 }
18173 else
18174 {
18175 if (total > 1000000)
18176 /* Do it differently for a large value, to avoid overflow. */
18177 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
18178 else
18179 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
18180 /* We can't normally display a 3-digit number,
18181 so get us a 2-digit number that is close. */
18182 if (total == 100)
18183 total = 99;
18184 if (toppos <= BUF_BEGV (b))
18185 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
18186 else
18187 sprintf (decode_mode_spec_buf, "%2d%%", total);
18188 return decode_mode_spec_buf;
18189 }
18190 }
18191
18192 case 's':
18193 /* status of process */
18194 obj = Fget_buffer_process (Fcurrent_buffer ());
18195 if (NILP (obj))
18196 return "no process";
18197 #ifdef subprocesses
18198 obj = Fsymbol_name (Fprocess_status (obj));
18199 #endif
18200 break;
18201
18202 case '@':
18203 {
18204 Lisp_Object val;
18205 val = call1 (intern ("file-remote-p"), current_buffer->directory);
18206 if (NILP (val))
18207 return "-";
18208 else
18209 return "@";
18210 }
18211
18212 case 't': /* indicate TEXT or BINARY */
18213 #ifdef MODE_LINE_BINARY_TEXT
18214 return MODE_LINE_BINARY_TEXT (b);
18215 #else
18216 return "T";
18217 #endif
18218
18219 case 'z':
18220 /* coding-system (not including end-of-line format) */
18221 case 'Z':
18222 /* coding-system (including end-of-line type) */
18223 {
18224 int eol_flag = (c == 'Z');
18225 char *p = decode_mode_spec_buf;
18226
18227 if (! FRAME_WINDOW_P (f))
18228 {
18229 /* No need to mention EOL here--the terminal never needs
18230 to do EOL conversion. */
18231 p = decode_mode_spec_coding (CODING_ID_NAME
18232 (FRAME_KEYBOARD_CODING (f)->id),
18233 p, 0);
18234 p = decode_mode_spec_coding (CODING_ID_NAME
18235 (FRAME_TERMINAL_CODING (f)->id),
18236 p, 0);
18237 }
18238 p = decode_mode_spec_coding (b->buffer_file_coding_system,
18239 p, eol_flag);
18240
18241 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
18242 #ifdef subprocesses
18243 obj = Fget_buffer_process (Fcurrent_buffer ());
18244 if (PROCESSP (obj))
18245 {
18246 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
18247 p, eol_flag);
18248 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
18249 p, eol_flag);
18250 }
18251 #endif /* subprocesses */
18252 #endif /* 0 */
18253 *p = 0;
18254 return decode_mode_spec_buf;
18255 }
18256 }
18257
18258 if (STRINGP (obj))
18259 {
18260 *multibyte = STRING_MULTIBYTE (obj);
18261 return (char *) SDATA (obj);
18262 }
18263 else
18264 return "";
18265 }
18266
18267
18268 /* Count up to COUNT lines starting from START / START_BYTE.
18269 But don't go beyond LIMIT_BYTE.
18270 Return the number of lines thus found (always nonnegative).
18271
18272 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
18273
18274 static int
18275 display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
18276 int start, start_byte, limit_byte, count;
18277 int *byte_pos_ptr;
18278 {
18279 register unsigned char *cursor;
18280 unsigned char *base;
18281
18282 register int ceiling;
18283 register unsigned char *ceiling_addr;
18284 int orig_count = count;
18285
18286 /* If we are not in selective display mode,
18287 check only for newlines. */
18288 int selective_display = (!NILP (current_buffer->selective_display)
18289 && !INTEGERP (current_buffer->selective_display));
18290
18291 if (count > 0)
18292 {
18293 while (start_byte < limit_byte)
18294 {
18295 ceiling = BUFFER_CEILING_OF (start_byte);
18296 ceiling = min (limit_byte - 1, ceiling);
18297 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
18298 base = (cursor = BYTE_POS_ADDR (start_byte));
18299 while (1)
18300 {
18301 if (selective_display)
18302 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
18303 ;
18304 else
18305 while (*cursor != '\n' && ++cursor != ceiling_addr)
18306 ;
18307
18308 if (cursor != ceiling_addr)
18309 {
18310 if (--count == 0)
18311 {
18312 start_byte += cursor - base + 1;
18313 *byte_pos_ptr = start_byte;
18314 return orig_count;
18315 }
18316 else
18317 if (++cursor == ceiling_addr)
18318 break;
18319 }
18320 else
18321 break;
18322 }
18323 start_byte += cursor - base;
18324 }
18325 }
18326 else
18327 {
18328 while (start_byte > limit_byte)
18329 {
18330 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
18331 ceiling = max (limit_byte, ceiling);
18332 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
18333 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
18334 while (1)
18335 {
18336 if (selective_display)
18337 while (--cursor != ceiling_addr
18338 && *cursor != '\n' && *cursor != 015)
18339 ;
18340 else
18341 while (--cursor != ceiling_addr && *cursor != '\n')
18342 ;
18343
18344 if (cursor != ceiling_addr)
18345 {
18346 if (++count == 0)
18347 {
18348 start_byte += cursor - base + 1;
18349 *byte_pos_ptr = start_byte;
18350 /* When scanning backwards, we should
18351 not count the newline posterior to which we stop. */
18352 return - orig_count - 1;
18353 }
18354 }
18355 else
18356 break;
18357 }
18358 /* Here we add 1 to compensate for the last decrement
18359 of CURSOR, which took it past the valid range. */
18360 start_byte += cursor - base + 1;
18361 }
18362 }
18363
18364 *byte_pos_ptr = limit_byte;
18365
18366 if (count < 0)
18367 return - orig_count + count;
18368 return orig_count - count;
18369
18370 }
18371
18372
18373 \f
18374 /***********************************************************************
18375 Displaying strings
18376 ***********************************************************************/
18377
18378 /* Display a NUL-terminated string, starting with index START.
18379
18380 If STRING is non-null, display that C string. Otherwise, the Lisp
18381 string LISP_STRING is displayed.
18382
18383 If FACE_STRING is not nil, FACE_STRING_POS is a position in
18384 FACE_STRING. Display STRING or LISP_STRING with the face at
18385 FACE_STRING_POS in FACE_STRING:
18386
18387 Display the string in the environment given by IT, but use the
18388 standard display table, temporarily.
18389
18390 FIELD_WIDTH is the minimum number of output glyphs to produce.
18391 If STRING has fewer characters than FIELD_WIDTH, pad to the right
18392 with spaces. If STRING has more characters, more than FIELD_WIDTH
18393 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
18394
18395 PRECISION is the maximum number of characters to output from
18396 STRING. PRECISION < 0 means don't truncate the string.
18397
18398 This is roughly equivalent to printf format specifiers:
18399
18400 FIELD_WIDTH PRECISION PRINTF
18401 ----------------------------------------
18402 -1 -1 %s
18403 -1 10 %.10s
18404 10 -1 %10s
18405 20 10 %20.10s
18406
18407 MULTIBYTE zero means do not display multibyte chars, > 0 means do
18408 display them, and < 0 means obey the current buffer's value of
18409 enable_multibyte_characters.
18410
18411 Value is the number of columns displayed. */
18412
18413 static int
18414 display_string (string, lisp_string, face_string, face_string_pos,
18415 start, it, field_width, precision, max_x, multibyte)
18416 unsigned char *string;
18417 Lisp_Object lisp_string;
18418 Lisp_Object face_string;
18419 int face_string_pos;
18420 int start;
18421 struct it *it;
18422 int field_width, precision, max_x;
18423 int multibyte;
18424 {
18425 int hpos_at_start = it->hpos;
18426 int saved_face_id = it->face_id;
18427 struct glyph_row *row = it->glyph_row;
18428
18429 /* Initialize the iterator IT for iteration over STRING beginning
18430 with index START. */
18431 reseat_to_string (it, string, lisp_string, start,
18432 precision, field_width, multibyte);
18433
18434 /* If displaying STRING, set up the face of the iterator
18435 from LISP_STRING, if that's given. */
18436 if (STRINGP (face_string))
18437 {
18438 int endptr;
18439 struct face *face;
18440
18441 it->face_id
18442 = face_at_string_position (it->w, face_string, face_string_pos,
18443 0, it->region_beg_charpos,
18444 it->region_end_charpos,
18445 &endptr, it->base_face_id, 0);
18446 face = FACE_FROM_ID (it->f, it->face_id);
18447 it->face_box_p = face->box != FACE_NO_BOX;
18448 }
18449
18450 /* Set max_x to the maximum allowed X position. Don't let it go
18451 beyond the right edge of the window. */
18452 if (max_x <= 0)
18453 max_x = it->last_visible_x;
18454 else
18455 max_x = min (max_x, it->last_visible_x);
18456
18457 /* Skip over display elements that are not visible. because IT->w is
18458 hscrolled. */
18459 if (it->current_x < it->first_visible_x)
18460 move_it_in_display_line_to (it, 100000, it->first_visible_x,
18461 MOVE_TO_POS | MOVE_TO_X);
18462
18463 row->ascent = it->max_ascent;
18464 row->height = it->max_ascent + it->max_descent;
18465 row->phys_ascent = it->max_phys_ascent;
18466 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
18467 row->extra_line_spacing = it->max_extra_line_spacing;
18468
18469 /* This condition is for the case that we are called with current_x
18470 past last_visible_x. */
18471 while (it->current_x < max_x)
18472 {
18473 int x_before, x, n_glyphs_before, i, nglyphs;
18474
18475 /* Get the next display element. */
18476 if (!get_next_display_element (it))
18477 break;
18478
18479 /* Produce glyphs. */
18480 x_before = it->current_x;
18481 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
18482 PRODUCE_GLYPHS (it);
18483
18484 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
18485 i = 0;
18486 x = x_before;
18487 while (i < nglyphs)
18488 {
18489 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
18490
18491 if (!it->truncate_lines_p
18492 && x + glyph->pixel_width > max_x)
18493 {
18494 /* End of continued line or max_x reached. */
18495 if (CHAR_GLYPH_PADDING_P (*glyph))
18496 {
18497 /* A wide character is unbreakable. */
18498 it->glyph_row->used[TEXT_AREA] = n_glyphs_before;
18499 it->current_x = x_before;
18500 }
18501 else
18502 {
18503 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
18504 it->current_x = x;
18505 }
18506 break;
18507 }
18508 else if (x + glyph->pixel_width >= it->first_visible_x)
18509 {
18510 /* Glyph is at least partially visible. */
18511 ++it->hpos;
18512 if (x < it->first_visible_x)
18513 it->glyph_row->x = x - it->first_visible_x;
18514 }
18515 else
18516 {
18517 /* Glyph is off the left margin of the display area.
18518 Should not happen. */
18519 abort ();
18520 }
18521
18522 row->ascent = max (row->ascent, it->max_ascent);
18523 row->height = max (row->height, it->max_ascent + it->max_descent);
18524 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
18525 row->phys_height = max (row->phys_height,
18526 it->max_phys_ascent + it->max_phys_descent);
18527 row->extra_line_spacing = max (row->extra_line_spacing,
18528 it->max_extra_line_spacing);
18529 x += glyph->pixel_width;
18530 ++i;
18531 }
18532
18533 /* Stop if max_x reached. */
18534 if (i < nglyphs)
18535 break;
18536
18537 /* Stop at line ends. */
18538 if (ITERATOR_AT_END_OF_LINE_P (it))
18539 {
18540 it->continuation_lines_width = 0;
18541 break;
18542 }
18543
18544 set_iterator_to_next (it, 1);
18545
18546 /* Stop if truncating at the right edge. */
18547 if (it->truncate_lines_p
18548 && it->current_x >= it->last_visible_x)
18549 {
18550 /* Add truncation mark, but don't do it if the line is
18551 truncated at a padding space. */
18552 if (IT_CHARPOS (*it) < it->string_nchars)
18553 {
18554 if (!FRAME_WINDOW_P (it->f))
18555 {
18556 int i, n;
18557
18558 if (it->current_x > it->last_visible_x)
18559 {
18560 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
18561 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
18562 break;
18563 for (n = row->used[TEXT_AREA]; i < n; ++i)
18564 {
18565 row->used[TEXT_AREA] = i;
18566 produce_special_glyphs (it, IT_TRUNCATION);
18567 }
18568 }
18569 produce_special_glyphs (it, IT_TRUNCATION);
18570 }
18571 it->glyph_row->truncated_on_right_p = 1;
18572 }
18573 break;
18574 }
18575 }
18576
18577 /* Maybe insert a truncation at the left. */
18578 if (it->first_visible_x
18579 && IT_CHARPOS (*it) > 0)
18580 {
18581 if (!FRAME_WINDOW_P (it->f))
18582 insert_left_trunc_glyphs (it);
18583 it->glyph_row->truncated_on_left_p = 1;
18584 }
18585
18586 it->face_id = saved_face_id;
18587
18588 /* Value is number of columns displayed. */
18589 return it->hpos - hpos_at_start;
18590 }
18591
18592
18593 \f
18594 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
18595 appears as an element of LIST or as the car of an element of LIST.
18596 If PROPVAL is a list, compare each element against LIST in that
18597 way, and return 1/2 if any element of PROPVAL is found in LIST.
18598 Otherwise return 0. This function cannot quit.
18599 The return value is 2 if the text is invisible but with an ellipsis
18600 and 1 if it's invisible and without an ellipsis. */
18601
18602 int
18603 invisible_p (propval, list)
18604 register Lisp_Object propval;
18605 Lisp_Object list;
18606 {
18607 register Lisp_Object tail, proptail;
18608
18609 for (tail = list; CONSP (tail); tail = XCDR (tail))
18610 {
18611 register Lisp_Object tem;
18612 tem = XCAR (tail);
18613 if (EQ (propval, tem))
18614 return 1;
18615 if (CONSP (tem) && EQ (propval, XCAR (tem)))
18616 return NILP (XCDR (tem)) ? 1 : 2;
18617 }
18618
18619 if (CONSP (propval))
18620 {
18621 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
18622 {
18623 Lisp_Object propelt;
18624 propelt = XCAR (proptail);
18625 for (tail = list; CONSP (tail); tail = XCDR (tail))
18626 {
18627 register Lisp_Object tem;
18628 tem = XCAR (tail);
18629 if (EQ (propelt, tem))
18630 return 1;
18631 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
18632 return NILP (XCDR (tem)) ? 1 : 2;
18633 }
18634 }
18635 }
18636
18637 return 0;
18638 }
18639
18640 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
18641 doc: /* Non-nil if the property makes the text invisible.
18642 POS-OR-PROP can be a marker or number, in which case it is taken to be
18643 a position in the current buffer and the value of the `invisible' property
18644 is checked; or it can be some other value, which is then presumed to be the
18645 value of the `invisible' property of the text of interest.
18646 The non-nil value returned can be t for truly invisible text or something
18647 else if the text is replaced by an ellipsis. */)
18648 (pos_or_prop)
18649 Lisp_Object pos_or_prop;
18650 {
18651 Lisp_Object prop
18652 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
18653 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
18654 : pos_or_prop);
18655 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
18656 return (invis == 0 ? Qnil
18657 : invis == 1 ? Qt
18658 : make_number (invis));
18659 }
18660
18661 /* Calculate a width or height in pixels from a specification using
18662 the following elements:
18663
18664 SPEC ::=
18665 NUM - a (fractional) multiple of the default font width/height
18666 (NUM) - specifies exactly NUM pixels
18667 UNIT - a fixed number of pixels, see below.
18668 ELEMENT - size of a display element in pixels, see below.
18669 (NUM . SPEC) - equals NUM * SPEC
18670 (+ SPEC SPEC ...) - add pixel values
18671 (- SPEC SPEC ...) - subtract pixel values
18672 (- SPEC) - negate pixel value
18673
18674 NUM ::=
18675 INT or FLOAT - a number constant
18676 SYMBOL - use symbol's (buffer local) variable binding.
18677
18678 UNIT ::=
18679 in - pixels per inch *)
18680 mm - pixels per 1/1000 meter *)
18681 cm - pixels per 1/100 meter *)
18682 width - width of current font in pixels.
18683 height - height of current font in pixels.
18684
18685 *) using the ratio(s) defined in display-pixels-per-inch.
18686
18687 ELEMENT ::=
18688
18689 left-fringe - left fringe width in pixels
18690 right-fringe - right fringe width in pixels
18691
18692 left-margin - left margin width in pixels
18693 right-margin - right margin width in pixels
18694
18695 scroll-bar - scroll-bar area width in pixels
18696
18697 Examples:
18698
18699 Pixels corresponding to 5 inches:
18700 (5 . in)
18701
18702 Total width of non-text areas on left side of window (if scroll-bar is on left):
18703 '(space :width (+ left-fringe left-margin scroll-bar))
18704
18705 Align to first text column (in header line):
18706 '(space :align-to 0)
18707
18708 Align to middle of text area minus half the width of variable `my-image'
18709 containing a loaded image:
18710 '(space :align-to (0.5 . (- text my-image)))
18711
18712 Width of left margin minus width of 1 character in the default font:
18713 '(space :width (- left-margin 1))
18714
18715 Width of left margin minus width of 2 characters in the current font:
18716 '(space :width (- left-margin (2 . width)))
18717
18718 Center 1 character over left-margin (in header line):
18719 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
18720
18721 Different ways to express width of left fringe plus left margin minus one pixel:
18722 '(space :width (- (+ left-fringe left-margin) (1)))
18723 '(space :width (+ left-fringe left-margin (- (1))))
18724 '(space :width (+ left-fringe left-margin (-1)))
18725
18726 */
18727
18728 #define NUMVAL(X) \
18729 ((INTEGERP (X) || FLOATP (X)) \
18730 ? XFLOATINT (X) \
18731 : - 1)
18732
18733 int
18734 calc_pixel_width_or_height (res, it, prop, font, width_p, align_to)
18735 double *res;
18736 struct it *it;
18737 Lisp_Object prop;
18738 void *font;
18739 int width_p, *align_to;
18740 {
18741 double pixels;
18742
18743 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
18744 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
18745
18746 if (NILP (prop))
18747 return OK_PIXELS (0);
18748
18749 xassert (FRAME_LIVE_P (it->f));
18750
18751 if (SYMBOLP (prop))
18752 {
18753 if (SCHARS (SYMBOL_NAME (prop)) == 2)
18754 {
18755 char *unit = SDATA (SYMBOL_NAME (prop));
18756
18757 if (unit[0] == 'i' && unit[1] == 'n')
18758 pixels = 1.0;
18759 else if (unit[0] == 'm' && unit[1] == 'm')
18760 pixels = 25.4;
18761 else if (unit[0] == 'c' && unit[1] == 'm')
18762 pixels = 2.54;
18763 else
18764 pixels = 0;
18765 if (pixels > 0)
18766 {
18767 double ppi;
18768 #ifdef HAVE_WINDOW_SYSTEM
18769 if (FRAME_WINDOW_P (it->f)
18770 && (ppi = (width_p
18771 ? FRAME_X_DISPLAY_INFO (it->f)->resx
18772 : FRAME_X_DISPLAY_INFO (it->f)->resy),
18773 ppi > 0))
18774 return OK_PIXELS (ppi / pixels);
18775 #endif
18776
18777 if ((ppi = NUMVAL (Vdisplay_pixels_per_inch), ppi > 0)
18778 || (CONSP (Vdisplay_pixels_per_inch)
18779 && (ppi = (width_p
18780 ? NUMVAL (XCAR (Vdisplay_pixels_per_inch))
18781 : NUMVAL (XCDR (Vdisplay_pixels_per_inch))),
18782 ppi > 0)))
18783 return OK_PIXELS (ppi / pixels);
18784
18785 return 0;
18786 }
18787 }
18788
18789 #ifdef HAVE_WINDOW_SYSTEM
18790 if (EQ (prop, Qheight))
18791 return OK_PIXELS (font ? FONT_HEIGHT ((XFontStruct *)font) : FRAME_LINE_HEIGHT (it->f));
18792 if (EQ (prop, Qwidth))
18793 return OK_PIXELS (font ? FONT_WIDTH ((XFontStruct *)font) : FRAME_COLUMN_WIDTH (it->f));
18794 #else
18795 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
18796 return OK_PIXELS (1);
18797 #endif
18798
18799 if (EQ (prop, Qtext))
18800 return OK_PIXELS (width_p
18801 ? window_box_width (it->w, TEXT_AREA)
18802 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
18803
18804 if (align_to && *align_to < 0)
18805 {
18806 *res = 0;
18807 if (EQ (prop, Qleft))
18808 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
18809 if (EQ (prop, Qright))
18810 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
18811 if (EQ (prop, Qcenter))
18812 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
18813 + window_box_width (it->w, TEXT_AREA) / 2);
18814 if (EQ (prop, Qleft_fringe))
18815 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
18816 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
18817 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
18818 if (EQ (prop, Qright_fringe))
18819 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
18820 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
18821 : window_box_right_offset (it->w, TEXT_AREA));
18822 if (EQ (prop, Qleft_margin))
18823 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
18824 if (EQ (prop, Qright_margin))
18825 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
18826 if (EQ (prop, Qscroll_bar))
18827 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
18828 ? 0
18829 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
18830 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
18831 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
18832 : 0)));
18833 }
18834 else
18835 {
18836 if (EQ (prop, Qleft_fringe))
18837 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
18838 if (EQ (prop, Qright_fringe))
18839 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
18840 if (EQ (prop, Qleft_margin))
18841 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
18842 if (EQ (prop, Qright_margin))
18843 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
18844 if (EQ (prop, Qscroll_bar))
18845 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
18846 }
18847
18848 prop = Fbuffer_local_value (prop, it->w->buffer);
18849 }
18850
18851 if (INTEGERP (prop) || FLOATP (prop))
18852 {
18853 int base_unit = (width_p
18854 ? FRAME_COLUMN_WIDTH (it->f)
18855 : FRAME_LINE_HEIGHT (it->f));
18856 return OK_PIXELS (XFLOATINT (prop) * base_unit);
18857 }
18858
18859 if (CONSP (prop))
18860 {
18861 Lisp_Object car = XCAR (prop);
18862 Lisp_Object cdr = XCDR (prop);
18863
18864 if (SYMBOLP (car))
18865 {
18866 #ifdef HAVE_WINDOW_SYSTEM
18867 if (FRAME_WINDOW_P (it->f)
18868 && valid_image_p (prop))
18869 {
18870 int id = lookup_image (it->f, prop);
18871 struct image *img = IMAGE_FROM_ID (it->f, id);
18872
18873 return OK_PIXELS (width_p ? img->width : img->height);
18874 }
18875 #endif
18876 if (EQ (car, Qplus) || EQ (car, Qminus))
18877 {
18878 int first = 1;
18879 double px;
18880
18881 pixels = 0;
18882 while (CONSP (cdr))
18883 {
18884 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
18885 font, width_p, align_to))
18886 return 0;
18887 if (first)
18888 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
18889 else
18890 pixels += px;
18891 cdr = XCDR (cdr);
18892 }
18893 if (EQ (car, Qminus))
18894 pixels = -pixels;
18895 return OK_PIXELS (pixels);
18896 }
18897
18898 car = Fbuffer_local_value (car, it->w->buffer);
18899 }
18900
18901 if (INTEGERP (car) || FLOATP (car))
18902 {
18903 double fact;
18904 pixels = XFLOATINT (car);
18905 if (NILP (cdr))
18906 return OK_PIXELS (pixels);
18907 if (calc_pixel_width_or_height (&fact, it, cdr,
18908 font, width_p, align_to))
18909 return OK_PIXELS (pixels * fact);
18910 return 0;
18911 }
18912
18913 return 0;
18914 }
18915
18916 return 0;
18917 }
18918
18919 \f
18920 /***********************************************************************
18921 Glyph Display
18922 ***********************************************************************/
18923
18924 #ifdef HAVE_WINDOW_SYSTEM
18925
18926 #if GLYPH_DEBUG
18927
18928 void
18929 dump_glyph_string (s)
18930 struct glyph_string *s;
18931 {
18932 fprintf (stderr, "glyph string\n");
18933 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
18934 s->x, s->y, s->width, s->height);
18935 fprintf (stderr, " ybase = %d\n", s->ybase);
18936 fprintf (stderr, " hl = %d\n", s->hl);
18937 fprintf (stderr, " left overhang = %d, right = %d\n",
18938 s->left_overhang, s->right_overhang);
18939 fprintf (stderr, " nchars = %d\n", s->nchars);
18940 fprintf (stderr, " extends to end of line = %d\n",
18941 s->extends_to_end_of_line_p);
18942 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
18943 fprintf (stderr, " bg width = %d\n", s->background_width);
18944 }
18945
18946 #endif /* GLYPH_DEBUG */
18947
18948 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
18949 of XChar2b structures for S; it can't be allocated in
18950 init_glyph_string because it must be allocated via `alloca'. W
18951 is the window on which S is drawn. ROW and AREA are the glyph row
18952 and area within the row from which S is constructed. START is the
18953 index of the first glyph structure covered by S. HL is a
18954 face-override for drawing S. */
18955
18956 #ifdef HAVE_NTGUI
18957 #define OPTIONAL_HDC(hdc) hdc,
18958 #define DECLARE_HDC(hdc) HDC hdc;
18959 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
18960 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
18961 #endif
18962
18963 #ifndef OPTIONAL_HDC
18964 #define OPTIONAL_HDC(hdc)
18965 #define DECLARE_HDC(hdc)
18966 #define ALLOCATE_HDC(hdc, f)
18967 #define RELEASE_HDC(hdc, f)
18968 #endif
18969
18970 static void
18971 init_glyph_string (s, OPTIONAL_HDC (hdc) char2b, w, row, area, start, hl)
18972 struct glyph_string *s;
18973 DECLARE_HDC (hdc)
18974 XChar2b *char2b;
18975 struct window *w;
18976 struct glyph_row *row;
18977 enum glyph_row_area area;
18978 int start;
18979 enum draw_glyphs_face hl;
18980 {
18981 bzero (s, sizeof *s);
18982 s->w = w;
18983 s->f = XFRAME (w->frame);
18984 #ifdef HAVE_NTGUI
18985 s->hdc = hdc;
18986 #endif
18987 s->display = FRAME_X_DISPLAY (s->f);
18988 s->window = FRAME_X_WINDOW (s->f);
18989 s->char2b = char2b;
18990 s->hl = hl;
18991 s->row = row;
18992 s->area = area;
18993 s->first_glyph = row->glyphs[area] + start;
18994 s->height = row->height;
18995 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
18996
18997 /* Display the internal border below the tool-bar window. */
18998 if (WINDOWP (s->f->tool_bar_window)
18999 && s->w == XWINDOW (s->f->tool_bar_window))
19000 s->y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
19001
19002 s->ybase = s->y + row->ascent;
19003 }
19004
19005
19006 /* Append the list of glyph strings with head H and tail T to the list
19007 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
19008
19009 static INLINE void
19010 append_glyph_string_lists (head, tail, h, t)
19011 struct glyph_string **head, **tail;
19012 struct glyph_string *h, *t;
19013 {
19014 if (h)
19015 {
19016 if (*head)
19017 (*tail)->next = h;
19018 else
19019 *head = h;
19020 h->prev = *tail;
19021 *tail = t;
19022 }
19023 }
19024
19025
19026 /* Prepend the list of glyph strings with head H and tail T to the
19027 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
19028 result. */
19029
19030 static INLINE void
19031 prepend_glyph_string_lists (head, tail, h, t)
19032 struct glyph_string **head, **tail;
19033 struct glyph_string *h, *t;
19034 {
19035 if (h)
19036 {
19037 if (*head)
19038 (*head)->prev = t;
19039 else
19040 *tail = t;
19041 t->next = *head;
19042 *head = h;
19043 }
19044 }
19045
19046
19047 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
19048 Set *HEAD and *TAIL to the resulting list. */
19049
19050 static INLINE void
19051 append_glyph_string (head, tail, s)
19052 struct glyph_string **head, **tail;
19053 struct glyph_string *s;
19054 {
19055 s->next = s->prev = NULL;
19056 append_glyph_string_lists (head, tail, s, s);
19057 }
19058
19059
19060 /* Get face and two-byte form of character C in face FACE_ID on frame
19061 F. The encoding of C is returned in *CHAR2B. MULTIBYTE_P non-zero
19062 means we want to display multibyte text. DISPLAY_P non-zero means
19063 make sure that X resources for the face returned are allocated.
19064 Value is a pointer to a realized face that is ready for display if
19065 DISPLAY_P is non-zero. */
19066
19067 static INLINE struct face *
19068 get_char_face_and_encoding (f, c, face_id, char2b, multibyte_p, display_p)
19069 struct frame *f;
19070 int c, face_id;
19071 XChar2b *char2b;
19072 int multibyte_p, display_p;
19073 {
19074 struct face *face = FACE_FROM_ID (f, face_id);
19075
19076 #ifdef USE_FONT_BACKEND
19077 if (enable_font_backend)
19078 {
19079 struct font *font = (struct font *) face->font_info;
19080
19081 if (font)
19082 {
19083 unsigned code = font->driver->encode_char (font, c);
19084
19085 if (code != FONT_INVALID_CODE)
19086 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
19087 else
19088 STORE_XCHAR2B (char2b, 0, 0);
19089 }
19090 }
19091 else
19092 #endif /* USE_FONT_BACKEND */
19093 if (!multibyte_p)
19094 {
19095 /* Unibyte case. We don't have to encode, but we have to make
19096 sure to use a face suitable for unibyte. */
19097 STORE_XCHAR2B (char2b, 0, c);
19098 face_id = FACE_FOR_CHAR (f, face, c, -1, Qnil);
19099 face = FACE_FROM_ID (f, face_id);
19100 }
19101 else if (c < 128)
19102 {
19103 /* Case of ASCII in a face known to fit ASCII. */
19104 STORE_XCHAR2B (char2b, 0, c);
19105 }
19106 else if (face->font != NULL)
19107 {
19108 struct font_info *font_info
19109 = FONT_INFO_FROM_ID (f, face->font_info_id);
19110 struct charset *charset = CHARSET_FROM_ID (font_info->charset);
19111 unsigned code = ENCODE_CHAR (charset, c);
19112
19113 if (CHARSET_DIMENSION (charset) == 1)
19114 STORE_XCHAR2B (char2b, 0, code);
19115 else
19116 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
19117 /* Maybe encode the character in *CHAR2B. */
19118 FRAME_RIF (f)->encode_char (c, char2b, font_info, charset, NULL);
19119 }
19120
19121 /* Make sure X resources of the face are allocated. */
19122 #ifdef HAVE_X_WINDOWS
19123 if (display_p)
19124 #endif
19125 {
19126 xassert (face != NULL);
19127 PREPARE_FACE_FOR_DISPLAY (f, face);
19128 }
19129
19130 return face;
19131 }
19132
19133
19134 /* Get face and two-byte form of character glyph GLYPH on frame F.
19135 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
19136 a pointer to a realized face that is ready for display. */
19137
19138 static INLINE struct face *
19139 get_glyph_face_and_encoding (f, glyph, char2b, two_byte_p)
19140 struct frame *f;
19141 struct glyph *glyph;
19142 XChar2b *char2b;
19143 int *two_byte_p;
19144 {
19145 struct face *face;
19146
19147 xassert (glyph->type == CHAR_GLYPH);
19148 face = FACE_FROM_ID (f, glyph->face_id);
19149
19150 if (two_byte_p)
19151 *two_byte_p = 0;
19152
19153 #ifdef USE_FONT_BACKEND
19154 if (enable_font_backend)
19155 {
19156 struct font *font = (struct font *) face->font_info;
19157
19158 if (font)
19159 {
19160 unsigned code = font->driver->encode_char (font, glyph->u.ch);
19161
19162 if (code != FONT_INVALID_CODE)
19163 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
19164 else
19165 STORE_XCHAR2B (char2b, 0, code);
19166 }
19167 }
19168 else
19169 #endif /* USE_FONT_BACKEND */
19170 if (!glyph->multibyte_p)
19171 {
19172 /* Unibyte case. We don't have to encode, but we have to make
19173 sure to use a face suitable for unibyte. */
19174 STORE_XCHAR2B (char2b, 0, glyph->u.ch);
19175 }
19176 else if (glyph->u.ch < 128)
19177 {
19178 /* Case of ASCII in a face known to fit ASCII. */
19179 STORE_XCHAR2B (char2b, 0, glyph->u.ch);
19180 }
19181 else
19182 {
19183 struct font_info *font_info
19184 = FONT_INFO_FROM_ID (f, face->font_info_id);
19185 if (font_info)
19186 {
19187 struct charset *charset = CHARSET_FROM_ID (font_info->charset);
19188 unsigned code = ENCODE_CHAR (charset, glyph->u.ch);
19189
19190 if (CHARSET_DIMENSION (charset) == 1)
19191 STORE_XCHAR2B (char2b, 0, code);
19192 else
19193 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
19194
19195 /* Maybe encode the character in *CHAR2B. */
19196 if (CHARSET_ID (charset) != charset_ascii)
19197 {
19198 glyph->font_type
19199 = FRAME_RIF (f)->encode_char (glyph->u.ch, char2b, font_info,
19200 charset, two_byte_p);
19201 }
19202 }
19203 }
19204
19205 /* Make sure X resources of the face are allocated. */
19206 xassert (face != NULL);
19207 PREPARE_FACE_FOR_DISPLAY (f, face);
19208 return face;
19209 }
19210
19211
19212 /* Fill glyph string S with composition components specified by S->cmp.
19213
19214 BASE_FACE is the base face of the composition.
19215 S->gidx is the index of the first component for S.
19216
19217 OVERLAPS non-zero means S should draw the foreground only, and use
19218 its physical height for clipping. See also draw_glyphs.
19219
19220 Value is the index of a component not in S. */
19221
19222 static int
19223 fill_composite_glyph_string (s, base_face, overlaps)
19224 struct glyph_string *s;
19225 struct face *base_face;
19226 int overlaps;
19227 {
19228 int i;
19229
19230 xassert (s);
19231
19232 s->for_overlaps = overlaps;
19233
19234 #ifdef USE_FONT_BACKEND
19235 if (enable_font_backend && s->cmp->method == COMPOSITION_WITH_GLYPH_STRING)
19236 {
19237 Lisp_Object gstring
19238 = AREF (XHASH_TABLE (composition_hash_table)->key_and_value,
19239 s->cmp->hash_index * 2);
19240
19241 s->face = base_face;
19242 s->font_info = s->cmp->font;
19243 s->font = s->font_info->font;
19244 for (i = 0, s->nchars = 0; i < s->cmp->glyph_len; i++, s->nchars++)
19245 {
19246 Lisp_Object g = LGSTRING_GLYPH (gstring, i);
19247 unsigned code;
19248 XChar2b * store_pos;
19249 if (NILP (LGLYPH_FROM (g)))
19250 break;
19251 code = XUINT (LGLYPH_CODE (g));
19252 store_pos = s->char2b + i;
19253 STORE_XCHAR2B (store_pos, code >> 8, code & 0xFF);
19254 }
19255 s->width = s->cmp->pixel_width;
19256 }
19257 else
19258 #endif /* USE_FONT_BACKEND */
19259 {
19260 /* For all glyphs of this composition, starting at the offset
19261 S->gidx, until we reach the end of the definition or encounter a
19262 glyph that requires the different face, add it to S. */
19263 struct face *face;
19264
19265 s->face = NULL;
19266 s->font = NULL;
19267 s->font_info = NULL;
19268 for (i = s->gidx; i < s->cmp->glyph_len; i++)
19269 {
19270 int c = COMPOSITION_GLYPH (s->cmp, i);
19271
19272 if (c != '\t')
19273 {
19274 int face_id = FACE_FOR_CHAR (s->f, base_face, c, -1, Qnil);
19275
19276 face = get_char_face_and_encoding (s->f, c, face_id,
19277 s->char2b + i, 1, 1);
19278 if (face)
19279 {
19280 if (! s->face)
19281 {
19282 s->face = face;
19283 s->font = s->face->font;
19284 s->font_info = FONT_INFO_FROM_FACE (s->f, s->face);
19285 }
19286 else if (s->face != face)
19287 break;
19288 }
19289 }
19290 ++s->nchars;
19291 }
19292
19293 /* All glyph strings for the same composition has the same width,
19294 i.e. the width set for the first component of the composition. */
19295 s->width = s->first_glyph->pixel_width;
19296 }
19297
19298 /* If the specified font could not be loaded, use the frame's
19299 default font, but record the fact that we couldn't load it in
19300 the glyph string so that we can draw rectangles for the
19301 characters of the glyph string. */
19302 if (s->font == NULL)
19303 {
19304 s->font_not_found_p = 1;
19305 s->font = FRAME_FONT (s->f);
19306 }
19307
19308 /* Adjust base line for subscript/superscript text. */
19309 s->ybase += s->first_glyph->voffset;
19310
19311 /* This glyph string must always be drawn with 16-bit functions. */
19312 s->two_byte_p = 1;
19313
19314 return s->gidx + s->nchars;
19315 }
19316
19317
19318 /* Fill glyph string S from a sequence of character glyphs.
19319
19320 FACE_ID is the face id of the string. START is the index of the
19321 first glyph to consider, END is the index of the last + 1.
19322 OVERLAPS non-zero means S should draw the foreground only, and use
19323 its physical height for clipping. See also draw_glyphs.
19324
19325 Value is the index of the first glyph not in S. */
19326
19327 static int
19328 fill_glyph_string (s, face_id, start, end, overlaps)
19329 struct glyph_string *s;
19330 int face_id;
19331 int start, end, overlaps;
19332 {
19333 struct glyph *glyph, *last;
19334 int voffset;
19335 int glyph_not_available_p;
19336
19337 xassert (s->f == XFRAME (s->w->frame));
19338 xassert (s->nchars == 0);
19339 xassert (start >= 0 && end > start);
19340
19341 s->for_overlaps = overlaps,
19342 glyph = s->row->glyphs[s->area] + start;
19343 last = s->row->glyphs[s->area] + end;
19344 voffset = glyph->voffset;
19345
19346 glyph_not_available_p = glyph->glyph_not_available_p;
19347
19348 while (glyph < last
19349 && glyph->type == CHAR_GLYPH
19350 && glyph->voffset == voffset
19351 /* Same face id implies same font, nowadays. */
19352 && glyph->face_id == face_id
19353 && glyph->glyph_not_available_p == glyph_not_available_p)
19354 {
19355 int two_byte_p;
19356
19357 s->face = get_glyph_face_and_encoding (s->f, glyph,
19358 s->char2b + s->nchars,
19359 &two_byte_p);
19360 s->two_byte_p = two_byte_p;
19361 ++s->nchars;
19362 xassert (s->nchars <= end - start);
19363 s->width += glyph->pixel_width;
19364 ++glyph;
19365 }
19366
19367 s->font = s->face->font;
19368 s->font_info = FONT_INFO_FROM_FACE (s->f, s->face);
19369
19370 /* If the specified font could not be loaded, use the frame's font,
19371 but record the fact that we couldn't load it in
19372 S->font_not_found_p so that we can draw rectangles for the
19373 characters of the glyph string. */
19374 if (s->font == NULL || glyph_not_available_p)
19375 {
19376 s->font_not_found_p = 1;
19377 s->font = FRAME_FONT (s->f);
19378 }
19379
19380 /* Adjust base line for subscript/superscript text. */
19381 s->ybase += voffset;
19382
19383 xassert (s->face && s->face->gc);
19384 return glyph - s->row->glyphs[s->area];
19385 }
19386
19387
19388 /* Fill glyph string S from image glyph S->first_glyph. */
19389
19390 static void
19391 fill_image_glyph_string (s)
19392 struct glyph_string *s;
19393 {
19394 xassert (s->first_glyph->type == IMAGE_GLYPH);
19395 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
19396 xassert (s->img);
19397 s->slice = s->first_glyph->slice;
19398 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
19399 s->font = s->face->font;
19400 s->width = s->first_glyph->pixel_width;
19401
19402 /* Adjust base line for subscript/superscript text. */
19403 s->ybase += s->first_glyph->voffset;
19404 }
19405
19406
19407 /* Fill glyph string S from a sequence of stretch glyphs.
19408
19409 ROW is the glyph row in which the glyphs are found, AREA is the
19410 area within the row. START is the index of the first glyph to
19411 consider, END is the index of the last + 1.
19412
19413 Value is the index of the first glyph not in S. */
19414
19415 static int
19416 fill_stretch_glyph_string (s, row, area, start, end)
19417 struct glyph_string *s;
19418 struct glyph_row *row;
19419 enum glyph_row_area area;
19420 int start, end;
19421 {
19422 struct glyph *glyph, *last;
19423 int voffset, face_id;
19424
19425 xassert (s->first_glyph->type == STRETCH_GLYPH);
19426
19427 glyph = s->row->glyphs[s->area] + start;
19428 last = s->row->glyphs[s->area] + end;
19429 face_id = glyph->face_id;
19430 s->face = FACE_FROM_ID (s->f, face_id);
19431 s->font = s->face->font;
19432 s->font_info = FONT_INFO_FROM_FACE (s->f, s->face);
19433 s->width = glyph->pixel_width;
19434 s->nchars = 1;
19435 voffset = glyph->voffset;
19436
19437 for (++glyph;
19438 (glyph < last
19439 && glyph->type == STRETCH_GLYPH
19440 && glyph->voffset == voffset
19441 && glyph->face_id == face_id);
19442 ++glyph)
19443 s->width += glyph->pixel_width;
19444
19445 /* Adjust base line for subscript/superscript text. */
19446 s->ybase += voffset;
19447
19448 /* The case that face->gc == 0 is handled when drawing the glyph
19449 string by calling PREPARE_FACE_FOR_DISPLAY. */
19450 xassert (s->face);
19451 return glyph - s->row->glyphs[s->area];
19452 }
19453
19454 static XCharStruct *
19455 get_per_char_metric (f, font, font_info, char2b, font_type)
19456 struct frame *f;
19457 XFontStruct *font;
19458 struct font_info *font_info;
19459 XChar2b *char2b;
19460 int font_type;
19461 {
19462 #ifdef USE_FONT_BACKEND
19463 if (enable_font_backend)
19464 {
19465 static XCharStruct pcm_value;
19466 unsigned code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
19467 struct font *fontp;
19468 struct font_metrics metrics;
19469
19470 if (! font_info || code == FONT_INVALID_CODE)
19471 return NULL;
19472 fontp = (struct font *) font_info;
19473 fontp->driver->text_extents (fontp, &code, 1, &metrics);
19474 pcm_value.lbearing = metrics.lbearing;
19475 pcm_value.rbearing = metrics.rbearing;
19476 pcm_value.ascent = metrics.ascent;
19477 pcm_value.descent = metrics.descent;
19478 pcm_value.width = metrics.width;
19479 return &pcm_value;
19480 }
19481 #endif /* USE_FONT_BACKEND */
19482 return FRAME_RIF (f)->per_char_metric (font, char2b, font_type);
19483 }
19484
19485 /* EXPORT for RIF:
19486 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
19487 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
19488 assumed to be zero. */
19489
19490 void
19491 x_get_glyph_overhangs (glyph, f, left, right)
19492 struct glyph *glyph;
19493 struct frame *f;
19494 int *left, *right;
19495 {
19496 *left = *right = 0;
19497
19498 if (glyph->type == CHAR_GLYPH)
19499 {
19500 XFontStruct *font;
19501 struct face *face;
19502 struct font_info *font_info;
19503 XChar2b char2b;
19504 XCharStruct *pcm;
19505
19506 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
19507 font = face->font;
19508 font_info = FONT_INFO_FROM_FACE (f, face);
19509 if (font /* ++KFS: Should this be font_info ? */
19510 && (pcm = get_per_char_metric (f, font, font_info, &char2b, glyph->font_type)))
19511 {
19512 if (pcm->rbearing > pcm->width)
19513 *right = pcm->rbearing - pcm->width;
19514 if (pcm->lbearing < 0)
19515 *left = -pcm->lbearing;
19516 }
19517 }
19518 else if (glyph->type == COMPOSITE_GLYPH)
19519 {
19520 struct composition *cmp = composition_table[glyph->u.cmp_id];
19521
19522 *right = cmp->rbearing - cmp->pixel_width;
19523 *left = - cmp->lbearing;
19524 }
19525 }
19526
19527
19528 /* Return the index of the first glyph preceding glyph string S that
19529 is overwritten by S because of S's left overhang. Value is -1
19530 if no glyphs are overwritten. */
19531
19532 static int
19533 left_overwritten (s)
19534 struct glyph_string *s;
19535 {
19536 int k;
19537
19538 if (s->left_overhang)
19539 {
19540 int x = 0, i;
19541 struct glyph *glyphs = s->row->glyphs[s->area];
19542 int first = s->first_glyph - glyphs;
19543
19544 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
19545 x -= glyphs[i].pixel_width;
19546
19547 k = i + 1;
19548 }
19549 else
19550 k = -1;
19551
19552 return k;
19553 }
19554
19555
19556 /* Return the index of the first glyph preceding glyph string S that
19557 is overwriting S because of its right overhang. Value is -1 if no
19558 glyph in front of S overwrites S. */
19559
19560 static int
19561 left_overwriting (s)
19562 struct glyph_string *s;
19563 {
19564 int i, k, x;
19565 struct glyph *glyphs = s->row->glyphs[s->area];
19566 int first = s->first_glyph - glyphs;
19567
19568 k = -1;
19569 x = 0;
19570 for (i = first - 1; i >= 0; --i)
19571 {
19572 int left, right;
19573 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
19574 if (x + right > 0)
19575 k = i;
19576 x -= glyphs[i].pixel_width;
19577 }
19578
19579 return k;
19580 }
19581
19582
19583 /* Return the index of the last glyph following glyph string S that is
19584 not overwritten by S because of S's right overhang. Value is -1 if
19585 no such glyph is found. */
19586
19587 static int
19588 right_overwritten (s)
19589 struct glyph_string *s;
19590 {
19591 int k = -1;
19592
19593 if (s->right_overhang)
19594 {
19595 int x = 0, i;
19596 struct glyph *glyphs = s->row->glyphs[s->area];
19597 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
19598 int end = s->row->used[s->area];
19599
19600 for (i = first; i < end && s->right_overhang > x; ++i)
19601 x += glyphs[i].pixel_width;
19602
19603 k = i;
19604 }
19605
19606 return k;
19607 }
19608
19609
19610 /* Return the index of the last glyph following glyph string S that
19611 overwrites S because of its left overhang. Value is negative
19612 if no such glyph is found. */
19613
19614 static int
19615 right_overwriting (s)
19616 struct glyph_string *s;
19617 {
19618 int i, k, x;
19619 int end = s->row->used[s->area];
19620 struct glyph *glyphs = s->row->glyphs[s->area];
19621 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
19622
19623 k = -1;
19624 x = 0;
19625 for (i = first; i < end; ++i)
19626 {
19627 int left, right;
19628 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
19629 if (x - left < 0)
19630 k = i;
19631 x += glyphs[i].pixel_width;
19632 }
19633
19634 return k;
19635 }
19636
19637
19638 /* Set background width of glyph string S. START is the index of the
19639 first glyph following S. LAST_X is the right-most x-position + 1
19640 in the drawing area. */
19641
19642 static INLINE void
19643 set_glyph_string_background_width (s, start, last_x)
19644 struct glyph_string *s;
19645 int start;
19646 int last_x;
19647 {
19648 /* If the face of this glyph string has to be drawn to the end of
19649 the drawing area, set S->extends_to_end_of_line_p. */
19650
19651 if (start == s->row->used[s->area]
19652 && s->area == TEXT_AREA
19653 && ((s->row->fill_line_p
19654 && (s->hl == DRAW_NORMAL_TEXT
19655 || s->hl == DRAW_IMAGE_RAISED
19656 || s->hl == DRAW_IMAGE_SUNKEN))
19657 || s->hl == DRAW_MOUSE_FACE))
19658 s->extends_to_end_of_line_p = 1;
19659
19660 /* If S extends its face to the end of the line, set its
19661 background_width to the distance to the right edge of the drawing
19662 area. */
19663 if (s->extends_to_end_of_line_p)
19664 s->background_width = last_x - s->x + 1;
19665 else
19666 s->background_width = s->width;
19667 }
19668
19669
19670 /* Compute overhangs and x-positions for glyph string S and its
19671 predecessors, or successors. X is the starting x-position for S.
19672 BACKWARD_P non-zero means process predecessors. */
19673
19674 static void
19675 compute_overhangs_and_x (s, x, backward_p)
19676 struct glyph_string *s;
19677 int x;
19678 int backward_p;
19679 {
19680 if (backward_p)
19681 {
19682 while (s)
19683 {
19684 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
19685 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
19686 x -= s->width;
19687 s->x = x;
19688 s = s->prev;
19689 }
19690 }
19691 else
19692 {
19693 while (s)
19694 {
19695 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
19696 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
19697 s->x = x;
19698 x += s->width;
19699 s = s->next;
19700 }
19701 }
19702 }
19703
19704
19705
19706 /* The following macros are only called from draw_glyphs below.
19707 They reference the following parameters of that function directly:
19708 `w', `row', `area', and `overlap_p'
19709 as well as the following local variables:
19710 `s', `f', and `hdc' (in W32) */
19711
19712 #ifdef HAVE_NTGUI
19713 /* On W32, silently add local `hdc' variable to argument list of
19714 init_glyph_string. */
19715 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
19716 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
19717 #else
19718 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
19719 init_glyph_string (s, char2b, w, row, area, start, hl)
19720 #endif
19721
19722 /* Add a glyph string for a stretch glyph to the list of strings
19723 between HEAD and TAIL. START is the index of the stretch glyph in
19724 row area AREA of glyph row ROW. END is the index of the last glyph
19725 in that glyph row area. X is the current output position assigned
19726 to the new glyph string constructed. HL overrides that face of the
19727 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
19728 is the right-most x-position of the drawing area. */
19729
19730 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
19731 and below -- keep them on one line. */
19732 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
19733 do \
19734 { \
19735 s = (struct glyph_string *) alloca (sizeof *s); \
19736 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
19737 START = fill_stretch_glyph_string (s, row, area, START, END); \
19738 append_glyph_string (&HEAD, &TAIL, s); \
19739 s->x = (X); \
19740 } \
19741 while (0)
19742
19743
19744 /* Add a glyph string for an image glyph to the list of strings
19745 between HEAD and TAIL. START is the index of the image glyph in
19746 row area AREA of glyph row ROW. END is the index of the last glyph
19747 in that glyph row area. X is the current output position assigned
19748 to the new glyph string constructed. HL overrides that face of the
19749 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
19750 is the right-most x-position of the drawing area. */
19751
19752 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
19753 do \
19754 { \
19755 s = (struct glyph_string *) alloca (sizeof *s); \
19756 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
19757 fill_image_glyph_string (s); \
19758 append_glyph_string (&HEAD, &TAIL, s); \
19759 ++START; \
19760 s->x = (X); \
19761 } \
19762 while (0)
19763
19764
19765 /* Add a glyph string for a sequence of character glyphs to the list
19766 of strings between HEAD and TAIL. START is the index of the first
19767 glyph in row area AREA of glyph row ROW that is part of the new
19768 glyph string. END is the index of the last glyph in that glyph row
19769 area. X is the current output position assigned to the new glyph
19770 string constructed. HL overrides that face of the glyph; e.g. it
19771 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
19772 right-most x-position of the drawing area. */
19773
19774 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
19775 do \
19776 { \
19777 int face_id; \
19778 XChar2b *char2b; \
19779 \
19780 face_id = (row)->glyphs[area][START].face_id; \
19781 \
19782 s = (struct glyph_string *) alloca (sizeof *s); \
19783 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
19784 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
19785 append_glyph_string (&HEAD, &TAIL, s); \
19786 s->x = (X); \
19787 START = fill_glyph_string (s, face_id, START, END, overlaps); \
19788 } \
19789 while (0)
19790
19791
19792 /* Add a glyph string for a composite sequence to the list of strings
19793 between HEAD and TAIL. START is the index of the first glyph in
19794 row area AREA of glyph row ROW that is part of the new glyph
19795 string. END is the index of the last glyph in that glyph row area.
19796 X is the current output position assigned to the new glyph string
19797 constructed. HL overrides that face of the glyph; e.g. it is
19798 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
19799 x-position of the drawing area. */
19800
19801 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
19802 do { \
19803 int face_id = (row)->glyphs[area][START].face_id; \
19804 struct face *base_face = FACE_FROM_ID (f, face_id); \
19805 int cmp_id = (row)->glyphs[area][START].u.cmp_id; \
19806 struct composition *cmp = composition_table[cmp_id]; \
19807 XChar2b *char2b; \
19808 struct glyph_string *first_s; \
19809 int n; \
19810 \
19811 char2b = (XChar2b *) alloca ((sizeof *char2b) * cmp->glyph_len); \
19812 base_face = base_face->ascii_face; \
19813 \
19814 /* Make glyph_strings for each glyph sequence that is drawable by \
19815 the same face, and append them to HEAD/TAIL. */ \
19816 for (n = 0; n < cmp->glyph_len;) \
19817 { \
19818 s = (struct glyph_string *) alloca (sizeof *s); \
19819 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
19820 append_glyph_string (&(HEAD), &(TAIL), s); \
19821 s->cmp = cmp; \
19822 s->gidx = n; \
19823 s->x = (X); \
19824 if (n == 0) \
19825 first_s = s; \
19826 n = fill_composite_glyph_string (s, base_face, overlaps); \
19827 } \
19828 \
19829 ++START; \
19830 s = first_s; \
19831 } while (0)
19832
19833
19834 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
19835 of AREA of glyph row ROW on window W between indices START and END.
19836 HL overrides the face for drawing glyph strings, e.g. it is
19837 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
19838 x-positions of the drawing area.
19839
19840 This is an ugly monster macro construct because we must use alloca
19841 to allocate glyph strings (because draw_glyphs can be called
19842 asynchronously). */
19843
19844 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
19845 do \
19846 { \
19847 HEAD = TAIL = NULL; \
19848 while (START < END) \
19849 { \
19850 struct glyph *first_glyph = (row)->glyphs[area] + START; \
19851 switch (first_glyph->type) \
19852 { \
19853 case CHAR_GLYPH: \
19854 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
19855 HL, X, LAST_X); \
19856 break; \
19857 \
19858 case COMPOSITE_GLYPH: \
19859 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
19860 HL, X, LAST_X); \
19861 break; \
19862 \
19863 case STRETCH_GLYPH: \
19864 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
19865 HL, X, LAST_X); \
19866 break; \
19867 \
19868 case IMAGE_GLYPH: \
19869 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
19870 HL, X, LAST_X); \
19871 break; \
19872 \
19873 default: \
19874 abort (); \
19875 } \
19876 \
19877 if (s) \
19878 { \
19879 set_glyph_string_background_width (s, START, LAST_X); \
19880 (X) += s->width; \
19881 } \
19882 } \
19883 } \
19884 while (0)
19885
19886
19887 /* Draw glyphs between START and END in AREA of ROW on window W,
19888 starting at x-position X. X is relative to AREA in W. HL is a
19889 face-override with the following meaning:
19890
19891 DRAW_NORMAL_TEXT draw normally
19892 DRAW_CURSOR draw in cursor face
19893 DRAW_MOUSE_FACE draw in mouse face.
19894 DRAW_INVERSE_VIDEO draw in mode line face
19895 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
19896 DRAW_IMAGE_RAISED draw an image with a raised relief around it
19897
19898 If OVERLAPS is non-zero, draw only the foreground of characters and
19899 clip to the physical height of ROW. Non-zero value also defines
19900 the overlapping part to be drawn:
19901
19902 OVERLAPS_PRED overlap with preceding rows
19903 OVERLAPS_SUCC overlap with succeeding rows
19904 OVERLAPS_BOTH overlap with both preceding/succeeding rows
19905 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
19906
19907 Value is the x-position reached, relative to AREA of W. */
19908
19909 static int
19910 draw_glyphs (w, x, row, area, start, end, hl, overlaps)
19911 struct window *w;
19912 int x;
19913 struct glyph_row *row;
19914 enum glyph_row_area area;
19915 EMACS_INT start, end;
19916 enum draw_glyphs_face hl;
19917 int overlaps;
19918 {
19919 struct glyph_string *head, *tail;
19920 struct glyph_string *s;
19921 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
19922 int last_x, area_width;
19923 int x_reached;
19924 int i, j;
19925 struct frame *f = XFRAME (WINDOW_FRAME (w));
19926 DECLARE_HDC (hdc);
19927
19928 ALLOCATE_HDC (hdc, f);
19929
19930 /* Let's rather be paranoid than getting a SEGV. */
19931 end = min (end, row->used[area]);
19932 start = max (0, start);
19933 start = min (end, start);
19934
19935 /* Translate X to frame coordinates. Set last_x to the right
19936 end of the drawing area. */
19937 if (row->full_width_p)
19938 {
19939 /* X is relative to the left edge of W, without scroll bars
19940 or fringes. */
19941 x += WINDOW_LEFT_EDGE_X (w);
19942 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
19943 }
19944 else
19945 {
19946 int area_left = window_box_left (w, area);
19947 x += area_left;
19948 area_width = window_box_width (w, area);
19949 last_x = area_left + area_width;
19950 }
19951
19952 /* Build a doubly-linked list of glyph_string structures between
19953 head and tail from what we have to draw. Note that the macro
19954 BUILD_GLYPH_STRINGS will modify its start parameter. That's
19955 the reason we use a separate variable `i'. */
19956 i = start;
19957 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
19958 if (tail)
19959 x_reached = tail->x + tail->background_width;
19960 else
19961 x_reached = x;
19962
19963 /* If there are any glyphs with lbearing < 0 or rbearing > width in
19964 the row, redraw some glyphs in front or following the glyph
19965 strings built above. */
19966 if (head && !overlaps && row->contains_overlapping_glyphs_p)
19967 {
19968 int dummy_x = 0;
19969 struct glyph_string *h, *t;
19970
19971 /* Compute overhangs for all glyph strings. */
19972 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
19973 for (s = head; s; s = s->next)
19974 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
19975
19976 /* Prepend glyph strings for glyphs in front of the first glyph
19977 string that are overwritten because of the first glyph
19978 string's left overhang. The background of all strings
19979 prepended must be drawn because the first glyph string
19980 draws over it. */
19981 i = left_overwritten (head);
19982 if (i >= 0)
19983 {
19984 j = i;
19985 BUILD_GLYPH_STRINGS (j, start, h, t,
19986 DRAW_NORMAL_TEXT, dummy_x, last_x);
19987 start = i;
19988 compute_overhangs_and_x (t, head->x, 1);
19989 prepend_glyph_string_lists (&head, &tail, h, t);
19990 clip_head = head;
19991 }
19992
19993 /* Prepend glyph strings for glyphs in front of the first glyph
19994 string that overwrite that glyph string because of their
19995 right overhang. For these strings, only the foreground must
19996 be drawn, because it draws over the glyph string at `head'.
19997 The background must not be drawn because this would overwrite
19998 right overhangs of preceding glyphs for which no glyph
19999 strings exist. */
20000 i = left_overwriting (head);
20001 if (i >= 0)
20002 {
20003 clip_head = head;
20004 BUILD_GLYPH_STRINGS (i, start, h, t,
20005 DRAW_NORMAL_TEXT, dummy_x, last_x);
20006 for (s = h; s; s = s->next)
20007 s->background_filled_p = 1;
20008 compute_overhangs_and_x (t, head->x, 1);
20009 prepend_glyph_string_lists (&head, &tail, h, t);
20010 }
20011
20012 /* Append glyphs strings for glyphs following the last glyph
20013 string tail that are overwritten by tail. The background of
20014 these strings has to be drawn because tail's foreground draws
20015 over it. */
20016 i = right_overwritten (tail);
20017 if (i >= 0)
20018 {
20019 BUILD_GLYPH_STRINGS (end, i, h, t,
20020 DRAW_NORMAL_TEXT, x, last_x);
20021 compute_overhangs_and_x (h, tail->x + tail->width, 0);
20022 append_glyph_string_lists (&head, &tail, h, t);
20023 clip_tail = tail;
20024 }
20025
20026 /* Append glyph strings for glyphs following the last glyph
20027 string tail that overwrite tail. The foreground of such
20028 glyphs has to be drawn because it writes into the background
20029 of tail. The background must not be drawn because it could
20030 paint over the foreground of following glyphs. */
20031 i = right_overwriting (tail);
20032 if (i >= 0)
20033 {
20034 clip_tail = tail;
20035 i++; /* We must include the Ith glyph. */
20036 BUILD_GLYPH_STRINGS (end, i, h, t,
20037 DRAW_NORMAL_TEXT, x, last_x);
20038 for (s = h; s; s = s->next)
20039 s->background_filled_p = 1;
20040 compute_overhangs_and_x (h, tail->x + tail->width, 0);
20041 append_glyph_string_lists (&head, &tail, h, t);
20042 }
20043 if (clip_head || clip_tail)
20044 for (s = head; s; s = s->next)
20045 {
20046 s->clip_head = clip_head;
20047 s->clip_tail = clip_tail;
20048 }
20049 }
20050
20051 /* Draw all strings. */
20052 for (s = head; s; s = s->next)
20053 FRAME_RIF (f)->draw_glyph_string (s);
20054
20055 if (area == TEXT_AREA
20056 && !row->full_width_p
20057 /* When drawing overlapping rows, only the glyph strings'
20058 foreground is drawn, which doesn't erase a cursor
20059 completely. */
20060 && !overlaps)
20061 {
20062 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
20063 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
20064 : (tail ? tail->x + tail->background_width : x));
20065
20066 int text_left = window_box_left (w, TEXT_AREA);
20067 x0 -= text_left;
20068 x1 -= text_left;
20069
20070 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
20071 row->y, MATRIX_ROW_BOTTOM_Y (row));
20072 }
20073
20074 /* Value is the x-position up to which drawn, relative to AREA of W.
20075 This doesn't include parts drawn because of overhangs. */
20076 if (row->full_width_p)
20077 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
20078 else
20079 x_reached -= window_box_left (w, area);
20080
20081 RELEASE_HDC (hdc, f);
20082
20083 return x_reached;
20084 }
20085
20086 /* Expand row matrix if too narrow. Don't expand if area
20087 is not present. */
20088
20089 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
20090 { \
20091 if (!fonts_changed_p \
20092 && (it->glyph_row->glyphs[area] \
20093 < it->glyph_row->glyphs[area + 1])) \
20094 { \
20095 it->w->ncols_scale_factor++; \
20096 fonts_changed_p = 1; \
20097 } \
20098 }
20099
20100 /* Store one glyph for IT->char_to_display in IT->glyph_row.
20101 Called from x_produce_glyphs when IT->glyph_row is non-null. */
20102
20103 static INLINE void
20104 append_glyph (it)
20105 struct it *it;
20106 {
20107 struct glyph *glyph;
20108 enum glyph_row_area area = it->area;
20109
20110 xassert (it->glyph_row);
20111 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
20112
20113 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
20114 if (glyph < it->glyph_row->glyphs[area + 1])
20115 {
20116 glyph->charpos = CHARPOS (it->position);
20117 glyph->object = it->object;
20118 glyph->pixel_width = it->pixel_width;
20119 glyph->ascent = it->ascent;
20120 glyph->descent = it->descent;
20121 glyph->voffset = it->voffset;
20122 glyph->type = CHAR_GLYPH;
20123 glyph->multibyte_p = it->multibyte_p;
20124 glyph->left_box_line_p = it->start_of_box_run_p;
20125 glyph->right_box_line_p = it->end_of_box_run_p;
20126 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
20127 || it->phys_descent > it->descent);
20128 glyph->padding_p = 0;
20129 glyph->glyph_not_available_p = it->glyph_not_available_p;
20130 glyph->face_id = it->face_id;
20131 glyph->u.ch = it->char_to_display;
20132 glyph->slice = null_glyph_slice;
20133 glyph->font_type = FONT_TYPE_UNKNOWN;
20134 ++it->glyph_row->used[area];
20135 }
20136 else
20137 IT_EXPAND_MATRIX_WIDTH (it, area);
20138 }
20139
20140 /* Store one glyph for the composition IT->cmp_id in IT->glyph_row.
20141 Called from x_produce_glyphs when IT->glyph_row is non-null. */
20142
20143 static INLINE void
20144 append_composite_glyph (it)
20145 struct it *it;
20146 {
20147 struct glyph *glyph;
20148 enum glyph_row_area area = it->area;
20149
20150 xassert (it->glyph_row);
20151
20152 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
20153 if (glyph < it->glyph_row->glyphs[area + 1])
20154 {
20155 glyph->charpos = CHARPOS (it->position);
20156 glyph->object = it->object;
20157 glyph->pixel_width = it->pixel_width;
20158 glyph->ascent = it->ascent;
20159 glyph->descent = it->descent;
20160 glyph->voffset = it->voffset;
20161 glyph->type = COMPOSITE_GLYPH;
20162 glyph->multibyte_p = it->multibyte_p;
20163 glyph->left_box_line_p = it->start_of_box_run_p;
20164 glyph->right_box_line_p = it->end_of_box_run_p;
20165 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
20166 || it->phys_descent > it->descent);
20167 glyph->padding_p = 0;
20168 glyph->glyph_not_available_p = 0;
20169 glyph->face_id = it->face_id;
20170 glyph->u.cmp_id = it->cmp_id;
20171 glyph->slice = null_glyph_slice;
20172 glyph->font_type = FONT_TYPE_UNKNOWN;
20173 ++it->glyph_row->used[area];
20174 }
20175 else
20176 IT_EXPAND_MATRIX_WIDTH (it, area);
20177 }
20178
20179
20180 /* Change IT->ascent and IT->height according to the setting of
20181 IT->voffset. */
20182
20183 static INLINE void
20184 take_vertical_position_into_account (it)
20185 struct it *it;
20186 {
20187 if (it->voffset)
20188 {
20189 if (it->voffset < 0)
20190 /* Increase the ascent so that we can display the text higher
20191 in the line. */
20192 it->ascent -= it->voffset;
20193 else
20194 /* Increase the descent so that we can display the text lower
20195 in the line. */
20196 it->descent += it->voffset;
20197 }
20198 }
20199
20200
20201 /* Produce glyphs/get display metrics for the image IT is loaded with.
20202 See the description of struct display_iterator in dispextern.h for
20203 an overview of struct display_iterator. */
20204
20205 static void
20206 produce_image_glyph (it)
20207 struct it *it;
20208 {
20209 struct image *img;
20210 struct face *face;
20211 int glyph_ascent, crop;
20212 struct glyph_slice slice;
20213
20214 xassert (it->what == IT_IMAGE);
20215
20216 face = FACE_FROM_ID (it->f, it->face_id);
20217 xassert (face);
20218 /* Make sure X resources of the face is loaded. */
20219 PREPARE_FACE_FOR_DISPLAY (it->f, face);
20220
20221 if (it->image_id < 0)
20222 {
20223 /* Fringe bitmap. */
20224 it->ascent = it->phys_ascent = 0;
20225 it->descent = it->phys_descent = 0;
20226 it->pixel_width = 0;
20227 it->nglyphs = 0;
20228 return;
20229 }
20230
20231 img = IMAGE_FROM_ID (it->f, it->image_id);
20232 xassert (img);
20233 /* Make sure X resources of the image is loaded. */
20234 prepare_image_for_display (it->f, img);
20235
20236 slice.x = slice.y = 0;
20237 slice.width = img->width;
20238 slice.height = img->height;
20239
20240 if (INTEGERP (it->slice.x))
20241 slice.x = XINT (it->slice.x);
20242 else if (FLOATP (it->slice.x))
20243 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
20244
20245 if (INTEGERP (it->slice.y))
20246 slice.y = XINT (it->slice.y);
20247 else if (FLOATP (it->slice.y))
20248 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
20249
20250 if (INTEGERP (it->slice.width))
20251 slice.width = XINT (it->slice.width);
20252 else if (FLOATP (it->slice.width))
20253 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
20254
20255 if (INTEGERP (it->slice.height))
20256 slice.height = XINT (it->slice.height);
20257 else if (FLOATP (it->slice.height))
20258 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
20259
20260 if (slice.x >= img->width)
20261 slice.x = img->width;
20262 if (slice.y >= img->height)
20263 slice.y = img->height;
20264 if (slice.x + slice.width >= img->width)
20265 slice.width = img->width - slice.x;
20266 if (slice.y + slice.height > img->height)
20267 slice.height = img->height - slice.y;
20268
20269 if (slice.width == 0 || slice.height == 0)
20270 return;
20271
20272 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
20273
20274 it->descent = slice.height - glyph_ascent;
20275 if (slice.y == 0)
20276 it->descent += img->vmargin;
20277 if (slice.y + slice.height == img->height)
20278 it->descent += img->vmargin;
20279 it->phys_descent = it->descent;
20280
20281 it->pixel_width = slice.width;
20282 if (slice.x == 0)
20283 it->pixel_width += img->hmargin;
20284 if (slice.x + slice.width == img->width)
20285 it->pixel_width += img->hmargin;
20286
20287 /* It's quite possible for images to have an ascent greater than
20288 their height, so don't get confused in that case. */
20289 if (it->descent < 0)
20290 it->descent = 0;
20291
20292 #if 0 /* this breaks image tiling */
20293 /* If this glyph is alone on the last line, adjust it.ascent to minimum row ascent. */
20294 int face_ascent = face->font ? FONT_BASE (face->font) : FRAME_BASELINE_OFFSET (it->f);
20295 if (face_ascent > it->ascent)
20296 it->ascent = it->phys_ascent = face_ascent;
20297 #endif
20298
20299 it->nglyphs = 1;
20300
20301 if (face->box != FACE_NO_BOX)
20302 {
20303 if (face->box_line_width > 0)
20304 {
20305 if (slice.y == 0)
20306 it->ascent += face->box_line_width;
20307 if (slice.y + slice.height == img->height)
20308 it->descent += face->box_line_width;
20309 }
20310
20311 if (it->start_of_box_run_p && slice.x == 0)
20312 it->pixel_width += eabs (face->box_line_width);
20313 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
20314 it->pixel_width += eabs (face->box_line_width);
20315 }
20316
20317 take_vertical_position_into_account (it);
20318
20319 /* Automatically crop wide image glyphs at right edge so we can
20320 draw the cursor on same display row. */
20321 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
20322 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
20323 {
20324 it->pixel_width -= crop;
20325 slice.width -= crop;
20326 }
20327
20328 if (it->glyph_row)
20329 {
20330 struct glyph *glyph;
20331 enum glyph_row_area area = it->area;
20332
20333 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
20334 if (glyph < it->glyph_row->glyphs[area + 1])
20335 {
20336 glyph->charpos = CHARPOS (it->position);
20337 glyph->object = it->object;
20338 glyph->pixel_width = it->pixel_width;
20339 glyph->ascent = glyph_ascent;
20340 glyph->descent = it->descent;
20341 glyph->voffset = it->voffset;
20342 glyph->type = IMAGE_GLYPH;
20343 glyph->multibyte_p = it->multibyte_p;
20344 glyph->left_box_line_p = it->start_of_box_run_p;
20345 glyph->right_box_line_p = it->end_of_box_run_p;
20346 glyph->overlaps_vertically_p = 0;
20347 glyph->padding_p = 0;
20348 glyph->glyph_not_available_p = 0;
20349 glyph->face_id = it->face_id;
20350 glyph->u.img_id = img->id;
20351 glyph->slice = slice;
20352 glyph->font_type = FONT_TYPE_UNKNOWN;
20353 ++it->glyph_row->used[area];
20354 }
20355 else
20356 IT_EXPAND_MATRIX_WIDTH (it, area);
20357 }
20358 }
20359
20360
20361 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
20362 of the glyph, WIDTH and HEIGHT are the width and height of the
20363 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
20364
20365 static void
20366 append_stretch_glyph (it, object, width, height, ascent)
20367 struct it *it;
20368 Lisp_Object object;
20369 int width, height;
20370 int ascent;
20371 {
20372 struct glyph *glyph;
20373 enum glyph_row_area area = it->area;
20374
20375 xassert (ascent >= 0 && ascent <= height);
20376
20377 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
20378 if (glyph < it->glyph_row->glyphs[area + 1])
20379 {
20380 glyph->charpos = CHARPOS (it->position);
20381 glyph->object = object;
20382 glyph->pixel_width = width;
20383 glyph->ascent = ascent;
20384 glyph->descent = height - ascent;
20385 glyph->voffset = it->voffset;
20386 glyph->type = STRETCH_GLYPH;
20387 glyph->multibyte_p = it->multibyte_p;
20388 glyph->left_box_line_p = it->start_of_box_run_p;
20389 glyph->right_box_line_p = it->end_of_box_run_p;
20390 glyph->overlaps_vertically_p = 0;
20391 glyph->padding_p = 0;
20392 glyph->glyph_not_available_p = 0;
20393 glyph->face_id = it->face_id;
20394 glyph->u.stretch.ascent = ascent;
20395 glyph->u.stretch.height = height;
20396 glyph->slice = null_glyph_slice;
20397 glyph->font_type = FONT_TYPE_UNKNOWN;
20398 ++it->glyph_row->used[area];
20399 }
20400 else
20401 IT_EXPAND_MATRIX_WIDTH (it, area);
20402 }
20403
20404
20405 /* Produce a stretch glyph for iterator IT. IT->object is the value
20406 of the glyph property displayed. The value must be a list
20407 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
20408 being recognized:
20409
20410 1. `:width WIDTH' specifies that the space should be WIDTH *
20411 canonical char width wide. WIDTH may be an integer or floating
20412 point number.
20413
20414 2. `:relative-width FACTOR' specifies that the width of the stretch
20415 should be computed from the width of the first character having the
20416 `glyph' property, and should be FACTOR times that width.
20417
20418 3. `:align-to HPOS' specifies that the space should be wide enough
20419 to reach HPOS, a value in canonical character units.
20420
20421 Exactly one of the above pairs must be present.
20422
20423 4. `:height HEIGHT' specifies that the height of the stretch produced
20424 should be HEIGHT, measured in canonical character units.
20425
20426 5. `:relative-height FACTOR' specifies that the height of the
20427 stretch should be FACTOR times the height of the characters having
20428 the glyph property.
20429
20430 Either none or exactly one of 4 or 5 must be present.
20431
20432 6. `:ascent ASCENT' specifies that ASCENT percent of the height
20433 of the stretch should be used for the ascent of the stretch.
20434 ASCENT must be in the range 0 <= ASCENT <= 100. */
20435
20436 static void
20437 produce_stretch_glyph (it)
20438 struct it *it;
20439 {
20440 /* (space :width WIDTH :height HEIGHT ...) */
20441 Lisp_Object prop, plist;
20442 int width = 0, height = 0, align_to = -1;
20443 int zero_width_ok_p = 0, zero_height_ok_p = 0;
20444 int ascent = 0;
20445 double tem;
20446 struct face *face = FACE_FROM_ID (it->f, it->face_id);
20447 XFontStruct *font = face->font ? face->font : FRAME_FONT (it->f);
20448
20449 PREPARE_FACE_FOR_DISPLAY (it->f, face);
20450
20451 /* List should start with `space'. */
20452 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
20453 plist = XCDR (it->object);
20454
20455 /* Compute the width of the stretch. */
20456 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
20457 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
20458 {
20459 /* Absolute width `:width WIDTH' specified and valid. */
20460 zero_width_ok_p = 1;
20461 width = (int)tem;
20462 }
20463 else if (prop = Fplist_get (plist, QCrelative_width),
20464 NUMVAL (prop) > 0)
20465 {
20466 /* Relative width `:relative-width FACTOR' specified and valid.
20467 Compute the width of the characters having the `glyph'
20468 property. */
20469 struct it it2;
20470 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
20471
20472 it2 = *it;
20473 if (it->multibyte_p)
20474 {
20475 int maxlen = ((IT_BYTEPOS (*it) >= GPT ? ZV : GPT)
20476 - IT_BYTEPOS (*it));
20477 it2.c = STRING_CHAR_AND_LENGTH (p, maxlen, it2.len);
20478 }
20479 else
20480 it2.c = *p, it2.len = 1;
20481
20482 it2.glyph_row = NULL;
20483 it2.what = IT_CHARACTER;
20484 x_produce_glyphs (&it2);
20485 width = NUMVAL (prop) * it2.pixel_width;
20486 }
20487 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
20488 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
20489 {
20490 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
20491 align_to = (align_to < 0
20492 ? 0
20493 : align_to - window_box_left_offset (it->w, TEXT_AREA));
20494 else if (align_to < 0)
20495 align_to = window_box_left_offset (it->w, TEXT_AREA);
20496 width = max (0, (int)tem + align_to - it->current_x);
20497 zero_width_ok_p = 1;
20498 }
20499 else
20500 /* Nothing specified -> width defaults to canonical char width. */
20501 width = FRAME_COLUMN_WIDTH (it->f);
20502
20503 if (width <= 0 && (width < 0 || !zero_width_ok_p))
20504 width = 1;
20505
20506 /* Compute height. */
20507 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
20508 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
20509 {
20510 height = (int)tem;
20511 zero_height_ok_p = 1;
20512 }
20513 else if (prop = Fplist_get (plist, QCrelative_height),
20514 NUMVAL (prop) > 0)
20515 height = FONT_HEIGHT (font) * NUMVAL (prop);
20516 else
20517 height = FONT_HEIGHT (font);
20518
20519 if (height <= 0 && (height < 0 || !zero_height_ok_p))
20520 height = 1;
20521
20522 /* Compute percentage of height used for ascent. If
20523 `:ascent ASCENT' is present and valid, use that. Otherwise,
20524 derive the ascent from the font in use. */
20525 if (prop = Fplist_get (plist, QCascent),
20526 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
20527 ascent = height * NUMVAL (prop) / 100.0;
20528 else if (!NILP (prop)
20529 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
20530 ascent = min (max (0, (int)tem), height);
20531 else
20532 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
20533
20534 if (width > 0 && !it->truncate_lines_p
20535 && it->current_x + width > it->last_visible_x)
20536 width = it->last_visible_x - it->current_x - 1;
20537
20538 if (width > 0 && height > 0 && it->glyph_row)
20539 {
20540 Lisp_Object object = it->stack[it->sp - 1].string;
20541 if (!STRINGP (object))
20542 object = it->w->buffer;
20543 append_stretch_glyph (it, object, width, height, ascent);
20544 }
20545
20546 it->pixel_width = width;
20547 it->ascent = it->phys_ascent = ascent;
20548 it->descent = it->phys_descent = height - it->ascent;
20549 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
20550
20551 take_vertical_position_into_account (it);
20552 }
20553
20554 /* Get line-height and line-spacing property at point.
20555 If line-height has format (HEIGHT TOTAL), return TOTAL
20556 in TOTAL_HEIGHT. */
20557
20558 static Lisp_Object
20559 get_line_height_property (it, prop)
20560 struct it *it;
20561 Lisp_Object prop;
20562 {
20563 Lisp_Object position;
20564
20565 if (STRINGP (it->object))
20566 position = make_number (IT_STRING_CHARPOS (*it));
20567 else if (BUFFERP (it->object))
20568 position = make_number (IT_CHARPOS (*it));
20569 else
20570 return Qnil;
20571
20572 return Fget_char_property (position, prop, it->object);
20573 }
20574
20575 /* Calculate line-height and line-spacing properties.
20576 An integer value specifies explicit pixel value.
20577 A float value specifies relative value to current face height.
20578 A cons (float . face-name) specifies relative value to
20579 height of specified face font.
20580
20581 Returns height in pixels, or nil. */
20582
20583
20584 static Lisp_Object
20585 calc_line_height_property (it, val, font, boff, override)
20586 struct it *it;
20587 Lisp_Object val;
20588 XFontStruct *font;
20589 int boff, override;
20590 {
20591 Lisp_Object face_name = Qnil;
20592 int ascent, descent, height;
20593
20594 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
20595 return val;
20596
20597 if (CONSP (val))
20598 {
20599 face_name = XCAR (val);
20600 val = XCDR (val);
20601 if (!NUMBERP (val))
20602 val = make_number (1);
20603 if (NILP (face_name))
20604 {
20605 height = it->ascent + it->descent;
20606 goto scale;
20607 }
20608 }
20609
20610 if (NILP (face_name))
20611 {
20612 font = FRAME_FONT (it->f);
20613 boff = FRAME_BASELINE_OFFSET (it->f);
20614 }
20615 else if (EQ (face_name, Qt))
20616 {
20617 override = 0;
20618 }
20619 else
20620 {
20621 int face_id;
20622 struct face *face;
20623 struct font_info *font_info;
20624
20625 face_id = lookup_named_face (it->f, face_name, 0);
20626 if (face_id < 0)
20627 return make_number (-1);
20628
20629 face = FACE_FROM_ID (it->f, face_id);
20630 font = face->font;
20631 if (font == NULL)
20632 return make_number (-1);
20633
20634 font_info = FONT_INFO_FROM_FACE (it->f, face);
20635 boff = font_info->baseline_offset;
20636 if (font_info->vertical_centering)
20637 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
20638 }
20639
20640 ascent = FONT_BASE (font) + boff;
20641 descent = FONT_DESCENT (font) - boff;
20642
20643 if (override)
20644 {
20645 it->override_ascent = ascent;
20646 it->override_descent = descent;
20647 it->override_boff = boff;
20648 }
20649
20650 height = ascent + descent;
20651
20652 scale:
20653 if (FLOATP (val))
20654 height = (int)(XFLOAT_DATA (val) * height);
20655 else if (INTEGERP (val))
20656 height *= XINT (val);
20657
20658 return make_number (height);
20659 }
20660
20661
20662 /* RIF:
20663 Produce glyphs/get display metrics for the display element IT is
20664 loaded with. See the description of struct it in dispextern.h
20665 for an overview of struct it. */
20666
20667 void
20668 x_produce_glyphs (it)
20669 struct it *it;
20670 {
20671 int extra_line_spacing = it->extra_line_spacing;
20672
20673 it->glyph_not_available_p = 0;
20674
20675 if (it->what == IT_CHARACTER)
20676 {
20677 XChar2b char2b;
20678 XFontStruct *font;
20679 struct face *face = FACE_FROM_ID (it->f, it->face_id);
20680 XCharStruct *pcm;
20681 int font_not_found_p;
20682 struct font_info *font_info;
20683 int boff; /* baseline offset */
20684 /* We may change it->multibyte_p upon unibyte<->multibyte
20685 conversion. So, save the current value now and restore it
20686 later.
20687
20688 Note: It seems that we don't have to record multibyte_p in
20689 struct glyph because the character code itself tells if or
20690 not the character is multibyte. Thus, in the future, we must
20691 consider eliminating the field `multibyte_p' in the struct
20692 glyph. */
20693 int saved_multibyte_p = it->multibyte_p;
20694
20695 /* Maybe translate single-byte characters to multibyte, or the
20696 other way. */
20697 it->char_to_display = it->c;
20698 if (!ASCII_BYTE_P (it->c)
20699 && ! it->multibyte_p)
20700 {
20701 if (SINGLE_BYTE_CHAR_P (it->c)
20702 && unibyte_display_via_language_environment)
20703 it->char_to_display = unibyte_char_to_multibyte (it->c);
20704 if (! SINGLE_BYTE_CHAR_P (it->char_to_display))
20705 {
20706 it->multibyte_p = 1;
20707 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display,
20708 -1, Qnil);
20709 face = FACE_FROM_ID (it->f, it->face_id);
20710 }
20711 }
20712
20713 /* Get font to use. Encode IT->char_to_display. */
20714 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
20715 &char2b, it->multibyte_p, 0);
20716 font = face->font;
20717
20718 /* When no suitable font found, use the default font. */
20719 font_not_found_p = font == NULL;
20720 if (font_not_found_p)
20721 {
20722 font = FRAME_FONT (it->f);
20723 boff = FRAME_BASELINE_OFFSET (it->f);
20724 font_info = NULL;
20725 }
20726 else
20727 {
20728 font_info = FONT_INFO_FROM_FACE (it->f, face);
20729 boff = font_info->baseline_offset;
20730 if (font_info->vertical_centering)
20731 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
20732 }
20733
20734 if (it->char_to_display >= ' '
20735 && (!it->multibyte_p || it->char_to_display < 128))
20736 {
20737 /* Either unibyte or ASCII. */
20738 int stretched_p;
20739
20740 it->nglyphs = 1;
20741
20742 pcm = get_per_char_metric (it->f, font, font_info, &char2b,
20743 FONT_TYPE_FOR_UNIBYTE (font, it->char_to_display));
20744
20745 if (it->override_ascent >= 0)
20746 {
20747 it->ascent = it->override_ascent;
20748 it->descent = it->override_descent;
20749 boff = it->override_boff;
20750 }
20751 else
20752 {
20753 it->ascent = FONT_BASE (font) + boff;
20754 it->descent = FONT_DESCENT (font) - boff;
20755 }
20756
20757 if (pcm)
20758 {
20759 it->phys_ascent = pcm->ascent + boff;
20760 it->phys_descent = pcm->descent - boff;
20761 it->pixel_width = pcm->width;
20762 }
20763 else
20764 {
20765 it->glyph_not_available_p = 1;
20766 it->phys_ascent = it->ascent;
20767 it->phys_descent = it->descent;
20768 it->pixel_width = FONT_WIDTH (font);
20769 }
20770
20771 if (it->constrain_row_ascent_descent_p)
20772 {
20773 if (it->descent > it->max_descent)
20774 {
20775 it->ascent += it->descent - it->max_descent;
20776 it->descent = it->max_descent;
20777 }
20778 if (it->ascent > it->max_ascent)
20779 {
20780 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
20781 it->ascent = it->max_ascent;
20782 }
20783 it->phys_ascent = min (it->phys_ascent, it->ascent);
20784 it->phys_descent = min (it->phys_descent, it->descent);
20785 extra_line_spacing = 0;
20786 }
20787
20788 /* If this is a space inside a region of text with
20789 `space-width' property, change its width. */
20790 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
20791 if (stretched_p)
20792 it->pixel_width *= XFLOATINT (it->space_width);
20793
20794 /* If face has a box, add the box thickness to the character
20795 height. If character has a box line to the left and/or
20796 right, add the box line width to the character's width. */
20797 if (face->box != FACE_NO_BOX)
20798 {
20799 int thick = face->box_line_width;
20800
20801 if (thick > 0)
20802 {
20803 it->ascent += thick;
20804 it->descent += thick;
20805 }
20806 else
20807 thick = -thick;
20808
20809 if (it->start_of_box_run_p)
20810 it->pixel_width += thick;
20811 if (it->end_of_box_run_p)
20812 it->pixel_width += thick;
20813 }
20814
20815 /* If face has an overline, add the height of the overline
20816 (1 pixel) and a 1 pixel margin to the character height. */
20817 if (face->overline_p)
20818 it->ascent += overline_margin;
20819
20820 if (it->constrain_row_ascent_descent_p)
20821 {
20822 if (it->ascent > it->max_ascent)
20823 it->ascent = it->max_ascent;
20824 if (it->descent > it->max_descent)
20825 it->descent = it->max_descent;
20826 }
20827
20828 take_vertical_position_into_account (it);
20829
20830 /* If we have to actually produce glyphs, do it. */
20831 if (it->glyph_row)
20832 {
20833 if (stretched_p)
20834 {
20835 /* Translate a space with a `space-width' property
20836 into a stretch glyph. */
20837 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
20838 / FONT_HEIGHT (font));
20839 append_stretch_glyph (it, it->object, it->pixel_width,
20840 it->ascent + it->descent, ascent);
20841 }
20842 else
20843 append_glyph (it);
20844
20845 /* If characters with lbearing or rbearing are displayed
20846 in this line, record that fact in a flag of the
20847 glyph row. This is used to optimize X output code. */
20848 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
20849 it->glyph_row->contains_overlapping_glyphs_p = 1;
20850 }
20851 }
20852 else if (it->char_to_display == '\n')
20853 {
20854 /* A newline has no width but we need the height of the line.
20855 But if previous part of the line set a height, don't
20856 increase that height */
20857
20858 Lisp_Object height;
20859 Lisp_Object total_height = Qnil;
20860
20861 it->override_ascent = -1;
20862 it->pixel_width = 0;
20863 it->nglyphs = 0;
20864
20865 height = get_line_height_property(it, Qline_height);
20866 /* Split (line-height total-height) list */
20867 if (CONSP (height)
20868 && CONSP (XCDR (height))
20869 && NILP (XCDR (XCDR (height))))
20870 {
20871 total_height = XCAR (XCDR (height));
20872 height = XCAR (height);
20873 }
20874 height = calc_line_height_property(it, height, font, boff, 1);
20875
20876 if (it->override_ascent >= 0)
20877 {
20878 it->ascent = it->override_ascent;
20879 it->descent = it->override_descent;
20880 boff = it->override_boff;
20881 }
20882 else
20883 {
20884 it->ascent = FONT_BASE (font) + boff;
20885 it->descent = FONT_DESCENT (font) - boff;
20886 }
20887
20888 if (EQ (height, Qt))
20889 {
20890 if (it->descent > it->max_descent)
20891 {
20892 it->ascent += it->descent - it->max_descent;
20893 it->descent = it->max_descent;
20894 }
20895 if (it->ascent > it->max_ascent)
20896 {
20897 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
20898 it->ascent = it->max_ascent;
20899 }
20900 it->phys_ascent = min (it->phys_ascent, it->ascent);
20901 it->phys_descent = min (it->phys_descent, it->descent);
20902 it->constrain_row_ascent_descent_p = 1;
20903 extra_line_spacing = 0;
20904 }
20905 else
20906 {
20907 Lisp_Object spacing;
20908
20909 it->phys_ascent = it->ascent;
20910 it->phys_descent = it->descent;
20911
20912 if ((it->max_ascent > 0 || it->max_descent > 0)
20913 && face->box != FACE_NO_BOX
20914 && face->box_line_width > 0)
20915 {
20916 it->ascent += face->box_line_width;
20917 it->descent += face->box_line_width;
20918 }
20919 if (!NILP (height)
20920 && XINT (height) > it->ascent + it->descent)
20921 it->ascent = XINT (height) - it->descent;
20922
20923 if (!NILP (total_height))
20924 spacing = calc_line_height_property(it, total_height, font, boff, 0);
20925 else
20926 {
20927 spacing = get_line_height_property(it, Qline_spacing);
20928 spacing = calc_line_height_property(it, spacing, font, boff, 0);
20929 }
20930 if (INTEGERP (spacing))
20931 {
20932 extra_line_spacing = XINT (spacing);
20933 if (!NILP (total_height))
20934 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
20935 }
20936 }
20937 }
20938 else if (it->char_to_display == '\t')
20939 {
20940 int tab_width = it->tab_width * FRAME_SPACE_WIDTH (it->f);
20941 int x = it->current_x + it->continuation_lines_width;
20942 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
20943
20944 /* If the distance from the current position to the next tab
20945 stop is less than a space character width, use the
20946 tab stop after that. */
20947 if (next_tab_x - x < FRAME_SPACE_WIDTH (it->f))
20948 next_tab_x += tab_width;
20949
20950 it->pixel_width = next_tab_x - x;
20951 it->nglyphs = 1;
20952 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
20953 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
20954
20955 if (it->glyph_row)
20956 {
20957 append_stretch_glyph (it, it->object, it->pixel_width,
20958 it->ascent + it->descent, it->ascent);
20959 }
20960 }
20961 else
20962 {
20963 /* A multi-byte character. Assume that the display width of the
20964 character is the width of the character multiplied by the
20965 width of the font. */
20966
20967 /* If we found a font, this font should give us the right
20968 metrics. If we didn't find a font, use the frame's
20969 default font and calculate the width of the character by
20970 multiplying the width of font by the width of the
20971 character. */
20972
20973 pcm = get_per_char_metric (it->f, font, font_info, &char2b,
20974 FONT_TYPE_FOR_MULTIBYTE (font, it->c));
20975
20976 if (font_not_found_p || !pcm)
20977 {
20978 int char_width = CHAR_WIDTH (it->char_to_display);
20979
20980 if (char_width == 0)
20981 /* This is a non spacing character. But, as we are
20982 going to display an empty box, the box must occupy
20983 at least one column. */
20984 char_width = 1;
20985 it->glyph_not_available_p = 1;
20986 it->pixel_width = FRAME_COLUMN_WIDTH (it->f) * char_width;
20987 it->phys_ascent = FONT_BASE (font) + boff;
20988 it->phys_descent = FONT_DESCENT (font) - boff;
20989 }
20990 else
20991 {
20992 it->pixel_width = pcm->width;
20993 it->phys_ascent = pcm->ascent + boff;
20994 it->phys_descent = pcm->descent - boff;
20995 if (it->glyph_row
20996 && (pcm->lbearing < 0
20997 || pcm->rbearing > pcm->width))
20998 it->glyph_row->contains_overlapping_glyphs_p = 1;
20999 }
21000 it->nglyphs = 1;
21001 it->ascent = FONT_BASE (font) + boff;
21002 it->descent = FONT_DESCENT (font) - boff;
21003 if (face->box != FACE_NO_BOX)
21004 {
21005 int thick = face->box_line_width;
21006
21007 if (thick > 0)
21008 {
21009 it->ascent += thick;
21010 it->descent += thick;
21011 }
21012 else
21013 thick = - thick;
21014
21015 if (it->start_of_box_run_p)
21016 it->pixel_width += thick;
21017 if (it->end_of_box_run_p)
21018 it->pixel_width += thick;
21019 }
21020
21021 /* If face has an overline, add the height of the overline
21022 (1 pixel) and a 1 pixel margin to the character height. */
21023 if (face->overline_p)
21024 it->ascent += overline_margin;
21025
21026 take_vertical_position_into_account (it);
21027
21028 if (it->glyph_row)
21029 append_glyph (it);
21030 }
21031 it->multibyte_p = saved_multibyte_p;
21032 }
21033 else if (it->what == IT_COMPOSITION)
21034 {
21035 /* Note: A composition is represented as one glyph in the
21036 glyph matrix. There are no padding glyphs.
21037
21038 Important is that pixel_width, ascent, and descent are the
21039 values of what is drawn by draw_glyphs (i.e. the values of
21040 the overall glyphs composed). */
21041 struct face *face = FACE_FROM_ID (it->f, it->face_id);
21042 int boff; /* baseline offset */
21043 struct composition *cmp = composition_table[it->cmp_id];
21044 int glyph_len = cmp->glyph_len;
21045 XFontStruct *font = face->font;
21046
21047 it->nglyphs = 1;
21048
21049 #ifdef USE_FONT_BACKEND
21050 if (cmp->method == COMPOSITION_WITH_GLYPH_STRING)
21051 {
21052 if (! cmp->font || cmp->font != font)
21053 font_prepare_composition (cmp);
21054 }
21055 else
21056 #endif /* USE_FONT_BACKEND */
21057 /* If we have not yet calculated pixel size data of glyphs of
21058 the composition for the current face font, calculate them
21059 now. Theoretically, we have to check all fonts for the
21060 glyphs, but that requires much time and memory space. So,
21061 here we check only the font of the first glyph. This leads
21062 to incorrect display, but it's very rare, and C-l (recenter)
21063 can correct the display anyway. */
21064 if (! cmp->font || cmp->font != font)
21065 {
21066 /* Ascent and descent of the font of the first character
21067 of this composition (adjusted by baseline offset).
21068 Ascent and descent of overall glyphs should not be less
21069 than them respectively. */
21070 int font_ascent, font_descent, font_height;
21071 /* Bounding box of the overall glyphs. */
21072 int leftmost, rightmost, lowest, highest;
21073 int lbearing, rbearing;
21074 int i, width, ascent, descent;
21075 int left_padded = 0, right_padded = 0;
21076 int face_id;
21077 int c;
21078 XChar2b char2b;
21079 XCharStruct *pcm;
21080 int font_not_found_p;
21081 struct font_info *font_info;
21082 int pos;
21083
21084 for (glyph_len = cmp->glyph_len; glyph_len > 0; glyph_len--)
21085 if ((c = COMPOSITION_GLYPH (cmp, glyph_len - 1)) != '\t')
21086 break;
21087 if (glyph_len < cmp->glyph_len)
21088 right_padded = 1;
21089 for (i = 0; i < glyph_len; i++)
21090 {
21091 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
21092 break;
21093 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
21094 }
21095 if (i > 0)
21096 left_padded = 1;
21097
21098 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
21099 : IT_CHARPOS (*it));
21100 /* When no suitable font found, use the default font. */
21101 font_not_found_p = font == NULL;
21102 if (font_not_found_p)
21103 {
21104 face = face->ascii_face;
21105 font = face->font;
21106 }
21107 font_info = FONT_INFO_FROM_FACE (it->f, face);
21108 boff = font_info->baseline_offset;
21109 if (font_info->vertical_centering)
21110 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
21111 font_ascent = FONT_BASE (font) + boff;
21112 font_descent = FONT_DESCENT (font) - boff;
21113 font_height = FONT_HEIGHT (font);
21114
21115 cmp->font = (void *) font;
21116
21117 pcm = NULL;
21118 if (! font_not_found_p)
21119 {
21120 get_char_face_and_encoding (it->f, c, it->face_id,
21121 &char2b, it->multibyte_p, 0);
21122 pcm = get_per_char_metric (it->f, font, font_info, &char2b,
21123 FONT_TYPE_FOR_MULTIBYTE (font, c));
21124 }
21125
21126 /* Initialize the bounding box. */
21127 if (pcm)
21128 {
21129 width = pcm->width;
21130 ascent = pcm->ascent;
21131 descent = pcm->descent;
21132 lbearing = pcm->lbearing;
21133 rbearing = pcm->rbearing;
21134 }
21135 else
21136 {
21137 width = FONT_WIDTH (font);
21138 ascent = FONT_BASE (font);
21139 descent = FONT_DESCENT (font);
21140 lbearing = 0;
21141 rbearing = width;
21142 }
21143
21144 rightmost = width;
21145 leftmost = 0;
21146 lowest = - descent + boff;
21147 highest = ascent + boff;
21148
21149 if (! font_not_found_p
21150 && font_info->default_ascent
21151 && CHAR_TABLE_P (Vuse_default_ascent)
21152 && !NILP (Faref (Vuse_default_ascent,
21153 make_number (it->char_to_display))))
21154 highest = font_info->default_ascent + boff;
21155
21156 /* Draw the first glyph at the normal position. It may be
21157 shifted to right later if some other glyphs are drawn
21158 at the left. */
21159 cmp->offsets[i * 2] = 0;
21160 cmp->offsets[i * 2 + 1] = boff;
21161 cmp->lbearing = lbearing;
21162 cmp->rbearing = rbearing;
21163
21164 /* Set cmp->offsets for the remaining glyphs. */
21165 for (i++; i < glyph_len; i++)
21166 {
21167 int left, right, btm, top;
21168 int ch = COMPOSITION_GLYPH (cmp, i);
21169 int face_id;
21170 struct face *this_face;
21171 int this_boff;
21172
21173 if (ch == '\t')
21174 ch = ' ';
21175 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
21176 this_face = FACE_FROM_ID (it->f, face_id);
21177 font = this_face->font;
21178
21179 if (font == NULL)
21180 pcm = NULL;
21181 else
21182 {
21183 font_info = FONT_INFO_FROM_FACE (it->f, this_face);
21184 this_boff = font_info->baseline_offset;
21185 if (font_info->vertical_centering)
21186 this_boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
21187 get_char_face_and_encoding (it->f, ch, face_id,
21188 &char2b, it->multibyte_p, 0);
21189 pcm = get_per_char_metric (it->f, font, font_info, &char2b,
21190 FONT_TYPE_FOR_MULTIBYTE (font,
21191 ch));
21192 }
21193 if (! pcm)
21194 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
21195 else
21196 {
21197 width = pcm->width;
21198 ascent = pcm->ascent;
21199 descent = pcm->descent;
21200 lbearing = pcm->lbearing;
21201 rbearing = pcm->rbearing;
21202 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
21203 {
21204 /* Relative composition with or without
21205 alternate chars. */
21206 left = (leftmost + rightmost - width) / 2;
21207 btm = - descent + boff;
21208 if (font_info->relative_compose
21209 && (! CHAR_TABLE_P (Vignore_relative_composition)
21210 || NILP (Faref (Vignore_relative_composition,
21211 make_number (ch)))))
21212 {
21213
21214 if (- descent >= font_info->relative_compose)
21215 /* One extra pixel between two glyphs. */
21216 btm = highest + 1;
21217 else if (ascent <= 0)
21218 /* One extra pixel between two glyphs. */
21219 btm = lowest - 1 - ascent - descent;
21220 }
21221 }
21222 else
21223 {
21224 /* A composition rule is specified by an integer
21225 value that encodes global and new reference
21226 points (GREF and NREF). GREF and NREF are
21227 specified by numbers as below:
21228
21229 0---1---2 -- ascent
21230 | |
21231 | |
21232 | |
21233 9--10--11 -- center
21234 | |
21235 ---3---4---5--- baseline
21236 | |
21237 6---7---8 -- descent
21238 */
21239 int rule = COMPOSITION_RULE (cmp, i);
21240 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
21241
21242 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
21243 grefx = gref % 3, nrefx = nref % 3;
21244 grefy = gref / 3, nrefy = nref / 3;
21245 if (xoff)
21246 xoff = font_height * (xoff - 128) / 256;
21247 if (yoff)
21248 yoff = font_height * (yoff - 128) / 256;
21249
21250 left = (leftmost
21251 + grefx * (rightmost - leftmost) / 2
21252 - nrefx * width / 2
21253 + xoff);
21254
21255 btm = ((grefy == 0 ? highest
21256 : grefy == 1 ? 0
21257 : grefy == 2 ? lowest
21258 : (highest + lowest) / 2)
21259 - (nrefy == 0 ? ascent + descent
21260 : nrefy == 1 ? descent - boff
21261 : nrefy == 2 ? 0
21262 : (ascent + descent) / 2)
21263 + yoff);
21264 }
21265
21266 cmp->offsets[i * 2] = left;
21267 cmp->offsets[i * 2 + 1] = btm + descent;
21268
21269 /* Update the bounding box of the overall glyphs. */
21270 if (width > 0)
21271 {
21272 right = left + width;
21273 if (left < leftmost)
21274 leftmost = left;
21275 if (right > rightmost)
21276 rightmost = right;
21277 }
21278 top = btm + descent + ascent;
21279 if (top > highest)
21280 highest = top;
21281 if (btm < lowest)
21282 lowest = btm;
21283
21284 if (cmp->lbearing > left + lbearing)
21285 cmp->lbearing = left + lbearing;
21286 if (cmp->rbearing < left + rbearing)
21287 cmp->rbearing = left + rbearing;
21288 }
21289 }
21290
21291 /* If there are glyphs whose x-offsets are negative,
21292 shift all glyphs to the right and make all x-offsets
21293 non-negative. */
21294 if (leftmost < 0)
21295 {
21296 for (i = 0; i < cmp->glyph_len; i++)
21297 cmp->offsets[i * 2] -= leftmost;
21298 rightmost -= leftmost;
21299 cmp->lbearing -= leftmost;
21300 cmp->rbearing -= leftmost;
21301 }
21302
21303 if (left_padded && cmp->lbearing < 0)
21304 {
21305 for (i = 0; i < cmp->glyph_len; i++)
21306 cmp->offsets[i * 2] -= cmp->lbearing;
21307 rightmost -= cmp->lbearing;
21308 cmp->rbearing -= cmp->lbearing;
21309 cmp->lbearing = 0;
21310 }
21311 if (right_padded && rightmost < cmp->rbearing)
21312 {
21313 rightmost = cmp->rbearing;
21314 }
21315
21316 cmp->pixel_width = rightmost;
21317 cmp->ascent = highest;
21318 cmp->descent = - lowest;
21319 if (cmp->ascent < font_ascent)
21320 cmp->ascent = font_ascent;
21321 if (cmp->descent < font_descent)
21322 cmp->descent = font_descent;
21323 }
21324
21325 if (it->glyph_row
21326 && (cmp->lbearing < 0
21327 || cmp->rbearing > cmp->pixel_width))
21328 it->glyph_row->contains_overlapping_glyphs_p = 1;
21329
21330 it->pixel_width = cmp->pixel_width;
21331 it->ascent = it->phys_ascent = cmp->ascent;
21332 it->descent = it->phys_descent = cmp->descent;
21333
21334 if (face->box != FACE_NO_BOX)
21335 {
21336 int thick = face->box_line_width;
21337
21338 if (thick > 0)
21339 {
21340 it->ascent += thick;
21341 it->descent += thick;
21342 }
21343 else
21344 thick = - thick;
21345
21346 if (it->start_of_box_run_p)
21347 it->pixel_width += thick;
21348 if (it->end_of_box_run_p)
21349 it->pixel_width += thick;
21350 }
21351
21352 /* If face has an overline, add the height of the overline
21353 (1 pixel) and a 1 pixel margin to the character height. */
21354 if (face->overline_p)
21355 it->ascent += overline_margin;
21356
21357 take_vertical_position_into_account (it);
21358
21359 if (it->glyph_row)
21360 append_composite_glyph (it);
21361 }
21362 else if (it->what == IT_IMAGE)
21363 produce_image_glyph (it);
21364 else if (it->what == IT_STRETCH)
21365 produce_stretch_glyph (it);
21366
21367 /* Accumulate dimensions. Note: can't assume that it->descent > 0
21368 because this isn't true for images with `:ascent 100'. */
21369 xassert (it->ascent >= 0 && it->descent >= 0);
21370 if (it->area == TEXT_AREA)
21371 it->current_x += it->pixel_width;
21372
21373 if (extra_line_spacing > 0)
21374 {
21375 it->descent += extra_line_spacing;
21376 if (extra_line_spacing > it->max_extra_line_spacing)
21377 it->max_extra_line_spacing = extra_line_spacing;
21378 }
21379
21380 it->max_ascent = max (it->max_ascent, it->ascent);
21381 it->max_descent = max (it->max_descent, it->descent);
21382 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
21383 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
21384 }
21385
21386 /* EXPORT for RIF:
21387 Output LEN glyphs starting at START at the nominal cursor position.
21388 Advance the nominal cursor over the text. The global variable
21389 updated_window contains the window being updated, updated_row is
21390 the glyph row being updated, and updated_area is the area of that
21391 row being updated. */
21392
21393 void
21394 x_write_glyphs (start, len)
21395 struct glyph *start;
21396 int len;
21397 {
21398 int x, hpos;
21399
21400 xassert (updated_window && updated_row);
21401 BLOCK_INPUT;
21402
21403 /* Write glyphs. */
21404
21405 hpos = start - updated_row->glyphs[updated_area];
21406 x = draw_glyphs (updated_window, output_cursor.x,
21407 updated_row, updated_area,
21408 hpos, hpos + len,
21409 DRAW_NORMAL_TEXT, 0);
21410
21411 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
21412 if (updated_area == TEXT_AREA
21413 && updated_window->phys_cursor_on_p
21414 && updated_window->phys_cursor.vpos == output_cursor.vpos
21415 && updated_window->phys_cursor.hpos >= hpos
21416 && updated_window->phys_cursor.hpos < hpos + len)
21417 updated_window->phys_cursor_on_p = 0;
21418
21419 UNBLOCK_INPUT;
21420
21421 /* Advance the output cursor. */
21422 output_cursor.hpos += len;
21423 output_cursor.x = x;
21424 }
21425
21426
21427 /* EXPORT for RIF:
21428 Insert LEN glyphs from START at the nominal cursor position. */
21429
21430 void
21431 x_insert_glyphs (start, len)
21432 struct glyph *start;
21433 int len;
21434 {
21435 struct frame *f;
21436 struct window *w;
21437 int line_height, shift_by_width, shifted_region_width;
21438 struct glyph_row *row;
21439 struct glyph *glyph;
21440 int frame_x, frame_y;
21441 EMACS_INT hpos;
21442
21443 xassert (updated_window && updated_row);
21444 BLOCK_INPUT;
21445 w = updated_window;
21446 f = XFRAME (WINDOW_FRAME (w));
21447
21448 /* Get the height of the line we are in. */
21449 row = updated_row;
21450 line_height = row->height;
21451
21452 /* Get the width of the glyphs to insert. */
21453 shift_by_width = 0;
21454 for (glyph = start; glyph < start + len; ++glyph)
21455 shift_by_width += glyph->pixel_width;
21456
21457 /* Get the width of the region to shift right. */
21458 shifted_region_width = (window_box_width (w, updated_area)
21459 - output_cursor.x
21460 - shift_by_width);
21461
21462 /* Shift right. */
21463 frame_x = window_box_left (w, updated_area) + output_cursor.x;
21464 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
21465
21466 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
21467 line_height, shift_by_width);
21468
21469 /* Write the glyphs. */
21470 hpos = start - row->glyphs[updated_area];
21471 draw_glyphs (w, output_cursor.x, row, updated_area,
21472 hpos, hpos + len,
21473 DRAW_NORMAL_TEXT, 0);
21474
21475 /* Advance the output cursor. */
21476 output_cursor.hpos += len;
21477 output_cursor.x += shift_by_width;
21478 UNBLOCK_INPUT;
21479 }
21480
21481
21482 /* EXPORT for RIF:
21483 Erase the current text line from the nominal cursor position
21484 (inclusive) to pixel column TO_X (exclusive). The idea is that
21485 everything from TO_X onward is already erased.
21486
21487 TO_X is a pixel position relative to updated_area of
21488 updated_window. TO_X == -1 means clear to the end of this area. */
21489
21490 void
21491 x_clear_end_of_line (to_x)
21492 int to_x;
21493 {
21494 struct frame *f;
21495 struct window *w = updated_window;
21496 int max_x, min_y, max_y;
21497 int from_x, from_y, to_y;
21498
21499 xassert (updated_window && updated_row);
21500 f = XFRAME (w->frame);
21501
21502 if (updated_row->full_width_p)
21503 max_x = WINDOW_TOTAL_WIDTH (w);
21504 else
21505 max_x = window_box_width (w, updated_area);
21506 max_y = window_text_bottom_y (w);
21507
21508 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
21509 of window. For TO_X > 0, truncate to end of drawing area. */
21510 if (to_x == 0)
21511 return;
21512 else if (to_x < 0)
21513 to_x = max_x;
21514 else
21515 to_x = min (to_x, max_x);
21516
21517 to_y = min (max_y, output_cursor.y + updated_row->height);
21518
21519 /* Notice if the cursor will be cleared by this operation. */
21520 if (!updated_row->full_width_p)
21521 notice_overwritten_cursor (w, updated_area,
21522 output_cursor.x, -1,
21523 updated_row->y,
21524 MATRIX_ROW_BOTTOM_Y (updated_row));
21525
21526 from_x = output_cursor.x;
21527
21528 /* Translate to frame coordinates. */
21529 if (updated_row->full_width_p)
21530 {
21531 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
21532 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
21533 }
21534 else
21535 {
21536 int area_left = window_box_left (w, updated_area);
21537 from_x += area_left;
21538 to_x += area_left;
21539 }
21540
21541 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
21542 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
21543 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
21544
21545 /* Prevent inadvertently clearing to end of the X window. */
21546 if (to_x > from_x && to_y > from_y)
21547 {
21548 BLOCK_INPUT;
21549 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
21550 to_x - from_x, to_y - from_y);
21551 UNBLOCK_INPUT;
21552 }
21553 }
21554
21555 #endif /* HAVE_WINDOW_SYSTEM */
21556
21557
21558 \f
21559 /***********************************************************************
21560 Cursor types
21561 ***********************************************************************/
21562
21563 /* Value is the internal representation of the specified cursor type
21564 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
21565 of the bar cursor. */
21566
21567 static enum text_cursor_kinds
21568 get_specified_cursor_type (arg, width)
21569 Lisp_Object arg;
21570 int *width;
21571 {
21572 enum text_cursor_kinds type;
21573
21574 if (NILP (arg))
21575 return NO_CURSOR;
21576
21577 if (EQ (arg, Qbox))
21578 return FILLED_BOX_CURSOR;
21579
21580 if (EQ (arg, Qhollow))
21581 return HOLLOW_BOX_CURSOR;
21582
21583 if (EQ (arg, Qbar))
21584 {
21585 *width = 2;
21586 return BAR_CURSOR;
21587 }
21588
21589 if (CONSP (arg)
21590 && EQ (XCAR (arg), Qbar)
21591 && INTEGERP (XCDR (arg))
21592 && XINT (XCDR (arg)) >= 0)
21593 {
21594 *width = XINT (XCDR (arg));
21595 return BAR_CURSOR;
21596 }
21597
21598 if (EQ (arg, Qhbar))
21599 {
21600 *width = 2;
21601 return HBAR_CURSOR;
21602 }
21603
21604 if (CONSP (arg)
21605 && EQ (XCAR (arg), Qhbar)
21606 && INTEGERP (XCDR (arg))
21607 && XINT (XCDR (arg)) >= 0)
21608 {
21609 *width = XINT (XCDR (arg));
21610 return HBAR_CURSOR;
21611 }
21612
21613 /* Treat anything unknown as "hollow box cursor".
21614 It was bad to signal an error; people have trouble fixing
21615 .Xdefaults with Emacs, when it has something bad in it. */
21616 type = HOLLOW_BOX_CURSOR;
21617
21618 return type;
21619 }
21620
21621 /* Set the default cursor types for specified frame. */
21622 void
21623 set_frame_cursor_types (f, arg)
21624 struct frame *f;
21625 Lisp_Object arg;
21626 {
21627 int width;
21628 Lisp_Object tem;
21629
21630 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
21631 FRAME_CURSOR_WIDTH (f) = width;
21632
21633 /* By default, set up the blink-off state depending on the on-state. */
21634
21635 tem = Fassoc (arg, Vblink_cursor_alist);
21636 if (!NILP (tem))
21637 {
21638 FRAME_BLINK_OFF_CURSOR (f)
21639 = get_specified_cursor_type (XCDR (tem), &width);
21640 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
21641 }
21642 else
21643 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
21644 }
21645
21646
21647 /* Return the cursor we want to be displayed in window W. Return
21648 width of bar/hbar cursor through WIDTH arg. Return with
21649 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
21650 (i.e. if the `system caret' should track this cursor).
21651
21652 In a mini-buffer window, we want the cursor only to appear if we
21653 are reading input from this window. For the selected window, we
21654 want the cursor type given by the frame parameter or buffer local
21655 setting of cursor-type. If explicitly marked off, draw no cursor.
21656 In all other cases, we want a hollow box cursor. */
21657
21658 static enum text_cursor_kinds
21659 get_window_cursor_type (w, glyph, width, active_cursor)
21660 struct window *w;
21661 struct glyph *glyph;
21662 int *width;
21663 int *active_cursor;
21664 {
21665 struct frame *f = XFRAME (w->frame);
21666 struct buffer *b = XBUFFER (w->buffer);
21667 int cursor_type = DEFAULT_CURSOR;
21668 Lisp_Object alt_cursor;
21669 int non_selected = 0;
21670
21671 *active_cursor = 1;
21672
21673 /* Echo area */
21674 if (cursor_in_echo_area
21675 && FRAME_HAS_MINIBUF_P (f)
21676 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
21677 {
21678 if (w == XWINDOW (echo_area_window))
21679 {
21680 if (EQ (b->cursor_type, Qt) || NILP (b->cursor_type))
21681 {
21682 *width = FRAME_CURSOR_WIDTH (f);
21683 return FRAME_DESIRED_CURSOR (f);
21684 }
21685 else
21686 return get_specified_cursor_type (b->cursor_type, width);
21687 }
21688
21689 *active_cursor = 0;
21690 non_selected = 1;
21691 }
21692
21693 /* Detect a nonselected window or nonselected frame. */
21694 else if (w != XWINDOW (f->selected_window)
21695 #ifdef HAVE_WINDOW_SYSTEM
21696 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame
21697 #endif
21698 )
21699 {
21700 *active_cursor = 0;
21701
21702 if (MINI_WINDOW_P (w) && minibuf_level == 0)
21703 return NO_CURSOR;
21704
21705 non_selected = 1;
21706 }
21707
21708 /* Never display a cursor in a window in which cursor-type is nil. */
21709 if (NILP (b->cursor_type))
21710 return NO_CURSOR;
21711
21712 /* Get the normal cursor type for this window. */
21713 if (EQ (b->cursor_type, Qt))
21714 {
21715 cursor_type = FRAME_DESIRED_CURSOR (f);
21716 *width = FRAME_CURSOR_WIDTH (f);
21717 }
21718 else
21719 cursor_type = get_specified_cursor_type (b->cursor_type, width);
21720
21721 /* Use cursor-in-non-selected-windows instead
21722 for non-selected window or frame. */
21723 if (non_selected)
21724 {
21725 alt_cursor = b->cursor_in_non_selected_windows;
21726 if (!EQ (Qt, alt_cursor))
21727 return get_specified_cursor_type (alt_cursor, width);
21728 /* t means modify the normal cursor type. */
21729 if (cursor_type == FILLED_BOX_CURSOR)
21730 cursor_type = HOLLOW_BOX_CURSOR;
21731 else if (cursor_type == BAR_CURSOR && *width > 1)
21732 --*width;
21733 return cursor_type;
21734 }
21735
21736 /* Use normal cursor if not blinked off. */
21737 if (!w->cursor_off_p)
21738 {
21739 #ifdef HAVE_WINDOW_SYSTEM
21740 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
21741 {
21742 if (cursor_type == FILLED_BOX_CURSOR)
21743 {
21744 /* Using a block cursor on large images can be very annoying.
21745 So use a hollow cursor for "large" images.
21746 If image is not transparent (no mask), also use hollow cursor. */
21747 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
21748 if (img != NULL && IMAGEP (img->spec))
21749 {
21750 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
21751 where N = size of default frame font size.
21752 This should cover most of the "tiny" icons people may use. */
21753 if (!img->mask
21754 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
21755 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
21756 cursor_type = HOLLOW_BOX_CURSOR;
21757 }
21758 }
21759 else if (cursor_type != NO_CURSOR)
21760 {
21761 /* Display current only supports BOX and HOLLOW cursors for images.
21762 So for now, unconditionally use a HOLLOW cursor when cursor is
21763 not a solid box cursor. */
21764 cursor_type = HOLLOW_BOX_CURSOR;
21765 }
21766 }
21767 #endif
21768 return cursor_type;
21769 }
21770
21771 /* Cursor is blinked off, so determine how to "toggle" it. */
21772
21773 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
21774 if ((alt_cursor = Fassoc (b->cursor_type, Vblink_cursor_alist), !NILP (alt_cursor)))
21775 return get_specified_cursor_type (XCDR (alt_cursor), width);
21776
21777 /* Then see if frame has specified a specific blink off cursor type. */
21778 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
21779 {
21780 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
21781 return FRAME_BLINK_OFF_CURSOR (f);
21782 }
21783
21784 #if 0
21785 /* Some people liked having a permanently visible blinking cursor,
21786 while others had very strong opinions against it. So it was
21787 decided to remove it. KFS 2003-09-03 */
21788
21789 /* Finally perform built-in cursor blinking:
21790 filled box <-> hollow box
21791 wide [h]bar <-> narrow [h]bar
21792 narrow [h]bar <-> no cursor
21793 other type <-> no cursor */
21794
21795 if (cursor_type == FILLED_BOX_CURSOR)
21796 return HOLLOW_BOX_CURSOR;
21797
21798 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
21799 {
21800 *width = 1;
21801 return cursor_type;
21802 }
21803 #endif
21804
21805 return NO_CURSOR;
21806 }
21807
21808
21809 #ifdef HAVE_WINDOW_SYSTEM
21810
21811 /* Notice when the text cursor of window W has been completely
21812 overwritten by a drawing operation that outputs glyphs in AREA
21813 starting at X0 and ending at X1 in the line starting at Y0 and
21814 ending at Y1. X coordinates are area-relative. X1 < 0 means all
21815 the rest of the line after X0 has been written. Y coordinates
21816 are window-relative. */
21817
21818 static void
21819 notice_overwritten_cursor (w, area, x0, x1, y0, y1)
21820 struct window *w;
21821 enum glyph_row_area area;
21822 int x0, y0, x1, y1;
21823 {
21824 int cx0, cx1, cy0, cy1;
21825 struct glyph_row *row;
21826
21827 if (!w->phys_cursor_on_p)
21828 return;
21829 if (area != TEXT_AREA)
21830 return;
21831
21832 if (w->phys_cursor.vpos < 0
21833 || w->phys_cursor.vpos >= w->current_matrix->nrows
21834 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
21835 !(row->enabled_p && row->displays_text_p)))
21836 return;
21837
21838 if (row->cursor_in_fringe_p)
21839 {
21840 row->cursor_in_fringe_p = 0;
21841 draw_fringe_bitmap (w, row, 0);
21842 w->phys_cursor_on_p = 0;
21843 return;
21844 }
21845
21846 cx0 = w->phys_cursor.x;
21847 cx1 = cx0 + w->phys_cursor_width;
21848 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
21849 return;
21850
21851 /* The cursor image will be completely removed from the
21852 screen if the output area intersects the cursor area in
21853 y-direction. When we draw in [y0 y1[, and some part of
21854 the cursor is at y < y0, that part must have been drawn
21855 before. When scrolling, the cursor is erased before
21856 actually scrolling, so we don't come here. When not
21857 scrolling, the rows above the old cursor row must have
21858 changed, and in this case these rows must have written
21859 over the cursor image.
21860
21861 Likewise if part of the cursor is below y1, with the
21862 exception of the cursor being in the first blank row at
21863 the buffer and window end because update_text_area
21864 doesn't draw that row. (Except when it does, but
21865 that's handled in update_text_area.) */
21866
21867 cy0 = w->phys_cursor.y;
21868 cy1 = cy0 + w->phys_cursor_height;
21869 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
21870 return;
21871
21872 w->phys_cursor_on_p = 0;
21873 }
21874
21875 #endif /* HAVE_WINDOW_SYSTEM */
21876
21877 \f
21878 /************************************************************************
21879 Mouse Face
21880 ************************************************************************/
21881
21882 #ifdef HAVE_WINDOW_SYSTEM
21883
21884 /* EXPORT for RIF:
21885 Fix the display of area AREA of overlapping row ROW in window W
21886 with respect to the overlapping part OVERLAPS. */
21887
21888 void
21889 x_fix_overlapping_area (w, row, area, overlaps)
21890 struct window *w;
21891 struct glyph_row *row;
21892 enum glyph_row_area area;
21893 int overlaps;
21894 {
21895 int i, x;
21896
21897 BLOCK_INPUT;
21898
21899 x = 0;
21900 for (i = 0; i < row->used[area];)
21901 {
21902 if (row->glyphs[area][i].overlaps_vertically_p)
21903 {
21904 int start = i, start_x = x;
21905
21906 do
21907 {
21908 x += row->glyphs[area][i].pixel_width;
21909 ++i;
21910 }
21911 while (i < row->used[area]
21912 && row->glyphs[area][i].overlaps_vertically_p);
21913
21914 draw_glyphs (w, start_x, row, area,
21915 start, i,
21916 DRAW_NORMAL_TEXT, overlaps);
21917 }
21918 else
21919 {
21920 x += row->glyphs[area][i].pixel_width;
21921 ++i;
21922 }
21923 }
21924
21925 UNBLOCK_INPUT;
21926 }
21927
21928
21929 /* EXPORT:
21930 Draw the cursor glyph of window W in glyph row ROW. See the
21931 comment of draw_glyphs for the meaning of HL. */
21932
21933 void
21934 draw_phys_cursor_glyph (w, row, hl)
21935 struct window *w;
21936 struct glyph_row *row;
21937 enum draw_glyphs_face hl;
21938 {
21939 /* If cursor hpos is out of bounds, don't draw garbage. This can
21940 happen in mini-buffer windows when switching between echo area
21941 glyphs and mini-buffer. */
21942 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
21943 {
21944 int on_p = w->phys_cursor_on_p;
21945 int x1;
21946 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
21947 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
21948 hl, 0);
21949 w->phys_cursor_on_p = on_p;
21950
21951 if (hl == DRAW_CURSOR)
21952 w->phys_cursor_width = x1 - w->phys_cursor.x;
21953 /* When we erase the cursor, and ROW is overlapped by other
21954 rows, make sure that these overlapping parts of other rows
21955 are redrawn. */
21956 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
21957 {
21958 w->phys_cursor_width = x1 - w->phys_cursor.x;
21959
21960 if (row > w->current_matrix->rows
21961 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
21962 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
21963 OVERLAPS_ERASED_CURSOR);
21964
21965 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
21966 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
21967 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
21968 OVERLAPS_ERASED_CURSOR);
21969 }
21970 }
21971 }
21972
21973
21974 /* EXPORT:
21975 Erase the image of a cursor of window W from the screen. */
21976
21977 void
21978 erase_phys_cursor (w)
21979 struct window *w;
21980 {
21981 struct frame *f = XFRAME (w->frame);
21982 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
21983 int hpos = w->phys_cursor.hpos;
21984 int vpos = w->phys_cursor.vpos;
21985 int mouse_face_here_p = 0;
21986 struct glyph_matrix *active_glyphs = w->current_matrix;
21987 struct glyph_row *cursor_row;
21988 struct glyph *cursor_glyph;
21989 enum draw_glyphs_face hl;
21990
21991 /* No cursor displayed or row invalidated => nothing to do on the
21992 screen. */
21993 if (w->phys_cursor_type == NO_CURSOR)
21994 goto mark_cursor_off;
21995
21996 /* VPOS >= active_glyphs->nrows means that window has been resized.
21997 Don't bother to erase the cursor. */
21998 if (vpos >= active_glyphs->nrows)
21999 goto mark_cursor_off;
22000
22001 /* If row containing cursor is marked invalid, there is nothing we
22002 can do. */
22003 cursor_row = MATRIX_ROW (active_glyphs, vpos);
22004 if (!cursor_row->enabled_p)
22005 goto mark_cursor_off;
22006
22007 /* If line spacing is > 0, old cursor may only be partially visible in
22008 window after split-window. So adjust visible height. */
22009 cursor_row->visible_height = min (cursor_row->visible_height,
22010 window_text_bottom_y (w) - cursor_row->y);
22011
22012 /* If row is completely invisible, don't attempt to delete a cursor which
22013 isn't there. This can happen if cursor is at top of a window, and
22014 we switch to a buffer with a header line in that window. */
22015 if (cursor_row->visible_height <= 0)
22016 goto mark_cursor_off;
22017
22018 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
22019 if (cursor_row->cursor_in_fringe_p)
22020 {
22021 cursor_row->cursor_in_fringe_p = 0;
22022 draw_fringe_bitmap (w, cursor_row, 0);
22023 goto mark_cursor_off;
22024 }
22025
22026 /* This can happen when the new row is shorter than the old one.
22027 In this case, either draw_glyphs or clear_end_of_line
22028 should have cleared the cursor. Note that we wouldn't be
22029 able to erase the cursor in this case because we don't have a
22030 cursor glyph at hand. */
22031 if (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])
22032 goto mark_cursor_off;
22033
22034 /* If the cursor is in the mouse face area, redisplay that when
22035 we clear the cursor. */
22036 if (! NILP (dpyinfo->mouse_face_window)
22037 && w == XWINDOW (dpyinfo->mouse_face_window)
22038 && (vpos > dpyinfo->mouse_face_beg_row
22039 || (vpos == dpyinfo->mouse_face_beg_row
22040 && hpos >= dpyinfo->mouse_face_beg_col))
22041 && (vpos < dpyinfo->mouse_face_end_row
22042 || (vpos == dpyinfo->mouse_face_end_row
22043 && hpos < dpyinfo->mouse_face_end_col))
22044 /* Don't redraw the cursor's spot in mouse face if it is at the
22045 end of a line (on a newline). The cursor appears there, but
22046 mouse highlighting does not. */
22047 && cursor_row->used[TEXT_AREA] > hpos)
22048 mouse_face_here_p = 1;
22049
22050 /* Maybe clear the display under the cursor. */
22051 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
22052 {
22053 int x, y, left_x;
22054 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
22055 int width;
22056
22057 cursor_glyph = get_phys_cursor_glyph (w);
22058 if (cursor_glyph == NULL)
22059 goto mark_cursor_off;
22060
22061 width = cursor_glyph->pixel_width;
22062 left_x = window_box_left_offset (w, TEXT_AREA);
22063 x = w->phys_cursor.x;
22064 if (x < left_x)
22065 width -= left_x - x;
22066 width = min (width, window_box_width (w, TEXT_AREA) - x);
22067 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
22068 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, max (x, left_x));
22069
22070 if (width > 0)
22071 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
22072 }
22073
22074 /* Erase the cursor by redrawing the character underneath it. */
22075 if (mouse_face_here_p)
22076 hl = DRAW_MOUSE_FACE;
22077 else
22078 hl = DRAW_NORMAL_TEXT;
22079 draw_phys_cursor_glyph (w, cursor_row, hl);
22080
22081 mark_cursor_off:
22082 w->phys_cursor_on_p = 0;
22083 w->phys_cursor_type = NO_CURSOR;
22084 }
22085
22086
22087 /* EXPORT:
22088 Display or clear cursor of window W. If ON is zero, clear the
22089 cursor. If it is non-zero, display the cursor. If ON is nonzero,
22090 where to put the cursor is specified by HPOS, VPOS, X and Y. */
22091
22092 void
22093 display_and_set_cursor (w, on, hpos, vpos, x, y)
22094 struct window *w;
22095 int on, hpos, vpos, x, y;
22096 {
22097 struct frame *f = XFRAME (w->frame);
22098 int new_cursor_type;
22099 int new_cursor_width;
22100 int active_cursor;
22101 struct glyph_row *glyph_row;
22102 struct glyph *glyph;
22103
22104 /* This is pointless on invisible frames, and dangerous on garbaged
22105 windows and frames; in the latter case, the frame or window may
22106 be in the midst of changing its size, and x and y may be off the
22107 window. */
22108 if (! FRAME_VISIBLE_P (f)
22109 || FRAME_GARBAGED_P (f)
22110 || vpos >= w->current_matrix->nrows
22111 || hpos >= w->current_matrix->matrix_w)
22112 return;
22113
22114 /* If cursor is off and we want it off, return quickly. */
22115 if (!on && !w->phys_cursor_on_p)
22116 return;
22117
22118 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
22119 /* If cursor row is not enabled, we don't really know where to
22120 display the cursor. */
22121 if (!glyph_row->enabled_p)
22122 {
22123 w->phys_cursor_on_p = 0;
22124 return;
22125 }
22126
22127 glyph = NULL;
22128 if (!glyph_row->exact_window_width_line_p
22129 || hpos < glyph_row->used[TEXT_AREA])
22130 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
22131
22132 xassert (interrupt_input_blocked);
22133
22134 /* Set new_cursor_type to the cursor we want to be displayed. */
22135 new_cursor_type = get_window_cursor_type (w, glyph,
22136 &new_cursor_width, &active_cursor);
22137
22138 /* If cursor is currently being shown and we don't want it to be or
22139 it is in the wrong place, or the cursor type is not what we want,
22140 erase it. */
22141 if (w->phys_cursor_on_p
22142 && (!on
22143 || w->phys_cursor.x != x
22144 || w->phys_cursor.y != y
22145 || new_cursor_type != w->phys_cursor_type
22146 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
22147 && new_cursor_width != w->phys_cursor_width)))
22148 erase_phys_cursor (w);
22149
22150 /* Don't check phys_cursor_on_p here because that flag is only set
22151 to zero in some cases where we know that the cursor has been
22152 completely erased, to avoid the extra work of erasing the cursor
22153 twice. In other words, phys_cursor_on_p can be 1 and the cursor
22154 still not be visible, or it has only been partly erased. */
22155 if (on)
22156 {
22157 w->phys_cursor_ascent = glyph_row->ascent;
22158 w->phys_cursor_height = glyph_row->height;
22159
22160 /* Set phys_cursor_.* before x_draw_.* is called because some
22161 of them may need the information. */
22162 w->phys_cursor.x = x;
22163 w->phys_cursor.y = glyph_row->y;
22164 w->phys_cursor.hpos = hpos;
22165 w->phys_cursor.vpos = vpos;
22166 }
22167
22168 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
22169 new_cursor_type, new_cursor_width,
22170 on, active_cursor);
22171 }
22172
22173
22174 /* Switch the display of W's cursor on or off, according to the value
22175 of ON. */
22176
22177 static void
22178 update_window_cursor (w, on)
22179 struct window *w;
22180 int on;
22181 {
22182 /* Don't update cursor in windows whose frame is in the process
22183 of being deleted. */
22184 if (w->current_matrix)
22185 {
22186 BLOCK_INPUT;
22187 display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
22188 w->phys_cursor.x, w->phys_cursor.y);
22189 UNBLOCK_INPUT;
22190 }
22191 }
22192
22193
22194 /* Call update_window_cursor with parameter ON_P on all leaf windows
22195 in the window tree rooted at W. */
22196
22197 static void
22198 update_cursor_in_window_tree (w, on_p)
22199 struct window *w;
22200 int on_p;
22201 {
22202 while (w)
22203 {
22204 if (!NILP (w->hchild))
22205 update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
22206 else if (!NILP (w->vchild))
22207 update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
22208 else
22209 update_window_cursor (w, on_p);
22210
22211 w = NILP (w->next) ? 0 : XWINDOW (w->next);
22212 }
22213 }
22214
22215
22216 /* EXPORT:
22217 Display the cursor on window W, or clear it, according to ON_P.
22218 Don't change the cursor's position. */
22219
22220 void
22221 x_update_cursor (f, on_p)
22222 struct frame *f;
22223 int on_p;
22224 {
22225 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
22226 }
22227
22228
22229 /* EXPORT:
22230 Clear the cursor of window W to background color, and mark the
22231 cursor as not shown. This is used when the text where the cursor
22232 is is about to be rewritten. */
22233
22234 void
22235 x_clear_cursor (w)
22236 struct window *w;
22237 {
22238 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
22239 update_window_cursor (w, 0);
22240 }
22241
22242
22243 /* EXPORT:
22244 Display the active region described by mouse_face_* according to DRAW. */
22245
22246 void
22247 show_mouse_face (dpyinfo, draw)
22248 Display_Info *dpyinfo;
22249 enum draw_glyphs_face draw;
22250 {
22251 struct window *w = XWINDOW (dpyinfo->mouse_face_window);
22252 struct frame *f = XFRAME (WINDOW_FRAME (w));
22253
22254 if (/* If window is in the process of being destroyed, don't bother
22255 to do anything. */
22256 w->current_matrix != NULL
22257 /* Don't update mouse highlight if hidden */
22258 && (draw != DRAW_MOUSE_FACE || !dpyinfo->mouse_face_hidden)
22259 /* Recognize when we are called to operate on rows that don't exist
22260 anymore. This can happen when a window is split. */
22261 && dpyinfo->mouse_face_end_row < w->current_matrix->nrows)
22262 {
22263 int phys_cursor_on_p = w->phys_cursor_on_p;
22264 struct glyph_row *row, *first, *last;
22265
22266 first = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_beg_row);
22267 last = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_end_row);
22268
22269 for (row = first; row <= last && row->enabled_p; ++row)
22270 {
22271 int start_hpos, end_hpos, start_x;
22272
22273 /* For all but the first row, the highlight starts at column 0. */
22274 if (row == first)
22275 {
22276 start_hpos = dpyinfo->mouse_face_beg_col;
22277 start_x = dpyinfo->mouse_face_beg_x;
22278 }
22279 else
22280 {
22281 start_hpos = 0;
22282 start_x = 0;
22283 }
22284
22285 if (row == last)
22286 end_hpos = dpyinfo->mouse_face_end_col;
22287 else
22288 {
22289 end_hpos = row->used[TEXT_AREA];
22290 if (draw == DRAW_NORMAL_TEXT)
22291 row->fill_line_p = 1; /* Clear to end of line */
22292 }
22293
22294 if (end_hpos > start_hpos)
22295 {
22296 draw_glyphs (w, start_x, row, TEXT_AREA,
22297 start_hpos, end_hpos,
22298 draw, 0);
22299
22300 row->mouse_face_p
22301 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
22302 }
22303 }
22304
22305 /* When we've written over the cursor, arrange for it to
22306 be displayed again. */
22307 if (phys_cursor_on_p && !w->phys_cursor_on_p)
22308 {
22309 BLOCK_INPUT;
22310 display_and_set_cursor (w, 1,
22311 w->phys_cursor.hpos, w->phys_cursor.vpos,
22312 w->phys_cursor.x, w->phys_cursor.y);
22313 UNBLOCK_INPUT;
22314 }
22315 }
22316
22317 /* Change the mouse cursor. */
22318 if (draw == DRAW_NORMAL_TEXT && !EQ (dpyinfo->mouse_face_window, f->tool_bar_window))
22319 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
22320 else if (draw == DRAW_MOUSE_FACE)
22321 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
22322 else
22323 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
22324 }
22325
22326 /* EXPORT:
22327 Clear out the mouse-highlighted active region.
22328 Redraw it un-highlighted first. Value is non-zero if mouse
22329 face was actually drawn unhighlighted. */
22330
22331 int
22332 clear_mouse_face (dpyinfo)
22333 Display_Info *dpyinfo;
22334 {
22335 int cleared = 0;
22336
22337 if (!dpyinfo->mouse_face_hidden && !NILP (dpyinfo->mouse_face_window))
22338 {
22339 show_mouse_face (dpyinfo, DRAW_NORMAL_TEXT);
22340 cleared = 1;
22341 }
22342
22343 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
22344 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
22345 dpyinfo->mouse_face_window = Qnil;
22346 dpyinfo->mouse_face_overlay = Qnil;
22347 return cleared;
22348 }
22349
22350
22351 /* EXPORT:
22352 Non-zero if physical cursor of window W is within mouse face. */
22353
22354 int
22355 cursor_in_mouse_face_p (w)
22356 struct window *w;
22357 {
22358 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
22359 int in_mouse_face = 0;
22360
22361 if (WINDOWP (dpyinfo->mouse_face_window)
22362 && XWINDOW (dpyinfo->mouse_face_window) == w)
22363 {
22364 int hpos = w->phys_cursor.hpos;
22365 int vpos = w->phys_cursor.vpos;
22366
22367 if (vpos >= dpyinfo->mouse_face_beg_row
22368 && vpos <= dpyinfo->mouse_face_end_row
22369 && (vpos > dpyinfo->mouse_face_beg_row
22370 || hpos >= dpyinfo->mouse_face_beg_col)
22371 && (vpos < dpyinfo->mouse_face_end_row
22372 || hpos < dpyinfo->mouse_face_end_col
22373 || dpyinfo->mouse_face_past_end))
22374 in_mouse_face = 1;
22375 }
22376
22377 return in_mouse_face;
22378 }
22379
22380
22381
22382 \f
22383 /* Find the glyph matrix position of buffer position CHARPOS in window
22384 *W. HPOS, *VPOS, *X, and *Y are set to the positions found. W's
22385 current glyphs must be up to date. If CHARPOS is above window
22386 start return (0, 0, 0, 0). If CHARPOS is after end of W, return end
22387 of last line in W. In the row containing CHARPOS, stop before glyphs
22388 having STOP as object. */
22389
22390 #if 1 /* This is a version of fast_find_position that's more correct
22391 in the presence of hscrolling, for example. I didn't install
22392 it right away because the problem fixed is minor, it failed
22393 in 20.x as well, and I think it's too risky to install
22394 so near the release of 21.1. 2001-09-25 gerd. */
22395
22396 static int
22397 fast_find_position (w, charpos, hpos, vpos, x, y, stop)
22398 struct window *w;
22399 EMACS_INT charpos;
22400 int *hpos, *vpos, *x, *y;
22401 Lisp_Object stop;
22402 {
22403 struct glyph_row *row, *first;
22404 struct glyph *glyph, *end;
22405 int past_end = 0;
22406
22407 first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
22408 if (charpos < MATRIX_ROW_START_CHARPOS (first))
22409 {
22410 *x = first->x;
22411 *y = first->y;
22412 *hpos = 0;
22413 *vpos = MATRIX_ROW_VPOS (first, w->current_matrix);
22414 return 1;
22415 }
22416
22417 row = row_containing_pos (w, charpos, first, NULL, 0);
22418 if (row == NULL)
22419 {
22420 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
22421 past_end = 1;
22422 }
22423
22424 /* If whole rows or last part of a row came from a display overlay,
22425 row_containing_pos will skip over such rows because their end pos
22426 equals the start pos of the overlay or interval.
22427
22428 Move back if we have a STOP object and previous row's
22429 end glyph came from STOP. */
22430 if (!NILP (stop))
22431 {
22432 struct glyph_row *prev;
22433 while ((prev = row - 1, prev >= first)
22434 && MATRIX_ROW_END_CHARPOS (prev) == charpos
22435 && prev->used[TEXT_AREA] > 0)
22436 {
22437 struct glyph *beg = prev->glyphs[TEXT_AREA];
22438 glyph = beg + prev->used[TEXT_AREA];
22439 while (--glyph >= beg
22440 && INTEGERP (glyph->object));
22441 if (glyph < beg
22442 || !EQ (stop, glyph->object))
22443 break;
22444 row = prev;
22445 }
22446 }
22447
22448 *x = row->x;
22449 *y = row->y;
22450 *vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
22451
22452 glyph = row->glyphs[TEXT_AREA];
22453 end = glyph + row->used[TEXT_AREA];
22454
22455 /* Skip over glyphs not having an object at the start of the row.
22456 These are special glyphs like truncation marks on terminal
22457 frames. */
22458 if (row->displays_text_p)
22459 while (glyph < end
22460 && INTEGERP (glyph->object)
22461 && !EQ (stop, glyph->object)
22462 && glyph->charpos < 0)
22463 {
22464 *x += glyph->pixel_width;
22465 ++glyph;
22466 }
22467
22468 while (glyph < end
22469 && !INTEGERP (glyph->object)
22470 && !EQ (stop, glyph->object)
22471 && (!BUFFERP (glyph->object)
22472 || glyph->charpos < charpos))
22473 {
22474 *x += glyph->pixel_width;
22475 ++glyph;
22476 }
22477
22478 *hpos = glyph - row->glyphs[TEXT_AREA];
22479 return !past_end;
22480 }
22481
22482 #else /* not 1 */
22483
22484 static int
22485 fast_find_position (w, pos, hpos, vpos, x, y, stop)
22486 struct window *w;
22487 EMACS_INT pos;
22488 int *hpos, *vpos, *x, *y;
22489 Lisp_Object stop;
22490 {
22491 int i;
22492 int lastcol;
22493 int maybe_next_line_p = 0;
22494 int line_start_position;
22495 int yb = window_text_bottom_y (w);
22496 struct glyph_row *row, *best_row;
22497 int row_vpos, best_row_vpos;
22498 int current_x;
22499
22500 row = best_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
22501 row_vpos = best_row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
22502
22503 while (row->y < yb)
22504 {
22505 if (row->used[TEXT_AREA])
22506 line_start_position = row->glyphs[TEXT_AREA]->charpos;
22507 else
22508 line_start_position = 0;
22509
22510 if (line_start_position > pos)
22511 break;
22512 /* If the position sought is the end of the buffer,
22513 don't include the blank lines at the bottom of the window. */
22514 else if (line_start_position == pos
22515 && pos == BUF_ZV (XBUFFER (w->buffer)))
22516 {
22517 maybe_next_line_p = 1;
22518 break;
22519 }
22520 else if (line_start_position > 0)
22521 {
22522 best_row = row;
22523 best_row_vpos = row_vpos;
22524 }
22525
22526 if (row->y + row->height >= yb)
22527 break;
22528
22529 ++row;
22530 ++row_vpos;
22531 }
22532
22533 /* Find the right column within BEST_ROW. */
22534 lastcol = 0;
22535 current_x = best_row->x;
22536 for (i = 0; i < best_row->used[TEXT_AREA]; i++)
22537 {
22538 struct glyph *glyph = best_row->glyphs[TEXT_AREA] + i;
22539 int charpos = glyph->charpos;
22540
22541 if (BUFFERP (glyph->object))
22542 {
22543 if (charpos == pos)
22544 {
22545 *hpos = i;
22546 *vpos = best_row_vpos;
22547 *x = current_x;
22548 *y = best_row->y;
22549 return 1;
22550 }
22551 else if (charpos > pos)
22552 break;
22553 }
22554 else if (EQ (glyph->object, stop))
22555 break;
22556
22557 if (charpos > 0)
22558 lastcol = i;
22559 current_x += glyph->pixel_width;
22560 }
22561
22562 /* If we're looking for the end of the buffer,
22563 and we didn't find it in the line we scanned,
22564 use the start of the following line. */
22565 if (maybe_next_line_p)
22566 {
22567 ++best_row;
22568 ++best_row_vpos;
22569 lastcol = 0;
22570 current_x = best_row->x;
22571 }
22572
22573 *vpos = best_row_vpos;
22574 *hpos = lastcol + 1;
22575 *x = current_x;
22576 *y = best_row->y;
22577 return 0;
22578 }
22579
22580 #endif /* not 1 */
22581
22582
22583 /* Find the position of the glyph for position POS in OBJECT in
22584 window W's current matrix, and return in *X, *Y the pixel
22585 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
22586
22587 RIGHT_P non-zero means return the position of the right edge of the
22588 glyph, RIGHT_P zero means return the left edge position.
22589
22590 If no glyph for POS exists in the matrix, return the position of
22591 the glyph with the next smaller position that is in the matrix, if
22592 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
22593 exists in the matrix, return the position of the glyph with the
22594 next larger position in OBJECT.
22595
22596 Value is non-zero if a glyph was found. */
22597
22598 static int
22599 fast_find_string_pos (w, pos, object, hpos, vpos, x, y, right_p)
22600 struct window *w;
22601 EMACS_INT pos;
22602 Lisp_Object object;
22603 int *hpos, *vpos, *x, *y;
22604 int right_p;
22605 {
22606 int yb = window_text_bottom_y (w);
22607 struct glyph_row *r;
22608 struct glyph *best_glyph = NULL;
22609 struct glyph_row *best_row = NULL;
22610 int best_x = 0;
22611
22612 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
22613 r->enabled_p && r->y < yb;
22614 ++r)
22615 {
22616 struct glyph *g = r->glyphs[TEXT_AREA];
22617 struct glyph *e = g + r->used[TEXT_AREA];
22618 int gx;
22619
22620 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
22621 if (EQ (g->object, object))
22622 {
22623 if (g->charpos == pos)
22624 {
22625 best_glyph = g;
22626 best_x = gx;
22627 best_row = r;
22628 goto found;
22629 }
22630 else if (best_glyph == NULL
22631 || ((eabs (g->charpos - pos)
22632 < eabs (best_glyph->charpos - pos))
22633 && (right_p
22634 ? g->charpos < pos
22635 : g->charpos > pos)))
22636 {
22637 best_glyph = g;
22638 best_x = gx;
22639 best_row = r;
22640 }
22641 }
22642 }
22643
22644 found:
22645
22646 if (best_glyph)
22647 {
22648 *x = best_x;
22649 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
22650
22651 if (right_p)
22652 {
22653 *x += best_glyph->pixel_width;
22654 ++*hpos;
22655 }
22656
22657 *y = best_row->y;
22658 *vpos = best_row - w->current_matrix->rows;
22659 }
22660
22661 return best_glyph != NULL;
22662 }
22663
22664
22665 /* See if position X, Y is within a hot-spot of an image. */
22666
22667 static int
22668 on_hot_spot_p (hot_spot, x, y)
22669 Lisp_Object hot_spot;
22670 int x, y;
22671 {
22672 if (!CONSP (hot_spot))
22673 return 0;
22674
22675 if (EQ (XCAR (hot_spot), Qrect))
22676 {
22677 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
22678 Lisp_Object rect = XCDR (hot_spot);
22679 Lisp_Object tem;
22680 if (!CONSP (rect))
22681 return 0;
22682 if (!CONSP (XCAR (rect)))
22683 return 0;
22684 if (!CONSP (XCDR (rect)))
22685 return 0;
22686 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
22687 return 0;
22688 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
22689 return 0;
22690 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
22691 return 0;
22692 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
22693 return 0;
22694 return 1;
22695 }
22696 else if (EQ (XCAR (hot_spot), Qcircle))
22697 {
22698 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
22699 Lisp_Object circ = XCDR (hot_spot);
22700 Lisp_Object lr, lx0, ly0;
22701 if (CONSP (circ)
22702 && CONSP (XCAR (circ))
22703 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
22704 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
22705 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
22706 {
22707 double r = XFLOATINT (lr);
22708 double dx = XINT (lx0) - x;
22709 double dy = XINT (ly0) - y;
22710 return (dx * dx + dy * dy <= r * r);
22711 }
22712 }
22713 else if (EQ (XCAR (hot_spot), Qpoly))
22714 {
22715 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
22716 if (VECTORP (XCDR (hot_spot)))
22717 {
22718 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
22719 Lisp_Object *poly = v->contents;
22720 int n = v->size;
22721 int i;
22722 int inside = 0;
22723 Lisp_Object lx, ly;
22724 int x0, y0;
22725
22726 /* Need an even number of coordinates, and at least 3 edges. */
22727 if (n < 6 || n & 1)
22728 return 0;
22729
22730 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
22731 If count is odd, we are inside polygon. Pixels on edges
22732 may or may not be included depending on actual geometry of the
22733 polygon. */
22734 if ((lx = poly[n-2], !INTEGERP (lx))
22735 || (ly = poly[n-1], !INTEGERP (lx)))
22736 return 0;
22737 x0 = XINT (lx), y0 = XINT (ly);
22738 for (i = 0; i < n; i += 2)
22739 {
22740 int x1 = x0, y1 = y0;
22741 if ((lx = poly[i], !INTEGERP (lx))
22742 || (ly = poly[i+1], !INTEGERP (ly)))
22743 return 0;
22744 x0 = XINT (lx), y0 = XINT (ly);
22745
22746 /* Does this segment cross the X line? */
22747 if (x0 >= x)
22748 {
22749 if (x1 >= x)
22750 continue;
22751 }
22752 else if (x1 < x)
22753 continue;
22754 if (y > y0 && y > y1)
22755 continue;
22756 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
22757 inside = !inside;
22758 }
22759 return inside;
22760 }
22761 }
22762 return 0;
22763 }
22764
22765 Lisp_Object
22766 find_hot_spot (map, x, y)
22767 Lisp_Object map;
22768 int x, y;
22769 {
22770 while (CONSP (map))
22771 {
22772 if (CONSP (XCAR (map))
22773 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
22774 return XCAR (map);
22775 map = XCDR (map);
22776 }
22777
22778 return Qnil;
22779 }
22780
22781 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
22782 3, 3, 0,
22783 doc: /* Lookup in image map MAP coordinates X and Y.
22784 An image map is an alist where each element has the format (AREA ID PLIST).
22785 An AREA is specified as either a rectangle, a circle, or a polygon:
22786 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
22787 pixel coordinates of the upper left and bottom right corners.
22788 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
22789 and the radius of the circle; r may be a float or integer.
22790 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
22791 vector describes one corner in the polygon.
22792 Returns the alist element for the first matching AREA in MAP. */)
22793 (map, x, y)
22794 Lisp_Object map;
22795 Lisp_Object x, y;
22796 {
22797 if (NILP (map))
22798 return Qnil;
22799
22800 CHECK_NUMBER (x);
22801 CHECK_NUMBER (y);
22802
22803 return find_hot_spot (map, XINT (x), XINT (y));
22804 }
22805
22806
22807 /* Display frame CURSOR, optionally using shape defined by POINTER. */
22808 static void
22809 define_frame_cursor1 (f, cursor, pointer)
22810 struct frame *f;
22811 Cursor cursor;
22812 Lisp_Object pointer;
22813 {
22814 /* Do not change cursor shape while dragging mouse. */
22815 if (!NILP (do_mouse_tracking))
22816 return;
22817
22818 if (!NILP (pointer))
22819 {
22820 if (EQ (pointer, Qarrow))
22821 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
22822 else if (EQ (pointer, Qhand))
22823 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
22824 else if (EQ (pointer, Qtext))
22825 cursor = FRAME_X_OUTPUT (f)->text_cursor;
22826 else if (EQ (pointer, intern ("hdrag")))
22827 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
22828 #ifdef HAVE_X_WINDOWS
22829 else if (EQ (pointer, intern ("vdrag")))
22830 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
22831 #endif
22832 else if (EQ (pointer, intern ("hourglass")))
22833 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
22834 else if (EQ (pointer, Qmodeline))
22835 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
22836 else
22837 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
22838 }
22839
22840 if (cursor != No_Cursor)
22841 FRAME_RIF (f)->define_frame_cursor (f, cursor);
22842 }
22843
22844 /* Take proper action when mouse has moved to the mode or header line
22845 or marginal area AREA of window W, x-position X and y-position Y.
22846 X is relative to the start of the text display area of W, so the
22847 width of bitmap areas and scroll bars must be subtracted to get a
22848 position relative to the start of the mode line. */
22849
22850 static void
22851 note_mode_line_or_margin_highlight (window, x, y, area)
22852 Lisp_Object window;
22853 int x, y;
22854 enum window_part area;
22855 {
22856 struct window *w = XWINDOW (window);
22857 struct frame *f = XFRAME (w->frame);
22858 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
22859 Cursor cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
22860 Lisp_Object pointer = Qnil;
22861 int charpos, dx, dy, width, height;
22862 Lisp_Object string, object = Qnil;
22863 Lisp_Object pos, help;
22864
22865 Lisp_Object mouse_face;
22866 int original_x_pixel = x;
22867 struct glyph * glyph = NULL, * row_start_glyph = NULL;
22868 struct glyph_row *row;
22869
22870 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
22871 {
22872 int x0;
22873 struct glyph *end;
22874
22875 string = mode_line_string (w, area, &x, &y, &charpos,
22876 &object, &dx, &dy, &width, &height);
22877
22878 row = (area == ON_MODE_LINE
22879 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
22880 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
22881
22882 /* Find glyph */
22883 if (row->mode_line_p && row->enabled_p)
22884 {
22885 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
22886 end = glyph + row->used[TEXT_AREA];
22887
22888 for (x0 = original_x_pixel;
22889 glyph < end && x0 >= glyph->pixel_width;
22890 ++glyph)
22891 x0 -= glyph->pixel_width;
22892
22893 if (glyph >= end)
22894 glyph = NULL;
22895 }
22896 }
22897 else
22898 {
22899 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
22900 string = marginal_area_string (w, area, &x, &y, &charpos,
22901 &object, &dx, &dy, &width, &height);
22902 }
22903
22904 help = Qnil;
22905
22906 if (IMAGEP (object))
22907 {
22908 Lisp_Object image_map, hotspot;
22909 if ((image_map = Fplist_get (XCDR (object), QCmap),
22910 !NILP (image_map))
22911 && (hotspot = find_hot_spot (image_map, dx, dy),
22912 CONSP (hotspot))
22913 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
22914 {
22915 Lisp_Object area_id, plist;
22916
22917 area_id = XCAR (hotspot);
22918 /* Could check AREA_ID to see if we enter/leave this hot-spot.
22919 If so, we could look for mouse-enter, mouse-leave
22920 properties in PLIST (and do something...). */
22921 hotspot = XCDR (hotspot);
22922 if (CONSP (hotspot)
22923 && (plist = XCAR (hotspot), CONSP (plist)))
22924 {
22925 pointer = Fplist_get (plist, Qpointer);
22926 if (NILP (pointer))
22927 pointer = Qhand;
22928 help = Fplist_get (plist, Qhelp_echo);
22929 if (!NILP (help))
22930 {
22931 help_echo_string = help;
22932 /* Is this correct? ++kfs */
22933 XSETWINDOW (help_echo_window, w);
22934 help_echo_object = w->buffer;
22935 help_echo_pos = charpos;
22936 }
22937 }
22938 }
22939 if (NILP (pointer))
22940 pointer = Fplist_get (XCDR (object), QCpointer);
22941 }
22942
22943 if (STRINGP (string))
22944 {
22945 pos = make_number (charpos);
22946 /* If we're on a string with `help-echo' text property, arrange
22947 for the help to be displayed. This is done by setting the
22948 global variable help_echo_string to the help string. */
22949 if (NILP (help))
22950 {
22951 help = Fget_text_property (pos, Qhelp_echo, string);
22952 if (!NILP (help))
22953 {
22954 help_echo_string = help;
22955 XSETWINDOW (help_echo_window, w);
22956 help_echo_object = string;
22957 help_echo_pos = charpos;
22958 }
22959 }
22960
22961 if (NILP (pointer))
22962 pointer = Fget_text_property (pos, Qpointer, string);
22963
22964 /* Change the mouse pointer according to what is under X/Y. */
22965 if (NILP (pointer) && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
22966 {
22967 Lisp_Object map;
22968 map = Fget_text_property (pos, Qlocal_map, string);
22969 if (!KEYMAPP (map))
22970 map = Fget_text_property (pos, Qkeymap, string);
22971 if (!KEYMAPP (map))
22972 cursor = dpyinfo->vertical_scroll_bar_cursor;
22973 }
22974
22975 /* Change the mouse face according to what is under X/Y. */
22976 mouse_face = Fget_text_property (pos, Qmouse_face, string);
22977 if (!NILP (mouse_face)
22978 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
22979 && glyph)
22980 {
22981 Lisp_Object b, e;
22982
22983 struct glyph * tmp_glyph;
22984
22985 int gpos;
22986 int gseq_length;
22987 int total_pixel_width;
22988 int ignore;
22989
22990 int vpos, hpos;
22991
22992 b = Fprevious_single_property_change (make_number (charpos + 1),
22993 Qmouse_face, string, Qnil);
22994 if (NILP (b))
22995 b = make_number (0);
22996
22997 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
22998 if (NILP (e))
22999 e = make_number (SCHARS (string));
23000
23001 /* Calculate the position(glyph position: GPOS) of GLYPH in
23002 displayed string. GPOS is different from CHARPOS.
23003
23004 CHARPOS is the position of glyph in internal string
23005 object. A mode line string format has structures which
23006 is converted to a flatten by emacs lisp interpreter.
23007 The internal string is an element of the structures.
23008 The displayed string is the flatten string. */
23009 gpos = 0;
23010 if (glyph > row_start_glyph)
23011 {
23012 tmp_glyph = glyph - 1;
23013 while (tmp_glyph >= row_start_glyph
23014 && tmp_glyph->charpos >= XINT (b)
23015 && EQ (tmp_glyph->object, glyph->object))
23016 {
23017 tmp_glyph--;
23018 gpos++;
23019 }
23020 }
23021
23022 /* Calculate the lenght(glyph sequence length: GSEQ_LENGTH) of
23023 displayed string holding GLYPH.
23024
23025 GSEQ_LENGTH is different from SCHARS (STRING).
23026 SCHARS (STRING) returns the length of the internal string. */
23027 for (tmp_glyph = glyph, gseq_length = gpos;
23028 tmp_glyph->charpos < XINT (e);
23029 tmp_glyph++, gseq_length++)
23030 {
23031 if (!EQ (tmp_glyph->object, glyph->object))
23032 break;
23033 }
23034
23035 total_pixel_width = 0;
23036 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
23037 total_pixel_width += tmp_glyph->pixel_width;
23038
23039 /* Pre calculation of re-rendering position */
23040 vpos = (x - gpos);
23041 hpos = (area == ON_MODE_LINE
23042 ? (w->current_matrix)->nrows - 1
23043 : 0);
23044
23045 /* If the re-rendering position is included in the last
23046 re-rendering area, we should do nothing. */
23047 if ( EQ (window, dpyinfo->mouse_face_window)
23048 && dpyinfo->mouse_face_beg_col <= vpos
23049 && vpos < dpyinfo->mouse_face_end_col
23050 && dpyinfo->mouse_face_beg_row == hpos )
23051 return;
23052
23053 if (clear_mouse_face (dpyinfo))
23054 cursor = No_Cursor;
23055
23056 dpyinfo->mouse_face_beg_col = vpos;
23057 dpyinfo->mouse_face_beg_row = hpos;
23058
23059 dpyinfo->mouse_face_beg_x = original_x_pixel - (total_pixel_width + dx);
23060 dpyinfo->mouse_face_beg_y = 0;
23061
23062 dpyinfo->mouse_face_end_col = vpos + gseq_length;
23063 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_beg_row;
23064
23065 dpyinfo->mouse_face_end_x = 0;
23066 dpyinfo->mouse_face_end_y = 0;
23067
23068 dpyinfo->mouse_face_past_end = 0;
23069 dpyinfo->mouse_face_window = window;
23070
23071 dpyinfo->mouse_face_face_id = face_at_string_position (w, string,
23072 charpos,
23073 0, 0, 0, &ignore,
23074 glyph->face_id, 1);
23075 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
23076
23077 if (NILP (pointer))
23078 pointer = Qhand;
23079 }
23080 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
23081 clear_mouse_face (dpyinfo);
23082 }
23083 define_frame_cursor1 (f, cursor, pointer);
23084 }
23085
23086
23087 /* EXPORT:
23088 Take proper action when the mouse has moved to position X, Y on
23089 frame F as regards highlighting characters that have mouse-face
23090 properties. Also de-highlighting chars where the mouse was before.
23091 X and Y can be negative or out of range. */
23092
23093 void
23094 note_mouse_highlight (f, x, y)
23095 struct frame *f;
23096 int x, y;
23097 {
23098 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
23099 enum window_part part;
23100 Lisp_Object window;
23101 struct window *w;
23102 Cursor cursor = No_Cursor;
23103 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
23104 struct buffer *b;
23105
23106 /* When a menu is active, don't highlight because this looks odd. */
23107 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (MAC_OS)
23108 if (popup_activated ())
23109 return;
23110 #endif
23111
23112 if (NILP (Vmouse_highlight)
23113 || !f->glyphs_initialized_p)
23114 return;
23115
23116 dpyinfo->mouse_face_mouse_x = x;
23117 dpyinfo->mouse_face_mouse_y = y;
23118 dpyinfo->mouse_face_mouse_frame = f;
23119
23120 if (dpyinfo->mouse_face_defer)
23121 return;
23122
23123 if (gc_in_progress)
23124 {
23125 dpyinfo->mouse_face_deferred_gc = 1;
23126 return;
23127 }
23128
23129 /* Which window is that in? */
23130 window = window_from_coordinates (f, x, y, &part, 0, 0, 1);
23131
23132 /* If we were displaying active text in another window, clear that.
23133 Also clear if we move out of text area in same window. */
23134 if (! EQ (window, dpyinfo->mouse_face_window)
23135 || (part != ON_TEXT && part != ON_MODE_LINE && part != ON_HEADER_LINE
23136 && !NILP (dpyinfo->mouse_face_window)))
23137 clear_mouse_face (dpyinfo);
23138
23139 /* Not on a window -> return. */
23140 if (!WINDOWP (window))
23141 return;
23142
23143 /* Reset help_echo_string. It will get recomputed below. */
23144 help_echo_string = Qnil;
23145
23146 /* Convert to window-relative pixel coordinates. */
23147 w = XWINDOW (window);
23148 frame_to_window_pixel_xy (w, &x, &y);
23149
23150 /* Handle tool-bar window differently since it doesn't display a
23151 buffer. */
23152 if (EQ (window, f->tool_bar_window))
23153 {
23154 note_tool_bar_highlight (f, x, y);
23155 return;
23156 }
23157
23158 /* Mouse is on the mode, header line or margin? */
23159 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
23160 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
23161 {
23162 note_mode_line_or_margin_highlight (window, x, y, part);
23163 return;
23164 }
23165
23166 if (part == ON_VERTICAL_BORDER)
23167 {
23168 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
23169 help_echo_string = build_string ("drag-mouse-1: resize");
23170 }
23171 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
23172 || part == ON_SCROLL_BAR)
23173 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
23174 else
23175 cursor = FRAME_X_OUTPUT (f)->text_cursor;
23176
23177 /* Are we in a window whose display is up to date?
23178 And verify the buffer's text has not changed. */
23179 b = XBUFFER (w->buffer);
23180 if (part == ON_TEXT
23181 && EQ (w->window_end_valid, w->buffer)
23182 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
23183 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
23184 {
23185 int hpos, vpos, pos, i, dx, dy, area;
23186 struct glyph *glyph;
23187 Lisp_Object object;
23188 Lisp_Object mouse_face = Qnil, overlay = Qnil, position;
23189 Lisp_Object *overlay_vec = NULL;
23190 int noverlays;
23191 struct buffer *obuf;
23192 int obegv, ozv, same_region;
23193
23194 /* Find the glyph under X/Y. */
23195 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
23196
23197 /* Look for :pointer property on image. */
23198 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
23199 {
23200 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
23201 if (img != NULL && IMAGEP (img->spec))
23202 {
23203 Lisp_Object image_map, hotspot;
23204 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
23205 !NILP (image_map))
23206 && (hotspot = find_hot_spot (image_map,
23207 glyph->slice.x + dx,
23208 glyph->slice.y + dy),
23209 CONSP (hotspot))
23210 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
23211 {
23212 Lisp_Object area_id, plist;
23213
23214 area_id = XCAR (hotspot);
23215 /* Could check AREA_ID to see if we enter/leave this hot-spot.
23216 If so, we could look for mouse-enter, mouse-leave
23217 properties in PLIST (and do something...). */
23218 hotspot = XCDR (hotspot);
23219 if (CONSP (hotspot)
23220 && (plist = XCAR (hotspot), CONSP (plist)))
23221 {
23222 pointer = Fplist_get (plist, Qpointer);
23223 if (NILP (pointer))
23224 pointer = Qhand;
23225 help_echo_string = Fplist_get (plist, Qhelp_echo);
23226 if (!NILP (help_echo_string))
23227 {
23228 help_echo_window = window;
23229 help_echo_object = glyph->object;
23230 help_echo_pos = glyph->charpos;
23231 }
23232 }
23233 }
23234 if (NILP (pointer))
23235 pointer = Fplist_get (XCDR (img->spec), QCpointer);
23236 }
23237 }
23238
23239 /* Clear mouse face if X/Y not over text. */
23240 if (glyph == NULL
23241 || area != TEXT_AREA
23242 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p)
23243 {
23244 if (clear_mouse_face (dpyinfo))
23245 cursor = No_Cursor;
23246 if (NILP (pointer))
23247 {
23248 if (area != TEXT_AREA)
23249 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
23250 else
23251 pointer = Vvoid_text_area_pointer;
23252 }
23253 goto set_cursor;
23254 }
23255
23256 pos = glyph->charpos;
23257 object = glyph->object;
23258 if (!STRINGP (object) && !BUFFERP (object))
23259 goto set_cursor;
23260
23261 /* If we get an out-of-range value, return now; avoid an error. */
23262 if (BUFFERP (object) && pos > BUF_Z (b))
23263 goto set_cursor;
23264
23265 /* Make the window's buffer temporarily current for
23266 overlays_at and compute_char_face. */
23267 obuf = current_buffer;
23268 current_buffer = b;
23269 obegv = BEGV;
23270 ozv = ZV;
23271 BEGV = BEG;
23272 ZV = Z;
23273
23274 /* Is this char mouse-active or does it have help-echo? */
23275 position = make_number (pos);
23276
23277 if (BUFFERP (object))
23278 {
23279 /* Put all the overlays we want in a vector in overlay_vec. */
23280 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
23281 /* Sort overlays into increasing priority order. */
23282 noverlays = sort_overlays (overlay_vec, noverlays, w);
23283 }
23284 else
23285 noverlays = 0;
23286
23287 same_region = (EQ (window, dpyinfo->mouse_face_window)
23288 && vpos >= dpyinfo->mouse_face_beg_row
23289 && vpos <= dpyinfo->mouse_face_end_row
23290 && (vpos > dpyinfo->mouse_face_beg_row
23291 || hpos >= dpyinfo->mouse_face_beg_col)
23292 && (vpos < dpyinfo->mouse_face_end_row
23293 || hpos < dpyinfo->mouse_face_end_col
23294 || dpyinfo->mouse_face_past_end));
23295
23296 if (same_region)
23297 cursor = No_Cursor;
23298
23299 /* Check mouse-face highlighting. */
23300 if (! same_region
23301 /* If there exists an overlay with mouse-face overlapping
23302 the one we are currently highlighting, we have to
23303 check if we enter the overlapping overlay, and then
23304 highlight only that. */
23305 || (OVERLAYP (dpyinfo->mouse_face_overlay)
23306 && mouse_face_overlay_overlaps (dpyinfo->mouse_face_overlay)))
23307 {
23308 /* Find the highest priority overlay that has a mouse-face
23309 property. */
23310 overlay = Qnil;
23311 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
23312 {
23313 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
23314 if (!NILP (mouse_face))
23315 overlay = overlay_vec[i];
23316 }
23317
23318 /* If we're actually highlighting the same overlay as
23319 before, there's no need to do that again. */
23320 if (!NILP (overlay)
23321 && EQ (overlay, dpyinfo->mouse_face_overlay))
23322 goto check_help_echo;
23323
23324 dpyinfo->mouse_face_overlay = overlay;
23325
23326 /* Clear the display of the old active region, if any. */
23327 if (clear_mouse_face (dpyinfo))
23328 cursor = No_Cursor;
23329
23330 /* If no overlay applies, get a text property. */
23331 if (NILP (overlay))
23332 mouse_face = Fget_text_property (position, Qmouse_face, object);
23333
23334 /* Handle the overlay case. */
23335 if (!NILP (overlay))
23336 {
23337 /* Find the range of text around this char that
23338 should be active. */
23339 Lisp_Object before, after;
23340 int ignore;
23341
23342 before = Foverlay_start (overlay);
23343 after = Foverlay_end (overlay);
23344 /* Record this as the current active region. */
23345 fast_find_position (w, XFASTINT (before),
23346 &dpyinfo->mouse_face_beg_col,
23347 &dpyinfo->mouse_face_beg_row,
23348 &dpyinfo->mouse_face_beg_x,
23349 &dpyinfo->mouse_face_beg_y, Qnil);
23350
23351 dpyinfo->mouse_face_past_end
23352 = !fast_find_position (w, XFASTINT (after),
23353 &dpyinfo->mouse_face_end_col,
23354 &dpyinfo->mouse_face_end_row,
23355 &dpyinfo->mouse_face_end_x,
23356 &dpyinfo->mouse_face_end_y, Qnil);
23357 dpyinfo->mouse_face_window = window;
23358
23359 dpyinfo->mouse_face_face_id
23360 = face_at_buffer_position (w, pos, 0, 0,
23361 &ignore, pos + 1,
23362 !dpyinfo->mouse_face_hidden);
23363
23364 /* Display it as active. */
23365 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
23366 cursor = No_Cursor;
23367 }
23368 /* Handle the text property case. */
23369 else if (!NILP (mouse_face) && BUFFERP (object))
23370 {
23371 /* Find the range of text around this char that
23372 should be active. */
23373 Lisp_Object before, after, beginning, end;
23374 int ignore;
23375
23376 beginning = Fmarker_position (w->start);
23377 end = make_number (BUF_Z (XBUFFER (object))
23378 - XFASTINT (w->window_end_pos));
23379 before
23380 = Fprevious_single_property_change (make_number (pos + 1),
23381 Qmouse_face,
23382 object, beginning);
23383 after
23384 = Fnext_single_property_change (position, Qmouse_face,
23385 object, end);
23386
23387 /* Record this as the current active region. */
23388 fast_find_position (w, XFASTINT (before),
23389 &dpyinfo->mouse_face_beg_col,
23390 &dpyinfo->mouse_face_beg_row,
23391 &dpyinfo->mouse_face_beg_x,
23392 &dpyinfo->mouse_face_beg_y, Qnil);
23393 dpyinfo->mouse_face_past_end
23394 = !fast_find_position (w, XFASTINT (after),
23395 &dpyinfo->mouse_face_end_col,
23396 &dpyinfo->mouse_face_end_row,
23397 &dpyinfo->mouse_face_end_x,
23398 &dpyinfo->mouse_face_end_y, Qnil);
23399 dpyinfo->mouse_face_window = window;
23400
23401 if (BUFFERP (object))
23402 dpyinfo->mouse_face_face_id
23403 = face_at_buffer_position (w, pos, 0, 0,
23404 &ignore, pos + 1,
23405 !dpyinfo->mouse_face_hidden);
23406
23407 /* Display it as active. */
23408 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
23409 cursor = No_Cursor;
23410 }
23411 else if (!NILP (mouse_face) && STRINGP (object))
23412 {
23413 Lisp_Object b, e;
23414 int ignore;
23415
23416 b = Fprevious_single_property_change (make_number (pos + 1),
23417 Qmouse_face,
23418 object, Qnil);
23419 e = Fnext_single_property_change (position, Qmouse_face,
23420 object, Qnil);
23421 if (NILP (b))
23422 b = make_number (0);
23423 if (NILP (e))
23424 e = make_number (SCHARS (object) - 1);
23425
23426 fast_find_string_pos (w, XINT (b), object,
23427 &dpyinfo->mouse_face_beg_col,
23428 &dpyinfo->mouse_face_beg_row,
23429 &dpyinfo->mouse_face_beg_x,
23430 &dpyinfo->mouse_face_beg_y, 0);
23431 fast_find_string_pos (w, XINT (e), object,
23432 &dpyinfo->mouse_face_end_col,
23433 &dpyinfo->mouse_face_end_row,
23434 &dpyinfo->mouse_face_end_x,
23435 &dpyinfo->mouse_face_end_y, 1);
23436 dpyinfo->mouse_face_past_end = 0;
23437 dpyinfo->mouse_face_window = window;
23438 dpyinfo->mouse_face_face_id
23439 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
23440 glyph->face_id, 1);
23441 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
23442 cursor = No_Cursor;
23443 }
23444 else if (STRINGP (object) && NILP (mouse_face))
23445 {
23446 /* A string which doesn't have mouse-face, but
23447 the text ``under'' it might have. */
23448 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
23449 int start = MATRIX_ROW_START_CHARPOS (r);
23450
23451 pos = string_buffer_position (w, object, start);
23452 if (pos > 0)
23453 mouse_face = get_char_property_and_overlay (make_number (pos),
23454 Qmouse_face,
23455 w->buffer,
23456 &overlay);
23457 if (!NILP (mouse_face) && !NILP (overlay))
23458 {
23459 Lisp_Object before = Foverlay_start (overlay);
23460 Lisp_Object after = Foverlay_end (overlay);
23461 int ignore;
23462
23463 /* Note that we might not be able to find position
23464 BEFORE in the glyph matrix if the overlay is
23465 entirely covered by a `display' property. In
23466 this case, we overshoot. So let's stop in
23467 the glyph matrix before glyphs for OBJECT. */
23468 fast_find_position (w, XFASTINT (before),
23469 &dpyinfo->mouse_face_beg_col,
23470 &dpyinfo->mouse_face_beg_row,
23471 &dpyinfo->mouse_face_beg_x,
23472 &dpyinfo->mouse_face_beg_y,
23473 object);
23474
23475 dpyinfo->mouse_face_past_end
23476 = !fast_find_position (w, XFASTINT (after),
23477 &dpyinfo->mouse_face_end_col,
23478 &dpyinfo->mouse_face_end_row,
23479 &dpyinfo->mouse_face_end_x,
23480 &dpyinfo->mouse_face_end_y,
23481 Qnil);
23482 dpyinfo->mouse_face_window = window;
23483 dpyinfo->mouse_face_face_id
23484 = face_at_buffer_position (w, pos, 0, 0,
23485 &ignore, pos + 1,
23486 !dpyinfo->mouse_face_hidden);
23487
23488 /* Display it as active. */
23489 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
23490 cursor = No_Cursor;
23491 }
23492 }
23493 }
23494
23495 check_help_echo:
23496
23497 /* Look for a `help-echo' property. */
23498 if (NILP (help_echo_string)) {
23499 Lisp_Object help, overlay;
23500
23501 /* Check overlays first. */
23502 help = overlay = Qnil;
23503 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
23504 {
23505 overlay = overlay_vec[i];
23506 help = Foverlay_get (overlay, Qhelp_echo);
23507 }
23508
23509 if (!NILP (help))
23510 {
23511 help_echo_string = help;
23512 help_echo_window = window;
23513 help_echo_object = overlay;
23514 help_echo_pos = pos;
23515 }
23516 else
23517 {
23518 Lisp_Object object = glyph->object;
23519 int charpos = glyph->charpos;
23520
23521 /* Try text properties. */
23522 if (STRINGP (object)
23523 && charpos >= 0
23524 && charpos < SCHARS (object))
23525 {
23526 help = Fget_text_property (make_number (charpos),
23527 Qhelp_echo, object);
23528 if (NILP (help))
23529 {
23530 /* If the string itself doesn't specify a help-echo,
23531 see if the buffer text ``under'' it does. */
23532 struct glyph_row *r
23533 = MATRIX_ROW (w->current_matrix, vpos);
23534 int start = MATRIX_ROW_START_CHARPOS (r);
23535 int pos = string_buffer_position (w, object, start);
23536 if (pos > 0)
23537 {
23538 help = Fget_char_property (make_number (pos),
23539 Qhelp_echo, w->buffer);
23540 if (!NILP (help))
23541 {
23542 charpos = pos;
23543 object = w->buffer;
23544 }
23545 }
23546 }
23547 }
23548 else if (BUFFERP (object)
23549 && charpos >= BEGV
23550 && charpos < ZV)
23551 help = Fget_text_property (make_number (charpos), Qhelp_echo,
23552 object);
23553
23554 if (!NILP (help))
23555 {
23556 help_echo_string = help;
23557 help_echo_window = window;
23558 help_echo_object = object;
23559 help_echo_pos = charpos;
23560 }
23561 }
23562 }
23563
23564 /* Look for a `pointer' property. */
23565 if (NILP (pointer))
23566 {
23567 /* Check overlays first. */
23568 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
23569 pointer = Foverlay_get (overlay_vec[i], Qpointer);
23570
23571 if (NILP (pointer))
23572 {
23573 Lisp_Object object = glyph->object;
23574 int charpos = glyph->charpos;
23575
23576 /* Try text properties. */
23577 if (STRINGP (object)
23578 && charpos >= 0
23579 && charpos < SCHARS (object))
23580 {
23581 pointer = Fget_text_property (make_number (charpos),
23582 Qpointer, object);
23583 if (NILP (pointer))
23584 {
23585 /* If the string itself doesn't specify a pointer,
23586 see if the buffer text ``under'' it does. */
23587 struct glyph_row *r
23588 = MATRIX_ROW (w->current_matrix, vpos);
23589 int start = MATRIX_ROW_START_CHARPOS (r);
23590 int pos = string_buffer_position (w, object, start);
23591 if (pos > 0)
23592 pointer = Fget_char_property (make_number (pos),
23593 Qpointer, w->buffer);
23594 }
23595 }
23596 else if (BUFFERP (object)
23597 && charpos >= BEGV
23598 && charpos < ZV)
23599 pointer = Fget_text_property (make_number (charpos),
23600 Qpointer, object);
23601 }
23602 }
23603
23604 BEGV = obegv;
23605 ZV = ozv;
23606 current_buffer = obuf;
23607 }
23608
23609 set_cursor:
23610
23611 define_frame_cursor1 (f, cursor, pointer);
23612 }
23613
23614
23615 /* EXPORT for RIF:
23616 Clear any mouse-face on window W. This function is part of the
23617 redisplay interface, and is called from try_window_id and similar
23618 functions to ensure the mouse-highlight is off. */
23619
23620 void
23621 x_clear_window_mouse_face (w)
23622 struct window *w;
23623 {
23624 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
23625 Lisp_Object window;
23626
23627 BLOCK_INPUT;
23628 XSETWINDOW (window, w);
23629 if (EQ (window, dpyinfo->mouse_face_window))
23630 clear_mouse_face (dpyinfo);
23631 UNBLOCK_INPUT;
23632 }
23633
23634
23635 /* EXPORT:
23636 Just discard the mouse face information for frame F, if any.
23637 This is used when the size of F is changed. */
23638
23639 void
23640 cancel_mouse_face (f)
23641 struct frame *f;
23642 {
23643 Lisp_Object window;
23644 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
23645
23646 window = dpyinfo->mouse_face_window;
23647 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
23648 {
23649 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
23650 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
23651 dpyinfo->mouse_face_window = Qnil;
23652 }
23653 }
23654
23655
23656 #endif /* HAVE_WINDOW_SYSTEM */
23657
23658 \f
23659 /***********************************************************************
23660 Exposure Events
23661 ***********************************************************************/
23662
23663 #ifdef HAVE_WINDOW_SYSTEM
23664
23665 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
23666 which intersects rectangle R. R is in window-relative coordinates. */
23667
23668 static void
23669 expose_area (w, row, r, area)
23670 struct window *w;
23671 struct glyph_row *row;
23672 XRectangle *r;
23673 enum glyph_row_area area;
23674 {
23675 struct glyph *first = row->glyphs[area];
23676 struct glyph *end = row->glyphs[area] + row->used[area];
23677 struct glyph *last;
23678 int first_x, start_x, x;
23679
23680 if (area == TEXT_AREA && row->fill_line_p)
23681 /* If row extends face to end of line write the whole line. */
23682 draw_glyphs (w, 0, row, area,
23683 0, row->used[area],
23684 DRAW_NORMAL_TEXT, 0);
23685 else
23686 {
23687 /* Set START_X to the window-relative start position for drawing glyphs of
23688 AREA. The first glyph of the text area can be partially visible.
23689 The first glyphs of other areas cannot. */
23690 start_x = window_box_left_offset (w, area);
23691 x = start_x;
23692 if (area == TEXT_AREA)
23693 x += row->x;
23694
23695 /* Find the first glyph that must be redrawn. */
23696 while (first < end
23697 && x + first->pixel_width < r->x)
23698 {
23699 x += first->pixel_width;
23700 ++first;
23701 }
23702
23703 /* Find the last one. */
23704 last = first;
23705 first_x = x;
23706 while (last < end
23707 && x < r->x + r->width)
23708 {
23709 x += last->pixel_width;
23710 ++last;
23711 }
23712
23713 /* Repaint. */
23714 if (last > first)
23715 draw_glyphs (w, first_x - start_x, row, area,
23716 first - row->glyphs[area], last - row->glyphs[area],
23717 DRAW_NORMAL_TEXT, 0);
23718 }
23719 }
23720
23721
23722 /* Redraw the parts of the glyph row ROW on window W intersecting
23723 rectangle R. R is in window-relative coordinates. Value is
23724 non-zero if mouse-face was overwritten. */
23725
23726 static int
23727 expose_line (w, row, r)
23728 struct window *w;
23729 struct glyph_row *row;
23730 XRectangle *r;
23731 {
23732 xassert (row->enabled_p);
23733
23734 if (row->mode_line_p || w->pseudo_window_p)
23735 draw_glyphs (w, 0, row, TEXT_AREA,
23736 0, row->used[TEXT_AREA],
23737 DRAW_NORMAL_TEXT, 0);
23738 else
23739 {
23740 if (row->used[LEFT_MARGIN_AREA])
23741 expose_area (w, row, r, LEFT_MARGIN_AREA);
23742 if (row->used[TEXT_AREA])
23743 expose_area (w, row, r, TEXT_AREA);
23744 if (row->used[RIGHT_MARGIN_AREA])
23745 expose_area (w, row, r, RIGHT_MARGIN_AREA);
23746 draw_row_fringe_bitmaps (w, row);
23747 }
23748
23749 return row->mouse_face_p;
23750 }
23751
23752
23753 /* Redraw those parts of glyphs rows during expose event handling that
23754 overlap other rows. Redrawing of an exposed line writes over parts
23755 of lines overlapping that exposed line; this function fixes that.
23756
23757 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
23758 row in W's current matrix that is exposed and overlaps other rows.
23759 LAST_OVERLAPPING_ROW is the last such row. */
23760
23761 static void
23762 expose_overlaps (w, first_overlapping_row, last_overlapping_row, r)
23763 struct window *w;
23764 struct glyph_row *first_overlapping_row;
23765 struct glyph_row *last_overlapping_row;
23766 XRectangle *r;
23767 {
23768 struct glyph_row *row;
23769
23770 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
23771 if (row->overlapping_p)
23772 {
23773 xassert (row->enabled_p && !row->mode_line_p);
23774
23775 row->clip = r;
23776 if (row->used[LEFT_MARGIN_AREA])
23777 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
23778
23779 if (row->used[TEXT_AREA])
23780 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
23781
23782 if (row->used[RIGHT_MARGIN_AREA])
23783 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
23784 row->clip = NULL;
23785 }
23786 }
23787
23788
23789 /* Return non-zero if W's cursor intersects rectangle R. */
23790
23791 static int
23792 phys_cursor_in_rect_p (w, r)
23793 struct window *w;
23794 XRectangle *r;
23795 {
23796 XRectangle cr, result;
23797 struct glyph *cursor_glyph;
23798
23799 cursor_glyph = get_phys_cursor_glyph (w);
23800 if (cursor_glyph)
23801 {
23802 /* r is relative to W's box, but w->phys_cursor.x is relative
23803 to left edge of W's TEXT area. Adjust it. */
23804 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
23805 cr.y = w->phys_cursor.y;
23806 cr.width = cursor_glyph->pixel_width;
23807 cr.height = w->phys_cursor_height;
23808 /* ++KFS: W32 version used W32-specific IntersectRect here, but
23809 I assume the effect is the same -- and this is portable. */
23810 return x_intersect_rectangles (&cr, r, &result);
23811 }
23812 /* If we don't understand the format, pretend we're not in the hot-spot. */
23813 return 0;
23814 }
23815
23816
23817 /* EXPORT:
23818 Draw a vertical window border to the right of window W if W doesn't
23819 have vertical scroll bars. */
23820
23821 void
23822 x_draw_vertical_border (w)
23823 struct window *w;
23824 {
23825 struct frame *f = XFRAME (WINDOW_FRAME (w));
23826
23827 /* We could do better, if we knew what type of scroll-bar the adjacent
23828 windows (on either side) have... But we don't :-(
23829 However, I think this works ok. ++KFS 2003-04-25 */
23830
23831 /* Redraw borders between horizontally adjacent windows. Don't
23832 do it for frames with vertical scroll bars because either the
23833 right scroll bar of a window, or the left scroll bar of its
23834 neighbor will suffice as a border. */
23835 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
23836 return;
23837
23838 if (!WINDOW_RIGHTMOST_P (w)
23839 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
23840 {
23841 int x0, x1, y0, y1;
23842
23843 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
23844 y1 -= 1;
23845
23846 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
23847 x1 -= 1;
23848
23849 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
23850 }
23851 else if (!WINDOW_LEFTMOST_P (w)
23852 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
23853 {
23854 int x0, x1, y0, y1;
23855
23856 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
23857 y1 -= 1;
23858
23859 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
23860 x0 -= 1;
23861
23862 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
23863 }
23864 }
23865
23866
23867 /* Redraw the part of window W intersection rectangle FR. Pixel
23868 coordinates in FR are frame-relative. Call this function with
23869 input blocked. Value is non-zero if the exposure overwrites
23870 mouse-face. */
23871
23872 static int
23873 expose_window (w, fr)
23874 struct window *w;
23875 XRectangle *fr;
23876 {
23877 struct frame *f = XFRAME (w->frame);
23878 XRectangle wr, r;
23879 int mouse_face_overwritten_p = 0;
23880
23881 /* If window is not yet fully initialized, do nothing. This can
23882 happen when toolkit scroll bars are used and a window is split.
23883 Reconfiguring the scroll bar will generate an expose for a newly
23884 created window. */
23885 if (w->current_matrix == NULL)
23886 return 0;
23887
23888 /* When we're currently updating the window, display and current
23889 matrix usually don't agree. Arrange for a thorough display
23890 later. */
23891 if (w == updated_window)
23892 {
23893 SET_FRAME_GARBAGED (f);
23894 return 0;
23895 }
23896
23897 /* Frame-relative pixel rectangle of W. */
23898 wr.x = WINDOW_LEFT_EDGE_X (w);
23899 wr.y = WINDOW_TOP_EDGE_Y (w);
23900 wr.width = WINDOW_TOTAL_WIDTH (w);
23901 wr.height = WINDOW_TOTAL_HEIGHT (w);
23902
23903 if (x_intersect_rectangles (fr, &wr, &r))
23904 {
23905 int yb = window_text_bottom_y (w);
23906 struct glyph_row *row;
23907 int cursor_cleared_p;
23908 struct glyph_row *first_overlapping_row, *last_overlapping_row;
23909
23910 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
23911 r.x, r.y, r.width, r.height));
23912
23913 /* Convert to window coordinates. */
23914 r.x -= WINDOW_LEFT_EDGE_X (w);
23915 r.y -= WINDOW_TOP_EDGE_Y (w);
23916
23917 /* Turn off the cursor. */
23918 if (!w->pseudo_window_p
23919 && phys_cursor_in_rect_p (w, &r))
23920 {
23921 x_clear_cursor (w);
23922 cursor_cleared_p = 1;
23923 }
23924 else
23925 cursor_cleared_p = 0;
23926
23927 /* Update lines intersecting rectangle R. */
23928 first_overlapping_row = last_overlapping_row = NULL;
23929 for (row = w->current_matrix->rows;
23930 row->enabled_p;
23931 ++row)
23932 {
23933 int y0 = row->y;
23934 int y1 = MATRIX_ROW_BOTTOM_Y (row);
23935
23936 if ((y0 >= r.y && y0 < r.y + r.height)
23937 || (y1 > r.y && y1 < r.y + r.height)
23938 || (r.y >= y0 && r.y < y1)
23939 || (r.y + r.height > y0 && r.y + r.height < y1))
23940 {
23941 /* A header line may be overlapping, but there is no need
23942 to fix overlapping areas for them. KFS 2005-02-12 */
23943 if (row->overlapping_p && !row->mode_line_p)
23944 {
23945 if (first_overlapping_row == NULL)
23946 first_overlapping_row = row;
23947 last_overlapping_row = row;
23948 }
23949
23950 row->clip = fr;
23951 if (expose_line (w, row, &r))
23952 mouse_face_overwritten_p = 1;
23953 row->clip = NULL;
23954 }
23955 else if (row->overlapping_p)
23956 {
23957 /* We must redraw a row overlapping the exposed area. */
23958 if (y0 < r.y
23959 ? y0 + row->phys_height > r.y
23960 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
23961 {
23962 if (first_overlapping_row == NULL)
23963 first_overlapping_row = row;
23964 last_overlapping_row = row;
23965 }
23966 }
23967
23968 if (y1 >= yb)
23969 break;
23970 }
23971
23972 /* Display the mode line if there is one. */
23973 if (WINDOW_WANTS_MODELINE_P (w)
23974 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
23975 row->enabled_p)
23976 && row->y < r.y + r.height)
23977 {
23978 if (expose_line (w, row, &r))
23979 mouse_face_overwritten_p = 1;
23980 }
23981
23982 if (!w->pseudo_window_p)
23983 {
23984 /* Fix the display of overlapping rows. */
23985 if (first_overlapping_row)
23986 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
23987 fr);
23988
23989 /* Draw border between windows. */
23990 x_draw_vertical_border (w);
23991
23992 /* Turn the cursor on again. */
23993 if (cursor_cleared_p)
23994 update_window_cursor (w, 1);
23995 }
23996 }
23997
23998 return mouse_face_overwritten_p;
23999 }
24000
24001
24002
24003 /* Redraw (parts) of all windows in the window tree rooted at W that
24004 intersect R. R contains frame pixel coordinates. Value is
24005 non-zero if the exposure overwrites mouse-face. */
24006
24007 static int
24008 expose_window_tree (w, r)
24009 struct window *w;
24010 XRectangle *r;
24011 {
24012 struct frame *f = XFRAME (w->frame);
24013 int mouse_face_overwritten_p = 0;
24014
24015 while (w && !FRAME_GARBAGED_P (f))
24016 {
24017 if (!NILP (w->hchild))
24018 mouse_face_overwritten_p
24019 |= expose_window_tree (XWINDOW (w->hchild), r);
24020 else if (!NILP (w->vchild))
24021 mouse_face_overwritten_p
24022 |= expose_window_tree (XWINDOW (w->vchild), r);
24023 else
24024 mouse_face_overwritten_p |= expose_window (w, r);
24025
24026 w = NILP (w->next) ? NULL : XWINDOW (w->next);
24027 }
24028
24029 return mouse_face_overwritten_p;
24030 }
24031
24032
24033 /* EXPORT:
24034 Redisplay an exposed area of frame F. X and Y are the upper-left
24035 corner of the exposed rectangle. W and H are width and height of
24036 the exposed area. All are pixel values. W or H zero means redraw
24037 the entire frame. */
24038
24039 void
24040 expose_frame (f, x, y, w, h)
24041 struct frame *f;
24042 int x, y, w, h;
24043 {
24044 XRectangle r;
24045 int mouse_face_overwritten_p = 0;
24046
24047 TRACE ((stderr, "expose_frame "));
24048
24049 /* No need to redraw if frame will be redrawn soon. */
24050 if (FRAME_GARBAGED_P (f))
24051 {
24052 TRACE ((stderr, " garbaged\n"));
24053 return;
24054 }
24055
24056 /* If basic faces haven't been realized yet, there is no point in
24057 trying to redraw anything. This can happen when we get an expose
24058 event while Emacs is starting, e.g. by moving another window. */
24059 if (FRAME_FACE_CACHE (f) == NULL
24060 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
24061 {
24062 TRACE ((stderr, " no faces\n"));
24063 return;
24064 }
24065
24066 if (w == 0 || h == 0)
24067 {
24068 r.x = r.y = 0;
24069 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
24070 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
24071 }
24072 else
24073 {
24074 r.x = x;
24075 r.y = y;
24076 r.width = w;
24077 r.height = h;
24078 }
24079
24080 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
24081 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
24082
24083 if (WINDOWP (f->tool_bar_window))
24084 mouse_face_overwritten_p
24085 |= expose_window (XWINDOW (f->tool_bar_window), &r);
24086
24087 #ifdef HAVE_X_WINDOWS
24088 #ifndef MSDOS
24089 #ifndef USE_X_TOOLKIT
24090 if (WINDOWP (f->menu_bar_window))
24091 mouse_face_overwritten_p
24092 |= expose_window (XWINDOW (f->menu_bar_window), &r);
24093 #endif /* not USE_X_TOOLKIT */
24094 #endif
24095 #endif
24096
24097 /* Some window managers support a focus-follows-mouse style with
24098 delayed raising of frames. Imagine a partially obscured frame,
24099 and moving the mouse into partially obscured mouse-face on that
24100 frame. The visible part of the mouse-face will be highlighted,
24101 then the WM raises the obscured frame. With at least one WM, KDE
24102 2.1, Emacs is not getting any event for the raising of the frame
24103 (even tried with SubstructureRedirectMask), only Expose events.
24104 These expose events will draw text normally, i.e. not
24105 highlighted. Which means we must redo the highlight here.
24106 Subsume it under ``we love X''. --gerd 2001-08-15 */
24107 /* Included in Windows version because Windows most likely does not
24108 do the right thing if any third party tool offers
24109 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
24110 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
24111 {
24112 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
24113 if (f == dpyinfo->mouse_face_mouse_frame)
24114 {
24115 int x = dpyinfo->mouse_face_mouse_x;
24116 int y = dpyinfo->mouse_face_mouse_y;
24117 clear_mouse_face (dpyinfo);
24118 note_mouse_highlight (f, x, y);
24119 }
24120 }
24121 }
24122
24123
24124 /* EXPORT:
24125 Determine the intersection of two rectangles R1 and R2. Return
24126 the intersection in *RESULT. Value is non-zero if RESULT is not
24127 empty. */
24128
24129 int
24130 x_intersect_rectangles (r1, r2, result)
24131 XRectangle *r1, *r2, *result;
24132 {
24133 XRectangle *left, *right;
24134 XRectangle *upper, *lower;
24135 int intersection_p = 0;
24136
24137 /* Rearrange so that R1 is the left-most rectangle. */
24138 if (r1->x < r2->x)
24139 left = r1, right = r2;
24140 else
24141 left = r2, right = r1;
24142
24143 /* X0 of the intersection is right.x0, if this is inside R1,
24144 otherwise there is no intersection. */
24145 if (right->x <= left->x + left->width)
24146 {
24147 result->x = right->x;
24148
24149 /* The right end of the intersection is the minimum of the
24150 the right ends of left and right. */
24151 result->width = (min (left->x + left->width, right->x + right->width)
24152 - result->x);
24153
24154 /* Same game for Y. */
24155 if (r1->y < r2->y)
24156 upper = r1, lower = r2;
24157 else
24158 upper = r2, lower = r1;
24159
24160 /* The upper end of the intersection is lower.y0, if this is inside
24161 of upper. Otherwise, there is no intersection. */
24162 if (lower->y <= upper->y + upper->height)
24163 {
24164 result->y = lower->y;
24165
24166 /* The lower end of the intersection is the minimum of the lower
24167 ends of upper and lower. */
24168 result->height = (min (lower->y + lower->height,
24169 upper->y + upper->height)
24170 - result->y);
24171 intersection_p = 1;
24172 }
24173 }
24174
24175 return intersection_p;
24176 }
24177
24178 #endif /* HAVE_WINDOW_SYSTEM */
24179
24180 \f
24181 /***********************************************************************
24182 Initialization
24183 ***********************************************************************/
24184
24185 void
24186 syms_of_xdisp ()
24187 {
24188 Vwith_echo_area_save_vector = Qnil;
24189 staticpro (&Vwith_echo_area_save_vector);
24190
24191 Vmessage_stack = Qnil;
24192 staticpro (&Vmessage_stack);
24193
24194 Qinhibit_redisplay = intern ("inhibit-redisplay");
24195 staticpro (&Qinhibit_redisplay);
24196
24197 message_dolog_marker1 = Fmake_marker ();
24198 staticpro (&message_dolog_marker1);
24199 message_dolog_marker2 = Fmake_marker ();
24200 staticpro (&message_dolog_marker2);
24201 message_dolog_marker3 = Fmake_marker ();
24202 staticpro (&message_dolog_marker3);
24203
24204 #if GLYPH_DEBUG
24205 defsubr (&Sdump_frame_glyph_matrix);
24206 defsubr (&Sdump_glyph_matrix);
24207 defsubr (&Sdump_glyph_row);
24208 defsubr (&Sdump_tool_bar_row);
24209 defsubr (&Strace_redisplay);
24210 defsubr (&Strace_to_stderr);
24211 #endif
24212 #ifdef HAVE_WINDOW_SYSTEM
24213 defsubr (&Stool_bar_lines_needed);
24214 defsubr (&Slookup_image_map);
24215 #endif
24216 defsubr (&Sformat_mode_line);
24217 defsubr (&Sinvisible_p);
24218
24219 staticpro (&Qmenu_bar_update_hook);
24220 Qmenu_bar_update_hook = intern ("menu-bar-update-hook");
24221
24222 staticpro (&Qoverriding_terminal_local_map);
24223 Qoverriding_terminal_local_map = intern ("overriding-terminal-local-map");
24224
24225 staticpro (&Qoverriding_local_map);
24226 Qoverriding_local_map = intern ("overriding-local-map");
24227
24228 staticpro (&Qwindow_scroll_functions);
24229 Qwindow_scroll_functions = intern ("window-scroll-functions");
24230
24231 staticpro (&Qredisplay_end_trigger_functions);
24232 Qredisplay_end_trigger_functions = intern ("redisplay-end-trigger-functions");
24233
24234 staticpro (&Qinhibit_point_motion_hooks);
24235 Qinhibit_point_motion_hooks = intern ("inhibit-point-motion-hooks");
24236
24237 QCdata = intern (":data");
24238 staticpro (&QCdata);
24239 Qdisplay = intern ("display");
24240 staticpro (&Qdisplay);
24241 Qspace_width = intern ("space-width");
24242 staticpro (&Qspace_width);
24243 Qraise = intern ("raise");
24244 staticpro (&Qraise);
24245 Qslice = intern ("slice");
24246 staticpro (&Qslice);
24247 Qspace = intern ("space");
24248 staticpro (&Qspace);
24249 Qmargin = intern ("margin");
24250 staticpro (&Qmargin);
24251 Qpointer = intern ("pointer");
24252 staticpro (&Qpointer);
24253 Qleft_margin = intern ("left-margin");
24254 staticpro (&Qleft_margin);
24255 Qright_margin = intern ("right-margin");
24256 staticpro (&Qright_margin);
24257 Qcenter = intern ("center");
24258 staticpro (&Qcenter);
24259 Qline_height = intern ("line-height");
24260 staticpro (&Qline_height);
24261 QCalign_to = intern (":align-to");
24262 staticpro (&QCalign_to);
24263 QCrelative_width = intern (":relative-width");
24264 staticpro (&QCrelative_width);
24265 QCrelative_height = intern (":relative-height");
24266 staticpro (&QCrelative_height);
24267 QCeval = intern (":eval");
24268 staticpro (&QCeval);
24269 QCpropertize = intern (":propertize");
24270 staticpro (&QCpropertize);
24271 QCfile = intern (":file");
24272 staticpro (&QCfile);
24273 Qfontified = intern ("fontified");
24274 staticpro (&Qfontified);
24275 Qfontification_functions = intern ("fontification-functions");
24276 staticpro (&Qfontification_functions);
24277 Qtrailing_whitespace = intern ("trailing-whitespace");
24278 staticpro (&Qtrailing_whitespace);
24279 Qescape_glyph = intern ("escape-glyph");
24280 staticpro (&Qescape_glyph);
24281 Qnobreak_space = intern ("nobreak-space");
24282 staticpro (&Qnobreak_space);
24283 Qimage = intern ("image");
24284 staticpro (&Qimage);
24285 QCmap = intern (":map");
24286 staticpro (&QCmap);
24287 QCpointer = intern (":pointer");
24288 staticpro (&QCpointer);
24289 Qrect = intern ("rect");
24290 staticpro (&Qrect);
24291 Qcircle = intern ("circle");
24292 staticpro (&Qcircle);
24293 Qpoly = intern ("poly");
24294 staticpro (&Qpoly);
24295 Qmessage_truncate_lines = intern ("message-truncate-lines");
24296 staticpro (&Qmessage_truncate_lines);
24297 Qgrow_only = intern ("grow-only");
24298 staticpro (&Qgrow_only);
24299 Qinhibit_menubar_update = intern ("inhibit-menubar-update");
24300 staticpro (&Qinhibit_menubar_update);
24301 Qinhibit_eval_during_redisplay = intern ("inhibit-eval-during-redisplay");
24302 staticpro (&Qinhibit_eval_during_redisplay);
24303 Qposition = intern ("position");
24304 staticpro (&Qposition);
24305 Qbuffer_position = intern ("buffer-position");
24306 staticpro (&Qbuffer_position);
24307 Qobject = intern ("object");
24308 staticpro (&Qobject);
24309 Qbar = intern ("bar");
24310 staticpro (&Qbar);
24311 Qhbar = intern ("hbar");
24312 staticpro (&Qhbar);
24313 Qbox = intern ("box");
24314 staticpro (&Qbox);
24315 Qhollow = intern ("hollow");
24316 staticpro (&Qhollow);
24317 Qhand = intern ("hand");
24318 staticpro (&Qhand);
24319 Qarrow = intern ("arrow");
24320 staticpro (&Qarrow);
24321 Qtext = intern ("text");
24322 staticpro (&Qtext);
24323 Qrisky_local_variable = intern ("risky-local-variable");
24324 staticpro (&Qrisky_local_variable);
24325 Qinhibit_free_realized_faces = intern ("inhibit-free-realized-faces");
24326 staticpro (&Qinhibit_free_realized_faces);
24327
24328 list_of_error = Fcons (Fcons (intern ("error"),
24329 Fcons (intern ("void-variable"), Qnil)),
24330 Qnil);
24331 staticpro (&list_of_error);
24332
24333 Qlast_arrow_position = intern ("last-arrow-position");
24334 staticpro (&Qlast_arrow_position);
24335 Qlast_arrow_string = intern ("last-arrow-string");
24336 staticpro (&Qlast_arrow_string);
24337
24338 Qoverlay_arrow_string = intern ("overlay-arrow-string");
24339 staticpro (&Qoverlay_arrow_string);
24340 Qoverlay_arrow_bitmap = intern ("overlay-arrow-bitmap");
24341 staticpro (&Qoverlay_arrow_bitmap);
24342
24343 echo_buffer[0] = echo_buffer[1] = Qnil;
24344 staticpro (&echo_buffer[0]);
24345 staticpro (&echo_buffer[1]);
24346
24347 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
24348 staticpro (&echo_area_buffer[0]);
24349 staticpro (&echo_area_buffer[1]);
24350
24351 Vmessages_buffer_name = build_string ("*Messages*");
24352 staticpro (&Vmessages_buffer_name);
24353
24354 mode_line_proptrans_alist = Qnil;
24355 staticpro (&mode_line_proptrans_alist);
24356 mode_line_string_list = Qnil;
24357 staticpro (&mode_line_string_list);
24358 mode_line_string_face = Qnil;
24359 staticpro (&mode_line_string_face);
24360 mode_line_string_face_prop = Qnil;
24361 staticpro (&mode_line_string_face_prop);
24362 Vmode_line_unwind_vector = Qnil;
24363 staticpro (&Vmode_line_unwind_vector);
24364
24365 help_echo_string = Qnil;
24366 staticpro (&help_echo_string);
24367 help_echo_object = Qnil;
24368 staticpro (&help_echo_object);
24369 help_echo_window = Qnil;
24370 staticpro (&help_echo_window);
24371 previous_help_echo_string = Qnil;
24372 staticpro (&previous_help_echo_string);
24373 help_echo_pos = -1;
24374
24375 #ifdef HAVE_WINDOW_SYSTEM
24376 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
24377 doc: /* *Non-nil means draw block cursor as wide as the glyph under it.
24378 For example, if a block cursor is over a tab, it will be drawn as
24379 wide as that tab on the display. */);
24380 x_stretch_cursor_p = 0;
24381 #endif
24382
24383 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
24384 doc: /* *Non-nil means highlight trailing whitespace.
24385 The face used for trailing whitespace is `trailing-whitespace'. */);
24386 Vshow_trailing_whitespace = Qnil;
24387
24388 DEFVAR_LISP ("nobreak-char-display", &Vnobreak_char_display,
24389 doc: /* *Control highlighting of nobreak space and soft hyphen.
24390 A value of t means highlight the character itself (for nobreak space,
24391 use face `nobreak-space').
24392 A value of nil means no highlighting.
24393 Other values mean display the escape glyph followed by an ordinary
24394 space or ordinary hyphen. */);
24395 Vnobreak_char_display = Qt;
24396
24397 DEFVAR_LISP ("void-text-area-pointer", &Vvoid_text_area_pointer,
24398 doc: /* *The pointer shape to show in void text areas.
24399 A value of nil means to show the text pointer. Other options are `arrow',
24400 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
24401 Vvoid_text_area_pointer = Qarrow;
24402
24403 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
24404 doc: /* Non-nil means don't actually do any redisplay.
24405 This is used for internal purposes. */);
24406 Vinhibit_redisplay = Qnil;
24407
24408 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
24409 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
24410 Vglobal_mode_string = Qnil;
24411
24412 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
24413 doc: /* Marker for where to display an arrow on top of the buffer text.
24414 This must be the beginning of a line in order to work.
24415 See also `overlay-arrow-string'. */);
24416 Voverlay_arrow_position = Qnil;
24417
24418 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
24419 doc: /* String to display as an arrow in non-window frames.
24420 See also `overlay-arrow-position'. */);
24421 Voverlay_arrow_string = build_string ("=>");
24422
24423 DEFVAR_LISP ("overlay-arrow-variable-list", &Voverlay_arrow_variable_list,
24424 doc: /* List of variables (symbols) which hold markers for overlay arrows.
24425 The symbols on this list are examined during redisplay to determine
24426 where to display overlay arrows. */);
24427 Voverlay_arrow_variable_list
24428 = Fcons (intern ("overlay-arrow-position"), Qnil);
24429
24430 DEFVAR_INT ("scroll-step", &scroll_step,
24431 doc: /* *The number of lines to try scrolling a window by when point moves out.
24432 If that fails to bring point back on frame, point is centered instead.
24433 If this is zero, point is always centered after it moves off frame.
24434 If you want scrolling to always be a line at a time, you should set
24435 `scroll-conservatively' to a large value rather than set this to 1. */);
24436
24437 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
24438 doc: /* *Scroll up to this many lines, to bring point back on screen.
24439 A value of zero means to scroll the text to center point vertically
24440 in the window. */);
24441 scroll_conservatively = 0;
24442
24443 DEFVAR_INT ("scroll-margin", &scroll_margin,
24444 doc: /* *Number of lines of margin at the top and bottom of a window.
24445 Recenter the window whenever point gets within this many lines
24446 of the top or bottom of the window. */);
24447 scroll_margin = 0;
24448
24449 DEFVAR_LISP ("display-pixels-per-inch", &Vdisplay_pixels_per_inch,
24450 doc: /* Pixels per inch value for non-window system displays.
24451 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
24452 Vdisplay_pixels_per_inch = make_float (72.0);
24453
24454 #if GLYPH_DEBUG
24455 DEFVAR_INT ("debug-end-pos", &debug_end_pos, doc: /* Don't ask. */);
24456 #endif
24457
24458 DEFVAR_BOOL ("truncate-partial-width-windows",
24459 &truncate_partial_width_windows,
24460 doc: /* *Non-nil means truncate lines in all windows less than full frame wide. */);
24461 truncate_partial_width_windows = 1;
24462
24463 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
24464 doc: /* When nil, display the mode-line/header-line/menu-bar in the default face.
24465 Any other value means to use the appropriate face, `mode-line',
24466 `header-line', or `menu' respectively. */);
24467 mode_line_inverse_video = 1;
24468
24469 DEFVAR_LISP ("line-number-display-limit", &Vline_number_display_limit,
24470 doc: /* *Maximum buffer size for which line number should be displayed.
24471 If the buffer is bigger than this, the line number does not appear
24472 in the mode line. A value of nil means no limit. */);
24473 Vline_number_display_limit = Qnil;
24474
24475 DEFVAR_INT ("line-number-display-limit-width",
24476 &line_number_display_limit_width,
24477 doc: /* *Maximum line width (in characters) for line number display.
24478 If the average length of the lines near point is bigger than this, then the
24479 line number may be omitted from the mode line. */);
24480 line_number_display_limit_width = 200;
24481
24482 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
24483 doc: /* *Non-nil means highlight region even in nonselected windows. */);
24484 highlight_nonselected_windows = 0;
24485
24486 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
24487 doc: /* Non-nil if more than one frame is visible on this display.
24488 Minibuffer-only frames don't count, but iconified frames do.
24489 This variable is not guaranteed to be accurate except while processing
24490 `frame-title-format' and `icon-title-format'. */);
24491
24492 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
24493 doc: /* Template for displaying the title bar of visible frames.
24494 \(Assuming the window manager supports this feature.)
24495
24496 This variable has the same structure as `mode-line-format', except that
24497 the %c and %l constructs are ignored. It is used only on frames for
24498 which no explicit name has been set \(see `modify-frame-parameters'). */);
24499
24500 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
24501 doc: /* Template for displaying the title bar of an iconified frame.
24502 \(Assuming the window manager supports this feature.)
24503 This variable has the same structure as `mode-line-format' (which see),
24504 and is used only on frames for which no explicit name has been set
24505 \(see `modify-frame-parameters'). */);
24506 Vicon_title_format
24507 = Vframe_title_format
24508 = Fcons (intern ("multiple-frames"),
24509 Fcons (build_string ("%b"),
24510 Fcons (Fcons (empty_unibyte_string,
24511 Fcons (intern ("invocation-name"),
24512 Fcons (build_string ("@"),
24513 Fcons (intern ("system-name"),
24514 Qnil)))),
24515 Qnil)));
24516
24517 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
24518 doc: /* Maximum number of lines to keep in the message log buffer.
24519 If nil, disable message logging. If t, log messages but don't truncate
24520 the buffer when it becomes large. */);
24521 Vmessage_log_max = make_number (100);
24522
24523 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
24524 doc: /* Functions called before redisplay, if window sizes have changed.
24525 The value should be a list of functions that take one argument.
24526 Just before redisplay, for each frame, if any of its windows have changed
24527 size since the last redisplay, or have been split or deleted,
24528 all the functions in the list are called, with the frame as argument. */);
24529 Vwindow_size_change_functions = Qnil;
24530
24531 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
24532 doc: /* List of functions to call before redisplaying a window with scrolling.
24533 Each function is called with two arguments, the window
24534 and its new display-start position. Note that the value of `window-end'
24535 is not valid when these functions are called. */);
24536 Vwindow_scroll_functions = Qnil;
24537
24538 DEFVAR_LISP ("redisplay-end-trigger-functions", &Vredisplay_end_trigger_functions,
24539 doc: /* Functions called when redisplay of a window reaches the end trigger.
24540 Each function is called with two arguments, the window and the end trigger value.
24541 See `set-window-redisplay-end-trigger'. */);
24542 Vredisplay_end_trigger_functions = Qnil;
24543
24544 DEFVAR_LISP ("mouse-autoselect-window", &Vmouse_autoselect_window,
24545 doc: /* *Non-nil means autoselect window with mouse pointer.
24546 If nil, do not autoselect windows.
24547 A positive number means delay autoselection by that many seconds: a
24548 window is autoselected only after the mouse has remained in that
24549 window for the duration of the delay.
24550 A negative number has a similar effect, but causes windows to be
24551 autoselected only after the mouse has stopped moving. \(Because of
24552 the way Emacs compares mouse events, you will occasionally wait twice
24553 that time before the window gets selected.\)
24554 Any other value means to autoselect window instantaneously when the
24555 mouse pointer enters it.
24556
24557 Autoselection selects the minibuffer only if it is active, and never
24558 unselects the minibuffer if it is active.
24559
24560 When customizing this variable make sure that the actual value of
24561 `focus-follows-mouse' matches the behavior of your window manager. */);
24562 Vmouse_autoselect_window = Qnil;
24563
24564 DEFVAR_LISP ("auto-resize-tool-bars", &Vauto_resize_tool_bars,
24565 doc: /* *Non-nil means automatically resize tool-bars.
24566 This dynamically changes the tool-bar's height to the minimum height
24567 that is needed to make all tool-bar items visible.
24568 If value is `grow-only', the tool-bar's height is only increased
24569 automatically; to decrease the tool-bar height, use \\[recenter]. */);
24570 Vauto_resize_tool_bars = Qt;
24571
24572 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
24573 doc: /* *Non-nil means raise tool-bar buttons when the mouse moves over them. */);
24574 auto_raise_tool_bar_buttons_p = 1;
24575
24576 DEFVAR_BOOL ("make-cursor-line-fully-visible", &make_cursor_line_fully_visible_p,
24577 doc: /* *Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
24578 make_cursor_line_fully_visible_p = 1;
24579
24580 DEFVAR_LISP ("tool-bar-border", &Vtool_bar_border,
24581 doc: /* *Border below tool-bar in pixels.
24582 If an integer, use it as the height of the border.
24583 If it is one of `internal-border-width' or `border-width', use the
24584 value of the corresponding frame parameter.
24585 Otherwise, no border is added below the tool-bar. */);
24586 Vtool_bar_border = Qinternal_border_width;
24587
24588 DEFVAR_LISP ("tool-bar-button-margin", &Vtool_bar_button_margin,
24589 doc: /* *Margin around tool-bar buttons in pixels.
24590 If an integer, use that for both horizontal and vertical margins.
24591 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
24592 HORZ specifying the horizontal margin, and VERT specifying the
24593 vertical margin. */);
24594 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
24595
24596 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
24597 doc: /* *Relief thickness of tool-bar buttons. */);
24598 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
24599
24600 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
24601 doc: /* List of functions to call to fontify regions of text.
24602 Each function is called with one argument POS. Functions must
24603 fontify a region starting at POS in the current buffer, and give
24604 fontified regions the property `fontified'. */);
24605 Vfontification_functions = Qnil;
24606 Fmake_variable_buffer_local (Qfontification_functions);
24607
24608 DEFVAR_BOOL ("unibyte-display-via-language-environment",
24609 &unibyte_display_via_language_environment,
24610 doc: /* *Non-nil means display unibyte text according to language environment.
24611 Specifically this means that unibyte non-ASCII characters
24612 are displayed by converting them to the equivalent multibyte characters
24613 according to the current language environment. As a result, they are
24614 displayed according to the current fontset. */);
24615 unibyte_display_via_language_environment = 0;
24616
24617 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
24618 doc: /* *Maximum height for resizing mini-windows.
24619 If a float, it specifies a fraction of the mini-window frame's height.
24620 If an integer, it specifies a number of lines. */);
24621 Vmax_mini_window_height = make_float (0.25);
24622
24623 DEFVAR_LISP ("resize-mini-windows", &Vresize_mini_windows,
24624 doc: /* *How to resize mini-windows.
24625 A value of nil means don't automatically resize mini-windows.
24626 A value of t means resize them to fit the text displayed in them.
24627 A value of `grow-only', the default, means let mini-windows grow
24628 only, until their display becomes empty, at which point the windows
24629 go back to their normal size. */);
24630 Vresize_mini_windows = Qgrow_only;
24631
24632 DEFVAR_LISP ("blink-cursor-alist", &Vblink_cursor_alist,
24633 doc: /* Alist specifying how to blink the cursor off.
24634 Each element has the form (ON-STATE . OFF-STATE). Whenever the
24635 `cursor-type' frame-parameter or variable equals ON-STATE,
24636 comparing using `equal', Emacs uses OFF-STATE to specify
24637 how to blink it off. ON-STATE and OFF-STATE are values for
24638 the `cursor-type' frame parameter.
24639
24640 If a frame's ON-STATE has no entry in this list,
24641 the frame's other specifications determine how to blink the cursor off. */);
24642 Vblink_cursor_alist = Qnil;
24643
24644 DEFVAR_BOOL ("auto-hscroll-mode", &automatic_hscrolling_p,
24645 doc: /* *Non-nil means scroll the display automatically to make point visible. */);
24646 automatic_hscrolling_p = 1;
24647
24648 DEFVAR_INT ("hscroll-margin", &hscroll_margin,
24649 doc: /* *How many columns away from the window edge point is allowed to get
24650 before automatic hscrolling will horizontally scroll the window. */);
24651 hscroll_margin = 5;
24652
24653 DEFVAR_LISP ("hscroll-step", &Vhscroll_step,
24654 doc: /* *How many columns to scroll the window when point gets too close to the edge.
24655 When point is less than `hscroll-margin' columns from the window
24656 edge, automatic hscrolling will scroll the window by the amount of columns
24657 determined by this variable. If its value is a positive integer, scroll that
24658 many columns. If it's a positive floating-point number, it specifies the
24659 fraction of the window's width to scroll. If it's nil or zero, point will be
24660 centered horizontally after the scroll. Any other value, including negative
24661 numbers, are treated as if the value were zero.
24662
24663 Automatic hscrolling always moves point outside the scroll margin, so if
24664 point was more than scroll step columns inside the margin, the window will
24665 scroll more than the value given by the scroll step.
24666
24667 Note that the lower bound for automatic hscrolling specified by `scroll-left'
24668 and `scroll-right' overrides this variable's effect. */);
24669 Vhscroll_step = make_number (0);
24670
24671 DEFVAR_BOOL ("message-truncate-lines", &message_truncate_lines,
24672 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
24673 Bind this around calls to `message' to let it take effect. */);
24674 message_truncate_lines = 0;
24675
24676 DEFVAR_LISP ("menu-bar-update-hook", &Vmenu_bar_update_hook,
24677 doc: /* Normal hook run to update the menu bar definitions.
24678 Redisplay runs this hook before it redisplays the menu bar.
24679 This is used to update submenus such as Buffers,
24680 whose contents depend on various data. */);
24681 Vmenu_bar_update_hook = Qnil;
24682
24683 DEFVAR_LISP ("menu-updating-frame", &Vmenu_updating_frame,
24684 doc: /* Frame for which we are updating a menu.
24685 The enable predicate for a menu binding should check this variable. */);
24686 Vmenu_updating_frame = Qnil;
24687
24688 DEFVAR_BOOL ("inhibit-menubar-update", &inhibit_menubar_update,
24689 doc: /* Non-nil means don't update menu bars. Internal use only. */);
24690 inhibit_menubar_update = 0;
24691
24692 DEFVAR_BOOL ("inhibit-eval-during-redisplay", &inhibit_eval_during_redisplay,
24693 doc: /* Non-nil means don't eval Lisp during redisplay. */);
24694 inhibit_eval_during_redisplay = 0;
24695
24696 DEFVAR_BOOL ("inhibit-free-realized-faces", &inhibit_free_realized_faces,
24697 doc: /* Non-nil means don't free realized faces. Internal use only. */);
24698 inhibit_free_realized_faces = 0;
24699
24700 #if GLYPH_DEBUG
24701 DEFVAR_BOOL ("inhibit-try-window-id", &inhibit_try_window_id,
24702 doc: /* Inhibit try_window_id display optimization. */);
24703 inhibit_try_window_id = 0;
24704
24705 DEFVAR_BOOL ("inhibit-try-window-reusing", &inhibit_try_window_reusing,
24706 doc: /* Inhibit try_window_reusing display optimization. */);
24707 inhibit_try_window_reusing = 0;
24708
24709 DEFVAR_BOOL ("inhibit-try-cursor-movement", &inhibit_try_cursor_movement,
24710 doc: /* Inhibit try_cursor_movement display optimization. */);
24711 inhibit_try_cursor_movement = 0;
24712 #endif /* GLYPH_DEBUG */
24713
24714 DEFVAR_INT ("overline-margin", &overline_margin,
24715 doc: /* *Space between overline and text, in pixels.
24716 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
24717 margin to the caracter height. */);
24718 overline_margin = 2;
24719 }
24720
24721
24722 /* Initialize this module when Emacs starts. */
24723
24724 void
24725 init_xdisp ()
24726 {
24727 Lisp_Object root_window;
24728 struct window *mini_w;
24729
24730 current_header_line_height = current_mode_line_height = -1;
24731
24732 CHARPOS (this_line_start_pos) = 0;
24733
24734 mini_w = XWINDOW (minibuf_window);
24735 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
24736
24737 if (!noninteractive)
24738 {
24739 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
24740 int i;
24741
24742 XWINDOW (root_window)->top_line = make_number (FRAME_TOP_MARGIN (f));
24743 set_window_height (root_window,
24744 FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f),
24745 0);
24746 mini_w->top_line = make_number (FRAME_LINES (f) - 1);
24747 set_window_height (minibuf_window, 1, 0);
24748
24749 XWINDOW (root_window)->total_cols = make_number (FRAME_COLS (f));
24750 mini_w->total_cols = make_number (FRAME_COLS (f));
24751
24752 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
24753 scratch_glyph_row.glyphs[TEXT_AREA + 1]
24754 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
24755
24756 /* The default ellipsis glyphs `...'. */
24757 for (i = 0; i < 3; ++i)
24758 default_invis_vector[i] = make_number ('.');
24759 }
24760
24761 {
24762 /* Allocate the buffer for frame titles.
24763 Also used for `format-mode-line'. */
24764 int size = 100;
24765 mode_line_noprop_buf = (char *) xmalloc (size);
24766 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
24767 mode_line_noprop_ptr = mode_line_noprop_buf;
24768 mode_line_target = MODE_LINE_DISPLAY;
24769 }
24770
24771 help_echo_showing_p = 0;
24772 }
24773
24774
24775 /* arch-tag: eacc864d-bb6a-4b74-894a-1a4399a1358b
24776 (do not change this comment) */