]> 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 display_replaced_p = 1;
3848 }
3849 }
3850 else if (VECTORP (prop))
3851 {
3852 int i;
3853 for (i = 0; i < ASIZE (prop); ++i)
3854 if (handle_single_display_spec (it, AREF (prop, i), object,
3855 position, display_replaced_p))
3856 display_replaced_p = 1;
3857 }
3858 else
3859 {
3860 int ret = handle_single_display_spec (it, prop, object, position, 0);
3861 if (ret < 0) /* Replaced by "", i.e. nothing. */
3862 return HANDLED_RECOMPUTE_PROPS;
3863 if (ret)
3864 display_replaced_p = 1;
3865 }
3866
3867 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
3868 }
3869
3870
3871 /* Value is the position of the end of the `display' property starting
3872 at START_POS in OBJECT. */
3873
3874 static struct text_pos
3875 display_prop_end (it, object, start_pos)
3876 struct it *it;
3877 Lisp_Object object;
3878 struct text_pos start_pos;
3879 {
3880 Lisp_Object end;
3881 struct text_pos end_pos;
3882
3883 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
3884 Qdisplay, object, Qnil);
3885 CHARPOS (end_pos) = XFASTINT (end);
3886 if (STRINGP (object))
3887 compute_string_pos (&end_pos, start_pos, it->string);
3888 else
3889 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
3890
3891 return end_pos;
3892 }
3893
3894
3895 /* Set up IT from a single `display' specification PROP. OBJECT
3896 is the object in which the `display' property was found. *POSITION
3897 is the position at which it was found. DISPLAY_REPLACED_P non-zero
3898 means that we previously saw a display specification which already
3899 replaced text display with something else, for example an image;
3900 we ignore such properties after the first one has been processed.
3901
3902 If PROP is a `space' or `image' specification, and in some other
3903 cases too, set *POSITION to the position where the `display'
3904 property ends.
3905
3906 Value is non-zero if something was found which replaces the display
3907 of buffer or string text. Specifically, the value is -1 if that
3908 "something" is "nothing". */
3909
3910 static int
3911 handle_single_display_spec (it, spec, object, position,
3912 display_replaced_before_p)
3913 struct it *it;
3914 Lisp_Object spec;
3915 Lisp_Object object;
3916 struct text_pos *position;
3917 int display_replaced_before_p;
3918 {
3919 Lisp_Object form;
3920 Lisp_Object location, value;
3921 struct text_pos start_pos, save_pos;
3922 int valid_p;
3923
3924 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
3925 If the result is non-nil, use VALUE instead of SPEC. */
3926 form = Qt;
3927 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
3928 {
3929 spec = XCDR (spec);
3930 if (!CONSP (spec))
3931 return 0;
3932 form = XCAR (spec);
3933 spec = XCDR (spec);
3934 }
3935
3936 if (!NILP (form) && !EQ (form, Qt))
3937 {
3938 int count = SPECPDL_INDEX ();
3939 struct gcpro gcpro1;
3940
3941 /* Bind `object' to the object having the `display' property, a
3942 buffer or string. Bind `position' to the position in the
3943 object where the property was found, and `buffer-position'
3944 to the current position in the buffer. */
3945 specbind (Qobject, object);
3946 specbind (Qposition, make_number (CHARPOS (*position)));
3947 specbind (Qbuffer_position,
3948 make_number (STRINGP (object)
3949 ? IT_CHARPOS (*it) : CHARPOS (*position)));
3950 GCPRO1 (form);
3951 form = safe_eval (form);
3952 UNGCPRO;
3953 unbind_to (count, Qnil);
3954 }
3955
3956 if (NILP (form))
3957 return 0;
3958
3959 /* Handle `(height HEIGHT)' specifications. */
3960 if (CONSP (spec)
3961 && EQ (XCAR (spec), Qheight)
3962 && CONSP (XCDR (spec)))
3963 {
3964 if (!FRAME_WINDOW_P (it->f))
3965 return 0;
3966
3967 it->font_height = XCAR (XCDR (spec));
3968 if (!NILP (it->font_height))
3969 {
3970 struct face *face = FACE_FROM_ID (it->f, it->face_id);
3971 int new_height = -1;
3972
3973 if (CONSP (it->font_height)
3974 && (EQ (XCAR (it->font_height), Qplus)
3975 || EQ (XCAR (it->font_height), Qminus))
3976 && CONSP (XCDR (it->font_height))
3977 && INTEGERP (XCAR (XCDR (it->font_height))))
3978 {
3979 /* `(+ N)' or `(- N)' where N is an integer. */
3980 int steps = XINT (XCAR (XCDR (it->font_height)));
3981 if (EQ (XCAR (it->font_height), Qplus))
3982 steps = - steps;
3983 it->face_id = smaller_face (it->f, it->face_id, steps);
3984 }
3985 else if (FUNCTIONP (it->font_height))
3986 {
3987 /* Call function with current height as argument.
3988 Value is the new height. */
3989 Lisp_Object height;
3990 height = safe_call1 (it->font_height,
3991 face->lface[LFACE_HEIGHT_INDEX]);
3992 if (NUMBERP (height))
3993 new_height = XFLOATINT (height);
3994 }
3995 else if (NUMBERP (it->font_height))
3996 {
3997 /* Value is a multiple of the canonical char height. */
3998 struct face *face;
3999
4000 face = FACE_FROM_ID (it->f, DEFAULT_FACE_ID);
4001 new_height = (XFLOATINT (it->font_height)
4002 * XINT (face->lface[LFACE_HEIGHT_INDEX]));
4003 }
4004 else
4005 {
4006 /* Evaluate IT->font_height with `height' bound to the
4007 current specified height to get the new height. */
4008 int count = SPECPDL_INDEX ();
4009
4010 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4011 value = safe_eval (it->font_height);
4012 unbind_to (count, Qnil);
4013
4014 if (NUMBERP (value))
4015 new_height = XFLOATINT (value);
4016 }
4017
4018 if (new_height > 0)
4019 it->face_id = face_with_height (it->f, it->face_id, new_height);
4020 }
4021
4022 return 0;
4023 }
4024
4025 /* Handle `(space_width WIDTH)'. */
4026 if (CONSP (spec)
4027 && EQ (XCAR (spec), Qspace_width)
4028 && CONSP (XCDR (spec)))
4029 {
4030 if (!FRAME_WINDOW_P (it->f))
4031 return 0;
4032
4033 value = XCAR (XCDR (spec));
4034 if (NUMBERP (value) && XFLOATINT (value) > 0)
4035 it->space_width = value;
4036
4037 return 0;
4038 }
4039
4040 /* Handle `(slice X Y WIDTH HEIGHT)'. */
4041 if (CONSP (spec)
4042 && EQ (XCAR (spec), Qslice))
4043 {
4044 Lisp_Object tem;
4045
4046 if (!FRAME_WINDOW_P (it->f))
4047 return 0;
4048
4049 if (tem = XCDR (spec), CONSP (tem))
4050 {
4051 it->slice.x = XCAR (tem);
4052 if (tem = XCDR (tem), CONSP (tem))
4053 {
4054 it->slice.y = XCAR (tem);
4055 if (tem = XCDR (tem), CONSP (tem))
4056 {
4057 it->slice.width = XCAR (tem);
4058 if (tem = XCDR (tem), CONSP (tem))
4059 it->slice.height = XCAR (tem);
4060 }
4061 }
4062 }
4063
4064 return 0;
4065 }
4066
4067 /* Handle `(raise FACTOR)'. */
4068 if (CONSP (spec)
4069 && EQ (XCAR (spec), Qraise)
4070 && CONSP (XCDR (spec)))
4071 {
4072 if (!FRAME_WINDOW_P (it->f))
4073 return 0;
4074
4075 #ifdef HAVE_WINDOW_SYSTEM
4076 value = XCAR (XCDR (spec));
4077 if (NUMBERP (value))
4078 {
4079 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4080 it->voffset = - (XFLOATINT (value)
4081 * (FONT_HEIGHT (face->font)));
4082 }
4083 #endif /* HAVE_WINDOW_SYSTEM */
4084
4085 return 0;
4086 }
4087
4088 /* Don't handle the other kinds of display specifications
4089 inside a string that we got from a `display' property. */
4090 if (it->string_from_display_prop_p)
4091 return 0;
4092
4093 /* Characters having this form of property are not displayed, so
4094 we have to find the end of the property. */
4095 start_pos = *position;
4096 *position = display_prop_end (it, object, start_pos);
4097 value = Qnil;
4098
4099 /* Stop the scan at that end position--we assume that all
4100 text properties change there. */
4101 it->stop_charpos = position->charpos;
4102
4103 /* Handle `(left-fringe BITMAP [FACE])'
4104 and `(right-fringe BITMAP [FACE])'. */
4105 if (CONSP (spec)
4106 && (EQ (XCAR (spec), Qleft_fringe)
4107 || EQ (XCAR (spec), Qright_fringe))
4108 && CONSP (XCDR (spec)))
4109 {
4110 int face_id = DEFAULT_FACE_ID;
4111 int fringe_bitmap;
4112
4113 if (!FRAME_WINDOW_P (it->f))
4114 /* If we return here, POSITION has been advanced
4115 across the text with this property. */
4116 return 0;
4117
4118 #ifdef HAVE_WINDOW_SYSTEM
4119 value = XCAR (XCDR (spec));
4120 if (!SYMBOLP (value)
4121 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
4122 /* If we return here, POSITION has been advanced
4123 across the text with this property. */
4124 return 0;
4125
4126 if (CONSP (XCDR (XCDR (spec))))
4127 {
4128 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
4129 int face_id2 = lookup_derived_face (it->f, face_name,
4130 FRINGE_FACE_ID, 0);
4131 if (face_id2 >= 0)
4132 face_id = face_id2;
4133 }
4134
4135 /* Save current settings of IT so that we can restore them
4136 when we are finished with the glyph property value. */
4137
4138 save_pos = it->position;
4139 it->position = *position;
4140 push_it (it);
4141 it->position = save_pos;
4142
4143 it->area = TEXT_AREA;
4144 it->what = IT_IMAGE;
4145 it->image_id = -1; /* no image */
4146 it->position = start_pos;
4147 it->object = NILP (object) ? it->w->buffer : object;
4148 it->method = GET_FROM_IMAGE;
4149 it->face_id = face_id;
4150
4151 /* Say that we haven't consumed the characters with
4152 `display' property yet. The call to pop_it in
4153 set_iterator_to_next will clean this up. */
4154 *position = start_pos;
4155
4156 if (EQ (XCAR (spec), Qleft_fringe))
4157 {
4158 it->left_user_fringe_bitmap = fringe_bitmap;
4159 it->left_user_fringe_face_id = face_id;
4160 }
4161 else
4162 {
4163 it->right_user_fringe_bitmap = fringe_bitmap;
4164 it->right_user_fringe_face_id = face_id;
4165 }
4166 #endif /* HAVE_WINDOW_SYSTEM */
4167 return 1;
4168 }
4169
4170 /* Prepare to handle `((margin left-margin) ...)',
4171 `((margin right-margin) ...)' and `((margin nil) ...)'
4172 prefixes for display specifications. */
4173 location = Qunbound;
4174 if (CONSP (spec) && CONSP (XCAR (spec)))
4175 {
4176 Lisp_Object tem;
4177
4178 value = XCDR (spec);
4179 if (CONSP (value))
4180 value = XCAR (value);
4181
4182 tem = XCAR (spec);
4183 if (EQ (XCAR (tem), Qmargin)
4184 && (tem = XCDR (tem),
4185 tem = CONSP (tem) ? XCAR (tem) : Qnil,
4186 (NILP (tem)
4187 || EQ (tem, Qleft_margin)
4188 || EQ (tem, Qright_margin))))
4189 location = tem;
4190 }
4191
4192 if (EQ (location, Qunbound))
4193 {
4194 location = Qnil;
4195 value = spec;
4196 }
4197
4198 /* After this point, VALUE is the property after any
4199 margin prefix has been stripped. It must be a string,
4200 an image specification, or `(space ...)'.
4201
4202 LOCATION specifies where to display: `left-margin',
4203 `right-margin' or nil. */
4204
4205 valid_p = (STRINGP (value)
4206 #ifdef HAVE_WINDOW_SYSTEM
4207 || (FRAME_WINDOW_P (it->f) && valid_image_p (value))
4208 #endif /* not HAVE_WINDOW_SYSTEM */
4209 || (CONSP (value) && EQ (XCAR (value), Qspace)));
4210
4211 if (valid_p && !display_replaced_before_p)
4212 {
4213 /* Save current settings of IT so that we can restore them
4214 when we are finished with the glyph property value. */
4215 save_pos = it->position;
4216 it->position = *position;
4217 push_it (it);
4218 it->position = save_pos;
4219
4220 if (NILP (location))
4221 it->area = TEXT_AREA;
4222 else if (EQ (location, Qleft_margin))
4223 it->area = LEFT_MARGIN_AREA;
4224 else
4225 it->area = RIGHT_MARGIN_AREA;
4226
4227 if (STRINGP (value))
4228 {
4229 if (SCHARS (value) == 0)
4230 {
4231 pop_it (it);
4232 return -1; /* Replaced by "", i.e. nothing. */
4233 }
4234 it->string = value;
4235 it->multibyte_p = STRING_MULTIBYTE (it->string);
4236 it->current.overlay_string_index = -1;
4237 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4238 it->end_charpos = it->string_nchars = SCHARS (it->string);
4239 it->method = GET_FROM_STRING;
4240 it->stop_charpos = 0;
4241 it->string_from_display_prop_p = 1;
4242 /* Say that we haven't consumed the characters with
4243 `display' property yet. The call to pop_it in
4244 set_iterator_to_next will clean this up. */
4245 *position = start_pos;
4246 }
4247 else if (CONSP (value) && EQ (XCAR (value), Qspace))
4248 {
4249 it->method = GET_FROM_STRETCH;
4250 it->object = value;
4251 *position = it->position = start_pos;
4252 }
4253 #ifdef HAVE_WINDOW_SYSTEM
4254 else
4255 {
4256 it->what = IT_IMAGE;
4257 it->image_id = lookup_image (it->f, value);
4258 it->position = start_pos;
4259 it->object = NILP (object) ? it->w->buffer : object;
4260 it->method = GET_FROM_IMAGE;
4261
4262 /* Say that we haven't consumed the characters with
4263 `display' property yet. The call to pop_it in
4264 set_iterator_to_next will clean this up. */
4265 *position = start_pos;
4266 }
4267 #endif /* HAVE_WINDOW_SYSTEM */
4268
4269 return 1;
4270 }
4271
4272 /* Invalid property or property not supported. Restore
4273 POSITION to what it was before. */
4274 *position = start_pos;
4275 return 0;
4276 }
4277
4278
4279 /* Check if SPEC is a display sub-property value whose text should be
4280 treated as intangible. */
4281
4282 static int
4283 single_display_spec_intangible_p (prop)
4284 Lisp_Object prop;
4285 {
4286 /* Skip over `when FORM'. */
4287 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
4288 {
4289 prop = XCDR (prop);
4290 if (!CONSP (prop))
4291 return 0;
4292 prop = XCDR (prop);
4293 }
4294
4295 if (STRINGP (prop))
4296 return 1;
4297
4298 if (!CONSP (prop))
4299 return 0;
4300
4301 /* Skip over `margin LOCATION'. If LOCATION is in the margins,
4302 we don't need to treat text as intangible. */
4303 if (EQ (XCAR (prop), Qmargin))
4304 {
4305 prop = XCDR (prop);
4306 if (!CONSP (prop))
4307 return 0;
4308
4309 prop = XCDR (prop);
4310 if (!CONSP (prop)
4311 || EQ (XCAR (prop), Qleft_margin)
4312 || EQ (XCAR (prop), Qright_margin))
4313 return 0;
4314 }
4315
4316 return (CONSP (prop)
4317 && (EQ (XCAR (prop), Qimage)
4318 || EQ (XCAR (prop), Qspace)));
4319 }
4320
4321
4322 /* Check if PROP is a display property value whose text should be
4323 treated as intangible. */
4324
4325 int
4326 display_prop_intangible_p (prop)
4327 Lisp_Object prop;
4328 {
4329 if (CONSP (prop)
4330 && CONSP (XCAR (prop))
4331 && !EQ (Qmargin, XCAR (XCAR (prop))))
4332 {
4333 /* A list of sub-properties. */
4334 while (CONSP (prop))
4335 {
4336 if (single_display_spec_intangible_p (XCAR (prop)))
4337 return 1;
4338 prop = XCDR (prop);
4339 }
4340 }
4341 else if (VECTORP (prop))
4342 {
4343 /* A vector of sub-properties. */
4344 int i;
4345 for (i = 0; i < ASIZE (prop); ++i)
4346 if (single_display_spec_intangible_p (AREF (prop, i)))
4347 return 1;
4348 }
4349 else
4350 return single_display_spec_intangible_p (prop);
4351
4352 return 0;
4353 }
4354
4355
4356 /* Return 1 if PROP is a display sub-property value containing STRING. */
4357
4358 static int
4359 single_display_spec_string_p (prop, string)
4360 Lisp_Object prop, string;
4361 {
4362 if (EQ (string, prop))
4363 return 1;
4364
4365 /* Skip over `when FORM'. */
4366 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
4367 {
4368 prop = XCDR (prop);
4369 if (!CONSP (prop))
4370 return 0;
4371 prop = XCDR (prop);
4372 }
4373
4374 if (CONSP (prop))
4375 /* Skip over `margin LOCATION'. */
4376 if (EQ (XCAR (prop), Qmargin))
4377 {
4378 prop = XCDR (prop);
4379 if (!CONSP (prop))
4380 return 0;
4381
4382 prop = XCDR (prop);
4383 if (!CONSP (prop))
4384 return 0;
4385 }
4386
4387 return CONSP (prop) && EQ (XCAR (prop), string);
4388 }
4389
4390
4391 /* Return 1 if STRING appears in the `display' property PROP. */
4392
4393 static int
4394 display_prop_string_p (prop, string)
4395 Lisp_Object prop, string;
4396 {
4397 if (CONSP (prop)
4398 && CONSP (XCAR (prop))
4399 && !EQ (Qmargin, XCAR (XCAR (prop))))
4400 {
4401 /* A list of sub-properties. */
4402 while (CONSP (prop))
4403 {
4404 if (single_display_spec_string_p (XCAR (prop), string))
4405 return 1;
4406 prop = XCDR (prop);
4407 }
4408 }
4409 else if (VECTORP (prop))
4410 {
4411 /* A vector of sub-properties. */
4412 int i;
4413 for (i = 0; i < ASIZE (prop); ++i)
4414 if (single_display_spec_string_p (AREF (prop, i), string))
4415 return 1;
4416 }
4417 else
4418 return single_display_spec_string_p (prop, string);
4419
4420 return 0;
4421 }
4422
4423
4424 /* Determine from which buffer position in W's buffer STRING comes
4425 from. AROUND_CHARPOS is an approximate position where it could
4426 be from. Value is the buffer position or 0 if it couldn't be
4427 determined.
4428
4429 W's buffer must be current.
4430
4431 This function is necessary because we don't record buffer positions
4432 in glyphs generated from strings (to keep struct glyph small).
4433 This function may only use code that doesn't eval because it is
4434 called asynchronously from note_mouse_highlight. */
4435
4436 int
4437 string_buffer_position (w, string, around_charpos)
4438 struct window *w;
4439 Lisp_Object string;
4440 int around_charpos;
4441 {
4442 Lisp_Object limit, prop, pos;
4443 const int MAX_DISTANCE = 1000;
4444 int found = 0;
4445
4446 pos = make_number (around_charpos);
4447 limit = make_number (min (XINT (pos) + MAX_DISTANCE, ZV));
4448 while (!found && !EQ (pos, limit))
4449 {
4450 prop = Fget_char_property (pos, Qdisplay, Qnil);
4451 if (!NILP (prop) && display_prop_string_p (prop, string))
4452 found = 1;
4453 else
4454 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil, limit);
4455 }
4456
4457 if (!found)
4458 {
4459 pos = make_number (around_charpos);
4460 limit = make_number (max (XINT (pos) - MAX_DISTANCE, BEGV));
4461 while (!found && !EQ (pos, limit))
4462 {
4463 prop = Fget_char_property (pos, Qdisplay, Qnil);
4464 if (!NILP (prop) && display_prop_string_p (prop, string))
4465 found = 1;
4466 else
4467 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
4468 limit);
4469 }
4470 }
4471
4472 return found ? XINT (pos) : 0;
4473 }
4474
4475
4476 \f
4477 /***********************************************************************
4478 `composition' property
4479 ***********************************************************************/
4480
4481 static enum prop_handled
4482 handle_auto_composed_prop (it)
4483 struct it *it;
4484 {
4485 enum prop_handled handled = HANDLED_NORMALLY;
4486
4487 if (FUNCTIONP (Vauto_composition_function))
4488 {
4489 Lisp_Object val;
4490 EMACS_INT pos, this_pos;
4491
4492 if (STRINGP (it->string))
4493 pos = IT_STRING_CHARPOS (*it);
4494 else
4495 pos = IT_CHARPOS (*it);
4496 this_pos = pos;
4497
4498 val =Fget_char_property (make_number (pos), Qauto_composed, it->string);
4499 if (! NILP (val))
4500 {
4501 Lisp_Object limit = Qnil, next;
4502
4503 /* As Fnext_single_char_property_change is very slow, we
4504 limit the search to the current line. */
4505 if (STRINGP (it->string))
4506 limit = make_number (SCHARS (it->string));
4507 else
4508 limit = make_number (find_next_newline_no_quit (pos, 1));
4509
4510 next = (Fnext_single_property_change
4511 (make_number (pos), Qauto_composed, it->string, limit));
4512 if (XINT (next) < XINT (limit))
4513 {
4514 /* The current point is auto-composed, but there exist
4515 characters not yet composed beyond the auto-composed
4516 region. There's a possiblity that the last
4517 characters in the region may be newly composed. */
4518 int charpos = XINT (next) - 1, bytepos, c;
4519
4520 if (STRINGP (it->string))
4521 {
4522 bytepos = string_char_to_byte (it->string, charpos);
4523 c = SDATA (it->string)[bytepos];
4524 }
4525 else
4526 {
4527 bytepos = CHAR_TO_BYTE (charpos);
4528 c = FETCH_BYTE (bytepos);
4529 }
4530 if (c != '\n')
4531 /* If the last character is not newline, it may be
4532 composed with the following characters. */
4533 val = Qnil, pos = charpos + 1;
4534 }
4535 }
4536 if (NILP (val))
4537 {
4538 int count = SPECPDL_INDEX ();
4539 Lisp_Object args[4];
4540
4541 args[0] = Vauto_composition_function;
4542 specbind (Qauto_composition_function, Qnil);
4543 args[1] = make_number (pos);
4544 args[2] = it->string;
4545 #ifdef USE_FONT_BACKEND
4546 if (enable_font_backend)
4547 {
4548 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4549 int c;
4550
4551 if (STRINGP (it->string))
4552 {
4553 EMACS_INT pos_byte = IT_STRING_BYTEPOS (*it);
4554 const unsigned char *s = SDATA (it->string) + pos_byte;
4555
4556 if (STRING_MULTIBYTE (it->string))
4557 it->c = STRING_CHAR (s, 0);
4558 else
4559 it->c = *s;
4560 }
4561 else
4562 {
4563 EMACS_INT pos_byte = IT_BYTEPOS (*it);
4564
4565 it->c = FETCH_CHAR (pos_byte);
4566 }
4567 args[3] = font_at (it->c, this_pos, face, it->w, it->string);
4568 }
4569 else
4570 #endif /* USE_FONT_BACKEND */
4571 args[3] = Qnil;
4572 safe_call (4, args);
4573 unbind_to (count, Qnil);
4574
4575 if (this_pos == pos)
4576 {
4577 val = Fget_char_property (args[1], Qauto_composed, it->string);
4578 /* Return HANDLED_RECOMPUTE_PROPS only if function composed
4579 something. This avoids an endless loop if they failed to
4580 fontify the text for which reason ever. */
4581 if (! NILP (val))
4582 handled = HANDLED_RECOMPUTE_PROPS;
4583 }
4584 else
4585 handled = HANDLED_RECOMPUTE_PROPS;
4586 }
4587 }
4588
4589 return handled;
4590 }
4591
4592 /* Set up iterator IT from `composition' property at its current
4593 position. Called from handle_stop. */
4594
4595 static enum prop_handled
4596 handle_composition_prop (it)
4597 struct it *it;
4598 {
4599 Lisp_Object prop, string;
4600 EMACS_INT pos, pos_byte, start, end;
4601 enum prop_handled handled = HANDLED_NORMALLY;
4602
4603 if (STRINGP (it->string))
4604 {
4605 pos = IT_STRING_CHARPOS (*it);
4606 pos_byte = IT_STRING_BYTEPOS (*it);
4607 string = it->string;
4608 }
4609 else
4610 {
4611 pos = IT_CHARPOS (*it);
4612 pos_byte = IT_BYTEPOS (*it);
4613 string = Qnil;
4614 }
4615
4616 /* If there's a valid composition and point is not inside of the
4617 composition (in the case that the composition is from the current
4618 buffer), draw a glyph composed from the composition components. */
4619 if (find_composition (pos, -1, &start, &end, &prop, string)
4620 && COMPOSITION_VALID_P (start, end, prop)
4621 && (STRINGP (it->string) || (PT <= start || PT >= end)))
4622 {
4623 int id;
4624
4625 if (start != pos)
4626 {
4627 if (STRINGP (it->string))
4628 pos_byte = string_char_to_byte (it->string, start);
4629 else
4630 pos_byte = CHAR_TO_BYTE (start);
4631 }
4632 id = get_composition_id (start, pos_byte, end - start, prop, string);
4633
4634 if (id >= 0)
4635 {
4636 struct composition *cmp = composition_table[id];
4637
4638 if (cmp->glyph_len == 0)
4639 {
4640 /* No glyph. */
4641 if (STRINGP (it->string))
4642 {
4643 IT_STRING_CHARPOS (*it) = end;
4644 IT_STRING_BYTEPOS (*it) = string_char_to_byte (it->string,
4645 end);
4646 }
4647 else
4648 {
4649 IT_CHARPOS (*it) = end;
4650 IT_BYTEPOS (*it) = CHAR_TO_BYTE (end);
4651 }
4652 return HANDLED_RECOMPUTE_PROPS;
4653 }
4654
4655 it->stop_charpos = end;
4656 push_it (it);
4657
4658 it->method = GET_FROM_COMPOSITION;
4659 it->cmp_id = id;
4660 it->cmp_len = COMPOSITION_LENGTH (prop);
4661 /* For a terminal, draw only the first (non-TAB) character
4662 of the components. */
4663 #ifdef USE_FONT_BACKEND
4664 if (composition_table[id]->method == COMPOSITION_WITH_GLYPH_STRING)
4665 {
4666 Lisp_Object lgstring = AREF (XHASH_TABLE (composition_hash_table)
4667 ->key_and_value,
4668 cmp->hash_index * 2);
4669
4670 it->c = XINT (LGLYPH_CHAR (LGSTRING_GLYPH (lgstring, 0)));
4671 }
4672 else
4673 #endif /* USE_FONT_BACKEND */
4674 {
4675 int i;
4676
4677 for (i = 0; i < cmp->glyph_len; i++)
4678 if ((it->c = COMPOSITION_GLYPH (composition_table[id], i))
4679 != '\t')
4680 break;
4681 }
4682 if (it->c == '\t')
4683 it->c = ' ';
4684 it->len = (STRINGP (it->string)
4685 ? string_char_to_byte (it->string, end)
4686 : CHAR_TO_BYTE (end)) - pos_byte;
4687 handled = HANDLED_RETURN;
4688 }
4689 }
4690
4691 return handled;
4692 }
4693
4694
4695 \f
4696 /***********************************************************************
4697 Overlay strings
4698 ***********************************************************************/
4699
4700 /* The following structure is used to record overlay strings for
4701 later sorting in load_overlay_strings. */
4702
4703 struct overlay_entry
4704 {
4705 Lisp_Object overlay;
4706 Lisp_Object string;
4707 int priority;
4708 int after_string_p;
4709 };
4710
4711
4712 /* Set up iterator IT from overlay strings at its current position.
4713 Called from handle_stop. */
4714
4715 static enum prop_handled
4716 handle_overlay_change (it)
4717 struct it *it;
4718 {
4719 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
4720 return HANDLED_RECOMPUTE_PROPS;
4721 else
4722 return HANDLED_NORMALLY;
4723 }
4724
4725
4726 /* Set up the next overlay string for delivery by IT, if there is an
4727 overlay string to deliver. Called by set_iterator_to_next when the
4728 end of the current overlay string is reached. If there are more
4729 overlay strings to display, IT->string and
4730 IT->current.overlay_string_index are set appropriately here.
4731 Otherwise IT->string is set to nil. */
4732
4733 static void
4734 next_overlay_string (it)
4735 struct it *it;
4736 {
4737 ++it->current.overlay_string_index;
4738 if (it->current.overlay_string_index == it->n_overlay_strings)
4739 {
4740 /* No more overlay strings. Restore IT's settings to what
4741 they were before overlay strings were processed, and
4742 continue to deliver from current_buffer. */
4743 int display_ellipsis_p = it->stack[it->sp - 1].display_ellipsis_p;
4744
4745 pop_it (it);
4746 xassert (it->sp > 0
4747 || it->method == GET_FROM_COMPOSITION
4748 || (NILP (it->string)
4749 && it->method == GET_FROM_BUFFER
4750 && it->stop_charpos >= BEGV
4751 && it->stop_charpos <= it->end_charpos));
4752 it->current.overlay_string_index = -1;
4753 it->n_overlay_strings = 0;
4754
4755 /* If we're at the end of the buffer, record that we have
4756 processed the overlay strings there already, so that
4757 next_element_from_buffer doesn't try it again. */
4758 if (IT_CHARPOS (*it) >= it->end_charpos)
4759 it->overlay_strings_at_end_processed_p = 1;
4760
4761 /* If we have to display `...' for invisible text, set
4762 the iterator up for that. */
4763 if (display_ellipsis_p)
4764 setup_for_ellipsis (it, 0);
4765 }
4766 else
4767 {
4768 /* There are more overlay strings to process. If
4769 IT->current.overlay_string_index has advanced to a position
4770 where we must load IT->overlay_strings with more strings, do
4771 it. */
4772 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
4773
4774 if (it->current.overlay_string_index && i == 0)
4775 load_overlay_strings (it, 0);
4776
4777 /* Initialize IT to deliver display elements from the overlay
4778 string. */
4779 it->string = it->overlay_strings[i];
4780 it->multibyte_p = STRING_MULTIBYTE (it->string);
4781 SET_TEXT_POS (it->current.string_pos, 0, 0);
4782 it->method = GET_FROM_STRING;
4783 it->stop_charpos = 0;
4784 }
4785
4786 CHECK_IT (it);
4787 }
4788
4789
4790 /* Compare two overlay_entry structures E1 and E2. Used as a
4791 comparison function for qsort in load_overlay_strings. Overlay
4792 strings for the same position are sorted so that
4793
4794 1. All after-strings come in front of before-strings, except
4795 when they come from the same overlay.
4796
4797 2. Within after-strings, strings are sorted so that overlay strings
4798 from overlays with higher priorities come first.
4799
4800 2. Within before-strings, strings are sorted so that overlay
4801 strings from overlays with higher priorities come last.
4802
4803 Value is analogous to strcmp. */
4804
4805
4806 static int
4807 compare_overlay_entries (e1, e2)
4808 void *e1, *e2;
4809 {
4810 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
4811 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
4812 int result;
4813
4814 if (entry1->after_string_p != entry2->after_string_p)
4815 {
4816 /* Let after-strings appear in front of before-strings if
4817 they come from different overlays. */
4818 if (EQ (entry1->overlay, entry2->overlay))
4819 result = entry1->after_string_p ? 1 : -1;
4820 else
4821 result = entry1->after_string_p ? -1 : 1;
4822 }
4823 else if (entry1->after_string_p)
4824 /* After-strings sorted in order of decreasing priority. */
4825 result = entry2->priority - entry1->priority;
4826 else
4827 /* Before-strings sorted in order of increasing priority. */
4828 result = entry1->priority - entry2->priority;
4829
4830 return result;
4831 }
4832
4833
4834 /* Load the vector IT->overlay_strings with overlay strings from IT's
4835 current buffer position, or from CHARPOS if that is > 0. Set
4836 IT->n_overlays to the total number of overlay strings found.
4837
4838 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
4839 a time. On entry into load_overlay_strings,
4840 IT->current.overlay_string_index gives the number of overlay
4841 strings that have already been loaded by previous calls to this
4842 function.
4843
4844 IT->add_overlay_start contains an additional overlay start
4845 position to consider for taking overlay strings from, if non-zero.
4846 This position comes into play when the overlay has an `invisible'
4847 property, and both before and after-strings. When we've skipped to
4848 the end of the overlay, because of its `invisible' property, we
4849 nevertheless want its before-string to appear.
4850 IT->add_overlay_start will contain the overlay start position
4851 in this case.
4852
4853 Overlay strings are sorted so that after-string strings come in
4854 front of before-string strings. Within before and after-strings,
4855 strings are sorted by overlay priority. See also function
4856 compare_overlay_entries. */
4857
4858 static void
4859 load_overlay_strings (it, charpos)
4860 struct it *it;
4861 int charpos;
4862 {
4863 extern Lisp_Object Qafter_string, Qbefore_string, Qwindow, Qpriority;
4864 Lisp_Object overlay, window, str, invisible;
4865 struct Lisp_Overlay *ov;
4866 int start, end;
4867 int size = 20;
4868 int n = 0, i, j, invis_p;
4869 struct overlay_entry *entries
4870 = (struct overlay_entry *) alloca (size * sizeof *entries);
4871
4872 if (charpos <= 0)
4873 charpos = IT_CHARPOS (*it);
4874
4875 /* Append the overlay string STRING of overlay OVERLAY to vector
4876 `entries' which has size `size' and currently contains `n'
4877 elements. AFTER_P non-zero means STRING is an after-string of
4878 OVERLAY. */
4879 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
4880 do \
4881 { \
4882 Lisp_Object priority; \
4883 \
4884 if (n == size) \
4885 { \
4886 int new_size = 2 * size; \
4887 struct overlay_entry *old = entries; \
4888 entries = \
4889 (struct overlay_entry *) alloca (new_size \
4890 * sizeof *entries); \
4891 bcopy (old, entries, size * sizeof *entries); \
4892 size = new_size; \
4893 } \
4894 \
4895 entries[n].string = (STRING); \
4896 entries[n].overlay = (OVERLAY); \
4897 priority = Foverlay_get ((OVERLAY), Qpriority); \
4898 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
4899 entries[n].after_string_p = (AFTER_P); \
4900 ++n; \
4901 } \
4902 while (0)
4903
4904 /* Process overlay before the overlay center. */
4905 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
4906 {
4907 XSETMISC (overlay, ov);
4908 xassert (OVERLAYP (overlay));
4909 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4910 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4911
4912 if (end < charpos)
4913 break;
4914
4915 /* Skip this overlay if it doesn't start or end at IT's current
4916 position. */
4917 if (end != charpos && start != charpos)
4918 continue;
4919
4920 /* Skip this overlay if it doesn't apply to IT->w. */
4921 window = Foverlay_get (overlay, Qwindow);
4922 if (WINDOWP (window) && XWINDOW (window) != it->w)
4923 continue;
4924
4925 /* If the text ``under'' the overlay is invisible, both before-
4926 and after-strings from this overlay are visible; start and
4927 end position are indistinguishable. */
4928 invisible = Foverlay_get (overlay, Qinvisible);
4929 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4930
4931 /* If overlay has a non-empty before-string, record it. */
4932 if ((start == charpos || (end == charpos && invis_p))
4933 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4934 && SCHARS (str))
4935 RECORD_OVERLAY_STRING (overlay, str, 0);
4936
4937 /* If overlay has a non-empty after-string, record it. */
4938 if ((end == charpos || (start == charpos && invis_p))
4939 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4940 && SCHARS (str))
4941 RECORD_OVERLAY_STRING (overlay, str, 1);
4942 }
4943
4944 /* Process overlays after the overlay center. */
4945 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
4946 {
4947 XSETMISC (overlay, ov);
4948 xassert (OVERLAYP (overlay));
4949 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4950 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4951
4952 if (start > charpos)
4953 break;
4954
4955 /* Skip this overlay if it doesn't start or end at IT's current
4956 position. */
4957 if (end != charpos && start != charpos)
4958 continue;
4959
4960 /* Skip this overlay if it doesn't apply to IT->w. */
4961 window = Foverlay_get (overlay, Qwindow);
4962 if (WINDOWP (window) && XWINDOW (window) != it->w)
4963 continue;
4964
4965 /* If the text ``under'' the overlay is invisible, it has a zero
4966 dimension, and both before- and after-strings apply. */
4967 invisible = Foverlay_get (overlay, Qinvisible);
4968 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4969
4970 /* If overlay has a non-empty before-string, record it. */
4971 if ((start == charpos || (end == charpos && invis_p))
4972 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4973 && SCHARS (str))
4974 RECORD_OVERLAY_STRING (overlay, str, 0);
4975
4976 /* If overlay has a non-empty after-string, record it. */
4977 if ((end == charpos || (start == charpos && invis_p))
4978 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4979 && SCHARS (str))
4980 RECORD_OVERLAY_STRING (overlay, str, 1);
4981 }
4982
4983 #undef RECORD_OVERLAY_STRING
4984
4985 /* Sort entries. */
4986 if (n > 1)
4987 qsort (entries, n, sizeof *entries, compare_overlay_entries);
4988
4989 /* Record the total number of strings to process. */
4990 it->n_overlay_strings = n;
4991
4992 /* IT->current.overlay_string_index is the number of overlay strings
4993 that have already been consumed by IT. Copy some of the
4994 remaining overlay strings to IT->overlay_strings. */
4995 i = 0;
4996 j = it->current.overlay_string_index;
4997 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
4998 it->overlay_strings[i++] = entries[j++].string;
4999
5000 CHECK_IT (it);
5001 }
5002
5003
5004 /* Get the first chunk of overlay strings at IT's current buffer
5005 position, or at CHARPOS if that is > 0. Value is non-zero if at
5006 least one overlay string was found. */
5007
5008 static int
5009 get_overlay_strings_1 (it, charpos, compute_stop_p)
5010 struct it *it;
5011 int charpos;
5012 {
5013 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
5014 process. This fills IT->overlay_strings with strings, and sets
5015 IT->n_overlay_strings to the total number of strings to process.
5016 IT->pos.overlay_string_index has to be set temporarily to zero
5017 because load_overlay_strings needs this; it must be set to -1
5018 when no overlay strings are found because a zero value would
5019 indicate a position in the first overlay string. */
5020 it->current.overlay_string_index = 0;
5021 load_overlay_strings (it, charpos);
5022
5023 /* If we found overlay strings, set up IT to deliver display
5024 elements from the first one. Otherwise set up IT to deliver
5025 from current_buffer. */
5026 if (it->n_overlay_strings)
5027 {
5028 /* Make sure we know settings in current_buffer, so that we can
5029 restore meaningful values when we're done with the overlay
5030 strings. */
5031 if (compute_stop_p)
5032 compute_stop_pos (it);
5033 xassert (it->face_id >= 0);
5034
5035 /* Save IT's settings. They are restored after all overlay
5036 strings have been processed. */
5037 xassert (!compute_stop_p || it->sp == 0);
5038 push_it (it);
5039
5040 /* Set up IT to deliver display elements from the first overlay
5041 string. */
5042 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5043 it->string = it->overlay_strings[0];
5044 it->stop_charpos = 0;
5045 xassert (STRINGP (it->string));
5046 it->end_charpos = SCHARS (it->string);
5047 it->multibyte_p = STRING_MULTIBYTE (it->string);
5048 it->method = GET_FROM_STRING;
5049 return 1;
5050 }
5051
5052 it->current.overlay_string_index = -1;
5053 return 0;
5054 }
5055
5056 static int
5057 get_overlay_strings (it, charpos)
5058 struct it *it;
5059 int charpos;
5060 {
5061 it->string = Qnil;
5062 it->method = GET_FROM_BUFFER;
5063
5064 (void) get_overlay_strings_1 (it, charpos, 1);
5065
5066 CHECK_IT (it);
5067
5068 /* Value is non-zero if we found at least one overlay string. */
5069 return STRINGP (it->string);
5070 }
5071
5072
5073 \f
5074 /***********************************************************************
5075 Saving and restoring state
5076 ***********************************************************************/
5077
5078 /* Save current settings of IT on IT->stack. Called, for example,
5079 before setting up IT for an overlay string, to be able to restore
5080 IT's settings to what they were after the overlay string has been
5081 processed. */
5082
5083 static void
5084 push_it (it)
5085 struct it *it;
5086 {
5087 struct iterator_stack_entry *p;
5088
5089 xassert (it->sp < IT_STACK_SIZE);
5090 p = it->stack + it->sp;
5091
5092 p->stop_charpos = it->stop_charpos;
5093 xassert (it->face_id >= 0);
5094 p->face_id = it->face_id;
5095 p->string = it->string;
5096 p->method = it->method;
5097 switch (p->method)
5098 {
5099 case GET_FROM_IMAGE:
5100 p->u.image.object = it->object;
5101 p->u.image.image_id = it->image_id;
5102 p->u.image.slice = it->slice;
5103 break;
5104 case GET_FROM_COMPOSITION:
5105 p->u.comp.object = it->object;
5106 p->u.comp.c = it->c;
5107 p->u.comp.len = it->len;
5108 p->u.comp.cmp_id = it->cmp_id;
5109 p->u.comp.cmp_len = it->cmp_len;
5110 break;
5111 case GET_FROM_STRETCH:
5112 p->u.stretch.object = it->object;
5113 break;
5114 }
5115 p->position = it->position;
5116 p->current = it->current;
5117 p->end_charpos = it->end_charpos;
5118 p->string_nchars = it->string_nchars;
5119 p->area = it->area;
5120 p->multibyte_p = it->multibyte_p;
5121 p->space_width = it->space_width;
5122 p->font_height = it->font_height;
5123 p->voffset = it->voffset;
5124 p->string_from_display_prop_p = it->string_from_display_prop_p;
5125 p->display_ellipsis_p = 0;
5126 ++it->sp;
5127 }
5128
5129
5130 /* Restore IT's settings from IT->stack. Called, for example, when no
5131 more overlay strings must be processed, and we return to delivering
5132 display elements from a buffer, or when the end of a string from a
5133 `display' property is reached and we return to delivering display
5134 elements from an overlay string, or from a buffer. */
5135
5136 static void
5137 pop_it (it)
5138 struct it *it;
5139 {
5140 struct iterator_stack_entry *p;
5141
5142 xassert (it->sp > 0);
5143 --it->sp;
5144 p = it->stack + it->sp;
5145 it->stop_charpos = p->stop_charpos;
5146 it->face_id = p->face_id;
5147 it->current = p->current;
5148 it->position = p->position;
5149 it->string = p->string;
5150 if (NILP (it->string))
5151 SET_TEXT_POS (it->current.string_pos, -1, -1);
5152 it->method = p->method;
5153 switch (it->method)
5154 {
5155 case GET_FROM_IMAGE:
5156 it->image_id = p->u.image.image_id;
5157 it->object = p->u.image.object;
5158 it->slice = p->u.image.slice;
5159 break;
5160 case GET_FROM_COMPOSITION:
5161 it->object = p->u.comp.object;
5162 it->c = p->u.comp.c;
5163 it->len = p->u.comp.len;
5164 it->cmp_id = p->u.comp.cmp_id;
5165 it->cmp_len = p->u.comp.cmp_len;
5166 break;
5167 case GET_FROM_STRETCH:
5168 it->object = p->u.comp.object;
5169 break;
5170 case GET_FROM_BUFFER:
5171 it->object = it->w->buffer;
5172 break;
5173 case GET_FROM_STRING:
5174 it->object = it->string;
5175 break;
5176 }
5177 it->end_charpos = p->end_charpos;
5178 it->string_nchars = p->string_nchars;
5179 it->area = p->area;
5180 it->multibyte_p = p->multibyte_p;
5181 it->space_width = p->space_width;
5182 it->font_height = p->font_height;
5183 it->voffset = p->voffset;
5184 it->string_from_display_prop_p = p->string_from_display_prop_p;
5185 }
5186
5187
5188 \f
5189 /***********************************************************************
5190 Moving over lines
5191 ***********************************************************************/
5192
5193 /* Set IT's current position to the previous line start. */
5194
5195 static void
5196 back_to_previous_line_start (it)
5197 struct it *it;
5198 {
5199 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
5200 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
5201 }
5202
5203
5204 /* Move IT to the next line start.
5205
5206 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
5207 we skipped over part of the text (as opposed to moving the iterator
5208 continuously over the text). Otherwise, don't change the value
5209 of *SKIPPED_P.
5210
5211 Newlines may come from buffer text, overlay strings, or strings
5212 displayed via the `display' property. That's the reason we can't
5213 simply use find_next_newline_no_quit.
5214
5215 Note that this function may not skip over invisible text that is so
5216 because of text properties and immediately follows a newline. If
5217 it would, function reseat_at_next_visible_line_start, when called
5218 from set_iterator_to_next, would effectively make invisible
5219 characters following a newline part of the wrong glyph row, which
5220 leads to wrong cursor motion. */
5221
5222 static int
5223 forward_to_next_line_start (it, skipped_p)
5224 struct it *it;
5225 int *skipped_p;
5226 {
5227 int old_selective, newline_found_p, n;
5228 const int MAX_NEWLINE_DISTANCE = 500;
5229
5230 /* If already on a newline, just consume it to avoid unintended
5231 skipping over invisible text below. */
5232 if (it->what == IT_CHARACTER
5233 && it->c == '\n'
5234 && CHARPOS (it->position) == IT_CHARPOS (*it))
5235 {
5236 set_iterator_to_next (it, 0);
5237 it->c = 0;
5238 return 1;
5239 }
5240
5241 /* Don't handle selective display in the following. It's (a)
5242 unnecessary because it's done by the caller, and (b) leads to an
5243 infinite recursion because next_element_from_ellipsis indirectly
5244 calls this function. */
5245 old_selective = it->selective;
5246 it->selective = 0;
5247
5248 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
5249 from buffer text. */
5250 for (n = newline_found_p = 0;
5251 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
5252 n += STRINGP (it->string) ? 0 : 1)
5253 {
5254 if (!get_next_display_element (it))
5255 return 0;
5256 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
5257 set_iterator_to_next (it, 0);
5258 }
5259
5260 /* If we didn't find a newline near enough, see if we can use a
5261 short-cut. */
5262 if (!newline_found_p)
5263 {
5264 int start = IT_CHARPOS (*it);
5265 int limit = find_next_newline_no_quit (start, 1);
5266 Lisp_Object pos;
5267
5268 xassert (!STRINGP (it->string));
5269
5270 /* If there isn't any `display' property in sight, and no
5271 overlays, we can just use the position of the newline in
5272 buffer text. */
5273 if (it->stop_charpos >= limit
5274 || ((pos = Fnext_single_property_change (make_number (start),
5275 Qdisplay,
5276 Qnil, make_number (limit)),
5277 NILP (pos))
5278 && next_overlay_change (start) == ZV))
5279 {
5280 IT_CHARPOS (*it) = limit;
5281 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
5282 *skipped_p = newline_found_p = 1;
5283 }
5284 else
5285 {
5286 while (get_next_display_element (it)
5287 && !newline_found_p)
5288 {
5289 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
5290 set_iterator_to_next (it, 0);
5291 }
5292 }
5293 }
5294
5295 it->selective = old_selective;
5296 return newline_found_p;
5297 }
5298
5299
5300 /* Set IT's current position to the previous visible line start. Skip
5301 invisible text that is so either due to text properties or due to
5302 selective display. Caution: this does not change IT->current_x and
5303 IT->hpos. */
5304
5305 static void
5306 back_to_previous_visible_line_start (it)
5307 struct it *it;
5308 {
5309 while (IT_CHARPOS (*it) > BEGV)
5310 {
5311 back_to_previous_line_start (it);
5312
5313 if (IT_CHARPOS (*it) <= BEGV)
5314 break;
5315
5316 /* If selective > 0, then lines indented more than that values
5317 are invisible. */
5318 if (it->selective > 0
5319 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5320 (double) it->selective)) /* iftc */
5321 continue;
5322
5323 /* Check the newline before point for invisibility. */
5324 {
5325 Lisp_Object prop;
5326 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
5327 Qinvisible, it->window);
5328 if (TEXT_PROP_MEANS_INVISIBLE (prop))
5329 continue;
5330 }
5331
5332 if (IT_CHARPOS (*it) <= BEGV)
5333 break;
5334
5335 {
5336 struct it it2;
5337 int pos;
5338 int beg, end;
5339 Lisp_Object val, overlay;
5340
5341 /* If newline is part of a composition, continue from start of composition */
5342 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
5343 && beg < IT_CHARPOS (*it))
5344 goto replaced;
5345
5346 /* If newline is replaced by a display property, find start of overlay
5347 or interval and continue search from that point. */
5348 it2 = *it;
5349 pos = --IT_CHARPOS (it2);
5350 --IT_BYTEPOS (it2);
5351 it2.sp = 0;
5352 if (handle_display_prop (&it2) == HANDLED_RETURN
5353 && !NILP (val = get_char_property_and_overlay
5354 (make_number (pos), Qdisplay, Qnil, &overlay))
5355 && (OVERLAYP (overlay)
5356 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
5357 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
5358 goto replaced;
5359
5360 /* Newline is not replaced by anything -- so we are done. */
5361 break;
5362
5363 replaced:
5364 if (beg < BEGV)
5365 beg = BEGV;
5366 IT_CHARPOS (*it) = beg;
5367 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
5368 }
5369 }
5370
5371 it->continuation_lines_width = 0;
5372
5373 xassert (IT_CHARPOS (*it) >= BEGV);
5374 xassert (IT_CHARPOS (*it) == BEGV
5375 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5376 CHECK_IT (it);
5377 }
5378
5379
5380 /* Reseat iterator IT at the previous visible line start. Skip
5381 invisible text that is so either due to text properties or due to
5382 selective display. At the end, update IT's overlay information,
5383 face information etc. */
5384
5385 void
5386 reseat_at_previous_visible_line_start (it)
5387 struct it *it;
5388 {
5389 back_to_previous_visible_line_start (it);
5390 reseat (it, it->current.pos, 1);
5391 CHECK_IT (it);
5392 }
5393
5394
5395 /* Reseat iterator IT on the next visible line start in the current
5396 buffer. ON_NEWLINE_P non-zero means position IT on the newline
5397 preceding the line start. Skip over invisible text that is so
5398 because of selective display. Compute faces, overlays etc at the
5399 new position. Note that this function does not skip over text that
5400 is invisible because of text properties. */
5401
5402 static void
5403 reseat_at_next_visible_line_start (it, on_newline_p)
5404 struct it *it;
5405 int on_newline_p;
5406 {
5407 int newline_found_p, skipped_p = 0;
5408
5409 newline_found_p = forward_to_next_line_start (it, &skipped_p);
5410
5411 /* Skip over lines that are invisible because they are indented
5412 more than the value of IT->selective. */
5413 if (it->selective > 0)
5414 while (IT_CHARPOS (*it) < ZV
5415 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5416 (double) it->selective)) /* iftc */
5417 {
5418 xassert (IT_BYTEPOS (*it) == BEGV
5419 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5420 newline_found_p = forward_to_next_line_start (it, &skipped_p);
5421 }
5422
5423 /* Position on the newline if that's what's requested. */
5424 if (on_newline_p && newline_found_p)
5425 {
5426 if (STRINGP (it->string))
5427 {
5428 if (IT_STRING_CHARPOS (*it) > 0)
5429 {
5430 --IT_STRING_CHARPOS (*it);
5431 --IT_STRING_BYTEPOS (*it);
5432 }
5433 }
5434 else if (IT_CHARPOS (*it) > BEGV)
5435 {
5436 --IT_CHARPOS (*it);
5437 --IT_BYTEPOS (*it);
5438 reseat (it, it->current.pos, 0);
5439 }
5440 }
5441 else if (skipped_p)
5442 reseat (it, it->current.pos, 0);
5443
5444 CHECK_IT (it);
5445 }
5446
5447
5448 \f
5449 /***********************************************************************
5450 Changing an iterator's position
5451 ***********************************************************************/
5452
5453 /* Change IT's current position to POS in current_buffer. If FORCE_P
5454 is non-zero, always check for text properties at the new position.
5455 Otherwise, text properties are only looked up if POS >=
5456 IT->check_charpos of a property. */
5457
5458 static void
5459 reseat (it, pos, force_p)
5460 struct it *it;
5461 struct text_pos pos;
5462 int force_p;
5463 {
5464 int original_pos = IT_CHARPOS (*it);
5465
5466 reseat_1 (it, pos, 0);
5467
5468 /* Determine where to check text properties. Avoid doing it
5469 where possible because text property lookup is very expensive. */
5470 if (force_p
5471 || CHARPOS (pos) > it->stop_charpos
5472 || CHARPOS (pos) < original_pos)
5473 handle_stop (it);
5474
5475 CHECK_IT (it);
5476 }
5477
5478
5479 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
5480 IT->stop_pos to POS, also. */
5481
5482 static void
5483 reseat_1 (it, pos, set_stop_p)
5484 struct it *it;
5485 struct text_pos pos;
5486 int set_stop_p;
5487 {
5488 /* Don't call this function when scanning a C string. */
5489 xassert (it->s == NULL);
5490
5491 /* POS must be a reasonable value. */
5492 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
5493
5494 it->current.pos = it->position = pos;
5495 it->end_charpos = ZV;
5496 it->dpvec = NULL;
5497 it->current.dpvec_index = -1;
5498 it->current.overlay_string_index = -1;
5499 IT_STRING_CHARPOS (*it) = -1;
5500 IT_STRING_BYTEPOS (*it) = -1;
5501 it->string = Qnil;
5502 it->method = GET_FROM_BUFFER;
5503 it->object = it->w->buffer;
5504 it->area = TEXT_AREA;
5505 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
5506 it->sp = 0;
5507 it->string_from_display_prop_p = 0;
5508 it->face_before_selective_p = 0;
5509
5510 if (set_stop_p)
5511 it->stop_charpos = CHARPOS (pos);
5512 }
5513
5514
5515 /* Set up IT for displaying a string, starting at CHARPOS in window W.
5516 If S is non-null, it is a C string to iterate over. Otherwise,
5517 STRING gives a Lisp string to iterate over.
5518
5519 If PRECISION > 0, don't return more then PRECISION number of
5520 characters from the string.
5521
5522 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
5523 characters have been returned. FIELD_WIDTH < 0 means an infinite
5524 field width.
5525
5526 MULTIBYTE = 0 means disable processing of multibyte characters,
5527 MULTIBYTE > 0 means enable it,
5528 MULTIBYTE < 0 means use IT->multibyte_p.
5529
5530 IT must be initialized via a prior call to init_iterator before
5531 calling this function. */
5532
5533 static void
5534 reseat_to_string (it, s, string, charpos, precision, field_width, multibyte)
5535 struct it *it;
5536 unsigned char *s;
5537 Lisp_Object string;
5538 int charpos;
5539 int precision, field_width, multibyte;
5540 {
5541 /* No region in strings. */
5542 it->region_beg_charpos = it->region_end_charpos = -1;
5543
5544 /* No text property checks performed by default, but see below. */
5545 it->stop_charpos = -1;
5546
5547 /* Set iterator position and end position. */
5548 bzero (&it->current, sizeof it->current);
5549 it->current.overlay_string_index = -1;
5550 it->current.dpvec_index = -1;
5551 xassert (charpos >= 0);
5552
5553 /* If STRING is specified, use its multibyteness, otherwise use the
5554 setting of MULTIBYTE, if specified. */
5555 if (multibyte >= 0)
5556 it->multibyte_p = multibyte > 0;
5557
5558 if (s == NULL)
5559 {
5560 xassert (STRINGP (string));
5561 it->string = string;
5562 it->s = NULL;
5563 it->end_charpos = it->string_nchars = SCHARS (string);
5564 it->method = GET_FROM_STRING;
5565 it->current.string_pos = string_pos (charpos, string);
5566 }
5567 else
5568 {
5569 it->s = s;
5570 it->string = Qnil;
5571
5572 /* Note that we use IT->current.pos, not it->current.string_pos,
5573 for displaying C strings. */
5574 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
5575 if (it->multibyte_p)
5576 {
5577 it->current.pos = c_string_pos (charpos, s, 1);
5578 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
5579 }
5580 else
5581 {
5582 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
5583 it->end_charpos = it->string_nchars = strlen (s);
5584 }
5585
5586 it->method = GET_FROM_C_STRING;
5587 }
5588
5589 /* PRECISION > 0 means don't return more than PRECISION characters
5590 from the string. */
5591 if (precision > 0 && it->end_charpos - charpos > precision)
5592 it->end_charpos = it->string_nchars = charpos + precision;
5593
5594 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
5595 characters have been returned. FIELD_WIDTH == 0 means don't pad,
5596 FIELD_WIDTH < 0 means infinite field width. This is useful for
5597 padding with `-' at the end of a mode line. */
5598 if (field_width < 0)
5599 field_width = INFINITY;
5600 if (field_width > it->end_charpos - charpos)
5601 it->end_charpos = charpos + field_width;
5602
5603 /* Use the standard display table for displaying strings. */
5604 if (DISP_TABLE_P (Vstandard_display_table))
5605 it->dp = XCHAR_TABLE (Vstandard_display_table);
5606
5607 it->stop_charpos = charpos;
5608 CHECK_IT (it);
5609 }
5610
5611
5612 \f
5613 /***********************************************************************
5614 Iteration
5615 ***********************************************************************/
5616
5617 /* Map enum it_method value to corresponding next_element_from_* function. */
5618
5619 static int (* get_next_element[NUM_IT_METHODS]) P_ ((struct it *it)) =
5620 {
5621 next_element_from_buffer,
5622 next_element_from_display_vector,
5623 next_element_from_composition,
5624 next_element_from_string,
5625 next_element_from_c_string,
5626 next_element_from_image,
5627 next_element_from_stretch
5628 };
5629
5630
5631 /* Load IT's display element fields with information about the next
5632 display element from the current position of IT. Value is zero if
5633 end of buffer (or C string) is reached. */
5634
5635 static struct frame *last_escape_glyph_frame = NULL;
5636 static unsigned last_escape_glyph_face_id = (1 << FACE_ID_BITS);
5637 static int last_escape_glyph_merged_face_id = 0;
5638
5639 int
5640 get_next_display_element (it)
5641 struct it *it;
5642 {
5643 /* Non-zero means that we found a display element. Zero means that
5644 we hit the end of what we iterate over. Performance note: the
5645 function pointer `method' used here turns out to be faster than
5646 using a sequence of if-statements. */
5647 int success_p;
5648
5649 get_next:
5650 success_p = (*get_next_element[it->method]) (it);
5651
5652 if (it->what == IT_CHARACTER)
5653 {
5654 /* Map via display table or translate control characters.
5655 IT->c, IT->len etc. have been set to the next character by
5656 the function call above. If we have a display table, and it
5657 contains an entry for IT->c, translate it. Don't do this if
5658 IT->c itself comes from a display table, otherwise we could
5659 end up in an infinite recursion. (An alternative could be to
5660 count the recursion depth of this function and signal an
5661 error when a certain maximum depth is reached.) Is it worth
5662 it? */
5663 if (success_p && it->dpvec == NULL)
5664 {
5665 Lisp_Object dv;
5666
5667 if (it->dp
5668 && (dv = DISP_CHAR_VECTOR (it->dp, it->c),
5669 VECTORP (dv)))
5670 {
5671 struct Lisp_Vector *v = XVECTOR (dv);
5672
5673 /* Return the first character from the display table
5674 entry, if not empty. If empty, don't display the
5675 current character. */
5676 if (v->size)
5677 {
5678 it->dpvec_char_len = it->len;
5679 it->dpvec = v->contents;
5680 it->dpend = v->contents + v->size;
5681 it->current.dpvec_index = 0;
5682 it->dpvec_face_id = -1;
5683 it->saved_face_id = it->face_id;
5684 it->method = GET_FROM_DISPLAY_VECTOR;
5685 it->ellipsis_p = 0;
5686 }
5687 else
5688 {
5689 set_iterator_to_next (it, 0);
5690 }
5691 goto get_next;
5692 }
5693
5694 /* Translate control characters into `\003' or `^C' form.
5695 Control characters coming from a display table entry are
5696 currently not translated because we use IT->dpvec to hold
5697 the translation. This could easily be changed but I
5698 don't believe that it is worth doing.
5699
5700 If it->multibyte_p is nonzero, non-printable non-ASCII
5701 characters are also translated to octal form.
5702
5703 If it->multibyte_p is zero, eight-bit characters that
5704 don't have corresponding multibyte char code are also
5705 translated to octal form. */
5706 else if ((it->c < ' '
5707 ? (it->area != TEXT_AREA
5708 /* In mode line, treat \n, \t like other crl chars. */
5709 || (it->c != '\t'
5710 && it->glyph_row && it->glyph_row->mode_line_p)
5711 || (it->c != '\n' && it->c != '\t'))
5712 : (it->multibyte_p
5713 ? (!CHAR_PRINTABLE_P (it->c)
5714 || (!NILP (Vnobreak_char_display)
5715 && (it->c == 0xA0 /* NO-BREAK SPACE */
5716 || it->c == 0xAD /* SOFT HYPHEN */)))
5717 : (it->c >= 127
5718 && (! unibyte_display_via_language_environment
5719 || (UNIBYTE_CHAR_HAS_MULTIBYTE_P (it->c)))))))
5720 {
5721 /* IT->c is a control character which must be displayed
5722 either as '\003' or as `^C' where the '\\' and '^'
5723 can be defined in the display table. Fill
5724 IT->ctl_chars with glyphs for what we have to
5725 display. Then, set IT->dpvec to these glyphs. */
5726 GLYPH g;
5727 int ctl_len;
5728 int face_id, lface_id = 0 ;
5729 GLYPH escape_glyph;
5730
5731 /* Handle control characters with ^. */
5732
5733 if (it->c < 128 && it->ctl_arrow_p)
5734 {
5735 g = '^'; /* default glyph for Control */
5736 /* Set IT->ctl_chars[0] to the glyph for `^'. */
5737 if (it->dp
5738 && INTEGERP (DISP_CTRL_GLYPH (it->dp))
5739 && GLYPH_CHAR_VALID_P (XINT (DISP_CTRL_GLYPH (it->dp))))
5740 {
5741 g = XINT (DISP_CTRL_GLYPH (it->dp));
5742 lface_id = FAST_GLYPH_FACE (g);
5743 }
5744 if (lface_id)
5745 {
5746 g = FAST_GLYPH_CHAR (g);
5747 face_id = merge_faces (it->f, Qt, lface_id,
5748 it->face_id);
5749 }
5750 else if (it->f == last_escape_glyph_frame
5751 && it->face_id == last_escape_glyph_face_id)
5752 {
5753 face_id = last_escape_glyph_merged_face_id;
5754 }
5755 else
5756 {
5757 /* Merge the escape-glyph face into the current face. */
5758 face_id = merge_faces (it->f, Qescape_glyph, 0,
5759 it->face_id);
5760 last_escape_glyph_frame = it->f;
5761 last_escape_glyph_face_id = it->face_id;
5762 last_escape_glyph_merged_face_id = face_id;
5763 }
5764
5765 XSETINT (it->ctl_chars[0], g);
5766 g = it->c ^ 0100;
5767 XSETINT (it->ctl_chars[1], g);
5768 ctl_len = 2;
5769 goto display_control;
5770 }
5771
5772 /* Handle non-break space in the mode where it only gets
5773 highlighting. */
5774
5775 if (EQ (Vnobreak_char_display, Qt)
5776 && it->c == 0xA0)
5777 {
5778 /* Merge the no-break-space face into the current face. */
5779 face_id = merge_faces (it->f, Qnobreak_space, 0,
5780 it->face_id);
5781
5782 g = it->c = ' ';
5783 XSETINT (it->ctl_chars[0], g);
5784 ctl_len = 1;
5785 goto display_control;
5786 }
5787
5788 /* Handle sequences that start with the "escape glyph". */
5789
5790 /* the default escape glyph is \. */
5791 escape_glyph = '\\';
5792
5793 if (it->dp
5794 && INTEGERP (DISP_ESCAPE_GLYPH (it->dp))
5795 && GLYPH_CHAR_VALID_P (XFASTINT (DISP_ESCAPE_GLYPH (it->dp))))
5796 {
5797 escape_glyph = XFASTINT (DISP_ESCAPE_GLYPH (it->dp));
5798 lface_id = FAST_GLYPH_FACE (escape_glyph);
5799 }
5800 if (lface_id)
5801 {
5802 /* The display table specified a face.
5803 Merge it into face_id and also into escape_glyph. */
5804 escape_glyph = FAST_GLYPH_CHAR (escape_glyph);
5805 face_id = merge_faces (it->f, Qt, lface_id,
5806 it->face_id);
5807 }
5808 else if (it->f == last_escape_glyph_frame
5809 && it->face_id == last_escape_glyph_face_id)
5810 {
5811 face_id = last_escape_glyph_merged_face_id;
5812 }
5813 else
5814 {
5815 /* Merge the escape-glyph face into the current face. */
5816 face_id = merge_faces (it->f, Qescape_glyph, 0,
5817 it->face_id);
5818 last_escape_glyph_frame = it->f;
5819 last_escape_glyph_face_id = it->face_id;
5820 last_escape_glyph_merged_face_id = face_id;
5821 }
5822
5823 /* Handle soft hyphens in the mode where they only get
5824 highlighting. */
5825
5826 if (EQ (Vnobreak_char_display, Qt)
5827 && it->c == 0xAD)
5828 {
5829 g = it->c = '-';
5830 XSETINT (it->ctl_chars[0], g);
5831 ctl_len = 1;
5832 goto display_control;
5833 }
5834
5835 /* Handle non-break space and soft hyphen
5836 with the escape glyph. */
5837
5838 if (it->c == 0xA0 || it->c == 0xAD)
5839 {
5840 XSETINT (it->ctl_chars[0], escape_glyph);
5841 g = it->c = (it->c == 0xA0 ? ' ' : '-');
5842 XSETINT (it->ctl_chars[1], g);
5843 ctl_len = 2;
5844 goto display_control;
5845 }
5846
5847 {
5848 unsigned char str[MAX_MULTIBYTE_LENGTH];
5849 int len;
5850 int i;
5851
5852 /* Set IT->ctl_chars[0] to the glyph for `\\'. */
5853 if (CHAR_BYTE8_P (it->c))
5854 {
5855 str[0] = CHAR_TO_BYTE8 (it->c);
5856 len = 1;
5857 }
5858 else if (it->c < 256)
5859 {
5860 str[0] = it->c;
5861 len = 1;
5862 }
5863 else
5864 {
5865 /* It's an invalid character, which shouldn't
5866 happen actually, but due to bugs it may
5867 happen. Let's print the char as is, there's
5868 not much meaningful we can do with it. */
5869 str[0] = it->c;
5870 str[1] = it->c >> 8;
5871 str[2] = it->c >> 16;
5872 str[3] = it->c >> 24;
5873 len = 4;
5874 }
5875
5876 for (i = 0; i < len; i++)
5877 {
5878 XSETINT (it->ctl_chars[i * 4], escape_glyph);
5879 /* Insert three more glyphs into IT->ctl_chars for
5880 the octal display of the character. */
5881 g = ((str[i] >> 6) & 7) + '0';
5882 XSETINT (it->ctl_chars[i * 4 + 1], g);
5883 g = ((str[i] >> 3) & 7) + '0';
5884 XSETINT (it->ctl_chars[i * 4 + 2], g);
5885 g = (str[i] & 7) + '0';
5886 XSETINT (it->ctl_chars[i * 4 + 3], g);
5887 }
5888 ctl_len = len * 4;
5889 }
5890
5891 display_control:
5892 /* Set up IT->dpvec and return first character from it. */
5893 it->dpvec_char_len = it->len;
5894 it->dpvec = it->ctl_chars;
5895 it->dpend = it->dpvec + ctl_len;
5896 it->current.dpvec_index = 0;
5897 it->dpvec_face_id = face_id;
5898 it->saved_face_id = it->face_id;
5899 it->method = GET_FROM_DISPLAY_VECTOR;
5900 it->ellipsis_p = 0;
5901 goto get_next;
5902 }
5903 }
5904 }
5905
5906 /* Adjust face id for a multibyte character. There are no multibyte
5907 character in unibyte text. */
5908 if ((it->what == IT_CHARACTER || it->what == IT_COMPOSITION)
5909 && it->multibyte_p
5910 && success_p
5911 && FRAME_WINDOW_P (it->f))
5912 {
5913 struct face *face = FACE_FROM_ID (it->f, it->face_id);
5914 int pos = (it->s ? -1
5915 : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
5916 : IT_CHARPOS (*it));
5917
5918 it->face_id = FACE_FOR_CHAR (it->f, face, it->c, pos, it->string);
5919 }
5920
5921 /* Is this character the last one of a run of characters with
5922 box? If yes, set IT->end_of_box_run_p to 1. */
5923 if (it->face_box_p
5924 && it->s == NULL)
5925 {
5926 int face_id;
5927 struct face *face;
5928
5929 it->end_of_box_run_p
5930 = ((face_id = face_after_it_pos (it),
5931 face_id != it->face_id)
5932 && (face = FACE_FROM_ID (it->f, face_id),
5933 face->box == FACE_NO_BOX));
5934 }
5935
5936 /* Value is 0 if end of buffer or string reached. */
5937 return success_p;
5938 }
5939
5940
5941 /* Move IT to the next display element.
5942
5943 RESEAT_P non-zero means if called on a newline in buffer text,
5944 skip to the next visible line start.
5945
5946 Functions get_next_display_element and set_iterator_to_next are
5947 separate because I find this arrangement easier to handle than a
5948 get_next_display_element function that also increments IT's
5949 position. The way it is we can first look at an iterator's current
5950 display element, decide whether it fits on a line, and if it does,
5951 increment the iterator position. The other way around we probably
5952 would either need a flag indicating whether the iterator has to be
5953 incremented the next time, or we would have to implement a
5954 decrement position function which would not be easy to write. */
5955
5956 void
5957 set_iterator_to_next (it, reseat_p)
5958 struct it *it;
5959 int reseat_p;
5960 {
5961 /* Reset flags indicating start and end of a sequence of characters
5962 with box. Reset them at the start of this function because
5963 moving the iterator to a new position might set them. */
5964 it->start_of_box_run_p = it->end_of_box_run_p = 0;
5965
5966 switch (it->method)
5967 {
5968 case GET_FROM_BUFFER:
5969 /* The current display element of IT is a character from
5970 current_buffer. Advance in the buffer, and maybe skip over
5971 invisible lines that are so because of selective display. */
5972 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
5973 reseat_at_next_visible_line_start (it, 0);
5974 else
5975 {
5976 xassert (it->len != 0);
5977 IT_BYTEPOS (*it) += it->len;
5978 IT_CHARPOS (*it) += 1;
5979 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
5980 }
5981 break;
5982
5983 case GET_FROM_COMPOSITION:
5984 xassert (it->cmp_id >= 0 && it->cmp_id < n_compositions);
5985 xassert (it->sp > 0);
5986 pop_it (it);
5987 if (it->method == GET_FROM_STRING)
5988 {
5989 IT_STRING_BYTEPOS (*it) += it->len;
5990 IT_STRING_CHARPOS (*it) += it->cmp_len;
5991 goto consider_string_end;
5992 }
5993 else if (it->method == GET_FROM_BUFFER)
5994 {
5995 IT_BYTEPOS (*it) += it->len;
5996 IT_CHARPOS (*it) += it->cmp_len;
5997 }
5998 break;
5999
6000 case GET_FROM_C_STRING:
6001 /* Current display element of IT is from a C string. */
6002 IT_BYTEPOS (*it) += it->len;
6003 IT_CHARPOS (*it) += 1;
6004 break;
6005
6006 case GET_FROM_DISPLAY_VECTOR:
6007 /* Current display element of IT is from a display table entry.
6008 Advance in the display table definition. Reset it to null if
6009 end reached, and continue with characters from buffers/
6010 strings. */
6011 ++it->current.dpvec_index;
6012
6013 /* Restore face of the iterator to what they were before the
6014 display vector entry (these entries may contain faces). */
6015 it->face_id = it->saved_face_id;
6016
6017 if (it->dpvec + it->current.dpvec_index == it->dpend)
6018 {
6019 int recheck_faces = it->ellipsis_p;
6020
6021 if (it->s)
6022 it->method = GET_FROM_C_STRING;
6023 else if (STRINGP (it->string))
6024 it->method = GET_FROM_STRING;
6025 else
6026 {
6027 it->method = GET_FROM_BUFFER;
6028 it->object = it->w->buffer;
6029 }
6030
6031 it->dpvec = NULL;
6032 it->current.dpvec_index = -1;
6033
6034 /* Skip over characters which were displayed via IT->dpvec. */
6035 if (it->dpvec_char_len < 0)
6036 reseat_at_next_visible_line_start (it, 1);
6037 else if (it->dpvec_char_len > 0)
6038 {
6039 if (it->method == GET_FROM_STRING
6040 && it->n_overlay_strings > 0)
6041 it->ignore_overlay_strings_at_pos_p = 1;
6042 it->len = it->dpvec_char_len;
6043 set_iterator_to_next (it, reseat_p);
6044 }
6045
6046 /* Maybe recheck faces after display vector */
6047 if (recheck_faces)
6048 it->stop_charpos = IT_CHARPOS (*it);
6049 }
6050 break;
6051
6052 case GET_FROM_STRING:
6053 /* Current display element is a character from a Lisp string. */
6054 xassert (it->s == NULL && STRINGP (it->string));
6055 IT_STRING_BYTEPOS (*it) += it->len;
6056 IT_STRING_CHARPOS (*it) += 1;
6057
6058 consider_string_end:
6059
6060 if (it->current.overlay_string_index >= 0)
6061 {
6062 /* IT->string is an overlay string. Advance to the
6063 next, if there is one. */
6064 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
6065 next_overlay_string (it);
6066 }
6067 else
6068 {
6069 /* IT->string is not an overlay string. If we reached
6070 its end, and there is something on IT->stack, proceed
6071 with what is on the stack. This can be either another
6072 string, this time an overlay string, or a buffer. */
6073 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
6074 && it->sp > 0)
6075 {
6076 pop_it (it);
6077 if (it->method == GET_FROM_STRING)
6078 goto consider_string_end;
6079 }
6080 }
6081 break;
6082
6083 case GET_FROM_IMAGE:
6084 case GET_FROM_STRETCH:
6085 /* The position etc with which we have to proceed are on
6086 the stack. The position may be at the end of a string,
6087 if the `display' property takes up the whole string. */
6088 xassert (it->sp > 0);
6089 pop_it (it);
6090 if (it->method == GET_FROM_STRING)
6091 goto consider_string_end;
6092 break;
6093
6094 default:
6095 /* There are no other methods defined, so this should be a bug. */
6096 abort ();
6097 }
6098
6099 xassert (it->method != GET_FROM_STRING
6100 || (STRINGP (it->string)
6101 && IT_STRING_CHARPOS (*it) >= 0));
6102 }
6103
6104 /* Load IT's display element fields with information about the next
6105 display element which comes from a display table entry or from the
6106 result of translating a control character to one of the forms `^C'
6107 or `\003'.
6108
6109 IT->dpvec holds the glyphs to return as characters.
6110 IT->saved_face_id holds the face id before the display vector--
6111 it is restored into IT->face_idin set_iterator_to_next. */
6112
6113 static int
6114 next_element_from_display_vector (it)
6115 struct it *it;
6116 {
6117 /* Precondition. */
6118 xassert (it->dpvec && it->current.dpvec_index >= 0);
6119
6120 it->face_id = it->saved_face_id;
6121
6122 if (INTEGERP (*it->dpvec)
6123 && GLYPH_CHAR_VALID_P (XFASTINT (*it->dpvec)))
6124 {
6125 GLYPH g;
6126
6127 g = XFASTINT (it->dpvec[it->current.dpvec_index]);
6128 it->c = FAST_GLYPH_CHAR (g);
6129 it->len = CHAR_BYTES (it->c);
6130
6131 /* The entry may contain a face id to use. Such a face id is
6132 the id of a Lisp face, not a realized face. A face id of
6133 zero means no face is specified. */
6134 if (it->dpvec_face_id >= 0)
6135 it->face_id = it->dpvec_face_id;
6136 else
6137 {
6138 int lface_id = FAST_GLYPH_FACE (g);
6139 if (lface_id > 0)
6140 it->face_id = merge_faces (it->f, Qt, lface_id,
6141 it->saved_face_id);
6142 }
6143 }
6144 else
6145 /* Display table entry is invalid. Return a space. */
6146 it->c = ' ', it->len = 1;
6147
6148 /* Don't change position and object of the iterator here. They are
6149 still the values of the character that had this display table
6150 entry or was translated, and that's what we want. */
6151 it->what = IT_CHARACTER;
6152 return 1;
6153 }
6154
6155
6156 /* Load IT with the next display element from Lisp string IT->string.
6157 IT->current.string_pos is the current position within the string.
6158 If IT->current.overlay_string_index >= 0, the Lisp string is an
6159 overlay string. */
6160
6161 static int
6162 next_element_from_string (it)
6163 struct it *it;
6164 {
6165 struct text_pos position;
6166
6167 xassert (STRINGP (it->string));
6168 xassert (IT_STRING_CHARPOS (*it) >= 0);
6169 position = it->current.string_pos;
6170
6171 /* Time to check for invisible text? */
6172 if (IT_STRING_CHARPOS (*it) < it->end_charpos
6173 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
6174 {
6175 handle_stop (it);
6176
6177 /* Since a handler may have changed IT->method, we must
6178 recurse here. */
6179 return get_next_display_element (it);
6180 }
6181
6182 if (it->current.overlay_string_index >= 0)
6183 {
6184 /* Get the next character from an overlay string. In overlay
6185 strings, There is no field width or padding with spaces to
6186 do. */
6187 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
6188 {
6189 it->what = IT_EOB;
6190 return 0;
6191 }
6192 else if (STRING_MULTIBYTE (it->string))
6193 {
6194 int remaining = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
6195 const unsigned char *s = (SDATA (it->string)
6196 + IT_STRING_BYTEPOS (*it));
6197 it->c = string_char_and_length (s, remaining, &it->len);
6198 }
6199 else
6200 {
6201 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
6202 it->len = 1;
6203 }
6204 }
6205 else
6206 {
6207 /* Get the next character from a Lisp string that is not an
6208 overlay string. Such strings come from the mode line, for
6209 example. We may have to pad with spaces, or truncate the
6210 string. See also next_element_from_c_string. */
6211 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
6212 {
6213 it->what = IT_EOB;
6214 return 0;
6215 }
6216 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
6217 {
6218 /* Pad with spaces. */
6219 it->c = ' ', it->len = 1;
6220 CHARPOS (position) = BYTEPOS (position) = -1;
6221 }
6222 else if (STRING_MULTIBYTE (it->string))
6223 {
6224 int maxlen = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
6225 const unsigned char *s = (SDATA (it->string)
6226 + IT_STRING_BYTEPOS (*it));
6227 it->c = string_char_and_length (s, maxlen, &it->len);
6228 }
6229 else
6230 {
6231 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
6232 it->len = 1;
6233 }
6234 }
6235
6236 /* Record what we have and where it came from. */
6237 it->what = IT_CHARACTER;
6238 it->object = it->string;
6239 it->position = position;
6240 return 1;
6241 }
6242
6243
6244 /* Load IT with next display element from C string IT->s.
6245 IT->string_nchars is the maximum number of characters to return
6246 from the string. IT->end_charpos may be greater than
6247 IT->string_nchars when this function is called, in which case we
6248 may have to return padding spaces. Value is zero if end of string
6249 reached, including padding spaces. */
6250
6251 static int
6252 next_element_from_c_string (it)
6253 struct it *it;
6254 {
6255 int success_p = 1;
6256
6257 xassert (it->s);
6258 it->what = IT_CHARACTER;
6259 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
6260 it->object = Qnil;
6261
6262 /* IT's position can be greater IT->string_nchars in case a field
6263 width or precision has been specified when the iterator was
6264 initialized. */
6265 if (IT_CHARPOS (*it) >= it->end_charpos)
6266 {
6267 /* End of the game. */
6268 it->what = IT_EOB;
6269 success_p = 0;
6270 }
6271 else if (IT_CHARPOS (*it) >= it->string_nchars)
6272 {
6273 /* Pad with spaces. */
6274 it->c = ' ', it->len = 1;
6275 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
6276 }
6277 else if (it->multibyte_p)
6278 {
6279 /* Implementation note: The calls to strlen apparently aren't a
6280 performance problem because there is no noticeable performance
6281 difference between Emacs running in unibyte or multibyte mode. */
6282 int maxlen = strlen (it->s) - IT_BYTEPOS (*it);
6283 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it),
6284 maxlen, &it->len);
6285 }
6286 else
6287 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
6288
6289 return success_p;
6290 }
6291
6292
6293 /* Set up IT to return characters from an ellipsis, if appropriate.
6294 The definition of the ellipsis glyphs may come from a display table
6295 entry. This function Fills IT with the first glyph from the
6296 ellipsis if an ellipsis is to be displayed. */
6297
6298 static int
6299 next_element_from_ellipsis (it)
6300 struct it *it;
6301 {
6302 if (it->selective_display_ellipsis_p)
6303 setup_for_ellipsis (it, it->len);
6304 else
6305 {
6306 /* The face at the current position may be different from the
6307 face we find after the invisible text. Remember what it
6308 was in IT->saved_face_id, and signal that it's there by
6309 setting face_before_selective_p. */
6310 it->saved_face_id = it->face_id;
6311 it->method = GET_FROM_BUFFER;
6312 it->object = it->w->buffer;
6313 reseat_at_next_visible_line_start (it, 1);
6314 it->face_before_selective_p = 1;
6315 }
6316
6317 return get_next_display_element (it);
6318 }
6319
6320
6321 /* Deliver an image display element. The iterator IT is already
6322 filled with image information (done in handle_display_prop). Value
6323 is always 1. */
6324
6325
6326 static int
6327 next_element_from_image (it)
6328 struct it *it;
6329 {
6330 it->what = IT_IMAGE;
6331 return 1;
6332 }
6333
6334
6335 /* Fill iterator IT with next display element from a stretch glyph
6336 property. IT->object is the value of the text property. Value is
6337 always 1. */
6338
6339 static int
6340 next_element_from_stretch (it)
6341 struct it *it;
6342 {
6343 it->what = IT_STRETCH;
6344 return 1;
6345 }
6346
6347
6348 /* Load IT with the next display element from current_buffer. Value
6349 is zero if end of buffer reached. IT->stop_charpos is the next
6350 position at which to stop and check for text properties or buffer
6351 end. */
6352
6353 static int
6354 next_element_from_buffer (it)
6355 struct it *it;
6356 {
6357 int success_p = 1;
6358
6359 /* Check this assumption, otherwise, we would never enter the
6360 if-statement, below. */
6361 xassert (IT_CHARPOS (*it) >= BEGV
6362 && IT_CHARPOS (*it) <= it->stop_charpos);
6363
6364 if (IT_CHARPOS (*it) >= it->stop_charpos)
6365 {
6366 if (IT_CHARPOS (*it) >= it->end_charpos)
6367 {
6368 int overlay_strings_follow_p;
6369
6370 /* End of the game, except when overlay strings follow that
6371 haven't been returned yet. */
6372 if (it->overlay_strings_at_end_processed_p)
6373 overlay_strings_follow_p = 0;
6374 else
6375 {
6376 it->overlay_strings_at_end_processed_p = 1;
6377 overlay_strings_follow_p = get_overlay_strings (it, 0);
6378 }
6379
6380 if (overlay_strings_follow_p)
6381 success_p = get_next_display_element (it);
6382 else
6383 {
6384 it->what = IT_EOB;
6385 it->position = it->current.pos;
6386 success_p = 0;
6387 }
6388 }
6389 else
6390 {
6391 handle_stop (it);
6392 return get_next_display_element (it);
6393 }
6394 }
6395 else
6396 {
6397 /* No face changes, overlays etc. in sight, so just return a
6398 character from current_buffer. */
6399 unsigned char *p;
6400
6401 /* Maybe run the redisplay end trigger hook. Performance note:
6402 This doesn't seem to cost measurable time. */
6403 if (it->redisplay_end_trigger_charpos
6404 && it->glyph_row
6405 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
6406 run_redisplay_end_trigger_hook (it);
6407
6408 /* Get the next character, maybe multibyte. */
6409 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
6410 if (it->multibyte_p && !ASCII_BYTE_P (*p))
6411 {
6412 int maxlen = ((IT_BYTEPOS (*it) >= GPT_BYTE ? ZV_BYTE : GPT_BYTE)
6413 - IT_BYTEPOS (*it));
6414 it->c = string_char_and_length (p, maxlen, &it->len);
6415 }
6416 else
6417 it->c = *p, it->len = 1;
6418
6419 /* Record what we have and where it came from. */
6420 it->what = IT_CHARACTER;
6421 it->object = it->w->buffer;
6422 it->position = it->current.pos;
6423
6424 /* Normally we return the character found above, except when we
6425 really want to return an ellipsis for selective display. */
6426 if (it->selective)
6427 {
6428 if (it->c == '\n')
6429 {
6430 /* A value of selective > 0 means hide lines indented more
6431 than that number of columns. */
6432 if (it->selective > 0
6433 && IT_CHARPOS (*it) + 1 < ZV
6434 && indented_beyond_p (IT_CHARPOS (*it) + 1,
6435 IT_BYTEPOS (*it) + 1,
6436 (double) it->selective)) /* iftc */
6437 {
6438 success_p = next_element_from_ellipsis (it);
6439 it->dpvec_char_len = -1;
6440 }
6441 }
6442 else if (it->c == '\r' && it->selective == -1)
6443 {
6444 /* A value of selective == -1 means that everything from the
6445 CR to the end of the line is invisible, with maybe an
6446 ellipsis displayed for it. */
6447 success_p = next_element_from_ellipsis (it);
6448 it->dpvec_char_len = -1;
6449 }
6450 }
6451 }
6452
6453 /* Value is zero if end of buffer reached. */
6454 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
6455 return success_p;
6456 }
6457
6458
6459 /* Run the redisplay end trigger hook for IT. */
6460
6461 static void
6462 run_redisplay_end_trigger_hook (it)
6463 struct it *it;
6464 {
6465 Lisp_Object args[3];
6466
6467 /* IT->glyph_row should be non-null, i.e. we should be actually
6468 displaying something, or otherwise we should not run the hook. */
6469 xassert (it->glyph_row);
6470
6471 /* Set up hook arguments. */
6472 args[0] = Qredisplay_end_trigger_functions;
6473 args[1] = it->window;
6474 XSETINT (args[2], it->redisplay_end_trigger_charpos);
6475 it->redisplay_end_trigger_charpos = 0;
6476
6477 /* Since we are *trying* to run these functions, don't try to run
6478 them again, even if they get an error. */
6479 it->w->redisplay_end_trigger = Qnil;
6480 Frun_hook_with_args (3, args);
6481
6482 /* Notice if it changed the face of the character we are on. */
6483 handle_face_prop (it);
6484 }
6485
6486
6487 /* Deliver a composition display element. The iterator IT is already
6488 filled with composition information (done in
6489 handle_composition_prop). Value is always 1. */
6490
6491 static int
6492 next_element_from_composition (it)
6493 struct it *it;
6494 {
6495 it->what = IT_COMPOSITION;
6496 it->position = (STRINGP (it->string)
6497 ? it->current.string_pos
6498 : it->current.pos);
6499 if (STRINGP (it->string))
6500 it->object = it->string;
6501 else
6502 it->object = it->w->buffer;
6503 return 1;
6504 }
6505
6506
6507 \f
6508 /***********************************************************************
6509 Moving an iterator without producing glyphs
6510 ***********************************************************************/
6511
6512 /* Check if iterator is at a position corresponding to a valid buffer
6513 position after some move_it_ call. */
6514
6515 #define IT_POS_VALID_AFTER_MOVE_P(it) \
6516 ((it)->method == GET_FROM_STRING \
6517 ? IT_STRING_CHARPOS (*it) == 0 \
6518 : 1)
6519
6520
6521 /* Move iterator IT to a specified buffer or X position within one
6522 line on the display without producing glyphs.
6523
6524 OP should be a bit mask including some or all of these bits:
6525 MOVE_TO_X: Stop on reaching x-position TO_X.
6526 MOVE_TO_POS: Stop on reaching buffer or string position TO_CHARPOS.
6527 Regardless of OP's value, stop in reaching the end of the display line.
6528
6529 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
6530 This means, in particular, that TO_X includes window's horizontal
6531 scroll amount.
6532
6533 The return value has several possible values that
6534 say what condition caused the scan to stop:
6535
6536 MOVE_POS_MATCH_OR_ZV
6537 - when TO_POS or ZV was reached.
6538
6539 MOVE_X_REACHED
6540 -when TO_X was reached before TO_POS or ZV were reached.
6541
6542 MOVE_LINE_CONTINUED
6543 - when we reached the end of the display area and the line must
6544 be continued.
6545
6546 MOVE_LINE_TRUNCATED
6547 - when we reached the end of the display area and the line is
6548 truncated.
6549
6550 MOVE_NEWLINE_OR_CR
6551 - when we stopped at a line end, i.e. a newline or a CR and selective
6552 display is on. */
6553
6554 static enum move_it_result
6555 move_it_in_display_line_to (it, to_charpos, to_x, op)
6556 struct it *it;
6557 int to_charpos, to_x, op;
6558 {
6559 enum move_it_result result = MOVE_UNDEFINED;
6560 struct glyph_row *saved_glyph_row;
6561
6562 /* Don't produce glyphs in produce_glyphs. */
6563 saved_glyph_row = it->glyph_row;
6564 it->glyph_row = NULL;
6565
6566 #define BUFFER_POS_REACHED_P() \
6567 ((op & MOVE_TO_POS) != 0 \
6568 && BUFFERP (it->object) \
6569 && IT_CHARPOS (*it) >= to_charpos \
6570 && (it->method == GET_FROM_BUFFER \
6571 || (it->method == GET_FROM_DISPLAY_VECTOR \
6572 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
6573
6574
6575 while (1)
6576 {
6577 int x, i, ascent = 0, descent = 0;
6578
6579 /* Stop if we move beyond TO_CHARPOS (after an image or stretch glyph). */
6580 if ((op & MOVE_TO_POS) != 0
6581 && BUFFERP (it->object)
6582 && it->method == GET_FROM_BUFFER
6583 && IT_CHARPOS (*it) > to_charpos)
6584 {
6585 result = MOVE_POS_MATCH_OR_ZV;
6586 break;
6587 }
6588
6589 /* Stop when ZV reached.
6590 We used to stop here when TO_CHARPOS reached as well, but that is
6591 too soon if this glyph does not fit on this line. So we handle it
6592 explicitly below. */
6593 if (!get_next_display_element (it)
6594 || (it->truncate_lines_p
6595 && BUFFER_POS_REACHED_P ()))
6596 {
6597 result = MOVE_POS_MATCH_OR_ZV;
6598 break;
6599 }
6600
6601 /* The call to produce_glyphs will get the metrics of the
6602 display element IT is loaded with. We record in x the
6603 x-position before this display element in case it does not
6604 fit on the line. */
6605 x = it->current_x;
6606
6607 /* Remember the line height so far in case the next element doesn't
6608 fit on the line. */
6609 if (!it->truncate_lines_p)
6610 {
6611 ascent = it->max_ascent;
6612 descent = it->max_descent;
6613 }
6614
6615 PRODUCE_GLYPHS (it);
6616
6617 if (it->area != TEXT_AREA)
6618 {
6619 set_iterator_to_next (it, 1);
6620 continue;
6621 }
6622
6623 /* The number of glyphs we get back in IT->nglyphs will normally
6624 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
6625 character on a terminal frame, or (iii) a line end. For the
6626 second case, IT->nglyphs - 1 padding glyphs will be present
6627 (on X frames, there is only one glyph produced for a
6628 composite character.
6629
6630 The behavior implemented below means, for continuation lines,
6631 that as many spaces of a TAB as fit on the current line are
6632 displayed there. For terminal frames, as many glyphs of a
6633 multi-glyph character are displayed in the current line, too.
6634 This is what the old redisplay code did, and we keep it that
6635 way. Under X, the whole shape of a complex character must
6636 fit on the line or it will be completely displayed in the
6637 next line.
6638
6639 Note that both for tabs and padding glyphs, all glyphs have
6640 the same width. */
6641 if (it->nglyphs)
6642 {
6643 /* More than one glyph or glyph doesn't fit on line. All
6644 glyphs have the same width. */
6645 int single_glyph_width = it->pixel_width / it->nglyphs;
6646 int new_x;
6647 int x_before_this_char = x;
6648 int hpos_before_this_char = it->hpos;
6649
6650 for (i = 0; i < it->nglyphs; ++i, x = new_x)
6651 {
6652 new_x = x + single_glyph_width;
6653
6654 /* We want to leave anything reaching TO_X to the caller. */
6655 if ((op & MOVE_TO_X) && new_x > to_x)
6656 {
6657 if (BUFFER_POS_REACHED_P ())
6658 goto buffer_pos_reached;
6659 it->current_x = x;
6660 result = MOVE_X_REACHED;
6661 break;
6662 }
6663 else if (/* Lines are continued. */
6664 !it->truncate_lines_p
6665 && (/* And glyph doesn't fit on the line. */
6666 new_x > it->last_visible_x
6667 /* Or it fits exactly and we're on a window
6668 system frame. */
6669 || (new_x == it->last_visible_x
6670 && FRAME_WINDOW_P (it->f))))
6671 {
6672 if (/* IT->hpos == 0 means the very first glyph
6673 doesn't fit on the line, e.g. a wide image. */
6674 it->hpos == 0
6675 || (new_x == it->last_visible_x
6676 && FRAME_WINDOW_P (it->f)))
6677 {
6678 ++it->hpos;
6679 it->current_x = new_x;
6680
6681 /* The character's last glyph just barely fits
6682 in this row. */
6683 if (i == it->nglyphs - 1)
6684 {
6685 /* If this is the destination position,
6686 return a position *before* it in this row,
6687 now that we know it fits in this row. */
6688 if (BUFFER_POS_REACHED_P ())
6689 {
6690 it->hpos = hpos_before_this_char;
6691 it->current_x = x_before_this_char;
6692 result = MOVE_POS_MATCH_OR_ZV;
6693 break;
6694 }
6695
6696 set_iterator_to_next (it, 1);
6697 #ifdef HAVE_WINDOW_SYSTEM
6698 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
6699 {
6700 if (!get_next_display_element (it))
6701 {
6702 result = MOVE_POS_MATCH_OR_ZV;
6703 break;
6704 }
6705 if (BUFFER_POS_REACHED_P ())
6706 {
6707 if (ITERATOR_AT_END_OF_LINE_P (it))
6708 result = MOVE_POS_MATCH_OR_ZV;
6709 else
6710 result = MOVE_LINE_CONTINUED;
6711 break;
6712 }
6713 if (ITERATOR_AT_END_OF_LINE_P (it))
6714 {
6715 result = MOVE_NEWLINE_OR_CR;
6716 break;
6717 }
6718 }
6719 #endif /* HAVE_WINDOW_SYSTEM */
6720 }
6721 }
6722 else
6723 {
6724 it->current_x = x;
6725 it->max_ascent = ascent;
6726 it->max_descent = descent;
6727 }
6728
6729 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
6730 IT_CHARPOS (*it)));
6731 result = MOVE_LINE_CONTINUED;
6732 break;
6733 }
6734 else if (BUFFER_POS_REACHED_P ())
6735 goto buffer_pos_reached;
6736 else if (new_x > it->first_visible_x)
6737 {
6738 /* Glyph is visible. Increment number of glyphs that
6739 would be displayed. */
6740 ++it->hpos;
6741 }
6742 else
6743 {
6744 /* Glyph is completely off the left margin of the display
6745 area. Nothing to do. */
6746 }
6747 }
6748
6749 if (result != MOVE_UNDEFINED)
6750 break;
6751 }
6752 else if (BUFFER_POS_REACHED_P ())
6753 {
6754 buffer_pos_reached:
6755 it->current_x = x;
6756 it->max_ascent = ascent;
6757 it->max_descent = descent;
6758 result = MOVE_POS_MATCH_OR_ZV;
6759 break;
6760 }
6761 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
6762 {
6763 /* Stop when TO_X specified and reached. This check is
6764 necessary here because of lines consisting of a line end,
6765 only. The line end will not produce any glyphs and we
6766 would never get MOVE_X_REACHED. */
6767 xassert (it->nglyphs == 0);
6768 result = MOVE_X_REACHED;
6769 break;
6770 }
6771
6772 /* Is this a line end? If yes, we're done. */
6773 if (ITERATOR_AT_END_OF_LINE_P (it))
6774 {
6775 result = MOVE_NEWLINE_OR_CR;
6776 break;
6777 }
6778
6779 /* The current display element has been consumed. Advance
6780 to the next. */
6781 set_iterator_to_next (it, 1);
6782
6783 /* Stop if lines are truncated and IT's current x-position is
6784 past the right edge of the window now. */
6785 if (it->truncate_lines_p
6786 && it->current_x >= it->last_visible_x)
6787 {
6788 #ifdef HAVE_WINDOW_SYSTEM
6789 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
6790 {
6791 if (!get_next_display_element (it)
6792 || BUFFER_POS_REACHED_P ())
6793 {
6794 result = MOVE_POS_MATCH_OR_ZV;
6795 break;
6796 }
6797 if (ITERATOR_AT_END_OF_LINE_P (it))
6798 {
6799 result = MOVE_NEWLINE_OR_CR;
6800 break;
6801 }
6802 }
6803 #endif /* HAVE_WINDOW_SYSTEM */
6804 result = MOVE_LINE_TRUNCATED;
6805 break;
6806 }
6807 }
6808
6809 #undef BUFFER_POS_REACHED_P
6810
6811 /* Restore the iterator settings altered at the beginning of this
6812 function. */
6813 it->glyph_row = saved_glyph_row;
6814 return result;
6815 }
6816
6817
6818 /* Move IT forward until it satisfies one or more of the criteria in
6819 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
6820
6821 OP is a bit-mask that specifies where to stop, and in particular,
6822 which of those four position arguments makes a difference. See the
6823 description of enum move_operation_enum.
6824
6825 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
6826 screen line, this function will set IT to the next position >
6827 TO_CHARPOS. */
6828
6829 void
6830 move_it_to (it, to_charpos, to_x, to_y, to_vpos, op)
6831 struct it *it;
6832 int to_charpos, to_x, to_y, to_vpos;
6833 int op;
6834 {
6835 enum move_it_result skip, skip2 = MOVE_X_REACHED;
6836 int line_height;
6837 int reached = 0;
6838
6839 for (;;)
6840 {
6841 if (op & MOVE_TO_VPOS)
6842 {
6843 /* If no TO_CHARPOS and no TO_X specified, stop at the
6844 start of the line TO_VPOS. */
6845 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
6846 {
6847 if (it->vpos == to_vpos)
6848 {
6849 reached = 1;
6850 break;
6851 }
6852 else
6853 skip = move_it_in_display_line_to (it, -1, -1, 0);
6854 }
6855 else
6856 {
6857 /* TO_VPOS >= 0 means stop at TO_X in the line at
6858 TO_VPOS, or at TO_POS, whichever comes first. */
6859 if (it->vpos == to_vpos)
6860 {
6861 reached = 2;
6862 break;
6863 }
6864
6865 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
6866
6867 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
6868 {
6869 reached = 3;
6870 break;
6871 }
6872 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
6873 {
6874 /* We have reached TO_X but not in the line we want. */
6875 skip = move_it_in_display_line_to (it, to_charpos,
6876 -1, MOVE_TO_POS);
6877 if (skip == MOVE_POS_MATCH_OR_ZV)
6878 {
6879 reached = 4;
6880 break;
6881 }
6882 }
6883 }
6884 }
6885 else if (op & MOVE_TO_Y)
6886 {
6887 struct it it_backup;
6888
6889 /* TO_Y specified means stop at TO_X in the line containing
6890 TO_Y---or at TO_CHARPOS if this is reached first. The
6891 problem is that we can't really tell whether the line
6892 contains TO_Y before we have completely scanned it, and
6893 this may skip past TO_X. What we do is to first scan to
6894 TO_X.
6895
6896 If TO_X is not specified, use a TO_X of zero. The reason
6897 is to make the outcome of this function more predictable.
6898 If we didn't use TO_X == 0, we would stop at the end of
6899 the line which is probably not what a caller would expect
6900 to happen. */
6901 skip = move_it_in_display_line_to (it, to_charpos,
6902 ((op & MOVE_TO_X)
6903 ? to_x : 0),
6904 (MOVE_TO_X
6905 | (op & MOVE_TO_POS)));
6906
6907 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
6908 if (skip == MOVE_POS_MATCH_OR_ZV)
6909 {
6910 reached = 5;
6911 break;
6912 }
6913
6914 /* If TO_X was reached, we would like to know whether TO_Y
6915 is in the line. This can only be said if we know the
6916 total line height which requires us to scan the rest of
6917 the line. */
6918 if (skip == MOVE_X_REACHED)
6919 {
6920 /* Wait! We can conclude that TO_Y is in the line if
6921 the already scanned glyphs make the line tall enough
6922 because further scanning doesn't make it shorter. */
6923 line_height = it->max_ascent + it->max_descent;
6924 if (to_y >= it->current_y
6925 && to_y < it->current_y + line_height)
6926 {
6927 reached = 6;
6928 break;
6929 }
6930 it_backup = *it;
6931 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
6932 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
6933 op & MOVE_TO_POS);
6934 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
6935 }
6936
6937 /* Now, decide whether TO_Y is in this line. */
6938 line_height = it->max_ascent + it->max_descent;
6939 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
6940
6941 if (to_y >= it->current_y
6942 && to_y < it->current_y + line_height)
6943 {
6944 if (skip == MOVE_X_REACHED)
6945 /* If TO_Y is in this line and TO_X was reached above,
6946 we scanned too far. We have to restore IT's settings
6947 to the ones before skipping. */
6948 *it = it_backup;
6949 reached = 6;
6950 }
6951 else if (skip == MOVE_X_REACHED)
6952 {
6953 skip = skip2;
6954 if (skip == MOVE_POS_MATCH_OR_ZV)
6955 reached = 7;
6956 }
6957
6958 if (reached)
6959 break;
6960 }
6961 else if (BUFFERP (it->object)
6962 && it->method == GET_FROM_BUFFER
6963 && IT_CHARPOS (*it) >= to_charpos)
6964 skip = MOVE_POS_MATCH_OR_ZV;
6965 else
6966 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
6967
6968 switch (skip)
6969 {
6970 case MOVE_POS_MATCH_OR_ZV:
6971 reached = 8;
6972 goto out;
6973
6974 case MOVE_NEWLINE_OR_CR:
6975 set_iterator_to_next (it, 1);
6976 it->continuation_lines_width = 0;
6977 break;
6978
6979 case MOVE_LINE_TRUNCATED:
6980 it->continuation_lines_width = 0;
6981 reseat_at_next_visible_line_start (it, 0);
6982 if ((op & MOVE_TO_POS) != 0
6983 && IT_CHARPOS (*it) > to_charpos)
6984 {
6985 reached = 9;
6986 goto out;
6987 }
6988 break;
6989
6990 case MOVE_LINE_CONTINUED:
6991 /* For continued lines ending in a tab, some of the glyphs
6992 associated with the tab are displayed on the current
6993 line. Since it->current_x does not include these glyphs,
6994 we use it->last_visible_x instead. */
6995 it->continuation_lines_width +=
6996 (it->c == '\t') ? it->last_visible_x : it->current_x;
6997 break;
6998
6999 default:
7000 abort ();
7001 }
7002
7003 /* Reset/increment for the next run. */
7004 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
7005 it->current_x = it->hpos = 0;
7006 it->current_y += it->max_ascent + it->max_descent;
7007 ++it->vpos;
7008 last_height = it->max_ascent + it->max_descent;
7009 last_max_ascent = it->max_ascent;
7010 it->max_ascent = it->max_descent = 0;
7011 }
7012
7013 out:
7014
7015 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
7016 }
7017
7018
7019 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
7020
7021 If DY > 0, move IT backward at least that many pixels. DY = 0
7022 means move IT backward to the preceding line start or BEGV. This
7023 function may move over more than DY pixels if IT->current_y - DY
7024 ends up in the middle of a line; in this case IT->current_y will be
7025 set to the top of the line moved to. */
7026
7027 void
7028 move_it_vertically_backward (it, dy)
7029 struct it *it;
7030 int dy;
7031 {
7032 int nlines, h;
7033 struct it it2, it3;
7034 int start_pos;
7035
7036 move_further_back:
7037 xassert (dy >= 0);
7038
7039 start_pos = IT_CHARPOS (*it);
7040
7041 /* Estimate how many newlines we must move back. */
7042 nlines = max (1, dy / FRAME_LINE_HEIGHT (it->f));
7043
7044 /* Set the iterator's position that many lines back. */
7045 while (nlines-- && IT_CHARPOS (*it) > BEGV)
7046 back_to_previous_visible_line_start (it);
7047
7048 /* Reseat the iterator here. When moving backward, we don't want
7049 reseat to skip forward over invisible text, set up the iterator
7050 to deliver from overlay strings at the new position etc. So,
7051 use reseat_1 here. */
7052 reseat_1 (it, it->current.pos, 1);
7053
7054 /* We are now surely at a line start. */
7055 it->current_x = it->hpos = 0;
7056 it->continuation_lines_width = 0;
7057
7058 /* Move forward and see what y-distance we moved. First move to the
7059 start of the next line so that we get its height. We need this
7060 height to be able to tell whether we reached the specified
7061 y-distance. */
7062 it2 = *it;
7063 it2.max_ascent = it2.max_descent = 0;
7064 do
7065 {
7066 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
7067 MOVE_TO_POS | MOVE_TO_VPOS);
7068 }
7069 while (!IT_POS_VALID_AFTER_MOVE_P (&it2));
7070 xassert (IT_CHARPOS (*it) >= BEGV);
7071 it3 = it2;
7072
7073 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
7074 xassert (IT_CHARPOS (*it) >= BEGV);
7075 /* H is the actual vertical distance from the position in *IT
7076 and the starting position. */
7077 h = it2.current_y - it->current_y;
7078 /* NLINES is the distance in number of lines. */
7079 nlines = it2.vpos - it->vpos;
7080
7081 /* Correct IT's y and vpos position
7082 so that they are relative to the starting point. */
7083 it->vpos -= nlines;
7084 it->current_y -= h;
7085
7086 if (dy == 0)
7087 {
7088 /* DY == 0 means move to the start of the screen line. The
7089 value of nlines is > 0 if continuation lines were involved. */
7090 if (nlines > 0)
7091 move_it_by_lines (it, nlines, 1);
7092 #if 0
7093 /* I think this assert is bogus if buffer contains
7094 invisible text or images. KFS. */
7095 xassert (IT_CHARPOS (*it) <= start_pos);
7096 #endif
7097 }
7098 else
7099 {
7100 /* The y-position we try to reach, relative to *IT.
7101 Note that H has been subtracted in front of the if-statement. */
7102 int target_y = it->current_y + h - dy;
7103 int y0 = it3.current_y;
7104 int y1 = line_bottom_y (&it3);
7105 int line_height = y1 - y0;
7106
7107 /* If we did not reach target_y, try to move further backward if
7108 we can. If we moved too far backward, try to move forward. */
7109 if (target_y < it->current_y
7110 /* This is heuristic. In a window that's 3 lines high, with
7111 a line height of 13 pixels each, recentering with point
7112 on the bottom line will try to move -39/2 = 19 pixels
7113 backward. Try to avoid moving into the first line. */
7114 && (it->current_y - target_y
7115 > min (window_box_height (it->w), line_height * 2 / 3))
7116 && IT_CHARPOS (*it) > BEGV)
7117 {
7118 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
7119 target_y - it->current_y));
7120 dy = it->current_y - target_y;
7121 goto move_further_back;
7122 }
7123 else if (target_y >= it->current_y + line_height
7124 && IT_CHARPOS (*it) < ZV)
7125 {
7126 /* Should move forward by at least one line, maybe more.
7127
7128 Note: Calling move_it_by_lines can be expensive on
7129 terminal frames, where compute_motion is used (via
7130 vmotion) to do the job, when there are very long lines
7131 and truncate-lines is nil. That's the reason for
7132 treating terminal frames specially here. */
7133
7134 if (!FRAME_WINDOW_P (it->f))
7135 move_it_vertically (it, target_y - (it->current_y + line_height));
7136 else
7137 {
7138 do
7139 {
7140 move_it_by_lines (it, 1, 1);
7141 }
7142 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
7143 }
7144
7145 #if 0
7146 /* I think this assert is bogus if buffer contains
7147 invisible text or images. KFS. */
7148 xassert (IT_CHARPOS (*it) >= BEGV);
7149 #endif
7150 }
7151 }
7152 }
7153
7154
7155 /* Move IT by a specified amount of pixel lines DY. DY negative means
7156 move backwards. DY = 0 means move to start of screen line. At the
7157 end, IT will be on the start of a screen line. */
7158
7159 void
7160 move_it_vertically (it, dy)
7161 struct it *it;
7162 int dy;
7163 {
7164 if (dy <= 0)
7165 move_it_vertically_backward (it, -dy);
7166 else
7167 {
7168 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
7169 move_it_to (it, ZV, -1, it->current_y + dy, -1,
7170 MOVE_TO_POS | MOVE_TO_Y);
7171 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
7172
7173 /* If buffer ends in ZV without a newline, move to the start of
7174 the line to satisfy the post-condition. */
7175 if (IT_CHARPOS (*it) == ZV
7176 && ZV > BEGV
7177 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
7178 move_it_by_lines (it, 0, 0);
7179 }
7180 }
7181
7182
7183 /* Move iterator IT past the end of the text line it is in. */
7184
7185 void
7186 move_it_past_eol (it)
7187 struct it *it;
7188 {
7189 enum move_it_result rc;
7190
7191 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
7192 if (rc == MOVE_NEWLINE_OR_CR)
7193 set_iterator_to_next (it, 0);
7194 }
7195
7196
7197 #if 0 /* Currently not used. */
7198
7199 /* Return non-zero if some text between buffer positions START_CHARPOS
7200 and END_CHARPOS is invisible. IT->window is the window for text
7201 property lookup. */
7202
7203 static int
7204 invisible_text_between_p (it, start_charpos, end_charpos)
7205 struct it *it;
7206 int start_charpos, end_charpos;
7207 {
7208 Lisp_Object prop, limit;
7209 int invisible_found_p;
7210
7211 xassert (it != NULL && start_charpos <= end_charpos);
7212
7213 /* Is text at START invisible? */
7214 prop = Fget_char_property (make_number (start_charpos), Qinvisible,
7215 it->window);
7216 if (TEXT_PROP_MEANS_INVISIBLE (prop))
7217 invisible_found_p = 1;
7218 else
7219 {
7220 limit = Fnext_single_char_property_change (make_number (start_charpos),
7221 Qinvisible, Qnil,
7222 make_number (end_charpos));
7223 invisible_found_p = XFASTINT (limit) < end_charpos;
7224 }
7225
7226 return invisible_found_p;
7227 }
7228
7229 #endif /* 0 */
7230
7231
7232 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
7233 negative means move up. DVPOS == 0 means move to the start of the
7234 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
7235 NEED_Y_P is zero, IT->current_y will be left unchanged.
7236
7237 Further optimization ideas: If we would know that IT->f doesn't use
7238 a face with proportional font, we could be faster for
7239 truncate-lines nil. */
7240
7241 void
7242 move_it_by_lines (it, dvpos, need_y_p)
7243 struct it *it;
7244 int dvpos, need_y_p;
7245 {
7246 struct position pos;
7247
7248 /* The commented-out optimization uses vmotion on terminals. This
7249 gives bad results, because elements like it->what, on which
7250 callers such as pos_visible_p rely, aren't updated. */
7251 /* if (!FRAME_WINDOW_P (it->f))
7252 {
7253 struct text_pos textpos;
7254
7255 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
7256 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
7257 reseat (it, textpos, 1);
7258 it->vpos += pos.vpos;
7259 it->current_y += pos.vpos;
7260 }
7261 else */
7262
7263 if (dvpos == 0)
7264 {
7265 /* DVPOS == 0 means move to the start of the screen line. */
7266 move_it_vertically_backward (it, 0);
7267 xassert (it->current_x == 0 && it->hpos == 0);
7268 /* Let next call to line_bottom_y calculate real line height */
7269 last_height = 0;
7270 }
7271 else if (dvpos > 0)
7272 {
7273 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
7274 if (!IT_POS_VALID_AFTER_MOVE_P (it))
7275 move_it_to (it, IT_CHARPOS (*it) + 1, -1, -1, -1, MOVE_TO_POS);
7276 }
7277 else
7278 {
7279 struct it it2;
7280 int start_charpos, i;
7281
7282 /* Start at the beginning of the screen line containing IT's
7283 position. This may actually move vertically backwards,
7284 in case of overlays, so adjust dvpos accordingly. */
7285 dvpos += it->vpos;
7286 move_it_vertically_backward (it, 0);
7287 dvpos -= it->vpos;
7288
7289 /* Go back -DVPOS visible lines and reseat the iterator there. */
7290 start_charpos = IT_CHARPOS (*it);
7291 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > BEGV; --i)
7292 back_to_previous_visible_line_start (it);
7293 reseat (it, it->current.pos, 1);
7294
7295 /* Move further back if we end up in a string or an image. */
7296 while (!IT_POS_VALID_AFTER_MOVE_P (it))
7297 {
7298 /* First try to move to start of display line. */
7299 dvpos += it->vpos;
7300 move_it_vertically_backward (it, 0);
7301 dvpos -= it->vpos;
7302 if (IT_POS_VALID_AFTER_MOVE_P (it))
7303 break;
7304 /* If start of line is still in string or image,
7305 move further back. */
7306 back_to_previous_visible_line_start (it);
7307 reseat (it, it->current.pos, 1);
7308 dvpos--;
7309 }
7310
7311 it->current_x = it->hpos = 0;
7312
7313 /* Above call may have moved too far if continuation lines
7314 are involved. Scan forward and see if it did. */
7315 it2 = *it;
7316 it2.vpos = it2.current_y = 0;
7317 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
7318 it->vpos -= it2.vpos;
7319 it->current_y -= it2.current_y;
7320 it->current_x = it->hpos = 0;
7321
7322 /* If we moved too far back, move IT some lines forward. */
7323 if (it2.vpos > -dvpos)
7324 {
7325 int delta = it2.vpos + dvpos;
7326 it2 = *it;
7327 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
7328 /* Move back again if we got too far ahead. */
7329 if (IT_CHARPOS (*it) >= start_charpos)
7330 *it = it2;
7331 }
7332 }
7333 }
7334
7335 /* Return 1 if IT points into the middle of a display vector. */
7336
7337 int
7338 in_display_vector_p (it)
7339 struct it *it;
7340 {
7341 return (it->method == GET_FROM_DISPLAY_VECTOR
7342 && it->current.dpvec_index > 0
7343 && it->dpvec + it->current.dpvec_index != it->dpend);
7344 }
7345
7346 \f
7347 /***********************************************************************
7348 Messages
7349 ***********************************************************************/
7350
7351
7352 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
7353 to *Messages*. */
7354
7355 void
7356 add_to_log (format, arg1, arg2)
7357 char *format;
7358 Lisp_Object arg1, arg2;
7359 {
7360 Lisp_Object args[3];
7361 Lisp_Object msg, fmt;
7362 char *buffer;
7363 int len;
7364 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
7365 USE_SAFE_ALLOCA;
7366
7367 /* Do nothing if called asynchronously. Inserting text into
7368 a buffer may call after-change-functions and alike and
7369 that would means running Lisp asynchronously. */
7370 if (handling_signal)
7371 return;
7372
7373 fmt = msg = Qnil;
7374 GCPRO4 (fmt, msg, arg1, arg2);
7375
7376 args[0] = fmt = build_string (format);
7377 args[1] = arg1;
7378 args[2] = arg2;
7379 msg = Fformat (3, args);
7380
7381 len = SBYTES (msg) + 1;
7382 SAFE_ALLOCA (buffer, char *, len);
7383 bcopy (SDATA (msg), buffer, len);
7384
7385 message_dolog (buffer, len - 1, 1, 0);
7386 SAFE_FREE ();
7387
7388 UNGCPRO;
7389 }
7390
7391
7392 /* Output a newline in the *Messages* buffer if "needs" one. */
7393
7394 void
7395 message_log_maybe_newline ()
7396 {
7397 if (message_log_need_newline)
7398 message_dolog ("", 0, 1, 0);
7399 }
7400
7401
7402 /* Add a string M of length NBYTES to the message log, optionally
7403 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
7404 nonzero, means interpret the contents of M as multibyte. This
7405 function calls low-level routines in order to bypass text property
7406 hooks, etc. which might not be safe to run.
7407
7408 This may GC (insert may run before/after change hooks),
7409 so the buffer M must NOT point to a Lisp string. */
7410
7411 void
7412 message_dolog (m, nbytes, nlflag, multibyte)
7413 const char *m;
7414 int nbytes, nlflag, multibyte;
7415 {
7416 if (!NILP (Vmemory_full))
7417 return;
7418
7419 if (!NILP (Vmessage_log_max))
7420 {
7421 struct buffer *oldbuf;
7422 Lisp_Object oldpoint, oldbegv, oldzv;
7423 int old_windows_or_buffers_changed = windows_or_buffers_changed;
7424 int point_at_end = 0;
7425 int zv_at_end = 0;
7426 Lisp_Object old_deactivate_mark, tem;
7427 struct gcpro gcpro1;
7428
7429 old_deactivate_mark = Vdeactivate_mark;
7430 oldbuf = current_buffer;
7431 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
7432 current_buffer->undo_list = Qt;
7433
7434 oldpoint = message_dolog_marker1;
7435 set_marker_restricted (oldpoint, make_number (PT), Qnil);
7436 oldbegv = message_dolog_marker2;
7437 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
7438 oldzv = message_dolog_marker3;
7439 set_marker_restricted (oldzv, make_number (ZV), Qnil);
7440 GCPRO1 (old_deactivate_mark);
7441
7442 if (PT == Z)
7443 point_at_end = 1;
7444 if (ZV == Z)
7445 zv_at_end = 1;
7446
7447 BEGV = BEG;
7448 BEGV_BYTE = BEG_BYTE;
7449 ZV = Z;
7450 ZV_BYTE = Z_BYTE;
7451 TEMP_SET_PT_BOTH (Z, Z_BYTE);
7452
7453 /* Insert the string--maybe converting multibyte to single byte
7454 or vice versa, so that all the text fits the buffer. */
7455 if (multibyte
7456 && NILP (current_buffer->enable_multibyte_characters))
7457 {
7458 int i, c, char_bytes;
7459 unsigned char work[1];
7460
7461 /* Convert a multibyte string to single-byte
7462 for the *Message* buffer. */
7463 for (i = 0; i < nbytes; i += char_bytes)
7464 {
7465 c = string_char_and_length (m + i, nbytes - i, &char_bytes);
7466 work[0] = (ASCII_CHAR_P (c)
7467 ? c
7468 : multibyte_char_to_unibyte (c, Qnil));
7469 insert_1_both (work, 1, 1, 1, 0, 0);
7470 }
7471 }
7472 else if (! multibyte
7473 && ! NILP (current_buffer->enable_multibyte_characters))
7474 {
7475 int i, c, char_bytes;
7476 unsigned char *msg = (unsigned char *) m;
7477 unsigned char str[MAX_MULTIBYTE_LENGTH];
7478 /* Convert a single-byte string to multibyte
7479 for the *Message* buffer. */
7480 for (i = 0; i < nbytes; i++)
7481 {
7482 c = msg[i];
7483 c = unibyte_char_to_multibyte (c);
7484 char_bytes = CHAR_STRING (c, str);
7485 insert_1_both (str, 1, char_bytes, 1, 0, 0);
7486 }
7487 }
7488 else if (nbytes)
7489 insert_1 (m, nbytes, 1, 0, 0);
7490
7491 if (nlflag)
7492 {
7493 int this_bol, this_bol_byte, prev_bol, prev_bol_byte, dup;
7494 insert_1 ("\n", 1, 1, 0, 0);
7495
7496 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
7497 this_bol = PT;
7498 this_bol_byte = PT_BYTE;
7499
7500 /* See if this line duplicates the previous one.
7501 If so, combine duplicates. */
7502 if (this_bol > BEG)
7503 {
7504 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
7505 prev_bol = PT;
7506 prev_bol_byte = PT_BYTE;
7507
7508 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
7509 this_bol, this_bol_byte);
7510 if (dup)
7511 {
7512 del_range_both (prev_bol, prev_bol_byte,
7513 this_bol, this_bol_byte, 0);
7514 if (dup > 1)
7515 {
7516 char dupstr[40];
7517 int duplen;
7518
7519 /* If you change this format, don't forget to also
7520 change message_log_check_duplicate. */
7521 sprintf (dupstr, " [%d times]", dup);
7522 duplen = strlen (dupstr);
7523 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
7524 insert_1 (dupstr, duplen, 1, 0, 1);
7525 }
7526 }
7527 }
7528
7529 /* If we have more than the desired maximum number of lines
7530 in the *Messages* buffer now, delete the oldest ones.
7531 This is safe because we don't have undo in this buffer. */
7532
7533 if (NATNUMP (Vmessage_log_max))
7534 {
7535 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
7536 -XFASTINT (Vmessage_log_max) - 1, 0);
7537 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
7538 }
7539 }
7540 BEGV = XMARKER (oldbegv)->charpos;
7541 BEGV_BYTE = marker_byte_position (oldbegv);
7542
7543 if (zv_at_end)
7544 {
7545 ZV = Z;
7546 ZV_BYTE = Z_BYTE;
7547 }
7548 else
7549 {
7550 ZV = XMARKER (oldzv)->charpos;
7551 ZV_BYTE = marker_byte_position (oldzv);
7552 }
7553
7554 if (point_at_end)
7555 TEMP_SET_PT_BOTH (Z, Z_BYTE);
7556 else
7557 /* We can't do Fgoto_char (oldpoint) because it will run some
7558 Lisp code. */
7559 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
7560 XMARKER (oldpoint)->bytepos);
7561
7562 UNGCPRO;
7563 unchain_marker (XMARKER (oldpoint));
7564 unchain_marker (XMARKER (oldbegv));
7565 unchain_marker (XMARKER (oldzv));
7566
7567 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
7568 set_buffer_internal (oldbuf);
7569 if (NILP (tem))
7570 windows_or_buffers_changed = old_windows_or_buffers_changed;
7571 message_log_need_newline = !nlflag;
7572 Vdeactivate_mark = old_deactivate_mark;
7573 }
7574 }
7575
7576
7577 /* We are at the end of the buffer after just having inserted a newline.
7578 (Note: We depend on the fact we won't be crossing the gap.)
7579 Check to see if the most recent message looks a lot like the previous one.
7580 Return 0 if different, 1 if the new one should just replace it, or a
7581 value N > 1 if we should also append " [N times]". */
7582
7583 static int
7584 message_log_check_duplicate (prev_bol, prev_bol_byte, this_bol, this_bol_byte)
7585 int prev_bol, this_bol;
7586 int prev_bol_byte, this_bol_byte;
7587 {
7588 int i;
7589 int len = Z_BYTE - 1 - this_bol_byte;
7590 int seen_dots = 0;
7591 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
7592 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
7593
7594 for (i = 0; i < len; i++)
7595 {
7596 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
7597 seen_dots = 1;
7598 if (p1[i] != p2[i])
7599 return seen_dots;
7600 }
7601 p1 += len;
7602 if (*p1 == '\n')
7603 return 2;
7604 if (*p1++ == ' ' && *p1++ == '[')
7605 {
7606 int n = 0;
7607 while (*p1 >= '0' && *p1 <= '9')
7608 n = n * 10 + *p1++ - '0';
7609 if (strncmp (p1, " times]\n", 8) == 0)
7610 return n+1;
7611 }
7612 return 0;
7613 }
7614 \f
7615
7616 /* Display an echo area message M with a specified length of NBYTES
7617 bytes. The string may include null characters. If M is 0, clear
7618 out any existing message, and let the mini-buffer text show
7619 through.
7620
7621 This may GC, so the buffer M must NOT point to a Lisp string. */
7622
7623 void
7624 message2 (m, nbytes, multibyte)
7625 const char *m;
7626 int nbytes;
7627 int multibyte;
7628 {
7629 /* First flush out any partial line written with print. */
7630 message_log_maybe_newline ();
7631 if (m)
7632 message_dolog (m, nbytes, 1, multibyte);
7633 message2_nolog (m, nbytes, multibyte);
7634 }
7635
7636
7637 /* The non-logging counterpart of message2. */
7638
7639 void
7640 message2_nolog (m, nbytes, multibyte)
7641 const char *m;
7642 int nbytes, multibyte;
7643 {
7644 struct frame *sf = SELECTED_FRAME ();
7645 message_enable_multibyte = multibyte;
7646
7647 if (noninteractive)
7648 {
7649 if (noninteractive_need_newline)
7650 putc ('\n', stderr);
7651 noninteractive_need_newline = 0;
7652 if (m)
7653 fwrite (m, nbytes, 1, stderr);
7654 if (cursor_in_echo_area == 0)
7655 fprintf (stderr, "\n");
7656 fflush (stderr);
7657 }
7658 /* A null message buffer means that the frame hasn't really been
7659 initialized yet. Error messages get reported properly by
7660 cmd_error, so this must be just an informative message; toss it. */
7661 else if (INTERACTIVE
7662 && sf->glyphs_initialized_p
7663 && FRAME_MESSAGE_BUF (sf))
7664 {
7665 Lisp_Object mini_window;
7666 struct frame *f;
7667
7668 /* Get the frame containing the mini-buffer
7669 that the selected frame is using. */
7670 mini_window = FRAME_MINIBUF_WINDOW (sf);
7671 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7672
7673 FRAME_SAMPLE_VISIBILITY (f);
7674 if (FRAME_VISIBLE_P (sf)
7675 && ! FRAME_VISIBLE_P (f))
7676 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
7677
7678 if (m)
7679 {
7680 set_message (m, Qnil, nbytes, multibyte);
7681 if (minibuffer_auto_raise)
7682 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
7683 }
7684 else
7685 clear_message (1, 1);
7686
7687 do_pending_window_change (0);
7688 echo_area_display (1);
7689 do_pending_window_change (0);
7690 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
7691 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
7692 }
7693 }
7694
7695
7696 /* Display an echo area message M with a specified length of NBYTES
7697 bytes. The string may include null characters. If M is not a
7698 string, clear out any existing message, and let the mini-buffer
7699 text show through.
7700
7701 This function cancels echoing. */
7702
7703 void
7704 message3 (m, nbytes, multibyte)
7705 Lisp_Object m;
7706 int nbytes;
7707 int multibyte;
7708 {
7709 struct gcpro gcpro1;
7710
7711 GCPRO1 (m);
7712 clear_message (1,1);
7713 cancel_echoing ();
7714
7715 /* First flush out any partial line written with print. */
7716 message_log_maybe_newline ();
7717 if (STRINGP (m))
7718 {
7719 char *buffer;
7720 USE_SAFE_ALLOCA;
7721
7722 SAFE_ALLOCA (buffer, char *, nbytes);
7723 bcopy (SDATA (m), buffer, nbytes);
7724 message_dolog (buffer, nbytes, 1, multibyte);
7725 SAFE_FREE ();
7726 }
7727 message3_nolog (m, nbytes, multibyte);
7728
7729 UNGCPRO;
7730 }
7731
7732
7733 /* The non-logging version of message3.
7734 This does not cancel echoing, because it is used for echoing.
7735 Perhaps we need to make a separate function for echoing
7736 and make this cancel echoing. */
7737
7738 void
7739 message3_nolog (m, nbytes, multibyte)
7740 Lisp_Object m;
7741 int nbytes, multibyte;
7742 {
7743 struct frame *sf = SELECTED_FRAME ();
7744 message_enable_multibyte = multibyte;
7745
7746 if (noninteractive)
7747 {
7748 if (noninteractive_need_newline)
7749 putc ('\n', stderr);
7750 noninteractive_need_newline = 0;
7751 if (STRINGP (m))
7752 fwrite (SDATA (m), nbytes, 1, stderr);
7753 if (cursor_in_echo_area == 0)
7754 fprintf (stderr, "\n");
7755 fflush (stderr);
7756 }
7757 /* A null message buffer means that the frame hasn't really been
7758 initialized yet. Error messages get reported properly by
7759 cmd_error, so this must be just an informative message; toss it. */
7760 else if (INTERACTIVE
7761 && sf->glyphs_initialized_p
7762 && FRAME_MESSAGE_BUF (sf))
7763 {
7764 Lisp_Object mini_window;
7765 Lisp_Object frame;
7766 struct frame *f;
7767
7768 /* Get the frame containing the mini-buffer
7769 that the selected frame is using. */
7770 mini_window = FRAME_MINIBUF_WINDOW (sf);
7771 frame = XWINDOW (mini_window)->frame;
7772 f = XFRAME (frame);
7773
7774 FRAME_SAMPLE_VISIBILITY (f);
7775 if (FRAME_VISIBLE_P (sf)
7776 && !FRAME_VISIBLE_P (f))
7777 Fmake_frame_visible (frame);
7778
7779 if (STRINGP (m) && SCHARS (m) > 0)
7780 {
7781 set_message (NULL, m, nbytes, multibyte);
7782 if (minibuffer_auto_raise)
7783 Fraise_frame (frame);
7784 /* Assume we are not echoing.
7785 (If we are, echo_now will override this.) */
7786 echo_message_buffer = Qnil;
7787 }
7788 else
7789 clear_message (1, 1);
7790
7791 do_pending_window_change (0);
7792 echo_area_display (1);
7793 do_pending_window_change (0);
7794 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
7795 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
7796 }
7797 }
7798
7799
7800 /* Display a null-terminated echo area message M. If M is 0, clear
7801 out any existing message, and let the mini-buffer text show through.
7802
7803 The buffer M must continue to exist until after the echo area gets
7804 cleared or some other message gets displayed there. Do not pass
7805 text that is stored in a Lisp string. Do not pass text in a buffer
7806 that was alloca'd. */
7807
7808 void
7809 message1 (m)
7810 char *m;
7811 {
7812 message2 (m, (m ? strlen (m) : 0), 0);
7813 }
7814
7815
7816 /* The non-logging counterpart of message1. */
7817
7818 void
7819 message1_nolog (m)
7820 char *m;
7821 {
7822 message2_nolog (m, (m ? strlen (m) : 0), 0);
7823 }
7824
7825 /* Display a message M which contains a single %s
7826 which gets replaced with STRING. */
7827
7828 void
7829 message_with_string (m, string, log)
7830 char *m;
7831 Lisp_Object string;
7832 int log;
7833 {
7834 CHECK_STRING (string);
7835
7836 if (noninteractive)
7837 {
7838 if (m)
7839 {
7840 if (noninteractive_need_newline)
7841 putc ('\n', stderr);
7842 noninteractive_need_newline = 0;
7843 fprintf (stderr, m, SDATA (string));
7844 if (cursor_in_echo_area == 0)
7845 fprintf (stderr, "\n");
7846 fflush (stderr);
7847 }
7848 }
7849 else if (INTERACTIVE)
7850 {
7851 /* The frame whose minibuffer we're going to display the message on.
7852 It may be larger than the selected frame, so we need
7853 to use its buffer, not the selected frame's buffer. */
7854 Lisp_Object mini_window;
7855 struct frame *f, *sf = SELECTED_FRAME ();
7856
7857 /* Get the frame containing the minibuffer
7858 that the selected frame is using. */
7859 mini_window = FRAME_MINIBUF_WINDOW (sf);
7860 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7861
7862 /* A null message buffer means that the frame hasn't really been
7863 initialized yet. Error messages get reported properly by
7864 cmd_error, so this must be just an informative message; toss it. */
7865 if (FRAME_MESSAGE_BUF (f))
7866 {
7867 Lisp_Object args[2], message;
7868 struct gcpro gcpro1, gcpro2;
7869
7870 args[0] = build_string (m);
7871 args[1] = message = string;
7872 GCPRO2 (args[0], message);
7873 gcpro1.nvars = 2;
7874
7875 message = Fformat (2, args);
7876
7877 if (log)
7878 message3 (message, SBYTES (message), STRING_MULTIBYTE (message));
7879 else
7880 message3_nolog (message, SBYTES (message), STRING_MULTIBYTE (message));
7881
7882 UNGCPRO;
7883
7884 /* Print should start at the beginning of the message
7885 buffer next time. */
7886 message_buf_print = 0;
7887 }
7888 }
7889 }
7890
7891
7892 /* Dump an informative message to the minibuf. If M is 0, clear out
7893 any existing message, and let the mini-buffer text show through. */
7894
7895 /* VARARGS 1 */
7896 void
7897 message (m, a1, a2, a3)
7898 char *m;
7899 EMACS_INT a1, a2, a3;
7900 {
7901 if (noninteractive)
7902 {
7903 if (m)
7904 {
7905 if (noninteractive_need_newline)
7906 putc ('\n', stderr);
7907 noninteractive_need_newline = 0;
7908 fprintf (stderr, m, a1, a2, a3);
7909 if (cursor_in_echo_area == 0)
7910 fprintf (stderr, "\n");
7911 fflush (stderr);
7912 }
7913 }
7914 else if (INTERACTIVE)
7915 {
7916 /* The frame whose mini-buffer we're going to display the message
7917 on. It may be larger than the selected frame, so we need to
7918 use its buffer, not the selected frame's buffer. */
7919 Lisp_Object mini_window;
7920 struct frame *f, *sf = SELECTED_FRAME ();
7921
7922 /* Get the frame containing the mini-buffer
7923 that the selected frame is using. */
7924 mini_window = FRAME_MINIBUF_WINDOW (sf);
7925 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7926
7927 /* A null message buffer means that the frame hasn't really been
7928 initialized yet. Error messages get reported properly by
7929 cmd_error, so this must be just an informative message; toss
7930 it. */
7931 if (FRAME_MESSAGE_BUF (f))
7932 {
7933 if (m)
7934 {
7935 int len;
7936 #ifdef NO_ARG_ARRAY
7937 char *a[3];
7938 a[0] = (char *) a1;
7939 a[1] = (char *) a2;
7940 a[2] = (char *) a3;
7941
7942 len = doprnt (FRAME_MESSAGE_BUF (f),
7943 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
7944 #else
7945 len = doprnt (FRAME_MESSAGE_BUF (f),
7946 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3,
7947 (char **) &a1);
7948 #endif /* NO_ARG_ARRAY */
7949
7950 message2 (FRAME_MESSAGE_BUF (f), len, 0);
7951 }
7952 else
7953 message1 (0);
7954
7955 /* Print should start at the beginning of the message
7956 buffer next time. */
7957 message_buf_print = 0;
7958 }
7959 }
7960 }
7961
7962
7963 /* The non-logging version of message. */
7964
7965 void
7966 message_nolog (m, a1, a2, a3)
7967 char *m;
7968 EMACS_INT a1, a2, a3;
7969 {
7970 Lisp_Object old_log_max;
7971 old_log_max = Vmessage_log_max;
7972 Vmessage_log_max = Qnil;
7973 message (m, a1, a2, a3);
7974 Vmessage_log_max = old_log_max;
7975 }
7976
7977
7978 /* Display the current message in the current mini-buffer. This is
7979 only called from error handlers in process.c, and is not time
7980 critical. */
7981
7982 void
7983 update_echo_area ()
7984 {
7985 if (!NILP (echo_area_buffer[0]))
7986 {
7987 Lisp_Object string;
7988 string = Fcurrent_message ();
7989 message3 (string, SBYTES (string),
7990 !NILP (current_buffer->enable_multibyte_characters));
7991 }
7992 }
7993
7994
7995 /* Make sure echo area buffers in `echo_buffers' are live.
7996 If they aren't, make new ones. */
7997
7998 static void
7999 ensure_echo_area_buffers ()
8000 {
8001 int i;
8002
8003 for (i = 0; i < 2; ++i)
8004 if (!BUFFERP (echo_buffer[i])
8005 || NILP (XBUFFER (echo_buffer[i])->name))
8006 {
8007 char name[30];
8008 Lisp_Object old_buffer;
8009 int j;
8010
8011 old_buffer = echo_buffer[i];
8012 sprintf (name, " *Echo Area %d*", i);
8013 echo_buffer[i] = Fget_buffer_create (build_string (name));
8014 XBUFFER (echo_buffer[i])->truncate_lines = Qnil;
8015
8016 for (j = 0; j < 2; ++j)
8017 if (EQ (old_buffer, echo_area_buffer[j]))
8018 echo_area_buffer[j] = echo_buffer[i];
8019 }
8020 }
8021
8022
8023 /* Call FN with args A1..A4 with either the current or last displayed
8024 echo_area_buffer as current buffer.
8025
8026 WHICH zero means use the current message buffer
8027 echo_area_buffer[0]. If that is nil, choose a suitable buffer
8028 from echo_buffer[] and clear it.
8029
8030 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
8031 suitable buffer from echo_buffer[] and clear it.
8032
8033 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
8034 that the current message becomes the last displayed one, make
8035 choose a suitable buffer for echo_area_buffer[0], and clear it.
8036
8037 Value is what FN returns. */
8038
8039 static int
8040 with_echo_area_buffer (w, which, fn, a1, a2, a3, a4)
8041 struct window *w;
8042 int which;
8043 int (*fn) P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
8044 EMACS_INT a1;
8045 Lisp_Object a2;
8046 EMACS_INT a3, a4;
8047 {
8048 Lisp_Object buffer;
8049 int this_one, the_other, clear_buffer_p, rc;
8050 int count = SPECPDL_INDEX ();
8051
8052 /* If buffers aren't live, make new ones. */
8053 ensure_echo_area_buffers ();
8054
8055 clear_buffer_p = 0;
8056
8057 if (which == 0)
8058 this_one = 0, the_other = 1;
8059 else if (which > 0)
8060 this_one = 1, the_other = 0;
8061 else
8062 {
8063 this_one = 0, the_other = 1;
8064 clear_buffer_p = 1;
8065
8066 /* We need a fresh one in case the current echo buffer equals
8067 the one containing the last displayed echo area message. */
8068 if (!NILP (echo_area_buffer[this_one])
8069 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
8070 echo_area_buffer[this_one] = Qnil;
8071 }
8072
8073 /* Choose a suitable buffer from echo_buffer[] is we don't
8074 have one. */
8075 if (NILP (echo_area_buffer[this_one]))
8076 {
8077 echo_area_buffer[this_one]
8078 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
8079 ? echo_buffer[the_other]
8080 : echo_buffer[this_one]);
8081 clear_buffer_p = 1;
8082 }
8083
8084 buffer = echo_area_buffer[this_one];
8085
8086 /* Don't get confused by reusing the buffer used for echoing
8087 for a different purpose. */
8088 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
8089 cancel_echoing ();
8090
8091 record_unwind_protect (unwind_with_echo_area_buffer,
8092 with_echo_area_buffer_unwind_data (w));
8093
8094 /* Make the echo area buffer current. Note that for display
8095 purposes, it is not necessary that the displayed window's buffer
8096 == current_buffer, except for text property lookup. So, let's
8097 only set that buffer temporarily here without doing a full
8098 Fset_window_buffer. We must also change w->pointm, though,
8099 because otherwise an assertions in unshow_buffer fails, and Emacs
8100 aborts. */
8101 set_buffer_internal_1 (XBUFFER (buffer));
8102 if (w)
8103 {
8104 w->buffer = buffer;
8105 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
8106 }
8107
8108 current_buffer->undo_list = Qt;
8109 current_buffer->read_only = Qnil;
8110 specbind (Qinhibit_read_only, Qt);
8111 specbind (Qinhibit_modification_hooks, Qt);
8112
8113 if (clear_buffer_p && Z > BEG)
8114 del_range (BEG, Z);
8115
8116 xassert (BEGV >= BEG);
8117 xassert (ZV <= Z && ZV >= BEGV);
8118
8119 rc = fn (a1, a2, a3, a4);
8120
8121 xassert (BEGV >= BEG);
8122 xassert (ZV <= Z && ZV >= BEGV);
8123
8124 unbind_to (count, Qnil);
8125 return rc;
8126 }
8127
8128
8129 /* Save state that should be preserved around the call to the function
8130 FN called in with_echo_area_buffer. */
8131
8132 static Lisp_Object
8133 with_echo_area_buffer_unwind_data (w)
8134 struct window *w;
8135 {
8136 int i = 0;
8137 Lisp_Object vector;
8138
8139 /* Reduce consing by keeping one vector in
8140 Vwith_echo_area_save_vector. */
8141 vector = Vwith_echo_area_save_vector;
8142 Vwith_echo_area_save_vector = Qnil;
8143
8144 if (NILP (vector))
8145 vector = Fmake_vector (make_number (7), Qnil);
8146
8147 XSETBUFFER (AREF (vector, i), current_buffer); ++i;
8148 AREF (vector, i) = Vdeactivate_mark, ++i;
8149 AREF (vector, i) = make_number (windows_or_buffers_changed), ++i;
8150
8151 if (w)
8152 {
8153 XSETWINDOW (AREF (vector, i), w); ++i;
8154 AREF (vector, i) = w->buffer; ++i;
8155 AREF (vector, i) = make_number (XMARKER (w->pointm)->charpos); ++i;
8156 AREF (vector, i) = make_number (XMARKER (w->pointm)->bytepos); ++i;
8157 }
8158 else
8159 {
8160 int end = i + 4;
8161 for (; i < end; ++i)
8162 AREF (vector, i) = Qnil;
8163 }
8164
8165 xassert (i == ASIZE (vector));
8166 return vector;
8167 }
8168
8169
8170 /* Restore global state from VECTOR which was created by
8171 with_echo_area_buffer_unwind_data. */
8172
8173 static Lisp_Object
8174 unwind_with_echo_area_buffer (vector)
8175 Lisp_Object vector;
8176 {
8177 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
8178 Vdeactivate_mark = AREF (vector, 1);
8179 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
8180
8181 if (WINDOWP (AREF (vector, 3)))
8182 {
8183 struct window *w;
8184 Lisp_Object buffer, charpos, bytepos;
8185
8186 w = XWINDOW (AREF (vector, 3));
8187 buffer = AREF (vector, 4);
8188 charpos = AREF (vector, 5);
8189 bytepos = AREF (vector, 6);
8190
8191 w->buffer = buffer;
8192 set_marker_both (w->pointm, buffer,
8193 XFASTINT (charpos), XFASTINT (bytepos));
8194 }
8195
8196 Vwith_echo_area_save_vector = vector;
8197 return Qnil;
8198 }
8199
8200
8201 /* Set up the echo area for use by print functions. MULTIBYTE_P
8202 non-zero means we will print multibyte. */
8203
8204 void
8205 setup_echo_area_for_printing (multibyte_p)
8206 int multibyte_p;
8207 {
8208 /* If we can't find an echo area any more, exit. */
8209 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
8210 Fkill_emacs (Qnil);
8211
8212 ensure_echo_area_buffers ();
8213
8214 if (!message_buf_print)
8215 {
8216 /* A message has been output since the last time we printed.
8217 Choose a fresh echo area buffer. */
8218 if (EQ (echo_area_buffer[1], echo_buffer[0]))
8219 echo_area_buffer[0] = echo_buffer[1];
8220 else
8221 echo_area_buffer[0] = echo_buffer[0];
8222
8223 /* Switch to that buffer and clear it. */
8224 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
8225 current_buffer->truncate_lines = Qnil;
8226
8227 if (Z > BEG)
8228 {
8229 int count = SPECPDL_INDEX ();
8230 specbind (Qinhibit_read_only, Qt);
8231 /* Note that undo recording is always disabled. */
8232 del_range (BEG, Z);
8233 unbind_to (count, Qnil);
8234 }
8235 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
8236
8237 /* Set up the buffer for the multibyteness we need. */
8238 if (multibyte_p
8239 != !NILP (current_buffer->enable_multibyte_characters))
8240 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
8241
8242 /* Raise the frame containing the echo area. */
8243 if (minibuffer_auto_raise)
8244 {
8245 struct frame *sf = SELECTED_FRAME ();
8246 Lisp_Object mini_window;
8247 mini_window = FRAME_MINIBUF_WINDOW (sf);
8248 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
8249 }
8250
8251 message_log_maybe_newline ();
8252 message_buf_print = 1;
8253 }
8254 else
8255 {
8256 if (NILP (echo_area_buffer[0]))
8257 {
8258 if (EQ (echo_area_buffer[1], echo_buffer[0]))
8259 echo_area_buffer[0] = echo_buffer[1];
8260 else
8261 echo_area_buffer[0] = echo_buffer[0];
8262 }
8263
8264 if (current_buffer != XBUFFER (echo_area_buffer[0]))
8265 {
8266 /* Someone switched buffers between print requests. */
8267 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
8268 current_buffer->truncate_lines = Qnil;
8269 }
8270 }
8271 }
8272
8273
8274 /* Display an echo area message in window W. Value is non-zero if W's
8275 height is changed. If display_last_displayed_message_p is
8276 non-zero, display the message that was last displayed, otherwise
8277 display the current message. */
8278
8279 static int
8280 display_echo_area (w)
8281 struct window *w;
8282 {
8283 int i, no_message_p, window_height_changed_p, count;
8284
8285 /* Temporarily disable garbage collections while displaying the echo
8286 area. This is done because a GC can print a message itself.
8287 That message would modify the echo area buffer's contents while a
8288 redisplay of the buffer is going on, and seriously confuse
8289 redisplay. */
8290 count = inhibit_garbage_collection ();
8291
8292 /* If there is no message, we must call display_echo_area_1
8293 nevertheless because it resizes the window. But we will have to
8294 reset the echo_area_buffer in question to nil at the end because
8295 with_echo_area_buffer will sets it to an empty buffer. */
8296 i = display_last_displayed_message_p ? 1 : 0;
8297 no_message_p = NILP (echo_area_buffer[i]);
8298
8299 window_height_changed_p
8300 = with_echo_area_buffer (w, display_last_displayed_message_p,
8301 display_echo_area_1,
8302 (EMACS_INT) w, Qnil, 0, 0);
8303
8304 if (no_message_p)
8305 echo_area_buffer[i] = Qnil;
8306
8307 unbind_to (count, Qnil);
8308 return window_height_changed_p;
8309 }
8310
8311
8312 /* Helper for display_echo_area. Display the current buffer which
8313 contains the current echo area message in window W, a mini-window,
8314 a pointer to which is passed in A1. A2..A4 are currently not used.
8315 Change the height of W so that all of the message is displayed.
8316 Value is non-zero if height of W was changed. */
8317
8318 static int
8319 display_echo_area_1 (a1, a2, a3, a4)
8320 EMACS_INT a1;
8321 Lisp_Object a2;
8322 EMACS_INT a3, a4;
8323 {
8324 struct window *w = (struct window *) a1;
8325 Lisp_Object window;
8326 struct text_pos start;
8327 int window_height_changed_p = 0;
8328
8329 /* Do this before displaying, so that we have a large enough glyph
8330 matrix for the display. If we can't get enough space for the
8331 whole text, display the last N lines. That works by setting w->start. */
8332 window_height_changed_p = resize_mini_window (w, 0);
8333
8334 /* Use the starting position chosen by resize_mini_window. */
8335 SET_TEXT_POS_FROM_MARKER (start, w->start);
8336
8337 /* Display. */
8338 clear_glyph_matrix (w->desired_matrix);
8339 XSETWINDOW (window, w);
8340 try_window (window, start, 0);
8341
8342 return window_height_changed_p;
8343 }
8344
8345
8346 /* Resize the echo area window to exactly the size needed for the
8347 currently displayed message, if there is one. If a mini-buffer
8348 is active, don't shrink it. */
8349
8350 void
8351 resize_echo_area_exactly ()
8352 {
8353 if (BUFFERP (echo_area_buffer[0])
8354 && WINDOWP (echo_area_window))
8355 {
8356 struct window *w = XWINDOW (echo_area_window);
8357 int resized_p;
8358 Lisp_Object resize_exactly;
8359
8360 if (minibuf_level == 0)
8361 resize_exactly = Qt;
8362 else
8363 resize_exactly = Qnil;
8364
8365 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
8366 (EMACS_INT) w, resize_exactly, 0, 0);
8367 if (resized_p)
8368 {
8369 ++windows_or_buffers_changed;
8370 ++update_mode_lines;
8371 redisplay_internal (0);
8372 }
8373 }
8374 }
8375
8376
8377 /* Callback function for with_echo_area_buffer, when used from
8378 resize_echo_area_exactly. A1 contains a pointer to the window to
8379 resize, EXACTLY non-nil means resize the mini-window exactly to the
8380 size of the text displayed. A3 and A4 are not used. Value is what
8381 resize_mini_window returns. */
8382
8383 static int
8384 resize_mini_window_1 (a1, exactly, a3, a4)
8385 EMACS_INT a1;
8386 Lisp_Object exactly;
8387 EMACS_INT a3, a4;
8388 {
8389 return resize_mini_window ((struct window *) a1, !NILP (exactly));
8390 }
8391
8392
8393 /* Resize mini-window W to fit the size of its contents. EXACT:P
8394 means size the window exactly to the size needed. Otherwise, it's
8395 only enlarged until W's buffer is empty.
8396
8397 Set W->start to the right place to begin display. If the whole
8398 contents fit, start at the beginning. Otherwise, start so as
8399 to make the end of the contents appear. This is particularly
8400 important for y-or-n-p, but seems desirable generally.
8401
8402 Value is non-zero if the window height has been changed. */
8403
8404 int
8405 resize_mini_window (w, exact_p)
8406 struct window *w;
8407 int exact_p;
8408 {
8409 struct frame *f = XFRAME (w->frame);
8410 int window_height_changed_p = 0;
8411
8412 xassert (MINI_WINDOW_P (w));
8413
8414 /* By default, start display at the beginning. */
8415 set_marker_both (w->start, w->buffer,
8416 BUF_BEGV (XBUFFER (w->buffer)),
8417 BUF_BEGV_BYTE (XBUFFER (w->buffer)));
8418
8419 /* Don't resize windows while redisplaying a window; it would
8420 confuse redisplay functions when the size of the window they are
8421 displaying changes from under them. Such a resizing can happen,
8422 for instance, when which-func prints a long message while
8423 we are running fontification-functions. We're running these
8424 functions with safe_call which binds inhibit-redisplay to t. */
8425 if (!NILP (Vinhibit_redisplay))
8426 return 0;
8427
8428 /* Nil means don't try to resize. */
8429 if (NILP (Vresize_mini_windows)
8430 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
8431 return 0;
8432
8433 if (!FRAME_MINIBUF_ONLY_P (f))
8434 {
8435 struct it it;
8436 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
8437 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
8438 int height, max_height;
8439 int unit = FRAME_LINE_HEIGHT (f);
8440 struct text_pos start;
8441 struct buffer *old_current_buffer = NULL;
8442
8443 if (current_buffer != XBUFFER (w->buffer))
8444 {
8445 old_current_buffer = current_buffer;
8446 set_buffer_internal (XBUFFER (w->buffer));
8447 }
8448
8449 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
8450
8451 /* Compute the max. number of lines specified by the user. */
8452 if (FLOATP (Vmax_mini_window_height))
8453 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
8454 else if (INTEGERP (Vmax_mini_window_height))
8455 max_height = XINT (Vmax_mini_window_height);
8456 else
8457 max_height = total_height / 4;
8458
8459 /* Correct that max. height if it's bogus. */
8460 max_height = max (1, max_height);
8461 max_height = min (total_height, max_height);
8462
8463 /* Find out the height of the text in the window. */
8464 if (it.truncate_lines_p)
8465 height = 1;
8466 else
8467 {
8468 last_height = 0;
8469 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
8470 if (it.max_ascent == 0 && it.max_descent == 0)
8471 height = it.current_y + last_height;
8472 else
8473 height = it.current_y + it.max_ascent + it.max_descent;
8474 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
8475 height = (height + unit - 1) / unit;
8476 }
8477
8478 /* Compute a suitable window start. */
8479 if (height > max_height)
8480 {
8481 height = max_height;
8482 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
8483 move_it_vertically_backward (&it, (height - 1) * unit);
8484 start = it.current.pos;
8485 }
8486 else
8487 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
8488 SET_MARKER_FROM_TEXT_POS (w->start, start);
8489
8490 if (EQ (Vresize_mini_windows, Qgrow_only))
8491 {
8492 /* Let it grow only, until we display an empty message, in which
8493 case the window shrinks again. */
8494 if (height > WINDOW_TOTAL_LINES (w))
8495 {
8496 int old_height = WINDOW_TOTAL_LINES (w);
8497 freeze_window_starts (f, 1);
8498 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8499 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8500 }
8501 else if (height < WINDOW_TOTAL_LINES (w)
8502 && (exact_p || BEGV == ZV))
8503 {
8504 int old_height = WINDOW_TOTAL_LINES (w);
8505 freeze_window_starts (f, 0);
8506 shrink_mini_window (w);
8507 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8508 }
8509 }
8510 else
8511 {
8512 /* Always resize to exact size needed. */
8513 if (height > WINDOW_TOTAL_LINES (w))
8514 {
8515 int old_height = WINDOW_TOTAL_LINES (w);
8516 freeze_window_starts (f, 1);
8517 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8518 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8519 }
8520 else if (height < WINDOW_TOTAL_LINES (w))
8521 {
8522 int old_height = WINDOW_TOTAL_LINES (w);
8523 freeze_window_starts (f, 0);
8524 shrink_mini_window (w);
8525
8526 if (height)
8527 {
8528 freeze_window_starts (f, 1);
8529 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8530 }
8531
8532 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8533 }
8534 }
8535
8536 if (old_current_buffer)
8537 set_buffer_internal (old_current_buffer);
8538 }
8539
8540 return window_height_changed_p;
8541 }
8542
8543
8544 /* Value is the current message, a string, or nil if there is no
8545 current message. */
8546
8547 Lisp_Object
8548 current_message ()
8549 {
8550 Lisp_Object msg;
8551
8552 if (NILP (echo_area_buffer[0]))
8553 msg = Qnil;
8554 else
8555 {
8556 with_echo_area_buffer (0, 0, current_message_1,
8557 (EMACS_INT) &msg, Qnil, 0, 0);
8558 if (NILP (msg))
8559 echo_area_buffer[0] = Qnil;
8560 }
8561
8562 return msg;
8563 }
8564
8565
8566 static int
8567 current_message_1 (a1, a2, a3, a4)
8568 EMACS_INT a1;
8569 Lisp_Object a2;
8570 EMACS_INT a3, a4;
8571 {
8572 Lisp_Object *msg = (Lisp_Object *) a1;
8573
8574 if (Z > BEG)
8575 *msg = make_buffer_string (BEG, Z, 1);
8576 else
8577 *msg = Qnil;
8578 return 0;
8579 }
8580
8581
8582 /* Push the current message on Vmessage_stack for later restauration
8583 by restore_message. Value is non-zero if the current message isn't
8584 empty. This is a relatively infrequent operation, so it's not
8585 worth optimizing. */
8586
8587 int
8588 push_message ()
8589 {
8590 Lisp_Object msg;
8591 msg = current_message ();
8592 Vmessage_stack = Fcons (msg, Vmessage_stack);
8593 return STRINGP (msg);
8594 }
8595
8596
8597 /* Restore message display from the top of Vmessage_stack. */
8598
8599 void
8600 restore_message ()
8601 {
8602 Lisp_Object msg;
8603
8604 xassert (CONSP (Vmessage_stack));
8605 msg = XCAR (Vmessage_stack);
8606 if (STRINGP (msg))
8607 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
8608 else
8609 message3_nolog (msg, 0, 0);
8610 }
8611
8612
8613 /* Handler for record_unwind_protect calling pop_message. */
8614
8615 Lisp_Object
8616 pop_message_unwind (dummy)
8617 Lisp_Object dummy;
8618 {
8619 pop_message ();
8620 return Qnil;
8621 }
8622
8623 /* Pop the top-most entry off Vmessage_stack. */
8624
8625 void
8626 pop_message ()
8627 {
8628 xassert (CONSP (Vmessage_stack));
8629 Vmessage_stack = XCDR (Vmessage_stack);
8630 }
8631
8632
8633 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
8634 exits. If the stack is not empty, we have a missing pop_message
8635 somewhere. */
8636
8637 void
8638 check_message_stack ()
8639 {
8640 if (!NILP (Vmessage_stack))
8641 abort ();
8642 }
8643
8644
8645 /* Truncate to NCHARS what will be displayed in the echo area the next
8646 time we display it---but don't redisplay it now. */
8647
8648 void
8649 truncate_echo_area (nchars)
8650 int nchars;
8651 {
8652 if (nchars == 0)
8653 echo_area_buffer[0] = Qnil;
8654 /* A null message buffer means that the frame hasn't really been
8655 initialized yet. Error messages get reported properly by
8656 cmd_error, so this must be just an informative message; toss it. */
8657 else if (!noninteractive
8658 && INTERACTIVE
8659 && !NILP (echo_area_buffer[0]))
8660 {
8661 struct frame *sf = SELECTED_FRAME ();
8662 if (FRAME_MESSAGE_BUF (sf))
8663 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
8664 }
8665 }
8666
8667
8668 /* Helper function for truncate_echo_area. Truncate the current
8669 message to at most NCHARS characters. */
8670
8671 static int
8672 truncate_message_1 (nchars, a2, a3, a4)
8673 EMACS_INT nchars;
8674 Lisp_Object a2;
8675 EMACS_INT a3, a4;
8676 {
8677 if (BEG + nchars < Z)
8678 del_range (BEG + nchars, Z);
8679 if (Z == BEG)
8680 echo_area_buffer[0] = Qnil;
8681 return 0;
8682 }
8683
8684
8685 /* Set the current message to a substring of S or STRING.
8686
8687 If STRING is a Lisp string, set the message to the first NBYTES
8688 bytes from STRING. NBYTES zero means use the whole string. If
8689 STRING is multibyte, the message will be displayed multibyte.
8690
8691 If S is not null, set the message to the first LEN bytes of S. LEN
8692 zero means use the whole string. MULTIBYTE_P non-zero means S is
8693 multibyte. Display the message multibyte in that case.
8694
8695 Doesn't GC, as with_echo_area_buffer binds Qinhibit_modification_hooks
8696 to t before calling set_message_1 (which calls insert).
8697 */
8698
8699 void
8700 set_message (s, string, nbytes, multibyte_p)
8701 const char *s;
8702 Lisp_Object string;
8703 int nbytes, multibyte_p;
8704 {
8705 message_enable_multibyte
8706 = ((s && multibyte_p)
8707 || (STRINGP (string) && STRING_MULTIBYTE (string)));
8708
8709 with_echo_area_buffer (0, -1, set_message_1,
8710 (EMACS_INT) s, string, nbytes, multibyte_p);
8711 message_buf_print = 0;
8712 help_echo_showing_p = 0;
8713 }
8714
8715
8716 /* Helper function for set_message. Arguments have the same meaning
8717 as there, with A1 corresponding to S and A2 corresponding to STRING
8718 This function is called with the echo area buffer being
8719 current. */
8720
8721 static int
8722 set_message_1 (a1, a2, nbytes, multibyte_p)
8723 EMACS_INT a1;
8724 Lisp_Object a2;
8725 EMACS_INT nbytes, multibyte_p;
8726 {
8727 const char *s = (const char *) a1;
8728 Lisp_Object string = a2;
8729
8730 /* Change multibyteness of the echo buffer appropriately. */
8731 if (message_enable_multibyte
8732 != !NILP (current_buffer->enable_multibyte_characters))
8733 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
8734
8735 current_buffer->truncate_lines = message_truncate_lines ? Qt : Qnil;
8736
8737 /* Insert new message at BEG. */
8738 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
8739
8740 if (STRINGP (string))
8741 {
8742 int nchars;
8743
8744 if (nbytes == 0)
8745 nbytes = SBYTES (string);
8746 nchars = string_byte_to_char (string, nbytes);
8747
8748 /* This function takes care of single/multibyte conversion. We
8749 just have to ensure that the echo area buffer has the right
8750 setting of enable_multibyte_characters. */
8751 insert_from_string (string, 0, 0, nchars, nbytes, 1);
8752 }
8753 else if (s)
8754 {
8755 if (nbytes == 0)
8756 nbytes = strlen (s);
8757
8758 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
8759 {
8760 /* Convert from multi-byte to single-byte. */
8761 int i, c, n;
8762 unsigned char work[1];
8763
8764 /* Convert a multibyte string to single-byte. */
8765 for (i = 0; i < nbytes; i += n)
8766 {
8767 c = string_char_and_length (s + i, nbytes - i, &n);
8768 work[0] = (ASCII_CHAR_P (c)
8769 ? c
8770 : multibyte_char_to_unibyte (c, Qnil));
8771 insert_1_both (work, 1, 1, 1, 0, 0);
8772 }
8773 }
8774 else if (!multibyte_p
8775 && !NILP (current_buffer->enable_multibyte_characters))
8776 {
8777 /* Convert from single-byte to multi-byte. */
8778 int i, c, n;
8779 const unsigned char *msg = (const unsigned char *) s;
8780 unsigned char str[MAX_MULTIBYTE_LENGTH];
8781
8782 /* Convert a single-byte string to multibyte. */
8783 for (i = 0; i < nbytes; i++)
8784 {
8785 c = msg[i];
8786 c = unibyte_char_to_multibyte (c);
8787 n = CHAR_STRING (c, str);
8788 insert_1_both (str, 1, n, 1, 0, 0);
8789 }
8790 }
8791 else
8792 insert_1 (s, nbytes, 1, 0, 0);
8793 }
8794
8795 return 0;
8796 }
8797
8798
8799 /* Clear messages. CURRENT_P non-zero means clear the current
8800 message. LAST_DISPLAYED_P non-zero means clear the message
8801 last displayed. */
8802
8803 void
8804 clear_message (current_p, last_displayed_p)
8805 int current_p, last_displayed_p;
8806 {
8807 if (current_p)
8808 {
8809 echo_area_buffer[0] = Qnil;
8810 message_cleared_p = 1;
8811 }
8812
8813 if (last_displayed_p)
8814 echo_area_buffer[1] = Qnil;
8815
8816 message_buf_print = 0;
8817 }
8818
8819 /* Clear garbaged frames.
8820
8821 This function is used where the old redisplay called
8822 redraw_garbaged_frames which in turn called redraw_frame which in
8823 turn called clear_frame. The call to clear_frame was a source of
8824 flickering. I believe a clear_frame is not necessary. It should
8825 suffice in the new redisplay to invalidate all current matrices,
8826 and ensure a complete redisplay of all windows. */
8827
8828 static void
8829 clear_garbaged_frames ()
8830 {
8831 if (frame_garbaged)
8832 {
8833 Lisp_Object tail, frame;
8834 int changed_count = 0;
8835
8836 FOR_EACH_FRAME (tail, frame)
8837 {
8838 struct frame *f = XFRAME (frame);
8839
8840 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
8841 {
8842 if (f->resized_p)
8843 {
8844 Fredraw_frame (frame);
8845 f->force_flush_display_p = 1;
8846 }
8847 clear_current_matrices (f);
8848 changed_count++;
8849 f->garbaged = 0;
8850 f->resized_p = 0;
8851 }
8852 }
8853
8854 frame_garbaged = 0;
8855 if (changed_count)
8856 ++windows_or_buffers_changed;
8857 }
8858 }
8859
8860
8861 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
8862 is non-zero update selected_frame. Value is non-zero if the
8863 mini-windows height has been changed. */
8864
8865 static int
8866 echo_area_display (update_frame_p)
8867 int update_frame_p;
8868 {
8869 Lisp_Object mini_window;
8870 struct window *w;
8871 struct frame *f;
8872 int window_height_changed_p = 0;
8873 struct frame *sf = SELECTED_FRAME ();
8874
8875 mini_window = FRAME_MINIBUF_WINDOW (sf);
8876 w = XWINDOW (mini_window);
8877 f = XFRAME (WINDOW_FRAME (w));
8878
8879 /* Don't display if frame is invisible or not yet initialized. */
8880 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
8881 return 0;
8882
8883 /* The terminal frame is used as the first Emacs frame on the Mac OS. */
8884 #ifndef MAC_OS8
8885 #ifdef HAVE_WINDOW_SYSTEM
8886 /* When Emacs starts, selected_frame may be the initial terminal
8887 frame. If we let this through, a message would be displayed on
8888 the terminal. */
8889 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
8890 return 0;
8891 #endif /* HAVE_WINDOW_SYSTEM */
8892 #endif
8893
8894 /* Redraw garbaged frames. */
8895 if (frame_garbaged)
8896 clear_garbaged_frames ();
8897
8898 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
8899 {
8900 echo_area_window = mini_window;
8901 window_height_changed_p = display_echo_area (w);
8902 w->must_be_updated_p = 1;
8903
8904 /* Update the display, unless called from redisplay_internal.
8905 Also don't update the screen during redisplay itself. The
8906 update will happen at the end of redisplay, and an update
8907 here could cause confusion. */
8908 if (update_frame_p && !redisplaying_p)
8909 {
8910 int n = 0;
8911
8912 /* If the display update has been interrupted by pending
8913 input, update mode lines in the frame. Due to the
8914 pending input, it might have been that redisplay hasn't
8915 been called, so that mode lines above the echo area are
8916 garbaged. This looks odd, so we prevent it here. */
8917 if (!display_completed)
8918 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
8919
8920 if (window_height_changed_p
8921 /* Don't do this if Emacs is shutting down. Redisplay
8922 needs to run hooks. */
8923 && !NILP (Vrun_hooks))
8924 {
8925 /* Must update other windows. Likewise as in other
8926 cases, don't let this update be interrupted by
8927 pending input. */
8928 int count = SPECPDL_INDEX ();
8929 specbind (Qredisplay_dont_pause, Qt);
8930 windows_or_buffers_changed = 1;
8931 redisplay_internal (0);
8932 unbind_to (count, Qnil);
8933 }
8934 else if (FRAME_WINDOW_P (f) && n == 0)
8935 {
8936 /* Window configuration is the same as before.
8937 Can do with a display update of the echo area,
8938 unless we displayed some mode lines. */
8939 update_single_window (w, 1);
8940 FRAME_RIF (f)->flush_display (f);
8941 }
8942 else
8943 update_frame (f, 1, 1);
8944
8945 /* If cursor is in the echo area, make sure that the next
8946 redisplay displays the minibuffer, so that the cursor will
8947 be replaced with what the minibuffer wants. */
8948 if (cursor_in_echo_area)
8949 ++windows_or_buffers_changed;
8950 }
8951 }
8952 else if (!EQ (mini_window, selected_window))
8953 windows_or_buffers_changed++;
8954
8955 /* Last displayed message is now the current message. */
8956 echo_area_buffer[1] = echo_area_buffer[0];
8957 /* Inform read_char that we're not echoing. */
8958 echo_message_buffer = Qnil;
8959
8960 /* Prevent redisplay optimization in redisplay_internal by resetting
8961 this_line_start_pos. This is done because the mini-buffer now
8962 displays the message instead of its buffer text. */
8963 if (EQ (mini_window, selected_window))
8964 CHARPOS (this_line_start_pos) = 0;
8965
8966 return window_height_changed_p;
8967 }
8968
8969
8970 \f
8971 /***********************************************************************
8972 Mode Lines and Frame Titles
8973 ***********************************************************************/
8974
8975 /* A buffer for constructing non-propertized mode-line strings and
8976 frame titles in it; allocated from the heap in init_xdisp and
8977 resized as needed in store_mode_line_noprop_char. */
8978
8979 static char *mode_line_noprop_buf;
8980
8981 /* The buffer's end, and a current output position in it. */
8982
8983 static char *mode_line_noprop_buf_end;
8984 static char *mode_line_noprop_ptr;
8985
8986 #define MODE_LINE_NOPROP_LEN(start) \
8987 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
8988
8989 static enum {
8990 MODE_LINE_DISPLAY = 0,
8991 MODE_LINE_TITLE,
8992 MODE_LINE_NOPROP,
8993 MODE_LINE_STRING
8994 } mode_line_target;
8995
8996 /* Alist that caches the results of :propertize.
8997 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
8998 static Lisp_Object mode_line_proptrans_alist;
8999
9000 /* List of strings making up the mode-line. */
9001 static Lisp_Object mode_line_string_list;
9002
9003 /* Base face property when building propertized mode line string. */
9004 static Lisp_Object mode_line_string_face;
9005 static Lisp_Object mode_line_string_face_prop;
9006
9007
9008 /* Unwind data for mode line strings */
9009
9010 static Lisp_Object Vmode_line_unwind_vector;
9011
9012 static Lisp_Object
9013 format_mode_line_unwind_data (obuf, save_proptrans)
9014 struct buffer *obuf;
9015 {
9016 Lisp_Object vector;
9017
9018 /* Reduce consing by keeping one vector in
9019 Vwith_echo_area_save_vector. */
9020 vector = Vmode_line_unwind_vector;
9021 Vmode_line_unwind_vector = Qnil;
9022
9023 if (NILP (vector))
9024 vector = Fmake_vector (make_number (7), Qnil);
9025
9026 AREF (vector, 0) = make_number (mode_line_target);
9027 AREF (vector, 1) = make_number (MODE_LINE_NOPROP_LEN (0));
9028 AREF (vector, 2) = mode_line_string_list;
9029 AREF (vector, 3) = (save_proptrans ? mode_line_proptrans_alist : Qt);
9030 AREF (vector, 4) = mode_line_string_face;
9031 AREF (vector, 5) = mode_line_string_face_prop;
9032
9033 if (obuf)
9034 XSETBUFFER (AREF (vector, 6), obuf);
9035 else
9036 AREF (vector, 6) = Qnil;
9037
9038 return vector;
9039 }
9040
9041 static Lisp_Object
9042 unwind_format_mode_line (vector)
9043 Lisp_Object vector;
9044 {
9045 mode_line_target = XINT (AREF (vector, 0));
9046 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
9047 mode_line_string_list = AREF (vector, 2);
9048 if (! EQ (AREF (vector, 3), Qt))
9049 mode_line_proptrans_alist = AREF (vector, 3);
9050 mode_line_string_face = AREF (vector, 4);
9051 mode_line_string_face_prop = AREF (vector, 5);
9052
9053 if (!NILP (AREF (vector, 6)))
9054 {
9055 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
9056 AREF (vector, 6) = Qnil;
9057 }
9058
9059 Vmode_line_unwind_vector = vector;
9060 return Qnil;
9061 }
9062
9063
9064 /* Store a single character C for the frame title in mode_line_noprop_buf.
9065 Re-allocate mode_line_noprop_buf if necessary. */
9066
9067 static void
9068 #ifdef PROTOTYPES
9069 store_mode_line_noprop_char (char c)
9070 #else
9071 store_mode_line_noprop_char (c)
9072 char c;
9073 #endif
9074 {
9075 /* If output position has reached the end of the allocated buffer,
9076 double the buffer's size. */
9077 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
9078 {
9079 int len = MODE_LINE_NOPROP_LEN (0);
9080 int new_size = 2 * len * sizeof *mode_line_noprop_buf;
9081 mode_line_noprop_buf = (char *) xrealloc (mode_line_noprop_buf, new_size);
9082 mode_line_noprop_buf_end = mode_line_noprop_buf + new_size;
9083 mode_line_noprop_ptr = mode_line_noprop_buf + len;
9084 }
9085
9086 *mode_line_noprop_ptr++ = c;
9087 }
9088
9089
9090 /* Store part of a frame title in mode_line_noprop_buf, beginning at
9091 mode_line_noprop_ptr. STR is the string to store. Do not copy
9092 characters that yield more columns than PRECISION; PRECISION <= 0
9093 means copy the whole string. Pad with spaces until FIELD_WIDTH
9094 number of characters have been copied; FIELD_WIDTH <= 0 means don't
9095 pad. Called from display_mode_element when it is used to build a
9096 frame title. */
9097
9098 static int
9099 store_mode_line_noprop (str, field_width, precision)
9100 const unsigned char *str;
9101 int field_width, precision;
9102 {
9103 int n = 0;
9104 int dummy, nbytes;
9105
9106 /* Copy at most PRECISION chars from STR. */
9107 nbytes = strlen (str);
9108 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
9109 while (nbytes--)
9110 store_mode_line_noprop_char (*str++);
9111
9112 /* Fill up with spaces until FIELD_WIDTH reached. */
9113 while (field_width > 0
9114 && n < field_width)
9115 {
9116 store_mode_line_noprop_char (' ');
9117 ++n;
9118 }
9119
9120 return n;
9121 }
9122
9123 /***********************************************************************
9124 Frame Titles
9125 ***********************************************************************/
9126
9127 #ifdef HAVE_WINDOW_SYSTEM
9128
9129 /* Set the title of FRAME, if it has changed. The title format is
9130 Vicon_title_format if FRAME is iconified, otherwise it is
9131 frame_title_format. */
9132
9133 static void
9134 x_consider_frame_title (frame)
9135 Lisp_Object frame;
9136 {
9137 struct frame *f = XFRAME (frame);
9138
9139 if (FRAME_WINDOW_P (f)
9140 || FRAME_MINIBUF_ONLY_P (f)
9141 || f->explicit_name)
9142 {
9143 /* Do we have more than one visible frame on this X display? */
9144 Lisp_Object tail;
9145 Lisp_Object fmt;
9146 int title_start;
9147 char *title;
9148 int len;
9149 struct it it;
9150 int count = SPECPDL_INDEX ();
9151
9152 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
9153 {
9154 Lisp_Object other_frame = XCAR (tail);
9155 struct frame *tf = XFRAME (other_frame);
9156
9157 if (tf != f
9158 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
9159 && !FRAME_MINIBUF_ONLY_P (tf)
9160 && !EQ (other_frame, tip_frame)
9161 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
9162 break;
9163 }
9164
9165 /* Set global variable indicating that multiple frames exist. */
9166 multiple_frames = CONSP (tail);
9167
9168 /* Switch to the buffer of selected window of the frame. Set up
9169 mode_line_target so that display_mode_element will output into
9170 mode_line_noprop_buf; then display the title. */
9171 record_unwind_protect (unwind_format_mode_line,
9172 format_mode_line_unwind_data (current_buffer, 0));
9173
9174 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
9175 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
9176
9177 mode_line_target = MODE_LINE_TITLE;
9178 title_start = MODE_LINE_NOPROP_LEN (0);
9179 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
9180 NULL, DEFAULT_FACE_ID);
9181 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
9182 len = MODE_LINE_NOPROP_LEN (title_start);
9183 title = mode_line_noprop_buf + title_start;
9184 unbind_to (count, Qnil);
9185
9186 /* Set the title only if it's changed. This avoids consing in
9187 the common case where it hasn't. (If it turns out that we've
9188 already wasted too much time by walking through the list with
9189 display_mode_element, then we might need to optimize at a
9190 higher level than this.) */
9191 if (! STRINGP (f->name)
9192 || SBYTES (f->name) != len
9193 || bcmp (title, SDATA (f->name), len) != 0)
9194 x_implicitly_set_name (f, make_string (title, len), Qnil);
9195 }
9196 }
9197
9198 #endif /* not HAVE_WINDOW_SYSTEM */
9199
9200
9201
9202 \f
9203 /***********************************************************************
9204 Menu Bars
9205 ***********************************************************************/
9206
9207
9208 /* Prepare for redisplay by updating menu-bar item lists when
9209 appropriate. This can call eval. */
9210
9211 void
9212 prepare_menu_bars ()
9213 {
9214 int all_windows;
9215 struct gcpro gcpro1, gcpro2;
9216 struct frame *f;
9217 Lisp_Object tooltip_frame;
9218
9219 #ifdef HAVE_WINDOW_SYSTEM
9220 tooltip_frame = tip_frame;
9221 #else
9222 tooltip_frame = Qnil;
9223 #endif
9224
9225 /* Update all frame titles based on their buffer names, etc. We do
9226 this before the menu bars so that the buffer-menu will show the
9227 up-to-date frame titles. */
9228 #ifdef HAVE_WINDOW_SYSTEM
9229 if (windows_or_buffers_changed || update_mode_lines)
9230 {
9231 Lisp_Object tail, frame;
9232
9233 FOR_EACH_FRAME (tail, frame)
9234 {
9235 f = XFRAME (frame);
9236 if (!EQ (frame, tooltip_frame)
9237 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
9238 x_consider_frame_title (frame);
9239 }
9240 }
9241 #endif /* HAVE_WINDOW_SYSTEM */
9242
9243 /* Update the menu bar item lists, if appropriate. This has to be
9244 done before any actual redisplay or generation of display lines. */
9245 all_windows = (update_mode_lines
9246 || buffer_shared > 1
9247 || windows_or_buffers_changed);
9248 if (all_windows)
9249 {
9250 Lisp_Object tail, frame;
9251 int count = SPECPDL_INDEX ();
9252 /* 1 means that update_menu_bar has run its hooks
9253 so any further calls to update_menu_bar shouldn't do so again. */
9254 int menu_bar_hooks_run = 0;
9255
9256 record_unwind_save_match_data ();
9257
9258 FOR_EACH_FRAME (tail, frame)
9259 {
9260 f = XFRAME (frame);
9261
9262 /* Ignore tooltip frame. */
9263 if (EQ (frame, tooltip_frame))
9264 continue;
9265
9266 /* If a window on this frame changed size, report that to
9267 the user and clear the size-change flag. */
9268 if (FRAME_WINDOW_SIZES_CHANGED (f))
9269 {
9270 Lisp_Object functions;
9271
9272 /* Clear flag first in case we get an error below. */
9273 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
9274 functions = Vwindow_size_change_functions;
9275 GCPRO2 (tail, functions);
9276
9277 while (CONSP (functions))
9278 {
9279 call1 (XCAR (functions), frame);
9280 functions = XCDR (functions);
9281 }
9282 UNGCPRO;
9283 }
9284
9285 GCPRO1 (tail);
9286 menu_bar_hooks_run = update_menu_bar (f, 0, menu_bar_hooks_run);
9287 #ifdef HAVE_WINDOW_SYSTEM
9288 update_tool_bar (f, 0);
9289 #ifdef MAC_OS
9290 mac_update_title_bar (f, 0);
9291 #endif
9292 #endif
9293 UNGCPRO;
9294 }
9295
9296 unbind_to (count, Qnil);
9297 }
9298 else
9299 {
9300 struct frame *sf = SELECTED_FRAME ();
9301 update_menu_bar (sf, 1, 0);
9302 #ifdef HAVE_WINDOW_SYSTEM
9303 update_tool_bar (sf, 1);
9304 #ifdef MAC_OS
9305 mac_update_title_bar (sf, 1);
9306 #endif
9307 #endif
9308 }
9309
9310 /* Motif needs this. See comment in xmenu.c. Turn it off when
9311 pending_menu_activation is not defined. */
9312 #ifdef USE_X_TOOLKIT
9313 pending_menu_activation = 0;
9314 #endif
9315 }
9316
9317
9318 /* Update the menu bar item list for frame F. This has to be done
9319 before we start to fill in any display lines, because it can call
9320 eval.
9321
9322 If SAVE_MATCH_DATA is non-zero, we must save and restore it here.
9323
9324 If HOOKS_RUN is 1, that means a previous call to update_menu_bar
9325 already ran the menu bar hooks for this redisplay, so there
9326 is no need to run them again. The return value is the
9327 updated value of this flag, to pass to the next call. */
9328
9329 static int
9330 update_menu_bar (f, save_match_data, hooks_run)
9331 struct frame *f;
9332 int save_match_data;
9333 int hooks_run;
9334 {
9335 Lisp_Object window;
9336 register struct window *w;
9337
9338 /* If called recursively during a menu update, do nothing. This can
9339 happen when, for instance, an activate-menubar-hook causes a
9340 redisplay. */
9341 if (inhibit_menubar_update)
9342 return hooks_run;
9343
9344 window = FRAME_SELECTED_WINDOW (f);
9345 w = XWINDOW (window);
9346
9347 #if 0 /* The if statement below this if statement used to include the
9348 condition !NILP (w->update_mode_line), rather than using
9349 update_mode_lines directly, and this if statement may have
9350 been added to make that condition work. Now the if
9351 statement below matches its comment, this isn't needed. */
9352 if (update_mode_lines)
9353 w->update_mode_line = Qt;
9354 #endif
9355
9356 if (FRAME_WINDOW_P (f)
9357 ?
9358 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
9359 || defined (USE_GTK)
9360 FRAME_EXTERNAL_MENU_BAR (f)
9361 #else
9362 FRAME_MENU_BAR_LINES (f) > 0
9363 #endif
9364 : FRAME_MENU_BAR_LINES (f) > 0)
9365 {
9366 /* If the user has switched buffers or windows, we need to
9367 recompute to reflect the new bindings. But we'll
9368 recompute when update_mode_lines is set too; that means
9369 that people can use force-mode-line-update to request
9370 that the menu bar be recomputed. The adverse effect on
9371 the rest of the redisplay algorithm is about the same as
9372 windows_or_buffers_changed anyway. */
9373 if (windows_or_buffers_changed
9374 /* This used to test w->update_mode_line, but we believe
9375 there is no need to recompute the menu in that case. */
9376 || update_mode_lines
9377 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
9378 < BUF_MODIFF (XBUFFER (w->buffer)))
9379 != !NILP (w->last_had_star))
9380 || ((!NILP (Vtransient_mark_mode)
9381 && !NILP (XBUFFER (w->buffer)->mark_active))
9382 != !NILP (w->region_showing)))
9383 {
9384 struct buffer *prev = current_buffer;
9385 int count = SPECPDL_INDEX ();
9386
9387 specbind (Qinhibit_menubar_update, Qt);
9388
9389 set_buffer_internal_1 (XBUFFER (w->buffer));
9390 if (save_match_data)
9391 record_unwind_save_match_data ();
9392 if (NILP (Voverriding_local_map_menu_flag))
9393 {
9394 specbind (Qoverriding_terminal_local_map, Qnil);
9395 specbind (Qoverriding_local_map, Qnil);
9396 }
9397
9398 if (!hooks_run)
9399 {
9400 /* Run the Lucid hook. */
9401 safe_run_hooks (Qactivate_menubar_hook);
9402
9403 /* If it has changed current-menubar from previous value,
9404 really recompute the menu-bar from the value. */
9405 if (! NILP (Vlucid_menu_bar_dirty_flag))
9406 call0 (Qrecompute_lucid_menubar);
9407
9408 safe_run_hooks (Qmenu_bar_update_hook);
9409
9410 hooks_run = 1;
9411 }
9412
9413 XSETFRAME (Vmenu_updating_frame, f);
9414 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
9415
9416 /* Redisplay the menu bar in case we changed it. */
9417 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
9418 || defined (USE_GTK)
9419 if (FRAME_WINDOW_P (f))
9420 {
9421 #ifdef MAC_OS
9422 /* All frames on Mac OS share the same menubar. So only
9423 the selected frame should be allowed to set it. */
9424 if (f == SELECTED_FRAME ())
9425 #endif
9426 set_frame_menubar (f, 0, 0);
9427 }
9428 else
9429 /* On a terminal screen, the menu bar is an ordinary screen
9430 line, and this makes it get updated. */
9431 w->update_mode_line = Qt;
9432 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
9433 /* In the non-toolkit version, the menu bar is an ordinary screen
9434 line, and this makes it get updated. */
9435 w->update_mode_line = Qt;
9436 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
9437
9438 unbind_to (count, Qnil);
9439 set_buffer_internal_1 (prev);
9440 }
9441 }
9442
9443 return hooks_run;
9444 }
9445
9446
9447 \f
9448 /***********************************************************************
9449 Output Cursor
9450 ***********************************************************************/
9451
9452 #ifdef HAVE_WINDOW_SYSTEM
9453
9454 /* EXPORT:
9455 Nominal cursor position -- where to draw output.
9456 HPOS and VPOS are window relative glyph matrix coordinates.
9457 X and Y are window relative pixel coordinates. */
9458
9459 struct cursor_pos output_cursor;
9460
9461
9462 /* EXPORT:
9463 Set the global variable output_cursor to CURSOR. All cursor
9464 positions are relative to updated_window. */
9465
9466 void
9467 set_output_cursor (cursor)
9468 struct cursor_pos *cursor;
9469 {
9470 output_cursor.hpos = cursor->hpos;
9471 output_cursor.vpos = cursor->vpos;
9472 output_cursor.x = cursor->x;
9473 output_cursor.y = cursor->y;
9474 }
9475
9476
9477 /* EXPORT for RIF:
9478 Set a nominal cursor position.
9479
9480 HPOS and VPOS are column/row positions in a window glyph matrix. X
9481 and Y are window text area relative pixel positions.
9482
9483 If this is done during an update, updated_window will contain the
9484 window that is being updated and the position is the future output
9485 cursor position for that window. If updated_window is null, use
9486 selected_window and display the cursor at the given position. */
9487
9488 void
9489 x_cursor_to (vpos, hpos, y, x)
9490 int vpos, hpos, y, x;
9491 {
9492 struct window *w;
9493
9494 /* If updated_window is not set, work on selected_window. */
9495 if (updated_window)
9496 w = updated_window;
9497 else
9498 w = XWINDOW (selected_window);
9499
9500 /* Set the output cursor. */
9501 output_cursor.hpos = hpos;
9502 output_cursor.vpos = vpos;
9503 output_cursor.x = x;
9504 output_cursor.y = y;
9505
9506 /* If not called as part of an update, really display the cursor.
9507 This will also set the cursor position of W. */
9508 if (updated_window == NULL)
9509 {
9510 BLOCK_INPUT;
9511 display_and_set_cursor (w, 1, hpos, vpos, x, y);
9512 if (FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
9513 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (SELECTED_FRAME ());
9514 UNBLOCK_INPUT;
9515 }
9516 }
9517
9518 #endif /* HAVE_WINDOW_SYSTEM */
9519
9520 \f
9521 /***********************************************************************
9522 Tool-bars
9523 ***********************************************************************/
9524
9525 #ifdef HAVE_WINDOW_SYSTEM
9526
9527 /* Where the mouse was last time we reported a mouse event. */
9528
9529 FRAME_PTR last_mouse_frame;
9530
9531 /* Tool-bar item index of the item on which a mouse button was pressed
9532 or -1. */
9533
9534 int last_tool_bar_item;
9535
9536
9537 /* Update the tool-bar item list for frame F. This has to be done
9538 before we start to fill in any display lines. Called from
9539 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
9540 and restore it here. */
9541
9542 static void
9543 update_tool_bar (f, save_match_data)
9544 struct frame *f;
9545 int save_match_data;
9546 {
9547 #if defined (USE_GTK) || USE_MAC_TOOLBAR
9548 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
9549 #else
9550 int do_update = WINDOWP (f->tool_bar_window)
9551 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
9552 #endif
9553
9554 if (do_update)
9555 {
9556 Lisp_Object window;
9557 struct window *w;
9558
9559 window = FRAME_SELECTED_WINDOW (f);
9560 w = XWINDOW (window);
9561
9562 /* If the user has switched buffers or windows, we need to
9563 recompute to reflect the new bindings. But we'll
9564 recompute when update_mode_lines is set too; that means
9565 that people can use force-mode-line-update to request
9566 that the menu bar be recomputed. The adverse effect on
9567 the rest of the redisplay algorithm is about the same as
9568 windows_or_buffers_changed anyway. */
9569 if (windows_or_buffers_changed
9570 || !NILP (w->update_mode_line)
9571 || update_mode_lines
9572 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
9573 < BUF_MODIFF (XBUFFER (w->buffer)))
9574 != !NILP (w->last_had_star))
9575 || ((!NILP (Vtransient_mark_mode)
9576 && !NILP (XBUFFER (w->buffer)->mark_active))
9577 != !NILP (w->region_showing)))
9578 {
9579 struct buffer *prev = current_buffer;
9580 int count = SPECPDL_INDEX ();
9581 Lisp_Object new_tool_bar;
9582 int new_n_tool_bar;
9583 struct gcpro gcpro1;
9584
9585 /* Set current_buffer to the buffer of the selected
9586 window of the frame, so that we get the right local
9587 keymaps. */
9588 set_buffer_internal_1 (XBUFFER (w->buffer));
9589
9590 /* Save match data, if we must. */
9591 if (save_match_data)
9592 record_unwind_save_match_data ();
9593
9594 /* Make sure that we don't accidentally use bogus keymaps. */
9595 if (NILP (Voverriding_local_map_menu_flag))
9596 {
9597 specbind (Qoverriding_terminal_local_map, Qnil);
9598 specbind (Qoverriding_local_map, Qnil);
9599 }
9600
9601 GCPRO1 (new_tool_bar);
9602
9603 /* Build desired tool-bar items from keymaps. */
9604 new_tool_bar = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
9605 &new_n_tool_bar);
9606
9607 /* Redisplay the tool-bar if we changed it. */
9608 if (new_n_tool_bar != f->n_tool_bar_items
9609 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
9610 {
9611 /* Redisplay that happens asynchronously due to an expose event
9612 may access f->tool_bar_items. Make sure we update both
9613 variables within BLOCK_INPUT so no such event interrupts. */
9614 BLOCK_INPUT;
9615 f->tool_bar_items = new_tool_bar;
9616 f->n_tool_bar_items = new_n_tool_bar;
9617 w->update_mode_line = Qt;
9618 UNBLOCK_INPUT;
9619 }
9620
9621 UNGCPRO;
9622
9623 unbind_to (count, Qnil);
9624 set_buffer_internal_1 (prev);
9625 }
9626 }
9627 }
9628
9629
9630 /* Set F->desired_tool_bar_string to a Lisp string representing frame
9631 F's desired tool-bar contents. F->tool_bar_items must have
9632 been set up previously by calling prepare_menu_bars. */
9633
9634 static void
9635 build_desired_tool_bar_string (f)
9636 struct frame *f;
9637 {
9638 int i, size, size_needed;
9639 struct gcpro gcpro1, gcpro2, gcpro3;
9640 Lisp_Object image, plist, props;
9641
9642 image = plist = props = Qnil;
9643 GCPRO3 (image, plist, props);
9644
9645 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
9646 Otherwise, make a new string. */
9647
9648 /* The size of the string we might be able to reuse. */
9649 size = (STRINGP (f->desired_tool_bar_string)
9650 ? SCHARS (f->desired_tool_bar_string)
9651 : 0);
9652
9653 /* We need one space in the string for each image. */
9654 size_needed = f->n_tool_bar_items;
9655
9656 /* Reuse f->desired_tool_bar_string, if possible. */
9657 if (size < size_needed || NILP (f->desired_tool_bar_string))
9658 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
9659 make_number (' '));
9660 else
9661 {
9662 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
9663 Fremove_text_properties (make_number (0), make_number (size),
9664 props, f->desired_tool_bar_string);
9665 }
9666
9667 /* Put a `display' property on the string for the images to display,
9668 put a `menu_item' property on tool-bar items with a value that
9669 is the index of the item in F's tool-bar item vector. */
9670 for (i = 0; i < f->n_tool_bar_items; ++i)
9671 {
9672 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
9673
9674 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
9675 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
9676 int hmargin, vmargin, relief, idx, end;
9677 extern Lisp_Object QCrelief, QCmargin, QCconversion;
9678
9679 /* If image is a vector, choose the image according to the
9680 button state. */
9681 image = PROP (TOOL_BAR_ITEM_IMAGES);
9682 if (VECTORP (image))
9683 {
9684 if (enabled_p)
9685 idx = (selected_p
9686 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
9687 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
9688 else
9689 idx = (selected_p
9690 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
9691 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
9692
9693 xassert (ASIZE (image) >= idx);
9694 image = AREF (image, idx);
9695 }
9696 else
9697 idx = -1;
9698
9699 /* Ignore invalid image specifications. */
9700 if (!valid_image_p (image))
9701 continue;
9702
9703 /* Display the tool-bar button pressed, or depressed. */
9704 plist = Fcopy_sequence (XCDR (image));
9705
9706 /* Compute margin and relief to draw. */
9707 relief = (tool_bar_button_relief >= 0
9708 ? tool_bar_button_relief
9709 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
9710 hmargin = vmargin = relief;
9711
9712 if (INTEGERP (Vtool_bar_button_margin)
9713 && XINT (Vtool_bar_button_margin) > 0)
9714 {
9715 hmargin += XFASTINT (Vtool_bar_button_margin);
9716 vmargin += XFASTINT (Vtool_bar_button_margin);
9717 }
9718 else if (CONSP (Vtool_bar_button_margin))
9719 {
9720 if (INTEGERP (XCAR (Vtool_bar_button_margin))
9721 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
9722 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
9723
9724 if (INTEGERP (XCDR (Vtool_bar_button_margin))
9725 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
9726 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
9727 }
9728
9729 if (auto_raise_tool_bar_buttons_p)
9730 {
9731 /* Add a `:relief' property to the image spec if the item is
9732 selected. */
9733 if (selected_p)
9734 {
9735 plist = Fplist_put (plist, QCrelief, make_number (-relief));
9736 hmargin -= relief;
9737 vmargin -= relief;
9738 }
9739 }
9740 else
9741 {
9742 /* If image is selected, display it pressed, i.e. with a
9743 negative relief. If it's not selected, display it with a
9744 raised relief. */
9745 plist = Fplist_put (plist, QCrelief,
9746 (selected_p
9747 ? make_number (-relief)
9748 : make_number (relief)));
9749 hmargin -= relief;
9750 vmargin -= relief;
9751 }
9752
9753 /* Put a margin around the image. */
9754 if (hmargin || vmargin)
9755 {
9756 if (hmargin == vmargin)
9757 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
9758 else
9759 plist = Fplist_put (plist, QCmargin,
9760 Fcons (make_number (hmargin),
9761 make_number (vmargin)));
9762 }
9763
9764 /* If button is not enabled, and we don't have special images
9765 for the disabled state, make the image appear disabled by
9766 applying an appropriate algorithm to it. */
9767 if (!enabled_p && idx < 0)
9768 plist = Fplist_put (plist, QCconversion, Qdisabled);
9769
9770 /* Put a `display' text property on the string for the image to
9771 display. Put a `menu-item' property on the string that gives
9772 the start of this item's properties in the tool-bar items
9773 vector. */
9774 image = Fcons (Qimage, plist);
9775 props = list4 (Qdisplay, image,
9776 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
9777
9778 /* Let the last image hide all remaining spaces in the tool bar
9779 string. The string can be longer than needed when we reuse a
9780 previous string. */
9781 if (i + 1 == f->n_tool_bar_items)
9782 end = SCHARS (f->desired_tool_bar_string);
9783 else
9784 end = i + 1;
9785 Fadd_text_properties (make_number (i), make_number (end),
9786 props, f->desired_tool_bar_string);
9787 #undef PROP
9788 }
9789
9790 UNGCPRO;
9791 }
9792
9793
9794 /* Display one line of the tool-bar of frame IT->f.
9795
9796 HEIGHT specifies the desired height of the tool-bar line.
9797 If the actual height of the glyph row is less than HEIGHT, the
9798 row's height is increased to HEIGHT, and the icons are centered
9799 vertically in the new height.
9800
9801 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
9802 count a final empty row in case the tool-bar width exactly matches
9803 the window width.
9804 */
9805
9806 static void
9807 display_tool_bar_line (it, height)
9808 struct it *it;
9809 int height;
9810 {
9811 struct glyph_row *row = it->glyph_row;
9812 int max_x = it->last_visible_x;
9813 struct glyph *last;
9814
9815 prepare_desired_row (row);
9816 row->y = it->current_y;
9817
9818 /* Note that this isn't made use of if the face hasn't a box,
9819 so there's no need to check the face here. */
9820 it->start_of_box_run_p = 1;
9821
9822 while (it->current_x < max_x)
9823 {
9824 int x, n_glyphs_before, i, nglyphs;
9825 struct it it_before;
9826
9827 /* Get the next display element. */
9828 if (!get_next_display_element (it))
9829 {
9830 /* Don't count empty row if we are counting needed tool-bar lines. */
9831 if (height < 0 && !it->hpos)
9832 return;
9833 break;
9834 }
9835
9836 /* Produce glyphs. */
9837 n_glyphs_before = row->used[TEXT_AREA];
9838 it_before = *it;
9839
9840 PRODUCE_GLYPHS (it);
9841
9842 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
9843 i = 0;
9844 x = it_before.current_x;
9845 while (i < nglyphs)
9846 {
9847 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
9848
9849 if (x + glyph->pixel_width > max_x)
9850 {
9851 /* Glyph doesn't fit on line. Backtrack. */
9852 row->used[TEXT_AREA] = n_glyphs_before;
9853 *it = it_before;
9854 /* If this is the only glyph on this line, it will never fit on the
9855 toolbar, so skip it. But ensure there is at least one glyph,
9856 so we don't accidentally disable the tool-bar. */
9857 if (n_glyphs_before == 0
9858 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
9859 break;
9860 goto out;
9861 }
9862
9863 ++it->hpos;
9864 x += glyph->pixel_width;
9865 ++i;
9866 }
9867
9868 /* Stop at line ends. */
9869 if (ITERATOR_AT_END_OF_LINE_P (it))
9870 break;
9871
9872 set_iterator_to_next (it, 1);
9873 }
9874
9875 out:;
9876
9877 row->displays_text_p = row->used[TEXT_AREA] != 0;
9878
9879 /* Use default face for the border below the tool bar.
9880
9881 FIXME: When auto-resize-tool-bars is grow-only, there is
9882 no additional border below the possibly empty tool-bar lines.
9883 So to make the extra empty lines look "normal", we have to
9884 use the tool-bar face for the border too. */
9885 if (!row->displays_text_p && !EQ (Vauto_resize_tool_bars, Qgrow_only))
9886 it->face_id = DEFAULT_FACE_ID;
9887
9888 extend_face_to_end_of_line (it);
9889 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
9890 last->right_box_line_p = 1;
9891 if (last == row->glyphs[TEXT_AREA])
9892 last->left_box_line_p = 1;
9893
9894 /* Make line the desired height and center it vertically. */
9895 if ((height -= it->max_ascent + it->max_descent) > 0)
9896 {
9897 /* Don't add more than one line height. */
9898 height %= FRAME_LINE_HEIGHT (it->f);
9899 it->max_ascent += height / 2;
9900 it->max_descent += (height + 1) / 2;
9901 }
9902
9903 compute_line_metrics (it);
9904
9905 /* If line is empty, make it occupy the rest of the tool-bar. */
9906 if (!row->displays_text_p)
9907 {
9908 row->height = row->phys_height = it->last_visible_y - row->y;
9909 row->visible_height = row->height;
9910 row->ascent = row->phys_ascent = 0;
9911 row->extra_line_spacing = 0;
9912 }
9913
9914 row->full_width_p = 1;
9915 row->continued_p = 0;
9916 row->truncated_on_left_p = 0;
9917 row->truncated_on_right_p = 0;
9918
9919 it->current_x = it->hpos = 0;
9920 it->current_y += row->height;
9921 ++it->vpos;
9922 ++it->glyph_row;
9923 }
9924
9925
9926 /* Max tool-bar height. */
9927
9928 #define MAX_FRAME_TOOL_BAR_HEIGHT(f) \
9929 ((FRAME_LINE_HEIGHT (f) * FRAME_LINES (f)))
9930
9931 /* Value is the number of screen lines needed to make all tool-bar
9932 items of frame F visible. The number of actual rows needed is
9933 returned in *N_ROWS if non-NULL. */
9934
9935 static int
9936 tool_bar_lines_needed (f, n_rows)
9937 struct frame *f;
9938 int *n_rows;
9939 {
9940 struct window *w = XWINDOW (f->tool_bar_window);
9941 struct it it;
9942 /* tool_bar_lines_needed is called from redisplay_tool_bar after building
9943 the desired matrix, so use (unused) mode-line row as temporary row to
9944 avoid destroying the first tool-bar row. */
9945 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
9946
9947 /* Initialize an iterator for iteration over
9948 F->desired_tool_bar_string in the tool-bar window of frame F. */
9949 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
9950 it.first_visible_x = 0;
9951 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
9952 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
9953
9954 while (!ITERATOR_AT_END_P (&it))
9955 {
9956 clear_glyph_row (temp_row);
9957 it.glyph_row = temp_row;
9958 display_tool_bar_line (&it, -1);
9959 }
9960 clear_glyph_row (temp_row);
9961
9962 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
9963 if (n_rows)
9964 *n_rows = it.vpos > 0 ? it.vpos : -1;
9965
9966 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
9967 }
9968
9969
9970 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
9971 0, 1, 0,
9972 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
9973 (frame)
9974 Lisp_Object frame;
9975 {
9976 struct frame *f;
9977 struct window *w;
9978 int nlines = 0;
9979
9980 if (NILP (frame))
9981 frame = selected_frame;
9982 else
9983 CHECK_FRAME (frame);
9984 f = XFRAME (frame);
9985
9986 if (WINDOWP (f->tool_bar_window)
9987 || (w = XWINDOW (f->tool_bar_window),
9988 WINDOW_TOTAL_LINES (w) > 0))
9989 {
9990 update_tool_bar (f, 1);
9991 if (f->n_tool_bar_items)
9992 {
9993 build_desired_tool_bar_string (f);
9994 nlines = tool_bar_lines_needed (f, NULL);
9995 }
9996 }
9997
9998 return make_number (nlines);
9999 }
10000
10001
10002 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
10003 height should be changed. */
10004
10005 static int
10006 redisplay_tool_bar (f)
10007 struct frame *f;
10008 {
10009 struct window *w;
10010 struct it it;
10011 struct glyph_row *row;
10012
10013 #if defined (USE_GTK) || USE_MAC_TOOLBAR
10014 if (FRAME_EXTERNAL_TOOL_BAR (f))
10015 update_frame_tool_bar (f);
10016 return 0;
10017 #endif
10018
10019 /* If frame hasn't a tool-bar window or if it is zero-height, don't
10020 do anything. This means you must start with tool-bar-lines
10021 non-zero to get the auto-sizing effect. Or in other words, you
10022 can turn off tool-bars by specifying tool-bar-lines zero. */
10023 if (!WINDOWP (f->tool_bar_window)
10024 || (w = XWINDOW (f->tool_bar_window),
10025 WINDOW_TOTAL_LINES (w) == 0))
10026 return 0;
10027
10028 /* Set up an iterator for the tool-bar window. */
10029 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
10030 it.first_visible_x = 0;
10031 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
10032 row = it.glyph_row;
10033
10034 /* Build a string that represents the contents of the tool-bar. */
10035 build_desired_tool_bar_string (f);
10036 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
10037
10038 if (f->n_tool_bar_rows == 0)
10039 {
10040 int nlines;
10041
10042 if ((nlines = tool_bar_lines_needed (f, &f->n_tool_bar_rows),
10043 nlines != WINDOW_TOTAL_LINES (w)))
10044 {
10045 extern Lisp_Object Qtool_bar_lines;
10046 Lisp_Object frame;
10047 int old_height = WINDOW_TOTAL_LINES (w);
10048
10049 XSETFRAME (frame, f);
10050 Fmodify_frame_parameters (frame,
10051 Fcons (Fcons (Qtool_bar_lines,
10052 make_number (nlines)),
10053 Qnil));
10054 if (WINDOW_TOTAL_LINES (w) != old_height)
10055 {
10056 clear_glyph_matrix (w->desired_matrix);
10057 fonts_changed_p = 1;
10058 return 1;
10059 }
10060 }
10061 }
10062
10063 /* Display as many lines as needed to display all tool-bar items. */
10064
10065 if (f->n_tool_bar_rows > 0)
10066 {
10067 int border, rows, height, extra;
10068
10069 if (INTEGERP (Vtool_bar_border))
10070 border = XINT (Vtool_bar_border);
10071 else if (EQ (Vtool_bar_border, Qinternal_border_width))
10072 border = FRAME_INTERNAL_BORDER_WIDTH (f);
10073 else if (EQ (Vtool_bar_border, Qborder_width))
10074 border = f->border_width;
10075 else
10076 border = 0;
10077 if (border < 0)
10078 border = 0;
10079
10080 rows = f->n_tool_bar_rows;
10081 height = max (1, (it.last_visible_y - border) / rows);
10082 extra = it.last_visible_y - border - height * rows;
10083
10084 while (it.current_y < it.last_visible_y)
10085 {
10086 int h = 0;
10087 if (extra > 0 && rows-- > 0)
10088 {
10089 h = (extra + rows - 1) / rows;
10090 extra -= h;
10091 }
10092 display_tool_bar_line (&it, height + h);
10093 }
10094 }
10095 else
10096 {
10097 while (it.current_y < it.last_visible_y)
10098 display_tool_bar_line (&it, 0);
10099 }
10100
10101 /* It doesn't make much sense to try scrolling in the tool-bar
10102 window, so don't do it. */
10103 w->desired_matrix->no_scrolling_p = 1;
10104 w->must_be_updated_p = 1;
10105
10106 if (!NILP (Vauto_resize_tool_bars))
10107 {
10108 int max_tool_bar_height = MAX_FRAME_TOOL_BAR_HEIGHT (f);
10109 int change_height_p = 0;
10110
10111 /* If we couldn't display everything, change the tool-bar's
10112 height if there is room for more. */
10113 if (IT_STRING_CHARPOS (it) < it.end_charpos
10114 && it.current_y < max_tool_bar_height)
10115 change_height_p = 1;
10116
10117 row = it.glyph_row - 1;
10118
10119 /* If there are blank lines at the end, except for a partially
10120 visible blank line at the end that is smaller than
10121 FRAME_LINE_HEIGHT, change the tool-bar's height. */
10122 if (!row->displays_text_p
10123 && row->height >= FRAME_LINE_HEIGHT (f))
10124 change_height_p = 1;
10125
10126 /* If row displays tool-bar items, but is partially visible,
10127 change the tool-bar's height. */
10128 if (row->displays_text_p
10129 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y
10130 && MATRIX_ROW_BOTTOM_Y (row) < max_tool_bar_height)
10131 change_height_p = 1;
10132
10133 /* Resize windows as needed by changing the `tool-bar-lines'
10134 frame parameter. */
10135 if (change_height_p)
10136 {
10137 extern Lisp_Object Qtool_bar_lines;
10138 Lisp_Object frame;
10139 int old_height = WINDOW_TOTAL_LINES (w);
10140 int nrows;
10141 int nlines = tool_bar_lines_needed (f, &nrows);
10142
10143 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
10144 && !f->minimize_tool_bar_window_p)
10145 ? (nlines > old_height)
10146 : (nlines != old_height));
10147 f->minimize_tool_bar_window_p = 0;
10148
10149 if (change_height_p)
10150 {
10151 XSETFRAME (frame, f);
10152 Fmodify_frame_parameters (frame,
10153 Fcons (Fcons (Qtool_bar_lines,
10154 make_number (nlines)),
10155 Qnil));
10156 if (WINDOW_TOTAL_LINES (w) != old_height)
10157 {
10158 clear_glyph_matrix (w->desired_matrix);
10159 f->n_tool_bar_rows = nrows;
10160 fonts_changed_p = 1;
10161 return 1;
10162 }
10163 }
10164 }
10165 }
10166
10167 f->minimize_tool_bar_window_p = 0;
10168 return 0;
10169 }
10170
10171
10172 /* Get information about the tool-bar item which is displayed in GLYPH
10173 on frame F. Return in *PROP_IDX the index where tool-bar item
10174 properties start in F->tool_bar_items. Value is zero if
10175 GLYPH doesn't display a tool-bar item. */
10176
10177 static int
10178 tool_bar_item_info (f, glyph, prop_idx)
10179 struct frame *f;
10180 struct glyph *glyph;
10181 int *prop_idx;
10182 {
10183 Lisp_Object prop;
10184 int success_p;
10185 int charpos;
10186
10187 /* This function can be called asynchronously, which means we must
10188 exclude any possibility that Fget_text_property signals an
10189 error. */
10190 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
10191 charpos = max (0, charpos);
10192
10193 /* Get the text property `menu-item' at pos. The value of that
10194 property is the start index of this item's properties in
10195 F->tool_bar_items. */
10196 prop = Fget_text_property (make_number (charpos),
10197 Qmenu_item, f->current_tool_bar_string);
10198 if (INTEGERP (prop))
10199 {
10200 *prop_idx = XINT (prop);
10201 success_p = 1;
10202 }
10203 else
10204 success_p = 0;
10205
10206 return success_p;
10207 }
10208
10209 \f
10210 /* Get information about the tool-bar item at position X/Y on frame F.
10211 Return in *GLYPH a pointer to the glyph of the tool-bar item in
10212 the current matrix of the tool-bar window of F, or NULL if not
10213 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
10214 item in F->tool_bar_items. Value is
10215
10216 -1 if X/Y is not on a tool-bar item
10217 0 if X/Y is on the same item that was highlighted before.
10218 1 otherwise. */
10219
10220 static int
10221 get_tool_bar_item (f, x, y, glyph, hpos, vpos, prop_idx)
10222 struct frame *f;
10223 int x, y;
10224 struct glyph **glyph;
10225 int *hpos, *vpos, *prop_idx;
10226 {
10227 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10228 struct window *w = XWINDOW (f->tool_bar_window);
10229 int area;
10230
10231 /* Find the glyph under X/Y. */
10232 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
10233 if (*glyph == NULL)
10234 return -1;
10235
10236 /* Get the start of this tool-bar item's properties in
10237 f->tool_bar_items. */
10238 if (!tool_bar_item_info (f, *glyph, prop_idx))
10239 return -1;
10240
10241 /* Is mouse on the highlighted item? */
10242 if (EQ (f->tool_bar_window, dpyinfo->mouse_face_window)
10243 && *vpos >= dpyinfo->mouse_face_beg_row
10244 && *vpos <= dpyinfo->mouse_face_end_row
10245 && (*vpos > dpyinfo->mouse_face_beg_row
10246 || *hpos >= dpyinfo->mouse_face_beg_col)
10247 && (*vpos < dpyinfo->mouse_face_end_row
10248 || *hpos < dpyinfo->mouse_face_end_col
10249 || dpyinfo->mouse_face_past_end))
10250 return 0;
10251
10252 return 1;
10253 }
10254
10255
10256 /* EXPORT:
10257 Handle mouse button event on the tool-bar of frame F, at
10258 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
10259 0 for button release. MODIFIERS is event modifiers for button
10260 release. */
10261
10262 void
10263 handle_tool_bar_click (f, x, y, down_p, modifiers)
10264 struct frame *f;
10265 int x, y, down_p;
10266 unsigned int modifiers;
10267 {
10268 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10269 struct window *w = XWINDOW (f->tool_bar_window);
10270 int hpos, vpos, prop_idx;
10271 struct glyph *glyph;
10272 Lisp_Object enabled_p;
10273
10274 /* If not on the highlighted tool-bar item, return. */
10275 frame_to_window_pixel_xy (w, &x, &y);
10276 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
10277 return;
10278
10279 /* If item is disabled, do nothing. */
10280 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
10281 if (NILP (enabled_p))
10282 return;
10283
10284 if (down_p)
10285 {
10286 /* Show item in pressed state. */
10287 show_mouse_face (dpyinfo, DRAW_IMAGE_SUNKEN);
10288 dpyinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
10289 last_tool_bar_item = prop_idx;
10290 }
10291 else
10292 {
10293 Lisp_Object key, frame;
10294 struct input_event event;
10295 EVENT_INIT (event);
10296
10297 /* Show item in released state. */
10298 show_mouse_face (dpyinfo, DRAW_IMAGE_RAISED);
10299 dpyinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
10300
10301 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
10302
10303 XSETFRAME (frame, f);
10304 event.kind = TOOL_BAR_EVENT;
10305 event.frame_or_window = frame;
10306 event.arg = frame;
10307 kbd_buffer_store_event (&event);
10308
10309 event.kind = TOOL_BAR_EVENT;
10310 event.frame_or_window = frame;
10311 event.arg = key;
10312 event.modifiers = modifiers;
10313 kbd_buffer_store_event (&event);
10314 last_tool_bar_item = -1;
10315 }
10316 }
10317
10318
10319 /* Possibly highlight a tool-bar item on frame F when mouse moves to
10320 tool-bar window-relative coordinates X/Y. Called from
10321 note_mouse_highlight. */
10322
10323 static void
10324 note_tool_bar_highlight (f, x, y)
10325 struct frame *f;
10326 int x, y;
10327 {
10328 Lisp_Object window = f->tool_bar_window;
10329 struct window *w = XWINDOW (window);
10330 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10331 int hpos, vpos;
10332 struct glyph *glyph;
10333 struct glyph_row *row;
10334 int i;
10335 Lisp_Object enabled_p;
10336 int prop_idx;
10337 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
10338 int mouse_down_p, rc;
10339
10340 /* Function note_mouse_highlight is called with negative x(y
10341 values when mouse moves outside of the frame. */
10342 if (x <= 0 || y <= 0)
10343 {
10344 clear_mouse_face (dpyinfo);
10345 return;
10346 }
10347
10348 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
10349 if (rc < 0)
10350 {
10351 /* Not on tool-bar item. */
10352 clear_mouse_face (dpyinfo);
10353 return;
10354 }
10355 else if (rc == 0)
10356 /* On same tool-bar item as before. */
10357 goto set_help_echo;
10358
10359 clear_mouse_face (dpyinfo);
10360
10361 /* Mouse is down, but on different tool-bar item? */
10362 mouse_down_p = (dpyinfo->grabbed
10363 && f == last_mouse_frame
10364 && FRAME_LIVE_P (f));
10365 if (mouse_down_p
10366 && last_tool_bar_item != prop_idx)
10367 return;
10368
10369 dpyinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
10370 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
10371
10372 /* If tool-bar item is not enabled, don't highlight it. */
10373 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
10374 if (!NILP (enabled_p))
10375 {
10376 /* Compute the x-position of the glyph. In front and past the
10377 image is a space. We include this in the highlighted area. */
10378 row = MATRIX_ROW (w->current_matrix, vpos);
10379 for (i = x = 0; i < hpos; ++i)
10380 x += row->glyphs[TEXT_AREA][i].pixel_width;
10381
10382 /* Record this as the current active region. */
10383 dpyinfo->mouse_face_beg_col = hpos;
10384 dpyinfo->mouse_face_beg_row = vpos;
10385 dpyinfo->mouse_face_beg_x = x;
10386 dpyinfo->mouse_face_beg_y = row->y;
10387 dpyinfo->mouse_face_past_end = 0;
10388
10389 dpyinfo->mouse_face_end_col = hpos + 1;
10390 dpyinfo->mouse_face_end_row = vpos;
10391 dpyinfo->mouse_face_end_x = x + glyph->pixel_width;
10392 dpyinfo->mouse_face_end_y = row->y;
10393 dpyinfo->mouse_face_window = window;
10394 dpyinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
10395
10396 /* Display it as active. */
10397 show_mouse_face (dpyinfo, draw);
10398 dpyinfo->mouse_face_image_state = draw;
10399 }
10400
10401 set_help_echo:
10402
10403 /* Set help_echo_string to a help string to display for this tool-bar item.
10404 XTread_socket does the rest. */
10405 help_echo_object = help_echo_window = Qnil;
10406 help_echo_pos = -1;
10407 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
10408 if (NILP (help_echo_string))
10409 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
10410 }
10411
10412 #endif /* HAVE_WINDOW_SYSTEM */
10413
10414
10415 \f
10416 /************************************************************************
10417 Horizontal scrolling
10418 ************************************************************************/
10419
10420 static int hscroll_window_tree P_ ((Lisp_Object));
10421 static int hscroll_windows P_ ((Lisp_Object));
10422
10423 /* For all leaf windows in the window tree rooted at WINDOW, set their
10424 hscroll value so that PT is (i) visible in the window, and (ii) so
10425 that it is not within a certain margin at the window's left and
10426 right border. Value is non-zero if any window's hscroll has been
10427 changed. */
10428
10429 static int
10430 hscroll_window_tree (window)
10431 Lisp_Object window;
10432 {
10433 int hscrolled_p = 0;
10434 int hscroll_relative_p = FLOATP (Vhscroll_step);
10435 int hscroll_step_abs = 0;
10436 double hscroll_step_rel = 0;
10437
10438 if (hscroll_relative_p)
10439 {
10440 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
10441 if (hscroll_step_rel < 0)
10442 {
10443 hscroll_relative_p = 0;
10444 hscroll_step_abs = 0;
10445 }
10446 }
10447 else if (INTEGERP (Vhscroll_step))
10448 {
10449 hscroll_step_abs = XINT (Vhscroll_step);
10450 if (hscroll_step_abs < 0)
10451 hscroll_step_abs = 0;
10452 }
10453 else
10454 hscroll_step_abs = 0;
10455
10456 while (WINDOWP (window))
10457 {
10458 struct window *w = XWINDOW (window);
10459
10460 if (WINDOWP (w->hchild))
10461 hscrolled_p |= hscroll_window_tree (w->hchild);
10462 else if (WINDOWP (w->vchild))
10463 hscrolled_p |= hscroll_window_tree (w->vchild);
10464 else if (w->cursor.vpos >= 0)
10465 {
10466 int h_margin;
10467 int text_area_width;
10468 struct glyph_row *current_cursor_row
10469 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
10470 struct glyph_row *desired_cursor_row
10471 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
10472 struct glyph_row *cursor_row
10473 = (desired_cursor_row->enabled_p
10474 ? desired_cursor_row
10475 : current_cursor_row);
10476
10477 text_area_width = window_box_width (w, TEXT_AREA);
10478
10479 /* Scroll when cursor is inside this scroll margin. */
10480 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
10481
10482 if ((XFASTINT (w->hscroll)
10483 && w->cursor.x <= h_margin)
10484 || (cursor_row->enabled_p
10485 && cursor_row->truncated_on_right_p
10486 && (w->cursor.x >= text_area_width - h_margin)))
10487 {
10488 struct it it;
10489 int hscroll;
10490 struct buffer *saved_current_buffer;
10491 int pt;
10492 int wanted_x;
10493
10494 /* Find point in a display of infinite width. */
10495 saved_current_buffer = current_buffer;
10496 current_buffer = XBUFFER (w->buffer);
10497
10498 if (w == XWINDOW (selected_window))
10499 pt = BUF_PT (current_buffer);
10500 else
10501 {
10502 pt = marker_position (w->pointm);
10503 pt = max (BEGV, pt);
10504 pt = min (ZV, pt);
10505 }
10506
10507 /* Move iterator to pt starting at cursor_row->start in
10508 a line with infinite width. */
10509 init_to_row_start (&it, w, cursor_row);
10510 it.last_visible_x = INFINITY;
10511 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
10512 current_buffer = saved_current_buffer;
10513
10514 /* Position cursor in window. */
10515 if (!hscroll_relative_p && hscroll_step_abs == 0)
10516 hscroll = max (0, (it.current_x
10517 - (ITERATOR_AT_END_OF_LINE_P (&it)
10518 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
10519 : (text_area_width / 2))))
10520 / FRAME_COLUMN_WIDTH (it.f);
10521 else if (w->cursor.x >= text_area_width - h_margin)
10522 {
10523 if (hscroll_relative_p)
10524 wanted_x = text_area_width * (1 - hscroll_step_rel)
10525 - h_margin;
10526 else
10527 wanted_x = text_area_width
10528 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
10529 - h_margin;
10530 hscroll
10531 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
10532 }
10533 else
10534 {
10535 if (hscroll_relative_p)
10536 wanted_x = text_area_width * hscroll_step_rel
10537 + h_margin;
10538 else
10539 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
10540 + h_margin;
10541 hscroll
10542 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
10543 }
10544 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
10545
10546 /* Don't call Fset_window_hscroll if value hasn't
10547 changed because it will prevent redisplay
10548 optimizations. */
10549 if (XFASTINT (w->hscroll) != hscroll)
10550 {
10551 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
10552 w->hscroll = make_number (hscroll);
10553 hscrolled_p = 1;
10554 }
10555 }
10556 }
10557
10558 window = w->next;
10559 }
10560
10561 /* Value is non-zero if hscroll of any leaf window has been changed. */
10562 return hscrolled_p;
10563 }
10564
10565
10566 /* Set hscroll so that cursor is visible and not inside horizontal
10567 scroll margins for all windows in the tree rooted at WINDOW. See
10568 also hscroll_window_tree above. Value is non-zero if any window's
10569 hscroll has been changed. If it has, desired matrices on the frame
10570 of WINDOW are cleared. */
10571
10572 static int
10573 hscroll_windows (window)
10574 Lisp_Object window;
10575 {
10576 int hscrolled_p;
10577
10578 if (automatic_hscrolling_p)
10579 {
10580 hscrolled_p = hscroll_window_tree (window);
10581 if (hscrolled_p)
10582 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
10583 }
10584 else
10585 hscrolled_p = 0;
10586 return hscrolled_p;
10587 }
10588
10589
10590 \f
10591 /************************************************************************
10592 Redisplay
10593 ************************************************************************/
10594
10595 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
10596 to a non-zero value. This is sometimes handy to have in a debugger
10597 session. */
10598
10599 #if GLYPH_DEBUG
10600
10601 /* First and last unchanged row for try_window_id. */
10602
10603 int debug_first_unchanged_at_end_vpos;
10604 int debug_last_unchanged_at_beg_vpos;
10605
10606 /* Delta vpos and y. */
10607
10608 int debug_dvpos, debug_dy;
10609
10610 /* Delta in characters and bytes for try_window_id. */
10611
10612 int debug_delta, debug_delta_bytes;
10613
10614 /* Values of window_end_pos and window_end_vpos at the end of
10615 try_window_id. */
10616
10617 EMACS_INT debug_end_pos, debug_end_vpos;
10618
10619 /* Append a string to W->desired_matrix->method. FMT is a printf
10620 format string. A1...A9 are a supplement for a variable-length
10621 argument list. If trace_redisplay_p is non-zero also printf the
10622 resulting string to stderr. */
10623
10624 static void
10625 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
10626 struct window *w;
10627 char *fmt;
10628 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
10629 {
10630 char buffer[512];
10631 char *method = w->desired_matrix->method;
10632 int len = strlen (method);
10633 int size = sizeof w->desired_matrix->method;
10634 int remaining = size - len - 1;
10635
10636 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
10637 if (len && remaining)
10638 {
10639 method[len] = '|';
10640 --remaining, ++len;
10641 }
10642
10643 strncpy (method + len, buffer, remaining);
10644
10645 if (trace_redisplay_p)
10646 fprintf (stderr, "%p (%s): %s\n",
10647 w,
10648 ((BUFFERP (w->buffer)
10649 && STRINGP (XBUFFER (w->buffer)->name))
10650 ? (char *) SDATA (XBUFFER (w->buffer)->name)
10651 : "no buffer"),
10652 buffer);
10653 }
10654
10655 #endif /* GLYPH_DEBUG */
10656
10657
10658 /* Value is non-zero if all changes in window W, which displays
10659 current_buffer, are in the text between START and END. START is a
10660 buffer position, END is given as a distance from Z. Used in
10661 redisplay_internal for display optimization. */
10662
10663 static INLINE int
10664 text_outside_line_unchanged_p (w, start, end)
10665 struct window *w;
10666 int start, end;
10667 {
10668 int unchanged_p = 1;
10669
10670 /* If text or overlays have changed, see where. */
10671 if (XFASTINT (w->last_modified) < MODIFF
10672 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
10673 {
10674 /* Gap in the line? */
10675 if (GPT < start || Z - GPT < end)
10676 unchanged_p = 0;
10677
10678 /* Changes start in front of the line, or end after it? */
10679 if (unchanged_p
10680 && (BEG_UNCHANGED < start - 1
10681 || END_UNCHANGED < end))
10682 unchanged_p = 0;
10683
10684 /* If selective display, can't optimize if changes start at the
10685 beginning of the line. */
10686 if (unchanged_p
10687 && INTEGERP (current_buffer->selective_display)
10688 && XINT (current_buffer->selective_display) > 0
10689 && (BEG_UNCHANGED < start || GPT <= start))
10690 unchanged_p = 0;
10691
10692 /* If there are overlays at the start or end of the line, these
10693 may have overlay strings with newlines in them. A change at
10694 START, for instance, may actually concern the display of such
10695 overlay strings as well, and they are displayed on different
10696 lines. So, quickly rule out this case. (For the future, it
10697 might be desirable to implement something more telling than
10698 just BEG/END_UNCHANGED.) */
10699 if (unchanged_p)
10700 {
10701 if (BEG + BEG_UNCHANGED == start
10702 && overlay_touches_p (start))
10703 unchanged_p = 0;
10704 if (END_UNCHANGED == end
10705 && overlay_touches_p (Z - end))
10706 unchanged_p = 0;
10707 }
10708 }
10709
10710 return unchanged_p;
10711 }
10712
10713
10714 /* Do a frame update, taking possible shortcuts into account. This is
10715 the main external entry point for redisplay.
10716
10717 If the last redisplay displayed an echo area message and that message
10718 is no longer requested, we clear the echo area or bring back the
10719 mini-buffer if that is in use. */
10720
10721 void
10722 redisplay ()
10723 {
10724 redisplay_internal (0);
10725 }
10726
10727
10728 static Lisp_Object
10729 overlay_arrow_string_or_property (var)
10730 Lisp_Object var;
10731 {
10732 Lisp_Object val;
10733
10734 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
10735 return val;
10736
10737 return Voverlay_arrow_string;
10738 }
10739
10740 /* Return 1 if there are any overlay-arrows in current_buffer. */
10741 static int
10742 overlay_arrow_in_current_buffer_p ()
10743 {
10744 Lisp_Object vlist;
10745
10746 for (vlist = Voverlay_arrow_variable_list;
10747 CONSP (vlist);
10748 vlist = XCDR (vlist))
10749 {
10750 Lisp_Object var = XCAR (vlist);
10751 Lisp_Object val;
10752
10753 if (!SYMBOLP (var))
10754 continue;
10755 val = find_symbol_value (var);
10756 if (MARKERP (val)
10757 && current_buffer == XMARKER (val)->buffer)
10758 return 1;
10759 }
10760 return 0;
10761 }
10762
10763
10764 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
10765 has changed. */
10766
10767 static int
10768 overlay_arrows_changed_p ()
10769 {
10770 Lisp_Object vlist;
10771
10772 for (vlist = Voverlay_arrow_variable_list;
10773 CONSP (vlist);
10774 vlist = XCDR (vlist))
10775 {
10776 Lisp_Object var = XCAR (vlist);
10777 Lisp_Object val, pstr;
10778
10779 if (!SYMBOLP (var))
10780 continue;
10781 val = find_symbol_value (var);
10782 if (!MARKERP (val))
10783 continue;
10784 if (! EQ (COERCE_MARKER (val),
10785 Fget (var, Qlast_arrow_position))
10786 || ! (pstr = overlay_arrow_string_or_property (var),
10787 EQ (pstr, Fget (var, Qlast_arrow_string))))
10788 return 1;
10789 }
10790 return 0;
10791 }
10792
10793 /* Mark overlay arrows to be updated on next redisplay. */
10794
10795 static void
10796 update_overlay_arrows (up_to_date)
10797 int up_to_date;
10798 {
10799 Lisp_Object vlist;
10800
10801 for (vlist = Voverlay_arrow_variable_list;
10802 CONSP (vlist);
10803 vlist = XCDR (vlist))
10804 {
10805 Lisp_Object var = XCAR (vlist);
10806
10807 if (!SYMBOLP (var))
10808 continue;
10809
10810 if (up_to_date > 0)
10811 {
10812 Lisp_Object val = find_symbol_value (var);
10813 Fput (var, Qlast_arrow_position,
10814 COERCE_MARKER (val));
10815 Fput (var, Qlast_arrow_string,
10816 overlay_arrow_string_or_property (var));
10817 }
10818 else if (up_to_date < 0
10819 || !NILP (Fget (var, Qlast_arrow_position)))
10820 {
10821 Fput (var, Qlast_arrow_position, Qt);
10822 Fput (var, Qlast_arrow_string, Qt);
10823 }
10824 }
10825 }
10826
10827
10828 /* Return overlay arrow string to display at row.
10829 Return integer (bitmap number) for arrow bitmap in left fringe.
10830 Return nil if no overlay arrow. */
10831
10832 static Lisp_Object
10833 overlay_arrow_at_row (it, row)
10834 struct it *it;
10835 struct glyph_row *row;
10836 {
10837 Lisp_Object vlist;
10838
10839 for (vlist = Voverlay_arrow_variable_list;
10840 CONSP (vlist);
10841 vlist = XCDR (vlist))
10842 {
10843 Lisp_Object var = XCAR (vlist);
10844 Lisp_Object val;
10845
10846 if (!SYMBOLP (var))
10847 continue;
10848
10849 val = find_symbol_value (var);
10850
10851 if (MARKERP (val)
10852 && current_buffer == XMARKER (val)->buffer
10853 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
10854 {
10855 if (FRAME_WINDOW_P (it->f)
10856 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
10857 {
10858 #ifdef HAVE_WINDOW_SYSTEM
10859 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
10860 {
10861 int fringe_bitmap;
10862 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
10863 return make_number (fringe_bitmap);
10864 }
10865 #endif
10866 return make_number (-1); /* Use default arrow bitmap */
10867 }
10868 return overlay_arrow_string_or_property (var);
10869 }
10870 }
10871
10872 return Qnil;
10873 }
10874
10875 /* Return 1 if point moved out of or into a composition. Otherwise
10876 return 0. PREV_BUF and PREV_PT are the last point buffer and
10877 position. BUF and PT are the current point buffer and position. */
10878
10879 int
10880 check_point_in_composition (prev_buf, prev_pt, buf, pt)
10881 struct buffer *prev_buf, *buf;
10882 int prev_pt, pt;
10883 {
10884 EMACS_INT start, end;
10885 Lisp_Object prop;
10886 Lisp_Object buffer;
10887
10888 XSETBUFFER (buffer, buf);
10889 /* Check a composition at the last point if point moved within the
10890 same buffer. */
10891 if (prev_buf == buf)
10892 {
10893 if (prev_pt == pt)
10894 /* Point didn't move. */
10895 return 0;
10896
10897 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
10898 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
10899 && COMPOSITION_VALID_P (start, end, prop)
10900 && start < prev_pt && end > prev_pt)
10901 /* The last point was within the composition. Return 1 iff
10902 point moved out of the composition. */
10903 return (pt <= start || pt >= end);
10904 }
10905
10906 /* Check a composition at the current point. */
10907 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
10908 && find_composition (pt, -1, &start, &end, &prop, buffer)
10909 && COMPOSITION_VALID_P (start, end, prop)
10910 && start < pt && end > pt);
10911 }
10912
10913
10914 /* Reconsider the setting of B->clip_changed which is displayed
10915 in window W. */
10916
10917 static INLINE void
10918 reconsider_clip_changes (w, b)
10919 struct window *w;
10920 struct buffer *b;
10921 {
10922 if (b->clip_changed
10923 && !NILP (w->window_end_valid)
10924 && w->current_matrix->buffer == b
10925 && w->current_matrix->zv == BUF_ZV (b)
10926 && w->current_matrix->begv == BUF_BEGV (b))
10927 b->clip_changed = 0;
10928
10929 /* If display wasn't paused, and W is not a tool bar window, see if
10930 point has been moved into or out of a composition. In that case,
10931 we set b->clip_changed to 1 to force updating the screen. If
10932 b->clip_changed has already been set to 1, we can skip this
10933 check. */
10934 if (!b->clip_changed
10935 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
10936 {
10937 int pt;
10938
10939 if (w == XWINDOW (selected_window))
10940 pt = BUF_PT (current_buffer);
10941 else
10942 pt = marker_position (w->pointm);
10943
10944 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
10945 || pt != XINT (w->last_point))
10946 && check_point_in_composition (w->current_matrix->buffer,
10947 XINT (w->last_point),
10948 XBUFFER (w->buffer), pt))
10949 b->clip_changed = 1;
10950 }
10951 }
10952 \f
10953
10954 /* Select FRAME to forward the values of frame-local variables into C
10955 variables so that the redisplay routines can access those values
10956 directly. */
10957
10958 static void
10959 select_frame_for_redisplay (frame)
10960 Lisp_Object frame;
10961 {
10962 Lisp_Object tail, sym, val;
10963 Lisp_Object old = selected_frame;
10964
10965 xassert (FRAMEP (frame) && FRAME_LIVE_P (XFRAME (frame)));
10966
10967 selected_frame = frame;
10968
10969 for (tail = XFRAME (frame)->param_alist; CONSP (tail); tail = XCDR (tail))
10970 if (CONSP (XCAR (tail))
10971 && (sym = XCAR (XCAR (tail)),
10972 SYMBOLP (sym))
10973 && (sym = indirect_variable (sym),
10974 val = SYMBOL_VALUE (sym),
10975 (BUFFER_LOCAL_VALUEP (val)
10976 || SOME_BUFFER_LOCAL_VALUEP (val)))
10977 && XBUFFER_LOCAL_VALUE (val)->check_frame)
10978 /* Use find_symbol_value rather than Fsymbol_value
10979 to avoid an error if it is void. */
10980 find_symbol_value (sym);
10981
10982 for (tail = XFRAME (old)->param_alist; CONSP (tail); tail = XCDR (tail))
10983 if (CONSP (XCAR (tail))
10984 && (sym = XCAR (XCAR (tail)),
10985 SYMBOLP (sym))
10986 && (sym = indirect_variable (sym),
10987 val = SYMBOL_VALUE (sym),
10988 (BUFFER_LOCAL_VALUEP (val)
10989 || SOME_BUFFER_LOCAL_VALUEP (val)))
10990 && XBUFFER_LOCAL_VALUE (val)->check_frame)
10991 find_symbol_value (sym);
10992 }
10993
10994
10995 #define STOP_POLLING \
10996 do { if (! polling_stopped_here) stop_polling (); \
10997 polling_stopped_here = 1; } while (0)
10998
10999 #define RESUME_POLLING \
11000 do { if (polling_stopped_here) start_polling (); \
11001 polling_stopped_here = 0; } while (0)
11002
11003
11004 /* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
11005 response to any user action; therefore, we should preserve the echo
11006 area. (Actually, our caller does that job.) Perhaps in the future
11007 avoid recentering windows if it is not necessary; currently that
11008 causes some problems. */
11009
11010 static void
11011 redisplay_internal (preserve_echo_area)
11012 int preserve_echo_area;
11013 {
11014 struct window *w = XWINDOW (selected_window);
11015 struct frame *f;
11016 int pause;
11017 int must_finish = 0;
11018 struct text_pos tlbufpos, tlendpos;
11019 int number_of_visible_frames;
11020 int count, count1;
11021 struct frame *sf;
11022 int polling_stopped_here = 0;
11023 Lisp_Object old_frame = selected_frame;
11024
11025 /* Non-zero means redisplay has to consider all windows on all
11026 frames. Zero means, only selected_window is considered. */
11027 int consider_all_windows_p;
11028
11029 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
11030
11031 /* No redisplay if running in batch mode or frame is not yet fully
11032 initialized, or redisplay is explicitly turned off by setting
11033 Vinhibit_redisplay. */
11034 if (noninteractive
11035 || !NILP (Vinhibit_redisplay))
11036 return;
11037
11038 /* Don't examine these until after testing Vinhibit_redisplay.
11039 When Emacs is shutting down, perhaps because its connection to
11040 X has dropped, we should not look at them at all. */
11041 f = XFRAME (w->frame);
11042 sf = SELECTED_FRAME ();
11043
11044 if (!f->glyphs_initialized_p)
11045 return;
11046
11047 /* The flag redisplay_performed_directly_p is set by
11048 direct_output_for_insert when it already did the whole screen
11049 update necessary. */
11050 if (redisplay_performed_directly_p)
11051 {
11052 redisplay_performed_directly_p = 0;
11053 if (!hscroll_windows (selected_window))
11054 return;
11055 }
11056
11057 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (MAC_OS)
11058 if (popup_activated ())
11059 return;
11060 #endif
11061
11062 /* I don't think this happens but let's be paranoid. */
11063 if (redisplaying_p)
11064 return;
11065
11066 /* Record a function that resets redisplaying_p to its old value
11067 when we leave this function. */
11068 count = SPECPDL_INDEX ();
11069 record_unwind_protect (unwind_redisplay,
11070 Fcons (make_number (redisplaying_p), selected_frame));
11071 ++redisplaying_p;
11072 specbind (Qinhibit_free_realized_faces, Qnil);
11073
11074 {
11075 Lisp_Object tail, frame;
11076
11077 FOR_EACH_FRAME (tail, frame)
11078 {
11079 struct frame *f = XFRAME (frame);
11080 f->already_hscrolled_p = 0;
11081 }
11082 }
11083
11084 retry:
11085 if (!EQ (old_frame, selected_frame)
11086 && FRAME_LIVE_P (XFRAME (old_frame)))
11087 /* When running redisplay, we play a bit fast-and-loose and allow e.g.
11088 selected_frame and selected_window to be temporarily out-of-sync so
11089 when we come back here via `goto retry', we need to resync because we
11090 may need to run Elisp code (via prepare_menu_bars). */
11091 select_frame_for_redisplay (old_frame);
11092
11093 pause = 0;
11094 reconsider_clip_changes (w, current_buffer);
11095 last_escape_glyph_frame = NULL;
11096 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
11097
11098 /* If new fonts have been loaded that make a glyph matrix adjustment
11099 necessary, do it. */
11100 if (fonts_changed_p)
11101 {
11102 adjust_glyphs (NULL);
11103 ++windows_or_buffers_changed;
11104 fonts_changed_p = 0;
11105 }
11106
11107 /* If face_change_count is non-zero, init_iterator will free all
11108 realized faces, which includes the faces referenced from current
11109 matrices. So, we can't reuse current matrices in this case. */
11110 if (face_change_count)
11111 ++windows_or_buffers_changed;
11112
11113 if (FRAME_TERMCAP_P (sf)
11114 && FRAME_TTY (sf)->previous_frame != sf)
11115 {
11116 /* Since frames on a single ASCII terminal share the same
11117 display area, displaying a different frame means redisplay
11118 the whole thing. */
11119 windows_or_buffers_changed++;
11120 SET_FRAME_GARBAGED (sf);
11121 FRAME_TTY (sf)->previous_frame = sf;
11122 }
11123
11124 /* Set the visible flags for all frames. Do this before checking
11125 for resized or garbaged frames; they want to know if their frames
11126 are visible. See the comment in frame.h for
11127 FRAME_SAMPLE_VISIBILITY. */
11128 {
11129 Lisp_Object tail, frame;
11130
11131 number_of_visible_frames = 0;
11132
11133 FOR_EACH_FRAME (tail, frame)
11134 {
11135 struct frame *f = XFRAME (frame);
11136
11137 FRAME_SAMPLE_VISIBILITY (f);
11138 if (FRAME_VISIBLE_P (f))
11139 ++number_of_visible_frames;
11140 clear_desired_matrices (f);
11141 }
11142 }
11143
11144
11145 /* Notice any pending interrupt request to change frame size. */
11146 do_pending_window_change (1);
11147
11148 /* Clear frames marked as garbaged. */
11149 if (frame_garbaged)
11150 clear_garbaged_frames ();
11151
11152 /* Build menubar and tool-bar items. */
11153 if (NILP (Vmemory_full))
11154 prepare_menu_bars ();
11155
11156 if (windows_or_buffers_changed)
11157 update_mode_lines++;
11158
11159 /* Detect case that we need to write or remove a star in the mode line. */
11160 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
11161 {
11162 w->update_mode_line = Qt;
11163 if (buffer_shared > 1)
11164 update_mode_lines++;
11165 }
11166
11167 /* Avoid invocation of point motion hooks by `current_column' below. */
11168 count1 = SPECPDL_INDEX ();
11169 specbind (Qinhibit_point_motion_hooks, Qt);
11170
11171 /* If %c is in the mode line, update it if needed. */
11172 if (!NILP (w->column_number_displayed)
11173 /* This alternative quickly identifies a common case
11174 where no change is needed. */
11175 && !(PT == XFASTINT (w->last_point)
11176 && XFASTINT (w->last_modified) >= MODIFF
11177 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
11178 && (XFASTINT (w->column_number_displayed)
11179 != (int) current_column ())) /* iftc */
11180 w->update_mode_line = Qt;
11181
11182 unbind_to (count1, Qnil);
11183
11184 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
11185
11186 /* The variable buffer_shared is set in redisplay_window and
11187 indicates that we redisplay a buffer in different windows. See
11188 there. */
11189 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
11190 || cursor_type_changed);
11191
11192 /* If specs for an arrow have changed, do thorough redisplay
11193 to ensure we remove any arrow that should no longer exist. */
11194 if (overlay_arrows_changed_p ())
11195 consider_all_windows_p = windows_or_buffers_changed = 1;
11196
11197 /* Normally the message* functions will have already displayed and
11198 updated the echo area, but the frame may have been trashed, or
11199 the update may have been preempted, so display the echo area
11200 again here. Checking message_cleared_p captures the case that
11201 the echo area should be cleared. */
11202 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
11203 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
11204 || (message_cleared_p
11205 && minibuf_level == 0
11206 /* If the mini-window is currently selected, this means the
11207 echo-area doesn't show through. */
11208 && !MINI_WINDOW_P (XWINDOW (selected_window))))
11209 {
11210 int window_height_changed_p = echo_area_display (0);
11211 must_finish = 1;
11212
11213 /* If we don't display the current message, don't clear the
11214 message_cleared_p flag, because, if we did, we wouldn't clear
11215 the echo area in the next redisplay which doesn't preserve
11216 the echo area. */
11217 if (!display_last_displayed_message_p)
11218 message_cleared_p = 0;
11219
11220 if (fonts_changed_p)
11221 goto retry;
11222 else if (window_height_changed_p)
11223 {
11224 consider_all_windows_p = 1;
11225 ++update_mode_lines;
11226 ++windows_or_buffers_changed;
11227
11228 /* If window configuration was changed, frames may have been
11229 marked garbaged. Clear them or we will experience
11230 surprises wrt scrolling. */
11231 if (frame_garbaged)
11232 clear_garbaged_frames ();
11233 }
11234 }
11235 else if (EQ (selected_window, minibuf_window)
11236 && (current_buffer->clip_changed
11237 || XFASTINT (w->last_modified) < MODIFF
11238 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
11239 && resize_mini_window (w, 0))
11240 {
11241 /* Resized active mini-window to fit the size of what it is
11242 showing if its contents might have changed. */
11243 must_finish = 1;
11244 consider_all_windows_p = 1;
11245 ++windows_or_buffers_changed;
11246 ++update_mode_lines;
11247
11248 /* If window configuration was changed, frames may have been
11249 marked garbaged. Clear them or we will experience
11250 surprises wrt scrolling. */
11251 if (frame_garbaged)
11252 clear_garbaged_frames ();
11253 }
11254
11255
11256 /* If showing the region, and mark has changed, we must redisplay
11257 the whole window. The assignment to this_line_start_pos prevents
11258 the optimization directly below this if-statement. */
11259 if (((!NILP (Vtransient_mark_mode)
11260 && !NILP (XBUFFER (w->buffer)->mark_active))
11261 != !NILP (w->region_showing))
11262 || (!NILP (w->region_showing)
11263 && !EQ (w->region_showing,
11264 Fmarker_position (XBUFFER (w->buffer)->mark))))
11265 CHARPOS (this_line_start_pos) = 0;
11266
11267 /* Optimize the case that only the line containing the cursor in the
11268 selected window has changed. Variables starting with this_ are
11269 set in display_line and record information about the line
11270 containing the cursor. */
11271 tlbufpos = this_line_start_pos;
11272 tlendpos = this_line_end_pos;
11273 if (!consider_all_windows_p
11274 && CHARPOS (tlbufpos) > 0
11275 && NILP (w->update_mode_line)
11276 && !current_buffer->clip_changed
11277 && !current_buffer->prevent_redisplay_optimizations_p
11278 && FRAME_VISIBLE_P (XFRAME (w->frame))
11279 && !FRAME_OBSCURED_P (XFRAME (w->frame))
11280 /* Make sure recorded data applies to current buffer, etc. */
11281 && this_line_buffer == current_buffer
11282 && current_buffer == XBUFFER (w->buffer)
11283 && NILP (w->force_start)
11284 && NILP (w->optional_new_start)
11285 /* Point must be on the line that we have info recorded about. */
11286 && PT >= CHARPOS (tlbufpos)
11287 && PT <= Z - CHARPOS (tlendpos)
11288 /* All text outside that line, including its final newline,
11289 must be unchanged */
11290 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
11291 CHARPOS (tlendpos)))
11292 {
11293 if (CHARPOS (tlbufpos) > BEGV
11294 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
11295 && (CHARPOS (tlbufpos) == ZV
11296 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
11297 /* Former continuation line has disappeared by becoming empty */
11298 goto cancel;
11299 else if (XFASTINT (w->last_modified) < MODIFF
11300 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
11301 || MINI_WINDOW_P (w))
11302 {
11303 /* We have to handle the case of continuation around a
11304 wide-column character (See the comment in indent.c around
11305 line 885).
11306
11307 For instance, in the following case:
11308
11309 -------- Insert --------
11310 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
11311 J_I_ ==> J_I_ `^^' are cursors.
11312 ^^ ^^
11313 -------- --------
11314
11315 As we have to redraw the line above, we should goto cancel. */
11316
11317 struct it it;
11318 int line_height_before = this_line_pixel_height;
11319
11320 /* Note that start_display will handle the case that the
11321 line starting at tlbufpos is a continuation lines. */
11322 start_display (&it, w, tlbufpos);
11323
11324 /* Implementation note: It this still necessary? */
11325 if (it.current_x != this_line_start_x)
11326 goto cancel;
11327
11328 TRACE ((stderr, "trying display optimization 1\n"));
11329 w->cursor.vpos = -1;
11330 overlay_arrow_seen = 0;
11331 it.vpos = this_line_vpos;
11332 it.current_y = this_line_y;
11333 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
11334 display_line (&it);
11335
11336 /* If line contains point, is not continued,
11337 and ends at same distance from eob as before, we win */
11338 if (w->cursor.vpos >= 0
11339 /* Line is not continued, otherwise this_line_start_pos
11340 would have been set to 0 in display_line. */
11341 && CHARPOS (this_line_start_pos)
11342 /* Line ends as before. */
11343 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
11344 /* Line has same height as before. Otherwise other lines
11345 would have to be shifted up or down. */
11346 && this_line_pixel_height == line_height_before)
11347 {
11348 /* If this is not the window's last line, we must adjust
11349 the charstarts of the lines below. */
11350 if (it.current_y < it.last_visible_y)
11351 {
11352 struct glyph_row *row
11353 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
11354 int delta, delta_bytes;
11355
11356 if (Z - CHARPOS (tlendpos) == ZV)
11357 {
11358 /* This line ends at end of (accessible part of)
11359 buffer. There is no newline to count. */
11360 delta = (Z
11361 - CHARPOS (tlendpos)
11362 - MATRIX_ROW_START_CHARPOS (row));
11363 delta_bytes = (Z_BYTE
11364 - BYTEPOS (tlendpos)
11365 - MATRIX_ROW_START_BYTEPOS (row));
11366 }
11367 else
11368 {
11369 /* This line ends in a newline. Must take
11370 account of the newline and the rest of the
11371 text that follows. */
11372 delta = (Z
11373 - CHARPOS (tlendpos)
11374 - MATRIX_ROW_START_CHARPOS (row));
11375 delta_bytes = (Z_BYTE
11376 - BYTEPOS (tlendpos)
11377 - MATRIX_ROW_START_BYTEPOS (row));
11378 }
11379
11380 increment_matrix_positions (w->current_matrix,
11381 this_line_vpos + 1,
11382 w->current_matrix->nrows,
11383 delta, delta_bytes);
11384 }
11385
11386 /* If this row displays text now but previously didn't,
11387 or vice versa, w->window_end_vpos may have to be
11388 adjusted. */
11389 if ((it.glyph_row - 1)->displays_text_p)
11390 {
11391 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
11392 XSETINT (w->window_end_vpos, this_line_vpos);
11393 }
11394 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
11395 && this_line_vpos > 0)
11396 XSETINT (w->window_end_vpos, this_line_vpos - 1);
11397 w->window_end_valid = Qnil;
11398
11399 /* Update hint: No need to try to scroll in update_window. */
11400 w->desired_matrix->no_scrolling_p = 1;
11401
11402 #if GLYPH_DEBUG
11403 *w->desired_matrix->method = 0;
11404 debug_method_add (w, "optimization 1");
11405 #endif
11406 #ifdef HAVE_WINDOW_SYSTEM
11407 update_window_fringes (w, 0);
11408 #endif
11409 goto update;
11410 }
11411 else
11412 goto cancel;
11413 }
11414 else if (/* Cursor position hasn't changed. */
11415 PT == XFASTINT (w->last_point)
11416 /* Make sure the cursor was last displayed
11417 in this window. Otherwise we have to reposition it. */
11418 && 0 <= w->cursor.vpos
11419 && WINDOW_TOTAL_LINES (w) > w->cursor.vpos)
11420 {
11421 if (!must_finish)
11422 {
11423 do_pending_window_change (1);
11424
11425 /* We used to always goto end_of_redisplay here, but this
11426 isn't enough if we have a blinking cursor. */
11427 if (w->cursor_off_p == w->last_cursor_off_p)
11428 goto end_of_redisplay;
11429 }
11430 goto update;
11431 }
11432 /* If highlighting the region, or if the cursor is in the echo area,
11433 then we can't just move the cursor. */
11434 else if (! (!NILP (Vtransient_mark_mode)
11435 && !NILP (current_buffer->mark_active))
11436 && (EQ (selected_window, current_buffer->last_selected_window)
11437 || highlight_nonselected_windows)
11438 && NILP (w->region_showing)
11439 && NILP (Vshow_trailing_whitespace)
11440 && !cursor_in_echo_area)
11441 {
11442 struct it it;
11443 struct glyph_row *row;
11444
11445 /* Skip from tlbufpos to PT and see where it is. Note that
11446 PT may be in invisible text. If so, we will end at the
11447 next visible position. */
11448 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
11449 NULL, DEFAULT_FACE_ID);
11450 it.current_x = this_line_start_x;
11451 it.current_y = this_line_y;
11452 it.vpos = this_line_vpos;
11453
11454 /* The call to move_it_to stops in front of PT, but
11455 moves over before-strings. */
11456 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
11457
11458 if (it.vpos == this_line_vpos
11459 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
11460 row->enabled_p))
11461 {
11462 xassert (this_line_vpos == it.vpos);
11463 xassert (this_line_y == it.current_y);
11464 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11465 #if GLYPH_DEBUG
11466 *w->desired_matrix->method = 0;
11467 debug_method_add (w, "optimization 3");
11468 #endif
11469 goto update;
11470 }
11471 else
11472 goto cancel;
11473 }
11474
11475 cancel:
11476 /* Text changed drastically or point moved off of line. */
11477 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
11478 }
11479
11480 CHARPOS (this_line_start_pos) = 0;
11481 consider_all_windows_p |= buffer_shared > 1;
11482 ++clear_face_cache_count;
11483 #ifdef HAVE_WINDOW_SYSTEM
11484 ++clear_image_cache_count;
11485 #endif
11486
11487 /* Build desired matrices, and update the display. If
11488 consider_all_windows_p is non-zero, do it for all windows on all
11489 frames. Otherwise do it for selected_window, only. */
11490
11491 if (consider_all_windows_p)
11492 {
11493 Lisp_Object tail, frame;
11494
11495 FOR_EACH_FRAME (tail, frame)
11496 XFRAME (frame)->updated_p = 0;
11497
11498 /* Recompute # windows showing selected buffer. This will be
11499 incremented each time such a window is displayed. */
11500 buffer_shared = 0;
11501
11502 FOR_EACH_FRAME (tail, frame)
11503 {
11504 struct frame *f = XFRAME (frame);
11505
11506 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
11507 {
11508 if (! EQ (frame, selected_frame))
11509 /* Select the frame, for the sake of frame-local
11510 variables. */
11511 select_frame_for_redisplay (frame);
11512
11513 /* Mark all the scroll bars to be removed; we'll redeem
11514 the ones we want when we redisplay their windows. */
11515 if (FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
11516 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
11517
11518 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
11519 redisplay_windows (FRAME_ROOT_WINDOW (f));
11520
11521 /* Any scroll bars which redisplay_windows should have
11522 nuked should now go away. */
11523 if (FRAME_TERMINAL (f)->judge_scroll_bars_hook)
11524 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
11525
11526 /* If fonts changed, display again. */
11527 /* ??? rms: I suspect it is a mistake to jump all the way
11528 back to retry here. It should just retry this frame. */
11529 if (fonts_changed_p)
11530 goto retry;
11531
11532 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
11533 {
11534 /* See if we have to hscroll. */
11535 if (!f->already_hscrolled_p)
11536 {
11537 f->already_hscrolled_p = 1;
11538 if (hscroll_windows (f->root_window))
11539 goto retry;
11540 }
11541
11542 /* Prevent various kinds of signals during display
11543 update. stdio is not robust about handling
11544 signals, which can cause an apparent I/O
11545 error. */
11546 if (interrupt_input)
11547 unrequest_sigio ();
11548 STOP_POLLING;
11549
11550 /* Update the display. */
11551 set_window_update_flags (XWINDOW (f->root_window), 1);
11552 pause |= update_frame (f, 0, 0);
11553 #if 0 /* Exiting the loop can leave the wrong value for buffer_shared. */
11554 if (pause)
11555 break;
11556 #endif
11557
11558 f->updated_p = 1;
11559 }
11560 }
11561 }
11562
11563 if (!pause)
11564 {
11565 /* Do the mark_window_display_accurate after all windows have
11566 been redisplayed because this call resets flags in buffers
11567 which are needed for proper redisplay. */
11568 FOR_EACH_FRAME (tail, frame)
11569 {
11570 struct frame *f = XFRAME (frame);
11571 if (f->updated_p)
11572 {
11573 mark_window_display_accurate (f->root_window, 1);
11574 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
11575 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
11576 }
11577 }
11578 }
11579 }
11580 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
11581 {
11582 Lisp_Object mini_window;
11583 struct frame *mini_frame;
11584
11585 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
11586 /* Use list_of_error, not Qerror, so that
11587 we catch only errors and don't run the debugger. */
11588 internal_condition_case_1 (redisplay_window_1, selected_window,
11589 list_of_error,
11590 redisplay_window_error);
11591
11592 /* Compare desired and current matrices, perform output. */
11593
11594 update:
11595 /* If fonts changed, display again. */
11596 if (fonts_changed_p)
11597 goto retry;
11598
11599 /* Prevent various kinds of signals during display update.
11600 stdio is not robust about handling signals,
11601 which can cause an apparent I/O error. */
11602 if (interrupt_input)
11603 unrequest_sigio ();
11604 STOP_POLLING;
11605
11606 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
11607 {
11608 if (hscroll_windows (selected_window))
11609 goto retry;
11610
11611 XWINDOW (selected_window)->must_be_updated_p = 1;
11612 pause = update_frame (sf, 0, 0);
11613 }
11614
11615 /* We may have called echo_area_display at the top of this
11616 function. If the echo area is on another frame, that may
11617 have put text on a frame other than the selected one, so the
11618 above call to update_frame would not have caught it. Catch
11619 it here. */
11620 mini_window = FRAME_MINIBUF_WINDOW (sf);
11621 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
11622
11623 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
11624 {
11625 XWINDOW (mini_window)->must_be_updated_p = 1;
11626 pause |= update_frame (mini_frame, 0, 0);
11627 if (!pause && hscroll_windows (mini_window))
11628 goto retry;
11629 }
11630 }
11631
11632 /* If display was paused because of pending input, make sure we do a
11633 thorough update the next time. */
11634 if (pause)
11635 {
11636 /* Prevent the optimization at the beginning of
11637 redisplay_internal that tries a single-line update of the
11638 line containing the cursor in the selected window. */
11639 CHARPOS (this_line_start_pos) = 0;
11640
11641 /* Let the overlay arrow be updated the next time. */
11642 update_overlay_arrows (0);
11643
11644 /* If we pause after scrolling, some rows in the current
11645 matrices of some windows are not valid. */
11646 if (!WINDOW_FULL_WIDTH_P (w)
11647 && !FRAME_WINDOW_P (XFRAME (w->frame)))
11648 update_mode_lines = 1;
11649 }
11650 else
11651 {
11652 if (!consider_all_windows_p)
11653 {
11654 /* This has already been done above if
11655 consider_all_windows_p is set. */
11656 mark_window_display_accurate_1 (w, 1);
11657
11658 /* Say overlay arrows are up to date. */
11659 update_overlay_arrows (1);
11660
11661 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
11662 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
11663 }
11664
11665 update_mode_lines = 0;
11666 windows_or_buffers_changed = 0;
11667 cursor_type_changed = 0;
11668 }
11669
11670 /* Start SIGIO interrupts coming again. Having them off during the
11671 code above makes it less likely one will discard output, but not
11672 impossible, since there might be stuff in the system buffer here.
11673 But it is much hairier to try to do anything about that. */
11674 if (interrupt_input)
11675 request_sigio ();
11676 RESUME_POLLING;
11677
11678 /* If a frame has become visible which was not before, redisplay
11679 again, so that we display it. Expose events for such a frame
11680 (which it gets when becoming visible) don't call the parts of
11681 redisplay constructing glyphs, so simply exposing a frame won't
11682 display anything in this case. So, we have to display these
11683 frames here explicitly. */
11684 if (!pause)
11685 {
11686 Lisp_Object tail, frame;
11687 int new_count = 0;
11688
11689 FOR_EACH_FRAME (tail, frame)
11690 {
11691 int this_is_visible = 0;
11692
11693 if (XFRAME (frame)->visible)
11694 this_is_visible = 1;
11695 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
11696 if (XFRAME (frame)->visible)
11697 this_is_visible = 1;
11698
11699 if (this_is_visible)
11700 new_count++;
11701 }
11702
11703 if (new_count != number_of_visible_frames)
11704 windows_or_buffers_changed++;
11705 }
11706
11707 /* Change frame size now if a change is pending. */
11708 do_pending_window_change (1);
11709
11710 /* If we just did a pending size change, or have additional
11711 visible frames, redisplay again. */
11712 if (windows_or_buffers_changed && !pause)
11713 goto retry;
11714
11715 /* Clear the face cache eventually. */
11716 if (consider_all_windows_p)
11717 {
11718 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
11719 {
11720 clear_face_cache (0);
11721 clear_face_cache_count = 0;
11722 }
11723 #ifdef HAVE_WINDOW_SYSTEM
11724 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
11725 {
11726 Lisp_Object tail, frame;
11727 FOR_EACH_FRAME (tail, frame)
11728 {
11729 struct frame *f = XFRAME (frame);
11730 if (FRAME_WINDOW_P (f))
11731 clear_image_cache (f, 0);
11732 }
11733 clear_image_cache_count = 0;
11734 }
11735 #endif /* HAVE_WINDOW_SYSTEM */
11736 }
11737
11738 end_of_redisplay:
11739 unbind_to (count, Qnil);
11740 RESUME_POLLING;
11741 }
11742
11743
11744 /* Redisplay, but leave alone any recent echo area message unless
11745 another message has been requested in its place.
11746
11747 This is useful in situations where you need to redisplay but no
11748 user action has occurred, making it inappropriate for the message
11749 area to be cleared. See tracking_off and
11750 wait_reading_process_output for examples of these situations.
11751
11752 FROM_WHERE is an integer saying from where this function was
11753 called. This is useful for debugging. */
11754
11755 void
11756 redisplay_preserve_echo_area (from_where)
11757 int from_where;
11758 {
11759 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
11760
11761 if (!NILP (echo_area_buffer[1]))
11762 {
11763 /* We have a previously displayed message, but no current
11764 message. Redisplay the previous message. */
11765 display_last_displayed_message_p = 1;
11766 redisplay_internal (1);
11767 display_last_displayed_message_p = 0;
11768 }
11769 else
11770 redisplay_internal (1);
11771
11772 if (FRAME_RIF (SELECTED_FRAME ()) != NULL
11773 && FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
11774 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (NULL);
11775 }
11776
11777
11778 /* Function registered with record_unwind_protect in
11779 redisplay_internal. Reset redisplaying_p to the value it had
11780 before redisplay_internal was called, and clear
11781 prevent_freeing_realized_faces_p. It also selects the previously
11782 selected frame, unless it has been deleted (by an X connection
11783 failure during redisplay, for example). */
11784
11785 static Lisp_Object
11786 unwind_redisplay (val)
11787 Lisp_Object val;
11788 {
11789 Lisp_Object old_redisplaying_p, old_frame;
11790
11791 old_redisplaying_p = XCAR (val);
11792 redisplaying_p = XFASTINT (old_redisplaying_p);
11793 old_frame = XCDR (val);
11794 if (! EQ (old_frame, selected_frame)
11795 && FRAME_LIVE_P (XFRAME (old_frame)))
11796 select_frame_for_redisplay (old_frame);
11797 return Qnil;
11798 }
11799
11800
11801 /* Mark the display of window W as accurate or inaccurate. If
11802 ACCURATE_P is non-zero mark display of W as accurate. If
11803 ACCURATE_P is zero, arrange for W to be redisplayed the next time
11804 redisplay_internal is called. */
11805
11806 static void
11807 mark_window_display_accurate_1 (w, accurate_p)
11808 struct window *w;
11809 int accurate_p;
11810 {
11811 if (BUFFERP (w->buffer))
11812 {
11813 struct buffer *b = XBUFFER (w->buffer);
11814
11815 w->last_modified
11816 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
11817 w->last_overlay_modified
11818 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
11819 w->last_had_star
11820 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
11821
11822 if (accurate_p)
11823 {
11824 b->clip_changed = 0;
11825 b->prevent_redisplay_optimizations_p = 0;
11826
11827 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
11828 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
11829 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
11830 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
11831
11832 w->current_matrix->buffer = b;
11833 w->current_matrix->begv = BUF_BEGV (b);
11834 w->current_matrix->zv = BUF_ZV (b);
11835
11836 w->last_cursor = w->cursor;
11837 w->last_cursor_off_p = w->cursor_off_p;
11838
11839 if (w == XWINDOW (selected_window))
11840 w->last_point = make_number (BUF_PT (b));
11841 else
11842 w->last_point = make_number (XMARKER (w->pointm)->charpos);
11843 }
11844 }
11845
11846 if (accurate_p)
11847 {
11848 w->window_end_valid = w->buffer;
11849 #if 0 /* This is incorrect with variable-height lines. */
11850 xassert (XINT (w->window_end_vpos)
11851 < (WINDOW_TOTAL_LINES (w)
11852 - (WINDOW_WANTS_MODELINE_P (w) ? 1 : 0)));
11853 #endif
11854 w->update_mode_line = Qnil;
11855 }
11856 }
11857
11858
11859 /* Mark the display of windows in the window tree rooted at WINDOW as
11860 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
11861 windows as accurate. If ACCURATE_P is zero, arrange for windows to
11862 be redisplayed the next time redisplay_internal is called. */
11863
11864 void
11865 mark_window_display_accurate (window, accurate_p)
11866 Lisp_Object window;
11867 int accurate_p;
11868 {
11869 struct window *w;
11870
11871 for (; !NILP (window); window = w->next)
11872 {
11873 w = XWINDOW (window);
11874 mark_window_display_accurate_1 (w, accurate_p);
11875
11876 if (!NILP (w->vchild))
11877 mark_window_display_accurate (w->vchild, accurate_p);
11878 if (!NILP (w->hchild))
11879 mark_window_display_accurate (w->hchild, accurate_p);
11880 }
11881
11882 if (accurate_p)
11883 {
11884 update_overlay_arrows (1);
11885 }
11886 else
11887 {
11888 /* Force a thorough redisplay the next time by setting
11889 last_arrow_position and last_arrow_string to t, which is
11890 unequal to any useful value of Voverlay_arrow_... */
11891 update_overlay_arrows (-1);
11892 }
11893 }
11894
11895
11896 /* Return value in display table DP (Lisp_Char_Table *) for character
11897 C. Since a display table doesn't have any parent, we don't have to
11898 follow parent. Do not call this function directly but use the
11899 macro DISP_CHAR_VECTOR. */
11900
11901 Lisp_Object
11902 disp_char_vector (dp, c)
11903 struct Lisp_Char_Table *dp;
11904 int c;
11905 {
11906 Lisp_Object val;
11907
11908 if (ASCII_CHAR_P (c))
11909 {
11910 val = dp->ascii;
11911 if (SUB_CHAR_TABLE_P (val))
11912 val = XSUB_CHAR_TABLE (val)->contents[c];
11913 }
11914 else
11915 {
11916 Lisp_Object table;
11917
11918 XSETCHAR_TABLE (table, dp);
11919 val = char_table_ref (table, c);
11920 }
11921 if (NILP (val))
11922 val = dp->defalt;
11923 return val;
11924 }
11925
11926
11927 \f
11928 /***********************************************************************
11929 Window Redisplay
11930 ***********************************************************************/
11931
11932 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
11933
11934 static void
11935 redisplay_windows (window)
11936 Lisp_Object window;
11937 {
11938 while (!NILP (window))
11939 {
11940 struct window *w = XWINDOW (window);
11941
11942 if (!NILP (w->hchild))
11943 redisplay_windows (w->hchild);
11944 else if (!NILP (w->vchild))
11945 redisplay_windows (w->vchild);
11946 else
11947 {
11948 displayed_buffer = XBUFFER (w->buffer);
11949 /* Use list_of_error, not Qerror, so that
11950 we catch only errors and don't run the debugger. */
11951 internal_condition_case_1 (redisplay_window_0, window,
11952 list_of_error,
11953 redisplay_window_error);
11954 }
11955
11956 window = w->next;
11957 }
11958 }
11959
11960 static Lisp_Object
11961 redisplay_window_error ()
11962 {
11963 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
11964 return Qnil;
11965 }
11966
11967 static Lisp_Object
11968 redisplay_window_0 (window)
11969 Lisp_Object window;
11970 {
11971 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
11972 redisplay_window (window, 0);
11973 return Qnil;
11974 }
11975
11976 static Lisp_Object
11977 redisplay_window_1 (window)
11978 Lisp_Object window;
11979 {
11980 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
11981 redisplay_window (window, 1);
11982 return Qnil;
11983 }
11984 \f
11985
11986 /* Increment GLYPH until it reaches END or CONDITION fails while
11987 adding (GLYPH)->pixel_width to X. */
11988
11989 #define SKIP_GLYPHS(glyph, end, x, condition) \
11990 do \
11991 { \
11992 (x) += (glyph)->pixel_width; \
11993 ++(glyph); \
11994 } \
11995 while ((glyph) < (end) && (condition))
11996
11997
11998 /* Set cursor position of W. PT is assumed to be displayed in ROW.
11999 DELTA is the number of bytes by which positions recorded in ROW
12000 differ from current buffer positions.
12001
12002 Return 0 if cursor is not on this row. 1 otherwise. */
12003
12004 int
12005 set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
12006 struct window *w;
12007 struct glyph_row *row;
12008 struct glyph_matrix *matrix;
12009 int delta, delta_bytes, dy, dvpos;
12010 {
12011 struct glyph *glyph = row->glyphs[TEXT_AREA];
12012 struct glyph *end = glyph + row->used[TEXT_AREA];
12013 struct glyph *cursor = NULL;
12014 /* The first glyph that starts a sequence of glyphs from string. */
12015 struct glyph *string_start;
12016 /* The X coordinate of string_start. */
12017 int string_start_x;
12018 /* The last known character position. */
12019 int last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
12020 /* The last known character position before string_start. */
12021 int string_before_pos;
12022 int x = row->x;
12023 int cursor_x = x;
12024 int cursor_from_overlay_pos = 0;
12025 int pt_old = PT - delta;
12026
12027 /* Skip over glyphs not having an object at the start of the row.
12028 These are special glyphs like truncation marks on terminal
12029 frames. */
12030 if (row->displays_text_p)
12031 while (glyph < end
12032 && INTEGERP (glyph->object)
12033 && glyph->charpos < 0)
12034 {
12035 x += glyph->pixel_width;
12036 ++glyph;
12037 }
12038
12039 string_start = NULL;
12040 while (glyph < end
12041 && !INTEGERP (glyph->object)
12042 && (!BUFFERP (glyph->object)
12043 || (last_pos = glyph->charpos) < pt_old))
12044 {
12045 if (! STRINGP (glyph->object))
12046 {
12047 string_start = NULL;
12048 x += glyph->pixel_width;
12049 ++glyph;
12050 if (cursor_from_overlay_pos
12051 && last_pos >= cursor_from_overlay_pos)
12052 {
12053 cursor_from_overlay_pos = 0;
12054 cursor = 0;
12055 }
12056 }
12057 else
12058 {
12059 if (string_start == NULL)
12060 {
12061 string_before_pos = last_pos;
12062 string_start = glyph;
12063 string_start_x = x;
12064 }
12065 /* Skip all glyphs from string. */
12066 do
12067 {
12068 Lisp_Object cprop;
12069 int pos;
12070 if ((cursor == NULL || glyph > cursor)
12071 && (cprop = Fget_char_property (make_number ((glyph)->charpos),
12072 Qcursor, (glyph)->object),
12073 !NILP (cprop))
12074 && (pos = string_buffer_position (w, glyph->object,
12075 string_before_pos),
12076 (pos == 0 /* From overlay */
12077 || pos == pt_old)))
12078 {
12079 /* Estimate overlay buffer position from the buffer
12080 positions of the glyphs before and after the overlay.
12081 Add 1 to last_pos so that if point corresponds to the
12082 glyph right after the overlay, we still use a 'cursor'
12083 property found in that overlay. */
12084 cursor_from_overlay_pos = (pos ? 0 : last_pos
12085 + (INTEGERP (cprop) ? XINT (cprop) : 0));
12086 cursor = glyph;
12087 cursor_x = x;
12088 }
12089 x += glyph->pixel_width;
12090 ++glyph;
12091 }
12092 while (glyph < end && EQ (glyph->object, string_start->object));
12093 }
12094 }
12095
12096 if (cursor != NULL)
12097 {
12098 glyph = cursor;
12099 x = cursor_x;
12100 }
12101 else if (row->ends_in_ellipsis_p && glyph == end)
12102 {
12103 /* Scan back over the ellipsis glyphs, decrementing positions. */
12104 while (glyph > row->glyphs[TEXT_AREA]
12105 && (glyph - 1)->charpos == last_pos)
12106 glyph--, x -= glyph->pixel_width;
12107 /* That loop always goes one position too far,
12108 including the glyph before the ellipsis.
12109 So scan forward over that one. */
12110 x += glyph->pixel_width;
12111 glyph++;
12112 }
12113 else if (string_start
12114 && (glyph == end || !BUFFERP (glyph->object) || last_pos > pt_old))
12115 {
12116 /* We may have skipped over point because the previous glyphs
12117 are from string. As there's no easy way to know the
12118 character position of the current glyph, find the correct
12119 glyph on point by scanning from string_start again. */
12120 Lisp_Object limit;
12121 Lisp_Object string;
12122 struct glyph *stop = glyph;
12123 int pos;
12124
12125 limit = make_number (pt_old + 1);
12126 glyph = string_start;
12127 x = string_start_x;
12128 string = glyph->object;
12129 pos = string_buffer_position (w, string, string_before_pos);
12130 /* If STRING is from overlay, LAST_POS == 0. We skip such glyphs
12131 because we always put cursor after overlay strings. */
12132 while (pos == 0 && glyph < stop)
12133 {
12134 string = glyph->object;
12135 SKIP_GLYPHS (glyph, stop, x, EQ (glyph->object, string));
12136 if (glyph < stop)
12137 pos = string_buffer_position (w, glyph->object, string_before_pos);
12138 }
12139
12140 while (glyph < stop)
12141 {
12142 pos = XINT (Fnext_single_char_property_change
12143 (make_number (pos), Qdisplay, Qnil, limit));
12144 if (pos > pt_old)
12145 break;
12146 /* Skip glyphs from the same string. */
12147 string = glyph->object;
12148 SKIP_GLYPHS (glyph, stop, x, EQ (glyph->object, string));
12149 /* Skip glyphs from an overlay. */
12150 while (glyph < stop
12151 && ! string_buffer_position (w, glyph->object, pos))
12152 {
12153 string = glyph->object;
12154 SKIP_GLYPHS (glyph, stop, x, EQ (glyph->object, string));
12155 }
12156 }
12157
12158 /* If we reached the end of the line, and end was from a string,
12159 cursor is not on this line. */
12160 if (glyph == end && row->continued_p)
12161 return 0;
12162 }
12163
12164 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
12165 w->cursor.x = x;
12166 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
12167 w->cursor.y = row->y + dy;
12168
12169 if (w == XWINDOW (selected_window))
12170 {
12171 if (!row->continued_p
12172 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
12173 && row->x == 0)
12174 {
12175 this_line_buffer = XBUFFER (w->buffer);
12176
12177 CHARPOS (this_line_start_pos)
12178 = MATRIX_ROW_START_CHARPOS (row) + delta;
12179 BYTEPOS (this_line_start_pos)
12180 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
12181
12182 CHARPOS (this_line_end_pos)
12183 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
12184 BYTEPOS (this_line_end_pos)
12185 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
12186
12187 this_line_y = w->cursor.y;
12188 this_line_pixel_height = row->height;
12189 this_line_vpos = w->cursor.vpos;
12190 this_line_start_x = row->x;
12191 }
12192 else
12193 CHARPOS (this_line_start_pos) = 0;
12194 }
12195
12196 return 1;
12197 }
12198
12199
12200 /* Run window scroll functions, if any, for WINDOW with new window
12201 start STARTP. Sets the window start of WINDOW to that position.
12202
12203 We assume that the window's buffer is really current. */
12204
12205 static INLINE struct text_pos
12206 run_window_scroll_functions (window, startp)
12207 Lisp_Object window;
12208 struct text_pos startp;
12209 {
12210 struct window *w = XWINDOW (window);
12211 SET_MARKER_FROM_TEXT_POS (w->start, startp);
12212
12213 if (current_buffer != XBUFFER (w->buffer))
12214 abort ();
12215
12216 if (!NILP (Vwindow_scroll_functions))
12217 {
12218 run_hook_with_args_2 (Qwindow_scroll_functions, window,
12219 make_number (CHARPOS (startp)));
12220 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12221 /* In case the hook functions switch buffers. */
12222 if (current_buffer != XBUFFER (w->buffer))
12223 set_buffer_internal_1 (XBUFFER (w->buffer));
12224 }
12225
12226 return startp;
12227 }
12228
12229
12230 /* Make sure the line containing the cursor is fully visible.
12231 A value of 1 means there is nothing to be done.
12232 (Either the line is fully visible, or it cannot be made so,
12233 or we cannot tell.)
12234
12235 If FORCE_P is non-zero, return 0 even if partial visible cursor row
12236 is higher than window.
12237
12238 A value of 0 means the caller should do scrolling
12239 as if point had gone off the screen. */
12240
12241 static int
12242 cursor_row_fully_visible_p (w, force_p, current_matrix_p)
12243 struct window *w;
12244 int force_p;
12245 int current_matrix_p;
12246 {
12247 struct glyph_matrix *matrix;
12248 struct glyph_row *row;
12249 int window_height;
12250
12251 if (!make_cursor_line_fully_visible_p)
12252 return 1;
12253
12254 /* It's not always possible to find the cursor, e.g, when a window
12255 is full of overlay strings. Don't do anything in that case. */
12256 if (w->cursor.vpos < 0)
12257 return 1;
12258
12259 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
12260 row = MATRIX_ROW (matrix, w->cursor.vpos);
12261
12262 /* If the cursor row is not partially visible, there's nothing to do. */
12263 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
12264 return 1;
12265
12266 /* If the row the cursor is in is taller than the window's height,
12267 it's not clear what to do, so do nothing. */
12268 window_height = window_box_height (w);
12269 if (row->height >= window_height)
12270 {
12271 if (!force_p || MINI_WINDOW_P (w)
12272 || w->vscroll || w->cursor.vpos == 0)
12273 return 1;
12274 }
12275 return 0;
12276
12277 #if 0
12278 /* This code used to try to scroll the window just enough to make
12279 the line visible. It returned 0 to say that the caller should
12280 allocate larger glyph matrices. */
12281
12282 if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (w, row))
12283 {
12284 int dy = row->height - row->visible_height;
12285 w->vscroll = 0;
12286 w->cursor.y += dy;
12287 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
12288 }
12289 else /* MATRIX_ROW_PARTIALLY_VISIBLE_AT_BOTTOM_P (w, row)) */
12290 {
12291 int dy = - (row->height - row->visible_height);
12292 w->vscroll = dy;
12293 w->cursor.y += dy;
12294 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
12295 }
12296
12297 /* When we change the cursor y-position of the selected window,
12298 change this_line_y as well so that the display optimization for
12299 the cursor line of the selected window in redisplay_internal uses
12300 the correct y-position. */
12301 if (w == XWINDOW (selected_window))
12302 this_line_y = w->cursor.y;
12303
12304 /* If vscrolling requires a larger glyph matrix, arrange for a fresh
12305 redisplay with larger matrices. */
12306 if (matrix->nrows < required_matrix_height (w))
12307 {
12308 fonts_changed_p = 1;
12309 return 0;
12310 }
12311
12312 return 1;
12313 #endif /* 0 */
12314 }
12315
12316
12317 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
12318 non-zero means only WINDOW is redisplayed in redisplay_internal.
12319 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
12320 in redisplay_window to bring a partially visible line into view in
12321 the case that only the cursor has moved.
12322
12323 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
12324 last screen line's vertical height extends past the end of the screen.
12325
12326 Value is
12327
12328 1 if scrolling succeeded
12329
12330 0 if scrolling didn't find point.
12331
12332 -1 if new fonts have been loaded so that we must interrupt
12333 redisplay, adjust glyph matrices, and try again. */
12334
12335 enum
12336 {
12337 SCROLLING_SUCCESS,
12338 SCROLLING_FAILED,
12339 SCROLLING_NEED_LARGER_MATRICES
12340 };
12341
12342 static int
12343 try_scrolling (window, just_this_one_p, scroll_conservatively,
12344 scroll_step, temp_scroll_step, last_line_misfit)
12345 Lisp_Object window;
12346 int just_this_one_p;
12347 EMACS_INT scroll_conservatively, scroll_step;
12348 int temp_scroll_step;
12349 int last_line_misfit;
12350 {
12351 struct window *w = XWINDOW (window);
12352 struct frame *f = XFRAME (w->frame);
12353 struct text_pos scroll_margin_pos;
12354 struct text_pos pos;
12355 struct text_pos startp;
12356 struct it it;
12357 Lisp_Object window_end;
12358 int this_scroll_margin;
12359 int dy = 0;
12360 int scroll_max;
12361 int rc;
12362 int amount_to_scroll = 0;
12363 Lisp_Object aggressive;
12364 int height;
12365 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
12366
12367 #if GLYPH_DEBUG
12368 debug_method_add (w, "try_scrolling");
12369 #endif
12370
12371 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12372
12373 /* Compute scroll margin height in pixels. We scroll when point is
12374 within this distance from the top or bottom of the window. */
12375 if (scroll_margin > 0)
12376 {
12377 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
12378 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
12379 }
12380 else
12381 this_scroll_margin = 0;
12382
12383 /* Force scroll_conservatively to have a reasonable value so it doesn't
12384 cause an overflow while computing how much to scroll. */
12385 if (scroll_conservatively)
12386 scroll_conservatively = min (scroll_conservatively,
12387 MOST_POSITIVE_FIXNUM / FRAME_LINE_HEIGHT (f));
12388
12389 /* Compute how much we should try to scroll maximally to bring point
12390 into view. */
12391 if (scroll_step || scroll_conservatively || temp_scroll_step)
12392 scroll_max = max (scroll_step,
12393 max (scroll_conservatively, temp_scroll_step));
12394 else if (NUMBERP (current_buffer->scroll_down_aggressively)
12395 || NUMBERP (current_buffer->scroll_up_aggressively))
12396 /* We're trying to scroll because of aggressive scrolling
12397 but no scroll_step is set. Choose an arbitrary one. Maybe
12398 there should be a variable for this. */
12399 scroll_max = 10;
12400 else
12401 scroll_max = 0;
12402 scroll_max *= FRAME_LINE_HEIGHT (f);
12403
12404 /* Decide whether we have to scroll down. Start at the window end
12405 and move this_scroll_margin up to find the position of the scroll
12406 margin. */
12407 window_end = Fwindow_end (window, Qt);
12408
12409 too_near_end:
12410
12411 CHARPOS (scroll_margin_pos) = XINT (window_end);
12412 BYTEPOS (scroll_margin_pos) = CHAR_TO_BYTE (CHARPOS (scroll_margin_pos));
12413
12414 if (this_scroll_margin || extra_scroll_margin_lines)
12415 {
12416 start_display (&it, w, scroll_margin_pos);
12417 if (this_scroll_margin)
12418 move_it_vertically_backward (&it, this_scroll_margin);
12419 if (extra_scroll_margin_lines)
12420 move_it_by_lines (&it, - extra_scroll_margin_lines, 0);
12421 scroll_margin_pos = it.current.pos;
12422 }
12423
12424 if (PT >= CHARPOS (scroll_margin_pos))
12425 {
12426 int y0;
12427
12428 /* Point is in the scroll margin at the bottom of the window, or
12429 below. Compute a new window start that makes point visible. */
12430
12431 /* Compute the distance from the scroll margin to PT.
12432 Give up if the distance is greater than scroll_max. */
12433 start_display (&it, w, scroll_margin_pos);
12434 y0 = it.current_y;
12435 move_it_to (&it, PT, 0, it.last_visible_y, -1,
12436 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
12437
12438 /* To make point visible, we have to move the window start
12439 down so that the line the cursor is in is visible, which
12440 means we have to add in the height of the cursor line. */
12441 dy = line_bottom_y (&it) - y0;
12442
12443 if (dy > scroll_max)
12444 return SCROLLING_FAILED;
12445
12446 /* Move the window start down. If scrolling conservatively,
12447 move it just enough down to make point visible. If
12448 scroll_step is set, move it down by scroll_step. */
12449 start_display (&it, w, startp);
12450
12451 if (scroll_conservatively)
12452 /* Set AMOUNT_TO_SCROLL to at least one line,
12453 and at most scroll_conservatively lines. */
12454 amount_to_scroll
12455 = min (max (dy, FRAME_LINE_HEIGHT (f)),
12456 FRAME_LINE_HEIGHT (f) * scroll_conservatively);
12457 else if (scroll_step || temp_scroll_step)
12458 amount_to_scroll = scroll_max;
12459 else
12460 {
12461 aggressive = current_buffer->scroll_up_aggressively;
12462 height = WINDOW_BOX_TEXT_HEIGHT (w);
12463 if (NUMBERP (aggressive))
12464 {
12465 double float_amount = XFLOATINT (aggressive) * height;
12466 amount_to_scroll = float_amount;
12467 if (amount_to_scroll == 0 && float_amount > 0)
12468 amount_to_scroll = 1;
12469 }
12470 }
12471
12472 if (amount_to_scroll <= 0)
12473 return SCROLLING_FAILED;
12474
12475 /* If moving by amount_to_scroll leaves STARTP unchanged,
12476 move it down one screen line. */
12477
12478 move_it_vertically (&it, amount_to_scroll);
12479 if (CHARPOS (it.current.pos) == CHARPOS (startp))
12480 move_it_by_lines (&it, 1, 1);
12481 startp = it.current.pos;
12482 }
12483 else
12484 {
12485 /* See if point is inside the scroll margin at the top of the
12486 window. */
12487 scroll_margin_pos = startp;
12488 if (this_scroll_margin)
12489 {
12490 start_display (&it, w, startp);
12491 move_it_vertically (&it, this_scroll_margin);
12492 scroll_margin_pos = it.current.pos;
12493 }
12494
12495 if (PT < CHARPOS (scroll_margin_pos))
12496 {
12497 /* Point is in the scroll margin at the top of the window or
12498 above what is displayed in the window. */
12499 int y0;
12500
12501 /* Compute the vertical distance from PT to the scroll
12502 margin position. Give up if distance is greater than
12503 scroll_max. */
12504 SET_TEXT_POS (pos, PT, PT_BYTE);
12505 start_display (&it, w, pos);
12506 y0 = it.current_y;
12507 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
12508 it.last_visible_y, -1,
12509 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
12510 dy = it.current_y - y0;
12511 if (dy > scroll_max)
12512 return SCROLLING_FAILED;
12513
12514 /* Compute new window start. */
12515 start_display (&it, w, startp);
12516
12517 if (scroll_conservatively)
12518 amount_to_scroll
12519 = max (dy, FRAME_LINE_HEIGHT (f) * max (scroll_step, temp_scroll_step));
12520 else if (scroll_step || temp_scroll_step)
12521 amount_to_scroll = scroll_max;
12522 else
12523 {
12524 aggressive = current_buffer->scroll_down_aggressively;
12525 height = WINDOW_BOX_TEXT_HEIGHT (w);
12526 if (NUMBERP (aggressive))
12527 {
12528 double float_amount = XFLOATINT (aggressive) * height;
12529 amount_to_scroll = float_amount;
12530 if (amount_to_scroll == 0 && float_amount > 0)
12531 amount_to_scroll = 1;
12532 }
12533 }
12534
12535 if (amount_to_scroll <= 0)
12536 return SCROLLING_FAILED;
12537
12538 move_it_vertically_backward (&it, amount_to_scroll);
12539 startp = it.current.pos;
12540 }
12541 }
12542
12543 /* Run window scroll functions. */
12544 startp = run_window_scroll_functions (window, startp);
12545
12546 /* Display the window. Give up if new fonts are loaded, or if point
12547 doesn't appear. */
12548 if (!try_window (window, startp, 0))
12549 rc = SCROLLING_NEED_LARGER_MATRICES;
12550 else if (w->cursor.vpos < 0)
12551 {
12552 clear_glyph_matrix (w->desired_matrix);
12553 rc = SCROLLING_FAILED;
12554 }
12555 else
12556 {
12557 /* Maybe forget recorded base line for line number display. */
12558 if (!just_this_one_p
12559 || current_buffer->clip_changed
12560 || BEG_UNCHANGED < CHARPOS (startp))
12561 w->base_line_number = Qnil;
12562
12563 /* If cursor ends up on a partially visible line,
12564 treat that as being off the bottom of the screen. */
12565 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0))
12566 {
12567 clear_glyph_matrix (w->desired_matrix);
12568 ++extra_scroll_margin_lines;
12569 goto too_near_end;
12570 }
12571 rc = SCROLLING_SUCCESS;
12572 }
12573
12574 return rc;
12575 }
12576
12577
12578 /* Compute a suitable window start for window W if display of W starts
12579 on a continuation line. Value is non-zero if a new window start
12580 was computed.
12581
12582 The new window start will be computed, based on W's width, starting
12583 from the start of the continued line. It is the start of the
12584 screen line with the minimum distance from the old start W->start. */
12585
12586 static int
12587 compute_window_start_on_continuation_line (w)
12588 struct window *w;
12589 {
12590 struct text_pos pos, start_pos;
12591 int window_start_changed_p = 0;
12592
12593 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
12594
12595 /* If window start is on a continuation line... Window start may be
12596 < BEGV in case there's invisible text at the start of the
12597 buffer (M-x rmail, for example). */
12598 if (CHARPOS (start_pos) > BEGV
12599 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
12600 {
12601 struct it it;
12602 struct glyph_row *row;
12603
12604 /* Handle the case that the window start is out of range. */
12605 if (CHARPOS (start_pos) < BEGV)
12606 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
12607 else if (CHARPOS (start_pos) > ZV)
12608 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
12609
12610 /* Find the start of the continued line. This should be fast
12611 because scan_buffer is fast (newline cache). */
12612 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
12613 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
12614 row, DEFAULT_FACE_ID);
12615 reseat_at_previous_visible_line_start (&it);
12616
12617 /* If the line start is "too far" away from the window start,
12618 say it takes too much time to compute a new window start. */
12619 if (CHARPOS (start_pos) - IT_CHARPOS (it)
12620 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
12621 {
12622 int min_distance, distance;
12623
12624 /* Move forward by display lines to find the new window
12625 start. If window width was enlarged, the new start can
12626 be expected to be > the old start. If window width was
12627 decreased, the new window start will be < the old start.
12628 So, we're looking for the display line start with the
12629 minimum distance from the old window start. */
12630 pos = it.current.pos;
12631 min_distance = INFINITY;
12632 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
12633 distance < min_distance)
12634 {
12635 min_distance = distance;
12636 pos = it.current.pos;
12637 move_it_by_lines (&it, 1, 0);
12638 }
12639
12640 /* Set the window start there. */
12641 SET_MARKER_FROM_TEXT_POS (w->start, pos);
12642 window_start_changed_p = 1;
12643 }
12644 }
12645
12646 return window_start_changed_p;
12647 }
12648
12649
12650 /* Try cursor movement in case text has not changed in window WINDOW,
12651 with window start STARTP. Value is
12652
12653 CURSOR_MOVEMENT_SUCCESS if successful
12654
12655 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
12656
12657 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
12658 display. *SCROLL_STEP is set to 1, under certain circumstances, if
12659 we want to scroll as if scroll-step were set to 1. See the code.
12660
12661 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
12662 which case we have to abort this redisplay, and adjust matrices
12663 first. */
12664
12665 enum
12666 {
12667 CURSOR_MOVEMENT_SUCCESS,
12668 CURSOR_MOVEMENT_CANNOT_BE_USED,
12669 CURSOR_MOVEMENT_MUST_SCROLL,
12670 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
12671 };
12672
12673 static int
12674 try_cursor_movement (window, startp, scroll_step)
12675 Lisp_Object window;
12676 struct text_pos startp;
12677 int *scroll_step;
12678 {
12679 struct window *w = XWINDOW (window);
12680 struct frame *f = XFRAME (w->frame);
12681 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
12682
12683 #if GLYPH_DEBUG
12684 if (inhibit_try_cursor_movement)
12685 return rc;
12686 #endif
12687
12688 /* Handle case where text has not changed, only point, and it has
12689 not moved off the frame. */
12690 if (/* Point may be in this window. */
12691 PT >= CHARPOS (startp)
12692 /* Selective display hasn't changed. */
12693 && !current_buffer->clip_changed
12694 /* Function force-mode-line-update is used to force a thorough
12695 redisplay. It sets either windows_or_buffers_changed or
12696 update_mode_lines. So don't take a shortcut here for these
12697 cases. */
12698 && !update_mode_lines
12699 && !windows_or_buffers_changed
12700 && !cursor_type_changed
12701 /* Can't use this case if highlighting a region. When a
12702 region exists, cursor movement has to do more than just
12703 set the cursor. */
12704 && !(!NILP (Vtransient_mark_mode)
12705 && !NILP (current_buffer->mark_active))
12706 && NILP (w->region_showing)
12707 && NILP (Vshow_trailing_whitespace)
12708 /* Right after splitting windows, last_point may be nil. */
12709 && INTEGERP (w->last_point)
12710 /* This code is not used for mini-buffer for the sake of the case
12711 of redisplaying to replace an echo area message; since in
12712 that case the mini-buffer contents per se are usually
12713 unchanged. This code is of no real use in the mini-buffer
12714 since the handling of this_line_start_pos, etc., in redisplay
12715 handles the same cases. */
12716 && !EQ (window, minibuf_window)
12717 /* When splitting windows or for new windows, it happens that
12718 redisplay is called with a nil window_end_vpos or one being
12719 larger than the window. This should really be fixed in
12720 window.c. I don't have this on my list, now, so we do
12721 approximately the same as the old redisplay code. --gerd. */
12722 && INTEGERP (w->window_end_vpos)
12723 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
12724 && (FRAME_WINDOW_P (f)
12725 || !overlay_arrow_in_current_buffer_p ()))
12726 {
12727 int this_scroll_margin, top_scroll_margin;
12728 struct glyph_row *row = NULL;
12729
12730 #if GLYPH_DEBUG
12731 debug_method_add (w, "cursor movement");
12732 #endif
12733
12734 /* Scroll if point within this distance from the top or bottom
12735 of the window. This is a pixel value. */
12736 this_scroll_margin = max (0, scroll_margin);
12737 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
12738 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
12739
12740 top_scroll_margin = this_scroll_margin;
12741 if (WINDOW_WANTS_HEADER_LINE_P (w))
12742 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
12743
12744 /* Start with the row the cursor was displayed during the last
12745 not paused redisplay. Give up if that row is not valid. */
12746 if (w->last_cursor.vpos < 0
12747 || w->last_cursor.vpos >= w->current_matrix->nrows)
12748 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12749 else
12750 {
12751 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
12752 if (row->mode_line_p)
12753 ++row;
12754 if (!row->enabled_p)
12755 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12756 }
12757
12758 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
12759 {
12760 int scroll_p = 0;
12761 int last_y = window_text_bottom_y (w) - this_scroll_margin;
12762
12763 if (PT > XFASTINT (w->last_point))
12764 {
12765 /* Point has moved forward. */
12766 while (MATRIX_ROW_END_CHARPOS (row) < PT
12767 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
12768 {
12769 xassert (row->enabled_p);
12770 ++row;
12771 }
12772
12773 /* The end position of a row equals the start position
12774 of the next row. If PT is there, we would rather
12775 display it in the next line. */
12776 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
12777 && MATRIX_ROW_END_CHARPOS (row) == PT
12778 && !cursor_row_p (w, row))
12779 ++row;
12780
12781 /* If within the scroll margin, scroll. Note that
12782 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
12783 the next line would be drawn, and that
12784 this_scroll_margin can be zero. */
12785 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
12786 || PT > MATRIX_ROW_END_CHARPOS (row)
12787 /* Line is completely visible last line in window
12788 and PT is to be set in the next line. */
12789 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
12790 && PT == MATRIX_ROW_END_CHARPOS (row)
12791 && !row->ends_at_zv_p
12792 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
12793 scroll_p = 1;
12794 }
12795 else if (PT < XFASTINT (w->last_point))
12796 {
12797 /* Cursor has to be moved backward. Note that PT >=
12798 CHARPOS (startp) because of the outer if-statement. */
12799 while (!row->mode_line_p
12800 && (MATRIX_ROW_START_CHARPOS (row) > PT
12801 || (MATRIX_ROW_START_CHARPOS (row) == PT
12802 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
12803 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
12804 row > w->current_matrix->rows
12805 && (row-1)->ends_in_newline_from_string_p))))
12806 && (row->y > top_scroll_margin
12807 || CHARPOS (startp) == BEGV))
12808 {
12809 xassert (row->enabled_p);
12810 --row;
12811 }
12812
12813 /* Consider the following case: Window starts at BEGV,
12814 there is invisible, intangible text at BEGV, so that
12815 display starts at some point START > BEGV. It can
12816 happen that we are called with PT somewhere between
12817 BEGV and START. Try to handle that case. */
12818 if (row < w->current_matrix->rows
12819 || row->mode_line_p)
12820 {
12821 row = w->current_matrix->rows;
12822 if (row->mode_line_p)
12823 ++row;
12824 }
12825
12826 /* Due to newlines in overlay strings, we may have to
12827 skip forward over overlay strings. */
12828 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
12829 && MATRIX_ROW_END_CHARPOS (row) == PT
12830 && !cursor_row_p (w, row))
12831 ++row;
12832
12833 /* If within the scroll margin, scroll. */
12834 if (row->y < top_scroll_margin
12835 && CHARPOS (startp) != BEGV)
12836 scroll_p = 1;
12837 }
12838 else
12839 {
12840 /* Cursor did not move. So don't scroll even if cursor line
12841 is partially visible, as it was so before. */
12842 rc = CURSOR_MOVEMENT_SUCCESS;
12843 }
12844
12845 if (PT < MATRIX_ROW_START_CHARPOS (row)
12846 || PT > MATRIX_ROW_END_CHARPOS (row))
12847 {
12848 /* if PT is not in the glyph row, give up. */
12849 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12850 }
12851 else if (rc != CURSOR_MOVEMENT_SUCCESS
12852 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
12853 && make_cursor_line_fully_visible_p)
12854 {
12855 if (PT == MATRIX_ROW_END_CHARPOS (row)
12856 && !row->ends_at_zv_p
12857 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
12858 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12859 else if (row->height > window_box_height (w))
12860 {
12861 /* If we end up in a partially visible line, let's
12862 make it fully visible, except when it's taller
12863 than the window, in which case we can't do much
12864 about it. */
12865 *scroll_step = 1;
12866 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12867 }
12868 else
12869 {
12870 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
12871 if (!cursor_row_fully_visible_p (w, 0, 1))
12872 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12873 else
12874 rc = CURSOR_MOVEMENT_SUCCESS;
12875 }
12876 }
12877 else if (scroll_p)
12878 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12879 else
12880 {
12881 do
12882 {
12883 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
12884 {
12885 rc = CURSOR_MOVEMENT_SUCCESS;
12886 break;
12887 }
12888 ++row;
12889 }
12890 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
12891 && MATRIX_ROW_START_CHARPOS (row) == PT
12892 && cursor_row_p (w, row));
12893 }
12894 }
12895 }
12896
12897 return rc;
12898 }
12899
12900 void
12901 set_vertical_scroll_bar (w)
12902 struct window *w;
12903 {
12904 int start, end, whole;
12905
12906 /* Calculate the start and end positions for the current window.
12907 At some point, it would be nice to choose between scrollbars
12908 which reflect the whole buffer size, with special markers
12909 indicating narrowing, and scrollbars which reflect only the
12910 visible region.
12911
12912 Note that mini-buffers sometimes aren't displaying any text. */
12913 if (!MINI_WINDOW_P (w)
12914 || (w == XWINDOW (minibuf_window)
12915 && NILP (echo_area_buffer[0])))
12916 {
12917 struct buffer *buf = XBUFFER (w->buffer);
12918 whole = BUF_ZV (buf) - BUF_BEGV (buf);
12919 start = marker_position (w->start) - BUF_BEGV (buf);
12920 /* I don't think this is guaranteed to be right. For the
12921 moment, we'll pretend it is. */
12922 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
12923
12924 if (end < start)
12925 end = start;
12926 if (whole < (end - start))
12927 whole = end - start;
12928 }
12929 else
12930 start = end = whole = 0;
12931
12932 /* Indicate what this scroll bar ought to be displaying now. */
12933 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
12934 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
12935 (w, end - start, whole, start);
12936 }
12937
12938
12939 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
12940 selected_window is redisplayed.
12941
12942 We can return without actually redisplaying the window if
12943 fonts_changed_p is nonzero. In that case, redisplay_internal will
12944 retry. */
12945
12946 static void
12947 redisplay_window (window, just_this_one_p)
12948 Lisp_Object window;
12949 int just_this_one_p;
12950 {
12951 struct window *w = XWINDOW (window);
12952 struct frame *f = XFRAME (w->frame);
12953 struct buffer *buffer = XBUFFER (w->buffer);
12954 struct buffer *old = current_buffer;
12955 struct text_pos lpoint, opoint, startp;
12956 int update_mode_line;
12957 int tem;
12958 struct it it;
12959 /* Record it now because it's overwritten. */
12960 int current_matrix_up_to_date_p = 0;
12961 int used_current_matrix_p = 0;
12962 /* This is less strict than current_matrix_up_to_date_p.
12963 It indictes that the buffer contents and narrowing are unchanged. */
12964 int buffer_unchanged_p = 0;
12965 int temp_scroll_step = 0;
12966 int count = SPECPDL_INDEX ();
12967 int rc;
12968 int centering_position = -1;
12969 int last_line_misfit = 0;
12970 int beg_unchanged, end_unchanged;
12971
12972 SET_TEXT_POS (lpoint, PT, PT_BYTE);
12973 opoint = lpoint;
12974
12975 /* W must be a leaf window here. */
12976 xassert (!NILP (w->buffer));
12977 #if GLYPH_DEBUG
12978 *w->desired_matrix->method = 0;
12979 #endif
12980
12981 specbind (Qinhibit_point_motion_hooks, Qt);
12982
12983 reconsider_clip_changes (w, buffer);
12984
12985 /* Has the mode line to be updated? */
12986 update_mode_line = (!NILP (w->update_mode_line)
12987 || update_mode_lines
12988 || buffer->clip_changed
12989 || buffer->prevent_redisplay_optimizations_p);
12990
12991 if (MINI_WINDOW_P (w))
12992 {
12993 if (w == XWINDOW (echo_area_window)
12994 && !NILP (echo_area_buffer[0]))
12995 {
12996 if (update_mode_line)
12997 /* We may have to update a tty frame's menu bar or a
12998 tool-bar. Example `M-x C-h C-h C-g'. */
12999 goto finish_menu_bars;
13000 else
13001 /* We've already displayed the echo area glyphs in this window. */
13002 goto finish_scroll_bars;
13003 }
13004 else if ((w != XWINDOW (minibuf_window)
13005 || minibuf_level == 0)
13006 /* When buffer is nonempty, redisplay window normally. */
13007 && BUF_Z (XBUFFER (w->buffer)) == BUF_BEG (XBUFFER (w->buffer))
13008 /* Quail displays non-mini buffers in minibuffer window.
13009 In that case, redisplay the window normally. */
13010 && !NILP (Fmemq (w->buffer, Vminibuffer_list)))
13011 {
13012 /* W is a mini-buffer window, but it's not active, so clear
13013 it. */
13014 int yb = window_text_bottom_y (w);
13015 struct glyph_row *row;
13016 int y;
13017
13018 for (y = 0, row = w->desired_matrix->rows;
13019 y < yb;
13020 y += row->height, ++row)
13021 blank_row (w, row, y);
13022 goto finish_scroll_bars;
13023 }
13024
13025 clear_glyph_matrix (w->desired_matrix);
13026 }
13027
13028 /* Otherwise set up data on this window; select its buffer and point
13029 value. */
13030 /* Really select the buffer, for the sake of buffer-local
13031 variables. */
13032 set_buffer_internal_1 (XBUFFER (w->buffer));
13033 SET_TEXT_POS (opoint, PT, PT_BYTE);
13034
13035 beg_unchanged = BEG_UNCHANGED;
13036 end_unchanged = END_UNCHANGED;
13037
13038 current_matrix_up_to_date_p
13039 = (!NILP (w->window_end_valid)
13040 && !current_buffer->clip_changed
13041 && !current_buffer->prevent_redisplay_optimizations_p
13042 && XFASTINT (w->last_modified) >= MODIFF
13043 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
13044
13045 buffer_unchanged_p
13046 = (!NILP (w->window_end_valid)
13047 && !current_buffer->clip_changed
13048 && XFASTINT (w->last_modified) >= MODIFF
13049 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
13050
13051 /* When windows_or_buffers_changed is non-zero, we can't rely on
13052 the window end being valid, so set it to nil there. */
13053 if (windows_or_buffers_changed)
13054 {
13055 /* If window starts on a continuation line, maybe adjust the
13056 window start in case the window's width changed. */
13057 if (XMARKER (w->start)->buffer == current_buffer)
13058 compute_window_start_on_continuation_line (w);
13059
13060 w->window_end_valid = Qnil;
13061 }
13062
13063 /* Some sanity checks. */
13064 CHECK_WINDOW_END (w);
13065 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
13066 abort ();
13067 if (BYTEPOS (opoint) < CHARPOS (opoint))
13068 abort ();
13069
13070 /* If %c is in mode line, update it if needed. */
13071 if (!NILP (w->column_number_displayed)
13072 /* This alternative quickly identifies a common case
13073 where no change is needed. */
13074 && !(PT == XFASTINT (w->last_point)
13075 && XFASTINT (w->last_modified) >= MODIFF
13076 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
13077 && (XFASTINT (w->column_number_displayed)
13078 != (int) current_column ())) /* iftc */
13079 update_mode_line = 1;
13080
13081 /* Count number of windows showing the selected buffer. An indirect
13082 buffer counts as its base buffer. */
13083 if (!just_this_one_p)
13084 {
13085 struct buffer *current_base, *window_base;
13086 current_base = current_buffer;
13087 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
13088 if (current_base->base_buffer)
13089 current_base = current_base->base_buffer;
13090 if (window_base->base_buffer)
13091 window_base = window_base->base_buffer;
13092 if (current_base == window_base)
13093 buffer_shared++;
13094 }
13095
13096 /* Point refers normally to the selected window. For any other
13097 window, set up appropriate value. */
13098 if (!EQ (window, selected_window))
13099 {
13100 int new_pt = XMARKER (w->pointm)->charpos;
13101 int new_pt_byte = marker_byte_position (w->pointm);
13102 if (new_pt < BEGV)
13103 {
13104 new_pt = BEGV;
13105 new_pt_byte = BEGV_BYTE;
13106 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
13107 }
13108 else if (new_pt > (ZV - 1))
13109 {
13110 new_pt = ZV;
13111 new_pt_byte = ZV_BYTE;
13112 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
13113 }
13114
13115 /* We don't use SET_PT so that the point-motion hooks don't run. */
13116 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
13117 }
13118
13119 /* If any of the character widths specified in the display table
13120 have changed, invalidate the width run cache. It's true that
13121 this may be a bit late to catch such changes, but the rest of
13122 redisplay goes (non-fatally) haywire when the display table is
13123 changed, so why should we worry about doing any better? */
13124 if (current_buffer->width_run_cache)
13125 {
13126 struct Lisp_Char_Table *disptab = buffer_display_table ();
13127
13128 if (! disptab_matches_widthtab (disptab,
13129 XVECTOR (current_buffer->width_table)))
13130 {
13131 invalidate_region_cache (current_buffer,
13132 current_buffer->width_run_cache,
13133 BEG, Z);
13134 recompute_width_table (current_buffer, disptab);
13135 }
13136 }
13137
13138 /* If window-start is screwed up, choose a new one. */
13139 if (XMARKER (w->start)->buffer != current_buffer)
13140 goto recenter;
13141
13142 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13143
13144 /* If someone specified a new starting point but did not insist,
13145 check whether it can be used. */
13146 if (!NILP (w->optional_new_start)
13147 && CHARPOS (startp) >= BEGV
13148 && CHARPOS (startp) <= ZV)
13149 {
13150 w->optional_new_start = Qnil;
13151 start_display (&it, w, startp);
13152 move_it_to (&it, PT, 0, it.last_visible_y, -1,
13153 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
13154 if (IT_CHARPOS (it) == PT)
13155 w->force_start = Qt;
13156 /* IT may overshoot PT if text at PT is invisible. */
13157 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
13158 w->force_start = Qt;
13159 }
13160
13161 force_start:
13162
13163 /* Handle case where place to start displaying has been specified,
13164 unless the specified location is outside the accessible range. */
13165 if (!NILP (w->force_start)
13166 || w->frozen_window_start_p)
13167 {
13168 /* We set this later on if we have to adjust point. */
13169 int new_vpos = -1;
13170 int val;
13171
13172 w->force_start = Qnil;
13173 w->vscroll = 0;
13174 w->window_end_valid = Qnil;
13175
13176 /* Forget any recorded base line for line number display. */
13177 if (!buffer_unchanged_p)
13178 w->base_line_number = Qnil;
13179
13180 /* Redisplay the mode line. Select the buffer properly for that.
13181 Also, run the hook window-scroll-functions
13182 because we have scrolled. */
13183 /* Note, we do this after clearing force_start because
13184 if there's an error, it is better to forget about force_start
13185 than to get into an infinite loop calling the hook functions
13186 and having them get more errors. */
13187 if (!update_mode_line
13188 || ! NILP (Vwindow_scroll_functions))
13189 {
13190 update_mode_line = 1;
13191 w->update_mode_line = Qt;
13192 startp = run_window_scroll_functions (window, startp);
13193 }
13194
13195 w->last_modified = make_number (0);
13196 w->last_overlay_modified = make_number (0);
13197 if (CHARPOS (startp) < BEGV)
13198 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
13199 else if (CHARPOS (startp) > ZV)
13200 SET_TEXT_POS (startp, ZV, ZV_BYTE);
13201
13202 /* Redisplay, then check if cursor has been set during the
13203 redisplay. Give up if new fonts were loaded. */
13204 val = try_window (window, startp, 1);
13205 if (!val)
13206 {
13207 w->force_start = Qt;
13208 clear_glyph_matrix (w->desired_matrix);
13209 goto need_larger_matrices;
13210 }
13211 /* Point was outside the scroll margins. */
13212 if (val < 0)
13213 new_vpos = window_box_height (w) / 2;
13214
13215 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
13216 {
13217 /* If point does not appear, try to move point so it does
13218 appear. The desired matrix has been built above, so we
13219 can use it here. */
13220 new_vpos = window_box_height (w) / 2;
13221 }
13222
13223 if (!cursor_row_fully_visible_p (w, 0, 0))
13224 {
13225 /* Point does appear, but on a line partly visible at end of window.
13226 Move it back to a fully-visible line. */
13227 new_vpos = window_box_height (w);
13228 }
13229
13230 /* If we need to move point for either of the above reasons,
13231 now actually do it. */
13232 if (new_vpos >= 0)
13233 {
13234 struct glyph_row *row;
13235
13236 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
13237 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
13238 ++row;
13239
13240 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
13241 MATRIX_ROW_START_BYTEPOS (row));
13242
13243 if (w != XWINDOW (selected_window))
13244 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
13245 else if (current_buffer == old)
13246 SET_TEXT_POS (lpoint, PT, PT_BYTE);
13247
13248 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
13249
13250 /* If we are highlighting the region, then we just changed
13251 the region, so redisplay to show it. */
13252 if (!NILP (Vtransient_mark_mode)
13253 && !NILP (current_buffer->mark_active))
13254 {
13255 clear_glyph_matrix (w->desired_matrix);
13256 if (!try_window (window, startp, 0))
13257 goto need_larger_matrices;
13258 }
13259 }
13260
13261 #if GLYPH_DEBUG
13262 debug_method_add (w, "forced window start");
13263 #endif
13264 goto done;
13265 }
13266
13267 /* Handle case where text has not changed, only point, and it has
13268 not moved off the frame, and we are not retrying after hscroll.
13269 (current_matrix_up_to_date_p is nonzero when retrying.) */
13270 if (current_matrix_up_to_date_p
13271 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
13272 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
13273 {
13274 switch (rc)
13275 {
13276 case CURSOR_MOVEMENT_SUCCESS:
13277 used_current_matrix_p = 1;
13278 goto done;
13279
13280 #if 0 /* try_cursor_movement never returns this value. */
13281 case CURSOR_MOVEMENT_NEED_LARGER_MATRICES:
13282 goto need_larger_matrices;
13283 #endif
13284
13285 case CURSOR_MOVEMENT_MUST_SCROLL:
13286 goto try_to_scroll;
13287
13288 default:
13289 abort ();
13290 }
13291 }
13292 /* If current starting point was originally the beginning of a line
13293 but no longer is, find a new starting point. */
13294 else if (!NILP (w->start_at_line_beg)
13295 && !(CHARPOS (startp) <= BEGV
13296 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
13297 {
13298 #if GLYPH_DEBUG
13299 debug_method_add (w, "recenter 1");
13300 #endif
13301 goto recenter;
13302 }
13303
13304 /* Try scrolling with try_window_id. Value is > 0 if update has
13305 been done, it is -1 if we know that the same window start will
13306 not work. It is 0 if unsuccessful for some other reason. */
13307 else if ((tem = try_window_id (w)) != 0)
13308 {
13309 #if GLYPH_DEBUG
13310 debug_method_add (w, "try_window_id %d", tem);
13311 #endif
13312
13313 if (fonts_changed_p)
13314 goto need_larger_matrices;
13315 if (tem > 0)
13316 goto done;
13317
13318 /* Otherwise try_window_id has returned -1 which means that we
13319 don't want the alternative below this comment to execute. */
13320 }
13321 else if (CHARPOS (startp) >= BEGV
13322 && CHARPOS (startp) <= ZV
13323 && PT >= CHARPOS (startp)
13324 && (CHARPOS (startp) < ZV
13325 /* Avoid starting at end of buffer. */
13326 || CHARPOS (startp) == BEGV
13327 || (XFASTINT (w->last_modified) >= MODIFF
13328 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
13329 {
13330
13331 /* If first window line is a continuation line, and window start
13332 is inside the modified region, but the first change is before
13333 current window start, we must select a new window start.
13334
13335 However, if this is the result of a down-mouse event (e.g. by
13336 extending the mouse-drag-overlay), we don't want to select a
13337 new window start, since that would change the position under
13338 the mouse, resulting in an unwanted mouse-movement rather
13339 than a simple mouse-click. */
13340 if (NILP (w->start_at_line_beg)
13341 && NILP (do_mouse_tracking)
13342 && CHARPOS (startp) > BEGV
13343 && CHARPOS (startp) > BEG + beg_unchanged
13344 && CHARPOS (startp) <= Z - end_unchanged)
13345 {
13346 w->force_start = Qt;
13347 if (XMARKER (w->start)->buffer == current_buffer)
13348 compute_window_start_on_continuation_line (w);
13349 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13350 goto force_start;
13351 }
13352
13353 #if GLYPH_DEBUG
13354 debug_method_add (w, "same window start");
13355 #endif
13356
13357 /* Try to redisplay starting at same place as before.
13358 If point has not moved off frame, accept the results. */
13359 if (!current_matrix_up_to_date_p
13360 /* Don't use try_window_reusing_current_matrix in this case
13361 because a window scroll function can have changed the
13362 buffer. */
13363 || !NILP (Vwindow_scroll_functions)
13364 || MINI_WINDOW_P (w)
13365 || !(used_current_matrix_p
13366 = try_window_reusing_current_matrix (w)))
13367 {
13368 IF_DEBUG (debug_method_add (w, "1"));
13369 if (try_window (window, startp, 1) < 0)
13370 /* -1 means we need to scroll.
13371 0 means we need new matrices, but fonts_changed_p
13372 is set in that case, so we will detect it below. */
13373 goto try_to_scroll;
13374 }
13375
13376 if (fonts_changed_p)
13377 goto need_larger_matrices;
13378
13379 if (w->cursor.vpos >= 0)
13380 {
13381 if (!just_this_one_p
13382 || current_buffer->clip_changed
13383 || BEG_UNCHANGED < CHARPOS (startp))
13384 /* Forget any recorded base line for line number display. */
13385 w->base_line_number = Qnil;
13386
13387 if (!cursor_row_fully_visible_p (w, 1, 0))
13388 {
13389 clear_glyph_matrix (w->desired_matrix);
13390 last_line_misfit = 1;
13391 }
13392 /* Drop through and scroll. */
13393 else
13394 goto done;
13395 }
13396 else
13397 clear_glyph_matrix (w->desired_matrix);
13398 }
13399
13400 try_to_scroll:
13401
13402 w->last_modified = make_number (0);
13403 w->last_overlay_modified = make_number (0);
13404
13405 /* Redisplay the mode line. Select the buffer properly for that. */
13406 if (!update_mode_line)
13407 {
13408 update_mode_line = 1;
13409 w->update_mode_line = Qt;
13410 }
13411
13412 /* Try to scroll by specified few lines. */
13413 if ((scroll_conservatively
13414 || scroll_step
13415 || temp_scroll_step
13416 || NUMBERP (current_buffer->scroll_up_aggressively)
13417 || NUMBERP (current_buffer->scroll_down_aggressively))
13418 && !current_buffer->clip_changed
13419 && CHARPOS (startp) >= BEGV
13420 && CHARPOS (startp) <= ZV)
13421 {
13422 /* The function returns -1 if new fonts were loaded, 1 if
13423 successful, 0 if not successful. */
13424 int rc = try_scrolling (window, just_this_one_p,
13425 scroll_conservatively,
13426 scroll_step,
13427 temp_scroll_step, last_line_misfit);
13428 switch (rc)
13429 {
13430 case SCROLLING_SUCCESS:
13431 goto done;
13432
13433 case SCROLLING_NEED_LARGER_MATRICES:
13434 goto need_larger_matrices;
13435
13436 case SCROLLING_FAILED:
13437 break;
13438
13439 default:
13440 abort ();
13441 }
13442 }
13443
13444 /* Finally, just choose place to start which centers point */
13445
13446 recenter:
13447 if (centering_position < 0)
13448 centering_position = window_box_height (w) / 2;
13449
13450 #if GLYPH_DEBUG
13451 debug_method_add (w, "recenter");
13452 #endif
13453
13454 /* w->vscroll = 0; */
13455
13456 /* Forget any previously recorded base line for line number display. */
13457 if (!buffer_unchanged_p)
13458 w->base_line_number = Qnil;
13459
13460 /* Move backward half the height of the window. */
13461 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
13462 it.current_y = it.last_visible_y;
13463 move_it_vertically_backward (&it, centering_position);
13464 xassert (IT_CHARPOS (it) >= BEGV);
13465
13466 /* The function move_it_vertically_backward may move over more
13467 than the specified y-distance. If it->w is small, e.g. a
13468 mini-buffer window, we may end up in front of the window's
13469 display area. Start displaying at the start of the line
13470 containing PT in this case. */
13471 if (it.current_y <= 0)
13472 {
13473 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
13474 move_it_vertically_backward (&it, 0);
13475 #if 0
13476 /* I think this assert is bogus if buffer contains
13477 invisible text or images. KFS. */
13478 xassert (IT_CHARPOS (it) <= PT);
13479 #endif
13480 it.current_y = 0;
13481 }
13482
13483 it.current_x = it.hpos = 0;
13484
13485 /* Set startp here explicitly in case that helps avoid an infinite loop
13486 in case the window-scroll-functions functions get errors. */
13487 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
13488
13489 /* Run scroll hooks. */
13490 startp = run_window_scroll_functions (window, it.current.pos);
13491
13492 /* Redisplay the window. */
13493 if (!current_matrix_up_to_date_p
13494 || windows_or_buffers_changed
13495 || cursor_type_changed
13496 /* Don't use try_window_reusing_current_matrix in this case
13497 because it can have changed the buffer. */
13498 || !NILP (Vwindow_scroll_functions)
13499 || !just_this_one_p
13500 || MINI_WINDOW_P (w)
13501 || !(used_current_matrix_p
13502 = try_window_reusing_current_matrix (w)))
13503 try_window (window, startp, 0);
13504
13505 /* If new fonts have been loaded (due to fontsets), give up. We
13506 have to start a new redisplay since we need to re-adjust glyph
13507 matrices. */
13508 if (fonts_changed_p)
13509 goto need_larger_matrices;
13510
13511 /* If cursor did not appear assume that the middle of the window is
13512 in the first line of the window. Do it again with the next line.
13513 (Imagine a window of height 100, displaying two lines of height
13514 60. Moving back 50 from it->last_visible_y will end in the first
13515 line.) */
13516 if (w->cursor.vpos < 0)
13517 {
13518 if (!NILP (w->window_end_valid)
13519 && PT >= Z - XFASTINT (w->window_end_pos))
13520 {
13521 clear_glyph_matrix (w->desired_matrix);
13522 move_it_by_lines (&it, 1, 0);
13523 try_window (window, it.current.pos, 0);
13524 }
13525 else if (PT < IT_CHARPOS (it))
13526 {
13527 clear_glyph_matrix (w->desired_matrix);
13528 move_it_by_lines (&it, -1, 0);
13529 try_window (window, it.current.pos, 0);
13530 }
13531 else
13532 {
13533 /* Not much we can do about it. */
13534 }
13535 }
13536
13537 /* Consider the following case: Window starts at BEGV, there is
13538 invisible, intangible text at BEGV, so that display starts at
13539 some point START > BEGV. It can happen that we are called with
13540 PT somewhere between BEGV and START. Try to handle that case. */
13541 if (w->cursor.vpos < 0)
13542 {
13543 struct glyph_row *row = w->current_matrix->rows;
13544 if (row->mode_line_p)
13545 ++row;
13546 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13547 }
13548
13549 if (!cursor_row_fully_visible_p (w, 0, 0))
13550 {
13551 /* If vscroll is enabled, disable it and try again. */
13552 if (w->vscroll)
13553 {
13554 w->vscroll = 0;
13555 clear_glyph_matrix (w->desired_matrix);
13556 goto recenter;
13557 }
13558
13559 /* If centering point failed to make the whole line visible,
13560 put point at the top instead. That has to make the whole line
13561 visible, if it can be done. */
13562 if (centering_position == 0)
13563 goto done;
13564
13565 clear_glyph_matrix (w->desired_matrix);
13566 centering_position = 0;
13567 goto recenter;
13568 }
13569
13570 done:
13571
13572 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13573 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
13574 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
13575 ? Qt : Qnil);
13576
13577 /* Display the mode line, if we must. */
13578 if ((update_mode_line
13579 /* If window not full width, must redo its mode line
13580 if (a) the window to its side is being redone and
13581 (b) we do a frame-based redisplay. This is a consequence
13582 of how inverted lines are drawn in frame-based redisplay. */
13583 || (!just_this_one_p
13584 && !FRAME_WINDOW_P (f)
13585 && !WINDOW_FULL_WIDTH_P (w))
13586 /* Line number to display. */
13587 || INTEGERP (w->base_line_pos)
13588 /* Column number is displayed and different from the one displayed. */
13589 || (!NILP (w->column_number_displayed)
13590 && (XFASTINT (w->column_number_displayed)
13591 != (int) current_column ()))) /* iftc */
13592 /* This means that the window has a mode line. */
13593 && (WINDOW_WANTS_MODELINE_P (w)
13594 || WINDOW_WANTS_HEADER_LINE_P (w)))
13595 {
13596 display_mode_lines (w);
13597
13598 /* If mode line height has changed, arrange for a thorough
13599 immediate redisplay using the correct mode line height. */
13600 if (WINDOW_WANTS_MODELINE_P (w)
13601 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
13602 {
13603 fonts_changed_p = 1;
13604 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
13605 = DESIRED_MODE_LINE_HEIGHT (w);
13606 }
13607
13608 /* If top line height has changed, arrange for a thorough
13609 immediate redisplay using the correct mode line height. */
13610 if (WINDOW_WANTS_HEADER_LINE_P (w)
13611 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
13612 {
13613 fonts_changed_p = 1;
13614 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
13615 = DESIRED_HEADER_LINE_HEIGHT (w);
13616 }
13617
13618 if (fonts_changed_p)
13619 goto need_larger_matrices;
13620 }
13621
13622 if (!line_number_displayed
13623 && !BUFFERP (w->base_line_pos))
13624 {
13625 w->base_line_pos = Qnil;
13626 w->base_line_number = Qnil;
13627 }
13628
13629 finish_menu_bars:
13630
13631 /* When we reach a frame's selected window, redo the frame's menu bar. */
13632 if (update_mode_line
13633 && EQ (FRAME_SELECTED_WINDOW (f), window))
13634 {
13635 int redisplay_menu_p = 0;
13636 int redisplay_tool_bar_p = 0;
13637
13638 if (FRAME_WINDOW_P (f))
13639 {
13640 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
13641 || defined (USE_GTK)
13642 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
13643 #else
13644 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
13645 #endif
13646 }
13647 else
13648 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
13649
13650 if (redisplay_menu_p)
13651 display_menu_bar (w);
13652
13653 #ifdef HAVE_WINDOW_SYSTEM
13654 if (FRAME_WINDOW_P (f))
13655 {
13656 #if defined (USE_GTK) || USE_MAC_TOOLBAR
13657 redisplay_tool_bar_p = FRAME_EXTERNAL_TOOL_BAR (f);
13658 #else
13659 redisplay_tool_bar_p = WINDOWP (f->tool_bar_window)
13660 && (FRAME_TOOL_BAR_LINES (f) > 0
13661 || !NILP (Vauto_resize_tool_bars));
13662 #endif
13663
13664 if (redisplay_tool_bar_p && redisplay_tool_bar (f))
13665 {
13666 extern int ignore_mouse_drag_p;
13667 ignore_mouse_drag_p = 1;
13668 }
13669 }
13670 #endif
13671 }
13672
13673 #ifdef HAVE_WINDOW_SYSTEM
13674 if (FRAME_WINDOW_P (f)
13675 && update_window_fringes (w, (just_this_one_p
13676 || (!used_current_matrix_p && !overlay_arrow_seen)
13677 || w->pseudo_window_p)))
13678 {
13679 update_begin (f);
13680 BLOCK_INPUT;
13681 if (draw_window_fringes (w, 1))
13682 x_draw_vertical_border (w);
13683 UNBLOCK_INPUT;
13684 update_end (f);
13685 }
13686 #endif /* HAVE_WINDOW_SYSTEM */
13687
13688 /* We go to this label, with fonts_changed_p nonzero,
13689 if it is necessary to try again using larger glyph matrices.
13690 We have to redeem the scroll bar even in this case,
13691 because the loop in redisplay_internal expects that. */
13692 need_larger_matrices:
13693 ;
13694 finish_scroll_bars:
13695
13696 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
13697 {
13698 /* Set the thumb's position and size. */
13699 set_vertical_scroll_bar (w);
13700
13701 /* Note that we actually used the scroll bar attached to this
13702 window, so it shouldn't be deleted at the end of redisplay. */
13703 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
13704 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
13705 }
13706
13707 /* Restore current_buffer and value of point in it. */
13708 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
13709 set_buffer_internal_1 (old);
13710 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
13711 shorter. This can be caused by log truncation in *Messages*. */
13712 if (CHARPOS (lpoint) <= ZV)
13713 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
13714
13715 unbind_to (count, Qnil);
13716 }
13717
13718
13719 /* Build the complete desired matrix of WINDOW with a window start
13720 buffer position POS.
13721
13722 Value is 1 if successful. It is zero if fonts were loaded during
13723 redisplay which makes re-adjusting glyph matrices necessary, and -1
13724 if point would appear in the scroll margins.
13725 (We check that only if CHECK_MARGINS is nonzero. */
13726
13727 int
13728 try_window (window, pos, check_margins)
13729 Lisp_Object window;
13730 struct text_pos pos;
13731 int check_margins;
13732 {
13733 struct window *w = XWINDOW (window);
13734 struct it it;
13735 struct glyph_row *last_text_row = NULL;
13736 struct frame *f = XFRAME (w->frame);
13737
13738 /* Make POS the new window start. */
13739 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
13740
13741 /* Mark cursor position as unknown. No overlay arrow seen. */
13742 w->cursor.vpos = -1;
13743 overlay_arrow_seen = 0;
13744
13745 /* Initialize iterator and info to start at POS. */
13746 start_display (&it, w, pos);
13747
13748 /* Display all lines of W. */
13749 while (it.current_y < it.last_visible_y)
13750 {
13751 if (display_line (&it))
13752 last_text_row = it.glyph_row - 1;
13753 if (fonts_changed_p)
13754 return 0;
13755 }
13756
13757 /* Don't let the cursor end in the scroll margins. */
13758 if (check_margins
13759 && !MINI_WINDOW_P (w))
13760 {
13761 int this_scroll_margin;
13762
13763 this_scroll_margin = max (0, scroll_margin);
13764 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
13765 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
13766
13767 if ((w->cursor.y >= 0 /* not vscrolled */
13768 && w->cursor.y < this_scroll_margin
13769 && CHARPOS (pos) > BEGV
13770 && IT_CHARPOS (it) < ZV)
13771 /* rms: considering make_cursor_line_fully_visible_p here
13772 seems to give wrong results. We don't want to recenter
13773 when the last line is partly visible, we want to allow
13774 that case to be handled in the usual way. */
13775 || (w->cursor.y + 1) > it.last_visible_y)
13776 {
13777 w->cursor.vpos = -1;
13778 clear_glyph_matrix (w->desired_matrix);
13779 return -1;
13780 }
13781 }
13782
13783 /* If bottom moved off end of frame, change mode line percentage. */
13784 if (XFASTINT (w->window_end_pos) <= 0
13785 && Z != IT_CHARPOS (it))
13786 w->update_mode_line = Qt;
13787
13788 /* Set window_end_pos to the offset of the last character displayed
13789 on the window from the end of current_buffer. Set
13790 window_end_vpos to its row number. */
13791 if (last_text_row)
13792 {
13793 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
13794 w->window_end_bytepos
13795 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
13796 w->window_end_pos
13797 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
13798 w->window_end_vpos
13799 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
13800 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
13801 ->displays_text_p);
13802 }
13803 else
13804 {
13805 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
13806 w->window_end_pos = make_number (Z - ZV);
13807 w->window_end_vpos = make_number (0);
13808 }
13809
13810 /* But that is not valid info until redisplay finishes. */
13811 w->window_end_valid = Qnil;
13812 return 1;
13813 }
13814
13815
13816 \f
13817 /************************************************************************
13818 Window redisplay reusing current matrix when buffer has not changed
13819 ************************************************************************/
13820
13821 /* Try redisplay of window W showing an unchanged buffer with a
13822 different window start than the last time it was displayed by
13823 reusing its current matrix. Value is non-zero if successful.
13824 W->start is the new window start. */
13825
13826 static int
13827 try_window_reusing_current_matrix (w)
13828 struct window *w;
13829 {
13830 struct frame *f = XFRAME (w->frame);
13831 struct glyph_row *row, *bottom_row;
13832 struct it it;
13833 struct run run;
13834 struct text_pos start, new_start;
13835 int nrows_scrolled, i;
13836 struct glyph_row *last_text_row;
13837 struct glyph_row *last_reused_text_row;
13838 struct glyph_row *start_row;
13839 int start_vpos, min_y, max_y;
13840
13841 #if GLYPH_DEBUG
13842 if (inhibit_try_window_reusing)
13843 return 0;
13844 #endif
13845
13846 if (/* This function doesn't handle terminal frames. */
13847 !FRAME_WINDOW_P (f)
13848 /* Don't try to reuse the display if windows have been split
13849 or such. */
13850 || windows_or_buffers_changed
13851 || cursor_type_changed)
13852 return 0;
13853
13854 /* Can't do this if region may have changed. */
13855 if ((!NILP (Vtransient_mark_mode)
13856 && !NILP (current_buffer->mark_active))
13857 || !NILP (w->region_showing)
13858 || !NILP (Vshow_trailing_whitespace))
13859 return 0;
13860
13861 /* If top-line visibility has changed, give up. */
13862 if (WINDOW_WANTS_HEADER_LINE_P (w)
13863 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
13864 return 0;
13865
13866 /* Give up if old or new display is scrolled vertically. We could
13867 make this function handle this, but right now it doesn't. */
13868 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
13869 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
13870 return 0;
13871
13872 /* The variable new_start now holds the new window start. The old
13873 start `start' can be determined from the current matrix. */
13874 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
13875 start = start_row->start.pos;
13876 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
13877
13878 /* Clear the desired matrix for the display below. */
13879 clear_glyph_matrix (w->desired_matrix);
13880
13881 if (CHARPOS (new_start) <= CHARPOS (start))
13882 {
13883 int first_row_y;
13884
13885 /* Don't use this method if the display starts with an ellipsis
13886 displayed for invisible text. It's not easy to handle that case
13887 below, and it's certainly not worth the effort since this is
13888 not a frequent case. */
13889 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
13890 return 0;
13891
13892 IF_DEBUG (debug_method_add (w, "twu1"));
13893
13894 /* Display up to a row that can be reused. The variable
13895 last_text_row is set to the last row displayed that displays
13896 text. Note that it.vpos == 0 if or if not there is a
13897 header-line; it's not the same as the MATRIX_ROW_VPOS! */
13898 start_display (&it, w, new_start);
13899 first_row_y = it.current_y;
13900 w->cursor.vpos = -1;
13901 last_text_row = last_reused_text_row = NULL;
13902
13903 while (it.current_y < it.last_visible_y
13904 && !fonts_changed_p)
13905 {
13906 /* If we have reached into the characters in the START row,
13907 that means the line boundaries have changed. So we
13908 can't start copying with the row START. Maybe it will
13909 work to start copying with the following row. */
13910 while (IT_CHARPOS (it) > CHARPOS (start))
13911 {
13912 /* Advance to the next row as the "start". */
13913 start_row++;
13914 start = start_row->start.pos;
13915 /* If there are no more rows to try, or just one, give up. */
13916 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
13917 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
13918 || CHARPOS (start) == ZV)
13919 {
13920 clear_glyph_matrix (w->desired_matrix);
13921 return 0;
13922 }
13923
13924 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
13925 }
13926 /* If we have reached alignment,
13927 we can copy the rest of the rows. */
13928 if (IT_CHARPOS (it) == CHARPOS (start))
13929 break;
13930
13931 if (display_line (&it))
13932 last_text_row = it.glyph_row - 1;
13933 }
13934
13935 /* A value of current_y < last_visible_y means that we stopped
13936 at the previous window start, which in turn means that we
13937 have at least one reusable row. */
13938 if (it.current_y < it.last_visible_y)
13939 {
13940 /* IT.vpos always starts from 0; it counts text lines. */
13941 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
13942
13943 /* Find PT if not already found in the lines displayed. */
13944 if (w->cursor.vpos < 0)
13945 {
13946 int dy = it.current_y - start_row->y;
13947
13948 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
13949 row = row_containing_pos (w, PT, row, NULL, dy);
13950 if (row)
13951 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
13952 dy, nrows_scrolled);
13953 else
13954 {
13955 clear_glyph_matrix (w->desired_matrix);
13956 return 0;
13957 }
13958 }
13959
13960 /* Scroll the display. Do it before the current matrix is
13961 changed. The problem here is that update has not yet
13962 run, i.e. part of the current matrix is not up to date.
13963 scroll_run_hook will clear the cursor, and use the
13964 current matrix to get the height of the row the cursor is
13965 in. */
13966 run.current_y = start_row->y;
13967 run.desired_y = it.current_y;
13968 run.height = it.last_visible_y - it.current_y;
13969
13970 if (run.height > 0 && run.current_y != run.desired_y)
13971 {
13972 update_begin (f);
13973 FRAME_RIF (f)->update_window_begin_hook (w);
13974 FRAME_RIF (f)->clear_window_mouse_face (w);
13975 FRAME_RIF (f)->scroll_run_hook (w, &run);
13976 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
13977 update_end (f);
13978 }
13979
13980 /* Shift current matrix down by nrows_scrolled lines. */
13981 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
13982 rotate_matrix (w->current_matrix,
13983 start_vpos,
13984 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
13985 nrows_scrolled);
13986
13987 /* Disable lines that must be updated. */
13988 for (i = 0; i < nrows_scrolled; ++i)
13989 (start_row + i)->enabled_p = 0;
13990
13991 /* Re-compute Y positions. */
13992 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
13993 max_y = it.last_visible_y;
13994 for (row = start_row + nrows_scrolled;
13995 row < bottom_row;
13996 ++row)
13997 {
13998 row->y = it.current_y;
13999 row->visible_height = row->height;
14000
14001 if (row->y < min_y)
14002 row->visible_height -= min_y - row->y;
14003 if (row->y + row->height > max_y)
14004 row->visible_height -= row->y + row->height - max_y;
14005 row->redraw_fringe_bitmaps_p = 1;
14006
14007 it.current_y += row->height;
14008
14009 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14010 last_reused_text_row = row;
14011 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
14012 break;
14013 }
14014
14015 /* Disable lines in the current matrix which are now
14016 below the window. */
14017 for (++row; row < bottom_row; ++row)
14018 row->enabled_p = row->mode_line_p = 0;
14019 }
14020
14021 /* Update window_end_pos etc.; last_reused_text_row is the last
14022 reused row from the current matrix containing text, if any.
14023 The value of last_text_row is the last displayed line
14024 containing text. */
14025 if (last_reused_text_row)
14026 {
14027 w->window_end_bytepos
14028 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
14029 w->window_end_pos
14030 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
14031 w->window_end_vpos
14032 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
14033 w->current_matrix));
14034 }
14035 else if (last_text_row)
14036 {
14037 w->window_end_bytepos
14038 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
14039 w->window_end_pos
14040 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
14041 w->window_end_vpos
14042 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
14043 }
14044 else
14045 {
14046 /* This window must be completely empty. */
14047 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
14048 w->window_end_pos = make_number (Z - ZV);
14049 w->window_end_vpos = make_number (0);
14050 }
14051 w->window_end_valid = Qnil;
14052
14053 /* Update hint: don't try scrolling again in update_window. */
14054 w->desired_matrix->no_scrolling_p = 1;
14055
14056 #if GLYPH_DEBUG
14057 debug_method_add (w, "try_window_reusing_current_matrix 1");
14058 #endif
14059 return 1;
14060 }
14061 else if (CHARPOS (new_start) > CHARPOS (start))
14062 {
14063 struct glyph_row *pt_row, *row;
14064 struct glyph_row *first_reusable_row;
14065 struct glyph_row *first_row_to_display;
14066 int dy;
14067 int yb = window_text_bottom_y (w);
14068
14069 /* Find the row starting at new_start, if there is one. Don't
14070 reuse a partially visible line at the end. */
14071 first_reusable_row = start_row;
14072 while (first_reusable_row->enabled_p
14073 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
14074 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
14075 < CHARPOS (new_start)))
14076 ++first_reusable_row;
14077
14078 /* Give up if there is no row to reuse. */
14079 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
14080 || !first_reusable_row->enabled_p
14081 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
14082 != CHARPOS (new_start)))
14083 return 0;
14084
14085 /* We can reuse fully visible rows beginning with
14086 first_reusable_row to the end of the window. Set
14087 first_row_to_display to the first row that cannot be reused.
14088 Set pt_row to the row containing point, if there is any. */
14089 pt_row = NULL;
14090 for (first_row_to_display = first_reusable_row;
14091 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
14092 ++first_row_to_display)
14093 {
14094 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
14095 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
14096 pt_row = first_row_to_display;
14097 }
14098
14099 /* Start displaying at the start of first_row_to_display. */
14100 xassert (first_row_to_display->y < yb);
14101 init_to_row_start (&it, w, first_row_to_display);
14102
14103 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
14104 - start_vpos);
14105 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
14106 - nrows_scrolled);
14107 it.current_y = (first_row_to_display->y - first_reusable_row->y
14108 + WINDOW_HEADER_LINE_HEIGHT (w));
14109
14110 /* Display lines beginning with first_row_to_display in the
14111 desired matrix. Set last_text_row to the last row displayed
14112 that displays text. */
14113 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
14114 if (pt_row == NULL)
14115 w->cursor.vpos = -1;
14116 last_text_row = NULL;
14117 while (it.current_y < it.last_visible_y && !fonts_changed_p)
14118 if (display_line (&it))
14119 last_text_row = it.glyph_row - 1;
14120
14121 /* Give up If point isn't in a row displayed or reused. */
14122 if (w->cursor.vpos < 0)
14123 {
14124 clear_glyph_matrix (w->desired_matrix);
14125 return 0;
14126 }
14127
14128 /* If point is in a reused row, adjust y and vpos of the cursor
14129 position. */
14130 if (pt_row)
14131 {
14132 w->cursor.vpos -= nrows_scrolled;
14133 w->cursor.y -= first_reusable_row->y - start_row->y;
14134 }
14135
14136 /* Scroll the display. */
14137 run.current_y = first_reusable_row->y;
14138 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
14139 run.height = it.last_visible_y - run.current_y;
14140 dy = run.current_y - run.desired_y;
14141
14142 if (run.height)
14143 {
14144 update_begin (f);
14145 FRAME_RIF (f)->update_window_begin_hook (w);
14146 FRAME_RIF (f)->clear_window_mouse_face (w);
14147 FRAME_RIF (f)->scroll_run_hook (w, &run);
14148 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
14149 update_end (f);
14150 }
14151
14152 /* Adjust Y positions of reused rows. */
14153 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
14154 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
14155 max_y = it.last_visible_y;
14156 for (row = first_reusable_row; row < first_row_to_display; ++row)
14157 {
14158 row->y -= dy;
14159 row->visible_height = row->height;
14160 if (row->y < min_y)
14161 row->visible_height -= min_y - row->y;
14162 if (row->y + row->height > max_y)
14163 row->visible_height -= row->y + row->height - max_y;
14164 row->redraw_fringe_bitmaps_p = 1;
14165 }
14166
14167 /* Scroll the current matrix. */
14168 xassert (nrows_scrolled > 0);
14169 rotate_matrix (w->current_matrix,
14170 start_vpos,
14171 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
14172 -nrows_scrolled);
14173
14174 /* Disable rows not reused. */
14175 for (row -= nrows_scrolled; row < bottom_row; ++row)
14176 row->enabled_p = 0;
14177
14178 /* Point may have moved to a different line, so we cannot assume that
14179 the previous cursor position is valid; locate the correct row. */
14180 if (pt_row)
14181 {
14182 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
14183 row < bottom_row && PT >= MATRIX_ROW_END_CHARPOS (row);
14184 row++)
14185 {
14186 w->cursor.vpos++;
14187 w->cursor.y = row->y;
14188 }
14189 if (row < bottom_row)
14190 {
14191 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
14192 while (glyph->charpos < PT)
14193 {
14194 w->cursor.hpos++;
14195 w->cursor.x += glyph->pixel_width;
14196 glyph++;
14197 }
14198 }
14199 }
14200
14201 /* Adjust window end. A null value of last_text_row means that
14202 the window end is in reused rows which in turn means that
14203 only its vpos can have changed. */
14204 if (last_text_row)
14205 {
14206 w->window_end_bytepos
14207 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
14208 w->window_end_pos
14209 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
14210 w->window_end_vpos
14211 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
14212 }
14213 else
14214 {
14215 w->window_end_vpos
14216 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
14217 }
14218
14219 w->window_end_valid = Qnil;
14220 w->desired_matrix->no_scrolling_p = 1;
14221
14222 #if GLYPH_DEBUG
14223 debug_method_add (w, "try_window_reusing_current_matrix 2");
14224 #endif
14225 return 1;
14226 }
14227
14228 return 0;
14229 }
14230
14231
14232 \f
14233 /************************************************************************
14234 Window redisplay reusing current matrix when buffer has changed
14235 ************************************************************************/
14236
14237 static struct glyph_row *find_last_unchanged_at_beg_row P_ ((struct window *));
14238 static struct glyph_row *find_first_unchanged_at_end_row P_ ((struct window *,
14239 int *, int *));
14240 static struct glyph_row *
14241 find_last_row_displaying_text P_ ((struct glyph_matrix *, struct it *,
14242 struct glyph_row *));
14243
14244
14245 /* Return the last row in MATRIX displaying text. If row START is
14246 non-null, start searching with that row. IT gives the dimensions
14247 of the display. Value is null if matrix is empty; otherwise it is
14248 a pointer to the row found. */
14249
14250 static struct glyph_row *
14251 find_last_row_displaying_text (matrix, it, start)
14252 struct glyph_matrix *matrix;
14253 struct it *it;
14254 struct glyph_row *start;
14255 {
14256 struct glyph_row *row, *row_found;
14257
14258 /* Set row_found to the last row in IT->w's current matrix
14259 displaying text. The loop looks funny but think of partially
14260 visible lines. */
14261 row_found = NULL;
14262 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
14263 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14264 {
14265 xassert (row->enabled_p);
14266 row_found = row;
14267 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
14268 break;
14269 ++row;
14270 }
14271
14272 return row_found;
14273 }
14274
14275
14276 /* Return the last row in the current matrix of W that is not affected
14277 by changes at the start of current_buffer that occurred since W's
14278 current matrix was built. Value is null if no such row exists.
14279
14280 BEG_UNCHANGED us the number of characters unchanged at the start of
14281 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
14282 first changed character in current_buffer. Characters at positions <
14283 BEG + BEG_UNCHANGED are at the same buffer positions as they were
14284 when the current matrix was built. */
14285
14286 static struct glyph_row *
14287 find_last_unchanged_at_beg_row (w)
14288 struct window *w;
14289 {
14290 int first_changed_pos = BEG + BEG_UNCHANGED;
14291 struct glyph_row *row;
14292 struct glyph_row *row_found = NULL;
14293 int yb = window_text_bottom_y (w);
14294
14295 /* Find the last row displaying unchanged text. */
14296 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14297 while (MATRIX_ROW_DISPLAYS_TEXT_P (row)
14298 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos)
14299 {
14300 if (/* If row ends before first_changed_pos, it is unchanged,
14301 except in some case. */
14302 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
14303 /* When row ends in ZV and we write at ZV it is not
14304 unchanged. */
14305 && !row->ends_at_zv_p
14306 /* When first_changed_pos is the end of a continued line,
14307 row is not unchanged because it may be no longer
14308 continued. */
14309 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
14310 && (row->continued_p
14311 || row->exact_window_width_line_p)))
14312 row_found = row;
14313
14314 /* Stop if last visible row. */
14315 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
14316 break;
14317
14318 ++row;
14319 }
14320
14321 return row_found;
14322 }
14323
14324
14325 /* Find the first glyph row in the current matrix of W that is not
14326 affected by changes at the end of current_buffer since the
14327 time W's current matrix was built.
14328
14329 Return in *DELTA the number of chars by which buffer positions in
14330 unchanged text at the end of current_buffer must be adjusted.
14331
14332 Return in *DELTA_BYTES the corresponding number of bytes.
14333
14334 Value is null if no such row exists, i.e. all rows are affected by
14335 changes. */
14336
14337 static struct glyph_row *
14338 find_first_unchanged_at_end_row (w, delta, delta_bytes)
14339 struct window *w;
14340 int *delta, *delta_bytes;
14341 {
14342 struct glyph_row *row;
14343 struct glyph_row *row_found = NULL;
14344
14345 *delta = *delta_bytes = 0;
14346
14347 /* Display must not have been paused, otherwise the current matrix
14348 is not up to date. */
14349 if (NILP (w->window_end_valid))
14350 abort ();
14351
14352 /* A value of window_end_pos >= END_UNCHANGED means that the window
14353 end is in the range of changed text. If so, there is no
14354 unchanged row at the end of W's current matrix. */
14355 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
14356 return NULL;
14357
14358 /* Set row to the last row in W's current matrix displaying text. */
14359 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
14360
14361 /* If matrix is entirely empty, no unchanged row exists. */
14362 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14363 {
14364 /* The value of row is the last glyph row in the matrix having a
14365 meaningful buffer position in it. The end position of row
14366 corresponds to window_end_pos. This allows us to translate
14367 buffer positions in the current matrix to current buffer
14368 positions for characters not in changed text. */
14369 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
14370 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
14371 int last_unchanged_pos, last_unchanged_pos_old;
14372 struct glyph_row *first_text_row
14373 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14374
14375 *delta = Z - Z_old;
14376 *delta_bytes = Z_BYTE - Z_BYTE_old;
14377
14378 /* Set last_unchanged_pos to the buffer position of the last
14379 character in the buffer that has not been changed. Z is the
14380 index + 1 of the last character in current_buffer, i.e. by
14381 subtracting END_UNCHANGED we get the index of the last
14382 unchanged character, and we have to add BEG to get its buffer
14383 position. */
14384 last_unchanged_pos = Z - END_UNCHANGED + BEG;
14385 last_unchanged_pos_old = last_unchanged_pos - *delta;
14386
14387 /* Search backward from ROW for a row displaying a line that
14388 starts at a minimum position >= last_unchanged_pos_old. */
14389 for (; row > first_text_row; --row)
14390 {
14391 /* This used to abort, but it can happen.
14392 It is ok to just stop the search instead here. KFS. */
14393 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
14394 break;
14395
14396 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
14397 row_found = row;
14398 }
14399 }
14400
14401 if (row_found && !MATRIX_ROW_DISPLAYS_TEXT_P (row_found))
14402 abort ();
14403
14404 return row_found;
14405 }
14406
14407
14408 /* Make sure that glyph rows in the current matrix of window W
14409 reference the same glyph memory as corresponding rows in the
14410 frame's frame matrix. This function is called after scrolling W's
14411 current matrix on a terminal frame in try_window_id and
14412 try_window_reusing_current_matrix. */
14413
14414 static void
14415 sync_frame_with_window_matrix_rows (w)
14416 struct window *w;
14417 {
14418 struct frame *f = XFRAME (w->frame);
14419 struct glyph_row *window_row, *window_row_end, *frame_row;
14420
14421 /* Preconditions: W must be a leaf window and full-width. Its frame
14422 must have a frame matrix. */
14423 xassert (NILP (w->hchild) && NILP (w->vchild));
14424 xassert (WINDOW_FULL_WIDTH_P (w));
14425 xassert (!FRAME_WINDOW_P (f));
14426
14427 /* If W is a full-width window, glyph pointers in W's current matrix
14428 have, by definition, to be the same as glyph pointers in the
14429 corresponding frame matrix. Note that frame matrices have no
14430 marginal areas (see build_frame_matrix). */
14431 window_row = w->current_matrix->rows;
14432 window_row_end = window_row + w->current_matrix->nrows;
14433 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
14434 while (window_row < window_row_end)
14435 {
14436 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
14437 struct glyph *end = window_row->glyphs[LAST_AREA];
14438
14439 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
14440 frame_row->glyphs[TEXT_AREA] = start;
14441 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
14442 frame_row->glyphs[LAST_AREA] = end;
14443
14444 /* Disable frame rows whose corresponding window rows have
14445 been disabled in try_window_id. */
14446 if (!window_row->enabled_p)
14447 frame_row->enabled_p = 0;
14448
14449 ++window_row, ++frame_row;
14450 }
14451 }
14452
14453
14454 /* Find the glyph row in window W containing CHARPOS. Consider all
14455 rows between START and END (not inclusive). END null means search
14456 all rows to the end of the display area of W. Value is the row
14457 containing CHARPOS or null. */
14458
14459 struct glyph_row *
14460 row_containing_pos (w, charpos, start, end, dy)
14461 struct window *w;
14462 int charpos;
14463 struct glyph_row *start, *end;
14464 int dy;
14465 {
14466 struct glyph_row *row = start;
14467 int last_y;
14468
14469 /* If we happen to start on a header-line, skip that. */
14470 if (row->mode_line_p)
14471 ++row;
14472
14473 if ((end && row >= end) || !row->enabled_p)
14474 return NULL;
14475
14476 last_y = window_text_bottom_y (w) - dy;
14477
14478 while (1)
14479 {
14480 /* Give up if we have gone too far. */
14481 if (end && row >= end)
14482 return NULL;
14483 /* This formerly returned if they were equal.
14484 I think that both quantities are of a "last plus one" type;
14485 if so, when they are equal, the row is within the screen. -- rms. */
14486 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
14487 return NULL;
14488
14489 /* If it is in this row, return this row. */
14490 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
14491 || (MATRIX_ROW_END_CHARPOS (row) == charpos
14492 /* The end position of a row equals the start
14493 position of the next row. If CHARPOS is there, we
14494 would rather display it in the next line, except
14495 when this line ends in ZV. */
14496 && !row->ends_at_zv_p
14497 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
14498 && charpos >= MATRIX_ROW_START_CHARPOS (row))
14499 return row;
14500 ++row;
14501 }
14502 }
14503
14504
14505 /* Try to redisplay window W by reusing its existing display. W's
14506 current matrix must be up to date when this function is called,
14507 i.e. window_end_valid must not be nil.
14508
14509 Value is
14510
14511 1 if display has been updated
14512 0 if otherwise unsuccessful
14513 -1 if redisplay with same window start is known not to succeed
14514
14515 The following steps are performed:
14516
14517 1. Find the last row in the current matrix of W that is not
14518 affected by changes at the start of current_buffer. If no such row
14519 is found, give up.
14520
14521 2. Find the first row in W's current matrix that is not affected by
14522 changes at the end of current_buffer. Maybe there is no such row.
14523
14524 3. Display lines beginning with the row + 1 found in step 1 to the
14525 row found in step 2 or, if step 2 didn't find a row, to the end of
14526 the window.
14527
14528 4. If cursor is not known to appear on the window, give up.
14529
14530 5. If display stopped at the row found in step 2, scroll the
14531 display and current matrix as needed.
14532
14533 6. Maybe display some lines at the end of W, if we must. This can
14534 happen under various circumstances, like a partially visible line
14535 becoming fully visible, or because newly displayed lines are displayed
14536 in smaller font sizes.
14537
14538 7. Update W's window end information. */
14539
14540 static int
14541 try_window_id (w)
14542 struct window *w;
14543 {
14544 struct frame *f = XFRAME (w->frame);
14545 struct glyph_matrix *current_matrix = w->current_matrix;
14546 struct glyph_matrix *desired_matrix = w->desired_matrix;
14547 struct glyph_row *last_unchanged_at_beg_row;
14548 struct glyph_row *first_unchanged_at_end_row;
14549 struct glyph_row *row;
14550 struct glyph_row *bottom_row;
14551 int bottom_vpos;
14552 struct it it;
14553 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
14554 struct text_pos start_pos;
14555 struct run run;
14556 int first_unchanged_at_end_vpos = 0;
14557 struct glyph_row *last_text_row, *last_text_row_at_end;
14558 struct text_pos start;
14559 int first_changed_charpos, last_changed_charpos;
14560
14561 #if GLYPH_DEBUG
14562 if (inhibit_try_window_id)
14563 return 0;
14564 #endif
14565
14566 /* This is handy for debugging. */
14567 #if 0
14568 #define GIVE_UP(X) \
14569 do { \
14570 fprintf (stderr, "try_window_id give up %d\n", (X)); \
14571 return 0; \
14572 } while (0)
14573 #else
14574 #define GIVE_UP(X) return 0
14575 #endif
14576
14577 SET_TEXT_POS_FROM_MARKER (start, w->start);
14578
14579 /* Don't use this for mini-windows because these can show
14580 messages and mini-buffers, and we don't handle that here. */
14581 if (MINI_WINDOW_P (w))
14582 GIVE_UP (1);
14583
14584 /* This flag is used to prevent redisplay optimizations. */
14585 if (windows_or_buffers_changed || cursor_type_changed)
14586 GIVE_UP (2);
14587
14588 /* Verify that narrowing has not changed.
14589 Also verify that we were not told to prevent redisplay optimizations.
14590 It would be nice to further
14591 reduce the number of cases where this prevents try_window_id. */
14592 if (current_buffer->clip_changed
14593 || current_buffer->prevent_redisplay_optimizations_p)
14594 GIVE_UP (3);
14595
14596 /* Window must either use window-based redisplay or be full width. */
14597 if (!FRAME_WINDOW_P (f)
14598 && (!FRAME_LINE_INS_DEL_OK (f)
14599 || !WINDOW_FULL_WIDTH_P (w)))
14600 GIVE_UP (4);
14601
14602 /* Give up if point is not known NOT to appear in W. */
14603 if (PT < CHARPOS (start))
14604 GIVE_UP (5);
14605
14606 /* Another way to prevent redisplay optimizations. */
14607 if (XFASTINT (w->last_modified) == 0)
14608 GIVE_UP (6);
14609
14610 /* Verify that window is not hscrolled. */
14611 if (XFASTINT (w->hscroll) != 0)
14612 GIVE_UP (7);
14613
14614 /* Verify that display wasn't paused. */
14615 if (NILP (w->window_end_valid))
14616 GIVE_UP (8);
14617
14618 /* Can't use this if highlighting a region because a cursor movement
14619 will do more than just set the cursor. */
14620 if (!NILP (Vtransient_mark_mode)
14621 && !NILP (current_buffer->mark_active))
14622 GIVE_UP (9);
14623
14624 /* Likewise if highlighting trailing whitespace. */
14625 if (!NILP (Vshow_trailing_whitespace))
14626 GIVE_UP (11);
14627
14628 /* Likewise if showing a region. */
14629 if (!NILP (w->region_showing))
14630 GIVE_UP (10);
14631
14632 /* Can use this if overlay arrow position and or string have changed. */
14633 if (overlay_arrows_changed_p ())
14634 GIVE_UP (12);
14635
14636
14637 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
14638 only if buffer has really changed. The reason is that the gap is
14639 initially at Z for freshly visited files. The code below would
14640 set end_unchanged to 0 in that case. */
14641 if (MODIFF > SAVE_MODIFF
14642 /* This seems to happen sometimes after saving a buffer. */
14643 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
14644 {
14645 if (GPT - BEG < BEG_UNCHANGED)
14646 BEG_UNCHANGED = GPT - BEG;
14647 if (Z - GPT < END_UNCHANGED)
14648 END_UNCHANGED = Z - GPT;
14649 }
14650
14651 /* The position of the first and last character that has been changed. */
14652 first_changed_charpos = BEG + BEG_UNCHANGED;
14653 last_changed_charpos = Z - END_UNCHANGED;
14654
14655 /* If window starts after a line end, and the last change is in
14656 front of that newline, then changes don't affect the display.
14657 This case happens with stealth-fontification. Note that although
14658 the display is unchanged, glyph positions in the matrix have to
14659 be adjusted, of course. */
14660 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
14661 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
14662 && ((last_changed_charpos < CHARPOS (start)
14663 && CHARPOS (start) == BEGV)
14664 || (last_changed_charpos < CHARPOS (start) - 1
14665 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
14666 {
14667 int Z_old, delta, Z_BYTE_old, delta_bytes;
14668 struct glyph_row *r0;
14669
14670 /* Compute how many chars/bytes have been added to or removed
14671 from the buffer. */
14672 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
14673 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
14674 delta = Z - Z_old;
14675 delta_bytes = Z_BYTE - Z_BYTE_old;
14676
14677 /* Give up if PT is not in the window. Note that it already has
14678 been checked at the start of try_window_id that PT is not in
14679 front of the window start. */
14680 if (PT >= MATRIX_ROW_END_CHARPOS (row) + delta)
14681 GIVE_UP (13);
14682
14683 /* If window start is unchanged, we can reuse the whole matrix
14684 as is, after adjusting glyph positions. No need to compute
14685 the window end again, since its offset from Z hasn't changed. */
14686 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
14687 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + delta
14688 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + delta_bytes
14689 /* PT must not be in a partially visible line. */
14690 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + delta
14691 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
14692 {
14693 /* Adjust positions in the glyph matrix. */
14694 if (delta || delta_bytes)
14695 {
14696 struct glyph_row *r1
14697 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
14698 increment_matrix_positions (w->current_matrix,
14699 MATRIX_ROW_VPOS (r0, current_matrix),
14700 MATRIX_ROW_VPOS (r1, current_matrix),
14701 delta, delta_bytes);
14702 }
14703
14704 /* Set the cursor. */
14705 row = row_containing_pos (w, PT, r0, NULL, 0);
14706 if (row)
14707 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
14708 else
14709 abort ();
14710 return 1;
14711 }
14712 }
14713
14714 /* Handle the case that changes are all below what is displayed in
14715 the window, and that PT is in the window. This shortcut cannot
14716 be taken if ZV is visible in the window, and text has been added
14717 there that is visible in the window. */
14718 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
14719 /* ZV is not visible in the window, or there are no
14720 changes at ZV, actually. */
14721 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
14722 || first_changed_charpos == last_changed_charpos))
14723 {
14724 struct glyph_row *r0;
14725
14726 /* Give up if PT is not in the window. Note that it already has
14727 been checked at the start of try_window_id that PT is not in
14728 front of the window start. */
14729 if (PT >= MATRIX_ROW_END_CHARPOS (row))
14730 GIVE_UP (14);
14731
14732 /* If window start is unchanged, we can reuse the whole matrix
14733 as is, without changing glyph positions since no text has
14734 been added/removed in front of the window end. */
14735 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
14736 if (TEXT_POS_EQUAL_P (start, r0->start.pos)
14737 /* PT must not be in a partially visible line. */
14738 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
14739 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
14740 {
14741 /* We have to compute the window end anew since text
14742 can have been added/removed after it. */
14743 w->window_end_pos
14744 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
14745 w->window_end_bytepos
14746 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
14747
14748 /* Set the cursor. */
14749 row = row_containing_pos (w, PT, r0, NULL, 0);
14750 if (row)
14751 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
14752 else
14753 abort ();
14754 return 2;
14755 }
14756 }
14757
14758 /* Give up if window start is in the changed area.
14759
14760 The condition used to read
14761
14762 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
14763
14764 but why that was tested escapes me at the moment. */
14765 if (CHARPOS (start) >= first_changed_charpos
14766 && CHARPOS (start) <= last_changed_charpos)
14767 GIVE_UP (15);
14768
14769 /* Check that window start agrees with the start of the first glyph
14770 row in its current matrix. Check this after we know the window
14771 start is not in changed text, otherwise positions would not be
14772 comparable. */
14773 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
14774 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
14775 GIVE_UP (16);
14776
14777 /* Give up if the window ends in strings. Overlay strings
14778 at the end are difficult to handle, so don't try. */
14779 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
14780 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
14781 GIVE_UP (20);
14782
14783 /* Compute the position at which we have to start displaying new
14784 lines. Some of the lines at the top of the window might be
14785 reusable because they are not displaying changed text. Find the
14786 last row in W's current matrix not affected by changes at the
14787 start of current_buffer. Value is null if changes start in the
14788 first line of window. */
14789 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
14790 if (last_unchanged_at_beg_row)
14791 {
14792 /* Avoid starting to display in the moddle of a character, a TAB
14793 for instance. This is easier than to set up the iterator
14794 exactly, and it's not a frequent case, so the additional
14795 effort wouldn't really pay off. */
14796 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
14797 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
14798 && last_unchanged_at_beg_row > w->current_matrix->rows)
14799 --last_unchanged_at_beg_row;
14800
14801 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
14802 GIVE_UP (17);
14803
14804 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
14805 GIVE_UP (18);
14806 start_pos = it.current.pos;
14807
14808 /* Start displaying new lines in the desired matrix at the same
14809 vpos we would use in the current matrix, i.e. below
14810 last_unchanged_at_beg_row. */
14811 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
14812 current_matrix);
14813 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
14814 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
14815
14816 xassert (it.hpos == 0 && it.current_x == 0);
14817 }
14818 else
14819 {
14820 /* There are no reusable lines at the start of the window.
14821 Start displaying in the first text line. */
14822 start_display (&it, w, start);
14823 it.vpos = it.first_vpos;
14824 start_pos = it.current.pos;
14825 }
14826
14827 /* Find the first row that is not affected by changes at the end of
14828 the buffer. Value will be null if there is no unchanged row, in
14829 which case we must redisplay to the end of the window. delta
14830 will be set to the value by which buffer positions beginning with
14831 first_unchanged_at_end_row have to be adjusted due to text
14832 changes. */
14833 first_unchanged_at_end_row
14834 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
14835 IF_DEBUG (debug_delta = delta);
14836 IF_DEBUG (debug_delta_bytes = delta_bytes);
14837
14838 /* Set stop_pos to the buffer position up to which we will have to
14839 display new lines. If first_unchanged_at_end_row != NULL, this
14840 is the buffer position of the start of the line displayed in that
14841 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
14842 that we don't stop at a buffer position. */
14843 stop_pos = 0;
14844 if (first_unchanged_at_end_row)
14845 {
14846 xassert (last_unchanged_at_beg_row == NULL
14847 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
14848
14849 /* If this is a continuation line, move forward to the next one
14850 that isn't. Changes in lines above affect this line.
14851 Caution: this may move first_unchanged_at_end_row to a row
14852 not displaying text. */
14853 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
14854 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
14855 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
14856 < it.last_visible_y))
14857 ++first_unchanged_at_end_row;
14858
14859 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
14860 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
14861 >= it.last_visible_y))
14862 first_unchanged_at_end_row = NULL;
14863 else
14864 {
14865 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
14866 + delta);
14867 first_unchanged_at_end_vpos
14868 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
14869 xassert (stop_pos >= Z - END_UNCHANGED);
14870 }
14871 }
14872 else if (last_unchanged_at_beg_row == NULL)
14873 GIVE_UP (19);
14874
14875
14876 #if GLYPH_DEBUG
14877
14878 /* Either there is no unchanged row at the end, or the one we have
14879 now displays text. This is a necessary condition for the window
14880 end pos calculation at the end of this function. */
14881 xassert (first_unchanged_at_end_row == NULL
14882 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
14883
14884 debug_last_unchanged_at_beg_vpos
14885 = (last_unchanged_at_beg_row
14886 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
14887 : -1);
14888 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
14889
14890 #endif /* GLYPH_DEBUG != 0 */
14891
14892
14893 /* Display new lines. Set last_text_row to the last new line
14894 displayed which has text on it, i.e. might end up as being the
14895 line where the window_end_vpos is. */
14896 w->cursor.vpos = -1;
14897 last_text_row = NULL;
14898 overlay_arrow_seen = 0;
14899 while (it.current_y < it.last_visible_y
14900 && !fonts_changed_p
14901 && (first_unchanged_at_end_row == NULL
14902 || IT_CHARPOS (it) < stop_pos))
14903 {
14904 if (display_line (&it))
14905 last_text_row = it.glyph_row - 1;
14906 }
14907
14908 if (fonts_changed_p)
14909 return -1;
14910
14911
14912 /* Compute differences in buffer positions, y-positions etc. for
14913 lines reused at the bottom of the window. Compute what we can
14914 scroll. */
14915 if (first_unchanged_at_end_row
14916 /* No lines reused because we displayed everything up to the
14917 bottom of the window. */
14918 && it.current_y < it.last_visible_y)
14919 {
14920 dvpos = (it.vpos
14921 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
14922 current_matrix));
14923 dy = it.current_y - first_unchanged_at_end_row->y;
14924 run.current_y = first_unchanged_at_end_row->y;
14925 run.desired_y = run.current_y + dy;
14926 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
14927 }
14928 else
14929 {
14930 delta = dvpos = dy = run.current_y = run.desired_y = run.height = 0;
14931 first_unchanged_at_end_row = NULL;
14932 }
14933 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
14934
14935
14936 /* Find the cursor if not already found. We have to decide whether
14937 PT will appear on this window (it sometimes doesn't, but this is
14938 not a very frequent case.) This decision has to be made before
14939 the current matrix is altered. A value of cursor.vpos < 0 means
14940 that PT is either in one of the lines beginning at
14941 first_unchanged_at_end_row or below the window. Don't care for
14942 lines that might be displayed later at the window end; as
14943 mentioned, this is not a frequent case. */
14944 if (w->cursor.vpos < 0)
14945 {
14946 /* Cursor in unchanged rows at the top? */
14947 if (PT < CHARPOS (start_pos)
14948 && last_unchanged_at_beg_row)
14949 {
14950 row = row_containing_pos (w, PT,
14951 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
14952 last_unchanged_at_beg_row + 1, 0);
14953 if (row)
14954 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
14955 }
14956
14957 /* Start from first_unchanged_at_end_row looking for PT. */
14958 else if (first_unchanged_at_end_row)
14959 {
14960 row = row_containing_pos (w, PT - delta,
14961 first_unchanged_at_end_row, NULL, 0);
14962 if (row)
14963 set_cursor_from_row (w, row, w->current_matrix, delta,
14964 delta_bytes, dy, dvpos);
14965 }
14966
14967 /* Give up if cursor was not found. */
14968 if (w->cursor.vpos < 0)
14969 {
14970 clear_glyph_matrix (w->desired_matrix);
14971 return -1;
14972 }
14973 }
14974
14975 /* Don't let the cursor end in the scroll margins. */
14976 {
14977 int this_scroll_margin, cursor_height;
14978
14979 this_scroll_margin = max (0, scroll_margin);
14980 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
14981 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
14982 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
14983
14984 if ((w->cursor.y < this_scroll_margin
14985 && CHARPOS (start) > BEGV)
14986 /* Old redisplay didn't take scroll margin into account at the bottom,
14987 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
14988 || (w->cursor.y + (make_cursor_line_fully_visible_p
14989 ? cursor_height + this_scroll_margin
14990 : 1)) > it.last_visible_y)
14991 {
14992 w->cursor.vpos = -1;
14993 clear_glyph_matrix (w->desired_matrix);
14994 return -1;
14995 }
14996 }
14997
14998 /* Scroll the display. Do it before changing the current matrix so
14999 that xterm.c doesn't get confused about where the cursor glyph is
15000 found. */
15001 if (dy && run.height)
15002 {
15003 update_begin (f);
15004
15005 if (FRAME_WINDOW_P (f))
15006 {
15007 FRAME_RIF (f)->update_window_begin_hook (w);
15008 FRAME_RIF (f)->clear_window_mouse_face (w);
15009 FRAME_RIF (f)->scroll_run_hook (w, &run);
15010 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
15011 }
15012 else
15013 {
15014 /* Terminal frame. In this case, dvpos gives the number of
15015 lines to scroll by; dvpos < 0 means scroll up. */
15016 int first_unchanged_at_end_vpos
15017 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
15018 int from = WINDOW_TOP_EDGE_LINE (w) + first_unchanged_at_end_vpos;
15019 int end = (WINDOW_TOP_EDGE_LINE (w)
15020 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
15021 + window_internal_height (w));
15022
15023 /* Perform the operation on the screen. */
15024 if (dvpos > 0)
15025 {
15026 /* Scroll last_unchanged_at_beg_row to the end of the
15027 window down dvpos lines. */
15028 set_terminal_window (f, end);
15029
15030 /* On dumb terminals delete dvpos lines at the end
15031 before inserting dvpos empty lines. */
15032 if (!FRAME_SCROLL_REGION_OK (f))
15033 ins_del_lines (f, end - dvpos, -dvpos);
15034
15035 /* Insert dvpos empty lines in front of
15036 last_unchanged_at_beg_row. */
15037 ins_del_lines (f, from, dvpos);
15038 }
15039 else if (dvpos < 0)
15040 {
15041 /* Scroll up last_unchanged_at_beg_vpos to the end of
15042 the window to last_unchanged_at_beg_vpos - |dvpos|. */
15043 set_terminal_window (f, end);
15044
15045 /* Delete dvpos lines in front of
15046 last_unchanged_at_beg_vpos. ins_del_lines will set
15047 the cursor to the given vpos and emit |dvpos| delete
15048 line sequences. */
15049 ins_del_lines (f, from + dvpos, dvpos);
15050
15051 /* On a dumb terminal insert dvpos empty lines at the
15052 end. */
15053 if (!FRAME_SCROLL_REGION_OK (f))
15054 ins_del_lines (f, end + dvpos, -dvpos);
15055 }
15056
15057 set_terminal_window (f, 0);
15058 }
15059
15060 update_end (f);
15061 }
15062
15063 /* Shift reused rows of the current matrix to the right position.
15064 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
15065 text. */
15066 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
15067 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
15068 if (dvpos < 0)
15069 {
15070 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
15071 bottom_vpos, dvpos);
15072 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
15073 bottom_vpos, 0);
15074 }
15075 else if (dvpos > 0)
15076 {
15077 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
15078 bottom_vpos, dvpos);
15079 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
15080 first_unchanged_at_end_vpos + dvpos, 0);
15081 }
15082
15083 /* For frame-based redisplay, make sure that current frame and window
15084 matrix are in sync with respect to glyph memory. */
15085 if (!FRAME_WINDOW_P (f))
15086 sync_frame_with_window_matrix_rows (w);
15087
15088 /* Adjust buffer positions in reused rows. */
15089 if (delta || delta_bytes)
15090 increment_matrix_positions (current_matrix,
15091 first_unchanged_at_end_vpos + dvpos,
15092 bottom_vpos, delta, delta_bytes);
15093
15094 /* Adjust Y positions. */
15095 if (dy)
15096 shift_glyph_matrix (w, current_matrix,
15097 first_unchanged_at_end_vpos + dvpos,
15098 bottom_vpos, dy);
15099
15100 if (first_unchanged_at_end_row)
15101 {
15102 first_unchanged_at_end_row += dvpos;
15103 if (first_unchanged_at_end_row->y >= it.last_visible_y
15104 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
15105 first_unchanged_at_end_row = NULL;
15106 }
15107
15108 /* If scrolling up, there may be some lines to display at the end of
15109 the window. */
15110 last_text_row_at_end = NULL;
15111 if (dy < 0)
15112 {
15113 /* Scrolling up can leave for example a partially visible line
15114 at the end of the window to be redisplayed. */
15115 /* Set last_row to the glyph row in the current matrix where the
15116 window end line is found. It has been moved up or down in
15117 the matrix by dvpos. */
15118 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
15119 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
15120
15121 /* If last_row is the window end line, it should display text. */
15122 xassert (last_row->displays_text_p);
15123
15124 /* If window end line was partially visible before, begin
15125 displaying at that line. Otherwise begin displaying with the
15126 line following it. */
15127 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
15128 {
15129 init_to_row_start (&it, w, last_row);
15130 it.vpos = last_vpos;
15131 it.current_y = last_row->y;
15132 }
15133 else
15134 {
15135 init_to_row_end (&it, w, last_row);
15136 it.vpos = 1 + last_vpos;
15137 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
15138 ++last_row;
15139 }
15140
15141 /* We may start in a continuation line. If so, we have to
15142 get the right continuation_lines_width and current_x. */
15143 it.continuation_lines_width = last_row->continuation_lines_width;
15144 it.hpos = it.current_x = 0;
15145
15146 /* Display the rest of the lines at the window end. */
15147 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
15148 while (it.current_y < it.last_visible_y
15149 && !fonts_changed_p)
15150 {
15151 /* Is it always sure that the display agrees with lines in
15152 the current matrix? I don't think so, so we mark rows
15153 displayed invalid in the current matrix by setting their
15154 enabled_p flag to zero. */
15155 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
15156 if (display_line (&it))
15157 last_text_row_at_end = it.glyph_row - 1;
15158 }
15159 }
15160
15161 /* Update window_end_pos and window_end_vpos. */
15162 if (first_unchanged_at_end_row
15163 && !last_text_row_at_end)
15164 {
15165 /* Window end line if one of the preserved rows from the current
15166 matrix. Set row to the last row displaying text in current
15167 matrix starting at first_unchanged_at_end_row, after
15168 scrolling. */
15169 xassert (first_unchanged_at_end_row->displays_text_p);
15170 row = find_last_row_displaying_text (w->current_matrix, &it,
15171 first_unchanged_at_end_row);
15172 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
15173
15174 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
15175 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
15176 w->window_end_vpos
15177 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
15178 xassert (w->window_end_bytepos >= 0);
15179 IF_DEBUG (debug_method_add (w, "A"));
15180 }
15181 else if (last_text_row_at_end)
15182 {
15183 w->window_end_pos
15184 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
15185 w->window_end_bytepos
15186 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
15187 w->window_end_vpos
15188 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
15189 xassert (w->window_end_bytepos >= 0);
15190 IF_DEBUG (debug_method_add (w, "B"));
15191 }
15192 else if (last_text_row)
15193 {
15194 /* We have displayed either to the end of the window or at the
15195 end of the window, i.e. the last row with text is to be found
15196 in the desired matrix. */
15197 w->window_end_pos
15198 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
15199 w->window_end_bytepos
15200 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
15201 w->window_end_vpos
15202 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
15203 xassert (w->window_end_bytepos >= 0);
15204 }
15205 else if (first_unchanged_at_end_row == NULL
15206 && last_text_row == NULL
15207 && last_text_row_at_end == NULL)
15208 {
15209 /* Displayed to end of window, but no line containing text was
15210 displayed. Lines were deleted at the end of the window. */
15211 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
15212 int vpos = XFASTINT (w->window_end_vpos);
15213 struct glyph_row *current_row = current_matrix->rows + vpos;
15214 struct glyph_row *desired_row = desired_matrix->rows + vpos;
15215
15216 for (row = NULL;
15217 row == NULL && vpos >= first_vpos;
15218 --vpos, --current_row, --desired_row)
15219 {
15220 if (desired_row->enabled_p)
15221 {
15222 if (desired_row->displays_text_p)
15223 row = desired_row;
15224 }
15225 else if (current_row->displays_text_p)
15226 row = current_row;
15227 }
15228
15229 xassert (row != NULL);
15230 w->window_end_vpos = make_number (vpos + 1);
15231 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
15232 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
15233 xassert (w->window_end_bytepos >= 0);
15234 IF_DEBUG (debug_method_add (w, "C"));
15235 }
15236 else
15237 abort ();
15238
15239 #if 0 /* This leads to problems, for instance when the cursor is
15240 at ZV, and the cursor line displays no text. */
15241 /* Disable rows below what's displayed in the window. This makes
15242 debugging easier. */
15243 enable_glyph_matrix_rows (current_matrix,
15244 XFASTINT (w->window_end_vpos) + 1,
15245 bottom_vpos, 0);
15246 #endif
15247
15248 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
15249 debug_end_vpos = XFASTINT (w->window_end_vpos));
15250
15251 /* Record that display has not been completed. */
15252 w->window_end_valid = Qnil;
15253 w->desired_matrix->no_scrolling_p = 1;
15254 return 3;
15255
15256 #undef GIVE_UP
15257 }
15258
15259
15260 \f
15261 /***********************************************************************
15262 More debugging support
15263 ***********************************************************************/
15264
15265 #if GLYPH_DEBUG
15266
15267 void dump_glyph_row P_ ((struct glyph_row *, int, int));
15268 void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
15269 void dump_glyph P_ ((struct glyph_row *, struct glyph *, int));
15270
15271
15272 /* Dump the contents of glyph matrix MATRIX on stderr.
15273
15274 GLYPHS 0 means don't show glyph contents.
15275 GLYPHS 1 means show glyphs in short form
15276 GLYPHS > 1 means show glyphs in long form. */
15277
15278 void
15279 dump_glyph_matrix (matrix, glyphs)
15280 struct glyph_matrix *matrix;
15281 int glyphs;
15282 {
15283 int i;
15284 for (i = 0; i < matrix->nrows; ++i)
15285 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
15286 }
15287
15288
15289 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
15290 the glyph row and area where the glyph comes from. */
15291
15292 void
15293 dump_glyph (row, glyph, area)
15294 struct glyph_row *row;
15295 struct glyph *glyph;
15296 int area;
15297 {
15298 if (glyph->type == CHAR_GLYPH)
15299 {
15300 fprintf (stderr,
15301 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15302 glyph - row->glyphs[TEXT_AREA],
15303 'C',
15304 glyph->charpos,
15305 (BUFFERP (glyph->object)
15306 ? 'B'
15307 : (STRINGP (glyph->object)
15308 ? 'S'
15309 : '-')),
15310 glyph->pixel_width,
15311 glyph->u.ch,
15312 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
15313 ? glyph->u.ch
15314 : '.'),
15315 glyph->face_id,
15316 glyph->left_box_line_p,
15317 glyph->right_box_line_p);
15318 }
15319 else if (glyph->type == STRETCH_GLYPH)
15320 {
15321 fprintf (stderr,
15322 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15323 glyph - row->glyphs[TEXT_AREA],
15324 'S',
15325 glyph->charpos,
15326 (BUFFERP (glyph->object)
15327 ? 'B'
15328 : (STRINGP (glyph->object)
15329 ? 'S'
15330 : '-')),
15331 glyph->pixel_width,
15332 0,
15333 '.',
15334 glyph->face_id,
15335 glyph->left_box_line_p,
15336 glyph->right_box_line_p);
15337 }
15338 else if (glyph->type == IMAGE_GLYPH)
15339 {
15340 fprintf (stderr,
15341 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15342 glyph - row->glyphs[TEXT_AREA],
15343 'I',
15344 glyph->charpos,
15345 (BUFFERP (glyph->object)
15346 ? 'B'
15347 : (STRINGP (glyph->object)
15348 ? 'S'
15349 : '-')),
15350 glyph->pixel_width,
15351 glyph->u.img_id,
15352 '.',
15353 glyph->face_id,
15354 glyph->left_box_line_p,
15355 glyph->right_box_line_p);
15356 }
15357 else if (glyph->type == COMPOSITE_GLYPH)
15358 {
15359 fprintf (stderr,
15360 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15361 glyph - row->glyphs[TEXT_AREA],
15362 '+',
15363 glyph->charpos,
15364 (BUFFERP (glyph->object)
15365 ? 'B'
15366 : (STRINGP (glyph->object)
15367 ? 'S'
15368 : '-')),
15369 glyph->pixel_width,
15370 glyph->u.cmp_id,
15371 '.',
15372 glyph->face_id,
15373 glyph->left_box_line_p,
15374 glyph->right_box_line_p);
15375 }
15376 }
15377
15378
15379 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
15380 GLYPHS 0 means don't show glyph contents.
15381 GLYPHS 1 means show glyphs in short form
15382 GLYPHS > 1 means show glyphs in long form. */
15383
15384 void
15385 dump_glyph_row (row, vpos, glyphs)
15386 struct glyph_row *row;
15387 int vpos, glyphs;
15388 {
15389 if (glyphs != 1)
15390 {
15391 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
15392 fprintf (stderr, "======================================================================\n");
15393
15394 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d\
15395 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
15396 vpos,
15397 MATRIX_ROW_START_CHARPOS (row),
15398 MATRIX_ROW_END_CHARPOS (row),
15399 row->used[TEXT_AREA],
15400 row->contains_overlapping_glyphs_p,
15401 row->enabled_p,
15402 row->truncated_on_left_p,
15403 row->truncated_on_right_p,
15404 row->continued_p,
15405 MATRIX_ROW_CONTINUATION_LINE_P (row),
15406 row->displays_text_p,
15407 row->ends_at_zv_p,
15408 row->fill_line_p,
15409 row->ends_in_middle_of_char_p,
15410 row->starts_in_middle_of_char_p,
15411 row->mouse_face_p,
15412 row->x,
15413 row->y,
15414 row->pixel_width,
15415 row->height,
15416 row->visible_height,
15417 row->ascent,
15418 row->phys_ascent);
15419 fprintf (stderr, "%9d %5d\t%5d\n", row->start.overlay_string_index,
15420 row->end.overlay_string_index,
15421 row->continuation_lines_width);
15422 fprintf (stderr, "%9d %5d\n",
15423 CHARPOS (row->start.string_pos),
15424 CHARPOS (row->end.string_pos));
15425 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
15426 row->end.dpvec_index);
15427 }
15428
15429 if (glyphs > 1)
15430 {
15431 int area;
15432
15433 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
15434 {
15435 struct glyph *glyph = row->glyphs[area];
15436 struct glyph *glyph_end = glyph + row->used[area];
15437
15438 /* Glyph for a line end in text. */
15439 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
15440 ++glyph_end;
15441
15442 if (glyph < glyph_end)
15443 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
15444
15445 for (; glyph < glyph_end; ++glyph)
15446 dump_glyph (row, glyph, area);
15447 }
15448 }
15449 else if (glyphs == 1)
15450 {
15451 int area;
15452
15453 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
15454 {
15455 char *s = (char *) alloca (row->used[area] + 1);
15456 int i;
15457
15458 for (i = 0; i < row->used[area]; ++i)
15459 {
15460 struct glyph *glyph = row->glyphs[area] + i;
15461 if (glyph->type == CHAR_GLYPH
15462 && glyph->u.ch < 0x80
15463 && glyph->u.ch >= ' ')
15464 s[i] = glyph->u.ch;
15465 else
15466 s[i] = '.';
15467 }
15468
15469 s[i] = '\0';
15470 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
15471 }
15472 }
15473 }
15474
15475
15476 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
15477 Sdump_glyph_matrix, 0, 1, "p",
15478 doc: /* Dump the current matrix of the selected window to stderr.
15479 Shows contents of glyph row structures. With non-nil
15480 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
15481 glyphs in short form, otherwise show glyphs in long form. */)
15482 (glyphs)
15483 Lisp_Object glyphs;
15484 {
15485 struct window *w = XWINDOW (selected_window);
15486 struct buffer *buffer = XBUFFER (w->buffer);
15487
15488 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
15489 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
15490 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
15491 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
15492 fprintf (stderr, "=============================================\n");
15493 dump_glyph_matrix (w->current_matrix,
15494 NILP (glyphs) ? 0 : XINT (glyphs));
15495 return Qnil;
15496 }
15497
15498
15499 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
15500 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
15501 ()
15502 {
15503 struct frame *f = XFRAME (selected_frame);
15504 dump_glyph_matrix (f->current_matrix, 1);
15505 return Qnil;
15506 }
15507
15508
15509 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
15510 doc: /* Dump glyph row ROW to stderr.
15511 GLYPH 0 means don't dump glyphs.
15512 GLYPH 1 means dump glyphs in short form.
15513 GLYPH > 1 or omitted means dump glyphs in long form. */)
15514 (row, glyphs)
15515 Lisp_Object row, glyphs;
15516 {
15517 struct glyph_matrix *matrix;
15518 int vpos;
15519
15520 CHECK_NUMBER (row);
15521 matrix = XWINDOW (selected_window)->current_matrix;
15522 vpos = XINT (row);
15523 if (vpos >= 0 && vpos < matrix->nrows)
15524 dump_glyph_row (MATRIX_ROW (matrix, vpos),
15525 vpos,
15526 INTEGERP (glyphs) ? XINT (glyphs) : 2);
15527 return Qnil;
15528 }
15529
15530
15531 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
15532 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
15533 GLYPH 0 means don't dump glyphs.
15534 GLYPH 1 means dump glyphs in short form.
15535 GLYPH > 1 or omitted means dump glyphs in long form. */)
15536 (row, glyphs)
15537 Lisp_Object row, glyphs;
15538 {
15539 struct frame *sf = SELECTED_FRAME ();
15540 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
15541 int vpos;
15542
15543 CHECK_NUMBER (row);
15544 vpos = XINT (row);
15545 if (vpos >= 0 && vpos < m->nrows)
15546 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
15547 INTEGERP (glyphs) ? XINT (glyphs) : 2);
15548 return Qnil;
15549 }
15550
15551
15552 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
15553 doc: /* Toggle tracing of redisplay.
15554 With ARG, turn tracing on if and only if ARG is positive. */)
15555 (arg)
15556 Lisp_Object arg;
15557 {
15558 if (NILP (arg))
15559 trace_redisplay_p = !trace_redisplay_p;
15560 else
15561 {
15562 arg = Fprefix_numeric_value (arg);
15563 trace_redisplay_p = XINT (arg) > 0;
15564 }
15565
15566 return Qnil;
15567 }
15568
15569
15570 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
15571 doc: /* Like `format', but print result to stderr.
15572 usage: (trace-to-stderr STRING &rest OBJECTS) */)
15573 (nargs, args)
15574 int nargs;
15575 Lisp_Object *args;
15576 {
15577 Lisp_Object s = Fformat (nargs, args);
15578 fprintf (stderr, "%s", SDATA (s));
15579 return Qnil;
15580 }
15581
15582 #endif /* GLYPH_DEBUG */
15583
15584
15585 \f
15586 /***********************************************************************
15587 Building Desired Matrix Rows
15588 ***********************************************************************/
15589
15590 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
15591 Used for non-window-redisplay windows, and for windows w/o left fringe. */
15592
15593 static struct glyph_row *
15594 get_overlay_arrow_glyph_row (w, overlay_arrow_string)
15595 struct window *w;
15596 Lisp_Object overlay_arrow_string;
15597 {
15598 struct frame *f = XFRAME (WINDOW_FRAME (w));
15599 struct buffer *buffer = XBUFFER (w->buffer);
15600 struct buffer *old = current_buffer;
15601 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
15602 int arrow_len = SCHARS (overlay_arrow_string);
15603 const unsigned char *arrow_end = arrow_string + arrow_len;
15604 const unsigned char *p;
15605 struct it it;
15606 int multibyte_p;
15607 int n_glyphs_before;
15608
15609 set_buffer_temp (buffer);
15610 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
15611 it.glyph_row->used[TEXT_AREA] = 0;
15612 SET_TEXT_POS (it.position, 0, 0);
15613
15614 multibyte_p = !NILP (buffer->enable_multibyte_characters);
15615 p = arrow_string;
15616 while (p < arrow_end)
15617 {
15618 Lisp_Object face, ilisp;
15619
15620 /* Get the next character. */
15621 if (multibyte_p)
15622 it.c = string_char_and_length (p, arrow_len, &it.len);
15623 else
15624 it.c = *p, it.len = 1;
15625 p += it.len;
15626
15627 /* Get its face. */
15628 ilisp = make_number (p - arrow_string);
15629 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
15630 it.face_id = compute_char_face (f, it.c, face);
15631
15632 /* Compute its width, get its glyphs. */
15633 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
15634 SET_TEXT_POS (it.position, -1, -1);
15635 PRODUCE_GLYPHS (&it);
15636
15637 /* If this character doesn't fit any more in the line, we have
15638 to remove some glyphs. */
15639 if (it.current_x > it.last_visible_x)
15640 {
15641 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
15642 break;
15643 }
15644 }
15645
15646 set_buffer_temp (old);
15647 return it.glyph_row;
15648 }
15649
15650
15651 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
15652 glyphs are only inserted for terminal frames since we can't really
15653 win with truncation glyphs when partially visible glyphs are
15654 involved. Which glyphs to insert is determined by
15655 produce_special_glyphs. */
15656
15657 static void
15658 insert_left_trunc_glyphs (it)
15659 struct it *it;
15660 {
15661 struct it truncate_it;
15662 struct glyph *from, *end, *to, *toend;
15663
15664 xassert (!FRAME_WINDOW_P (it->f));
15665
15666 /* Get the truncation glyphs. */
15667 truncate_it = *it;
15668 truncate_it.current_x = 0;
15669 truncate_it.face_id = DEFAULT_FACE_ID;
15670 truncate_it.glyph_row = &scratch_glyph_row;
15671 truncate_it.glyph_row->used[TEXT_AREA] = 0;
15672 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
15673 truncate_it.object = make_number (0);
15674 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
15675
15676 /* Overwrite glyphs from IT with truncation glyphs. */
15677 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
15678 end = from + truncate_it.glyph_row->used[TEXT_AREA];
15679 to = it->glyph_row->glyphs[TEXT_AREA];
15680 toend = to + it->glyph_row->used[TEXT_AREA];
15681
15682 while (from < end)
15683 *to++ = *from++;
15684
15685 /* There may be padding glyphs left over. Overwrite them too. */
15686 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
15687 {
15688 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
15689 while (from < end)
15690 *to++ = *from++;
15691 }
15692
15693 if (to > toend)
15694 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
15695 }
15696
15697
15698 /* Compute the pixel height and width of IT->glyph_row.
15699
15700 Most of the time, ascent and height of a display line will be equal
15701 to the max_ascent and max_height values of the display iterator
15702 structure. This is not the case if
15703
15704 1. We hit ZV without displaying anything. In this case, max_ascent
15705 and max_height will be zero.
15706
15707 2. We have some glyphs that don't contribute to the line height.
15708 (The glyph row flag contributes_to_line_height_p is for future
15709 pixmap extensions).
15710
15711 The first case is easily covered by using default values because in
15712 these cases, the line height does not really matter, except that it
15713 must not be zero. */
15714
15715 static void
15716 compute_line_metrics (it)
15717 struct it *it;
15718 {
15719 struct glyph_row *row = it->glyph_row;
15720 int area, i;
15721
15722 if (FRAME_WINDOW_P (it->f))
15723 {
15724 int i, min_y, max_y;
15725
15726 /* The line may consist of one space only, that was added to
15727 place the cursor on it. If so, the row's height hasn't been
15728 computed yet. */
15729 if (row->height == 0)
15730 {
15731 if (it->max_ascent + it->max_descent == 0)
15732 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
15733 row->ascent = it->max_ascent;
15734 row->height = it->max_ascent + it->max_descent;
15735 row->phys_ascent = it->max_phys_ascent;
15736 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
15737 row->extra_line_spacing = it->max_extra_line_spacing;
15738 }
15739
15740 /* Compute the width of this line. */
15741 row->pixel_width = row->x;
15742 for (i = 0; i < row->used[TEXT_AREA]; ++i)
15743 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
15744
15745 xassert (row->pixel_width >= 0);
15746 xassert (row->ascent >= 0 && row->height > 0);
15747
15748 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
15749 || MATRIX_ROW_OVERLAPS_PRED_P (row));
15750
15751 /* If first line's physical ascent is larger than its logical
15752 ascent, use the physical ascent, and make the row taller.
15753 This makes accented characters fully visible. */
15754 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
15755 && row->phys_ascent > row->ascent)
15756 {
15757 row->height += row->phys_ascent - row->ascent;
15758 row->ascent = row->phys_ascent;
15759 }
15760
15761 /* Compute how much of the line is visible. */
15762 row->visible_height = row->height;
15763
15764 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
15765 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
15766
15767 if (row->y < min_y)
15768 row->visible_height -= min_y - row->y;
15769 if (row->y + row->height > max_y)
15770 row->visible_height -= row->y + row->height - max_y;
15771 }
15772 else
15773 {
15774 row->pixel_width = row->used[TEXT_AREA];
15775 if (row->continued_p)
15776 row->pixel_width -= it->continuation_pixel_width;
15777 else if (row->truncated_on_right_p)
15778 row->pixel_width -= it->truncation_pixel_width;
15779 row->ascent = row->phys_ascent = 0;
15780 row->height = row->phys_height = row->visible_height = 1;
15781 row->extra_line_spacing = 0;
15782 }
15783
15784 /* Compute a hash code for this row. */
15785 row->hash = 0;
15786 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
15787 for (i = 0; i < row->used[area]; ++i)
15788 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
15789 + row->glyphs[area][i].u.val
15790 + row->glyphs[area][i].face_id
15791 + row->glyphs[area][i].padding_p
15792 + (row->glyphs[area][i].type << 2));
15793
15794 it->max_ascent = it->max_descent = 0;
15795 it->max_phys_ascent = it->max_phys_descent = 0;
15796 }
15797
15798
15799 /* Append one space to the glyph row of iterator IT if doing a
15800 window-based redisplay. The space has the same face as
15801 IT->face_id. Value is non-zero if a space was added.
15802
15803 This function is called to make sure that there is always one glyph
15804 at the end of a glyph row that the cursor can be set on under
15805 window-systems. (If there weren't such a glyph we would not know
15806 how wide and tall a box cursor should be displayed).
15807
15808 At the same time this space let's a nicely handle clearing to the
15809 end of the line if the row ends in italic text. */
15810
15811 static int
15812 append_space_for_newline (it, default_face_p)
15813 struct it *it;
15814 int default_face_p;
15815 {
15816 if (FRAME_WINDOW_P (it->f))
15817 {
15818 int n = it->glyph_row->used[TEXT_AREA];
15819
15820 if (it->glyph_row->glyphs[TEXT_AREA] + n
15821 < it->glyph_row->glyphs[1 + TEXT_AREA])
15822 {
15823 /* Save some values that must not be changed.
15824 Must save IT->c and IT->len because otherwise
15825 ITERATOR_AT_END_P wouldn't work anymore after
15826 append_space_for_newline has been called. */
15827 enum display_element_type saved_what = it->what;
15828 int saved_c = it->c, saved_len = it->len;
15829 int saved_x = it->current_x;
15830 int saved_face_id = it->face_id;
15831 struct text_pos saved_pos;
15832 Lisp_Object saved_object;
15833 struct face *face;
15834
15835 saved_object = it->object;
15836 saved_pos = it->position;
15837
15838 it->what = IT_CHARACTER;
15839 bzero (&it->position, sizeof it->position);
15840 it->object = make_number (0);
15841 it->c = ' ';
15842 it->len = 1;
15843
15844 if (default_face_p)
15845 it->face_id = DEFAULT_FACE_ID;
15846 else if (it->face_before_selective_p)
15847 it->face_id = it->saved_face_id;
15848 face = FACE_FROM_ID (it->f, it->face_id);
15849 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
15850
15851 PRODUCE_GLYPHS (it);
15852
15853 it->override_ascent = -1;
15854 it->constrain_row_ascent_descent_p = 0;
15855 it->current_x = saved_x;
15856 it->object = saved_object;
15857 it->position = saved_pos;
15858 it->what = saved_what;
15859 it->face_id = saved_face_id;
15860 it->len = saved_len;
15861 it->c = saved_c;
15862 return 1;
15863 }
15864 }
15865
15866 return 0;
15867 }
15868
15869
15870 /* Extend the face of the last glyph in the text area of IT->glyph_row
15871 to the end of the display line. Called from display_line.
15872 If the glyph row is empty, add a space glyph to it so that we
15873 know the face to draw. Set the glyph row flag fill_line_p. */
15874
15875 static void
15876 extend_face_to_end_of_line (it)
15877 struct it *it;
15878 {
15879 struct face *face;
15880 struct frame *f = it->f;
15881
15882 /* If line is already filled, do nothing. */
15883 if (it->current_x >= it->last_visible_x)
15884 return;
15885
15886 /* Face extension extends the background and box of IT->face_id
15887 to the end of the line. If the background equals the background
15888 of the frame, we don't have to do anything. */
15889 if (it->face_before_selective_p)
15890 face = FACE_FROM_ID (it->f, it->saved_face_id);
15891 else
15892 face = FACE_FROM_ID (f, it->face_id);
15893
15894 if (FRAME_WINDOW_P (f)
15895 && it->glyph_row->displays_text_p
15896 && face->box == FACE_NO_BOX
15897 && face->background == FRAME_BACKGROUND_PIXEL (f)
15898 && !face->stipple)
15899 return;
15900
15901 /* Set the glyph row flag indicating that the face of the last glyph
15902 in the text area has to be drawn to the end of the text area. */
15903 it->glyph_row->fill_line_p = 1;
15904
15905 /* If current character of IT is not ASCII, make sure we have the
15906 ASCII face. This will be automatically undone the next time
15907 get_next_display_element returns a multibyte character. Note
15908 that the character will always be single byte in unibyte text. */
15909 if (!ASCII_CHAR_P (it->c))
15910 {
15911 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
15912 }
15913
15914 if (FRAME_WINDOW_P (f))
15915 {
15916 /* If the row is empty, add a space with the current face of IT,
15917 so that we know which face to draw. */
15918 if (it->glyph_row->used[TEXT_AREA] == 0)
15919 {
15920 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
15921 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
15922 it->glyph_row->used[TEXT_AREA] = 1;
15923 }
15924 }
15925 else
15926 {
15927 /* Save some values that must not be changed. */
15928 int saved_x = it->current_x;
15929 struct text_pos saved_pos;
15930 Lisp_Object saved_object;
15931 enum display_element_type saved_what = it->what;
15932 int saved_face_id = it->face_id;
15933
15934 saved_object = it->object;
15935 saved_pos = it->position;
15936
15937 it->what = IT_CHARACTER;
15938 bzero (&it->position, sizeof it->position);
15939 it->object = make_number (0);
15940 it->c = ' ';
15941 it->len = 1;
15942 it->face_id = face->id;
15943
15944 PRODUCE_GLYPHS (it);
15945
15946 while (it->current_x <= it->last_visible_x)
15947 PRODUCE_GLYPHS (it);
15948
15949 /* Don't count these blanks really. It would let us insert a left
15950 truncation glyph below and make us set the cursor on them, maybe. */
15951 it->current_x = saved_x;
15952 it->object = saved_object;
15953 it->position = saved_pos;
15954 it->what = saved_what;
15955 it->face_id = saved_face_id;
15956 }
15957 }
15958
15959
15960 /* Value is non-zero if text starting at CHARPOS in current_buffer is
15961 trailing whitespace. */
15962
15963 static int
15964 trailing_whitespace_p (charpos)
15965 int charpos;
15966 {
15967 int bytepos = CHAR_TO_BYTE (charpos);
15968 int c = 0;
15969
15970 while (bytepos < ZV_BYTE
15971 && (c = FETCH_CHAR (bytepos),
15972 c == ' ' || c == '\t'))
15973 ++bytepos;
15974
15975 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
15976 {
15977 if (bytepos != PT_BYTE)
15978 return 1;
15979 }
15980 return 0;
15981 }
15982
15983
15984 /* Highlight trailing whitespace, if any, in ROW. */
15985
15986 void
15987 highlight_trailing_whitespace (f, row)
15988 struct frame *f;
15989 struct glyph_row *row;
15990 {
15991 int used = row->used[TEXT_AREA];
15992
15993 if (used)
15994 {
15995 struct glyph *start = row->glyphs[TEXT_AREA];
15996 struct glyph *glyph = start + used - 1;
15997
15998 /* Skip over glyphs inserted to display the cursor at the
15999 end of a line, for extending the face of the last glyph
16000 to the end of the line on terminals, and for truncation
16001 and continuation glyphs. */
16002 while (glyph >= start
16003 && glyph->type == CHAR_GLYPH
16004 && INTEGERP (glyph->object))
16005 --glyph;
16006
16007 /* If last glyph is a space or stretch, and it's trailing
16008 whitespace, set the face of all trailing whitespace glyphs in
16009 IT->glyph_row to `trailing-whitespace'. */
16010 if (glyph >= start
16011 && BUFFERP (glyph->object)
16012 && (glyph->type == STRETCH_GLYPH
16013 || (glyph->type == CHAR_GLYPH
16014 && glyph->u.ch == ' '))
16015 && trailing_whitespace_p (glyph->charpos))
16016 {
16017 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
16018 if (face_id < 0)
16019 return;
16020
16021 while (glyph >= start
16022 && BUFFERP (glyph->object)
16023 && (glyph->type == STRETCH_GLYPH
16024 || (glyph->type == CHAR_GLYPH
16025 && glyph->u.ch == ' ')))
16026 (glyph--)->face_id = face_id;
16027 }
16028 }
16029 }
16030
16031
16032 /* Value is non-zero if glyph row ROW in window W should be
16033 used to hold the cursor. */
16034
16035 static int
16036 cursor_row_p (w, row)
16037 struct window *w;
16038 struct glyph_row *row;
16039 {
16040 int cursor_row_p = 1;
16041
16042 if (PT == MATRIX_ROW_END_CHARPOS (row))
16043 {
16044 /* Suppose the row ends on a string.
16045 Unless the row is continued, that means it ends on a newline
16046 in the string. If it's anything other than a display string
16047 (e.g. a before-string from an overlay), we don't want the
16048 cursor there. (This heuristic seems to give the optimal
16049 behavior for the various types of multi-line strings.) */
16050 if (CHARPOS (row->end.string_pos) >= 0)
16051 {
16052 if (row->continued_p)
16053 cursor_row_p = 1;
16054 else
16055 {
16056 /* Check for `display' property. */
16057 struct glyph *beg = row->glyphs[TEXT_AREA];
16058 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
16059 struct glyph *glyph;
16060
16061 cursor_row_p = 0;
16062 for (glyph = end; glyph >= beg; --glyph)
16063 if (STRINGP (glyph->object))
16064 {
16065 Lisp_Object prop
16066 = Fget_char_property (make_number (PT),
16067 Qdisplay, Qnil);
16068 cursor_row_p =
16069 (!NILP (prop)
16070 && display_prop_string_p (prop, glyph->object));
16071 break;
16072 }
16073 }
16074 }
16075 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
16076 {
16077 /* If the row ends in middle of a real character,
16078 and the line is continued, we want the cursor here.
16079 That's because MATRIX_ROW_END_CHARPOS would equal
16080 PT if PT is before the character. */
16081 if (!row->ends_in_ellipsis_p)
16082 cursor_row_p = row->continued_p;
16083 else
16084 /* If the row ends in an ellipsis, then
16085 MATRIX_ROW_END_CHARPOS will equal point after the invisible text.
16086 We want that position to be displayed after the ellipsis. */
16087 cursor_row_p = 0;
16088 }
16089 /* If the row ends at ZV, display the cursor at the end of that
16090 row instead of at the start of the row below. */
16091 else if (row->ends_at_zv_p)
16092 cursor_row_p = 1;
16093 else
16094 cursor_row_p = 0;
16095 }
16096
16097 return cursor_row_p;
16098 }
16099
16100
16101 /* Construct the glyph row IT->glyph_row in the desired matrix of
16102 IT->w from text at the current position of IT. See dispextern.h
16103 for an overview of struct it. Value is non-zero if
16104 IT->glyph_row displays text, as opposed to a line displaying ZV
16105 only. */
16106
16107 static int
16108 display_line (it)
16109 struct it *it;
16110 {
16111 struct glyph_row *row = it->glyph_row;
16112 Lisp_Object overlay_arrow_string;
16113
16114 /* We always start displaying at hpos zero even if hscrolled. */
16115 xassert (it->hpos == 0 && it->current_x == 0);
16116
16117 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
16118 >= it->w->desired_matrix->nrows)
16119 {
16120 it->w->nrows_scale_factor++;
16121 fonts_changed_p = 1;
16122 return 0;
16123 }
16124
16125 /* Is IT->w showing the region? */
16126 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
16127
16128 /* Clear the result glyph row and enable it. */
16129 prepare_desired_row (row);
16130
16131 row->y = it->current_y;
16132 row->start = it->start;
16133 row->continuation_lines_width = it->continuation_lines_width;
16134 row->displays_text_p = 1;
16135 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
16136 it->starts_in_middle_of_char_p = 0;
16137
16138 /* Arrange the overlays nicely for our purposes. Usually, we call
16139 display_line on only one line at a time, in which case this
16140 can't really hurt too much, or we call it on lines which appear
16141 one after another in the buffer, in which case all calls to
16142 recenter_overlay_lists but the first will be pretty cheap. */
16143 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
16144
16145 /* Move over display elements that are not visible because we are
16146 hscrolled. This may stop at an x-position < IT->first_visible_x
16147 if the first glyph is partially visible or if we hit a line end. */
16148 if (it->current_x < it->first_visible_x)
16149 {
16150 move_it_in_display_line_to (it, ZV, it->first_visible_x,
16151 MOVE_TO_POS | MOVE_TO_X);
16152 }
16153
16154 /* Get the initial row height. This is either the height of the
16155 text hscrolled, if there is any, or zero. */
16156 row->ascent = it->max_ascent;
16157 row->height = it->max_ascent + it->max_descent;
16158 row->phys_ascent = it->max_phys_ascent;
16159 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
16160 row->extra_line_spacing = it->max_extra_line_spacing;
16161
16162 /* Loop generating characters. The loop is left with IT on the next
16163 character to display. */
16164 while (1)
16165 {
16166 int n_glyphs_before, hpos_before, x_before;
16167 int x, i, nglyphs;
16168 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
16169
16170 /* Retrieve the next thing to display. Value is zero if end of
16171 buffer reached. */
16172 if (!get_next_display_element (it))
16173 {
16174 /* Maybe add a space at the end of this line that is used to
16175 display the cursor there under X. Set the charpos of the
16176 first glyph of blank lines not corresponding to any text
16177 to -1. */
16178 #ifdef HAVE_WINDOW_SYSTEM
16179 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16180 row->exact_window_width_line_p = 1;
16181 else
16182 #endif /* HAVE_WINDOW_SYSTEM */
16183 if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
16184 || row->used[TEXT_AREA] == 0)
16185 {
16186 row->glyphs[TEXT_AREA]->charpos = -1;
16187 row->displays_text_p = 0;
16188
16189 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines)
16190 && (!MINI_WINDOW_P (it->w)
16191 || (minibuf_level && EQ (it->window, minibuf_window))))
16192 row->indicate_empty_line_p = 1;
16193 }
16194
16195 it->continuation_lines_width = 0;
16196 row->ends_at_zv_p = 1;
16197 break;
16198 }
16199
16200 /* Now, get the metrics of what we want to display. This also
16201 generates glyphs in `row' (which is IT->glyph_row). */
16202 n_glyphs_before = row->used[TEXT_AREA];
16203 x = it->current_x;
16204
16205 /* Remember the line height so far in case the next element doesn't
16206 fit on the line. */
16207 if (!it->truncate_lines_p)
16208 {
16209 ascent = it->max_ascent;
16210 descent = it->max_descent;
16211 phys_ascent = it->max_phys_ascent;
16212 phys_descent = it->max_phys_descent;
16213 }
16214
16215 PRODUCE_GLYPHS (it);
16216
16217 /* If this display element was in marginal areas, continue with
16218 the next one. */
16219 if (it->area != TEXT_AREA)
16220 {
16221 row->ascent = max (row->ascent, it->max_ascent);
16222 row->height = max (row->height, it->max_ascent + it->max_descent);
16223 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
16224 row->phys_height = max (row->phys_height,
16225 it->max_phys_ascent + it->max_phys_descent);
16226 row->extra_line_spacing = max (row->extra_line_spacing,
16227 it->max_extra_line_spacing);
16228 set_iterator_to_next (it, 1);
16229 continue;
16230 }
16231
16232 /* Does the display element fit on the line? If we truncate
16233 lines, we should draw past the right edge of the window. If
16234 we don't truncate, we want to stop so that we can display the
16235 continuation glyph before the right margin. If lines are
16236 continued, there are two possible strategies for characters
16237 resulting in more than 1 glyph (e.g. tabs): Display as many
16238 glyphs as possible in this line and leave the rest for the
16239 continuation line, or display the whole element in the next
16240 line. Original redisplay did the former, so we do it also. */
16241 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
16242 hpos_before = it->hpos;
16243 x_before = x;
16244
16245 if (/* Not a newline. */
16246 nglyphs > 0
16247 /* Glyphs produced fit entirely in the line. */
16248 && it->current_x < it->last_visible_x)
16249 {
16250 it->hpos += nglyphs;
16251 row->ascent = max (row->ascent, it->max_ascent);
16252 row->height = max (row->height, it->max_ascent + it->max_descent);
16253 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
16254 row->phys_height = max (row->phys_height,
16255 it->max_phys_ascent + it->max_phys_descent);
16256 row->extra_line_spacing = max (row->extra_line_spacing,
16257 it->max_extra_line_spacing);
16258 if (it->current_x - it->pixel_width < it->first_visible_x)
16259 row->x = x - it->first_visible_x;
16260 }
16261 else
16262 {
16263 int new_x;
16264 struct glyph *glyph;
16265
16266 for (i = 0; i < nglyphs; ++i, x = new_x)
16267 {
16268 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
16269 new_x = x + glyph->pixel_width;
16270
16271 if (/* Lines are continued. */
16272 !it->truncate_lines_p
16273 && (/* Glyph doesn't fit on the line. */
16274 new_x > it->last_visible_x
16275 /* Or it fits exactly on a window system frame. */
16276 || (new_x == it->last_visible_x
16277 && FRAME_WINDOW_P (it->f))))
16278 {
16279 /* End of a continued line. */
16280
16281 if (it->hpos == 0
16282 || (new_x == it->last_visible_x
16283 && FRAME_WINDOW_P (it->f)))
16284 {
16285 /* Current glyph is the only one on the line or
16286 fits exactly on the line. We must continue
16287 the line because we can't draw the cursor
16288 after the glyph. */
16289 row->continued_p = 1;
16290 it->current_x = new_x;
16291 it->continuation_lines_width += new_x;
16292 ++it->hpos;
16293 if (i == nglyphs - 1)
16294 {
16295 set_iterator_to_next (it, 1);
16296 #ifdef HAVE_WINDOW_SYSTEM
16297 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16298 {
16299 if (!get_next_display_element (it))
16300 {
16301 row->exact_window_width_line_p = 1;
16302 it->continuation_lines_width = 0;
16303 row->continued_p = 0;
16304 row->ends_at_zv_p = 1;
16305 }
16306 else if (ITERATOR_AT_END_OF_LINE_P (it))
16307 {
16308 row->continued_p = 0;
16309 row->exact_window_width_line_p = 1;
16310 }
16311 }
16312 #endif /* HAVE_WINDOW_SYSTEM */
16313 }
16314 }
16315 else if (CHAR_GLYPH_PADDING_P (*glyph)
16316 && !FRAME_WINDOW_P (it->f))
16317 {
16318 /* A padding glyph that doesn't fit on this line.
16319 This means the whole character doesn't fit
16320 on the line. */
16321 row->used[TEXT_AREA] = n_glyphs_before;
16322
16323 /* Fill the rest of the row with continuation
16324 glyphs like in 20.x. */
16325 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
16326 < row->glyphs[1 + TEXT_AREA])
16327 produce_special_glyphs (it, IT_CONTINUATION);
16328
16329 row->continued_p = 1;
16330 it->current_x = x_before;
16331 it->continuation_lines_width += x_before;
16332
16333 /* Restore the height to what it was before the
16334 element not fitting on the line. */
16335 it->max_ascent = ascent;
16336 it->max_descent = descent;
16337 it->max_phys_ascent = phys_ascent;
16338 it->max_phys_descent = phys_descent;
16339 }
16340 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
16341 {
16342 /* A TAB that extends past the right edge of the
16343 window. This produces a single glyph on
16344 window system frames. We leave the glyph in
16345 this row and let it fill the row, but don't
16346 consume the TAB. */
16347 it->continuation_lines_width += it->last_visible_x;
16348 row->ends_in_middle_of_char_p = 1;
16349 row->continued_p = 1;
16350 glyph->pixel_width = it->last_visible_x - x;
16351 it->starts_in_middle_of_char_p = 1;
16352 }
16353 else
16354 {
16355 /* Something other than a TAB that draws past
16356 the right edge of the window. Restore
16357 positions to values before the element. */
16358 row->used[TEXT_AREA] = n_glyphs_before + i;
16359
16360 /* Display continuation glyphs. */
16361 if (!FRAME_WINDOW_P (it->f))
16362 produce_special_glyphs (it, IT_CONTINUATION);
16363 row->continued_p = 1;
16364
16365 it->current_x = x_before;
16366 it->continuation_lines_width += x;
16367 extend_face_to_end_of_line (it);
16368
16369 if (nglyphs > 1 && i > 0)
16370 {
16371 row->ends_in_middle_of_char_p = 1;
16372 it->starts_in_middle_of_char_p = 1;
16373 }
16374
16375 /* Restore the height to what it was before the
16376 element not fitting on the line. */
16377 it->max_ascent = ascent;
16378 it->max_descent = descent;
16379 it->max_phys_ascent = phys_ascent;
16380 it->max_phys_descent = phys_descent;
16381 }
16382
16383 break;
16384 }
16385 else if (new_x > it->first_visible_x)
16386 {
16387 /* Increment number of glyphs actually displayed. */
16388 ++it->hpos;
16389
16390 if (x < it->first_visible_x)
16391 /* Glyph is partially visible, i.e. row starts at
16392 negative X position. */
16393 row->x = x - it->first_visible_x;
16394 }
16395 else
16396 {
16397 /* Glyph is completely off the left margin of the
16398 window. This should not happen because of the
16399 move_it_in_display_line at the start of this
16400 function, unless the text display area of the
16401 window is empty. */
16402 xassert (it->first_visible_x <= it->last_visible_x);
16403 }
16404 }
16405
16406 row->ascent = max (row->ascent, it->max_ascent);
16407 row->height = max (row->height, it->max_ascent + it->max_descent);
16408 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
16409 row->phys_height = max (row->phys_height,
16410 it->max_phys_ascent + it->max_phys_descent);
16411 row->extra_line_spacing = max (row->extra_line_spacing,
16412 it->max_extra_line_spacing);
16413
16414 /* End of this display line if row is continued. */
16415 if (row->continued_p || row->ends_at_zv_p)
16416 break;
16417 }
16418
16419 at_end_of_line:
16420 /* Is this a line end? If yes, we're also done, after making
16421 sure that a non-default face is extended up to the right
16422 margin of the window. */
16423 if (ITERATOR_AT_END_OF_LINE_P (it))
16424 {
16425 int used_before = row->used[TEXT_AREA];
16426
16427 row->ends_in_newline_from_string_p = STRINGP (it->object);
16428
16429 #ifdef HAVE_WINDOW_SYSTEM
16430 /* Add a space at the end of the line that is used to
16431 display the cursor there. */
16432 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16433 append_space_for_newline (it, 0);
16434 #endif /* HAVE_WINDOW_SYSTEM */
16435
16436 /* Extend the face to the end of the line. */
16437 extend_face_to_end_of_line (it);
16438
16439 /* Make sure we have the position. */
16440 if (used_before == 0)
16441 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
16442
16443 /* Consume the line end. This skips over invisible lines. */
16444 set_iterator_to_next (it, 1);
16445 it->continuation_lines_width = 0;
16446 break;
16447 }
16448
16449 /* Proceed with next display element. Note that this skips
16450 over lines invisible because of selective display. */
16451 set_iterator_to_next (it, 1);
16452
16453 /* If we truncate lines, we are done when the last displayed
16454 glyphs reach past the right margin of the window. */
16455 if (it->truncate_lines_p
16456 && (FRAME_WINDOW_P (it->f)
16457 ? (it->current_x >= it->last_visible_x)
16458 : (it->current_x > it->last_visible_x)))
16459 {
16460 /* Maybe add truncation glyphs. */
16461 if (!FRAME_WINDOW_P (it->f))
16462 {
16463 int i, n;
16464
16465 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
16466 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
16467 break;
16468
16469 for (n = row->used[TEXT_AREA]; i < n; ++i)
16470 {
16471 row->used[TEXT_AREA] = i;
16472 produce_special_glyphs (it, IT_TRUNCATION);
16473 }
16474 }
16475 #ifdef HAVE_WINDOW_SYSTEM
16476 else
16477 {
16478 /* Don't truncate if we can overflow newline into fringe. */
16479 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16480 {
16481 if (!get_next_display_element (it))
16482 {
16483 it->continuation_lines_width = 0;
16484 row->ends_at_zv_p = 1;
16485 row->exact_window_width_line_p = 1;
16486 break;
16487 }
16488 if (ITERATOR_AT_END_OF_LINE_P (it))
16489 {
16490 row->exact_window_width_line_p = 1;
16491 goto at_end_of_line;
16492 }
16493 }
16494 }
16495 #endif /* HAVE_WINDOW_SYSTEM */
16496
16497 row->truncated_on_right_p = 1;
16498 it->continuation_lines_width = 0;
16499 reseat_at_next_visible_line_start (it, 0);
16500 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
16501 it->hpos = hpos_before;
16502 it->current_x = x_before;
16503 break;
16504 }
16505 }
16506
16507 /* If line is not empty and hscrolled, maybe insert truncation glyphs
16508 at the left window margin. */
16509 if (it->first_visible_x
16510 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
16511 {
16512 if (!FRAME_WINDOW_P (it->f))
16513 insert_left_trunc_glyphs (it);
16514 row->truncated_on_left_p = 1;
16515 }
16516
16517 /* If the start of this line is the overlay arrow-position, then
16518 mark this glyph row as the one containing the overlay arrow.
16519 This is clearly a mess with variable size fonts. It would be
16520 better to let it be displayed like cursors under X. */
16521 if ((row->displays_text_p || !overlay_arrow_seen)
16522 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
16523 !NILP (overlay_arrow_string)))
16524 {
16525 /* Overlay arrow in window redisplay is a fringe bitmap. */
16526 if (STRINGP (overlay_arrow_string))
16527 {
16528 struct glyph_row *arrow_row
16529 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
16530 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
16531 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
16532 struct glyph *p = row->glyphs[TEXT_AREA];
16533 struct glyph *p2, *end;
16534
16535 /* Copy the arrow glyphs. */
16536 while (glyph < arrow_end)
16537 *p++ = *glyph++;
16538
16539 /* Throw away padding glyphs. */
16540 p2 = p;
16541 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
16542 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
16543 ++p2;
16544 if (p2 > p)
16545 {
16546 while (p2 < end)
16547 *p++ = *p2++;
16548 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
16549 }
16550 }
16551 else
16552 {
16553 xassert (INTEGERP (overlay_arrow_string));
16554 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
16555 }
16556 overlay_arrow_seen = 1;
16557 }
16558
16559 /* Compute pixel dimensions of this line. */
16560 compute_line_metrics (it);
16561
16562 /* Remember the position at which this line ends. */
16563 row->end = it->current;
16564
16565 /* Record whether this row ends inside an ellipsis. */
16566 row->ends_in_ellipsis_p
16567 = (it->method == GET_FROM_DISPLAY_VECTOR
16568 && it->ellipsis_p);
16569
16570 /* Save fringe bitmaps in this row. */
16571 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
16572 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
16573 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
16574 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
16575
16576 it->left_user_fringe_bitmap = 0;
16577 it->left_user_fringe_face_id = 0;
16578 it->right_user_fringe_bitmap = 0;
16579 it->right_user_fringe_face_id = 0;
16580
16581 /* Maybe set the cursor. */
16582 if (it->w->cursor.vpos < 0
16583 && PT >= MATRIX_ROW_START_CHARPOS (row)
16584 && PT <= MATRIX_ROW_END_CHARPOS (row)
16585 && cursor_row_p (it->w, row))
16586 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
16587
16588 /* Highlight trailing whitespace. */
16589 if (!NILP (Vshow_trailing_whitespace))
16590 highlight_trailing_whitespace (it->f, it->glyph_row);
16591
16592 /* Prepare for the next line. This line starts horizontally at (X
16593 HPOS) = (0 0). Vertical positions are incremented. As a
16594 convenience for the caller, IT->glyph_row is set to the next
16595 row to be used. */
16596 it->current_x = it->hpos = 0;
16597 it->current_y += row->height;
16598 ++it->vpos;
16599 ++it->glyph_row;
16600 it->start = it->current;
16601 return row->displays_text_p;
16602 }
16603
16604
16605 \f
16606 /***********************************************************************
16607 Menu Bar
16608 ***********************************************************************/
16609
16610 /* Redisplay the menu bar in the frame for window W.
16611
16612 The menu bar of X frames that don't have X toolkit support is
16613 displayed in a special window W->frame->menu_bar_window.
16614
16615 The menu bar of terminal frames is treated specially as far as
16616 glyph matrices are concerned. Menu bar lines are not part of
16617 windows, so the update is done directly on the frame matrix rows
16618 for the menu bar. */
16619
16620 static void
16621 display_menu_bar (w)
16622 struct window *w;
16623 {
16624 struct frame *f = XFRAME (WINDOW_FRAME (w));
16625 struct it it;
16626 Lisp_Object items;
16627 int i;
16628
16629 /* Don't do all this for graphical frames. */
16630 #ifdef HAVE_NTGUI
16631 if (FRAME_W32_P (f))
16632 return;
16633 #endif
16634 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
16635 if (FRAME_X_P (f))
16636 return;
16637 #endif
16638 #ifdef MAC_OS
16639 if (FRAME_MAC_P (f))
16640 return;
16641 #endif
16642
16643 #ifdef USE_X_TOOLKIT
16644 xassert (!FRAME_WINDOW_P (f));
16645 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
16646 it.first_visible_x = 0;
16647 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
16648 #else /* not USE_X_TOOLKIT */
16649 if (FRAME_WINDOW_P (f))
16650 {
16651 /* Menu bar lines are displayed in the desired matrix of the
16652 dummy window menu_bar_window. */
16653 struct window *menu_w;
16654 xassert (WINDOWP (f->menu_bar_window));
16655 menu_w = XWINDOW (f->menu_bar_window);
16656 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
16657 MENU_FACE_ID);
16658 it.first_visible_x = 0;
16659 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
16660 }
16661 else
16662 {
16663 /* This is a TTY frame, i.e. character hpos/vpos are used as
16664 pixel x/y. */
16665 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
16666 MENU_FACE_ID);
16667 it.first_visible_x = 0;
16668 it.last_visible_x = FRAME_COLS (f);
16669 }
16670 #endif /* not USE_X_TOOLKIT */
16671
16672 if (! mode_line_inverse_video)
16673 /* Force the menu-bar to be displayed in the default face. */
16674 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
16675
16676 /* Clear all rows of the menu bar. */
16677 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
16678 {
16679 struct glyph_row *row = it.glyph_row + i;
16680 clear_glyph_row (row);
16681 row->enabled_p = 1;
16682 row->full_width_p = 1;
16683 }
16684
16685 /* Display all items of the menu bar. */
16686 items = FRAME_MENU_BAR_ITEMS (it.f);
16687 for (i = 0; i < XVECTOR (items)->size; i += 4)
16688 {
16689 Lisp_Object string;
16690
16691 /* Stop at nil string. */
16692 string = AREF (items, i + 1);
16693 if (NILP (string))
16694 break;
16695
16696 /* Remember where item was displayed. */
16697 AREF (items, i + 3) = make_number (it.hpos);
16698
16699 /* Display the item, pad with one space. */
16700 if (it.current_x < it.last_visible_x)
16701 display_string (NULL, string, Qnil, 0, 0, &it,
16702 SCHARS (string) + 1, 0, 0, -1);
16703 }
16704
16705 /* Fill out the line with spaces. */
16706 if (it.current_x < it.last_visible_x)
16707 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
16708
16709 /* Compute the total height of the lines. */
16710 compute_line_metrics (&it);
16711 }
16712
16713
16714 \f
16715 /***********************************************************************
16716 Mode Line
16717 ***********************************************************************/
16718
16719 /* Redisplay mode lines in the window tree whose root is WINDOW. If
16720 FORCE is non-zero, redisplay mode lines unconditionally.
16721 Otherwise, redisplay only mode lines that are garbaged. Value is
16722 the number of windows whose mode lines were redisplayed. */
16723
16724 static int
16725 redisplay_mode_lines (window, force)
16726 Lisp_Object window;
16727 int force;
16728 {
16729 int nwindows = 0;
16730
16731 while (!NILP (window))
16732 {
16733 struct window *w = XWINDOW (window);
16734
16735 if (WINDOWP (w->hchild))
16736 nwindows += redisplay_mode_lines (w->hchild, force);
16737 else if (WINDOWP (w->vchild))
16738 nwindows += redisplay_mode_lines (w->vchild, force);
16739 else if (force
16740 || FRAME_GARBAGED_P (XFRAME (w->frame))
16741 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
16742 {
16743 struct text_pos lpoint;
16744 struct buffer *old = current_buffer;
16745
16746 /* Set the window's buffer for the mode line display. */
16747 SET_TEXT_POS (lpoint, PT, PT_BYTE);
16748 set_buffer_internal_1 (XBUFFER (w->buffer));
16749
16750 /* Point refers normally to the selected window. For any
16751 other window, set up appropriate value. */
16752 if (!EQ (window, selected_window))
16753 {
16754 struct text_pos pt;
16755
16756 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
16757 if (CHARPOS (pt) < BEGV)
16758 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
16759 else if (CHARPOS (pt) > (ZV - 1))
16760 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
16761 else
16762 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
16763 }
16764
16765 /* Display mode lines. */
16766 clear_glyph_matrix (w->desired_matrix);
16767 if (display_mode_lines (w))
16768 {
16769 ++nwindows;
16770 w->must_be_updated_p = 1;
16771 }
16772
16773 /* Restore old settings. */
16774 set_buffer_internal_1 (old);
16775 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
16776 }
16777
16778 window = w->next;
16779 }
16780
16781 return nwindows;
16782 }
16783
16784
16785 /* Display the mode and/or top line of window W. Value is the number
16786 of mode lines displayed. */
16787
16788 static int
16789 display_mode_lines (w)
16790 struct window *w;
16791 {
16792 Lisp_Object old_selected_window, old_selected_frame;
16793 int n = 0;
16794
16795 old_selected_frame = selected_frame;
16796 selected_frame = w->frame;
16797 old_selected_window = selected_window;
16798 XSETWINDOW (selected_window, w);
16799
16800 /* These will be set while the mode line specs are processed. */
16801 line_number_displayed = 0;
16802 w->column_number_displayed = Qnil;
16803
16804 if (WINDOW_WANTS_MODELINE_P (w))
16805 {
16806 struct window *sel_w = XWINDOW (old_selected_window);
16807
16808 /* Select mode line face based on the real selected window. */
16809 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
16810 current_buffer->mode_line_format);
16811 ++n;
16812 }
16813
16814 if (WINDOW_WANTS_HEADER_LINE_P (w))
16815 {
16816 display_mode_line (w, HEADER_LINE_FACE_ID,
16817 current_buffer->header_line_format);
16818 ++n;
16819 }
16820
16821 selected_frame = old_selected_frame;
16822 selected_window = old_selected_window;
16823 return n;
16824 }
16825
16826
16827 /* Display mode or top line of window W. FACE_ID specifies which line
16828 to display; it is either MODE_LINE_FACE_ID or HEADER_LINE_FACE_ID.
16829 FORMAT is the mode line format to display. Value is the pixel
16830 height of the mode line displayed. */
16831
16832 static int
16833 display_mode_line (w, face_id, format)
16834 struct window *w;
16835 enum face_id face_id;
16836 Lisp_Object format;
16837 {
16838 struct it it;
16839 struct face *face;
16840 int count = SPECPDL_INDEX ();
16841
16842 init_iterator (&it, w, -1, -1, NULL, face_id);
16843 /* Don't extend on a previously drawn mode-line.
16844 This may happen if called from pos_visible_p. */
16845 it.glyph_row->enabled_p = 0;
16846 prepare_desired_row (it.glyph_row);
16847
16848 it.glyph_row->mode_line_p = 1;
16849
16850 if (! mode_line_inverse_video)
16851 /* Force the mode-line to be displayed in the default face. */
16852 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
16853
16854 record_unwind_protect (unwind_format_mode_line,
16855 format_mode_line_unwind_data (NULL, 0));
16856
16857 mode_line_target = MODE_LINE_DISPLAY;
16858
16859 /* Temporarily make frame's keyboard the current kboard so that
16860 kboard-local variables in the mode_line_format will get the right
16861 values. */
16862 push_kboard (FRAME_KBOARD (it.f));
16863 record_unwind_save_match_data ();
16864 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
16865 pop_kboard ();
16866
16867 unbind_to (count, Qnil);
16868
16869 /* Fill up with spaces. */
16870 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
16871
16872 compute_line_metrics (&it);
16873 it.glyph_row->full_width_p = 1;
16874 it.glyph_row->continued_p = 0;
16875 it.glyph_row->truncated_on_left_p = 0;
16876 it.glyph_row->truncated_on_right_p = 0;
16877
16878 /* Make a 3D mode-line have a shadow at its right end. */
16879 face = FACE_FROM_ID (it.f, face_id);
16880 extend_face_to_end_of_line (&it);
16881 if (face->box != FACE_NO_BOX)
16882 {
16883 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
16884 + it.glyph_row->used[TEXT_AREA] - 1);
16885 last->right_box_line_p = 1;
16886 }
16887
16888 return it.glyph_row->height;
16889 }
16890
16891 /* Move element ELT in LIST to the front of LIST.
16892 Return the updated list. */
16893
16894 static Lisp_Object
16895 move_elt_to_front (elt, list)
16896 Lisp_Object elt, list;
16897 {
16898 register Lisp_Object tail, prev;
16899 register Lisp_Object tem;
16900
16901 tail = list;
16902 prev = Qnil;
16903 while (CONSP (tail))
16904 {
16905 tem = XCAR (tail);
16906
16907 if (EQ (elt, tem))
16908 {
16909 /* Splice out the link TAIL. */
16910 if (NILP (prev))
16911 list = XCDR (tail);
16912 else
16913 Fsetcdr (prev, XCDR (tail));
16914
16915 /* Now make it the first. */
16916 Fsetcdr (tail, list);
16917 return tail;
16918 }
16919 else
16920 prev = tail;
16921 tail = XCDR (tail);
16922 QUIT;
16923 }
16924
16925 /* Not found--return unchanged LIST. */
16926 return list;
16927 }
16928
16929 /* Contribute ELT to the mode line for window IT->w. How it
16930 translates into text depends on its data type.
16931
16932 IT describes the display environment in which we display, as usual.
16933
16934 DEPTH is the depth in recursion. It is used to prevent
16935 infinite recursion here.
16936
16937 FIELD_WIDTH is the number of characters the display of ELT should
16938 occupy in the mode line, and PRECISION is the maximum number of
16939 characters to display from ELT's representation. See
16940 display_string for details.
16941
16942 Returns the hpos of the end of the text generated by ELT.
16943
16944 PROPS is a property list to add to any string we encounter.
16945
16946 If RISKY is nonzero, remove (disregard) any properties in any string
16947 we encounter, and ignore :eval and :propertize.
16948
16949 The global variable `mode_line_target' determines whether the
16950 output is passed to `store_mode_line_noprop',
16951 `store_mode_line_string', or `display_string'. */
16952
16953 static int
16954 display_mode_element (it, depth, field_width, precision, elt, props, risky)
16955 struct it *it;
16956 int depth;
16957 int field_width, precision;
16958 Lisp_Object elt, props;
16959 int risky;
16960 {
16961 int n = 0, field, prec;
16962 int literal = 0;
16963
16964 tail_recurse:
16965 if (depth > 100)
16966 elt = build_string ("*too-deep*");
16967
16968 depth++;
16969
16970 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
16971 {
16972 case Lisp_String:
16973 {
16974 /* A string: output it and check for %-constructs within it. */
16975 unsigned char c;
16976 int offset = 0;
16977
16978 if (SCHARS (elt) > 0
16979 && (!NILP (props) || risky))
16980 {
16981 Lisp_Object oprops, aelt;
16982 oprops = Ftext_properties_at (make_number (0), elt);
16983
16984 /* If the starting string's properties are not what
16985 we want, translate the string. Also, if the string
16986 is risky, do that anyway. */
16987
16988 if (NILP (Fequal (props, oprops)) || risky)
16989 {
16990 /* If the starting string has properties,
16991 merge the specified ones onto the existing ones. */
16992 if (! NILP (oprops) && !risky)
16993 {
16994 Lisp_Object tem;
16995
16996 oprops = Fcopy_sequence (oprops);
16997 tem = props;
16998 while (CONSP (tem))
16999 {
17000 oprops = Fplist_put (oprops, XCAR (tem),
17001 XCAR (XCDR (tem)));
17002 tem = XCDR (XCDR (tem));
17003 }
17004 props = oprops;
17005 }
17006
17007 aelt = Fassoc (elt, mode_line_proptrans_alist);
17008 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
17009 {
17010 /* AELT is what we want. Move it to the front
17011 without consing. */
17012 elt = XCAR (aelt);
17013 mode_line_proptrans_alist
17014 = move_elt_to_front (aelt, mode_line_proptrans_alist);
17015 }
17016 else
17017 {
17018 Lisp_Object tem;
17019
17020 /* If AELT has the wrong props, it is useless.
17021 so get rid of it. */
17022 if (! NILP (aelt))
17023 mode_line_proptrans_alist
17024 = Fdelq (aelt, mode_line_proptrans_alist);
17025
17026 elt = Fcopy_sequence (elt);
17027 Fset_text_properties (make_number (0), Flength (elt),
17028 props, elt);
17029 /* Add this item to mode_line_proptrans_alist. */
17030 mode_line_proptrans_alist
17031 = Fcons (Fcons (elt, props),
17032 mode_line_proptrans_alist);
17033 /* Truncate mode_line_proptrans_alist
17034 to at most 50 elements. */
17035 tem = Fnthcdr (make_number (50),
17036 mode_line_proptrans_alist);
17037 if (! NILP (tem))
17038 XSETCDR (tem, Qnil);
17039 }
17040 }
17041 }
17042
17043 offset = 0;
17044
17045 if (literal)
17046 {
17047 prec = precision - n;
17048 switch (mode_line_target)
17049 {
17050 case MODE_LINE_NOPROP:
17051 case MODE_LINE_TITLE:
17052 n += store_mode_line_noprop (SDATA (elt), -1, prec);
17053 break;
17054 case MODE_LINE_STRING:
17055 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
17056 break;
17057 case MODE_LINE_DISPLAY:
17058 n += display_string (NULL, elt, Qnil, 0, 0, it,
17059 0, prec, 0, STRING_MULTIBYTE (elt));
17060 break;
17061 }
17062
17063 break;
17064 }
17065
17066 /* Handle the non-literal case. */
17067
17068 while ((precision <= 0 || n < precision)
17069 && SREF (elt, offset) != 0
17070 && (mode_line_target != MODE_LINE_DISPLAY
17071 || it->current_x < it->last_visible_x))
17072 {
17073 int last_offset = offset;
17074
17075 /* Advance to end of string or next format specifier. */
17076 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
17077 ;
17078
17079 if (offset - 1 != last_offset)
17080 {
17081 int nchars, nbytes;
17082
17083 /* Output to end of string or up to '%'. Field width
17084 is length of string. Don't output more than
17085 PRECISION allows us. */
17086 offset--;
17087
17088 prec = c_string_width (SDATA (elt) + last_offset,
17089 offset - last_offset, precision - n,
17090 &nchars, &nbytes);
17091
17092 switch (mode_line_target)
17093 {
17094 case MODE_LINE_NOPROP:
17095 case MODE_LINE_TITLE:
17096 n += store_mode_line_noprop (SDATA (elt) + last_offset, 0, prec);
17097 break;
17098 case MODE_LINE_STRING:
17099 {
17100 int bytepos = last_offset;
17101 int charpos = string_byte_to_char (elt, bytepos);
17102 int endpos = (precision <= 0
17103 ? string_byte_to_char (elt, offset)
17104 : charpos + nchars);
17105
17106 n += store_mode_line_string (NULL,
17107 Fsubstring (elt, make_number (charpos),
17108 make_number (endpos)),
17109 0, 0, 0, Qnil);
17110 }
17111 break;
17112 case MODE_LINE_DISPLAY:
17113 {
17114 int bytepos = last_offset;
17115 int charpos = string_byte_to_char (elt, bytepos);
17116
17117 if (precision <= 0)
17118 nchars = string_byte_to_char (elt, offset) - charpos;
17119 n += display_string (NULL, elt, Qnil, 0, charpos,
17120 it, 0, nchars, 0,
17121 STRING_MULTIBYTE (elt));
17122 }
17123 break;
17124 }
17125 }
17126 else /* c == '%' */
17127 {
17128 int percent_position = offset;
17129
17130 /* Get the specified minimum width. Zero means
17131 don't pad. */
17132 field = 0;
17133 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
17134 field = field * 10 + c - '0';
17135
17136 /* Don't pad beyond the total padding allowed. */
17137 if (field_width - n > 0 && field > field_width - n)
17138 field = field_width - n;
17139
17140 /* Note that either PRECISION <= 0 or N < PRECISION. */
17141 prec = precision - n;
17142
17143 if (c == 'M')
17144 n += display_mode_element (it, depth, field, prec,
17145 Vglobal_mode_string, props,
17146 risky);
17147 else if (c != 0)
17148 {
17149 int multibyte;
17150 int bytepos, charpos;
17151 unsigned char *spec;
17152
17153 bytepos = percent_position;
17154 charpos = (STRING_MULTIBYTE (elt)
17155 ? string_byte_to_char (elt, bytepos)
17156 : bytepos);
17157
17158 spec
17159 = decode_mode_spec (it->w, c, field, prec, &multibyte);
17160
17161 switch (mode_line_target)
17162 {
17163 case MODE_LINE_NOPROP:
17164 case MODE_LINE_TITLE:
17165 n += store_mode_line_noprop (spec, field, prec);
17166 break;
17167 case MODE_LINE_STRING:
17168 {
17169 int len = strlen (spec);
17170 Lisp_Object tem = make_string (spec, len);
17171 props = Ftext_properties_at (make_number (charpos), elt);
17172 /* Should only keep face property in props */
17173 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
17174 }
17175 break;
17176 case MODE_LINE_DISPLAY:
17177 {
17178 int nglyphs_before, nwritten;
17179
17180 nglyphs_before = it->glyph_row->used[TEXT_AREA];
17181 nwritten = display_string (spec, Qnil, elt,
17182 charpos, 0, it,
17183 field, prec, 0,
17184 multibyte);
17185
17186 /* Assign to the glyphs written above the
17187 string where the `%x' came from, position
17188 of the `%'. */
17189 if (nwritten > 0)
17190 {
17191 struct glyph *glyph
17192 = (it->glyph_row->glyphs[TEXT_AREA]
17193 + nglyphs_before);
17194 int i;
17195
17196 for (i = 0; i < nwritten; ++i)
17197 {
17198 glyph[i].object = elt;
17199 glyph[i].charpos = charpos;
17200 }
17201
17202 n += nwritten;
17203 }
17204 }
17205 break;
17206 }
17207 }
17208 else /* c == 0 */
17209 break;
17210 }
17211 }
17212 }
17213 break;
17214
17215 case Lisp_Symbol:
17216 /* A symbol: process the value of the symbol recursively
17217 as if it appeared here directly. Avoid error if symbol void.
17218 Special case: if value of symbol is a string, output the string
17219 literally. */
17220 {
17221 register Lisp_Object tem;
17222
17223 /* If the variable is not marked as risky to set
17224 then its contents are risky to use. */
17225 if (NILP (Fget (elt, Qrisky_local_variable)))
17226 risky = 1;
17227
17228 tem = Fboundp (elt);
17229 if (!NILP (tem))
17230 {
17231 tem = Fsymbol_value (elt);
17232 /* If value is a string, output that string literally:
17233 don't check for % within it. */
17234 if (STRINGP (tem))
17235 literal = 1;
17236
17237 if (!EQ (tem, elt))
17238 {
17239 /* Give up right away for nil or t. */
17240 elt = tem;
17241 goto tail_recurse;
17242 }
17243 }
17244 }
17245 break;
17246
17247 case Lisp_Cons:
17248 {
17249 register Lisp_Object car, tem;
17250
17251 /* A cons cell: five distinct cases.
17252 If first element is :eval or :propertize, do something special.
17253 If first element is a string or a cons, process all the elements
17254 and effectively concatenate them.
17255 If first element is a negative number, truncate displaying cdr to
17256 at most that many characters. If positive, pad (with spaces)
17257 to at least that many characters.
17258 If first element is a symbol, process the cadr or caddr recursively
17259 according to whether the symbol's value is non-nil or nil. */
17260 car = XCAR (elt);
17261 if (EQ (car, QCeval))
17262 {
17263 /* An element of the form (:eval FORM) means evaluate FORM
17264 and use the result as mode line elements. */
17265
17266 if (risky)
17267 break;
17268
17269 if (CONSP (XCDR (elt)))
17270 {
17271 Lisp_Object spec;
17272 spec = safe_eval (XCAR (XCDR (elt)));
17273 n += display_mode_element (it, depth, field_width - n,
17274 precision - n, spec, props,
17275 risky);
17276 }
17277 }
17278 else if (EQ (car, QCpropertize))
17279 {
17280 /* An element of the form (:propertize ELT PROPS...)
17281 means display ELT but applying properties PROPS. */
17282
17283 if (risky)
17284 break;
17285
17286 if (CONSP (XCDR (elt)))
17287 n += display_mode_element (it, depth, field_width - n,
17288 precision - n, XCAR (XCDR (elt)),
17289 XCDR (XCDR (elt)), risky);
17290 }
17291 else if (SYMBOLP (car))
17292 {
17293 tem = Fboundp (car);
17294 elt = XCDR (elt);
17295 if (!CONSP (elt))
17296 goto invalid;
17297 /* elt is now the cdr, and we know it is a cons cell.
17298 Use its car if CAR has a non-nil value. */
17299 if (!NILP (tem))
17300 {
17301 tem = Fsymbol_value (car);
17302 if (!NILP (tem))
17303 {
17304 elt = XCAR (elt);
17305 goto tail_recurse;
17306 }
17307 }
17308 /* Symbol's value is nil (or symbol is unbound)
17309 Get the cddr of the original list
17310 and if possible find the caddr and use that. */
17311 elt = XCDR (elt);
17312 if (NILP (elt))
17313 break;
17314 else if (!CONSP (elt))
17315 goto invalid;
17316 elt = XCAR (elt);
17317 goto tail_recurse;
17318 }
17319 else if (INTEGERP (car))
17320 {
17321 register int lim = XINT (car);
17322 elt = XCDR (elt);
17323 if (lim < 0)
17324 {
17325 /* Negative int means reduce maximum width. */
17326 if (precision <= 0)
17327 precision = -lim;
17328 else
17329 precision = min (precision, -lim);
17330 }
17331 else if (lim > 0)
17332 {
17333 /* Padding specified. Don't let it be more than
17334 current maximum. */
17335 if (precision > 0)
17336 lim = min (precision, lim);
17337
17338 /* If that's more padding than already wanted, queue it.
17339 But don't reduce padding already specified even if
17340 that is beyond the current truncation point. */
17341 field_width = max (lim, field_width);
17342 }
17343 goto tail_recurse;
17344 }
17345 else if (STRINGP (car) || CONSP (car))
17346 {
17347 register int limit = 50;
17348 /* Limit is to protect against circular lists. */
17349 while (CONSP (elt)
17350 && --limit > 0
17351 && (precision <= 0 || n < precision))
17352 {
17353 n += display_mode_element (it, depth,
17354 /* Do padding only after the last
17355 element in the list. */
17356 (! CONSP (XCDR (elt))
17357 ? field_width - n
17358 : 0),
17359 precision - n, XCAR (elt),
17360 props, risky);
17361 elt = XCDR (elt);
17362 }
17363 }
17364 }
17365 break;
17366
17367 default:
17368 invalid:
17369 elt = build_string ("*invalid*");
17370 goto tail_recurse;
17371 }
17372
17373 /* Pad to FIELD_WIDTH. */
17374 if (field_width > 0 && n < field_width)
17375 {
17376 switch (mode_line_target)
17377 {
17378 case MODE_LINE_NOPROP:
17379 case MODE_LINE_TITLE:
17380 n += store_mode_line_noprop ("", field_width - n, 0);
17381 break;
17382 case MODE_LINE_STRING:
17383 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
17384 break;
17385 case MODE_LINE_DISPLAY:
17386 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
17387 0, 0, 0);
17388 break;
17389 }
17390 }
17391
17392 return n;
17393 }
17394
17395 /* Store a mode-line string element in mode_line_string_list.
17396
17397 If STRING is non-null, display that C string. Otherwise, the Lisp
17398 string LISP_STRING is displayed.
17399
17400 FIELD_WIDTH is the minimum number of output glyphs to produce.
17401 If STRING has fewer characters than FIELD_WIDTH, pad to the right
17402 with spaces. FIELD_WIDTH <= 0 means don't pad.
17403
17404 PRECISION is the maximum number of characters to output from
17405 STRING. PRECISION <= 0 means don't truncate the string.
17406
17407 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
17408 properties to the string.
17409
17410 PROPS are the properties to add to the string.
17411 The mode_line_string_face face property is always added to the string.
17412 */
17413
17414 static int
17415 store_mode_line_string (string, lisp_string, copy_string, field_width, precision, props)
17416 char *string;
17417 Lisp_Object lisp_string;
17418 int copy_string;
17419 int field_width;
17420 int precision;
17421 Lisp_Object props;
17422 {
17423 int len;
17424 int n = 0;
17425
17426 if (string != NULL)
17427 {
17428 len = strlen (string);
17429 if (precision > 0 && len > precision)
17430 len = precision;
17431 lisp_string = make_string (string, len);
17432 if (NILP (props))
17433 props = mode_line_string_face_prop;
17434 else if (!NILP (mode_line_string_face))
17435 {
17436 Lisp_Object face = Fplist_get (props, Qface);
17437 props = Fcopy_sequence (props);
17438 if (NILP (face))
17439 face = mode_line_string_face;
17440 else
17441 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
17442 props = Fplist_put (props, Qface, face);
17443 }
17444 Fadd_text_properties (make_number (0), make_number (len),
17445 props, lisp_string);
17446 }
17447 else
17448 {
17449 len = XFASTINT (Flength (lisp_string));
17450 if (precision > 0 && len > precision)
17451 {
17452 len = precision;
17453 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
17454 precision = -1;
17455 }
17456 if (!NILP (mode_line_string_face))
17457 {
17458 Lisp_Object face;
17459 if (NILP (props))
17460 props = Ftext_properties_at (make_number (0), lisp_string);
17461 face = Fplist_get (props, Qface);
17462 if (NILP (face))
17463 face = mode_line_string_face;
17464 else
17465 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
17466 props = Fcons (Qface, Fcons (face, Qnil));
17467 if (copy_string)
17468 lisp_string = Fcopy_sequence (lisp_string);
17469 }
17470 if (!NILP (props))
17471 Fadd_text_properties (make_number (0), make_number (len),
17472 props, lisp_string);
17473 }
17474
17475 if (len > 0)
17476 {
17477 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
17478 n += len;
17479 }
17480
17481 if (field_width > len)
17482 {
17483 field_width -= len;
17484 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
17485 if (!NILP (props))
17486 Fadd_text_properties (make_number (0), make_number (field_width),
17487 props, lisp_string);
17488 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
17489 n += field_width;
17490 }
17491
17492 return n;
17493 }
17494
17495
17496 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
17497 1, 4, 0,
17498 doc: /* Format a string out of a mode line format specification.
17499 First arg FORMAT specifies the mode line format (see `mode-line-format'
17500 for details) to use.
17501
17502 Optional second arg FACE specifies the face property to put
17503 on all characters for which no face is specified.
17504 The value t means whatever face the window's mode line currently uses
17505 \(either `mode-line' or `mode-line-inactive', depending).
17506 A value of nil means the default is no face property.
17507 If FACE is an integer, the value string has no text properties.
17508
17509 Optional third and fourth args WINDOW and BUFFER specify the window
17510 and buffer to use as the context for the formatting (defaults
17511 are the selected window and the window's buffer). */)
17512 (format, face, window, buffer)
17513 Lisp_Object format, face, window, buffer;
17514 {
17515 struct it it;
17516 int len;
17517 struct window *w;
17518 struct buffer *old_buffer = NULL;
17519 int face_id = -1;
17520 int no_props = INTEGERP (face);
17521 int count = SPECPDL_INDEX ();
17522 Lisp_Object str;
17523 int string_start = 0;
17524
17525 if (NILP (window))
17526 window = selected_window;
17527 CHECK_WINDOW (window);
17528 w = XWINDOW (window);
17529
17530 if (NILP (buffer))
17531 buffer = w->buffer;
17532 CHECK_BUFFER (buffer);
17533
17534 if (NILP (format))
17535 return empty_unibyte_string;
17536
17537 if (no_props)
17538 face = Qnil;
17539
17540 if (!NILP (face))
17541 {
17542 if (EQ (face, Qt))
17543 face = (EQ (window, selected_window) ? Qmode_line : Qmode_line_inactive);
17544 face_id = lookup_named_face (XFRAME (WINDOW_FRAME (w)), face, 0);
17545 }
17546
17547 if (face_id < 0)
17548 face_id = DEFAULT_FACE_ID;
17549
17550 if (XBUFFER (buffer) != current_buffer)
17551 old_buffer = current_buffer;
17552
17553 /* Save things including mode_line_proptrans_alist,
17554 and set that to nil so that we don't alter the outer value. */
17555 record_unwind_protect (unwind_format_mode_line,
17556 format_mode_line_unwind_data (old_buffer, 1));
17557 mode_line_proptrans_alist = Qnil;
17558
17559 if (old_buffer)
17560 set_buffer_internal_1 (XBUFFER (buffer));
17561
17562 init_iterator (&it, w, -1, -1, NULL, face_id);
17563
17564 if (no_props)
17565 {
17566 mode_line_target = MODE_LINE_NOPROP;
17567 mode_line_string_face_prop = Qnil;
17568 mode_line_string_list = Qnil;
17569 string_start = MODE_LINE_NOPROP_LEN (0);
17570 }
17571 else
17572 {
17573 mode_line_target = MODE_LINE_STRING;
17574 mode_line_string_list = Qnil;
17575 mode_line_string_face = face;
17576 mode_line_string_face_prop
17577 = (NILP (face) ? Qnil : Fcons (Qface, Fcons (face, Qnil)));
17578 }
17579
17580 push_kboard (FRAME_KBOARD (it.f));
17581 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
17582 pop_kboard ();
17583
17584 if (no_props)
17585 {
17586 len = MODE_LINE_NOPROP_LEN (string_start);
17587 str = make_string (mode_line_noprop_buf + string_start, len);
17588 }
17589 else
17590 {
17591 mode_line_string_list = Fnreverse (mode_line_string_list);
17592 str = Fmapconcat (intern ("identity"), mode_line_string_list,
17593 empty_unibyte_string);
17594 }
17595
17596 unbind_to (count, Qnil);
17597 return str;
17598 }
17599
17600 /* Write a null-terminated, right justified decimal representation of
17601 the positive integer D to BUF using a minimal field width WIDTH. */
17602
17603 static void
17604 pint2str (buf, width, d)
17605 register char *buf;
17606 register int width;
17607 register int d;
17608 {
17609 register char *p = buf;
17610
17611 if (d <= 0)
17612 *p++ = '0';
17613 else
17614 {
17615 while (d > 0)
17616 {
17617 *p++ = d % 10 + '0';
17618 d /= 10;
17619 }
17620 }
17621
17622 for (width -= (int) (p - buf); width > 0; --width)
17623 *p++ = ' ';
17624 *p-- = '\0';
17625 while (p > buf)
17626 {
17627 d = *buf;
17628 *buf++ = *p;
17629 *p-- = d;
17630 }
17631 }
17632
17633 /* Write a null-terminated, right justified decimal and "human
17634 readable" representation of the nonnegative integer D to BUF using
17635 a minimal field width WIDTH. D should be smaller than 999.5e24. */
17636
17637 static const char power_letter[] =
17638 {
17639 0, /* not used */
17640 'k', /* kilo */
17641 'M', /* mega */
17642 'G', /* giga */
17643 'T', /* tera */
17644 'P', /* peta */
17645 'E', /* exa */
17646 'Z', /* zetta */
17647 'Y' /* yotta */
17648 };
17649
17650 static void
17651 pint2hrstr (buf, width, d)
17652 char *buf;
17653 int width;
17654 int d;
17655 {
17656 /* We aim to represent the nonnegative integer D as
17657 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
17658 int quotient = d;
17659 int remainder = 0;
17660 /* -1 means: do not use TENTHS. */
17661 int tenths = -1;
17662 int exponent = 0;
17663
17664 /* Length of QUOTIENT.TENTHS as a string. */
17665 int length;
17666
17667 char * psuffix;
17668 char * p;
17669
17670 if (1000 <= quotient)
17671 {
17672 /* Scale to the appropriate EXPONENT. */
17673 do
17674 {
17675 remainder = quotient % 1000;
17676 quotient /= 1000;
17677 exponent++;
17678 }
17679 while (1000 <= quotient);
17680
17681 /* Round to nearest and decide whether to use TENTHS or not. */
17682 if (quotient <= 9)
17683 {
17684 tenths = remainder / 100;
17685 if (50 <= remainder % 100)
17686 {
17687 if (tenths < 9)
17688 tenths++;
17689 else
17690 {
17691 quotient++;
17692 if (quotient == 10)
17693 tenths = -1;
17694 else
17695 tenths = 0;
17696 }
17697 }
17698 }
17699 else
17700 if (500 <= remainder)
17701 {
17702 if (quotient < 999)
17703 quotient++;
17704 else
17705 {
17706 quotient = 1;
17707 exponent++;
17708 tenths = 0;
17709 }
17710 }
17711 }
17712
17713 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
17714 if (tenths == -1 && quotient <= 99)
17715 if (quotient <= 9)
17716 length = 1;
17717 else
17718 length = 2;
17719 else
17720 length = 3;
17721 p = psuffix = buf + max (width, length);
17722
17723 /* Print EXPONENT. */
17724 if (exponent)
17725 *psuffix++ = power_letter[exponent];
17726 *psuffix = '\0';
17727
17728 /* Print TENTHS. */
17729 if (tenths >= 0)
17730 {
17731 *--p = '0' + tenths;
17732 *--p = '.';
17733 }
17734
17735 /* Print QUOTIENT. */
17736 do
17737 {
17738 int digit = quotient % 10;
17739 *--p = '0' + digit;
17740 }
17741 while ((quotient /= 10) != 0);
17742
17743 /* Print leading spaces. */
17744 while (buf < p)
17745 *--p = ' ';
17746 }
17747
17748 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
17749 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
17750 type of CODING_SYSTEM. Return updated pointer into BUF. */
17751
17752 static unsigned char invalid_eol_type[] = "(*invalid*)";
17753
17754 static char *
17755 decode_mode_spec_coding (coding_system, buf, eol_flag)
17756 Lisp_Object coding_system;
17757 register char *buf;
17758 int eol_flag;
17759 {
17760 Lisp_Object val;
17761 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
17762 const unsigned char *eol_str;
17763 int eol_str_len;
17764 /* The EOL conversion we are using. */
17765 Lisp_Object eoltype;
17766
17767 val = CODING_SYSTEM_SPEC (coding_system);
17768 eoltype = Qnil;
17769
17770 if (!VECTORP (val)) /* Not yet decided. */
17771 {
17772 if (multibyte)
17773 *buf++ = '-';
17774 if (eol_flag)
17775 eoltype = eol_mnemonic_undecided;
17776 /* Don't mention EOL conversion if it isn't decided. */
17777 }
17778 else
17779 {
17780 Lisp_Object attrs;
17781 Lisp_Object eolvalue;
17782
17783 attrs = AREF (val, 0);
17784 eolvalue = AREF (val, 2);
17785
17786 if (multibyte)
17787 *buf++ = XFASTINT (CODING_ATTR_MNEMONIC (attrs));
17788
17789 if (eol_flag)
17790 {
17791 /* The EOL conversion that is normal on this system. */
17792
17793 if (NILP (eolvalue)) /* Not yet decided. */
17794 eoltype = eol_mnemonic_undecided;
17795 else if (VECTORP (eolvalue)) /* Not yet decided. */
17796 eoltype = eol_mnemonic_undecided;
17797 else /* eolvalue is Qunix, Qdos, or Qmac. */
17798 eoltype = (EQ (eolvalue, Qunix)
17799 ? eol_mnemonic_unix
17800 : (EQ (eolvalue, Qdos) == 1
17801 ? eol_mnemonic_dos : eol_mnemonic_mac));
17802 }
17803 }
17804
17805 if (eol_flag)
17806 {
17807 /* Mention the EOL conversion if it is not the usual one. */
17808 if (STRINGP (eoltype))
17809 {
17810 eol_str = SDATA (eoltype);
17811 eol_str_len = SBYTES (eoltype);
17812 }
17813 else if (CHARACTERP (eoltype))
17814 {
17815 unsigned char *tmp = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
17816 eol_str_len = CHAR_STRING (XINT (eoltype), tmp);
17817 eol_str = tmp;
17818 }
17819 else
17820 {
17821 eol_str = invalid_eol_type;
17822 eol_str_len = sizeof (invalid_eol_type) - 1;
17823 }
17824 bcopy (eol_str, buf, eol_str_len);
17825 buf += eol_str_len;
17826 }
17827
17828 return buf;
17829 }
17830
17831 /* Return a string for the output of a mode line %-spec for window W,
17832 generated by character C. PRECISION >= 0 means don't return a
17833 string longer than that value. FIELD_WIDTH > 0 means pad the
17834 string returned with spaces to that value. Return 1 in *MULTIBYTE
17835 if the result is multibyte text.
17836
17837 Note we operate on the current buffer for most purposes,
17838 the exception being w->base_line_pos. */
17839
17840 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
17841
17842 static char *
17843 decode_mode_spec (w, c, field_width, precision, multibyte)
17844 struct window *w;
17845 register int c;
17846 int field_width, precision;
17847 int *multibyte;
17848 {
17849 Lisp_Object obj;
17850 struct frame *f = XFRAME (WINDOW_FRAME (w));
17851 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
17852 struct buffer *b = current_buffer;
17853
17854 obj = Qnil;
17855 *multibyte = 0;
17856
17857 switch (c)
17858 {
17859 case '*':
17860 if (!NILP (b->read_only))
17861 return "%";
17862 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
17863 return "*";
17864 return "-";
17865
17866 case '+':
17867 /* This differs from %* only for a modified read-only buffer. */
17868 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
17869 return "*";
17870 if (!NILP (b->read_only))
17871 return "%";
17872 return "-";
17873
17874 case '&':
17875 /* This differs from %* in ignoring read-only-ness. */
17876 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
17877 return "*";
17878 return "-";
17879
17880 case '%':
17881 return "%";
17882
17883 case '[':
17884 {
17885 int i;
17886 char *p;
17887
17888 if (command_loop_level > 5)
17889 return "[[[... ";
17890 p = decode_mode_spec_buf;
17891 for (i = 0; i < command_loop_level; i++)
17892 *p++ = '[';
17893 *p = 0;
17894 return decode_mode_spec_buf;
17895 }
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 register int i;
17914
17915 /* Let lots_of_dashes be a string of infinite length. */
17916 if (mode_line_target == MODE_LINE_NOPROP ||
17917 mode_line_target == MODE_LINE_STRING)
17918 return "--";
17919 if (field_width <= 0
17920 || field_width > sizeof (lots_of_dashes))
17921 {
17922 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
17923 decode_mode_spec_buf[i] = '-';
17924 decode_mode_spec_buf[i] = '\0';
17925 return decode_mode_spec_buf;
17926 }
17927 else
17928 return lots_of_dashes;
17929 }
17930
17931 case 'b':
17932 obj = b->name;
17933 break;
17934
17935 case 'c':
17936 /* %c and %l are ignored in `frame-title-format'.
17937 (In redisplay_internal, the frame title is drawn _before_ the
17938 windows are updated, so the stuff which depends on actual
17939 window contents (such as %l) may fail to render properly, or
17940 even crash emacs.) */
17941 if (mode_line_target == MODE_LINE_TITLE)
17942 return "";
17943 else
17944 {
17945 int col = (int) current_column (); /* iftc */
17946 w->column_number_displayed = make_number (col);
17947 pint2str (decode_mode_spec_buf, field_width, col);
17948 return decode_mode_spec_buf;
17949 }
17950
17951 case 'e':
17952 #ifndef SYSTEM_MALLOC
17953 {
17954 if (NILP (Vmemory_full))
17955 return "";
17956 else
17957 return "!MEM FULL! ";
17958 }
17959 #else
17960 return "";
17961 #endif
17962
17963 case 'F':
17964 /* %F displays the frame name. */
17965 if (!NILP (f->title))
17966 return (char *) SDATA (f->title);
17967 if (f->explicit_name || ! FRAME_WINDOW_P (f))
17968 return (char *) SDATA (f->name);
17969 return "Emacs";
17970
17971 case 'f':
17972 obj = b->filename;
17973 break;
17974
17975 case 'i':
17976 {
17977 int size = ZV - BEGV;
17978 pint2str (decode_mode_spec_buf, field_width, size);
17979 return decode_mode_spec_buf;
17980 }
17981
17982 case 'I':
17983 {
17984 int size = ZV - BEGV;
17985 pint2hrstr (decode_mode_spec_buf, field_width, size);
17986 return decode_mode_spec_buf;
17987 }
17988
17989 case 'l':
17990 {
17991 int startpos, startpos_byte, line, linepos, linepos_byte;
17992 int topline, nlines, junk, height;
17993
17994 /* %c and %l are ignored in `frame-title-format'. */
17995 if (mode_line_target == MODE_LINE_TITLE)
17996 return "";
17997
17998 startpos = XMARKER (w->start)->charpos;
17999 startpos_byte = marker_byte_position (w->start);
18000 height = WINDOW_TOTAL_LINES (w);
18001
18002 /* If we decided that this buffer isn't suitable for line numbers,
18003 don't forget that too fast. */
18004 if (EQ (w->base_line_pos, w->buffer))
18005 goto no_value;
18006 /* But do forget it, if the window shows a different buffer now. */
18007 else if (BUFFERP (w->base_line_pos))
18008 w->base_line_pos = Qnil;
18009
18010 /* If the buffer is very big, don't waste time. */
18011 if (INTEGERP (Vline_number_display_limit)
18012 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
18013 {
18014 w->base_line_pos = Qnil;
18015 w->base_line_number = Qnil;
18016 goto no_value;
18017 }
18018
18019 if (!NILP (w->base_line_number)
18020 && !NILP (w->base_line_pos)
18021 && XFASTINT (w->base_line_pos) <= startpos)
18022 {
18023 line = XFASTINT (w->base_line_number);
18024 linepos = XFASTINT (w->base_line_pos);
18025 linepos_byte = buf_charpos_to_bytepos (b, linepos);
18026 }
18027 else
18028 {
18029 line = 1;
18030 linepos = BUF_BEGV (b);
18031 linepos_byte = BUF_BEGV_BYTE (b);
18032 }
18033
18034 /* Count lines from base line to window start position. */
18035 nlines = display_count_lines (linepos, linepos_byte,
18036 startpos_byte,
18037 startpos, &junk);
18038
18039 topline = nlines + line;
18040
18041 /* Determine a new base line, if the old one is too close
18042 or too far away, or if we did not have one.
18043 "Too close" means it's plausible a scroll-down would
18044 go back past it. */
18045 if (startpos == BUF_BEGV (b))
18046 {
18047 w->base_line_number = make_number (topline);
18048 w->base_line_pos = make_number (BUF_BEGV (b));
18049 }
18050 else if (nlines < height + 25 || nlines > height * 3 + 50
18051 || linepos == BUF_BEGV (b))
18052 {
18053 int limit = BUF_BEGV (b);
18054 int limit_byte = BUF_BEGV_BYTE (b);
18055 int position;
18056 int distance = (height * 2 + 30) * line_number_display_limit_width;
18057
18058 if (startpos - distance > limit)
18059 {
18060 limit = startpos - distance;
18061 limit_byte = CHAR_TO_BYTE (limit);
18062 }
18063
18064 nlines = display_count_lines (startpos, startpos_byte,
18065 limit_byte,
18066 - (height * 2 + 30),
18067 &position);
18068 /* If we couldn't find the lines we wanted within
18069 line_number_display_limit_width chars per line,
18070 give up on line numbers for this window. */
18071 if (position == limit_byte && limit == startpos - distance)
18072 {
18073 w->base_line_pos = w->buffer;
18074 w->base_line_number = Qnil;
18075 goto no_value;
18076 }
18077
18078 w->base_line_number = make_number (topline - nlines);
18079 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
18080 }
18081
18082 /* Now count lines from the start pos to point. */
18083 nlines = display_count_lines (startpos, startpos_byte,
18084 PT_BYTE, PT, &junk);
18085
18086 /* Record that we did display the line number. */
18087 line_number_displayed = 1;
18088
18089 /* Make the string to show. */
18090 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
18091 return decode_mode_spec_buf;
18092 no_value:
18093 {
18094 char* p = decode_mode_spec_buf;
18095 int pad = field_width - 2;
18096 while (pad-- > 0)
18097 *p++ = ' ';
18098 *p++ = '?';
18099 *p++ = '?';
18100 *p = '\0';
18101 return decode_mode_spec_buf;
18102 }
18103 }
18104 break;
18105
18106 case 'm':
18107 obj = b->mode_name;
18108 break;
18109
18110 case 'n':
18111 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
18112 return " Narrow";
18113 break;
18114
18115 case 'p':
18116 {
18117 int pos = marker_position (w->start);
18118 int total = BUF_ZV (b) - BUF_BEGV (b);
18119
18120 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
18121 {
18122 if (pos <= BUF_BEGV (b))
18123 return "All";
18124 else
18125 return "Bottom";
18126 }
18127 else if (pos <= BUF_BEGV (b))
18128 return "Top";
18129 else
18130 {
18131 if (total > 1000000)
18132 /* Do it differently for a large value, to avoid overflow. */
18133 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
18134 else
18135 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
18136 /* We can't normally display a 3-digit number,
18137 so get us a 2-digit number that is close. */
18138 if (total == 100)
18139 total = 99;
18140 sprintf (decode_mode_spec_buf, "%2d%%", total);
18141 return decode_mode_spec_buf;
18142 }
18143 }
18144
18145 /* Display percentage of size above the bottom of the screen. */
18146 case 'P':
18147 {
18148 int toppos = marker_position (w->start);
18149 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
18150 int total = BUF_ZV (b) - BUF_BEGV (b);
18151
18152 if (botpos >= BUF_ZV (b))
18153 {
18154 if (toppos <= BUF_BEGV (b))
18155 return "All";
18156 else
18157 return "Bottom";
18158 }
18159 else
18160 {
18161 if (total > 1000000)
18162 /* Do it differently for a large value, to avoid overflow. */
18163 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
18164 else
18165 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
18166 /* We can't normally display a 3-digit number,
18167 so get us a 2-digit number that is close. */
18168 if (total == 100)
18169 total = 99;
18170 if (toppos <= BUF_BEGV (b))
18171 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
18172 else
18173 sprintf (decode_mode_spec_buf, "%2d%%", total);
18174 return decode_mode_spec_buf;
18175 }
18176 }
18177
18178 case 's':
18179 /* status of process */
18180 obj = Fget_buffer_process (Fcurrent_buffer ());
18181 if (NILP (obj))
18182 return "no process";
18183 #ifdef subprocesses
18184 obj = Fsymbol_name (Fprocess_status (obj));
18185 #endif
18186 break;
18187
18188 case '@':
18189 {
18190 Lisp_Object val;
18191 val = call1 (intern ("file-remote-p"), current_buffer->directory);
18192 if (NILP (val))
18193 return "-";
18194 else
18195 return "@";
18196 }
18197
18198 case 't': /* indicate TEXT or BINARY */
18199 #ifdef MODE_LINE_BINARY_TEXT
18200 return MODE_LINE_BINARY_TEXT (b);
18201 #else
18202 return "T";
18203 #endif
18204
18205 case 'z':
18206 /* coding-system (not including end-of-line format) */
18207 case 'Z':
18208 /* coding-system (including end-of-line type) */
18209 {
18210 int eol_flag = (c == 'Z');
18211 char *p = decode_mode_spec_buf;
18212
18213 if (! FRAME_WINDOW_P (f))
18214 {
18215 /* No need to mention EOL here--the terminal never needs
18216 to do EOL conversion. */
18217 p = decode_mode_spec_coding (CODING_ID_NAME
18218 (FRAME_KEYBOARD_CODING (f)->id),
18219 p, 0);
18220 p = decode_mode_spec_coding (CODING_ID_NAME
18221 (FRAME_TERMINAL_CODING (f)->id),
18222 p, 0);
18223 }
18224 p = decode_mode_spec_coding (b->buffer_file_coding_system,
18225 p, eol_flag);
18226
18227 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
18228 #ifdef subprocesses
18229 obj = Fget_buffer_process (Fcurrent_buffer ());
18230 if (PROCESSP (obj))
18231 {
18232 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
18233 p, eol_flag);
18234 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
18235 p, eol_flag);
18236 }
18237 #endif /* subprocesses */
18238 #endif /* 0 */
18239 *p = 0;
18240 return decode_mode_spec_buf;
18241 }
18242 }
18243
18244 if (STRINGP (obj))
18245 {
18246 *multibyte = STRING_MULTIBYTE (obj);
18247 return (char *) SDATA (obj);
18248 }
18249 else
18250 return "";
18251 }
18252
18253
18254 /* Count up to COUNT lines starting from START / START_BYTE.
18255 But don't go beyond LIMIT_BYTE.
18256 Return the number of lines thus found (always nonnegative).
18257
18258 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
18259
18260 static int
18261 display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
18262 int start, start_byte, limit_byte, count;
18263 int *byte_pos_ptr;
18264 {
18265 register unsigned char *cursor;
18266 unsigned char *base;
18267
18268 register int ceiling;
18269 register unsigned char *ceiling_addr;
18270 int orig_count = count;
18271
18272 /* If we are not in selective display mode,
18273 check only for newlines. */
18274 int selective_display = (!NILP (current_buffer->selective_display)
18275 && !INTEGERP (current_buffer->selective_display));
18276
18277 if (count > 0)
18278 {
18279 while (start_byte < limit_byte)
18280 {
18281 ceiling = BUFFER_CEILING_OF (start_byte);
18282 ceiling = min (limit_byte - 1, ceiling);
18283 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
18284 base = (cursor = BYTE_POS_ADDR (start_byte));
18285 while (1)
18286 {
18287 if (selective_display)
18288 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
18289 ;
18290 else
18291 while (*cursor != '\n' && ++cursor != ceiling_addr)
18292 ;
18293
18294 if (cursor != ceiling_addr)
18295 {
18296 if (--count == 0)
18297 {
18298 start_byte += cursor - base + 1;
18299 *byte_pos_ptr = start_byte;
18300 return orig_count;
18301 }
18302 else
18303 if (++cursor == ceiling_addr)
18304 break;
18305 }
18306 else
18307 break;
18308 }
18309 start_byte += cursor - base;
18310 }
18311 }
18312 else
18313 {
18314 while (start_byte > limit_byte)
18315 {
18316 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
18317 ceiling = max (limit_byte, ceiling);
18318 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
18319 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
18320 while (1)
18321 {
18322 if (selective_display)
18323 while (--cursor != ceiling_addr
18324 && *cursor != '\n' && *cursor != 015)
18325 ;
18326 else
18327 while (--cursor != ceiling_addr && *cursor != '\n')
18328 ;
18329
18330 if (cursor != ceiling_addr)
18331 {
18332 if (++count == 0)
18333 {
18334 start_byte += cursor - base + 1;
18335 *byte_pos_ptr = start_byte;
18336 /* When scanning backwards, we should
18337 not count the newline posterior to which we stop. */
18338 return - orig_count - 1;
18339 }
18340 }
18341 else
18342 break;
18343 }
18344 /* Here we add 1 to compensate for the last decrement
18345 of CURSOR, which took it past the valid range. */
18346 start_byte += cursor - base + 1;
18347 }
18348 }
18349
18350 *byte_pos_ptr = limit_byte;
18351
18352 if (count < 0)
18353 return - orig_count + count;
18354 return orig_count - count;
18355
18356 }
18357
18358
18359 \f
18360 /***********************************************************************
18361 Displaying strings
18362 ***********************************************************************/
18363
18364 /* Display a NUL-terminated string, starting with index START.
18365
18366 If STRING is non-null, display that C string. Otherwise, the Lisp
18367 string LISP_STRING is displayed.
18368
18369 If FACE_STRING is not nil, FACE_STRING_POS is a position in
18370 FACE_STRING. Display STRING or LISP_STRING with the face at
18371 FACE_STRING_POS in FACE_STRING:
18372
18373 Display the string in the environment given by IT, but use the
18374 standard display table, temporarily.
18375
18376 FIELD_WIDTH is the minimum number of output glyphs to produce.
18377 If STRING has fewer characters than FIELD_WIDTH, pad to the right
18378 with spaces. If STRING has more characters, more than FIELD_WIDTH
18379 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
18380
18381 PRECISION is the maximum number of characters to output from
18382 STRING. PRECISION < 0 means don't truncate the string.
18383
18384 This is roughly equivalent to printf format specifiers:
18385
18386 FIELD_WIDTH PRECISION PRINTF
18387 ----------------------------------------
18388 -1 -1 %s
18389 -1 10 %.10s
18390 10 -1 %10s
18391 20 10 %20.10s
18392
18393 MULTIBYTE zero means do not display multibyte chars, > 0 means do
18394 display them, and < 0 means obey the current buffer's value of
18395 enable_multibyte_characters.
18396
18397 Value is the number of columns displayed. */
18398
18399 static int
18400 display_string (string, lisp_string, face_string, face_string_pos,
18401 start, it, field_width, precision, max_x, multibyte)
18402 unsigned char *string;
18403 Lisp_Object lisp_string;
18404 Lisp_Object face_string;
18405 int face_string_pos;
18406 int start;
18407 struct it *it;
18408 int field_width, precision, max_x;
18409 int multibyte;
18410 {
18411 int hpos_at_start = it->hpos;
18412 int saved_face_id = it->face_id;
18413 struct glyph_row *row = it->glyph_row;
18414
18415 /* Initialize the iterator IT for iteration over STRING beginning
18416 with index START. */
18417 reseat_to_string (it, string, lisp_string, start,
18418 precision, field_width, multibyte);
18419
18420 /* If displaying STRING, set up the face of the iterator
18421 from LISP_STRING, if that's given. */
18422 if (STRINGP (face_string))
18423 {
18424 int endptr;
18425 struct face *face;
18426
18427 it->face_id
18428 = face_at_string_position (it->w, face_string, face_string_pos,
18429 0, it->region_beg_charpos,
18430 it->region_end_charpos,
18431 &endptr, it->base_face_id, 0);
18432 face = FACE_FROM_ID (it->f, it->face_id);
18433 it->face_box_p = face->box != FACE_NO_BOX;
18434 }
18435
18436 /* Set max_x to the maximum allowed X position. Don't let it go
18437 beyond the right edge of the window. */
18438 if (max_x <= 0)
18439 max_x = it->last_visible_x;
18440 else
18441 max_x = min (max_x, it->last_visible_x);
18442
18443 /* Skip over display elements that are not visible. because IT->w is
18444 hscrolled. */
18445 if (it->current_x < it->first_visible_x)
18446 move_it_in_display_line_to (it, 100000, it->first_visible_x,
18447 MOVE_TO_POS | MOVE_TO_X);
18448
18449 row->ascent = it->max_ascent;
18450 row->height = it->max_ascent + it->max_descent;
18451 row->phys_ascent = it->max_phys_ascent;
18452 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
18453 row->extra_line_spacing = it->max_extra_line_spacing;
18454
18455 /* This condition is for the case that we are called with current_x
18456 past last_visible_x. */
18457 while (it->current_x < max_x)
18458 {
18459 int x_before, x, n_glyphs_before, i, nglyphs;
18460
18461 /* Get the next display element. */
18462 if (!get_next_display_element (it))
18463 break;
18464
18465 /* Produce glyphs. */
18466 x_before = it->current_x;
18467 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
18468 PRODUCE_GLYPHS (it);
18469
18470 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
18471 i = 0;
18472 x = x_before;
18473 while (i < nglyphs)
18474 {
18475 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
18476
18477 if (!it->truncate_lines_p
18478 && x + glyph->pixel_width > max_x)
18479 {
18480 /* End of continued line or max_x reached. */
18481 if (CHAR_GLYPH_PADDING_P (*glyph))
18482 {
18483 /* A wide character is unbreakable. */
18484 it->glyph_row->used[TEXT_AREA] = n_glyphs_before;
18485 it->current_x = x_before;
18486 }
18487 else
18488 {
18489 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
18490 it->current_x = x;
18491 }
18492 break;
18493 }
18494 else if (x + glyph->pixel_width >= it->first_visible_x)
18495 {
18496 /* Glyph is at least partially visible. */
18497 ++it->hpos;
18498 if (x < it->first_visible_x)
18499 it->glyph_row->x = x - it->first_visible_x;
18500 }
18501 else
18502 {
18503 /* Glyph is off the left margin of the display area.
18504 Should not happen. */
18505 abort ();
18506 }
18507
18508 row->ascent = max (row->ascent, it->max_ascent);
18509 row->height = max (row->height, it->max_ascent + it->max_descent);
18510 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
18511 row->phys_height = max (row->phys_height,
18512 it->max_phys_ascent + it->max_phys_descent);
18513 row->extra_line_spacing = max (row->extra_line_spacing,
18514 it->max_extra_line_spacing);
18515 x += glyph->pixel_width;
18516 ++i;
18517 }
18518
18519 /* Stop if max_x reached. */
18520 if (i < nglyphs)
18521 break;
18522
18523 /* Stop at line ends. */
18524 if (ITERATOR_AT_END_OF_LINE_P (it))
18525 {
18526 it->continuation_lines_width = 0;
18527 break;
18528 }
18529
18530 set_iterator_to_next (it, 1);
18531
18532 /* Stop if truncating at the right edge. */
18533 if (it->truncate_lines_p
18534 && it->current_x >= it->last_visible_x)
18535 {
18536 /* Add truncation mark, but don't do it if the line is
18537 truncated at a padding space. */
18538 if (IT_CHARPOS (*it) < it->string_nchars)
18539 {
18540 if (!FRAME_WINDOW_P (it->f))
18541 {
18542 int i, n;
18543
18544 if (it->current_x > it->last_visible_x)
18545 {
18546 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
18547 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
18548 break;
18549 for (n = row->used[TEXT_AREA]; i < n; ++i)
18550 {
18551 row->used[TEXT_AREA] = i;
18552 produce_special_glyphs (it, IT_TRUNCATION);
18553 }
18554 }
18555 produce_special_glyphs (it, IT_TRUNCATION);
18556 }
18557 it->glyph_row->truncated_on_right_p = 1;
18558 }
18559 break;
18560 }
18561 }
18562
18563 /* Maybe insert a truncation at the left. */
18564 if (it->first_visible_x
18565 && IT_CHARPOS (*it) > 0)
18566 {
18567 if (!FRAME_WINDOW_P (it->f))
18568 insert_left_trunc_glyphs (it);
18569 it->glyph_row->truncated_on_left_p = 1;
18570 }
18571
18572 it->face_id = saved_face_id;
18573
18574 /* Value is number of columns displayed. */
18575 return it->hpos - hpos_at_start;
18576 }
18577
18578
18579 \f
18580 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
18581 appears as an element of LIST or as the car of an element of LIST.
18582 If PROPVAL is a list, compare each element against LIST in that
18583 way, and return 1/2 if any element of PROPVAL is found in LIST.
18584 Otherwise return 0. This function cannot quit.
18585 The return value is 2 if the text is invisible but with an ellipsis
18586 and 1 if it's invisible and without an ellipsis. */
18587
18588 int
18589 invisible_p (propval, list)
18590 register Lisp_Object propval;
18591 Lisp_Object list;
18592 {
18593 register Lisp_Object tail, proptail;
18594
18595 for (tail = list; CONSP (tail); tail = XCDR (tail))
18596 {
18597 register Lisp_Object tem;
18598 tem = XCAR (tail);
18599 if (EQ (propval, tem))
18600 return 1;
18601 if (CONSP (tem) && EQ (propval, XCAR (tem)))
18602 return NILP (XCDR (tem)) ? 1 : 2;
18603 }
18604
18605 if (CONSP (propval))
18606 {
18607 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
18608 {
18609 Lisp_Object propelt;
18610 propelt = XCAR (proptail);
18611 for (tail = list; CONSP (tail); tail = XCDR (tail))
18612 {
18613 register Lisp_Object tem;
18614 tem = XCAR (tail);
18615 if (EQ (propelt, tem))
18616 return 1;
18617 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
18618 return NILP (XCDR (tem)) ? 1 : 2;
18619 }
18620 }
18621 }
18622
18623 return 0;
18624 }
18625
18626 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
18627 doc: /* Non-nil if the property makes the text invisible.
18628 POS-OR-PROP can be a marker or number, in which case it is taken to be
18629 a position in the current buffer and the value of the `invisible' property
18630 is checked; or it can be some other value, which is then presumed to be the
18631 value of the `invisible' property of the text of interest.
18632 The non-nil value returned can be t for truly invisible text or something
18633 else if the text is replaced by an ellipsis. */)
18634 (pos_or_prop)
18635 Lisp_Object pos_or_prop;
18636 {
18637 Lisp_Object prop
18638 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
18639 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
18640 : pos_or_prop);
18641 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
18642 return (invis == 0 ? Qnil
18643 : invis == 1 ? Qt
18644 : make_number (invis));
18645 }
18646
18647 /* Calculate a width or height in pixels from a specification using
18648 the following elements:
18649
18650 SPEC ::=
18651 NUM - a (fractional) multiple of the default font width/height
18652 (NUM) - specifies exactly NUM pixels
18653 UNIT - a fixed number of pixels, see below.
18654 ELEMENT - size of a display element in pixels, see below.
18655 (NUM . SPEC) - equals NUM * SPEC
18656 (+ SPEC SPEC ...) - add pixel values
18657 (- SPEC SPEC ...) - subtract pixel values
18658 (- SPEC) - negate pixel value
18659
18660 NUM ::=
18661 INT or FLOAT - a number constant
18662 SYMBOL - use symbol's (buffer local) variable binding.
18663
18664 UNIT ::=
18665 in - pixels per inch *)
18666 mm - pixels per 1/1000 meter *)
18667 cm - pixels per 1/100 meter *)
18668 width - width of current font in pixels.
18669 height - height of current font in pixels.
18670
18671 *) using the ratio(s) defined in display-pixels-per-inch.
18672
18673 ELEMENT ::=
18674
18675 left-fringe - left fringe width in pixels
18676 right-fringe - right fringe width in pixels
18677
18678 left-margin - left margin width in pixels
18679 right-margin - right margin width in pixels
18680
18681 scroll-bar - scroll-bar area width in pixels
18682
18683 Examples:
18684
18685 Pixels corresponding to 5 inches:
18686 (5 . in)
18687
18688 Total width of non-text areas on left side of window (if scroll-bar is on left):
18689 '(space :width (+ left-fringe left-margin scroll-bar))
18690
18691 Align to first text column (in header line):
18692 '(space :align-to 0)
18693
18694 Align to middle of text area minus half the width of variable `my-image'
18695 containing a loaded image:
18696 '(space :align-to (0.5 . (- text my-image)))
18697
18698 Width of left margin minus width of 1 character in the default font:
18699 '(space :width (- left-margin 1))
18700
18701 Width of left margin minus width of 2 characters in the current font:
18702 '(space :width (- left-margin (2 . width)))
18703
18704 Center 1 character over left-margin (in header line):
18705 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
18706
18707 Different ways to express width of left fringe plus left margin minus one pixel:
18708 '(space :width (- (+ left-fringe left-margin) (1)))
18709 '(space :width (+ left-fringe left-margin (- (1))))
18710 '(space :width (+ left-fringe left-margin (-1)))
18711
18712 */
18713
18714 #define NUMVAL(X) \
18715 ((INTEGERP (X) || FLOATP (X)) \
18716 ? XFLOATINT (X) \
18717 : - 1)
18718
18719 int
18720 calc_pixel_width_or_height (res, it, prop, font, width_p, align_to)
18721 double *res;
18722 struct it *it;
18723 Lisp_Object prop;
18724 void *font;
18725 int width_p, *align_to;
18726 {
18727 double pixels;
18728
18729 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
18730 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
18731
18732 if (NILP (prop))
18733 return OK_PIXELS (0);
18734
18735 xassert (FRAME_LIVE_P (it->f));
18736
18737 if (SYMBOLP (prop))
18738 {
18739 if (SCHARS (SYMBOL_NAME (prop)) == 2)
18740 {
18741 char *unit = SDATA (SYMBOL_NAME (prop));
18742
18743 if (unit[0] == 'i' && unit[1] == 'n')
18744 pixels = 1.0;
18745 else if (unit[0] == 'm' && unit[1] == 'm')
18746 pixels = 25.4;
18747 else if (unit[0] == 'c' && unit[1] == 'm')
18748 pixels = 2.54;
18749 else
18750 pixels = 0;
18751 if (pixels > 0)
18752 {
18753 double ppi;
18754 #ifdef HAVE_WINDOW_SYSTEM
18755 if (FRAME_WINDOW_P (it->f)
18756 && (ppi = (width_p
18757 ? FRAME_X_DISPLAY_INFO (it->f)->resx
18758 : FRAME_X_DISPLAY_INFO (it->f)->resy),
18759 ppi > 0))
18760 return OK_PIXELS (ppi / pixels);
18761 #endif
18762
18763 if ((ppi = NUMVAL (Vdisplay_pixels_per_inch), ppi > 0)
18764 || (CONSP (Vdisplay_pixels_per_inch)
18765 && (ppi = (width_p
18766 ? NUMVAL (XCAR (Vdisplay_pixels_per_inch))
18767 : NUMVAL (XCDR (Vdisplay_pixels_per_inch))),
18768 ppi > 0)))
18769 return OK_PIXELS (ppi / pixels);
18770
18771 return 0;
18772 }
18773 }
18774
18775 #ifdef HAVE_WINDOW_SYSTEM
18776 if (EQ (prop, Qheight))
18777 return OK_PIXELS (font ? FONT_HEIGHT ((XFontStruct *)font) : FRAME_LINE_HEIGHT (it->f));
18778 if (EQ (prop, Qwidth))
18779 return OK_PIXELS (font ? FONT_WIDTH ((XFontStruct *)font) : FRAME_COLUMN_WIDTH (it->f));
18780 #else
18781 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
18782 return OK_PIXELS (1);
18783 #endif
18784
18785 if (EQ (prop, Qtext))
18786 return OK_PIXELS (width_p
18787 ? window_box_width (it->w, TEXT_AREA)
18788 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
18789
18790 if (align_to && *align_to < 0)
18791 {
18792 *res = 0;
18793 if (EQ (prop, Qleft))
18794 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
18795 if (EQ (prop, Qright))
18796 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
18797 if (EQ (prop, Qcenter))
18798 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
18799 + window_box_width (it->w, TEXT_AREA) / 2);
18800 if (EQ (prop, Qleft_fringe))
18801 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
18802 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
18803 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
18804 if (EQ (prop, Qright_fringe))
18805 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
18806 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
18807 : window_box_right_offset (it->w, TEXT_AREA));
18808 if (EQ (prop, Qleft_margin))
18809 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
18810 if (EQ (prop, Qright_margin))
18811 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
18812 if (EQ (prop, Qscroll_bar))
18813 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
18814 ? 0
18815 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
18816 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
18817 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
18818 : 0)));
18819 }
18820 else
18821 {
18822 if (EQ (prop, Qleft_fringe))
18823 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
18824 if (EQ (prop, Qright_fringe))
18825 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
18826 if (EQ (prop, Qleft_margin))
18827 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
18828 if (EQ (prop, Qright_margin))
18829 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
18830 if (EQ (prop, Qscroll_bar))
18831 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
18832 }
18833
18834 prop = Fbuffer_local_value (prop, it->w->buffer);
18835 }
18836
18837 if (INTEGERP (prop) || FLOATP (prop))
18838 {
18839 int base_unit = (width_p
18840 ? FRAME_COLUMN_WIDTH (it->f)
18841 : FRAME_LINE_HEIGHT (it->f));
18842 return OK_PIXELS (XFLOATINT (prop) * base_unit);
18843 }
18844
18845 if (CONSP (prop))
18846 {
18847 Lisp_Object car = XCAR (prop);
18848 Lisp_Object cdr = XCDR (prop);
18849
18850 if (SYMBOLP (car))
18851 {
18852 #ifdef HAVE_WINDOW_SYSTEM
18853 if (FRAME_WINDOW_P (it->f)
18854 && valid_image_p (prop))
18855 {
18856 int id = lookup_image (it->f, prop);
18857 struct image *img = IMAGE_FROM_ID (it->f, id);
18858
18859 return OK_PIXELS (width_p ? img->width : img->height);
18860 }
18861 #endif
18862 if (EQ (car, Qplus) || EQ (car, Qminus))
18863 {
18864 int first = 1;
18865 double px;
18866
18867 pixels = 0;
18868 while (CONSP (cdr))
18869 {
18870 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
18871 font, width_p, align_to))
18872 return 0;
18873 if (first)
18874 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
18875 else
18876 pixels += px;
18877 cdr = XCDR (cdr);
18878 }
18879 if (EQ (car, Qminus))
18880 pixels = -pixels;
18881 return OK_PIXELS (pixels);
18882 }
18883
18884 car = Fbuffer_local_value (car, it->w->buffer);
18885 }
18886
18887 if (INTEGERP (car) || FLOATP (car))
18888 {
18889 double fact;
18890 pixels = XFLOATINT (car);
18891 if (NILP (cdr))
18892 return OK_PIXELS (pixels);
18893 if (calc_pixel_width_or_height (&fact, it, cdr,
18894 font, width_p, align_to))
18895 return OK_PIXELS (pixels * fact);
18896 return 0;
18897 }
18898
18899 return 0;
18900 }
18901
18902 return 0;
18903 }
18904
18905 \f
18906 /***********************************************************************
18907 Glyph Display
18908 ***********************************************************************/
18909
18910 #ifdef HAVE_WINDOW_SYSTEM
18911
18912 #if GLYPH_DEBUG
18913
18914 void
18915 dump_glyph_string (s)
18916 struct glyph_string *s;
18917 {
18918 fprintf (stderr, "glyph string\n");
18919 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
18920 s->x, s->y, s->width, s->height);
18921 fprintf (stderr, " ybase = %d\n", s->ybase);
18922 fprintf (stderr, " hl = %d\n", s->hl);
18923 fprintf (stderr, " left overhang = %d, right = %d\n",
18924 s->left_overhang, s->right_overhang);
18925 fprintf (stderr, " nchars = %d\n", s->nchars);
18926 fprintf (stderr, " extends to end of line = %d\n",
18927 s->extends_to_end_of_line_p);
18928 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
18929 fprintf (stderr, " bg width = %d\n", s->background_width);
18930 }
18931
18932 #endif /* GLYPH_DEBUG */
18933
18934 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
18935 of XChar2b structures for S; it can't be allocated in
18936 init_glyph_string because it must be allocated via `alloca'. W
18937 is the window on which S is drawn. ROW and AREA are the glyph row
18938 and area within the row from which S is constructed. START is the
18939 index of the first glyph structure covered by S. HL is a
18940 face-override for drawing S. */
18941
18942 #ifdef HAVE_NTGUI
18943 #define OPTIONAL_HDC(hdc) hdc,
18944 #define DECLARE_HDC(hdc) HDC hdc;
18945 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
18946 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
18947 #endif
18948
18949 #ifndef OPTIONAL_HDC
18950 #define OPTIONAL_HDC(hdc)
18951 #define DECLARE_HDC(hdc)
18952 #define ALLOCATE_HDC(hdc, f)
18953 #define RELEASE_HDC(hdc, f)
18954 #endif
18955
18956 static void
18957 init_glyph_string (s, OPTIONAL_HDC (hdc) char2b, w, row, area, start, hl)
18958 struct glyph_string *s;
18959 DECLARE_HDC (hdc)
18960 XChar2b *char2b;
18961 struct window *w;
18962 struct glyph_row *row;
18963 enum glyph_row_area area;
18964 int start;
18965 enum draw_glyphs_face hl;
18966 {
18967 bzero (s, sizeof *s);
18968 s->w = w;
18969 s->f = XFRAME (w->frame);
18970 #ifdef HAVE_NTGUI
18971 s->hdc = hdc;
18972 #endif
18973 s->display = FRAME_X_DISPLAY (s->f);
18974 s->window = FRAME_X_WINDOW (s->f);
18975 s->char2b = char2b;
18976 s->hl = hl;
18977 s->row = row;
18978 s->area = area;
18979 s->first_glyph = row->glyphs[area] + start;
18980 s->height = row->height;
18981 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
18982
18983 /* Display the internal border below the tool-bar window. */
18984 if (WINDOWP (s->f->tool_bar_window)
18985 && s->w == XWINDOW (s->f->tool_bar_window))
18986 s->y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
18987
18988 s->ybase = s->y + row->ascent;
18989 }
18990
18991
18992 /* Append the list of glyph strings with head H and tail T to the list
18993 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
18994
18995 static INLINE void
18996 append_glyph_string_lists (head, tail, h, t)
18997 struct glyph_string **head, **tail;
18998 struct glyph_string *h, *t;
18999 {
19000 if (h)
19001 {
19002 if (*head)
19003 (*tail)->next = h;
19004 else
19005 *head = h;
19006 h->prev = *tail;
19007 *tail = t;
19008 }
19009 }
19010
19011
19012 /* Prepend the list of glyph strings with head H and tail T to the
19013 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
19014 result. */
19015
19016 static INLINE void
19017 prepend_glyph_string_lists (head, tail, h, t)
19018 struct glyph_string **head, **tail;
19019 struct glyph_string *h, *t;
19020 {
19021 if (h)
19022 {
19023 if (*head)
19024 (*head)->prev = t;
19025 else
19026 *tail = t;
19027 t->next = *head;
19028 *head = h;
19029 }
19030 }
19031
19032
19033 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
19034 Set *HEAD and *TAIL to the resulting list. */
19035
19036 static INLINE void
19037 append_glyph_string (head, tail, s)
19038 struct glyph_string **head, **tail;
19039 struct glyph_string *s;
19040 {
19041 s->next = s->prev = NULL;
19042 append_glyph_string_lists (head, tail, s, s);
19043 }
19044
19045
19046 /* Get face and two-byte form of character C in face FACE_ID on frame
19047 F. The encoding of C is returned in *CHAR2B. MULTIBYTE_P non-zero
19048 means we want to display multibyte text. DISPLAY_P non-zero means
19049 make sure that X resources for the face returned are allocated.
19050 Value is a pointer to a realized face that is ready for display if
19051 DISPLAY_P is non-zero. */
19052
19053 static INLINE struct face *
19054 get_char_face_and_encoding (f, c, face_id, char2b, multibyte_p, display_p)
19055 struct frame *f;
19056 int c, face_id;
19057 XChar2b *char2b;
19058 int multibyte_p, display_p;
19059 {
19060 struct face *face = FACE_FROM_ID (f, face_id);
19061
19062 #ifdef USE_FONT_BACKEND
19063 if (enable_font_backend)
19064 {
19065 struct font *font = (struct font *) face->font_info;
19066
19067 if (font)
19068 {
19069 unsigned code = font->driver->encode_char (font, c);
19070
19071 if (code != FONT_INVALID_CODE)
19072 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
19073 else
19074 STORE_XCHAR2B (char2b, 0, 0);
19075 }
19076 }
19077 else
19078 #endif /* USE_FONT_BACKEND */
19079 if (!multibyte_p)
19080 {
19081 /* Unibyte case. We don't have to encode, but we have to make
19082 sure to use a face suitable for unibyte. */
19083 STORE_XCHAR2B (char2b, 0, c);
19084 face_id = FACE_FOR_CHAR (f, face, c, -1, Qnil);
19085 face = FACE_FROM_ID (f, face_id);
19086 }
19087 else if (c < 128)
19088 {
19089 /* Case of ASCII in a face known to fit ASCII. */
19090 STORE_XCHAR2B (char2b, 0, c);
19091 }
19092 else if (face->font != NULL)
19093 {
19094 struct font_info *font_info
19095 = FONT_INFO_FROM_ID (f, face->font_info_id);
19096 struct charset *charset = CHARSET_FROM_ID (font_info->charset);
19097 unsigned code = ENCODE_CHAR (charset, c);
19098
19099 if (CHARSET_DIMENSION (charset) == 1)
19100 STORE_XCHAR2B (char2b, 0, code);
19101 else
19102 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
19103 /* Maybe encode the character in *CHAR2B. */
19104 FRAME_RIF (f)->encode_char (c, char2b, font_info, charset, NULL);
19105 }
19106
19107 /* Make sure X resources of the face are allocated. */
19108 #ifdef HAVE_X_WINDOWS
19109 if (display_p)
19110 #endif
19111 {
19112 xassert (face != NULL);
19113 PREPARE_FACE_FOR_DISPLAY (f, face);
19114 }
19115
19116 return face;
19117 }
19118
19119
19120 /* Get face and two-byte form of character glyph GLYPH on frame F.
19121 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
19122 a pointer to a realized face that is ready for display. */
19123
19124 static INLINE struct face *
19125 get_glyph_face_and_encoding (f, glyph, char2b, two_byte_p)
19126 struct frame *f;
19127 struct glyph *glyph;
19128 XChar2b *char2b;
19129 int *two_byte_p;
19130 {
19131 struct face *face;
19132
19133 xassert (glyph->type == CHAR_GLYPH);
19134 face = FACE_FROM_ID (f, glyph->face_id);
19135
19136 if (two_byte_p)
19137 *two_byte_p = 0;
19138
19139 #ifdef USE_FONT_BACKEND
19140 if (enable_font_backend)
19141 {
19142 struct font *font = (struct font *) face->font_info;
19143
19144 if (font)
19145 {
19146 unsigned code = font->driver->encode_char (font, glyph->u.ch);
19147
19148 if (code != FONT_INVALID_CODE)
19149 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
19150 else
19151 STORE_XCHAR2B (char2b, 0, code);
19152 }
19153 }
19154 else
19155 #endif /* USE_FONT_BACKEND */
19156 if (!glyph->multibyte_p)
19157 {
19158 /* Unibyte case. We don't have to encode, but we have to make
19159 sure to use a face suitable for unibyte. */
19160 STORE_XCHAR2B (char2b, 0, glyph->u.ch);
19161 }
19162 else if (glyph->u.ch < 128)
19163 {
19164 /* Case of ASCII in a face known to fit ASCII. */
19165 STORE_XCHAR2B (char2b, 0, glyph->u.ch);
19166 }
19167 else
19168 {
19169 struct font_info *font_info
19170 = FONT_INFO_FROM_ID (f, face->font_info_id);
19171 if (font_info)
19172 {
19173 struct charset *charset = CHARSET_FROM_ID (font_info->charset);
19174 unsigned code = ENCODE_CHAR (charset, glyph->u.ch);
19175
19176 if (CHARSET_DIMENSION (charset) == 1)
19177 STORE_XCHAR2B (char2b, 0, code);
19178 else
19179 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
19180
19181 /* Maybe encode the character in *CHAR2B. */
19182 if (CHARSET_ID (charset) != charset_ascii)
19183 {
19184 glyph->font_type
19185 = FRAME_RIF (f)->encode_char (glyph->u.ch, char2b, font_info,
19186 charset, two_byte_p);
19187 }
19188 }
19189 }
19190
19191 /* Make sure X resources of the face are allocated. */
19192 xassert (face != NULL);
19193 PREPARE_FACE_FOR_DISPLAY (f, face);
19194 return face;
19195 }
19196
19197
19198 /* Fill glyph string S with composition components specified by S->cmp.
19199
19200 BASE_FACE is the base face of the composition.
19201 S->gidx is the index of the first component for S.
19202
19203 OVERLAPS non-zero means S should draw the foreground only, and use
19204 its physical height for clipping. See also draw_glyphs.
19205
19206 Value is the index of a component not in S. */
19207
19208 static int
19209 fill_composite_glyph_string (s, base_face, overlaps)
19210 struct glyph_string *s;
19211 struct face *base_face;
19212 int overlaps;
19213 {
19214 int i;
19215
19216 xassert (s);
19217
19218 s->for_overlaps = overlaps;
19219
19220 #ifdef USE_FONT_BACKEND
19221 if (enable_font_backend && s->cmp->method == COMPOSITION_WITH_GLYPH_STRING)
19222 {
19223 Lisp_Object gstring
19224 = AREF (XHASH_TABLE (composition_hash_table)->key_and_value,
19225 s->cmp->hash_index * 2);
19226
19227 s->face = base_face;
19228 s->font_info = s->cmp->font;
19229 s->font = s->font_info->font;
19230 for (i = 0, s->nchars = 0; i < s->cmp->glyph_len; i++, s->nchars++)
19231 {
19232 Lisp_Object g = LGSTRING_GLYPH (gstring, i);
19233 unsigned code;
19234 XChar2b * store_pos;
19235 if (NILP (LGLYPH_FROM (g)))
19236 break;
19237 code = XUINT (LGLYPH_CODE (g));
19238 store_pos = s->char2b + i;
19239 STORE_XCHAR2B (store_pos, code >> 8, code & 0xFF);
19240 }
19241 s->width = s->cmp->pixel_width;
19242 }
19243 else
19244 #endif /* USE_FONT_BACKEND */
19245 {
19246 /* For all glyphs of this composition, starting at the offset
19247 S->gidx, until we reach the end of the definition or encounter a
19248 glyph that requires the different face, add it to S. */
19249 struct face *face;
19250
19251 s->face = NULL;
19252 s->font = NULL;
19253 s->font_info = NULL;
19254 for (i = s->gidx; i < s->cmp->glyph_len; i++)
19255 {
19256 int c = COMPOSITION_GLYPH (s->cmp, i);
19257
19258 if (c != '\t')
19259 {
19260 int face_id = FACE_FOR_CHAR (s->f, base_face, c, -1, Qnil);
19261
19262 face = get_char_face_and_encoding (s->f, c, face_id,
19263 s->char2b + i, 1, 1);
19264 if (face)
19265 {
19266 if (! s->face)
19267 {
19268 s->face = face;
19269 s->font = s->face->font;
19270 s->font_info = FONT_INFO_FROM_FACE (s->f, s->face);
19271 }
19272 else if (s->face != face)
19273 break;
19274 }
19275 }
19276 ++s->nchars;
19277 }
19278
19279 /* All glyph strings for the same composition has the same width,
19280 i.e. the width set for the first component of the composition. */
19281 s->width = s->first_glyph->pixel_width;
19282 }
19283
19284 /* If the specified font could not be loaded, use the frame's
19285 default font, but record the fact that we couldn't load it in
19286 the glyph string so that we can draw rectangles for the
19287 characters of the glyph string. */
19288 if (s->font == NULL)
19289 {
19290 s->font_not_found_p = 1;
19291 s->font = FRAME_FONT (s->f);
19292 }
19293
19294 /* Adjust base line for subscript/superscript text. */
19295 s->ybase += s->first_glyph->voffset;
19296
19297 /* This glyph string must always be drawn with 16-bit functions. */
19298 s->two_byte_p = 1;
19299
19300 return s->gidx + s->nchars;
19301 }
19302
19303
19304 /* Fill glyph string S from a sequence of character glyphs.
19305
19306 FACE_ID is the face id of the string. START is the index of the
19307 first glyph to consider, END is the index of the last + 1.
19308 OVERLAPS non-zero means S should draw the foreground only, and use
19309 its physical height for clipping. See also draw_glyphs.
19310
19311 Value is the index of the first glyph not in S. */
19312
19313 static int
19314 fill_glyph_string (s, face_id, start, end, overlaps)
19315 struct glyph_string *s;
19316 int face_id;
19317 int start, end, overlaps;
19318 {
19319 struct glyph *glyph, *last;
19320 int voffset;
19321 int glyph_not_available_p;
19322
19323 xassert (s->f == XFRAME (s->w->frame));
19324 xassert (s->nchars == 0);
19325 xassert (start >= 0 && end > start);
19326
19327 s->for_overlaps = overlaps,
19328 glyph = s->row->glyphs[s->area] + start;
19329 last = s->row->glyphs[s->area] + end;
19330 voffset = glyph->voffset;
19331
19332 glyph_not_available_p = glyph->glyph_not_available_p;
19333
19334 while (glyph < last
19335 && glyph->type == CHAR_GLYPH
19336 && glyph->voffset == voffset
19337 /* Same face id implies same font, nowadays. */
19338 && glyph->face_id == face_id
19339 && glyph->glyph_not_available_p == glyph_not_available_p)
19340 {
19341 int two_byte_p;
19342
19343 s->face = get_glyph_face_and_encoding (s->f, glyph,
19344 s->char2b + s->nchars,
19345 &two_byte_p);
19346 s->two_byte_p = two_byte_p;
19347 ++s->nchars;
19348 xassert (s->nchars <= end - start);
19349 s->width += glyph->pixel_width;
19350 ++glyph;
19351 }
19352
19353 s->font = s->face->font;
19354 s->font_info = FONT_INFO_FROM_FACE (s->f, s->face);
19355
19356 /* If the specified font could not be loaded, use the frame's font,
19357 but record the fact that we couldn't load it in
19358 S->font_not_found_p so that we can draw rectangles for the
19359 characters of the glyph string. */
19360 if (s->font == NULL || glyph_not_available_p)
19361 {
19362 s->font_not_found_p = 1;
19363 s->font = FRAME_FONT (s->f);
19364 }
19365
19366 /* Adjust base line for subscript/superscript text. */
19367 s->ybase += voffset;
19368
19369 xassert (s->face && s->face->gc);
19370 return glyph - s->row->glyphs[s->area];
19371 }
19372
19373
19374 /* Fill glyph string S from image glyph S->first_glyph. */
19375
19376 static void
19377 fill_image_glyph_string (s)
19378 struct glyph_string *s;
19379 {
19380 xassert (s->first_glyph->type == IMAGE_GLYPH);
19381 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
19382 xassert (s->img);
19383 s->slice = s->first_glyph->slice;
19384 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
19385 s->font = s->face->font;
19386 s->width = s->first_glyph->pixel_width;
19387
19388 /* Adjust base line for subscript/superscript text. */
19389 s->ybase += s->first_glyph->voffset;
19390 }
19391
19392
19393 /* Fill glyph string S from a sequence of stretch glyphs.
19394
19395 ROW is the glyph row in which the glyphs are found, AREA is the
19396 area within the row. START is the index of the first glyph to
19397 consider, END is the index of the last + 1.
19398
19399 Value is the index of the first glyph not in S. */
19400
19401 static int
19402 fill_stretch_glyph_string (s, row, area, start, end)
19403 struct glyph_string *s;
19404 struct glyph_row *row;
19405 enum glyph_row_area area;
19406 int start, end;
19407 {
19408 struct glyph *glyph, *last;
19409 int voffset, face_id;
19410
19411 xassert (s->first_glyph->type == STRETCH_GLYPH);
19412
19413 glyph = s->row->glyphs[s->area] + start;
19414 last = s->row->glyphs[s->area] + end;
19415 face_id = glyph->face_id;
19416 s->face = FACE_FROM_ID (s->f, face_id);
19417 s->font = s->face->font;
19418 s->font_info = FONT_INFO_FROM_FACE (s->f, s->face);
19419 s->width = glyph->pixel_width;
19420 s->nchars = 1;
19421 voffset = glyph->voffset;
19422
19423 for (++glyph;
19424 (glyph < last
19425 && glyph->type == STRETCH_GLYPH
19426 && glyph->voffset == voffset
19427 && glyph->face_id == face_id);
19428 ++glyph)
19429 s->width += glyph->pixel_width;
19430
19431 /* Adjust base line for subscript/superscript text. */
19432 s->ybase += voffset;
19433
19434 /* The case that face->gc == 0 is handled when drawing the glyph
19435 string by calling PREPARE_FACE_FOR_DISPLAY. */
19436 xassert (s->face);
19437 return glyph - s->row->glyphs[s->area];
19438 }
19439
19440 static XCharStruct *
19441 get_per_char_metric (f, font, font_info, char2b, font_type)
19442 struct frame *f;
19443 XFontStruct *font;
19444 struct font_info *font_info;
19445 XChar2b *char2b;
19446 int font_type;
19447 {
19448 #ifdef USE_FONT_BACKEND
19449 if (enable_font_backend)
19450 {
19451 static XCharStruct pcm_value;
19452 unsigned code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
19453 struct font *fontp;
19454 struct font_metrics metrics;
19455
19456 if (! font_info || code == FONT_INVALID_CODE)
19457 return NULL;
19458 fontp = (struct font *) font_info;
19459 fontp->driver->text_extents (fontp, &code, 1, &metrics);
19460 pcm_value.lbearing = metrics.lbearing;
19461 pcm_value.rbearing = metrics.rbearing;
19462 pcm_value.ascent = metrics.ascent;
19463 pcm_value.descent = metrics.descent;
19464 pcm_value.width = metrics.width;
19465 return &pcm_value;
19466 }
19467 #endif /* USE_FONT_BACKEND */
19468 return FRAME_RIF (f)->per_char_metric (font, char2b, font_type);
19469 }
19470
19471 /* EXPORT for RIF:
19472 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
19473 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
19474 assumed to be zero. */
19475
19476 void
19477 x_get_glyph_overhangs (glyph, f, left, right)
19478 struct glyph *glyph;
19479 struct frame *f;
19480 int *left, *right;
19481 {
19482 *left = *right = 0;
19483
19484 if (glyph->type == CHAR_GLYPH)
19485 {
19486 XFontStruct *font;
19487 struct face *face;
19488 struct font_info *font_info;
19489 XChar2b char2b;
19490 XCharStruct *pcm;
19491
19492 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
19493 font = face->font;
19494 font_info = FONT_INFO_FROM_FACE (f, face);
19495 if (font /* ++KFS: Should this be font_info ? */
19496 && (pcm = get_per_char_metric (f, font, font_info, &char2b, glyph->font_type)))
19497 {
19498 if (pcm->rbearing > pcm->width)
19499 *right = pcm->rbearing - pcm->width;
19500 if (pcm->lbearing < 0)
19501 *left = -pcm->lbearing;
19502 }
19503 }
19504 else if (glyph->type == COMPOSITE_GLYPH)
19505 {
19506 struct composition *cmp = composition_table[glyph->u.cmp_id];
19507
19508 *right = cmp->rbearing - cmp->pixel_width;
19509 *left = - cmp->lbearing;
19510 }
19511 }
19512
19513
19514 /* Return the index of the first glyph preceding glyph string S that
19515 is overwritten by S because of S's left overhang. Value is -1
19516 if no glyphs are overwritten. */
19517
19518 static int
19519 left_overwritten (s)
19520 struct glyph_string *s;
19521 {
19522 int k;
19523
19524 if (s->left_overhang)
19525 {
19526 int x = 0, i;
19527 struct glyph *glyphs = s->row->glyphs[s->area];
19528 int first = s->first_glyph - glyphs;
19529
19530 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
19531 x -= glyphs[i].pixel_width;
19532
19533 k = i + 1;
19534 }
19535 else
19536 k = -1;
19537
19538 return k;
19539 }
19540
19541
19542 /* Return the index of the first glyph preceding glyph string S that
19543 is overwriting S because of its right overhang. Value is -1 if no
19544 glyph in front of S overwrites S. */
19545
19546 static int
19547 left_overwriting (s)
19548 struct glyph_string *s;
19549 {
19550 int i, k, x;
19551 struct glyph *glyphs = s->row->glyphs[s->area];
19552 int first = s->first_glyph - glyphs;
19553
19554 k = -1;
19555 x = 0;
19556 for (i = first - 1; i >= 0; --i)
19557 {
19558 int left, right;
19559 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
19560 if (x + right > 0)
19561 k = i;
19562 x -= glyphs[i].pixel_width;
19563 }
19564
19565 return k;
19566 }
19567
19568
19569 /* Return the index of the last glyph following glyph string S that is
19570 not overwritten by S because of S's right overhang. Value is -1 if
19571 no such glyph is found. */
19572
19573 static int
19574 right_overwritten (s)
19575 struct glyph_string *s;
19576 {
19577 int k = -1;
19578
19579 if (s->right_overhang)
19580 {
19581 int x = 0, i;
19582 struct glyph *glyphs = s->row->glyphs[s->area];
19583 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
19584 int end = s->row->used[s->area];
19585
19586 for (i = first; i < end && s->right_overhang > x; ++i)
19587 x += glyphs[i].pixel_width;
19588
19589 k = i;
19590 }
19591
19592 return k;
19593 }
19594
19595
19596 /* Return the index of the last glyph following glyph string S that
19597 overwrites S because of its left overhang. Value is negative
19598 if no such glyph is found. */
19599
19600 static int
19601 right_overwriting (s)
19602 struct glyph_string *s;
19603 {
19604 int i, k, x;
19605 int end = s->row->used[s->area];
19606 struct glyph *glyphs = s->row->glyphs[s->area];
19607 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
19608
19609 k = -1;
19610 x = 0;
19611 for (i = first; i < end; ++i)
19612 {
19613 int left, right;
19614 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
19615 if (x - left < 0)
19616 k = i;
19617 x += glyphs[i].pixel_width;
19618 }
19619
19620 return k;
19621 }
19622
19623
19624 /* Set background width of glyph string S. START is the index of the
19625 first glyph following S. LAST_X is the right-most x-position + 1
19626 in the drawing area. */
19627
19628 static INLINE void
19629 set_glyph_string_background_width (s, start, last_x)
19630 struct glyph_string *s;
19631 int start;
19632 int last_x;
19633 {
19634 /* If the face of this glyph string has to be drawn to the end of
19635 the drawing area, set S->extends_to_end_of_line_p. */
19636
19637 if (start == s->row->used[s->area]
19638 && s->area == TEXT_AREA
19639 && ((s->row->fill_line_p
19640 && (s->hl == DRAW_NORMAL_TEXT
19641 || s->hl == DRAW_IMAGE_RAISED
19642 || s->hl == DRAW_IMAGE_SUNKEN))
19643 || s->hl == DRAW_MOUSE_FACE))
19644 s->extends_to_end_of_line_p = 1;
19645
19646 /* If S extends its face to the end of the line, set its
19647 background_width to the distance to the right edge of the drawing
19648 area. */
19649 if (s->extends_to_end_of_line_p)
19650 s->background_width = last_x - s->x + 1;
19651 else
19652 s->background_width = s->width;
19653 }
19654
19655
19656 /* Compute overhangs and x-positions for glyph string S and its
19657 predecessors, or successors. X is the starting x-position for S.
19658 BACKWARD_P non-zero means process predecessors. */
19659
19660 static void
19661 compute_overhangs_and_x (s, x, backward_p)
19662 struct glyph_string *s;
19663 int x;
19664 int backward_p;
19665 {
19666 if (backward_p)
19667 {
19668 while (s)
19669 {
19670 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
19671 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
19672 x -= s->width;
19673 s->x = x;
19674 s = s->prev;
19675 }
19676 }
19677 else
19678 {
19679 while (s)
19680 {
19681 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
19682 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
19683 s->x = x;
19684 x += s->width;
19685 s = s->next;
19686 }
19687 }
19688 }
19689
19690
19691
19692 /* The following macros are only called from draw_glyphs below.
19693 They reference the following parameters of that function directly:
19694 `w', `row', `area', and `overlap_p'
19695 as well as the following local variables:
19696 `s', `f', and `hdc' (in W32) */
19697
19698 #ifdef HAVE_NTGUI
19699 /* On W32, silently add local `hdc' variable to argument list of
19700 init_glyph_string. */
19701 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
19702 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
19703 #else
19704 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
19705 init_glyph_string (s, char2b, w, row, area, start, hl)
19706 #endif
19707
19708 /* Add a glyph string for a stretch glyph to the list of strings
19709 between HEAD and TAIL. START is the index of the stretch glyph in
19710 row area AREA of glyph row ROW. END is the index of the last glyph
19711 in that glyph row area. X is the current output position assigned
19712 to the new glyph string constructed. HL overrides that face of the
19713 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
19714 is the right-most x-position of the drawing area. */
19715
19716 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
19717 and below -- keep them on one line. */
19718 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
19719 do \
19720 { \
19721 s = (struct glyph_string *) alloca (sizeof *s); \
19722 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
19723 START = fill_stretch_glyph_string (s, row, area, START, END); \
19724 append_glyph_string (&HEAD, &TAIL, s); \
19725 s->x = (X); \
19726 } \
19727 while (0)
19728
19729
19730 /* Add a glyph string for an image glyph to the list of strings
19731 between HEAD and TAIL. START is the index of the image glyph in
19732 row area AREA of glyph row ROW. END is the index of the last glyph
19733 in that glyph row area. X is the current output position assigned
19734 to the new glyph string constructed. HL overrides that face of the
19735 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
19736 is the right-most x-position of the drawing area. */
19737
19738 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
19739 do \
19740 { \
19741 s = (struct glyph_string *) alloca (sizeof *s); \
19742 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
19743 fill_image_glyph_string (s); \
19744 append_glyph_string (&HEAD, &TAIL, s); \
19745 ++START; \
19746 s->x = (X); \
19747 } \
19748 while (0)
19749
19750
19751 /* Add a glyph string for a sequence of character glyphs to the list
19752 of strings between HEAD and TAIL. START is the index of the first
19753 glyph in row area AREA of glyph row ROW that is part of the new
19754 glyph string. END is the index of the last glyph in that glyph row
19755 area. X is the current output position assigned to the new glyph
19756 string constructed. HL overrides that face of the glyph; e.g. it
19757 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
19758 right-most x-position of the drawing area. */
19759
19760 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
19761 do \
19762 { \
19763 int face_id; \
19764 XChar2b *char2b; \
19765 \
19766 face_id = (row)->glyphs[area][START].face_id; \
19767 \
19768 s = (struct glyph_string *) alloca (sizeof *s); \
19769 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
19770 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
19771 append_glyph_string (&HEAD, &TAIL, s); \
19772 s->x = (X); \
19773 START = fill_glyph_string (s, face_id, START, END, overlaps); \
19774 } \
19775 while (0)
19776
19777
19778 /* Add a glyph string for a composite sequence to the list of strings
19779 between HEAD and TAIL. START is the index of the first glyph in
19780 row area AREA of glyph row ROW that is part of the new glyph
19781 string. END is the index of the last glyph in that glyph row area.
19782 X is the current output position assigned to the new glyph string
19783 constructed. HL overrides that face of the glyph; e.g. it is
19784 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
19785 x-position of the drawing area. */
19786
19787 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
19788 do { \
19789 int face_id = (row)->glyphs[area][START].face_id; \
19790 struct face *base_face = FACE_FROM_ID (f, face_id); \
19791 int cmp_id = (row)->glyphs[area][START].u.cmp_id; \
19792 struct composition *cmp = composition_table[cmp_id]; \
19793 XChar2b *char2b; \
19794 struct glyph_string *first_s; \
19795 int n; \
19796 \
19797 char2b = (XChar2b *) alloca ((sizeof *char2b) * cmp->glyph_len); \
19798 base_face = base_face->ascii_face; \
19799 \
19800 /* Make glyph_strings for each glyph sequence that is drawable by \
19801 the same face, and append them to HEAD/TAIL. */ \
19802 for (n = 0; n < cmp->glyph_len;) \
19803 { \
19804 s = (struct glyph_string *) alloca (sizeof *s); \
19805 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
19806 append_glyph_string (&(HEAD), &(TAIL), s); \
19807 s->cmp = cmp; \
19808 s->gidx = n; \
19809 s->x = (X); \
19810 if (n == 0) \
19811 first_s = s; \
19812 n = fill_composite_glyph_string (s, base_face, overlaps); \
19813 } \
19814 \
19815 ++START; \
19816 s = first_s; \
19817 } while (0)
19818
19819
19820 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
19821 of AREA of glyph row ROW on window W between indices START and END.
19822 HL overrides the face for drawing glyph strings, e.g. it is
19823 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
19824 x-positions of the drawing area.
19825
19826 This is an ugly monster macro construct because we must use alloca
19827 to allocate glyph strings (because draw_glyphs can be called
19828 asynchronously). */
19829
19830 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
19831 do \
19832 { \
19833 HEAD = TAIL = NULL; \
19834 while (START < END) \
19835 { \
19836 struct glyph *first_glyph = (row)->glyphs[area] + START; \
19837 switch (first_glyph->type) \
19838 { \
19839 case CHAR_GLYPH: \
19840 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
19841 HL, X, LAST_X); \
19842 break; \
19843 \
19844 case COMPOSITE_GLYPH: \
19845 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
19846 HL, X, LAST_X); \
19847 break; \
19848 \
19849 case STRETCH_GLYPH: \
19850 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
19851 HL, X, LAST_X); \
19852 break; \
19853 \
19854 case IMAGE_GLYPH: \
19855 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
19856 HL, X, LAST_X); \
19857 break; \
19858 \
19859 default: \
19860 abort (); \
19861 } \
19862 \
19863 if (s) \
19864 { \
19865 set_glyph_string_background_width (s, START, LAST_X); \
19866 (X) += s->width; \
19867 } \
19868 } \
19869 } \
19870 while (0)
19871
19872
19873 /* Draw glyphs between START and END in AREA of ROW on window W,
19874 starting at x-position X. X is relative to AREA in W. HL is a
19875 face-override with the following meaning:
19876
19877 DRAW_NORMAL_TEXT draw normally
19878 DRAW_CURSOR draw in cursor face
19879 DRAW_MOUSE_FACE draw in mouse face.
19880 DRAW_INVERSE_VIDEO draw in mode line face
19881 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
19882 DRAW_IMAGE_RAISED draw an image with a raised relief around it
19883
19884 If OVERLAPS is non-zero, draw only the foreground of characters and
19885 clip to the physical height of ROW. Non-zero value also defines
19886 the overlapping part to be drawn:
19887
19888 OVERLAPS_PRED overlap with preceding rows
19889 OVERLAPS_SUCC overlap with succeeding rows
19890 OVERLAPS_BOTH overlap with both preceding/succeeding rows
19891 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
19892
19893 Value is the x-position reached, relative to AREA of W. */
19894
19895 static int
19896 draw_glyphs (w, x, row, area, start, end, hl, overlaps)
19897 struct window *w;
19898 int x;
19899 struct glyph_row *row;
19900 enum glyph_row_area area;
19901 EMACS_INT start, end;
19902 enum draw_glyphs_face hl;
19903 int overlaps;
19904 {
19905 struct glyph_string *head, *tail;
19906 struct glyph_string *s;
19907 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
19908 int last_x, area_width;
19909 int x_reached;
19910 int i, j;
19911 struct frame *f = XFRAME (WINDOW_FRAME (w));
19912 DECLARE_HDC (hdc);
19913
19914 ALLOCATE_HDC (hdc, f);
19915
19916 /* Let's rather be paranoid than getting a SEGV. */
19917 end = min (end, row->used[area]);
19918 start = max (0, start);
19919 start = min (end, start);
19920
19921 /* Translate X to frame coordinates. Set last_x to the right
19922 end of the drawing area. */
19923 if (row->full_width_p)
19924 {
19925 /* X is relative to the left edge of W, without scroll bars
19926 or fringes. */
19927 x += WINDOW_LEFT_EDGE_X (w);
19928 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
19929 }
19930 else
19931 {
19932 int area_left = window_box_left (w, area);
19933 x += area_left;
19934 area_width = window_box_width (w, area);
19935 last_x = area_left + area_width;
19936 }
19937
19938 /* Build a doubly-linked list of glyph_string structures between
19939 head and tail from what we have to draw. Note that the macro
19940 BUILD_GLYPH_STRINGS will modify its start parameter. That's
19941 the reason we use a separate variable `i'. */
19942 i = start;
19943 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
19944 if (tail)
19945 x_reached = tail->x + tail->background_width;
19946 else
19947 x_reached = x;
19948
19949 /* If there are any glyphs with lbearing < 0 or rbearing > width in
19950 the row, redraw some glyphs in front or following the glyph
19951 strings built above. */
19952 if (head && !overlaps && row->contains_overlapping_glyphs_p)
19953 {
19954 int dummy_x = 0;
19955 struct glyph_string *h, *t;
19956
19957 /* Compute overhangs for all glyph strings. */
19958 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
19959 for (s = head; s; s = s->next)
19960 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
19961
19962 /* Prepend glyph strings for glyphs in front of the first glyph
19963 string that are overwritten because of the first glyph
19964 string's left overhang. The background of all strings
19965 prepended must be drawn because the first glyph string
19966 draws over it. */
19967 i = left_overwritten (head);
19968 if (i >= 0)
19969 {
19970 j = i;
19971 BUILD_GLYPH_STRINGS (j, start, h, t,
19972 DRAW_NORMAL_TEXT, dummy_x, last_x);
19973 start = i;
19974 compute_overhangs_and_x (t, head->x, 1);
19975 prepend_glyph_string_lists (&head, &tail, h, t);
19976 clip_head = head;
19977 }
19978
19979 /* Prepend glyph strings for glyphs in front of the first glyph
19980 string that overwrite that glyph string because of their
19981 right overhang. For these strings, only the foreground must
19982 be drawn, because it draws over the glyph string at `head'.
19983 The background must not be drawn because this would overwrite
19984 right overhangs of preceding glyphs for which no glyph
19985 strings exist. */
19986 i = left_overwriting (head);
19987 if (i >= 0)
19988 {
19989 clip_head = head;
19990 BUILD_GLYPH_STRINGS (i, start, h, t,
19991 DRAW_NORMAL_TEXT, dummy_x, last_x);
19992 for (s = h; s; s = s->next)
19993 s->background_filled_p = 1;
19994 compute_overhangs_and_x (t, head->x, 1);
19995 prepend_glyph_string_lists (&head, &tail, h, t);
19996 }
19997
19998 /* Append glyphs strings for glyphs following the last glyph
19999 string tail that are overwritten by tail. The background of
20000 these strings has to be drawn because tail's foreground draws
20001 over it. */
20002 i = right_overwritten (tail);
20003 if (i >= 0)
20004 {
20005 BUILD_GLYPH_STRINGS (end, i, h, t,
20006 DRAW_NORMAL_TEXT, x, last_x);
20007 compute_overhangs_and_x (h, tail->x + tail->width, 0);
20008 append_glyph_string_lists (&head, &tail, h, t);
20009 clip_tail = tail;
20010 }
20011
20012 /* Append glyph strings for glyphs following the last glyph
20013 string tail that overwrite tail. The foreground of such
20014 glyphs has to be drawn because it writes into the background
20015 of tail. The background must not be drawn because it could
20016 paint over the foreground of following glyphs. */
20017 i = right_overwriting (tail);
20018 if (i >= 0)
20019 {
20020 clip_tail = tail;
20021 i++; /* We must include the Ith glyph. */
20022 BUILD_GLYPH_STRINGS (end, i, h, t,
20023 DRAW_NORMAL_TEXT, x, last_x);
20024 for (s = h; s; s = s->next)
20025 s->background_filled_p = 1;
20026 compute_overhangs_and_x (h, tail->x + tail->width, 0);
20027 append_glyph_string_lists (&head, &tail, h, t);
20028 }
20029 if (clip_head || clip_tail)
20030 for (s = head; s; s = s->next)
20031 {
20032 s->clip_head = clip_head;
20033 s->clip_tail = clip_tail;
20034 }
20035 }
20036
20037 /* Draw all strings. */
20038 for (s = head; s; s = s->next)
20039 FRAME_RIF (f)->draw_glyph_string (s);
20040
20041 if (area == TEXT_AREA
20042 && !row->full_width_p
20043 /* When drawing overlapping rows, only the glyph strings'
20044 foreground is drawn, which doesn't erase a cursor
20045 completely. */
20046 && !overlaps)
20047 {
20048 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
20049 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
20050 : (tail ? tail->x + tail->background_width : x));
20051
20052 int text_left = window_box_left (w, TEXT_AREA);
20053 x0 -= text_left;
20054 x1 -= text_left;
20055
20056 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
20057 row->y, MATRIX_ROW_BOTTOM_Y (row));
20058 }
20059
20060 /* Value is the x-position up to which drawn, relative to AREA of W.
20061 This doesn't include parts drawn because of overhangs. */
20062 if (row->full_width_p)
20063 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
20064 else
20065 x_reached -= window_box_left (w, area);
20066
20067 RELEASE_HDC (hdc, f);
20068
20069 return x_reached;
20070 }
20071
20072 /* Expand row matrix if too narrow. Don't expand if area
20073 is not present. */
20074
20075 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
20076 { \
20077 if (!fonts_changed_p \
20078 && (it->glyph_row->glyphs[area] \
20079 < it->glyph_row->glyphs[area + 1])) \
20080 { \
20081 it->w->ncols_scale_factor++; \
20082 fonts_changed_p = 1; \
20083 } \
20084 }
20085
20086 /* Store one glyph for IT->char_to_display in IT->glyph_row.
20087 Called from x_produce_glyphs when IT->glyph_row is non-null. */
20088
20089 static INLINE void
20090 append_glyph (it)
20091 struct it *it;
20092 {
20093 struct glyph *glyph;
20094 enum glyph_row_area area = it->area;
20095
20096 xassert (it->glyph_row);
20097 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
20098
20099 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
20100 if (glyph < it->glyph_row->glyphs[area + 1])
20101 {
20102 glyph->charpos = CHARPOS (it->position);
20103 glyph->object = it->object;
20104 glyph->pixel_width = it->pixel_width;
20105 glyph->ascent = it->ascent;
20106 glyph->descent = it->descent;
20107 glyph->voffset = it->voffset;
20108 glyph->type = CHAR_GLYPH;
20109 glyph->multibyte_p = it->multibyte_p;
20110 glyph->left_box_line_p = it->start_of_box_run_p;
20111 glyph->right_box_line_p = it->end_of_box_run_p;
20112 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
20113 || it->phys_descent > it->descent);
20114 glyph->padding_p = 0;
20115 glyph->glyph_not_available_p = it->glyph_not_available_p;
20116 glyph->face_id = it->face_id;
20117 glyph->u.ch = it->char_to_display;
20118 glyph->slice = null_glyph_slice;
20119 glyph->font_type = FONT_TYPE_UNKNOWN;
20120 ++it->glyph_row->used[area];
20121 }
20122 else
20123 IT_EXPAND_MATRIX_WIDTH (it, area);
20124 }
20125
20126 /* Store one glyph for the composition IT->cmp_id in IT->glyph_row.
20127 Called from x_produce_glyphs when IT->glyph_row is non-null. */
20128
20129 static INLINE void
20130 append_composite_glyph (it)
20131 struct it *it;
20132 {
20133 struct glyph *glyph;
20134 enum glyph_row_area area = it->area;
20135
20136 xassert (it->glyph_row);
20137
20138 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
20139 if (glyph < it->glyph_row->glyphs[area + 1])
20140 {
20141 glyph->charpos = CHARPOS (it->position);
20142 glyph->object = it->object;
20143 glyph->pixel_width = it->pixel_width;
20144 glyph->ascent = it->ascent;
20145 glyph->descent = it->descent;
20146 glyph->voffset = it->voffset;
20147 glyph->type = COMPOSITE_GLYPH;
20148 glyph->multibyte_p = it->multibyte_p;
20149 glyph->left_box_line_p = it->start_of_box_run_p;
20150 glyph->right_box_line_p = it->end_of_box_run_p;
20151 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
20152 || it->phys_descent > it->descent);
20153 glyph->padding_p = 0;
20154 glyph->glyph_not_available_p = 0;
20155 glyph->face_id = it->face_id;
20156 glyph->u.cmp_id = it->cmp_id;
20157 glyph->slice = null_glyph_slice;
20158 glyph->font_type = FONT_TYPE_UNKNOWN;
20159 ++it->glyph_row->used[area];
20160 }
20161 else
20162 IT_EXPAND_MATRIX_WIDTH (it, area);
20163 }
20164
20165
20166 /* Change IT->ascent and IT->height according to the setting of
20167 IT->voffset. */
20168
20169 static INLINE void
20170 take_vertical_position_into_account (it)
20171 struct it *it;
20172 {
20173 if (it->voffset)
20174 {
20175 if (it->voffset < 0)
20176 /* Increase the ascent so that we can display the text higher
20177 in the line. */
20178 it->ascent -= it->voffset;
20179 else
20180 /* Increase the descent so that we can display the text lower
20181 in the line. */
20182 it->descent += it->voffset;
20183 }
20184 }
20185
20186
20187 /* Produce glyphs/get display metrics for the image IT is loaded with.
20188 See the description of struct display_iterator in dispextern.h for
20189 an overview of struct display_iterator. */
20190
20191 static void
20192 produce_image_glyph (it)
20193 struct it *it;
20194 {
20195 struct image *img;
20196 struct face *face;
20197 int glyph_ascent, crop;
20198 struct glyph_slice slice;
20199
20200 xassert (it->what == IT_IMAGE);
20201
20202 face = FACE_FROM_ID (it->f, it->face_id);
20203 xassert (face);
20204 /* Make sure X resources of the face is loaded. */
20205 PREPARE_FACE_FOR_DISPLAY (it->f, face);
20206
20207 if (it->image_id < 0)
20208 {
20209 /* Fringe bitmap. */
20210 it->ascent = it->phys_ascent = 0;
20211 it->descent = it->phys_descent = 0;
20212 it->pixel_width = 0;
20213 it->nglyphs = 0;
20214 return;
20215 }
20216
20217 img = IMAGE_FROM_ID (it->f, it->image_id);
20218 xassert (img);
20219 /* Make sure X resources of the image is loaded. */
20220 prepare_image_for_display (it->f, img);
20221
20222 slice.x = slice.y = 0;
20223 slice.width = img->width;
20224 slice.height = img->height;
20225
20226 if (INTEGERP (it->slice.x))
20227 slice.x = XINT (it->slice.x);
20228 else if (FLOATP (it->slice.x))
20229 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
20230
20231 if (INTEGERP (it->slice.y))
20232 slice.y = XINT (it->slice.y);
20233 else if (FLOATP (it->slice.y))
20234 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
20235
20236 if (INTEGERP (it->slice.width))
20237 slice.width = XINT (it->slice.width);
20238 else if (FLOATP (it->slice.width))
20239 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
20240
20241 if (INTEGERP (it->slice.height))
20242 slice.height = XINT (it->slice.height);
20243 else if (FLOATP (it->slice.height))
20244 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
20245
20246 if (slice.x >= img->width)
20247 slice.x = img->width;
20248 if (slice.y >= img->height)
20249 slice.y = img->height;
20250 if (slice.x + slice.width >= img->width)
20251 slice.width = img->width - slice.x;
20252 if (slice.y + slice.height > img->height)
20253 slice.height = img->height - slice.y;
20254
20255 if (slice.width == 0 || slice.height == 0)
20256 return;
20257
20258 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
20259
20260 it->descent = slice.height - glyph_ascent;
20261 if (slice.y == 0)
20262 it->descent += img->vmargin;
20263 if (slice.y + slice.height == img->height)
20264 it->descent += img->vmargin;
20265 it->phys_descent = it->descent;
20266
20267 it->pixel_width = slice.width;
20268 if (slice.x == 0)
20269 it->pixel_width += img->hmargin;
20270 if (slice.x + slice.width == img->width)
20271 it->pixel_width += img->hmargin;
20272
20273 /* It's quite possible for images to have an ascent greater than
20274 their height, so don't get confused in that case. */
20275 if (it->descent < 0)
20276 it->descent = 0;
20277
20278 #if 0 /* this breaks image tiling */
20279 /* If this glyph is alone on the last line, adjust it.ascent to minimum row ascent. */
20280 int face_ascent = face->font ? FONT_BASE (face->font) : FRAME_BASELINE_OFFSET (it->f);
20281 if (face_ascent > it->ascent)
20282 it->ascent = it->phys_ascent = face_ascent;
20283 #endif
20284
20285 it->nglyphs = 1;
20286
20287 if (face->box != FACE_NO_BOX)
20288 {
20289 if (face->box_line_width > 0)
20290 {
20291 if (slice.y == 0)
20292 it->ascent += face->box_line_width;
20293 if (slice.y + slice.height == img->height)
20294 it->descent += face->box_line_width;
20295 }
20296
20297 if (it->start_of_box_run_p && slice.x == 0)
20298 it->pixel_width += eabs (face->box_line_width);
20299 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
20300 it->pixel_width += eabs (face->box_line_width);
20301 }
20302
20303 take_vertical_position_into_account (it);
20304
20305 /* Automatically crop wide image glyphs at right edge so we can
20306 draw the cursor on same display row. */
20307 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
20308 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
20309 {
20310 it->pixel_width -= crop;
20311 slice.width -= crop;
20312 }
20313
20314 if (it->glyph_row)
20315 {
20316 struct glyph *glyph;
20317 enum glyph_row_area area = it->area;
20318
20319 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
20320 if (glyph < it->glyph_row->glyphs[area + 1])
20321 {
20322 glyph->charpos = CHARPOS (it->position);
20323 glyph->object = it->object;
20324 glyph->pixel_width = it->pixel_width;
20325 glyph->ascent = glyph_ascent;
20326 glyph->descent = it->descent;
20327 glyph->voffset = it->voffset;
20328 glyph->type = IMAGE_GLYPH;
20329 glyph->multibyte_p = it->multibyte_p;
20330 glyph->left_box_line_p = it->start_of_box_run_p;
20331 glyph->right_box_line_p = it->end_of_box_run_p;
20332 glyph->overlaps_vertically_p = 0;
20333 glyph->padding_p = 0;
20334 glyph->glyph_not_available_p = 0;
20335 glyph->face_id = it->face_id;
20336 glyph->u.img_id = img->id;
20337 glyph->slice = slice;
20338 glyph->font_type = FONT_TYPE_UNKNOWN;
20339 ++it->glyph_row->used[area];
20340 }
20341 else
20342 IT_EXPAND_MATRIX_WIDTH (it, area);
20343 }
20344 }
20345
20346
20347 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
20348 of the glyph, WIDTH and HEIGHT are the width and height of the
20349 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
20350
20351 static void
20352 append_stretch_glyph (it, object, width, height, ascent)
20353 struct it *it;
20354 Lisp_Object object;
20355 int width, height;
20356 int ascent;
20357 {
20358 struct glyph *glyph;
20359 enum glyph_row_area area = it->area;
20360
20361 xassert (ascent >= 0 && ascent <= height);
20362
20363 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
20364 if (glyph < it->glyph_row->glyphs[area + 1])
20365 {
20366 glyph->charpos = CHARPOS (it->position);
20367 glyph->object = object;
20368 glyph->pixel_width = width;
20369 glyph->ascent = ascent;
20370 glyph->descent = height - ascent;
20371 glyph->voffset = it->voffset;
20372 glyph->type = STRETCH_GLYPH;
20373 glyph->multibyte_p = it->multibyte_p;
20374 glyph->left_box_line_p = it->start_of_box_run_p;
20375 glyph->right_box_line_p = it->end_of_box_run_p;
20376 glyph->overlaps_vertically_p = 0;
20377 glyph->padding_p = 0;
20378 glyph->glyph_not_available_p = 0;
20379 glyph->face_id = it->face_id;
20380 glyph->u.stretch.ascent = ascent;
20381 glyph->u.stretch.height = height;
20382 glyph->slice = null_glyph_slice;
20383 glyph->font_type = FONT_TYPE_UNKNOWN;
20384 ++it->glyph_row->used[area];
20385 }
20386 else
20387 IT_EXPAND_MATRIX_WIDTH (it, area);
20388 }
20389
20390
20391 /* Produce a stretch glyph for iterator IT. IT->object is the value
20392 of the glyph property displayed. The value must be a list
20393 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
20394 being recognized:
20395
20396 1. `:width WIDTH' specifies that the space should be WIDTH *
20397 canonical char width wide. WIDTH may be an integer or floating
20398 point number.
20399
20400 2. `:relative-width FACTOR' specifies that the width of the stretch
20401 should be computed from the width of the first character having the
20402 `glyph' property, and should be FACTOR times that width.
20403
20404 3. `:align-to HPOS' specifies that the space should be wide enough
20405 to reach HPOS, a value in canonical character units.
20406
20407 Exactly one of the above pairs must be present.
20408
20409 4. `:height HEIGHT' specifies that the height of the stretch produced
20410 should be HEIGHT, measured in canonical character units.
20411
20412 5. `:relative-height FACTOR' specifies that the height of the
20413 stretch should be FACTOR times the height of the characters having
20414 the glyph property.
20415
20416 Either none or exactly one of 4 or 5 must be present.
20417
20418 6. `:ascent ASCENT' specifies that ASCENT percent of the height
20419 of the stretch should be used for the ascent of the stretch.
20420 ASCENT must be in the range 0 <= ASCENT <= 100. */
20421
20422 static void
20423 produce_stretch_glyph (it)
20424 struct it *it;
20425 {
20426 /* (space :width WIDTH :height HEIGHT ...) */
20427 Lisp_Object prop, plist;
20428 int width = 0, height = 0, align_to = -1;
20429 int zero_width_ok_p = 0, zero_height_ok_p = 0;
20430 int ascent = 0;
20431 double tem;
20432 struct face *face = FACE_FROM_ID (it->f, it->face_id);
20433 XFontStruct *font = face->font ? face->font : FRAME_FONT (it->f);
20434
20435 PREPARE_FACE_FOR_DISPLAY (it->f, face);
20436
20437 /* List should start with `space'. */
20438 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
20439 plist = XCDR (it->object);
20440
20441 /* Compute the width of the stretch. */
20442 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
20443 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
20444 {
20445 /* Absolute width `:width WIDTH' specified and valid. */
20446 zero_width_ok_p = 1;
20447 width = (int)tem;
20448 }
20449 else if (prop = Fplist_get (plist, QCrelative_width),
20450 NUMVAL (prop) > 0)
20451 {
20452 /* Relative width `:relative-width FACTOR' specified and valid.
20453 Compute the width of the characters having the `glyph'
20454 property. */
20455 struct it it2;
20456 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
20457
20458 it2 = *it;
20459 if (it->multibyte_p)
20460 {
20461 int maxlen = ((IT_BYTEPOS (*it) >= GPT ? ZV : GPT)
20462 - IT_BYTEPOS (*it));
20463 it2.c = STRING_CHAR_AND_LENGTH (p, maxlen, it2.len);
20464 }
20465 else
20466 it2.c = *p, it2.len = 1;
20467
20468 it2.glyph_row = NULL;
20469 it2.what = IT_CHARACTER;
20470 x_produce_glyphs (&it2);
20471 width = NUMVAL (prop) * it2.pixel_width;
20472 }
20473 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
20474 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
20475 {
20476 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
20477 align_to = (align_to < 0
20478 ? 0
20479 : align_to - window_box_left_offset (it->w, TEXT_AREA));
20480 else if (align_to < 0)
20481 align_to = window_box_left_offset (it->w, TEXT_AREA);
20482 width = max (0, (int)tem + align_to - it->current_x);
20483 zero_width_ok_p = 1;
20484 }
20485 else
20486 /* Nothing specified -> width defaults to canonical char width. */
20487 width = FRAME_COLUMN_WIDTH (it->f);
20488
20489 if (width <= 0 && (width < 0 || !zero_width_ok_p))
20490 width = 1;
20491
20492 /* Compute height. */
20493 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
20494 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
20495 {
20496 height = (int)tem;
20497 zero_height_ok_p = 1;
20498 }
20499 else if (prop = Fplist_get (plist, QCrelative_height),
20500 NUMVAL (prop) > 0)
20501 height = FONT_HEIGHT (font) * NUMVAL (prop);
20502 else
20503 height = FONT_HEIGHT (font);
20504
20505 if (height <= 0 && (height < 0 || !zero_height_ok_p))
20506 height = 1;
20507
20508 /* Compute percentage of height used for ascent. If
20509 `:ascent ASCENT' is present and valid, use that. Otherwise,
20510 derive the ascent from the font in use. */
20511 if (prop = Fplist_get (plist, QCascent),
20512 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
20513 ascent = height * NUMVAL (prop) / 100.0;
20514 else if (!NILP (prop)
20515 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
20516 ascent = min (max (0, (int)tem), height);
20517 else
20518 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
20519
20520 if (width > 0 && !it->truncate_lines_p
20521 && it->current_x + width > it->last_visible_x)
20522 width = it->last_visible_x - it->current_x - 1;
20523
20524 if (width > 0 && height > 0 && it->glyph_row)
20525 {
20526 Lisp_Object object = it->stack[it->sp - 1].string;
20527 if (!STRINGP (object))
20528 object = it->w->buffer;
20529 append_stretch_glyph (it, object, width, height, ascent);
20530 }
20531
20532 it->pixel_width = width;
20533 it->ascent = it->phys_ascent = ascent;
20534 it->descent = it->phys_descent = height - it->ascent;
20535 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
20536
20537 take_vertical_position_into_account (it);
20538 }
20539
20540 /* Get line-height and line-spacing property at point.
20541 If line-height has format (HEIGHT TOTAL), return TOTAL
20542 in TOTAL_HEIGHT. */
20543
20544 static Lisp_Object
20545 get_line_height_property (it, prop)
20546 struct it *it;
20547 Lisp_Object prop;
20548 {
20549 Lisp_Object position;
20550
20551 if (STRINGP (it->object))
20552 position = make_number (IT_STRING_CHARPOS (*it));
20553 else if (BUFFERP (it->object))
20554 position = make_number (IT_CHARPOS (*it));
20555 else
20556 return Qnil;
20557
20558 return Fget_char_property (position, prop, it->object);
20559 }
20560
20561 /* Calculate line-height and line-spacing properties.
20562 An integer value specifies explicit pixel value.
20563 A float value specifies relative value to current face height.
20564 A cons (float . face-name) specifies relative value to
20565 height of specified face font.
20566
20567 Returns height in pixels, or nil. */
20568
20569
20570 static Lisp_Object
20571 calc_line_height_property (it, val, font, boff, override)
20572 struct it *it;
20573 Lisp_Object val;
20574 XFontStruct *font;
20575 int boff, override;
20576 {
20577 Lisp_Object face_name = Qnil;
20578 int ascent, descent, height;
20579
20580 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
20581 return val;
20582
20583 if (CONSP (val))
20584 {
20585 face_name = XCAR (val);
20586 val = XCDR (val);
20587 if (!NUMBERP (val))
20588 val = make_number (1);
20589 if (NILP (face_name))
20590 {
20591 height = it->ascent + it->descent;
20592 goto scale;
20593 }
20594 }
20595
20596 if (NILP (face_name))
20597 {
20598 font = FRAME_FONT (it->f);
20599 boff = FRAME_BASELINE_OFFSET (it->f);
20600 }
20601 else if (EQ (face_name, Qt))
20602 {
20603 override = 0;
20604 }
20605 else
20606 {
20607 int face_id;
20608 struct face *face;
20609 struct font_info *font_info;
20610
20611 face_id = lookup_named_face (it->f, face_name, 0);
20612 if (face_id < 0)
20613 return make_number (-1);
20614
20615 face = FACE_FROM_ID (it->f, face_id);
20616 font = face->font;
20617 if (font == NULL)
20618 return make_number (-1);
20619
20620 font_info = FONT_INFO_FROM_FACE (it->f, face);
20621 boff = font_info->baseline_offset;
20622 if (font_info->vertical_centering)
20623 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
20624 }
20625
20626 ascent = FONT_BASE (font) + boff;
20627 descent = FONT_DESCENT (font) - boff;
20628
20629 if (override)
20630 {
20631 it->override_ascent = ascent;
20632 it->override_descent = descent;
20633 it->override_boff = boff;
20634 }
20635
20636 height = ascent + descent;
20637
20638 scale:
20639 if (FLOATP (val))
20640 height = (int)(XFLOAT_DATA (val) * height);
20641 else if (INTEGERP (val))
20642 height *= XINT (val);
20643
20644 return make_number (height);
20645 }
20646
20647
20648 /* RIF:
20649 Produce glyphs/get display metrics for the display element IT is
20650 loaded with. See the description of struct it in dispextern.h
20651 for an overview of struct it. */
20652
20653 void
20654 x_produce_glyphs (it)
20655 struct it *it;
20656 {
20657 int extra_line_spacing = it->extra_line_spacing;
20658
20659 it->glyph_not_available_p = 0;
20660
20661 if (it->what == IT_CHARACTER)
20662 {
20663 XChar2b char2b;
20664 XFontStruct *font;
20665 struct face *face = FACE_FROM_ID (it->f, it->face_id);
20666 XCharStruct *pcm;
20667 int font_not_found_p;
20668 struct font_info *font_info;
20669 int boff; /* baseline offset */
20670 /* We may change it->multibyte_p upon unibyte<->multibyte
20671 conversion. So, save the current value now and restore it
20672 later.
20673
20674 Note: It seems that we don't have to record multibyte_p in
20675 struct glyph because the character code itself tells if or
20676 not the character is multibyte. Thus, in the future, we must
20677 consider eliminating the field `multibyte_p' in the struct
20678 glyph. */
20679 int saved_multibyte_p = it->multibyte_p;
20680
20681 /* Maybe translate single-byte characters to multibyte, or the
20682 other way. */
20683 it->char_to_display = it->c;
20684 if (!ASCII_BYTE_P (it->c)
20685 && ! it->multibyte_p)
20686 {
20687 if (SINGLE_BYTE_CHAR_P (it->c)
20688 && unibyte_display_via_language_environment)
20689 it->char_to_display = unibyte_char_to_multibyte (it->c);
20690 if (! SINGLE_BYTE_CHAR_P (it->char_to_display))
20691 {
20692 it->multibyte_p = 1;
20693 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display,
20694 -1, Qnil);
20695 face = FACE_FROM_ID (it->f, it->face_id);
20696 }
20697 }
20698
20699 /* Get font to use. Encode IT->char_to_display. */
20700 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
20701 &char2b, it->multibyte_p, 0);
20702 font = face->font;
20703
20704 /* When no suitable font found, use the default font. */
20705 font_not_found_p = font == NULL;
20706 if (font_not_found_p)
20707 {
20708 font = FRAME_FONT (it->f);
20709 boff = FRAME_BASELINE_OFFSET (it->f);
20710 font_info = NULL;
20711 }
20712 else
20713 {
20714 font_info = FONT_INFO_FROM_FACE (it->f, face);
20715 boff = font_info->baseline_offset;
20716 if (font_info->vertical_centering)
20717 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
20718 }
20719
20720 if (it->char_to_display >= ' '
20721 && (!it->multibyte_p || it->char_to_display < 128))
20722 {
20723 /* Either unibyte or ASCII. */
20724 int stretched_p;
20725
20726 it->nglyphs = 1;
20727
20728 pcm = get_per_char_metric (it->f, font, font_info, &char2b,
20729 FONT_TYPE_FOR_UNIBYTE (font, it->char_to_display));
20730
20731 if (it->override_ascent >= 0)
20732 {
20733 it->ascent = it->override_ascent;
20734 it->descent = it->override_descent;
20735 boff = it->override_boff;
20736 }
20737 else
20738 {
20739 it->ascent = FONT_BASE (font) + boff;
20740 it->descent = FONT_DESCENT (font) - boff;
20741 }
20742
20743 if (pcm)
20744 {
20745 it->phys_ascent = pcm->ascent + boff;
20746 it->phys_descent = pcm->descent - boff;
20747 it->pixel_width = pcm->width;
20748 }
20749 else
20750 {
20751 it->glyph_not_available_p = 1;
20752 it->phys_ascent = it->ascent;
20753 it->phys_descent = it->descent;
20754 it->pixel_width = FONT_WIDTH (font);
20755 }
20756
20757 if (it->constrain_row_ascent_descent_p)
20758 {
20759 if (it->descent > it->max_descent)
20760 {
20761 it->ascent += it->descent - it->max_descent;
20762 it->descent = it->max_descent;
20763 }
20764 if (it->ascent > it->max_ascent)
20765 {
20766 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
20767 it->ascent = it->max_ascent;
20768 }
20769 it->phys_ascent = min (it->phys_ascent, it->ascent);
20770 it->phys_descent = min (it->phys_descent, it->descent);
20771 extra_line_spacing = 0;
20772 }
20773
20774 /* If this is a space inside a region of text with
20775 `space-width' property, change its width. */
20776 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
20777 if (stretched_p)
20778 it->pixel_width *= XFLOATINT (it->space_width);
20779
20780 /* If face has a box, add the box thickness to the character
20781 height. If character has a box line to the left and/or
20782 right, add the box line width to the character's width. */
20783 if (face->box != FACE_NO_BOX)
20784 {
20785 int thick = face->box_line_width;
20786
20787 if (thick > 0)
20788 {
20789 it->ascent += thick;
20790 it->descent += thick;
20791 }
20792 else
20793 thick = -thick;
20794
20795 if (it->start_of_box_run_p)
20796 it->pixel_width += thick;
20797 if (it->end_of_box_run_p)
20798 it->pixel_width += thick;
20799 }
20800
20801 /* If face has an overline, add the height of the overline
20802 (1 pixel) and a 1 pixel margin to the character height. */
20803 if (face->overline_p)
20804 it->ascent += overline_margin;
20805
20806 if (it->constrain_row_ascent_descent_p)
20807 {
20808 if (it->ascent > it->max_ascent)
20809 it->ascent = it->max_ascent;
20810 if (it->descent > it->max_descent)
20811 it->descent = it->max_descent;
20812 }
20813
20814 take_vertical_position_into_account (it);
20815
20816 /* If we have to actually produce glyphs, do it. */
20817 if (it->glyph_row)
20818 {
20819 if (stretched_p)
20820 {
20821 /* Translate a space with a `space-width' property
20822 into a stretch glyph. */
20823 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
20824 / FONT_HEIGHT (font));
20825 append_stretch_glyph (it, it->object, it->pixel_width,
20826 it->ascent + it->descent, ascent);
20827 }
20828 else
20829 append_glyph (it);
20830
20831 /* If characters with lbearing or rbearing are displayed
20832 in this line, record that fact in a flag of the
20833 glyph row. This is used to optimize X output code. */
20834 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
20835 it->glyph_row->contains_overlapping_glyphs_p = 1;
20836 }
20837 }
20838 else if (it->char_to_display == '\n')
20839 {
20840 /* A newline has no width but we need the height of the line.
20841 But if previous part of the line set a height, don't
20842 increase that height */
20843
20844 Lisp_Object height;
20845 Lisp_Object total_height = Qnil;
20846
20847 it->override_ascent = -1;
20848 it->pixel_width = 0;
20849 it->nglyphs = 0;
20850
20851 height = get_line_height_property(it, Qline_height);
20852 /* Split (line-height total-height) list */
20853 if (CONSP (height)
20854 && CONSP (XCDR (height))
20855 && NILP (XCDR (XCDR (height))))
20856 {
20857 total_height = XCAR (XCDR (height));
20858 height = XCAR (height);
20859 }
20860 height = calc_line_height_property(it, height, font, boff, 1);
20861
20862 if (it->override_ascent >= 0)
20863 {
20864 it->ascent = it->override_ascent;
20865 it->descent = it->override_descent;
20866 boff = it->override_boff;
20867 }
20868 else
20869 {
20870 it->ascent = FONT_BASE (font) + boff;
20871 it->descent = FONT_DESCENT (font) - boff;
20872 }
20873
20874 if (EQ (height, Qt))
20875 {
20876 if (it->descent > it->max_descent)
20877 {
20878 it->ascent += it->descent - it->max_descent;
20879 it->descent = it->max_descent;
20880 }
20881 if (it->ascent > it->max_ascent)
20882 {
20883 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
20884 it->ascent = it->max_ascent;
20885 }
20886 it->phys_ascent = min (it->phys_ascent, it->ascent);
20887 it->phys_descent = min (it->phys_descent, it->descent);
20888 it->constrain_row_ascent_descent_p = 1;
20889 extra_line_spacing = 0;
20890 }
20891 else
20892 {
20893 Lisp_Object spacing;
20894
20895 it->phys_ascent = it->ascent;
20896 it->phys_descent = it->descent;
20897
20898 if ((it->max_ascent > 0 || it->max_descent > 0)
20899 && face->box != FACE_NO_BOX
20900 && face->box_line_width > 0)
20901 {
20902 it->ascent += face->box_line_width;
20903 it->descent += face->box_line_width;
20904 }
20905 if (!NILP (height)
20906 && XINT (height) > it->ascent + it->descent)
20907 it->ascent = XINT (height) - it->descent;
20908
20909 if (!NILP (total_height))
20910 spacing = calc_line_height_property(it, total_height, font, boff, 0);
20911 else
20912 {
20913 spacing = get_line_height_property(it, Qline_spacing);
20914 spacing = calc_line_height_property(it, spacing, font, boff, 0);
20915 }
20916 if (INTEGERP (spacing))
20917 {
20918 extra_line_spacing = XINT (spacing);
20919 if (!NILP (total_height))
20920 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
20921 }
20922 }
20923 }
20924 else if (it->char_to_display == '\t')
20925 {
20926 int tab_width = it->tab_width * FRAME_SPACE_WIDTH (it->f);
20927 int x = it->current_x + it->continuation_lines_width;
20928 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
20929
20930 /* If the distance from the current position to the next tab
20931 stop is less than a space character width, use the
20932 tab stop after that. */
20933 if (next_tab_x - x < FRAME_SPACE_WIDTH (it->f))
20934 next_tab_x += tab_width;
20935
20936 it->pixel_width = next_tab_x - x;
20937 it->nglyphs = 1;
20938 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
20939 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
20940
20941 if (it->glyph_row)
20942 {
20943 append_stretch_glyph (it, it->object, it->pixel_width,
20944 it->ascent + it->descent, it->ascent);
20945 }
20946 }
20947 else
20948 {
20949 /* A multi-byte character. Assume that the display width of the
20950 character is the width of the character multiplied by the
20951 width of the font. */
20952
20953 /* If we found a font, this font should give us the right
20954 metrics. If we didn't find a font, use the frame's
20955 default font and calculate the width of the character by
20956 multiplying the width of font by the width of the
20957 character. */
20958
20959 pcm = get_per_char_metric (it->f, font, font_info, &char2b,
20960 FONT_TYPE_FOR_MULTIBYTE (font, it->c));
20961
20962 if (font_not_found_p || !pcm)
20963 {
20964 int char_width = CHAR_WIDTH (it->char_to_display);
20965
20966 if (char_width == 0)
20967 /* This is a non spacing character. But, as we are
20968 going to display an empty box, the box must occupy
20969 at least one column. */
20970 char_width = 1;
20971 it->glyph_not_available_p = 1;
20972 it->pixel_width = FRAME_COLUMN_WIDTH (it->f) * char_width;
20973 it->phys_ascent = FONT_BASE (font) + boff;
20974 it->phys_descent = FONT_DESCENT (font) - boff;
20975 }
20976 else
20977 {
20978 it->pixel_width = pcm->width;
20979 it->phys_ascent = pcm->ascent + boff;
20980 it->phys_descent = pcm->descent - boff;
20981 if (it->glyph_row
20982 && (pcm->lbearing < 0
20983 || pcm->rbearing > pcm->width))
20984 it->glyph_row->contains_overlapping_glyphs_p = 1;
20985 }
20986 it->nglyphs = 1;
20987 it->ascent = FONT_BASE (font) + boff;
20988 it->descent = FONT_DESCENT (font) - boff;
20989 if (face->box != FACE_NO_BOX)
20990 {
20991 int thick = face->box_line_width;
20992
20993 if (thick > 0)
20994 {
20995 it->ascent += thick;
20996 it->descent += thick;
20997 }
20998 else
20999 thick = - thick;
21000
21001 if (it->start_of_box_run_p)
21002 it->pixel_width += thick;
21003 if (it->end_of_box_run_p)
21004 it->pixel_width += thick;
21005 }
21006
21007 /* If face has an overline, add the height of the overline
21008 (1 pixel) and a 1 pixel margin to the character height. */
21009 if (face->overline_p)
21010 it->ascent += overline_margin;
21011
21012 take_vertical_position_into_account (it);
21013
21014 if (it->glyph_row)
21015 append_glyph (it);
21016 }
21017 it->multibyte_p = saved_multibyte_p;
21018 }
21019 else if (it->what == IT_COMPOSITION)
21020 {
21021 /* Note: A composition is represented as one glyph in the
21022 glyph matrix. There are no padding glyphs.
21023
21024 Important is that pixel_width, ascent, and descent are the
21025 values of what is drawn by draw_glyphs (i.e. the values of
21026 the overall glyphs composed). */
21027 struct face *face = FACE_FROM_ID (it->f, it->face_id);
21028 int boff; /* baseline offset */
21029 struct composition *cmp = composition_table[it->cmp_id];
21030 int glyph_len = cmp->glyph_len;
21031 XFontStruct *font = face->font;
21032
21033 it->nglyphs = 1;
21034
21035 #ifdef USE_FONT_BACKEND
21036 if (cmp->method == COMPOSITION_WITH_GLYPH_STRING)
21037 {
21038 if (! cmp->font || cmp->font != font)
21039 font_prepare_composition (cmp);
21040 }
21041 else
21042 #endif /* USE_FONT_BACKEND */
21043 /* If we have not yet calculated pixel size data of glyphs of
21044 the composition for the current face font, calculate them
21045 now. Theoretically, we have to check all fonts for the
21046 glyphs, but that requires much time and memory space. So,
21047 here we check only the font of the first glyph. This leads
21048 to incorrect display, but it's very rare, and C-l (recenter)
21049 can correct the display anyway. */
21050 if (! cmp->font || cmp->font != font)
21051 {
21052 /* Ascent and descent of the font of the first character
21053 of this composition (adjusted by baseline offset).
21054 Ascent and descent of overall glyphs should not be less
21055 than them respectively. */
21056 int font_ascent, font_descent, font_height;
21057 /* Bounding box of the overall glyphs. */
21058 int leftmost, rightmost, lowest, highest;
21059 int lbearing, rbearing;
21060 int i, width, ascent, descent;
21061 int left_padded = 0, right_padded = 0;
21062 int face_id;
21063 int c;
21064 XChar2b char2b;
21065 XCharStruct *pcm;
21066 int font_not_found_p;
21067 struct font_info *font_info;
21068 int pos;
21069
21070 for (glyph_len = cmp->glyph_len; glyph_len > 0; glyph_len--)
21071 if ((c = COMPOSITION_GLYPH (cmp, glyph_len - 1)) != '\t')
21072 break;
21073 if (glyph_len < cmp->glyph_len)
21074 right_padded = 1;
21075 for (i = 0; i < glyph_len; i++)
21076 {
21077 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
21078 break;
21079 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
21080 }
21081 if (i > 0)
21082 left_padded = 1;
21083
21084 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
21085 : IT_CHARPOS (*it));
21086 /* When no suitable font found, use the default font. */
21087 font_not_found_p = font == NULL;
21088 if (font_not_found_p)
21089 {
21090 face = face->ascii_face;
21091 font = face->font;
21092 }
21093 font_info = FONT_INFO_FROM_FACE (it->f, face);
21094 boff = font_info->baseline_offset;
21095 if (font_info->vertical_centering)
21096 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
21097 font_ascent = FONT_BASE (font) + boff;
21098 font_descent = FONT_DESCENT (font) - boff;
21099 font_height = FONT_HEIGHT (font);
21100
21101 cmp->font = (void *) font;
21102
21103 pcm = NULL;
21104 if (! font_not_found_p)
21105 {
21106 get_char_face_and_encoding (it->f, c, it->face_id,
21107 &char2b, it->multibyte_p, 0);
21108 pcm = get_per_char_metric (it->f, font, font_info, &char2b,
21109 FONT_TYPE_FOR_MULTIBYTE (font, c));
21110 }
21111
21112 /* Initialize the bounding box. */
21113 if (pcm)
21114 {
21115 width = pcm->width;
21116 ascent = pcm->ascent;
21117 descent = pcm->descent;
21118 lbearing = pcm->lbearing;
21119 rbearing = pcm->rbearing;
21120 }
21121 else
21122 {
21123 width = FONT_WIDTH (font);
21124 ascent = FONT_BASE (font);
21125 descent = FONT_DESCENT (font);
21126 lbearing = 0;
21127 rbearing = width;
21128 }
21129
21130 rightmost = width;
21131 leftmost = 0;
21132 lowest = - descent + boff;
21133 highest = ascent + boff;
21134
21135 if (! font_not_found_p
21136 && font_info->default_ascent
21137 && CHAR_TABLE_P (Vuse_default_ascent)
21138 && !NILP (Faref (Vuse_default_ascent,
21139 make_number (it->char_to_display))))
21140 highest = font_info->default_ascent + boff;
21141
21142 /* Draw the first glyph at the normal position. It may be
21143 shifted to right later if some other glyphs are drawn
21144 at the left. */
21145 cmp->offsets[i * 2] = 0;
21146 cmp->offsets[i * 2 + 1] = boff;
21147 cmp->lbearing = lbearing;
21148 cmp->rbearing = rbearing;
21149
21150 /* Set cmp->offsets for the remaining glyphs. */
21151 for (i++; i < glyph_len; i++)
21152 {
21153 int left, right, btm, top;
21154 int ch = COMPOSITION_GLYPH (cmp, i);
21155 int face_id;
21156 struct face *this_face;
21157 int this_boff;
21158
21159 if (ch == '\t')
21160 ch = ' ';
21161 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
21162 this_face = FACE_FROM_ID (it->f, face_id);
21163 font = this_face->font;
21164
21165 if (font == NULL)
21166 pcm = NULL;
21167 else
21168 {
21169 font_info = FONT_INFO_FROM_FACE (it->f, this_face);
21170 this_boff = font_info->baseline_offset;
21171 if (font_info->vertical_centering)
21172 this_boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
21173 get_char_face_and_encoding (it->f, ch, face_id,
21174 &char2b, it->multibyte_p, 0);
21175 pcm = get_per_char_metric (it->f, font, font_info, &char2b,
21176 FONT_TYPE_FOR_MULTIBYTE (font,
21177 ch));
21178 }
21179 if (! pcm)
21180 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
21181 else
21182 {
21183 width = pcm->width;
21184 ascent = pcm->ascent;
21185 descent = pcm->descent;
21186 lbearing = pcm->lbearing;
21187 rbearing = pcm->rbearing;
21188 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
21189 {
21190 /* Relative composition with or without
21191 alternate chars. */
21192 left = (leftmost + rightmost - width) / 2;
21193 btm = - descent + boff;
21194 if (font_info->relative_compose
21195 && (! CHAR_TABLE_P (Vignore_relative_composition)
21196 || NILP (Faref (Vignore_relative_composition,
21197 make_number (ch)))))
21198 {
21199
21200 if (- descent >= font_info->relative_compose)
21201 /* One extra pixel between two glyphs. */
21202 btm = highest + 1;
21203 else if (ascent <= 0)
21204 /* One extra pixel between two glyphs. */
21205 btm = lowest - 1 - ascent - descent;
21206 }
21207 }
21208 else
21209 {
21210 /* A composition rule is specified by an integer
21211 value that encodes global and new reference
21212 points (GREF and NREF). GREF and NREF are
21213 specified by numbers as below:
21214
21215 0---1---2 -- ascent
21216 | |
21217 | |
21218 | |
21219 9--10--11 -- center
21220 | |
21221 ---3---4---5--- baseline
21222 | |
21223 6---7---8 -- descent
21224 */
21225 int rule = COMPOSITION_RULE (cmp, i);
21226 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
21227
21228 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
21229 grefx = gref % 3, nrefx = nref % 3;
21230 grefy = gref / 3, nrefy = nref / 3;
21231 if (xoff)
21232 xoff = font_height * (xoff - 128) / 256;
21233 if (yoff)
21234 yoff = font_height * (yoff - 128) / 256;
21235
21236 left = (leftmost
21237 + grefx * (rightmost - leftmost) / 2
21238 - nrefx * width / 2
21239 + xoff);
21240
21241 btm = ((grefy == 0 ? highest
21242 : grefy == 1 ? 0
21243 : grefy == 2 ? lowest
21244 : (highest + lowest) / 2)
21245 - (nrefy == 0 ? ascent + descent
21246 : nrefy == 1 ? descent - boff
21247 : nrefy == 2 ? 0
21248 : (ascent + descent) / 2)
21249 + yoff);
21250 }
21251
21252 cmp->offsets[i * 2] = left;
21253 cmp->offsets[i * 2 + 1] = btm + descent;
21254
21255 /* Update the bounding box of the overall glyphs. */
21256 if (width > 0)
21257 {
21258 right = left + width;
21259 if (left < leftmost)
21260 leftmost = left;
21261 if (right > rightmost)
21262 rightmost = right;
21263 }
21264 top = btm + descent + ascent;
21265 if (top > highest)
21266 highest = top;
21267 if (btm < lowest)
21268 lowest = btm;
21269
21270 if (cmp->lbearing > left + lbearing)
21271 cmp->lbearing = left + lbearing;
21272 if (cmp->rbearing < left + rbearing)
21273 cmp->rbearing = left + rbearing;
21274 }
21275 }
21276
21277 /* If there are glyphs whose x-offsets are negative,
21278 shift all glyphs to the right and make all x-offsets
21279 non-negative. */
21280 if (leftmost < 0)
21281 {
21282 for (i = 0; i < cmp->glyph_len; i++)
21283 cmp->offsets[i * 2] -= leftmost;
21284 rightmost -= leftmost;
21285 cmp->lbearing -= leftmost;
21286 cmp->rbearing -= leftmost;
21287 }
21288
21289 if (left_padded && cmp->lbearing < 0)
21290 {
21291 for (i = 0; i < cmp->glyph_len; i++)
21292 cmp->offsets[i * 2] -= cmp->lbearing;
21293 rightmost -= cmp->lbearing;
21294 cmp->rbearing -= cmp->lbearing;
21295 cmp->lbearing = 0;
21296 }
21297 if (right_padded && rightmost < cmp->rbearing)
21298 {
21299 rightmost = cmp->rbearing;
21300 }
21301
21302 cmp->pixel_width = rightmost;
21303 cmp->ascent = highest;
21304 cmp->descent = - lowest;
21305 if (cmp->ascent < font_ascent)
21306 cmp->ascent = font_ascent;
21307 if (cmp->descent < font_descent)
21308 cmp->descent = font_descent;
21309 }
21310
21311 if (it->glyph_row
21312 && (cmp->lbearing < 0
21313 || cmp->rbearing > cmp->pixel_width))
21314 it->glyph_row->contains_overlapping_glyphs_p = 1;
21315
21316 it->pixel_width = cmp->pixel_width;
21317 it->ascent = it->phys_ascent = cmp->ascent;
21318 it->descent = it->phys_descent = cmp->descent;
21319
21320 if (face->box != FACE_NO_BOX)
21321 {
21322 int thick = face->box_line_width;
21323
21324 if (thick > 0)
21325 {
21326 it->ascent += thick;
21327 it->descent += thick;
21328 }
21329 else
21330 thick = - thick;
21331
21332 if (it->start_of_box_run_p)
21333 it->pixel_width += thick;
21334 if (it->end_of_box_run_p)
21335 it->pixel_width += thick;
21336 }
21337
21338 /* If face has an overline, add the height of the overline
21339 (1 pixel) and a 1 pixel margin to the character height. */
21340 if (face->overline_p)
21341 it->ascent += overline_margin;
21342
21343 take_vertical_position_into_account (it);
21344
21345 if (it->glyph_row)
21346 append_composite_glyph (it);
21347 }
21348 else if (it->what == IT_IMAGE)
21349 produce_image_glyph (it);
21350 else if (it->what == IT_STRETCH)
21351 produce_stretch_glyph (it);
21352
21353 /* Accumulate dimensions. Note: can't assume that it->descent > 0
21354 because this isn't true for images with `:ascent 100'. */
21355 xassert (it->ascent >= 0 && it->descent >= 0);
21356 if (it->area == TEXT_AREA)
21357 it->current_x += it->pixel_width;
21358
21359 if (extra_line_spacing > 0)
21360 {
21361 it->descent += extra_line_spacing;
21362 if (extra_line_spacing > it->max_extra_line_spacing)
21363 it->max_extra_line_spacing = extra_line_spacing;
21364 }
21365
21366 it->max_ascent = max (it->max_ascent, it->ascent);
21367 it->max_descent = max (it->max_descent, it->descent);
21368 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
21369 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
21370 }
21371
21372 /* EXPORT for RIF:
21373 Output LEN glyphs starting at START at the nominal cursor position.
21374 Advance the nominal cursor over the text. The global variable
21375 updated_window contains the window being updated, updated_row is
21376 the glyph row being updated, and updated_area is the area of that
21377 row being updated. */
21378
21379 void
21380 x_write_glyphs (start, len)
21381 struct glyph *start;
21382 int len;
21383 {
21384 int x, hpos;
21385
21386 xassert (updated_window && updated_row);
21387 BLOCK_INPUT;
21388
21389 /* Write glyphs. */
21390
21391 hpos = start - updated_row->glyphs[updated_area];
21392 x = draw_glyphs (updated_window, output_cursor.x,
21393 updated_row, updated_area,
21394 hpos, hpos + len,
21395 DRAW_NORMAL_TEXT, 0);
21396
21397 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
21398 if (updated_area == TEXT_AREA
21399 && updated_window->phys_cursor_on_p
21400 && updated_window->phys_cursor.vpos == output_cursor.vpos
21401 && updated_window->phys_cursor.hpos >= hpos
21402 && updated_window->phys_cursor.hpos < hpos + len)
21403 updated_window->phys_cursor_on_p = 0;
21404
21405 UNBLOCK_INPUT;
21406
21407 /* Advance the output cursor. */
21408 output_cursor.hpos += len;
21409 output_cursor.x = x;
21410 }
21411
21412
21413 /* EXPORT for RIF:
21414 Insert LEN glyphs from START at the nominal cursor position. */
21415
21416 void
21417 x_insert_glyphs (start, len)
21418 struct glyph *start;
21419 int len;
21420 {
21421 struct frame *f;
21422 struct window *w;
21423 int line_height, shift_by_width, shifted_region_width;
21424 struct glyph_row *row;
21425 struct glyph *glyph;
21426 int frame_x, frame_y;
21427 EMACS_INT hpos;
21428
21429 xassert (updated_window && updated_row);
21430 BLOCK_INPUT;
21431 w = updated_window;
21432 f = XFRAME (WINDOW_FRAME (w));
21433
21434 /* Get the height of the line we are in. */
21435 row = updated_row;
21436 line_height = row->height;
21437
21438 /* Get the width of the glyphs to insert. */
21439 shift_by_width = 0;
21440 for (glyph = start; glyph < start + len; ++glyph)
21441 shift_by_width += glyph->pixel_width;
21442
21443 /* Get the width of the region to shift right. */
21444 shifted_region_width = (window_box_width (w, updated_area)
21445 - output_cursor.x
21446 - shift_by_width);
21447
21448 /* Shift right. */
21449 frame_x = window_box_left (w, updated_area) + output_cursor.x;
21450 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
21451
21452 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
21453 line_height, shift_by_width);
21454
21455 /* Write the glyphs. */
21456 hpos = start - row->glyphs[updated_area];
21457 draw_glyphs (w, output_cursor.x, row, updated_area,
21458 hpos, hpos + len,
21459 DRAW_NORMAL_TEXT, 0);
21460
21461 /* Advance the output cursor. */
21462 output_cursor.hpos += len;
21463 output_cursor.x += shift_by_width;
21464 UNBLOCK_INPUT;
21465 }
21466
21467
21468 /* EXPORT for RIF:
21469 Erase the current text line from the nominal cursor position
21470 (inclusive) to pixel column TO_X (exclusive). The idea is that
21471 everything from TO_X onward is already erased.
21472
21473 TO_X is a pixel position relative to updated_area of
21474 updated_window. TO_X == -1 means clear to the end of this area. */
21475
21476 void
21477 x_clear_end_of_line (to_x)
21478 int to_x;
21479 {
21480 struct frame *f;
21481 struct window *w = updated_window;
21482 int max_x, min_y, max_y;
21483 int from_x, from_y, to_y;
21484
21485 xassert (updated_window && updated_row);
21486 f = XFRAME (w->frame);
21487
21488 if (updated_row->full_width_p)
21489 max_x = WINDOW_TOTAL_WIDTH (w);
21490 else
21491 max_x = window_box_width (w, updated_area);
21492 max_y = window_text_bottom_y (w);
21493
21494 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
21495 of window. For TO_X > 0, truncate to end of drawing area. */
21496 if (to_x == 0)
21497 return;
21498 else if (to_x < 0)
21499 to_x = max_x;
21500 else
21501 to_x = min (to_x, max_x);
21502
21503 to_y = min (max_y, output_cursor.y + updated_row->height);
21504
21505 /* Notice if the cursor will be cleared by this operation. */
21506 if (!updated_row->full_width_p)
21507 notice_overwritten_cursor (w, updated_area,
21508 output_cursor.x, -1,
21509 updated_row->y,
21510 MATRIX_ROW_BOTTOM_Y (updated_row));
21511
21512 from_x = output_cursor.x;
21513
21514 /* Translate to frame coordinates. */
21515 if (updated_row->full_width_p)
21516 {
21517 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
21518 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
21519 }
21520 else
21521 {
21522 int area_left = window_box_left (w, updated_area);
21523 from_x += area_left;
21524 to_x += area_left;
21525 }
21526
21527 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
21528 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
21529 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
21530
21531 /* Prevent inadvertently clearing to end of the X window. */
21532 if (to_x > from_x && to_y > from_y)
21533 {
21534 BLOCK_INPUT;
21535 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
21536 to_x - from_x, to_y - from_y);
21537 UNBLOCK_INPUT;
21538 }
21539 }
21540
21541 #endif /* HAVE_WINDOW_SYSTEM */
21542
21543
21544 \f
21545 /***********************************************************************
21546 Cursor types
21547 ***********************************************************************/
21548
21549 /* Value is the internal representation of the specified cursor type
21550 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
21551 of the bar cursor. */
21552
21553 static enum text_cursor_kinds
21554 get_specified_cursor_type (arg, width)
21555 Lisp_Object arg;
21556 int *width;
21557 {
21558 enum text_cursor_kinds type;
21559
21560 if (NILP (arg))
21561 return NO_CURSOR;
21562
21563 if (EQ (arg, Qbox))
21564 return FILLED_BOX_CURSOR;
21565
21566 if (EQ (arg, Qhollow))
21567 return HOLLOW_BOX_CURSOR;
21568
21569 if (EQ (arg, Qbar))
21570 {
21571 *width = 2;
21572 return BAR_CURSOR;
21573 }
21574
21575 if (CONSP (arg)
21576 && EQ (XCAR (arg), Qbar)
21577 && INTEGERP (XCDR (arg))
21578 && XINT (XCDR (arg)) >= 0)
21579 {
21580 *width = XINT (XCDR (arg));
21581 return BAR_CURSOR;
21582 }
21583
21584 if (EQ (arg, Qhbar))
21585 {
21586 *width = 2;
21587 return HBAR_CURSOR;
21588 }
21589
21590 if (CONSP (arg)
21591 && EQ (XCAR (arg), Qhbar)
21592 && INTEGERP (XCDR (arg))
21593 && XINT (XCDR (arg)) >= 0)
21594 {
21595 *width = XINT (XCDR (arg));
21596 return HBAR_CURSOR;
21597 }
21598
21599 /* Treat anything unknown as "hollow box cursor".
21600 It was bad to signal an error; people have trouble fixing
21601 .Xdefaults with Emacs, when it has something bad in it. */
21602 type = HOLLOW_BOX_CURSOR;
21603
21604 return type;
21605 }
21606
21607 /* Set the default cursor types for specified frame. */
21608 void
21609 set_frame_cursor_types (f, arg)
21610 struct frame *f;
21611 Lisp_Object arg;
21612 {
21613 int width;
21614 Lisp_Object tem;
21615
21616 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
21617 FRAME_CURSOR_WIDTH (f) = width;
21618
21619 /* By default, set up the blink-off state depending on the on-state. */
21620
21621 tem = Fassoc (arg, Vblink_cursor_alist);
21622 if (!NILP (tem))
21623 {
21624 FRAME_BLINK_OFF_CURSOR (f)
21625 = get_specified_cursor_type (XCDR (tem), &width);
21626 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
21627 }
21628 else
21629 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
21630 }
21631
21632
21633 /* Return the cursor we want to be displayed in window W. Return
21634 width of bar/hbar cursor through WIDTH arg. Return with
21635 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
21636 (i.e. if the `system caret' should track this cursor).
21637
21638 In a mini-buffer window, we want the cursor only to appear if we
21639 are reading input from this window. For the selected window, we
21640 want the cursor type given by the frame parameter or buffer local
21641 setting of cursor-type. If explicitly marked off, draw no cursor.
21642 In all other cases, we want a hollow box cursor. */
21643
21644 static enum text_cursor_kinds
21645 get_window_cursor_type (w, glyph, width, active_cursor)
21646 struct window *w;
21647 struct glyph *glyph;
21648 int *width;
21649 int *active_cursor;
21650 {
21651 struct frame *f = XFRAME (w->frame);
21652 struct buffer *b = XBUFFER (w->buffer);
21653 int cursor_type = DEFAULT_CURSOR;
21654 Lisp_Object alt_cursor;
21655 int non_selected = 0;
21656
21657 *active_cursor = 1;
21658
21659 /* Echo area */
21660 if (cursor_in_echo_area
21661 && FRAME_HAS_MINIBUF_P (f)
21662 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
21663 {
21664 if (w == XWINDOW (echo_area_window))
21665 {
21666 if (EQ (b->cursor_type, Qt) || NILP (b->cursor_type))
21667 {
21668 *width = FRAME_CURSOR_WIDTH (f);
21669 return FRAME_DESIRED_CURSOR (f);
21670 }
21671 else
21672 return get_specified_cursor_type (b->cursor_type, width);
21673 }
21674
21675 *active_cursor = 0;
21676 non_selected = 1;
21677 }
21678
21679 /* Detect a nonselected window or nonselected frame. */
21680 else if (w != XWINDOW (f->selected_window)
21681 #ifdef HAVE_WINDOW_SYSTEM
21682 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame
21683 #endif
21684 )
21685 {
21686 *active_cursor = 0;
21687
21688 if (MINI_WINDOW_P (w) && minibuf_level == 0)
21689 return NO_CURSOR;
21690
21691 non_selected = 1;
21692 }
21693
21694 /* Never display a cursor in a window in which cursor-type is nil. */
21695 if (NILP (b->cursor_type))
21696 return NO_CURSOR;
21697
21698 /* Get the normal cursor type for this window. */
21699 if (EQ (b->cursor_type, Qt))
21700 {
21701 cursor_type = FRAME_DESIRED_CURSOR (f);
21702 *width = FRAME_CURSOR_WIDTH (f);
21703 }
21704 else
21705 cursor_type = get_specified_cursor_type (b->cursor_type, width);
21706
21707 /* Use cursor-in-non-selected-windows instead
21708 for non-selected window or frame. */
21709 if (non_selected)
21710 {
21711 alt_cursor = b->cursor_in_non_selected_windows;
21712 if (!EQ (Qt, alt_cursor))
21713 return get_specified_cursor_type (alt_cursor, width);
21714 /* t means modify the normal cursor type. */
21715 if (cursor_type == FILLED_BOX_CURSOR)
21716 cursor_type = HOLLOW_BOX_CURSOR;
21717 else if (cursor_type == BAR_CURSOR && *width > 1)
21718 --*width;
21719 return cursor_type;
21720 }
21721
21722 /* Use normal cursor if not blinked off. */
21723 if (!w->cursor_off_p)
21724 {
21725 #ifdef HAVE_WINDOW_SYSTEM
21726 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
21727 {
21728 if (cursor_type == FILLED_BOX_CURSOR)
21729 {
21730 /* Using a block cursor on large images can be very annoying.
21731 So use a hollow cursor for "large" images.
21732 If image is not transparent (no mask), also use hollow cursor. */
21733 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
21734 if (img != NULL && IMAGEP (img->spec))
21735 {
21736 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
21737 where N = size of default frame font size.
21738 This should cover most of the "tiny" icons people may use. */
21739 if (!img->mask
21740 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
21741 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
21742 cursor_type = HOLLOW_BOX_CURSOR;
21743 }
21744 }
21745 else if (cursor_type != NO_CURSOR)
21746 {
21747 /* Display current only supports BOX and HOLLOW cursors for images.
21748 So for now, unconditionally use a HOLLOW cursor when cursor is
21749 not a solid box cursor. */
21750 cursor_type = HOLLOW_BOX_CURSOR;
21751 }
21752 }
21753 #endif
21754 return cursor_type;
21755 }
21756
21757 /* Cursor is blinked off, so determine how to "toggle" it. */
21758
21759 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
21760 if ((alt_cursor = Fassoc (b->cursor_type, Vblink_cursor_alist), !NILP (alt_cursor)))
21761 return get_specified_cursor_type (XCDR (alt_cursor), width);
21762
21763 /* Then see if frame has specified a specific blink off cursor type. */
21764 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
21765 {
21766 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
21767 return FRAME_BLINK_OFF_CURSOR (f);
21768 }
21769
21770 #if 0
21771 /* Some people liked having a permanently visible blinking cursor,
21772 while others had very strong opinions against it. So it was
21773 decided to remove it. KFS 2003-09-03 */
21774
21775 /* Finally perform built-in cursor blinking:
21776 filled box <-> hollow box
21777 wide [h]bar <-> narrow [h]bar
21778 narrow [h]bar <-> no cursor
21779 other type <-> no cursor */
21780
21781 if (cursor_type == FILLED_BOX_CURSOR)
21782 return HOLLOW_BOX_CURSOR;
21783
21784 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
21785 {
21786 *width = 1;
21787 return cursor_type;
21788 }
21789 #endif
21790
21791 return NO_CURSOR;
21792 }
21793
21794
21795 #ifdef HAVE_WINDOW_SYSTEM
21796
21797 /* Notice when the text cursor of window W has been completely
21798 overwritten by a drawing operation that outputs glyphs in AREA
21799 starting at X0 and ending at X1 in the line starting at Y0 and
21800 ending at Y1. X coordinates are area-relative. X1 < 0 means all
21801 the rest of the line after X0 has been written. Y coordinates
21802 are window-relative. */
21803
21804 static void
21805 notice_overwritten_cursor (w, area, x0, x1, y0, y1)
21806 struct window *w;
21807 enum glyph_row_area area;
21808 int x0, y0, x1, y1;
21809 {
21810 int cx0, cx1, cy0, cy1;
21811 struct glyph_row *row;
21812
21813 if (!w->phys_cursor_on_p)
21814 return;
21815 if (area != TEXT_AREA)
21816 return;
21817
21818 if (w->phys_cursor.vpos < 0
21819 || w->phys_cursor.vpos >= w->current_matrix->nrows
21820 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
21821 !(row->enabled_p && row->displays_text_p)))
21822 return;
21823
21824 if (row->cursor_in_fringe_p)
21825 {
21826 row->cursor_in_fringe_p = 0;
21827 draw_fringe_bitmap (w, row, 0);
21828 w->phys_cursor_on_p = 0;
21829 return;
21830 }
21831
21832 cx0 = w->phys_cursor.x;
21833 cx1 = cx0 + w->phys_cursor_width;
21834 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
21835 return;
21836
21837 /* The cursor image will be completely removed from the
21838 screen if the output area intersects the cursor area in
21839 y-direction. When we draw in [y0 y1[, and some part of
21840 the cursor is at y < y0, that part must have been drawn
21841 before. When scrolling, the cursor is erased before
21842 actually scrolling, so we don't come here. When not
21843 scrolling, the rows above the old cursor row must have
21844 changed, and in this case these rows must have written
21845 over the cursor image.
21846
21847 Likewise if part of the cursor is below y1, with the
21848 exception of the cursor being in the first blank row at
21849 the buffer and window end because update_text_area
21850 doesn't draw that row. (Except when it does, but
21851 that's handled in update_text_area.) */
21852
21853 cy0 = w->phys_cursor.y;
21854 cy1 = cy0 + w->phys_cursor_height;
21855 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
21856 return;
21857
21858 w->phys_cursor_on_p = 0;
21859 }
21860
21861 #endif /* HAVE_WINDOW_SYSTEM */
21862
21863 \f
21864 /************************************************************************
21865 Mouse Face
21866 ************************************************************************/
21867
21868 #ifdef HAVE_WINDOW_SYSTEM
21869
21870 /* EXPORT for RIF:
21871 Fix the display of area AREA of overlapping row ROW in window W
21872 with respect to the overlapping part OVERLAPS. */
21873
21874 void
21875 x_fix_overlapping_area (w, row, area, overlaps)
21876 struct window *w;
21877 struct glyph_row *row;
21878 enum glyph_row_area area;
21879 int overlaps;
21880 {
21881 int i, x;
21882
21883 BLOCK_INPUT;
21884
21885 x = 0;
21886 for (i = 0; i < row->used[area];)
21887 {
21888 if (row->glyphs[area][i].overlaps_vertically_p)
21889 {
21890 int start = i, start_x = x;
21891
21892 do
21893 {
21894 x += row->glyphs[area][i].pixel_width;
21895 ++i;
21896 }
21897 while (i < row->used[area]
21898 && row->glyphs[area][i].overlaps_vertically_p);
21899
21900 draw_glyphs (w, start_x, row, area,
21901 start, i,
21902 DRAW_NORMAL_TEXT, overlaps);
21903 }
21904 else
21905 {
21906 x += row->glyphs[area][i].pixel_width;
21907 ++i;
21908 }
21909 }
21910
21911 UNBLOCK_INPUT;
21912 }
21913
21914
21915 /* EXPORT:
21916 Draw the cursor glyph of window W in glyph row ROW. See the
21917 comment of draw_glyphs for the meaning of HL. */
21918
21919 void
21920 draw_phys_cursor_glyph (w, row, hl)
21921 struct window *w;
21922 struct glyph_row *row;
21923 enum draw_glyphs_face hl;
21924 {
21925 /* If cursor hpos is out of bounds, don't draw garbage. This can
21926 happen in mini-buffer windows when switching between echo area
21927 glyphs and mini-buffer. */
21928 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
21929 {
21930 int on_p = w->phys_cursor_on_p;
21931 int x1;
21932 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
21933 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
21934 hl, 0);
21935 w->phys_cursor_on_p = on_p;
21936
21937 if (hl == DRAW_CURSOR)
21938 w->phys_cursor_width = x1 - w->phys_cursor.x;
21939 /* When we erase the cursor, and ROW is overlapped by other
21940 rows, make sure that these overlapping parts of other rows
21941 are redrawn. */
21942 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
21943 {
21944 w->phys_cursor_width = x1 - w->phys_cursor.x;
21945
21946 if (row > w->current_matrix->rows
21947 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
21948 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
21949 OVERLAPS_ERASED_CURSOR);
21950
21951 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
21952 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
21953 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
21954 OVERLAPS_ERASED_CURSOR);
21955 }
21956 }
21957 }
21958
21959
21960 /* EXPORT:
21961 Erase the image of a cursor of window W from the screen. */
21962
21963 void
21964 erase_phys_cursor (w)
21965 struct window *w;
21966 {
21967 struct frame *f = XFRAME (w->frame);
21968 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
21969 int hpos = w->phys_cursor.hpos;
21970 int vpos = w->phys_cursor.vpos;
21971 int mouse_face_here_p = 0;
21972 struct glyph_matrix *active_glyphs = w->current_matrix;
21973 struct glyph_row *cursor_row;
21974 struct glyph *cursor_glyph;
21975 enum draw_glyphs_face hl;
21976
21977 /* No cursor displayed or row invalidated => nothing to do on the
21978 screen. */
21979 if (w->phys_cursor_type == NO_CURSOR)
21980 goto mark_cursor_off;
21981
21982 /* VPOS >= active_glyphs->nrows means that window has been resized.
21983 Don't bother to erase the cursor. */
21984 if (vpos >= active_glyphs->nrows)
21985 goto mark_cursor_off;
21986
21987 /* If row containing cursor is marked invalid, there is nothing we
21988 can do. */
21989 cursor_row = MATRIX_ROW (active_glyphs, vpos);
21990 if (!cursor_row->enabled_p)
21991 goto mark_cursor_off;
21992
21993 /* If line spacing is > 0, old cursor may only be partially visible in
21994 window after split-window. So adjust visible height. */
21995 cursor_row->visible_height = min (cursor_row->visible_height,
21996 window_text_bottom_y (w) - cursor_row->y);
21997
21998 /* If row is completely invisible, don't attempt to delete a cursor which
21999 isn't there. This can happen if cursor is at top of a window, and
22000 we switch to a buffer with a header line in that window. */
22001 if (cursor_row->visible_height <= 0)
22002 goto mark_cursor_off;
22003
22004 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
22005 if (cursor_row->cursor_in_fringe_p)
22006 {
22007 cursor_row->cursor_in_fringe_p = 0;
22008 draw_fringe_bitmap (w, cursor_row, 0);
22009 goto mark_cursor_off;
22010 }
22011
22012 /* This can happen when the new row is shorter than the old one.
22013 In this case, either draw_glyphs or clear_end_of_line
22014 should have cleared the cursor. Note that we wouldn't be
22015 able to erase the cursor in this case because we don't have a
22016 cursor glyph at hand. */
22017 if (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])
22018 goto mark_cursor_off;
22019
22020 /* If the cursor is in the mouse face area, redisplay that when
22021 we clear the cursor. */
22022 if (! NILP (dpyinfo->mouse_face_window)
22023 && w == XWINDOW (dpyinfo->mouse_face_window)
22024 && (vpos > dpyinfo->mouse_face_beg_row
22025 || (vpos == dpyinfo->mouse_face_beg_row
22026 && hpos >= dpyinfo->mouse_face_beg_col))
22027 && (vpos < dpyinfo->mouse_face_end_row
22028 || (vpos == dpyinfo->mouse_face_end_row
22029 && hpos < dpyinfo->mouse_face_end_col))
22030 /* Don't redraw the cursor's spot in mouse face if it is at the
22031 end of a line (on a newline). The cursor appears there, but
22032 mouse highlighting does not. */
22033 && cursor_row->used[TEXT_AREA] > hpos)
22034 mouse_face_here_p = 1;
22035
22036 /* Maybe clear the display under the cursor. */
22037 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
22038 {
22039 int x, y, left_x;
22040 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
22041 int width;
22042
22043 cursor_glyph = get_phys_cursor_glyph (w);
22044 if (cursor_glyph == NULL)
22045 goto mark_cursor_off;
22046
22047 width = cursor_glyph->pixel_width;
22048 left_x = window_box_left_offset (w, TEXT_AREA);
22049 x = w->phys_cursor.x;
22050 if (x < left_x)
22051 width -= left_x - x;
22052 width = min (width, window_box_width (w, TEXT_AREA) - x);
22053 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
22054 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, max (x, left_x));
22055
22056 if (width > 0)
22057 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
22058 }
22059
22060 /* Erase the cursor by redrawing the character underneath it. */
22061 if (mouse_face_here_p)
22062 hl = DRAW_MOUSE_FACE;
22063 else
22064 hl = DRAW_NORMAL_TEXT;
22065 draw_phys_cursor_glyph (w, cursor_row, hl);
22066
22067 mark_cursor_off:
22068 w->phys_cursor_on_p = 0;
22069 w->phys_cursor_type = NO_CURSOR;
22070 }
22071
22072
22073 /* EXPORT:
22074 Display or clear cursor of window W. If ON is zero, clear the
22075 cursor. If it is non-zero, display the cursor. If ON is nonzero,
22076 where to put the cursor is specified by HPOS, VPOS, X and Y. */
22077
22078 void
22079 display_and_set_cursor (w, on, hpos, vpos, x, y)
22080 struct window *w;
22081 int on, hpos, vpos, x, y;
22082 {
22083 struct frame *f = XFRAME (w->frame);
22084 int new_cursor_type;
22085 int new_cursor_width;
22086 int active_cursor;
22087 struct glyph_row *glyph_row;
22088 struct glyph *glyph;
22089
22090 /* This is pointless on invisible frames, and dangerous on garbaged
22091 windows and frames; in the latter case, the frame or window may
22092 be in the midst of changing its size, and x and y may be off the
22093 window. */
22094 if (! FRAME_VISIBLE_P (f)
22095 || FRAME_GARBAGED_P (f)
22096 || vpos >= w->current_matrix->nrows
22097 || hpos >= w->current_matrix->matrix_w)
22098 return;
22099
22100 /* If cursor is off and we want it off, return quickly. */
22101 if (!on && !w->phys_cursor_on_p)
22102 return;
22103
22104 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
22105 /* If cursor row is not enabled, we don't really know where to
22106 display the cursor. */
22107 if (!glyph_row->enabled_p)
22108 {
22109 w->phys_cursor_on_p = 0;
22110 return;
22111 }
22112
22113 glyph = NULL;
22114 if (!glyph_row->exact_window_width_line_p
22115 || hpos < glyph_row->used[TEXT_AREA])
22116 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
22117
22118 xassert (interrupt_input_blocked);
22119
22120 /* Set new_cursor_type to the cursor we want to be displayed. */
22121 new_cursor_type = get_window_cursor_type (w, glyph,
22122 &new_cursor_width, &active_cursor);
22123
22124 /* If cursor is currently being shown and we don't want it to be or
22125 it is in the wrong place, or the cursor type is not what we want,
22126 erase it. */
22127 if (w->phys_cursor_on_p
22128 && (!on
22129 || w->phys_cursor.x != x
22130 || w->phys_cursor.y != y
22131 || new_cursor_type != w->phys_cursor_type
22132 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
22133 && new_cursor_width != w->phys_cursor_width)))
22134 erase_phys_cursor (w);
22135
22136 /* Don't check phys_cursor_on_p here because that flag is only set
22137 to zero in some cases where we know that the cursor has been
22138 completely erased, to avoid the extra work of erasing the cursor
22139 twice. In other words, phys_cursor_on_p can be 1 and the cursor
22140 still not be visible, or it has only been partly erased. */
22141 if (on)
22142 {
22143 w->phys_cursor_ascent = glyph_row->ascent;
22144 w->phys_cursor_height = glyph_row->height;
22145
22146 /* Set phys_cursor_.* before x_draw_.* is called because some
22147 of them may need the information. */
22148 w->phys_cursor.x = x;
22149 w->phys_cursor.y = glyph_row->y;
22150 w->phys_cursor.hpos = hpos;
22151 w->phys_cursor.vpos = vpos;
22152 }
22153
22154 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
22155 new_cursor_type, new_cursor_width,
22156 on, active_cursor);
22157 }
22158
22159
22160 /* Switch the display of W's cursor on or off, according to the value
22161 of ON. */
22162
22163 static void
22164 update_window_cursor (w, on)
22165 struct window *w;
22166 int on;
22167 {
22168 /* Don't update cursor in windows whose frame is in the process
22169 of being deleted. */
22170 if (w->current_matrix)
22171 {
22172 BLOCK_INPUT;
22173 display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
22174 w->phys_cursor.x, w->phys_cursor.y);
22175 UNBLOCK_INPUT;
22176 }
22177 }
22178
22179
22180 /* Call update_window_cursor with parameter ON_P on all leaf windows
22181 in the window tree rooted at W. */
22182
22183 static void
22184 update_cursor_in_window_tree (w, on_p)
22185 struct window *w;
22186 int on_p;
22187 {
22188 while (w)
22189 {
22190 if (!NILP (w->hchild))
22191 update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
22192 else if (!NILP (w->vchild))
22193 update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
22194 else
22195 update_window_cursor (w, on_p);
22196
22197 w = NILP (w->next) ? 0 : XWINDOW (w->next);
22198 }
22199 }
22200
22201
22202 /* EXPORT:
22203 Display the cursor on window W, or clear it, according to ON_P.
22204 Don't change the cursor's position. */
22205
22206 void
22207 x_update_cursor (f, on_p)
22208 struct frame *f;
22209 int on_p;
22210 {
22211 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
22212 }
22213
22214
22215 /* EXPORT:
22216 Clear the cursor of window W to background color, and mark the
22217 cursor as not shown. This is used when the text where the cursor
22218 is is about to be rewritten. */
22219
22220 void
22221 x_clear_cursor (w)
22222 struct window *w;
22223 {
22224 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
22225 update_window_cursor (w, 0);
22226 }
22227
22228
22229 /* EXPORT:
22230 Display the active region described by mouse_face_* according to DRAW. */
22231
22232 void
22233 show_mouse_face (dpyinfo, draw)
22234 Display_Info *dpyinfo;
22235 enum draw_glyphs_face draw;
22236 {
22237 struct window *w = XWINDOW (dpyinfo->mouse_face_window);
22238 struct frame *f = XFRAME (WINDOW_FRAME (w));
22239
22240 if (/* If window is in the process of being destroyed, don't bother
22241 to do anything. */
22242 w->current_matrix != NULL
22243 /* Don't update mouse highlight if hidden */
22244 && (draw != DRAW_MOUSE_FACE || !dpyinfo->mouse_face_hidden)
22245 /* Recognize when we are called to operate on rows that don't exist
22246 anymore. This can happen when a window is split. */
22247 && dpyinfo->mouse_face_end_row < w->current_matrix->nrows)
22248 {
22249 int phys_cursor_on_p = w->phys_cursor_on_p;
22250 struct glyph_row *row, *first, *last;
22251
22252 first = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_beg_row);
22253 last = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_end_row);
22254
22255 for (row = first; row <= last && row->enabled_p; ++row)
22256 {
22257 int start_hpos, end_hpos, start_x;
22258
22259 /* For all but the first row, the highlight starts at column 0. */
22260 if (row == first)
22261 {
22262 start_hpos = dpyinfo->mouse_face_beg_col;
22263 start_x = dpyinfo->mouse_face_beg_x;
22264 }
22265 else
22266 {
22267 start_hpos = 0;
22268 start_x = 0;
22269 }
22270
22271 if (row == last)
22272 end_hpos = dpyinfo->mouse_face_end_col;
22273 else
22274 {
22275 end_hpos = row->used[TEXT_AREA];
22276 if (draw == DRAW_NORMAL_TEXT)
22277 row->fill_line_p = 1; /* Clear to end of line */
22278 }
22279
22280 if (end_hpos > start_hpos)
22281 {
22282 draw_glyphs (w, start_x, row, TEXT_AREA,
22283 start_hpos, end_hpos,
22284 draw, 0);
22285
22286 row->mouse_face_p
22287 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
22288 }
22289 }
22290
22291 /* When we've written over the cursor, arrange for it to
22292 be displayed again. */
22293 if (phys_cursor_on_p && !w->phys_cursor_on_p)
22294 {
22295 BLOCK_INPUT;
22296 display_and_set_cursor (w, 1,
22297 w->phys_cursor.hpos, w->phys_cursor.vpos,
22298 w->phys_cursor.x, w->phys_cursor.y);
22299 UNBLOCK_INPUT;
22300 }
22301 }
22302
22303 /* Change the mouse cursor. */
22304 if (draw == DRAW_NORMAL_TEXT && !EQ (dpyinfo->mouse_face_window, f->tool_bar_window))
22305 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
22306 else if (draw == DRAW_MOUSE_FACE)
22307 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
22308 else
22309 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
22310 }
22311
22312 /* EXPORT:
22313 Clear out the mouse-highlighted active region.
22314 Redraw it un-highlighted first. Value is non-zero if mouse
22315 face was actually drawn unhighlighted. */
22316
22317 int
22318 clear_mouse_face (dpyinfo)
22319 Display_Info *dpyinfo;
22320 {
22321 int cleared = 0;
22322
22323 if (!dpyinfo->mouse_face_hidden && !NILP (dpyinfo->mouse_face_window))
22324 {
22325 show_mouse_face (dpyinfo, DRAW_NORMAL_TEXT);
22326 cleared = 1;
22327 }
22328
22329 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
22330 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
22331 dpyinfo->mouse_face_window = Qnil;
22332 dpyinfo->mouse_face_overlay = Qnil;
22333 return cleared;
22334 }
22335
22336
22337 /* EXPORT:
22338 Non-zero if physical cursor of window W is within mouse face. */
22339
22340 int
22341 cursor_in_mouse_face_p (w)
22342 struct window *w;
22343 {
22344 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
22345 int in_mouse_face = 0;
22346
22347 if (WINDOWP (dpyinfo->mouse_face_window)
22348 && XWINDOW (dpyinfo->mouse_face_window) == w)
22349 {
22350 int hpos = w->phys_cursor.hpos;
22351 int vpos = w->phys_cursor.vpos;
22352
22353 if (vpos >= dpyinfo->mouse_face_beg_row
22354 && vpos <= dpyinfo->mouse_face_end_row
22355 && (vpos > dpyinfo->mouse_face_beg_row
22356 || hpos >= dpyinfo->mouse_face_beg_col)
22357 && (vpos < dpyinfo->mouse_face_end_row
22358 || hpos < dpyinfo->mouse_face_end_col
22359 || dpyinfo->mouse_face_past_end))
22360 in_mouse_face = 1;
22361 }
22362
22363 return in_mouse_face;
22364 }
22365
22366
22367
22368 \f
22369 /* Find the glyph matrix position of buffer position CHARPOS in window
22370 *W. HPOS, *VPOS, *X, and *Y are set to the positions found. W's
22371 current glyphs must be up to date. If CHARPOS is above window
22372 start return (0, 0, 0, 0). If CHARPOS is after end of W, return end
22373 of last line in W. In the row containing CHARPOS, stop before glyphs
22374 having STOP as object. */
22375
22376 #if 1 /* This is a version of fast_find_position that's more correct
22377 in the presence of hscrolling, for example. I didn't install
22378 it right away because the problem fixed is minor, it failed
22379 in 20.x as well, and I think it's too risky to install
22380 so near the release of 21.1. 2001-09-25 gerd. */
22381
22382 static int
22383 fast_find_position (w, charpos, hpos, vpos, x, y, stop)
22384 struct window *w;
22385 EMACS_INT charpos;
22386 int *hpos, *vpos, *x, *y;
22387 Lisp_Object stop;
22388 {
22389 struct glyph_row *row, *first;
22390 struct glyph *glyph, *end;
22391 int past_end = 0;
22392
22393 first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
22394 if (charpos < MATRIX_ROW_START_CHARPOS (first))
22395 {
22396 *x = first->x;
22397 *y = first->y;
22398 *hpos = 0;
22399 *vpos = MATRIX_ROW_VPOS (first, w->current_matrix);
22400 return 1;
22401 }
22402
22403 row = row_containing_pos (w, charpos, first, NULL, 0);
22404 if (row == NULL)
22405 {
22406 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
22407 past_end = 1;
22408 }
22409
22410 /* If whole rows or last part of a row came from a display overlay,
22411 row_containing_pos will skip over such rows because their end pos
22412 equals the start pos of the overlay or interval.
22413
22414 Move back if we have a STOP object and previous row's
22415 end glyph came from STOP. */
22416 if (!NILP (stop))
22417 {
22418 struct glyph_row *prev;
22419 while ((prev = row - 1, prev >= first)
22420 && MATRIX_ROW_END_CHARPOS (prev) == charpos
22421 && prev->used[TEXT_AREA] > 0)
22422 {
22423 struct glyph *beg = prev->glyphs[TEXT_AREA];
22424 glyph = beg + prev->used[TEXT_AREA];
22425 while (--glyph >= beg
22426 && INTEGERP (glyph->object));
22427 if (glyph < beg
22428 || !EQ (stop, glyph->object))
22429 break;
22430 row = prev;
22431 }
22432 }
22433
22434 *x = row->x;
22435 *y = row->y;
22436 *vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
22437
22438 glyph = row->glyphs[TEXT_AREA];
22439 end = glyph + row->used[TEXT_AREA];
22440
22441 /* Skip over glyphs not having an object at the start of the row.
22442 These are special glyphs like truncation marks on terminal
22443 frames. */
22444 if (row->displays_text_p)
22445 while (glyph < end
22446 && INTEGERP (glyph->object)
22447 && !EQ (stop, glyph->object)
22448 && glyph->charpos < 0)
22449 {
22450 *x += glyph->pixel_width;
22451 ++glyph;
22452 }
22453
22454 while (glyph < end
22455 && !INTEGERP (glyph->object)
22456 && !EQ (stop, glyph->object)
22457 && (!BUFFERP (glyph->object)
22458 || glyph->charpos < charpos))
22459 {
22460 *x += glyph->pixel_width;
22461 ++glyph;
22462 }
22463
22464 *hpos = glyph - row->glyphs[TEXT_AREA];
22465 return !past_end;
22466 }
22467
22468 #else /* not 1 */
22469
22470 static int
22471 fast_find_position (w, pos, hpos, vpos, x, y, stop)
22472 struct window *w;
22473 EMACS_INT pos;
22474 int *hpos, *vpos, *x, *y;
22475 Lisp_Object stop;
22476 {
22477 int i;
22478 int lastcol;
22479 int maybe_next_line_p = 0;
22480 int line_start_position;
22481 int yb = window_text_bottom_y (w);
22482 struct glyph_row *row, *best_row;
22483 int row_vpos, best_row_vpos;
22484 int current_x;
22485
22486 row = best_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
22487 row_vpos = best_row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
22488
22489 while (row->y < yb)
22490 {
22491 if (row->used[TEXT_AREA])
22492 line_start_position = row->glyphs[TEXT_AREA]->charpos;
22493 else
22494 line_start_position = 0;
22495
22496 if (line_start_position > pos)
22497 break;
22498 /* If the position sought is the end of the buffer,
22499 don't include the blank lines at the bottom of the window. */
22500 else if (line_start_position == pos
22501 && pos == BUF_ZV (XBUFFER (w->buffer)))
22502 {
22503 maybe_next_line_p = 1;
22504 break;
22505 }
22506 else if (line_start_position > 0)
22507 {
22508 best_row = row;
22509 best_row_vpos = row_vpos;
22510 }
22511
22512 if (row->y + row->height >= yb)
22513 break;
22514
22515 ++row;
22516 ++row_vpos;
22517 }
22518
22519 /* Find the right column within BEST_ROW. */
22520 lastcol = 0;
22521 current_x = best_row->x;
22522 for (i = 0; i < best_row->used[TEXT_AREA]; i++)
22523 {
22524 struct glyph *glyph = best_row->glyphs[TEXT_AREA] + i;
22525 int charpos = glyph->charpos;
22526
22527 if (BUFFERP (glyph->object))
22528 {
22529 if (charpos == pos)
22530 {
22531 *hpos = i;
22532 *vpos = best_row_vpos;
22533 *x = current_x;
22534 *y = best_row->y;
22535 return 1;
22536 }
22537 else if (charpos > pos)
22538 break;
22539 }
22540 else if (EQ (glyph->object, stop))
22541 break;
22542
22543 if (charpos > 0)
22544 lastcol = i;
22545 current_x += glyph->pixel_width;
22546 }
22547
22548 /* If we're looking for the end of the buffer,
22549 and we didn't find it in the line we scanned,
22550 use the start of the following line. */
22551 if (maybe_next_line_p)
22552 {
22553 ++best_row;
22554 ++best_row_vpos;
22555 lastcol = 0;
22556 current_x = best_row->x;
22557 }
22558
22559 *vpos = best_row_vpos;
22560 *hpos = lastcol + 1;
22561 *x = current_x;
22562 *y = best_row->y;
22563 return 0;
22564 }
22565
22566 #endif /* not 1 */
22567
22568
22569 /* Find the position of the glyph for position POS in OBJECT in
22570 window W's current matrix, and return in *X, *Y the pixel
22571 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
22572
22573 RIGHT_P non-zero means return the position of the right edge of the
22574 glyph, RIGHT_P zero means return the left edge position.
22575
22576 If no glyph for POS exists in the matrix, return the position of
22577 the glyph with the next smaller position that is in the matrix, if
22578 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
22579 exists in the matrix, return the position of the glyph with the
22580 next larger position in OBJECT.
22581
22582 Value is non-zero if a glyph was found. */
22583
22584 static int
22585 fast_find_string_pos (w, pos, object, hpos, vpos, x, y, right_p)
22586 struct window *w;
22587 EMACS_INT pos;
22588 Lisp_Object object;
22589 int *hpos, *vpos, *x, *y;
22590 int right_p;
22591 {
22592 int yb = window_text_bottom_y (w);
22593 struct glyph_row *r;
22594 struct glyph *best_glyph = NULL;
22595 struct glyph_row *best_row = NULL;
22596 int best_x = 0;
22597
22598 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
22599 r->enabled_p && r->y < yb;
22600 ++r)
22601 {
22602 struct glyph *g = r->glyphs[TEXT_AREA];
22603 struct glyph *e = g + r->used[TEXT_AREA];
22604 int gx;
22605
22606 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
22607 if (EQ (g->object, object))
22608 {
22609 if (g->charpos == pos)
22610 {
22611 best_glyph = g;
22612 best_x = gx;
22613 best_row = r;
22614 goto found;
22615 }
22616 else if (best_glyph == NULL
22617 || ((eabs (g->charpos - pos)
22618 < eabs (best_glyph->charpos - pos))
22619 && (right_p
22620 ? g->charpos < pos
22621 : g->charpos > pos)))
22622 {
22623 best_glyph = g;
22624 best_x = gx;
22625 best_row = r;
22626 }
22627 }
22628 }
22629
22630 found:
22631
22632 if (best_glyph)
22633 {
22634 *x = best_x;
22635 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
22636
22637 if (right_p)
22638 {
22639 *x += best_glyph->pixel_width;
22640 ++*hpos;
22641 }
22642
22643 *y = best_row->y;
22644 *vpos = best_row - w->current_matrix->rows;
22645 }
22646
22647 return best_glyph != NULL;
22648 }
22649
22650
22651 /* See if position X, Y is within a hot-spot of an image. */
22652
22653 static int
22654 on_hot_spot_p (hot_spot, x, y)
22655 Lisp_Object hot_spot;
22656 int x, y;
22657 {
22658 if (!CONSP (hot_spot))
22659 return 0;
22660
22661 if (EQ (XCAR (hot_spot), Qrect))
22662 {
22663 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
22664 Lisp_Object rect = XCDR (hot_spot);
22665 Lisp_Object tem;
22666 if (!CONSP (rect))
22667 return 0;
22668 if (!CONSP (XCAR (rect)))
22669 return 0;
22670 if (!CONSP (XCDR (rect)))
22671 return 0;
22672 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
22673 return 0;
22674 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
22675 return 0;
22676 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
22677 return 0;
22678 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
22679 return 0;
22680 return 1;
22681 }
22682 else if (EQ (XCAR (hot_spot), Qcircle))
22683 {
22684 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
22685 Lisp_Object circ = XCDR (hot_spot);
22686 Lisp_Object lr, lx0, ly0;
22687 if (CONSP (circ)
22688 && CONSP (XCAR (circ))
22689 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
22690 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
22691 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
22692 {
22693 double r = XFLOATINT (lr);
22694 double dx = XINT (lx0) - x;
22695 double dy = XINT (ly0) - y;
22696 return (dx * dx + dy * dy <= r * r);
22697 }
22698 }
22699 else if (EQ (XCAR (hot_spot), Qpoly))
22700 {
22701 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
22702 if (VECTORP (XCDR (hot_spot)))
22703 {
22704 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
22705 Lisp_Object *poly = v->contents;
22706 int n = v->size;
22707 int i;
22708 int inside = 0;
22709 Lisp_Object lx, ly;
22710 int x0, y0;
22711
22712 /* Need an even number of coordinates, and at least 3 edges. */
22713 if (n < 6 || n & 1)
22714 return 0;
22715
22716 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
22717 If count is odd, we are inside polygon. Pixels on edges
22718 may or may not be included depending on actual geometry of the
22719 polygon. */
22720 if ((lx = poly[n-2], !INTEGERP (lx))
22721 || (ly = poly[n-1], !INTEGERP (lx)))
22722 return 0;
22723 x0 = XINT (lx), y0 = XINT (ly);
22724 for (i = 0; i < n; i += 2)
22725 {
22726 int x1 = x0, y1 = y0;
22727 if ((lx = poly[i], !INTEGERP (lx))
22728 || (ly = poly[i+1], !INTEGERP (ly)))
22729 return 0;
22730 x0 = XINT (lx), y0 = XINT (ly);
22731
22732 /* Does this segment cross the X line? */
22733 if (x0 >= x)
22734 {
22735 if (x1 >= x)
22736 continue;
22737 }
22738 else if (x1 < x)
22739 continue;
22740 if (y > y0 && y > y1)
22741 continue;
22742 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
22743 inside = !inside;
22744 }
22745 return inside;
22746 }
22747 }
22748 return 0;
22749 }
22750
22751 Lisp_Object
22752 find_hot_spot (map, x, y)
22753 Lisp_Object map;
22754 int x, y;
22755 {
22756 while (CONSP (map))
22757 {
22758 if (CONSP (XCAR (map))
22759 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
22760 return XCAR (map);
22761 map = XCDR (map);
22762 }
22763
22764 return Qnil;
22765 }
22766
22767 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
22768 3, 3, 0,
22769 doc: /* Lookup in image map MAP coordinates X and Y.
22770 An image map is an alist where each element has the format (AREA ID PLIST).
22771 An AREA is specified as either a rectangle, a circle, or a polygon:
22772 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
22773 pixel coordinates of the upper left and bottom right corners.
22774 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
22775 and the radius of the circle; r may be a float or integer.
22776 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
22777 vector describes one corner in the polygon.
22778 Returns the alist element for the first matching AREA in MAP. */)
22779 (map, x, y)
22780 Lisp_Object map;
22781 Lisp_Object x, y;
22782 {
22783 if (NILP (map))
22784 return Qnil;
22785
22786 CHECK_NUMBER (x);
22787 CHECK_NUMBER (y);
22788
22789 return find_hot_spot (map, XINT (x), XINT (y));
22790 }
22791
22792
22793 /* Display frame CURSOR, optionally using shape defined by POINTER. */
22794 static void
22795 define_frame_cursor1 (f, cursor, pointer)
22796 struct frame *f;
22797 Cursor cursor;
22798 Lisp_Object pointer;
22799 {
22800 /* Do not change cursor shape while dragging mouse. */
22801 if (!NILP (do_mouse_tracking))
22802 return;
22803
22804 if (!NILP (pointer))
22805 {
22806 if (EQ (pointer, Qarrow))
22807 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
22808 else if (EQ (pointer, Qhand))
22809 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
22810 else if (EQ (pointer, Qtext))
22811 cursor = FRAME_X_OUTPUT (f)->text_cursor;
22812 else if (EQ (pointer, intern ("hdrag")))
22813 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
22814 #ifdef HAVE_X_WINDOWS
22815 else if (EQ (pointer, intern ("vdrag")))
22816 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
22817 #endif
22818 else if (EQ (pointer, intern ("hourglass")))
22819 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
22820 else if (EQ (pointer, Qmodeline))
22821 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
22822 else
22823 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
22824 }
22825
22826 if (cursor != No_Cursor)
22827 FRAME_RIF (f)->define_frame_cursor (f, cursor);
22828 }
22829
22830 /* Take proper action when mouse has moved to the mode or header line
22831 or marginal area AREA of window W, x-position X and y-position Y.
22832 X is relative to the start of the text display area of W, so the
22833 width of bitmap areas and scroll bars must be subtracted to get a
22834 position relative to the start of the mode line. */
22835
22836 static void
22837 note_mode_line_or_margin_highlight (window, x, y, area)
22838 Lisp_Object window;
22839 int x, y;
22840 enum window_part area;
22841 {
22842 struct window *w = XWINDOW (window);
22843 struct frame *f = XFRAME (w->frame);
22844 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
22845 Cursor cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
22846 Lisp_Object pointer = Qnil;
22847 int charpos, dx, dy, width, height;
22848 Lisp_Object string, object = Qnil;
22849 Lisp_Object pos, help;
22850
22851 Lisp_Object mouse_face;
22852 int original_x_pixel = x;
22853 struct glyph * glyph = NULL, * row_start_glyph = NULL;
22854 struct glyph_row *row;
22855
22856 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
22857 {
22858 int x0;
22859 struct glyph *end;
22860
22861 string = mode_line_string (w, area, &x, &y, &charpos,
22862 &object, &dx, &dy, &width, &height);
22863
22864 row = (area == ON_MODE_LINE
22865 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
22866 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
22867
22868 /* Find glyph */
22869 if (row->mode_line_p && row->enabled_p)
22870 {
22871 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
22872 end = glyph + row->used[TEXT_AREA];
22873
22874 for (x0 = original_x_pixel;
22875 glyph < end && x0 >= glyph->pixel_width;
22876 ++glyph)
22877 x0 -= glyph->pixel_width;
22878
22879 if (glyph >= end)
22880 glyph = NULL;
22881 }
22882 }
22883 else
22884 {
22885 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
22886 string = marginal_area_string (w, area, &x, &y, &charpos,
22887 &object, &dx, &dy, &width, &height);
22888 }
22889
22890 help = Qnil;
22891
22892 if (IMAGEP (object))
22893 {
22894 Lisp_Object image_map, hotspot;
22895 if ((image_map = Fplist_get (XCDR (object), QCmap),
22896 !NILP (image_map))
22897 && (hotspot = find_hot_spot (image_map, dx, dy),
22898 CONSP (hotspot))
22899 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
22900 {
22901 Lisp_Object area_id, plist;
22902
22903 area_id = XCAR (hotspot);
22904 /* Could check AREA_ID to see if we enter/leave this hot-spot.
22905 If so, we could look for mouse-enter, mouse-leave
22906 properties in PLIST (and do something...). */
22907 hotspot = XCDR (hotspot);
22908 if (CONSP (hotspot)
22909 && (plist = XCAR (hotspot), CONSP (plist)))
22910 {
22911 pointer = Fplist_get (plist, Qpointer);
22912 if (NILP (pointer))
22913 pointer = Qhand;
22914 help = Fplist_get (plist, Qhelp_echo);
22915 if (!NILP (help))
22916 {
22917 help_echo_string = help;
22918 /* Is this correct? ++kfs */
22919 XSETWINDOW (help_echo_window, w);
22920 help_echo_object = w->buffer;
22921 help_echo_pos = charpos;
22922 }
22923 }
22924 }
22925 if (NILP (pointer))
22926 pointer = Fplist_get (XCDR (object), QCpointer);
22927 }
22928
22929 if (STRINGP (string))
22930 {
22931 pos = make_number (charpos);
22932 /* If we're on a string with `help-echo' text property, arrange
22933 for the help to be displayed. This is done by setting the
22934 global variable help_echo_string to the help string. */
22935 if (NILP (help))
22936 {
22937 help = Fget_text_property (pos, Qhelp_echo, string);
22938 if (!NILP (help))
22939 {
22940 help_echo_string = help;
22941 XSETWINDOW (help_echo_window, w);
22942 help_echo_object = string;
22943 help_echo_pos = charpos;
22944 }
22945 }
22946
22947 if (NILP (pointer))
22948 pointer = Fget_text_property (pos, Qpointer, string);
22949
22950 /* Change the mouse pointer according to what is under X/Y. */
22951 if (NILP (pointer) && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
22952 {
22953 Lisp_Object map;
22954 map = Fget_text_property (pos, Qlocal_map, string);
22955 if (!KEYMAPP (map))
22956 map = Fget_text_property (pos, Qkeymap, string);
22957 if (!KEYMAPP (map))
22958 cursor = dpyinfo->vertical_scroll_bar_cursor;
22959 }
22960
22961 /* Change the mouse face according to what is under X/Y. */
22962 mouse_face = Fget_text_property (pos, Qmouse_face, string);
22963 if (!NILP (mouse_face)
22964 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
22965 && glyph)
22966 {
22967 Lisp_Object b, e;
22968
22969 struct glyph * tmp_glyph;
22970
22971 int gpos;
22972 int gseq_length;
22973 int total_pixel_width;
22974 int ignore;
22975
22976 int vpos, hpos;
22977
22978 b = Fprevious_single_property_change (make_number (charpos + 1),
22979 Qmouse_face, string, Qnil);
22980 if (NILP (b))
22981 b = make_number (0);
22982
22983 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
22984 if (NILP (e))
22985 e = make_number (SCHARS (string));
22986
22987 /* Calculate the position(glyph position: GPOS) of GLYPH in
22988 displayed string. GPOS is different from CHARPOS.
22989
22990 CHARPOS is the position of glyph in internal string
22991 object. A mode line string format has structures which
22992 is converted to a flatten by emacs lisp interpreter.
22993 The internal string is an element of the structures.
22994 The displayed string is the flatten string. */
22995 gpos = 0;
22996 if (glyph > row_start_glyph)
22997 {
22998 tmp_glyph = glyph - 1;
22999 while (tmp_glyph >= row_start_glyph
23000 && tmp_glyph->charpos >= XINT (b)
23001 && EQ (tmp_glyph->object, glyph->object))
23002 {
23003 tmp_glyph--;
23004 gpos++;
23005 }
23006 }
23007
23008 /* Calculate the lenght(glyph sequence length: GSEQ_LENGTH) of
23009 displayed string holding GLYPH.
23010
23011 GSEQ_LENGTH is different from SCHARS (STRING).
23012 SCHARS (STRING) returns the length of the internal string. */
23013 for (tmp_glyph = glyph, gseq_length = gpos;
23014 tmp_glyph->charpos < XINT (e);
23015 tmp_glyph++, gseq_length++)
23016 {
23017 if (!EQ (tmp_glyph->object, glyph->object))
23018 break;
23019 }
23020
23021 total_pixel_width = 0;
23022 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
23023 total_pixel_width += tmp_glyph->pixel_width;
23024
23025 /* Pre calculation of re-rendering position */
23026 vpos = (x - gpos);
23027 hpos = (area == ON_MODE_LINE
23028 ? (w->current_matrix)->nrows - 1
23029 : 0);
23030
23031 /* If the re-rendering position is included in the last
23032 re-rendering area, we should do nothing. */
23033 if ( EQ (window, dpyinfo->mouse_face_window)
23034 && dpyinfo->mouse_face_beg_col <= vpos
23035 && vpos < dpyinfo->mouse_face_end_col
23036 && dpyinfo->mouse_face_beg_row == hpos )
23037 return;
23038
23039 if (clear_mouse_face (dpyinfo))
23040 cursor = No_Cursor;
23041
23042 dpyinfo->mouse_face_beg_col = vpos;
23043 dpyinfo->mouse_face_beg_row = hpos;
23044
23045 dpyinfo->mouse_face_beg_x = original_x_pixel - (total_pixel_width + dx);
23046 dpyinfo->mouse_face_beg_y = 0;
23047
23048 dpyinfo->mouse_face_end_col = vpos + gseq_length;
23049 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_beg_row;
23050
23051 dpyinfo->mouse_face_end_x = 0;
23052 dpyinfo->mouse_face_end_y = 0;
23053
23054 dpyinfo->mouse_face_past_end = 0;
23055 dpyinfo->mouse_face_window = window;
23056
23057 dpyinfo->mouse_face_face_id = face_at_string_position (w, string,
23058 charpos,
23059 0, 0, 0, &ignore,
23060 glyph->face_id, 1);
23061 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
23062
23063 if (NILP (pointer))
23064 pointer = Qhand;
23065 }
23066 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
23067 clear_mouse_face (dpyinfo);
23068 }
23069 define_frame_cursor1 (f, cursor, pointer);
23070 }
23071
23072
23073 /* EXPORT:
23074 Take proper action when the mouse has moved to position X, Y on
23075 frame F as regards highlighting characters that have mouse-face
23076 properties. Also de-highlighting chars where the mouse was before.
23077 X and Y can be negative or out of range. */
23078
23079 void
23080 note_mouse_highlight (f, x, y)
23081 struct frame *f;
23082 int x, y;
23083 {
23084 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
23085 enum window_part part;
23086 Lisp_Object window;
23087 struct window *w;
23088 Cursor cursor = No_Cursor;
23089 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
23090 struct buffer *b;
23091
23092 /* When a menu is active, don't highlight because this looks odd. */
23093 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (MAC_OS)
23094 if (popup_activated ())
23095 return;
23096 #endif
23097
23098 if (NILP (Vmouse_highlight)
23099 || !f->glyphs_initialized_p)
23100 return;
23101
23102 dpyinfo->mouse_face_mouse_x = x;
23103 dpyinfo->mouse_face_mouse_y = y;
23104 dpyinfo->mouse_face_mouse_frame = f;
23105
23106 if (dpyinfo->mouse_face_defer)
23107 return;
23108
23109 if (gc_in_progress)
23110 {
23111 dpyinfo->mouse_face_deferred_gc = 1;
23112 return;
23113 }
23114
23115 /* Which window is that in? */
23116 window = window_from_coordinates (f, x, y, &part, 0, 0, 1);
23117
23118 /* If we were displaying active text in another window, clear that.
23119 Also clear if we move out of text area in same window. */
23120 if (! EQ (window, dpyinfo->mouse_face_window)
23121 || (part != ON_TEXT && part != ON_MODE_LINE && part != ON_HEADER_LINE
23122 && !NILP (dpyinfo->mouse_face_window)))
23123 clear_mouse_face (dpyinfo);
23124
23125 /* Not on a window -> return. */
23126 if (!WINDOWP (window))
23127 return;
23128
23129 /* Reset help_echo_string. It will get recomputed below. */
23130 help_echo_string = Qnil;
23131
23132 /* Convert to window-relative pixel coordinates. */
23133 w = XWINDOW (window);
23134 frame_to_window_pixel_xy (w, &x, &y);
23135
23136 /* Handle tool-bar window differently since it doesn't display a
23137 buffer. */
23138 if (EQ (window, f->tool_bar_window))
23139 {
23140 note_tool_bar_highlight (f, x, y);
23141 return;
23142 }
23143
23144 /* Mouse is on the mode, header line or margin? */
23145 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
23146 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
23147 {
23148 note_mode_line_or_margin_highlight (window, x, y, part);
23149 return;
23150 }
23151
23152 if (part == ON_VERTICAL_BORDER)
23153 {
23154 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
23155 help_echo_string = build_string ("drag-mouse-1: resize");
23156 }
23157 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
23158 || part == ON_SCROLL_BAR)
23159 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
23160 else
23161 cursor = FRAME_X_OUTPUT (f)->text_cursor;
23162
23163 /* Are we in a window whose display is up to date?
23164 And verify the buffer's text has not changed. */
23165 b = XBUFFER (w->buffer);
23166 if (part == ON_TEXT
23167 && EQ (w->window_end_valid, w->buffer)
23168 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
23169 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
23170 {
23171 int hpos, vpos, pos, i, dx, dy, area;
23172 struct glyph *glyph;
23173 Lisp_Object object;
23174 Lisp_Object mouse_face = Qnil, overlay = Qnil, position;
23175 Lisp_Object *overlay_vec = NULL;
23176 int noverlays;
23177 struct buffer *obuf;
23178 int obegv, ozv, same_region;
23179
23180 /* Find the glyph under X/Y. */
23181 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
23182
23183 /* Look for :pointer property on image. */
23184 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
23185 {
23186 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
23187 if (img != NULL && IMAGEP (img->spec))
23188 {
23189 Lisp_Object image_map, hotspot;
23190 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
23191 !NILP (image_map))
23192 && (hotspot = find_hot_spot (image_map,
23193 glyph->slice.x + dx,
23194 glyph->slice.y + dy),
23195 CONSP (hotspot))
23196 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
23197 {
23198 Lisp_Object area_id, plist;
23199
23200 area_id = XCAR (hotspot);
23201 /* Could check AREA_ID to see if we enter/leave this hot-spot.
23202 If so, we could look for mouse-enter, mouse-leave
23203 properties in PLIST (and do something...). */
23204 hotspot = XCDR (hotspot);
23205 if (CONSP (hotspot)
23206 && (plist = XCAR (hotspot), CONSP (plist)))
23207 {
23208 pointer = Fplist_get (plist, Qpointer);
23209 if (NILP (pointer))
23210 pointer = Qhand;
23211 help_echo_string = Fplist_get (plist, Qhelp_echo);
23212 if (!NILP (help_echo_string))
23213 {
23214 help_echo_window = window;
23215 help_echo_object = glyph->object;
23216 help_echo_pos = glyph->charpos;
23217 }
23218 }
23219 }
23220 if (NILP (pointer))
23221 pointer = Fplist_get (XCDR (img->spec), QCpointer);
23222 }
23223 }
23224
23225 /* Clear mouse face if X/Y not over text. */
23226 if (glyph == NULL
23227 || area != TEXT_AREA
23228 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p)
23229 {
23230 if (clear_mouse_face (dpyinfo))
23231 cursor = No_Cursor;
23232 if (NILP (pointer))
23233 {
23234 if (area != TEXT_AREA)
23235 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
23236 else
23237 pointer = Vvoid_text_area_pointer;
23238 }
23239 goto set_cursor;
23240 }
23241
23242 pos = glyph->charpos;
23243 object = glyph->object;
23244 if (!STRINGP (object) && !BUFFERP (object))
23245 goto set_cursor;
23246
23247 /* If we get an out-of-range value, return now; avoid an error. */
23248 if (BUFFERP (object) && pos > BUF_Z (b))
23249 goto set_cursor;
23250
23251 /* Make the window's buffer temporarily current for
23252 overlays_at and compute_char_face. */
23253 obuf = current_buffer;
23254 current_buffer = b;
23255 obegv = BEGV;
23256 ozv = ZV;
23257 BEGV = BEG;
23258 ZV = Z;
23259
23260 /* Is this char mouse-active or does it have help-echo? */
23261 position = make_number (pos);
23262
23263 if (BUFFERP (object))
23264 {
23265 /* Put all the overlays we want in a vector in overlay_vec. */
23266 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
23267 /* Sort overlays into increasing priority order. */
23268 noverlays = sort_overlays (overlay_vec, noverlays, w);
23269 }
23270 else
23271 noverlays = 0;
23272
23273 same_region = (EQ (window, dpyinfo->mouse_face_window)
23274 && vpos >= dpyinfo->mouse_face_beg_row
23275 && vpos <= dpyinfo->mouse_face_end_row
23276 && (vpos > dpyinfo->mouse_face_beg_row
23277 || hpos >= dpyinfo->mouse_face_beg_col)
23278 && (vpos < dpyinfo->mouse_face_end_row
23279 || hpos < dpyinfo->mouse_face_end_col
23280 || dpyinfo->mouse_face_past_end));
23281
23282 if (same_region)
23283 cursor = No_Cursor;
23284
23285 /* Check mouse-face highlighting. */
23286 if (! same_region
23287 /* If there exists an overlay with mouse-face overlapping
23288 the one we are currently highlighting, we have to
23289 check if we enter the overlapping overlay, and then
23290 highlight only that. */
23291 || (OVERLAYP (dpyinfo->mouse_face_overlay)
23292 && mouse_face_overlay_overlaps (dpyinfo->mouse_face_overlay)))
23293 {
23294 /* Find the highest priority overlay that has a mouse-face
23295 property. */
23296 overlay = Qnil;
23297 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
23298 {
23299 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
23300 if (!NILP (mouse_face))
23301 overlay = overlay_vec[i];
23302 }
23303
23304 /* If we're actually highlighting the same overlay as
23305 before, there's no need to do that again. */
23306 if (!NILP (overlay)
23307 && EQ (overlay, dpyinfo->mouse_face_overlay))
23308 goto check_help_echo;
23309
23310 dpyinfo->mouse_face_overlay = overlay;
23311
23312 /* Clear the display of the old active region, if any. */
23313 if (clear_mouse_face (dpyinfo))
23314 cursor = No_Cursor;
23315
23316 /* If no overlay applies, get a text property. */
23317 if (NILP (overlay))
23318 mouse_face = Fget_text_property (position, Qmouse_face, object);
23319
23320 /* Handle the overlay case. */
23321 if (!NILP (overlay))
23322 {
23323 /* Find the range of text around this char that
23324 should be active. */
23325 Lisp_Object before, after;
23326 int ignore;
23327
23328 before = Foverlay_start (overlay);
23329 after = Foverlay_end (overlay);
23330 /* Record this as the current active region. */
23331 fast_find_position (w, XFASTINT (before),
23332 &dpyinfo->mouse_face_beg_col,
23333 &dpyinfo->mouse_face_beg_row,
23334 &dpyinfo->mouse_face_beg_x,
23335 &dpyinfo->mouse_face_beg_y, Qnil);
23336
23337 dpyinfo->mouse_face_past_end
23338 = !fast_find_position (w, XFASTINT (after),
23339 &dpyinfo->mouse_face_end_col,
23340 &dpyinfo->mouse_face_end_row,
23341 &dpyinfo->mouse_face_end_x,
23342 &dpyinfo->mouse_face_end_y, Qnil);
23343 dpyinfo->mouse_face_window = window;
23344
23345 dpyinfo->mouse_face_face_id
23346 = face_at_buffer_position (w, pos, 0, 0,
23347 &ignore, pos + 1,
23348 !dpyinfo->mouse_face_hidden);
23349
23350 /* Display it as active. */
23351 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
23352 cursor = No_Cursor;
23353 }
23354 /* Handle the text property case. */
23355 else if (!NILP (mouse_face) && BUFFERP (object))
23356 {
23357 /* Find the range of text around this char that
23358 should be active. */
23359 Lisp_Object before, after, beginning, end;
23360 int ignore;
23361
23362 beginning = Fmarker_position (w->start);
23363 end = make_number (BUF_Z (XBUFFER (object))
23364 - XFASTINT (w->window_end_pos));
23365 before
23366 = Fprevious_single_property_change (make_number (pos + 1),
23367 Qmouse_face,
23368 object, beginning);
23369 after
23370 = Fnext_single_property_change (position, Qmouse_face,
23371 object, end);
23372
23373 /* Record this as the current active region. */
23374 fast_find_position (w, XFASTINT (before),
23375 &dpyinfo->mouse_face_beg_col,
23376 &dpyinfo->mouse_face_beg_row,
23377 &dpyinfo->mouse_face_beg_x,
23378 &dpyinfo->mouse_face_beg_y, Qnil);
23379 dpyinfo->mouse_face_past_end
23380 = !fast_find_position (w, XFASTINT (after),
23381 &dpyinfo->mouse_face_end_col,
23382 &dpyinfo->mouse_face_end_row,
23383 &dpyinfo->mouse_face_end_x,
23384 &dpyinfo->mouse_face_end_y, Qnil);
23385 dpyinfo->mouse_face_window = window;
23386
23387 if (BUFFERP (object))
23388 dpyinfo->mouse_face_face_id
23389 = face_at_buffer_position (w, pos, 0, 0,
23390 &ignore, pos + 1,
23391 !dpyinfo->mouse_face_hidden);
23392
23393 /* Display it as active. */
23394 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
23395 cursor = No_Cursor;
23396 }
23397 else if (!NILP (mouse_face) && STRINGP (object))
23398 {
23399 Lisp_Object b, e;
23400 int ignore;
23401
23402 b = Fprevious_single_property_change (make_number (pos + 1),
23403 Qmouse_face,
23404 object, Qnil);
23405 e = Fnext_single_property_change (position, Qmouse_face,
23406 object, Qnil);
23407 if (NILP (b))
23408 b = make_number (0);
23409 if (NILP (e))
23410 e = make_number (SCHARS (object) - 1);
23411
23412 fast_find_string_pos (w, XINT (b), object,
23413 &dpyinfo->mouse_face_beg_col,
23414 &dpyinfo->mouse_face_beg_row,
23415 &dpyinfo->mouse_face_beg_x,
23416 &dpyinfo->mouse_face_beg_y, 0);
23417 fast_find_string_pos (w, XINT (e), object,
23418 &dpyinfo->mouse_face_end_col,
23419 &dpyinfo->mouse_face_end_row,
23420 &dpyinfo->mouse_face_end_x,
23421 &dpyinfo->mouse_face_end_y, 1);
23422 dpyinfo->mouse_face_past_end = 0;
23423 dpyinfo->mouse_face_window = window;
23424 dpyinfo->mouse_face_face_id
23425 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
23426 glyph->face_id, 1);
23427 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
23428 cursor = No_Cursor;
23429 }
23430 else if (STRINGP (object) && NILP (mouse_face))
23431 {
23432 /* A string which doesn't have mouse-face, but
23433 the text ``under'' it might have. */
23434 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
23435 int start = MATRIX_ROW_START_CHARPOS (r);
23436
23437 pos = string_buffer_position (w, object, start);
23438 if (pos > 0)
23439 mouse_face = get_char_property_and_overlay (make_number (pos),
23440 Qmouse_face,
23441 w->buffer,
23442 &overlay);
23443 if (!NILP (mouse_face) && !NILP (overlay))
23444 {
23445 Lisp_Object before = Foverlay_start (overlay);
23446 Lisp_Object after = Foverlay_end (overlay);
23447 int ignore;
23448
23449 /* Note that we might not be able to find position
23450 BEFORE in the glyph matrix if the overlay is
23451 entirely covered by a `display' property. In
23452 this case, we overshoot. So let's stop in
23453 the glyph matrix before glyphs for OBJECT. */
23454 fast_find_position (w, XFASTINT (before),
23455 &dpyinfo->mouse_face_beg_col,
23456 &dpyinfo->mouse_face_beg_row,
23457 &dpyinfo->mouse_face_beg_x,
23458 &dpyinfo->mouse_face_beg_y,
23459 object);
23460
23461 dpyinfo->mouse_face_past_end
23462 = !fast_find_position (w, XFASTINT (after),
23463 &dpyinfo->mouse_face_end_col,
23464 &dpyinfo->mouse_face_end_row,
23465 &dpyinfo->mouse_face_end_x,
23466 &dpyinfo->mouse_face_end_y,
23467 Qnil);
23468 dpyinfo->mouse_face_window = window;
23469 dpyinfo->mouse_face_face_id
23470 = face_at_buffer_position (w, pos, 0, 0,
23471 &ignore, pos + 1,
23472 !dpyinfo->mouse_face_hidden);
23473
23474 /* Display it as active. */
23475 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
23476 cursor = No_Cursor;
23477 }
23478 }
23479 }
23480
23481 check_help_echo:
23482
23483 /* Look for a `help-echo' property. */
23484 if (NILP (help_echo_string)) {
23485 Lisp_Object help, overlay;
23486
23487 /* Check overlays first. */
23488 help = overlay = Qnil;
23489 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
23490 {
23491 overlay = overlay_vec[i];
23492 help = Foverlay_get (overlay, Qhelp_echo);
23493 }
23494
23495 if (!NILP (help))
23496 {
23497 help_echo_string = help;
23498 help_echo_window = window;
23499 help_echo_object = overlay;
23500 help_echo_pos = pos;
23501 }
23502 else
23503 {
23504 Lisp_Object object = glyph->object;
23505 int charpos = glyph->charpos;
23506
23507 /* Try text properties. */
23508 if (STRINGP (object)
23509 && charpos >= 0
23510 && charpos < SCHARS (object))
23511 {
23512 help = Fget_text_property (make_number (charpos),
23513 Qhelp_echo, object);
23514 if (NILP (help))
23515 {
23516 /* If the string itself doesn't specify a help-echo,
23517 see if the buffer text ``under'' it does. */
23518 struct glyph_row *r
23519 = MATRIX_ROW (w->current_matrix, vpos);
23520 int start = MATRIX_ROW_START_CHARPOS (r);
23521 int pos = string_buffer_position (w, object, start);
23522 if (pos > 0)
23523 {
23524 help = Fget_char_property (make_number (pos),
23525 Qhelp_echo, w->buffer);
23526 if (!NILP (help))
23527 {
23528 charpos = pos;
23529 object = w->buffer;
23530 }
23531 }
23532 }
23533 }
23534 else if (BUFFERP (object)
23535 && charpos >= BEGV
23536 && charpos < ZV)
23537 help = Fget_text_property (make_number (charpos), Qhelp_echo,
23538 object);
23539
23540 if (!NILP (help))
23541 {
23542 help_echo_string = help;
23543 help_echo_window = window;
23544 help_echo_object = object;
23545 help_echo_pos = charpos;
23546 }
23547 }
23548 }
23549
23550 /* Look for a `pointer' property. */
23551 if (NILP (pointer))
23552 {
23553 /* Check overlays first. */
23554 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
23555 pointer = Foverlay_get (overlay_vec[i], Qpointer);
23556
23557 if (NILP (pointer))
23558 {
23559 Lisp_Object object = glyph->object;
23560 int charpos = glyph->charpos;
23561
23562 /* Try text properties. */
23563 if (STRINGP (object)
23564 && charpos >= 0
23565 && charpos < SCHARS (object))
23566 {
23567 pointer = Fget_text_property (make_number (charpos),
23568 Qpointer, object);
23569 if (NILP (pointer))
23570 {
23571 /* If the string itself doesn't specify a pointer,
23572 see if the buffer text ``under'' it does. */
23573 struct glyph_row *r
23574 = MATRIX_ROW (w->current_matrix, vpos);
23575 int start = MATRIX_ROW_START_CHARPOS (r);
23576 int pos = string_buffer_position (w, object, start);
23577 if (pos > 0)
23578 pointer = Fget_char_property (make_number (pos),
23579 Qpointer, w->buffer);
23580 }
23581 }
23582 else if (BUFFERP (object)
23583 && charpos >= BEGV
23584 && charpos < ZV)
23585 pointer = Fget_text_property (make_number (charpos),
23586 Qpointer, object);
23587 }
23588 }
23589
23590 BEGV = obegv;
23591 ZV = ozv;
23592 current_buffer = obuf;
23593 }
23594
23595 set_cursor:
23596
23597 define_frame_cursor1 (f, cursor, pointer);
23598 }
23599
23600
23601 /* EXPORT for RIF:
23602 Clear any mouse-face on window W. This function is part of the
23603 redisplay interface, and is called from try_window_id and similar
23604 functions to ensure the mouse-highlight is off. */
23605
23606 void
23607 x_clear_window_mouse_face (w)
23608 struct window *w;
23609 {
23610 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
23611 Lisp_Object window;
23612
23613 BLOCK_INPUT;
23614 XSETWINDOW (window, w);
23615 if (EQ (window, dpyinfo->mouse_face_window))
23616 clear_mouse_face (dpyinfo);
23617 UNBLOCK_INPUT;
23618 }
23619
23620
23621 /* EXPORT:
23622 Just discard the mouse face information for frame F, if any.
23623 This is used when the size of F is changed. */
23624
23625 void
23626 cancel_mouse_face (f)
23627 struct frame *f;
23628 {
23629 Lisp_Object window;
23630 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
23631
23632 window = dpyinfo->mouse_face_window;
23633 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
23634 {
23635 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
23636 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
23637 dpyinfo->mouse_face_window = Qnil;
23638 }
23639 }
23640
23641
23642 #endif /* HAVE_WINDOW_SYSTEM */
23643
23644 \f
23645 /***********************************************************************
23646 Exposure Events
23647 ***********************************************************************/
23648
23649 #ifdef HAVE_WINDOW_SYSTEM
23650
23651 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
23652 which intersects rectangle R. R is in window-relative coordinates. */
23653
23654 static void
23655 expose_area (w, row, r, area)
23656 struct window *w;
23657 struct glyph_row *row;
23658 XRectangle *r;
23659 enum glyph_row_area area;
23660 {
23661 struct glyph *first = row->glyphs[area];
23662 struct glyph *end = row->glyphs[area] + row->used[area];
23663 struct glyph *last;
23664 int first_x, start_x, x;
23665
23666 if (area == TEXT_AREA && row->fill_line_p)
23667 /* If row extends face to end of line write the whole line. */
23668 draw_glyphs (w, 0, row, area,
23669 0, row->used[area],
23670 DRAW_NORMAL_TEXT, 0);
23671 else
23672 {
23673 /* Set START_X to the window-relative start position for drawing glyphs of
23674 AREA. The first glyph of the text area can be partially visible.
23675 The first glyphs of other areas cannot. */
23676 start_x = window_box_left_offset (w, area);
23677 x = start_x;
23678 if (area == TEXT_AREA)
23679 x += row->x;
23680
23681 /* Find the first glyph that must be redrawn. */
23682 while (first < end
23683 && x + first->pixel_width < r->x)
23684 {
23685 x += first->pixel_width;
23686 ++first;
23687 }
23688
23689 /* Find the last one. */
23690 last = first;
23691 first_x = x;
23692 while (last < end
23693 && x < r->x + r->width)
23694 {
23695 x += last->pixel_width;
23696 ++last;
23697 }
23698
23699 /* Repaint. */
23700 if (last > first)
23701 draw_glyphs (w, first_x - start_x, row, area,
23702 first - row->glyphs[area], last - row->glyphs[area],
23703 DRAW_NORMAL_TEXT, 0);
23704 }
23705 }
23706
23707
23708 /* Redraw the parts of the glyph row ROW on window W intersecting
23709 rectangle R. R is in window-relative coordinates. Value is
23710 non-zero if mouse-face was overwritten. */
23711
23712 static int
23713 expose_line (w, row, r)
23714 struct window *w;
23715 struct glyph_row *row;
23716 XRectangle *r;
23717 {
23718 xassert (row->enabled_p);
23719
23720 if (row->mode_line_p || w->pseudo_window_p)
23721 draw_glyphs (w, 0, row, TEXT_AREA,
23722 0, row->used[TEXT_AREA],
23723 DRAW_NORMAL_TEXT, 0);
23724 else
23725 {
23726 if (row->used[LEFT_MARGIN_AREA])
23727 expose_area (w, row, r, LEFT_MARGIN_AREA);
23728 if (row->used[TEXT_AREA])
23729 expose_area (w, row, r, TEXT_AREA);
23730 if (row->used[RIGHT_MARGIN_AREA])
23731 expose_area (w, row, r, RIGHT_MARGIN_AREA);
23732 draw_row_fringe_bitmaps (w, row);
23733 }
23734
23735 return row->mouse_face_p;
23736 }
23737
23738
23739 /* Redraw those parts of glyphs rows during expose event handling that
23740 overlap other rows. Redrawing of an exposed line writes over parts
23741 of lines overlapping that exposed line; this function fixes that.
23742
23743 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
23744 row in W's current matrix that is exposed and overlaps other rows.
23745 LAST_OVERLAPPING_ROW is the last such row. */
23746
23747 static void
23748 expose_overlaps (w, first_overlapping_row, last_overlapping_row, r)
23749 struct window *w;
23750 struct glyph_row *first_overlapping_row;
23751 struct glyph_row *last_overlapping_row;
23752 XRectangle *r;
23753 {
23754 struct glyph_row *row;
23755
23756 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
23757 if (row->overlapping_p)
23758 {
23759 xassert (row->enabled_p && !row->mode_line_p);
23760
23761 row->clip = r;
23762 if (row->used[LEFT_MARGIN_AREA])
23763 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
23764
23765 if (row->used[TEXT_AREA])
23766 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
23767
23768 if (row->used[RIGHT_MARGIN_AREA])
23769 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
23770 row->clip = NULL;
23771 }
23772 }
23773
23774
23775 /* Return non-zero if W's cursor intersects rectangle R. */
23776
23777 static int
23778 phys_cursor_in_rect_p (w, r)
23779 struct window *w;
23780 XRectangle *r;
23781 {
23782 XRectangle cr, result;
23783 struct glyph *cursor_glyph;
23784
23785 cursor_glyph = get_phys_cursor_glyph (w);
23786 if (cursor_glyph)
23787 {
23788 /* r is relative to W's box, but w->phys_cursor.x is relative
23789 to left edge of W's TEXT area. Adjust it. */
23790 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
23791 cr.y = w->phys_cursor.y;
23792 cr.width = cursor_glyph->pixel_width;
23793 cr.height = w->phys_cursor_height;
23794 /* ++KFS: W32 version used W32-specific IntersectRect here, but
23795 I assume the effect is the same -- and this is portable. */
23796 return x_intersect_rectangles (&cr, r, &result);
23797 }
23798 /* If we don't understand the format, pretend we're not in the hot-spot. */
23799 return 0;
23800 }
23801
23802
23803 /* EXPORT:
23804 Draw a vertical window border to the right of window W if W doesn't
23805 have vertical scroll bars. */
23806
23807 void
23808 x_draw_vertical_border (w)
23809 struct window *w;
23810 {
23811 struct frame *f = XFRAME (WINDOW_FRAME (w));
23812
23813 /* We could do better, if we knew what type of scroll-bar the adjacent
23814 windows (on either side) have... But we don't :-(
23815 However, I think this works ok. ++KFS 2003-04-25 */
23816
23817 /* Redraw borders between horizontally adjacent windows. Don't
23818 do it for frames with vertical scroll bars because either the
23819 right scroll bar of a window, or the left scroll bar of its
23820 neighbor will suffice as a border. */
23821 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
23822 return;
23823
23824 if (!WINDOW_RIGHTMOST_P (w)
23825 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
23826 {
23827 int x0, x1, y0, y1;
23828
23829 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
23830 y1 -= 1;
23831
23832 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
23833 x1 -= 1;
23834
23835 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
23836 }
23837 else if (!WINDOW_LEFTMOST_P (w)
23838 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
23839 {
23840 int x0, x1, y0, y1;
23841
23842 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
23843 y1 -= 1;
23844
23845 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
23846 x0 -= 1;
23847
23848 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
23849 }
23850 }
23851
23852
23853 /* Redraw the part of window W intersection rectangle FR. Pixel
23854 coordinates in FR are frame-relative. Call this function with
23855 input blocked. Value is non-zero if the exposure overwrites
23856 mouse-face. */
23857
23858 static int
23859 expose_window (w, fr)
23860 struct window *w;
23861 XRectangle *fr;
23862 {
23863 struct frame *f = XFRAME (w->frame);
23864 XRectangle wr, r;
23865 int mouse_face_overwritten_p = 0;
23866
23867 /* If window is not yet fully initialized, do nothing. This can
23868 happen when toolkit scroll bars are used and a window is split.
23869 Reconfiguring the scroll bar will generate an expose for a newly
23870 created window. */
23871 if (w->current_matrix == NULL)
23872 return 0;
23873
23874 /* When we're currently updating the window, display and current
23875 matrix usually don't agree. Arrange for a thorough display
23876 later. */
23877 if (w == updated_window)
23878 {
23879 SET_FRAME_GARBAGED (f);
23880 return 0;
23881 }
23882
23883 /* Frame-relative pixel rectangle of W. */
23884 wr.x = WINDOW_LEFT_EDGE_X (w);
23885 wr.y = WINDOW_TOP_EDGE_Y (w);
23886 wr.width = WINDOW_TOTAL_WIDTH (w);
23887 wr.height = WINDOW_TOTAL_HEIGHT (w);
23888
23889 if (x_intersect_rectangles (fr, &wr, &r))
23890 {
23891 int yb = window_text_bottom_y (w);
23892 struct glyph_row *row;
23893 int cursor_cleared_p;
23894 struct glyph_row *first_overlapping_row, *last_overlapping_row;
23895
23896 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
23897 r.x, r.y, r.width, r.height));
23898
23899 /* Convert to window coordinates. */
23900 r.x -= WINDOW_LEFT_EDGE_X (w);
23901 r.y -= WINDOW_TOP_EDGE_Y (w);
23902
23903 /* Turn off the cursor. */
23904 if (!w->pseudo_window_p
23905 && phys_cursor_in_rect_p (w, &r))
23906 {
23907 x_clear_cursor (w);
23908 cursor_cleared_p = 1;
23909 }
23910 else
23911 cursor_cleared_p = 0;
23912
23913 /* Update lines intersecting rectangle R. */
23914 first_overlapping_row = last_overlapping_row = NULL;
23915 for (row = w->current_matrix->rows;
23916 row->enabled_p;
23917 ++row)
23918 {
23919 int y0 = row->y;
23920 int y1 = MATRIX_ROW_BOTTOM_Y (row);
23921
23922 if ((y0 >= r.y && y0 < r.y + r.height)
23923 || (y1 > r.y && y1 < r.y + r.height)
23924 || (r.y >= y0 && r.y < y1)
23925 || (r.y + r.height > y0 && r.y + r.height < y1))
23926 {
23927 /* A header line may be overlapping, but there is no need
23928 to fix overlapping areas for them. KFS 2005-02-12 */
23929 if (row->overlapping_p && !row->mode_line_p)
23930 {
23931 if (first_overlapping_row == NULL)
23932 first_overlapping_row = row;
23933 last_overlapping_row = row;
23934 }
23935
23936 row->clip = fr;
23937 if (expose_line (w, row, &r))
23938 mouse_face_overwritten_p = 1;
23939 row->clip = NULL;
23940 }
23941 else if (row->overlapping_p)
23942 {
23943 /* We must redraw a row overlapping the exposed area. */
23944 if (y0 < r.y
23945 ? y0 + row->phys_height > r.y
23946 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
23947 {
23948 if (first_overlapping_row == NULL)
23949 first_overlapping_row = row;
23950 last_overlapping_row = row;
23951 }
23952 }
23953
23954 if (y1 >= yb)
23955 break;
23956 }
23957
23958 /* Display the mode line if there is one. */
23959 if (WINDOW_WANTS_MODELINE_P (w)
23960 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
23961 row->enabled_p)
23962 && row->y < r.y + r.height)
23963 {
23964 if (expose_line (w, row, &r))
23965 mouse_face_overwritten_p = 1;
23966 }
23967
23968 if (!w->pseudo_window_p)
23969 {
23970 /* Fix the display of overlapping rows. */
23971 if (first_overlapping_row)
23972 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
23973 fr);
23974
23975 /* Draw border between windows. */
23976 x_draw_vertical_border (w);
23977
23978 /* Turn the cursor on again. */
23979 if (cursor_cleared_p)
23980 update_window_cursor (w, 1);
23981 }
23982 }
23983
23984 return mouse_face_overwritten_p;
23985 }
23986
23987
23988
23989 /* Redraw (parts) of all windows in the window tree rooted at W that
23990 intersect R. R contains frame pixel coordinates. Value is
23991 non-zero if the exposure overwrites mouse-face. */
23992
23993 static int
23994 expose_window_tree (w, r)
23995 struct window *w;
23996 XRectangle *r;
23997 {
23998 struct frame *f = XFRAME (w->frame);
23999 int mouse_face_overwritten_p = 0;
24000
24001 while (w && !FRAME_GARBAGED_P (f))
24002 {
24003 if (!NILP (w->hchild))
24004 mouse_face_overwritten_p
24005 |= expose_window_tree (XWINDOW (w->hchild), r);
24006 else if (!NILP (w->vchild))
24007 mouse_face_overwritten_p
24008 |= expose_window_tree (XWINDOW (w->vchild), r);
24009 else
24010 mouse_face_overwritten_p |= expose_window (w, r);
24011
24012 w = NILP (w->next) ? NULL : XWINDOW (w->next);
24013 }
24014
24015 return mouse_face_overwritten_p;
24016 }
24017
24018
24019 /* EXPORT:
24020 Redisplay an exposed area of frame F. X and Y are the upper-left
24021 corner of the exposed rectangle. W and H are width and height of
24022 the exposed area. All are pixel values. W or H zero means redraw
24023 the entire frame. */
24024
24025 void
24026 expose_frame (f, x, y, w, h)
24027 struct frame *f;
24028 int x, y, w, h;
24029 {
24030 XRectangle r;
24031 int mouse_face_overwritten_p = 0;
24032
24033 TRACE ((stderr, "expose_frame "));
24034
24035 /* No need to redraw if frame will be redrawn soon. */
24036 if (FRAME_GARBAGED_P (f))
24037 {
24038 TRACE ((stderr, " garbaged\n"));
24039 return;
24040 }
24041
24042 /* If basic faces haven't been realized yet, there is no point in
24043 trying to redraw anything. This can happen when we get an expose
24044 event while Emacs is starting, e.g. by moving another window. */
24045 if (FRAME_FACE_CACHE (f) == NULL
24046 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
24047 {
24048 TRACE ((stderr, " no faces\n"));
24049 return;
24050 }
24051
24052 if (w == 0 || h == 0)
24053 {
24054 r.x = r.y = 0;
24055 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
24056 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
24057 }
24058 else
24059 {
24060 r.x = x;
24061 r.y = y;
24062 r.width = w;
24063 r.height = h;
24064 }
24065
24066 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
24067 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
24068
24069 if (WINDOWP (f->tool_bar_window))
24070 mouse_face_overwritten_p
24071 |= expose_window (XWINDOW (f->tool_bar_window), &r);
24072
24073 #ifdef HAVE_X_WINDOWS
24074 #ifndef MSDOS
24075 #ifndef USE_X_TOOLKIT
24076 if (WINDOWP (f->menu_bar_window))
24077 mouse_face_overwritten_p
24078 |= expose_window (XWINDOW (f->menu_bar_window), &r);
24079 #endif /* not USE_X_TOOLKIT */
24080 #endif
24081 #endif
24082
24083 /* Some window managers support a focus-follows-mouse style with
24084 delayed raising of frames. Imagine a partially obscured frame,
24085 and moving the mouse into partially obscured mouse-face on that
24086 frame. The visible part of the mouse-face will be highlighted,
24087 then the WM raises the obscured frame. With at least one WM, KDE
24088 2.1, Emacs is not getting any event for the raising of the frame
24089 (even tried with SubstructureRedirectMask), only Expose events.
24090 These expose events will draw text normally, i.e. not
24091 highlighted. Which means we must redo the highlight here.
24092 Subsume it under ``we love X''. --gerd 2001-08-15 */
24093 /* Included in Windows version because Windows most likely does not
24094 do the right thing if any third party tool offers
24095 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
24096 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
24097 {
24098 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
24099 if (f == dpyinfo->mouse_face_mouse_frame)
24100 {
24101 int x = dpyinfo->mouse_face_mouse_x;
24102 int y = dpyinfo->mouse_face_mouse_y;
24103 clear_mouse_face (dpyinfo);
24104 note_mouse_highlight (f, x, y);
24105 }
24106 }
24107 }
24108
24109
24110 /* EXPORT:
24111 Determine the intersection of two rectangles R1 and R2. Return
24112 the intersection in *RESULT. Value is non-zero if RESULT is not
24113 empty. */
24114
24115 int
24116 x_intersect_rectangles (r1, r2, result)
24117 XRectangle *r1, *r2, *result;
24118 {
24119 XRectangle *left, *right;
24120 XRectangle *upper, *lower;
24121 int intersection_p = 0;
24122
24123 /* Rearrange so that R1 is the left-most rectangle. */
24124 if (r1->x < r2->x)
24125 left = r1, right = r2;
24126 else
24127 left = r2, right = r1;
24128
24129 /* X0 of the intersection is right.x0, if this is inside R1,
24130 otherwise there is no intersection. */
24131 if (right->x <= left->x + left->width)
24132 {
24133 result->x = right->x;
24134
24135 /* The right end of the intersection is the minimum of the
24136 the right ends of left and right. */
24137 result->width = (min (left->x + left->width, right->x + right->width)
24138 - result->x);
24139
24140 /* Same game for Y. */
24141 if (r1->y < r2->y)
24142 upper = r1, lower = r2;
24143 else
24144 upper = r2, lower = r1;
24145
24146 /* The upper end of the intersection is lower.y0, if this is inside
24147 of upper. Otherwise, there is no intersection. */
24148 if (lower->y <= upper->y + upper->height)
24149 {
24150 result->y = lower->y;
24151
24152 /* The lower end of the intersection is the minimum of the lower
24153 ends of upper and lower. */
24154 result->height = (min (lower->y + lower->height,
24155 upper->y + upper->height)
24156 - result->y);
24157 intersection_p = 1;
24158 }
24159 }
24160
24161 return intersection_p;
24162 }
24163
24164 #endif /* HAVE_WINDOW_SYSTEM */
24165
24166 \f
24167 /***********************************************************************
24168 Initialization
24169 ***********************************************************************/
24170
24171 void
24172 syms_of_xdisp ()
24173 {
24174 Vwith_echo_area_save_vector = Qnil;
24175 staticpro (&Vwith_echo_area_save_vector);
24176
24177 Vmessage_stack = Qnil;
24178 staticpro (&Vmessage_stack);
24179
24180 Qinhibit_redisplay = intern ("inhibit-redisplay");
24181 staticpro (&Qinhibit_redisplay);
24182
24183 message_dolog_marker1 = Fmake_marker ();
24184 staticpro (&message_dolog_marker1);
24185 message_dolog_marker2 = Fmake_marker ();
24186 staticpro (&message_dolog_marker2);
24187 message_dolog_marker3 = Fmake_marker ();
24188 staticpro (&message_dolog_marker3);
24189
24190 #if GLYPH_DEBUG
24191 defsubr (&Sdump_frame_glyph_matrix);
24192 defsubr (&Sdump_glyph_matrix);
24193 defsubr (&Sdump_glyph_row);
24194 defsubr (&Sdump_tool_bar_row);
24195 defsubr (&Strace_redisplay);
24196 defsubr (&Strace_to_stderr);
24197 #endif
24198 #ifdef HAVE_WINDOW_SYSTEM
24199 defsubr (&Stool_bar_lines_needed);
24200 defsubr (&Slookup_image_map);
24201 #endif
24202 defsubr (&Sformat_mode_line);
24203 defsubr (&Sinvisible_p);
24204
24205 staticpro (&Qmenu_bar_update_hook);
24206 Qmenu_bar_update_hook = intern ("menu-bar-update-hook");
24207
24208 staticpro (&Qoverriding_terminal_local_map);
24209 Qoverriding_terminal_local_map = intern ("overriding-terminal-local-map");
24210
24211 staticpro (&Qoverriding_local_map);
24212 Qoverriding_local_map = intern ("overriding-local-map");
24213
24214 staticpro (&Qwindow_scroll_functions);
24215 Qwindow_scroll_functions = intern ("window-scroll-functions");
24216
24217 staticpro (&Qredisplay_end_trigger_functions);
24218 Qredisplay_end_trigger_functions = intern ("redisplay-end-trigger-functions");
24219
24220 staticpro (&Qinhibit_point_motion_hooks);
24221 Qinhibit_point_motion_hooks = intern ("inhibit-point-motion-hooks");
24222
24223 QCdata = intern (":data");
24224 staticpro (&QCdata);
24225 Qdisplay = intern ("display");
24226 staticpro (&Qdisplay);
24227 Qspace_width = intern ("space-width");
24228 staticpro (&Qspace_width);
24229 Qraise = intern ("raise");
24230 staticpro (&Qraise);
24231 Qslice = intern ("slice");
24232 staticpro (&Qslice);
24233 Qspace = intern ("space");
24234 staticpro (&Qspace);
24235 Qmargin = intern ("margin");
24236 staticpro (&Qmargin);
24237 Qpointer = intern ("pointer");
24238 staticpro (&Qpointer);
24239 Qleft_margin = intern ("left-margin");
24240 staticpro (&Qleft_margin);
24241 Qright_margin = intern ("right-margin");
24242 staticpro (&Qright_margin);
24243 Qcenter = intern ("center");
24244 staticpro (&Qcenter);
24245 Qline_height = intern ("line-height");
24246 staticpro (&Qline_height);
24247 QCalign_to = intern (":align-to");
24248 staticpro (&QCalign_to);
24249 QCrelative_width = intern (":relative-width");
24250 staticpro (&QCrelative_width);
24251 QCrelative_height = intern (":relative-height");
24252 staticpro (&QCrelative_height);
24253 QCeval = intern (":eval");
24254 staticpro (&QCeval);
24255 QCpropertize = intern (":propertize");
24256 staticpro (&QCpropertize);
24257 QCfile = intern (":file");
24258 staticpro (&QCfile);
24259 Qfontified = intern ("fontified");
24260 staticpro (&Qfontified);
24261 Qfontification_functions = intern ("fontification-functions");
24262 staticpro (&Qfontification_functions);
24263 Qtrailing_whitespace = intern ("trailing-whitespace");
24264 staticpro (&Qtrailing_whitespace);
24265 Qescape_glyph = intern ("escape-glyph");
24266 staticpro (&Qescape_glyph);
24267 Qnobreak_space = intern ("nobreak-space");
24268 staticpro (&Qnobreak_space);
24269 Qimage = intern ("image");
24270 staticpro (&Qimage);
24271 QCmap = intern (":map");
24272 staticpro (&QCmap);
24273 QCpointer = intern (":pointer");
24274 staticpro (&QCpointer);
24275 Qrect = intern ("rect");
24276 staticpro (&Qrect);
24277 Qcircle = intern ("circle");
24278 staticpro (&Qcircle);
24279 Qpoly = intern ("poly");
24280 staticpro (&Qpoly);
24281 Qmessage_truncate_lines = intern ("message-truncate-lines");
24282 staticpro (&Qmessage_truncate_lines);
24283 Qgrow_only = intern ("grow-only");
24284 staticpro (&Qgrow_only);
24285 Qinhibit_menubar_update = intern ("inhibit-menubar-update");
24286 staticpro (&Qinhibit_menubar_update);
24287 Qinhibit_eval_during_redisplay = intern ("inhibit-eval-during-redisplay");
24288 staticpro (&Qinhibit_eval_during_redisplay);
24289 Qposition = intern ("position");
24290 staticpro (&Qposition);
24291 Qbuffer_position = intern ("buffer-position");
24292 staticpro (&Qbuffer_position);
24293 Qobject = intern ("object");
24294 staticpro (&Qobject);
24295 Qbar = intern ("bar");
24296 staticpro (&Qbar);
24297 Qhbar = intern ("hbar");
24298 staticpro (&Qhbar);
24299 Qbox = intern ("box");
24300 staticpro (&Qbox);
24301 Qhollow = intern ("hollow");
24302 staticpro (&Qhollow);
24303 Qhand = intern ("hand");
24304 staticpro (&Qhand);
24305 Qarrow = intern ("arrow");
24306 staticpro (&Qarrow);
24307 Qtext = intern ("text");
24308 staticpro (&Qtext);
24309 Qrisky_local_variable = intern ("risky-local-variable");
24310 staticpro (&Qrisky_local_variable);
24311 Qinhibit_free_realized_faces = intern ("inhibit-free-realized-faces");
24312 staticpro (&Qinhibit_free_realized_faces);
24313
24314 list_of_error = Fcons (Fcons (intern ("error"),
24315 Fcons (intern ("void-variable"), Qnil)),
24316 Qnil);
24317 staticpro (&list_of_error);
24318
24319 Qlast_arrow_position = intern ("last-arrow-position");
24320 staticpro (&Qlast_arrow_position);
24321 Qlast_arrow_string = intern ("last-arrow-string");
24322 staticpro (&Qlast_arrow_string);
24323
24324 Qoverlay_arrow_string = intern ("overlay-arrow-string");
24325 staticpro (&Qoverlay_arrow_string);
24326 Qoverlay_arrow_bitmap = intern ("overlay-arrow-bitmap");
24327 staticpro (&Qoverlay_arrow_bitmap);
24328
24329 echo_buffer[0] = echo_buffer[1] = Qnil;
24330 staticpro (&echo_buffer[0]);
24331 staticpro (&echo_buffer[1]);
24332
24333 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
24334 staticpro (&echo_area_buffer[0]);
24335 staticpro (&echo_area_buffer[1]);
24336
24337 Vmessages_buffer_name = build_string ("*Messages*");
24338 staticpro (&Vmessages_buffer_name);
24339
24340 mode_line_proptrans_alist = Qnil;
24341 staticpro (&mode_line_proptrans_alist);
24342 mode_line_string_list = Qnil;
24343 staticpro (&mode_line_string_list);
24344 mode_line_string_face = Qnil;
24345 staticpro (&mode_line_string_face);
24346 mode_line_string_face_prop = Qnil;
24347 staticpro (&mode_line_string_face_prop);
24348 Vmode_line_unwind_vector = Qnil;
24349 staticpro (&Vmode_line_unwind_vector);
24350
24351 help_echo_string = Qnil;
24352 staticpro (&help_echo_string);
24353 help_echo_object = Qnil;
24354 staticpro (&help_echo_object);
24355 help_echo_window = Qnil;
24356 staticpro (&help_echo_window);
24357 previous_help_echo_string = Qnil;
24358 staticpro (&previous_help_echo_string);
24359 help_echo_pos = -1;
24360
24361 #ifdef HAVE_WINDOW_SYSTEM
24362 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
24363 doc: /* *Non-nil means draw block cursor as wide as the glyph under it.
24364 For example, if a block cursor is over a tab, it will be drawn as
24365 wide as that tab on the display. */);
24366 x_stretch_cursor_p = 0;
24367 #endif
24368
24369 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
24370 doc: /* *Non-nil means highlight trailing whitespace.
24371 The face used for trailing whitespace is `trailing-whitespace'. */);
24372 Vshow_trailing_whitespace = Qnil;
24373
24374 DEFVAR_LISP ("nobreak-char-display", &Vnobreak_char_display,
24375 doc: /* *Control highlighting of nobreak space and soft hyphen.
24376 A value of t means highlight the character itself (for nobreak space,
24377 use face `nobreak-space').
24378 A value of nil means no highlighting.
24379 Other values mean display the escape glyph followed by an ordinary
24380 space or ordinary hyphen. */);
24381 Vnobreak_char_display = Qt;
24382
24383 DEFVAR_LISP ("void-text-area-pointer", &Vvoid_text_area_pointer,
24384 doc: /* *The pointer shape to show in void text areas.
24385 A value of nil means to show the text pointer. Other options are `arrow',
24386 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
24387 Vvoid_text_area_pointer = Qarrow;
24388
24389 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
24390 doc: /* Non-nil means don't actually do any redisplay.
24391 This is used for internal purposes. */);
24392 Vinhibit_redisplay = Qnil;
24393
24394 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
24395 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
24396 Vglobal_mode_string = Qnil;
24397
24398 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
24399 doc: /* Marker for where to display an arrow on top of the buffer text.
24400 This must be the beginning of a line in order to work.
24401 See also `overlay-arrow-string'. */);
24402 Voverlay_arrow_position = Qnil;
24403
24404 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
24405 doc: /* String to display as an arrow in non-window frames.
24406 See also `overlay-arrow-position'. */);
24407 Voverlay_arrow_string = build_string ("=>");
24408
24409 DEFVAR_LISP ("overlay-arrow-variable-list", &Voverlay_arrow_variable_list,
24410 doc: /* List of variables (symbols) which hold markers for overlay arrows.
24411 The symbols on this list are examined during redisplay to determine
24412 where to display overlay arrows. */);
24413 Voverlay_arrow_variable_list
24414 = Fcons (intern ("overlay-arrow-position"), Qnil);
24415
24416 DEFVAR_INT ("scroll-step", &scroll_step,
24417 doc: /* *The number of lines to try scrolling a window by when point moves out.
24418 If that fails to bring point back on frame, point is centered instead.
24419 If this is zero, point is always centered after it moves off frame.
24420 If you want scrolling to always be a line at a time, you should set
24421 `scroll-conservatively' to a large value rather than set this to 1. */);
24422
24423 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
24424 doc: /* *Scroll up to this many lines, to bring point back on screen.
24425 A value of zero means to scroll the text to center point vertically
24426 in the window. */);
24427 scroll_conservatively = 0;
24428
24429 DEFVAR_INT ("scroll-margin", &scroll_margin,
24430 doc: /* *Number of lines of margin at the top and bottom of a window.
24431 Recenter the window whenever point gets within this many lines
24432 of the top or bottom of the window. */);
24433 scroll_margin = 0;
24434
24435 DEFVAR_LISP ("display-pixels-per-inch", &Vdisplay_pixels_per_inch,
24436 doc: /* Pixels per inch value for non-window system displays.
24437 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
24438 Vdisplay_pixels_per_inch = make_float (72.0);
24439
24440 #if GLYPH_DEBUG
24441 DEFVAR_INT ("debug-end-pos", &debug_end_pos, doc: /* Don't ask. */);
24442 #endif
24443
24444 DEFVAR_BOOL ("truncate-partial-width-windows",
24445 &truncate_partial_width_windows,
24446 doc: /* *Non-nil means truncate lines in all windows less than full frame wide. */);
24447 truncate_partial_width_windows = 1;
24448
24449 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
24450 doc: /* When nil, display the mode-line/header-line/menu-bar in the default face.
24451 Any other value means to use the appropriate face, `mode-line',
24452 `header-line', or `menu' respectively. */);
24453 mode_line_inverse_video = 1;
24454
24455 DEFVAR_LISP ("line-number-display-limit", &Vline_number_display_limit,
24456 doc: /* *Maximum buffer size for which line number should be displayed.
24457 If the buffer is bigger than this, the line number does not appear
24458 in the mode line. A value of nil means no limit. */);
24459 Vline_number_display_limit = Qnil;
24460
24461 DEFVAR_INT ("line-number-display-limit-width",
24462 &line_number_display_limit_width,
24463 doc: /* *Maximum line width (in characters) for line number display.
24464 If the average length of the lines near point is bigger than this, then the
24465 line number may be omitted from the mode line. */);
24466 line_number_display_limit_width = 200;
24467
24468 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
24469 doc: /* *Non-nil means highlight region even in nonselected windows. */);
24470 highlight_nonselected_windows = 0;
24471
24472 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
24473 doc: /* Non-nil if more than one frame is visible on this display.
24474 Minibuffer-only frames don't count, but iconified frames do.
24475 This variable is not guaranteed to be accurate except while processing
24476 `frame-title-format' and `icon-title-format'. */);
24477
24478 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
24479 doc: /* Template for displaying the title bar of visible frames.
24480 \(Assuming the window manager supports this feature.)
24481
24482 This variable has the same structure as `mode-line-format', except that
24483 the %c and %l constructs are ignored. It is used only on frames for
24484 which no explicit name has been set \(see `modify-frame-parameters'). */);
24485
24486 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
24487 doc: /* Template for displaying the title bar of an iconified frame.
24488 \(Assuming the window manager supports this feature.)
24489 This variable has the same structure as `mode-line-format' (which see),
24490 and is used only on frames for which no explicit name has been set
24491 \(see `modify-frame-parameters'). */);
24492 Vicon_title_format
24493 = Vframe_title_format
24494 = Fcons (intern ("multiple-frames"),
24495 Fcons (build_string ("%b"),
24496 Fcons (Fcons (empty_unibyte_string,
24497 Fcons (intern ("invocation-name"),
24498 Fcons (build_string ("@"),
24499 Fcons (intern ("system-name"),
24500 Qnil)))),
24501 Qnil)));
24502
24503 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
24504 doc: /* Maximum number of lines to keep in the message log buffer.
24505 If nil, disable message logging. If t, log messages but don't truncate
24506 the buffer when it becomes large. */);
24507 Vmessage_log_max = make_number (100);
24508
24509 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
24510 doc: /* Functions called before redisplay, if window sizes have changed.
24511 The value should be a list of functions that take one argument.
24512 Just before redisplay, for each frame, if any of its windows have changed
24513 size since the last redisplay, or have been split or deleted,
24514 all the functions in the list are called, with the frame as argument. */);
24515 Vwindow_size_change_functions = Qnil;
24516
24517 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
24518 doc: /* List of functions to call before redisplaying a window with scrolling.
24519 Each function is called with two arguments, the window
24520 and its new display-start position. Note that the value of `window-end'
24521 is not valid when these functions are called. */);
24522 Vwindow_scroll_functions = Qnil;
24523
24524 DEFVAR_LISP ("redisplay-end-trigger-functions", &Vredisplay_end_trigger_functions,
24525 doc: /* Functions called when redisplay of a window reaches the end trigger.
24526 Each function is called with two arguments, the window and the end trigger value.
24527 See `set-window-redisplay-end-trigger'. */);
24528 Vredisplay_end_trigger_functions = Qnil;
24529
24530 DEFVAR_LISP ("mouse-autoselect-window", &Vmouse_autoselect_window,
24531 doc: /* *Non-nil means autoselect window with mouse pointer.
24532 If nil, do not autoselect windows.
24533 A positive number means delay autoselection by that many seconds: a
24534 window is autoselected only after the mouse has remained in that
24535 window for the duration of the delay.
24536 A negative number has a similar effect, but causes windows to be
24537 autoselected only after the mouse has stopped moving. \(Because of
24538 the way Emacs compares mouse events, you will occasionally wait twice
24539 that time before the window gets selected.\)
24540 Any other value means to autoselect window instantaneously when the
24541 mouse pointer enters it.
24542
24543 Autoselection selects the minibuffer only if it is active, and never
24544 unselects the minibuffer if it is active.
24545
24546 When customizing this variable make sure that the actual value of
24547 `focus-follows-mouse' matches the behavior of your window manager. */);
24548 Vmouse_autoselect_window = Qnil;
24549
24550 DEFVAR_LISP ("auto-resize-tool-bars", &Vauto_resize_tool_bars,
24551 doc: /* *Non-nil means automatically resize tool-bars.
24552 This dynamically changes the tool-bar's height to the minimum height
24553 that is needed to make all tool-bar items visible.
24554 If value is `grow-only', the tool-bar's height is only increased
24555 automatically; to decrease the tool-bar height, use \\[recenter]. */);
24556 Vauto_resize_tool_bars = Qt;
24557
24558 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
24559 doc: /* *Non-nil means raise tool-bar buttons when the mouse moves over them. */);
24560 auto_raise_tool_bar_buttons_p = 1;
24561
24562 DEFVAR_BOOL ("make-cursor-line-fully-visible", &make_cursor_line_fully_visible_p,
24563 doc: /* *Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
24564 make_cursor_line_fully_visible_p = 1;
24565
24566 DEFVAR_LISP ("tool-bar-border", &Vtool_bar_border,
24567 doc: /* *Border below tool-bar in pixels.
24568 If an integer, use it as the height of the border.
24569 If it is one of `internal-border-width' or `border-width', use the
24570 value of the corresponding frame parameter.
24571 Otherwise, no border is added below the tool-bar. */);
24572 Vtool_bar_border = Qinternal_border_width;
24573
24574 DEFVAR_LISP ("tool-bar-button-margin", &Vtool_bar_button_margin,
24575 doc: /* *Margin around tool-bar buttons in pixels.
24576 If an integer, use that for both horizontal and vertical margins.
24577 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
24578 HORZ specifying the horizontal margin, and VERT specifying the
24579 vertical margin. */);
24580 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
24581
24582 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
24583 doc: /* *Relief thickness of tool-bar buttons. */);
24584 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
24585
24586 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
24587 doc: /* List of functions to call to fontify regions of text.
24588 Each function is called with one argument POS. Functions must
24589 fontify a region starting at POS in the current buffer, and give
24590 fontified regions the property `fontified'. */);
24591 Vfontification_functions = Qnil;
24592 Fmake_variable_buffer_local (Qfontification_functions);
24593
24594 DEFVAR_BOOL ("unibyte-display-via-language-environment",
24595 &unibyte_display_via_language_environment,
24596 doc: /* *Non-nil means display unibyte text according to language environment.
24597 Specifically this means that unibyte non-ASCII characters
24598 are displayed by converting them to the equivalent multibyte characters
24599 according to the current language environment. As a result, they are
24600 displayed according to the current fontset. */);
24601 unibyte_display_via_language_environment = 0;
24602
24603 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
24604 doc: /* *Maximum height for resizing mini-windows.
24605 If a float, it specifies a fraction of the mini-window frame's height.
24606 If an integer, it specifies a number of lines. */);
24607 Vmax_mini_window_height = make_float (0.25);
24608
24609 DEFVAR_LISP ("resize-mini-windows", &Vresize_mini_windows,
24610 doc: /* *How to resize mini-windows.
24611 A value of nil means don't automatically resize mini-windows.
24612 A value of t means resize them to fit the text displayed in them.
24613 A value of `grow-only', the default, means let mini-windows grow
24614 only, until their display becomes empty, at which point the windows
24615 go back to their normal size. */);
24616 Vresize_mini_windows = Qgrow_only;
24617
24618 DEFVAR_LISP ("blink-cursor-alist", &Vblink_cursor_alist,
24619 doc: /* Alist specifying how to blink the cursor off.
24620 Each element has the form (ON-STATE . OFF-STATE). Whenever the
24621 `cursor-type' frame-parameter or variable equals ON-STATE,
24622 comparing using `equal', Emacs uses OFF-STATE to specify
24623 how to blink it off. ON-STATE and OFF-STATE are values for
24624 the `cursor-type' frame parameter.
24625
24626 If a frame's ON-STATE has no entry in this list,
24627 the frame's other specifications determine how to blink the cursor off. */);
24628 Vblink_cursor_alist = Qnil;
24629
24630 DEFVAR_BOOL ("auto-hscroll-mode", &automatic_hscrolling_p,
24631 doc: /* *Non-nil means scroll the display automatically to make point visible. */);
24632 automatic_hscrolling_p = 1;
24633
24634 DEFVAR_INT ("hscroll-margin", &hscroll_margin,
24635 doc: /* *How many columns away from the window edge point is allowed to get
24636 before automatic hscrolling will horizontally scroll the window. */);
24637 hscroll_margin = 5;
24638
24639 DEFVAR_LISP ("hscroll-step", &Vhscroll_step,
24640 doc: /* *How many columns to scroll the window when point gets too close to the edge.
24641 When point is less than `hscroll-margin' columns from the window
24642 edge, automatic hscrolling will scroll the window by the amount of columns
24643 determined by this variable. If its value is a positive integer, scroll that
24644 many columns. If it's a positive floating-point number, it specifies the
24645 fraction of the window's width to scroll. If it's nil or zero, point will be
24646 centered horizontally after the scroll. Any other value, including negative
24647 numbers, are treated as if the value were zero.
24648
24649 Automatic hscrolling always moves point outside the scroll margin, so if
24650 point was more than scroll step columns inside the margin, the window will
24651 scroll more than the value given by the scroll step.
24652
24653 Note that the lower bound for automatic hscrolling specified by `scroll-left'
24654 and `scroll-right' overrides this variable's effect. */);
24655 Vhscroll_step = make_number (0);
24656
24657 DEFVAR_BOOL ("message-truncate-lines", &message_truncate_lines,
24658 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
24659 Bind this around calls to `message' to let it take effect. */);
24660 message_truncate_lines = 0;
24661
24662 DEFVAR_LISP ("menu-bar-update-hook", &Vmenu_bar_update_hook,
24663 doc: /* Normal hook run to update the menu bar definitions.
24664 Redisplay runs this hook before it redisplays the menu bar.
24665 This is used to update submenus such as Buffers,
24666 whose contents depend on various data. */);
24667 Vmenu_bar_update_hook = Qnil;
24668
24669 DEFVAR_LISP ("menu-updating-frame", &Vmenu_updating_frame,
24670 doc: /* Frame for which we are updating a menu.
24671 The enable predicate for a menu binding should check this variable. */);
24672 Vmenu_updating_frame = Qnil;
24673
24674 DEFVAR_BOOL ("inhibit-menubar-update", &inhibit_menubar_update,
24675 doc: /* Non-nil means don't update menu bars. Internal use only. */);
24676 inhibit_menubar_update = 0;
24677
24678 DEFVAR_BOOL ("inhibit-eval-during-redisplay", &inhibit_eval_during_redisplay,
24679 doc: /* Non-nil means don't eval Lisp during redisplay. */);
24680 inhibit_eval_during_redisplay = 0;
24681
24682 DEFVAR_BOOL ("inhibit-free-realized-faces", &inhibit_free_realized_faces,
24683 doc: /* Non-nil means don't free realized faces. Internal use only. */);
24684 inhibit_free_realized_faces = 0;
24685
24686 #if GLYPH_DEBUG
24687 DEFVAR_BOOL ("inhibit-try-window-id", &inhibit_try_window_id,
24688 doc: /* Inhibit try_window_id display optimization. */);
24689 inhibit_try_window_id = 0;
24690
24691 DEFVAR_BOOL ("inhibit-try-window-reusing", &inhibit_try_window_reusing,
24692 doc: /* Inhibit try_window_reusing display optimization. */);
24693 inhibit_try_window_reusing = 0;
24694
24695 DEFVAR_BOOL ("inhibit-try-cursor-movement", &inhibit_try_cursor_movement,
24696 doc: /* Inhibit try_cursor_movement display optimization. */);
24697 inhibit_try_cursor_movement = 0;
24698 #endif /* GLYPH_DEBUG */
24699
24700 DEFVAR_INT ("overline-margin", &overline_margin,
24701 doc: /* *Space between overline and text, in pixels.
24702 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
24703 margin to the caracter height. */);
24704 overline_margin = 2;
24705 }
24706
24707
24708 /* Initialize this module when Emacs starts. */
24709
24710 void
24711 init_xdisp ()
24712 {
24713 Lisp_Object root_window;
24714 struct window *mini_w;
24715
24716 current_header_line_height = current_mode_line_height = -1;
24717
24718 CHARPOS (this_line_start_pos) = 0;
24719
24720 mini_w = XWINDOW (minibuf_window);
24721 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
24722
24723 if (!noninteractive)
24724 {
24725 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
24726 int i;
24727
24728 XWINDOW (root_window)->top_line = make_number (FRAME_TOP_MARGIN (f));
24729 set_window_height (root_window,
24730 FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f),
24731 0);
24732 mini_w->top_line = make_number (FRAME_LINES (f) - 1);
24733 set_window_height (minibuf_window, 1, 0);
24734
24735 XWINDOW (root_window)->total_cols = make_number (FRAME_COLS (f));
24736 mini_w->total_cols = make_number (FRAME_COLS (f));
24737
24738 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
24739 scratch_glyph_row.glyphs[TEXT_AREA + 1]
24740 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
24741
24742 /* The default ellipsis glyphs `...'. */
24743 for (i = 0; i < 3; ++i)
24744 default_invis_vector[i] = make_number ('.');
24745 }
24746
24747 {
24748 /* Allocate the buffer for frame titles.
24749 Also used for `format-mode-line'. */
24750 int size = 100;
24751 mode_line_noprop_buf = (char *) xmalloc (size);
24752 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
24753 mode_line_noprop_ptr = mode_line_noprop_buf;
24754 mode_line_target = MODE_LINE_DISPLAY;
24755 }
24756
24757 help_echo_showing_p = 0;
24758 }
24759
24760
24761 /* arch-tag: eacc864d-bb6a-4b74-894a-1a4399a1358b
24762 (do not change this comment) */