]> 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, 2008 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, Lisp_Object,
967 struct text_pos *, 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 This sets HANDLED to HANDLED_RECOMPUTE_PROPS
3082 if it finds overlays. */
3083 if (handle_overlay_change_p)
3084 handled = handle_overlay_change (it);
3085 }
3086 }
3087 while (handled == HANDLED_RECOMPUTE_PROPS);
3088
3089 /* Determine where to stop next. */
3090 if (handled == HANDLED_NORMALLY)
3091 compute_stop_pos (it);
3092 }
3093
3094
3095 /* Compute IT->stop_charpos from text property and overlay change
3096 information for IT's current position. */
3097
3098 static void
3099 compute_stop_pos (it)
3100 struct it *it;
3101 {
3102 register INTERVAL iv, next_iv;
3103 Lisp_Object object, limit, position;
3104
3105 /* If nowhere else, stop at the end. */
3106 it->stop_charpos = it->end_charpos;
3107
3108 if (STRINGP (it->string))
3109 {
3110 /* Strings are usually short, so don't limit the search for
3111 properties. */
3112 object = it->string;
3113 limit = Qnil;
3114 position = make_number (IT_STRING_CHARPOS (*it));
3115 }
3116 else
3117 {
3118 int charpos;
3119
3120 /* If next overlay change is in front of the current stop pos
3121 (which is IT->end_charpos), stop there. Note: value of
3122 next_overlay_change is point-max if no overlay change
3123 follows. */
3124 charpos = next_overlay_change (IT_CHARPOS (*it));
3125 if (charpos < it->stop_charpos)
3126 it->stop_charpos = charpos;
3127
3128 /* If showing the region, we have to stop at the region
3129 start or end because the face might change there. */
3130 if (it->region_beg_charpos > 0)
3131 {
3132 if (IT_CHARPOS (*it) < it->region_beg_charpos)
3133 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
3134 else if (IT_CHARPOS (*it) < it->region_end_charpos)
3135 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
3136 }
3137
3138 /* Set up variables for computing the stop position from text
3139 property changes. */
3140 XSETBUFFER (object, current_buffer);
3141 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3142 position = make_number (IT_CHARPOS (*it));
3143
3144 }
3145
3146 /* Get the interval containing IT's position. Value is a null
3147 interval if there isn't such an interval. */
3148 iv = validate_interval_range (object, &position, &position, 0);
3149 if (!NULL_INTERVAL_P (iv))
3150 {
3151 Lisp_Object values_here[LAST_PROP_IDX];
3152 struct props *p;
3153
3154 /* Get properties here. */
3155 for (p = it_props; p->handler; ++p)
3156 values_here[p->idx] = textget (iv->plist, *p->name);
3157
3158 /* Look for an interval following iv that has different
3159 properties. */
3160 for (next_iv = next_interval (iv);
3161 (!NULL_INTERVAL_P (next_iv)
3162 && (NILP (limit)
3163 || XFASTINT (limit) > next_iv->position));
3164 next_iv = next_interval (next_iv))
3165 {
3166 for (p = it_props; p->handler; ++p)
3167 {
3168 Lisp_Object new_value;
3169
3170 new_value = textget (next_iv->plist, *p->name);
3171 if (!EQ (values_here[p->idx], new_value))
3172 break;
3173 }
3174
3175 if (p->handler)
3176 break;
3177 }
3178
3179 if (!NULL_INTERVAL_P (next_iv))
3180 {
3181 if (INTEGERP (limit)
3182 && next_iv->position >= XFASTINT (limit))
3183 /* No text property change up to limit. */
3184 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3185 else
3186 /* Text properties change in next_iv. */
3187 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3188 }
3189 }
3190
3191 xassert (STRINGP (it->string)
3192 || (it->stop_charpos >= BEGV
3193 && it->stop_charpos >= IT_CHARPOS (*it)));
3194 }
3195
3196
3197 /* Return the position of the next overlay change after POS in
3198 current_buffer. Value is point-max if no overlay change
3199 follows. This is like `next-overlay-change' but doesn't use
3200 xmalloc. */
3201
3202 static int
3203 next_overlay_change (pos)
3204 int pos;
3205 {
3206 int noverlays;
3207 int endpos;
3208 Lisp_Object *overlays;
3209 int i;
3210
3211 /* Get all overlays at the given position. */
3212 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
3213
3214 /* If any of these overlays ends before endpos,
3215 use its ending point instead. */
3216 for (i = 0; i < noverlays; ++i)
3217 {
3218 Lisp_Object oend;
3219 int oendpos;
3220
3221 oend = OVERLAY_END (overlays[i]);
3222 oendpos = OVERLAY_POSITION (oend);
3223 endpos = min (endpos, oendpos);
3224 }
3225
3226 return endpos;
3227 }
3228
3229
3230 \f
3231 /***********************************************************************
3232 Fontification
3233 ***********************************************************************/
3234
3235 /* Handle changes in the `fontified' property of the current buffer by
3236 calling hook functions from Qfontification_functions to fontify
3237 regions of text. */
3238
3239 static enum prop_handled
3240 handle_fontified_prop (it)
3241 struct it *it;
3242 {
3243 Lisp_Object prop, pos;
3244 enum prop_handled handled = HANDLED_NORMALLY;
3245
3246 if (!NILP (Vmemory_full))
3247 return handled;
3248
3249 /* Get the value of the `fontified' property at IT's current buffer
3250 position. (The `fontified' property doesn't have a special
3251 meaning in strings.) If the value is nil, call functions from
3252 Qfontification_functions. */
3253 if (!STRINGP (it->string)
3254 && it->s == NULL
3255 && !NILP (Vfontification_functions)
3256 && !NILP (Vrun_hooks)
3257 && (pos = make_number (IT_CHARPOS (*it)),
3258 prop = Fget_char_property (pos, Qfontified, Qnil),
3259 /* Ignore the special cased nil value always present at EOB since
3260 no amount of fontifying will be able to change it. */
3261 NILP (prop) && IT_CHARPOS (*it) < Z))
3262 {
3263 int count = SPECPDL_INDEX ();
3264 Lisp_Object val;
3265
3266 val = Vfontification_functions;
3267 specbind (Qfontification_functions, Qnil);
3268
3269 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3270 safe_call1 (val, pos);
3271 else
3272 {
3273 Lisp_Object globals, fn;
3274 struct gcpro gcpro1, gcpro2;
3275
3276 globals = Qnil;
3277 GCPRO2 (val, globals);
3278
3279 for (; CONSP (val); val = XCDR (val))
3280 {
3281 fn = XCAR (val);
3282
3283 if (EQ (fn, Qt))
3284 {
3285 /* A value of t indicates this hook has a local
3286 binding; it means to run the global binding too.
3287 In a global value, t should not occur. If it
3288 does, we must ignore it to avoid an endless
3289 loop. */
3290 for (globals = Fdefault_value (Qfontification_functions);
3291 CONSP (globals);
3292 globals = XCDR (globals))
3293 {
3294 fn = XCAR (globals);
3295 if (!EQ (fn, Qt))
3296 safe_call1 (fn, pos);
3297 }
3298 }
3299 else
3300 safe_call1 (fn, pos);
3301 }
3302
3303 UNGCPRO;
3304 }
3305
3306 unbind_to (count, Qnil);
3307
3308 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3309 something. This avoids an endless loop if they failed to
3310 fontify the text for which reason ever. */
3311 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3312 handled = HANDLED_RECOMPUTE_PROPS;
3313 }
3314
3315 return handled;
3316 }
3317
3318
3319 \f
3320 /***********************************************************************
3321 Faces
3322 ***********************************************************************/
3323
3324 /* Set up iterator IT from face properties at its current position.
3325 Called from handle_stop. */
3326
3327 static enum prop_handled
3328 handle_face_prop (it)
3329 struct it *it;
3330 {
3331 int new_face_id, next_stop;
3332
3333 if (!STRINGP (it->string))
3334 {
3335 new_face_id
3336 = face_at_buffer_position (it->w,
3337 IT_CHARPOS (*it),
3338 it->region_beg_charpos,
3339 it->region_end_charpos,
3340 &next_stop,
3341 (IT_CHARPOS (*it)
3342 + TEXT_PROP_DISTANCE_LIMIT),
3343 0);
3344
3345 /* Is this a start of a run of characters with box face?
3346 Caveat: this can be called for a freshly initialized
3347 iterator; face_id is -1 in this case. We know that the new
3348 face will not change until limit, i.e. if the new face has a
3349 box, all characters up to limit will have one. But, as
3350 usual, we don't know whether limit is really the end. */
3351 if (new_face_id != it->face_id)
3352 {
3353 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3354
3355 /* If new face has a box but old face has not, this is
3356 the start of a run of characters with box, i.e. it has
3357 a shadow on the left side. The value of face_id of the
3358 iterator will be -1 if this is the initial call that gets
3359 the face. In this case, we have to look in front of IT's
3360 position and see whether there is a face != new_face_id. */
3361 it->start_of_box_run_p
3362 = (new_face->box != FACE_NO_BOX
3363 && (it->face_id >= 0
3364 || IT_CHARPOS (*it) == BEG
3365 || new_face_id != face_before_it_pos (it)));
3366 it->face_box_p = new_face->box != FACE_NO_BOX;
3367 }
3368 }
3369 else
3370 {
3371 int base_face_id, bufpos;
3372 int i;
3373 Lisp_Object from_overlay
3374 = (it->current.overlay_string_index >= 0
3375 ? it->string_overlays[it->current.overlay_string_index]
3376 : Qnil);
3377
3378 /* See if we got to this string directly or indirectly from
3379 an overlay property. That includes the before-string or
3380 after-string of an overlay, strings in display properties
3381 provided by an overlay, their text properties, etc.
3382
3383 FROM_OVERLAY is the overlay that brought us here, or nil if none. */
3384 if (! NILP (from_overlay))
3385 for (i = it->sp - 1; i >= 0; i--)
3386 {
3387 if (it->stack[i].current.overlay_string_index >= 0)
3388 from_overlay
3389 = it->string_overlays[it->stack[i].current.overlay_string_index];
3390 else if (! NILP (it->stack[i].from_overlay))
3391 from_overlay = it->stack[i].from_overlay;
3392
3393 if (!NILP (from_overlay))
3394 break;
3395 }
3396
3397 if (! NILP (from_overlay))
3398 {
3399 bufpos = IT_CHARPOS (*it);
3400 /* For a string from an overlay, the base face depends
3401 only on text properties and ignores overlays. */
3402 base_face_id
3403 = face_for_overlay_string (it->w,
3404 IT_CHARPOS (*it),
3405 it->region_beg_charpos,
3406 it->region_end_charpos,
3407 &next_stop,
3408 (IT_CHARPOS (*it)
3409 + TEXT_PROP_DISTANCE_LIMIT),
3410 0,
3411 from_overlay);
3412 }
3413 else
3414 {
3415 bufpos = 0;
3416
3417 /* For strings from a `display' property, use the face at
3418 IT's current buffer position as the base face to merge
3419 with, so that overlay strings appear in the same face as
3420 surrounding text, unless they specify their own
3421 faces. */
3422 base_face_id = underlying_face_id (it);
3423 }
3424
3425 new_face_id = face_at_string_position (it->w,
3426 it->string,
3427 IT_STRING_CHARPOS (*it),
3428 bufpos,
3429 it->region_beg_charpos,
3430 it->region_end_charpos,
3431 &next_stop,
3432 base_face_id, 0);
3433
3434 #if 0 /* This shouldn't be neccessary. Let's check it. */
3435 /* If IT is used to display a mode line we would really like to
3436 use the mode line face instead of the frame's default face. */
3437 if (it->glyph_row == MATRIX_MODE_LINE_ROW (it->w->desired_matrix)
3438 && new_face_id == DEFAULT_FACE_ID)
3439 new_face_id = CURRENT_MODE_LINE_FACE_ID (it->w);
3440 #endif
3441
3442 /* Is this a start of a run of characters with box? Caveat:
3443 this can be called for a freshly allocated iterator; face_id
3444 is -1 is this case. We know that the new face will not
3445 change until the next check pos, i.e. if the new face has a
3446 box, all characters up to that position will have a
3447 box. But, as usual, we don't know whether that position
3448 is really the end. */
3449 if (new_face_id != it->face_id)
3450 {
3451 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3452 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3453
3454 /* If new face has a box but old face hasn't, this is the
3455 start of a run of characters with box, i.e. it has a
3456 shadow on the left side. */
3457 it->start_of_box_run_p
3458 = new_face->box && (old_face == NULL || !old_face->box);
3459 it->face_box_p = new_face->box != FACE_NO_BOX;
3460 }
3461 }
3462
3463 it->face_id = new_face_id;
3464 return HANDLED_NORMALLY;
3465 }
3466
3467
3468 /* Return the ID of the face ``underlying'' IT's current position,
3469 which is in a string. If the iterator is associated with a
3470 buffer, return the face at IT's current buffer position.
3471 Otherwise, use the iterator's base_face_id. */
3472
3473 static int
3474 underlying_face_id (it)
3475 struct it *it;
3476 {
3477 int face_id = it->base_face_id, i;
3478
3479 xassert (STRINGP (it->string));
3480
3481 for (i = it->sp - 1; i >= 0; --i)
3482 if (NILP (it->stack[i].string))
3483 face_id = it->stack[i].face_id;
3484
3485 return face_id;
3486 }
3487
3488
3489 /* Compute the face one character before or after the current position
3490 of IT. BEFORE_P non-zero means get the face in front of IT's
3491 position. Value is the id of the face. */
3492
3493 static int
3494 face_before_or_after_it_pos (it, before_p)
3495 struct it *it;
3496 int before_p;
3497 {
3498 int face_id, limit;
3499 int next_check_charpos;
3500 struct text_pos pos;
3501
3502 xassert (it->s == NULL);
3503
3504 if (STRINGP (it->string))
3505 {
3506 int bufpos, base_face_id;
3507
3508 /* No face change past the end of the string (for the case
3509 we are padding with spaces). No face change before the
3510 string start. */
3511 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
3512 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
3513 return it->face_id;
3514
3515 /* Set pos to the position before or after IT's current position. */
3516 if (before_p)
3517 pos = string_pos (IT_STRING_CHARPOS (*it) - 1, it->string);
3518 else
3519 /* For composition, we must check the character after the
3520 composition. */
3521 pos = (it->what == IT_COMPOSITION
3522 ? string_pos (IT_STRING_CHARPOS (*it) + it->cmp_len, it->string)
3523 : string_pos (IT_STRING_CHARPOS (*it) + 1, it->string));
3524
3525 if (it->current.overlay_string_index >= 0)
3526 bufpos = IT_CHARPOS (*it);
3527 else
3528 bufpos = 0;
3529
3530 base_face_id = underlying_face_id (it);
3531
3532 /* Get the face for ASCII, or unibyte. */
3533 face_id = face_at_string_position (it->w,
3534 it->string,
3535 CHARPOS (pos),
3536 bufpos,
3537 it->region_beg_charpos,
3538 it->region_end_charpos,
3539 &next_check_charpos,
3540 base_face_id, 0);
3541
3542 /* Correct the face for charsets different from ASCII. Do it
3543 for the multibyte case only. The face returned above is
3544 suitable for unibyte text if IT->string is unibyte. */
3545 if (STRING_MULTIBYTE (it->string))
3546 {
3547 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos);
3548 int rest = SBYTES (it->string) - BYTEPOS (pos);
3549 int c, len;
3550 struct face *face = FACE_FROM_ID (it->f, face_id);
3551
3552 c = string_char_and_length (p, rest, &len);
3553 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), it->string);
3554 }
3555 }
3556 else
3557 {
3558 if ((IT_CHARPOS (*it) >= ZV && !before_p)
3559 || (IT_CHARPOS (*it) <= BEGV && before_p))
3560 return it->face_id;
3561
3562 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
3563 pos = it->current.pos;
3564
3565 if (before_p)
3566 DEC_TEXT_POS (pos, it->multibyte_p);
3567 else
3568 {
3569 if (it->what == IT_COMPOSITION)
3570 /* For composition, we must check the position after the
3571 composition. */
3572 pos.charpos += it->cmp_len, pos.bytepos += it->len;
3573 else
3574 INC_TEXT_POS (pos, it->multibyte_p);
3575 }
3576
3577 /* Determine face for CHARSET_ASCII, or unibyte. */
3578 face_id = face_at_buffer_position (it->w,
3579 CHARPOS (pos),
3580 it->region_beg_charpos,
3581 it->region_end_charpos,
3582 &next_check_charpos,
3583 limit, 0);
3584
3585 /* Correct the face for charsets different from ASCII. Do it
3586 for the multibyte case only. The face returned above is
3587 suitable for unibyte text if current_buffer is unibyte. */
3588 if (it->multibyte_p)
3589 {
3590 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
3591 struct face *face = FACE_FROM_ID (it->f, face_id);
3592 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), Qnil);
3593 }
3594 }
3595
3596 return face_id;
3597 }
3598
3599
3600 \f
3601 /***********************************************************************
3602 Invisible text
3603 ***********************************************************************/
3604
3605 /* Set up iterator IT from invisible properties at its current
3606 position. Called from handle_stop. */
3607
3608 static enum prop_handled
3609 handle_invisible_prop (it)
3610 struct it *it;
3611 {
3612 enum prop_handled handled = HANDLED_NORMALLY;
3613
3614 if (STRINGP (it->string))
3615 {
3616 extern Lisp_Object Qinvisible;
3617 Lisp_Object prop, end_charpos, limit, charpos;
3618
3619 /* Get the value of the invisible text property at the
3620 current position. Value will be nil if there is no such
3621 property. */
3622 charpos = make_number (IT_STRING_CHARPOS (*it));
3623 prop = Fget_text_property (charpos, Qinvisible, it->string);
3624
3625 if (!NILP (prop)
3626 && IT_STRING_CHARPOS (*it) < it->end_charpos)
3627 {
3628 handled = HANDLED_RECOMPUTE_PROPS;
3629
3630 /* Get the position at which the next change of the
3631 invisible text property can be found in IT->string.
3632 Value will be nil if the property value is the same for
3633 all the rest of IT->string. */
3634 XSETINT (limit, SCHARS (it->string));
3635 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
3636 it->string, limit);
3637
3638 /* Text at current position is invisible. The next
3639 change in the property is at position end_charpos.
3640 Move IT's current position to that position. */
3641 if (INTEGERP (end_charpos)
3642 && XFASTINT (end_charpos) < XFASTINT (limit))
3643 {
3644 struct text_pos old;
3645 old = it->current.string_pos;
3646 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
3647 compute_string_pos (&it->current.string_pos, old, it->string);
3648 }
3649 else
3650 {
3651 /* The rest of the string is invisible. If this is an
3652 overlay string, proceed with the next overlay string
3653 or whatever comes and return a character from there. */
3654 if (it->current.overlay_string_index >= 0)
3655 {
3656 next_overlay_string (it);
3657 /* Don't check for overlay strings when we just
3658 finished processing them. */
3659 handled = HANDLED_OVERLAY_STRING_CONSUMED;
3660 }
3661 else
3662 {
3663 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
3664 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
3665 }
3666 }
3667 }
3668 }
3669 else
3670 {
3671 int invis_p;
3672 EMACS_INT newpos, next_stop, start_charpos;
3673 Lisp_Object pos, prop, overlay;
3674
3675 /* First of all, is there invisible text at this position? */
3676 start_charpos = IT_CHARPOS (*it);
3677 pos = make_number (IT_CHARPOS (*it));
3678 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
3679 &overlay);
3680 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3681
3682 /* If we are on invisible text, skip over it. */
3683 if (invis_p && IT_CHARPOS (*it) < it->end_charpos)
3684 {
3685 /* Record whether we have to display an ellipsis for the
3686 invisible text. */
3687 int display_ellipsis_p = invis_p == 2;
3688
3689 handled = HANDLED_RECOMPUTE_PROPS;
3690
3691 /* Loop skipping over invisible text. The loop is left at
3692 ZV or with IT on the first char being visible again. */
3693 do
3694 {
3695 /* Try to skip some invisible text. Return value is the
3696 position reached which can be equal to IT's position
3697 if there is nothing invisible here. This skips both
3698 over invisible text properties and overlays with
3699 invisible property. */
3700 newpos = skip_invisible (IT_CHARPOS (*it),
3701 &next_stop, ZV, it->window);
3702
3703 /* If we skipped nothing at all we weren't at invisible
3704 text in the first place. If everything to the end of
3705 the buffer was skipped, end the loop. */
3706 if (newpos == IT_CHARPOS (*it) || newpos >= ZV)
3707 invis_p = 0;
3708 else
3709 {
3710 /* We skipped some characters but not necessarily
3711 all there are. Check if we ended up on visible
3712 text. Fget_char_property returns the property of
3713 the char before the given position, i.e. if we
3714 get invis_p = 0, this means that the char at
3715 newpos is visible. */
3716 pos = make_number (newpos);
3717 prop = Fget_char_property (pos, Qinvisible, it->window);
3718 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3719 }
3720
3721 /* If we ended up on invisible text, proceed to
3722 skip starting with next_stop. */
3723 if (invis_p)
3724 IT_CHARPOS (*it) = next_stop;
3725
3726 /* If there are adjacent invisible texts, don't lose the
3727 second one's ellipsis. */
3728 if (invis_p == 2)
3729 display_ellipsis_p = 1;
3730 }
3731 while (invis_p);
3732
3733 /* The position newpos is now either ZV or on visible text. */
3734 IT_CHARPOS (*it) = newpos;
3735 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
3736
3737 /* If there are before-strings at the start of invisible
3738 text, and the text is invisible because of a text
3739 property, arrange to show before-strings because 20.x did
3740 it that way. (If the text is invisible because of an
3741 overlay property instead of a text property, this is
3742 already handled in the overlay code.) */
3743 if (NILP (overlay)
3744 && get_overlay_strings (it, start_charpos))
3745 {
3746 handled = HANDLED_RECOMPUTE_PROPS;
3747 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
3748 }
3749 else if (display_ellipsis_p)
3750 {
3751 /* Make sure that the glyphs of the ellipsis will get
3752 correct `charpos' values. If we would not update
3753 it->position here, the glyphs would belong to the
3754 last visible character _before_ the invisible
3755 text, which confuses `set_cursor_from_row'.
3756
3757 We use the last invisible position instead of the
3758 first because this way the cursor is always drawn on
3759 the first "." of the ellipsis, whenever PT is inside
3760 the invisible text. Otherwise the cursor would be
3761 placed _after_ the ellipsis when the point is after the
3762 first invisible character. */
3763 if (!STRINGP (it->object))
3764 {
3765 it->position.charpos = IT_CHARPOS (*it) - 1;
3766 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
3767 }
3768 setup_for_ellipsis (it, 0);
3769 /* Let the ellipsis display before
3770 considering any properties of the following char.
3771 Fixes jasonr@gnu.org 01 Oct 07 bug. */
3772 handled = HANDLED_RETURN;
3773 }
3774 }
3775 }
3776
3777 return handled;
3778 }
3779
3780
3781 /* Make iterator IT return `...' next.
3782 Replaces LEN characters from buffer. */
3783
3784 static void
3785 setup_for_ellipsis (it, len)
3786 struct it *it;
3787 int len;
3788 {
3789 /* Use the display table definition for `...'. Invalid glyphs
3790 will be handled by the method returning elements from dpvec. */
3791 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
3792 {
3793 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
3794 it->dpvec = v->contents;
3795 it->dpend = v->contents + v->size;
3796 }
3797 else
3798 {
3799 /* Default `...'. */
3800 it->dpvec = default_invis_vector;
3801 it->dpend = default_invis_vector + 3;
3802 }
3803
3804 it->dpvec_char_len = len;
3805 it->current.dpvec_index = 0;
3806 it->dpvec_face_id = -1;
3807
3808 /* Remember the current face id in case glyphs specify faces.
3809 IT's face is restored in set_iterator_to_next.
3810 saved_face_id was set to preceding char's face in handle_stop. */
3811 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
3812 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
3813
3814 it->method = GET_FROM_DISPLAY_VECTOR;
3815 it->ellipsis_p = 1;
3816 }
3817
3818
3819 \f
3820 /***********************************************************************
3821 'display' property
3822 ***********************************************************************/
3823
3824 /* Set up iterator IT from `display' property at its current position.
3825 Called from handle_stop.
3826 We return HANDLED_RETURN if some part of the display property
3827 overrides the display of the buffer text itself.
3828 Otherwise we return HANDLED_NORMALLY. */
3829
3830 static enum prop_handled
3831 handle_display_prop (it)
3832 struct it *it;
3833 {
3834 Lisp_Object prop, object, overlay;
3835 struct text_pos *position;
3836 /* Nonzero if some property replaces the display of the text itself. */
3837 int display_replaced_p = 0;
3838
3839 if (STRINGP (it->string))
3840 {
3841 object = it->string;
3842 position = &it->current.string_pos;
3843 }
3844 else
3845 {
3846 XSETWINDOW (object, it->w);
3847 position = &it->current.pos;
3848 }
3849
3850 /* Reset those iterator values set from display property values. */
3851 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
3852 it->space_width = Qnil;
3853 it->font_height = Qnil;
3854 it->voffset = 0;
3855
3856 /* We don't support recursive `display' properties, i.e. string
3857 values that have a string `display' property, that have a string
3858 `display' property etc. */
3859 if (!it->string_from_display_prop_p)
3860 it->area = TEXT_AREA;
3861
3862 prop = get_char_property_and_overlay (make_number (position->charpos),
3863 Qdisplay, object, &overlay);
3864 if (NILP (prop))
3865 return HANDLED_NORMALLY;
3866 /* Now OVERLAY is the overlay that gave us this property, or nil
3867 if it was a text property. */
3868
3869 if (!STRINGP (it->string))
3870 object = it->w->buffer;
3871
3872 if (CONSP (prop)
3873 /* Simple properties. */
3874 && !EQ (XCAR (prop), Qimage)
3875 && !EQ (XCAR (prop), Qspace)
3876 && !EQ (XCAR (prop), Qwhen)
3877 && !EQ (XCAR (prop), Qslice)
3878 && !EQ (XCAR (prop), Qspace_width)
3879 && !EQ (XCAR (prop), Qheight)
3880 && !EQ (XCAR (prop), Qraise)
3881 /* Marginal area specifications. */
3882 && !(CONSP (XCAR (prop)) && EQ (XCAR (XCAR (prop)), Qmargin))
3883 && !EQ (XCAR (prop), Qleft_fringe)
3884 && !EQ (XCAR (prop), Qright_fringe)
3885 && !NILP (XCAR (prop)))
3886 {
3887 for (; CONSP (prop); prop = XCDR (prop))
3888 {
3889 if (handle_single_display_spec (it, XCAR (prop), object, overlay,
3890 position, display_replaced_p))
3891 {
3892 display_replaced_p = 1;
3893 /* If some text in a string is replaced, `position' no
3894 longer points to the position of `object'. */
3895 if (STRINGP (object))
3896 break;
3897 }
3898 }
3899 }
3900 else if (VECTORP (prop))
3901 {
3902 int i;
3903 for (i = 0; i < ASIZE (prop); ++i)
3904 if (handle_single_display_spec (it, AREF (prop, i), object, overlay,
3905 position, display_replaced_p))
3906 {
3907 display_replaced_p = 1;
3908 /* If some text in a string is replaced, `position' no
3909 longer points to the position of `object'. */
3910 if (STRINGP (object))
3911 break;
3912 }
3913 }
3914 else
3915 {
3916 int ret = handle_single_display_spec (it, prop, object, overlay,
3917 position, 0);
3918 if (ret < 0) /* Replaced by "", i.e. nothing. */
3919 return HANDLED_RECOMPUTE_PROPS;
3920 if (ret)
3921 display_replaced_p = 1;
3922 }
3923
3924 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
3925 }
3926
3927
3928 /* Value is the position of the end of the `display' property starting
3929 at START_POS in OBJECT. */
3930
3931 static struct text_pos
3932 display_prop_end (it, object, start_pos)
3933 struct it *it;
3934 Lisp_Object object;
3935 struct text_pos start_pos;
3936 {
3937 Lisp_Object end;
3938 struct text_pos end_pos;
3939
3940 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
3941 Qdisplay, object, Qnil);
3942 CHARPOS (end_pos) = XFASTINT (end);
3943 if (STRINGP (object))
3944 compute_string_pos (&end_pos, start_pos, it->string);
3945 else
3946 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
3947
3948 return end_pos;
3949 }
3950
3951
3952 /* Set up IT from a single `display' specification PROP. OBJECT
3953 is the object in which the `display' property was found. *POSITION
3954 is the position at which it was found. DISPLAY_REPLACED_P non-zero
3955 means that we previously saw a display specification which already
3956 replaced text display with something else, for example an image;
3957 we ignore such properties after the first one has been processed.
3958
3959 OVERLAY is the overlay this `display' property came from,
3960 or nil if it was a text property.
3961
3962 If PROP is a `space' or `image' specification, and in some other
3963 cases too, set *POSITION to the position where the `display'
3964 property ends.
3965
3966 Value is non-zero if something was found which replaces the display
3967 of buffer or string text. Specifically, the value is -1 if that
3968 "something" is "nothing". */
3969
3970 static int
3971 handle_single_display_spec (it, spec, object, overlay, position,
3972 display_replaced_before_p)
3973 struct it *it;
3974 Lisp_Object spec;
3975 Lisp_Object object;
3976 Lisp_Object overlay;
3977 struct text_pos *position;
3978 int display_replaced_before_p;
3979 {
3980 Lisp_Object form;
3981 Lisp_Object location, value;
3982 struct text_pos start_pos, save_pos;
3983 int valid_p;
3984
3985 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
3986 If the result is non-nil, use VALUE instead of SPEC. */
3987 form = Qt;
3988 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
3989 {
3990 spec = XCDR (spec);
3991 if (!CONSP (spec))
3992 return 0;
3993 form = XCAR (spec);
3994 spec = XCDR (spec);
3995 }
3996
3997 if (!NILP (form) && !EQ (form, Qt))
3998 {
3999 int count = SPECPDL_INDEX ();
4000 struct gcpro gcpro1;
4001
4002 /* Bind `object' to the object having the `display' property, a
4003 buffer or string. Bind `position' to the position in the
4004 object where the property was found, and `buffer-position'
4005 to the current position in the buffer. */
4006 specbind (Qobject, object);
4007 specbind (Qposition, make_number (CHARPOS (*position)));
4008 specbind (Qbuffer_position,
4009 make_number (STRINGP (object)
4010 ? IT_CHARPOS (*it) : CHARPOS (*position)));
4011 GCPRO1 (form);
4012 form = safe_eval (form);
4013 UNGCPRO;
4014 unbind_to (count, Qnil);
4015 }
4016
4017 if (NILP (form))
4018 return 0;
4019
4020 /* Handle `(height HEIGHT)' specifications. */
4021 if (CONSP (spec)
4022 && EQ (XCAR (spec), Qheight)
4023 && CONSP (XCDR (spec)))
4024 {
4025 if (!FRAME_WINDOW_P (it->f))
4026 return 0;
4027
4028 it->font_height = XCAR (XCDR (spec));
4029 if (!NILP (it->font_height))
4030 {
4031 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4032 int new_height = -1;
4033
4034 if (CONSP (it->font_height)
4035 && (EQ (XCAR (it->font_height), Qplus)
4036 || EQ (XCAR (it->font_height), Qminus))
4037 && CONSP (XCDR (it->font_height))
4038 && INTEGERP (XCAR (XCDR (it->font_height))))
4039 {
4040 /* `(+ N)' or `(- N)' where N is an integer. */
4041 int steps = XINT (XCAR (XCDR (it->font_height)));
4042 if (EQ (XCAR (it->font_height), Qplus))
4043 steps = - steps;
4044 it->face_id = smaller_face (it->f, it->face_id, steps);
4045 }
4046 else if (FUNCTIONP (it->font_height))
4047 {
4048 /* Call function with current height as argument.
4049 Value is the new height. */
4050 Lisp_Object height;
4051 height = safe_call1 (it->font_height,
4052 face->lface[LFACE_HEIGHT_INDEX]);
4053 if (NUMBERP (height))
4054 new_height = XFLOATINT (height);
4055 }
4056 else if (NUMBERP (it->font_height))
4057 {
4058 /* Value is a multiple of the canonical char height. */
4059 struct face *face;
4060
4061 face = FACE_FROM_ID (it->f, DEFAULT_FACE_ID);
4062 new_height = (XFLOATINT (it->font_height)
4063 * XINT (face->lface[LFACE_HEIGHT_INDEX]));
4064 }
4065 else
4066 {
4067 /* Evaluate IT->font_height with `height' bound to the
4068 current specified height to get the new height. */
4069 int count = SPECPDL_INDEX ();
4070
4071 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4072 value = safe_eval (it->font_height);
4073 unbind_to (count, Qnil);
4074
4075 if (NUMBERP (value))
4076 new_height = XFLOATINT (value);
4077 }
4078
4079 if (new_height > 0)
4080 it->face_id = face_with_height (it->f, it->face_id, new_height);
4081 }
4082
4083 return 0;
4084 }
4085
4086 /* Handle `(space-width WIDTH)'. */
4087 if (CONSP (spec)
4088 && EQ (XCAR (spec), Qspace_width)
4089 && CONSP (XCDR (spec)))
4090 {
4091 if (!FRAME_WINDOW_P (it->f))
4092 return 0;
4093
4094 value = XCAR (XCDR (spec));
4095 if (NUMBERP (value) && XFLOATINT (value) > 0)
4096 it->space_width = value;
4097
4098 return 0;
4099 }
4100
4101 /* Handle `(slice X Y WIDTH HEIGHT)'. */
4102 if (CONSP (spec)
4103 && EQ (XCAR (spec), Qslice))
4104 {
4105 Lisp_Object tem;
4106
4107 if (!FRAME_WINDOW_P (it->f))
4108 return 0;
4109
4110 if (tem = XCDR (spec), CONSP (tem))
4111 {
4112 it->slice.x = XCAR (tem);
4113 if (tem = XCDR (tem), CONSP (tem))
4114 {
4115 it->slice.y = XCAR (tem);
4116 if (tem = XCDR (tem), CONSP (tem))
4117 {
4118 it->slice.width = XCAR (tem);
4119 if (tem = XCDR (tem), CONSP (tem))
4120 it->slice.height = XCAR (tem);
4121 }
4122 }
4123 }
4124
4125 return 0;
4126 }
4127
4128 /* Handle `(raise FACTOR)'. */
4129 if (CONSP (spec)
4130 && EQ (XCAR (spec), Qraise)
4131 && CONSP (XCDR (spec)))
4132 {
4133 if (!FRAME_WINDOW_P (it->f))
4134 return 0;
4135
4136 #ifdef HAVE_WINDOW_SYSTEM
4137 value = XCAR (XCDR (spec));
4138 if (NUMBERP (value))
4139 {
4140 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4141 it->voffset = - (XFLOATINT (value)
4142 * (FONT_HEIGHT (face->font)));
4143 }
4144 #endif /* HAVE_WINDOW_SYSTEM */
4145
4146 return 0;
4147 }
4148
4149 /* Don't handle the other kinds of display specifications
4150 inside a string that we got from a `display' property. */
4151 if (it->string_from_display_prop_p)
4152 return 0;
4153
4154 /* Characters having this form of property are not displayed, so
4155 we have to find the end of the property. */
4156 start_pos = *position;
4157 *position = display_prop_end (it, object, start_pos);
4158 value = Qnil;
4159
4160 /* Stop the scan at that end position--we assume that all
4161 text properties change there. */
4162 it->stop_charpos = position->charpos;
4163
4164 /* Handle `(left-fringe BITMAP [FACE])'
4165 and `(right-fringe BITMAP [FACE])'. */
4166 if (CONSP (spec)
4167 && (EQ (XCAR (spec), Qleft_fringe)
4168 || EQ (XCAR (spec), Qright_fringe))
4169 && CONSP (XCDR (spec)))
4170 {
4171 int face_id = DEFAULT_FACE_ID;
4172 int fringe_bitmap;
4173
4174 if (!FRAME_WINDOW_P (it->f))
4175 /* If we return here, POSITION has been advanced
4176 across the text with this property. */
4177 return 0;
4178
4179 #ifdef HAVE_WINDOW_SYSTEM
4180 value = XCAR (XCDR (spec));
4181 if (!SYMBOLP (value)
4182 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
4183 /* If we return here, POSITION has been advanced
4184 across the text with this property. */
4185 return 0;
4186
4187 if (CONSP (XCDR (XCDR (spec))))
4188 {
4189 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
4190 int face_id2 = lookup_derived_face (it->f, face_name,
4191 FRINGE_FACE_ID, 0);
4192 if (face_id2 >= 0)
4193 face_id = face_id2;
4194 }
4195
4196 /* Save current settings of IT so that we can restore them
4197 when we are finished with the glyph property value. */
4198
4199 save_pos = it->position;
4200 it->position = *position;
4201 push_it (it);
4202 it->position = save_pos;
4203
4204 it->area = TEXT_AREA;
4205 it->what = IT_IMAGE;
4206 it->image_id = -1; /* no image */
4207 it->position = start_pos;
4208 it->object = NILP (object) ? it->w->buffer : object;
4209 it->method = GET_FROM_IMAGE;
4210 it->from_overlay = Qnil;
4211 it->face_id = face_id;
4212
4213 /* Say that we haven't consumed the characters with
4214 `display' property yet. The call to pop_it in
4215 set_iterator_to_next will clean this up. */
4216 *position = start_pos;
4217
4218 if (EQ (XCAR (spec), Qleft_fringe))
4219 {
4220 it->left_user_fringe_bitmap = fringe_bitmap;
4221 it->left_user_fringe_face_id = face_id;
4222 }
4223 else
4224 {
4225 it->right_user_fringe_bitmap = fringe_bitmap;
4226 it->right_user_fringe_face_id = face_id;
4227 }
4228 #endif /* HAVE_WINDOW_SYSTEM */
4229 return 1;
4230 }
4231
4232 /* Prepare to handle `((margin left-margin) ...)',
4233 `((margin right-margin) ...)' and `((margin nil) ...)'
4234 prefixes for display specifications. */
4235 location = Qunbound;
4236 if (CONSP (spec) && CONSP (XCAR (spec)))
4237 {
4238 Lisp_Object tem;
4239
4240 value = XCDR (spec);
4241 if (CONSP (value))
4242 value = XCAR (value);
4243
4244 tem = XCAR (spec);
4245 if (EQ (XCAR (tem), Qmargin)
4246 && (tem = XCDR (tem),
4247 tem = CONSP (tem) ? XCAR (tem) : Qnil,
4248 (NILP (tem)
4249 || EQ (tem, Qleft_margin)
4250 || EQ (tem, Qright_margin))))
4251 location = tem;
4252 }
4253
4254 if (EQ (location, Qunbound))
4255 {
4256 location = Qnil;
4257 value = spec;
4258 }
4259
4260 /* After this point, VALUE is the property after any
4261 margin prefix has been stripped. It must be a string,
4262 an image specification, or `(space ...)'.
4263
4264 LOCATION specifies where to display: `left-margin',
4265 `right-margin' or nil. */
4266
4267 valid_p = (STRINGP (value)
4268 #ifdef HAVE_WINDOW_SYSTEM
4269 || (FRAME_WINDOW_P (it->f) && valid_image_p (value))
4270 #endif /* not HAVE_WINDOW_SYSTEM */
4271 || (CONSP (value) && EQ (XCAR (value), Qspace)));
4272
4273 if (valid_p && !display_replaced_before_p)
4274 {
4275 /* Save current settings of IT so that we can restore them
4276 when we are finished with the glyph property value. */
4277 save_pos = it->position;
4278 it->position = *position;
4279 push_it (it);
4280 it->position = save_pos;
4281 it->from_overlay = overlay;
4282
4283 if (NILP (location))
4284 it->area = TEXT_AREA;
4285 else if (EQ (location, Qleft_margin))
4286 it->area = LEFT_MARGIN_AREA;
4287 else
4288 it->area = RIGHT_MARGIN_AREA;
4289
4290 if (STRINGP (value))
4291 {
4292 if (SCHARS (value) == 0)
4293 {
4294 pop_it (it);
4295 return -1; /* Replaced by "", i.e. nothing. */
4296 }
4297 it->string = value;
4298 it->multibyte_p = STRING_MULTIBYTE (it->string);
4299 it->current.overlay_string_index = -1;
4300 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4301 it->end_charpos = it->string_nchars = SCHARS (it->string);
4302 it->method = GET_FROM_STRING;
4303 it->stop_charpos = 0;
4304 it->string_from_display_prop_p = 1;
4305 /* Say that we haven't consumed the characters with
4306 `display' property yet. The call to pop_it in
4307 set_iterator_to_next will clean this up. */
4308 if (BUFFERP (object))
4309 it->current.pos = start_pos;
4310 }
4311 else if (CONSP (value) && EQ (XCAR (value), Qspace))
4312 {
4313 it->method = GET_FROM_STRETCH;
4314 it->object = value;
4315 it->position = start_pos;
4316 if (BUFFERP (object))
4317 it->current.pos = start_pos;
4318 }
4319 #ifdef HAVE_WINDOW_SYSTEM
4320 else
4321 {
4322 it->what = IT_IMAGE;
4323 it->image_id = lookup_image (it->f, value);
4324 it->position = start_pos;
4325 it->object = NILP (object) ? it->w->buffer : object;
4326 it->method = GET_FROM_IMAGE;
4327
4328 /* Say that we haven't consumed the characters with
4329 `display' property yet. The call to pop_it in
4330 set_iterator_to_next will clean this up. */
4331 if (BUFFERP (object))
4332 it->current.pos = start_pos;
4333 }
4334 #endif /* HAVE_WINDOW_SYSTEM */
4335
4336 return 1;
4337 }
4338
4339 /* Invalid property or property not supported. Restore
4340 POSITION to what it was before. */
4341 *position = start_pos;
4342 return 0;
4343 }
4344
4345
4346 /* Check if SPEC is a display sub-property value whose text should be
4347 treated as intangible. */
4348
4349 static int
4350 single_display_spec_intangible_p (prop)
4351 Lisp_Object prop;
4352 {
4353 /* Skip over `when FORM'. */
4354 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
4355 {
4356 prop = XCDR (prop);
4357 if (!CONSP (prop))
4358 return 0;
4359 prop = XCDR (prop);
4360 }
4361
4362 if (STRINGP (prop))
4363 return 1;
4364
4365 if (!CONSP (prop))
4366 return 0;
4367
4368 /* Skip over `margin LOCATION'. If LOCATION is in the margins,
4369 we don't need to treat text as intangible. */
4370 if (EQ (XCAR (prop), Qmargin))
4371 {
4372 prop = XCDR (prop);
4373 if (!CONSP (prop))
4374 return 0;
4375
4376 prop = XCDR (prop);
4377 if (!CONSP (prop)
4378 || EQ (XCAR (prop), Qleft_margin)
4379 || EQ (XCAR (prop), Qright_margin))
4380 return 0;
4381 }
4382
4383 return (CONSP (prop)
4384 && (EQ (XCAR (prop), Qimage)
4385 || EQ (XCAR (prop), Qspace)));
4386 }
4387
4388
4389 /* Check if PROP is a display property value whose text should be
4390 treated as intangible. */
4391
4392 int
4393 display_prop_intangible_p (prop)
4394 Lisp_Object prop;
4395 {
4396 if (CONSP (prop)
4397 && CONSP (XCAR (prop))
4398 && !EQ (Qmargin, XCAR (XCAR (prop))))
4399 {
4400 /* A list of sub-properties. */
4401 while (CONSP (prop))
4402 {
4403 if (single_display_spec_intangible_p (XCAR (prop)))
4404 return 1;
4405 prop = XCDR (prop);
4406 }
4407 }
4408 else if (VECTORP (prop))
4409 {
4410 /* A vector of sub-properties. */
4411 int i;
4412 for (i = 0; i < ASIZE (prop); ++i)
4413 if (single_display_spec_intangible_p (AREF (prop, i)))
4414 return 1;
4415 }
4416 else
4417 return single_display_spec_intangible_p (prop);
4418
4419 return 0;
4420 }
4421
4422
4423 /* Return 1 if PROP is a display sub-property value containing STRING. */
4424
4425 static int
4426 single_display_spec_string_p (prop, string)
4427 Lisp_Object prop, string;
4428 {
4429 if (EQ (string, prop))
4430 return 1;
4431
4432 /* Skip over `when FORM'. */
4433 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
4434 {
4435 prop = XCDR (prop);
4436 if (!CONSP (prop))
4437 return 0;
4438 prop = XCDR (prop);
4439 }
4440
4441 if (CONSP (prop))
4442 /* Skip over `margin LOCATION'. */
4443 if (EQ (XCAR (prop), Qmargin))
4444 {
4445 prop = XCDR (prop);
4446 if (!CONSP (prop))
4447 return 0;
4448
4449 prop = XCDR (prop);
4450 if (!CONSP (prop))
4451 return 0;
4452 }
4453
4454 return CONSP (prop) && EQ (XCAR (prop), string);
4455 }
4456
4457
4458 /* Return 1 if STRING appears in the `display' property PROP. */
4459
4460 static int
4461 display_prop_string_p (prop, string)
4462 Lisp_Object prop, string;
4463 {
4464 if (CONSP (prop)
4465 && CONSP (XCAR (prop))
4466 && !EQ (Qmargin, XCAR (XCAR (prop))))
4467 {
4468 /* A list of sub-properties. */
4469 while (CONSP (prop))
4470 {
4471 if (single_display_spec_string_p (XCAR (prop), string))
4472 return 1;
4473 prop = XCDR (prop);
4474 }
4475 }
4476 else if (VECTORP (prop))
4477 {
4478 /* A vector of sub-properties. */
4479 int i;
4480 for (i = 0; i < ASIZE (prop); ++i)
4481 if (single_display_spec_string_p (AREF (prop, i), string))
4482 return 1;
4483 }
4484 else
4485 return single_display_spec_string_p (prop, string);
4486
4487 return 0;
4488 }
4489
4490
4491 /* Determine from which buffer position in W's buffer STRING comes
4492 from. AROUND_CHARPOS is an approximate position where it could
4493 be from. Value is the buffer position or 0 if it couldn't be
4494 determined.
4495
4496 W's buffer must be current.
4497
4498 This function is necessary because we don't record buffer positions
4499 in glyphs generated from strings (to keep struct glyph small).
4500 This function may only use code that doesn't eval because it is
4501 called asynchronously from note_mouse_highlight. */
4502
4503 int
4504 string_buffer_position (w, string, around_charpos)
4505 struct window *w;
4506 Lisp_Object string;
4507 int around_charpos;
4508 {
4509 Lisp_Object limit, prop, pos;
4510 const int MAX_DISTANCE = 1000;
4511 int found = 0;
4512
4513 pos = make_number (around_charpos);
4514 limit = make_number (min (XINT (pos) + MAX_DISTANCE, ZV));
4515 while (!found && !EQ (pos, limit))
4516 {
4517 prop = Fget_char_property (pos, Qdisplay, Qnil);
4518 if (!NILP (prop) && display_prop_string_p (prop, string))
4519 found = 1;
4520 else
4521 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil, limit);
4522 }
4523
4524 if (!found)
4525 {
4526 pos = make_number (around_charpos);
4527 limit = make_number (max (XINT (pos) - MAX_DISTANCE, BEGV));
4528 while (!found && !EQ (pos, limit))
4529 {
4530 prop = Fget_char_property (pos, Qdisplay, Qnil);
4531 if (!NILP (prop) && display_prop_string_p (prop, string))
4532 found = 1;
4533 else
4534 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
4535 limit);
4536 }
4537 }
4538
4539 return found ? XINT (pos) : 0;
4540 }
4541
4542
4543 \f
4544 /***********************************************************************
4545 `composition' property
4546 ***********************************************************************/
4547
4548 static enum prop_handled
4549 handle_auto_composed_prop (it)
4550 struct it *it;
4551 {
4552 enum prop_handled handled = HANDLED_NORMALLY;
4553
4554 if (FUNCTIONP (Vauto_composition_function))
4555 {
4556 Lisp_Object val = Qnil;
4557 EMACS_INT pos, limit = -1;
4558
4559 if (STRINGP (it->string))
4560 pos = IT_STRING_CHARPOS (*it);
4561 else
4562 pos = IT_CHARPOS (*it);
4563
4564 val = Fget_text_property (make_number (pos), Qauto_composed, it->string);
4565 if (! NILP (val))
4566 {
4567 Lisp_Object cmp_prop;
4568 EMACS_INT cmp_start, cmp_end;
4569
4570 #ifdef USE_FONT_BACKEND
4571 if (enable_font_backend
4572 && get_property_and_range (pos, Qcomposition, &cmp_prop,
4573 &cmp_start, &cmp_end, it->string)
4574 && cmp_start == pos
4575 && COMPOSITION_METHOD (cmp_prop) == COMPOSITION_WITH_GLYPH_STRING)
4576 {
4577 Lisp_Object gstring = COMPOSITION_COMPONENTS (cmp_prop);
4578 Lisp_Object font_object = LGSTRING_FONT (gstring);
4579
4580 if (! EQ (font_object,
4581 font_at (-1, pos, FACE_FROM_ID (it->f, it->face_id),
4582 it->w, it->string)))
4583 /* We must re-compute the composition for the
4584 different font. */
4585 val = Qnil;
4586 }
4587 #endif
4588 if (! NILP (val))
4589 {
4590 Lisp_Object end;
4591
4592 /* As Fnext_single_char_property_change is very slow, we
4593 limit the search to the current line. */
4594 if (STRINGP (it->string))
4595 limit = SCHARS (it->string);
4596 else
4597 limit = find_next_newline_no_quit (pos, 1);
4598 end = Fnext_single_char_property_change (make_number (pos),
4599 Qauto_composed,
4600 it->string,
4601 make_number (limit));
4602
4603 if (XINT (end) < limit)
4604 /* The current point is auto-composed, but there exist
4605 characters not yet composed beyond the
4606 auto-composed region. There's a possiblity that
4607 the last characters in the region may be newly
4608 composed. */
4609 val = Qnil;
4610 }
4611 }
4612 if (NILP (val))
4613 {
4614 if (limit < 0)
4615 limit = (STRINGP (it->string) ? SCHARS (it->string)
4616 : find_next_newline_no_quit (pos, 1));
4617 if (pos < limit)
4618 {
4619 int count = SPECPDL_INDEX ();
4620 Lisp_Object args[5];
4621
4622 args[0] = Vauto_composition_function;
4623 specbind (Qauto_composition_function, Qnil);
4624 args[1] = make_number (pos);
4625 args[2] = make_number (limit);
4626 #ifdef USE_FONT_BACKEND
4627 if (enable_font_backend)
4628 args[3] = it->window;
4629 else
4630 #endif /* USE_FONT_BACKEND */
4631 args[3] = Qnil;
4632 args[4] = it->string;
4633 safe_call (5, args);
4634 unbind_to (count, Qnil);
4635 }
4636 }
4637 }
4638
4639 return handled;
4640 }
4641
4642 /* Set up iterator IT from `composition' property at its current
4643 position. Called from handle_stop. */
4644
4645 static enum prop_handled
4646 handle_composition_prop (it)
4647 struct it *it;
4648 {
4649 Lisp_Object prop, string;
4650 EMACS_INT pos, pos_byte, start, end;
4651 enum prop_handled handled = HANDLED_NORMALLY;
4652
4653 if (STRINGP (it->string))
4654 {
4655 pos = IT_STRING_CHARPOS (*it);
4656 pos_byte = IT_STRING_BYTEPOS (*it);
4657 string = it->string;
4658 }
4659 else
4660 {
4661 pos = IT_CHARPOS (*it);
4662 pos_byte = IT_BYTEPOS (*it);
4663 string = Qnil;
4664 }
4665
4666 /* If there's a valid composition and point is not inside of the
4667 composition (in the case that the composition is from the current
4668 buffer), draw a glyph composed from the composition components. */
4669 if (find_composition (pos, -1, &start, &end, &prop, string)
4670 && COMPOSITION_VALID_P (start, end, prop)
4671 && (STRINGP (it->string) || (PT <= start || PT >= end)))
4672 {
4673 int id;
4674
4675 if (start != pos)
4676 {
4677 if (STRINGP (it->string))
4678 pos_byte = string_char_to_byte (it->string, start);
4679 else
4680 pos_byte = CHAR_TO_BYTE (start);
4681 }
4682 id = get_composition_id (start, pos_byte, end - start, prop, string);
4683
4684 if (id >= 0)
4685 {
4686 struct composition *cmp = composition_table[id];
4687
4688 if (cmp->glyph_len == 0)
4689 {
4690 /* No glyph. */
4691 if (STRINGP (it->string))
4692 {
4693 IT_STRING_CHARPOS (*it) = end;
4694 IT_STRING_BYTEPOS (*it) = string_char_to_byte (it->string,
4695 end);
4696 }
4697 else
4698 {
4699 IT_CHARPOS (*it) = end;
4700 IT_BYTEPOS (*it) = CHAR_TO_BYTE (end);
4701 }
4702 return HANDLED_RECOMPUTE_PROPS;
4703 }
4704
4705 it->stop_charpos = end;
4706 push_it (it);
4707
4708 it->method = GET_FROM_COMPOSITION;
4709 it->cmp_id = id;
4710 it->cmp_len = COMPOSITION_LENGTH (prop);
4711 /* For a terminal, draw only the first (non-TAB) character
4712 of the components. */
4713 #ifdef USE_FONT_BACKEND
4714 if (composition_table[id]->method == COMPOSITION_WITH_GLYPH_STRING)
4715 {
4716 Lisp_Object lgstring = AREF (XHASH_TABLE (composition_hash_table)
4717 ->key_and_value,
4718 cmp->hash_index * 2);
4719
4720 it->c = LGLYPH_CHAR (LGSTRING_GLYPH (lgstring, 0));
4721 }
4722 else
4723 #endif /* USE_FONT_BACKEND */
4724 {
4725 int i;
4726
4727 for (i = 0; i < cmp->glyph_len; i++)
4728 if ((it->c = COMPOSITION_GLYPH (composition_table[id], i))
4729 != '\t')
4730 break;
4731 }
4732 if (it->c == '\t')
4733 it->c = ' ';
4734 it->len = (STRINGP (it->string)
4735 ? string_char_to_byte (it->string, end)
4736 : CHAR_TO_BYTE (end)) - pos_byte;
4737 handled = HANDLED_RETURN;
4738 }
4739 }
4740
4741 return handled;
4742 }
4743
4744
4745 \f
4746 /***********************************************************************
4747 Overlay strings
4748 ***********************************************************************/
4749
4750 /* The following structure is used to record overlay strings for
4751 later sorting in load_overlay_strings. */
4752
4753 struct overlay_entry
4754 {
4755 Lisp_Object overlay;
4756 Lisp_Object string;
4757 int priority;
4758 int after_string_p;
4759 };
4760
4761
4762 /* Set up iterator IT from overlay strings at its current position.
4763 Called from handle_stop. */
4764
4765 static enum prop_handled
4766 handle_overlay_change (it)
4767 struct it *it;
4768 {
4769 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
4770 return HANDLED_RECOMPUTE_PROPS;
4771 else
4772 return HANDLED_NORMALLY;
4773 }
4774
4775
4776 /* Set up the next overlay string for delivery by IT, if there is an
4777 overlay string to deliver. Called by set_iterator_to_next when the
4778 end of the current overlay string is reached. If there are more
4779 overlay strings to display, IT->string and
4780 IT->current.overlay_string_index are set appropriately here.
4781 Otherwise IT->string is set to nil. */
4782
4783 static void
4784 next_overlay_string (it)
4785 struct it *it;
4786 {
4787 ++it->current.overlay_string_index;
4788 if (it->current.overlay_string_index == it->n_overlay_strings)
4789 {
4790 /* No more overlay strings. Restore IT's settings to what
4791 they were before overlay strings were processed, and
4792 continue to deliver from current_buffer. */
4793 int display_ellipsis_p = it->stack[it->sp - 1].display_ellipsis_p;
4794
4795 pop_it (it);
4796 xassert (it->sp > 0
4797 || it->method == GET_FROM_COMPOSITION
4798 || (NILP (it->string)
4799 && it->method == GET_FROM_BUFFER
4800 && it->stop_charpos >= BEGV
4801 && it->stop_charpos <= it->end_charpos));
4802 it->current.overlay_string_index = -1;
4803 it->n_overlay_strings = 0;
4804
4805 /* If we're at the end of the buffer, record that we have
4806 processed the overlay strings there already, so that
4807 next_element_from_buffer doesn't try it again. */
4808 if (IT_CHARPOS (*it) >= it->end_charpos)
4809 it->overlay_strings_at_end_processed_p = 1;
4810
4811 /* If we have to display `...' for invisible text, set
4812 the iterator up for that. */
4813 if (display_ellipsis_p)
4814 setup_for_ellipsis (it, 0);
4815 }
4816 else
4817 {
4818 /* There are more overlay strings to process. If
4819 IT->current.overlay_string_index has advanced to a position
4820 where we must load IT->overlay_strings with more strings, do
4821 it. */
4822 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
4823
4824 if (it->current.overlay_string_index && i == 0)
4825 load_overlay_strings (it, 0);
4826
4827 /* Initialize IT to deliver display elements from the overlay
4828 string. */
4829 it->string = it->overlay_strings[i];
4830 it->multibyte_p = STRING_MULTIBYTE (it->string);
4831 SET_TEXT_POS (it->current.string_pos, 0, 0);
4832 it->method = GET_FROM_STRING;
4833 it->stop_charpos = 0;
4834 }
4835
4836 CHECK_IT (it);
4837 }
4838
4839
4840 /* Compare two overlay_entry structures E1 and E2. Used as a
4841 comparison function for qsort in load_overlay_strings. Overlay
4842 strings for the same position are sorted so that
4843
4844 1. All after-strings come in front of before-strings, except
4845 when they come from the same overlay.
4846
4847 2. Within after-strings, strings are sorted so that overlay strings
4848 from overlays with higher priorities come first.
4849
4850 2. Within before-strings, strings are sorted so that overlay
4851 strings from overlays with higher priorities come last.
4852
4853 Value is analogous to strcmp. */
4854
4855
4856 static int
4857 compare_overlay_entries (e1, e2)
4858 void *e1, *e2;
4859 {
4860 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
4861 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
4862 int result;
4863
4864 if (entry1->after_string_p != entry2->after_string_p)
4865 {
4866 /* Let after-strings appear in front of before-strings if
4867 they come from different overlays. */
4868 if (EQ (entry1->overlay, entry2->overlay))
4869 result = entry1->after_string_p ? 1 : -1;
4870 else
4871 result = entry1->after_string_p ? -1 : 1;
4872 }
4873 else if (entry1->after_string_p)
4874 /* After-strings sorted in order of decreasing priority. */
4875 result = entry2->priority - entry1->priority;
4876 else
4877 /* Before-strings sorted in order of increasing priority. */
4878 result = entry1->priority - entry2->priority;
4879
4880 return result;
4881 }
4882
4883
4884 /* Load the vector IT->overlay_strings with overlay strings from IT's
4885 current buffer position, or from CHARPOS if that is > 0. Set
4886 IT->n_overlays to the total number of overlay strings found.
4887
4888 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
4889 a time. On entry into load_overlay_strings,
4890 IT->current.overlay_string_index gives the number of overlay
4891 strings that have already been loaded by previous calls to this
4892 function.
4893
4894 IT->add_overlay_start contains an additional overlay start
4895 position to consider for taking overlay strings from, if non-zero.
4896 This position comes into play when the overlay has an `invisible'
4897 property, and both before and after-strings. When we've skipped to
4898 the end of the overlay, because of its `invisible' property, we
4899 nevertheless want its before-string to appear.
4900 IT->add_overlay_start will contain the overlay start position
4901 in this case.
4902
4903 Overlay strings are sorted so that after-string strings come in
4904 front of before-string strings. Within before and after-strings,
4905 strings are sorted by overlay priority. See also function
4906 compare_overlay_entries. */
4907
4908 static void
4909 load_overlay_strings (it, charpos)
4910 struct it *it;
4911 int charpos;
4912 {
4913 extern Lisp_Object Qafter_string, Qbefore_string, Qwindow, Qpriority;
4914 Lisp_Object overlay, window, str, invisible;
4915 struct Lisp_Overlay *ov;
4916 int start, end;
4917 int size = 20;
4918 int n = 0, i, j, invis_p;
4919 struct overlay_entry *entries
4920 = (struct overlay_entry *) alloca (size * sizeof *entries);
4921
4922 if (charpos <= 0)
4923 charpos = IT_CHARPOS (*it);
4924
4925 /* Append the overlay string STRING of overlay OVERLAY to vector
4926 `entries' which has size `size' and currently contains `n'
4927 elements. AFTER_P non-zero means STRING is an after-string of
4928 OVERLAY. */
4929 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
4930 do \
4931 { \
4932 Lisp_Object priority; \
4933 \
4934 if (n == size) \
4935 { \
4936 int new_size = 2 * size; \
4937 struct overlay_entry *old = entries; \
4938 entries = \
4939 (struct overlay_entry *) alloca (new_size \
4940 * sizeof *entries); \
4941 bcopy (old, entries, size * sizeof *entries); \
4942 size = new_size; \
4943 } \
4944 \
4945 entries[n].string = (STRING); \
4946 entries[n].overlay = (OVERLAY); \
4947 priority = Foverlay_get ((OVERLAY), Qpriority); \
4948 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
4949 entries[n].after_string_p = (AFTER_P); \
4950 ++n; \
4951 } \
4952 while (0)
4953
4954 /* Process overlay before the overlay center. */
4955 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
4956 {
4957 XSETMISC (overlay, ov);
4958 xassert (OVERLAYP (overlay));
4959 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4960 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4961
4962 if (end < charpos)
4963 break;
4964
4965 /* Skip this overlay if it doesn't start or end at IT's current
4966 position. */
4967 if (end != charpos && start != charpos)
4968 continue;
4969
4970 /* Skip this overlay if it doesn't apply to IT->w. */
4971 window = Foverlay_get (overlay, Qwindow);
4972 if (WINDOWP (window) && XWINDOW (window) != it->w)
4973 continue;
4974
4975 /* If the text ``under'' the overlay is invisible, both before-
4976 and after-strings from this overlay are visible; start and
4977 end position are indistinguishable. */
4978 invisible = Foverlay_get (overlay, Qinvisible);
4979 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4980
4981 /* If overlay has a non-empty before-string, record it. */
4982 if ((start == charpos || (end == charpos && invis_p))
4983 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4984 && SCHARS (str))
4985 RECORD_OVERLAY_STRING (overlay, str, 0);
4986
4987 /* If overlay has a non-empty after-string, record it. */
4988 if ((end == charpos || (start == charpos && invis_p))
4989 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4990 && SCHARS (str))
4991 RECORD_OVERLAY_STRING (overlay, str, 1);
4992 }
4993
4994 /* Process overlays after the overlay center. */
4995 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
4996 {
4997 XSETMISC (overlay, ov);
4998 xassert (OVERLAYP (overlay));
4999 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5000 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5001
5002 if (start > charpos)
5003 break;
5004
5005 /* Skip this overlay if it doesn't start or end at IT's current
5006 position. */
5007 if (end != charpos && start != charpos)
5008 continue;
5009
5010 /* Skip this overlay if it doesn't apply to IT->w. */
5011 window = Foverlay_get (overlay, Qwindow);
5012 if (WINDOWP (window) && XWINDOW (window) != it->w)
5013 continue;
5014
5015 /* If the text ``under'' the overlay is invisible, it has a zero
5016 dimension, and both before- and after-strings apply. */
5017 invisible = Foverlay_get (overlay, Qinvisible);
5018 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5019
5020 /* If overlay has a non-empty before-string, record it. */
5021 if ((start == charpos || (end == charpos && invis_p))
5022 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5023 && SCHARS (str))
5024 RECORD_OVERLAY_STRING (overlay, str, 0);
5025
5026 /* If overlay has a non-empty after-string, record it. */
5027 if ((end == charpos || (start == charpos && invis_p))
5028 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5029 && SCHARS (str))
5030 RECORD_OVERLAY_STRING (overlay, str, 1);
5031 }
5032
5033 #undef RECORD_OVERLAY_STRING
5034
5035 /* Sort entries. */
5036 if (n > 1)
5037 qsort (entries, n, sizeof *entries, compare_overlay_entries);
5038
5039 /* Record the total number of strings to process. */
5040 it->n_overlay_strings = n;
5041
5042 /* IT->current.overlay_string_index is the number of overlay strings
5043 that have already been consumed by IT. Copy some of the
5044 remaining overlay strings to IT->overlay_strings. */
5045 i = 0;
5046 j = it->current.overlay_string_index;
5047 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
5048 {
5049 it->overlay_strings[i] = entries[j].string;
5050 it->string_overlays[i++] = entries[j++].overlay;
5051 }
5052
5053 CHECK_IT (it);
5054 }
5055
5056
5057 /* Get the first chunk of overlay strings at IT's current buffer
5058 position, or at CHARPOS if that is > 0. Value is non-zero if at
5059 least one overlay string was found. */
5060
5061 static int
5062 get_overlay_strings_1 (it, charpos, compute_stop_p)
5063 struct it *it;
5064 int charpos;
5065 {
5066 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
5067 process. This fills IT->overlay_strings with strings, and sets
5068 IT->n_overlay_strings to the total number of strings to process.
5069 IT->pos.overlay_string_index has to be set temporarily to zero
5070 because load_overlay_strings needs this; it must be set to -1
5071 when no overlay strings are found because a zero value would
5072 indicate a position in the first overlay string. */
5073 it->current.overlay_string_index = 0;
5074 load_overlay_strings (it, charpos);
5075
5076 /* If we found overlay strings, set up IT to deliver display
5077 elements from the first one. Otherwise set up IT to deliver
5078 from current_buffer. */
5079 if (it->n_overlay_strings)
5080 {
5081 /* Make sure we know settings in current_buffer, so that we can
5082 restore meaningful values when we're done with the overlay
5083 strings. */
5084 if (compute_stop_p)
5085 compute_stop_pos (it);
5086 xassert (it->face_id >= 0);
5087
5088 /* Save IT's settings. They are restored after all overlay
5089 strings have been processed. */
5090 xassert (!compute_stop_p || it->sp == 0);
5091 push_it (it);
5092
5093 /* Set up IT to deliver display elements from the first overlay
5094 string. */
5095 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5096 it->string = it->overlay_strings[0];
5097 it->from_overlay = Qnil;
5098 it->stop_charpos = 0;
5099 xassert (STRINGP (it->string));
5100 it->end_charpos = SCHARS (it->string);
5101 it->multibyte_p = STRING_MULTIBYTE (it->string);
5102 it->method = GET_FROM_STRING;
5103 return 1;
5104 }
5105
5106 it->current.overlay_string_index = -1;
5107 return 0;
5108 }
5109
5110 static int
5111 get_overlay_strings (it, charpos)
5112 struct it *it;
5113 int charpos;
5114 {
5115 it->string = Qnil;
5116 it->method = GET_FROM_BUFFER;
5117
5118 (void) get_overlay_strings_1 (it, charpos, 1);
5119
5120 CHECK_IT (it);
5121
5122 /* Value is non-zero if we found at least one overlay string. */
5123 return STRINGP (it->string);
5124 }
5125
5126
5127 \f
5128 /***********************************************************************
5129 Saving and restoring state
5130 ***********************************************************************/
5131
5132 /* Save current settings of IT on IT->stack. Called, for example,
5133 before setting up IT for an overlay string, to be able to restore
5134 IT's settings to what they were after the overlay string has been
5135 processed. */
5136
5137 static void
5138 push_it (it)
5139 struct it *it;
5140 {
5141 struct iterator_stack_entry *p;
5142
5143 xassert (it->sp < IT_STACK_SIZE);
5144 p = it->stack + it->sp;
5145
5146 p->stop_charpos = it->stop_charpos;
5147 xassert (it->face_id >= 0);
5148 p->face_id = it->face_id;
5149 p->string = it->string;
5150 p->method = it->method;
5151 p->from_overlay = it->from_overlay;
5152 switch (p->method)
5153 {
5154 case GET_FROM_IMAGE:
5155 p->u.image.object = it->object;
5156 p->u.image.image_id = it->image_id;
5157 p->u.image.slice = it->slice;
5158 break;
5159 case GET_FROM_COMPOSITION:
5160 p->u.comp.object = it->object;
5161 p->u.comp.c = it->c;
5162 p->u.comp.len = it->len;
5163 p->u.comp.cmp_id = it->cmp_id;
5164 p->u.comp.cmp_len = it->cmp_len;
5165 break;
5166 case GET_FROM_STRETCH:
5167 p->u.stretch.object = it->object;
5168 break;
5169 }
5170 p->position = it->position;
5171 p->current = it->current;
5172 p->end_charpos = it->end_charpos;
5173 p->string_nchars = it->string_nchars;
5174 p->area = it->area;
5175 p->multibyte_p = it->multibyte_p;
5176 p->space_width = it->space_width;
5177 p->font_height = it->font_height;
5178 p->voffset = it->voffset;
5179 p->string_from_display_prop_p = it->string_from_display_prop_p;
5180 p->display_ellipsis_p = 0;
5181 ++it->sp;
5182 }
5183
5184
5185 /* Restore IT's settings from IT->stack. Called, for example, when no
5186 more overlay strings must be processed, and we return to delivering
5187 display elements from a buffer, or when the end of a string from a
5188 `display' property is reached and we return to delivering display
5189 elements from an overlay string, or from a buffer. */
5190
5191 static void
5192 pop_it (it)
5193 struct it *it;
5194 {
5195 struct iterator_stack_entry *p;
5196
5197 xassert (it->sp > 0);
5198 --it->sp;
5199 p = it->stack + it->sp;
5200 it->stop_charpos = p->stop_charpos;
5201 it->face_id = p->face_id;
5202 it->current = p->current;
5203 it->position = p->position;
5204 it->string = p->string;
5205 it->from_overlay = p->from_overlay;
5206 if (NILP (it->string))
5207 SET_TEXT_POS (it->current.string_pos, -1, -1);
5208 it->method = p->method;
5209 switch (it->method)
5210 {
5211 case GET_FROM_IMAGE:
5212 it->image_id = p->u.image.image_id;
5213 it->object = p->u.image.object;
5214 it->slice = p->u.image.slice;
5215 break;
5216 case GET_FROM_COMPOSITION:
5217 it->object = p->u.comp.object;
5218 it->c = p->u.comp.c;
5219 it->len = p->u.comp.len;
5220 it->cmp_id = p->u.comp.cmp_id;
5221 it->cmp_len = p->u.comp.cmp_len;
5222 break;
5223 case GET_FROM_STRETCH:
5224 it->object = p->u.comp.object;
5225 break;
5226 case GET_FROM_BUFFER:
5227 it->object = it->w->buffer;
5228 break;
5229 case GET_FROM_STRING:
5230 it->object = it->string;
5231 break;
5232 }
5233 it->end_charpos = p->end_charpos;
5234 it->string_nchars = p->string_nchars;
5235 it->area = p->area;
5236 it->multibyte_p = p->multibyte_p;
5237 it->space_width = p->space_width;
5238 it->font_height = p->font_height;
5239 it->voffset = p->voffset;
5240 it->string_from_display_prop_p = p->string_from_display_prop_p;
5241 }
5242
5243
5244 \f
5245 /***********************************************************************
5246 Moving over lines
5247 ***********************************************************************/
5248
5249 /* Set IT's current position to the previous line start. */
5250
5251 static void
5252 back_to_previous_line_start (it)
5253 struct it *it;
5254 {
5255 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
5256 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
5257 }
5258
5259
5260 /* Move IT to the next line start.
5261
5262 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
5263 we skipped over part of the text (as opposed to moving the iterator
5264 continuously over the text). Otherwise, don't change the value
5265 of *SKIPPED_P.
5266
5267 Newlines may come from buffer text, overlay strings, or strings
5268 displayed via the `display' property. That's the reason we can't
5269 simply use find_next_newline_no_quit.
5270
5271 Note that this function may not skip over invisible text that is so
5272 because of text properties and immediately follows a newline. If
5273 it would, function reseat_at_next_visible_line_start, when called
5274 from set_iterator_to_next, would effectively make invisible
5275 characters following a newline part of the wrong glyph row, which
5276 leads to wrong cursor motion. */
5277
5278 static int
5279 forward_to_next_line_start (it, skipped_p)
5280 struct it *it;
5281 int *skipped_p;
5282 {
5283 int old_selective, newline_found_p, n;
5284 const int MAX_NEWLINE_DISTANCE = 500;
5285
5286 /* If already on a newline, just consume it to avoid unintended
5287 skipping over invisible text below. */
5288 if (it->what == IT_CHARACTER
5289 && it->c == '\n'
5290 && CHARPOS (it->position) == IT_CHARPOS (*it))
5291 {
5292 set_iterator_to_next (it, 0);
5293 it->c = 0;
5294 return 1;
5295 }
5296
5297 /* Don't handle selective display in the following. It's (a)
5298 unnecessary because it's done by the caller, and (b) leads to an
5299 infinite recursion because next_element_from_ellipsis indirectly
5300 calls this function. */
5301 old_selective = it->selective;
5302 it->selective = 0;
5303
5304 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
5305 from buffer text. */
5306 for (n = newline_found_p = 0;
5307 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
5308 n += STRINGP (it->string) ? 0 : 1)
5309 {
5310 if (!get_next_display_element (it))
5311 return 0;
5312 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
5313 set_iterator_to_next (it, 0);
5314 }
5315
5316 /* If we didn't find a newline near enough, see if we can use a
5317 short-cut. */
5318 if (!newline_found_p)
5319 {
5320 int start = IT_CHARPOS (*it);
5321 int limit = find_next_newline_no_quit (start, 1);
5322 Lisp_Object pos;
5323
5324 xassert (!STRINGP (it->string));
5325
5326 /* If there isn't any `display' property in sight, and no
5327 overlays, we can just use the position of the newline in
5328 buffer text. */
5329 if (it->stop_charpos >= limit
5330 || ((pos = Fnext_single_property_change (make_number (start),
5331 Qdisplay,
5332 Qnil, make_number (limit)),
5333 NILP (pos))
5334 && next_overlay_change (start) == ZV))
5335 {
5336 IT_CHARPOS (*it) = limit;
5337 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
5338 *skipped_p = newline_found_p = 1;
5339 }
5340 else
5341 {
5342 while (get_next_display_element (it)
5343 && !newline_found_p)
5344 {
5345 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
5346 set_iterator_to_next (it, 0);
5347 }
5348 }
5349 }
5350
5351 it->selective = old_selective;
5352 return newline_found_p;
5353 }
5354
5355
5356 /* Set IT's current position to the previous visible line start. Skip
5357 invisible text that is so either due to text properties or due to
5358 selective display. Caution: this does not change IT->current_x and
5359 IT->hpos. */
5360
5361 static void
5362 back_to_previous_visible_line_start (it)
5363 struct it *it;
5364 {
5365 while (IT_CHARPOS (*it) > BEGV)
5366 {
5367 back_to_previous_line_start (it);
5368
5369 if (IT_CHARPOS (*it) <= BEGV)
5370 break;
5371
5372 /* If selective > 0, then lines indented more than that values
5373 are invisible. */
5374 if (it->selective > 0
5375 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5376 (double) it->selective)) /* iftc */
5377 continue;
5378
5379 /* Check the newline before point for invisibility. */
5380 {
5381 Lisp_Object prop;
5382 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
5383 Qinvisible, it->window);
5384 if (TEXT_PROP_MEANS_INVISIBLE (prop))
5385 continue;
5386 }
5387
5388 if (IT_CHARPOS (*it) <= BEGV)
5389 break;
5390
5391 {
5392 struct it it2;
5393 int pos;
5394 EMACS_INT beg, end;
5395 Lisp_Object val, overlay;
5396
5397 /* If newline is part of a composition, continue from start of composition */
5398 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
5399 && beg < IT_CHARPOS (*it))
5400 goto replaced;
5401
5402 /* If newline is replaced by a display property, find start of overlay
5403 or interval and continue search from that point. */
5404 it2 = *it;
5405 pos = --IT_CHARPOS (it2);
5406 --IT_BYTEPOS (it2);
5407 it2.sp = 0;
5408 if (handle_display_prop (&it2) == HANDLED_RETURN
5409 && !NILP (val = get_char_property_and_overlay
5410 (make_number (pos), Qdisplay, Qnil, &overlay))
5411 && (OVERLAYP (overlay)
5412 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
5413 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
5414 goto replaced;
5415
5416 /* Newline is not replaced by anything -- so we are done. */
5417 break;
5418
5419 replaced:
5420 if (beg < BEGV)
5421 beg = BEGV;
5422 IT_CHARPOS (*it) = beg;
5423 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
5424 }
5425 }
5426
5427 it->continuation_lines_width = 0;
5428
5429 xassert (IT_CHARPOS (*it) >= BEGV);
5430 xassert (IT_CHARPOS (*it) == BEGV
5431 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5432 CHECK_IT (it);
5433 }
5434
5435
5436 /* Reseat iterator IT at the previous visible line start. Skip
5437 invisible text that is so either due to text properties or due to
5438 selective display. At the end, update IT's overlay information,
5439 face information etc. */
5440
5441 void
5442 reseat_at_previous_visible_line_start (it)
5443 struct it *it;
5444 {
5445 back_to_previous_visible_line_start (it);
5446 reseat (it, it->current.pos, 1);
5447 CHECK_IT (it);
5448 }
5449
5450
5451 /* Reseat iterator IT on the next visible line start in the current
5452 buffer. ON_NEWLINE_P non-zero means position IT on the newline
5453 preceding the line start. Skip over invisible text that is so
5454 because of selective display. Compute faces, overlays etc at the
5455 new position. Note that this function does not skip over text that
5456 is invisible because of text properties. */
5457
5458 static void
5459 reseat_at_next_visible_line_start (it, on_newline_p)
5460 struct it *it;
5461 int on_newline_p;
5462 {
5463 int newline_found_p, skipped_p = 0;
5464
5465 newline_found_p = forward_to_next_line_start (it, &skipped_p);
5466
5467 /* Skip over lines that are invisible because they are indented
5468 more than the value of IT->selective. */
5469 if (it->selective > 0)
5470 while (IT_CHARPOS (*it) < ZV
5471 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5472 (double) it->selective)) /* iftc */
5473 {
5474 xassert (IT_BYTEPOS (*it) == BEGV
5475 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5476 newline_found_p = forward_to_next_line_start (it, &skipped_p);
5477 }
5478
5479 /* Position on the newline if that's what's requested. */
5480 if (on_newline_p && newline_found_p)
5481 {
5482 if (STRINGP (it->string))
5483 {
5484 if (IT_STRING_CHARPOS (*it) > 0)
5485 {
5486 --IT_STRING_CHARPOS (*it);
5487 --IT_STRING_BYTEPOS (*it);
5488 }
5489 }
5490 else if (IT_CHARPOS (*it) > BEGV)
5491 {
5492 --IT_CHARPOS (*it);
5493 --IT_BYTEPOS (*it);
5494 reseat (it, it->current.pos, 0);
5495 }
5496 }
5497 else if (skipped_p)
5498 reseat (it, it->current.pos, 0);
5499
5500 CHECK_IT (it);
5501 }
5502
5503
5504 \f
5505 /***********************************************************************
5506 Changing an iterator's position
5507 ***********************************************************************/
5508
5509 /* Change IT's current position to POS in current_buffer. If FORCE_P
5510 is non-zero, always check for text properties at the new position.
5511 Otherwise, text properties are only looked up if POS >=
5512 IT->check_charpos of a property. */
5513
5514 static void
5515 reseat (it, pos, force_p)
5516 struct it *it;
5517 struct text_pos pos;
5518 int force_p;
5519 {
5520 int original_pos = IT_CHARPOS (*it);
5521
5522 reseat_1 (it, pos, 0);
5523
5524 /* Determine where to check text properties. Avoid doing it
5525 where possible because text property lookup is very expensive. */
5526 if (force_p
5527 || CHARPOS (pos) > it->stop_charpos
5528 || CHARPOS (pos) < original_pos)
5529 handle_stop (it);
5530
5531 CHECK_IT (it);
5532 }
5533
5534
5535 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
5536 IT->stop_pos to POS, also. */
5537
5538 static void
5539 reseat_1 (it, pos, set_stop_p)
5540 struct it *it;
5541 struct text_pos pos;
5542 int set_stop_p;
5543 {
5544 /* Don't call this function when scanning a C string. */
5545 xassert (it->s == NULL);
5546
5547 /* POS must be a reasonable value. */
5548 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
5549
5550 it->current.pos = it->position = pos;
5551 it->end_charpos = ZV;
5552 it->dpvec = NULL;
5553 it->current.dpvec_index = -1;
5554 it->current.overlay_string_index = -1;
5555 IT_STRING_CHARPOS (*it) = -1;
5556 IT_STRING_BYTEPOS (*it) = -1;
5557 it->string = Qnil;
5558 it->method = GET_FROM_BUFFER;
5559 it->object = it->w->buffer;
5560 it->area = TEXT_AREA;
5561 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
5562 it->sp = 0;
5563 it->string_from_display_prop_p = 0;
5564 it->face_before_selective_p = 0;
5565
5566 if (set_stop_p)
5567 it->stop_charpos = CHARPOS (pos);
5568 }
5569
5570
5571 /* Set up IT for displaying a string, starting at CHARPOS in window W.
5572 If S is non-null, it is a C string to iterate over. Otherwise,
5573 STRING gives a Lisp string to iterate over.
5574
5575 If PRECISION > 0, don't return more then PRECISION number of
5576 characters from the string.
5577
5578 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
5579 characters have been returned. FIELD_WIDTH < 0 means an infinite
5580 field width.
5581
5582 MULTIBYTE = 0 means disable processing of multibyte characters,
5583 MULTIBYTE > 0 means enable it,
5584 MULTIBYTE < 0 means use IT->multibyte_p.
5585
5586 IT must be initialized via a prior call to init_iterator before
5587 calling this function. */
5588
5589 static void
5590 reseat_to_string (it, s, string, charpos, precision, field_width, multibyte)
5591 struct it *it;
5592 unsigned char *s;
5593 Lisp_Object string;
5594 int charpos;
5595 int precision, field_width, multibyte;
5596 {
5597 /* No region in strings. */
5598 it->region_beg_charpos = it->region_end_charpos = -1;
5599
5600 /* No text property checks performed by default, but see below. */
5601 it->stop_charpos = -1;
5602
5603 /* Set iterator position and end position. */
5604 bzero (&it->current, sizeof it->current);
5605 it->current.overlay_string_index = -1;
5606 it->current.dpvec_index = -1;
5607 xassert (charpos >= 0);
5608
5609 /* If STRING is specified, use its multibyteness, otherwise use the
5610 setting of MULTIBYTE, if specified. */
5611 if (multibyte >= 0)
5612 it->multibyte_p = multibyte > 0;
5613
5614 if (s == NULL)
5615 {
5616 xassert (STRINGP (string));
5617 it->string = string;
5618 it->s = NULL;
5619 it->end_charpos = it->string_nchars = SCHARS (string);
5620 it->method = GET_FROM_STRING;
5621 it->current.string_pos = string_pos (charpos, string);
5622 }
5623 else
5624 {
5625 it->s = s;
5626 it->string = Qnil;
5627
5628 /* Note that we use IT->current.pos, not it->current.string_pos,
5629 for displaying C strings. */
5630 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
5631 if (it->multibyte_p)
5632 {
5633 it->current.pos = c_string_pos (charpos, s, 1);
5634 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
5635 }
5636 else
5637 {
5638 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
5639 it->end_charpos = it->string_nchars = strlen (s);
5640 }
5641
5642 it->method = GET_FROM_C_STRING;
5643 }
5644
5645 /* PRECISION > 0 means don't return more than PRECISION characters
5646 from the string. */
5647 if (precision > 0 && it->end_charpos - charpos > precision)
5648 it->end_charpos = it->string_nchars = charpos + precision;
5649
5650 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
5651 characters have been returned. FIELD_WIDTH == 0 means don't pad,
5652 FIELD_WIDTH < 0 means infinite field width. This is useful for
5653 padding with `-' at the end of a mode line. */
5654 if (field_width < 0)
5655 field_width = INFINITY;
5656 if (field_width > it->end_charpos - charpos)
5657 it->end_charpos = charpos + field_width;
5658
5659 /* Use the standard display table for displaying strings. */
5660 if (DISP_TABLE_P (Vstandard_display_table))
5661 it->dp = XCHAR_TABLE (Vstandard_display_table);
5662
5663 it->stop_charpos = charpos;
5664 CHECK_IT (it);
5665 }
5666
5667
5668 \f
5669 /***********************************************************************
5670 Iteration
5671 ***********************************************************************/
5672
5673 /* Map enum it_method value to corresponding next_element_from_* function. */
5674
5675 static int (* get_next_element[NUM_IT_METHODS]) P_ ((struct it *it)) =
5676 {
5677 next_element_from_buffer,
5678 next_element_from_display_vector,
5679 next_element_from_composition,
5680 next_element_from_string,
5681 next_element_from_c_string,
5682 next_element_from_image,
5683 next_element_from_stretch
5684 };
5685
5686
5687 /* Load IT's display element fields with information about the next
5688 display element from the current position of IT. Value is zero if
5689 end of buffer (or C string) is reached. */
5690
5691 static struct frame *last_escape_glyph_frame = NULL;
5692 static unsigned last_escape_glyph_face_id = (1 << FACE_ID_BITS);
5693 static int last_escape_glyph_merged_face_id = 0;
5694
5695 int
5696 get_next_display_element (it)
5697 struct it *it;
5698 {
5699 /* Non-zero means that we found a display element. Zero means that
5700 we hit the end of what we iterate over. Performance note: the
5701 function pointer `method' used here turns out to be faster than
5702 using a sequence of if-statements. */
5703 int success_p;
5704
5705 get_next:
5706 success_p = (*get_next_element[it->method]) (it);
5707
5708 if (it->what == IT_CHARACTER)
5709 {
5710 /* Map via display table or translate control characters.
5711 IT->c, IT->len etc. have been set to the next character by
5712 the function call above. If we have a display table, and it
5713 contains an entry for IT->c, translate it. Don't do this if
5714 IT->c itself comes from a display table, otherwise we could
5715 end up in an infinite recursion. (An alternative could be to
5716 count the recursion depth of this function and signal an
5717 error when a certain maximum depth is reached.) Is it worth
5718 it? */
5719 if (success_p && it->dpvec == NULL)
5720 {
5721 Lisp_Object dv;
5722
5723 if (it->dp
5724 && (dv = DISP_CHAR_VECTOR (it->dp, it->c),
5725 VECTORP (dv)))
5726 {
5727 struct Lisp_Vector *v = XVECTOR (dv);
5728
5729 /* Return the first character from the display table
5730 entry, if not empty. If empty, don't display the
5731 current character. */
5732 if (v->size)
5733 {
5734 it->dpvec_char_len = it->len;
5735 it->dpvec = v->contents;
5736 it->dpend = v->contents + v->size;
5737 it->current.dpvec_index = 0;
5738 it->dpvec_face_id = -1;
5739 it->saved_face_id = it->face_id;
5740 it->method = GET_FROM_DISPLAY_VECTOR;
5741 it->ellipsis_p = 0;
5742 }
5743 else
5744 {
5745 set_iterator_to_next (it, 0);
5746 }
5747 goto get_next;
5748 }
5749
5750 /* Translate control characters into `\003' or `^C' form.
5751 Control characters coming from a display table entry are
5752 currently not translated because we use IT->dpvec to hold
5753 the translation. This could easily be changed but I
5754 don't believe that it is worth doing.
5755
5756 If it->multibyte_p is nonzero, non-printable non-ASCII
5757 characters are also translated to octal form.
5758
5759 If it->multibyte_p is zero, eight-bit characters that
5760 don't have corresponding multibyte char code are also
5761 translated to octal form. */
5762 else if ((it->c < ' '
5763 ? (it->area != TEXT_AREA
5764 /* In mode line, treat \n, \t like other crl chars. */
5765 || (it->c != '\t'
5766 && it->glyph_row && it->glyph_row->mode_line_p)
5767 || (it->c != '\n' && it->c != '\t'))
5768 : (it->multibyte_p
5769 ? (!CHAR_PRINTABLE_P (it->c)
5770 || (!NILP (Vnobreak_char_display)
5771 && (it->c == 0xA0 /* NO-BREAK SPACE */
5772 || it->c == 0xAD /* SOFT HYPHEN */)))
5773 : (it->c >= 127
5774 && (! unibyte_display_via_language_environment
5775 || (UNIBYTE_CHAR_HAS_MULTIBYTE_P (it->c)))))))
5776 {
5777 /* IT->c is a control character which must be displayed
5778 either as '\003' or as `^C' where the '\\' and '^'
5779 can be defined in the display table. Fill
5780 IT->ctl_chars with glyphs for what we have to
5781 display. Then, set IT->dpvec to these glyphs. */
5782 GLYPH g;
5783 int ctl_len;
5784 int face_id, lface_id = 0 ;
5785 GLYPH escape_glyph;
5786
5787 /* Handle control characters with ^. */
5788
5789 if (it->c < 128 && it->ctl_arrow_p)
5790 {
5791 g = '^'; /* default glyph for Control */
5792 /* Set IT->ctl_chars[0] to the glyph for `^'. */
5793 if (it->dp
5794 && INTEGERP (DISP_CTRL_GLYPH (it->dp))
5795 && GLYPH_CHAR_VALID_P (XINT (DISP_CTRL_GLYPH (it->dp))))
5796 {
5797 g = XINT (DISP_CTRL_GLYPH (it->dp));
5798 lface_id = FAST_GLYPH_FACE (g);
5799 }
5800 if (lface_id)
5801 {
5802 g = FAST_GLYPH_CHAR (g);
5803 face_id = merge_faces (it->f, Qt, lface_id,
5804 it->face_id);
5805 }
5806 else if (it->f == last_escape_glyph_frame
5807 && it->face_id == last_escape_glyph_face_id)
5808 {
5809 face_id = last_escape_glyph_merged_face_id;
5810 }
5811 else
5812 {
5813 /* Merge the escape-glyph face into the current face. */
5814 face_id = merge_faces (it->f, Qescape_glyph, 0,
5815 it->face_id);
5816 last_escape_glyph_frame = it->f;
5817 last_escape_glyph_face_id = it->face_id;
5818 last_escape_glyph_merged_face_id = face_id;
5819 }
5820
5821 XSETINT (it->ctl_chars[0], g);
5822 g = it->c ^ 0100;
5823 XSETINT (it->ctl_chars[1], g);
5824 ctl_len = 2;
5825 goto display_control;
5826 }
5827
5828 /* Handle non-break space in the mode where it only gets
5829 highlighting. */
5830
5831 if (EQ (Vnobreak_char_display, Qt)
5832 && it->c == 0xA0)
5833 {
5834 /* Merge the no-break-space face into the current face. */
5835 face_id = merge_faces (it->f, Qnobreak_space, 0,
5836 it->face_id);
5837
5838 g = it->c = ' ';
5839 XSETINT (it->ctl_chars[0], g);
5840 ctl_len = 1;
5841 goto display_control;
5842 }
5843
5844 /* Handle sequences that start with the "escape glyph". */
5845
5846 /* the default escape glyph is \. */
5847 escape_glyph = '\\';
5848
5849 if (it->dp
5850 && INTEGERP (DISP_ESCAPE_GLYPH (it->dp))
5851 && GLYPH_CHAR_VALID_P (XFASTINT (DISP_ESCAPE_GLYPH (it->dp))))
5852 {
5853 escape_glyph = XFASTINT (DISP_ESCAPE_GLYPH (it->dp));
5854 lface_id = FAST_GLYPH_FACE (escape_glyph);
5855 }
5856 if (lface_id)
5857 {
5858 /* The display table specified a face.
5859 Merge it into face_id and also into escape_glyph. */
5860 escape_glyph = FAST_GLYPH_CHAR (escape_glyph);
5861 face_id = merge_faces (it->f, Qt, lface_id,
5862 it->face_id);
5863 }
5864 else if (it->f == last_escape_glyph_frame
5865 && it->face_id == last_escape_glyph_face_id)
5866 {
5867 face_id = last_escape_glyph_merged_face_id;
5868 }
5869 else
5870 {
5871 /* Merge the escape-glyph face into the current face. */
5872 face_id = merge_faces (it->f, Qescape_glyph, 0,
5873 it->face_id);
5874 last_escape_glyph_frame = it->f;
5875 last_escape_glyph_face_id = it->face_id;
5876 last_escape_glyph_merged_face_id = face_id;
5877 }
5878
5879 /* Handle soft hyphens in the mode where they only get
5880 highlighting. */
5881
5882 if (EQ (Vnobreak_char_display, Qt)
5883 && it->c == 0xAD)
5884 {
5885 g = it->c = '-';
5886 XSETINT (it->ctl_chars[0], g);
5887 ctl_len = 1;
5888 goto display_control;
5889 }
5890
5891 /* Handle non-break space and soft hyphen
5892 with the escape glyph. */
5893
5894 if (it->c == 0xA0 || it->c == 0xAD)
5895 {
5896 XSETINT (it->ctl_chars[0], escape_glyph);
5897 g = it->c = (it->c == 0xA0 ? ' ' : '-');
5898 XSETINT (it->ctl_chars[1], g);
5899 ctl_len = 2;
5900 goto display_control;
5901 }
5902
5903 {
5904 unsigned char str[MAX_MULTIBYTE_LENGTH];
5905 int len;
5906 int i;
5907
5908 /* Set IT->ctl_chars[0] to the glyph for `\\'. */
5909 if (CHAR_BYTE8_P (it->c))
5910 {
5911 str[0] = CHAR_TO_BYTE8 (it->c);
5912 len = 1;
5913 }
5914 else if (it->c < 256)
5915 {
5916 str[0] = it->c;
5917 len = 1;
5918 }
5919 else
5920 {
5921 /* It's an invalid character, which shouldn't
5922 happen actually, but due to bugs it may
5923 happen. Let's print the char as is, there's
5924 not much meaningful we can do with it. */
5925 str[0] = it->c;
5926 str[1] = it->c >> 8;
5927 str[2] = it->c >> 16;
5928 str[3] = it->c >> 24;
5929 len = 4;
5930 }
5931
5932 for (i = 0; i < len; i++)
5933 {
5934 XSETINT (it->ctl_chars[i * 4], escape_glyph);
5935 /* Insert three more glyphs into IT->ctl_chars for
5936 the octal display of the character. */
5937 g = ((str[i] >> 6) & 7) + '0';
5938 XSETINT (it->ctl_chars[i * 4 + 1], g);
5939 g = ((str[i] >> 3) & 7) + '0';
5940 XSETINT (it->ctl_chars[i * 4 + 2], g);
5941 g = (str[i] & 7) + '0';
5942 XSETINT (it->ctl_chars[i * 4 + 3], g);
5943 }
5944 ctl_len = len * 4;
5945 }
5946
5947 display_control:
5948 /* Set up IT->dpvec and return first character from it. */
5949 it->dpvec_char_len = it->len;
5950 it->dpvec = it->ctl_chars;
5951 it->dpend = it->dpvec + ctl_len;
5952 it->current.dpvec_index = 0;
5953 it->dpvec_face_id = face_id;
5954 it->saved_face_id = it->face_id;
5955 it->method = GET_FROM_DISPLAY_VECTOR;
5956 it->ellipsis_p = 0;
5957 goto get_next;
5958 }
5959 }
5960 }
5961
5962 /* Adjust face id for a multibyte character. There are no multibyte
5963 character in unibyte text. */
5964 if ((it->what == IT_CHARACTER || it->what == IT_COMPOSITION)
5965 && it->multibyte_p
5966 && success_p
5967 && FRAME_WINDOW_P (it->f))
5968 {
5969 struct face *face = FACE_FROM_ID (it->f, it->face_id);
5970 int pos = (it->s ? -1
5971 : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
5972 : IT_CHARPOS (*it));
5973
5974 it->face_id = FACE_FOR_CHAR (it->f, face, it->c, pos, it->string);
5975 }
5976
5977 /* Is this character the last one of a run of characters with
5978 box? If yes, set IT->end_of_box_run_p to 1. */
5979 if (it->face_box_p
5980 && it->s == NULL)
5981 {
5982 int face_id;
5983 struct face *face;
5984
5985 it->end_of_box_run_p
5986 = ((face_id = face_after_it_pos (it),
5987 face_id != it->face_id)
5988 && (face = FACE_FROM_ID (it->f, face_id),
5989 face->box == FACE_NO_BOX));
5990 }
5991
5992 /* Value is 0 if end of buffer or string reached. */
5993 return success_p;
5994 }
5995
5996
5997 /* Move IT to the next display element.
5998
5999 RESEAT_P non-zero means if called on a newline in buffer text,
6000 skip to the next visible line start.
6001
6002 Functions get_next_display_element and set_iterator_to_next are
6003 separate because I find this arrangement easier to handle than a
6004 get_next_display_element function that also increments IT's
6005 position. The way it is we can first look at an iterator's current
6006 display element, decide whether it fits on a line, and if it does,
6007 increment the iterator position. The other way around we probably
6008 would either need a flag indicating whether the iterator has to be
6009 incremented the next time, or we would have to implement a
6010 decrement position function which would not be easy to write. */
6011
6012 void
6013 set_iterator_to_next (it, reseat_p)
6014 struct it *it;
6015 int reseat_p;
6016 {
6017 /* Reset flags indicating start and end of a sequence of characters
6018 with box. Reset them at the start of this function because
6019 moving the iterator to a new position might set them. */
6020 it->start_of_box_run_p = it->end_of_box_run_p = 0;
6021
6022 switch (it->method)
6023 {
6024 case GET_FROM_BUFFER:
6025 /* The current display element of IT is a character from
6026 current_buffer. Advance in the buffer, and maybe skip over
6027 invisible lines that are so because of selective display. */
6028 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
6029 reseat_at_next_visible_line_start (it, 0);
6030 else
6031 {
6032 xassert (it->len != 0);
6033 IT_BYTEPOS (*it) += it->len;
6034 IT_CHARPOS (*it) += 1;
6035 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
6036 }
6037 break;
6038
6039 case GET_FROM_COMPOSITION:
6040 xassert (it->cmp_id >= 0 && it->cmp_id < n_compositions);
6041 xassert (it->sp > 0);
6042 pop_it (it);
6043 if (it->method == GET_FROM_STRING)
6044 {
6045 IT_STRING_BYTEPOS (*it) += it->len;
6046 IT_STRING_CHARPOS (*it) += it->cmp_len;
6047 goto consider_string_end;
6048 }
6049 else if (it->method == GET_FROM_BUFFER)
6050 {
6051 IT_BYTEPOS (*it) += it->len;
6052 IT_CHARPOS (*it) += it->cmp_len;
6053 }
6054 break;
6055
6056 case GET_FROM_C_STRING:
6057 /* Current display element of IT is from a C string. */
6058 IT_BYTEPOS (*it) += it->len;
6059 IT_CHARPOS (*it) += 1;
6060 break;
6061
6062 case GET_FROM_DISPLAY_VECTOR:
6063 /* Current display element of IT is from a display table entry.
6064 Advance in the display table definition. Reset it to null if
6065 end reached, and continue with characters from buffers/
6066 strings. */
6067 ++it->current.dpvec_index;
6068
6069 /* Restore face of the iterator to what they were before the
6070 display vector entry (these entries may contain faces). */
6071 it->face_id = it->saved_face_id;
6072
6073 if (it->dpvec + it->current.dpvec_index == it->dpend)
6074 {
6075 int recheck_faces = it->ellipsis_p;
6076
6077 if (it->s)
6078 it->method = GET_FROM_C_STRING;
6079 else if (STRINGP (it->string))
6080 it->method = GET_FROM_STRING;
6081 else
6082 {
6083 it->method = GET_FROM_BUFFER;
6084 it->object = it->w->buffer;
6085 }
6086
6087 it->dpvec = NULL;
6088 it->current.dpvec_index = -1;
6089
6090 /* Skip over characters which were displayed via IT->dpvec. */
6091 if (it->dpvec_char_len < 0)
6092 reseat_at_next_visible_line_start (it, 1);
6093 else if (it->dpvec_char_len > 0)
6094 {
6095 if (it->method == GET_FROM_STRING
6096 && it->n_overlay_strings > 0)
6097 it->ignore_overlay_strings_at_pos_p = 1;
6098 it->len = it->dpvec_char_len;
6099 set_iterator_to_next (it, reseat_p);
6100 }
6101
6102 /* Maybe recheck faces after display vector */
6103 if (recheck_faces)
6104 it->stop_charpos = IT_CHARPOS (*it);
6105 }
6106 break;
6107
6108 case GET_FROM_STRING:
6109 /* Current display element is a character from a Lisp string. */
6110 xassert (it->s == NULL && STRINGP (it->string));
6111 IT_STRING_BYTEPOS (*it) += it->len;
6112 IT_STRING_CHARPOS (*it) += 1;
6113
6114 consider_string_end:
6115
6116 if (it->current.overlay_string_index >= 0)
6117 {
6118 /* IT->string is an overlay string. Advance to the
6119 next, if there is one. */
6120 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
6121 next_overlay_string (it);
6122 }
6123 else
6124 {
6125 /* IT->string is not an overlay string. If we reached
6126 its end, and there is something on IT->stack, proceed
6127 with what is on the stack. This can be either another
6128 string, this time an overlay string, or a buffer. */
6129 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
6130 && it->sp > 0)
6131 {
6132 pop_it (it);
6133 if (it->method == GET_FROM_STRING)
6134 goto consider_string_end;
6135 }
6136 }
6137 break;
6138
6139 case GET_FROM_IMAGE:
6140 case GET_FROM_STRETCH:
6141 /* The position etc with which we have to proceed are on
6142 the stack. The position may be at the end of a string,
6143 if the `display' property takes up the whole string. */
6144 xassert (it->sp > 0);
6145 pop_it (it);
6146 if (it->method == GET_FROM_STRING)
6147 goto consider_string_end;
6148 break;
6149
6150 default:
6151 /* There are no other methods defined, so this should be a bug. */
6152 abort ();
6153 }
6154
6155 xassert (it->method != GET_FROM_STRING
6156 || (STRINGP (it->string)
6157 && IT_STRING_CHARPOS (*it) >= 0));
6158 }
6159
6160 /* Load IT's display element fields with information about the next
6161 display element which comes from a display table entry or from the
6162 result of translating a control character to one of the forms `^C'
6163 or `\003'.
6164
6165 IT->dpvec holds the glyphs to return as characters.
6166 IT->saved_face_id holds the face id before the display vector--
6167 it is restored into IT->face_idin set_iterator_to_next. */
6168
6169 static int
6170 next_element_from_display_vector (it)
6171 struct it *it;
6172 {
6173 /* Precondition. */
6174 xassert (it->dpvec && it->current.dpvec_index >= 0);
6175
6176 it->face_id = it->saved_face_id;
6177
6178 if (INTEGERP (*it->dpvec)
6179 && GLYPH_CHAR_VALID_P (XFASTINT (*it->dpvec)))
6180 {
6181 GLYPH g;
6182
6183 g = XFASTINT (it->dpvec[it->current.dpvec_index]);
6184 it->c = FAST_GLYPH_CHAR (g);
6185 it->len = CHAR_BYTES (it->c);
6186
6187 /* The entry may contain a face id to use. Such a face id is
6188 the id of a Lisp face, not a realized face. A face id of
6189 zero means no face is specified. */
6190 if (it->dpvec_face_id >= 0)
6191 it->face_id = it->dpvec_face_id;
6192 else
6193 {
6194 int lface_id = FAST_GLYPH_FACE (g);
6195 if (lface_id > 0)
6196 it->face_id = merge_faces (it->f, Qt, lface_id,
6197 it->saved_face_id);
6198 }
6199 }
6200 else
6201 /* Display table entry is invalid. Return a space. */
6202 it->c = ' ', it->len = 1;
6203
6204 /* Don't change position and object of the iterator here. They are
6205 still the values of the character that had this display table
6206 entry or was translated, and that's what we want. */
6207 it->what = IT_CHARACTER;
6208 return 1;
6209 }
6210
6211
6212 /* Load IT with the next display element from Lisp string IT->string.
6213 IT->current.string_pos is the current position within the string.
6214 If IT->current.overlay_string_index >= 0, the Lisp string is an
6215 overlay string. */
6216
6217 static int
6218 next_element_from_string (it)
6219 struct it *it;
6220 {
6221 struct text_pos position;
6222
6223 xassert (STRINGP (it->string));
6224 xassert (IT_STRING_CHARPOS (*it) >= 0);
6225 position = it->current.string_pos;
6226
6227 /* Time to check for invisible text? */
6228 if (IT_STRING_CHARPOS (*it) < it->end_charpos
6229 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
6230 {
6231 handle_stop (it);
6232
6233 /* Since a handler may have changed IT->method, we must
6234 recurse here. */
6235 return get_next_display_element (it);
6236 }
6237
6238 if (it->current.overlay_string_index >= 0)
6239 {
6240 /* Get the next character from an overlay string. In overlay
6241 strings, There is no field width or padding with spaces to
6242 do. */
6243 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
6244 {
6245 it->what = IT_EOB;
6246 return 0;
6247 }
6248 else if (STRING_MULTIBYTE (it->string))
6249 {
6250 int remaining = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
6251 const unsigned char *s = (SDATA (it->string)
6252 + IT_STRING_BYTEPOS (*it));
6253 it->c = string_char_and_length (s, remaining, &it->len);
6254 }
6255 else
6256 {
6257 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
6258 it->len = 1;
6259 }
6260 }
6261 else
6262 {
6263 /* Get the next character from a Lisp string that is not an
6264 overlay string. Such strings come from the mode line, for
6265 example. We may have to pad with spaces, or truncate the
6266 string. See also next_element_from_c_string. */
6267 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
6268 {
6269 it->what = IT_EOB;
6270 return 0;
6271 }
6272 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
6273 {
6274 /* Pad with spaces. */
6275 it->c = ' ', it->len = 1;
6276 CHARPOS (position) = BYTEPOS (position) = -1;
6277 }
6278 else if (STRING_MULTIBYTE (it->string))
6279 {
6280 int maxlen = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
6281 const unsigned char *s = (SDATA (it->string)
6282 + IT_STRING_BYTEPOS (*it));
6283 it->c = string_char_and_length (s, maxlen, &it->len);
6284 }
6285 else
6286 {
6287 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
6288 it->len = 1;
6289 }
6290 }
6291
6292 /* Record what we have and where it came from. */
6293 it->what = IT_CHARACTER;
6294 it->object = it->string;
6295 it->position = position;
6296 return 1;
6297 }
6298
6299
6300 /* Load IT with next display element from C string IT->s.
6301 IT->string_nchars is the maximum number of characters to return
6302 from the string. IT->end_charpos may be greater than
6303 IT->string_nchars when this function is called, in which case we
6304 may have to return padding spaces. Value is zero if end of string
6305 reached, including padding spaces. */
6306
6307 static int
6308 next_element_from_c_string (it)
6309 struct it *it;
6310 {
6311 int success_p = 1;
6312
6313 xassert (it->s);
6314 it->what = IT_CHARACTER;
6315 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
6316 it->object = Qnil;
6317
6318 /* IT's position can be greater IT->string_nchars in case a field
6319 width or precision has been specified when the iterator was
6320 initialized. */
6321 if (IT_CHARPOS (*it) >= it->end_charpos)
6322 {
6323 /* End of the game. */
6324 it->what = IT_EOB;
6325 success_p = 0;
6326 }
6327 else if (IT_CHARPOS (*it) >= it->string_nchars)
6328 {
6329 /* Pad with spaces. */
6330 it->c = ' ', it->len = 1;
6331 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
6332 }
6333 else if (it->multibyte_p)
6334 {
6335 /* Implementation note: The calls to strlen apparently aren't a
6336 performance problem because there is no noticeable performance
6337 difference between Emacs running in unibyte or multibyte mode. */
6338 int maxlen = strlen (it->s) - IT_BYTEPOS (*it);
6339 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it),
6340 maxlen, &it->len);
6341 }
6342 else
6343 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
6344
6345 return success_p;
6346 }
6347
6348
6349 /* Set up IT to return characters from an ellipsis, if appropriate.
6350 The definition of the ellipsis glyphs may come from a display table
6351 entry. This function Fills IT with the first glyph from the
6352 ellipsis if an ellipsis is to be displayed. */
6353
6354 static int
6355 next_element_from_ellipsis (it)
6356 struct it *it;
6357 {
6358 if (it->selective_display_ellipsis_p)
6359 setup_for_ellipsis (it, it->len);
6360 else
6361 {
6362 /* The face at the current position may be different from the
6363 face we find after the invisible text. Remember what it
6364 was in IT->saved_face_id, and signal that it's there by
6365 setting face_before_selective_p. */
6366 it->saved_face_id = it->face_id;
6367 it->method = GET_FROM_BUFFER;
6368 it->object = it->w->buffer;
6369 reseat_at_next_visible_line_start (it, 1);
6370 it->face_before_selective_p = 1;
6371 }
6372
6373 return get_next_display_element (it);
6374 }
6375
6376
6377 /* Deliver an image display element. The iterator IT is already
6378 filled with image information (done in handle_display_prop). Value
6379 is always 1. */
6380
6381
6382 static int
6383 next_element_from_image (it)
6384 struct it *it;
6385 {
6386 it->what = IT_IMAGE;
6387 return 1;
6388 }
6389
6390
6391 /* Fill iterator IT with next display element from a stretch glyph
6392 property. IT->object is the value of the text property. Value is
6393 always 1. */
6394
6395 static int
6396 next_element_from_stretch (it)
6397 struct it *it;
6398 {
6399 it->what = IT_STRETCH;
6400 return 1;
6401 }
6402
6403
6404 /* Load IT with the next display element from current_buffer. Value
6405 is zero if end of buffer reached. IT->stop_charpos is the next
6406 position at which to stop and check for text properties or buffer
6407 end. */
6408
6409 static int
6410 next_element_from_buffer (it)
6411 struct it *it;
6412 {
6413 int success_p = 1;
6414
6415 /* Check this assumption, otherwise, we would never enter the
6416 if-statement, below. */
6417 xassert (IT_CHARPOS (*it) >= BEGV
6418 && IT_CHARPOS (*it) <= it->stop_charpos);
6419
6420 if (IT_CHARPOS (*it) >= it->stop_charpos)
6421 {
6422 if (IT_CHARPOS (*it) >= it->end_charpos)
6423 {
6424 int overlay_strings_follow_p;
6425
6426 /* End of the game, except when overlay strings follow that
6427 haven't been returned yet. */
6428 if (it->overlay_strings_at_end_processed_p)
6429 overlay_strings_follow_p = 0;
6430 else
6431 {
6432 it->overlay_strings_at_end_processed_p = 1;
6433 overlay_strings_follow_p = get_overlay_strings (it, 0);
6434 }
6435
6436 if (overlay_strings_follow_p)
6437 success_p = get_next_display_element (it);
6438 else
6439 {
6440 it->what = IT_EOB;
6441 it->position = it->current.pos;
6442 success_p = 0;
6443 }
6444 }
6445 else
6446 {
6447 handle_stop (it);
6448 return get_next_display_element (it);
6449 }
6450 }
6451 else
6452 {
6453 /* No face changes, overlays etc. in sight, so just return a
6454 character from current_buffer. */
6455 unsigned char *p;
6456
6457 /* Maybe run the redisplay end trigger hook. Performance note:
6458 This doesn't seem to cost measurable time. */
6459 if (it->redisplay_end_trigger_charpos
6460 && it->glyph_row
6461 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
6462 run_redisplay_end_trigger_hook (it);
6463
6464 /* Get the next character, maybe multibyte. */
6465 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
6466 if (it->multibyte_p && !ASCII_BYTE_P (*p))
6467 {
6468 int maxlen = ((IT_BYTEPOS (*it) >= GPT_BYTE ? ZV_BYTE : GPT_BYTE)
6469 - IT_BYTEPOS (*it));
6470 it->c = string_char_and_length (p, maxlen, &it->len);
6471 }
6472 else
6473 it->c = *p, it->len = 1;
6474
6475 /* Record what we have and where it came from. */
6476 it->what = IT_CHARACTER;
6477 it->object = it->w->buffer;
6478 it->position = it->current.pos;
6479
6480 /* Normally we return the character found above, except when we
6481 really want to return an ellipsis for selective display. */
6482 if (it->selective)
6483 {
6484 if (it->c == '\n')
6485 {
6486 /* A value of selective > 0 means hide lines indented more
6487 than that number of columns. */
6488 if (it->selective > 0
6489 && IT_CHARPOS (*it) + 1 < ZV
6490 && indented_beyond_p (IT_CHARPOS (*it) + 1,
6491 IT_BYTEPOS (*it) + 1,
6492 (double) it->selective)) /* iftc */
6493 {
6494 success_p = next_element_from_ellipsis (it);
6495 it->dpvec_char_len = -1;
6496 }
6497 }
6498 else if (it->c == '\r' && it->selective == -1)
6499 {
6500 /* A value of selective == -1 means that everything from the
6501 CR to the end of the line is invisible, with maybe an
6502 ellipsis displayed for it. */
6503 success_p = next_element_from_ellipsis (it);
6504 it->dpvec_char_len = -1;
6505 }
6506 }
6507 }
6508
6509 /* Value is zero if end of buffer reached. */
6510 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
6511 return success_p;
6512 }
6513
6514
6515 /* Run the redisplay end trigger hook for IT. */
6516
6517 static void
6518 run_redisplay_end_trigger_hook (it)
6519 struct it *it;
6520 {
6521 Lisp_Object args[3];
6522
6523 /* IT->glyph_row should be non-null, i.e. we should be actually
6524 displaying something, or otherwise we should not run the hook. */
6525 xassert (it->glyph_row);
6526
6527 /* Set up hook arguments. */
6528 args[0] = Qredisplay_end_trigger_functions;
6529 args[1] = it->window;
6530 XSETINT (args[2], it->redisplay_end_trigger_charpos);
6531 it->redisplay_end_trigger_charpos = 0;
6532
6533 /* Since we are *trying* to run these functions, don't try to run
6534 them again, even if they get an error. */
6535 it->w->redisplay_end_trigger = Qnil;
6536 Frun_hook_with_args (3, args);
6537
6538 /* Notice if it changed the face of the character we are on. */
6539 handle_face_prop (it);
6540 }
6541
6542
6543 /* Deliver a composition display element. The iterator IT is already
6544 filled with composition information (done in
6545 handle_composition_prop). Value is always 1. */
6546
6547 static int
6548 next_element_from_composition (it)
6549 struct it *it;
6550 {
6551 it->what = IT_COMPOSITION;
6552 it->position = (STRINGP (it->string)
6553 ? it->current.string_pos
6554 : it->current.pos);
6555 if (STRINGP (it->string))
6556 it->object = it->string;
6557 else
6558 it->object = it->w->buffer;
6559 return 1;
6560 }
6561
6562
6563 \f
6564 /***********************************************************************
6565 Moving an iterator without producing glyphs
6566 ***********************************************************************/
6567
6568 /* Check if iterator is at a position corresponding to a valid buffer
6569 position after some move_it_ call. */
6570
6571 #define IT_POS_VALID_AFTER_MOVE_P(it) \
6572 ((it)->method == GET_FROM_STRING \
6573 ? IT_STRING_CHARPOS (*it) == 0 \
6574 : 1)
6575
6576
6577 /* Move iterator IT to a specified buffer or X position within one
6578 line on the display without producing glyphs.
6579
6580 OP should be a bit mask including some or all of these bits:
6581 MOVE_TO_X: Stop on reaching x-position TO_X.
6582 MOVE_TO_POS: Stop on reaching buffer or string position TO_CHARPOS.
6583 Regardless of OP's value, stop in reaching the end of the display line.
6584
6585 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
6586 This means, in particular, that TO_X includes window's horizontal
6587 scroll amount.
6588
6589 The return value has several possible values that
6590 say what condition caused the scan to stop:
6591
6592 MOVE_POS_MATCH_OR_ZV
6593 - when TO_POS or ZV was reached.
6594
6595 MOVE_X_REACHED
6596 -when TO_X was reached before TO_POS or ZV were reached.
6597
6598 MOVE_LINE_CONTINUED
6599 - when we reached the end of the display area and the line must
6600 be continued.
6601
6602 MOVE_LINE_TRUNCATED
6603 - when we reached the end of the display area and the line is
6604 truncated.
6605
6606 MOVE_NEWLINE_OR_CR
6607 - when we stopped at a line end, i.e. a newline or a CR and selective
6608 display is on. */
6609
6610 static enum move_it_result
6611 move_it_in_display_line_to (it, to_charpos, to_x, op)
6612 struct it *it;
6613 int to_charpos, to_x, op;
6614 {
6615 enum move_it_result result = MOVE_UNDEFINED;
6616 struct glyph_row *saved_glyph_row;
6617
6618 /* Don't produce glyphs in produce_glyphs. */
6619 saved_glyph_row = it->glyph_row;
6620 it->glyph_row = NULL;
6621
6622 #define BUFFER_POS_REACHED_P() \
6623 ((op & MOVE_TO_POS) != 0 \
6624 && BUFFERP (it->object) \
6625 && IT_CHARPOS (*it) >= to_charpos \
6626 && (it->method == GET_FROM_BUFFER \
6627 || (it->method == GET_FROM_DISPLAY_VECTOR \
6628 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
6629
6630
6631 while (1)
6632 {
6633 int x, i, ascent = 0, descent = 0;
6634
6635 /* Stop if we move beyond TO_CHARPOS (after an image or stretch glyph). */
6636 if ((op & MOVE_TO_POS) != 0
6637 && BUFFERP (it->object)
6638 && it->method == GET_FROM_BUFFER
6639 && IT_CHARPOS (*it) > to_charpos)
6640 {
6641 result = MOVE_POS_MATCH_OR_ZV;
6642 break;
6643 }
6644
6645 /* Stop when ZV reached.
6646 We used to stop here when TO_CHARPOS reached as well, but that is
6647 too soon if this glyph does not fit on this line. So we handle it
6648 explicitly below. */
6649 if (!get_next_display_element (it)
6650 || (it->truncate_lines_p
6651 && BUFFER_POS_REACHED_P ()))
6652 {
6653 result = MOVE_POS_MATCH_OR_ZV;
6654 break;
6655 }
6656
6657 /* The call to produce_glyphs will get the metrics of the
6658 display element IT is loaded with. We record in x the
6659 x-position before this display element in case it does not
6660 fit on the line. */
6661 x = it->current_x;
6662
6663 /* Remember the line height so far in case the next element doesn't
6664 fit on the line. */
6665 if (!it->truncate_lines_p)
6666 {
6667 ascent = it->max_ascent;
6668 descent = it->max_descent;
6669 }
6670
6671 PRODUCE_GLYPHS (it);
6672
6673 if (it->area != TEXT_AREA)
6674 {
6675 set_iterator_to_next (it, 1);
6676 continue;
6677 }
6678
6679 /* The number of glyphs we get back in IT->nglyphs will normally
6680 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
6681 character on a terminal frame, or (iii) a line end. For the
6682 second case, IT->nglyphs - 1 padding glyphs will be present
6683 (on X frames, there is only one glyph produced for a
6684 composite character.
6685
6686 The behavior implemented below means, for continuation lines,
6687 that as many spaces of a TAB as fit on the current line are
6688 displayed there. For terminal frames, as many glyphs of a
6689 multi-glyph character are displayed in the current line, too.
6690 This is what the old redisplay code did, and we keep it that
6691 way. Under X, the whole shape of a complex character must
6692 fit on the line or it will be completely displayed in the
6693 next line.
6694
6695 Note that both for tabs and padding glyphs, all glyphs have
6696 the same width. */
6697 if (it->nglyphs)
6698 {
6699 /* More than one glyph or glyph doesn't fit on line. All
6700 glyphs have the same width. */
6701 int single_glyph_width = it->pixel_width / it->nglyphs;
6702 int new_x;
6703 int x_before_this_char = x;
6704 int hpos_before_this_char = it->hpos;
6705
6706 for (i = 0; i < it->nglyphs; ++i, x = new_x)
6707 {
6708 new_x = x + single_glyph_width;
6709
6710 /* We want to leave anything reaching TO_X to the caller. */
6711 if ((op & MOVE_TO_X) && new_x > to_x)
6712 {
6713 if (BUFFER_POS_REACHED_P ())
6714 goto buffer_pos_reached;
6715 it->current_x = x;
6716 result = MOVE_X_REACHED;
6717 break;
6718 }
6719 else if (/* Lines are continued. */
6720 !it->truncate_lines_p
6721 && (/* And glyph doesn't fit on the line. */
6722 new_x > it->last_visible_x
6723 /* Or it fits exactly and we're on a window
6724 system frame. */
6725 || (new_x == it->last_visible_x
6726 && FRAME_WINDOW_P (it->f))))
6727 {
6728 if (/* IT->hpos == 0 means the very first glyph
6729 doesn't fit on the line, e.g. a wide image. */
6730 it->hpos == 0
6731 || (new_x == it->last_visible_x
6732 && FRAME_WINDOW_P (it->f)))
6733 {
6734 ++it->hpos;
6735 it->current_x = new_x;
6736
6737 /* The character's last glyph just barely fits
6738 in this row. */
6739 if (i == it->nglyphs - 1)
6740 {
6741 /* If this is the destination position,
6742 return a position *before* it in this row,
6743 now that we know it fits in this row. */
6744 if (BUFFER_POS_REACHED_P ())
6745 {
6746 it->hpos = hpos_before_this_char;
6747 it->current_x = x_before_this_char;
6748 result = MOVE_POS_MATCH_OR_ZV;
6749 break;
6750 }
6751
6752 set_iterator_to_next (it, 1);
6753 #ifdef HAVE_WINDOW_SYSTEM
6754 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
6755 {
6756 if (!get_next_display_element (it))
6757 {
6758 result = MOVE_POS_MATCH_OR_ZV;
6759 break;
6760 }
6761 if (BUFFER_POS_REACHED_P ())
6762 {
6763 if (ITERATOR_AT_END_OF_LINE_P (it))
6764 result = MOVE_POS_MATCH_OR_ZV;
6765 else
6766 result = MOVE_LINE_CONTINUED;
6767 break;
6768 }
6769 if (ITERATOR_AT_END_OF_LINE_P (it))
6770 {
6771 result = MOVE_NEWLINE_OR_CR;
6772 break;
6773 }
6774 }
6775 #endif /* HAVE_WINDOW_SYSTEM */
6776 }
6777 }
6778 else
6779 {
6780 it->current_x = x;
6781 it->max_ascent = ascent;
6782 it->max_descent = descent;
6783 }
6784
6785 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
6786 IT_CHARPOS (*it)));
6787 result = MOVE_LINE_CONTINUED;
6788 break;
6789 }
6790 else if (BUFFER_POS_REACHED_P ())
6791 goto buffer_pos_reached;
6792 else if (new_x > it->first_visible_x)
6793 {
6794 /* Glyph is visible. Increment number of glyphs that
6795 would be displayed. */
6796 ++it->hpos;
6797 }
6798 else
6799 {
6800 /* Glyph is completely off the left margin of the display
6801 area. Nothing to do. */
6802 }
6803 }
6804
6805 if (result != MOVE_UNDEFINED)
6806 break;
6807 }
6808 else if (BUFFER_POS_REACHED_P ())
6809 {
6810 buffer_pos_reached:
6811 it->current_x = x;
6812 it->max_ascent = ascent;
6813 it->max_descent = descent;
6814 result = MOVE_POS_MATCH_OR_ZV;
6815 break;
6816 }
6817 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
6818 {
6819 /* Stop when TO_X specified and reached. This check is
6820 necessary here because of lines consisting of a line end,
6821 only. The line end will not produce any glyphs and we
6822 would never get MOVE_X_REACHED. */
6823 xassert (it->nglyphs == 0);
6824 result = MOVE_X_REACHED;
6825 break;
6826 }
6827
6828 /* Is this a line end? If yes, we're done. */
6829 if (ITERATOR_AT_END_OF_LINE_P (it))
6830 {
6831 result = MOVE_NEWLINE_OR_CR;
6832 break;
6833 }
6834
6835 /* The current display element has been consumed. Advance
6836 to the next. */
6837 set_iterator_to_next (it, 1);
6838
6839 /* Stop if lines are truncated and IT's current x-position is
6840 past the right edge of the window now. */
6841 if (it->truncate_lines_p
6842 && it->current_x >= it->last_visible_x)
6843 {
6844 #ifdef HAVE_WINDOW_SYSTEM
6845 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
6846 {
6847 if (!get_next_display_element (it)
6848 || BUFFER_POS_REACHED_P ())
6849 {
6850 result = MOVE_POS_MATCH_OR_ZV;
6851 break;
6852 }
6853 if (ITERATOR_AT_END_OF_LINE_P (it))
6854 {
6855 result = MOVE_NEWLINE_OR_CR;
6856 break;
6857 }
6858 }
6859 #endif /* HAVE_WINDOW_SYSTEM */
6860 result = MOVE_LINE_TRUNCATED;
6861 break;
6862 }
6863 }
6864
6865 #undef BUFFER_POS_REACHED_P
6866
6867 /* Restore the iterator settings altered at the beginning of this
6868 function. */
6869 it->glyph_row = saved_glyph_row;
6870 return result;
6871 }
6872
6873
6874 /* Move IT forward until it satisfies one or more of the criteria in
6875 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
6876
6877 OP is a bit-mask that specifies where to stop, and in particular,
6878 which of those four position arguments makes a difference. See the
6879 description of enum move_operation_enum.
6880
6881 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
6882 screen line, this function will set IT to the next position >
6883 TO_CHARPOS. */
6884
6885 void
6886 move_it_to (it, to_charpos, to_x, to_y, to_vpos, op)
6887 struct it *it;
6888 int to_charpos, to_x, to_y, to_vpos;
6889 int op;
6890 {
6891 enum move_it_result skip, skip2 = MOVE_X_REACHED;
6892 int line_height;
6893 int reached = 0;
6894
6895 for (;;)
6896 {
6897 if (op & MOVE_TO_VPOS)
6898 {
6899 /* If no TO_CHARPOS and no TO_X specified, stop at the
6900 start of the line TO_VPOS. */
6901 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
6902 {
6903 if (it->vpos == to_vpos)
6904 {
6905 reached = 1;
6906 break;
6907 }
6908 else
6909 skip = move_it_in_display_line_to (it, -1, -1, 0);
6910 }
6911 else
6912 {
6913 /* TO_VPOS >= 0 means stop at TO_X in the line at
6914 TO_VPOS, or at TO_POS, whichever comes first. */
6915 if (it->vpos == to_vpos)
6916 {
6917 reached = 2;
6918 break;
6919 }
6920
6921 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
6922
6923 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
6924 {
6925 reached = 3;
6926 break;
6927 }
6928 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
6929 {
6930 /* We have reached TO_X but not in the line we want. */
6931 skip = move_it_in_display_line_to (it, to_charpos,
6932 -1, MOVE_TO_POS);
6933 if (skip == MOVE_POS_MATCH_OR_ZV)
6934 {
6935 reached = 4;
6936 break;
6937 }
6938 }
6939 }
6940 }
6941 else if (op & MOVE_TO_Y)
6942 {
6943 struct it it_backup;
6944
6945 /* TO_Y specified means stop at TO_X in the line containing
6946 TO_Y---or at TO_CHARPOS if this is reached first. The
6947 problem is that we can't really tell whether the line
6948 contains TO_Y before we have completely scanned it, and
6949 this may skip past TO_X. What we do is to first scan to
6950 TO_X.
6951
6952 If TO_X is not specified, use a TO_X of zero. The reason
6953 is to make the outcome of this function more predictable.
6954 If we didn't use TO_X == 0, we would stop at the end of
6955 the line which is probably not what a caller would expect
6956 to happen. */
6957 skip = move_it_in_display_line_to (it, to_charpos,
6958 ((op & MOVE_TO_X)
6959 ? to_x : 0),
6960 (MOVE_TO_X
6961 | (op & MOVE_TO_POS)));
6962
6963 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
6964 if (skip == MOVE_POS_MATCH_OR_ZV)
6965 {
6966 reached = 5;
6967 break;
6968 }
6969
6970 /* If TO_X was reached, we would like to know whether TO_Y
6971 is in the line. This can only be said if we know the
6972 total line height which requires us to scan the rest of
6973 the line. */
6974 if (skip == MOVE_X_REACHED)
6975 {
6976 /* Wait! We can conclude that TO_Y is in the line if
6977 the already scanned glyphs make the line tall enough
6978 because further scanning doesn't make it shorter. */
6979 line_height = it->max_ascent + it->max_descent;
6980 if (to_y >= it->current_y
6981 && to_y < it->current_y + line_height)
6982 {
6983 reached = 6;
6984 break;
6985 }
6986 it_backup = *it;
6987 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
6988 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
6989 op & MOVE_TO_POS);
6990 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
6991 }
6992
6993 /* Now, decide whether TO_Y is in this line. */
6994 line_height = it->max_ascent + it->max_descent;
6995 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
6996
6997 if (to_y >= it->current_y
6998 && to_y < it->current_y + line_height)
6999 {
7000 if (skip == MOVE_X_REACHED)
7001 /* If TO_Y is in this line and TO_X was reached above,
7002 we scanned too far. We have to restore IT's settings
7003 to the ones before skipping. */
7004 *it = it_backup;
7005 reached = 6;
7006 }
7007 else if (skip == MOVE_X_REACHED)
7008 {
7009 skip = skip2;
7010 if (skip == MOVE_POS_MATCH_OR_ZV)
7011 reached = 7;
7012 }
7013
7014 if (reached)
7015 break;
7016 }
7017 else if (BUFFERP (it->object)
7018 && it->method == GET_FROM_BUFFER
7019 && IT_CHARPOS (*it) >= to_charpos)
7020 skip = MOVE_POS_MATCH_OR_ZV;
7021 else
7022 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
7023
7024 switch (skip)
7025 {
7026 case MOVE_POS_MATCH_OR_ZV:
7027 reached = 8;
7028 goto out;
7029
7030 case MOVE_NEWLINE_OR_CR:
7031 set_iterator_to_next (it, 1);
7032 it->continuation_lines_width = 0;
7033 break;
7034
7035 case MOVE_LINE_TRUNCATED:
7036 it->continuation_lines_width = 0;
7037 reseat_at_next_visible_line_start (it, 0);
7038 if ((op & MOVE_TO_POS) != 0
7039 && IT_CHARPOS (*it) > to_charpos)
7040 {
7041 reached = 9;
7042 goto out;
7043 }
7044 break;
7045
7046 case MOVE_LINE_CONTINUED:
7047 /* For continued lines ending in a tab, some of the glyphs
7048 associated with the tab are displayed on the current
7049 line. Since it->current_x does not include these glyphs,
7050 we use it->last_visible_x instead. */
7051 it->continuation_lines_width +=
7052 (it->c == '\t') ? it->last_visible_x : it->current_x;
7053 break;
7054
7055 default:
7056 abort ();
7057 }
7058
7059 /* Reset/increment for the next run. */
7060 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
7061 it->current_x = it->hpos = 0;
7062 it->current_y += it->max_ascent + it->max_descent;
7063 ++it->vpos;
7064 last_height = it->max_ascent + it->max_descent;
7065 last_max_ascent = it->max_ascent;
7066 it->max_ascent = it->max_descent = 0;
7067 }
7068
7069 out:
7070
7071 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
7072 }
7073
7074
7075 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
7076
7077 If DY > 0, move IT backward at least that many pixels. DY = 0
7078 means move IT backward to the preceding line start or BEGV. This
7079 function may move over more than DY pixels if IT->current_y - DY
7080 ends up in the middle of a line; in this case IT->current_y will be
7081 set to the top of the line moved to. */
7082
7083 void
7084 move_it_vertically_backward (it, dy)
7085 struct it *it;
7086 int dy;
7087 {
7088 int nlines, h;
7089 struct it it2, it3;
7090 int start_pos;
7091
7092 move_further_back:
7093 xassert (dy >= 0);
7094
7095 start_pos = IT_CHARPOS (*it);
7096
7097 /* Estimate how many newlines we must move back. */
7098 nlines = max (1, dy / FRAME_LINE_HEIGHT (it->f));
7099
7100 /* Set the iterator's position that many lines back. */
7101 while (nlines-- && IT_CHARPOS (*it) > BEGV)
7102 back_to_previous_visible_line_start (it);
7103
7104 /* Reseat the iterator here. When moving backward, we don't want
7105 reseat to skip forward over invisible text, set up the iterator
7106 to deliver from overlay strings at the new position etc. So,
7107 use reseat_1 here. */
7108 reseat_1 (it, it->current.pos, 1);
7109
7110 /* We are now surely at a line start. */
7111 it->current_x = it->hpos = 0;
7112 it->continuation_lines_width = 0;
7113
7114 /* Move forward and see what y-distance we moved. First move to the
7115 start of the next line so that we get its height. We need this
7116 height to be able to tell whether we reached the specified
7117 y-distance. */
7118 it2 = *it;
7119 it2.max_ascent = it2.max_descent = 0;
7120 do
7121 {
7122 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
7123 MOVE_TO_POS | MOVE_TO_VPOS);
7124 }
7125 while (!IT_POS_VALID_AFTER_MOVE_P (&it2));
7126 xassert (IT_CHARPOS (*it) >= BEGV);
7127 it3 = it2;
7128
7129 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
7130 xassert (IT_CHARPOS (*it) >= BEGV);
7131 /* H is the actual vertical distance from the position in *IT
7132 and the starting position. */
7133 h = it2.current_y - it->current_y;
7134 /* NLINES is the distance in number of lines. */
7135 nlines = it2.vpos - it->vpos;
7136
7137 /* Correct IT's y and vpos position
7138 so that they are relative to the starting point. */
7139 it->vpos -= nlines;
7140 it->current_y -= h;
7141
7142 if (dy == 0)
7143 {
7144 /* DY == 0 means move to the start of the screen line. The
7145 value of nlines is > 0 if continuation lines were involved. */
7146 if (nlines > 0)
7147 move_it_by_lines (it, nlines, 1);
7148 #if 0
7149 /* I think this assert is bogus if buffer contains
7150 invisible text or images. KFS. */
7151 xassert (IT_CHARPOS (*it) <= start_pos);
7152 #endif
7153 }
7154 else
7155 {
7156 /* The y-position we try to reach, relative to *IT.
7157 Note that H has been subtracted in front of the if-statement. */
7158 int target_y = it->current_y + h - dy;
7159 int y0 = it3.current_y;
7160 int y1 = line_bottom_y (&it3);
7161 int line_height = y1 - y0;
7162
7163 /* If we did not reach target_y, try to move further backward if
7164 we can. If we moved too far backward, try to move forward. */
7165 if (target_y < it->current_y
7166 /* This is heuristic. In a window that's 3 lines high, with
7167 a line height of 13 pixels each, recentering with point
7168 on the bottom line will try to move -39/2 = 19 pixels
7169 backward. Try to avoid moving into the first line. */
7170 && (it->current_y - target_y
7171 > min (window_box_height (it->w), line_height * 2 / 3))
7172 && IT_CHARPOS (*it) > BEGV)
7173 {
7174 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
7175 target_y - it->current_y));
7176 dy = it->current_y - target_y;
7177 goto move_further_back;
7178 }
7179 else if (target_y >= it->current_y + line_height
7180 && IT_CHARPOS (*it) < ZV)
7181 {
7182 /* Should move forward by at least one line, maybe more.
7183
7184 Note: Calling move_it_by_lines can be expensive on
7185 terminal frames, where compute_motion is used (via
7186 vmotion) to do the job, when there are very long lines
7187 and truncate-lines is nil. That's the reason for
7188 treating terminal frames specially here. */
7189
7190 if (!FRAME_WINDOW_P (it->f))
7191 move_it_vertically (it, target_y - (it->current_y + line_height));
7192 else
7193 {
7194 do
7195 {
7196 move_it_by_lines (it, 1, 1);
7197 }
7198 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
7199 }
7200
7201 #if 0
7202 /* I think this assert is bogus if buffer contains
7203 invisible text or images. KFS. */
7204 xassert (IT_CHARPOS (*it) >= BEGV);
7205 #endif
7206 }
7207 }
7208 }
7209
7210
7211 /* Move IT by a specified amount of pixel lines DY. DY negative means
7212 move backwards. DY = 0 means move to start of screen line. At the
7213 end, IT will be on the start of a screen line. */
7214
7215 void
7216 move_it_vertically (it, dy)
7217 struct it *it;
7218 int dy;
7219 {
7220 if (dy <= 0)
7221 move_it_vertically_backward (it, -dy);
7222 else
7223 {
7224 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
7225 move_it_to (it, ZV, -1, it->current_y + dy, -1,
7226 MOVE_TO_POS | MOVE_TO_Y);
7227 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
7228
7229 /* If buffer ends in ZV without a newline, move to the start of
7230 the line to satisfy the post-condition. */
7231 if (IT_CHARPOS (*it) == ZV
7232 && ZV > BEGV
7233 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
7234 move_it_by_lines (it, 0, 0);
7235 }
7236 }
7237
7238
7239 /* Move iterator IT past the end of the text line it is in. */
7240
7241 void
7242 move_it_past_eol (it)
7243 struct it *it;
7244 {
7245 enum move_it_result rc;
7246
7247 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
7248 if (rc == MOVE_NEWLINE_OR_CR)
7249 set_iterator_to_next (it, 0);
7250 }
7251
7252
7253 #if 0 /* Currently not used. */
7254
7255 /* Return non-zero if some text between buffer positions START_CHARPOS
7256 and END_CHARPOS is invisible. IT->window is the window for text
7257 property lookup. */
7258
7259 static int
7260 invisible_text_between_p (it, start_charpos, end_charpos)
7261 struct it *it;
7262 int start_charpos, end_charpos;
7263 {
7264 Lisp_Object prop, limit;
7265 int invisible_found_p;
7266
7267 xassert (it != NULL && start_charpos <= end_charpos);
7268
7269 /* Is text at START invisible? */
7270 prop = Fget_char_property (make_number (start_charpos), Qinvisible,
7271 it->window);
7272 if (TEXT_PROP_MEANS_INVISIBLE (prop))
7273 invisible_found_p = 1;
7274 else
7275 {
7276 limit = Fnext_single_char_property_change (make_number (start_charpos),
7277 Qinvisible, Qnil,
7278 make_number (end_charpos));
7279 invisible_found_p = XFASTINT (limit) < end_charpos;
7280 }
7281
7282 return invisible_found_p;
7283 }
7284
7285 #endif /* 0 */
7286
7287
7288 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
7289 negative means move up. DVPOS == 0 means move to the start of the
7290 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
7291 NEED_Y_P is zero, IT->current_y will be left unchanged.
7292
7293 Further optimization ideas: If we would know that IT->f doesn't use
7294 a face with proportional font, we could be faster for
7295 truncate-lines nil. */
7296
7297 void
7298 move_it_by_lines (it, dvpos, need_y_p)
7299 struct it *it;
7300 int dvpos, need_y_p;
7301 {
7302 struct position pos;
7303
7304 /* The commented-out optimization uses vmotion on terminals. This
7305 gives bad results, because elements like it->what, on which
7306 callers such as pos_visible_p rely, aren't updated. */
7307 /* if (!FRAME_WINDOW_P (it->f))
7308 {
7309 struct text_pos textpos;
7310
7311 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
7312 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
7313 reseat (it, textpos, 1);
7314 it->vpos += pos.vpos;
7315 it->current_y += pos.vpos;
7316 }
7317 else */
7318
7319 if (dvpos == 0)
7320 {
7321 /* DVPOS == 0 means move to the start of the screen line. */
7322 move_it_vertically_backward (it, 0);
7323 xassert (it->current_x == 0 && it->hpos == 0);
7324 /* Let next call to line_bottom_y calculate real line height */
7325 last_height = 0;
7326 }
7327 else if (dvpos > 0)
7328 {
7329 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
7330 if (!IT_POS_VALID_AFTER_MOVE_P (it))
7331 move_it_to (it, IT_CHARPOS (*it) + 1, -1, -1, -1, MOVE_TO_POS);
7332 }
7333 else
7334 {
7335 struct it it2;
7336 int start_charpos, i;
7337
7338 /* Start at the beginning of the screen line containing IT's
7339 position. This may actually move vertically backwards,
7340 in case of overlays, so adjust dvpos accordingly. */
7341 dvpos += it->vpos;
7342 move_it_vertically_backward (it, 0);
7343 dvpos -= it->vpos;
7344
7345 /* Go back -DVPOS visible lines and reseat the iterator there. */
7346 start_charpos = IT_CHARPOS (*it);
7347 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > BEGV; --i)
7348 back_to_previous_visible_line_start (it);
7349 reseat (it, it->current.pos, 1);
7350
7351 /* Move further back if we end up in a string or an image. */
7352 while (!IT_POS_VALID_AFTER_MOVE_P (it))
7353 {
7354 /* First try to move to start of display line. */
7355 dvpos += it->vpos;
7356 move_it_vertically_backward (it, 0);
7357 dvpos -= it->vpos;
7358 if (IT_POS_VALID_AFTER_MOVE_P (it))
7359 break;
7360 /* If start of line is still in string or image,
7361 move further back. */
7362 back_to_previous_visible_line_start (it);
7363 reseat (it, it->current.pos, 1);
7364 dvpos--;
7365 }
7366
7367 it->current_x = it->hpos = 0;
7368
7369 /* Above call may have moved too far if continuation lines
7370 are involved. Scan forward and see if it did. */
7371 it2 = *it;
7372 it2.vpos = it2.current_y = 0;
7373 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
7374 it->vpos -= it2.vpos;
7375 it->current_y -= it2.current_y;
7376 it->current_x = it->hpos = 0;
7377
7378 /* If we moved too far back, move IT some lines forward. */
7379 if (it2.vpos > -dvpos)
7380 {
7381 int delta = it2.vpos + dvpos;
7382 it2 = *it;
7383 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
7384 /* Move back again if we got too far ahead. */
7385 if (IT_CHARPOS (*it) >= start_charpos)
7386 *it = it2;
7387 }
7388 }
7389 }
7390
7391 /* Return 1 if IT points into the middle of a display vector. */
7392
7393 int
7394 in_display_vector_p (it)
7395 struct it *it;
7396 {
7397 return (it->method == GET_FROM_DISPLAY_VECTOR
7398 && it->current.dpvec_index > 0
7399 && it->dpvec + it->current.dpvec_index != it->dpend);
7400 }
7401
7402 \f
7403 /***********************************************************************
7404 Messages
7405 ***********************************************************************/
7406
7407
7408 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
7409 to *Messages*. */
7410
7411 void
7412 add_to_log (format, arg1, arg2)
7413 char *format;
7414 Lisp_Object arg1, arg2;
7415 {
7416 Lisp_Object args[3];
7417 Lisp_Object msg, fmt;
7418 char *buffer;
7419 int len;
7420 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
7421 USE_SAFE_ALLOCA;
7422
7423 /* Do nothing if called asynchronously. Inserting text into
7424 a buffer may call after-change-functions and alike and
7425 that would means running Lisp asynchronously. */
7426 if (handling_signal)
7427 return;
7428
7429 fmt = msg = Qnil;
7430 GCPRO4 (fmt, msg, arg1, arg2);
7431
7432 args[0] = fmt = build_string (format);
7433 args[1] = arg1;
7434 args[2] = arg2;
7435 msg = Fformat (3, args);
7436
7437 len = SBYTES (msg) + 1;
7438 SAFE_ALLOCA (buffer, char *, len);
7439 bcopy (SDATA (msg), buffer, len);
7440
7441 message_dolog (buffer, len - 1, 1, 0);
7442 SAFE_FREE ();
7443
7444 UNGCPRO;
7445 }
7446
7447
7448 /* Output a newline in the *Messages* buffer if "needs" one. */
7449
7450 void
7451 message_log_maybe_newline ()
7452 {
7453 if (message_log_need_newline)
7454 message_dolog ("", 0, 1, 0);
7455 }
7456
7457
7458 /* Add a string M of length NBYTES to the message log, optionally
7459 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
7460 nonzero, means interpret the contents of M as multibyte. This
7461 function calls low-level routines in order to bypass text property
7462 hooks, etc. which might not be safe to run.
7463
7464 This may GC (insert may run before/after change hooks),
7465 so the buffer M must NOT point to a Lisp string. */
7466
7467 void
7468 message_dolog (m, nbytes, nlflag, multibyte)
7469 const char *m;
7470 int nbytes, nlflag, multibyte;
7471 {
7472 if (!NILP (Vmemory_full))
7473 return;
7474
7475 if (!NILP (Vmessage_log_max))
7476 {
7477 struct buffer *oldbuf;
7478 Lisp_Object oldpoint, oldbegv, oldzv;
7479 int old_windows_or_buffers_changed = windows_or_buffers_changed;
7480 int point_at_end = 0;
7481 int zv_at_end = 0;
7482 Lisp_Object old_deactivate_mark, tem;
7483 struct gcpro gcpro1;
7484
7485 old_deactivate_mark = Vdeactivate_mark;
7486 oldbuf = current_buffer;
7487 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
7488 current_buffer->undo_list = Qt;
7489
7490 oldpoint = message_dolog_marker1;
7491 set_marker_restricted (oldpoint, make_number (PT), Qnil);
7492 oldbegv = message_dolog_marker2;
7493 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
7494 oldzv = message_dolog_marker3;
7495 set_marker_restricted (oldzv, make_number (ZV), Qnil);
7496 GCPRO1 (old_deactivate_mark);
7497
7498 if (PT == Z)
7499 point_at_end = 1;
7500 if (ZV == Z)
7501 zv_at_end = 1;
7502
7503 BEGV = BEG;
7504 BEGV_BYTE = BEG_BYTE;
7505 ZV = Z;
7506 ZV_BYTE = Z_BYTE;
7507 TEMP_SET_PT_BOTH (Z, Z_BYTE);
7508
7509 /* Insert the string--maybe converting multibyte to single byte
7510 or vice versa, so that all the text fits the buffer. */
7511 if (multibyte
7512 && NILP (current_buffer->enable_multibyte_characters))
7513 {
7514 int i, c, char_bytes;
7515 unsigned char work[1];
7516
7517 /* Convert a multibyte string to single-byte
7518 for the *Message* buffer. */
7519 for (i = 0; i < nbytes; i += char_bytes)
7520 {
7521 c = string_char_and_length (m + i, nbytes - i, &char_bytes);
7522 work[0] = (ASCII_CHAR_P (c)
7523 ? c
7524 : multibyte_char_to_unibyte (c, Qnil));
7525 insert_1_both (work, 1, 1, 1, 0, 0);
7526 }
7527 }
7528 else if (! multibyte
7529 && ! NILP (current_buffer->enable_multibyte_characters))
7530 {
7531 int i, c, char_bytes;
7532 unsigned char *msg = (unsigned char *) m;
7533 unsigned char str[MAX_MULTIBYTE_LENGTH];
7534 /* Convert a single-byte string to multibyte
7535 for the *Message* buffer. */
7536 for (i = 0; i < nbytes; i++)
7537 {
7538 c = msg[i];
7539 c = unibyte_char_to_multibyte (c);
7540 char_bytes = CHAR_STRING (c, str);
7541 insert_1_both (str, 1, char_bytes, 1, 0, 0);
7542 }
7543 }
7544 else if (nbytes)
7545 insert_1 (m, nbytes, 1, 0, 0);
7546
7547 if (nlflag)
7548 {
7549 int this_bol, this_bol_byte, prev_bol, prev_bol_byte, dup;
7550 insert_1 ("\n", 1, 1, 0, 0);
7551
7552 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
7553 this_bol = PT;
7554 this_bol_byte = PT_BYTE;
7555
7556 /* See if this line duplicates the previous one.
7557 If so, combine duplicates. */
7558 if (this_bol > BEG)
7559 {
7560 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
7561 prev_bol = PT;
7562 prev_bol_byte = PT_BYTE;
7563
7564 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
7565 this_bol, this_bol_byte);
7566 if (dup)
7567 {
7568 del_range_both (prev_bol, prev_bol_byte,
7569 this_bol, this_bol_byte, 0);
7570 if (dup > 1)
7571 {
7572 char dupstr[40];
7573 int duplen;
7574
7575 /* If you change this format, don't forget to also
7576 change message_log_check_duplicate. */
7577 sprintf (dupstr, " [%d times]", dup);
7578 duplen = strlen (dupstr);
7579 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
7580 insert_1 (dupstr, duplen, 1, 0, 1);
7581 }
7582 }
7583 }
7584
7585 /* If we have more than the desired maximum number of lines
7586 in the *Messages* buffer now, delete the oldest ones.
7587 This is safe because we don't have undo in this buffer. */
7588
7589 if (NATNUMP (Vmessage_log_max))
7590 {
7591 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
7592 -XFASTINT (Vmessage_log_max) - 1, 0);
7593 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
7594 }
7595 }
7596 BEGV = XMARKER (oldbegv)->charpos;
7597 BEGV_BYTE = marker_byte_position (oldbegv);
7598
7599 if (zv_at_end)
7600 {
7601 ZV = Z;
7602 ZV_BYTE = Z_BYTE;
7603 }
7604 else
7605 {
7606 ZV = XMARKER (oldzv)->charpos;
7607 ZV_BYTE = marker_byte_position (oldzv);
7608 }
7609
7610 if (point_at_end)
7611 TEMP_SET_PT_BOTH (Z, Z_BYTE);
7612 else
7613 /* We can't do Fgoto_char (oldpoint) because it will run some
7614 Lisp code. */
7615 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
7616 XMARKER (oldpoint)->bytepos);
7617
7618 UNGCPRO;
7619 unchain_marker (XMARKER (oldpoint));
7620 unchain_marker (XMARKER (oldbegv));
7621 unchain_marker (XMARKER (oldzv));
7622
7623 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
7624 set_buffer_internal (oldbuf);
7625 if (NILP (tem))
7626 windows_or_buffers_changed = old_windows_or_buffers_changed;
7627 message_log_need_newline = !nlflag;
7628 Vdeactivate_mark = old_deactivate_mark;
7629 }
7630 }
7631
7632
7633 /* We are at the end of the buffer after just having inserted a newline.
7634 (Note: We depend on the fact we won't be crossing the gap.)
7635 Check to see if the most recent message looks a lot like the previous one.
7636 Return 0 if different, 1 if the new one should just replace it, or a
7637 value N > 1 if we should also append " [N times]". */
7638
7639 static int
7640 message_log_check_duplicate (prev_bol, prev_bol_byte, this_bol, this_bol_byte)
7641 int prev_bol, this_bol;
7642 int prev_bol_byte, this_bol_byte;
7643 {
7644 int i;
7645 int len = Z_BYTE - 1 - this_bol_byte;
7646 int seen_dots = 0;
7647 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
7648 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
7649
7650 for (i = 0; i < len; i++)
7651 {
7652 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
7653 seen_dots = 1;
7654 if (p1[i] != p2[i])
7655 return seen_dots;
7656 }
7657 p1 += len;
7658 if (*p1 == '\n')
7659 return 2;
7660 if (*p1++ == ' ' && *p1++ == '[')
7661 {
7662 int n = 0;
7663 while (*p1 >= '0' && *p1 <= '9')
7664 n = n * 10 + *p1++ - '0';
7665 if (strncmp (p1, " times]\n", 8) == 0)
7666 return n+1;
7667 }
7668 return 0;
7669 }
7670 \f
7671
7672 /* Display an echo area message M with a specified length of NBYTES
7673 bytes. The string may include null characters. If M is 0, clear
7674 out any existing message, and let the mini-buffer text show
7675 through.
7676
7677 This may GC, so the buffer M must NOT point to a Lisp string. */
7678
7679 void
7680 message2 (m, nbytes, multibyte)
7681 const char *m;
7682 int nbytes;
7683 int multibyte;
7684 {
7685 /* First flush out any partial line written with print. */
7686 message_log_maybe_newline ();
7687 if (m)
7688 message_dolog (m, nbytes, 1, multibyte);
7689 message2_nolog (m, nbytes, multibyte);
7690 }
7691
7692
7693 /* The non-logging counterpart of message2. */
7694
7695 void
7696 message2_nolog (m, nbytes, multibyte)
7697 const char *m;
7698 int nbytes, multibyte;
7699 {
7700 struct frame *sf = SELECTED_FRAME ();
7701 message_enable_multibyte = multibyte;
7702
7703 if (noninteractive)
7704 {
7705 if (noninteractive_need_newline)
7706 putc ('\n', stderr);
7707 noninteractive_need_newline = 0;
7708 if (m)
7709 fwrite (m, nbytes, 1, stderr);
7710 if (cursor_in_echo_area == 0)
7711 fprintf (stderr, "\n");
7712 fflush (stderr);
7713 }
7714 /* A null message buffer means that the frame hasn't really been
7715 initialized yet. Error messages get reported properly by
7716 cmd_error, so this must be just an informative message; toss it. */
7717 else if (INTERACTIVE
7718 && sf->glyphs_initialized_p
7719 && FRAME_MESSAGE_BUF (sf))
7720 {
7721 Lisp_Object mini_window;
7722 struct frame *f;
7723
7724 /* Get the frame containing the mini-buffer
7725 that the selected frame is using. */
7726 mini_window = FRAME_MINIBUF_WINDOW (sf);
7727 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7728
7729 FRAME_SAMPLE_VISIBILITY (f);
7730 if (FRAME_VISIBLE_P (sf)
7731 && ! FRAME_VISIBLE_P (f))
7732 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
7733
7734 if (m)
7735 {
7736 set_message (m, Qnil, nbytes, multibyte);
7737 if (minibuffer_auto_raise)
7738 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
7739 }
7740 else
7741 clear_message (1, 1);
7742
7743 do_pending_window_change (0);
7744 echo_area_display (1);
7745 do_pending_window_change (0);
7746 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
7747 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
7748 }
7749 }
7750
7751
7752 /* Display an echo area message M with a specified length of NBYTES
7753 bytes. The string may include null characters. If M is not a
7754 string, clear out any existing message, and let the mini-buffer
7755 text show through.
7756
7757 This function cancels echoing. */
7758
7759 void
7760 message3 (m, nbytes, multibyte)
7761 Lisp_Object m;
7762 int nbytes;
7763 int multibyte;
7764 {
7765 struct gcpro gcpro1;
7766
7767 GCPRO1 (m);
7768 clear_message (1,1);
7769 cancel_echoing ();
7770
7771 /* First flush out any partial line written with print. */
7772 message_log_maybe_newline ();
7773 if (STRINGP (m))
7774 {
7775 char *buffer;
7776 USE_SAFE_ALLOCA;
7777
7778 SAFE_ALLOCA (buffer, char *, nbytes);
7779 bcopy (SDATA (m), buffer, nbytes);
7780 message_dolog (buffer, nbytes, 1, multibyte);
7781 SAFE_FREE ();
7782 }
7783 message3_nolog (m, nbytes, multibyte);
7784
7785 UNGCPRO;
7786 }
7787
7788
7789 /* The non-logging version of message3.
7790 This does not cancel echoing, because it is used for echoing.
7791 Perhaps we need to make a separate function for echoing
7792 and make this cancel echoing. */
7793
7794 void
7795 message3_nolog (m, nbytes, multibyte)
7796 Lisp_Object m;
7797 int nbytes, multibyte;
7798 {
7799 struct frame *sf = SELECTED_FRAME ();
7800 message_enable_multibyte = multibyte;
7801
7802 if (noninteractive)
7803 {
7804 if (noninteractive_need_newline)
7805 putc ('\n', stderr);
7806 noninteractive_need_newline = 0;
7807 if (STRINGP (m))
7808 fwrite (SDATA (m), nbytes, 1, stderr);
7809 if (cursor_in_echo_area == 0)
7810 fprintf (stderr, "\n");
7811 fflush (stderr);
7812 }
7813 /* A null message buffer means that the frame hasn't really been
7814 initialized yet. Error messages get reported properly by
7815 cmd_error, so this must be just an informative message; toss it. */
7816 else if (INTERACTIVE
7817 && sf->glyphs_initialized_p
7818 && FRAME_MESSAGE_BUF (sf))
7819 {
7820 Lisp_Object mini_window;
7821 Lisp_Object frame;
7822 struct frame *f;
7823
7824 /* Get the frame containing the mini-buffer
7825 that the selected frame is using. */
7826 mini_window = FRAME_MINIBUF_WINDOW (sf);
7827 frame = XWINDOW (mini_window)->frame;
7828 f = XFRAME (frame);
7829
7830 FRAME_SAMPLE_VISIBILITY (f);
7831 if (FRAME_VISIBLE_P (sf)
7832 && !FRAME_VISIBLE_P (f))
7833 Fmake_frame_visible (frame);
7834
7835 if (STRINGP (m) && SCHARS (m) > 0)
7836 {
7837 set_message (NULL, m, nbytes, multibyte);
7838 if (minibuffer_auto_raise)
7839 Fraise_frame (frame);
7840 /* Assume we are not echoing.
7841 (If we are, echo_now will override this.) */
7842 echo_message_buffer = Qnil;
7843 }
7844 else
7845 clear_message (1, 1);
7846
7847 do_pending_window_change (0);
7848 echo_area_display (1);
7849 do_pending_window_change (0);
7850 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
7851 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
7852 }
7853 }
7854
7855
7856 /* Display a null-terminated echo area message M. If M is 0, clear
7857 out any existing message, and let the mini-buffer text show through.
7858
7859 The buffer M must continue to exist until after the echo area gets
7860 cleared or some other message gets displayed there. Do not pass
7861 text that is stored in a Lisp string. Do not pass text in a buffer
7862 that was alloca'd. */
7863
7864 void
7865 message1 (m)
7866 char *m;
7867 {
7868 message2 (m, (m ? strlen (m) : 0), 0);
7869 }
7870
7871
7872 /* The non-logging counterpart of message1. */
7873
7874 void
7875 message1_nolog (m)
7876 char *m;
7877 {
7878 message2_nolog (m, (m ? strlen (m) : 0), 0);
7879 }
7880
7881 /* Display a message M which contains a single %s
7882 which gets replaced with STRING. */
7883
7884 void
7885 message_with_string (m, string, log)
7886 char *m;
7887 Lisp_Object string;
7888 int log;
7889 {
7890 CHECK_STRING (string);
7891
7892 if (noninteractive)
7893 {
7894 if (m)
7895 {
7896 if (noninteractive_need_newline)
7897 putc ('\n', stderr);
7898 noninteractive_need_newline = 0;
7899 fprintf (stderr, m, SDATA (string));
7900 if (cursor_in_echo_area == 0)
7901 fprintf (stderr, "\n");
7902 fflush (stderr);
7903 }
7904 }
7905 else if (INTERACTIVE)
7906 {
7907 /* The frame whose minibuffer we're going to display the message on.
7908 It may be larger than the selected frame, so we need
7909 to use its buffer, not the selected frame's buffer. */
7910 Lisp_Object mini_window;
7911 struct frame *f, *sf = SELECTED_FRAME ();
7912
7913 /* Get the frame containing the minibuffer
7914 that the selected frame is using. */
7915 mini_window = FRAME_MINIBUF_WINDOW (sf);
7916 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7917
7918 /* A null message buffer means that the frame hasn't really been
7919 initialized yet. Error messages get reported properly by
7920 cmd_error, so this must be just an informative message; toss it. */
7921 if (FRAME_MESSAGE_BUF (f))
7922 {
7923 Lisp_Object args[2], message;
7924 struct gcpro gcpro1, gcpro2;
7925
7926 args[0] = build_string (m);
7927 args[1] = message = string;
7928 GCPRO2 (args[0], message);
7929 gcpro1.nvars = 2;
7930
7931 message = Fformat (2, args);
7932
7933 if (log)
7934 message3 (message, SBYTES (message), STRING_MULTIBYTE (message));
7935 else
7936 message3_nolog (message, SBYTES (message), STRING_MULTIBYTE (message));
7937
7938 UNGCPRO;
7939
7940 /* Print should start at the beginning of the message
7941 buffer next time. */
7942 message_buf_print = 0;
7943 }
7944 }
7945 }
7946
7947
7948 /* Dump an informative message to the minibuf. If M is 0, clear out
7949 any existing message, and let the mini-buffer text show through. */
7950
7951 /* VARARGS 1 */
7952 void
7953 message (m, a1, a2, a3)
7954 char *m;
7955 EMACS_INT a1, a2, a3;
7956 {
7957 if (noninteractive)
7958 {
7959 if (m)
7960 {
7961 if (noninteractive_need_newline)
7962 putc ('\n', stderr);
7963 noninteractive_need_newline = 0;
7964 fprintf (stderr, m, a1, a2, a3);
7965 if (cursor_in_echo_area == 0)
7966 fprintf (stderr, "\n");
7967 fflush (stderr);
7968 }
7969 }
7970 else if (INTERACTIVE)
7971 {
7972 /* The frame whose mini-buffer we're going to display the message
7973 on. It may be larger than the selected frame, so we need to
7974 use its buffer, not the selected frame's buffer. */
7975 Lisp_Object mini_window;
7976 struct frame *f, *sf = SELECTED_FRAME ();
7977
7978 /* Get the frame containing the mini-buffer
7979 that the selected frame is using. */
7980 mini_window = FRAME_MINIBUF_WINDOW (sf);
7981 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7982
7983 /* A null message buffer means that the frame hasn't really been
7984 initialized yet. Error messages get reported properly by
7985 cmd_error, so this must be just an informative message; toss
7986 it. */
7987 if (FRAME_MESSAGE_BUF (f))
7988 {
7989 if (m)
7990 {
7991 int len;
7992 #ifdef NO_ARG_ARRAY
7993 char *a[3];
7994 a[0] = (char *) a1;
7995 a[1] = (char *) a2;
7996 a[2] = (char *) a3;
7997
7998 len = doprnt (FRAME_MESSAGE_BUF (f),
7999 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
8000 #else
8001 len = doprnt (FRAME_MESSAGE_BUF (f),
8002 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3,
8003 (char **) &a1);
8004 #endif /* NO_ARG_ARRAY */
8005
8006 message2 (FRAME_MESSAGE_BUF (f), len, 0);
8007 }
8008 else
8009 message1 (0);
8010
8011 /* Print should start at the beginning of the message
8012 buffer next time. */
8013 message_buf_print = 0;
8014 }
8015 }
8016 }
8017
8018
8019 /* The non-logging version of message. */
8020
8021 void
8022 message_nolog (m, a1, a2, a3)
8023 char *m;
8024 EMACS_INT a1, a2, a3;
8025 {
8026 Lisp_Object old_log_max;
8027 old_log_max = Vmessage_log_max;
8028 Vmessage_log_max = Qnil;
8029 message (m, a1, a2, a3);
8030 Vmessage_log_max = old_log_max;
8031 }
8032
8033
8034 /* Display the current message in the current mini-buffer. This is
8035 only called from error handlers in process.c, and is not time
8036 critical. */
8037
8038 void
8039 update_echo_area ()
8040 {
8041 if (!NILP (echo_area_buffer[0]))
8042 {
8043 Lisp_Object string;
8044 string = Fcurrent_message ();
8045 message3 (string, SBYTES (string),
8046 !NILP (current_buffer->enable_multibyte_characters));
8047 }
8048 }
8049
8050
8051 /* Make sure echo area buffers in `echo_buffers' are live.
8052 If they aren't, make new ones. */
8053
8054 static void
8055 ensure_echo_area_buffers ()
8056 {
8057 int i;
8058
8059 for (i = 0; i < 2; ++i)
8060 if (!BUFFERP (echo_buffer[i])
8061 || NILP (XBUFFER (echo_buffer[i])->name))
8062 {
8063 char name[30];
8064 Lisp_Object old_buffer;
8065 int j;
8066
8067 old_buffer = echo_buffer[i];
8068 sprintf (name, " *Echo Area %d*", i);
8069 echo_buffer[i] = Fget_buffer_create (build_string (name));
8070 XBUFFER (echo_buffer[i])->truncate_lines = Qnil;
8071
8072 for (j = 0; j < 2; ++j)
8073 if (EQ (old_buffer, echo_area_buffer[j]))
8074 echo_area_buffer[j] = echo_buffer[i];
8075 }
8076 }
8077
8078
8079 /* Call FN with args A1..A4 with either the current or last displayed
8080 echo_area_buffer as current buffer.
8081
8082 WHICH zero means use the current message buffer
8083 echo_area_buffer[0]. If that is nil, choose a suitable buffer
8084 from echo_buffer[] and clear it.
8085
8086 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
8087 suitable buffer from echo_buffer[] and clear it.
8088
8089 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
8090 that the current message becomes the last displayed one, make
8091 choose a suitable buffer for echo_area_buffer[0], and clear it.
8092
8093 Value is what FN returns. */
8094
8095 static int
8096 with_echo_area_buffer (w, which, fn, a1, a2, a3, a4)
8097 struct window *w;
8098 int which;
8099 int (*fn) P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
8100 EMACS_INT a1;
8101 Lisp_Object a2;
8102 EMACS_INT a3, a4;
8103 {
8104 Lisp_Object buffer;
8105 int this_one, the_other, clear_buffer_p, rc;
8106 int count = SPECPDL_INDEX ();
8107
8108 /* If buffers aren't live, make new ones. */
8109 ensure_echo_area_buffers ();
8110
8111 clear_buffer_p = 0;
8112
8113 if (which == 0)
8114 this_one = 0, the_other = 1;
8115 else if (which > 0)
8116 this_one = 1, the_other = 0;
8117 else
8118 {
8119 this_one = 0, the_other = 1;
8120 clear_buffer_p = 1;
8121
8122 /* We need a fresh one in case the current echo buffer equals
8123 the one containing the last displayed echo area message. */
8124 if (!NILP (echo_area_buffer[this_one])
8125 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
8126 echo_area_buffer[this_one] = Qnil;
8127 }
8128
8129 /* Choose a suitable buffer from echo_buffer[] is we don't
8130 have one. */
8131 if (NILP (echo_area_buffer[this_one]))
8132 {
8133 echo_area_buffer[this_one]
8134 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
8135 ? echo_buffer[the_other]
8136 : echo_buffer[this_one]);
8137 clear_buffer_p = 1;
8138 }
8139
8140 buffer = echo_area_buffer[this_one];
8141
8142 /* Don't get confused by reusing the buffer used for echoing
8143 for a different purpose. */
8144 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
8145 cancel_echoing ();
8146
8147 record_unwind_protect (unwind_with_echo_area_buffer,
8148 with_echo_area_buffer_unwind_data (w));
8149
8150 /* Make the echo area buffer current. Note that for display
8151 purposes, it is not necessary that the displayed window's buffer
8152 == current_buffer, except for text property lookup. So, let's
8153 only set that buffer temporarily here without doing a full
8154 Fset_window_buffer. We must also change w->pointm, though,
8155 because otherwise an assertions in unshow_buffer fails, and Emacs
8156 aborts. */
8157 set_buffer_internal_1 (XBUFFER (buffer));
8158 if (w)
8159 {
8160 w->buffer = buffer;
8161 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
8162 }
8163
8164 current_buffer->undo_list = Qt;
8165 current_buffer->read_only = Qnil;
8166 specbind (Qinhibit_read_only, Qt);
8167 specbind (Qinhibit_modification_hooks, Qt);
8168
8169 if (clear_buffer_p && Z > BEG)
8170 del_range (BEG, Z);
8171
8172 xassert (BEGV >= BEG);
8173 xassert (ZV <= Z && ZV >= BEGV);
8174
8175 rc = fn (a1, a2, a3, a4);
8176
8177 xassert (BEGV >= BEG);
8178 xassert (ZV <= Z && ZV >= BEGV);
8179
8180 unbind_to (count, Qnil);
8181 return rc;
8182 }
8183
8184
8185 /* Save state that should be preserved around the call to the function
8186 FN called in with_echo_area_buffer. */
8187
8188 static Lisp_Object
8189 with_echo_area_buffer_unwind_data (w)
8190 struct window *w;
8191 {
8192 int i = 0;
8193 Lisp_Object vector;
8194
8195 /* Reduce consing by keeping one vector in
8196 Vwith_echo_area_save_vector. */
8197 vector = Vwith_echo_area_save_vector;
8198 Vwith_echo_area_save_vector = Qnil;
8199
8200 if (NILP (vector))
8201 vector = Fmake_vector (make_number (7), Qnil);
8202
8203 XSETBUFFER (AREF (vector, i), current_buffer); ++i;
8204 AREF (vector, i) = Vdeactivate_mark, ++i;
8205 AREF (vector, i) = make_number (windows_or_buffers_changed), ++i;
8206
8207 if (w)
8208 {
8209 XSETWINDOW (AREF (vector, i), w); ++i;
8210 AREF (vector, i) = w->buffer; ++i;
8211 AREF (vector, i) = make_number (XMARKER (w->pointm)->charpos); ++i;
8212 AREF (vector, i) = make_number (XMARKER (w->pointm)->bytepos); ++i;
8213 }
8214 else
8215 {
8216 int end = i + 4;
8217 for (; i < end; ++i)
8218 AREF (vector, i) = Qnil;
8219 }
8220
8221 xassert (i == ASIZE (vector));
8222 return vector;
8223 }
8224
8225
8226 /* Restore global state from VECTOR which was created by
8227 with_echo_area_buffer_unwind_data. */
8228
8229 static Lisp_Object
8230 unwind_with_echo_area_buffer (vector)
8231 Lisp_Object vector;
8232 {
8233 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
8234 Vdeactivate_mark = AREF (vector, 1);
8235 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
8236
8237 if (WINDOWP (AREF (vector, 3)))
8238 {
8239 struct window *w;
8240 Lisp_Object buffer, charpos, bytepos;
8241
8242 w = XWINDOW (AREF (vector, 3));
8243 buffer = AREF (vector, 4);
8244 charpos = AREF (vector, 5);
8245 bytepos = AREF (vector, 6);
8246
8247 w->buffer = buffer;
8248 set_marker_both (w->pointm, buffer,
8249 XFASTINT (charpos), XFASTINT (bytepos));
8250 }
8251
8252 Vwith_echo_area_save_vector = vector;
8253 return Qnil;
8254 }
8255
8256
8257 /* Set up the echo area for use by print functions. MULTIBYTE_P
8258 non-zero means we will print multibyte. */
8259
8260 void
8261 setup_echo_area_for_printing (multibyte_p)
8262 int multibyte_p;
8263 {
8264 /* If we can't find an echo area any more, exit. */
8265 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
8266 Fkill_emacs (Qnil);
8267
8268 ensure_echo_area_buffers ();
8269
8270 if (!message_buf_print)
8271 {
8272 /* A message has been output since the last time we printed.
8273 Choose a fresh echo area buffer. */
8274 if (EQ (echo_area_buffer[1], echo_buffer[0]))
8275 echo_area_buffer[0] = echo_buffer[1];
8276 else
8277 echo_area_buffer[0] = echo_buffer[0];
8278
8279 /* Switch to that buffer and clear it. */
8280 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
8281 current_buffer->truncate_lines = Qnil;
8282
8283 if (Z > BEG)
8284 {
8285 int count = SPECPDL_INDEX ();
8286 specbind (Qinhibit_read_only, Qt);
8287 /* Note that undo recording is always disabled. */
8288 del_range (BEG, Z);
8289 unbind_to (count, Qnil);
8290 }
8291 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
8292
8293 /* Set up the buffer for the multibyteness we need. */
8294 if (multibyte_p
8295 != !NILP (current_buffer->enable_multibyte_characters))
8296 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
8297
8298 /* Raise the frame containing the echo area. */
8299 if (minibuffer_auto_raise)
8300 {
8301 struct frame *sf = SELECTED_FRAME ();
8302 Lisp_Object mini_window;
8303 mini_window = FRAME_MINIBUF_WINDOW (sf);
8304 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
8305 }
8306
8307 message_log_maybe_newline ();
8308 message_buf_print = 1;
8309 }
8310 else
8311 {
8312 if (NILP (echo_area_buffer[0]))
8313 {
8314 if (EQ (echo_area_buffer[1], echo_buffer[0]))
8315 echo_area_buffer[0] = echo_buffer[1];
8316 else
8317 echo_area_buffer[0] = echo_buffer[0];
8318 }
8319
8320 if (current_buffer != XBUFFER (echo_area_buffer[0]))
8321 {
8322 /* Someone switched buffers between print requests. */
8323 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
8324 current_buffer->truncate_lines = Qnil;
8325 }
8326 }
8327 }
8328
8329
8330 /* Display an echo area message in window W. Value is non-zero if W's
8331 height is changed. If display_last_displayed_message_p is
8332 non-zero, display the message that was last displayed, otherwise
8333 display the current message. */
8334
8335 static int
8336 display_echo_area (w)
8337 struct window *w;
8338 {
8339 int i, no_message_p, window_height_changed_p, count;
8340
8341 /* Temporarily disable garbage collections while displaying the echo
8342 area. This is done because a GC can print a message itself.
8343 That message would modify the echo area buffer's contents while a
8344 redisplay of the buffer is going on, and seriously confuse
8345 redisplay. */
8346 count = inhibit_garbage_collection ();
8347
8348 /* If there is no message, we must call display_echo_area_1
8349 nevertheless because it resizes the window. But we will have to
8350 reset the echo_area_buffer in question to nil at the end because
8351 with_echo_area_buffer will sets it to an empty buffer. */
8352 i = display_last_displayed_message_p ? 1 : 0;
8353 no_message_p = NILP (echo_area_buffer[i]);
8354
8355 window_height_changed_p
8356 = with_echo_area_buffer (w, display_last_displayed_message_p,
8357 display_echo_area_1,
8358 (EMACS_INT) w, Qnil, 0, 0);
8359
8360 if (no_message_p)
8361 echo_area_buffer[i] = Qnil;
8362
8363 unbind_to (count, Qnil);
8364 return window_height_changed_p;
8365 }
8366
8367
8368 /* Helper for display_echo_area. Display the current buffer which
8369 contains the current echo area message in window W, a mini-window,
8370 a pointer to which is passed in A1. A2..A4 are currently not used.
8371 Change the height of W so that all of the message is displayed.
8372 Value is non-zero if height of W was changed. */
8373
8374 static int
8375 display_echo_area_1 (a1, a2, a3, a4)
8376 EMACS_INT a1;
8377 Lisp_Object a2;
8378 EMACS_INT a3, a4;
8379 {
8380 struct window *w = (struct window *) a1;
8381 Lisp_Object window;
8382 struct text_pos start;
8383 int window_height_changed_p = 0;
8384
8385 /* Do this before displaying, so that we have a large enough glyph
8386 matrix for the display. If we can't get enough space for the
8387 whole text, display the last N lines. That works by setting w->start. */
8388 window_height_changed_p = resize_mini_window (w, 0);
8389
8390 /* Use the starting position chosen by resize_mini_window. */
8391 SET_TEXT_POS_FROM_MARKER (start, w->start);
8392
8393 /* Display. */
8394 clear_glyph_matrix (w->desired_matrix);
8395 XSETWINDOW (window, w);
8396 try_window (window, start, 0);
8397
8398 return window_height_changed_p;
8399 }
8400
8401
8402 /* Resize the echo area window to exactly the size needed for the
8403 currently displayed message, if there is one. If a mini-buffer
8404 is active, don't shrink it. */
8405
8406 void
8407 resize_echo_area_exactly ()
8408 {
8409 if (BUFFERP (echo_area_buffer[0])
8410 && WINDOWP (echo_area_window))
8411 {
8412 struct window *w = XWINDOW (echo_area_window);
8413 int resized_p;
8414 Lisp_Object resize_exactly;
8415
8416 if (minibuf_level == 0)
8417 resize_exactly = Qt;
8418 else
8419 resize_exactly = Qnil;
8420
8421 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
8422 (EMACS_INT) w, resize_exactly, 0, 0);
8423 if (resized_p)
8424 {
8425 ++windows_or_buffers_changed;
8426 ++update_mode_lines;
8427 redisplay_internal (0);
8428 }
8429 }
8430 }
8431
8432
8433 /* Callback function for with_echo_area_buffer, when used from
8434 resize_echo_area_exactly. A1 contains a pointer to the window to
8435 resize, EXACTLY non-nil means resize the mini-window exactly to the
8436 size of the text displayed. A3 and A4 are not used. Value is what
8437 resize_mini_window returns. */
8438
8439 static int
8440 resize_mini_window_1 (a1, exactly, a3, a4)
8441 EMACS_INT a1;
8442 Lisp_Object exactly;
8443 EMACS_INT a3, a4;
8444 {
8445 return resize_mini_window ((struct window *) a1, !NILP (exactly));
8446 }
8447
8448
8449 /* Resize mini-window W to fit the size of its contents. EXACT:P
8450 means size the window exactly to the size needed. Otherwise, it's
8451 only enlarged until W's buffer is empty.
8452
8453 Set W->start to the right place to begin display. If the whole
8454 contents fit, start at the beginning. Otherwise, start so as
8455 to make the end of the contents appear. This is particularly
8456 important for y-or-n-p, but seems desirable generally.
8457
8458 Value is non-zero if the window height has been changed. */
8459
8460 int
8461 resize_mini_window (w, exact_p)
8462 struct window *w;
8463 int exact_p;
8464 {
8465 struct frame *f = XFRAME (w->frame);
8466 int window_height_changed_p = 0;
8467
8468 xassert (MINI_WINDOW_P (w));
8469
8470 /* By default, start display at the beginning. */
8471 set_marker_both (w->start, w->buffer,
8472 BUF_BEGV (XBUFFER (w->buffer)),
8473 BUF_BEGV_BYTE (XBUFFER (w->buffer)));
8474
8475 /* Don't resize windows while redisplaying a window; it would
8476 confuse redisplay functions when the size of the window they are
8477 displaying changes from under them. Such a resizing can happen,
8478 for instance, when which-func prints a long message while
8479 we are running fontification-functions. We're running these
8480 functions with safe_call which binds inhibit-redisplay to t. */
8481 if (!NILP (Vinhibit_redisplay))
8482 return 0;
8483
8484 /* Nil means don't try to resize. */
8485 if (NILP (Vresize_mini_windows)
8486 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
8487 return 0;
8488
8489 if (!FRAME_MINIBUF_ONLY_P (f))
8490 {
8491 struct it it;
8492 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
8493 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
8494 int height, max_height;
8495 int unit = FRAME_LINE_HEIGHT (f);
8496 struct text_pos start;
8497 struct buffer *old_current_buffer = NULL;
8498
8499 if (current_buffer != XBUFFER (w->buffer))
8500 {
8501 old_current_buffer = current_buffer;
8502 set_buffer_internal (XBUFFER (w->buffer));
8503 }
8504
8505 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
8506
8507 /* Compute the max. number of lines specified by the user. */
8508 if (FLOATP (Vmax_mini_window_height))
8509 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
8510 else if (INTEGERP (Vmax_mini_window_height))
8511 max_height = XINT (Vmax_mini_window_height);
8512 else
8513 max_height = total_height / 4;
8514
8515 /* Correct that max. height if it's bogus. */
8516 max_height = max (1, max_height);
8517 max_height = min (total_height, max_height);
8518
8519 /* Find out the height of the text in the window. */
8520 if (it.truncate_lines_p)
8521 height = 1;
8522 else
8523 {
8524 last_height = 0;
8525 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
8526 if (it.max_ascent == 0 && it.max_descent == 0)
8527 height = it.current_y + last_height;
8528 else
8529 height = it.current_y + it.max_ascent + it.max_descent;
8530 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
8531 height = (height + unit - 1) / unit;
8532 }
8533
8534 /* Compute a suitable window start. */
8535 if (height > max_height)
8536 {
8537 height = max_height;
8538 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
8539 move_it_vertically_backward (&it, (height - 1) * unit);
8540 start = it.current.pos;
8541 }
8542 else
8543 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
8544 SET_MARKER_FROM_TEXT_POS (w->start, start);
8545
8546 if (EQ (Vresize_mini_windows, Qgrow_only))
8547 {
8548 /* Let it grow only, until we display an empty message, in which
8549 case the window shrinks again. */
8550 if (height > WINDOW_TOTAL_LINES (w))
8551 {
8552 int old_height = WINDOW_TOTAL_LINES (w);
8553 freeze_window_starts (f, 1);
8554 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8555 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8556 }
8557 else if (height < WINDOW_TOTAL_LINES (w)
8558 && (exact_p || BEGV == ZV))
8559 {
8560 int old_height = WINDOW_TOTAL_LINES (w);
8561 freeze_window_starts (f, 0);
8562 shrink_mini_window (w);
8563 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8564 }
8565 }
8566 else
8567 {
8568 /* Always resize to exact size needed. */
8569 if (height > WINDOW_TOTAL_LINES (w))
8570 {
8571 int old_height = WINDOW_TOTAL_LINES (w);
8572 freeze_window_starts (f, 1);
8573 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8574 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8575 }
8576 else if (height < WINDOW_TOTAL_LINES (w))
8577 {
8578 int old_height = WINDOW_TOTAL_LINES (w);
8579 freeze_window_starts (f, 0);
8580 shrink_mini_window (w);
8581
8582 if (height)
8583 {
8584 freeze_window_starts (f, 1);
8585 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8586 }
8587
8588 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8589 }
8590 }
8591
8592 if (old_current_buffer)
8593 set_buffer_internal (old_current_buffer);
8594 }
8595
8596 return window_height_changed_p;
8597 }
8598
8599
8600 /* Value is the current message, a string, or nil if there is no
8601 current message. */
8602
8603 Lisp_Object
8604 current_message ()
8605 {
8606 Lisp_Object msg;
8607
8608 if (NILP (echo_area_buffer[0]))
8609 msg = Qnil;
8610 else
8611 {
8612 with_echo_area_buffer (0, 0, current_message_1,
8613 (EMACS_INT) &msg, Qnil, 0, 0);
8614 if (NILP (msg))
8615 echo_area_buffer[0] = Qnil;
8616 }
8617
8618 return msg;
8619 }
8620
8621
8622 static int
8623 current_message_1 (a1, a2, a3, a4)
8624 EMACS_INT a1;
8625 Lisp_Object a2;
8626 EMACS_INT a3, a4;
8627 {
8628 Lisp_Object *msg = (Lisp_Object *) a1;
8629
8630 if (Z > BEG)
8631 *msg = make_buffer_string (BEG, Z, 1);
8632 else
8633 *msg = Qnil;
8634 return 0;
8635 }
8636
8637
8638 /* Push the current message on Vmessage_stack for later restauration
8639 by restore_message. Value is non-zero if the current message isn't
8640 empty. This is a relatively infrequent operation, so it's not
8641 worth optimizing. */
8642
8643 int
8644 push_message ()
8645 {
8646 Lisp_Object msg;
8647 msg = current_message ();
8648 Vmessage_stack = Fcons (msg, Vmessage_stack);
8649 return STRINGP (msg);
8650 }
8651
8652
8653 /* Restore message display from the top of Vmessage_stack. */
8654
8655 void
8656 restore_message ()
8657 {
8658 Lisp_Object msg;
8659
8660 xassert (CONSP (Vmessage_stack));
8661 msg = XCAR (Vmessage_stack);
8662 if (STRINGP (msg))
8663 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
8664 else
8665 message3_nolog (msg, 0, 0);
8666 }
8667
8668
8669 /* Handler for record_unwind_protect calling pop_message. */
8670
8671 Lisp_Object
8672 pop_message_unwind (dummy)
8673 Lisp_Object dummy;
8674 {
8675 pop_message ();
8676 return Qnil;
8677 }
8678
8679 /* Pop the top-most entry off Vmessage_stack. */
8680
8681 void
8682 pop_message ()
8683 {
8684 xassert (CONSP (Vmessage_stack));
8685 Vmessage_stack = XCDR (Vmessage_stack);
8686 }
8687
8688
8689 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
8690 exits. If the stack is not empty, we have a missing pop_message
8691 somewhere. */
8692
8693 void
8694 check_message_stack ()
8695 {
8696 if (!NILP (Vmessage_stack))
8697 abort ();
8698 }
8699
8700
8701 /* Truncate to NCHARS what will be displayed in the echo area the next
8702 time we display it---but don't redisplay it now. */
8703
8704 void
8705 truncate_echo_area (nchars)
8706 int nchars;
8707 {
8708 if (nchars == 0)
8709 echo_area_buffer[0] = Qnil;
8710 /* A null message buffer means that the frame hasn't really been
8711 initialized yet. Error messages get reported properly by
8712 cmd_error, so this must be just an informative message; toss it. */
8713 else if (!noninteractive
8714 && INTERACTIVE
8715 && !NILP (echo_area_buffer[0]))
8716 {
8717 struct frame *sf = SELECTED_FRAME ();
8718 if (FRAME_MESSAGE_BUF (sf))
8719 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
8720 }
8721 }
8722
8723
8724 /* Helper function for truncate_echo_area. Truncate the current
8725 message to at most NCHARS characters. */
8726
8727 static int
8728 truncate_message_1 (nchars, a2, a3, a4)
8729 EMACS_INT nchars;
8730 Lisp_Object a2;
8731 EMACS_INT a3, a4;
8732 {
8733 if (BEG + nchars < Z)
8734 del_range (BEG + nchars, Z);
8735 if (Z == BEG)
8736 echo_area_buffer[0] = Qnil;
8737 return 0;
8738 }
8739
8740
8741 /* Set the current message to a substring of S or STRING.
8742
8743 If STRING is a Lisp string, set the message to the first NBYTES
8744 bytes from STRING. NBYTES zero means use the whole string. If
8745 STRING is multibyte, the message will be displayed multibyte.
8746
8747 If S is not null, set the message to the first LEN bytes of S. LEN
8748 zero means use the whole string. MULTIBYTE_P non-zero means S is
8749 multibyte. Display the message multibyte in that case.
8750
8751 Doesn't GC, as with_echo_area_buffer binds Qinhibit_modification_hooks
8752 to t before calling set_message_1 (which calls insert).
8753 */
8754
8755 void
8756 set_message (s, string, nbytes, multibyte_p)
8757 const char *s;
8758 Lisp_Object string;
8759 int nbytes, multibyte_p;
8760 {
8761 message_enable_multibyte
8762 = ((s && multibyte_p)
8763 || (STRINGP (string) && STRING_MULTIBYTE (string)));
8764
8765 with_echo_area_buffer (0, -1, set_message_1,
8766 (EMACS_INT) s, string, nbytes, multibyte_p);
8767 message_buf_print = 0;
8768 help_echo_showing_p = 0;
8769 }
8770
8771
8772 /* Helper function for set_message. Arguments have the same meaning
8773 as there, with A1 corresponding to S and A2 corresponding to STRING
8774 This function is called with the echo area buffer being
8775 current. */
8776
8777 static int
8778 set_message_1 (a1, a2, nbytes, multibyte_p)
8779 EMACS_INT a1;
8780 Lisp_Object a2;
8781 EMACS_INT nbytes, multibyte_p;
8782 {
8783 const char *s = (const char *) a1;
8784 Lisp_Object string = a2;
8785
8786 /* Change multibyteness of the echo buffer appropriately. */
8787 if (message_enable_multibyte
8788 != !NILP (current_buffer->enable_multibyte_characters))
8789 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
8790
8791 current_buffer->truncate_lines = message_truncate_lines ? Qt : Qnil;
8792
8793 /* Insert new message at BEG. */
8794 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
8795
8796 if (STRINGP (string))
8797 {
8798 int nchars;
8799
8800 if (nbytes == 0)
8801 nbytes = SBYTES (string);
8802 nchars = string_byte_to_char (string, nbytes);
8803
8804 /* This function takes care of single/multibyte conversion. We
8805 just have to ensure that the echo area buffer has the right
8806 setting of enable_multibyte_characters. */
8807 insert_from_string (string, 0, 0, nchars, nbytes, 1);
8808 }
8809 else if (s)
8810 {
8811 if (nbytes == 0)
8812 nbytes = strlen (s);
8813
8814 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
8815 {
8816 /* Convert from multi-byte to single-byte. */
8817 int i, c, n;
8818 unsigned char work[1];
8819
8820 /* Convert a multibyte string to single-byte. */
8821 for (i = 0; i < nbytes; i += n)
8822 {
8823 c = string_char_and_length (s + i, nbytes - i, &n);
8824 work[0] = (ASCII_CHAR_P (c)
8825 ? c
8826 : multibyte_char_to_unibyte (c, Qnil));
8827 insert_1_both (work, 1, 1, 1, 0, 0);
8828 }
8829 }
8830 else if (!multibyte_p
8831 && !NILP (current_buffer->enable_multibyte_characters))
8832 {
8833 /* Convert from single-byte to multi-byte. */
8834 int i, c, n;
8835 const unsigned char *msg = (const unsigned char *) s;
8836 unsigned char str[MAX_MULTIBYTE_LENGTH];
8837
8838 /* Convert a single-byte string to multibyte. */
8839 for (i = 0; i < nbytes; i++)
8840 {
8841 c = msg[i];
8842 c = unibyte_char_to_multibyte (c);
8843 n = CHAR_STRING (c, str);
8844 insert_1_both (str, 1, n, 1, 0, 0);
8845 }
8846 }
8847 else
8848 insert_1 (s, nbytes, 1, 0, 0);
8849 }
8850
8851 return 0;
8852 }
8853
8854
8855 /* Clear messages. CURRENT_P non-zero means clear the current
8856 message. LAST_DISPLAYED_P non-zero means clear the message
8857 last displayed. */
8858
8859 void
8860 clear_message (current_p, last_displayed_p)
8861 int current_p, last_displayed_p;
8862 {
8863 if (current_p)
8864 {
8865 echo_area_buffer[0] = Qnil;
8866 message_cleared_p = 1;
8867 }
8868
8869 if (last_displayed_p)
8870 echo_area_buffer[1] = Qnil;
8871
8872 message_buf_print = 0;
8873 }
8874
8875 /* Clear garbaged frames.
8876
8877 This function is used where the old redisplay called
8878 redraw_garbaged_frames which in turn called redraw_frame which in
8879 turn called clear_frame. The call to clear_frame was a source of
8880 flickering. I believe a clear_frame is not necessary. It should
8881 suffice in the new redisplay to invalidate all current matrices,
8882 and ensure a complete redisplay of all windows. */
8883
8884 static void
8885 clear_garbaged_frames ()
8886 {
8887 if (frame_garbaged)
8888 {
8889 Lisp_Object tail, frame;
8890 int changed_count = 0;
8891
8892 FOR_EACH_FRAME (tail, frame)
8893 {
8894 struct frame *f = XFRAME (frame);
8895
8896 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
8897 {
8898 if (f->resized_p)
8899 {
8900 Fredraw_frame (frame);
8901 f->force_flush_display_p = 1;
8902 }
8903 clear_current_matrices (f);
8904 changed_count++;
8905 f->garbaged = 0;
8906 f->resized_p = 0;
8907 }
8908 }
8909
8910 frame_garbaged = 0;
8911 if (changed_count)
8912 ++windows_or_buffers_changed;
8913 }
8914 }
8915
8916
8917 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
8918 is non-zero update selected_frame. Value is non-zero if the
8919 mini-windows height has been changed. */
8920
8921 static int
8922 echo_area_display (update_frame_p)
8923 int update_frame_p;
8924 {
8925 Lisp_Object mini_window;
8926 struct window *w;
8927 struct frame *f;
8928 int window_height_changed_p = 0;
8929 struct frame *sf = SELECTED_FRAME ();
8930
8931 mini_window = FRAME_MINIBUF_WINDOW (sf);
8932 w = XWINDOW (mini_window);
8933 f = XFRAME (WINDOW_FRAME (w));
8934
8935 /* Don't display if frame is invisible or not yet initialized. */
8936 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
8937 return 0;
8938
8939 /* The terminal frame is used as the first Emacs frame on the Mac OS. */
8940 #ifndef MAC_OS8
8941 #ifdef HAVE_WINDOW_SYSTEM
8942 /* When Emacs starts, selected_frame may be the initial terminal
8943 frame. If we let this through, a message would be displayed on
8944 the terminal. */
8945 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
8946 return 0;
8947 #endif /* HAVE_WINDOW_SYSTEM */
8948 #endif
8949
8950 /* Redraw garbaged frames. */
8951 if (frame_garbaged)
8952 clear_garbaged_frames ();
8953
8954 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
8955 {
8956 echo_area_window = mini_window;
8957 window_height_changed_p = display_echo_area (w);
8958 w->must_be_updated_p = 1;
8959
8960 /* Update the display, unless called from redisplay_internal.
8961 Also don't update the screen during redisplay itself. The
8962 update will happen at the end of redisplay, and an update
8963 here could cause confusion. */
8964 if (update_frame_p && !redisplaying_p)
8965 {
8966 int n = 0;
8967
8968 /* If the display update has been interrupted by pending
8969 input, update mode lines in the frame. Due to the
8970 pending input, it might have been that redisplay hasn't
8971 been called, so that mode lines above the echo area are
8972 garbaged. This looks odd, so we prevent it here. */
8973 if (!display_completed)
8974 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
8975
8976 if (window_height_changed_p
8977 /* Don't do this if Emacs is shutting down. Redisplay
8978 needs to run hooks. */
8979 && !NILP (Vrun_hooks))
8980 {
8981 /* Must update other windows. Likewise as in other
8982 cases, don't let this update be interrupted by
8983 pending input. */
8984 int count = SPECPDL_INDEX ();
8985 specbind (Qredisplay_dont_pause, Qt);
8986 windows_or_buffers_changed = 1;
8987 redisplay_internal (0);
8988 unbind_to (count, Qnil);
8989 }
8990 else if (FRAME_WINDOW_P (f) && n == 0)
8991 {
8992 /* Window configuration is the same as before.
8993 Can do with a display update of the echo area,
8994 unless we displayed some mode lines. */
8995 update_single_window (w, 1);
8996 FRAME_RIF (f)->flush_display (f);
8997 }
8998 else
8999 update_frame (f, 1, 1);
9000
9001 /* If cursor is in the echo area, make sure that the next
9002 redisplay displays the minibuffer, so that the cursor will
9003 be replaced with what the minibuffer wants. */
9004 if (cursor_in_echo_area)
9005 ++windows_or_buffers_changed;
9006 }
9007 }
9008 else if (!EQ (mini_window, selected_window))
9009 windows_or_buffers_changed++;
9010
9011 /* Last displayed message is now the current message. */
9012 echo_area_buffer[1] = echo_area_buffer[0];
9013 /* Inform read_char that we're not echoing. */
9014 echo_message_buffer = Qnil;
9015
9016 /* Prevent redisplay optimization in redisplay_internal by resetting
9017 this_line_start_pos. This is done because the mini-buffer now
9018 displays the message instead of its buffer text. */
9019 if (EQ (mini_window, selected_window))
9020 CHARPOS (this_line_start_pos) = 0;
9021
9022 return window_height_changed_p;
9023 }
9024
9025
9026 \f
9027 /***********************************************************************
9028 Mode Lines and Frame Titles
9029 ***********************************************************************/
9030
9031 /* A buffer for constructing non-propertized mode-line strings and
9032 frame titles in it; allocated from the heap in init_xdisp and
9033 resized as needed in store_mode_line_noprop_char. */
9034
9035 static char *mode_line_noprop_buf;
9036
9037 /* The buffer's end, and a current output position in it. */
9038
9039 static char *mode_line_noprop_buf_end;
9040 static char *mode_line_noprop_ptr;
9041
9042 #define MODE_LINE_NOPROP_LEN(start) \
9043 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
9044
9045 static enum {
9046 MODE_LINE_DISPLAY = 0,
9047 MODE_LINE_TITLE,
9048 MODE_LINE_NOPROP,
9049 MODE_LINE_STRING
9050 } mode_line_target;
9051
9052 /* Alist that caches the results of :propertize.
9053 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
9054 static Lisp_Object mode_line_proptrans_alist;
9055
9056 /* List of strings making up the mode-line. */
9057 static Lisp_Object mode_line_string_list;
9058
9059 /* Base face property when building propertized mode line string. */
9060 static Lisp_Object mode_line_string_face;
9061 static Lisp_Object mode_line_string_face_prop;
9062
9063
9064 /* Unwind data for mode line strings */
9065
9066 static Lisp_Object Vmode_line_unwind_vector;
9067
9068 static Lisp_Object
9069 format_mode_line_unwind_data (obuf, save_proptrans)
9070 struct buffer *obuf;
9071 {
9072 Lisp_Object vector;
9073
9074 /* Reduce consing by keeping one vector in
9075 Vwith_echo_area_save_vector. */
9076 vector = Vmode_line_unwind_vector;
9077 Vmode_line_unwind_vector = Qnil;
9078
9079 if (NILP (vector))
9080 vector = Fmake_vector (make_number (7), Qnil);
9081
9082 AREF (vector, 0) = make_number (mode_line_target);
9083 AREF (vector, 1) = make_number (MODE_LINE_NOPROP_LEN (0));
9084 AREF (vector, 2) = mode_line_string_list;
9085 AREF (vector, 3) = (save_proptrans ? mode_line_proptrans_alist : Qt);
9086 AREF (vector, 4) = mode_line_string_face;
9087 AREF (vector, 5) = mode_line_string_face_prop;
9088
9089 if (obuf)
9090 XSETBUFFER (AREF (vector, 6), obuf);
9091 else
9092 AREF (vector, 6) = Qnil;
9093
9094 return vector;
9095 }
9096
9097 static Lisp_Object
9098 unwind_format_mode_line (vector)
9099 Lisp_Object vector;
9100 {
9101 mode_line_target = XINT (AREF (vector, 0));
9102 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
9103 mode_line_string_list = AREF (vector, 2);
9104 if (! EQ (AREF (vector, 3), Qt))
9105 mode_line_proptrans_alist = AREF (vector, 3);
9106 mode_line_string_face = AREF (vector, 4);
9107 mode_line_string_face_prop = AREF (vector, 5);
9108
9109 if (!NILP (AREF (vector, 6)))
9110 {
9111 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
9112 AREF (vector, 6) = Qnil;
9113 }
9114
9115 Vmode_line_unwind_vector = vector;
9116 return Qnil;
9117 }
9118
9119
9120 /* Store a single character C for the frame title in mode_line_noprop_buf.
9121 Re-allocate mode_line_noprop_buf if necessary. */
9122
9123 static void
9124 #ifdef PROTOTYPES
9125 store_mode_line_noprop_char (char c)
9126 #else
9127 store_mode_line_noprop_char (c)
9128 char c;
9129 #endif
9130 {
9131 /* If output position has reached the end of the allocated buffer,
9132 double the buffer's size. */
9133 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
9134 {
9135 int len = MODE_LINE_NOPROP_LEN (0);
9136 int new_size = 2 * len * sizeof *mode_line_noprop_buf;
9137 mode_line_noprop_buf = (char *) xrealloc (mode_line_noprop_buf, new_size);
9138 mode_line_noprop_buf_end = mode_line_noprop_buf + new_size;
9139 mode_line_noprop_ptr = mode_line_noprop_buf + len;
9140 }
9141
9142 *mode_line_noprop_ptr++ = c;
9143 }
9144
9145
9146 /* Store part of a frame title in mode_line_noprop_buf, beginning at
9147 mode_line_noprop_ptr. STR is the string to store. Do not copy
9148 characters that yield more columns than PRECISION; PRECISION <= 0
9149 means copy the whole string. Pad with spaces until FIELD_WIDTH
9150 number of characters have been copied; FIELD_WIDTH <= 0 means don't
9151 pad. Called from display_mode_element when it is used to build a
9152 frame title. */
9153
9154 static int
9155 store_mode_line_noprop (str, field_width, precision)
9156 const unsigned char *str;
9157 int field_width, precision;
9158 {
9159 int n = 0;
9160 int dummy, nbytes;
9161
9162 /* Copy at most PRECISION chars from STR. */
9163 nbytes = strlen (str);
9164 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
9165 while (nbytes--)
9166 store_mode_line_noprop_char (*str++);
9167
9168 /* Fill up with spaces until FIELD_WIDTH reached. */
9169 while (field_width > 0
9170 && n < field_width)
9171 {
9172 store_mode_line_noprop_char (' ');
9173 ++n;
9174 }
9175
9176 return n;
9177 }
9178
9179 /***********************************************************************
9180 Frame Titles
9181 ***********************************************************************/
9182
9183 #ifdef HAVE_WINDOW_SYSTEM
9184
9185 /* Set the title of FRAME, if it has changed. The title format is
9186 Vicon_title_format if FRAME is iconified, otherwise it is
9187 frame_title_format. */
9188
9189 static void
9190 x_consider_frame_title (frame)
9191 Lisp_Object frame;
9192 {
9193 struct frame *f = XFRAME (frame);
9194
9195 if (FRAME_WINDOW_P (f)
9196 || FRAME_MINIBUF_ONLY_P (f)
9197 || f->explicit_name)
9198 {
9199 /* Do we have more than one visible frame on this X display? */
9200 Lisp_Object tail;
9201 Lisp_Object fmt;
9202 int title_start;
9203 char *title;
9204 int len;
9205 struct it it;
9206 int count = SPECPDL_INDEX ();
9207
9208 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
9209 {
9210 Lisp_Object other_frame = XCAR (tail);
9211 struct frame *tf = XFRAME (other_frame);
9212
9213 if (tf != f
9214 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
9215 && !FRAME_MINIBUF_ONLY_P (tf)
9216 && !EQ (other_frame, tip_frame)
9217 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
9218 break;
9219 }
9220
9221 /* Set global variable indicating that multiple frames exist. */
9222 multiple_frames = CONSP (tail);
9223
9224 /* Switch to the buffer of selected window of the frame. Set up
9225 mode_line_target so that display_mode_element will output into
9226 mode_line_noprop_buf; then display the title. */
9227 record_unwind_protect (unwind_format_mode_line,
9228 format_mode_line_unwind_data (current_buffer, 0));
9229
9230 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
9231 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
9232
9233 mode_line_target = MODE_LINE_TITLE;
9234 title_start = MODE_LINE_NOPROP_LEN (0);
9235 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
9236 NULL, DEFAULT_FACE_ID);
9237 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
9238 len = MODE_LINE_NOPROP_LEN (title_start);
9239 title = mode_line_noprop_buf + title_start;
9240 unbind_to (count, Qnil);
9241
9242 /* Set the title only if it's changed. This avoids consing in
9243 the common case where it hasn't. (If it turns out that we've
9244 already wasted too much time by walking through the list with
9245 display_mode_element, then we might need to optimize at a
9246 higher level than this.) */
9247 if (! STRINGP (f->name)
9248 || SBYTES (f->name) != len
9249 || bcmp (title, SDATA (f->name), len) != 0)
9250 x_implicitly_set_name (f, make_string (title, len), Qnil);
9251 }
9252 }
9253
9254 #endif /* not HAVE_WINDOW_SYSTEM */
9255
9256
9257
9258 \f
9259 /***********************************************************************
9260 Menu Bars
9261 ***********************************************************************/
9262
9263
9264 /* Prepare for redisplay by updating menu-bar item lists when
9265 appropriate. This can call eval. */
9266
9267 void
9268 prepare_menu_bars ()
9269 {
9270 int all_windows;
9271 struct gcpro gcpro1, gcpro2;
9272 struct frame *f;
9273 Lisp_Object tooltip_frame;
9274
9275 #ifdef HAVE_WINDOW_SYSTEM
9276 tooltip_frame = tip_frame;
9277 #else
9278 tooltip_frame = Qnil;
9279 #endif
9280
9281 /* Update all frame titles based on their buffer names, etc. We do
9282 this before the menu bars so that the buffer-menu will show the
9283 up-to-date frame titles. */
9284 #ifdef HAVE_WINDOW_SYSTEM
9285 if (windows_or_buffers_changed || update_mode_lines)
9286 {
9287 Lisp_Object tail, frame;
9288
9289 FOR_EACH_FRAME (tail, frame)
9290 {
9291 f = XFRAME (frame);
9292 if (!EQ (frame, tooltip_frame)
9293 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
9294 x_consider_frame_title (frame);
9295 }
9296 }
9297 #endif /* HAVE_WINDOW_SYSTEM */
9298
9299 /* Update the menu bar item lists, if appropriate. This has to be
9300 done before any actual redisplay or generation of display lines. */
9301 all_windows = (update_mode_lines
9302 || buffer_shared > 1
9303 || windows_or_buffers_changed);
9304 if (all_windows)
9305 {
9306 Lisp_Object tail, frame;
9307 int count = SPECPDL_INDEX ();
9308 /* 1 means that update_menu_bar has run its hooks
9309 so any further calls to update_menu_bar shouldn't do so again. */
9310 int menu_bar_hooks_run = 0;
9311
9312 record_unwind_save_match_data ();
9313
9314 FOR_EACH_FRAME (tail, frame)
9315 {
9316 f = XFRAME (frame);
9317
9318 /* Ignore tooltip frame. */
9319 if (EQ (frame, tooltip_frame))
9320 continue;
9321
9322 /* If a window on this frame changed size, report that to
9323 the user and clear the size-change flag. */
9324 if (FRAME_WINDOW_SIZES_CHANGED (f))
9325 {
9326 Lisp_Object functions;
9327
9328 /* Clear flag first in case we get an error below. */
9329 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
9330 functions = Vwindow_size_change_functions;
9331 GCPRO2 (tail, functions);
9332
9333 while (CONSP (functions))
9334 {
9335 call1 (XCAR (functions), frame);
9336 functions = XCDR (functions);
9337 }
9338 UNGCPRO;
9339 }
9340
9341 GCPRO1 (tail);
9342 menu_bar_hooks_run = update_menu_bar (f, 0, menu_bar_hooks_run);
9343 #ifdef HAVE_WINDOW_SYSTEM
9344 update_tool_bar (f, 0);
9345 #ifdef MAC_OS
9346 mac_update_title_bar (f, 0);
9347 #endif
9348 #endif
9349 UNGCPRO;
9350 }
9351
9352 unbind_to (count, Qnil);
9353 }
9354 else
9355 {
9356 struct frame *sf = SELECTED_FRAME ();
9357 update_menu_bar (sf, 1, 0);
9358 #ifdef HAVE_WINDOW_SYSTEM
9359 update_tool_bar (sf, 1);
9360 #ifdef MAC_OS
9361 mac_update_title_bar (sf, 1);
9362 #endif
9363 #endif
9364 }
9365
9366 /* Motif needs this. See comment in xmenu.c. Turn it off when
9367 pending_menu_activation is not defined. */
9368 #ifdef USE_X_TOOLKIT
9369 pending_menu_activation = 0;
9370 #endif
9371 }
9372
9373
9374 /* Update the menu bar item list for frame F. This has to be done
9375 before we start to fill in any display lines, because it can call
9376 eval.
9377
9378 If SAVE_MATCH_DATA is non-zero, we must save and restore it here.
9379
9380 If HOOKS_RUN is 1, that means a previous call to update_menu_bar
9381 already ran the menu bar hooks for this redisplay, so there
9382 is no need to run them again. The return value is the
9383 updated value of this flag, to pass to the next call. */
9384
9385 static int
9386 update_menu_bar (f, save_match_data, hooks_run)
9387 struct frame *f;
9388 int save_match_data;
9389 int hooks_run;
9390 {
9391 Lisp_Object window;
9392 register struct window *w;
9393
9394 /* If called recursively during a menu update, do nothing. This can
9395 happen when, for instance, an activate-menubar-hook causes a
9396 redisplay. */
9397 if (inhibit_menubar_update)
9398 return hooks_run;
9399
9400 window = FRAME_SELECTED_WINDOW (f);
9401 w = XWINDOW (window);
9402
9403 #if 0 /* The if statement below this if statement used to include the
9404 condition !NILP (w->update_mode_line), rather than using
9405 update_mode_lines directly, and this if statement may have
9406 been added to make that condition work. Now the if
9407 statement below matches its comment, this isn't needed. */
9408 if (update_mode_lines)
9409 w->update_mode_line = Qt;
9410 #endif
9411
9412 if (FRAME_WINDOW_P (f)
9413 ?
9414 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
9415 || defined (USE_GTK)
9416 FRAME_EXTERNAL_MENU_BAR (f)
9417 #else
9418 FRAME_MENU_BAR_LINES (f) > 0
9419 #endif
9420 : FRAME_MENU_BAR_LINES (f) > 0)
9421 {
9422 /* If the user has switched buffers or windows, we need to
9423 recompute to reflect the new bindings. But we'll
9424 recompute when update_mode_lines is set too; that means
9425 that people can use force-mode-line-update to request
9426 that the menu bar be recomputed. The adverse effect on
9427 the rest of the redisplay algorithm is about the same as
9428 windows_or_buffers_changed anyway. */
9429 if (windows_or_buffers_changed
9430 /* This used to test w->update_mode_line, but we believe
9431 there is no need to recompute the menu in that case. */
9432 || update_mode_lines
9433 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
9434 < BUF_MODIFF (XBUFFER (w->buffer)))
9435 != !NILP (w->last_had_star))
9436 || ((!NILP (Vtransient_mark_mode)
9437 && !NILP (XBUFFER (w->buffer)->mark_active))
9438 != !NILP (w->region_showing)))
9439 {
9440 struct buffer *prev = current_buffer;
9441 int count = SPECPDL_INDEX ();
9442
9443 specbind (Qinhibit_menubar_update, Qt);
9444
9445 set_buffer_internal_1 (XBUFFER (w->buffer));
9446 if (save_match_data)
9447 record_unwind_save_match_data ();
9448 if (NILP (Voverriding_local_map_menu_flag))
9449 {
9450 specbind (Qoverriding_terminal_local_map, Qnil);
9451 specbind (Qoverriding_local_map, Qnil);
9452 }
9453
9454 if (!hooks_run)
9455 {
9456 /* Run the Lucid hook. */
9457 safe_run_hooks (Qactivate_menubar_hook);
9458
9459 /* If it has changed current-menubar from previous value,
9460 really recompute the menu-bar from the value. */
9461 if (! NILP (Vlucid_menu_bar_dirty_flag))
9462 call0 (Qrecompute_lucid_menubar);
9463
9464 safe_run_hooks (Qmenu_bar_update_hook);
9465
9466 hooks_run = 1;
9467 }
9468
9469 XSETFRAME (Vmenu_updating_frame, f);
9470 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
9471
9472 /* Redisplay the menu bar in case we changed it. */
9473 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
9474 || defined (USE_GTK)
9475 if (FRAME_WINDOW_P (f))
9476 {
9477 #ifdef MAC_OS
9478 /* All frames on Mac OS share the same menubar. So only
9479 the selected frame should be allowed to set it. */
9480 if (f == SELECTED_FRAME ())
9481 #endif
9482 set_frame_menubar (f, 0, 0);
9483 }
9484 else
9485 /* On a terminal screen, the menu bar is an ordinary screen
9486 line, and this makes it get updated. */
9487 w->update_mode_line = Qt;
9488 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
9489 /* In the non-toolkit version, the menu bar is an ordinary screen
9490 line, and this makes it get updated. */
9491 w->update_mode_line = Qt;
9492 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
9493
9494 unbind_to (count, Qnil);
9495 set_buffer_internal_1 (prev);
9496 }
9497 }
9498
9499 return hooks_run;
9500 }
9501
9502
9503 \f
9504 /***********************************************************************
9505 Output Cursor
9506 ***********************************************************************/
9507
9508 #ifdef HAVE_WINDOW_SYSTEM
9509
9510 /* EXPORT:
9511 Nominal cursor position -- where to draw output.
9512 HPOS and VPOS are window relative glyph matrix coordinates.
9513 X and Y are window relative pixel coordinates. */
9514
9515 struct cursor_pos output_cursor;
9516
9517
9518 /* EXPORT:
9519 Set the global variable output_cursor to CURSOR. All cursor
9520 positions are relative to updated_window. */
9521
9522 void
9523 set_output_cursor (cursor)
9524 struct cursor_pos *cursor;
9525 {
9526 output_cursor.hpos = cursor->hpos;
9527 output_cursor.vpos = cursor->vpos;
9528 output_cursor.x = cursor->x;
9529 output_cursor.y = cursor->y;
9530 }
9531
9532
9533 /* EXPORT for RIF:
9534 Set a nominal cursor position.
9535
9536 HPOS and VPOS are column/row positions in a window glyph matrix. X
9537 and Y are window text area relative pixel positions.
9538
9539 If this is done during an update, updated_window will contain the
9540 window that is being updated and the position is the future output
9541 cursor position for that window. If updated_window is null, use
9542 selected_window and display the cursor at the given position. */
9543
9544 void
9545 x_cursor_to (vpos, hpos, y, x)
9546 int vpos, hpos, y, x;
9547 {
9548 struct window *w;
9549
9550 /* If updated_window is not set, work on selected_window. */
9551 if (updated_window)
9552 w = updated_window;
9553 else
9554 w = XWINDOW (selected_window);
9555
9556 /* Set the output cursor. */
9557 output_cursor.hpos = hpos;
9558 output_cursor.vpos = vpos;
9559 output_cursor.x = x;
9560 output_cursor.y = y;
9561
9562 /* If not called as part of an update, really display the cursor.
9563 This will also set the cursor position of W. */
9564 if (updated_window == NULL)
9565 {
9566 BLOCK_INPUT;
9567 display_and_set_cursor (w, 1, hpos, vpos, x, y);
9568 if (FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
9569 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (SELECTED_FRAME ());
9570 UNBLOCK_INPUT;
9571 }
9572 }
9573
9574 #endif /* HAVE_WINDOW_SYSTEM */
9575
9576 \f
9577 /***********************************************************************
9578 Tool-bars
9579 ***********************************************************************/
9580
9581 #ifdef HAVE_WINDOW_SYSTEM
9582
9583 /* Where the mouse was last time we reported a mouse event. */
9584
9585 FRAME_PTR last_mouse_frame;
9586
9587 /* Tool-bar item index of the item on which a mouse button was pressed
9588 or -1. */
9589
9590 int last_tool_bar_item;
9591
9592
9593 /* Update the tool-bar item list for frame F. This has to be done
9594 before we start to fill in any display lines. Called from
9595 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
9596 and restore it here. */
9597
9598 static void
9599 update_tool_bar (f, save_match_data)
9600 struct frame *f;
9601 int save_match_data;
9602 {
9603 #if defined (USE_GTK) || USE_MAC_TOOLBAR
9604 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
9605 #else
9606 int do_update = WINDOWP (f->tool_bar_window)
9607 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
9608 #endif
9609
9610 if (do_update)
9611 {
9612 Lisp_Object window;
9613 struct window *w;
9614
9615 window = FRAME_SELECTED_WINDOW (f);
9616 w = XWINDOW (window);
9617
9618 /* If the user has switched buffers or windows, we need to
9619 recompute to reflect the new bindings. But we'll
9620 recompute when update_mode_lines is set too; that means
9621 that people can use force-mode-line-update to request
9622 that the menu bar be recomputed. The adverse effect on
9623 the rest of the redisplay algorithm is about the same as
9624 windows_or_buffers_changed anyway. */
9625 if (windows_or_buffers_changed
9626 || !NILP (w->update_mode_line)
9627 || update_mode_lines
9628 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
9629 < BUF_MODIFF (XBUFFER (w->buffer)))
9630 != !NILP (w->last_had_star))
9631 || ((!NILP (Vtransient_mark_mode)
9632 && !NILP (XBUFFER (w->buffer)->mark_active))
9633 != !NILP (w->region_showing)))
9634 {
9635 struct buffer *prev = current_buffer;
9636 int count = SPECPDL_INDEX ();
9637 Lisp_Object new_tool_bar;
9638 int new_n_tool_bar;
9639 struct gcpro gcpro1;
9640
9641 /* Set current_buffer to the buffer of the selected
9642 window of the frame, so that we get the right local
9643 keymaps. */
9644 set_buffer_internal_1 (XBUFFER (w->buffer));
9645
9646 /* Save match data, if we must. */
9647 if (save_match_data)
9648 record_unwind_save_match_data ();
9649
9650 /* Make sure that we don't accidentally use bogus keymaps. */
9651 if (NILP (Voverriding_local_map_menu_flag))
9652 {
9653 specbind (Qoverriding_terminal_local_map, Qnil);
9654 specbind (Qoverriding_local_map, Qnil);
9655 }
9656
9657 GCPRO1 (new_tool_bar);
9658
9659 /* Build desired tool-bar items from keymaps. */
9660 new_tool_bar = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
9661 &new_n_tool_bar);
9662
9663 /* Redisplay the tool-bar if we changed it. */
9664 if (new_n_tool_bar != f->n_tool_bar_items
9665 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
9666 {
9667 /* Redisplay that happens asynchronously due to an expose event
9668 may access f->tool_bar_items. Make sure we update both
9669 variables within BLOCK_INPUT so no such event interrupts. */
9670 BLOCK_INPUT;
9671 f->tool_bar_items = new_tool_bar;
9672 f->n_tool_bar_items = new_n_tool_bar;
9673 w->update_mode_line = Qt;
9674 UNBLOCK_INPUT;
9675 }
9676
9677 UNGCPRO;
9678
9679 unbind_to (count, Qnil);
9680 set_buffer_internal_1 (prev);
9681 }
9682 }
9683 }
9684
9685
9686 /* Set F->desired_tool_bar_string to a Lisp string representing frame
9687 F's desired tool-bar contents. F->tool_bar_items must have
9688 been set up previously by calling prepare_menu_bars. */
9689
9690 static void
9691 build_desired_tool_bar_string (f)
9692 struct frame *f;
9693 {
9694 int i, size, size_needed;
9695 struct gcpro gcpro1, gcpro2, gcpro3;
9696 Lisp_Object image, plist, props;
9697
9698 image = plist = props = Qnil;
9699 GCPRO3 (image, plist, props);
9700
9701 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
9702 Otherwise, make a new string. */
9703
9704 /* The size of the string we might be able to reuse. */
9705 size = (STRINGP (f->desired_tool_bar_string)
9706 ? SCHARS (f->desired_tool_bar_string)
9707 : 0);
9708
9709 /* We need one space in the string for each image. */
9710 size_needed = f->n_tool_bar_items;
9711
9712 /* Reuse f->desired_tool_bar_string, if possible. */
9713 if (size < size_needed || NILP (f->desired_tool_bar_string))
9714 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
9715 make_number (' '));
9716 else
9717 {
9718 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
9719 Fremove_text_properties (make_number (0), make_number (size),
9720 props, f->desired_tool_bar_string);
9721 }
9722
9723 /* Put a `display' property on the string for the images to display,
9724 put a `menu_item' property on tool-bar items with a value that
9725 is the index of the item in F's tool-bar item vector. */
9726 for (i = 0; i < f->n_tool_bar_items; ++i)
9727 {
9728 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
9729
9730 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
9731 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
9732 int hmargin, vmargin, relief, idx, end;
9733 extern Lisp_Object QCrelief, QCmargin, QCconversion;
9734
9735 /* If image is a vector, choose the image according to the
9736 button state. */
9737 image = PROP (TOOL_BAR_ITEM_IMAGES);
9738 if (VECTORP (image))
9739 {
9740 if (enabled_p)
9741 idx = (selected_p
9742 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
9743 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
9744 else
9745 idx = (selected_p
9746 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
9747 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
9748
9749 xassert (ASIZE (image) >= idx);
9750 image = AREF (image, idx);
9751 }
9752 else
9753 idx = -1;
9754
9755 /* Ignore invalid image specifications. */
9756 if (!valid_image_p (image))
9757 continue;
9758
9759 /* Display the tool-bar button pressed, or depressed. */
9760 plist = Fcopy_sequence (XCDR (image));
9761
9762 /* Compute margin and relief to draw. */
9763 relief = (tool_bar_button_relief >= 0
9764 ? tool_bar_button_relief
9765 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
9766 hmargin = vmargin = relief;
9767
9768 if (INTEGERP (Vtool_bar_button_margin)
9769 && XINT (Vtool_bar_button_margin) > 0)
9770 {
9771 hmargin += XFASTINT (Vtool_bar_button_margin);
9772 vmargin += XFASTINT (Vtool_bar_button_margin);
9773 }
9774 else if (CONSP (Vtool_bar_button_margin))
9775 {
9776 if (INTEGERP (XCAR (Vtool_bar_button_margin))
9777 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
9778 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
9779
9780 if (INTEGERP (XCDR (Vtool_bar_button_margin))
9781 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
9782 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
9783 }
9784
9785 if (auto_raise_tool_bar_buttons_p)
9786 {
9787 /* Add a `:relief' property to the image spec if the item is
9788 selected. */
9789 if (selected_p)
9790 {
9791 plist = Fplist_put (plist, QCrelief, make_number (-relief));
9792 hmargin -= relief;
9793 vmargin -= relief;
9794 }
9795 }
9796 else
9797 {
9798 /* If image is selected, display it pressed, i.e. with a
9799 negative relief. If it's not selected, display it with a
9800 raised relief. */
9801 plist = Fplist_put (plist, QCrelief,
9802 (selected_p
9803 ? make_number (-relief)
9804 : make_number (relief)));
9805 hmargin -= relief;
9806 vmargin -= relief;
9807 }
9808
9809 /* Put a margin around the image. */
9810 if (hmargin || vmargin)
9811 {
9812 if (hmargin == vmargin)
9813 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
9814 else
9815 plist = Fplist_put (plist, QCmargin,
9816 Fcons (make_number (hmargin),
9817 make_number (vmargin)));
9818 }
9819
9820 /* If button is not enabled, and we don't have special images
9821 for the disabled state, make the image appear disabled by
9822 applying an appropriate algorithm to it. */
9823 if (!enabled_p && idx < 0)
9824 plist = Fplist_put (plist, QCconversion, Qdisabled);
9825
9826 /* Put a `display' text property on the string for the image to
9827 display. Put a `menu-item' property on the string that gives
9828 the start of this item's properties in the tool-bar items
9829 vector. */
9830 image = Fcons (Qimage, plist);
9831 props = list4 (Qdisplay, image,
9832 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
9833
9834 /* Let the last image hide all remaining spaces in the tool bar
9835 string. The string can be longer than needed when we reuse a
9836 previous string. */
9837 if (i + 1 == f->n_tool_bar_items)
9838 end = SCHARS (f->desired_tool_bar_string);
9839 else
9840 end = i + 1;
9841 Fadd_text_properties (make_number (i), make_number (end),
9842 props, f->desired_tool_bar_string);
9843 #undef PROP
9844 }
9845
9846 UNGCPRO;
9847 }
9848
9849
9850 /* Display one line of the tool-bar of frame IT->f.
9851
9852 HEIGHT specifies the desired height of the tool-bar line.
9853 If the actual height of the glyph row is less than HEIGHT, the
9854 row's height is increased to HEIGHT, and the icons are centered
9855 vertically in the new height.
9856
9857 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
9858 count a final empty row in case the tool-bar width exactly matches
9859 the window width.
9860 */
9861
9862 static void
9863 display_tool_bar_line (it, height)
9864 struct it *it;
9865 int height;
9866 {
9867 struct glyph_row *row = it->glyph_row;
9868 int max_x = it->last_visible_x;
9869 struct glyph *last;
9870
9871 prepare_desired_row (row);
9872 row->y = it->current_y;
9873
9874 /* Note that this isn't made use of if the face hasn't a box,
9875 so there's no need to check the face here. */
9876 it->start_of_box_run_p = 1;
9877
9878 while (it->current_x < max_x)
9879 {
9880 int x, n_glyphs_before, i, nglyphs;
9881 struct it it_before;
9882
9883 /* Get the next display element. */
9884 if (!get_next_display_element (it))
9885 {
9886 /* Don't count empty row if we are counting needed tool-bar lines. */
9887 if (height < 0 && !it->hpos)
9888 return;
9889 break;
9890 }
9891
9892 /* Produce glyphs. */
9893 n_glyphs_before = row->used[TEXT_AREA];
9894 it_before = *it;
9895
9896 PRODUCE_GLYPHS (it);
9897
9898 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
9899 i = 0;
9900 x = it_before.current_x;
9901 while (i < nglyphs)
9902 {
9903 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
9904
9905 if (x + glyph->pixel_width > max_x)
9906 {
9907 /* Glyph doesn't fit on line. Backtrack. */
9908 row->used[TEXT_AREA] = n_glyphs_before;
9909 *it = it_before;
9910 /* If this is the only glyph on this line, it will never fit on the
9911 toolbar, so skip it. But ensure there is at least one glyph,
9912 so we don't accidentally disable the tool-bar. */
9913 if (n_glyphs_before == 0
9914 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
9915 break;
9916 goto out;
9917 }
9918
9919 ++it->hpos;
9920 x += glyph->pixel_width;
9921 ++i;
9922 }
9923
9924 /* Stop at line ends. */
9925 if (ITERATOR_AT_END_OF_LINE_P (it))
9926 break;
9927
9928 set_iterator_to_next (it, 1);
9929 }
9930
9931 out:;
9932
9933 row->displays_text_p = row->used[TEXT_AREA] != 0;
9934
9935 /* Use default face for the border below the tool bar.
9936
9937 FIXME: When auto-resize-tool-bars is grow-only, there is
9938 no additional border below the possibly empty tool-bar lines.
9939 So to make the extra empty lines look "normal", we have to
9940 use the tool-bar face for the border too. */
9941 if (!row->displays_text_p && !EQ (Vauto_resize_tool_bars, Qgrow_only))
9942 it->face_id = DEFAULT_FACE_ID;
9943
9944 extend_face_to_end_of_line (it);
9945 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
9946 last->right_box_line_p = 1;
9947 if (last == row->glyphs[TEXT_AREA])
9948 last->left_box_line_p = 1;
9949
9950 /* Make line the desired height and center it vertically. */
9951 if ((height -= it->max_ascent + it->max_descent) > 0)
9952 {
9953 /* Don't add more than one line height. */
9954 height %= FRAME_LINE_HEIGHT (it->f);
9955 it->max_ascent += height / 2;
9956 it->max_descent += (height + 1) / 2;
9957 }
9958
9959 compute_line_metrics (it);
9960
9961 /* If line is empty, make it occupy the rest of the tool-bar. */
9962 if (!row->displays_text_p)
9963 {
9964 row->height = row->phys_height = it->last_visible_y - row->y;
9965 row->visible_height = row->height;
9966 row->ascent = row->phys_ascent = 0;
9967 row->extra_line_spacing = 0;
9968 }
9969
9970 row->full_width_p = 1;
9971 row->continued_p = 0;
9972 row->truncated_on_left_p = 0;
9973 row->truncated_on_right_p = 0;
9974
9975 it->current_x = it->hpos = 0;
9976 it->current_y += row->height;
9977 ++it->vpos;
9978 ++it->glyph_row;
9979 }
9980
9981
9982 /* Max tool-bar height. */
9983
9984 #define MAX_FRAME_TOOL_BAR_HEIGHT(f) \
9985 ((FRAME_LINE_HEIGHT (f) * FRAME_LINES (f)))
9986
9987 /* Value is the number of screen lines needed to make all tool-bar
9988 items of frame F visible. The number of actual rows needed is
9989 returned in *N_ROWS if non-NULL. */
9990
9991 static int
9992 tool_bar_lines_needed (f, n_rows)
9993 struct frame *f;
9994 int *n_rows;
9995 {
9996 struct window *w = XWINDOW (f->tool_bar_window);
9997 struct it it;
9998 /* tool_bar_lines_needed is called from redisplay_tool_bar after building
9999 the desired matrix, so use (unused) mode-line row as temporary row to
10000 avoid destroying the first tool-bar row. */
10001 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
10002
10003 /* Initialize an iterator for iteration over
10004 F->desired_tool_bar_string in the tool-bar window of frame F. */
10005 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
10006 it.first_visible_x = 0;
10007 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
10008 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
10009
10010 while (!ITERATOR_AT_END_P (&it))
10011 {
10012 clear_glyph_row (temp_row);
10013 it.glyph_row = temp_row;
10014 display_tool_bar_line (&it, -1);
10015 }
10016 clear_glyph_row (temp_row);
10017
10018 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
10019 if (n_rows)
10020 *n_rows = it.vpos > 0 ? it.vpos : -1;
10021
10022 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
10023 }
10024
10025
10026 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
10027 0, 1, 0,
10028 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
10029 (frame)
10030 Lisp_Object frame;
10031 {
10032 struct frame *f;
10033 struct window *w;
10034 int nlines = 0;
10035
10036 if (NILP (frame))
10037 frame = selected_frame;
10038 else
10039 CHECK_FRAME (frame);
10040 f = XFRAME (frame);
10041
10042 if (WINDOWP (f->tool_bar_window)
10043 || (w = XWINDOW (f->tool_bar_window),
10044 WINDOW_TOTAL_LINES (w) > 0))
10045 {
10046 update_tool_bar (f, 1);
10047 if (f->n_tool_bar_items)
10048 {
10049 build_desired_tool_bar_string (f);
10050 nlines = tool_bar_lines_needed (f, NULL);
10051 }
10052 }
10053
10054 return make_number (nlines);
10055 }
10056
10057
10058 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
10059 height should be changed. */
10060
10061 static int
10062 redisplay_tool_bar (f)
10063 struct frame *f;
10064 {
10065 struct window *w;
10066 struct it it;
10067 struct glyph_row *row;
10068
10069 #if defined (USE_GTK) || USE_MAC_TOOLBAR
10070 if (FRAME_EXTERNAL_TOOL_BAR (f))
10071 update_frame_tool_bar (f);
10072 return 0;
10073 #endif
10074
10075 /* If frame hasn't a tool-bar window or if it is zero-height, don't
10076 do anything. This means you must start with tool-bar-lines
10077 non-zero to get the auto-sizing effect. Or in other words, you
10078 can turn off tool-bars by specifying tool-bar-lines zero. */
10079 if (!WINDOWP (f->tool_bar_window)
10080 || (w = XWINDOW (f->tool_bar_window),
10081 WINDOW_TOTAL_LINES (w) == 0))
10082 return 0;
10083
10084 /* Set up an iterator for the tool-bar window. */
10085 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
10086 it.first_visible_x = 0;
10087 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
10088 row = it.glyph_row;
10089
10090 /* Build a string that represents the contents of the tool-bar. */
10091 build_desired_tool_bar_string (f);
10092 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
10093
10094 if (f->n_tool_bar_rows == 0)
10095 {
10096 int nlines;
10097
10098 if ((nlines = tool_bar_lines_needed (f, &f->n_tool_bar_rows),
10099 nlines != WINDOW_TOTAL_LINES (w)))
10100 {
10101 extern Lisp_Object Qtool_bar_lines;
10102 Lisp_Object frame;
10103 int old_height = WINDOW_TOTAL_LINES (w);
10104
10105 XSETFRAME (frame, f);
10106 Fmodify_frame_parameters (frame,
10107 Fcons (Fcons (Qtool_bar_lines,
10108 make_number (nlines)),
10109 Qnil));
10110 if (WINDOW_TOTAL_LINES (w) != old_height)
10111 {
10112 clear_glyph_matrix (w->desired_matrix);
10113 fonts_changed_p = 1;
10114 return 1;
10115 }
10116 }
10117 }
10118
10119 /* Display as many lines as needed to display all tool-bar items. */
10120
10121 if (f->n_tool_bar_rows > 0)
10122 {
10123 int border, rows, height, extra;
10124
10125 if (INTEGERP (Vtool_bar_border))
10126 border = XINT (Vtool_bar_border);
10127 else if (EQ (Vtool_bar_border, Qinternal_border_width))
10128 border = FRAME_INTERNAL_BORDER_WIDTH (f);
10129 else if (EQ (Vtool_bar_border, Qborder_width))
10130 border = f->border_width;
10131 else
10132 border = 0;
10133 if (border < 0)
10134 border = 0;
10135
10136 rows = f->n_tool_bar_rows;
10137 height = max (1, (it.last_visible_y - border) / rows);
10138 extra = it.last_visible_y - border - height * rows;
10139
10140 while (it.current_y < it.last_visible_y)
10141 {
10142 int h = 0;
10143 if (extra > 0 && rows-- > 0)
10144 {
10145 h = (extra + rows - 1) / rows;
10146 extra -= h;
10147 }
10148 display_tool_bar_line (&it, height + h);
10149 }
10150 }
10151 else
10152 {
10153 while (it.current_y < it.last_visible_y)
10154 display_tool_bar_line (&it, 0);
10155 }
10156
10157 /* It doesn't make much sense to try scrolling in the tool-bar
10158 window, so don't do it. */
10159 w->desired_matrix->no_scrolling_p = 1;
10160 w->must_be_updated_p = 1;
10161
10162 if (!NILP (Vauto_resize_tool_bars))
10163 {
10164 int max_tool_bar_height = MAX_FRAME_TOOL_BAR_HEIGHT (f);
10165 int change_height_p = 0;
10166
10167 /* If we couldn't display everything, change the tool-bar's
10168 height if there is room for more. */
10169 if (IT_STRING_CHARPOS (it) < it.end_charpos
10170 && it.current_y < max_tool_bar_height)
10171 change_height_p = 1;
10172
10173 row = it.glyph_row - 1;
10174
10175 /* If there are blank lines at the end, except for a partially
10176 visible blank line at the end that is smaller than
10177 FRAME_LINE_HEIGHT, change the tool-bar's height. */
10178 if (!row->displays_text_p
10179 && row->height >= FRAME_LINE_HEIGHT (f))
10180 change_height_p = 1;
10181
10182 /* If row displays tool-bar items, but is partially visible,
10183 change the tool-bar's height. */
10184 if (row->displays_text_p
10185 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y
10186 && MATRIX_ROW_BOTTOM_Y (row) < max_tool_bar_height)
10187 change_height_p = 1;
10188
10189 /* Resize windows as needed by changing the `tool-bar-lines'
10190 frame parameter. */
10191 if (change_height_p)
10192 {
10193 extern Lisp_Object Qtool_bar_lines;
10194 Lisp_Object frame;
10195 int old_height = WINDOW_TOTAL_LINES (w);
10196 int nrows;
10197 int nlines = tool_bar_lines_needed (f, &nrows);
10198
10199 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
10200 && !f->minimize_tool_bar_window_p)
10201 ? (nlines > old_height)
10202 : (nlines != old_height));
10203 f->minimize_tool_bar_window_p = 0;
10204
10205 if (change_height_p)
10206 {
10207 XSETFRAME (frame, f);
10208 Fmodify_frame_parameters (frame,
10209 Fcons (Fcons (Qtool_bar_lines,
10210 make_number (nlines)),
10211 Qnil));
10212 if (WINDOW_TOTAL_LINES (w) != old_height)
10213 {
10214 clear_glyph_matrix (w->desired_matrix);
10215 f->n_tool_bar_rows = nrows;
10216 fonts_changed_p = 1;
10217 return 1;
10218 }
10219 }
10220 }
10221 }
10222
10223 f->minimize_tool_bar_window_p = 0;
10224 return 0;
10225 }
10226
10227
10228 /* Get information about the tool-bar item which is displayed in GLYPH
10229 on frame F. Return in *PROP_IDX the index where tool-bar item
10230 properties start in F->tool_bar_items. Value is zero if
10231 GLYPH doesn't display a tool-bar item. */
10232
10233 static int
10234 tool_bar_item_info (f, glyph, prop_idx)
10235 struct frame *f;
10236 struct glyph *glyph;
10237 int *prop_idx;
10238 {
10239 Lisp_Object prop;
10240 int success_p;
10241 int charpos;
10242
10243 /* This function can be called asynchronously, which means we must
10244 exclude any possibility that Fget_text_property signals an
10245 error. */
10246 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
10247 charpos = max (0, charpos);
10248
10249 /* Get the text property `menu-item' at pos. The value of that
10250 property is the start index of this item's properties in
10251 F->tool_bar_items. */
10252 prop = Fget_text_property (make_number (charpos),
10253 Qmenu_item, f->current_tool_bar_string);
10254 if (INTEGERP (prop))
10255 {
10256 *prop_idx = XINT (prop);
10257 success_p = 1;
10258 }
10259 else
10260 success_p = 0;
10261
10262 return success_p;
10263 }
10264
10265 \f
10266 /* Get information about the tool-bar item at position X/Y on frame F.
10267 Return in *GLYPH a pointer to the glyph of the tool-bar item in
10268 the current matrix of the tool-bar window of F, or NULL if not
10269 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
10270 item in F->tool_bar_items. Value is
10271
10272 -1 if X/Y is not on a tool-bar item
10273 0 if X/Y is on the same item that was highlighted before.
10274 1 otherwise. */
10275
10276 static int
10277 get_tool_bar_item (f, x, y, glyph, hpos, vpos, prop_idx)
10278 struct frame *f;
10279 int x, y;
10280 struct glyph **glyph;
10281 int *hpos, *vpos, *prop_idx;
10282 {
10283 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10284 struct window *w = XWINDOW (f->tool_bar_window);
10285 int area;
10286
10287 /* Find the glyph under X/Y. */
10288 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
10289 if (*glyph == NULL)
10290 return -1;
10291
10292 /* Get the start of this tool-bar item's properties in
10293 f->tool_bar_items. */
10294 if (!tool_bar_item_info (f, *glyph, prop_idx))
10295 return -1;
10296
10297 /* Is mouse on the highlighted item? */
10298 if (EQ (f->tool_bar_window, dpyinfo->mouse_face_window)
10299 && *vpos >= dpyinfo->mouse_face_beg_row
10300 && *vpos <= dpyinfo->mouse_face_end_row
10301 && (*vpos > dpyinfo->mouse_face_beg_row
10302 || *hpos >= dpyinfo->mouse_face_beg_col)
10303 && (*vpos < dpyinfo->mouse_face_end_row
10304 || *hpos < dpyinfo->mouse_face_end_col
10305 || dpyinfo->mouse_face_past_end))
10306 return 0;
10307
10308 return 1;
10309 }
10310
10311
10312 /* EXPORT:
10313 Handle mouse button event on the tool-bar of frame F, at
10314 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
10315 0 for button release. MODIFIERS is event modifiers for button
10316 release. */
10317
10318 void
10319 handle_tool_bar_click (f, x, y, down_p, modifiers)
10320 struct frame *f;
10321 int x, y, down_p;
10322 unsigned int modifiers;
10323 {
10324 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10325 struct window *w = XWINDOW (f->tool_bar_window);
10326 int hpos, vpos, prop_idx;
10327 struct glyph *glyph;
10328 Lisp_Object enabled_p;
10329
10330 /* If not on the highlighted tool-bar item, return. */
10331 frame_to_window_pixel_xy (w, &x, &y);
10332 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
10333 return;
10334
10335 /* If item is disabled, do nothing. */
10336 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
10337 if (NILP (enabled_p))
10338 return;
10339
10340 if (down_p)
10341 {
10342 /* Show item in pressed state. */
10343 show_mouse_face (dpyinfo, DRAW_IMAGE_SUNKEN);
10344 dpyinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
10345 last_tool_bar_item = prop_idx;
10346 }
10347 else
10348 {
10349 Lisp_Object key, frame;
10350 struct input_event event;
10351 EVENT_INIT (event);
10352
10353 /* Show item in released state. */
10354 show_mouse_face (dpyinfo, DRAW_IMAGE_RAISED);
10355 dpyinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
10356
10357 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
10358
10359 XSETFRAME (frame, f);
10360 event.kind = TOOL_BAR_EVENT;
10361 event.frame_or_window = frame;
10362 event.arg = frame;
10363 kbd_buffer_store_event (&event);
10364
10365 event.kind = TOOL_BAR_EVENT;
10366 event.frame_or_window = frame;
10367 event.arg = key;
10368 event.modifiers = modifiers;
10369 kbd_buffer_store_event (&event);
10370 last_tool_bar_item = -1;
10371 }
10372 }
10373
10374
10375 /* Possibly highlight a tool-bar item on frame F when mouse moves to
10376 tool-bar window-relative coordinates X/Y. Called from
10377 note_mouse_highlight. */
10378
10379 static void
10380 note_tool_bar_highlight (f, x, y)
10381 struct frame *f;
10382 int x, y;
10383 {
10384 Lisp_Object window = f->tool_bar_window;
10385 struct window *w = XWINDOW (window);
10386 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10387 int hpos, vpos;
10388 struct glyph *glyph;
10389 struct glyph_row *row;
10390 int i;
10391 Lisp_Object enabled_p;
10392 int prop_idx;
10393 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
10394 int mouse_down_p, rc;
10395
10396 /* Function note_mouse_highlight is called with negative x(y
10397 values when mouse moves outside of the frame. */
10398 if (x <= 0 || y <= 0)
10399 {
10400 clear_mouse_face (dpyinfo);
10401 return;
10402 }
10403
10404 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
10405 if (rc < 0)
10406 {
10407 /* Not on tool-bar item. */
10408 clear_mouse_face (dpyinfo);
10409 return;
10410 }
10411 else if (rc == 0)
10412 /* On same tool-bar item as before. */
10413 goto set_help_echo;
10414
10415 clear_mouse_face (dpyinfo);
10416
10417 /* Mouse is down, but on different tool-bar item? */
10418 mouse_down_p = (dpyinfo->grabbed
10419 && f == last_mouse_frame
10420 && FRAME_LIVE_P (f));
10421 if (mouse_down_p
10422 && last_tool_bar_item != prop_idx)
10423 return;
10424
10425 dpyinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
10426 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
10427
10428 /* If tool-bar item is not enabled, don't highlight it. */
10429 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
10430 if (!NILP (enabled_p))
10431 {
10432 /* Compute the x-position of the glyph. In front and past the
10433 image is a space. We include this in the highlighted area. */
10434 row = MATRIX_ROW (w->current_matrix, vpos);
10435 for (i = x = 0; i < hpos; ++i)
10436 x += row->glyphs[TEXT_AREA][i].pixel_width;
10437
10438 /* Record this as the current active region. */
10439 dpyinfo->mouse_face_beg_col = hpos;
10440 dpyinfo->mouse_face_beg_row = vpos;
10441 dpyinfo->mouse_face_beg_x = x;
10442 dpyinfo->mouse_face_beg_y = row->y;
10443 dpyinfo->mouse_face_past_end = 0;
10444
10445 dpyinfo->mouse_face_end_col = hpos + 1;
10446 dpyinfo->mouse_face_end_row = vpos;
10447 dpyinfo->mouse_face_end_x = x + glyph->pixel_width;
10448 dpyinfo->mouse_face_end_y = row->y;
10449 dpyinfo->mouse_face_window = window;
10450 dpyinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
10451
10452 /* Display it as active. */
10453 show_mouse_face (dpyinfo, draw);
10454 dpyinfo->mouse_face_image_state = draw;
10455 }
10456
10457 set_help_echo:
10458
10459 /* Set help_echo_string to a help string to display for this tool-bar item.
10460 XTread_socket does the rest. */
10461 help_echo_object = help_echo_window = Qnil;
10462 help_echo_pos = -1;
10463 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
10464 if (NILP (help_echo_string))
10465 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
10466 }
10467
10468 #endif /* HAVE_WINDOW_SYSTEM */
10469
10470
10471 \f
10472 /************************************************************************
10473 Horizontal scrolling
10474 ************************************************************************/
10475
10476 static int hscroll_window_tree P_ ((Lisp_Object));
10477 static int hscroll_windows P_ ((Lisp_Object));
10478
10479 /* For all leaf windows in the window tree rooted at WINDOW, set their
10480 hscroll value so that PT is (i) visible in the window, and (ii) so
10481 that it is not within a certain margin at the window's left and
10482 right border. Value is non-zero if any window's hscroll has been
10483 changed. */
10484
10485 static int
10486 hscroll_window_tree (window)
10487 Lisp_Object window;
10488 {
10489 int hscrolled_p = 0;
10490 int hscroll_relative_p = FLOATP (Vhscroll_step);
10491 int hscroll_step_abs = 0;
10492 double hscroll_step_rel = 0;
10493
10494 if (hscroll_relative_p)
10495 {
10496 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
10497 if (hscroll_step_rel < 0)
10498 {
10499 hscroll_relative_p = 0;
10500 hscroll_step_abs = 0;
10501 }
10502 }
10503 else if (INTEGERP (Vhscroll_step))
10504 {
10505 hscroll_step_abs = XINT (Vhscroll_step);
10506 if (hscroll_step_abs < 0)
10507 hscroll_step_abs = 0;
10508 }
10509 else
10510 hscroll_step_abs = 0;
10511
10512 while (WINDOWP (window))
10513 {
10514 struct window *w = XWINDOW (window);
10515
10516 if (WINDOWP (w->hchild))
10517 hscrolled_p |= hscroll_window_tree (w->hchild);
10518 else if (WINDOWP (w->vchild))
10519 hscrolled_p |= hscroll_window_tree (w->vchild);
10520 else if (w->cursor.vpos >= 0)
10521 {
10522 int h_margin;
10523 int text_area_width;
10524 struct glyph_row *current_cursor_row
10525 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
10526 struct glyph_row *desired_cursor_row
10527 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
10528 struct glyph_row *cursor_row
10529 = (desired_cursor_row->enabled_p
10530 ? desired_cursor_row
10531 : current_cursor_row);
10532
10533 text_area_width = window_box_width (w, TEXT_AREA);
10534
10535 /* Scroll when cursor is inside this scroll margin. */
10536 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
10537
10538 if ((XFASTINT (w->hscroll)
10539 && w->cursor.x <= h_margin)
10540 || (cursor_row->enabled_p
10541 && cursor_row->truncated_on_right_p
10542 && (w->cursor.x >= text_area_width - h_margin)))
10543 {
10544 struct it it;
10545 int hscroll;
10546 struct buffer *saved_current_buffer;
10547 int pt;
10548 int wanted_x;
10549
10550 /* Find point in a display of infinite width. */
10551 saved_current_buffer = current_buffer;
10552 current_buffer = XBUFFER (w->buffer);
10553
10554 if (w == XWINDOW (selected_window))
10555 pt = BUF_PT (current_buffer);
10556 else
10557 {
10558 pt = marker_position (w->pointm);
10559 pt = max (BEGV, pt);
10560 pt = min (ZV, pt);
10561 }
10562
10563 /* Move iterator to pt starting at cursor_row->start in
10564 a line with infinite width. */
10565 init_to_row_start (&it, w, cursor_row);
10566 it.last_visible_x = INFINITY;
10567 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
10568 current_buffer = saved_current_buffer;
10569
10570 /* Position cursor in window. */
10571 if (!hscroll_relative_p && hscroll_step_abs == 0)
10572 hscroll = max (0, (it.current_x
10573 - (ITERATOR_AT_END_OF_LINE_P (&it)
10574 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
10575 : (text_area_width / 2))))
10576 / FRAME_COLUMN_WIDTH (it.f);
10577 else if (w->cursor.x >= text_area_width - h_margin)
10578 {
10579 if (hscroll_relative_p)
10580 wanted_x = text_area_width * (1 - hscroll_step_rel)
10581 - h_margin;
10582 else
10583 wanted_x = text_area_width
10584 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
10585 - h_margin;
10586 hscroll
10587 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
10588 }
10589 else
10590 {
10591 if (hscroll_relative_p)
10592 wanted_x = text_area_width * hscroll_step_rel
10593 + h_margin;
10594 else
10595 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
10596 + h_margin;
10597 hscroll
10598 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
10599 }
10600 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
10601
10602 /* Don't call Fset_window_hscroll if value hasn't
10603 changed because it will prevent redisplay
10604 optimizations. */
10605 if (XFASTINT (w->hscroll) != hscroll)
10606 {
10607 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
10608 w->hscroll = make_number (hscroll);
10609 hscrolled_p = 1;
10610 }
10611 }
10612 }
10613
10614 window = w->next;
10615 }
10616
10617 /* Value is non-zero if hscroll of any leaf window has been changed. */
10618 return hscrolled_p;
10619 }
10620
10621
10622 /* Set hscroll so that cursor is visible and not inside horizontal
10623 scroll margins for all windows in the tree rooted at WINDOW. See
10624 also hscroll_window_tree above. Value is non-zero if any window's
10625 hscroll has been changed. If it has, desired matrices on the frame
10626 of WINDOW are cleared. */
10627
10628 static int
10629 hscroll_windows (window)
10630 Lisp_Object window;
10631 {
10632 int hscrolled_p;
10633
10634 if (automatic_hscrolling_p)
10635 {
10636 hscrolled_p = hscroll_window_tree (window);
10637 if (hscrolled_p)
10638 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
10639 }
10640 else
10641 hscrolled_p = 0;
10642 return hscrolled_p;
10643 }
10644
10645
10646 \f
10647 /************************************************************************
10648 Redisplay
10649 ************************************************************************/
10650
10651 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
10652 to a non-zero value. This is sometimes handy to have in a debugger
10653 session. */
10654
10655 #if GLYPH_DEBUG
10656
10657 /* First and last unchanged row for try_window_id. */
10658
10659 int debug_first_unchanged_at_end_vpos;
10660 int debug_last_unchanged_at_beg_vpos;
10661
10662 /* Delta vpos and y. */
10663
10664 int debug_dvpos, debug_dy;
10665
10666 /* Delta in characters and bytes for try_window_id. */
10667
10668 int debug_delta, debug_delta_bytes;
10669
10670 /* Values of window_end_pos and window_end_vpos at the end of
10671 try_window_id. */
10672
10673 EMACS_INT debug_end_pos, debug_end_vpos;
10674
10675 /* Append a string to W->desired_matrix->method. FMT is a printf
10676 format string. A1...A9 are a supplement for a variable-length
10677 argument list. If trace_redisplay_p is non-zero also printf the
10678 resulting string to stderr. */
10679
10680 static void
10681 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
10682 struct window *w;
10683 char *fmt;
10684 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
10685 {
10686 char buffer[512];
10687 char *method = w->desired_matrix->method;
10688 int len = strlen (method);
10689 int size = sizeof w->desired_matrix->method;
10690 int remaining = size - len - 1;
10691
10692 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
10693 if (len && remaining)
10694 {
10695 method[len] = '|';
10696 --remaining, ++len;
10697 }
10698
10699 strncpy (method + len, buffer, remaining);
10700
10701 if (trace_redisplay_p)
10702 fprintf (stderr, "%p (%s): %s\n",
10703 w,
10704 ((BUFFERP (w->buffer)
10705 && STRINGP (XBUFFER (w->buffer)->name))
10706 ? (char *) SDATA (XBUFFER (w->buffer)->name)
10707 : "no buffer"),
10708 buffer);
10709 }
10710
10711 #endif /* GLYPH_DEBUG */
10712
10713
10714 /* Value is non-zero if all changes in window W, which displays
10715 current_buffer, are in the text between START and END. START is a
10716 buffer position, END is given as a distance from Z. Used in
10717 redisplay_internal for display optimization. */
10718
10719 static INLINE int
10720 text_outside_line_unchanged_p (w, start, end)
10721 struct window *w;
10722 int start, end;
10723 {
10724 int unchanged_p = 1;
10725
10726 /* If text or overlays have changed, see where. */
10727 if (XFASTINT (w->last_modified) < MODIFF
10728 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
10729 {
10730 /* Gap in the line? */
10731 if (GPT < start || Z - GPT < end)
10732 unchanged_p = 0;
10733
10734 /* Changes start in front of the line, or end after it? */
10735 if (unchanged_p
10736 && (BEG_UNCHANGED < start - 1
10737 || END_UNCHANGED < end))
10738 unchanged_p = 0;
10739
10740 /* If selective display, can't optimize if changes start at the
10741 beginning of the line. */
10742 if (unchanged_p
10743 && INTEGERP (current_buffer->selective_display)
10744 && XINT (current_buffer->selective_display) > 0
10745 && (BEG_UNCHANGED < start || GPT <= start))
10746 unchanged_p = 0;
10747
10748 /* If there are overlays at the start or end of the line, these
10749 may have overlay strings with newlines in them. A change at
10750 START, for instance, may actually concern the display of such
10751 overlay strings as well, and they are displayed on different
10752 lines. So, quickly rule out this case. (For the future, it
10753 might be desirable to implement something more telling than
10754 just BEG/END_UNCHANGED.) */
10755 if (unchanged_p)
10756 {
10757 if (BEG + BEG_UNCHANGED == start
10758 && overlay_touches_p (start))
10759 unchanged_p = 0;
10760 if (END_UNCHANGED == end
10761 && overlay_touches_p (Z - end))
10762 unchanged_p = 0;
10763 }
10764 }
10765
10766 return unchanged_p;
10767 }
10768
10769
10770 /* Do a frame update, taking possible shortcuts into account. This is
10771 the main external entry point for redisplay.
10772
10773 If the last redisplay displayed an echo area message and that message
10774 is no longer requested, we clear the echo area or bring back the
10775 mini-buffer if that is in use. */
10776
10777 void
10778 redisplay ()
10779 {
10780 redisplay_internal (0);
10781 }
10782
10783
10784 static Lisp_Object
10785 overlay_arrow_string_or_property (var)
10786 Lisp_Object var;
10787 {
10788 Lisp_Object val;
10789
10790 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
10791 return val;
10792
10793 return Voverlay_arrow_string;
10794 }
10795
10796 /* Return 1 if there are any overlay-arrows in current_buffer. */
10797 static int
10798 overlay_arrow_in_current_buffer_p ()
10799 {
10800 Lisp_Object vlist;
10801
10802 for (vlist = Voverlay_arrow_variable_list;
10803 CONSP (vlist);
10804 vlist = XCDR (vlist))
10805 {
10806 Lisp_Object var = XCAR (vlist);
10807 Lisp_Object val;
10808
10809 if (!SYMBOLP (var))
10810 continue;
10811 val = find_symbol_value (var);
10812 if (MARKERP (val)
10813 && current_buffer == XMARKER (val)->buffer)
10814 return 1;
10815 }
10816 return 0;
10817 }
10818
10819
10820 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
10821 has changed. */
10822
10823 static int
10824 overlay_arrows_changed_p ()
10825 {
10826 Lisp_Object vlist;
10827
10828 for (vlist = Voverlay_arrow_variable_list;
10829 CONSP (vlist);
10830 vlist = XCDR (vlist))
10831 {
10832 Lisp_Object var = XCAR (vlist);
10833 Lisp_Object val, pstr;
10834
10835 if (!SYMBOLP (var))
10836 continue;
10837 val = find_symbol_value (var);
10838 if (!MARKERP (val))
10839 continue;
10840 if (! EQ (COERCE_MARKER (val),
10841 Fget (var, Qlast_arrow_position))
10842 || ! (pstr = overlay_arrow_string_or_property (var),
10843 EQ (pstr, Fget (var, Qlast_arrow_string))))
10844 return 1;
10845 }
10846 return 0;
10847 }
10848
10849 /* Mark overlay arrows to be updated on next redisplay. */
10850
10851 static void
10852 update_overlay_arrows (up_to_date)
10853 int up_to_date;
10854 {
10855 Lisp_Object vlist;
10856
10857 for (vlist = Voverlay_arrow_variable_list;
10858 CONSP (vlist);
10859 vlist = XCDR (vlist))
10860 {
10861 Lisp_Object var = XCAR (vlist);
10862
10863 if (!SYMBOLP (var))
10864 continue;
10865
10866 if (up_to_date > 0)
10867 {
10868 Lisp_Object val = find_symbol_value (var);
10869 Fput (var, Qlast_arrow_position,
10870 COERCE_MARKER (val));
10871 Fput (var, Qlast_arrow_string,
10872 overlay_arrow_string_or_property (var));
10873 }
10874 else if (up_to_date < 0
10875 || !NILP (Fget (var, Qlast_arrow_position)))
10876 {
10877 Fput (var, Qlast_arrow_position, Qt);
10878 Fput (var, Qlast_arrow_string, Qt);
10879 }
10880 }
10881 }
10882
10883
10884 /* Return overlay arrow string to display at row.
10885 Return integer (bitmap number) for arrow bitmap in left fringe.
10886 Return nil if no overlay arrow. */
10887
10888 static Lisp_Object
10889 overlay_arrow_at_row (it, row)
10890 struct it *it;
10891 struct glyph_row *row;
10892 {
10893 Lisp_Object vlist;
10894
10895 for (vlist = Voverlay_arrow_variable_list;
10896 CONSP (vlist);
10897 vlist = XCDR (vlist))
10898 {
10899 Lisp_Object var = XCAR (vlist);
10900 Lisp_Object val;
10901
10902 if (!SYMBOLP (var))
10903 continue;
10904
10905 val = find_symbol_value (var);
10906
10907 if (MARKERP (val)
10908 && current_buffer == XMARKER (val)->buffer
10909 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
10910 {
10911 if (FRAME_WINDOW_P (it->f)
10912 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
10913 {
10914 #ifdef HAVE_WINDOW_SYSTEM
10915 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
10916 {
10917 int fringe_bitmap;
10918 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
10919 return make_number (fringe_bitmap);
10920 }
10921 #endif
10922 return make_number (-1); /* Use default arrow bitmap */
10923 }
10924 return overlay_arrow_string_or_property (var);
10925 }
10926 }
10927
10928 return Qnil;
10929 }
10930
10931 /* Return 1 if point moved out of or into a composition. Otherwise
10932 return 0. PREV_BUF and PREV_PT are the last point buffer and
10933 position. BUF and PT are the current point buffer and position. */
10934
10935 int
10936 check_point_in_composition (prev_buf, prev_pt, buf, pt)
10937 struct buffer *prev_buf, *buf;
10938 int prev_pt, pt;
10939 {
10940 EMACS_INT start, end;
10941 Lisp_Object prop;
10942 Lisp_Object buffer;
10943
10944 XSETBUFFER (buffer, buf);
10945 /* Check a composition at the last point if point moved within the
10946 same buffer. */
10947 if (prev_buf == buf)
10948 {
10949 if (prev_pt == pt)
10950 /* Point didn't move. */
10951 return 0;
10952
10953 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
10954 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
10955 && COMPOSITION_VALID_P (start, end, prop)
10956 && start < prev_pt && end > prev_pt)
10957 /* The last point was within the composition. Return 1 iff
10958 point moved out of the composition. */
10959 return (pt <= start || pt >= end);
10960 }
10961
10962 /* Check a composition at the current point. */
10963 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
10964 && find_composition (pt, -1, &start, &end, &prop, buffer)
10965 && COMPOSITION_VALID_P (start, end, prop)
10966 && start < pt && end > pt);
10967 }
10968
10969
10970 /* Reconsider the setting of B->clip_changed which is displayed
10971 in window W. */
10972
10973 static INLINE void
10974 reconsider_clip_changes (w, b)
10975 struct window *w;
10976 struct buffer *b;
10977 {
10978 if (b->clip_changed
10979 && !NILP (w->window_end_valid)
10980 && w->current_matrix->buffer == b
10981 && w->current_matrix->zv == BUF_ZV (b)
10982 && w->current_matrix->begv == BUF_BEGV (b))
10983 b->clip_changed = 0;
10984
10985 /* If display wasn't paused, and W is not a tool bar window, see if
10986 point has been moved into or out of a composition. In that case,
10987 we set b->clip_changed to 1 to force updating the screen. If
10988 b->clip_changed has already been set to 1, we can skip this
10989 check. */
10990 if (!b->clip_changed
10991 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
10992 {
10993 int pt;
10994
10995 if (w == XWINDOW (selected_window))
10996 pt = BUF_PT (current_buffer);
10997 else
10998 pt = marker_position (w->pointm);
10999
11000 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
11001 || pt != XINT (w->last_point))
11002 && check_point_in_composition (w->current_matrix->buffer,
11003 XINT (w->last_point),
11004 XBUFFER (w->buffer), pt))
11005 b->clip_changed = 1;
11006 }
11007 }
11008 \f
11009
11010 /* Select FRAME to forward the values of frame-local variables into C
11011 variables so that the redisplay routines can access those values
11012 directly. */
11013
11014 static void
11015 select_frame_for_redisplay (frame)
11016 Lisp_Object frame;
11017 {
11018 Lisp_Object tail, sym, val;
11019 Lisp_Object old = selected_frame;
11020
11021 xassert (FRAMEP (frame) && FRAME_LIVE_P (XFRAME (frame)));
11022
11023 selected_frame = frame;
11024
11025 for (tail = XFRAME (frame)->param_alist; CONSP (tail); tail = XCDR (tail))
11026 if (CONSP (XCAR (tail))
11027 && (sym = XCAR (XCAR (tail)),
11028 SYMBOLP (sym))
11029 && (sym = indirect_variable (sym),
11030 val = SYMBOL_VALUE (sym),
11031 (BUFFER_LOCAL_VALUEP (val)))
11032 && XBUFFER_LOCAL_VALUE (val)->check_frame)
11033 /* Use find_symbol_value rather than Fsymbol_value
11034 to avoid an error if it is void. */
11035 find_symbol_value (sym);
11036
11037 for (tail = XFRAME (old)->param_alist; CONSP (tail); tail = XCDR (tail))
11038 if (CONSP (XCAR (tail))
11039 && (sym = XCAR (XCAR (tail)),
11040 SYMBOLP (sym))
11041 && (sym = indirect_variable (sym),
11042 val = SYMBOL_VALUE (sym),
11043 (BUFFER_LOCAL_VALUEP (val)))
11044 && XBUFFER_LOCAL_VALUE (val)->check_frame)
11045 find_symbol_value (sym);
11046 }
11047
11048
11049 #define STOP_POLLING \
11050 do { if (! polling_stopped_here) stop_polling (); \
11051 polling_stopped_here = 1; } while (0)
11052
11053 #define RESUME_POLLING \
11054 do { if (polling_stopped_here) start_polling (); \
11055 polling_stopped_here = 0; } while (0)
11056
11057
11058 /* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
11059 response to any user action; therefore, we should preserve the echo
11060 area. (Actually, our caller does that job.) Perhaps in the future
11061 avoid recentering windows if it is not necessary; currently that
11062 causes some problems. */
11063
11064 static void
11065 redisplay_internal (preserve_echo_area)
11066 int preserve_echo_area;
11067 {
11068 struct window *w = XWINDOW (selected_window);
11069 struct frame *f;
11070 int pause;
11071 int must_finish = 0;
11072 struct text_pos tlbufpos, tlendpos;
11073 int number_of_visible_frames;
11074 int count, count1;
11075 struct frame *sf;
11076 int polling_stopped_here = 0;
11077 Lisp_Object old_frame = selected_frame;
11078
11079 /* Non-zero means redisplay has to consider all windows on all
11080 frames. Zero means, only selected_window is considered. */
11081 int consider_all_windows_p;
11082
11083 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
11084
11085 /* No redisplay if running in batch mode or frame is not yet fully
11086 initialized, or redisplay is explicitly turned off by setting
11087 Vinhibit_redisplay. */
11088 if (noninteractive
11089 || !NILP (Vinhibit_redisplay))
11090 return;
11091
11092 /* Don't examine these until after testing Vinhibit_redisplay.
11093 When Emacs is shutting down, perhaps because its connection to
11094 X has dropped, we should not look at them at all. */
11095 f = XFRAME (w->frame);
11096 sf = SELECTED_FRAME ();
11097
11098 if (!f->glyphs_initialized_p)
11099 return;
11100
11101 /* The flag redisplay_performed_directly_p is set by
11102 direct_output_for_insert when it already did the whole screen
11103 update necessary. */
11104 if (redisplay_performed_directly_p)
11105 {
11106 redisplay_performed_directly_p = 0;
11107 if (!hscroll_windows (selected_window))
11108 return;
11109 }
11110
11111 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (MAC_OS)
11112 if (popup_activated ())
11113 return;
11114 #endif
11115
11116 /* I don't think this happens but let's be paranoid. */
11117 if (redisplaying_p)
11118 return;
11119
11120 /* Record a function that resets redisplaying_p to its old value
11121 when we leave this function. */
11122 count = SPECPDL_INDEX ();
11123 record_unwind_protect (unwind_redisplay,
11124 Fcons (make_number (redisplaying_p), selected_frame));
11125 ++redisplaying_p;
11126 specbind (Qinhibit_free_realized_faces, Qnil);
11127
11128 {
11129 Lisp_Object tail, frame;
11130
11131 FOR_EACH_FRAME (tail, frame)
11132 {
11133 struct frame *f = XFRAME (frame);
11134 f->already_hscrolled_p = 0;
11135 }
11136 }
11137
11138 retry:
11139 if (!EQ (old_frame, selected_frame)
11140 && FRAME_LIVE_P (XFRAME (old_frame)))
11141 /* When running redisplay, we play a bit fast-and-loose and allow e.g.
11142 selected_frame and selected_window to be temporarily out-of-sync so
11143 when we come back here via `goto retry', we need to resync because we
11144 may need to run Elisp code (via prepare_menu_bars). */
11145 select_frame_for_redisplay (old_frame);
11146
11147 pause = 0;
11148 reconsider_clip_changes (w, current_buffer);
11149 last_escape_glyph_frame = NULL;
11150 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
11151
11152 /* If new fonts have been loaded that make a glyph matrix adjustment
11153 necessary, do it. */
11154 if (fonts_changed_p)
11155 {
11156 adjust_glyphs (NULL);
11157 ++windows_or_buffers_changed;
11158 fonts_changed_p = 0;
11159 }
11160
11161 /* If face_change_count is non-zero, init_iterator will free all
11162 realized faces, which includes the faces referenced from current
11163 matrices. So, we can't reuse current matrices in this case. */
11164 if (face_change_count)
11165 ++windows_or_buffers_changed;
11166
11167 if (FRAME_TERMCAP_P (sf)
11168 && FRAME_TTY (sf)->previous_frame != sf)
11169 {
11170 /* Since frames on a single ASCII terminal share the same
11171 display area, displaying a different frame means redisplay
11172 the whole thing. */
11173 windows_or_buffers_changed++;
11174 SET_FRAME_GARBAGED (sf);
11175 FRAME_TTY (sf)->previous_frame = sf;
11176 }
11177
11178 /* Set the visible flags for all frames. Do this before checking
11179 for resized or garbaged frames; they want to know if their frames
11180 are visible. See the comment in frame.h for
11181 FRAME_SAMPLE_VISIBILITY. */
11182 {
11183 Lisp_Object tail, frame;
11184
11185 number_of_visible_frames = 0;
11186
11187 FOR_EACH_FRAME (tail, frame)
11188 {
11189 struct frame *f = XFRAME (frame);
11190
11191 FRAME_SAMPLE_VISIBILITY (f);
11192 if (FRAME_VISIBLE_P (f))
11193 ++number_of_visible_frames;
11194 clear_desired_matrices (f);
11195 }
11196 }
11197
11198
11199 /* Notice any pending interrupt request to change frame size. */
11200 do_pending_window_change (1);
11201
11202 /* Clear frames marked as garbaged. */
11203 if (frame_garbaged)
11204 clear_garbaged_frames ();
11205
11206 /* Build menubar and tool-bar items. */
11207 if (NILP (Vmemory_full))
11208 prepare_menu_bars ();
11209
11210 if (windows_or_buffers_changed)
11211 update_mode_lines++;
11212
11213 /* Detect case that we need to write or remove a star in the mode line. */
11214 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
11215 {
11216 w->update_mode_line = Qt;
11217 if (buffer_shared > 1)
11218 update_mode_lines++;
11219 }
11220
11221 /* Avoid invocation of point motion hooks by `current_column' below. */
11222 count1 = SPECPDL_INDEX ();
11223 specbind (Qinhibit_point_motion_hooks, Qt);
11224
11225 /* If %c is in the mode line, update it if needed. */
11226 if (!NILP (w->column_number_displayed)
11227 /* This alternative quickly identifies a common case
11228 where no change is needed. */
11229 && !(PT == XFASTINT (w->last_point)
11230 && XFASTINT (w->last_modified) >= MODIFF
11231 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
11232 && (XFASTINT (w->column_number_displayed)
11233 != (int) current_column ())) /* iftc */
11234 w->update_mode_line = Qt;
11235
11236 unbind_to (count1, Qnil);
11237
11238 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
11239
11240 /* The variable buffer_shared is set in redisplay_window and
11241 indicates that we redisplay a buffer in different windows. See
11242 there. */
11243 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
11244 || cursor_type_changed);
11245
11246 /* If specs for an arrow have changed, do thorough redisplay
11247 to ensure we remove any arrow that should no longer exist. */
11248 if (overlay_arrows_changed_p ())
11249 consider_all_windows_p = windows_or_buffers_changed = 1;
11250
11251 /* Normally the message* functions will have already displayed and
11252 updated the echo area, but the frame may have been trashed, or
11253 the update may have been preempted, so display the echo area
11254 again here. Checking message_cleared_p captures the case that
11255 the echo area should be cleared. */
11256 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
11257 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
11258 || (message_cleared_p
11259 && minibuf_level == 0
11260 /* If the mini-window is currently selected, this means the
11261 echo-area doesn't show through. */
11262 && !MINI_WINDOW_P (XWINDOW (selected_window))))
11263 {
11264 int window_height_changed_p = echo_area_display (0);
11265 must_finish = 1;
11266
11267 /* If we don't display the current message, don't clear the
11268 message_cleared_p flag, because, if we did, we wouldn't clear
11269 the echo area in the next redisplay which doesn't preserve
11270 the echo area. */
11271 if (!display_last_displayed_message_p)
11272 message_cleared_p = 0;
11273
11274 if (fonts_changed_p)
11275 goto retry;
11276 else if (window_height_changed_p)
11277 {
11278 consider_all_windows_p = 1;
11279 ++update_mode_lines;
11280 ++windows_or_buffers_changed;
11281
11282 /* If window configuration was changed, frames may have been
11283 marked garbaged. Clear them or we will experience
11284 surprises wrt scrolling. */
11285 if (frame_garbaged)
11286 clear_garbaged_frames ();
11287 }
11288 }
11289 else if (EQ (selected_window, minibuf_window)
11290 && (current_buffer->clip_changed
11291 || XFASTINT (w->last_modified) < MODIFF
11292 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
11293 && resize_mini_window (w, 0))
11294 {
11295 /* Resized active mini-window to fit the size of what it is
11296 showing if its contents might have changed. */
11297 must_finish = 1;
11298 consider_all_windows_p = 1;
11299 ++windows_or_buffers_changed;
11300 ++update_mode_lines;
11301
11302 /* If window configuration was changed, frames may have been
11303 marked garbaged. Clear them or we will experience
11304 surprises wrt scrolling. */
11305 if (frame_garbaged)
11306 clear_garbaged_frames ();
11307 }
11308
11309
11310 /* If showing the region, and mark has changed, we must redisplay
11311 the whole window. The assignment to this_line_start_pos prevents
11312 the optimization directly below this if-statement. */
11313 if (((!NILP (Vtransient_mark_mode)
11314 && !NILP (XBUFFER (w->buffer)->mark_active))
11315 != !NILP (w->region_showing))
11316 || (!NILP (w->region_showing)
11317 && !EQ (w->region_showing,
11318 Fmarker_position (XBUFFER (w->buffer)->mark))))
11319 CHARPOS (this_line_start_pos) = 0;
11320
11321 /* Optimize the case that only the line containing the cursor in the
11322 selected window has changed. Variables starting with this_ are
11323 set in display_line and record information about the line
11324 containing the cursor. */
11325 tlbufpos = this_line_start_pos;
11326 tlendpos = this_line_end_pos;
11327 if (!consider_all_windows_p
11328 && CHARPOS (tlbufpos) > 0
11329 && NILP (w->update_mode_line)
11330 && !current_buffer->clip_changed
11331 && !current_buffer->prevent_redisplay_optimizations_p
11332 && FRAME_VISIBLE_P (XFRAME (w->frame))
11333 && !FRAME_OBSCURED_P (XFRAME (w->frame))
11334 /* Make sure recorded data applies to current buffer, etc. */
11335 && this_line_buffer == current_buffer
11336 && current_buffer == XBUFFER (w->buffer)
11337 && NILP (w->force_start)
11338 && NILP (w->optional_new_start)
11339 /* Point must be on the line that we have info recorded about. */
11340 && PT >= CHARPOS (tlbufpos)
11341 && PT <= Z - CHARPOS (tlendpos)
11342 /* All text outside that line, including its final newline,
11343 must be unchanged */
11344 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
11345 CHARPOS (tlendpos)))
11346 {
11347 if (CHARPOS (tlbufpos) > BEGV
11348 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
11349 && (CHARPOS (tlbufpos) == ZV
11350 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
11351 /* Former continuation line has disappeared by becoming empty */
11352 goto cancel;
11353 else if (XFASTINT (w->last_modified) < MODIFF
11354 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
11355 || MINI_WINDOW_P (w))
11356 {
11357 /* We have to handle the case of continuation around a
11358 wide-column character (See the comment in indent.c around
11359 line 885).
11360
11361 For instance, in the following case:
11362
11363 -------- Insert --------
11364 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
11365 J_I_ ==> J_I_ `^^' are cursors.
11366 ^^ ^^
11367 -------- --------
11368
11369 As we have to redraw the line above, we should goto cancel. */
11370
11371 struct it it;
11372 int line_height_before = this_line_pixel_height;
11373
11374 /* Note that start_display will handle the case that the
11375 line starting at tlbufpos is a continuation lines. */
11376 start_display (&it, w, tlbufpos);
11377
11378 /* Implementation note: It this still necessary? */
11379 if (it.current_x != this_line_start_x)
11380 goto cancel;
11381
11382 TRACE ((stderr, "trying display optimization 1\n"));
11383 w->cursor.vpos = -1;
11384 overlay_arrow_seen = 0;
11385 it.vpos = this_line_vpos;
11386 it.current_y = this_line_y;
11387 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
11388 display_line (&it);
11389
11390 /* If line contains point, is not continued,
11391 and ends at same distance from eob as before, we win */
11392 if (w->cursor.vpos >= 0
11393 /* Line is not continued, otherwise this_line_start_pos
11394 would have been set to 0 in display_line. */
11395 && CHARPOS (this_line_start_pos)
11396 /* Line ends as before. */
11397 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
11398 /* Line has same height as before. Otherwise other lines
11399 would have to be shifted up or down. */
11400 && this_line_pixel_height == line_height_before)
11401 {
11402 /* If this is not the window's last line, we must adjust
11403 the charstarts of the lines below. */
11404 if (it.current_y < it.last_visible_y)
11405 {
11406 struct glyph_row *row
11407 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
11408 int delta, delta_bytes;
11409
11410 if (Z - CHARPOS (tlendpos) == ZV)
11411 {
11412 /* This line ends at end of (accessible part of)
11413 buffer. There is no newline to count. */
11414 delta = (Z
11415 - CHARPOS (tlendpos)
11416 - MATRIX_ROW_START_CHARPOS (row));
11417 delta_bytes = (Z_BYTE
11418 - BYTEPOS (tlendpos)
11419 - MATRIX_ROW_START_BYTEPOS (row));
11420 }
11421 else
11422 {
11423 /* This line ends in a newline. Must take
11424 account of the newline and the rest of the
11425 text that follows. */
11426 delta = (Z
11427 - CHARPOS (tlendpos)
11428 - MATRIX_ROW_START_CHARPOS (row));
11429 delta_bytes = (Z_BYTE
11430 - BYTEPOS (tlendpos)
11431 - MATRIX_ROW_START_BYTEPOS (row));
11432 }
11433
11434 increment_matrix_positions (w->current_matrix,
11435 this_line_vpos + 1,
11436 w->current_matrix->nrows,
11437 delta, delta_bytes);
11438 }
11439
11440 /* If this row displays text now but previously didn't,
11441 or vice versa, w->window_end_vpos may have to be
11442 adjusted. */
11443 if ((it.glyph_row - 1)->displays_text_p)
11444 {
11445 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
11446 XSETINT (w->window_end_vpos, this_line_vpos);
11447 }
11448 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
11449 && this_line_vpos > 0)
11450 XSETINT (w->window_end_vpos, this_line_vpos - 1);
11451 w->window_end_valid = Qnil;
11452
11453 /* Update hint: No need to try to scroll in update_window. */
11454 w->desired_matrix->no_scrolling_p = 1;
11455
11456 #if GLYPH_DEBUG
11457 *w->desired_matrix->method = 0;
11458 debug_method_add (w, "optimization 1");
11459 #endif
11460 #ifdef HAVE_WINDOW_SYSTEM
11461 update_window_fringes (w, 0);
11462 #endif
11463 goto update;
11464 }
11465 else
11466 goto cancel;
11467 }
11468 else if (/* Cursor position hasn't changed. */
11469 PT == XFASTINT (w->last_point)
11470 /* Make sure the cursor was last displayed
11471 in this window. Otherwise we have to reposition it. */
11472 && 0 <= w->cursor.vpos
11473 && WINDOW_TOTAL_LINES (w) > w->cursor.vpos)
11474 {
11475 if (!must_finish)
11476 {
11477 do_pending_window_change (1);
11478
11479 /* We used to always goto end_of_redisplay here, but this
11480 isn't enough if we have a blinking cursor. */
11481 if (w->cursor_off_p == w->last_cursor_off_p)
11482 goto end_of_redisplay;
11483 }
11484 goto update;
11485 }
11486 /* If highlighting the region, or if the cursor is in the echo area,
11487 then we can't just move the cursor. */
11488 else if (! (!NILP (Vtransient_mark_mode)
11489 && !NILP (current_buffer->mark_active))
11490 && (EQ (selected_window, current_buffer->last_selected_window)
11491 || highlight_nonselected_windows)
11492 && NILP (w->region_showing)
11493 && NILP (Vshow_trailing_whitespace)
11494 && !cursor_in_echo_area)
11495 {
11496 struct it it;
11497 struct glyph_row *row;
11498
11499 /* Skip from tlbufpos to PT and see where it is. Note that
11500 PT may be in invisible text. If so, we will end at the
11501 next visible position. */
11502 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
11503 NULL, DEFAULT_FACE_ID);
11504 it.current_x = this_line_start_x;
11505 it.current_y = this_line_y;
11506 it.vpos = this_line_vpos;
11507
11508 /* The call to move_it_to stops in front of PT, but
11509 moves over before-strings. */
11510 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
11511
11512 if (it.vpos == this_line_vpos
11513 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
11514 row->enabled_p))
11515 {
11516 xassert (this_line_vpos == it.vpos);
11517 xassert (this_line_y == it.current_y);
11518 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11519 #if GLYPH_DEBUG
11520 *w->desired_matrix->method = 0;
11521 debug_method_add (w, "optimization 3");
11522 #endif
11523 goto update;
11524 }
11525 else
11526 goto cancel;
11527 }
11528
11529 cancel:
11530 /* Text changed drastically or point moved off of line. */
11531 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
11532 }
11533
11534 CHARPOS (this_line_start_pos) = 0;
11535 consider_all_windows_p |= buffer_shared > 1;
11536 ++clear_face_cache_count;
11537 #ifdef HAVE_WINDOW_SYSTEM
11538 ++clear_image_cache_count;
11539 #endif
11540
11541 /* Build desired matrices, and update the display. If
11542 consider_all_windows_p is non-zero, do it for all windows on all
11543 frames. Otherwise do it for selected_window, only. */
11544
11545 if (consider_all_windows_p)
11546 {
11547 Lisp_Object tail, frame;
11548
11549 FOR_EACH_FRAME (tail, frame)
11550 XFRAME (frame)->updated_p = 0;
11551
11552 /* Recompute # windows showing selected buffer. This will be
11553 incremented each time such a window is displayed. */
11554 buffer_shared = 0;
11555
11556 FOR_EACH_FRAME (tail, frame)
11557 {
11558 struct frame *f = XFRAME (frame);
11559
11560 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
11561 {
11562 if (! EQ (frame, selected_frame))
11563 /* Select the frame, for the sake of frame-local
11564 variables. */
11565 select_frame_for_redisplay (frame);
11566
11567 /* Mark all the scroll bars to be removed; we'll redeem
11568 the ones we want when we redisplay their windows. */
11569 if (FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
11570 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
11571
11572 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
11573 redisplay_windows (FRAME_ROOT_WINDOW (f));
11574
11575 /* Any scroll bars which redisplay_windows should have
11576 nuked should now go away. */
11577 if (FRAME_TERMINAL (f)->judge_scroll_bars_hook)
11578 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
11579
11580 /* If fonts changed, display again. */
11581 /* ??? rms: I suspect it is a mistake to jump all the way
11582 back to retry here. It should just retry this frame. */
11583 if (fonts_changed_p)
11584 goto retry;
11585
11586 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
11587 {
11588 /* See if we have to hscroll. */
11589 if (!f->already_hscrolled_p)
11590 {
11591 f->already_hscrolled_p = 1;
11592 if (hscroll_windows (f->root_window))
11593 goto retry;
11594 }
11595
11596 /* Prevent various kinds of signals during display
11597 update. stdio is not robust about handling
11598 signals, which can cause an apparent I/O
11599 error. */
11600 if (interrupt_input)
11601 unrequest_sigio ();
11602 STOP_POLLING;
11603
11604 /* Update the display. */
11605 set_window_update_flags (XWINDOW (f->root_window), 1);
11606 pause |= update_frame (f, 0, 0);
11607 #if 0 /* Exiting the loop can leave the wrong value for buffer_shared. */
11608 if (pause)
11609 break;
11610 #endif
11611
11612 f->updated_p = 1;
11613 }
11614 }
11615 }
11616
11617 if (!pause)
11618 {
11619 /* Do the mark_window_display_accurate after all windows have
11620 been redisplayed because this call resets flags in buffers
11621 which are needed for proper redisplay. */
11622 FOR_EACH_FRAME (tail, frame)
11623 {
11624 struct frame *f = XFRAME (frame);
11625 if (f->updated_p)
11626 {
11627 mark_window_display_accurate (f->root_window, 1);
11628 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
11629 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
11630 }
11631 }
11632 }
11633 }
11634 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
11635 {
11636 Lisp_Object mini_window;
11637 struct frame *mini_frame;
11638
11639 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
11640 /* Use list_of_error, not Qerror, so that
11641 we catch only errors and don't run the debugger. */
11642 internal_condition_case_1 (redisplay_window_1, selected_window,
11643 list_of_error,
11644 redisplay_window_error);
11645
11646 /* Compare desired and current matrices, perform output. */
11647
11648 update:
11649 /* If fonts changed, display again. */
11650 if (fonts_changed_p)
11651 goto retry;
11652
11653 /* Prevent various kinds of signals during display update.
11654 stdio is not robust about handling signals,
11655 which can cause an apparent I/O error. */
11656 if (interrupt_input)
11657 unrequest_sigio ();
11658 STOP_POLLING;
11659
11660 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
11661 {
11662 if (hscroll_windows (selected_window))
11663 goto retry;
11664
11665 XWINDOW (selected_window)->must_be_updated_p = 1;
11666 pause = update_frame (sf, 0, 0);
11667 }
11668
11669 /* We may have called echo_area_display at the top of this
11670 function. If the echo area is on another frame, that may
11671 have put text on a frame other than the selected one, so the
11672 above call to update_frame would not have caught it. Catch
11673 it here. */
11674 mini_window = FRAME_MINIBUF_WINDOW (sf);
11675 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
11676
11677 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
11678 {
11679 XWINDOW (mini_window)->must_be_updated_p = 1;
11680 pause |= update_frame (mini_frame, 0, 0);
11681 if (!pause && hscroll_windows (mini_window))
11682 goto retry;
11683 }
11684 }
11685
11686 /* If display was paused because of pending input, make sure we do a
11687 thorough update the next time. */
11688 if (pause)
11689 {
11690 /* Prevent the optimization at the beginning of
11691 redisplay_internal that tries a single-line update of the
11692 line containing the cursor in the selected window. */
11693 CHARPOS (this_line_start_pos) = 0;
11694
11695 /* Let the overlay arrow be updated the next time. */
11696 update_overlay_arrows (0);
11697
11698 /* If we pause after scrolling, some rows in the current
11699 matrices of some windows are not valid. */
11700 if (!WINDOW_FULL_WIDTH_P (w)
11701 && !FRAME_WINDOW_P (XFRAME (w->frame)))
11702 update_mode_lines = 1;
11703 }
11704 else
11705 {
11706 if (!consider_all_windows_p)
11707 {
11708 /* This has already been done above if
11709 consider_all_windows_p is set. */
11710 mark_window_display_accurate_1 (w, 1);
11711
11712 /* Say overlay arrows are up to date. */
11713 update_overlay_arrows (1);
11714
11715 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
11716 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
11717 }
11718
11719 update_mode_lines = 0;
11720 windows_or_buffers_changed = 0;
11721 cursor_type_changed = 0;
11722 }
11723
11724 /* Start SIGIO interrupts coming again. Having them off during the
11725 code above makes it less likely one will discard output, but not
11726 impossible, since there might be stuff in the system buffer here.
11727 But it is much hairier to try to do anything about that. */
11728 if (interrupt_input)
11729 request_sigio ();
11730 RESUME_POLLING;
11731
11732 /* If a frame has become visible which was not before, redisplay
11733 again, so that we display it. Expose events for such a frame
11734 (which it gets when becoming visible) don't call the parts of
11735 redisplay constructing glyphs, so simply exposing a frame won't
11736 display anything in this case. So, we have to display these
11737 frames here explicitly. */
11738 if (!pause)
11739 {
11740 Lisp_Object tail, frame;
11741 int new_count = 0;
11742
11743 FOR_EACH_FRAME (tail, frame)
11744 {
11745 int this_is_visible = 0;
11746
11747 if (XFRAME (frame)->visible)
11748 this_is_visible = 1;
11749 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
11750 if (XFRAME (frame)->visible)
11751 this_is_visible = 1;
11752
11753 if (this_is_visible)
11754 new_count++;
11755 }
11756
11757 if (new_count != number_of_visible_frames)
11758 windows_or_buffers_changed++;
11759 }
11760
11761 /* Change frame size now if a change is pending. */
11762 do_pending_window_change (1);
11763
11764 /* If we just did a pending size change, or have additional
11765 visible frames, redisplay again. */
11766 if (windows_or_buffers_changed && !pause)
11767 goto retry;
11768
11769 /* Clear the face cache eventually. */
11770 if (consider_all_windows_p)
11771 {
11772 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
11773 {
11774 clear_face_cache (0);
11775 clear_face_cache_count = 0;
11776 }
11777 #ifdef HAVE_WINDOW_SYSTEM
11778 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
11779 {
11780 Lisp_Object tail, frame;
11781 FOR_EACH_FRAME (tail, frame)
11782 {
11783 struct frame *f = XFRAME (frame);
11784 if (FRAME_WINDOW_P (f))
11785 clear_image_cache (f, 0);
11786 }
11787 clear_image_cache_count = 0;
11788 }
11789 #endif /* HAVE_WINDOW_SYSTEM */
11790 }
11791
11792 end_of_redisplay:
11793 unbind_to (count, Qnil);
11794 RESUME_POLLING;
11795 }
11796
11797
11798 /* Redisplay, but leave alone any recent echo area message unless
11799 another message has been requested in its place.
11800
11801 This is useful in situations where you need to redisplay but no
11802 user action has occurred, making it inappropriate for the message
11803 area to be cleared. See tracking_off and
11804 wait_reading_process_output for examples of these situations.
11805
11806 FROM_WHERE is an integer saying from where this function was
11807 called. This is useful for debugging. */
11808
11809 void
11810 redisplay_preserve_echo_area (from_where)
11811 int from_where;
11812 {
11813 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
11814
11815 if (!NILP (echo_area_buffer[1]))
11816 {
11817 /* We have a previously displayed message, but no current
11818 message. Redisplay the previous message. */
11819 display_last_displayed_message_p = 1;
11820 redisplay_internal (1);
11821 display_last_displayed_message_p = 0;
11822 }
11823 else
11824 redisplay_internal (1);
11825
11826 if (FRAME_RIF (SELECTED_FRAME ()) != NULL
11827 && FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
11828 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (NULL);
11829 }
11830
11831
11832 /* Function registered with record_unwind_protect in
11833 redisplay_internal. Reset redisplaying_p to the value it had
11834 before redisplay_internal was called, and clear
11835 prevent_freeing_realized_faces_p. It also selects the previously
11836 selected frame, unless it has been deleted (by an X connection
11837 failure during redisplay, for example). */
11838
11839 static Lisp_Object
11840 unwind_redisplay (val)
11841 Lisp_Object val;
11842 {
11843 Lisp_Object old_redisplaying_p, old_frame;
11844
11845 old_redisplaying_p = XCAR (val);
11846 redisplaying_p = XFASTINT (old_redisplaying_p);
11847 old_frame = XCDR (val);
11848 if (! EQ (old_frame, selected_frame)
11849 && FRAME_LIVE_P (XFRAME (old_frame)))
11850 select_frame_for_redisplay (old_frame);
11851 return Qnil;
11852 }
11853
11854
11855 /* Mark the display of window W as accurate or inaccurate. If
11856 ACCURATE_P is non-zero mark display of W as accurate. If
11857 ACCURATE_P is zero, arrange for W to be redisplayed the next time
11858 redisplay_internal is called. */
11859
11860 static void
11861 mark_window_display_accurate_1 (w, accurate_p)
11862 struct window *w;
11863 int accurate_p;
11864 {
11865 if (BUFFERP (w->buffer))
11866 {
11867 struct buffer *b = XBUFFER (w->buffer);
11868
11869 w->last_modified
11870 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
11871 w->last_overlay_modified
11872 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
11873 w->last_had_star
11874 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
11875
11876 if (accurate_p)
11877 {
11878 b->clip_changed = 0;
11879 b->prevent_redisplay_optimizations_p = 0;
11880
11881 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
11882 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
11883 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
11884 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
11885
11886 w->current_matrix->buffer = b;
11887 w->current_matrix->begv = BUF_BEGV (b);
11888 w->current_matrix->zv = BUF_ZV (b);
11889
11890 w->last_cursor = w->cursor;
11891 w->last_cursor_off_p = w->cursor_off_p;
11892
11893 if (w == XWINDOW (selected_window))
11894 w->last_point = make_number (BUF_PT (b));
11895 else
11896 w->last_point = make_number (XMARKER (w->pointm)->charpos);
11897 }
11898 }
11899
11900 if (accurate_p)
11901 {
11902 w->window_end_valid = w->buffer;
11903 #if 0 /* This is incorrect with variable-height lines. */
11904 xassert (XINT (w->window_end_vpos)
11905 < (WINDOW_TOTAL_LINES (w)
11906 - (WINDOW_WANTS_MODELINE_P (w) ? 1 : 0)));
11907 #endif
11908 w->update_mode_line = Qnil;
11909 }
11910 }
11911
11912
11913 /* Mark the display of windows in the window tree rooted at WINDOW as
11914 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
11915 windows as accurate. If ACCURATE_P is zero, arrange for windows to
11916 be redisplayed the next time redisplay_internal is called. */
11917
11918 void
11919 mark_window_display_accurate (window, accurate_p)
11920 Lisp_Object window;
11921 int accurate_p;
11922 {
11923 struct window *w;
11924
11925 for (; !NILP (window); window = w->next)
11926 {
11927 w = XWINDOW (window);
11928 mark_window_display_accurate_1 (w, accurate_p);
11929
11930 if (!NILP (w->vchild))
11931 mark_window_display_accurate (w->vchild, accurate_p);
11932 if (!NILP (w->hchild))
11933 mark_window_display_accurate (w->hchild, accurate_p);
11934 }
11935
11936 if (accurate_p)
11937 {
11938 update_overlay_arrows (1);
11939 }
11940 else
11941 {
11942 /* Force a thorough redisplay the next time by setting
11943 last_arrow_position and last_arrow_string to t, which is
11944 unequal to any useful value of Voverlay_arrow_... */
11945 update_overlay_arrows (-1);
11946 }
11947 }
11948
11949
11950 /* Return value in display table DP (Lisp_Char_Table *) for character
11951 C. Since a display table doesn't have any parent, we don't have to
11952 follow parent. Do not call this function directly but use the
11953 macro DISP_CHAR_VECTOR. */
11954
11955 Lisp_Object
11956 disp_char_vector (dp, c)
11957 struct Lisp_Char_Table *dp;
11958 int c;
11959 {
11960 Lisp_Object val;
11961
11962 if (ASCII_CHAR_P (c))
11963 {
11964 val = dp->ascii;
11965 if (SUB_CHAR_TABLE_P (val))
11966 val = XSUB_CHAR_TABLE (val)->contents[c];
11967 }
11968 else
11969 {
11970 Lisp_Object table;
11971
11972 XSETCHAR_TABLE (table, dp);
11973 val = char_table_ref (table, c);
11974 }
11975 if (NILP (val))
11976 val = dp->defalt;
11977 return val;
11978 }
11979
11980
11981 \f
11982 /***********************************************************************
11983 Window Redisplay
11984 ***********************************************************************/
11985
11986 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
11987
11988 static void
11989 redisplay_windows (window)
11990 Lisp_Object window;
11991 {
11992 while (!NILP (window))
11993 {
11994 struct window *w = XWINDOW (window);
11995
11996 if (!NILP (w->hchild))
11997 redisplay_windows (w->hchild);
11998 else if (!NILP (w->vchild))
11999 redisplay_windows (w->vchild);
12000 else
12001 {
12002 displayed_buffer = XBUFFER (w->buffer);
12003 /* Use list_of_error, not Qerror, so that
12004 we catch only errors and don't run the debugger. */
12005 internal_condition_case_1 (redisplay_window_0, window,
12006 list_of_error,
12007 redisplay_window_error);
12008 }
12009
12010 window = w->next;
12011 }
12012 }
12013
12014 static Lisp_Object
12015 redisplay_window_error ()
12016 {
12017 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
12018 return Qnil;
12019 }
12020
12021 static Lisp_Object
12022 redisplay_window_0 (window)
12023 Lisp_Object window;
12024 {
12025 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
12026 redisplay_window (window, 0);
12027 return Qnil;
12028 }
12029
12030 static Lisp_Object
12031 redisplay_window_1 (window)
12032 Lisp_Object window;
12033 {
12034 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
12035 redisplay_window (window, 1);
12036 return Qnil;
12037 }
12038 \f
12039
12040 /* Increment GLYPH until it reaches END or CONDITION fails while
12041 adding (GLYPH)->pixel_width to X. */
12042
12043 #define SKIP_GLYPHS(glyph, end, x, condition) \
12044 do \
12045 { \
12046 (x) += (glyph)->pixel_width; \
12047 ++(glyph); \
12048 } \
12049 while ((glyph) < (end) && (condition))
12050
12051
12052 /* Set cursor position of W. PT is assumed to be displayed in ROW.
12053 DELTA is the number of bytes by which positions recorded in ROW
12054 differ from current buffer positions.
12055
12056 Return 0 if cursor is not on this row. 1 otherwise. */
12057
12058 int
12059 set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
12060 struct window *w;
12061 struct glyph_row *row;
12062 struct glyph_matrix *matrix;
12063 int delta, delta_bytes, dy, dvpos;
12064 {
12065 struct glyph *glyph = row->glyphs[TEXT_AREA];
12066 struct glyph *end = glyph + row->used[TEXT_AREA];
12067 struct glyph *cursor = NULL;
12068 /* The first glyph that starts a sequence of glyphs from string. */
12069 struct glyph *string_start;
12070 /* The X coordinate of string_start. */
12071 int string_start_x;
12072 /* The last known character position. */
12073 int last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
12074 /* The last known character position before string_start. */
12075 int string_before_pos;
12076 int x = row->x;
12077 int cursor_x = x;
12078 int cursor_from_overlay_pos = 0;
12079 int pt_old = PT - delta;
12080
12081 /* Skip over glyphs not having an object at the start of the row.
12082 These are special glyphs like truncation marks on terminal
12083 frames. */
12084 if (row->displays_text_p)
12085 while (glyph < end
12086 && INTEGERP (glyph->object)
12087 && glyph->charpos < 0)
12088 {
12089 x += glyph->pixel_width;
12090 ++glyph;
12091 }
12092
12093 string_start = NULL;
12094 while (glyph < end
12095 && !INTEGERP (glyph->object)
12096 && (!BUFFERP (glyph->object)
12097 || (last_pos = glyph->charpos) < pt_old))
12098 {
12099 if (! STRINGP (glyph->object))
12100 {
12101 string_start = NULL;
12102 x += glyph->pixel_width;
12103 ++glyph;
12104 if (cursor_from_overlay_pos
12105 && last_pos >= cursor_from_overlay_pos)
12106 {
12107 cursor_from_overlay_pos = 0;
12108 cursor = 0;
12109 }
12110 }
12111 else
12112 {
12113 if (string_start == NULL)
12114 {
12115 string_before_pos = last_pos;
12116 string_start = glyph;
12117 string_start_x = x;
12118 }
12119 /* Skip all glyphs from string. */
12120 do
12121 {
12122 Lisp_Object cprop;
12123 int pos;
12124 if ((cursor == NULL || glyph > cursor)
12125 && (cprop = Fget_char_property (make_number ((glyph)->charpos),
12126 Qcursor, (glyph)->object),
12127 !NILP (cprop))
12128 && (pos = string_buffer_position (w, glyph->object,
12129 string_before_pos),
12130 (pos == 0 /* From overlay */
12131 || pos == pt_old)))
12132 {
12133 /* Estimate overlay buffer position from the buffer
12134 positions of the glyphs before and after the overlay.
12135 Add 1 to last_pos so that if point corresponds to the
12136 glyph right after the overlay, we still use a 'cursor'
12137 property found in that overlay. */
12138 cursor_from_overlay_pos = (pos ? 0 : last_pos
12139 + (INTEGERP (cprop) ? XINT (cprop) : 0));
12140 cursor = glyph;
12141 cursor_x = x;
12142 }
12143 x += glyph->pixel_width;
12144 ++glyph;
12145 }
12146 while (glyph < end && EQ (glyph->object, string_start->object));
12147 }
12148 }
12149
12150 if (cursor != NULL)
12151 {
12152 glyph = cursor;
12153 x = cursor_x;
12154 }
12155 else if (row->ends_in_ellipsis_p && glyph == end)
12156 {
12157 /* Scan back over the ellipsis glyphs, decrementing positions. */
12158 while (glyph > row->glyphs[TEXT_AREA]
12159 && (glyph - 1)->charpos == last_pos)
12160 glyph--, x -= glyph->pixel_width;
12161 /* That loop always goes one position too far,
12162 including the glyph before the ellipsis.
12163 So scan forward over that one. */
12164 x += glyph->pixel_width;
12165 glyph++;
12166 }
12167 else if (string_start
12168 && (glyph == end || !BUFFERP (glyph->object) || last_pos > pt_old))
12169 {
12170 /* We may have skipped over point because the previous glyphs
12171 are from string. As there's no easy way to know the
12172 character position of the current glyph, find the correct
12173 glyph on point by scanning from string_start again. */
12174 Lisp_Object limit;
12175 Lisp_Object string;
12176 struct glyph *stop = glyph;
12177 int pos;
12178
12179 limit = make_number (pt_old + 1);
12180 glyph = string_start;
12181 x = string_start_x;
12182 string = glyph->object;
12183 pos = string_buffer_position (w, string, string_before_pos);
12184 /* If STRING is from overlay, LAST_POS == 0. We skip such glyphs
12185 because we always put cursor after overlay strings. */
12186 while (pos == 0 && glyph < stop)
12187 {
12188 string = glyph->object;
12189 SKIP_GLYPHS (glyph, stop, x, EQ (glyph->object, string));
12190 if (glyph < stop)
12191 pos = string_buffer_position (w, glyph->object, string_before_pos);
12192 }
12193
12194 while (glyph < stop)
12195 {
12196 pos = XINT (Fnext_single_char_property_change
12197 (make_number (pos), Qdisplay, Qnil, limit));
12198 if (pos > pt_old)
12199 break;
12200 /* Skip glyphs from the same string. */
12201 string = glyph->object;
12202 SKIP_GLYPHS (glyph, stop, x, EQ (glyph->object, string));
12203 /* Skip glyphs from an overlay. */
12204 while (glyph < stop
12205 && ! string_buffer_position (w, glyph->object, pos))
12206 {
12207 string = glyph->object;
12208 SKIP_GLYPHS (glyph, stop, x, EQ (glyph->object, string));
12209 }
12210 }
12211
12212 /* If we reached the end of the line, and end was from a string,
12213 cursor is not on this line. */
12214 if (glyph == end && row->continued_p)
12215 return 0;
12216 }
12217
12218 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
12219 w->cursor.x = x;
12220 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
12221 w->cursor.y = row->y + dy;
12222
12223 if (w == XWINDOW (selected_window))
12224 {
12225 if (!row->continued_p
12226 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
12227 && row->x == 0)
12228 {
12229 this_line_buffer = XBUFFER (w->buffer);
12230
12231 CHARPOS (this_line_start_pos)
12232 = MATRIX_ROW_START_CHARPOS (row) + delta;
12233 BYTEPOS (this_line_start_pos)
12234 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
12235
12236 CHARPOS (this_line_end_pos)
12237 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
12238 BYTEPOS (this_line_end_pos)
12239 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
12240
12241 this_line_y = w->cursor.y;
12242 this_line_pixel_height = row->height;
12243 this_line_vpos = w->cursor.vpos;
12244 this_line_start_x = row->x;
12245 }
12246 else
12247 CHARPOS (this_line_start_pos) = 0;
12248 }
12249
12250 return 1;
12251 }
12252
12253
12254 /* Run window scroll functions, if any, for WINDOW with new window
12255 start STARTP. Sets the window start of WINDOW to that position.
12256
12257 We assume that the window's buffer is really current. */
12258
12259 static INLINE struct text_pos
12260 run_window_scroll_functions (window, startp)
12261 Lisp_Object window;
12262 struct text_pos startp;
12263 {
12264 struct window *w = XWINDOW (window);
12265 SET_MARKER_FROM_TEXT_POS (w->start, startp);
12266
12267 if (current_buffer != XBUFFER (w->buffer))
12268 abort ();
12269
12270 if (!NILP (Vwindow_scroll_functions))
12271 {
12272 run_hook_with_args_2 (Qwindow_scroll_functions, window,
12273 make_number (CHARPOS (startp)));
12274 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12275 /* In case the hook functions switch buffers. */
12276 if (current_buffer != XBUFFER (w->buffer))
12277 set_buffer_internal_1 (XBUFFER (w->buffer));
12278 }
12279
12280 return startp;
12281 }
12282
12283
12284 /* Make sure the line containing the cursor is fully visible.
12285 A value of 1 means there is nothing to be done.
12286 (Either the line is fully visible, or it cannot be made so,
12287 or we cannot tell.)
12288
12289 If FORCE_P is non-zero, return 0 even if partial visible cursor row
12290 is higher than window.
12291
12292 A value of 0 means the caller should do scrolling
12293 as if point had gone off the screen. */
12294
12295 static int
12296 cursor_row_fully_visible_p (w, force_p, current_matrix_p)
12297 struct window *w;
12298 int force_p;
12299 int current_matrix_p;
12300 {
12301 struct glyph_matrix *matrix;
12302 struct glyph_row *row;
12303 int window_height;
12304
12305 if (!make_cursor_line_fully_visible_p)
12306 return 1;
12307
12308 /* It's not always possible to find the cursor, e.g, when a window
12309 is full of overlay strings. Don't do anything in that case. */
12310 if (w->cursor.vpos < 0)
12311 return 1;
12312
12313 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
12314 row = MATRIX_ROW (matrix, w->cursor.vpos);
12315
12316 /* If the cursor row is not partially visible, there's nothing to do. */
12317 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
12318 return 1;
12319
12320 /* If the row the cursor is in is taller than the window's height,
12321 it's not clear what to do, so do nothing. */
12322 window_height = window_box_height (w);
12323 if (row->height >= window_height)
12324 {
12325 if (!force_p || MINI_WINDOW_P (w)
12326 || w->vscroll || w->cursor.vpos == 0)
12327 return 1;
12328 }
12329 return 0;
12330
12331 #if 0
12332 /* This code used to try to scroll the window just enough to make
12333 the line visible. It returned 0 to say that the caller should
12334 allocate larger glyph matrices. */
12335
12336 if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (w, row))
12337 {
12338 int dy = row->height - row->visible_height;
12339 w->vscroll = 0;
12340 w->cursor.y += dy;
12341 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
12342 }
12343 else /* MATRIX_ROW_PARTIALLY_VISIBLE_AT_BOTTOM_P (w, row)) */
12344 {
12345 int dy = - (row->height - row->visible_height);
12346 w->vscroll = dy;
12347 w->cursor.y += dy;
12348 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
12349 }
12350
12351 /* When we change the cursor y-position of the selected window,
12352 change this_line_y as well so that the display optimization for
12353 the cursor line of the selected window in redisplay_internal uses
12354 the correct y-position. */
12355 if (w == XWINDOW (selected_window))
12356 this_line_y = w->cursor.y;
12357
12358 /* If vscrolling requires a larger glyph matrix, arrange for a fresh
12359 redisplay with larger matrices. */
12360 if (matrix->nrows < required_matrix_height (w))
12361 {
12362 fonts_changed_p = 1;
12363 return 0;
12364 }
12365
12366 return 1;
12367 #endif /* 0 */
12368 }
12369
12370
12371 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
12372 non-zero means only WINDOW is redisplayed in redisplay_internal.
12373 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
12374 in redisplay_window to bring a partially visible line into view in
12375 the case that only the cursor has moved.
12376
12377 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
12378 last screen line's vertical height extends past the end of the screen.
12379
12380 Value is
12381
12382 1 if scrolling succeeded
12383
12384 0 if scrolling didn't find point.
12385
12386 -1 if new fonts have been loaded so that we must interrupt
12387 redisplay, adjust glyph matrices, and try again. */
12388
12389 enum
12390 {
12391 SCROLLING_SUCCESS,
12392 SCROLLING_FAILED,
12393 SCROLLING_NEED_LARGER_MATRICES
12394 };
12395
12396 static int
12397 try_scrolling (window, just_this_one_p, scroll_conservatively,
12398 scroll_step, temp_scroll_step, last_line_misfit)
12399 Lisp_Object window;
12400 int just_this_one_p;
12401 EMACS_INT scroll_conservatively, scroll_step;
12402 int temp_scroll_step;
12403 int last_line_misfit;
12404 {
12405 struct window *w = XWINDOW (window);
12406 struct frame *f = XFRAME (w->frame);
12407 struct text_pos scroll_margin_pos;
12408 struct text_pos pos;
12409 struct text_pos startp;
12410 struct it it;
12411 Lisp_Object window_end;
12412 int this_scroll_margin;
12413 int dy = 0;
12414 int scroll_max;
12415 int rc;
12416 int amount_to_scroll = 0;
12417 Lisp_Object aggressive;
12418 int height;
12419 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
12420
12421 #if GLYPH_DEBUG
12422 debug_method_add (w, "try_scrolling");
12423 #endif
12424
12425 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12426
12427 /* Compute scroll margin height in pixels. We scroll when point is
12428 within this distance from the top or bottom of the window. */
12429 if (scroll_margin > 0)
12430 {
12431 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
12432 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
12433 }
12434 else
12435 this_scroll_margin = 0;
12436
12437 /* Force scroll_conservatively to have a reasonable value so it doesn't
12438 cause an overflow while computing how much to scroll. */
12439 if (scroll_conservatively)
12440 scroll_conservatively = min (scroll_conservatively,
12441 MOST_POSITIVE_FIXNUM / FRAME_LINE_HEIGHT (f));
12442
12443 /* Compute how much we should try to scroll maximally to bring point
12444 into view. */
12445 if (scroll_step || scroll_conservatively || temp_scroll_step)
12446 scroll_max = max (scroll_step,
12447 max (scroll_conservatively, temp_scroll_step));
12448 else if (NUMBERP (current_buffer->scroll_down_aggressively)
12449 || NUMBERP (current_buffer->scroll_up_aggressively))
12450 /* We're trying to scroll because of aggressive scrolling
12451 but no scroll_step is set. Choose an arbitrary one. Maybe
12452 there should be a variable for this. */
12453 scroll_max = 10;
12454 else
12455 scroll_max = 0;
12456 scroll_max *= FRAME_LINE_HEIGHT (f);
12457
12458 /* Decide whether we have to scroll down. Start at the window end
12459 and move this_scroll_margin up to find the position of the scroll
12460 margin. */
12461 window_end = Fwindow_end (window, Qt);
12462
12463 too_near_end:
12464
12465 CHARPOS (scroll_margin_pos) = XINT (window_end);
12466 BYTEPOS (scroll_margin_pos) = CHAR_TO_BYTE (CHARPOS (scroll_margin_pos));
12467
12468 if (this_scroll_margin || extra_scroll_margin_lines)
12469 {
12470 start_display (&it, w, scroll_margin_pos);
12471 if (this_scroll_margin)
12472 move_it_vertically_backward (&it, this_scroll_margin);
12473 if (extra_scroll_margin_lines)
12474 move_it_by_lines (&it, - extra_scroll_margin_lines, 0);
12475 scroll_margin_pos = it.current.pos;
12476 }
12477
12478 if (PT >= CHARPOS (scroll_margin_pos))
12479 {
12480 int y0;
12481
12482 /* Point is in the scroll margin at the bottom of the window, or
12483 below. Compute a new window start that makes point visible. */
12484
12485 /* Compute the distance from the scroll margin to PT.
12486 Give up if the distance is greater than scroll_max. */
12487 start_display (&it, w, scroll_margin_pos);
12488 y0 = it.current_y;
12489 move_it_to (&it, PT, 0, it.last_visible_y, -1,
12490 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
12491
12492 /* To make point visible, we have to move the window start
12493 down so that the line the cursor is in is visible, which
12494 means we have to add in the height of the cursor line. */
12495 dy = line_bottom_y (&it) - y0;
12496
12497 if (dy > scroll_max)
12498 return SCROLLING_FAILED;
12499
12500 /* Move the window start down. If scrolling conservatively,
12501 move it just enough down to make point visible. If
12502 scroll_step is set, move it down by scroll_step. */
12503 start_display (&it, w, startp);
12504
12505 if (scroll_conservatively)
12506 /* Set AMOUNT_TO_SCROLL to at least one line,
12507 and at most scroll_conservatively lines. */
12508 amount_to_scroll
12509 = min (max (dy, FRAME_LINE_HEIGHT (f)),
12510 FRAME_LINE_HEIGHT (f) * scroll_conservatively);
12511 else if (scroll_step || temp_scroll_step)
12512 amount_to_scroll = scroll_max;
12513 else
12514 {
12515 aggressive = current_buffer->scroll_up_aggressively;
12516 height = WINDOW_BOX_TEXT_HEIGHT (w);
12517 if (NUMBERP (aggressive))
12518 {
12519 double float_amount = XFLOATINT (aggressive) * height;
12520 amount_to_scroll = float_amount;
12521 if (amount_to_scroll == 0 && float_amount > 0)
12522 amount_to_scroll = 1;
12523 }
12524 }
12525
12526 if (amount_to_scroll <= 0)
12527 return SCROLLING_FAILED;
12528
12529 /* If moving by amount_to_scroll leaves STARTP unchanged,
12530 move it down one screen line. */
12531
12532 move_it_vertically (&it, amount_to_scroll);
12533 if (CHARPOS (it.current.pos) == CHARPOS (startp))
12534 move_it_by_lines (&it, 1, 1);
12535 startp = it.current.pos;
12536 }
12537 else
12538 {
12539 /* See if point is inside the scroll margin at the top of the
12540 window. */
12541 scroll_margin_pos = startp;
12542 if (this_scroll_margin)
12543 {
12544 start_display (&it, w, startp);
12545 move_it_vertically (&it, this_scroll_margin);
12546 scroll_margin_pos = it.current.pos;
12547 }
12548
12549 if (PT < CHARPOS (scroll_margin_pos))
12550 {
12551 /* Point is in the scroll margin at the top of the window or
12552 above what is displayed in the window. */
12553 int y0;
12554
12555 /* Compute the vertical distance from PT to the scroll
12556 margin position. Give up if distance is greater than
12557 scroll_max. */
12558 SET_TEXT_POS (pos, PT, PT_BYTE);
12559 start_display (&it, w, pos);
12560 y0 = it.current_y;
12561 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
12562 it.last_visible_y, -1,
12563 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
12564 dy = it.current_y - y0;
12565 if (dy > scroll_max)
12566 return SCROLLING_FAILED;
12567
12568 /* Compute new window start. */
12569 start_display (&it, w, startp);
12570
12571 if (scroll_conservatively)
12572 amount_to_scroll
12573 = max (dy, FRAME_LINE_HEIGHT (f) * max (scroll_step, temp_scroll_step));
12574 else if (scroll_step || temp_scroll_step)
12575 amount_to_scroll = scroll_max;
12576 else
12577 {
12578 aggressive = current_buffer->scroll_down_aggressively;
12579 height = WINDOW_BOX_TEXT_HEIGHT (w);
12580 if (NUMBERP (aggressive))
12581 {
12582 double float_amount = XFLOATINT (aggressive) * height;
12583 amount_to_scroll = float_amount;
12584 if (amount_to_scroll == 0 && float_amount > 0)
12585 amount_to_scroll = 1;
12586 }
12587 }
12588
12589 if (amount_to_scroll <= 0)
12590 return SCROLLING_FAILED;
12591
12592 move_it_vertically_backward (&it, amount_to_scroll);
12593 startp = it.current.pos;
12594 }
12595 }
12596
12597 /* Run window scroll functions. */
12598 startp = run_window_scroll_functions (window, startp);
12599
12600 /* Display the window. Give up if new fonts are loaded, or if point
12601 doesn't appear. */
12602 if (!try_window (window, startp, 0))
12603 rc = SCROLLING_NEED_LARGER_MATRICES;
12604 else if (w->cursor.vpos < 0)
12605 {
12606 clear_glyph_matrix (w->desired_matrix);
12607 rc = SCROLLING_FAILED;
12608 }
12609 else
12610 {
12611 /* Maybe forget recorded base line for line number display. */
12612 if (!just_this_one_p
12613 || current_buffer->clip_changed
12614 || BEG_UNCHANGED < CHARPOS (startp))
12615 w->base_line_number = Qnil;
12616
12617 /* If cursor ends up on a partially visible line,
12618 treat that as being off the bottom of the screen. */
12619 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0))
12620 {
12621 clear_glyph_matrix (w->desired_matrix);
12622 ++extra_scroll_margin_lines;
12623 goto too_near_end;
12624 }
12625 rc = SCROLLING_SUCCESS;
12626 }
12627
12628 return rc;
12629 }
12630
12631
12632 /* Compute a suitable window start for window W if display of W starts
12633 on a continuation line. Value is non-zero if a new window start
12634 was computed.
12635
12636 The new window start will be computed, based on W's width, starting
12637 from the start of the continued line. It is the start of the
12638 screen line with the minimum distance from the old start W->start. */
12639
12640 static int
12641 compute_window_start_on_continuation_line (w)
12642 struct window *w;
12643 {
12644 struct text_pos pos, start_pos;
12645 int window_start_changed_p = 0;
12646
12647 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
12648
12649 /* If window start is on a continuation line... Window start may be
12650 < BEGV in case there's invisible text at the start of the
12651 buffer (M-x rmail, for example). */
12652 if (CHARPOS (start_pos) > BEGV
12653 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
12654 {
12655 struct it it;
12656 struct glyph_row *row;
12657
12658 /* Handle the case that the window start is out of range. */
12659 if (CHARPOS (start_pos) < BEGV)
12660 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
12661 else if (CHARPOS (start_pos) > ZV)
12662 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
12663
12664 /* Find the start of the continued line. This should be fast
12665 because scan_buffer is fast (newline cache). */
12666 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
12667 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
12668 row, DEFAULT_FACE_ID);
12669 reseat_at_previous_visible_line_start (&it);
12670
12671 /* If the line start is "too far" away from the window start,
12672 say it takes too much time to compute a new window start. */
12673 if (CHARPOS (start_pos) - IT_CHARPOS (it)
12674 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
12675 {
12676 int min_distance, distance;
12677
12678 /* Move forward by display lines to find the new window
12679 start. If window width was enlarged, the new start can
12680 be expected to be > the old start. If window width was
12681 decreased, the new window start will be < the old start.
12682 So, we're looking for the display line start with the
12683 minimum distance from the old window start. */
12684 pos = it.current.pos;
12685 min_distance = INFINITY;
12686 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
12687 distance < min_distance)
12688 {
12689 min_distance = distance;
12690 pos = it.current.pos;
12691 move_it_by_lines (&it, 1, 0);
12692 }
12693
12694 /* Set the window start there. */
12695 SET_MARKER_FROM_TEXT_POS (w->start, pos);
12696 window_start_changed_p = 1;
12697 }
12698 }
12699
12700 return window_start_changed_p;
12701 }
12702
12703
12704 /* Try cursor movement in case text has not changed in window WINDOW,
12705 with window start STARTP. Value is
12706
12707 CURSOR_MOVEMENT_SUCCESS if successful
12708
12709 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
12710
12711 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
12712 display. *SCROLL_STEP is set to 1, under certain circumstances, if
12713 we want to scroll as if scroll-step were set to 1. See the code.
12714
12715 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
12716 which case we have to abort this redisplay, and adjust matrices
12717 first. */
12718
12719 enum
12720 {
12721 CURSOR_MOVEMENT_SUCCESS,
12722 CURSOR_MOVEMENT_CANNOT_BE_USED,
12723 CURSOR_MOVEMENT_MUST_SCROLL,
12724 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
12725 };
12726
12727 static int
12728 try_cursor_movement (window, startp, scroll_step)
12729 Lisp_Object window;
12730 struct text_pos startp;
12731 int *scroll_step;
12732 {
12733 struct window *w = XWINDOW (window);
12734 struct frame *f = XFRAME (w->frame);
12735 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
12736
12737 #if GLYPH_DEBUG
12738 if (inhibit_try_cursor_movement)
12739 return rc;
12740 #endif
12741
12742 /* Handle case where text has not changed, only point, and it has
12743 not moved off the frame. */
12744 if (/* Point may be in this window. */
12745 PT >= CHARPOS (startp)
12746 /* Selective display hasn't changed. */
12747 && !current_buffer->clip_changed
12748 /* Function force-mode-line-update is used to force a thorough
12749 redisplay. It sets either windows_or_buffers_changed or
12750 update_mode_lines. So don't take a shortcut here for these
12751 cases. */
12752 && !update_mode_lines
12753 && !windows_or_buffers_changed
12754 && !cursor_type_changed
12755 /* Can't use this case if highlighting a region. When a
12756 region exists, cursor movement has to do more than just
12757 set the cursor. */
12758 && !(!NILP (Vtransient_mark_mode)
12759 && !NILP (current_buffer->mark_active))
12760 && NILP (w->region_showing)
12761 && NILP (Vshow_trailing_whitespace)
12762 /* Right after splitting windows, last_point may be nil. */
12763 && INTEGERP (w->last_point)
12764 /* This code is not used for mini-buffer for the sake of the case
12765 of redisplaying to replace an echo area message; since in
12766 that case the mini-buffer contents per se are usually
12767 unchanged. This code is of no real use in the mini-buffer
12768 since the handling of this_line_start_pos, etc., in redisplay
12769 handles the same cases. */
12770 && !EQ (window, minibuf_window)
12771 /* When splitting windows or for new windows, it happens that
12772 redisplay is called with a nil window_end_vpos or one being
12773 larger than the window. This should really be fixed in
12774 window.c. I don't have this on my list, now, so we do
12775 approximately the same as the old redisplay code. --gerd. */
12776 && INTEGERP (w->window_end_vpos)
12777 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
12778 && (FRAME_WINDOW_P (f)
12779 || !overlay_arrow_in_current_buffer_p ()))
12780 {
12781 int this_scroll_margin, top_scroll_margin;
12782 struct glyph_row *row = NULL;
12783
12784 #if GLYPH_DEBUG
12785 debug_method_add (w, "cursor movement");
12786 #endif
12787
12788 /* Scroll if point within this distance from the top or bottom
12789 of the window. This is a pixel value. */
12790 this_scroll_margin = max (0, scroll_margin);
12791 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
12792 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
12793
12794 top_scroll_margin = this_scroll_margin;
12795 if (WINDOW_WANTS_HEADER_LINE_P (w))
12796 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
12797
12798 /* Start with the row the cursor was displayed during the last
12799 not paused redisplay. Give up if that row is not valid. */
12800 if (w->last_cursor.vpos < 0
12801 || w->last_cursor.vpos >= w->current_matrix->nrows)
12802 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12803 else
12804 {
12805 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
12806 if (row->mode_line_p)
12807 ++row;
12808 if (!row->enabled_p)
12809 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12810 }
12811
12812 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
12813 {
12814 int scroll_p = 0;
12815 int last_y = window_text_bottom_y (w) - this_scroll_margin;
12816
12817 if (PT > XFASTINT (w->last_point))
12818 {
12819 /* Point has moved forward. */
12820 while (MATRIX_ROW_END_CHARPOS (row) < PT
12821 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
12822 {
12823 xassert (row->enabled_p);
12824 ++row;
12825 }
12826
12827 /* The end position of a row equals the start position
12828 of the next row. If PT is there, we would rather
12829 display it in the next line. */
12830 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
12831 && MATRIX_ROW_END_CHARPOS (row) == PT
12832 && !cursor_row_p (w, row))
12833 ++row;
12834
12835 /* If within the scroll margin, scroll. Note that
12836 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
12837 the next line would be drawn, and that
12838 this_scroll_margin can be zero. */
12839 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
12840 || PT > MATRIX_ROW_END_CHARPOS (row)
12841 /* Line is completely visible last line in window
12842 and PT is to be set in the next line. */
12843 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
12844 && PT == MATRIX_ROW_END_CHARPOS (row)
12845 && !row->ends_at_zv_p
12846 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
12847 scroll_p = 1;
12848 }
12849 else if (PT < XFASTINT (w->last_point))
12850 {
12851 /* Cursor has to be moved backward. Note that PT >=
12852 CHARPOS (startp) because of the outer if-statement. */
12853 while (!row->mode_line_p
12854 && (MATRIX_ROW_START_CHARPOS (row) > PT
12855 || (MATRIX_ROW_START_CHARPOS (row) == PT
12856 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
12857 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
12858 row > w->current_matrix->rows
12859 && (row-1)->ends_in_newline_from_string_p))))
12860 && (row->y > top_scroll_margin
12861 || CHARPOS (startp) == BEGV))
12862 {
12863 xassert (row->enabled_p);
12864 --row;
12865 }
12866
12867 /* Consider the following case: Window starts at BEGV,
12868 there is invisible, intangible text at BEGV, so that
12869 display starts at some point START > BEGV. It can
12870 happen that we are called with PT somewhere between
12871 BEGV and START. Try to handle that case. */
12872 if (row < w->current_matrix->rows
12873 || row->mode_line_p)
12874 {
12875 row = w->current_matrix->rows;
12876 if (row->mode_line_p)
12877 ++row;
12878 }
12879
12880 /* Due to newlines in overlay strings, we may have to
12881 skip forward over overlay strings. */
12882 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
12883 && MATRIX_ROW_END_CHARPOS (row) == PT
12884 && !cursor_row_p (w, row))
12885 ++row;
12886
12887 /* If within the scroll margin, scroll. */
12888 if (row->y < top_scroll_margin
12889 && CHARPOS (startp) != BEGV)
12890 scroll_p = 1;
12891 }
12892 else
12893 {
12894 /* Cursor did not move. So don't scroll even if cursor line
12895 is partially visible, as it was so before. */
12896 rc = CURSOR_MOVEMENT_SUCCESS;
12897 }
12898
12899 if (PT < MATRIX_ROW_START_CHARPOS (row)
12900 || PT > MATRIX_ROW_END_CHARPOS (row))
12901 {
12902 /* if PT is not in the glyph row, give up. */
12903 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12904 }
12905 else if (rc != CURSOR_MOVEMENT_SUCCESS
12906 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
12907 && make_cursor_line_fully_visible_p)
12908 {
12909 if (PT == MATRIX_ROW_END_CHARPOS (row)
12910 && !row->ends_at_zv_p
12911 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
12912 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12913 else if (row->height > window_box_height (w))
12914 {
12915 /* If we end up in a partially visible line, let's
12916 make it fully visible, except when it's taller
12917 than the window, in which case we can't do much
12918 about it. */
12919 *scroll_step = 1;
12920 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12921 }
12922 else
12923 {
12924 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
12925 if (!cursor_row_fully_visible_p (w, 0, 1))
12926 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12927 else
12928 rc = CURSOR_MOVEMENT_SUCCESS;
12929 }
12930 }
12931 else if (scroll_p)
12932 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12933 else
12934 {
12935 do
12936 {
12937 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
12938 {
12939 rc = CURSOR_MOVEMENT_SUCCESS;
12940 break;
12941 }
12942 ++row;
12943 }
12944 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
12945 && MATRIX_ROW_START_CHARPOS (row) == PT
12946 && cursor_row_p (w, row));
12947 }
12948 }
12949 }
12950
12951 return rc;
12952 }
12953
12954 void
12955 set_vertical_scroll_bar (w)
12956 struct window *w;
12957 {
12958 int start, end, whole;
12959
12960 /* Calculate the start and end positions for the current window.
12961 At some point, it would be nice to choose between scrollbars
12962 which reflect the whole buffer size, with special markers
12963 indicating narrowing, and scrollbars which reflect only the
12964 visible region.
12965
12966 Note that mini-buffers sometimes aren't displaying any text. */
12967 if (!MINI_WINDOW_P (w)
12968 || (w == XWINDOW (minibuf_window)
12969 && NILP (echo_area_buffer[0])))
12970 {
12971 struct buffer *buf = XBUFFER (w->buffer);
12972 whole = BUF_ZV (buf) - BUF_BEGV (buf);
12973 start = marker_position (w->start) - BUF_BEGV (buf);
12974 /* I don't think this is guaranteed to be right. For the
12975 moment, we'll pretend it is. */
12976 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
12977
12978 if (end < start)
12979 end = start;
12980 if (whole < (end - start))
12981 whole = end - start;
12982 }
12983 else
12984 start = end = whole = 0;
12985
12986 /* Indicate what this scroll bar ought to be displaying now. */
12987 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
12988 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
12989 (w, end - start, whole, start);
12990 }
12991
12992
12993 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
12994 selected_window is redisplayed.
12995
12996 We can return without actually redisplaying the window if
12997 fonts_changed_p is nonzero. In that case, redisplay_internal will
12998 retry. */
12999
13000 static void
13001 redisplay_window (window, just_this_one_p)
13002 Lisp_Object window;
13003 int just_this_one_p;
13004 {
13005 struct window *w = XWINDOW (window);
13006 struct frame *f = XFRAME (w->frame);
13007 struct buffer *buffer = XBUFFER (w->buffer);
13008 struct buffer *old = current_buffer;
13009 struct text_pos lpoint, opoint, startp;
13010 int update_mode_line;
13011 int tem;
13012 struct it it;
13013 /* Record it now because it's overwritten. */
13014 int current_matrix_up_to_date_p = 0;
13015 int used_current_matrix_p = 0;
13016 /* This is less strict than current_matrix_up_to_date_p.
13017 It indictes that the buffer contents and narrowing are unchanged. */
13018 int buffer_unchanged_p = 0;
13019 int temp_scroll_step = 0;
13020 int count = SPECPDL_INDEX ();
13021 int rc;
13022 int centering_position = -1;
13023 int last_line_misfit = 0;
13024 int beg_unchanged, end_unchanged;
13025
13026 SET_TEXT_POS (lpoint, PT, PT_BYTE);
13027 opoint = lpoint;
13028
13029 /* W must be a leaf window here. */
13030 xassert (!NILP (w->buffer));
13031 #if GLYPH_DEBUG
13032 *w->desired_matrix->method = 0;
13033 #endif
13034
13035 specbind (Qinhibit_point_motion_hooks, Qt);
13036
13037 reconsider_clip_changes (w, buffer);
13038
13039 /* Has the mode line to be updated? */
13040 update_mode_line = (!NILP (w->update_mode_line)
13041 || update_mode_lines
13042 || buffer->clip_changed
13043 || buffer->prevent_redisplay_optimizations_p);
13044
13045 if (MINI_WINDOW_P (w))
13046 {
13047 if (w == XWINDOW (echo_area_window)
13048 && !NILP (echo_area_buffer[0]))
13049 {
13050 if (update_mode_line)
13051 /* We may have to update a tty frame's menu bar or a
13052 tool-bar. Example `M-x C-h C-h C-g'. */
13053 goto finish_menu_bars;
13054 else
13055 /* We've already displayed the echo area glyphs in this window. */
13056 goto finish_scroll_bars;
13057 }
13058 else if ((w != XWINDOW (minibuf_window)
13059 || minibuf_level == 0)
13060 /* When buffer is nonempty, redisplay window normally. */
13061 && BUF_Z (XBUFFER (w->buffer)) == BUF_BEG (XBUFFER (w->buffer))
13062 /* Quail displays non-mini buffers in minibuffer window.
13063 In that case, redisplay the window normally. */
13064 && !NILP (Fmemq (w->buffer, Vminibuffer_list)))
13065 {
13066 /* W is a mini-buffer window, but it's not active, so clear
13067 it. */
13068 int yb = window_text_bottom_y (w);
13069 struct glyph_row *row;
13070 int y;
13071
13072 for (y = 0, row = w->desired_matrix->rows;
13073 y < yb;
13074 y += row->height, ++row)
13075 blank_row (w, row, y);
13076 goto finish_scroll_bars;
13077 }
13078
13079 clear_glyph_matrix (w->desired_matrix);
13080 }
13081
13082 /* Otherwise set up data on this window; select its buffer and point
13083 value. */
13084 /* Really select the buffer, for the sake of buffer-local
13085 variables. */
13086 set_buffer_internal_1 (XBUFFER (w->buffer));
13087 SET_TEXT_POS (opoint, PT, PT_BYTE);
13088
13089 beg_unchanged = BEG_UNCHANGED;
13090 end_unchanged = END_UNCHANGED;
13091
13092 current_matrix_up_to_date_p
13093 = (!NILP (w->window_end_valid)
13094 && !current_buffer->clip_changed
13095 && !current_buffer->prevent_redisplay_optimizations_p
13096 && XFASTINT (w->last_modified) >= MODIFF
13097 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
13098
13099 buffer_unchanged_p
13100 = (!NILP (w->window_end_valid)
13101 && !current_buffer->clip_changed
13102 && XFASTINT (w->last_modified) >= MODIFF
13103 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
13104
13105 /* When windows_or_buffers_changed is non-zero, we can't rely on
13106 the window end being valid, so set it to nil there. */
13107 if (windows_or_buffers_changed)
13108 {
13109 /* If window starts on a continuation line, maybe adjust the
13110 window start in case the window's width changed. */
13111 if (XMARKER (w->start)->buffer == current_buffer)
13112 compute_window_start_on_continuation_line (w);
13113
13114 w->window_end_valid = Qnil;
13115 }
13116
13117 /* Some sanity checks. */
13118 CHECK_WINDOW_END (w);
13119 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
13120 abort ();
13121 if (BYTEPOS (opoint) < CHARPOS (opoint))
13122 abort ();
13123
13124 /* If %c is in mode line, update it if needed. */
13125 if (!NILP (w->column_number_displayed)
13126 /* This alternative quickly identifies a common case
13127 where no change is needed. */
13128 && !(PT == XFASTINT (w->last_point)
13129 && XFASTINT (w->last_modified) >= MODIFF
13130 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
13131 && (XFASTINT (w->column_number_displayed)
13132 != (int) current_column ())) /* iftc */
13133 update_mode_line = 1;
13134
13135 /* Count number of windows showing the selected buffer. An indirect
13136 buffer counts as its base buffer. */
13137 if (!just_this_one_p)
13138 {
13139 struct buffer *current_base, *window_base;
13140 current_base = current_buffer;
13141 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
13142 if (current_base->base_buffer)
13143 current_base = current_base->base_buffer;
13144 if (window_base->base_buffer)
13145 window_base = window_base->base_buffer;
13146 if (current_base == window_base)
13147 buffer_shared++;
13148 }
13149
13150 /* Point refers normally to the selected window. For any other
13151 window, set up appropriate value. */
13152 if (!EQ (window, selected_window))
13153 {
13154 int new_pt = XMARKER (w->pointm)->charpos;
13155 int new_pt_byte = marker_byte_position (w->pointm);
13156 if (new_pt < BEGV)
13157 {
13158 new_pt = BEGV;
13159 new_pt_byte = BEGV_BYTE;
13160 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
13161 }
13162 else if (new_pt > (ZV - 1))
13163 {
13164 new_pt = ZV;
13165 new_pt_byte = ZV_BYTE;
13166 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
13167 }
13168
13169 /* We don't use SET_PT so that the point-motion hooks don't run. */
13170 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
13171 }
13172
13173 /* If any of the character widths specified in the display table
13174 have changed, invalidate the width run cache. It's true that
13175 this may be a bit late to catch such changes, but the rest of
13176 redisplay goes (non-fatally) haywire when the display table is
13177 changed, so why should we worry about doing any better? */
13178 if (current_buffer->width_run_cache)
13179 {
13180 struct Lisp_Char_Table *disptab = buffer_display_table ();
13181
13182 if (! disptab_matches_widthtab (disptab,
13183 XVECTOR (current_buffer->width_table)))
13184 {
13185 invalidate_region_cache (current_buffer,
13186 current_buffer->width_run_cache,
13187 BEG, Z);
13188 recompute_width_table (current_buffer, disptab);
13189 }
13190 }
13191
13192 /* If window-start is screwed up, choose a new one. */
13193 if (XMARKER (w->start)->buffer != current_buffer)
13194 goto recenter;
13195
13196 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13197
13198 /* If someone specified a new starting point but did not insist,
13199 check whether it can be used. */
13200 if (!NILP (w->optional_new_start)
13201 && CHARPOS (startp) >= BEGV
13202 && CHARPOS (startp) <= ZV)
13203 {
13204 w->optional_new_start = Qnil;
13205 start_display (&it, w, startp);
13206 move_it_to (&it, PT, 0, it.last_visible_y, -1,
13207 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
13208 if (IT_CHARPOS (it) == PT)
13209 w->force_start = Qt;
13210 /* IT may overshoot PT if text at PT is invisible. */
13211 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
13212 w->force_start = Qt;
13213 }
13214
13215 force_start:
13216
13217 /* Handle case where place to start displaying has been specified,
13218 unless the specified location is outside the accessible range. */
13219 if (!NILP (w->force_start)
13220 || w->frozen_window_start_p)
13221 {
13222 /* We set this later on if we have to adjust point. */
13223 int new_vpos = -1;
13224 int val;
13225
13226 w->force_start = Qnil;
13227 w->vscroll = 0;
13228 w->window_end_valid = Qnil;
13229
13230 /* Forget any recorded base line for line number display. */
13231 if (!buffer_unchanged_p)
13232 w->base_line_number = Qnil;
13233
13234 /* Redisplay the mode line. Select the buffer properly for that.
13235 Also, run the hook window-scroll-functions
13236 because we have scrolled. */
13237 /* Note, we do this after clearing force_start because
13238 if there's an error, it is better to forget about force_start
13239 than to get into an infinite loop calling the hook functions
13240 and having them get more errors. */
13241 if (!update_mode_line
13242 || ! NILP (Vwindow_scroll_functions))
13243 {
13244 update_mode_line = 1;
13245 w->update_mode_line = Qt;
13246 startp = run_window_scroll_functions (window, startp);
13247 }
13248
13249 w->last_modified = make_number (0);
13250 w->last_overlay_modified = make_number (0);
13251 if (CHARPOS (startp) < BEGV)
13252 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
13253 else if (CHARPOS (startp) > ZV)
13254 SET_TEXT_POS (startp, ZV, ZV_BYTE);
13255
13256 /* Redisplay, then check if cursor has been set during the
13257 redisplay. Give up if new fonts were loaded. */
13258 val = try_window (window, startp, 1);
13259 if (!val)
13260 {
13261 w->force_start = Qt;
13262 clear_glyph_matrix (w->desired_matrix);
13263 goto need_larger_matrices;
13264 }
13265 /* Point was outside the scroll margins. */
13266 if (val < 0)
13267 new_vpos = window_box_height (w) / 2;
13268
13269 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
13270 {
13271 /* If point does not appear, try to move point so it does
13272 appear. The desired matrix has been built above, so we
13273 can use it here. */
13274 new_vpos = window_box_height (w) / 2;
13275 }
13276
13277 if (!cursor_row_fully_visible_p (w, 0, 0))
13278 {
13279 /* Point does appear, but on a line partly visible at end of window.
13280 Move it back to a fully-visible line. */
13281 new_vpos = window_box_height (w);
13282 }
13283
13284 /* If we need to move point for either of the above reasons,
13285 now actually do it. */
13286 if (new_vpos >= 0)
13287 {
13288 struct glyph_row *row;
13289
13290 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
13291 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
13292 ++row;
13293
13294 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
13295 MATRIX_ROW_START_BYTEPOS (row));
13296
13297 if (w != XWINDOW (selected_window))
13298 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
13299 else if (current_buffer == old)
13300 SET_TEXT_POS (lpoint, PT, PT_BYTE);
13301
13302 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
13303
13304 /* If we are highlighting the region, then we just changed
13305 the region, so redisplay to show it. */
13306 if (!NILP (Vtransient_mark_mode)
13307 && !NILP (current_buffer->mark_active))
13308 {
13309 clear_glyph_matrix (w->desired_matrix);
13310 if (!try_window (window, startp, 0))
13311 goto need_larger_matrices;
13312 }
13313 }
13314
13315 #if GLYPH_DEBUG
13316 debug_method_add (w, "forced window start");
13317 #endif
13318 goto done;
13319 }
13320
13321 /* Handle case where text has not changed, only point, and it has
13322 not moved off the frame, and we are not retrying after hscroll.
13323 (current_matrix_up_to_date_p is nonzero when retrying.) */
13324 if (current_matrix_up_to_date_p
13325 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
13326 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
13327 {
13328 switch (rc)
13329 {
13330 case CURSOR_MOVEMENT_SUCCESS:
13331 used_current_matrix_p = 1;
13332 goto done;
13333
13334 #if 0 /* try_cursor_movement never returns this value. */
13335 case CURSOR_MOVEMENT_NEED_LARGER_MATRICES:
13336 goto need_larger_matrices;
13337 #endif
13338
13339 case CURSOR_MOVEMENT_MUST_SCROLL:
13340 goto try_to_scroll;
13341
13342 default:
13343 abort ();
13344 }
13345 }
13346 /* If current starting point was originally the beginning of a line
13347 but no longer is, find a new starting point. */
13348 else if (!NILP (w->start_at_line_beg)
13349 && !(CHARPOS (startp) <= BEGV
13350 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
13351 {
13352 #if GLYPH_DEBUG
13353 debug_method_add (w, "recenter 1");
13354 #endif
13355 goto recenter;
13356 }
13357
13358 /* Try scrolling with try_window_id. Value is > 0 if update has
13359 been done, it is -1 if we know that the same window start will
13360 not work. It is 0 if unsuccessful for some other reason. */
13361 else if ((tem = try_window_id (w)) != 0)
13362 {
13363 #if GLYPH_DEBUG
13364 debug_method_add (w, "try_window_id %d", tem);
13365 #endif
13366
13367 if (fonts_changed_p)
13368 goto need_larger_matrices;
13369 if (tem > 0)
13370 goto done;
13371
13372 /* Otherwise try_window_id has returned -1 which means that we
13373 don't want the alternative below this comment to execute. */
13374 }
13375 else if (CHARPOS (startp) >= BEGV
13376 && CHARPOS (startp) <= ZV
13377 && PT >= CHARPOS (startp)
13378 && (CHARPOS (startp) < ZV
13379 /* Avoid starting at end of buffer. */
13380 || CHARPOS (startp) == BEGV
13381 || (XFASTINT (w->last_modified) >= MODIFF
13382 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
13383 {
13384
13385 /* If first window line is a continuation line, and window start
13386 is inside the modified region, but the first change is before
13387 current window start, we must select a new window start.
13388
13389 However, if this is the result of a down-mouse event (e.g. by
13390 extending the mouse-drag-overlay), we don't want to select a
13391 new window start, since that would change the position under
13392 the mouse, resulting in an unwanted mouse-movement rather
13393 than a simple mouse-click. */
13394 if (NILP (w->start_at_line_beg)
13395 && NILP (do_mouse_tracking)
13396 && CHARPOS (startp) > BEGV
13397 && CHARPOS (startp) > BEG + beg_unchanged
13398 && CHARPOS (startp) <= Z - end_unchanged)
13399 {
13400 w->force_start = Qt;
13401 if (XMARKER (w->start)->buffer == current_buffer)
13402 compute_window_start_on_continuation_line (w);
13403 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13404 goto force_start;
13405 }
13406
13407 #if GLYPH_DEBUG
13408 debug_method_add (w, "same window start");
13409 #endif
13410
13411 /* Try to redisplay starting at same place as before.
13412 If point has not moved off frame, accept the results. */
13413 if (!current_matrix_up_to_date_p
13414 /* Don't use try_window_reusing_current_matrix in this case
13415 because a window scroll function can have changed the
13416 buffer. */
13417 || !NILP (Vwindow_scroll_functions)
13418 || MINI_WINDOW_P (w)
13419 || !(used_current_matrix_p
13420 = try_window_reusing_current_matrix (w)))
13421 {
13422 IF_DEBUG (debug_method_add (w, "1"));
13423 if (try_window (window, startp, 1) < 0)
13424 /* -1 means we need to scroll.
13425 0 means we need new matrices, but fonts_changed_p
13426 is set in that case, so we will detect it below. */
13427 goto try_to_scroll;
13428 }
13429
13430 if (fonts_changed_p)
13431 goto need_larger_matrices;
13432
13433 if (w->cursor.vpos >= 0)
13434 {
13435 if (!just_this_one_p
13436 || current_buffer->clip_changed
13437 || BEG_UNCHANGED < CHARPOS (startp))
13438 /* Forget any recorded base line for line number display. */
13439 w->base_line_number = Qnil;
13440
13441 if (!cursor_row_fully_visible_p (w, 1, 0))
13442 {
13443 clear_glyph_matrix (w->desired_matrix);
13444 last_line_misfit = 1;
13445 }
13446 /* Drop through and scroll. */
13447 else
13448 goto done;
13449 }
13450 else
13451 clear_glyph_matrix (w->desired_matrix);
13452 }
13453
13454 try_to_scroll:
13455
13456 w->last_modified = make_number (0);
13457 w->last_overlay_modified = make_number (0);
13458
13459 /* Redisplay the mode line. Select the buffer properly for that. */
13460 if (!update_mode_line)
13461 {
13462 update_mode_line = 1;
13463 w->update_mode_line = Qt;
13464 }
13465
13466 /* Try to scroll by specified few lines. */
13467 if ((scroll_conservatively
13468 || scroll_step
13469 || temp_scroll_step
13470 || NUMBERP (current_buffer->scroll_up_aggressively)
13471 || NUMBERP (current_buffer->scroll_down_aggressively))
13472 && !current_buffer->clip_changed
13473 && CHARPOS (startp) >= BEGV
13474 && CHARPOS (startp) <= ZV)
13475 {
13476 /* The function returns -1 if new fonts were loaded, 1 if
13477 successful, 0 if not successful. */
13478 int rc = try_scrolling (window, just_this_one_p,
13479 scroll_conservatively,
13480 scroll_step,
13481 temp_scroll_step, last_line_misfit);
13482 switch (rc)
13483 {
13484 case SCROLLING_SUCCESS:
13485 goto done;
13486
13487 case SCROLLING_NEED_LARGER_MATRICES:
13488 goto need_larger_matrices;
13489
13490 case SCROLLING_FAILED:
13491 break;
13492
13493 default:
13494 abort ();
13495 }
13496 }
13497
13498 /* Finally, just choose place to start which centers point */
13499
13500 recenter:
13501 if (centering_position < 0)
13502 centering_position = window_box_height (w) / 2;
13503
13504 #if GLYPH_DEBUG
13505 debug_method_add (w, "recenter");
13506 #endif
13507
13508 /* w->vscroll = 0; */
13509
13510 /* Forget any previously recorded base line for line number display. */
13511 if (!buffer_unchanged_p)
13512 w->base_line_number = Qnil;
13513
13514 /* Move backward half the height of the window. */
13515 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
13516 it.current_y = it.last_visible_y;
13517 move_it_vertically_backward (&it, centering_position);
13518 xassert (IT_CHARPOS (it) >= BEGV);
13519
13520 /* The function move_it_vertically_backward may move over more
13521 than the specified y-distance. If it->w is small, e.g. a
13522 mini-buffer window, we may end up in front of the window's
13523 display area. Start displaying at the start of the line
13524 containing PT in this case. */
13525 if (it.current_y <= 0)
13526 {
13527 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
13528 move_it_vertically_backward (&it, 0);
13529 #if 0
13530 /* I think this assert is bogus if buffer contains
13531 invisible text or images. KFS. */
13532 xassert (IT_CHARPOS (it) <= PT);
13533 #endif
13534 it.current_y = 0;
13535 }
13536
13537 it.current_x = it.hpos = 0;
13538
13539 /* Set startp here explicitly in case that helps avoid an infinite loop
13540 in case the window-scroll-functions functions get errors. */
13541 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
13542
13543 /* Run scroll hooks. */
13544 startp = run_window_scroll_functions (window, it.current.pos);
13545
13546 /* Redisplay the window. */
13547 if (!current_matrix_up_to_date_p
13548 || windows_or_buffers_changed
13549 || cursor_type_changed
13550 /* Don't use try_window_reusing_current_matrix in this case
13551 because it can have changed the buffer. */
13552 || !NILP (Vwindow_scroll_functions)
13553 || !just_this_one_p
13554 || MINI_WINDOW_P (w)
13555 || !(used_current_matrix_p
13556 = try_window_reusing_current_matrix (w)))
13557 try_window (window, startp, 0);
13558
13559 /* If new fonts have been loaded (due to fontsets), give up. We
13560 have to start a new redisplay since we need to re-adjust glyph
13561 matrices. */
13562 if (fonts_changed_p)
13563 goto need_larger_matrices;
13564
13565 /* If cursor did not appear assume that the middle of the window is
13566 in the first line of the window. Do it again with the next line.
13567 (Imagine a window of height 100, displaying two lines of height
13568 60. Moving back 50 from it->last_visible_y will end in the first
13569 line.) */
13570 if (w->cursor.vpos < 0)
13571 {
13572 if (!NILP (w->window_end_valid)
13573 && PT >= Z - XFASTINT (w->window_end_pos))
13574 {
13575 clear_glyph_matrix (w->desired_matrix);
13576 move_it_by_lines (&it, 1, 0);
13577 try_window (window, it.current.pos, 0);
13578 }
13579 else if (PT < IT_CHARPOS (it))
13580 {
13581 clear_glyph_matrix (w->desired_matrix);
13582 move_it_by_lines (&it, -1, 0);
13583 try_window (window, it.current.pos, 0);
13584 }
13585 else
13586 {
13587 /* Not much we can do about it. */
13588 }
13589 }
13590
13591 /* Consider the following case: Window starts at BEGV, there is
13592 invisible, intangible text at BEGV, so that display starts at
13593 some point START > BEGV. It can happen that we are called with
13594 PT somewhere between BEGV and START. Try to handle that case. */
13595 if (w->cursor.vpos < 0)
13596 {
13597 struct glyph_row *row = w->current_matrix->rows;
13598 if (row->mode_line_p)
13599 ++row;
13600 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13601 }
13602
13603 if (!cursor_row_fully_visible_p (w, 0, 0))
13604 {
13605 /* If vscroll is enabled, disable it and try again. */
13606 if (w->vscroll)
13607 {
13608 w->vscroll = 0;
13609 clear_glyph_matrix (w->desired_matrix);
13610 goto recenter;
13611 }
13612
13613 /* If centering point failed to make the whole line visible,
13614 put point at the top instead. That has to make the whole line
13615 visible, if it can be done. */
13616 if (centering_position == 0)
13617 goto done;
13618
13619 clear_glyph_matrix (w->desired_matrix);
13620 centering_position = 0;
13621 goto recenter;
13622 }
13623
13624 done:
13625
13626 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13627 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
13628 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
13629 ? Qt : Qnil);
13630
13631 /* Display the mode line, if we must. */
13632 if ((update_mode_line
13633 /* If window not full width, must redo its mode line
13634 if (a) the window to its side is being redone and
13635 (b) we do a frame-based redisplay. This is a consequence
13636 of how inverted lines are drawn in frame-based redisplay. */
13637 || (!just_this_one_p
13638 && !FRAME_WINDOW_P (f)
13639 && !WINDOW_FULL_WIDTH_P (w))
13640 /* Line number to display. */
13641 || INTEGERP (w->base_line_pos)
13642 /* Column number is displayed and different from the one displayed. */
13643 || (!NILP (w->column_number_displayed)
13644 && (XFASTINT (w->column_number_displayed)
13645 != (int) current_column ()))) /* iftc */
13646 /* This means that the window has a mode line. */
13647 && (WINDOW_WANTS_MODELINE_P (w)
13648 || WINDOW_WANTS_HEADER_LINE_P (w)))
13649 {
13650 display_mode_lines (w);
13651
13652 /* If mode line height has changed, arrange for a thorough
13653 immediate redisplay using the correct mode line height. */
13654 if (WINDOW_WANTS_MODELINE_P (w)
13655 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
13656 {
13657 fonts_changed_p = 1;
13658 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
13659 = DESIRED_MODE_LINE_HEIGHT (w);
13660 }
13661
13662 /* If top line height has changed, arrange for a thorough
13663 immediate redisplay using the correct mode line height. */
13664 if (WINDOW_WANTS_HEADER_LINE_P (w)
13665 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
13666 {
13667 fonts_changed_p = 1;
13668 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
13669 = DESIRED_HEADER_LINE_HEIGHT (w);
13670 }
13671
13672 if (fonts_changed_p)
13673 goto need_larger_matrices;
13674 }
13675
13676 if (!line_number_displayed
13677 && !BUFFERP (w->base_line_pos))
13678 {
13679 w->base_line_pos = Qnil;
13680 w->base_line_number = Qnil;
13681 }
13682
13683 finish_menu_bars:
13684
13685 /* When we reach a frame's selected window, redo the frame's menu bar. */
13686 if (update_mode_line
13687 && EQ (FRAME_SELECTED_WINDOW (f), window))
13688 {
13689 int redisplay_menu_p = 0;
13690 int redisplay_tool_bar_p = 0;
13691
13692 if (FRAME_WINDOW_P (f))
13693 {
13694 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
13695 || defined (USE_GTK)
13696 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
13697 #else
13698 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
13699 #endif
13700 }
13701 else
13702 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
13703
13704 if (redisplay_menu_p)
13705 display_menu_bar (w);
13706
13707 #ifdef HAVE_WINDOW_SYSTEM
13708 if (FRAME_WINDOW_P (f))
13709 {
13710 #if defined (USE_GTK) || USE_MAC_TOOLBAR
13711 redisplay_tool_bar_p = FRAME_EXTERNAL_TOOL_BAR (f);
13712 #else
13713 redisplay_tool_bar_p = WINDOWP (f->tool_bar_window)
13714 && (FRAME_TOOL_BAR_LINES (f) > 0
13715 || !NILP (Vauto_resize_tool_bars));
13716 #endif
13717
13718 if (redisplay_tool_bar_p && redisplay_tool_bar (f))
13719 {
13720 extern int ignore_mouse_drag_p;
13721 ignore_mouse_drag_p = 1;
13722 }
13723 }
13724 #endif
13725 }
13726
13727 #ifdef HAVE_WINDOW_SYSTEM
13728 if (FRAME_WINDOW_P (f)
13729 && update_window_fringes (w, (just_this_one_p
13730 || (!used_current_matrix_p && !overlay_arrow_seen)
13731 || w->pseudo_window_p)))
13732 {
13733 update_begin (f);
13734 BLOCK_INPUT;
13735 if (draw_window_fringes (w, 1))
13736 x_draw_vertical_border (w);
13737 UNBLOCK_INPUT;
13738 update_end (f);
13739 }
13740 #endif /* HAVE_WINDOW_SYSTEM */
13741
13742 /* We go to this label, with fonts_changed_p nonzero,
13743 if it is necessary to try again using larger glyph matrices.
13744 We have to redeem the scroll bar even in this case,
13745 because the loop in redisplay_internal expects that. */
13746 need_larger_matrices:
13747 ;
13748 finish_scroll_bars:
13749
13750 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
13751 {
13752 /* Set the thumb's position and size. */
13753 set_vertical_scroll_bar (w);
13754
13755 /* Note that we actually used the scroll bar attached to this
13756 window, so it shouldn't be deleted at the end of redisplay. */
13757 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
13758 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
13759 }
13760
13761 /* Restore current_buffer and value of point in it. */
13762 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
13763 set_buffer_internal_1 (old);
13764 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
13765 shorter. This can be caused by log truncation in *Messages*. */
13766 if (CHARPOS (lpoint) <= ZV)
13767 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
13768
13769 unbind_to (count, Qnil);
13770 }
13771
13772
13773 /* Build the complete desired matrix of WINDOW with a window start
13774 buffer position POS.
13775
13776 Value is 1 if successful. It is zero if fonts were loaded during
13777 redisplay which makes re-adjusting glyph matrices necessary, and -1
13778 if point would appear in the scroll margins.
13779 (We check that only if CHECK_MARGINS is nonzero. */
13780
13781 int
13782 try_window (window, pos, check_margins)
13783 Lisp_Object window;
13784 struct text_pos pos;
13785 int check_margins;
13786 {
13787 struct window *w = XWINDOW (window);
13788 struct it it;
13789 struct glyph_row *last_text_row = NULL;
13790 struct frame *f = XFRAME (w->frame);
13791
13792 /* Make POS the new window start. */
13793 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
13794
13795 /* Mark cursor position as unknown. No overlay arrow seen. */
13796 w->cursor.vpos = -1;
13797 overlay_arrow_seen = 0;
13798
13799 /* Initialize iterator and info to start at POS. */
13800 start_display (&it, w, pos);
13801
13802 /* Display all lines of W. */
13803 while (it.current_y < it.last_visible_y)
13804 {
13805 if (display_line (&it))
13806 last_text_row = it.glyph_row - 1;
13807 if (fonts_changed_p)
13808 return 0;
13809 }
13810
13811 /* Don't let the cursor end in the scroll margins. */
13812 if (check_margins
13813 && !MINI_WINDOW_P (w))
13814 {
13815 int this_scroll_margin;
13816
13817 this_scroll_margin = max (0, scroll_margin);
13818 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
13819 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
13820
13821 if ((w->cursor.y >= 0 /* not vscrolled */
13822 && w->cursor.y < this_scroll_margin
13823 && CHARPOS (pos) > BEGV
13824 && IT_CHARPOS (it) < ZV)
13825 /* rms: considering make_cursor_line_fully_visible_p here
13826 seems to give wrong results. We don't want to recenter
13827 when the last line is partly visible, we want to allow
13828 that case to be handled in the usual way. */
13829 || (w->cursor.y + 1) > it.last_visible_y)
13830 {
13831 w->cursor.vpos = -1;
13832 clear_glyph_matrix (w->desired_matrix);
13833 return -1;
13834 }
13835 }
13836
13837 /* If bottom moved off end of frame, change mode line percentage. */
13838 if (XFASTINT (w->window_end_pos) <= 0
13839 && Z != IT_CHARPOS (it))
13840 w->update_mode_line = Qt;
13841
13842 /* Set window_end_pos to the offset of the last character displayed
13843 on the window from the end of current_buffer. Set
13844 window_end_vpos to its row number. */
13845 if (last_text_row)
13846 {
13847 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
13848 w->window_end_bytepos
13849 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
13850 w->window_end_pos
13851 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
13852 w->window_end_vpos
13853 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
13854 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
13855 ->displays_text_p);
13856 }
13857 else
13858 {
13859 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
13860 w->window_end_pos = make_number (Z - ZV);
13861 w->window_end_vpos = make_number (0);
13862 }
13863
13864 /* But that is not valid info until redisplay finishes. */
13865 w->window_end_valid = Qnil;
13866 return 1;
13867 }
13868
13869
13870 \f
13871 /************************************************************************
13872 Window redisplay reusing current matrix when buffer has not changed
13873 ************************************************************************/
13874
13875 /* Try redisplay of window W showing an unchanged buffer with a
13876 different window start than the last time it was displayed by
13877 reusing its current matrix. Value is non-zero if successful.
13878 W->start is the new window start. */
13879
13880 static int
13881 try_window_reusing_current_matrix (w)
13882 struct window *w;
13883 {
13884 struct frame *f = XFRAME (w->frame);
13885 struct glyph_row *row, *bottom_row;
13886 struct it it;
13887 struct run run;
13888 struct text_pos start, new_start;
13889 int nrows_scrolled, i;
13890 struct glyph_row *last_text_row;
13891 struct glyph_row *last_reused_text_row;
13892 struct glyph_row *start_row;
13893 int start_vpos, min_y, max_y;
13894
13895 #if GLYPH_DEBUG
13896 if (inhibit_try_window_reusing)
13897 return 0;
13898 #endif
13899
13900 if (/* This function doesn't handle terminal frames. */
13901 !FRAME_WINDOW_P (f)
13902 /* Don't try to reuse the display if windows have been split
13903 or such. */
13904 || windows_or_buffers_changed
13905 || cursor_type_changed)
13906 return 0;
13907
13908 /* Can't do this if region may have changed. */
13909 if ((!NILP (Vtransient_mark_mode)
13910 && !NILP (current_buffer->mark_active))
13911 || !NILP (w->region_showing)
13912 || !NILP (Vshow_trailing_whitespace))
13913 return 0;
13914
13915 /* If top-line visibility has changed, give up. */
13916 if (WINDOW_WANTS_HEADER_LINE_P (w)
13917 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
13918 return 0;
13919
13920 /* Give up if old or new display is scrolled vertically. We could
13921 make this function handle this, but right now it doesn't. */
13922 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
13923 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
13924 return 0;
13925
13926 /* The variable new_start now holds the new window start. The old
13927 start `start' can be determined from the current matrix. */
13928 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
13929 start = start_row->start.pos;
13930 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
13931
13932 /* Clear the desired matrix for the display below. */
13933 clear_glyph_matrix (w->desired_matrix);
13934
13935 if (CHARPOS (new_start) <= CHARPOS (start))
13936 {
13937 int first_row_y;
13938
13939 /* Don't use this method if the display starts with an ellipsis
13940 displayed for invisible text. It's not easy to handle that case
13941 below, and it's certainly not worth the effort since this is
13942 not a frequent case. */
13943 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
13944 return 0;
13945
13946 IF_DEBUG (debug_method_add (w, "twu1"));
13947
13948 /* Display up to a row that can be reused. The variable
13949 last_text_row is set to the last row displayed that displays
13950 text. Note that it.vpos == 0 if or if not there is a
13951 header-line; it's not the same as the MATRIX_ROW_VPOS! */
13952 start_display (&it, w, new_start);
13953 first_row_y = it.current_y;
13954 w->cursor.vpos = -1;
13955 last_text_row = last_reused_text_row = NULL;
13956
13957 while (it.current_y < it.last_visible_y
13958 && !fonts_changed_p)
13959 {
13960 /* If we have reached into the characters in the START row,
13961 that means the line boundaries have changed. So we
13962 can't start copying with the row START. Maybe it will
13963 work to start copying with the following row. */
13964 while (IT_CHARPOS (it) > CHARPOS (start))
13965 {
13966 /* Advance to the next row as the "start". */
13967 start_row++;
13968 start = start_row->start.pos;
13969 /* If there are no more rows to try, or just one, give up. */
13970 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
13971 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
13972 || CHARPOS (start) == ZV)
13973 {
13974 clear_glyph_matrix (w->desired_matrix);
13975 return 0;
13976 }
13977
13978 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
13979 }
13980 /* If we have reached alignment,
13981 we can copy the rest of the rows. */
13982 if (IT_CHARPOS (it) == CHARPOS (start))
13983 break;
13984
13985 if (display_line (&it))
13986 last_text_row = it.glyph_row - 1;
13987 }
13988
13989 /* A value of current_y < last_visible_y means that we stopped
13990 at the previous window start, which in turn means that we
13991 have at least one reusable row. */
13992 if (it.current_y < it.last_visible_y)
13993 {
13994 /* IT.vpos always starts from 0; it counts text lines. */
13995 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
13996
13997 /* Find PT if not already found in the lines displayed. */
13998 if (w->cursor.vpos < 0)
13999 {
14000 int dy = it.current_y - start_row->y;
14001
14002 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14003 row = row_containing_pos (w, PT, row, NULL, dy);
14004 if (row)
14005 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
14006 dy, nrows_scrolled);
14007 else
14008 {
14009 clear_glyph_matrix (w->desired_matrix);
14010 return 0;
14011 }
14012 }
14013
14014 /* Scroll the display. Do it before the current matrix is
14015 changed. The problem here is that update has not yet
14016 run, i.e. part of the current matrix is not up to date.
14017 scroll_run_hook will clear the cursor, and use the
14018 current matrix to get the height of the row the cursor is
14019 in. */
14020 run.current_y = start_row->y;
14021 run.desired_y = it.current_y;
14022 run.height = it.last_visible_y - it.current_y;
14023
14024 if (run.height > 0 && run.current_y != run.desired_y)
14025 {
14026 update_begin (f);
14027 FRAME_RIF (f)->update_window_begin_hook (w);
14028 FRAME_RIF (f)->clear_window_mouse_face (w);
14029 FRAME_RIF (f)->scroll_run_hook (w, &run);
14030 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
14031 update_end (f);
14032 }
14033
14034 /* Shift current matrix down by nrows_scrolled lines. */
14035 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
14036 rotate_matrix (w->current_matrix,
14037 start_vpos,
14038 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
14039 nrows_scrolled);
14040
14041 /* Disable lines that must be updated. */
14042 for (i = 0; i < nrows_scrolled; ++i)
14043 (start_row + i)->enabled_p = 0;
14044
14045 /* Re-compute Y positions. */
14046 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
14047 max_y = it.last_visible_y;
14048 for (row = start_row + nrows_scrolled;
14049 row < bottom_row;
14050 ++row)
14051 {
14052 row->y = it.current_y;
14053 row->visible_height = row->height;
14054
14055 if (row->y < min_y)
14056 row->visible_height -= min_y - row->y;
14057 if (row->y + row->height > max_y)
14058 row->visible_height -= row->y + row->height - max_y;
14059 row->redraw_fringe_bitmaps_p = 1;
14060
14061 it.current_y += row->height;
14062
14063 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14064 last_reused_text_row = row;
14065 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
14066 break;
14067 }
14068
14069 /* Disable lines in the current matrix which are now
14070 below the window. */
14071 for (++row; row < bottom_row; ++row)
14072 row->enabled_p = row->mode_line_p = 0;
14073 }
14074
14075 /* Update window_end_pos etc.; last_reused_text_row is the last
14076 reused row from the current matrix containing text, if any.
14077 The value of last_text_row is the last displayed line
14078 containing text. */
14079 if (last_reused_text_row)
14080 {
14081 w->window_end_bytepos
14082 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
14083 w->window_end_pos
14084 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
14085 w->window_end_vpos
14086 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
14087 w->current_matrix));
14088 }
14089 else if (last_text_row)
14090 {
14091 w->window_end_bytepos
14092 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
14093 w->window_end_pos
14094 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
14095 w->window_end_vpos
14096 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
14097 }
14098 else
14099 {
14100 /* This window must be completely empty. */
14101 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
14102 w->window_end_pos = make_number (Z - ZV);
14103 w->window_end_vpos = make_number (0);
14104 }
14105 w->window_end_valid = Qnil;
14106
14107 /* Update hint: don't try scrolling again in update_window. */
14108 w->desired_matrix->no_scrolling_p = 1;
14109
14110 #if GLYPH_DEBUG
14111 debug_method_add (w, "try_window_reusing_current_matrix 1");
14112 #endif
14113 return 1;
14114 }
14115 else if (CHARPOS (new_start) > CHARPOS (start))
14116 {
14117 struct glyph_row *pt_row, *row;
14118 struct glyph_row *first_reusable_row;
14119 struct glyph_row *first_row_to_display;
14120 int dy;
14121 int yb = window_text_bottom_y (w);
14122
14123 /* Find the row starting at new_start, if there is one. Don't
14124 reuse a partially visible line at the end. */
14125 first_reusable_row = start_row;
14126 while (first_reusable_row->enabled_p
14127 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
14128 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
14129 < CHARPOS (new_start)))
14130 ++first_reusable_row;
14131
14132 /* Give up if there is no row to reuse. */
14133 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
14134 || !first_reusable_row->enabled_p
14135 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
14136 != CHARPOS (new_start)))
14137 return 0;
14138
14139 /* We can reuse fully visible rows beginning with
14140 first_reusable_row to the end of the window. Set
14141 first_row_to_display to the first row that cannot be reused.
14142 Set pt_row to the row containing point, if there is any. */
14143 pt_row = NULL;
14144 for (first_row_to_display = first_reusable_row;
14145 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
14146 ++first_row_to_display)
14147 {
14148 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
14149 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
14150 pt_row = first_row_to_display;
14151 }
14152
14153 /* Start displaying at the start of first_row_to_display. */
14154 xassert (first_row_to_display->y < yb);
14155 init_to_row_start (&it, w, first_row_to_display);
14156
14157 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
14158 - start_vpos);
14159 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
14160 - nrows_scrolled);
14161 it.current_y = (first_row_to_display->y - first_reusable_row->y
14162 + WINDOW_HEADER_LINE_HEIGHT (w));
14163
14164 /* Display lines beginning with first_row_to_display in the
14165 desired matrix. Set last_text_row to the last row displayed
14166 that displays text. */
14167 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
14168 if (pt_row == NULL)
14169 w->cursor.vpos = -1;
14170 last_text_row = NULL;
14171 while (it.current_y < it.last_visible_y && !fonts_changed_p)
14172 if (display_line (&it))
14173 last_text_row = it.glyph_row - 1;
14174
14175 /* Give up If point isn't in a row displayed or reused. */
14176 if (w->cursor.vpos < 0)
14177 {
14178 clear_glyph_matrix (w->desired_matrix);
14179 return 0;
14180 }
14181
14182 /* If point is in a reused row, adjust y and vpos of the cursor
14183 position. */
14184 if (pt_row)
14185 {
14186 w->cursor.vpos -= nrows_scrolled;
14187 w->cursor.y -= first_reusable_row->y - start_row->y;
14188 }
14189
14190 /* Scroll the display. */
14191 run.current_y = first_reusable_row->y;
14192 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
14193 run.height = it.last_visible_y - run.current_y;
14194 dy = run.current_y - run.desired_y;
14195
14196 if (run.height)
14197 {
14198 update_begin (f);
14199 FRAME_RIF (f)->update_window_begin_hook (w);
14200 FRAME_RIF (f)->clear_window_mouse_face (w);
14201 FRAME_RIF (f)->scroll_run_hook (w, &run);
14202 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
14203 update_end (f);
14204 }
14205
14206 /* Adjust Y positions of reused rows. */
14207 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
14208 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
14209 max_y = it.last_visible_y;
14210 for (row = first_reusable_row; row < first_row_to_display; ++row)
14211 {
14212 row->y -= dy;
14213 row->visible_height = row->height;
14214 if (row->y < min_y)
14215 row->visible_height -= min_y - row->y;
14216 if (row->y + row->height > max_y)
14217 row->visible_height -= row->y + row->height - max_y;
14218 row->redraw_fringe_bitmaps_p = 1;
14219 }
14220
14221 /* Scroll the current matrix. */
14222 xassert (nrows_scrolled > 0);
14223 rotate_matrix (w->current_matrix,
14224 start_vpos,
14225 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
14226 -nrows_scrolled);
14227
14228 /* Disable rows not reused. */
14229 for (row -= nrows_scrolled; row < bottom_row; ++row)
14230 row->enabled_p = 0;
14231
14232 /* Point may have moved to a different line, so we cannot assume that
14233 the previous cursor position is valid; locate the correct row. */
14234 if (pt_row)
14235 {
14236 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
14237 row < bottom_row && PT >= MATRIX_ROW_END_CHARPOS (row);
14238 row++)
14239 {
14240 w->cursor.vpos++;
14241 w->cursor.y = row->y;
14242 }
14243 if (row < bottom_row)
14244 {
14245 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
14246 while (glyph->charpos < PT)
14247 {
14248 w->cursor.hpos++;
14249 w->cursor.x += glyph->pixel_width;
14250 glyph++;
14251 }
14252 }
14253 }
14254
14255 /* Adjust window end. A null value of last_text_row means that
14256 the window end is in reused rows which in turn means that
14257 only its vpos can have changed. */
14258 if (last_text_row)
14259 {
14260 w->window_end_bytepos
14261 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
14262 w->window_end_pos
14263 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
14264 w->window_end_vpos
14265 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
14266 }
14267 else
14268 {
14269 w->window_end_vpos
14270 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
14271 }
14272
14273 w->window_end_valid = Qnil;
14274 w->desired_matrix->no_scrolling_p = 1;
14275
14276 #if GLYPH_DEBUG
14277 debug_method_add (w, "try_window_reusing_current_matrix 2");
14278 #endif
14279 return 1;
14280 }
14281
14282 return 0;
14283 }
14284
14285
14286 \f
14287 /************************************************************************
14288 Window redisplay reusing current matrix when buffer has changed
14289 ************************************************************************/
14290
14291 static struct glyph_row *find_last_unchanged_at_beg_row P_ ((struct window *));
14292 static struct glyph_row *find_first_unchanged_at_end_row P_ ((struct window *,
14293 int *, int *));
14294 static struct glyph_row *
14295 find_last_row_displaying_text P_ ((struct glyph_matrix *, struct it *,
14296 struct glyph_row *));
14297
14298
14299 /* Return the last row in MATRIX displaying text. If row START is
14300 non-null, start searching with that row. IT gives the dimensions
14301 of the display. Value is null if matrix is empty; otherwise it is
14302 a pointer to the row found. */
14303
14304 static struct glyph_row *
14305 find_last_row_displaying_text (matrix, it, start)
14306 struct glyph_matrix *matrix;
14307 struct it *it;
14308 struct glyph_row *start;
14309 {
14310 struct glyph_row *row, *row_found;
14311
14312 /* Set row_found to the last row in IT->w's current matrix
14313 displaying text. The loop looks funny but think of partially
14314 visible lines. */
14315 row_found = NULL;
14316 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
14317 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14318 {
14319 xassert (row->enabled_p);
14320 row_found = row;
14321 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
14322 break;
14323 ++row;
14324 }
14325
14326 return row_found;
14327 }
14328
14329
14330 /* Return the last row in the current matrix of W that is not affected
14331 by changes at the start of current_buffer that occurred since W's
14332 current matrix was built. Value is null if no such row exists.
14333
14334 BEG_UNCHANGED us the number of characters unchanged at the start of
14335 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
14336 first changed character in current_buffer. Characters at positions <
14337 BEG + BEG_UNCHANGED are at the same buffer positions as they were
14338 when the current matrix was built. */
14339
14340 static struct glyph_row *
14341 find_last_unchanged_at_beg_row (w)
14342 struct window *w;
14343 {
14344 int first_changed_pos = BEG + BEG_UNCHANGED;
14345 struct glyph_row *row;
14346 struct glyph_row *row_found = NULL;
14347 int yb = window_text_bottom_y (w);
14348
14349 /* Find the last row displaying unchanged text. */
14350 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14351 while (MATRIX_ROW_DISPLAYS_TEXT_P (row)
14352 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos)
14353 {
14354 if (/* If row ends before first_changed_pos, it is unchanged,
14355 except in some case. */
14356 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
14357 /* When row ends in ZV and we write at ZV it is not
14358 unchanged. */
14359 && !row->ends_at_zv_p
14360 /* When first_changed_pos is the end of a continued line,
14361 row is not unchanged because it may be no longer
14362 continued. */
14363 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
14364 && (row->continued_p
14365 || row->exact_window_width_line_p)))
14366 row_found = row;
14367
14368 /* Stop if last visible row. */
14369 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
14370 break;
14371
14372 ++row;
14373 }
14374
14375 return row_found;
14376 }
14377
14378
14379 /* Find the first glyph row in the current matrix of W that is not
14380 affected by changes at the end of current_buffer since the
14381 time W's current matrix was built.
14382
14383 Return in *DELTA the number of chars by which buffer positions in
14384 unchanged text at the end of current_buffer must be adjusted.
14385
14386 Return in *DELTA_BYTES the corresponding number of bytes.
14387
14388 Value is null if no such row exists, i.e. all rows are affected by
14389 changes. */
14390
14391 static struct glyph_row *
14392 find_first_unchanged_at_end_row (w, delta, delta_bytes)
14393 struct window *w;
14394 int *delta, *delta_bytes;
14395 {
14396 struct glyph_row *row;
14397 struct glyph_row *row_found = NULL;
14398
14399 *delta = *delta_bytes = 0;
14400
14401 /* Display must not have been paused, otherwise the current matrix
14402 is not up to date. */
14403 if (NILP (w->window_end_valid))
14404 abort ();
14405
14406 /* A value of window_end_pos >= END_UNCHANGED means that the window
14407 end is in the range of changed text. If so, there is no
14408 unchanged row at the end of W's current matrix. */
14409 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
14410 return NULL;
14411
14412 /* Set row to the last row in W's current matrix displaying text. */
14413 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
14414
14415 /* If matrix is entirely empty, no unchanged row exists. */
14416 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14417 {
14418 /* The value of row is the last glyph row in the matrix having a
14419 meaningful buffer position in it. The end position of row
14420 corresponds to window_end_pos. This allows us to translate
14421 buffer positions in the current matrix to current buffer
14422 positions for characters not in changed text. */
14423 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
14424 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
14425 int last_unchanged_pos, last_unchanged_pos_old;
14426 struct glyph_row *first_text_row
14427 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14428
14429 *delta = Z - Z_old;
14430 *delta_bytes = Z_BYTE - Z_BYTE_old;
14431
14432 /* Set last_unchanged_pos to the buffer position of the last
14433 character in the buffer that has not been changed. Z is the
14434 index + 1 of the last character in current_buffer, i.e. by
14435 subtracting END_UNCHANGED we get the index of the last
14436 unchanged character, and we have to add BEG to get its buffer
14437 position. */
14438 last_unchanged_pos = Z - END_UNCHANGED + BEG;
14439 last_unchanged_pos_old = last_unchanged_pos - *delta;
14440
14441 /* Search backward from ROW for a row displaying a line that
14442 starts at a minimum position >= last_unchanged_pos_old. */
14443 for (; row > first_text_row; --row)
14444 {
14445 /* This used to abort, but it can happen.
14446 It is ok to just stop the search instead here. KFS. */
14447 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
14448 break;
14449
14450 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
14451 row_found = row;
14452 }
14453 }
14454
14455 if (row_found && !MATRIX_ROW_DISPLAYS_TEXT_P (row_found))
14456 abort ();
14457
14458 return row_found;
14459 }
14460
14461
14462 /* Make sure that glyph rows in the current matrix of window W
14463 reference the same glyph memory as corresponding rows in the
14464 frame's frame matrix. This function is called after scrolling W's
14465 current matrix on a terminal frame in try_window_id and
14466 try_window_reusing_current_matrix. */
14467
14468 static void
14469 sync_frame_with_window_matrix_rows (w)
14470 struct window *w;
14471 {
14472 struct frame *f = XFRAME (w->frame);
14473 struct glyph_row *window_row, *window_row_end, *frame_row;
14474
14475 /* Preconditions: W must be a leaf window and full-width. Its frame
14476 must have a frame matrix. */
14477 xassert (NILP (w->hchild) && NILP (w->vchild));
14478 xassert (WINDOW_FULL_WIDTH_P (w));
14479 xassert (!FRAME_WINDOW_P (f));
14480
14481 /* If W is a full-width window, glyph pointers in W's current matrix
14482 have, by definition, to be the same as glyph pointers in the
14483 corresponding frame matrix. Note that frame matrices have no
14484 marginal areas (see build_frame_matrix). */
14485 window_row = w->current_matrix->rows;
14486 window_row_end = window_row + w->current_matrix->nrows;
14487 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
14488 while (window_row < window_row_end)
14489 {
14490 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
14491 struct glyph *end = window_row->glyphs[LAST_AREA];
14492
14493 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
14494 frame_row->glyphs[TEXT_AREA] = start;
14495 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
14496 frame_row->glyphs[LAST_AREA] = end;
14497
14498 /* Disable frame rows whose corresponding window rows have
14499 been disabled in try_window_id. */
14500 if (!window_row->enabled_p)
14501 frame_row->enabled_p = 0;
14502
14503 ++window_row, ++frame_row;
14504 }
14505 }
14506
14507
14508 /* Find the glyph row in window W containing CHARPOS. Consider all
14509 rows between START and END (not inclusive). END null means search
14510 all rows to the end of the display area of W. Value is the row
14511 containing CHARPOS or null. */
14512
14513 struct glyph_row *
14514 row_containing_pos (w, charpos, start, end, dy)
14515 struct window *w;
14516 int charpos;
14517 struct glyph_row *start, *end;
14518 int dy;
14519 {
14520 struct glyph_row *row = start;
14521 int last_y;
14522
14523 /* If we happen to start on a header-line, skip that. */
14524 if (row->mode_line_p)
14525 ++row;
14526
14527 if ((end && row >= end) || !row->enabled_p)
14528 return NULL;
14529
14530 last_y = window_text_bottom_y (w) - dy;
14531
14532 while (1)
14533 {
14534 /* Give up if we have gone too far. */
14535 if (end && row >= end)
14536 return NULL;
14537 /* This formerly returned if they were equal.
14538 I think that both quantities are of a "last plus one" type;
14539 if so, when they are equal, the row is within the screen. -- rms. */
14540 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
14541 return NULL;
14542
14543 /* If it is in this row, return this row. */
14544 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
14545 || (MATRIX_ROW_END_CHARPOS (row) == charpos
14546 /* The end position of a row equals the start
14547 position of the next row. If CHARPOS is there, we
14548 would rather display it in the next line, except
14549 when this line ends in ZV. */
14550 && !row->ends_at_zv_p
14551 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
14552 && charpos >= MATRIX_ROW_START_CHARPOS (row))
14553 return row;
14554 ++row;
14555 }
14556 }
14557
14558
14559 /* Try to redisplay window W by reusing its existing display. W's
14560 current matrix must be up to date when this function is called,
14561 i.e. window_end_valid must not be nil.
14562
14563 Value is
14564
14565 1 if display has been updated
14566 0 if otherwise unsuccessful
14567 -1 if redisplay with same window start is known not to succeed
14568
14569 The following steps are performed:
14570
14571 1. Find the last row in the current matrix of W that is not
14572 affected by changes at the start of current_buffer. If no such row
14573 is found, give up.
14574
14575 2. Find the first row in W's current matrix that is not affected by
14576 changes at the end of current_buffer. Maybe there is no such row.
14577
14578 3. Display lines beginning with the row + 1 found in step 1 to the
14579 row found in step 2 or, if step 2 didn't find a row, to the end of
14580 the window.
14581
14582 4. If cursor is not known to appear on the window, give up.
14583
14584 5. If display stopped at the row found in step 2, scroll the
14585 display and current matrix as needed.
14586
14587 6. Maybe display some lines at the end of W, if we must. This can
14588 happen under various circumstances, like a partially visible line
14589 becoming fully visible, or because newly displayed lines are displayed
14590 in smaller font sizes.
14591
14592 7. Update W's window end information. */
14593
14594 static int
14595 try_window_id (w)
14596 struct window *w;
14597 {
14598 struct frame *f = XFRAME (w->frame);
14599 struct glyph_matrix *current_matrix = w->current_matrix;
14600 struct glyph_matrix *desired_matrix = w->desired_matrix;
14601 struct glyph_row *last_unchanged_at_beg_row;
14602 struct glyph_row *first_unchanged_at_end_row;
14603 struct glyph_row *row;
14604 struct glyph_row *bottom_row;
14605 int bottom_vpos;
14606 struct it it;
14607 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
14608 struct text_pos start_pos;
14609 struct run run;
14610 int first_unchanged_at_end_vpos = 0;
14611 struct glyph_row *last_text_row, *last_text_row_at_end;
14612 struct text_pos start;
14613 int first_changed_charpos, last_changed_charpos;
14614
14615 #if GLYPH_DEBUG
14616 if (inhibit_try_window_id)
14617 return 0;
14618 #endif
14619
14620 /* This is handy for debugging. */
14621 #if 0
14622 #define GIVE_UP(X) \
14623 do { \
14624 fprintf (stderr, "try_window_id give up %d\n", (X)); \
14625 return 0; \
14626 } while (0)
14627 #else
14628 #define GIVE_UP(X) return 0
14629 #endif
14630
14631 SET_TEXT_POS_FROM_MARKER (start, w->start);
14632
14633 /* Don't use this for mini-windows because these can show
14634 messages and mini-buffers, and we don't handle that here. */
14635 if (MINI_WINDOW_P (w))
14636 GIVE_UP (1);
14637
14638 /* This flag is used to prevent redisplay optimizations. */
14639 if (windows_or_buffers_changed || cursor_type_changed)
14640 GIVE_UP (2);
14641
14642 /* Verify that narrowing has not changed.
14643 Also verify that we were not told to prevent redisplay optimizations.
14644 It would be nice to further
14645 reduce the number of cases where this prevents try_window_id. */
14646 if (current_buffer->clip_changed
14647 || current_buffer->prevent_redisplay_optimizations_p)
14648 GIVE_UP (3);
14649
14650 /* Window must either use window-based redisplay or be full width. */
14651 if (!FRAME_WINDOW_P (f)
14652 && (!FRAME_LINE_INS_DEL_OK (f)
14653 || !WINDOW_FULL_WIDTH_P (w)))
14654 GIVE_UP (4);
14655
14656 /* Give up if point is not known NOT to appear in W. */
14657 if (PT < CHARPOS (start))
14658 GIVE_UP (5);
14659
14660 /* Another way to prevent redisplay optimizations. */
14661 if (XFASTINT (w->last_modified) == 0)
14662 GIVE_UP (6);
14663
14664 /* Verify that window is not hscrolled. */
14665 if (XFASTINT (w->hscroll) != 0)
14666 GIVE_UP (7);
14667
14668 /* Verify that display wasn't paused. */
14669 if (NILP (w->window_end_valid))
14670 GIVE_UP (8);
14671
14672 /* Can't use this if highlighting a region because a cursor movement
14673 will do more than just set the cursor. */
14674 if (!NILP (Vtransient_mark_mode)
14675 && !NILP (current_buffer->mark_active))
14676 GIVE_UP (9);
14677
14678 /* Likewise if highlighting trailing whitespace. */
14679 if (!NILP (Vshow_trailing_whitespace))
14680 GIVE_UP (11);
14681
14682 /* Likewise if showing a region. */
14683 if (!NILP (w->region_showing))
14684 GIVE_UP (10);
14685
14686 /* Can use this if overlay arrow position and or string have changed. */
14687 if (overlay_arrows_changed_p ())
14688 GIVE_UP (12);
14689
14690
14691 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
14692 only if buffer has really changed. The reason is that the gap is
14693 initially at Z for freshly visited files. The code below would
14694 set end_unchanged to 0 in that case. */
14695 if (MODIFF > SAVE_MODIFF
14696 /* This seems to happen sometimes after saving a buffer. */
14697 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
14698 {
14699 if (GPT - BEG < BEG_UNCHANGED)
14700 BEG_UNCHANGED = GPT - BEG;
14701 if (Z - GPT < END_UNCHANGED)
14702 END_UNCHANGED = Z - GPT;
14703 }
14704
14705 /* The position of the first and last character that has been changed. */
14706 first_changed_charpos = BEG + BEG_UNCHANGED;
14707 last_changed_charpos = Z - END_UNCHANGED;
14708
14709 /* If window starts after a line end, and the last change is in
14710 front of that newline, then changes don't affect the display.
14711 This case happens with stealth-fontification. Note that although
14712 the display is unchanged, glyph positions in the matrix have to
14713 be adjusted, of course. */
14714 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
14715 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
14716 && ((last_changed_charpos < CHARPOS (start)
14717 && CHARPOS (start) == BEGV)
14718 || (last_changed_charpos < CHARPOS (start) - 1
14719 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
14720 {
14721 int Z_old, delta, Z_BYTE_old, delta_bytes;
14722 struct glyph_row *r0;
14723
14724 /* Compute how many chars/bytes have been added to or removed
14725 from the buffer. */
14726 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
14727 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
14728 delta = Z - Z_old;
14729 delta_bytes = Z_BYTE - Z_BYTE_old;
14730
14731 /* Give up if PT is not in the window. Note that it already has
14732 been checked at the start of try_window_id that PT is not in
14733 front of the window start. */
14734 if (PT >= MATRIX_ROW_END_CHARPOS (row) + delta)
14735 GIVE_UP (13);
14736
14737 /* If window start is unchanged, we can reuse the whole matrix
14738 as is, after adjusting glyph positions. No need to compute
14739 the window end again, since its offset from Z hasn't changed. */
14740 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
14741 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + delta
14742 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + delta_bytes
14743 /* PT must not be in a partially visible line. */
14744 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + delta
14745 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
14746 {
14747 /* Adjust positions in the glyph matrix. */
14748 if (delta || delta_bytes)
14749 {
14750 struct glyph_row *r1
14751 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
14752 increment_matrix_positions (w->current_matrix,
14753 MATRIX_ROW_VPOS (r0, current_matrix),
14754 MATRIX_ROW_VPOS (r1, current_matrix),
14755 delta, delta_bytes);
14756 }
14757
14758 /* Set the cursor. */
14759 row = row_containing_pos (w, PT, r0, NULL, 0);
14760 if (row)
14761 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
14762 else
14763 abort ();
14764 return 1;
14765 }
14766 }
14767
14768 /* Handle the case that changes are all below what is displayed in
14769 the window, and that PT is in the window. This shortcut cannot
14770 be taken if ZV is visible in the window, and text has been added
14771 there that is visible in the window. */
14772 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
14773 /* ZV is not visible in the window, or there are no
14774 changes at ZV, actually. */
14775 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
14776 || first_changed_charpos == last_changed_charpos))
14777 {
14778 struct glyph_row *r0;
14779
14780 /* Give up if PT is not in the window. Note that it already has
14781 been checked at the start of try_window_id that PT is not in
14782 front of the window start. */
14783 if (PT >= MATRIX_ROW_END_CHARPOS (row))
14784 GIVE_UP (14);
14785
14786 /* If window start is unchanged, we can reuse the whole matrix
14787 as is, without changing glyph positions since no text has
14788 been added/removed in front of the window end. */
14789 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
14790 if (TEXT_POS_EQUAL_P (start, r0->start.pos)
14791 /* PT must not be in a partially visible line. */
14792 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
14793 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
14794 {
14795 /* We have to compute the window end anew since text
14796 can have been added/removed after it. */
14797 w->window_end_pos
14798 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
14799 w->window_end_bytepos
14800 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
14801
14802 /* Set the cursor. */
14803 row = row_containing_pos (w, PT, r0, NULL, 0);
14804 if (row)
14805 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
14806 else
14807 abort ();
14808 return 2;
14809 }
14810 }
14811
14812 /* Give up if window start is in the changed area.
14813
14814 The condition used to read
14815
14816 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
14817
14818 but why that was tested escapes me at the moment. */
14819 if (CHARPOS (start) >= first_changed_charpos
14820 && CHARPOS (start) <= last_changed_charpos)
14821 GIVE_UP (15);
14822
14823 /* Check that window start agrees with the start of the first glyph
14824 row in its current matrix. Check this after we know the window
14825 start is not in changed text, otherwise positions would not be
14826 comparable. */
14827 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
14828 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
14829 GIVE_UP (16);
14830
14831 /* Give up if the window ends in strings. Overlay strings
14832 at the end are difficult to handle, so don't try. */
14833 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
14834 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
14835 GIVE_UP (20);
14836
14837 /* Compute the position at which we have to start displaying new
14838 lines. Some of the lines at the top of the window might be
14839 reusable because they are not displaying changed text. Find the
14840 last row in W's current matrix not affected by changes at the
14841 start of current_buffer. Value is null if changes start in the
14842 first line of window. */
14843 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
14844 if (last_unchanged_at_beg_row)
14845 {
14846 /* Avoid starting to display in the moddle of a character, a TAB
14847 for instance. This is easier than to set up the iterator
14848 exactly, and it's not a frequent case, so the additional
14849 effort wouldn't really pay off. */
14850 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
14851 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
14852 && last_unchanged_at_beg_row > w->current_matrix->rows)
14853 --last_unchanged_at_beg_row;
14854
14855 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
14856 GIVE_UP (17);
14857
14858 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
14859 GIVE_UP (18);
14860 start_pos = it.current.pos;
14861
14862 /* Start displaying new lines in the desired matrix at the same
14863 vpos we would use in the current matrix, i.e. below
14864 last_unchanged_at_beg_row. */
14865 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
14866 current_matrix);
14867 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
14868 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
14869
14870 xassert (it.hpos == 0 && it.current_x == 0);
14871 }
14872 else
14873 {
14874 /* There are no reusable lines at the start of the window.
14875 Start displaying in the first text line. */
14876 start_display (&it, w, start);
14877 it.vpos = it.first_vpos;
14878 start_pos = it.current.pos;
14879 }
14880
14881 /* Find the first row that is not affected by changes at the end of
14882 the buffer. Value will be null if there is no unchanged row, in
14883 which case we must redisplay to the end of the window. delta
14884 will be set to the value by which buffer positions beginning with
14885 first_unchanged_at_end_row have to be adjusted due to text
14886 changes. */
14887 first_unchanged_at_end_row
14888 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
14889 IF_DEBUG (debug_delta = delta);
14890 IF_DEBUG (debug_delta_bytes = delta_bytes);
14891
14892 /* Set stop_pos to the buffer position up to which we will have to
14893 display new lines. If first_unchanged_at_end_row != NULL, this
14894 is the buffer position of the start of the line displayed in that
14895 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
14896 that we don't stop at a buffer position. */
14897 stop_pos = 0;
14898 if (first_unchanged_at_end_row)
14899 {
14900 xassert (last_unchanged_at_beg_row == NULL
14901 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
14902
14903 /* If this is a continuation line, move forward to the next one
14904 that isn't. Changes in lines above affect this line.
14905 Caution: this may move first_unchanged_at_end_row to a row
14906 not displaying text. */
14907 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
14908 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
14909 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
14910 < it.last_visible_y))
14911 ++first_unchanged_at_end_row;
14912
14913 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
14914 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
14915 >= it.last_visible_y))
14916 first_unchanged_at_end_row = NULL;
14917 else
14918 {
14919 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
14920 + delta);
14921 first_unchanged_at_end_vpos
14922 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
14923 xassert (stop_pos >= Z - END_UNCHANGED);
14924 }
14925 }
14926 else if (last_unchanged_at_beg_row == NULL)
14927 GIVE_UP (19);
14928
14929
14930 #if GLYPH_DEBUG
14931
14932 /* Either there is no unchanged row at the end, or the one we have
14933 now displays text. This is a necessary condition for the window
14934 end pos calculation at the end of this function. */
14935 xassert (first_unchanged_at_end_row == NULL
14936 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
14937
14938 debug_last_unchanged_at_beg_vpos
14939 = (last_unchanged_at_beg_row
14940 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
14941 : -1);
14942 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
14943
14944 #endif /* GLYPH_DEBUG != 0 */
14945
14946
14947 /* Display new lines. Set last_text_row to the last new line
14948 displayed which has text on it, i.e. might end up as being the
14949 line where the window_end_vpos is. */
14950 w->cursor.vpos = -1;
14951 last_text_row = NULL;
14952 overlay_arrow_seen = 0;
14953 while (it.current_y < it.last_visible_y
14954 && !fonts_changed_p
14955 && (first_unchanged_at_end_row == NULL
14956 || IT_CHARPOS (it) < stop_pos))
14957 {
14958 if (display_line (&it))
14959 last_text_row = it.glyph_row - 1;
14960 }
14961
14962 if (fonts_changed_p)
14963 return -1;
14964
14965
14966 /* Compute differences in buffer positions, y-positions etc. for
14967 lines reused at the bottom of the window. Compute what we can
14968 scroll. */
14969 if (first_unchanged_at_end_row
14970 /* No lines reused because we displayed everything up to the
14971 bottom of the window. */
14972 && it.current_y < it.last_visible_y)
14973 {
14974 dvpos = (it.vpos
14975 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
14976 current_matrix));
14977 dy = it.current_y - first_unchanged_at_end_row->y;
14978 run.current_y = first_unchanged_at_end_row->y;
14979 run.desired_y = run.current_y + dy;
14980 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
14981 }
14982 else
14983 {
14984 delta = dvpos = dy = run.current_y = run.desired_y = run.height = 0;
14985 first_unchanged_at_end_row = NULL;
14986 }
14987 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
14988
14989
14990 /* Find the cursor if not already found. We have to decide whether
14991 PT will appear on this window (it sometimes doesn't, but this is
14992 not a very frequent case.) This decision has to be made before
14993 the current matrix is altered. A value of cursor.vpos < 0 means
14994 that PT is either in one of the lines beginning at
14995 first_unchanged_at_end_row or below the window. Don't care for
14996 lines that might be displayed later at the window end; as
14997 mentioned, this is not a frequent case. */
14998 if (w->cursor.vpos < 0)
14999 {
15000 /* Cursor in unchanged rows at the top? */
15001 if (PT < CHARPOS (start_pos)
15002 && last_unchanged_at_beg_row)
15003 {
15004 row = row_containing_pos (w, PT,
15005 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
15006 last_unchanged_at_beg_row + 1, 0);
15007 if (row)
15008 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15009 }
15010
15011 /* Start from first_unchanged_at_end_row looking for PT. */
15012 else if (first_unchanged_at_end_row)
15013 {
15014 row = row_containing_pos (w, PT - delta,
15015 first_unchanged_at_end_row, NULL, 0);
15016 if (row)
15017 set_cursor_from_row (w, row, w->current_matrix, delta,
15018 delta_bytes, dy, dvpos);
15019 }
15020
15021 /* Give up if cursor was not found. */
15022 if (w->cursor.vpos < 0)
15023 {
15024 clear_glyph_matrix (w->desired_matrix);
15025 return -1;
15026 }
15027 }
15028
15029 /* Don't let the cursor end in the scroll margins. */
15030 {
15031 int this_scroll_margin, cursor_height;
15032
15033 this_scroll_margin = max (0, scroll_margin);
15034 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
15035 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
15036 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
15037
15038 if ((w->cursor.y < this_scroll_margin
15039 && CHARPOS (start) > BEGV)
15040 /* Old redisplay didn't take scroll margin into account at the bottom,
15041 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
15042 || (w->cursor.y + (make_cursor_line_fully_visible_p
15043 ? cursor_height + this_scroll_margin
15044 : 1)) > it.last_visible_y)
15045 {
15046 w->cursor.vpos = -1;
15047 clear_glyph_matrix (w->desired_matrix);
15048 return -1;
15049 }
15050 }
15051
15052 /* Scroll the display. Do it before changing the current matrix so
15053 that xterm.c doesn't get confused about where the cursor glyph is
15054 found. */
15055 if (dy && run.height)
15056 {
15057 update_begin (f);
15058
15059 if (FRAME_WINDOW_P (f))
15060 {
15061 FRAME_RIF (f)->update_window_begin_hook (w);
15062 FRAME_RIF (f)->clear_window_mouse_face (w);
15063 FRAME_RIF (f)->scroll_run_hook (w, &run);
15064 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
15065 }
15066 else
15067 {
15068 /* Terminal frame. In this case, dvpos gives the number of
15069 lines to scroll by; dvpos < 0 means scroll up. */
15070 int first_unchanged_at_end_vpos
15071 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
15072 int from = WINDOW_TOP_EDGE_LINE (w) + first_unchanged_at_end_vpos;
15073 int end = (WINDOW_TOP_EDGE_LINE (w)
15074 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
15075 + window_internal_height (w));
15076
15077 /* Perform the operation on the screen. */
15078 if (dvpos > 0)
15079 {
15080 /* Scroll last_unchanged_at_beg_row to the end of the
15081 window down dvpos lines. */
15082 set_terminal_window (f, end);
15083
15084 /* On dumb terminals delete dvpos lines at the end
15085 before inserting dvpos empty lines. */
15086 if (!FRAME_SCROLL_REGION_OK (f))
15087 ins_del_lines (f, end - dvpos, -dvpos);
15088
15089 /* Insert dvpos empty lines in front of
15090 last_unchanged_at_beg_row. */
15091 ins_del_lines (f, from, dvpos);
15092 }
15093 else if (dvpos < 0)
15094 {
15095 /* Scroll up last_unchanged_at_beg_vpos to the end of
15096 the window to last_unchanged_at_beg_vpos - |dvpos|. */
15097 set_terminal_window (f, end);
15098
15099 /* Delete dvpos lines in front of
15100 last_unchanged_at_beg_vpos. ins_del_lines will set
15101 the cursor to the given vpos and emit |dvpos| delete
15102 line sequences. */
15103 ins_del_lines (f, from + dvpos, dvpos);
15104
15105 /* On a dumb terminal insert dvpos empty lines at the
15106 end. */
15107 if (!FRAME_SCROLL_REGION_OK (f))
15108 ins_del_lines (f, end + dvpos, -dvpos);
15109 }
15110
15111 set_terminal_window (f, 0);
15112 }
15113
15114 update_end (f);
15115 }
15116
15117 /* Shift reused rows of the current matrix to the right position.
15118 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
15119 text. */
15120 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
15121 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
15122 if (dvpos < 0)
15123 {
15124 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
15125 bottom_vpos, dvpos);
15126 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
15127 bottom_vpos, 0);
15128 }
15129 else if (dvpos > 0)
15130 {
15131 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
15132 bottom_vpos, dvpos);
15133 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
15134 first_unchanged_at_end_vpos + dvpos, 0);
15135 }
15136
15137 /* For frame-based redisplay, make sure that current frame and window
15138 matrix are in sync with respect to glyph memory. */
15139 if (!FRAME_WINDOW_P (f))
15140 sync_frame_with_window_matrix_rows (w);
15141
15142 /* Adjust buffer positions in reused rows. */
15143 if (delta || delta_bytes)
15144 increment_matrix_positions (current_matrix,
15145 first_unchanged_at_end_vpos + dvpos,
15146 bottom_vpos, delta, delta_bytes);
15147
15148 /* Adjust Y positions. */
15149 if (dy)
15150 shift_glyph_matrix (w, current_matrix,
15151 first_unchanged_at_end_vpos + dvpos,
15152 bottom_vpos, dy);
15153
15154 if (first_unchanged_at_end_row)
15155 {
15156 first_unchanged_at_end_row += dvpos;
15157 if (first_unchanged_at_end_row->y >= it.last_visible_y
15158 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
15159 first_unchanged_at_end_row = NULL;
15160 }
15161
15162 /* If scrolling up, there may be some lines to display at the end of
15163 the window. */
15164 last_text_row_at_end = NULL;
15165 if (dy < 0)
15166 {
15167 /* Scrolling up can leave for example a partially visible line
15168 at the end of the window to be redisplayed. */
15169 /* Set last_row to the glyph row in the current matrix where the
15170 window end line is found. It has been moved up or down in
15171 the matrix by dvpos. */
15172 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
15173 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
15174
15175 /* If last_row is the window end line, it should display text. */
15176 xassert (last_row->displays_text_p);
15177
15178 /* If window end line was partially visible before, begin
15179 displaying at that line. Otherwise begin displaying with the
15180 line following it. */
15181 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
15182 {
15183 init_to_row_start (&it, w, last_row);
15184 it.vpos = last_vpos;
15185 it.current_y = last_row->y;
15186 }
15187 else
15188 {
15189 init_to_row_end (&it, w, last_row);
15190 it.vpos = 1 + last_vpos;
15191 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
15192 ++last_row;
15193 }
15194
15195 /* We may start in a continuation line. If so, we have to
15196 get the right continuation_lines_width and current_x. */
15197 it.continuation_lines_width = last_row->continuation_lines_width;
15198 it.hpos = it.current_x = 0;
15199
15200 /* Display the rest of the lines at the window end. */
15201 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
15202 while (it.current_y < it.last_visible_y
15203 && !fonts_changed_p)
15204 {
15205 /* Is it always sure that the display agrees with lines in
15206 the current matrix? I don't think so, so we mark rows
15207 displayed invalid in the current matrix by setting their
15208 enabled_p flag to zero. */
15209 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
15210 if (display_line (&it))
15211 last_text_row_at_end = it.glyph_row - 1;
15212 }
15213 }
15214
15215 /* Update window_end_pos and window_end_vpos. */
15216 if (first_unchanged_at_end_row
15217 && !last_text_row_at_end)
15218 {
15219 /* Window end line if one of the preserved rows from the current
15220 matrix. Set row to the last row displaying text in current
15221 matrix starting at first_unchanged_at_end_row, after
15222 scrolling. */
15223 xassert (first_unchanged_at_end_row->displays_text_p);
15224 row = find_last_row_displaying_text (w->current_matrix, &it,
15225 first_unchanged_at_end_row);
15226 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
15227
15228 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
15229 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
15230 w->window_end_vpos
15231 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
15232 xassert (w->window_end_bytepos >= 0);
15233 IF_DEBUG (debug_method_add (w, "A"));
15234 }
15235 else if (last_text_row_at_end)
15236 {
15237 w->window_end_pos
15238 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
15239 w->window_end_bytepos
15240 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
15241 w->window_end_vpos
15242 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
15243 xassert (w->window_end_bytepos >= 0);
15244 IF_DEBUG (debug_method_add (w, "B"));
15245 }
15246 else if (last_text_row)
15247 {
15248 /* We have displayed either to the end of the window or at the
15249 end of the window, i.e. the last row with text is to be found
15250 in the desired matrix. */
15251 w->window_end_pos
15252 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
15253 w->window_end_bytepos
15254 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
15255 w->window_end_vpos
15256 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
15257 xassert (w->window_end_bytepos >= 0);
15258 }
15259 else if (first_unchanged_at_end_row == NULL
15260 && last_text_row == NULL
15261 && last_text_row_at_end == NULL)
15262 {
15263 /* Displayed to end of window, but no line containing text was
15264 displayed. Lines were deleted at the end of the window. */
15265 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
15266 int vpos = XFASTINT (w->window_end_vpos);
15267 struct glyph_row *current_row = current_matrix->rows + vpos;
15268 struct glyph_row *desired_row = desired_matrix->rows + vpos;
15269
15270 for (row = NULL;
15271 row == NULL && vpos >= first_vpos;
15272 --vpos, --current_row, --desired_row)
15273 {
15274 if (desired_row->enabled_p)
15275 {
15276 if (desired_row->displays_text_p)
15277 row = desired_row;
15278 }
15279 else if (current_row->displays_text_p)
15280 row = current_row;
15281 }
15282
15283 xassert (row != NULL);
15284 w->window_end_vpos = make_number (vpos + 1);
15285 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
15286 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
15287 xassert (w->window_end_bytepos >= 0);
15288 IF_DEBUG (debug_method_add (w, "C"));
15289 }
15290 else
15291 abort ();
15292
15293 #if 0 /* This leads to problems, for instance when the cursor is
15294 at ZV, and the cursor line displays no text. */
15295 /* Disable rows below what's displayed in the window. This makes
15296 debugging easier. */
15297 enable_glyph_matrix_rows (current_matrix,
15298 XFASTINT (w->window_end_vpos) + 1,
15299 bottom_vpos, 0);
15300 #endif
15301
15302 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
15303 debug_end_vpos = XFASTINT (w->window_end_vpos));
15304
15305 /* Record that display has not been completed. */
15306 w->window_end_valid = Qnil;
15307 w->desired_matrix->no_scrolling_p = 1;
15308 return 3;
15309
15310 #undef GIVE_UP
15311 }
15312
15313
15314 \f
15315 /***********************************************************************
15316 More debugging support
15317 ***********************************************************************/
15318
15319 #if GLYPH_DEBUG
15320
15321 void dump_glyph_row P_ ((struct glyph_row *, int, int));
15322 void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
15323 void dump_glyph P_ ((struct glyph_row *, struct glyph *, int));
15324
15325
15326 /* Dump the contents of glyph matrix MATRIX on stderr.
15327
15328 GLYPHS 0 means don't show glyph contents.
15329 GLYPHS 1 means show glyphs in short form
15330 GLYPHS > 1 means show glyphs in long form. */
15331
15332 void
15333 dump_glyph_matrix (matrix, glyphs)
15334 struct glyph_matrix *matrix;
15335 int glyphs;
15336 {
15337 int i;
15338 for (i = 0; i < matrix->nrows; ++i)
15339 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
15340 }
15341
15342
15343 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
15344 the glyph row and area where the glyph comes from. */
15345
15346 void
15347 dump_glyph (row, glyph, area)
15348 struct glyph_row *row;
15349 struct glyph *glyph;
15350 int area;
15351 {
15352 if (glyph->type == CHAR_GLYPH)
15353 {
15354 fprintf (stderr,
15355 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15356 glyph - row->glyphs[TEXT_AREA],
15357 'C',
15358 glyph->charpos,
15359 (BUFFERP (glyph->object)
15360 ? 'B'
15361 : (STRINGP (glyph->object)
15362 ? 'S'
15363 : '-')),
15364 glyph->pixel_width,
15365 glyph->u.ch,
15366 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
15367 ? glyph->u.ch
15368 : '.'),
15369 glyph->face_id,
15370 glyph->left_box_line_p,
15371 glyph->right_box_line_p);
15372 }
15373 else if (glyph->type == STRETCH_GLYPH)
15374 {
15375 fprintf (stderr,
15376 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15377 glyph - row->glyphs[TEXT_AREA],
15378 'S',
15379 glyph->charpos,
15380 (BUFFERP (glyph->object)
15381 ? 'B'
15382 : (STRINGP (glyph->object)
15383 ? 'S'
15384 : '-')),
15385 glyph->pixel_width,
15386 0,
15387 '.',
15388 glyph->face_id,
15389 glyph->left_box_line_p,
15390 glyph->right_box_line_p);
15391 }
15392 else if (glyph->type == IMAGE_GLYPH)
15393 {
15394 fprintf (stderr,
15395 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15396 glyph - row->glyphs[TEXT_AREA],
15397 'I',
15398 glyph->charpos,
15399 (BUFFERP (glyph->object)
15400 ? 'B'
15401 : (STRINGP (glyph->object)
15402 ? 'S'
15403 : '-')),
15404 glyph->pixel_width,
15405 glyph->u.img_id,
15406 '.',
15407 glyph->face_id,
15408 glyph->left_box_line_p,
15409 glyph->right_box_line_p);
15410 }
15411 else if (glyph->type == COMPOSITE_GLYPH)
15412 {
15413 fprintf (stderr,
15414 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15415 glyph - row->glyphs[TEXT_AREA],
15416 '+',
15417 glyph->charpos,
15418 (BUFFERP (glyph->object)
15419 ? 'B'
15420 : (STRINGP (glyph->object)
15421 ? 'S'
15422 : '-')),
15423 glyph->pixel_width,
15424 glyph->u.cmp_id,
15425 '.',
15426 glyph->face_id,
15427 glyph->left_box_line_p,
15428 glyph->right_box_line_p);
15429 }
15430 }
15431
15432
15433 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
15434 GLYPHS 0 means don't show glyph contents.
15435 GLYPHS 1 means show glyphs in short form
15436 GLYPHS > 1 means show glyphs in long form. */
15437
15438 void
15439 dump_glyph_row (row, vpos, glyphs)
15440 struct glyph_row *row;
15441 int vpos, glyphs;
15442 {
15443 if (glyphs != 1)
15444 {
15445 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
15446 fprintf (stderr, "======================================================================\n");
15447
15448 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d\
15449 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
15450 vpos,
15451 MATRIX_ROW_START_CHARPOS (row),
15452 MATRIX_ROW_END_CHARPOS (row),
15453 row->used[TEXT_AREA],
15454 row->contains_overlapping_glyphs_p,
15455 row->enabled_p,
15456 row->truncated_on_left_p,
15457 row->truncated_on_right_p,
15458 row->continued_p,
15459 MATRIX_ROW_CONTINUATION_LINE_P (row),
15460 row->displays_text_p,
15461 row->ends_at_zv_p,
15462 row->fill_line_p,
15463 row->ends_in_middle_of_char_p,
15464 row->starts_in_middle_of_char_p,
15465 row->mouse_face_p,
15466 row->x,
15467 row->y,
15468 row->pixel_width,
15469 row->height,
15470 row->visible_height,
15471 row->ascent,
15472 row->phys_ascent);
15473 fprintf (stderr, "%9d %5d\t%5d\n", row->start.overlay_string_index,
15474 row->end.overlay_string_index,
15475 row->continuation_lines_width);
15476 fprintf (stderr, "%9d %5d\n",
15477 CHARPOS (row->start.string_pos),
15478 CHARPOS (row->end.string_pos));
15479 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
15480 row->end.dpvec_index);
15481 }
15482
15483 if (glyphs > 1)
15484 {
15485 int area;
15486
15487 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
15488 {
15489 struct glyph *glyph = row->glyphs[area];
15490 struct glyph *glyph_end = glyph + row->used[area];
15491
15492 /* Glyph for a line end in text. */
15493 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
15494 ++glyph_end;
15495
15496 if (glyph < glyph_end)
15497 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
15498
15499 for (; glyph < glyph_end; ++glyph)
15500 dump_glyph (row, glyph, area);
15501 }
15502 }
15503 else if (glyphs == 1)
15504 {
15505 int area;
15506
15507 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
15508 {
15509 char *s = (char *) alloca (row->used[area] + 1);
15510 int i;
15511
15512 for (i = 0; i < row->used[area]; ++i)
15513 {
15514 struct glyph *glyph = row->glyphs[area] + i;
15515 if (glyph->type == CHAR_GLYPH
15516 && glyph->u.ch < 0x80
15517 && glyph->u.ch >= ' ')
15518 s[i] = glyph->u.ch;
15519 else
15520 s[i] = '.';
15521 }
15522
15523 s[i] = '\0';
15524 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
15525 }
15526 }
15527 }
15528
15529
15530 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
15531 Sdump_glyph_matrix, 0, 1, "p",
15532 doc: /* Dump the current matrix of the selected window to stderr.
15533 Shows contents of glyph row structures. With non-nil
15534 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
15535 glyphs in short form, otherwise show glyphs in long form. */)
15536 (glyphs)
15537 Lisp_Object glyphs;
15538 {
15539 struct window *w = XWINDOW (selected_window);
15540 struct buffer *buffer = XBUFFER (w->buffer);
15541
15542 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
15543 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
15544 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
15545 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
15546 fprintf (stderr, "=============================================\n");
15547 dump_glyph_matrix (w->current_matrix,
15548 NILP (glyphs) ? 0 : XINT (glyphs));
15549 return Qnil;
15550 }
15551
15552
15553 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
15554 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
15555 ()
15556 {
15557 struct frame *f = XFRAME (selected_frame);
15558 dump_glyph_matrix (f->current_matrix, 1);
15559 return Qnil;
15560 }
15561
15562
15563 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
15564 doc: /* Dump glyph row ROW to stderr.
15565 GLYPH 0 means don't dump glyphs.
15566 GLYPH 1 means dump glyphs in short form.
15567 GLYPH > 1 or omitted means dump glyphs in long form. */)
15568 (row, glyphs)
15569 Lisp_Object row, glyphs;
15570 {
15571 struct glyph_matrix *matrix;
15572 int vpos;
15573
15574 CHECK_NUMBER (row);
15575 matrix = XWINDOW (selected_window)->current_matrix;
15576 vpos = XINT (row);
15577 if (vpos >= 0 && vpos < matrix->nrows)
15578 dump_glyph_row (MATRIX_ROW (matrix, vpos),
15579 vpos,
15580 INTEGERP (glyphs) ? XINT (glyphs) : 2);
15581 return Qnil;
15582 }
15583
15584
15585 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
15586 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
15587 GLYPH 0 means don't dump glyphs.
15588 GLYPH 1 means dump glyphs in short form.
15589 GLYPH > 1 or omitted means dump glyphs in long form. */)
15590 (row, glyphs)
15591 Lisp_Object row, glyphs;
15592 {
15593 struct frame *sf = SELECTED_FRAME ();
15594 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
15595 int vpos;
15596
15597 CHECK_NUMBER (row);
15598 vpos = XINT (row);
15599 if (vpos >= 0 && vpos < m->nrows)
15600 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
15601 INTEGERP (glyphs) ? XINT (glyphs) : 2);
15602 return Qnil;
15603 }
15604
15605
15606 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
15607 doc: /* Toggle tracing of redisplay.
15608 With ARG, turn tracing on if and only if ARG is positive. */)
15609 (arg)
15610 Lisp_Object arg;
15611 {
15612 if (NILP (arg))
15613 trace_redisplay_p = !trace_redisplay_p;
15614 else
15615 {
15616 arg = Fprefix_numeric_value (arg);
15617 trace_redisplay_p = XINT (arg) > 0;
15618 }
15619
15620 return Qnil;
15621 }
15622
15623
15624 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
15625 doc: /* Like `format', but print result to stderr.
15626 usage: (trace-to-stderr STRING &rest OBJECTS) */)
15627 (nargs, args)
15628 int nargs;
15629 Lisp_Object *args;
15630 {
15631 Lisp_Object s = Fformat (nargs, args);
15632 fprintf (stderr, "%s", SDATA (s));
15633 return Qnil;
15634 }
15635
15636 #endif /* GLYPH_DEBUG */
15637
15638
15639 \f
15640 /***********************************************************************
15641 Building Desired Matrix Rows
15642 ***********************************************************************/
15643
15644 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
15645 Used for non-window-redisplay windows, and for windows w/o left fringe. */
15646
15647 static struct glyph_row *
15648 get_overlay_arrow_glyph_row (w, overlay_arrow_string)
15649 struct window *w;
15650 Lisp_Object overlay_arrow_string;
15651 {
15652 struct frame *f = XFRAME (WINDOW_FRAME (w));
15653 struct buffer *buffer = XBUFFER (w->buffer);
15654 struct buffer *old = current_buffer;
15655 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
15656 int arrow_len = SCHARS (overlay_arrow_string);
15657 const unsigned char *arrow_end = arrow_string + arrow_len;
15658 const unsigned char *p;
15659 struct it it;
15660 int multibyte_p;
15661 int n_glyphs_before;
15662
15663 set_buffer_temp (buffer);
15664 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
15665 it.glyph_row->used[TEXT_AREA] = 0;
15666 SET_TEXT_POS (it.position, 0, 0);
15667
15668 multibyte_p = !NILP (buffer->enable_multibyte_characters);
15669 p = arrow_string;
15670 while (p < arrow_end)
15671 {
15672 Lisp_Object face, ilisp;
15673
15674 /* Get the next character. */
15675 if (multibyte_p)
15676 it.c = string_char_and_length (p, arrow_len, &it.len);
15677 else
15678 it.c = *p, it.len = 1;
15679 p += it.len;
15680
15681 /* Get its face. */
15682 ilisp = make_number (p - arrow_string);
15683 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
15684 it.face_id = compute_char_face (f, it.c, face);
15685
15686 /* Compute its width, get its glyphs. */
15687 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
15688 SET_TEXT_POS (it.position, -1, -1);
15689 PRODUCE_GLYPHS (&it);
15690
15691 /* If this character doesn't fit any more in the line, we have
15692 to remove some glyphs. */
15693 if (it.current_x > it.last_visible_x)
15694 {
15695 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
15696 break;
15697 }
15698 }
15699
15700 set_buffer_temp (old);
15701 return it.glyph_row;
15702 }
15703
15704
15705 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
15706 glyphs are only inserted for terminal frames since we can't really
15707 win with truncation glyphs when partially visible glyphs are
15708 involved. Which glyphs to insert is determined by
15709 produce_special_glyphs. */
15710
15711 static void
15712 insert_left_trunc_glyphs (it)
15713 struct it *it;
15714 {
15715 struct it truncate_it;
15716 struct glyph *from, *end, *to, *toend;
15717
15718 xassert (!FRAME_WINDOW_P (it->f));
15719
15720 /* Get the truncation glyphs. */
15721 truncate_it = *it;
15722 truncate_it.current_x = 0;
15723 truncate_it.face_id = DEFAULT_FACE_ID;
15724 truncate_it.glyph_row = &scratch_glyph_row;
15725 truncate_it.glyph_row->used[TEXT_AREA] = 0;
15726 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
15727 truncate_it.object = make_number (0);
15728 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
15729
15730 /* Overwrite glyphs from IT with truncation glyphs. */
15731 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
15732 end = from + truncate_it.glyph_row->used[TEXT_AREA];
15733 to = it->glyph_row->glyphs[TEXT_AREA];
15734 toend = to + it->glyph_row->used[TEXT_AREA];
15735
15736 while (from < end)
15737 *to++ = *from++;
15738
15739 /* There may be padding glyphs left over. Overwrite them too. */
15740 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
15741 {
15742 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
15743 while (from < end)
15744 *to++ = *from++;
15745 }
15746
15747 if (to > toend)
15748 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
15749 }
15750
15751
15752 /* Compute the pixel height and width of IT->glyph_row.
15753
15754 Most of the time, ascent and height of a display line will be equal
15755 to the max_ascent and max_height values of the display iterator
15756 structure. This is not the case if
15757
15758 1. We hit ZV without displaying anything. In this case, max_ascent
15759 and max_height will be zero.
15760
15761 2. We have some glyphs that don't contribute to the line height.
15762 (The glyph row flag contributes_to_line_height_p is for future
15763 pixmap extensions).
15764
15765 The first case is easily covered by using default values because in
15766 these cases, the line height does not really matter, except that it
15767 must not be zero. */
15768
15769 static void
15770 compute_line_metrics (it)
15771 struct it *it;
15772 {
15773 struct glyph_row *row = it->glyph_row;
15774 int area, i;
15775
15776 if (FRAME_WINDOW_P (it->f))
15777 {
15778 int i, min_y, max_y;
15779
15780 /* The line may consist of one space only, that was added to
15781 place the cursor on it. If so, the row's height hasn't been
15782 computed yet. */
15783 if (row->height == 0)
15784 {
15785 if (it->max_ascent + it->max_descent == 0)
15786 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
15787 row->ascent = it->max_ascent;
15788 row->height = it->max_ascent + it->max_descent;
15789 row->phys_ascent = it->max_phys_ascent;
15790 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
15791 row->extra_line_spacing = it->max_extra_line_spacing;
15792 }
15793
15794 /* Compute the width of this line. */
15795 row->pixel_width = row->x;
15796 for (i = 0; i < row->used[TEXT_AREA]; ++i)
15797 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
15798
15799 xassert (row->pixel_width >= 0);
15800 xassert (row->ascent >= 0 && row->height > 0);
15801
15802 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
15803 || MATRIX_ROW_OVERLAPS_PRED_P (row));
15804
15805 /* If first line's physical ascent is larger than its logical
15806 ascent, use the physical ascent, and make the row taller.
15807 This makes accented characters fully visible. */
15808 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
15809 && row->phys_ascent > row->ascent)
15810 {
15811 row->height += row->phys_ascent - row->ascent;
15812 row->ascent = row->phys_ascent;
15813 }
15814
15815 /* Compute how much of the line is visible. */
15816 row->visible_height = row->height;
15817
15818 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
15819 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
15820
15821 if (row->y < min_y)
15822 row->visible_height -= min_y - row->y;
15823 if (row->y + row->height > max_y)
15824 row->visible_height -= row->y + row->height - max_y;
15825 }
15826 else
15827 {
15828 row->pixel_width = row->used[TEXT_AREA];
15829 if (row->continued_p)
15830 row->pixel_width -= it->continuation_pixel_width;
15831 else if (row->truncated_on_right_p)
15832 row->pixel_width -= it->truncation_pixel_width;
15833 row->ascent = row->phys_ascent = 0;
15834 row->height = row->phys_height = row->visible_height = 1;
15835 row->extra_line_spacing = 0;
15836 }
15837
15838 /* Compute a hash code for this row. */
15839 row->hash = 0;
15840 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
15841 for (i = 0; i < row->used[area]; ++i)
15842 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
15843 + row->glyphs[area][i].u.val
15844 + row->glyphs[area][i].face_id
15845 + row->glyphs[area][i].padding_p
15846 + (row->glyphs[area][i].type << 2));
15847
15848 it->max_ascent = it->max_descent = 0;
15849 it->max_phys_ascent = it->max_phys_descent = 0;
15850 }
15851
15852
15853 /* Append one space to the glyph row of iterator IT if doing a
15854 window-based redisplay. The space has the same face as
15855 IT->face_id. Value is non-zero if a space was added.
15856
15857 This function is called to make sure that there is always one glyph
15858 at the end of a glyph row that the cursor can be set on under
15859 window-systems. (If there weren't such a glyph we would not know
15860 how wide and tall a box cursor should be displayed).
15861
15862 At the same time this space let's a nicely handle clearing to the
15863 end of the line if the row ends in italic text. */
15864
15865 static int
15866 append_space_for_newline (it, default_face_p)
15867 struct it *it;
15868 int default_face_p;
15869 {
15870 if (FRAME_WINDOW_P (it->f))
15871 {
15872 int n = it->glyph_row->used[TEXT_AREA];
15873
15874 if (it->glyph_row->glyphs[TEXT_AREA] + n
15875 < it->glyph_row->glyphs[1 + TEXT_AREA])
15876 {
15877 /* Save some values that must not be changed.
15878 Must save IT->c and IT->len because otherwise
15879 ITERATOR_AT_END_P wouldn't work anymore after
15880 append_space_for_newline has been called. */
15881 enum display_element_type saved_what = it->what;
15882 int saved_c = it->c, saved_len = it->len;
15883 int saved_x = it->current_x;
15884 int saved_face_id = it->face_id;
15885 struct text_pos saved_pos;
15886 Lisp_Object saved_object;
15887 struct face *face;
15888
15889 saved_object = it->object;
15890 saved_pos = it->position;
15891
15892 it->what = IT_CHARACTER;
15893 bzero (&it->position, sizeof it->position);
15894 it->object = make_number (0);
15895 it->c = ' ';
15896 it->len = 1;
15897
15898 if (default_face_p)
15899 it->face_id = DEFAULT_FACE_ID;
15900 else if (it->face_before_selective_p)
15901 it->face_id = it->saved_face_id;
15902 face = FACE_FROM_ID (it->f, it->face_id);
15903 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
15904
15905 PRODUCE_GLYPHS (it);
15906
15907 it->override_ascent = -1;
15908 it->constrain_row_ascent_descent_p = 0;
15909 it->current_x = saved_x;
15910 it->object = saved_object;
15911 it->position = saved_pos;
15912 it->what = saved_what;
15913 it->face_id = saved_face_id;
15914 it->len = saved_len;
15915 it->c = saved_c;
15916 return 1;
15917 }
15918 }
15919
15920 return 0;
15921 }
15922
15923
15924 /* Extend the face of the last glyph in the text area of IT->glyph_row
15925 to the end of the display line. Called from display_line.
15926 If the glyph row is empty, add a space glyph to it so that we
15927 know the face to draw. Set the glyph row flag fill_line_p. */
15928
15929 static void
15930 extend_face_to_end_of_line (it)
15931 struct it *it;
15932 {
15933 struct face *face;
15934 struct frame *f = it->f;
15935
15936 /* If line is already filled, do nothing. */
15937 if (it->current_x >= it->last_visible_x)
15938 return;
15939
15940 /* Face extension extends the background and box of IT->face_id
15941 to the end of the line. If the background equals the background
15942 of the frame, we don't have to do anything. */
15943 if (it->face_before_selective_p)
15944 face = FACE_FROM_ID (it->f, it->saved_face_id);
15945 else
15946 face = FACE_FROM_ID (f, it->face_id);
15947
15948 if (FRAME_WINDOW_P (f)
15949 && it->glyph_row->displays_text_p
15950 && face->box == FACE_NO_BOX
15951 && face->background == FRAME_BACKGROUND_PIXEL (f)
15952 && !face->stipple)
15953 return;
15954
15955 /* Set the glyph row flag indicating that the face of the last glyph
15956 in the text area has to be drawn to the end of the text area. */
15957 it->glyph_row->fill_line_p = 1;
15958
15959 /* If current character of IT is not ASCII, make sure we have the
15960 ASCII face. This will be automatically undone the next time
15961 get_next_display_element returns a multibyte character. Note
15962 that the character will always be single byte in unibyte text. */
15963 if (!ASCII_CHAR_P (it->c))
15964 {
15965 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
15966 }
15967
15968 if (FRAME_WINDOW_P (f))
15969 {
15970 /* If the row is empty, add a space with the current face of IT,
15971 so that we know which face to draw. */
15972 if (it->glyph_row->used[TEXT_AREA] == 0)
15973 {
15974 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
15975 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
15976 it->glyph_row->used[TEXT_AREA] = 1;
15977 }
15978 }
15979 else
15980 {
15981 /* Save some values that must not be changed. */
15982 int saved_x = it->current_x;
15983 struct text_pos saved_pos;
15984 Lisp_Object saved_object;
15985 enum display_element_type saved_what = it->what;
15986 int saved_face_id = it->face_id;
15987
15988 saved_object = it->object;
15989 saved_pos = it->position;
15990
15991 it->what = IT_CHARACTER;
15992 bzero (&it->position, sizeof it->position);
15993 it->object = make_number (0);
15994 it->c = ' ';
15995 it->len = 1;
15996 it->face_id = face->id;
15997
15998 PRODUCE_GLYPHS (it);
15999
16000 while (it->current_x <= it->last_visible_x)
16001 PRODUCE_GLYPHS (it);
16002
16003 /* Don't count these blanks really. It would let us insert a left
16004 truncation glyph below and make us set the cursor on them, maybe. */
16005 it->current_x = saved_x;
16006 it->object = saved_object;
16007 it->position = saved_pos;
16008 it->what = saved_what;
16009 it->face_id = saved_face_id;
16010 }
16011 }
16012
16013
16014 /* Value is non-zero if text starting at CHARPOS in current_buffer is
16015 trailing whitespace. */
16016
16017 static int
16018 trailing_whitespace_p (charpos)
16019 int charpos;
16020 {
16021 int bytepos = CHAR_TO_BYTE (charpos);
16022 int c = 0;
16023
16024 while (bytepos < ZV_BYTE
16025 && (c = FETCH_CHAR (bytepos),
16026 c == ' ' || c == '\t'))
16027 ++bytepos;
16028
16029 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
16030 {
16031 if (bytepos != PT_BYTE)
16032 return 1;
16033 }
16034 return 0;
16035 }
16036
16037
16038 /* Highlight trailing whitespace, if any, in ROW. */
16039
16040 void
16041 highlight_trailing_whitespace (f, row)
16042 struct frame *f;
16043 struct glyph_row *row;
16044 {
16045 int used = row->used[TEXT_AREA];
16046
16047 if (used)
16048 {
16049 struct glyph *start = row->glyphs[TEXT_AREA];
16050 struct glyph *glyph = start + used - 1;
16051
16052 /* Skip over glyphs inserted to display the cursor at the
16053 end of a line, for extending the face of the last glyph
16054 to the end of the line on terminals, and for truncation
16055 and continuation glyphs. */
16056 while (glyph >= start
16057 && glyph->type == CHAR_GLYPH
16058 && INTEGERP (glyph->object))
16059 --glyph;
16060
16061 /* If last glyph is a space or stretch, and it's trailing
16062 whitespace, set the face of all trailing whitespace glyphs in
16063 IT->glyph_row to `trailing-whitespace'. */
16064 if (glyph >= start
16065 && BUFFERP (glyph->object)
16066 && (glyph->type == STRETCH_GLYPH
16067 || (glyph->type == CHAR_GLYPH
16068 && glyph->u.ch == ' '))
16069 && trailing_whitespace_p (glyph->charpos))
16070 {
16071 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
16072 if (face_id < 0)
16073 return;
16074
16075 while (glyph >= start
16076 && BUFFERP (glyph->object)
16077 && (glyph->type == STRETCH_GLYPH
16078 || (glyph->type == CHAR_GLYPH
16079 && glyph->u.ch == ' ')))
16080 (glyph--)->face_id = face_id;
16081 }
16082 }
16083 }
16084
16085
16086 /* Value is non-zero if glyph row ROW in window W should be
16087 used to hold the cursor. */
16088
16089 static int
16090 cursor_row_p (w, row)
16091 struct window *w;
16092 struct glyph_row *row;
16093 {
16094 int cursor_row_p = 1;
16095
16096 if (PT == MATRIX_ROW_END_CHARPOS (row))
16097 {
16098 /* Suppose the row ends on a string.
16099 Unless the row is continued, that means it ends on a newline
16100 in the string. If it's anything other than a display string
16101 (e.g. a before-string from an overlay), we don't want the
16102 cursor there. (This heuristic seems to give the optimal
16103 behavior for the various types of multi-line strings.) */
16104 if (CHARPOS (row->end.string_pos) >= 0)
16105 {
16106 if (row->continued_p)
16107 cursor_row_p = 1;
16108 else
16109 {
16110 /* Check for `display' property. */
16111 struct glyph *beg = row->glyphs[TEXT_AREA];
16112 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
16113 struct glyph *glyph;
16114
16115 cursor_row_p = 0;
16116 for (glyph = end; glyph >= beg; --glyph)
16117 if (STRINGP (glyph->object))
16118 {
16119 Lisp_Object prop
16120 = Fget_char_property (make_number (PT),
16121 Qdisplay, Qnil);
16122 cursor_row_p =
16123 (!NILP (prop)
16124 && display_prop_string_p (prop, glyph->object));
16125 break;
16126 }
16127 }
16128 }
16129 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
16130 {
16131 /* If the row ends in middle of a real character,
16132 and the line is continued, we want the cursor here.
16133 That's because MATRIX_ROW_END_CHARPOS would equal
16134 PT if PT is before the character. */
16135 if (!row->ends_in_ellipsis_p)
16136 cursor_row_p = row->continued_p;
16137 else
16138 /* If the row ends in an ellipsis, then
16139 MATRIX_ROW_END_CHARPOS will equal point after the invisible text.
16140 We want that position to be displayed after the ellipsis. */
16141 cursor_row_p = 0;
16142 }
16143 /* If the row ends at ZV, display the cursor at the end of that
16144 row instead of at the start of the row below. */
16145 else if (row->ends_at_zv_p)
16146 cursor_row_p = 1;
16147 else
16148 cursor_row_p = 0;
16149 }
16150
16151 return cursor_row_p;
16152 }
16153
16154
16155 /* Construct the glyph row IT->glyph_row in the desired matrix of
16156 IT->w from text at the current position of IT. See dispextern.h
16157 for an overview of struct it. Value is non-zero if
16158 IT->glyph_row displays text, as opposed to a line displaying ZV
16159 only. */
16160
16161 static int
16162 display_line (it)
16163 struct it *it;
16164 {
16165 struct glyph_row *row = it->glyph_row;
16166 Lisp_Object overlay_arrow_string;
16167
16168 /* We always start displaying at hpos zero even if hscrolled. */
16169 xassert (it->hpos == 0 && it->current_x == 0);
16170
16171 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
16172 >= it->w->desired_matrix->nrows)
16173 {
16174 it->w->nrows_scale_factor++;
16175 fonts_changed_p = 1;
16176 return 0;
16177 }
16178
16179 /* Is IT->w showing the region? */
16180 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
16181
16182 /* Clear the result glyph row and enable it. */
16183 prepare_desired_row (row);
16184
16185 row->y = it->current_y;
16186 row->start = it->start;
16187 row->continuation_lines_width = it->continuation_lines_width;
16188 row->displays_text_p = 1;
16189 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
16190 it->starts_in_middle_of_char_p = 0;
16191
16192 /* Arrange the overlays nicely for our purposes. Usually, we call
16193 display_line on only one line at a time, in which case this
16194 can't really hurt too much, or we call it on lines which appear
16195 one after another in the buffer, in which case all calls to
16196 recenter_overlay_lists but the first will be pretty cheap. */
16197 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
16198
16199 /* Move over display elements that are not visible because we are
16200 hscrolled. This may stop at an x-position < IT->first_visible_x
16201 if the first glyph is partially visible or if we hit a line end. */
16202 if (it->current_x < it->first_visible_x)
16203 {
16204 move_it_in_display_line_to (it, ZV, it->first_visible_x,
16205 MOVE_TO_POS | MOVE_TO_X);
16206 }
16207
16208 /* Get the initial row height. This is either the height of the
16209 text hscrolled, if there is any, or zero. */
16210 row->ascent = it->max_ascent;
16211 row->height = it->max_ascent + it->max_descent;
16212 row->phys_ascent = it->max_phys_ascent;
16213 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
16214 row->extra_line_spacing = it->max_extra_line_spacing;
16215
16216 /* Loop generating characters. The loop is left with IT on the next
16217 character to display. */
16218 while (1)
16219 {
16220 int n_glyphs_before, hpos_before, x_before;
16221 int x, i, nglyphs;
16222 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
16223
16224 /* Retrieve the next thing to display. Value is zero if end of
16225 buffer reached. */
16226 if (!get_next_display_element (it))
16227 {
16228 /* Maybe add a space at the end of this line that is used to
16229 display the cursor there under X. Set the charpos of the
16230 first glyph of blank lines not corresponding to any text
16231 to -1. */
16232 #ifdef HAVE_WINDOW_SYSTEM
16233 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16234 row->exact_window_width_line_p = 1;
16235 else
16236 #endif /* HAVE_WINDOW_SYSTEM */
16237 if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
16238 || row->used[TEXT_AREA] == 0)
16239 {
16240 row->glyphs[TEXT_AREA]->charpos = -1;
16241 row->displays_text_p = 0;
16242
16243 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines)
16244 && (!MINI_WINDOW_P (it->w)
16245 || (minibuf_level && EQ (it->window, minibuf_window))))
16246 row->indicate_empty_line_p = 1;
16247 }
16248
16249 it->continuation_lines_width = 0;
16250 row->ends_at_zv_p = 1;
16251 break;
16252 }
16253
16254 /* Now, get the metrics of what we want to display. This also
16255 generates glyphs in `row' (which is IT->glyph_row). */
16256 n_glyphs_before = row->used[TEXT_AREA];
16257 x = it->current_x;
16258
16259 /* Remember the line height so far in case the next element doesn't
16260 fit on the line. */
16261 if (!it->truncate_lines_p)
16262 {
16263 ascent = it->max_ascent;
16264 descent = it->max_descent;
16265 phys_ascent = it->max_phys_ascent;
16266 phys_descent = it->max_phys_descent;
16267 }
16268
16269 PRODUCE_GLYPHS (it);
16270
16271 /* If this display element was in marginal areas, continue with
16272 the next one. */
16273 if (it->area != TEXT_AREA)
16274 {
16275 row->ascent = max (row->ascent, it->max_ascent);
16276 row->height = max (row->height, it->max_ascent + it->max_descent);
16277 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
16278 row->phys_height = max (row->phys_height,
16279 it->max_phys_ascent + it->max_phys_descent);
16280 row->extra_line_spacing = max (row->extra_line_spacing,
16281 it->max_extra_line_spacing);
16282 set_iterator_to_next (it, 1);
16283 continue;
16284 }
16285
16286 /* Does the display element fit on the line? If we truncate
16287 lines, we should draw past the right edge of the window. If
16288 we don't truncate, we want to stop so that we can display the
16289 continuation glyph before the right margin. If lines are
16290 continued, there are two possible strategies for characters
16291 resulting in more than 1 glyph (e.g. tabs): Display as many
16292 glyphs as possible in this line and leave the rest for the
16293 continuation line, or display the whole element in the next
16294 line. Original redisplay did the former, so we do it also. */
16295 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
16296 hpos_before = it->hpos;
16297 x_before = x;
16298
16299 if (/* Not a newline. */
16300 nglyphs > 0
16301 /* Glyphs produced fit entirely in the line. */
16302 && it->current_x < it->last_visible_x)
16303 {
16304 it->hpos += nglyphs;
16305 row->ascent = max (row->ascent, it->max_ascent);
16306 row->height = max (row->height, it->max_ascent + it->max_descent);
16307 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
16308 row->phys_height = max (row->phys_height,
16309 it->max_phys_ascent + it->max_phys_descent);
16310 row->extra_line_spacing = max (row->extra_line_spacing,
16311 it->max_extra_line_spacing);
16312 if (it->current_x - it->pixel_width < it->first_visible_x)
16313 row->x = x - it->first_visible_x;
16314 }
16315 else
16316 {
16317 int new_x;
16318 struct glyph *glyph;
16319
16320 for (i = 0; i < nglyphs; ++i, x = new_x)
16321 {
16322 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
16323 new_x = x + glyph->pixel_width;
16324
16325 if (/* Lines are continued. */
16326 !it->truncate_lines_p
16327 && (/* Glyph doesn't fit on the line. */
16328 new_x > it->last_visible_x
16329 /* Or it fits exactly on a window system frame. */
16330 || (new_x == it->last_visible_x
16331 && FRAME_WINDOW_P (it->f))))
16332 {
16333 /* End of a continued line. */
16334
16335 if (it->hpos == 0
16336 || (new_x == it->last_visible_x
16337 && FRAME_WINDOW_P (it->f)))
16338 {
16339 /* Current glyph is the only one on the line or
16340 fits exactly on the line. We must continue
16341 the line because we can't draw the cursor
16342 after the glyph. */
16343 row->continued_p = 1;
16344 it->current_x = new_x;
16345 it->continuation_lines_width += new_x;
16346 ++it->hpos;
16347 if (i == nglyphs - 1)
16348 {
16349 set_iterator_to_next (it, 1);
16350 #ifdef HAVE_WINDOW_SYSTEM
16351 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16352 {
16353 if (!get_next_display_element (it))
16354 {
16355 row->exact_window_width_line_p = 1;
16356 it->continuation_lines_width = 0;
16357 row->continued_p = 0;
16358 row->ends_at_zv_p = 1;
16359 }
16360 else if (ITERATOR_AT_END_OF_LINE_P (it))
16361 {
16362 row->continued_p = 0;
16363 row->exact_window_width_line_p = 1;
16364 }
16365 }
16366 #endif /* HAVE_WINDOW_SYSTEM */
16367 }
16368 }
16369 else if (CHAR_GLYPH_PADDING_P (*glyph)
16370 && !FRAME_WINDOW_P (it->f))
16371 {
16372 /* A padding glyph that doesn't fit on this line.
16373 This means the whole character doesn't fit
16374 on the line. */
16375 row->used[TEXT_AREA] = n_glyphs_before;
16376
16377 /* Fill the rest of the row with continuation
16378 glyphs like in 20.x. */
16379 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
16380 < row->glyphs[1 + TEXT_AREA])
16381 produce_special_glyphs (it, IT_CONTINUATION);
16382
16383 row->continued_p = 1;
16384 it->current_x = x_before;
16385 it->continuation_lines_width += x_before;
16386
16387 /* Restore the height to what it was before the
16388 element not fitting on the line. */
16389 it->max_ascent = ascent;
16390 it->max_descent = descent;
16391 it->max_phys_ascent = phys_ascent;
16392 it->max_phys_descent = phys_descent;
16393 }
16394 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
16395 {
16396 /* A TAB that extends past the right edge of the
16397 window. This produces a single glyph on
16398 window system frames. We leave the glyph in
16399 this row and let it fill the row, but don't
16400 consume the TAB. */
16401 it->continuation_lines_width += it->last_visible_x;
16402 row->ends_in_middle_of_char_p = 1;
16403 row->continued_p = 1;
16404 glyph->pixel_width = it->last_visible_x - x;
16405 it->starts_in_middle_of_char_p = 1;
16406 }
16407 else
16408 {
16409 /* Something other than a TAB that draws past
16410 the right edge of the window. Restore
16411 positions to values before the element. */
16412 row->used[TEXT_AREA] = n_glyphs_before + i;
16413
16414 /* Display continuation glyphs. */
16415 if (!FRAME_WINDOW_P (it->f))
16416 produce_special_glyphs (it, IT_CONTINUATION);
16417 row->continued_p = 1;
16418
16419 it->current_x = x_before;
16420 it->continuation_lines_width += x;
16421 extend_face_to_end_of_line (it);
16422
16423 if (nglyphs > 1 && i > 0)
16424 {
16425 row->ends_in_middle_of_char_p = 1;
16426 it->starts_in_middle_of_char_p = 1;
16427 }
16428
16429 /* Restore the height to what it was before the
16430 element not fitting on the line. */
16431 it->max_ascent = ascent;
16432 it->max_descent = descent;
16433 it->max_phys_ascent = phys_ascent;
16434 it->max_phys_descent = phys_descent;
16435 }
16436
16437 break;
16438 }
16439 else if (new_x > it->first_visible_x)
16440 {
16441 /* Increment number of glyphs actually displayed. */
16442 ++it->hpos;
16443
16444 if (x < it->first_visible_x)
16445 /* Glyph is partially visible, i.e. row starts at
16446 negative X position. */
16447 row->x = x - it->first_visible_x;
16448 }
16449 else
16450 {
16451 /* Glyph is completely off the left margin of the
16452 window. This should not happen because of the
16453 move_it_in_display_line at the start of this
16454 function, unless the text display area of the
16455 window is empty. */
16456 xassert (it->first_visible_x <= it->last_visible_x);
16457 }
16458 }
16459
16460 row->ascent = max (row->ascent, it->max_ascent);
16461 row->height = max (row->height, it->max_ascent + it->max_descent);
16462 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
16463 row->phys_height = max (row->phys_height,
16464 it->max_phys_ascent + it->max_phys_descent);
16465 row->extra_line_spacing = max (row->extra_line_spacing,
16466 it->max_extra_line_spacing);
16467
16468 /* End of this display line if row is continued. */
16469 if (row->continued_p || row->ends_at_zv_p)
16470 break;
16471 }
16472
16473 at_end_of_line:
16474 /* Is this a line end? If yes, we're also done, after making
16475 sure that a non-default face is extended up to the right
16476 margin of the window. */
16477 if (ITERATOR_AT_END_OF_LINE_P (it))
16478 {
16479 int used_before = row->used[TEXT_AREA];
16480
16481 row->ends_in_newline_from_string_p = STRINGP (it->object);
16482
16483 #ifdef HAVE_WINDOW_SYSTEM
16484 /* Add a space at the end of the line that is used to
16485 display the cursor there. */
16486 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16487 append_space_for_newline (it, 0);
16488 #endif /* HAVE_WINDOW_SYSTEM */
16489
16490 /* Extend the face to the end of the line. */
16491 extend_face_to_end_of_line (it);
16492
16493 /* Make sure we have the position. */
16494 if (used_before == 0)
16495 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
16496
16497 /* Consume the line end. This skips over invisible lines. */
16498 set_iterator_to_next (it, 1);
16499 it->continuation_lines_width = 0;
16500 break;
16501 }
16502
16503 /* Proceed with next display element. Note that this skips
16504 over lines invisible because of selective display. */
16505 set_iterator_to_next (it, 1);
16506
16507 /* If we truncate lines, we are done when the last displayed
16508 glyphs reach past the right margin of the window. */
16509 if (it->truncate_lines_p
16510 && (FRAME_WINDOW_P (it->f)
16511 ? (it->current_x >= it->last_visible_x)
16512 : (it->current_x > it->last_visible_x)))
16513 {
16514 /* Maybe add truncation glyphs. */
16515 if (!FRAME_WINDOW_P (it->f))
16516 {
16517 int i, n;
16518
16519 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
16520 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
16521 break;
16522
16523 for (n = row->used[TEXT_AREA]; i < n; ++i)
16524 {
16525 row->used[TEXT_AREA] = i;
16526 produce_special_glyphs (it, IT_TRUNCATION);
16527 }
16528 }
16529 #ifdef HAVE_WINDOW_SYSTEM
16530 else
16531 {
16532 /* Don't truncate if we can overflow newline into fringe. */
16533 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16534 {
16535 if (!get_next_display_element (it))
16536 {
16537 it->continuation_lines_width = 0;
16538 row->ends_at_zv_p = 1;
16539 row->exact_window_width_line_p = 1;
16540 break;
16541 }
16542 if (ITERATOR_AT_END_OF_LINE_P (it))
16543 {
16544 row->exact_window_width_line_p = 1;
16545 goto at_end_of_line;
16546 }
16547 }
16548 }
16549 #endif /* HAVE_WINDOW_SYSTEM */
16550
16551 row->truncated_on_right_p = 1;
16552 it->continuation_lines_width = 0;
16553 reseat_at_next_visible_line_start (it, 0);
16554 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
16555 it->hpos = hpos_before;
16556 it->current_x = x_before;
16557 break;
16558 }
16559 }
16560
16561 /* If line is not empty and hscrolled, maybe insert truncation glyphs
16562 at the left window margin. */
16563 if (it->first_visible_x
16564 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
16565 {
16566 if (!FRAME_WINDOW_P (it->f))
16567 insert_left_trunc_glyphs (it);
16568 row->truncated_on_left_p = 1;
16569 }
16570
16571 /* If the start of this line is the overlay arrow-position, then
16572 mark this glyph row as the one containing the overlay arrow.
16573 This is clearly a mess with variable size fonts. It would be
16574 better to let it be displayed like cursors under X. */
16575 if ((row->displays_text_p || !overlay_arrow_seen)
16576 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
16577 !NILP (overlay_arrow_string)))
16578 {
16579 /* Overlay arrow in window redisplay is a fringe bitmap. */
16580 if (STRINGP (overlay_arrow_string))
16581 {
16582 struct glyph_row *arrow_row
16583 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
16584 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
16585 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
16586 struct glyph *p = row->glyphs[TEXT_AREA];
16587 struct glyph *p2, *end;
16588
16589 /* Copy the arrow glyphs. */
16590 while (glyph < arrow_end)
16591 *p++ = *glyph++;
16592
16593 /* Throw away padding glyphs. */
16594 p2 = p;
16595 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
16596 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
16597 ++p2;
16598 if (p2 > p)
16599 {
16600 while (p2 < end)
16601 *p++ = *p2++;
16602 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
16603 }
16604 }
16605 else
16606 {
16607 xassert (INTEGERP (overlay_arrow_string));
16608 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
16609 }
16610 overlay_arrow_seen = 1;
16611 }
16612
16613 /* Compute pixel dimensions of this line. */
16614 compute_line_metrics (it);
16615
16616 /* Remember the position at which this line ends. */
16617 row->end = it->current;
16618
16619 /* Record whether this row ends inside an ellipsis. */
16620 row->ends_in_ellipsis_p
16621 = (it->method == GET_FROM_DISPLAY_VECTOR
16622 && it->ellipsis_p);
16623
16624 /* Save fringe bitmaps in this row. */
16625 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
16626 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
16627 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
16628 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
16629
16630 it->left_user_fringe_bitmap = 0;
16631 it->left_user_fringe_face_id = 0;
16632 it->right_user_fringe_bitmap = 0;
16633 it->right_user_fringe_face_id = 0;
16634
16635 /* Maybe set the cursor. */
16636 if (it->w->cursor.vpos < 0
16637 && PT >= MATRIX_ROW_START_CHARPOS (row)
16638 && PT <= MATRIX_ROW_END_CHARPOS (row)
16639 && cursor_row_p (it->w, row))
16640 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
16641
16642 /* Highlight trailing whitespace. */
16643 if (!NILP (Vshow_trailing_whitespace))
16644 highlight_trailing_whitespace (it->f, it->glyph_row);
16645
16646 /* Prepare for the next line. This line starts horizontally at (X
16647 HPOS) = (0 0). Vertical positions are incremented. As a
16648 convenience for the caller, IT->glyph_row is set to the next
16649 row to be used. */
16650 it->current_x = it->hpos = 0;
16651 it->current_y += row->height;
16652 ++it->vpos;
16653 ++it->glyph_row;
16654 it->start = it->current;
16655 return row->displays_text_p;
16656 }
16657
16658
16659 \f
16660 /***********************************************************************
16661 Menu Bar
16662 ***********************************************************************/
16663
16664 /* Redisplay the menu bar in the frame for window W.
16665
16666 The menu bar of X frames that don't have X toolkit support is
16667 displayed in a special window W->frame->menu_bar_window.
16668
16669 The menu bar of terminal frames is treated specially as far as
16670 glyph matrices are concerned. Menu bar lines are not part of
16671 windows, so the update is done directly on the frame matrix rows
16672 for the menu bar. */
16673
16674 static void
16675 display_menu_bar (w)
16676 struct window *w;
16677 {
16678 struct frame *f = XFRAME (WINDOW_FRAME (w));
16679 struct it it;
16680 Lisp_Object items;
16681 int i;
16682
16683 /* Don't do all this for graphical frames. */
16684 #ifdef HAVE_NTGUI
16685 if (FRAME_W32_P (f))
16686 return;
16687 #endif
16688 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
16689 if (FRAME_X_P (f))
16690 return;
16691 #endif
16692 #ifdef MAC_OS
16693 if (FRAME_MAC_P (f))
16694 return;
16695 #endif
16696
16697 #ifdef USE_X_TOOLKIT
16698 xassert (!FRAME_WINDOW_P (f));
16699 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
16700 it.first_visible_x = 0;
16701 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
16702 #else /* not USE_X_TOOLKIT */
16703 if (FRAME_WINDOW_P (f))
16704 {
16705 /* Menu bar lines are displayed in the desired matrix of the
16706 dummy window menu_bar_window. */
16707 struct window *menu_w;
16708 xassert (WINDOWP (f->menu_bar_window));
16709 menu_w = XWINDOW (f->menu_bar_window);
16710 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
16711 MENU_FACE_ID);
16712 it.first_visible_x = 0;
16713 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
16714 }
16715 else
16716 {
16717 /* This is a TTY frame, i.e. character hpos/vpos are used as
16718 pixel x/y. */
16719 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
16720 MENU_FACE_ID);
16721 it.first_visible_x = 0;
16722 it.last_visible_x = FRAME_COLS (f);
16723 }
16724 #endif /* not USE_X_TOOLKIT */
16725
16726 if (! mode_line_inverse_video)
16727 /* Force the menu-bar to be displayed in the default face. */
16728 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
16729
16730 /* Clear all rows of the menu bar. */
16731 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
16732 {
16733 struct glyph_row *row = it.glyph_row + i;
16734 clear_glyph_row (row);
16735 row->enabled_p = 1;
16736 row->full_width_p = 1;
16737 }
16738
16739 /* Display all items of the menu bar. */
16740 items = FRAME_MENU_BAR_ITEMS (it.f);
16741 for (i = 0; i < XVECTOR (items)->size; i += 4)
16742 {
16743 Lisp_Object string;
16744
16745 /* Stop at nil string. */
16746 string = AREF (items, i + 1);
16747 if (NILP (string))
16748 break;
16749
16750 /* Remember where item was displayed. */
16751 AREF (items, i + 3) = make_number (it.hpos);
16752
16753 /* Display the item, pad with one space. */
16754 if (it.current_x < it.last_visible_x)
16755 display_string (NULL, string, Qnil, 0, 0, &it,
16756 SCHARS (string) + 1, 0, 0, -1);
16757 }
16758
16759 /* Fill out the line with spaces. */
16760 if (it.current_x < it.last_visible_x)
16761 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
16762
16763 /* Compute the total height of the lines. */
16764 compute_line_metrics (&it);
16765 }
16766
16767
16768 \f
16769 /***********************************************************************
16770 Mode Line
16771 ***********************************************************************/
16772
16773 /* Redisplay mode lines in the window tree whose root is WINDOW. If
16774 FORCE is non-zero, redisplay mode lines unconditionally.
16775 Otherwise, redisplay only mode lines that are garbaged. Value is
16776 the number of windows whose mode lines were redisplayed. */
16777
16778 static int
16779 redisplay_mode_lines (window, force)
16780 Lisp_Object window;
16781 int force;
16782 {
16783 int nwindows = 0;
16784
16785 while (!NILP (window))
16786 {
16787 struct window *w = XWINDOW (window);
16788
16789 if (WINDOWP (w->hchild))
16790 nwindows += redisplay_mode_lines (w->hchild, force);
16791 else if (WINDOWP (w->vchild))
16792 nwindows += redisplay_mode_lines (w->vchild, force);
16793 else if (force
16794 || FRAME_GARBAGED_P (XFRAME (w->frame))
16795 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
16796 {
16797 struct text_pos lpoint;
16798 struct buffer *old = current_buffer;
16799
16800 /* Set the window's buffer for the mode line display. */
16801 SET_TEXT_POS (lpoint, PT, PT_BYTE);
16802 set_buffer_internal_1 (XBUFFER (w->buffer));
16803
16804 /* Point refers normally to the selected window. For any
16805 other window, set up appropriate value. */
16806 if (!EQ (window, selected_window))
16807 {
16808 struct text_pos pt;
16809
16810 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
16811 if (CHARPOS (pt) < BEGV)
16812 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
16813 else if (CHARPOS (pt) > (ZV - 1))
16814 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
16815 else
16816 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
16817 }
16818
16819 /* Display mode lines. */
16820 clear_glyph_matrix (w->desired_matrix);
16821 if (display_mode_lines (w))
16822 {
16823 ++nwindows;
16824 w->must_be_updated_p = 1;
16825 }
16826
16827 /* Restore old settings. */
16828 set_buffer_internal_1 (old);
16829 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
16830 }
16831
16832 window = w->next;
16833 }
16834
16835 return nwindows;
16836 }
16837
16838
16839 /* Display the mode and/or top line of window W. Value is the number
16840 of mode lines displayed. */
16841
16842 static int
16843 display_mode_lines (w)
16844 struct window *w;
16845 {
16846 Lisp_Object old_selected_window, old_selected_frame;
16847 int n = 0;
16848
16849 old_selected_frame = selected_frame;
16850 selected_frame = w->frame;
16851 old_selected_window = selected_window;
16852 XSETWINDOW (selected_window, w);
16853
16854 /* These will be set while the mode line specs are processed. */
16855 line_number_displayed = 0;
16856 w->column_number_displayed = Qnil;
16857
16858 if (WINDOW_WANTS_MODELINE_P (w))
16859 {
16860 struct window *sel_w = XWINDOW (old_selected_window);
16861
16862 /* Select mode line face based on the real selected window. */
16863 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
16864 current_buffer->mode_line_format);
16865 ++n;
16866 }
16867
16868 if (WINDOW_WANTS_HEADER_LINE_P (w))
16869 {
16870 display_mode_line (w, HEADER_LINE_FACE_ID,
16871 current_buffer->header_line_format);
16872 ++n;
16873 }
16874
16875 selected_frame = old_selected_frame;
16876 selected_window = old_selected_window;
16877 return n;
16878 }
16879
16880
16881 /* Display mode or top line of window W. FACE_ID specifies which line
16882 to display; it is either MODE_LINE_FACE_ID or HEADER_LINE_FACE_ID.
16883 FORMAT is the mode line format to display. Value is the pixel
16884 height of the mode line displayed. */
16885
16886 static int
16887 display_mode_line (w, face_id, format)
16888 struct window *w;
16889 enum face_id face_id;
16890 Lisp_Object format;
16891 {
16892 struct it it;
16893 struct face *face;
16894 int count = SPECPDL_INDEX ();
16895
16896 init_iterator (&it, w, -1, -1, NULL, face_id);
16897 /* Don't extend on a previously drawn mode-line.
16898 This may happen if called from pos_visible_p. */
16899 it.glyph_row->enabled_p = 0;
16900 prepare_desired_row (it.glyph_row);
16901
16902 it.glyph_row->mode_line_p = 1;
16903
16904 if (! mode_line_inverse_video)
16905 /* Force the mode-line to be displayed in the default face. */
16906 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
16907
16908 record_unwind_protect (unwind_format_mode_line,
16909 format_mode_line_unwind_data (NULL, 0));
16910
16911 mode_line_target = MODE_LINE_DISPLAY;
16912
16913 /* Temporarily make frame's keyboard the current kboard so that
16914 kboard-local variables in the mode_line_format will get the right
16915 values. */
16916 push_kboard (FRAME_KBOARD (it.f));
16917 record_unwind_save_match_data ();
16918 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
16919 pop_kboard ();
16920
16921 unbind_to (count, Qnil);
16922
16923 /* Fill up with spaces. */
16924 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
16925
16926 compute_line_metrics (&it);
16927 it.glyph_row->full_width_p = 1;
16928 it.glyph_row->continued_p = 0;
16929 it.glyph_row->truncated_on_left_p = 0;
16930 it.glyph_row->truncated_on_right_p = 0;
16931
16932 /* Make a 3D mode-line have a shadow at its right end. */
16933 face = FACE_FROM_ID (it.f, face_id);
16934 extend_face_to_end_of_line (&it);
16935 if (face->box != FACE_NO_BOX)
16936 {
16937 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
16938 + it.glyph_row->used[TEXT_AREA] - 1);
16939 last->right_box_line_p = 1;
16940 }
16941
16942 return it.glyph_row->height;
16943 }
16944
16945 /* Move element ELT in LIST to the front of LIST.
16946 Return the updated list. */
16947
16948 static Lisp_Object
16949 move_elt_to_front (elt, list)
16950 Lisp_Object elt, list;
16951 {
16952 register Lisp_Object tail, prev;
16953 register Lisp_Object tem;
16954
16955 tail = list;
16956 prev = Qnil;
16957 while (CONSP (tail))
16958 {
16959 tem = XCAR (tail);
16960
16961 if (EQ (elt, tem))
16962 {
16963 /* Splice out the link TAIL. */
16964 if (NILP (prev))
16965 list = XCDR (tail);
16966 else
16967 Fsetcdr (prev, XCDR (tail));
16968
16969 /* Now make it the first. */
16970 Fsetcdr (tail, list);
16971 return tail;
16972 }
16973 else
16974 prev = tail;
16975 tail = XCDR (tail);
16976 QUIT;
16977 }
16978
16979 /* Not found--return unchanged LIST. */
16980 return list;
16981 }
16982
16983 /* Contribute ELT to the mode line for window IT->w. How it
16984 translates into text depends on its data type.
16985
16986 IT describes the display environment in which we display, as usual.
16987
16988 DEPTH is the depth in recursion. It is used to prevent
16989 infinite recursion here.
16990
16991 FIELD_WIDTH is the number of characters the display of ELT should
16992 occupy in the mode line, and PRECISION is the maximum number of
16993 characters to display from ELT's representation. See
16994 display_string for details.
16995
16996 Returns the hpos of the end of the text generated by ELT.
16997
16998 PROPS is a property list to add to any string we encounter.
16999
17000 If RISKY is nonzero, remove (disregard) any properties in any string
17001 we encounter, and ignore :eval and :propertize.
17002
17003 The global variable `mode_line_target' determines whether the
17004 output is passed to `store_mode_line_noprop',
17005 `store_mode_line_string', or `display_string'. */
17006
17007 static int
17008 display_mode_element (it, depth, field_width, precision, elt, props, risky)
17009 struct it *it;
17010 int depth;
17011 int field_width, precision;
17012 Lisp_Object elt, props;
17013 int risky;
17014 {
17015 int n = 0, field, prec;
17016 int literal = 0;
17017
17018 tail_recurse:
17019 if (depth > 100)
17020 elt = build_string ("*too-deep*");
17021
17022 depth++;
17023
17024 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
17025 {
17026 case Lisp_String:
17027 {
17028 /* A string: output it and check for %-constructs within it. */
17029 unsigned char c;
17030 int offset = 0;
17031
17032 if (SCHARS (elt) > 0
17033 && (!NILP (props) || risky))
17034 {
17035 Lisp_Object oprops, aelt;
17036 oprops = Ftext_properties_at (make_number (0), elt);
17037
17038 /* If the starting string's properties are not what
17039 we want, translate the string. Also, if the string
17040 is risky, do that anyway. */
17041
17042 if (NILP (Fequal (props, oprops)) || risky)
17043 {
17044 /* If the starting string has properties,
17045 merge the specified ones onto the existing ones. */
17046 if (! NILP (oprops) && !risky)
17047 {
17048 Lisp_Object tem;
17049
17050 oprops = Fcopy_sequence (oprops);
17051 tem = props;
17052 while (CONSP (tem))
17053 {
17054 oprops = Fplist_put (oprops, XCAR (tem),
17055 XCAR (XCDR (tem)));
17056 tem = XCDR (XCDR (tem));
17057 }
17058 props = oprops;
17059 }
17060
17061 aelt = Fassoc (elt, mode_line_proptrans_alist);
17062 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
17063 {
17064 /* AELT is what we want. Move it to the front
17065 without consing. */
17066 elt = XCAR (aelt);
17067 mode_line_proptrans_alist
17068 = move_elt_to_front (aelt, mode_line_proptrans_alist);
17069 }
17070 else
17071 {
17072 Lisp_Object tem;
17073
17074 /* If AELT has the wrong props, it is useless.
17075 so get rid of it. */
17076 if (! NILP (aelt))
17077 mode_line_proptrans_alist
17078 = Fdelq (aelt, mode_line_proptrans_alist);
17079
17080 elt = Fcopy_sequence (elt);
17081 Fset_text_properties (make_number (0), Flength (elt),
17082 props, elt);
17083 /* Add this item to mode_line_proptrans_alist. */
17084 mode_line_proptrans_alist
17085 = Fcons (Fcons (elt, props),
17086 mode_line_proptrans_alist);
17087 /* Truncate mode_line_proptrans_alist
17088 to at most 50 elements. */
17089 tem = Fnthcdr (make_number (50),
17090 mode_line_proptrans_alist);
17091 if (! NILP (tem))
17092 XSETCDR (tem, Qnil);
17093 }
17094 }
17095 }
17096
17097 offset = 0;
17098
17099 if (literal)
17100 {
17101 prec = precision - n;
17102 switch (mode_line_target)
17103 {
17104 case MODE_LINE_NOPROP:
17105 case MODE_LINE_TITLE:
17106 n += store_mode_line_noprop (SDATA (elt), -1, prec);
17107 break;
17108 case MODE_LINE_STRING:
17109 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
17110 break;
17111 case MODE_LINE_DISPLAY:
17112 n += display_string (NULL, elt, Qnil, 0, 0, it,
17113 0, prec, 0, STRING_MULTIBYTE (elt));
17114 break;
17115 }
17116
17117 break;
17118 }
17119
17120 /* Handle the non-literal case. */
17121
17122 while ((precision <= 0 || n < precision)
17123 && SREF (elt, offset) != 0
17124 && (mode_line_target != MODE_LINE_DISPLAY
17125 || it->current_x < it->last_visible_x))
17126 {
17127 int last_offset = offset;
17128
17129 /* Advance to end of string or next format specifier. */
17130 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
17131 ;
17132
17133 if (offset - 1 != last_offset)
17134 {
17135 int nchars, nbytes;
17136
17137 /* Output to end of string or up to '%'. Field width
17138 is length of string. Don't output more than
17139 PRECISION allows us. */
17140 offset--;
17141
17142 prec = c_string_width (SDATA (elt) + last_offset,
17143 offset - last_offset, precision - n,
17144 &nchars, &nbytes);
17145
17146 switch (mode_line_target)
17147 {
17148 case MODE_LINE_NOPROP:
17149 case MODE_LINE_TITLE:
17150 n += store_mode_line_noprop (SDATA (elt) + last_offset, 0, prec);
17151 break;
17152 case MODE_LINE_STRING:
17153 {
17154 int bytepos = last_offset;
17155 int charpos = string_byte_to_char (elt, bytepos);
17156 int endpos = (precision <= 0
17157 ? string_byte_to_char (elt, offset)
17158 : charpos + nchars);
17159
17160 n += store_mode_line_string (NULL,
17161 Fsubstring (elt, make_number (charpos),
17162 make_number (endpos)),
17163 0, 0, 0, Qnil);
17164 }
17165 break;
17166 case MODE_LINE_DISPLAY:
17167 {
17168 int bytepos = last_offset;
17169 int charpos = string_byte_to_char (elt, bytepos);
17170
17171 if (precision <= 0)
17172 nchars = string_byte_to_char (elt, offset) - charpos;
17173 n += display_string (NULL, elt, Qnil, 0, charpos,
17174 it, 0, nchars, 0,
17175 STRING_MULTIBYTE (elt));
17176 }
17177 break;
17178 }
17179 }
17180 else /* c == '%' */
17181 {
17182 int percent_position = offset;
17183
17184 /* Get the specified minimum width. Zero means
17185 don't pad. */
17186 field = 0;
17187 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
17188 field = field * 10 + c - '0';
17189
17190 /* Don't pad beyond the total padding allowed. */
17191 if (field_width - n > 0 && field > field_width - n)
17192 field = field_width - n;
17193
17194 /* Note that either PRECISION <= 0 or N < PRECISION. */
17195 prec = precision - n;
17196
17197 if (c == 'M')
17198 n += display_mode_element (it, depth, field, prec,
17199 Vglobal_mode_string, props,
17200 risky);
17201 else if (c != 0)
17202 {
17203 int multibyte;
17204 int bytepos, charpos;
17205 unsigned char *spec;
17206
17207 bytepos = percent_position;
17208 charpos = (STRING_MULTIBYTE (elt)
17209 ? string_byte_to_char (elt, bytepos)
17210 : bytepos);
17211
17212 spec
17213 = decode_mode_spec (it->w, c, field, prec, &multibyte);
17214
17215 switch (mode_line_target)
17216 {
17217 case MODE_LINE_NOPROP:
17218 case MODE_LINE_TITLE:
17219 n += store_mode_line_noprop (spec, field, prec);
17220 break;
17221 case MODE_LINE_STRING:
17222 {
17223 int len = strlen (spec);
17224 Lisp_Object tem = make_string (spec, len);
17225 props = Ftext_properties_at (make_number (charpos), elt);
17226 /* Should only keep face property in props */
17227 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
17228 }
17229 break;
17230 case MODE_LINE_DISPLAY:
17231 {
17232 int nglyphs_before, nwritten;
17233
17234 nglyphs_before = it->glyph_row->used[TEXT_AREA];
17235 nwritten = display_string (spec, Qnil, elt,
17236 charpos, 0, it,
17237 field, prec, 0,
17238 multibyte);
17239
17240 /* Assign to the glyphs written above the
17241 string where the `%x' came from, position
17242 of the `%'. */
17243 if (nwritten > 0)
17244 {
17245 struct glyph *glyph
17246 = (it->glyph_row->glyphs[TEXT_AREA]
17247 + nglyphs_before);
17248 int i;
17249
17250 for (i = 0; i < nwritten; ++i)
17251 {
17252 glyph[i].object = elt;
17253 glyph[i].charpos = charpos;
17254 }
17255
17256 n += nwritten;
17257 }
17258 }
17259 break;
17260 }
17261 }
17262 else /* c == 0 */
17263 break;
17264 }
17265 }
17266 }
17267 break;
17268
17269 case Lisp_Symbol:
17270 /* A symbol: process the value of the symbol recursively
17271 as if it appeared here directly. Avoid error if symbol void.
17272 Special case: if value of symbol is a string, output the string
17273 literally. */
17274 {
17275 register Lisp_Object tem;
17276
17277 /* If the variable is not marked as risky to set
17278 then its contents are risky to use. */
17279 if (NILP (Fget (elt, Qrisky_local_variable)))
17280 risky = 1;
17281
17282 tem = Fboundp (elt);
17283 if (!NILP (tem))
17284 {
17285 tem = Fsymbol_value (elt);
17286 /* If value is a string, output that string literally:
17287 don't check for % within it. */
17288 if (STRINGP (tem))
17289 literal = 1;
17290
17291 if (!EQ (tem, elt))
17292 {
17293 /* Give up right away for nil or t. */
17294 elt = tem;
17295 goto tail_recurse;
17296 }
17297 }
17298 }
17299 break;
17300
17301 case Lisp_Cons:
17302 {
17303 register Lisp_Object car, tem;
17304
17305 /* A cons cell: five distinct cases.
17306 If first element is :eval or :propertize, do something special.
17307 If first element is a string or a cons, process all the elements
17308 and effectively concatenate them.
17309 If first element is a negative number, truncate displaying cdr to
17310 at most that many characters. If positive, pad (with spaces)
17311 to at least that many characters.
17312 If first element is a symbol, process the cadr or caddr recursively
17313 according to whether the symbol's value is non-nil or nil. */
17314 car = XCAR (elt);
17315 if (EQ (car, QCeval))
17316 {
17317 /* An element of the form (:eval FORM) means evaluate FORM
17318 and use the result as mode line elements. */
17319
17320 if (risky)
17321 break;
17322
17323 if (CONSP (XCDR (elt)))
17324 {
17325 Lisp_Object spec;
17326 spec = safe_eval (XCAR (XCDR (elt)));
17327 n += display_mode_element (it, depth, field_width - n,
17328 precision - n, spec, props,
17329 risky);
17330 }
17331 }
17332 else if (EQ (car, QCpropertize))
17333 {
17334 /* An element of the form (:propertize ELT PROPS...)
17335 means display ELT but applying properties PROPS. */
17336
17337 if (risky)
17338 break;
17339
17340 if (CONSP (XCDR (elt)))
17341 n += display_mode_element (it, depth, field_width - n,
17342 precision - n, XCAR (XCDR (elt)),
17343 XCDR (XCDR (elt)), risky);
17344 }
17345 else if (SYMBOLP (car))
17346 {
17347 tem = Fboundp (car);
17348 elt = XCDR (elt);
17349 if (!CONSP (elt))
17350 goto invalid;
17351 /* elt is now the cdr, and we know it is a cons cell.
17352 Use its car if CAR has a non-nil value. */
17353 if (!NILP (tem))
17354 {
17355 tem = Fsymbol_value (car);
17356 if (!NILP (tem))
17357 {
17358 elt = XCAR (elt);
17359 goto tail_recurse;
17360 }
17361 }
17362 /* Symbol's value is nil (or symbol is unbound)
17363 Get the cddr of the original list
17364 and if possible find the caddr and use that. */
17365 elt = XCDR (elt);
17366 if (NILP (elt))
17367 break;
17368 else if (!CONSP (elt))
17369 goto invalid;
17370 elt = XCAR (elt);
17371 goto tail_recurse;
17372 }
17373 else if (INTEGERP (car))
17374 {
17375 register int lim = XINT (car);
17376 elt = XCDR (elt);
17377 if (lim < 0)
17378 {
17379 /* Negative int means reduce maximum width. */
17380 if (precision <= 0)
17381 precision = -lim;
17382 else
17383 precision = min (precision, -lim);
17384 }
17385 else if (lim > 0)
17386 {
17387 /* Padding specified. Don't let it be more than
17388 current maximum. */
17389 if (precision > 0)
17390 lim = min (precision, lim);
17391
17392 /* If that's more padding than already wanted, queue it.
17393 But don't reduce padding already specified even if
17394 that is beyond the current truncation point. */
17395 field_width = max (lim, field_width);
17396 }
17397 goto tail_recurse;
17398 }
17399 else if (STRINGP (car) || CONSP (car))
17400 {
17401 register int limit = 50;
17402 /* Limit is to protect against circular lists. */
17403 while (CONSP (elt)
17404 && --limit > 0
17405 && (precision <= 0 || n < precision))
17406 {
17407 n += display_mode_element (it, depth,
17408 /* Do padding only after the last
17409 element in the list. */
17410 (! CONSP (XCDR (elt))
17411 ? field_width - n
17412 : 0),
17413 precision - n, XCAR (elt),
17414 props, risky);
17415 elt = XCDR (elt);
17416 }
17417 }
17418 }
17419 break;
17420
17421 default:
17422 invalid:
17423 elt = build_string ("*invalid*");
17424 goto tail_recurse;
17425 }
17426
17427 /* Pad to FIELD_WIDTH. */
17428 if (field_width > 0 && n < field_width)
17429 {
17430 switch (mode_line_target)
17431 {
17432 case MODE_LINE_NOPROP:
17433 case MODE_LINE_TITLE:
17434 n += store_mode_line_noprop ("", field_width - n, 0);
17435 break;
17436 case MODE_LINE_STRING:
17437 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
17438 break;
17439 case MODE_LINE_DISPLAY:
17440 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
17441 0, 0, 0);
17442 break;
17443 }
17444 }
17445
17446 return n;
17447 }
17448
17449 /* Store a mode-line string element in mode_line_string_list.
17450
17451 If STRING is non-null, display that C string. Otherwise, the Lisp
17452 string LISP_STRING is displayed.
17453
17454 FIELD_WIDTH is the minimum number of output glyphs to produce.
17455 If STRING has fewer characters than FIELD_WIDTH, pad to the right
17456 with spaces. FIELD_WIDTH <= 0 means don't pad.
17457
17458 PRECISION is the maximum number of characters to output from
17459 STRING. PRECISION <= 0 means don't truncate the string.
17460
17461 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
17462 properties to the string.
17463
17464 PROPS are the properties to add to the string.
17465 The mode_line_string_face face property is always added to the string.
17466 */
17467
17468 static int
17469 store_mode_line_string (string, lisp_string, copy_string, field_width, precision, props)
17470 char *string;
17471 Lisp_Object lisp_string;
17472 int copy_string;
17473 int field_width;
17474 int precision;
17475 Lisp_Object props;
17476 {
17477 int len;
17478 int n = 0;
17479
17480 if (string != NULL)
17481 {
17482 len = strlen (string);
17483 if (precision > 0 && len > precision)
17484 len = precision;
17485 lisp_string = make_string (string, len);
17486 if (NILP (props))
17487 props = mode_line_string_face_prop;
17488 else if (!NILP (mode_line_string_face))
17489 {
17490 Lisp_Object face = Fplist_get (props, Qface);
17491 props = Fcopy_sequence (props);
17492 if (NILP (face))
17493 face = mode_line_string_face;
17494 else
17495 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
17496 props = Fplist_put (props, Qface, face);
17497 }
17498 Fadd_text_properties (make_number (0), make_number (len),
17499 props, lisp_string);
17500 }
17501 else
17502 {
17503 len = XFASTINT (Flength (lisp_string));
17504 if (precision > 0 && len > precision)
17505 {
17506 len = precision;
17507 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
17508 precision = -1;
17509 }
17510 if (!NILP (mode_line_string_face))
17511 {
17512 Lisp_Object face;
17513 if (NILP (props))
17514 props = Ftext_properties_at (make_number (0), lisp_string);
17515 face = Fplist_get (props, Qface);
17516 if (NILP (face))
17517 face = mode_line_string_face;
17518 else
17519 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
17520 props = Fcons (Qface, Fcons (face, Qnil));
17521 if (copy_string)
17522 lisp_string = Fcopy_sequence (lisp_string);
17523 }
17524 if (!NILP (props))
17525 Fadd_text_properties (make_number (0), make_number (len),
17526 props, lisp_string);
17527 }
17528
17529 if (len > 0)
17530 {
17531 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
17532 n += len;
17533 }
17534
17535 if (field_width > len)
17536 {
17537 field_width -= len;
17538 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
17539 if (!NILP (props))
17540 Fadd_text_properties (make_number (0), make_number (field_width),
17541 props, lisp_string);
17542 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
17543 n += field_width;
17544 }
17545
17546 return n;
17547 }
17548
17549
17550 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
17551 1, 4, 0,
17552 doc: /* Format a string out of a mode line format specification.
17553 First arg FORMAT specifies the mode line format (see `mode-line-format'
17554 for details) to use.
17555
17556 Optional second arg FACE specifies the face property to put
17557 on all characters for which no face is specified.
17558 The value t means whatever face the window's mode line currently uses
17559 \(either `mode-line' or `mode-line-inactive', depending).
17560 A value of nil means the default is no face property.
17561 If FACE is an integer, the value string has no text properties.
17562
17563 Optional third and fourth args WINDOW and BUFFER specify the window
17564 and buffer to use as the context for the formatting (defaults
17565 are the selected window and the window's buffer). */)
17566 (format, face, window, buffer)
17567 Lisp_Object format, face, window, buffer;
17568 {
17569 struct it it;
17570 int len;
17571 struct window *w;
17572 struct buffer *old_buffer = NULL;
17573 int face_id = -1;
17574 int no_props = INTEGERP (face);
17575 int count = SPECPDL_INDEX ();
17576 Lisp_Object str;
17577 int string_start = 0;
17578
17579 if (NILP (window))
17580 window = selected_window;
17581 CHECK_WINDOW (window);
17582 w = XWINDOW (window);
17583
17584 if (NILP (buffer))
17585 buffer = w->buffer;
17586 CHECK_BUFFER (buffer);
17587
17588 /* Make formatting the modeline a non-op when noninteractive, otherwise
17589 there will be problems later caused by a partially initialized frame. */
17590 if (NILP (format) || noninteractive)
17591 return empty_unibyte_string;
17592
17593 if (no_props)
17594 face = Qnil;
17595
17596 if (!NILP (face))
17597 {
17598 if (EQ (face, Qt))
17599 face = (EQ (window, selected_window) ? Qmode_line : Qmode_line_inactive);
17600 face_id = lookup_named_face (XFRAME (WINDOW_FRAME (w)), face, 0);
17601 }
17602
17603 if (face_id < 0)
17604 face_id = DEFAULT_FACE_ID;
17605
17606 if (XBUFFER (buffer) != current_buffer)
17607 old_buffer = current_buffer;
17608
17609 /* Save things including mode_line_proptrans_alist,
17610 and set that to nil so that we don't alter the outer value. */
17611 record_unwind_protect (unwind_format_mode_line,
17612 format_mode_line_unwind_data (old_buffer, 1));
17613 mode_line_proptrans_alist = Qnil;
17614
17615 if (old_buffer)
17616 set_buffer_internal_1 (XBUFFER (buffer));
17617
17618 init_iterator (&it, w, -1, -1, NULL, face_id);
17619
17620 if (no_props)
17621 {
17622 mode_line_target = MODE_LINE_NOPROP;
17623 mode_line_string_face_prop = Qnil;
17624 mode_line_string_list = Qnil;
17625 string_start = MODE_LINE_NOPROP_LEN (0);
17626 }
17627 else
17628 {
17629 mode_line_target = MODE_LINE_STRING;
17630 mode_line_string_list = Qnil;
17631 mode_line_string_face = face;
17632 mode_line_string_face_prop
17633 = (NILP (face) ? Qnil : Fcons (Qface, Fcons (face, Qnil)));
17634 }
17635
17636 push_kboard (FRAME_KBOARD (it.f));
17637 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
17638 pop_kboard ();
17639
17640 if (no_props)
17641 {
17642 len = MODE_LINE_NOPROP_LEN (string_start);
17643 str = make_string (mode_line_noprop_buf + string_start, len);
17644 }
17645 else
17646 {
17647 mode_line_string_list = Fnreverse (mode_line_string_list);
17648 str = Fmapconcat (intern ("identity"), mode_line_string_list,
17649 empty_unibyte_string);
17650 }
17651
17652 unbind_to (count, Qnil);
17653 return str;
17654 }
17655
17656 /* Write a null-terminated, right justified decimal representation of
17657 the positive integer D to BUF using a minimal field width WIDTH. */
17658
17659 static void
17660 pint2str (buf, width, d)
17661 register char *buf;
17662 register int width;
17663 register int d;
17664 {
17665 register char *p = buf;
17666
17667 if (d <= 0)
17668 *p++ = '0';
17669 else
17670 {
17671 while (d > 0)
17672 {
17673 *p++ = d % 10 + '0';
17674 d /= 10;
17675 }
17676 }
17677
17678 for (width -= (int) (p - buf); width > 0; --width)
17679 *p++ = ' ';
17680 *p-- = '\0';
17681 while (p > buf)
17682 {
17683 d = *buf;
17684 *buf++ = *p;
17685 *p-- = d;
17686 }
17687 }
17688
17689 /* Write a null-terminated, right justified decimal and "human
17690 readable" representation of the nonnegative integer D to BUF using
17691 a minimal field width WIDTH. D should be smaller than 999.5e24. */
17692
17693 static const char power_letter[] =
17694 {
17695 0, /* not used */
17696 'k', /* kilo */
17697 'M', /* mega */
17698 'G', /* giga */
17699 'T', /* tera */
17700 'P', /* peta */
17701 'E', /* exa */
17702 'Z', /* zetta */
17703 'Y' /* yotta */
17704 };
17705
17706 static void
17707 pint2hrstr (buf, width, d)
17708 char *buf;
17709 int width;
17710 int d;
17711 {
17712 /* We aim to represent the nonnegative integer D as
17713 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
17714 int quotient = d;
17715 int remainder = 0;
17716 /* -1 means: do not use TENTHS. */
17717 int tenths = -1;
17718 int exponent = 0;
17719
17720 /* Length of QUOTIENT.TENTHS as a string. */
17721 int length;
17722
17723 char * psuffix;
17724 char * p;
17725
17726 if (1000 <= quotient)
17727 {
17728 /* Scale to the appropriate EXPONENT. */
17729 do
17730 {
17731 remainder = quotient % 1000;
17732 quotient /= 1000;
17733 exponent++;
17734 }
17735 while (1000 <= quotient);
17736
17737 /* Round to nearest and decide whether to use TENTHS or not. */
17738 if (quotient <= 9)
17739 {
17740 tenths = remainder / 100;
17741 if (50 <= remainder % 100)
17742 {
17743 if (tenths < 9)
17744 tenths++;
17745 else
17746 {
17747 quotient++;
17748 if (quotient == 10)
17749 tenths = -1;
17750 else
17751 tenths = 0;
17752 }
17753 }
17754 }
17755 else
17756 if (500 <= remainder)
17757 {
17758 if (quotient < 999)
17759 quotient++;
17760 else
17761 {
17762 quotient = 1;
17763 exponent++;
17764 tenths = 0;
17765 }
17766 }
17767 }
17768
17769 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
17770 if (tenths == -1 && quotient <= 99)
17771 if (quotient <= 9)
17772 length = 1;
17773 else
17774 length = 2;
17775 else
17776 length = 3;
17777 p = psuffix = buf + max (width, length);
17778
17779 /* Print EXPONENT. */
17780 if (exponent)
17781 *psuffix++ = power_letter[exponent];
17782 *psuffix = '\0';
17783
17784 /* Print TENTHS. */
17785 if (tenths >= 0)
17786 {
17787 *--p = '0' + tenths;
17788 *--p = '.';
17789 }
17790
17791 /* Print QUOTIENT. */
17792 do
17793 {
17794 int digit = quotient % 10;
17795 *--p = '0' + digit;
17796 }
17797 while ((quotient /= 10) != 0);
17798
17799 /* Print leading spaces. */
17800 while (buf < p)
17801 *--p = ' ';
17802 }
17803
17804 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
17805 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
17806 type of CODING_SYSTEM. Return updated pointer into BUF. */
17807
17808 static unsigned char invalid_eol_type[] = "(*invalid*)";
17809
17810 static char *
17811 decode_mode_spec_coding (coding_system, buf, eol_flag)
17812 Lisp_Object coding_system;
17813 register char *buf;
17814 int eol_flag;
17815 {
17816 Lisp_Object val;
17817 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
17818 const unsigned char *eol_str;
17819 int eol_str_len;
17820 /* The EOL conversion we are using. */
17821 Lisp_Object eoltype;
17822
17823 val = CODING_SYSTEM_SPEC (coding_system);
17824 eoltype = Qnil;
17825
17826 if (!VECTORP (val)) /* Not yet decided. */
17827 {
17828 if (multibyte)
17829 *buf++ = '-';
17830 if (eol_flag)
17831 eoltype = eol_mnemonic_undecided;
17832 /* Don't mention EOL conversion if it isn't decided. */
17833 }
17834 else
17835 {
17836 Lisp_Object attrs;
17837 Lisp_Object eolvalue;
17838
17839 attrs = AREF (val, 0);
17840 eolvalue = AREF (val, 2);
17841
17842 if (multibyte)
17843 *buf++ = XFASTINT (CODING_ATTR_MNEMONIC (attrs));
17844
17845 if (eol_flag)
17846 {
17847 /* The EOL conversion that is normal on this system. */
17848
17849 if (NILP (eolvalue)) /* Not yet decided. */
17850 eoltype = eol_mnemonic_undecided;
17851 else if (VECTORP (eolvalue)) /* Not yet decided. */
17852 eoltype = eol_mnemonic_undecided;
17853 else /* eolvalue is Qunix, Qdos, or Qmac. */
17854 eoltype = (EQ (eolvalue, Qunix)
17855 ? eol_mnemonic_unix
17856 : (EQ (eolvalue, Qdos) == 1
17857 ? eol_mnemonic_dos : eol_mnemonic_mac));
17858 }
17859 }
17860
17861 if (eol_flag)
17862 {
17863 /* Mention the EOL conversion if it is not the usual one. */
17864 if (STRINGP (eoltype))
17865 {
17866 eol_str = SDATA (eoltype);
17867 eol_str_len = SBYTES (eoltype);
17868 }
17869 else if (CHARACTERP (eoltype))
17870 {
17871 unsigned char *tmp = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
17872 eol_str_len = CHAR_STRING (XINT (eoltype), tmp);
17873 eol_str = tmp;
17874 }
17875 else
17876 {
17877 eol_str = invalid_eol_type;
17878 eol_str_len = sizeof (invalid_eol_type) - 1;
17879 }
17880 bcopy (eol_str, buf, eol_str_len);
17881 buf += eol_str_len;
17882 }
17883
17884 return buf;
17885 }
17886
17887 /* Return a string for the output of a mode line %-spec for window W,
17888 generated by character C. PRECISION >= 0 means don't return a
17889 string longer than that value. FIELD_WIDTH > 0 means pad the
17890 string returned with spaces to that value. Return 1 in *MULTIBYTE
17891 if the result is multibyte text.
17892
17893 Note we operate on the current buffer for most purposes,
17894 the exception being w->base_line_pos. */
17895
17896 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
17897
17898 static char *
17899 decode_mode_spec (w, c, field_width, precision, multibyte)
17900 struct window *w;
17901 register int c;
17902 int field_width, precision;
17903 int *multibyte;
17904 {
17905 Lisp_Object obj;
17906 struct frame *f = XFRAME (WINDOW_FRAME (w));
17907 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
17908 struct buffer *b = current_buffer;
17909
17910 obj = Qnil;
17911 *multibyte = 0;
17912
17913 switch (c)
17914 {
17915 case '*':
17916 if (!NILP (b->read_only))
17917 return "%";
17918 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
17919 return "*";
17920 return "-";
17921
17922 case '+':
17923 /* This differs from %* only for a modified read-only buffer. */
17924 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
17925 return "*";
17926 if (!NILP (b->read_only))
17927 return "%";
17928 return "-";
17929
17930 case '&':
17931 /* This differs from %* in ignoring read-only-ness. */
17932 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
17933 return "*";
17934 return "-";
17935
17936 case '%':
17937 return "%";
17938
17939 case '[':
17940 {
17941 int i;
17942 char *p;
17943
17944 if (command_loop_level > 5)
17945 return "[[[... ";
17946 p = decode_mode_spec_buf;
17947 for (i = 0; i < command_loop_level; i++)
17948 *p++ = '[';
17949 *p = 0;
17950 return decode_mode_spec_buf;
17951 }
17952
17953 case ']':
17954 {
17955 int i;
17956 char *p;
17957
17958 if (command_loop_level > 5)
17959 return " ...]]]";
17960 p = decode_mode_spec_buf;
17961 for (i = 0; i < command_loop_level; i++)
17962 *p++ = ']';
17963 *p = 0;
17964 return decode_mode_spec_buf;
17965 }
17966
17967 case '-':
17968 {
17969 register int i;
17970
17971 /* Let lots_of_dashes be a string of infinite length. */
17972 if (mode_line_target == MODE_LINE_NOPROP ||
17973 mode_line_target == MODE_LINE_STRING)
17974 return "--";
17975 if (field_width <= 0
17976 || field_width > sizeof (lots_of_dashes))
17977 {
17978 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
17979 decode_mode_spec_buf[i] = '-';
17980 decode_mode_spec_buf[i] = '\0';
17981 return decode_mode_spec_buf;
17982 }
17983 else
17984 return lots_of_dashes;
17985 }
17986
17987 case 'b':
17988 obj = b->name;
17989 break;
17990
17991 case 'c':
17992 /* %c and %l are ignored in `frame-title-format'.
17993 (In redisplay_internal, the frame title is drawn _before_ the
17994 windows are updated, so the stuff which depends on actual
17995 window contents (such as %l) may fail to render properly, or
17996 even crash emacs.) */
17997 if (mode_line_target == MODE_LINE_TITLE)
17998 return "";
17999 else
18000 {
18001 int col = (int) current_column (); /* iftc */
18002 w->column_number_displayed = make_number (col);
18003 pint2str (decode_mode_spec_buf, field_width, col);
18004 return decode_mode_spec_buf;
18005 }
18006
18007 case 'e':
18008 #ifndef SYSTEM_MALLOC
18009 {
18010 if (NILP (Vmemory_full))
18011 return "";
18012 else
18013 return "!MEM FULL! ";
18014 }
18015 #else
18016 return "";
18017 #endif
18018
18019 case 'F':
18020 /* %F displays the frame name. */
18021 if (!NILP (f->title))
18022 return (char *) SDATA (f->title);
18023 if (f->explicit_name || ! FRAME_WINDOW_P (f))
18024 return (char *) SDATA (f->name);
18025 return "Emacs";
18026
18027 case 'f':
18028 obj = b->filename;
18029 break;
18030
18031 case 'i':
18032 {
18033 int size = ZV - BEGV;
18034 pint2str (decode_mode_spec_buf, field_width, size);
18035 return decode_mode_spec_buf;
18036 }
18037
18038 case 'I':
18039 {
18040 int size = ZV - BEGV;
18041 pint2hrstr (decode_mode_spec_buf, field_width, size);
18042 return decode_mode_spec_buf;
18043 }
18044
18045 case 'l':
18046 {
18047 int startpos, startpos_byte, line, linepos, linepos_byte;
18048 int topline, nlines, junk, height;
18049
18050 /* %c and %l are ignored in `frame-title-format'. */
18051 if (mode_line_target == MODE_LINE_TITLE)
18052 return "";
18053
18054 startpos = XMARKER (w->start)->charpos;
18055 startpos_byte = marker_byte_position (w->start);
18056 height = WINDOW_TOTAL_LINES (w);
18057
18058 /* If we decided that this buffer isn't suitable for line numbers,
18059 don't forget that too fast. */
18060 if (EQ (w->base_line_pos, w->buffer))
18061 goto no_value;
18062 /* But do forget it, if the window shows a different buffer now. */
18063 else if (BUFFERP (w->base_line_pos))
18064 w->base_line_pos = Qnil;
18065
18066 /* If the buffer is very big, don't waste time. */
18067 if (INTEGERP (Vline_number_display_limit)
18068 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
18069 {
18070 w->base_line_pos = Qnil;
18071 w->base_line_number = Qnil;
18072 goto no_value;
18073 }
18074
18075 if (!NILP (w->base_line_number)
18076 && !NILP (w->base_line_pos)
18077 && XFASTINT (w->base_line_pos) <= startpos)
18078 {
18079 line = XFASTINT (w->base_line_number);
18080 linepos = XFASTINT (w->base_line_pos);
18081 linepos_byte = buf_charpos_to_bytepos (b, linepos);
18082 }
18083 else
18084 {
18085 line = 1;
18086 linepos = BUF_BEGV (b);
18087 linepos_byte = BUF_BEGV_BYTE (b);
18088 }
18089
18090 /* Count lines from base line to window start position. */
18091 nlines = display_count_lines (linepos, linepos_byte,
18092 startpos_byte,
18093 startpos, &junk);
18094
18095 topline = nlines + line;
18096
18097 /* Determine a new base line, if the old one is too close
18098 or too far away, or if we did not have one.
18099 "Too close" means it's plausible a scroll-down would
18100 go back past it. */
18101 if (startpos == BUF_BEGV (b))
18102 {
18103 w->base_line_number = make_number (topline);
18104 w->base_line_pos = make_number (BUF_BEGV (b));
18105 }
18106 else if (nlines < height + 25 || nlines > height * 3 + 50
18107 || linepos == BUF_BEGV (b))
18108 {
18109 int limit = BUF_BEGV (b);
18110 int limit_byte = BUF_BEGV_BYTE (b);
18111 int position;
18112 int distance = (height * 2 + 30) * line_number_display_limit_width;
18113
18114 if (startpos - distance > limit)
18115 {
18116 limit = startpos - distance;
18117 limit_byte = CHAR_TO_BYTE (limit);
18118 }
18119
18120 nlines = display_count_lines (startpos, startpos_byte,
18121 limit_byte,
18122 - (height * 2 + 30),
18123 &position);
18124 /* If we couldn't find the lines we wanted within
18125 line_number_display_limit_width chars per line,
18126 give up on line numbers for this window. */
18127 if (position == limit_byte && limit == startpos - distance)
18128 {
18129 w->base_line_pos = w->buffer;
18130 w->base_line_number = Qnil;
18131 goto no_value;
18132 }
18133
18134 w->base_line_number = make_number (topline - nlines);
18135 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
18136 }
18137
18138 /* Now count lines from the start pos to point. */
18139 nlines = display_count_lines (startpos, startpos_byte,
18140 PT_BYTE, PT, &junk);
18141
18142 /* Record that we did display the line number. */
18143 line_number_displayed = 1;
18144
18145 /* Make the string to show. */
18146 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
18147 return decode_mode_spec_buf;
18148 no_value:
18149 {
18150 char* p = decode_mode_spec_buf;
18151 int pad = field_width - 2;
18152 while (pad-- > 0)
18153 *p++ = ' ';
18154 *p++ = '?';
18155 *p++ = '?';
18156 *p = '\0';
18157 return decode_mode_spec_buf;
18158 }
18159 }
18160 break;
18161
18162 case 'm':
18163 obj = b->mode_name;
18164 break;
18165
18166 case 'n':
18167 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
18168 return " Narrow";
18169 break;
18170
18171 case 'p':
18172 {
18173 int pos = marker_position (w->start);
18174 int total = BUF_ZV (b) - BUF_BEGV (b);
18175
18176 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
18177 {
18178 if (pos <= BUF_BEGV (b))
18179 return "All";
18180 else
18181 return "Bottom";
18182 }
18183 else if (pos <= BUF_BEGV (b))
18184 return "Top";
18185 else
18186 {
18187 if (total > 1000000)
18188 /* Do it differently for a large value, to avoid overflow. */
18189 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
18190 else
18191 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
18192 /* We can't normally display a 3-digit number,
18193 so get us a 2-digit number that is close. */
18194 if (total == 100)
18195 total = 99;
18196 sprintf (decode_mode_spec_buf, "%2d%%", total);
18197 return decode_mode_spec_buf;
18198 }
18199 }
18200
18201 /* Display percentage of size above the bottom of the screen. */
18202 case 'P':
18203 {
18204 int toppos = marker_position (w->start);
18205 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
18206 int total = BUF_ZV (b) - BUF_BEGV (b);
18207
18208 if (botpos >= BUF_ZV (b))
18209 {
18210 if (toppos <= BUF_BEGV (b))
18211 return "All";
18212 else
18213 return "Bottom";
18214 }
18215 else
18216 {
18217 if (total > 1000000)
18218 /* Do it differently for a large value, to avoid overflow. */
18219 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
18220 else
18221 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
18222 /* We can't normally display a 3-digit number,
18223 so get us a 2-digit number that is close. */
18224 if (total == 100)
18225 total = 99;
18226 if (toppos <= BUF_BEGV (b))
18227 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
18228 else
18229 sprintf (decode_mode_spec_buf, "%2d%%", total);
18230 return decode_mode_spec_buf;
18231 }
18232 }
18233
18234 case 's':
18235 /* status of process */
18236 obj = Fget_buffer_process (Fcurrent_buffer ());
18237 if (NILP (obj))
18238 return "no process";
18239 #ifdef subprocesses
18240 obj = Fsymbol_name (Fprocess_status (obj));
18241 #endif
18242 break;
18243
18244 case '@':
18245 {
18246 Lisp_Object val;
18247 val = call1 (intern ("file-remote-p"), current_buffer->directory);
18248 if (NILP (val))
18249 return "-";
18250 else
18251 return "@";
18252 }
18253
18254 case 't': /* indicate TEXT or BINARY */
18255 #ifdef MODE_LINE_BINARY_TEXT
18256 return MODE_LINE_BINARY_TEXT (b);
18257 #else
18258 return "T";
18259 #endif
18260
18261 case 'z':
18262 /* coding-system (not including end-of-line format) */
18263 case 'Z':
18264 /* coding-system (including end-of-line type) */
18265 {
18266 int eol_flag = (c == 'Z');
18267 char *p = decode_mode_spec_buf;
18268
18269 if (! FRAME_WINDOW_P (f))
18270 {
18271 /* No need to mention EOL here--the terminal never needs
18272 to do EOL conversion. */
18273 p = decode_mode_spec_coding (CODING_ID_NAME
18274 (FRAME_KEYBOARD_CODING (f)->id),
18275 p, 0);
18276 p = decode_mode_spec_coding (CODING_ID_NAME
18277 (FRAME_TERMINAL_CODING (f)->id),
18278 p, 0);
18279 }
18280 p = decode_mode_spec_coding (b->buffer_file_coding_system,
18281 p, eol_flag);
18282
18283 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
18284 #ifdef subprocesses
18285 obj = Fget_buffer_process (Fcurrent_buffer ());
18286 if (PROCESSP (obj))
18287 {
18288 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
18289 p, eol_flag);
18290 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
18291 p, eol_flag);
18292 }
18293 #endif /* subprocesses */
18294 #endif /* 0 */
18295 *p = 0;
18296 return decode_mode_spec_buf;
18297 }
18298 }
18299
18300 if (STRINGP (obj))
18301 {
18302 *multibyte = STRING_MULTIBYTE (obj);
18303 return (char *) SDATA (obj);
18304 }
18305 else
18306 return "";
18307 }
18308
18309
18310 /* Count up to COUNT lines starting from START / START_BYTE.
18311 But don't go beyond LIMIT_BYTE.
18312 Return the number of lines thus found (always nonnegative).
18313
18314 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
18315
18316 static int
18317 display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
18318 int start, start_byte, limit_byte, count;
18319 int *byte_pos_ptr;
18320 {
18321 register unsigned char *cursor;
18322 unsigned char *base;
18323
18324 register int ceiling;
18325 register unsigned char *ceiling_addr;
18326 int orig_count = count;
18327
18328 /* If we are not in selective display mode,
18329 check only for newlines. */
18330 int selective_display = (!NILP (current_buffer->selective_display)
18331 && !INTEGERP (current_buffer->selective_display));
18332
18333 if (count > 0)
18334 {
18335 while (start_byte < limit_byte)
18336 {
18337 ceiling = BUFFER_CEILING_OF (start_byte);
18338 ceiling = min (limit_byte - 1, ceiling);
18339 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
18340 base = (cursor = BYTE_POS_ADDR (start_byte));
18341 while (1)
18342 {
18343 if (selective_display)
18344 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
18345 ;
18346 else
18347 while (*cursor != '\n' && ++cursor != ceiling_addr)
18348 ;
18349
18350 if (cursor != ceiling_addr)
18351 {
18352 if (--count == 0)
18353 {
18354 start_byte += cursor - base + 1;
18355 *byte_pos_ptr = start_byte;
18356 return orig_count;
18357 }
18358 else
18359 if (++cursor == ceiling_addr)
18360 break;
18361 }
18362 else
18363 break;
18364 }
18365 start_byte += cursor - base;
18366 }
18367 }
18368 else
18369 {
18370 while (start_byte > limit_byte)
18371 {
18372 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
18373 ceiling = max (limit_byte, ceiling);
18374 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
18375 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
18376 while (1)
18377 {
18378 if (selective_display)
18379 while (--cursor != ceiling_addr
18380 && *cursor != '\n' && *cursor != 015)
18381 ;
18382 else
18383 while (--cursor != ceiling_addr && *cursor != '\n')
18384 ;
18385
18386 if (cursor != ceiling_addr)
18387 {
18388 if (++count == 0)
18389 {
18390 start_byte += cursor - base + 1;
18391 *byte_pos_ptr = start_byte;
18392 /* When scanning backwards, we should
18393 not count the newline posterior to which we stop. */
18394 return - orig_count - 1;
18395 }
18396 }
18397 else
18398 break;
18399 }
18400 /* Here we add 1 to compensate for the last decrement
18401 of CURSOR, which took it past the valid range. */
18402 start_byte += cursor - base + 1;
18403 }
18404 }
18405
18406 *byte_pos_ptr = limit_byte;
18407
18408 if (count < 0)
18409 return - orig_count + count;
18410 return orig_count - count;
18411
18412 }
18413
18414
18415 \f
18416 /***********************************************************************
18417 Displaying strings
18418 ***********************************************************************/
18419
18420 /* Display a NUL-terminated string, starting with index START.
18421
18422 If STRING is non-null, display that C string. Otherwise, the Lisp
18423 string LISP_STRING is displayed.
18424
18425 If FACE_STRING is not nil, FACE_STRING_POS is a position in
18426 FACE_STRING. Display STRING or LISP_STRING with the face at
18427 FACE_STRING_POS in FACE_STRING:
18428
18429 Display the string in the environment given by IT, but use the
18430 standard display table, temporarily.
18431
18432 FIELD_WIDTH is the minimum number of output glyphs to produce.
18433 If STRING has fewer characters than FIELD_WIDTH, pad to the right
18434 with spaces. If STRING has more characters, more than FIELD_WIDTH
18435 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
18436
18437 PRECISION is the maximum number of characters to output from
18438 STRING. PRECISION < 0 means don't truncate the string.
18439
18440 This is roughly equivalent to printf format specifiers:
18441
18442 FIELD_WIDTH PRECISION PRINTF
18443 ----------------------------------------
18444 -1 -1 %s
18445 -1 10 %.10s
18446 10 -1 %10s
18447 20 10 %20.10s
18448
18449 MULTIBYTE zero means do not display multibyte chars, > 0 means do
18450 display them, and < 0 means obey the current buffer's value of
18451 enable_multibyte_characters.
18452
18453 Value is the number of columns displayed. */
18454
18455 static int
18456 display_string (string, lisp_string, face_string, face_string_pos,
18457 start, it, field_width, precision, max_x, multibyte)
18458 unsigned char *string;
18459 Lisp_Object lisp_string;
18460 Lisp_Object face_string;
18461 int face_string_pos;
18462 int start;
18463 struct it *it;
18464 int field_width, precision, max_x;
18465 int multibyte;
18466 {
18467 int hpos_at_start = it->hpos;
18468 int saved_face_id = it->face_id;
18469 struct glyph_row *row = it->glyph_row;
18470
18471 /* Initialize the iterator IT for iteration over STRING beginning
18472 with index START. */
18473 reseat_to_string (it, string, lisp_string, start,
18474 precision, field_width, multibyte);
18475
18476 /* If displaying STRING, set up the face of the iterator
18477 from LISP_STRING, if that's given. */
18478 if (STRINGP (face_string))
18479 {
18480 int endptr;
18481 struct face *face;
18482
18483 it->face_id
18484 = face_at_string_position (it->w, face_string, face_string_pos,
18485 0, it->region_beg_charpos,
18486 it->region_end_charpos,
18487 &endptr, it->base_face_id, 0);
18488 face = FACE_FROM_ID (it->f, it->face_id);
18489 it->face_box_p = face->box != FACE_NO_BOX;
18490 }
18491
18492 /* Set max_x to the maximum allowed X position. Don't let it go
18493 beyond the right edge of the window. */
18494 if (max_x <= 0)
18495 max_x = it->last_visible_x;
18496 else
18497 max_x = min (max_x, it->last_visible_x);
18498
18499 /* Skip over display elements that are not visible. because IT->w is
18500 hscrolled. */
18501 if (it->current_x < it->first_visible_x)
18502 move_it_in_display_line_to (it, 100000, it->first_visible_x,
18503 MOVE_TO_POS | MOVE_TO_X);
18504
18505 row->ascent = it->max_ascent;
18506 row->height = it->max_ascent + it->max_descent;
18507 row->phys_ascent = it->max_phys_ascent;
18508 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
18509 row->extra_line_spacing = it->max_extra_line_spacing;
18510
18511 /* This condition is for the case that we are called with current_x
18512 past last_visible_x. */
18513 while (it->current_x < max_x)
18514 {
18515 int x_before, x, n_glyphs_before, i, nglyphs;
18516
18517 /* Get the next display element. */
18518 if (!get_next_display_element (it))
18519 break;
18520
18521 /* Produce glyphs. */
18522 x_before = it->current_x;
18523 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
18524 PRODUCE_GLYPHS (it);
18525
18526 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
18527 i = 0;
18528 x = x_before;
18529 while (i < nglyphs)
18530 {
18531 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
18532
18533 if (!it->truncate_lines_p
18534 && x + glyph->pixel_width > max_x)
18535 {
18536 /* End of continued line or max_x reached. */
18537 if (CHAR_GLYPH_PADDING_P (*glyph))
18538 {
18539 /* A wide character is unbreakable. */
18540 it->glyph_row->used[TEXT_AREA] = n_glyphs_before;
18541 it->current_x = x_before;
18542 }
18543 else
18544 {
18545 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
18546 it->current_x = x;
18547 }
18548 break;
18549 }
18550 else if (x + glyph->pixel_width >= it->first_visible_x)
18551 {
18552 /* Glyph is at least partially visible. */
18553 ++it->hpos;
18554 if (x < it->first_visible_x)
18555 it->glyph_row->x = x - it->first_visible_x;
18556 }
18557 else
18558 {
18559 /* Glyph is off the left margin of the display area.
18560 Should not happen. */
18561 abort ();
18562 }
18563
18564 row->ascent = max (row->ascent, it->max_ascent);
18565 row->height = max (row->height, it->max_ascent + it->max_descent);
18566 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
18567 row->phys_height = max (row->phys_height,
18568 it->max_phys_ascent + it->max_phys_descent);
18569 row->extra_line_spacing = max (row->extra_line_spacing,
18570 it->max_extra_line_spacing);
18571 x += glyph->pixel_width;
18572 ++i;
18573 }
18574
18575 /* Stop if max_x reached. */
18576 if (i < nglyphs)
18577 break;
18578
18579 /* Stop at line ends. */
18580 if (ITERATOR_AT_END_OF_LINE_P (it))
18581 {
18582 it->continuation_lines_width = 0;
18583 break;
18584 }
18585
18586 set_iterator_to_next (it, 1);
18587
18588 /* Stop if truncating at the right edge. */
18589 if (it->truncate_lines_p
18590 && it->current_x >= it->last_visible_x)
18591 {
18592 /* Add truncation mark, but don't do it if the line is
18593 truncated at a padding space. */
18594 if (IT_CHARPOS (*it) < it->string_nchars)
18595 {
18596 if (!FRAME_WINDOW_P (it->f))
18597 {
18598 int i, n;
18599
18600 if (it->current_x > it->last_visible_x)
18601 {
18602 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
18603 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
18604 break;
18605 for (n = row->used[TEXT_AREA]; i < n; ++i)
18606 {
18607 row->used[TEXT_AREA] = i;
18608 produce_special_glyphs (it, IT_TRUNCATION);
18609 }
18610 }
18611 produce_special_glyphs (it, IT_TRUNCATION);
18612 }
18613 it->glyph_row->truncated_on_right_p = 1;
18614 }
18615 break;
18616 }
18617 }
18618
18619 /* Maybe insert a truncation at the left. */
18620 if (it->first_visible_x
18621 && IT_CHARPOS (*it) > 0)
18622 {
18623 if (!FRAME_WINDOW_P (it->f))
18624 insert_left_trunc_glyphs (it);
18625 it->glyph_row->truncated_on_left_p = 1;
18626 }
18627
18628 it->face_id = saved_face_id;
18629
18630 /* Value is number of columns displayed. */
18631 return it->hpos - hpos_at_start;
18632 }
18633
18634
18635 \f
18636 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
18637 appears as an element of LIST or as the car of an element of LIST.
18638 If PROPVAL is a list, compare each element against LIST in that
18639 way, and return 1/2 if any element of PROPVAL is found in LIST.
18640 Otherwise return 0. This function cannot quit.
18641 The return value is 2 if the text is invisible but with an ellipsis
18642 and 1 if it's invisible and without an ellipsis. */
18643
18644 int
18645 invisible_p (propval, list)
18646 register Lisp_Object propval;
18647 Lisp_Object list;
18648 {
18649 register Lisp_Object tail, proptail;
18650
18651 for (tail = list; CONSP (tail); tail = XCDR (tail))
18652 {
18653 register Lisp_Object tem;
18654 tem = XCAR (tail);
18655 if (EQ (propval, tem))
18656 return 1;
18657 if (CONSP (tem) && EQ (propval, XCAR (tem)))
18658 return NILP (XCDR (tem)) ? 1 : 2;
18659 }
18660
18661 if (CONSP (propval))
18662 {
18663 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
18664 {
18665 Lisp_Object propelt;
18666 propelt = XCAR (proptail);
18667 for (tail = list; CONSP (tail); tail = XCDR (tail))
18668 {
18669 register Lisp_Object tem;
18670 tem = XCAR (tail);
18671 if (EQ (propelt, tem))
18672 return 1;
18673 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
18674 return NILP (XCDR (tem)) ? 1 : 2;
18675 }
18676 }
18677 }
18678
18679 return 0;
18680 }
18681
18682 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
18683 doc: /* Non-nil if the property makes the text invisible.
18684 POS-OR-PROP can be a marker or number, in which case it is taken to be
18685 a position in the current buffer and the value of the `invisible' property
18686 is checked; or it can be some other value, which is then presumed to be the
18687 value of the `invisible' property of the text of interest.
18688 The non-nil value returned can be t for truly invisible text or something
18689 else if the text is replaced by an ellipsis. */)
18690 (pos_or_prop)
18691 Lisp_Object pos_or_prop;
18692 {
18693 Lisp_Object prop
18694 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
18695 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
18696 : pos_or_prop);
18697 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
18698 return (invis == 0 ? Qnil
18699 : invis == 1 ? Qt
18700 : make_number (invis));
18701 }
18702
18703 /* Calculate a width or height in pixels from a specification using
18704 the following elements:
18705
18706 SPEC ::=
18707 NUM - a (fractional) multiple of the default font width/height
18708 (NUM) - specifies exactly NUM pixels
18709 UNIT - a fixed number of pixels, see below.
18710 ELEMENT - size of a display element in pixels, see below.
18711 (NUM . SPEC) - equals NUM * SPEC
18712 (+ SPEC SPEC ...) - add pixel values
18713 (- SPEC SPEC ...) - subtract pixel values
18714 (- SPEC) - negate pixel value
18715
18716 NUM ::=
18717 INT or FLOAT - a number constant
18718 SYMBOL - use symbol's (buffer local) variable binding.
18719
18720 UNIT ::=
18721 in - pixels per inch *)
18722 mm - pixels per 1/1000 meter *)
18723 cm - pixels per 1/100 meter *)
18724 width - width of current font in pixels.
18725 height - height of current font in pixels.
18726
18727 *) using the ratio(s) defined in display-pixels-per-inch.
18728
18729 ELEMENT ::=
18730
18731 left-fringe - left fringe width in pixels
18732 right-fringe - right fringe width in pixels
18733
18734 left-margin - left margin width in pixels
18735 right-margin - right margin width in pixels
18736
18737 scroll-bar - scroll-bar area width in pixels
18738
18739 Examples:
18740
18741 Pixels corresponding to 5 inches:
18742 (5 . in)
18743
18744 Total width of non-text areas on left side of window (if scroll-bar is on left):
18745 '(space :width (+ left-fringe left-margin scroll-bar))
18746
18747 Align to first text column (in header line):
18748 '(space :align-to 0)
18749
18750 Align to middle of text area minus half the width of variable `my-image'
18751 containing a loaded image:
18752 '(space :align-to (0.5 . (- text my-image)))
18753
18754 Width of left margin minus width of 1 character in the default font:
18755 '(space :width (- left-margin 1))
18756
18757 Width of left margin minus width of 2 characters in the current font:
18758 '(space :width (- left-margin (2 . width)))
18759
18760 Center 1 character over left-margin (in header line):
18761 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
18762
18763 Different ways to express width of left fringe plus left margin minus one pixel:
18764 '(space :width (- (+ left-fringe left-margin) (1)))
18765 '(space :width (+ left-fringe left-margin (- (1))))
18766 '(space :width (+ left-fringe left-margin (-1)))
18767
18768 */
18769
18770 #define NUMVAL(X) \
18771 ((INTEGERP (X) || FLOATP (X)) \
18772 ? XFLOATINT (X) \
18773 : - 1)
18774
18775 int
18776 calc_pixel_width_or_height (res, it, prop, font, width_p, align_to)
18777 double *res;
18778 struct it *it;
18779 Lisp_Object prop;
18780 void *font;
18781 int width_p, *align_to;
18782 {
18783 double pixels;
18784
18785 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
18786 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
18787
18788 if (NILP (prop))
18789 return OK_PIXELS (0);
18790
18791 xassert (FRAME_LIVE_P (it->f));
18792
18793 if (SYMBOLP (prop))
18794 {
18795 if (SCHARS (SYMBOL_NAME (prop)) == 2)
18796 {
18797 char *unit = SDATA (SYMBOL_NAME (prop));
18798
18799 if (unit[0] == 'i' && unit[1] == 'n')
18800 pixels = 1.0;
18801 else if (unit[0] == 'm' && unit[1] == 'm')
18802 pixels = 25.4;
18803 else if (unit[0] == 'c' && unit[1] == 'm')
18804 pixels = 2.54;
18805 else
18806 pixels = 0;
18807 if (pixels > 0)
18808 {
18809 double ppi;
18810 #ifdef HAVE_WINDOW_SYSTEM
18811 if (FRAME_WINDOW_P (it->f)
18812 && (ppi = (width_p
18813 ? FRAME_X_DISPLAY_INFO (it->f)->resx
18814 : FRAME_X_DISPLAY_INFO (it->f)->resy),
18815 ppi > 0))
18816 return OK_PIXELS (ppi / pixels);
18817 #endif
18818
18819 if ((ppi = NUMVAL (Vdisplay_pixels_per_inch), ppi > 0)
18820 || (CONSP (Vdisplay_pixels_per_inch)
18821 && (ppi = (width_p
18822 ? NUMVAL (XCAR (Vdisplay_pixels_per_inch))
18823 : NUMVAL (XCDR (Vdisplay_pixels_per_inch))),
18824 ppi > 0)))
18825 return OK_PIXELS (ppi / pixels);
18826
18827 return 0;
18828 }
18829 }
18830
18831 #ifdef HAVE_WINDOW_SYSTEM
18832 if (EQ (prop, Qheight))
18833 return OK_PIXELS (font ? FONT_HEIGHT ((XFontStruct *)font) : FRAME_LINE_HEIGHT (it->f));
18834 if (EQ (prop, Qwidth))
18835 return OK_PIXELS (font ? FONT_WIDTH ((XFontStruct *)font) : FRAME_COLUMN_WIDTH (it->f));
18836 #else
18837 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
18838 return OK_PIXELS (1);
18839 #endif
18840
18841 if (EQ (prop, Qtext))
18842 return OK_PIXELS (width_p
18843 ? window_box_width (it->w, TEXT_AREA)
18844 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
18845
18846 if (align_to && *align_to < 0)
18847 {
18848 *res = 0;
18849 if (EQ (prop, Qleft))
18850 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
18851 if (EQ (prop, Qright))
18852 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
18853 if (EQ (prop, Qcenter))
18854 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
18855 + window_box_width (it->w, TEXT_AREA) / 2);
18856 if (EQ (prop, Qleft_fringe))
18857 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
18858 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
18859 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
18860 if (EQ (prop, Qright_fringe))
18861 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
18862 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
18863 : window_box_right_offset (it->w, TEXT_AREA));
18864 if (EQ (prop, Qleft_margin))
18865 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
18866 if (EQ (prop, Qright_margin))
18867 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
18868 if (EQ (prop, Qscroll_bar))
18869 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
18870 ? 0
18871 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
18872 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
18873 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
18874 : 0)));
18875 }
18876 else
18877 {
18878 if (EQ (prop, Qleft_fringe))
18879 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
18880 if (EQ (prop, Qright_fringe))
18881 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
18882 if (EQ (prop, Qleft_margin))
18883 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
18884 if (EQ (prop, Qright_margin))
18885 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
18886 if (EQ (prop, Qscroll_bar))
18887 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
18888 }
18889
18890 prop = Fbuffer_local_value (prop, it->w->buffer);
18891 }
18892
18893 if (INTEGERP (prop) || FLOATP (prop))
18894 {
18895 int base_unit = (width_p
18896 ? FRAME_COLUMN_WIDTH (it->f)
18897 : FRAME_LINE_HEIGHT (it->f));
18898 return OK_PIXELS (XFLOATINT (prop) * base_unit);
18899 }
18900
18901 if (CONSP (prop))
18902 {
18903 Lisp_Object car = XCAR (prop);
18904 Lisp_Object cdr = XCDR (prop);
18905
18906 if (SYMBOLP (car))
18907 {
18908 #ifdef HAVE_WINDOW_SYSTEM
18909 if (FRAME_WINDOW_P (it->f)
18910 && valid_image_p (prop))
18911 {
18912 int id = lookup_image (it->f, prop);
18913 struct image *img = IMAGE_FROM_ID (it->f, id);
18914
18915 return OK_PIXELS (width_p ? img->width : img->height);
18916 }
18917 #endif
18918 if (EQ (car, Qplus) || EQ (car, Qminus))
18919 {
18920 int first = 1;
18921 double px;
18922
18923 pixels = 0;
18924 while (CONSP (cdr))
18925 {
18926 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
18927 font, width_p, align_to))
18928 return 0;
18929 if (first)
18930 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
18931 else
18932 pixels += px;
18933 cdr = XCDR (cdr);
18934 }
18935 if (EQ (car, Qminus))
18936 pixels = -pixels;
18937 return OK_PIXELS (pixels);
18938 }
18939
18940 car = Fbuffer_local_value (car, it->w->buffer);
18941 }
18942
18943 if (INTEGERP (car) || FLOATP (car))
18944 {
18945 double fact;
18946 pixels = XFLOATINT (car);
18947 if (NILP (cdr))
18948 return OK_PIXELS (pixels);
18949 if (calc_pixel_width_or_height (&fact, it, cdr,
18950 font, width_p, align_to))
18951 return OK_PIXELS (pixels * fact);
18952 return 0;
18953 }
18954
18955 return 0;
18956 }
18957
18958 return 0;
18959 }
18960
18961 \f
18962 /***********************************************************************
18963 Glyph Display
18964 ***********************************************************************/
18965
18966 #ifdef HAVE_WINDOW_SYSTEM
18967
18968 #if GLYPH_DEBUG
18969
18970 void
18971 dump_glyph_string (s)
18972 struct glyph_string *s;
18973 {
18974 fprintf (stderr, "glyph string\n");
18975 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
18976 s->x, s->y, s->width, s->height);
18977 fprintf (stderr, " ybase = %d\n", s->ybase);
18978 fprintf (stderr, " hl = %d\n", s->hl);
18979 fprintf (stderr, " left overhang = %d, right = %d\n",
18980 s->left_overhang, s->right_overhang);
18981 fprintf (stderr, " nchars = %d\n", s->nchars);
18982 fprintf (stderr, " extends to end of line = %d\n",
18983 s->extends_to_end_of_line_p);
18984 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
18985 fprintf (stderr, " bg width = %d\n", s->background_width);
18986 }
18987
18988 #endif /* GLYPH_DEBUG */
18989
18990 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
18991 of XChar2b structures for S; it can't be allocated in
18992 init_glyph_string because it must be allocated via `alloca'. W
18993 is the window on which S is drawn. ROW and AREA are the glyph row
18994 and area within the row from which S is constructed. START is the
18995 index of the first glyph structure covered by S. HL is a
18996 face-override for drawing S. */
18997
18998 #ifdef HAVE_NTGUI
18999 #define OPTIONAL_HDC(hdc) hdc,
19000 #define DECLARE_HDC(hdc) HDC hdc;
19001 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
19002 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
19003 #endif
19004
19005 #ifndef OPTIONAL_HDC
19006 #define OPTIONAL_HDC(hdc)
19007 #define DECLARE_HDC(hdc)
19008 #define ALLOCATE_HDC(hdc, f)
19009 #define RELEASE_HDC(hdc, f)
19010 #endif
19011
19012 static void
19013 init_glyph_string (s, OPTIONAL_HDC (hdc) char2b, w, row, area, start, hl)
19014 struct glyph_string *s;
19015 DECLARE_HDC (hdc)
19016 XChar2b *char2b;
19017 struct window *w;
19018 struct glyph_row *row;
19019 enum glyph_row_area area;
19020 int start;
19021 enum draw_glyphs_face hl;
19022 {
19023 bzero (s, sizeof *s);
19024 s->w = w;
19025 s->f = XFRAME (w->frame);
19026 #ifdef HAVE_NTGUI
19027 s->hdc = hdc;
19028 #endif
19029 s->display = FRAME_X_DISPLAY (s->f);
19030 s->window = FRAME_X_WINDOW (s->f);
19031 s->char2b = char2b;
19032 s->hl = hl;
19033 s->row = row;
19034 s->area = area;
19035 s->first_glyph = row->glyphs[area] + start;
19036 s->height = row->height;
19037 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
19038
19039 /* Display the internal border below the tool-bar window. */
19040 if (WINDOWP (s->f->tool_bar_window)
19041 && s->w == XWINDOW (s->f->tool_bar_window))
19042 s->y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
19043
19044 s->ybase = s->y + row->ascent;
19045 }
19046
19047
19048 /* Append the list of glyph strings with head H and tail T to the list
19049 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
19050
19051 static INLINE void
19052 append_glyph_string_lists (head, tail, h, t)
19053 struct glyph_string **head, **tail;
19054 struct glyph_string *h, *t;
19055 {
19056 if (h)
19057 {
19058 if (*head)
19059 (*tail)->next = h;
19060 else
19061 *head = h;
19062 h->prev = *tail;
19063 *tail = t;
19064 }
19065 }
19066
19067
19068 /* Prepend the list of glyph strings with head H and tail T to the
19069 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
19070 result. */
19071
19072 static INLINE void
19073 prepend_glyph_string_lists (head, tail, h, t)
19074 struct glyph_string **head, **tail;
19075 struct glyph_string *h, *t;
19076 {
19077 if (h)
19078 {
19079 if (*head)
19080 (*head)->prev = t;
19081 else
19082 *tail = t;
19083 t->next = *head;
19084 *head = h;
19085 }
19086 }
19087
19088
19089 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
19090 Set *HEAD and *TAIL to the resulting list. */
19091
19092 static INLINE void
19093 append_glyph_string (head, tail, s)
19094 struct glyph_string **head, **tail;
19095 struct glyph_string *s;
19096 {
19097 s->next = s->prev = NULL;
19098 append_glyph_string_lists (head, tail, s, s);
19099 }
19100
19101
19102 /* Get face and two-byte form of character C in face FACE_ID on frame
19103 F. The encoding of C is returned in *CHAR2B. MULTIBYTE_P non-zero
19104 means we want to display multibyte text. DISPLAY_P non-zero means
19105 make sure that X resources for the face returned are allocated.
19106 Value is a pointer to a realized face that is ready for display if
19107 DISPLAY_P is non-zero. */
19108
19109 static INLINE struct face *
19110 get_char_face_and_encoding (f, c, face_id, char2b, multibyte_p, display_p)
19111 struct frame *f;
19112 int c, face_id;
19113 XChar2b *char2b;
19114 int multibyte_p, display_p;
19115 {
19116 struct face *face = FACE_FROM_ID (f, face_id);
19117
19118 #ifdef USE_FONT_BACKEND
19119 if (enable_font_backend)
19120 {
19121 struct font *font = (struct font *) face->font_info;
19122
19123 if (font)
19124 {
19125 unsigned code = font->driver->encode_char (font, c);
19126
19127 if (code != FONT_INVALID_CODE)
19128 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
19129 else
19130 STORE_XCHAR2B (char2b, 0, 0);
19131 }
19132 }
19133 else
19134 #endif /* USE_FONT_BACKEND */
19135 if (!multibyte_p)
19136 {
19137 /* Unibyte case. We don't have to encode, but we have to make
19138 sure to use a face suitable for unibyte. */
19139 STORE_XCHAR2B (char2b, 0, c);
19140 face_id = FACE_FOR_CHAR (f, face, c, -1, Qnil);
19141 face = FACE_FROM_ID (f, face_id);
19142 }
19143 else if (c < 128)
19144 {
19145 /* Case of ASCII in a face known to fit ASCII. */
19146 STORE_XCHAR2B (char2b, 0, c);
19147 }
19148 else if (face->font != NULL)
19149 {
19150 struct font_info *font_info
19151 = FONT_INFO_FROM_ID (f, face->font_info_id);
19152 struct charset *charset = CHARSET_FROM_ID (font_info->charset);
19153 unsigned code = ENCODE_CHAR (charset, c);
19154
19155 if (CHARSET_DIMENSION (charset) == 1)
19156 STORE_XCHAR2B (char2b, 0, code);
19157 else
19158 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
19159 /* Maybe encode the character in *CHAR2B. */
19160 FRAME_RIF (f)->encode_char (c, char2b, font_info, charset, NULL);
19161 }
19162
19163 /* Make sure X resources of the face are allocated. */
19164 #ifdef HAVE_X_WINDOWS
19165 if (display_p)
19166 #endif
19167 {
19168 xassert (face != NULL);
19169 PREPARE_FACE_FOR_DISPLAY (f, face);
19170 }
19171
19172 return face;
19173 }
19174
19175
19176 /* Get face and two-byte form of character glyph GLYPH on frame F.
19177 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
19178 a pointer to a realized face that is ready for display. */
19179
19180 static INLINE struct face *
19181 get_glyph_face_and_encoding (f, glyph, char2b, two_byte_p)
19182 struct frame *f;
19183 struct glyph *glyph;
19184 XChar2b *char2b;
19185 int *two_byte_p;
19186 {
19187 struct face *face;
19188
19189 xassert (glyph->type == CHAR_GLYPH);
19190 face = FACE_FROM_ID (f, glyph->face_id);
19191
19192 if (two_byte_p)
19193 *two_byte_p = 0;
19194
19195 #ifdef USE_FONT_BACKEND
19196 if (enable_font_backend)
19197 {
19198 struct font *font = (struct font *) face->font_info;
19199
19200 if (font)
19201 {
19202 unsigned code = font->driver->encode_char (font, glyph->u.ch);
19203
19204 if (code != FONT_INVALID_CODE)
19205 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
19206 else
19207 STORE_XCHAR2B (char2b, 0, code);
19208 }
19209 }
19210 else
19211 #endif /* USE_FONT_BACKEND */
19212 if (!glyph->multibyte_p)
19213 {
19214 /* Unibyte case. We don't have to encode, but we have to make
19215 sure to use a face suitable for unibyte. */
19216 STORE_XCHAR2B (char2b, 0, glyph->u.ch);
19217 }
19218 else if (glyph->u.ch < 128)
19219 {
19220 /* Case of ASCII in a face known to fit ASCII. */
19221 STORE_XCHAR2B (char2b, 0, glyph->u.ch);
19222 }
19223 else
19224 {
19225 struct font_info *font_info
19226 = FONT_INFO_FROM_ID (f, face->font_info_id);
19227 if (font_info)
19228 {
19229 struct charset *charset = CHARSET_FROM_ID (font_info->charset);
19230 unsigned code = ENCODE_CHAR (charset, glyph->u.ch);
19231
19232 if (CHARSET_DIMENSION (charset) == 1)
19233 STORE_XCHAR2B (char2b, 0, code);
19234 else
19235 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
19236
19237 /* Maybe encode the character in *CHAR2B. */
19238 if (CHARSET_ID (charset) != charset_ascii)
19239 {
19240 glyph->font_type
19241 = FRAME_RIF (f)->encode_char (glyph->u.ch, char2b, font_info,
19242 charset, two_byte_p);
19243 }
19244 }
19245 }
19246
19247 /* Make sure X resources of the face are allocated. */
19248 xassert (face != NULL);
19249 PREPARE_FACE_FOR_DISPLAY (f, face);
19250 return face;
19251 }
19252
19253
19254 /* Fill glyph string S with composition components specified by S->cmp.
19255
19256 BASE_FACE is the base face of the composition.
19257 S->gidx is the index of the first component for S.
19258
19259 OVERLAPS non-zero means S should draw the foreground only, and use
19260 its physical height for clipping. See also draw_glyphs.
19261
19262 Value is the index of a component not in S. */
19263
19264 static int
19265 fill_composite_glyph_string (s, base_face, overlaps)
19266 struct glyph_string *s;
19267 struct face *base_face;
19268 int overlaps;
19269 {
19270 int i;
19271
19272 xassert (s);
19273
19274 s->for_overlaps = overlaps;
19275
19276 #ifdef USE_FONT_BACKEND
19277 if (enable_font_backend && s->cmp->method == COMPOSITION_WITH_GLYPH_STRING)
19278 {
19279 Lisp_Object gstring
19280 = AREF (XHASH_TABLE (composition_hash_table)->key_and_value,
19281 s->cmp->hash_index * 2);
19282
19283 s->face = base_face;
19284 s->font_info = s->cmp->font;
19285 s->font = s->font_info->font;
19286 for (i = 0, s->nchars = 0; i < s->cmp->glyph_len; i++, s->nchars++)
19287 {
19288 Lisp_Object g = LGSTRING_GLYPH (gstring, i);
19289 unsigned code;
19290 XChar2b * store_pos;
19291 if (NILP (g))
19292 break;
19293 code = LGLYPH_CODE (g);
19294 store_pos = s->char2b + i;
19295 STORE_XCHAR2B (store_pos, code >> 8, code & 0xFF);
19296 }
19297 s->width = s->cmp->pixel_width;
19298 }
19299 else
19300 #endif /* USE_FONT_BACKEND */
19301 {
19302 /* For all glyphs of this composition, starting at the offset
19303 S->gidx, until we reach the end of the definition or encounter a
19304 glyph that requires the different face, add it to S. */
19305 struct face *face;
19306
19307 s->face = NULL;
19308 s->font = NULL;
19309 s->font_info = NULL;
19310 for (i = s->gidx; i < s->cmp->glyph_len; i++)
19311 {
19312 int c = COMPOSITION_GLYPH (s->cmp, i);
19313
19314 if (c != '\t')
19315 {
19316 int face_id = FACE_FOR_CHAR (s->f, base_face, c, -1, Qnil);
19317
19318 face = get_char_face_and_encoding (s->f, c, face_id,
19319 s->char2b + i, 1, 1);
19320 if (face)
19321 {
19322 if (! s->face)
19323 {
19324 s->face = face;
19325 s->font = s->face->font;
19326 s->font_info = FONT_INFO_FROM_FACE (s->f, s->face);
19327 }
19328 else if (s->face != face)
19329 break;
19330 }
19331 }
19332 ++s->nchars;
19333 }
19334
19335 /* All glyph strings for the same composition has the same width,
19336 i.e. the width set for the first component of the composition. */
19337 s->width = s->first_glyph->pixel_width;
19338 }
19339
19340 /* If the specified font could not be loaded, use the frame's
19341 default font, but record the fact that we couldn't load it in
19342 the glyph string so that we can draw rectangles for the
19343 characters of the glyph string. */
19344 if (s->font == NULL)
19345 {
19346 s->font_not_found_p = 1;
19347 s->font = FRAME_FONT (s->f);
19348 }
19349
19350 /* Adjust base line for subscript/superscript text. */
19351 s->ybase += s->first_glyph->voffset;
19352
19353 /* This glyph string must always be drawn with 16-bit functions. */
19354 s->two_byte_p = 1;
19355
19356 return s->gidx + s->nchars;
19357 }
19358
19359
19360 /* Fill glyph string S from a sequence of character glyphs.
19361
19362 FACE_ID is the face id of the string. START is the index of the
19363 first glyph to consider, END is the index of the last + 1.
19364 OVERLAPS non-zero means S should draw the foreground only, and use
19365 its physical height for clipping. See also draw_glyphs.
19366
19367 Value is the index of the first glyph not in S. */
19368
19369 static int
19370 fill_glyph_string (s, face_id, start, end, overlaps)
19371 struct glyph_string *s;
19372 int face_id;
19373 int start, end, overlaps;
19374 {
19375 struct glyph *glyph, *last;
19376 int voffset;
19377 int glyph_not_available_p;
19378
19379 xassert (s->f == XFRAME (s->w->frame));
19380 xassert (s->nchars == 0);
19381 xassert (start >= 0 && end > start);
19382
19383 s->for_overlaps = overlaps,
19384 glyph = s->row->glyphs[s->area] + start;
19385 last = s->row->glyphs[s->area] + end;
19386 voffset = glyph->voffset;
19387
19388 glyph_not_available_p = glyph->glyph_not_available_p;
19389
19390 while (glyph < last
19391 && glyph->type == CHAR_GLYPH
19392 && glyph->voffset == voffset
19393 /* Same face id implies same font, nowadays. */
19394 && glyph->face_id == face_id
19395 && glyph->glyph_not_available_p == glyph_not_available_p)
19396 {
19397 int two_byte_p;
19398
19399 s->face = get_glyph_face_and_encoding (s->f, glyph,
19400 s->char2b + s->nchars,
19401 &two_byte_p);
19402 s->two_byte_p = two_byte_p;
19403 ++s->nchars;
19404 xassert (s->nchars <= end - start);
19405 s->width += glyph->pixel_width;
19406 ++glyph;
19407 }
19408
19409 s->font = s->face->font;
19410 s->font_info = FONT_INFO_FROM_FACE (s->f, s->face);
19411
19412 /* If the specified font could not be loaded, use the frame's font,
19413 but record the fact that we couldn't load it in
19414 S->font_not_found_p so that we can draw rectangles for the
19415 characters of the glyph string. */
19416 if (s->font == NULL || glyph_not_available_p)
19417 {
19418 s->font_not_found_p = 1;
19419 s->font = FRAME_FONT (s->f);
19420 }
19421
19422 /* Adjust base line for subscript/superscript text. */
19423 s->ybase += voffset;
19424
19425 xassert (s->face && s->face->gc);
19426 return glyph - s->row->glyphs[s->area];
19427 }
19428
19429
19430 /* Fill glyph string S from image glyph S->first_glyph. */
19431
19432 static void
19433 fill_image_glyph_string (s)
19434 struct glyph_string *s;
19435 {
19436 xassert (s->first_glyph->type == IMAGE_GLYPH);
19437 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
19438 xassert (s->img);
19439 s->slice = s->first_glyph->slice;
19440 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
19441 s->font = s->face->font;
19442 s->width = s->first_glyph->pixel_width;
19443
19444 /* Adjust base line for subscript/superscript text. */
19445 s->ybase += s->first_glyph->voffset;
19446 }
19447
19448
19449 /* Fill glyph string S from a sequence of stretch glyphs.
19450
19451 ROW is the glyph row in which the glyphs are found, AREA is the
19452 area within the row. START is the index of the first glyph to
19453 consider, END is the index of the last + 1.
19454
19455 Value is the index of the first glyph not in S. */
19456
19457 static int
19458 fill_stretch_glyph_string (s, row, area, start, end)
19459 struct glyph_string *s;
19460 struct glyph_row *row;
19461 enum glyph_row_area area;
19462 int start, end;
19463 {
19464 struct glyph *glyph, *last;
19465 int voffset, face_id;
19466
19467 xassert (s->first_glyph->type == STRETCH_GLYPH);
19468
19469 glyph = s->row->glyphs[s->area] + start;
19470 last = s->row->glyphs[s->area] + end;
19471 face_id = glyph->face_id;
19472 s->face = FACE_FROM_ID (s->f, face_id);
19473 s->font = s->face->font;
19474 s->font_info = FONT_INFO_FROM_FACE (s->f, s->face);
19475 s->width = glyph->pixel_width;
19476 s->nchars = 1;
19477 voffset = glyph->voffset;
19478
19479 for (++glyph;
19480 (glyph < last
19481 && glyph->type == STRETCH_GLYPH
19482 && glyph->voffset == voffset
19483 && glyph->face_id == face_id);
19484 ++glyph)
19485 s->width += glyph->pixel_width;
19486
19487 /* Adjust base line for subscript/superscript text. */
19488 s->ybase += voffset;
19489
19490 /* The case that face->gc == 0 is handled when drawing the glyph
19491 string by calling PREPARE_FACE_FOR_DISPLAY. */
19492 xassert (s->face);
19493 return glyph - s->row->glyphs[s->area];
19494 }
19495
19496 static XCharStruct *
19497 get_per_char_metric (f, font, font_info, char2b, font_type)
19498 struct frame *f;
19499 XFontStruct *font;
19500 struct font_info *font_info;
19501 XChar2b *char2b;
19502 int font_type;
19503 {
19504 #ifdef USE_FONT_BACKEND
19505 if (enable_font_backend)
19506 {
19507 static XCharStruct pcm_value;
19508 unsigned code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
19509 struct font *fontp;
19510 struct font_metrics metrics;
19511
19512 if (! font_info || code == FONT_INVALID_CODE)
19513 return NULL;
19514 fontp = (struct font *) font_info;
19515 fontp->driver->text_extents (fontp, &code, 1, &metrics);
19516 pcm_value.lbearing = metrics.lbearing;
19517 pcm_value.rbearing = metrics.rbearing;
19518 pcm_value.ascent = metrics.ascent;
19519 pcm_value.descent = metrics.descent;
19520 pcm_value.width = metrics.width;
19521 return &pcm_value;
19522 }
19523 #endif /* USE_FONT_BACKEND */
19524 return FRAME_RIF (f)->per_char_metric (font, char2b, font_type);
19525 }
19526
19527 /* EXPORT for RIF:
19528 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
19529 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
19530 assumed to be zero. */
19531
19532 void
19533 x_get_glyph_overhangs (glyph, f, left, right)
19534 struct glyph *glyph;
19535 struct frame *f;
19536 int *left, *right;
19537 {
19538 *left = *right = 0;
19539
19540 if (glyph->type == CHAR_GLYPH)
19541 {
19542 XFontStruct *font;
19543 struct face *face;
19544 struct font_info *font_info;
19545 XChar2b char2b;
19546 XCharStruct *pcm;
19547
19548 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
19549 font = face->font;
19550 font_info = FONT_INFO_FROM_FACE (f, face);
19551 if (font /* ++KFS: Should this be font_info ? */
19552 && (pcm = get_per_char_metric (f, font, font_info, &char2b, glyph->font_type)))
19553 {
19554 if (pcm->rbearing > pcm->width)
19555 *right = pcm->rbearing - pcm->width;
19556 if (pcm->lbearing < 0)
19557 *left = -pcm->lbearing;
19558 }
19559 }
19560 else if (glyph->type == COMPOSITE_GLYPH)
19561 {
19562 struct composition *cmp = composition_table[glyph->u.cmp_id];
19563
19564 *right = cmp->rbearing - cmp->pixel_width;
19565 *left = - cmp->lbearing;
19566 }
19567 }
19568
19569
19570 /* Return the index of the first glyph preceding glyph string S that
19571 is overwritten by S because of S's left overhang. Value is -1
19572 if no glyphs are overwritten. */
19573
19574 static int
19575 left_overwritten (s)
19576 struct glyph_string *s;
19577 {
19578 int k;
19579
19580 if (s->left_overhang)
19581 {
19582 int x = 0, i;
19583 struct glyph *glyphs = s->row->glyphs[s->area];
19584 int first = s->first_glyph - glyphs;
19585
19586 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
19587 x -= glyphs[i].pixel_width;
19588
19589 k = i + 1;
19590 }
19591 else
19592 k = -1;
19593
19594 return k;
19595 }
19596
19597
19598 /* Return the index of the first glyph preceding glyph string S that
19599 is overwriting S because of its right overhang. Value is -1 if no
19600 glyph in front of S overwrites S. */
19601
19602 static int
19603 left_overwriting (s)
19604 struct glyph_string *s;
19605 {
19606 int i, k, x;
19607 struct glyph *glyphs = s->row->glyphs[s->area];
19608 int first = s->first_glyph - glyphs;
19609
19610 k = -1;
19611 x = 0;
19612 for (i = first - 1; i >= 0; --i)
19613 {
19614 int left, right;
19615 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
19616 if (x + right > 0)
19617 k = i;
19618 x -= glyphs[i].pixel_width;
19619 }
19620
19621 return k;
19622 }
19623
19624
19625 /* Return the index of the last glyph following glyph string S that is
19626 not overwritten by S because of S's right overhang. Value is -1 if
19627 no such glyph is found. */
19628
19629 static int
19630 right_overwritten (s)
19631 struct glyph_string *s;
19632 {
19633 int k = -1;
19634
19635 if (s->right_overhang)
19636 {
19637 int x = 0, i;
19638 struct glyph *glyphs = s->row->glyphs[s->area];
19639 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
19640 int end = s->row->used[s->area];
19641
19642 for (i = first; i < end && s->right_overhang > x; ++i)
19643 x += glyphs[i].pixel_width;
19644
19645 k = i;
19646 }
19647
19648 return k;
19649 }
19650
19651
19652 /* Return the index of the last glyph following glyph string S that
19653 overwrites S because of its left overhang. Value is negative
19654 if no such glyph is found. */
19655
19656 static int
19657 right_overwriting (s)
19658 struct glyph_string *s;
19659 {
19660 int i, k, x;
19661 int end = s->row->used[s->area];
19662 struct glyph *glyphs = s->row->glyphs[s->area];
19663 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
19664
19665 k = -1;
19666 x = 0;
19667 for (i = first; i < end; ++i)
19668 {
19669 int left, right;
19670 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
19671 if (x - left < 0)
19672 k = i;
19673 x += glyphs[i].pixel_width;
19674 }
19675
19676 return k;
19677 }
19678
19679
19680 /* Set background width of glyph string S. START is the index of the
19681 first glyph following S. LAST_X is the right-most x-position + 1
19682 in the drawing area. */
19683
19684 static INLINE void
19685 set_glyph_string_background_width (s, start, last_x)
19686 struct glyph_string *s;
19687 int start;
19688 int last_x;
19689 {
19690 /* If the face of this glyph string has to be drawn to the end of
19691 the drawing area, set S->extends_to_end_of_line_p. */
19692
19693 if (start == s->row->used[s->area]
19694 && s->area == TEXT_AREA
19695 && ((s->row->fill_line_p
19696 && (s->hl == DRAW_NORMAL_TEXT
19697 || s->hl == DRAW_IMAGE_RAISED
19698 || s->hl == DRAW_IMAGE_SUNKEN))
19699 || s->hl == DRAW_MOUSE_FACE))
19700 s->extends_to_end_of_line_p = 1;
19701
19702 /* If S extends its face to the end of the line, set its
19703 background_width to the distance to the right edge of the drawing
19704 area. */
19705 if (s->extends_to_end_of_line_p)
19706 s->background_width = last_x - s->x + 1;
19707 else
19708 s->background_width = s->width;
19709 }
19710
19711
19712 /* Compute overhangs and x-positions for glyph string S and its
19713 predecessors, or successors. X is the starting x-position for S.
19714 BACKWARD_P non-zero means process predecessors. */
19715
19716 static void
19717 compute_overhangs_and_x (s, x, backward_p)
19718 struct glyph_string *s;
19719 int x;
19720 int backward_p;
19721 {
19722 if (backward_p)
19723 {
19724 while (s)
19725 {
19726 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
19727 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
19728 x -= s->width;
19729 s->x = x;
19730 s = s->prev;
19731 }
19732 }
19733 else
19734 {
19735 while (s)
19736 {
19737 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
19738 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
19739 s->x = x;
19740 x += s->width;
19741 s = s->next;
19742 }
19743 }
19744 }
19745
19746
19747
19748 /* The following macros are only called from draw_glyphs below.
19749 They reference the following parameters of that function directly:
19750 `w', `row', `area', and `overlap_p'
19751 as well as the following local variables:
19752 `s', `f', and `hdc' (in W32) */
19753
19754 #ifdef HAVE_NTGUI
19755 /* On W32, silently add local `hdc' variable to argument list of
19756 init_glyph_string. */
19757 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
19758 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
19759 #else
19760 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
19761 init_glyph_string (s, char2b, w, row, area, start, hl)
19762 #endif
19763
19764 /* Add a glyph string for a stretch glyph to the list of strings
19765 between HEAD and TAIL. START is the index of the stretch glyph in
19766 row area AREA of glyph row ROW. END is the index of the last glyph
19767 in that glyph row area. X is the current output position assigned
19768 to the new glyph string constructed. HL overrides that face of the
19769 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
19770 is the right-most x-position of the drawing area. */
19771
19772 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
19773 and below -- keep them on one line. */
19774 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
19775 do \
19776 { \
19777 s = (struct glyph_string *) alloca (sizeof *s); \
19778 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
19779 START = fill_stretch_glyph_string (s, row, area, START, END); \
19780 append_glyph_string (&HEAD, &TAIL, s); \
19781 s->x = (X); \
19782 } \
19783 while (0)
19784
19785
19786 /* Add a glyph string for an image glyph to the list of strings
19787 between HEAD and TAIL. START is the index of the image glyph in
19788 row area AREA of glyph row ROW. END is the index of the last glyph
19789 in that glyph row area. X is the current output position assigned
19790 to the new glyph string constructed. HL overrides that face of the
19791 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
19792 is the right-most x-position of the drawing area. */
19793
19794 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
19795 do \
19796 { \
19797 s = (struct glyph_string *) alloca (sizeof *s); \
19798 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
19799 fill_image_glyph_string (s); \
19800 append_glyph_string (&HEAD, &TAIL, s); \
19801 ++START; \
19802 s->x = (X); \
19803 } \
19804 while (0)
19805
19806
19807 /* Add a glyph string for a sequence of character glyphs to the list
19808 of strings between HEAD and TAIL. START is the index of the first
19809 glyph in row area AREA of glyph row ROW that is part of the new
19810 glyph string. END is the index of the last glyph in that glyph row
19811 area. X is the current output position assigned to the new glyph
19812 string constructed. HL overrides that face of the glyph; e.g. it
19813 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
19814 right-most x-position of the drawing area. */
19815
19816 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
19817 do \
19818 { \
19819 int face_id; \
19820 XChar2b *char2b; \
19821 \
19822 face_id = (row)->glyphs[area][START].face_id; \
19823 \
19824 s = (struct glyph_string *) alloca (sizeof *s); \
19825 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
19826 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
19827 append_glyph_string (&HEAD, &TAIL, s); \
19828 s->x = (X); \
19829 START = fill_glyph_string (s, face_id, START, END, overlaps); \
19830 } \
19831 while (0)
19832
19833
19834 /* Add a glyph string for a composite sequence to the list of strings
19835 between HEAD and TAIL. START is the index of the first glyph in
19836 row area AREA of glyph row ROW that is part of the new glyph
19837 string. END is the index of the last glyph in that glyph row area.
19838 X is the current output position assigned to the new glyph string
19839 constructed. HL overrides that face of the glyph; e.g. it is
19840 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
19841 x-position of the drawing area. */
19842
19843 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
19844 do { \
19845 int face_id = (row)->glyphs[area][START].face_id; \
19846 struct face *base_face = FACE_FROM_ID (f, face_id); \
19847 int cmp_id = (row)->glyphs[area][START].u.cmp_id; \
19848 struct composition *cmp = composition_table[cmp_id]; \
19849 XChar2b *char2b; \
19850 struct glyph_string *first_s; \
19851 int n; \
19852 \
19853 char2b = (XChar2b *) alloca ((sizeof *char2b) * cmp->glyph_len); \
19854 base_face = base_face->ascii_face; \
19855 \
19856 /* Make glyph_strings for each glyph sequence that is drawable by \
19857 the same face, and append them to HEAD/TAIL. */ \
19858 for (n = 0; n < cmp->glyph_len;) \
19859 { \
19860 s = (struct glyph_string *) alloca (sizeof *s); \
19861 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
19862 append_glyph_string (&(HEAD), &(TAIL), s); \
19863 s->cmp = cmp; \
19864 s->gidx = n; \
19865 s->x = (X); \
19866 if (n == 0) \
19867 first_s = s; \
19868 n = fill_composite_glyph_string (s, base_face, overlaps); \
19869 } \
19870 \
19871 ++START; \
19872 s = first_s; \
19873 } while (0)
19874
19875
19876 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
19877 of AREA of glyph row ROW on window W between indices START and END.
19878 HL overrides the face for drawing glyph strings, e.g. it is
19879 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
19880 x-positions of the drawing area.
19881
19882 This is an ugly monster macro construct because we must use alloca
19883 to allocate glyph strings (because draw_glyphs can be called
19884 asynchronously). */
19885
19886 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
19887 do \
19888 { \
19889 HEAD = TAIL = NULL; \
19890 while (START < END) \
19891 { \
19892 struct glyph *first_glyph = (row)->glyphs[area] + START; \
19893 switch (first_glyph->type) \
19894 { \
19895 case CHAR_GLYPH: \
19896 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
19897 HL, X, LAST_X); \
19898 break; \
19899 \
19900 case COMPOSITE_GLYPH: \
19901 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
19902 HL, X, LAST_X); \
19903 break; \
19904 \
19905 case STRETCH_GLYPH: \
19906 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
19907 HL, X, LAST_X); \
19908 break; \
19909 \
19910 case IMAGE_GLYPH: \
19911 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
19912 HL, X, LAST_X); \
19913 break; \
19914 \
19915 default: \
19916 abort (); \
19917 } \
19918 \
19919 if (s) \
19920 { \
19921 set_glyph_string_background_width (s, START, LAST_X); \
19922 (X) += s->width; \
19923 } \
19924 } \
19925 } \
19926 while (0)
19927
19928
19929 /* Draw glyphs between START and END in AREA of ROW on window W,
19930 starting at x-position X. X is relative to AREA in W. HL is a
19931 face-override with the following meaning:
19932
19933 DRAW_NORMAL_TEXT draw normally
19934 DRAW_CURSOR draw in cursor face
19935 DRAW_MOUSE_FACE draw in mouse face.
19936 DRAW_INVERSE_VIDEO draw in mode line face
19937 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
19938 DRAW_IMAGE_RAISED draw an image with a raised relief around it
19939
19940 If OVERLAPS is non-zero, draw only the foreground of characters and
19941 clip to the physical height of ROW. Non-zero value also defines
19942 the overlapping part to be drawn:
19943
19944 OVERLAPS_PRED overlap with preceding rows
19945 OVERLAPS_SUCC overlap with succeeding rows
19946 OVERLAPS_BOTH overlap with both preceding/succeeding rows
19947 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
19948
19949 Value is the x-position reached, relative to AREA of W. */
19950
19951 static int
19952 draw_glyphs (w, x, row, area, start, end, hl, overlaps)
19953 struct window *w;
19954 int x;
19955 struct glyph_row *row;
19956 enum glyph_row_area area;
19957 EMACS_INT start, end;
19958 enum draw_glyphs_face hl;
19959 int overlaps;
19960 {
19961 struct glyph_string *head, *tail;
19962 struct glyph_string *s;
19963 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
19964 int last_x, area_width;
19965 int x_reached;
19966 int i, j;
19967 struct frame *f = XFRAME (WINDOW_FRAME (w));
19968 DECLARE_HDC (hdc);
19969
19970 ALLOCATE_HDC (hdc, f);
19971
19972 /* Let's rather be paranoid than getting a SEGV. */
19973 end = min (end, row->used[area]);
19974 start = max (0, start);
19975 start = min (end, start);
19976
19977 /* Translate X to frame coordinates. Set last_x to the right
19978 end of the drawing area. */
19979 if (row->full_width_p)
19980 {
19981 /* X is relative to the left edge of W, without scroll bars
19982 or fringes. */
19983 x += WINDOW_LEFT_EDGE_X (w);
19984 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
19985 }
19986 else
19987 {
19988 int area_left = window_box_left (w, area);
19989 x += area_left;
19990 area_width = window_box_width (w, area);
19991 last_x = area_left + area_width;
19992 }
19993
19994 /* Build a doubly-linked list of glyph_string structures between
19995 head and tail from what we have to draw. Note that the macro
19996 BUILD_GLYPH_STRINGS will modify its start parameter. That's
19997 the reason we use a separate variable `i'. */
19998 i = start;
19999 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
20000 if (tail)
20001 x_reached = tail->x + tail->background_width;
20002 else
20003 x_reached = x;
20004
20005 /* If there are any glyphs with lbearing < 0 or rbearing > width in
20006 the row, redraw some glyphs in front or following the glyph
20007 strings built above. */
20008 if (head && !overlaps && row->contains_overlapping_glyphs_p)
20009 {
20010 int dummy_x = 0;
20011 struct glyph_string *h, *t;
20012
20013 /* Compute overhangs for all glyph strings. */
20014 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
20015 for (s = head; s; s = s->next)
20016 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
20017
20018 /* Prepend glyph strings for glyphs in front of the first glyph
20019 string that are overwritten because of the first glyph
20020 string's left overhang. The background of all strings
20021 prepended must be drawn because the first glyph string
20022 draws over it. */
20023 i = left_overwritten (head);
20024 if (i >= 0)
20025 {
20026 j = i;
20027 BUILD_GLYPH_STRINGS (j, start, h, t,
20028 DRAW_NORMAL_TEXT, dummy_x, last_x);
20029 start = i;
20030 compute_overhangs_and_x (t, head->x, 1);
20031 prepend_glyph_string_lists (&head, &tail, h, t);
20032 clip_head = head;
20033 }
20034
20035 /* Prepend glyph strings for glyphs in front of the first glyph
20036 string that overwrite that glyph string because of their
20037 right overhang. For these strings, only the foreground must
20038 be drawn, because it draws over the glyph string at `head'.
20039 The background must not be drawn because this would overwrite
20040 right overhangs of preceding glyphs for which no glyph
20041 strings exist. */
20042 i = left_overwriting (head);
20043 if (i >= 0)
20044 {
20045 clip_head = head;
20046 BUILD_GLYPH_STRINGS (i, start, h, t,
20047 DRAW_NORMAL_TEXT, dummy_x, last_x);
20048 for (s = h; s; s = s->next)
20049 s->background_filled_p = 1;
20050 compute_overhangs_and_x (t, head->x, 1);
20051 prepend_glyph_string_lists (&head, &tail, h, t);
20052 }
20053
20054 /* Append glyphs strings for glyphs following the last glyph
20055 string tail that are overwritten by tail. The background of
20056 these strings has to be drawn because tail's foreground draws
20057 over it. */
20058 i = right_overwritten (tail);
20059 if (i >= 0)
20060 {
20061 BUILD_GLYPH_STRINGS (end, i, h, t,
20062 DRAW_NORMAL_TEXT, x, last_x);
20063 compute_overhangs_and_x (h, tail->x + tail->width, 0);
20064 append_glyph_string_lists (&head, &tail, h, t);
20065 clip_tail = tail;
20066 }
20067
20068 /* Append glyph strings for glyphs following the last glyph
20069 string tail that overwrite tail. The foreground of such
20070 glyphs has to be drawn because it writes into the background
20071 of tail. The background must not be drawn because it could
20072 paint over the foreground of following glyphs. */
20073 i = right_overwriting (tail);
20074 if (i >= 0)
20075 {
20076 clip_tail = tail;
20077 i++; /* We must include the Ith glyph. */
20078 BUILD_GLYPH_STRINGS (end, i, h, t,
20079 DRAW_NORMAL_TEXT, x, last_x);
20080 for (s = h; s; s = s->next)
20081 s->background_filled_p = 1;
20082 compute_overhangs_and_x (h, tail->x + tail->width, 0);
20083 append_glyph_string_lists (&head, &tail, h, t);
20084 }
20085 if (clip_head || clip_tail)
20086 for (s = head; s; s = s->next)
20087 {
20088 s->clip_head = clip_head;
20089 s->clip_tail = clip_tail;
20090 }
20091 }
20092
20093 /* Draw all strings. */
20094 for (s = head; s; s = s->next)
20095 FRAME_RIF (f)->draw_glyph_string (s);
20096
20097 if (area == TEXT_AREA
20098 && !row->full_width_p
20099 /* When drawing overlapping rows, only the glyph strings'
20100 foreground is drawn, which doesn't erase a cursor
20101 completely. */
20102 && !overlaps)
20103 {
20104 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
20105 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
20106 : (tail ? tail->x + tail->background_width : x));
20107
20108 int text_left = window_box_left (w, TEXT_AREA);
20109 x0 -= text_left;
20110 x1 -= text_left;
20111
20112 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
20113 row->y, MATRIX_ROW_BOTTOM_Y (row));
20114 }
20115
20116 /* Value is the x-position up to which drawn, relative to AREA of W.
20117 This doesn't include parts drawn because of overhangs. */
20118 if (row->full_width_p)
20119 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
20120 else
20121 x_reached -= window_box_left (w, area);
20122
20123 RELEASE_HDC (hdc, f);
20124
20125 return x_reached;
20126 }
20127
20128 /* Expand row matrix if too narrow. Don't expand if area
20129 is not present. */
20130
20131 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
20132 { \
20133 if (!fonts_changed_p \
20134 && (it->glyph_row->glyphs[area] \
20135 < it->glyph_row->glyphs[area + 1])) \
20136 { \
20137 it->w->ncols_scale_factor++; \
20138 fonts_changed_p = 1; \
20139 } \
20140 }
20141
20142 /* Store one glyph for IT->char_to_display in IT->glyph_row.
20143 Called from x_produce_glyphs when IT->glyph_row is non-null. */
20144
20145 static INLINE void
20146 append_glyph (it)
20147 struct it *it;
20148 {
20149 struct glyph *glyph;
20150 enum glyph_row_area area = it->area;
20151
20152 xassert (it->glyph_row);
20153 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
20154
20155 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
20156 if (glyph < it->glyph_row->glyphs[area + 1])
20157 {
20158 glyph->charpos = CHARPOS (it->position);
20159 glyph->object = it->object;
20160 glyph->pixel_width = it->pixel_width;
20161 glyph->ascent = it->ascent;
20162 glyph->descent = it->descent;
20163 glyph->voffset = it->voffset;
20164 glyph->type = CHAR_GLYPH;
20165 glyph->multibyte_p = it->multibyte_p;
20166 glyph->left_box_line_p = it->start_of_box_run_p;
20167 glyph->right_box_line_p = it->end_of_box_run_p;
20168 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
20169 || it->phys_descent > it->descent);
20170 glyph->padding_p = 0;
20171 glyph->glyph_not_available_p = it->glyph_not_available_p;
20172 glyph->face_id = it->face_id;
20173 glyph->u.ch = it->char_to_display;
20174 glyph->slice = null_glyph_slice;
20175 glyph->font_type = FONT_TYPE_UNKNOWN;
20176 ++it->glyph_row->used[area];
20177 }
20178 else
20179 IT_EXPAND_MATRIX_WIDTH (it, area);
20180 }
20181
20182 /* Store one glyph for the composition IT->cmp_id in IT->glyph_row.
20183 Called from x_produce_glyphs when IT->glyph_row is non-null. */
20184
20185 static INLINE void
20186 append_composite_glyph (it)
20187 struct it *it;
20188 {
20189 struct glyph *glyph;
20190 enum glyph_row_area area = it->area;
20191
20192 xassert (it->glyph_row);
20193
20194 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
20195 if (glyph < it->glyph_row->glyphs[area + 1])
20196 {
20197 glyph->charpos = CHARPOS (it->position);
20198 glyph->object = it->object;
20199 glyph->pixel_width = it->pixel_width;
20200 glyph->ascent = it->ascent;
20201 glyph->descent = it->descent;
20202 glyph->voffset = it->voffset;
20203 glyph->type = COMPOSITE_GLYPH;
20204 glyph->multibyte_p = it->multibyte_p;
20205 glyph->left_box_line_p = it->start_of_box_run_p;
20206 glyph->right_box_line_p = it->end_of_box_run_p;
20207 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
20208 || it->phys_descent > it->descent);
20209 glyph->padding_p = 0;
20210 glyph->glyph_not_available_p = 0;
20211 glyph->face_id = it->face_id;
20212 glyph->u.cmp_id = it->cmp_id;
20213 glyph->slice = null_glyph_slice;
20214 glyph->font_type = FONT_TYPE_UNKNOWN;
20215 ++it->glyph_row->used[area];
20216 }
20217 else
20218 IT_EXPAND_MATRIX_WIDTH (it, area);
20219 }
20220
20221
20222 /* Change IT->ascent and IT->height according to the setting of
20223 IT->voffset. */
20224
20225 static INLINE void
20226 take_vertical_position_into_account (it)
20227 struct it *it;
20228 {
20229 if (it->voffset)
20230 {
20231 if (it->voffset < 0)
20232 /* Increase the ascent so that we can display the text higher
20233 in the line. */
20234 it->ascent -= it->voffset;
20235 else
20236 /* Increase the descent so that we can display the text lower
20237 in the line. */
20238 it->descent += it->voffset;
20239 }
20240 }
20241
20242
20243 /* Produce glyphs/get display metrics for the image IT is loaded with.
20244 See the description of struct display_iterator in dispextern.h for
20245 an overview of struct display_iterator. */
20246
20247 static void
20248 produce_image_glyph (it)
20249 struct it *it;
20250 {
20251 struct image *img;
20252 struct face *face;
20253 int glyph_ascent, crop;
20254 struct glyph_slice slice;
20255
20256 xassert (it->what == IT_IMAGE);
20257
20258 face = FACE_FROM_ID (it->f, it->face_id);
20259 xassert (face);
20260 /* Make sure X resources of the face is loaded. */
20261 PREPARE_FACE_FOR_DISPLAY (it->f, face);
20262
20263 if (it->image_id < 0)
20264 {
20265 /* Fringe bitmap. */
20266 it->ascent = it->phys_ascent = 0;
20267 it->descent = it->phys_descent = 0;
20268 it->pixel_width = 0;
20269 it->nglyphs = 0;
20270 return;
20271 }
20272
20273 img = IMAGE_FROM_ID (it->f, it->image_id);
20274 xassert (img);
20275 /* Make sure X resources of the image is loaded. */
20276 prepare_image_for_display (it->f, img);
20277
20278 slice.x = slice.y = 0;
20279 slice.width = img->width;
20280 slice.height = img->height;
20281
20282 if (INTEGERP (it->slice.x))
20283 slice.x = XINT (it->slice.x);
20284 else if (FLOATP (it->slice.x))
20285 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
20286
20287 if (INTEGERP (it->slice.y))
20288 slice.y = XINT (it->slice.y);
20289 else if (FLOATP (it->slice.y))
20290 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
20291
20292 if (INTEGERP (it->slice.width))
20293 slice.width = XINT (it->slice.width);
20294 else if (FLOATP (it->slice.width))
20295 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
20296
20297 if (INTEGERP (it->slice.height))
20298 slice.height = XINT (it->slice.height);
20299 else if (FLOATP (it->slice.height))
20300 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
20301
20302 if (slice.x >= img->width)
20303 slice.x = img->width;
20304 if (slice.y >= img->height)
20305 slice.y = img->height;
20306 if (slice.x + slice.width >= img->width)
20307 slice.width = img->width - slice.x;
20308 if (slice.y + slice.height > img->height)
20309 slice.height = img->height - slice.y;
20310
20311 if (slice.width == 0 || slice.height == 0)
20312 return;
20313
20314 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
20315
20316 it->descent = slice.height - glyph_ascent;
20317 if (slice.y == 0)
20318 it->descent += img->vmargin;
20319 if (slice.y + slice.height == img->height)
20320 it->descent += img->vmargin;
20321 it->phys_descent = it->descent;
20322
20323 it->pixel_width = slice.width;
20324 if (slice.x == 0)
20325 it->pixel_width += img->hmargin;
20326 if (slice.x + slice.width == img->width)
20327 it->pixel_width += img->hmargin;
20328
20329 /* It's quite possible for images to have an ascent greater than
20330 their height, so don't get confused in that case. */
20331 if (it->descent < 0)
20332 it->descent = 0;
20333
20334 #if 0 /* this breaks image tiling */
20335 /* If this glyph is alone on the last line, adjust it.ascent to minimum row ascent. */
20336 int face_ascent = face->font ? FONT_BASE (face->font) : FRAME_BASELINE_OFFSET (it->f);
20337 if (face_ascent > it->ascent)
20338 it->ascent = it->phys_ascent = face_ascent;
20339 #endif
20340
20341 it->nglyphs = 1;
20342
20343 if (face->box != FACE_NO_BOX)
20344 {
20345 if (face->box_line_width > 0)
20346 {
20347 if (slice.y == 0)
20348 it->ascent += face->box_line_width;
20349 if (slice.y + slice.height == img->height)
20350 it->descent += face->box_line_width;
20351 }
20352
20353 if (it->start_of_box_run_p && slice.x == 0)
20354 it->pixel_width += eabs (face->box_line_width);
20355 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
20356 it->pixel_width += eabs (face->box_line_width);
20357 }
20358
20359 take_vertical_position_into_account (it);
20360
20361 /* Automatically crop wide image glyphs at right edge so we can
20362 draw the cursor on same display row. */
20363 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
20364 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
20365 {
20366 it->pixel_width -= crop;
20367 slice.width -= crop;
20368 }
20369
20370 if (it->glyph_row)
20371 {
20372 struct glyph *glyph;
20373 enum glyph_row_area area = it->area;
20374
20375 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
20376 if (glyph < it->glyph_row->glyphs[area + 1])
20377 {
20378 glyph->charpos = CHARPOS (it->position);
20379 glyph->object = it->object;
20380 glyph->pixel_width = it->pixel_width;
20381 glyph->ascent = glyph_ascent;
20382 glyph->descent = it->descent;
20383 glyph->voffset = it->voffset;
20384 glyph->type = IMAGE_GLYPH;
20385 glyph->multibyte_p = it->multibyte_p;
20386 glyph->left_box_line_p = it->start_of_box_run_p;
20387 glyph->right_box_line_p = it->end_of_box_run_p;
20388 glyph->overlaps_vertically_p = 0;
20389 glyph->padding_p = 0;
20390 glyph->glyph_not_available_p = 0;
20391 glyph->face_id = it->face_id;
20392 glyph->u.img_id = img->id;
20393 glyph->slice = slice;
20394 glyph->font_type = FONT_TYPE_UNKNOWN;
20395 ++it->glyph_row->used[area];
20396 }
20397 else
20398 IT_EXPAND_MATRIX_WIDTH (it, area);
20399 }
20400 }
20401
20402
20403 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
20404 of the glyph, WIDTH and HEIGHT are the width and height of the
20405 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
20406
20407 static void
20408 append_stretch_glyph (it, object, width, height, ascent)
20409 struct it *it;
20410 Lisp_Object object;
20411 int width, height;
20412 int ascent;
20413 {
20414 struct glyph *glyph;
20415 enum glyph_row_area area = it->area;
20416
20417 xassert (ascent >= 0 && ascent <= height);
20418
20419 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
20420 if (glyph < it->glyph_row->glyphs[area + 1])
20421 {
20422 glyph->charpos = CHARPOS (it->position);
20423 glyph->object = object;
20424 glyph->pixel_width = width;
20425 glyph->ascent = ascent;
20426 glyph->descent = height - ascent;
20427 glyph->voffset = it->voffset;
20428 glyph->type = STRETCH_GLYPH;
20429 glyph->multibyte_p = it->multibyte_p;
20430 glyph->left_box_line_p = it->start_of_box_run_p;
20431 glyph->right_box_line_p = it->end_of_box_run_p;
20432 glyph->overlaps_vertically_p = 0;
20433 glyph->padding_p = 0;
20434 glyph->glyph_not_available_p = 0;
20435 glyph->face_id = it->face_id;
20436 glyph->u.stretch.ascent = ascent;
20437 glyph->u.stretch.height = height;
20438 glyph->slice = null_glyph_slice;
20439 glyph->font_type = FONT_TYPE_UNKNOWN;
20440 ++it->glyph_row->used[area];
20441 }
20442 else
20443 IT_EXPAND_MATRIX_WIDTH (it, area);
20444 }
20445
20446
20447 /* Produce a stretch glyph for iterator IT. IT->object is the value
20448 of the glyph property displayed. The value must be a list
20449 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
20450 being recognized:
20451
20452 1. `:width WIDTH' specifies that the space should be WIDTH *
20453 canonical char width wide. WIDTH may be an integer or floating
20454 point number.
20455
20456 2. `:relative-width FACTOR' specifies that the width of the stretch
20457 should be computed from the width of the first character having the
20458 `glyph' property, and should be FACTOR times that width.
20459
20460 3. `:align-to HPOS' specifies that the space should be wide enough
20461 to reach HPOS, a value in canonical character units.
20462
20463 Exactly one of the above pairs must be present.
20464
20465 4. `:height HEIGHT' specifies that the height of the stretch produced
20466 should be HEIGHT, measured in canonical character units.
20467
20468 5. `:relative-height FACTOR' specifies that the height of the
20469 stretch should be FACTOR times the height of the characters having
20470 the glyph property.
20471
20472 Either none or exactly one of 4 or 5 must be present.
20473
20474 6. `:ascent ASCENT' specifies that ASCENT percent of the height
20475 of the stretch should be used for the ascent of the stretch.
20476 ASCENT must be in the range 0 <= ASCENT <= 100. */
20477
20478 static void
20479 produce_stretch_glyph (it)
20480 struct it *it;
20481 {
20482 /* (space :width WIDTH :height HEIGHT ...) */
20483 Lisp_Object prop, plist;
20484 int width = 0, height = 0, align_to = -1;
20485 int zero_width_ok_p = 0, zero_height_ok_p = 0;
20486 int ascent = 0;
20487 double tem;
20488 struct face *face = FACE_FROM_ID (it->f, it->face_id);
20489 XFontStruct *font = face->font ? face->font : FRAME_FONT (it->f);
20490
20491 PREPARE_FACE_FOR_DISPLAY (it->f, face);
20492
20493 /* List should start with `space'. */
20494 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
20495 plist = XCDR (it->object);
20496
20497 /* Compute the width of the stretch. */
20498 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
20499 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
20500 {
20501 /* Absolute width `:width WIDTH' specified and valid. */
20502 zero_width_ok_p = 1;
20503 width = (int)tem;
20504 }
20505 else if (prop = Fplist_get (plist, QCrelative_width),
20506 NUMVAL (prop) > 0)
20507 {
20508 /* Relative width `:relative-width FACTOR' specified and valid.
20509 Compute the width of the characters having the `glyph'
20510 property. */
20511 struct it it2;
20512 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
20513
20514 it2 = *it;
20515 if (it->multibyte_p)
20516 {
20517 int maxlen = ((IT_BYTEPOS (*it) >= GPT ? ZV : GPT)
20518 - IT_BYTEPOS (*it));
20519 it2.c = STRING_CHAR_AND_LENGTH (p, maxlen, it2.len);
20520 }
20521 else
20522 it2.c = *p, it2.len = 1;
20523
20524 it2.glyph_row = NULL;
20525 it2.what = IT_CHARACTER;
20526 x_produce_glyphs (&it2);
20527 width = NUMVAL (prop) * it2.pixel_width;
20528 }
20529 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
20530 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
20531 {
20532 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
20533 align_to = (align_to < 0
20534 ? 0
20535 : align_to - window_box_left_offset (it->w, TEXT_AREA));
20536 else if (align_to < 0)
20537 align_to = window_box_left_offset (it->w, TEXT_AREA);
20538 width = max (0, (int)tem + align_to - it->current_x);
20539 zero_width_ok_p = 1;
20540 }
20541 else
20542 /* Nothing specified -> width defaults to canonical char width. */
20543 width = FRAME_COLUMN_WIDTH (it->f);
20544
20545 if (width <= 0 && (width < 0 || !zero_width_ok_p))
20546 width = 1;
20547
20548 /* Compute height. */
20549 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
20550 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
20551 {
20552 height = (int)tem;
20553 zero_height_ok_p = 1;
20554 }
20555 else if (prop = Fplist_get (plist, QCrelative_height),
20556 NUMVAL (prop) > 0)
20557 height = FONT_HEIGHT (font) * NUMVAL (prop);
20558 else
20559 height = FONT_HEIGHT (font);
20560
20561 if (height <= 0 && (height < 0 || !zero_height_ok_p))
20562 height = 1;
20563
20564 /* Compute percentage of height used for ascent. If
20565 `:ascent ASCENT' is present and valid, use that. Otherwise,
20566 derive the ascent from the font in use. */
20567 if (prop = Fplist_get (plist, QCascent),
20568 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
20569 ascent = height * NUMVAL (prop) / 100.0;
20570 else if (!NILP (prop)
20571 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
20572 ascent = min (max (0, (int)tem), height);
20573 else
20574 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
20575
20576 if (width > 0 && !it->truncate_lines_p
20577 && it->current_x + width > it->last_visible_x)
20578 width = it->last_visible_x - it->current_x - 1;
20579
20580 if (width > 0 && height > 0 && it->glyph_row)
20581 {
20582 Lisp_Object object = it->stack[it->sp - 1].string;
20583 if (!STRINGP (object))
20584 object = it->w->buffer;
20585 append_stretch_glyph (it, object, width, height, ascent);
20586 }
20587
20588 it->pixel_width = width;
20589 it->ascent = it->phys_ascent = ascent;
20590 it->descent = it->phys_descent = height - it->ascent;
20591 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
20592
20593 take_vertical_position_into_account (it);
20594 }
20595
20596 /* Get line-height and line-spacing property at point.
20597 If line-height has format (HEIGHT TOTAL), return TOTAL
20598 in TOTAL_HEIGHT. */
20599
20600 static Lisp_Object
20601 get_line_height_property (it, prop)
20602 struct it *it;
20603 Lisp_Object prop;
20604 {
20605 Lisp_Object position;
20606
20607 if (STRINGP (it->object))
20608 position = make_number (IT_STRING_CHARPOS (*it));
20609 else if (BUFFERP (it->object))
20610 position = make_number (IT_CHARPOS (*it));
20611 else
20612 return Qnil;
20613
20614 return Fget_char_property (position, prop, it->object);
20615 }
20616
20617 /* Calculate line-height and line-spacing properties.
20618 An integer value specifies explicit pixel value.
20619 A float value specifies relative value to current face height.
20620 A cons (float . face-name) specifies relative value to
20621 height of specified face font.
20622
20623 Returns height in pixels, or nil. */
20624
20625
20626 static Lisp_Object
20627 calc_line_height_property (it, val, font, boff, override)
20628 struct it *it;
20629 Lisp_Object val;
20630 XFontStruct *font;
20631 int boff, override;
20632 {
20633 Lisp_Object face_name = Qnil;
20634 int ascent, descent, height;
20635
20636 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
20637 return val;
20638
20639 if (CONSP (val))
20640 {
20641 face_name = XCAR (val);
20642 val = XCDR (val);
20643 if (!NUMBERP (val))
20644 val = make_number (1);
20645 if (NILP (face_name))
20646 {
20647 height = it->ascent + it->descent;
20648 goto scale;
20649 }
20650 }
20651
20652 if (NILP (face_name))
20653 {
20654 font = FRAME_FONT (it->f);
20655 boff = FRAME_BASELINE_OFFSET (it->f);
20656 }
20657 else if (EQ (face_name, Qt))
20658 {
20659 override = 0;
20660 }
20661 else
20662 {
20663 int face_id;
20664 struct face *face;
20665 struct font_info *font_info;
20666
20667 face_id = lookup_named_face (it->f, face_name, 0);
20668 if (face_id < 0)
20669 return make_number (-1);
20670
20671 face = FACE_FROM_ID (it->f, face_id);
20672 font = face->font;
20673 if (font == NULL)
20674 return make_number (-1);
20675
20676 font_info = FONT_INFO_FROM_FACE (it->f, face);
20677 boff = font_info->baseline_offset;
20678 if (font_info->vertical_centering)
20679 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
20680 }
20681
20682 ascent = FONT_BASE (font) + boff;
20683 descent = FONT_DESCENT (font) - boff;
20684
20685 if (override)
20686 {
20687 it->override_ascent = ascent;
20688 it->override_descent = descent;
20689 it->override_boff = boff;
20690 }
20691
20692 height = ascent + descent;
20693
20694 scale:
20695 if (FLOATP (val))
20696 height = (int)(XFLOAT_DATA (val) * height);
20697 else if (INTEGERP (val))
20698 height *= XINT (val);
20699
20700 return make_number (height);
20701 }
20702
20703
20704 /* RIF:
20705 Produce glyphs/get display metrics for the display element IT is
20706 loaded with. See the description of struct it in dispextern.h
20707 for an overview of struct it. */
20708
20709 void
20710 x_produce_glyphs (it)
20711 struct it *it;
20712 {
20713 int extra_line_spacing = it->extra_line_spacing;
20714
20715 it->glyph_not_available_p = 0;
20716
20717 if (it->what == IT_CHARACTER)
20718 {
20719 XChar2b char2b;
20720 XFontStruct *font;
20721 struct face *face = FACE_FROM_ID (it->f, it->face_id);
20722 XCharStruct *pcm;
20723 int font_not_found_p;
20724 struct font_info *font_info;
20725 int boff; /* baseline offset */
20726 /* We may change it->multibyte_p upon unibyte<->multibyte
20727 conversion. So, save the current value now and restore it
20728 later.
20729
20730 Note: It seems that we don't have to record multibyte_p in
20731 struct glyph because the character code itself tells if or
20732 not the character is multibyte. Thus, in the future, we must
20733 consider eliminating the field `multibyte_p' in the struct
20734 glyph. */
20735 int saved_multibyte_p = it->multibyte_p;
20736
20737 /* Maybe translate single-byte characters to multibyte, or the
20738 other way. */
20739 it->char_to_display = it->c;
20740 if (!ASCII_BYTE_P (it->c)
20741 && ! it->multibyte_p)
20742 {
20743 if (SINGLE_BYTE_CHAR_P (it->c)
20744 && unibyte_display_via_language_environment)
20745 it->char_to_display = unibyte_char_to_multibyte (it->c);
20746 if (! SINGLE_BYTE_CHAR_P (it->char_to_display))
20747 {
20748 it->multibyte_p = 1;
20749 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display,
20750 -1, Qnil);
20751 face = FACE_FROM_ID (it->f, it->face_id);
20752 }
20753 }
20754
20755 /* Get font to use. Encode IT->char_to_display. */
20756 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
20757 &char2b, it->multibyte_p, 0);
20758 font = face->font;
20759
20760 /* When no suitable font found, use the default font. */
20761 font_not_found_p = font == NULL;
20762 if (font_not_found_p)
20763 {
20764 font = FRAME_FONT (it->f);
20765 boff = FRAME_BASELINE_OFFSET (it->f);
20766 font_info = NULL;
20767 }
20768 else
20769 {
20770 font_info = FONT_INFO_FROM_FACE (it->f, face);
20771 boff = font_info->baseline_offset;
20772 if (font_info->vertical_centering)
20773 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
20774 }
20775
20776 if (it->char_to_display >= ' '
20777 && (!it->multibyte_p || it->char_to_display < 128))
20778 {
20779 /* Either unibyte or ASCII. */
20780 int stretched_p;
20781
20782 it->nglyphs = 1;
20783
20784 pcm = get_per_char_metric (it->f, font, font_info, &char2b,
20785 FONT_TYPE_FOR_UNIBYTE (font, it->char_to_display));
20786
20787 if (it->override_ascent >= 0)
20788 {
20789 it->ascent = it->override_ascent;
20790 it->descent = it->override_descent;
20791 boff = it->override_boff;
20792 }
20793 else
20794 {
20795 it->ascent = FONT_BASE (font) + boff;
20796 it->descent = FONT_DESCENT (font) - boff;
20797 }
20798
20799 if (pcm)
20800 {
20801 it->phys_ascent = pcm->ascent + boff;
20802 it->phys_descent = pcm->descent - boff;
20803 it->pixel_width = pcm->width;
20804 }
20805 else
20806 {
20807 it->glyph_not_available_p = 1;
20808 it->phys_ascent = it->ascent;
20809 it->phys_descent = it->descent;
20810 it->pixel_width = FONT_WIDTH (font);
20811 }
20812
20813 if (it->constrain_row_ascent_descent_p)
20814 {
20815 if (it->descent > it->max_descent)
20816 {
20817 it->ascent += it->descent - it->max_descent;
20818 it->descent = it->max_descent;
20819 }
20820 if (it->ascent > it->max_ascent)
20821 {
20822 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
20823 it->ascent = it->max_ascent;
20824 }
20825 it->phys_ascent = min (it->phys_ascent, it->ascent);
20826 it->phys_descent = min (it->phys_descent, it->descent);
20827 extra_line_spacing = 0;
20828 }
20829
20830 /* If this is a space inside a region of text with
20831 `space-width' property, change its width. */
20832 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
20833 if (stretched_p)
20834 it->pixel_width *= XFLOATINT (it->space_width);
20835
20836 /* If face has a box, add the box thickness to the character
20837 height. If character has a box line to the left and/or
20838 right, add the box line width to the character's width. */
20839 if (face->box != FACE_NO_BOX)
20840 {
20841 int thick = face->box_line_width;
20842
20843 if (thick > 0)
20844 {
20845 it->ascent += thick;
20846 it->descent += thick;
20847 }
20848 else
20849 thick = -thick;
20850
20851 if (it->start_of_box_run_p)
20852 it->pixel_width += thick;
20853 if (it->end_of_box_run_p)
20854 it->pixel_width += thick;
20855 }
20856
20857 /* If face has an overline, add the height of the overline
20858 (1 pixel) and a 1 pixel margin to the character height. */
20859 if (face->overline_p)
20860 it->ascent += overline_margin;
20861
20862 if (it->constrain_row_ascent_descent_p)
20863 {
20864 if (it->ascent > it->max_ascent)
20865 it->ascent = it->max_ascent;
20866 if (it->descent > it->max_descent)
20867 it->descent = it->max_descent;
20868 }
20869
20870 take_vertical_position_into_account (it);
20871
20872 /* If we have to actually produce glyphs, do it. */
20873 if (it->glyph_row)
20874 {
20875 if (stretched_p)
20876 {
20877 /* Translate a space with a `space-width' property
20878 into a stretch glyph. */
20879 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
20880 / FONT_HEIGHT (font));
20881 append_stretch_glyph (it, it->object, it->pixel_width,
20882 it->ascent + it->descent, ascent);
20883 }
20884 else
20885 append_glyph (it);
20886
20887 /* If characters with lbearing or rbearing are displayed
20888 in this line, record that fact in a flag of the
20889 glyph row. This is used to optimize X output code. */
20890 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
20891 it->glyph_row->contains_overlapping_glyphs_p = 1;
20892 }
20893 }
20894 else if (it->char_to_display == '\n')
20895 {
20896 /* A newline has no width but we need the height of the line.
20897 But if previous part of the line set a height, don't
20898 increase that height */
20899
20900 Lisp_Object height;
20901 Lisp_Object total_height = Qnil;
20902
20903 it->override_ascent = -1;
20904 it->pixel_width = 0;
20905 it->nglyphs = 0;
20906
20907 height = get_line_height_property(it, Qline_height);
20908 /* Split (line-height total-height) list */
20909 if (CONSP (height)
20910 && CONSP (XCDR (height))
20911 && NILP (XCDR (XCDR (height))))
20912 {
20913 total_height = XCAR (XCDR (height));
20914 height = XCAR (height);
20915 }
20916 height = calc_line_height_property(it, height, font, boff, 1);
20917
20918 if (it->override_ascent >= 0)
20919 {
20920 it->ascent = it->override_ascent;
20921 it->descent = it->override_descent;
20922 boff = it->override_boff;
20923 }
20924 else
20925 {
20926 it->ascent = FONT_BASE (font) + boff;
20927 it->descent = FONT_DESCENT (font) - boff;
20928 }
20929
20930 if (EQ (height, Qt))
20931 {
20932 if (it->descent > it->max_descent)
20933 {
20934 it->ascent += it->descent - it->max_descent;
20935 it->descent = it->max_descent;
20936 }
20937 if (it->ascent > it->max_ascent)
20938 {
20939 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
20940 it->ascent = it->max_ascent;
20941 }
20942 it->phys_ascent = min (it->phys_ascent, it->ascent);
20943 it->phys_descent = min (it->phys_descent, it->descent);
20944 it->constrain_row_ascent_descent_p = 1;
20945 extra_line_spacing = 0;
20946 }
20947 else
20948 {
20949 Lisp_Object spacing;
20950
20951 it->phys_ascent = it->ascent;
20952 it->phys_descent = it->descent;
20953
20954 if ((it->max_ascent > 0 || it->max_descent > 0)
20955 && face->box != FACE_NO_BOX
20956 && face->box_line_width > 0)
20957 {
20958 it->ascent += face->box_line_width;
20959 it->descent += face->box_line_width;
20960 }
20961 if (!NILP (height)
20962 && XINT (height) > it->ascent + it->descent)
20963 it->ascent = XINT (height) - it->descent;
20964
20965 if (!NILP (total_height))
20966 spacing = calc_line_height_property(it, total_height, font, boff, 0);
20967 else
20968 {
20969 spacing = get_line_height_property(it, Qline_spacing);
20970 spacing = calc_line_height_property(it, spacing, font, boff, 0);
20971 }
20972 if (INTEGERP (spacing))
20973 {
20974 extra_line_spacing = XINT (spacing);
20975 if (!NILP (total_height))
20976 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
20977 }
20978 }
20979 }
20980 else if (it->char_to_display == '\t')
20981 {
20982 int tab_width = it->tab_width * FRAME_SPACE_WIDTH (it->f);
20983 int x = it->current_x + it->continuation_lines_width;
20984 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
20985
20986 /* If the distance from the current position to the next tab
20987 stop is less than a space character width, use the
20988 tab stop after that. */
20989 if (next_tab_x - x < FRAME_SPACE_WIDTH (it->f))
20990 next_tab_x += tab_width;
20991
20992 it->pixel_width = next_tab_x - x;
20993 it->nglyphs = 1;
20994 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
20995 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
20996
20997 if (it->glyph_row)
20998 {
20999 append_stretch_glyph (it, it->object, it->pixel_width,
21000 it->ascent + it->descent, it->ascent);
21001 }
21002 }
21003 else
21004 {
21005 /* A multi-byte character. Assume that the display width of the
21006 character is the width of the character multiplied by the
21007 width of the font. */
21008
21009 /* If we found a font, this font should give us the right
21010 metrics. If we didn't find a font, use the frame's
21011 default font and calculate the width of the character by
21012 multiplying the width of font by the width of the
21013 character. */
21014
21015 pcm = get_per_char_metric (it->f, font, font_info, &char2b,
21016 FONT_TYPE_FOR_MULTIBYTE (font, it->c));
21017
21018 if (font_not_found_p || !pcm)
21019 {
21020 int char_width = CHAR_WIDTH (it->char_to_display);
21021
21022 if (char_width == 0)
21023 /* This is a non spacing character. But, as we are
21024 going to display an empty box, the box must occupy
21025 at least one column. */
21026 char_width = 1;
21027 it->glyph_not_available_p = 1;
21028 it->pixel_width = FRAME_COLUMN_WIDTH (it->f) * char_width;
21029 it->phys_ascent = FONT_BASE (font) + boff;
21030 it->phys_descent = FONT_DESCENT (font) - boff;
21031 }
21032 else
21033 {
21034 it->pixel_width = pcm->width;
21035 it->phys_ascent = pcm->ascent + boff;
21036 it->phys_descent = pcm->descent - boff;
21037 if (it->glyph_row
21038 && (pcm->lbearing < 0
21039 || pcm->rbearing > pcm->width))
21040 it->glyph_row->contains_overlapping_glyphs_p = 1;
21041 }
21042 it->nglyphs = 1;
21043 it->ascent = FONT_BASE (font) + boff;
21044 it->descent = FONT_DESCENT (font) - boff;
21045 if (face->box != FACE_NO_BOX)
21046 {
21047 int thick = face->box_line_width;
21048
21049 if (thick > 0)
21050 {
21051 it->ascent += thick;
21052 it->descent += thick;
21053 }
21054 else
21055 thick = - thick;
21056
21057 if (it->start_of_box_run_p)
21058 it->pixel_width += thick;
21059 if (it->end_of_box_run_p)
21060 it->pixel_width += thick;
21061 }
21062
21063 /* If face has an overline, add the height of the overline
21064 (1 pixel) and a 1 pixel margin to the character height. */
21065 if (face->overline_p)
21066 it->ascent += overline_margin;
21067
21068 take_vertical_position_into_account (it);
21069
21070 if (it->glyph_row)
21071 append_glyph (it);
21072 }
21073 it->multibyte_p = saved_multibyte_p;
21074 }
21075 else if (it->what == IT_COMPOSITION)
21076 {
21077 /* Note: A composition is represented as one glyph in the
21078 glyph matrix. There are no padding glyphs.
21079
21080 Important is that pixel_width, ascent, and descent are the
21081 values of what is drawn by draw_glyphs (i.e. the values of
21082 the overall glyphs composed). */
21083 struct face *face = FACE_FROM_ID (it->f, it->face_id);
21084 int boff; /* baseline offset */
21085 struct composition *cmp = composition_table[it->cmp_id];
21086 int glyph_len = cmp->glyph_len;
21087 XFontStruct *font = face->font;
21088
21089 it->nglyphs = 1;
21090
21091 #ifdef USE_FONT_BACKEND
21092 if (cmp->method == COMPOSITION_WITH_GLYPH_STRING)
21093 {
21094 if (! cmp->font || cmp->font != font)
21095 font_prepare_composition (cmp, it->f);
21096 }
21097 else
21098 #endif /* USE_FONT_BACKEND */
21099 /* If we have not yet calculated pixel size data of glyphs of
21100 the composition for the current face font, calculate them
21101 now. Theoretically, we have to check all fonts for the
21102 glyphs, but that requires much time and memory space. So,
21103 here we check only the font of the first glyph. This leads
21104 to incorrect display, but it's very rare, and C-l (recenter)
21105 can correct the display anyway. */
21106 if (! cmp->font || cmp->font != font)
21107 {
21108 /* Ascent and descent of the font of the first character
21109 of this composition (adjusted by baseline offset).
21110 Ascent and descent of overall glyphs should not be less
21111 than them respectively. */
21112 int font_ascent, font_descent, font_height;
21113 /* Bounding box of the overall glyphs. */
21114 int leftmost, rightmost, lowest, highest;
21115 int lbearing, rbearing;
21116 int i, width, ascent, descent;
21117 int left_padded = 0, right_padded = 0;
21118 int face_id;
21119 int c;
21120 XChar2b char2b;
21121 XCharStruct *pcm;
21122 int font_not_found_p;
21123 struct font_info *font_info;
21124 int pos;
21125
21126 for (glyph_len = cmp->glyph_len; glyph_len > 0; glyph_len--)
21127 if ((c = COMPOSITION_GLYPH (cmp, glyph_len - 1)) != '\t')
21128 break;
21129 if (glyph_len < cmp->glyph_len)
21130 right_padded = 1;
21131 for (i = 0; i < glyph_len; i++)
21132 {
21133 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
21134 break;
21135 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
21136 }
21137 if (i > 0)
21138 left_padded = 1;
21139
21140 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
21141 : IT_CHARPOS (*it));
21142 /* When no suitable font found, use the default font. */
21143 font_not_found_p = font == NULL;
21144 if (font_not_found_p)
21145 {
21146 face = face->ascii_face;
21147 font = face->font;
21148 }
21149 font_info = FONT_INFO_FROM_FACE (it->f, face);
21150 boff = font_info->baseline_offset;
21151 if (font_info->vertical_centering)
21152 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
21153 font_ascent = FONT_BASE (font) + boff;
21154 font_descent = FONT_DESCENT (font) - boff;
21155 font_height = FONT_HEIGHT (font);
21156
21157 cmp->font = (void *) font;
21158
21159 pcm = NULL;
21160 if (! font_not_found_p)
21161 {
21162 get_char_face_and_encoding (it->f, c, it->face_id,
21163 &char2b, it->multibyte_p, 0);
21164 pcm = get_per_char_metric (it->f, font, font_info, &char2b,
21165 FONT_TYPE_FOR_MULTIBYTE (font, c));
21166 }
21167
21168 /* Initialize the bounding box. */
21169 if (pcm)
21170 {
21171 width = pcm->width;
21172 ascent = pcm->ascent;
21173 descent = pcm->descent;
21174 lbearing = pcm->lbearing;
21175 rbearing = pcm->rbearing;
21176 }
21177 else
21178 {
21179 width = FONT_WIDTH (font);
21180 ascent = FONT_BASE (font);
21181 descent = FONT_DESCENT (font);
21182 lbearing = 0;
21183 rbearing = width;
21184 }
21185
21186 rightmost = width;
21187 leftmost = 0;
21188 lowest = - descent + boff;
21189 highest = ascent + boff;
21190
21191 if (! font_not_found_p
21192 && font_info->default_ascent
21193 && CHAR_TABLE_P (Vuse_default_ascent)
21194 && !NILP (Faref (Vuse_default_ascent,
21195 make_number (it->char_to_display))))
21196 highest = font_info->default_ascent + boff;
21197
21198 /* Draw the first glyph at the normal position. It may be
21199 shifted to right later if some other glyphs are drawn
21200 at the left. */
21201 cmp->offsets[i * 2] = 0;
21202 cmp->offsets[i * 2 + 1] = boff;
21203 cmp->lbearing = lbearing;
21204 cmp->rbearing = rbearing;
21205
21206 /* Set cmp->offsets for the remaining glyphs. */
21207 for (i++; i < glyph_len; i++)
21208 {
21209 int left, right, btm, top;
21210 int ch = COMPOSITION_GLYPH (cmp, i);
21211 int face_id;
21212 struct face *this_face;
21213 int this_boff;
21214
21215 if (ch == '\t')
21216 ch = ' ';
21217 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
21218 this_face = FACE_FROM_ID (it->f, face_id);
21219 font = this_face->font;
21220
21221 if (font == NULL)
21222 pcm = NULL;
21223 else
21224 {
21225 font_info = FONT_INFO_FROM_FACE (it->f, this_face);
21226 this_boff = font_info->baseline_offset;
21227 if (font_info->vertical_centering)
21228 this_boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
21229 get_char_face_and_encoding (it->f, ch, face_id,
21230 &char2b, it->multibyte_p, 0);
21231 pcm = get_per_char_metric (it->f, font, font_info, &char2b,
21232 FONT_TYPE_FOR_MULTIBYTE (font,
21233 ch));
21234 }
21235 if (! pcm)
21236 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
21237 else
21238 {
21239 width = pcm->width;
21240 ascent = pcm->ascent;
21241 descent = pcm->descent;
21242 lbearing = pcm->lbearing;
21243 rbearing = pcm->rbearing;
21244 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
21245 {
21246 /* Relative composition with or without
21247 alternate chars. */
21248 left = (leftmost + rightmost - width) / 2;
21249 btm = - descent + boff;
21250 if (font_info->relative_compose
21251 && (! CHAR_TABLE_P (Vignore_relative_composition)
21252 || NILP (Faref (Vignore_relative_composition,
21253 make_number (ch)))))
21254 {
21255
21256 if (- descent >= font_info->relative_compose)
21257 /* One extra pixel between two glyphs. */
21258 btm = highest + 1;
21259 else if (ascent <= 0)
21260 /* One extra pixel between two glyphs. */
21261 btm = lowest - 1 - ascent - descent;
21262 }
21263 }
21264 else
21265 {
21266 /* A composition rule is specified by an integer
21267 value that encodes global and new reference
21268 points (GREF and NREF). GREF and NREF are
21269 specified by numbers as below:
21270
21271 0---1---2 -- ascent
21272 | |
21273 | |
21274 | |
21275 9--10--11 -- center
21276 | |
21277 ---3---4---5--- baseline
21278 | |
21279 6---7---8 -- descent
21280 */
21281 int rule = COMPOSITION_RULE (cmp, i);
21282 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
21283
21284 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
21285 grefx = gref % 3, nrefx = nref % 3;
21286 grefy = gref / 3, nrefy = nref / 3;
21287 if (xoff)
21288 xoff = font_height * (xoff - 128) / 256;
21289 if (yoff)
21290 yoff = font_height * (yoff - 128) / 256;
21291
21292 left = (leftmost
21293 + grefx * (rightmost - leftmost) / 2
21294 - nrefx * width / 2
21295 + xoff);
21296
21297 btm = ((grefy == 0 ? highest
21298 : grefy == 1 ? 0
21299 : grefy == 2 ? lowest
21300 : (highest + lowest) / 2)
21301 - (nrefy == 0 ? ascent + descent
21302 : nrefy == 1 ? descent - boff
21303 : nrefy == 2 ? 0
21304 : (ascent + descent) / 2)
21305 + yoff);
21306 }
21307
21308 cmp->offsets[i * 2] = left;
21309 cmp->offsets[i * 2 + 1] = btm + descent;
21310
21311 /* Update the bounding box of the overall glyphs. */
21312 if (width > 0)
21313 {
21314 right = left + width;
21315 if (left < leftmost)
21316 leftmost = left;
21317 if (right > rightmost)
21318 rightmost = right;
21319 }
21320 top = btm + descent + ascent;
21321 if (top > highest)
21322 highest = top;
21323 if (btm < lowest)
21324 lowest = btm;
21325
21326 if (cmp->lbearing > left + lbearing)
21327 cmp->lbearing = left + lbearing;
21328 if (cmp->rbearing < left + rbearing)
21329 cmp->rbearing = left + rbearing;
21330 }
21331 }
21332
21333 /* If there are glyphs whose x-offsets are negative,
21334 shift all glyphs to the right and make all x-offsets
21335 non-negative. */
21336 if (leftmost < 0)
21337 {
21338 for (i = 0; i < cmp->glyph_len; i++)
21339 cmp->offsets[i * 2] -= leftmost;
21340 rightmost -= leftmost;
21341 cmp->lbearing -= leftmost;
21342 cmp->rbearing -= leftmost;
21343 }
21344
21345 if (left_padded && cmp->lbearing < 0)
21346 {
21347 for (i = 0; i < cmp->glyph_len; i++)
21348 cmp->offsets[i * 2] -= cmp->lbearing;
21349 rightmost -= cmp->lbearing;
21350 cmp->rbearing -= cmp->lbearing;
21351 cmp->lbearing = 0;
21352 }
21353 if (right_padded && rightmost < cmp->rbearing)
21354 {
21355 rightmost = cmp->rbearing;
21356 }
21357
21358 cmp->pixel_width = rightmost;
21359 cmp->ascent = highest;
21360 cmp->descent = - lowest;
21361 if (cmp->ascent < font_ascent)
21362 cmp->ascent = font_ascent;
21363 if (cmp->descent < font_descent)
21364 cmp->descent = font_descent;
21365 }
21366
21367 if (it->glyph_row
21368 && (cmp->lbearing < 0
21369 || cmp->rbearing > cmp->pixel_width))
21370 it->glyph_row->contains_overlapping_glyphs_p = 1;
21371
21372 it->pixel_width = cmp->pixel_width;
21373 it->ascent = it->phys_ascent = cmp->ascent;
21374 it->descent = it->phys_descent = cmp->descent;
21375
21376 if (face->box != FACE_NO_BOX)
21377 {
21378 int thick = face->box_line_width;
21379
21380 if (thick > 0)
21381 {
21382 it->ascent += thick;
21383 it->descent += thick;
21384 }
21385 else
21386 thick = - thick;
21387
21388 if (it->start_of_box_run_p)
21389 it->pixel_width += thick;
21390 if (it->end_of_box_run_p)
21391 it->pixel_width += thick;
21392 }
21393
21394 /* If face has an overline, add the height of the overline
21395 (1 pixel) and a 1 pixel margin to the character height. */
21396 if (face->overline_p)
21397 it->ascent += overline_margin;
21398
21399 take_vertical_position_into_account (it);
21400
21401 if (it->glyph_row)
21402 append_composite_glyph (it);
21403 }
21404 else if (it->what == IT_IMAGE)
21405 produce_image_glyph (it);
21406 else if (it->what == IT_STRETCH)
21407 produce_stretch_glyph (it);
21408
21409 /* Accumulate dimensions. Note: can't assume that it->descent > 0
21410 because this isn't true for images with `:ascent 100'. */
21411 xassert (it->ascent >= 0 && it->descent >= 0);
21412 if (it->area == TEXT_AREA)
21413 it->current_x += it->pixel_width;
21414
21415 if (extra_line_spacing > 0)
21416 {
21417 it->descent += extra_line_spacing;
21418 if (extra_line_spacing > it->max_extra_line_spacing)
21419 it->max_extra_line_spacing = extra_line_spacing;
21420 }
21421
21422 it->max_ascent = max (it->max_ascent, it->ascent);
21423 it->max_descent = max (it->max_descent, it->descent);
21424 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
21425 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
21426 }
21427
21428 /* EXPORT for RIF:
21429 Output LEN glyphs starting at START at the nominal cursor position.
21430 Advance the nominal cursor over the text. The global variable
21431 updated_window contains the window being updated, updated_row is
21432 the glyph row being updated, and updated_area is the area of that
21433 row being updated. */
21434
21435 void
21436 x_write_glyphs (start, len)
21437 struct glyph *start;
21438 int len;
21439 {
21440 int x, hpos;
21441
21442 xassert (updated_window && updated_row);
21443 BLOCK_INPUT;
21444
21445 /* Write glyphs. */
21446
21447 hpos = start - updated_row->glyphs[updated_area];
21448 x = draw_glyphs (updated_window, output_cursor.x,
21449 updated_row, updated_area,
21450 hpos, hpos + len,
21451 DRAW_NORMAL_TEXT, 0);
21452
21453 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
21454 if (updated_area == TEXT_AREA
21455 && updated_window->phys_cursor_on_p
21456 && updated_window->phys_cursor.vpos == output_cursor.vpos
21457 && updated_window->phys_cursor.hpos >= hpos
21458 && updated_window->phys_cursor.hpos < hpos + len)
21459 updated_window->phys_cursor_on_p = 0;
21460
21461 UNBLOCK_INPUT;
21462
21463 /* Advance the output cursor. */
21464 output_cursor.hpos += len;
21465 output_cursor.x = x;
21466 }
21467
21468
21469 /* EXPORT for RIF:
21470 Insert LEN glyphs from START at the nominal cursor position. */
21471
21472 void
21473 x_insert_glyphs (start, len)
21474 struct glyph *start;
21475 int len;
21476 {
21477 struct frame *f;
21478 struct window *w;
21479 int line_height, shift_by_width, shifted_region_width;
21480 struct glyph_row *row;
21481 struct glyph *glyph;
21482 int frame_x, frame_y;
21483 EMACS_INT hpos;
21484
21485 xassert (updated_window && updated_row);
21486 BLOCK_INPUT;
21487 w = updated_window;
21488 f = XFRAME (WINDOW_FRAME (w));
21489
21490 /* Get the height of the line we are in. */
21491 row = updated_row;
21492 line_height = row->height;
21493
21494 /* Get the width of the glyphs to insert. */
21495 shift_by_width = 0;
21496 for (glyph = start; glyph < start + len; ++glyph)
21497 shift_by_width += glyph->pixel_width;
21498
21499 /* Get the width of the region to shift right. */
21500 shifted_region_width = (window_box_width (w, updated_area)
21501 - output_cursor.x
21502 - shift_by_width);
21503
21504 /* Shift right. */
21505 frame_x = window_box_left (w, updated_area) + output_cursor.x;
21506 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
21507
21508 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
21509 line_height, shift_by_width);
21510
21511 /* Write the glyphs. */
21512 hpos = start - row->glyphs[updated_area];
21513 draw_glyphs (w, output_cursor.x, row, updated_area,
21514 hpos, hpos + len,
21515 DRAW_NORMAL_TEXT, 0);
21516
21517 /* Advance the output cursor. */
21518 output_cursor.hpos += len;
21519 output_cursor.x += shift_by_width;
21520 UNBLOCK_INPUT;
21521 }
21522
21523
21524 /* EXPORT for RIF:
21525 Erase the current text line from the nominal cursor position
21526 (inclusive) to pixel column TO_X (exclusive). The idea is that
21527 everything from TO_X onward is already erased.
21528
21529 TO_X is a pixel position relative to updated_area of
21530 updated_window. TO_X == -1 means clear to the end of this area. */
21531
21532 void
21533 x_clear_end_of_line (to_x)
21534 int to_x;
21535 {
21536 struct frame *f;
21537 struct window *w = updated_window;
21538 int max_x, min_y, max_y;
21539 int from_x, from_y, to_y;
21540
21541 xassert (updated_window && updated_row);
21542 f = XFRAME (w->frame);
21543
21544 if (updated_row->full_width_p)
21545 max_x = WINDOW_TOTAL_WIDTH (w);
21546 else
21547 max_x = window_box_width (w, updated_area);
21548 max_y = window_text_bottom_y (w);
21549
21550 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
21551 of window. For TO_X > 0, truncate to end of drawing area. */
21552 if (to_x == 0)
21553 return;
21554 else if (to_x < 0)
21555 to_x = max_x;
21556 else
21557 to_x = min (to_x, max_x);
21558
21559 to_y = min (max_y, output_cursor.y + updated_row->height);
21560
21561 /* Notice if the cursor will be cleared by this operation. */
21562 if (!updated_row->full_width_p)
21563 notice_overwritten_cursor (w, updated_area,
21564 output_cursor.x, -1,
21565 updated_row->y,
21566 MATRIX_ROW_BOTTOM_Y (updated_row));
21567
21568 from_x = output_cursor.x;
21569
21570 /* Translate to frame coordinates. */
21571 if (updated_row->full_width_p)
21572 {
21573 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
21574 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
21575 }
21576 else
21577 {
21578 int area_left = window_box_left (w, updated_area);
21579 from_x += area_left;
21580 to_x += area_left;
21581 }
21582
21583 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
21584 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
21585 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
21586
21587 /* Prevent inadvertently clearing to end of the X window. */
21588 if (to_x > from_x && to_y > from_y)
21589 {
21590 BLOCK_INPUT;
21591 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
21592 to_x - from_x, to_y - from_y);
21593 UNBLOCK_INPUT;
21594 }
21595 }
21596
21597 #endif /* HAVE_WINDOW_SYSTEM */
21598
21599
21600 \f
21601 /***********************************************************************
21602 Cursor types
21603 ***********************************************************************/
21604
21605 /* Value is the internal representation of the specified cursor type
21606 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
21607 of the bar cursor. */
21608
21609 static enum text_cursor_kinds
21610 get_specified_cursor_type (arg, width)
21611 Lisp_Object arg;
21612 int *width;
21613 {
21614 enum text_cursor_kinds type;
21615
21616 if (NILP (arg))
21617 return NO_CURSOR;
21618
21619 if (EQ (arg, Qbox))
21620 return FILLED_BOX_CURSOR;
21621
21622 if (EQ (arg, Qhollow))
21623 return HOLLOW_BOX_CURSOR;
21624
21625 if (EQ (arg, Qbar))
21626 {
21627 *width = 2;
21628 return BAR_CURSOR;
21629 }
21630
21631 if (CONSP (arg)
21632 && EQ (XCAR (arg), Qbar)
21633 && INTEGERP (XCDR (arg))
21634 && XINT (XCDR (arg)) >= 0)
21635 {
21636 *width = XINT (XCDR (arg));
21637 return BAR_CURSOR;
21638 }
21639
21640 if (EQ (arg, Qhbar))
21641 {
21642 *width = 2;
21643 return HBAR_CURSOR;
21644 }
21645
21646 if (CONSP (arg)
21647 && EQ (XCAR (arg), Qhbar)
21648 && INTEGERP (XCDR (arg))
21649 && XINT (XCDR (arg)) >= 0)
21650 {
21651 *width = XINT (XCDR (arg));
21652 return HBAR_CURSOR;
21653 }
21654
21655 /* Treat anything unknown as "hollow box cursor".
21656 It was bad to signal an error; people have trouble fixing
21657 .Xdefaults with Emacs, when it has something bad in it. */
21658 type = HOLLOW_BOX_CURSOR;
21659
21660 return type;
21661 }
21662
21663 /* Set the default cursor types for specified frame. */
21664 void
21665 set_frame_cursor_types (f, arg)
21666 struct frame *f;
21667 Lisp_Object arg;
21668 {
21669 int width;
21670 Lisp_Object tem;
21671
21672 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
21673 FRAME_CURSOR_WIDTH (f) = width;
21674
21675 /* By default, set up the blink-off state depending on the on-state. */
21676
21677 tem = Fassoc (arg, Vblink_cursor_alist);
21678 if (!NILP (tem))
21679 {
21680 FRAME_BLINK_OFF_CURSOR (f)
21681 = get_specified_cursor_type (XCDR (tem), &width);
21682 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
21683 }
21684 else
21685 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
21686 }
21687
21688
21689 /* Return the cursor we want to be displayed in window W. Return
21690 width of bar/hbar cursor through WIDTH arg. Return with
21691 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
21692 (i.e. if the `system caret' should track this cursor).
21693
21694 In a mini-buffer window, we want the cursor only to appear if we
21695 are reading input from this window. For the selected window, we
21696 want the cursor type given by the frame parameter or buffer local
21697 setting of cursor-type. If explicitly marked off, draw no cursor.
21698 In all other cases, we want a hollow box cursor. */
21699
21700 static enum text_cursor_kinds
21701 get_window_cursor_type (w, glyph, width, active_cursor)
21702 struct window *w;
21703 struct glyph *glyph;
21704 int *width;
21705 int *active_cursor;
21706 {
21707 struct frame *f = XFRAME (w->frame);
21708 struct buffer *b = XBUFFER (w->buffer);
21709 int cursor_type = DEFAULT_CURSOR;
21710 Lisp_Object alt_cursor;
21711 int non_selected = 0;
21712
21713 *active_cursor = 1;
21714
21715 /* Echo area */
21716 if (cursor_in_echo_area
21717 && FRAME_HAS_MINIBUF_P (f)
21718 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
21719 {
21720 if (w == XWINDOW (echo_area_window))
21721 {
21722 if (EQ (b->cursor_type, Qt) || NILP (b->cursor_type))
21723 {
21724 *width = FRAME_CURSOR_WIDTH (f);
21725 return FRAME_DESIRED_CURSOR (f);
21726 }
21727 else
21728 return get_specified_cursor_type (b->cursor_type, width);
21729 }
21730
21731 *active_cursor = 0;
21732 non_selected = 1;
21733 }
21734
21735 /* Detect a nonselected window or nonselected frame. */
21736 else if (w != XWINDOW (f->selected_window)
21737 #ifdef HAVE_WINDOW_SYSTEM
21738 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame
21739 #endif
21740 )
21741 {
21742 *active_cursor = 0;
21743
21744 if (MINI_WINDOW_P (w) && minibuf_level == 0)
21745 return NO_CURSOR;
21746
21747 non_selected = 1;
21748 }
21749
21750 /* Never display a cursor in a window in which cursor-type is nil. */
21751 if (NILP (b->cursor_type))
21752 return NO_CURSOR;
21753
21754 /* Get the normal cursor type for this window. */
21755 if (EQ (b->cursor_type, Qt))
21756 {
21757 cursor_type = FRAME_DESIRED_CURSOR (f);
21758 *width = FRAME_CURSOR_WIDTH (f);
21759 }
21760 else
21761 cursor_type = get_specified_cursor_type (b->cursor_type, width);
21762
21763 /* Use cursor-in-non-selected-windows instead
21764 for non-selected window or frame. */
21765 if (non_selected)
21766 {
21767 alt_cursor = b->cursor_in_non_selected_windows;
21768 if (!EQ (Qt, alt_cursor))
21769 return get_specified_cursor_type (alt_cursor, width);
21770 /* t means modify the normal cursor type. */
21771 if (cursor_type == FILLED_BOX_CURSOR)
21772 cursor_type = HOLLOW_BOX_CURSOR;
21773 else if (cursor_type == BAR_CURSOR && *width > 1)
21774 --*width;
21775 return cursor_type;
21776 }
21777
21778 /* Use normal cursor if not blinked off. */
21779 if (!w->cursor_off_p)
21780 {
21781 #ifdef HAVE_WINDOW_SYSTEM
21782 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
21783 {
21784 if (cursor_type == FILLED_BOX_CURSOR)
21785 {
21786 /* Using a block cursor on large images can be very annoying.
21787 So use a hollow cursor for "large" images.
21788 If image is not transparent (no mask), also use hollow cursor. */
21789 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
21790 if (img != NULL && IMAGEP (img->spec))
21791 {
21792 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
21793 where N = size of default frame font size.
21794 This should cover most of the "tiny" icons people may use. */
21795 if (!img->mask
21796 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
21797 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
21798 cursor_type = HOLLOW_BOX_CURSOR;
21799 }
21800 }
21801 else if (cursor_type != NO_CURSOR)
21802 {
21803 /* Display current only supports BOX and HOLLOW cursors for images.
21804 So for now, unconditionally use a HOLLOW cursor when cursor is
21805 not a solid box cursor. */
21806 cursor_type = HOLLOW_BOX_CURSOR;
21807 }
21808 }
21809 #endif
21810 return cursor_type;
21811 }
21812
21813 /* Cursor is blinked off, so determine how to "toggle" it. */
21814
21815 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
21816 if ((alt_cursor = Fassoc (b->cursor_type, Vblink_cursor_alist), !NILP (alt_cursor)))
21817 return get_specified_cursor_type (XCDR (alt_cursor), width);
21818
21819 /* Then see if frame has specified a specific blink off cursor type. */
21820 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
21821 {
21822 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
21823 return FRAME_BLINK_OFF_CURSOR (f);
21824 }
21825
21826 #if 0
21827 /* Some people liked having a permanently visible blinking cursor,
21828 while others had very strong opinions against it. So it was
21829 decided to remove it. KFS 2003-09-03 */
21830
21831 /* Finally perform built-in cursor blinking:
21832 filled box <-> hollow box
21833 wide [h]bar <-> narrow [h]bar
21834 narrow [h]bar <-> no cursor
21835 other type <-> no cursor */
21836
21837 if (cursor_type == FILLED_BOX_CURSOR)
21838 return HOLLOW_BOX_CURSOR;
21839
21840 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
21841 {
21842 *width = 1;
21843 return cursor_type;
21844 }
21845 #endif
21846
21847 return NO_CURSOR;
21848 }
21849
21850
21851 #ifdef HAVE_WINDOW_SYSTEM
21852
21853 /* Notice when the text cursor of window W has been completely
21854 overwritten by a drawing operation that outputs glyphs in AREA
21855 starting at X0 and ending at X1 in the line starting at Y0 and
21856 ending at Y1. X coordinates are area-relative. X1 < 0 means all
21857 the rest of the line after X0 has been written. Y coordinates
21858 are window-relative. */
21859
21860 static void
21861 notice_overwritten_cursor (w, area, x0, x1, y0, y1)
21862 struct window *w;
21863 enum glyph_row_area area;
21864 int x0, y0, x1, y1;
21865 {
21866 int cx0, cx1, cy0, cy1;
21867 struct glyph_row *row;
21868
21869 if (!w->phys_cursor_on_p)
21870 return;
21871 if (area != TEXT_AREA)
21872 return;
21873
21874 if (w->phys_cursor.vpos < 0
21875 || w->phys_cursor.vpos >= w->current_matrix->nrows
21876 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
21877 !(row->enabled_p && row->displays_text_p)))
21878 return;
21879
21880 if (row->cursor_in_fringe_p)
21881 {
21882 row->cursor_in_fringe_p = 0;
21883 draw_fringe_bitmap (w, row, 0);
21884 w->phys_cursor_on_p = 0;
21885 return;
21886 }
21887
21888 cx0 = w->phys_cursor.x;
21889 cx1 = cx0 + w->phys_cursor_width;
21890 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
21891 return;
21892
21893 /* The cursor image will be completely removed from the
21894 screen if the output area intersects the cursor area in
21895 y-direction. When we draw in [y0 y1[, and some part of
21896 the cursor is at y < y0, that part must have been drawn
21897 before. When scrolling, the cursor is erased before
21898 actually scrolling, so we don't come here. When not
21899 scrolling, the rows above the old cursor row must have
21900 changed, and in this case these rows must have written
21901 over the cursor image.
21902
21903 Likewise if part of the cursor is below y1, with the
21904 exception of the cursor being in the first blank row at
21905 the buffer and window end because update_text_area
21906 doesn't draw that row. (Except when it does, but
21907 that's handled in update_text_area.) */
21908
21909 cy0 = w->phys_cursor.y;
21910 cy1 = cy0 + w->phys_cursor_height;
21911 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
21912 return;
21913
21914 w->phys_cursor_on_p = 0;
21915 }
21916
21917 #endif /* HAVE_WINDOW_SYSTEM */
21918
21919 \f
21920 /************************************************************************
21921 Mouse Face
21922 ************************************************************************/
21923
21924 #ifdef HAVE_WINDOW_SYSTEM
21925
21926 /* EXPORT for RIF:
21927 Fix the display of area AREA of overlapping row ROW in window W
21928 with respect to the overlapping part OVERLAPS. */
21929
21930 void
21931 x_fix_overlapping_area (w, row, area, overlaps)
21932 struct window *w;
21933 struct glyph_row *row;
21934 enum glyph_row_area area;
21935 int overlaps;
21936 {
21937 int i, x;
21938
21939 BLOCK_INPUT;
21940
21941 x = 0;
21942 for (i = 0; i < row->used[area];)
21943 {
21944 if (row->glyphs[area][i].overlaps_vertically_p)
21945 {
21946 int start = i, start_x = x;
21947
21948 do
21949 {
21950 x += row->glyphs[area][i].pixel_width;
21951 ++i;
21952 }
21953 while (i < row->used[area]
21954 && row->glyphs[area][i].overlaps_vertically_p);
21955
21956 draw_glyphs (w, start_x, row, area,
21957 start, i,
21958 DRAW_NORMAL_TEXT, overlaps);
21959 }
21960 else
21961 {
21962 x += row->glyphs[area][i].pixel_width;
21963 ++i;
21964 }
21965 }
21966
21967 UNBLOCK_INPUT;
21968 }
21969
21970
21971 /* EXPORT:
21972 Draw the cursor glyph of window W in glyph row ROW. See the
21973 comment of draw_glyphs for the meaning of HL. */
21974
21975 void
21976 draw_phys_cursor_glyph (w, row, hl)
21977 struct window *w;
21978 struct glyph_row *row;
21979 enum draw_glyphs_face hl;
21980 {
21981 /* If cursor hpos is out of bounds, don't draw garbage. This can
21982 happen in mini-buffer windows when switching between echo area
21983 glyphs and mini-buffer. */
21984 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
21985 {
21986 int on_p = w->phys_cursor_on_p;
21987 int x1;
21988 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
21989 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
21990 hl, 0);
21991 w->phys_cursor_on_p = on_p;
21992
21993 if (hl == DRAW_CURSOR)
21994 w->phys_cursor_width = x1 - w->phys_cursor.x;
21995 /* When we erase the cursor, and ROW is overlapped by other
21996 rows, make sure that these overlapping parts of other rows
21997 are redrawn. */
21998 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
21999 {
22000 w->phys_cursor_width = x1 - w->phys_cursor.x;
22001
22002 if (row > w->current_matrix->rows
22003 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
22004 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
22005 OVERLAPS_ERASED_CURSOR);
22006
22007 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
22008 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
22009 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
22010 OVERLAPS_ERASED_CURSOR);
22011 }
22012 }
22013 }
22014
22015
22016 /* EXPORT:
22017 Erase the image of a cursor of window W from the screen. */
22018
22019 void
22020 erase_phys_cursor (w)
22021 struct window *w;
22022 {
22023 struct frame *f = XFRAME (w->frame);
22024 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
22025 int hpos = w->phys_cursor.hpos;
22026 int vpos = w->phys_cursor.vpos;
22027 int mouse_face_here_p = 0;
22028 struct glyph_matrix *active_glyphs = w->current_matrix;
22029 struct glyph_row *cursor_row;
22030 struct glyph *cursor_glyph;
22031 enum draw_glyphs_face hl;
22032
22033 /* No cursor displayed or row invalidated => nothing to do on the
22034 screen. */
22035 if (w->phys_cursor_type == NO_CURSOR)
22036 goto mark_cursor_off;
22037
22038 /* VPOS >= active_glyphs->nrows means that window has been resized.
22039 Don't bother to erase the cursor. */
22040 if (vpos >= active_glyphs->nrows)
22041 goto mark_cursor_off;
22042
22043 /* If row containing cursor is marked invalid, there is nothing we
22044 can do. */
22045 cursor_row = MATRIX_ROW (active_glyphs, vpos);
22046 if (!cursor_row->enabled_p)
22047 goto mark_cursor_off;
22048
22049 /* If line spacing is > 0, old cursor may only be partially visible in
22050 window after split-window. So adjust visible height. */
22051 cursor_row->visible_height = min (cursor_row->visible_height,
22052 window_text_bottom_y (w) - cursor_row->y);
22053
22054 /* If row is completely invisible, don't attempt to delete a cursor which
22055 isn't there. This can happen if cursor is at top of a window, and
22056 we switch to a buffer with a header line in that window. */
22057 if (cursor_row->visible_height <= 0)
22058 goto mark_cursor_off;
22059
22060 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
22061 if (cursor_row->cursor_in_fringe_p)
22062 {
22063 cursor_row->cursor_in_fringe_p = 0;
22064 draw_fringe_bitmap (w, cursor_row, 0);
22065 goto mark_cursor_off;
22066 }
22067
22068 /* This can happen when the new row is shorter than the old one.
22069 In this case, either draw_glyphs or clear_end_of_line
22070 should have cleared the cursor. Note that we wouldn't be
22071 able to erase the cursor in this case because we don't have a
22072 cursor glyph at hand. */
22073 if (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])
22074 goto mark_cursor_off;
22075
22076 /* If the cursor is in the mouse face area, redisplay that when
22077 we clear the cursor. */
22078 if (! NILP (dpyinfo->mouse_face_window)
22079 && w == XWINDOW (dpyinfo->mouse_face_window)
22080 && (vpos > dpyinfo->mouse_face_beg_row
22081 || (vpos == dpyinfo->mouse_face_beg_row
22082 && hpos >= dpyinfo->mouse_face_beg_col))
22083 && (vpos < dpyinfo->mouse_face_end_row
22084 || (vpos == dpyinfo->mouse_face_end_row
22085 && hpos < dpyinfo->mouse_face_end_col))
22086 /* Don't redraw the cursor's spot in mouse face if it is at the
22087 end of a line (on a newline). The cursor appears there, but
22088 mouse highlighting does not. */
22089 && cursor_row->used[TEXT_AREA] > hpos)
22090 mouse_face_here_p = 1;
22091
22092 /* Maybe clear the display under the cursor. */
22093 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
22094 {
22095 int x, y, left_x;
22096 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
22097 int width;
22098
22099 cursor_glyph = get_phys_cursor_glyph (w);
22100 if (cursor_glyph == NULL)
22101 goto mark_cursor_off;
22102
22103 width = cursor_glyph->pixel_width;
22104 left_x = window_box_left_offset (w, TEXT_AREA);
22105 x = w->phys_cursor.x;
22106 if (x < left_x)
22107 width -= left_x - x;
22108 width = min (width, window_box_width (w, TEXT_AREA) - x);
22109 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
22110 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, max (x, left_x));
22111
22112 if (width > 0)
22113 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
22114 }
22115
22116 /* Erase the cursor by redrawing the character underneath it. */
22117 if (mouse_face_here_p)
22118 hl = DRAW_MOUSE_FACE;
22119 else
22120 hl = DRAW_NORMAL_TEXT;
22121 draw_phys_cursor_glyph (w, cursor_row, hl);
22122
22123 mark_cursor_off:
22124 w->phys_cursor_on_p = 0;
22125 w->phys_cursor_type = NO_CURSOR;
22126 }
22127
22128
22129 /* EXPORT:
22130 Display or clear cursor of window W. If ON is zero, clear the
22131 cursor. If it is non-zero, display the cursor. If ON is nonzero,
22132 where to put the cursor is specified by HPOS, VPOS, X and Y. */
22133
22134 void
22135 display_and_set_cursor (w, on, hpos, vpos, x, y)
22136 struct window *w;
22137 int on, hpos, vpos, x, y;
22138 {
22139 struct frame *f = XFRAME (w->frame);
22140 int new_cursor_type;
22141 int new_cursor_width;
22142 int active_cursor;
22143 struct glyph_row *glyph_row;
22144 struct glyph *glyph;
22145
22146 /* This is pointless on invisible frames, and dangerous on garbaged
22147 windows and frames; in the latter case, the frame or window may
22148 be in the midst of changing its size, and x and y may be off the
22149 window. */
22150 if (! FRAME_VISIBLE_P (f)
22151 || FRAME_GARBAGED_P (f)
22152 || vpos >= w->current_matrix->nrows
22153 || hpos >= w->current_matrix->matrix_w)
22154 return;
22155
22156 /* If cursor is off and we want it off, return quickly. */
22157 if (!on && !w->phys_cursor_on_p)
22158 return;
22159
22160 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
22161 /* If cursor row is not enabled, we don't really know where to
22162 display the cursor. */
22163 if (!glyph_row->enabled_p)
22164 {
22165 w->phys_cursor_on_p = 0;
22166 return;
22167 }
22168
22169 glyph = NULL;
22170 if (!glyph_row->exact_window_width_line_p
22171 || hpos < glyph_row->used[TEXT_AREA])
22172 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
22173
22174 xassert (interrupt_input_blocked);
22175
22176 /* Set new_cursor_type to the cursor we want to be displayed. */
22177 new_cursor_type = get_window_cursor_type (w, glyph,
22178 &new_cursor_width, &active_cursor);
22179
22180 /* If cursor is currently being shown and we don't want it to be or
22181 it is in the wrong place, or the cursor type is not what we want,
22182 erase it. */
22183 if (w->phys_cursor_on_p
22184 && (!on
22185 || w->phys_cursor.x != x
22186 || w->phys_cursor.y != y
22187 || new_cursor_type != w->phys_cursor_type
22188 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
22189 && new_cursor_width != w->phys_cursor_width)))
22190 erase_phys_cursor (w);
22191
22192 /* Don't check phys_cursor_on_p here because that flag is only set
22193 to zero in some cases where we know that the cursor has been
22194 completely erased, to avoid the extra work of erasing the cursor
22195 twice. In other words, phys_cursor_on_p can be 1 and the cursor
22196 still not be visible, or it has only been partly erased. */
22197 if (on)
22198 {
22199 w->phys_cursor_ascent = glyph_row->ascent;
22200 w->phys_cursor_height = glyph_row->height;
22201
22202 /* Set phys_cursor_.* before x_draw_.* is called because some
22203 of them may need the information. */
22204 w->phys_cursor.x = x;
22205 w->phys_cursor.y = glyph_row->y;
22206 w->phys_cursor.hpos = hpos;
22207 w->phys_cursor.vpos = vpos;
22208 }
22209
22210 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
22211 new_cursor_type, new_cursor_width,
22212 on, active_cursor);
22213 }
22214
22215
22216 /* Switch the display of W's cursor on or off, according to the value
22217 of ON. */
22218
22219 static void
22220 update_window_cursor (w, on)
22221 struct window *w;
22222 int on;
22223 {
22224 /* Don't update cursor in windows whose frame is in the process
22225 of being deleted. */
22226 if (w->current_matrix)
22227 {
22228 BLOCK_INPUT;
22229 display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
22230 w->phys_cursor.x, w->phys_cursor.y);
22231 UNBLOCK_INPUT;
22232 }
22233 }
22234
22235
22236 /* Call update_window_cursor with parameter ON_P on all leaf windows
22237 in the window tree rooted at W. */
22238
22239 static void
22240 update_cursor_in_window_tree (w, on_p)
22241 struct window *w;
22242 int on_p;
22243 {
22244 while (w)
22245 {
22246 if (!NILP (w->hchild))
22247 update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
22248 else if (!NILP (w->vchild))
22249 update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
22250 else
22251 update_window_cursor (w, on_p);
22252
22253 w = NILP (w->next) ? 0 : XWINDOW (w->next);
22254 }
22255 }
22256
22257
22258 /* EXPORT:
22259 Display the cursor on window W, or clear it, according to ON_P.
22260 Don't change the cursor's position. */
22261
22262 void
22263 x_update_cursor (f, on_p)
22264 struct frame *f;
22265 int on_p;
22266 {
22267 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
22268 }
22269
22270
22271 /* EXPORT:
22272 Clear the cursor of window W to background color, and mark the
22273 cursor as not shown. This is used when the text where the cursor
22274 is is about to be rewritten. */
22275
22276 void
22277 x_clear_cursor (w)
22278 struct window *w;
22279 {
22280 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
22281 update_window_cursor (w, 0);
22282 }
22283
22284
22285 /* EXPORT:
22286 Display the active region described by mouse_face_* according to DRAW. */
22287
22288 void
22289 show_mouse_face (dpyinfo, draw)
22290 Display_Info *dpyinfo;
22291 enum draw_glyphs_face draw;
22292 {
22293 struct window *w = XWINDOW (dpyinfo->mouse_face_window);
22294 struct frame *f = XFRAME (WINDOW_FRAME (w));
22295
22296 if (/* If window is in the process of being destroyed, don't bother
22297 to do anything. */
22298 w->current_matrix != NULL
22299 /* Don't update mouse highlight if hidden */
22300 && (draw != DRAW_MOUSE_FACE || !dpyinfo->mouse_face_hidden)
22301 /* Recognize when we are called to operate on rows that don't exist
22302 anymore. This can happen when a window is split. */
22303 && dpyinfo->mouse_face_end_row < w->current_matrix->nrows)
22304 {
22305 int phys_cursor_on_p = w->phys_cursor_on_p;
22306 struct glyph_row *row, *first, *last;
22307
22308 first = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_beg_row);
22309 last = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_end_row);
22310
22311 for (row = first; row <= last && row->enabled_p; ++row)
22312 {
22313 int start_hpos, end_hpos, start_x;
22314
22315 /* For all but the first row, the highlight starts at column 0. */
22316 if (row == first)
22317 {
22318 start_hpos = dpyinfo->mouse_face_beg_col;
22319 start_x = dpyinfo->mouse_face_beg_x;
22320 }
22321 else
22322 {
22323 start_hpos = 0;
22324 start_x = 0;
22325 }
22326
22327 if (row == last)
22328 end_hpos = dpyinfo->mouse_face_end_col;
22329 else
22330 {
22331 end_hpos = row->used[TEXT_AREA];
22332 if (draw == DRAW_NORMAL_TEXT)
22333 row->fill_line_p = 1; /* Clear to end of line */
22334 }
22335
22336 if (end_hpos > start_hpos)
22337 {
22338 draw_glyphs (w, start_x, row, TEXT_AREA,
22339 start_hpos, end_hpos,
22340 draw, 0);
22341
22342 row->mouse_face_p
22343 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
22344 }
22345 }
22346
22347 /* When we've written over the cursor, arrange for it to
22348 be displayed again. */
22349 if (phys_cursor_on_p && !w->phys_cursor_on_p)
22350 {
22351 BLOCK_INPUT;
22352 display_and_set_cursor (w, 1,
22353 w->phys_cursor.hpos, w->phys_cursor.vpos,
22354 w->phys_cursor.x, w->phys_cursor.y);
22355 UNBLOCK_INPUT;
22356 }
22357 }
22358
22359 /* Change the mouse cursor. */
22360 if (draw == DRAW_NORMAL_TEXT && !EQ (dpyinfo->mouse_face_window, f->tool_bar_window))
22361 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
22362 else if (draw == DRAW_MOUSE_FACE)
22363 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
22364 else
22365 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
22366 }
22367
22368 /* EXPORT:
22369 Clear out the mouse-highlighted active region.
22370 Redraw it un-highlighted first. Value is non-zero if mouse
22371 face was actually drawn unhighlighted. */
22372
22373 int
22374 clear_mouse_face (dpyinfo)
22375 Display_Info *dpyinfo;
22376 {
22377 int cleared = 0;
22378
22379 if (!dpyinfo->mouse_face_hidden && !NILP (dpyinfo->mouse_face_window))
22380 {
22381 show_mouse_face (dpyinfo, DRAW_NORMAL_TEXT);
22382 cleared = 1;
22383 }
22384
22385 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
22386 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
22387 dpyinfo->mouse_face_window = Qnil;
22388 dpyinfo->mouse_face_overlay = Qnil;
22389 return cleared;
22390 }
22391
22392
22393 /* EXPORT:
22394 Non-zero if physical cursor of window W is within mouse face. */
22395
22396 int
22397 cursor_in_mouse_face_p (w)
22398 struct window *w;
22399 {
22400 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
22401 int in_mouse_face = 0;
22402
22403 if (WINDOWP (dpyinfo->mouse_face_window)
22404 && XWINDOW (dpyinfo->mouse_face_window) == w)
22405 {
22406 int hpos = w->phys_cursor.hpos;
22407 int vpos = w->phys_cursor.vpos;
22408
22409 if (vpos >= dpyinfo->mouse_face_beg_row
22410 && vpos <= dpyinfo->mouse_face_end_row
22411 && (vpos > dpyinfo->mouse_face_beg_row
22412 || hpos >= dpyinfo->mouse_face_beg_col)
22413 && (vpos < dpyinfo->mouse_face_end_row
22414 || hpos < dpyinfo->mouse_face_end_col
22415 || dpyinfo->mouse_face_past_end))
22416 in_mouse_face = 1;
22417 }
22418
22419 return in_mouse_face;
22420 }
22421
22422
22423
22424 \f
22425 /* Find the glyph matrix position of buffer position CHARPOS in window
22426 *W. HPOS, *VPOS, *X, and *Y are set to the positions found. W's
22427 current glyphs must be up to date. If CHARPOS is above window
22428 start return (0, 0, 0, 0). If CHARPOS is after end of W, return end
22429 of last line in W. In the row containing CHARPOS, stop before glyphs
22430 having STOP as object. */
22431
22432 #if 1 /* This is a version of fast_find_position that's more correct
22433 in the presence of hscrolling, for example. I didn't install
22434 it right away because the problem fixed is minor, it failed
22435 in 20.x as well, and I think it's too risky to install
22436 so near the release of 21.1. 2001-09-25 gerd. */
22437
22438 static int
22439 fast_find_position (w, charpos, hpos, vpos, x, y, stop)
22440 struct window *w;
22441 EMACS_INT charpos;
22442 int *hpos, *vpos, *x, *y;
22443 Lisp_Object stop;
22444 {
22445 struct glyph_row *row, *first;
22446 struct glyph *glyph, *end;
22447 int past_end = 0;
22448
22449 first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
22450 if (charpos < MATRIX_ROW_START_CHARPOS (first))
22451 {
22452 *x = first->x;
22453 *y = first->y;
22454 *hpos = 0;
22455 *vpos = MATRIX_ROW_VPOS (first, w->current_matrix);
22456 return 1;
22457 }
22458
22459 row = row_containing_pos (w, charpos, first, NULL, 0);
22460 if (row == NULL)
22461 {
22462 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
22463 past_end = 1;
22464 }
22465
22466 /* If whole rows or last part of a row came from a display overlay,
22467 row_containing_pos will skip over such rows because their end pos
22468 equals the start pos of the overlay or interval.
22469
22470 Move back if we have a STOP object and previous row's
22471 end glyph came from STOP. */
22472 if (!NILP (stop))
22473 {
22474 struct glyph_row *prev;
22475 while ((prev = row - 1, prev >= first)
22476 && MATRIX_ROW_END_CHARPOS (prev) == charpos
22477 && prev->used[TEXT_AREA] > 0)
22478 {
22479 struct glyph *beg = prev->glyphs[TEXT_AREA];
22480 glyph = beg + prev->used[TEXT_AREA];
22481 while (--glyph >= beg
22482 && INTEGERP (glyph->object));
22483 if (glyph < beg
22484 || !EQ (stop, glyph->object))
22485 break;
22486 row = prev;
22487 }
22488 }
22489
22490 *x = row->x;
22491 *y = row->y;
22492 *vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
22493
22494 glyph = row->glyphs[TEXT_AREA];
22495 end = glyph + row->used[TEXT_AREA];
22496
22497 /* Skip over glyphs not having an object at the start of the row.
22498 These are special glyphs like truncation marks on terminal
22499 frames. */
22500 if (row->displays_text_p)
22501 while (glyph < end
22502 && INTEGERP (glyph->object)
22503 && !EQ (stop, glyph->object)
22504 && glyph->charpos < 0)
22505 {
22506 *x += glyph->pixel_width;
22507 ++glyph;
22508 }
22509
22510 while (glyph < end
22511 && !INTEGERP (glyph->object)
22512 && !EQ (stop, glyph->object)
22513 && (!BUFFERP (glyph->object)
22514 || glyph->charpos < charpos))
22515 {
22516 *x += glyph->pixel_width;
22517 ++glyph;
22518 }
22519
22520 *hpos = glyph - row->glyphs[TEXT_AREA];
22521 return !past_end;
22522 }
22523
22524 #else /* not 1 */
22525
22526 static int
22527 fast_find_position (w, pos, hpos, vpos, x, y, stop)
22528 struct window *w;
22529 EMACS_INT pos;
22530 int *hpos, *vpos, *x, *y;
22531 Lisp_Object stop;
22532 {
22533 int i;
22534 int lastcol;
22535 int maybe_next_line_p = 0;
22536 int line_start_position;
22537 int yb = window_text_bottom_y (w);
22538 struct glyph_row *row, *best_row;
22539 int row_vpos, best_row_vpos;
22540 int current_x;
22541
22542 row = best_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
22543 row_vpos = best_row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
22544
22545 while (row->y < yb)
22546 {
22547 if (row->used[TEXT_AREA])
22548 line_start_position = row->glyphs[TEXT_AREA]->charpos;
22549 else
22550 line_start_position = 0;
22551
22552 if (line_start_position > pos)
22553 break;
22554 /* If the position sought is the end of the buffer,
22555 don't include the blank lines at the bottom of the window. */
22556 else if (line_start_position == pos
22557 && pos == BUF_ZV (XBUFFER (w->buffer)))
22558 {
22559 maybe_next_line_p = 1;
22560 break;
22561 }
22562 else if (line_start_position > 0)
22563 {
22564 best_row = row;
22565 best_row_vpos = row_vpos;
22566 }
22567
22568 if (row->y + row->height >= yb)
22569 break;
22570
22571 ++row;
22572 ++row_vpos;
22573 }
22574
22575 /* Find the right column within BEST_ROW. */
22576 lastcol = 0;
22577 current_x = best_row->x;
22578 for (i = 0; i < best_row->used[TEXT_AREA]; i++)
22579 {
22580 struct glyph *glyph = best_row->glyphs[TEXT_AREA] + i;
22581 int charpos = glyph->charpos;
22582
22583 if (BUFFERP (glyph->object))
22584 {
22585 if (charpos == pos)
22586 {
22587 *hpos = i;
22588 *vpos = best_row_vpos;
22589 *x = current_x;
22590 *y = best_row->y;
22591 return 1;
22592 }
22593 else if (charpos > pos)
22594 break;
22595 }
22596 else if (EQ (glyph->object, stop))
22597 break;
22598
22599 if (charpos > 0)
22600 lastcol = i;
22601 current_x += glyph->pixel_width;
22602 }
22603
22604 /* If we're looking for the end of the buffer,
22605 and we didn't find it in the line we scanned,
22606 use the start of the following line. */
22607 if (maybe_next_line_p)
22608 {
22609 ++best_row;
22610 ++best_row_vpos;
22611 lastcol = 0;
22612 current_x = best_row->x;
22613 }
22614
22615 *vpos = best_row_vpos;
22616 *hpos = lastcol + 1;
22617 *x = current_x;
22618 *y = best_row->y;
22619 return 0;
22620 }
22621
22622 #endif /* not 1 */
22623
22624
22625 /* Find the position of the glyph for position POS in OBJECT in
22626 window W's current matrix, and return in *X, *Y the pixel
22627 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
22628
22629 RIGHT_P non-zero means return the position of the right edge of the
22630 glyph, RIGHT_P zero means return the left edge position.
22631
22632 If no glyph for POS exists in the matrix, return the position of
22633 the glyph with the next smaller position that is in the matrix, if
22634 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
22635 exists in the matrix, return the position of the glyph with the
22636 next larger position in OBJECT.
22637
22638 Value is non-zero if a glyph was found. */
22639
22640 static int
22641 fast_find_string_pos (w, pos, object, hpos, vpos, x, y, right_p)
22642 struct window *w;
22643 EMACS_INT pos;
22644 Lisp_Object object;
22645 int *hpos, *vpos, *x, *y;
22646 int right_p;
22647 {
22648 int yb = window_text_bottom_y (w);
22649 struct glyph_row *r;
22650 struct glyph *best_glyph = NULL;
22651 struct glyph_row *best_row = NULL;
22652 int best_x = 0;
22653
22654 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
22655 r->enabled_p && r->y < yb;
22656 ++r)
22657 {
22658 struct glyph *g = r->glyphs[TEXT_AREA];
22659 struct glyph *e = g + r->used[TEXT_AREA];
22660 int gx;
22661
22662 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
22663 if (EQ (g->object, object))
22664 {
22665 if (g->charpos == pos)
22666 {
22667 best_glyph = g;
22668 best_x = gx;
22669 best_row = r;
22670 goto found;
22671 }
22672 else if (best_glyph == NULL
22673 || ((eabs (g->charpos - pos)
22674 < eabs (best_glyph->charpos - pos))
22675 && (right_p
22676 ? g->charpos < pos
22677 : g->charpos > pos)))
22678 {
22679 best_glyph = g;
22680 best_x = gx;
22681 best_row = r;
22682 }
22683 }
22684 }
22685
22686 found:
22687
22688 if (best_glyph)
22689 {
22690 *x = best_x;
22691 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
22692
22693 if (right_p)
22694 {
22695 *x += best_glyph->pixel_width;
22696 ++*hpos;
22697 }
22698
22699 *y = best_row->y;
22700 *vpos = best_row - w->current_matrix->rows;
22701 }
22702
22703 return best_glyph != NULL;
22704 }
22705
22706
22707 /* See if position X, Y is within a hot-spot of an image. */
22708
22709 static int
22710 on_hot_spot_p (hot_spot, x, y)
22711 Lisp_Object hot_spot;
22712 int x, y;
22713 {
22714 if (!CONSP (hot_spot))
22715 return 0;
22716
22717 if (EQ (XCAR (hot_spot), Qrect))
22718 {
22719 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
22720 Lisp_Object rect = XCDR (hot_spot);
22721 Lisp_Object tem;
22722 if (!CONSP (rect))
22723 return 0;
22724 if (!CONSP (XCAR (rect)))
22725 return 0;
22726 if (!CONSP (XCDR (rect)))
22727 return 0;
22728 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
22729 return 0;
22730 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
22731 return 0;
22732 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
22733 return 0;
22734 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
22735 return 0;
22736 return 1;
22737 }
22738 else if (EQ (XCAR (hot_spot), Qcircle))
22739 {
22740 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
22741 Lisp_Object circ = XCDR (hot_spot);
22742 Lisp_Object lr, lx0, ly0;
22743 if (CONSP (circ)
22744 && CONSP (XCAR (circ))
22745 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
22746 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
22747 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
22748 {
22749 double r = XFLOATINT (lr);
22750 double dx = XINT (lx0) - x;
22751 double dy = XINT (ly0) - y;
22752 return (dx * dx + dy * dy <= r * r);
22753 }
22754 }
22755 else if (EQ (XCAR (hot_spot), Qpoly))
22756 {
22757 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
22758 if (VECTORP (XCDR (hot_spot)))
22759 {
22760 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
22761 Lisp_Object *poly = v->contents;
22762 int n = v->size;
22763 int i;
22764 int inside = 0;
22765 Lisp_Object lx, ly;
22766 int x0, y0;
22767
22768 /* Need an even number of coordinates, and at least 3 edges. */
22769 if (n < 6 || n & 1)
22770 return 0;
22771
22772 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
22773 If count is odd, we are inside polygon. Pixels on edges
22774 may or may not be included depending on actual geometry of the
22775 polygon. */
22776 if ((lx = poly[n-2], !INTEGERP (lx))
22777 || (ly = poly[n-1], !INTEGERP (lx)))
22778 return 0;
22779 x0 = XINT (lx), y0 = XINT (ly);
22780 for (i = 0; i < n; i += 2)
22781 {
22782 int x1 = x0, y1 = y0;
22783 if ((lx = poly[i], !INTEGERP (lx))
22784 || (ly = poly[i+1], !INTEGERP (ly)))
22785 return 0;
22786 x0 = XINT (lx), y0 = XINT (ly);
22787
22788 /* Does this segment cross the X line? */
22789 if (x0 >= x)
22790 {
22791 if (x1 >= x)
22792 continue;
22793 }
22794 else if (x1 < x)
22795 continue;
22796 if (y > y0 && y > y1)
22797 continue;
22798 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
22799 inside = !inside;
22800 }
22801 return inside;
22802 }
22803 }
22804 return 0;
22805 }
22806
22807 Lisp_Object
22808 find_hot_spot (map, x, y)
22809 Lisp_Object map;
22810 int x, y;
22811 {
22812 while (CONSP (map))
22813 {
22814 if (CONSP (XCAR (map))
22815 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
22816 return XCAR (map);
22817 map = XCDR (map);
22818 }
22819
22820 return Qnil;
22821 }
22822
22823 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
22824 3, 3, 0,
22825 doc: /* Lookup in image map MAP coordinates X and Y.
22826 An image map is an alist where each element has the format (AREA ID PLIST).
22827 An AREA is specified as either a rectangle, a circle, or a polygon:
22828 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
22829 pixel coordinates of the upper left and bottom right corners.
22830 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
22831 and the radius of the circle; r may be a float or integer.
22832 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
22833 vector describes one corner in the polygon.
22834 Returns the alist element for the first matching AREA in MAP. */)
22835 (map, x, y)
22836 Lisp_Object map;
22837 Lisp_Object x, y;
22838 {
22839 if (NILP (map))
22840 return Qnil;
22841
22842 CHECK_NUMBER (x);
22843 CHECK_NUMBER (y);
22844
22845 return find_hot_spot (map, XINT (x), XINT (y));
22846 }
22847
22848
22849 /* Display frame CURSOR, optionally using shape defined by POINTER. */
22850 static void
22851 define_frame_cursor1 (f, cursor, pointer)
22852 struct frame *f;
22853 Cursor cursor;
22854 Lisp_Object pointer;
22855 {
22856 /* Do not change cursor shape while dragging mouse. */
22857 if (!NILP (do_mouse_tracking))
22858 return;
22859
22860 if (!NILP (pointer))
22861 {
22862 if (EQ (pointer, Qarrow))
22863 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
22864 else if (EQ (pointer, Qhand))
22865 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
22866 else if (EQ (pointer, Qtext))
22867 cursor = FRAME_X_OUTPUT (f)->text_cursor;
22868 else if (EQ (pointer, intern ("hdrag")))
22869 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
22870 #ifdef HAVE_X_WINDOWS
22871 else if (EQ (pointer, intern ("vdrag")))
22872 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
22873 #endif
22874 else if (EQ (pointer, intern ("hourglass")))
22875 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
22876 else if (EQ (pointer, Qmodeline))
22877 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
22878 else
22879 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
22880 }
22881
22882 if (cursor != No_Cursor)
22883 FRAME_RIF (f)->define_frame_cursor (f, cursor);
22884 }
22885
22886 /* Take proper action when mouse has moved to the mode or header line
22887 or marginal area AREA of window W, x-position X and y-position Y.
22888 X is relative to the start of the text display area of W, so the
22889 width of bitmap areas and scroll bars must be subtracted to get a
22890 position relative to the start of the mode line. */
22891
22892 static void
22893 note_mode_line_or_margin_highlight (window, x, y, area)
22894 Lisp_Object window;
22895 int x, y;
22896 enum window_part area;
22897 {
22898 struct window *w = XWINDOW (window);
22899 struct frame *f = XFRAME (w->frame);
22900 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
22901 Cursor cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
22902 Lisp_Object pointer = Qnil;
22903 int charpos, dx, dy, width, height;
22904 Lisp_Object string, object = Qnil;
22905 Lisp_Object pos, help;
22906
22907 Lisp_Object mouse_face;
22908 int original_x_pixel = x;
22909 struct glyph * glyph = NULL, * row_start_glyph = NULL;
22910 struct glyph_row *row;
22911
22912 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
22913 {
22914 int x0;
22915 struct glyph *end;
22916
22917 string = mode_line_string (w, area, &x, &y, &charpos,
22918 &object, &dx, &dy, &width, &height);
22919
22920 row = (area == ON_MODE_LINE
22921 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
22922 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
22923
22924 /* Find glyph */
22925 if (row->mode_line_p && row->enabled_p)
22926 {
22927 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
22928 end = glyph + row->used[TEXT_AREA];
22929
22930 for (x0 = original_x_pixel;
22931 glyph < end && x0 >= glyph->pixel_width;
22932 ++glyph)
22933 x0 -= glyph->pixel_width;
22934
22935 if (glyph >= end)
22936 glyph = NULL;
22937 }
22938 }
22939 else
22940 {
22941 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
22942 string = marginal_area_string (w, area, &x, &y, &charpos,
22943 &object, &dx, &dy, &width, &height);
22944 }
22945
22946 help = Qnil;
22947
22948 if (IMAGEP (object))
22949 {
22950 Lisp_Object image_map, hotspot;
22951 if ((image_map = Fplist_get (XCDR (object), QCmap),
22952 !NILP (image_map))
22953 && (hotspot = find_hot_spot (image_map, dx, dy),
22954 CONSP (hotspot))
22955 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
22956 {
22957 Lisp_Object area_id, plist;
22958
22959 area_id = XCAR (hotspot);
22960 /* Could check AREA_ID to see if we enter/leave this hot-spot.
22961 If so, we could look for mouse-enter, mouse-leave
22962 properties in PLIST (and do something...). */
22963 hotspot = XCDR (hotspot);
22964 if (CONSP (hotspot)
22965 && (plist = XCAR (hotspot), CONSP (plist)))
22966 {
22967 pointer = Fplist_get (plist, Qpointer);
22968 if (NILP (pointer))
22969 pointer = Qhand;
22970 help = Fplist_get (plist, Qhelp_echo);
22971 if (!NILP (help))
22972 {
22973 help_echo_string = help;
22974 /* Is this correct? ++kfs */
22975 XSETWINDOW (help_echo_window, w);
22976 help_echo_object = w->buffer;
22977 help_echo_pos = charpos;
22978 }
22979 }
22980 }
22981 if (NILP (pointer))
22982 pointer = Fplist_get (XCDR (object), QCpointer);
22983 }
22984
22985 if (STRINGP (string))
22986 {
22987 pos = make_number (charpos);
22988 /* If we're on a string with `help-echo' text property, arrange
22989 for the help to be displayed. This is done by setting the
22990 global variable help_echo_string to the help string. */
22991 if (NILP (help))
22992 {
22993 help = Fget_text_property (pos, Qhelp_echo, string);
22994 if (!NILP (help))
22995 {
22996 help_echo_string = help;
22997 XSETWINDOW (help_echo_window, w);
22998 help_echo_object = string;
22999 help_echo_pos = charpos;
23000 }
23001 }
23002
23003 if (NILP (pointer))
23004 pointer = Fget_text_property (pos, Qpointer, string);
23005
23006 /* Change the mouse pointer according to what is under X/Y. */
23007 if (NILP (pointer) && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
23008 {
23009 Lisp_Object map;
23010 map = Fget_text_property (pos, Qlocal_map, string);
23011 if (!KEYMAPP (map))
23012 map = Fget_text_property (pos, Qkeymap, string);
23013 if (!KEYMAPP (map))
23014 cursor = dpyinfo->vertical_scroll_bar_cursor;
23015 }
23016
23017 /* Change the mouse face according to what is under X/Y. */
23018 mouse_face = Fget_text_property (pos, Qmouse_face, string);
23019 if (!NILP (mouse_face)
23020 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
23021 && glyph)
23022 {
23023 Lisp_Object b, e;
23024
23025 struct glyph * tmp_glyph;
23026
23027 int gpos;
23028 int gseq_length;
23029 int total_pixel_width;
23030 int ignore;
23031
23032 int vpos, hpos;
23033
23034 b = Fprevious_single_property_change (make_number (charpos + 1),
23035 Qmouse_face, string, Qnil);
23036 if (NILP (b))
23037 b = make_number (0);
23038
23039 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
23040 if (NILP (e))
23041 e = make_number (SCHARS (string));
23042
23043 /* Calculate the position(glyph position: GPOS) of GLYPH in
23044 displayed string. GPOS is different from CHARPOS.
23045
23046 CHARPOS is the position of glyph in internal string
23047 object. A mode line string format has structures which
23048 is converted to a flatten by emacs lisp interpreter.
23049 The internal string is an element of the structures.
23050 The displayed string is the flatten string. */
23051 gpos = 0;
23052 if (glyph > row_start_glyph)
23053 {
23054 tmp_glyph = glyph - 1;
23055 while (tmp_glyph >= row_start_glyph
23056 && tmp_glyph->charpos >= XINT (b)
23057 && EQ (tmp_glyph->object, glyph->object))
23058 {
23059 tmp_glyph--;
23060 gpos++;
23061 }
23062 }
23063
23064 /* Calculate the lenght(glyph sequence length: GSEQ_LENGTH) of
23065 displayed string holding GLYPH.
23066
23067 GSEQ_LENGTH is different from SCHARS (STRING).
23068 SCHARS (STRING) returns the length of the internal string. */
23069 for (tmp_glyph = glyph, gseq_length = gpos;
23070 tmp_glyph->charpos < XINT (e);
23071 tmp_glyph++, gseq_length++)
23072 {
23073 if (!EQ (tmp_glyph->object, glyph->object))
23074 break;
23075 }
23076
23077 total_pixel_width = 0;
23078 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
23079 total_pixel_width += tmp_glyph->pixel_width;
23080
23081 /* Pre calculation of re-rendering position */
23082 vpos = (x - gpos);
23083 hpos = (area == ON_MODE_LINE
23084 ? (w->current_matrix)->nrows - 1
23085 : 0);
23086
23087 /* If the re-rendering position is included in the last
23088 re-rendering area, we should do nothing. */
23089 if ( EQ (window, dpyinfo->mouse_face_window)
23090 && dpyinfo->mouse_face_beg_col <= vpos
23091 && vpos < dpyinfo->mouse_face_end_col
23092 && dpyinfo->mouse_face_beg_row == hpos )
23093 return;
23094
23095 if (clear_mouse_face (dpyinfo))
23096 cursor = No_Cursor;
23097
23098 dpyinfo->mouse_face_beg_col = vpos;
23099 dpyinfo->mouse_face_beg_row = hpos;
23100
23101 dpyinfo->mouse_face_beg_x = original_x_pixel - (total_pixel_width + dx);
23102 dpyinfo->mouse_face_beg_y = 0;
23103
23104 dpyinfo->mouse_face_end_col = vpos + gseq_length;
23105 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_beg_row;
23106
23107 dpyinfo->mouse_face_end_x = 0;
23108 dpyinfo->mouse_face_end_y = 0;
23109
23110 dpyinfo->mouse_face_past_end = 0;
23111 dpyinfo->mouse_face_window = window;
23112
23113 dpyinfo->mouse_face_face_id = face_at_string_position (w, string,
23114 charpos,
23115 0, 0, 0, &ignore,
23116 glyph->face_id, 1);
23117 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
23118
23119 if (NILP (pointer))
23120 pointer = Qhand;
23121 }
23122 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
23123 clear_mouse_face (dpyinfo);
23124 }
23125 define_frame_cursor1 (f, cursor, pointer);
23126 }
23127
23128
23129 /* EXPORT:
23130 Take proper action when the mouse has moved to position X, Y on
23131 frame F as regards highlighting characters that have mouse-face
23132 properties. Also de-highlighting chars where the mouse was before.
23133 X and Y can be negative or out of range. */
23134
23135 void
23136 note_mouse_highlight (f, x, y)
23137 struct frame *f;
23138 int x, y;
23139 {
23140 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
23141 enum window_part part;
23142 Lisp_Object window;
23143 struct window *w;
23144 Cursor cursor = No_Cursor;
23145 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
23146 struct buffer *b;
23147
23148 /* When a menu is active, don't highlight because this looks odd. */
23149 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (MAC_OS)
23150 if (popup_activated ())
23151 return;
23152 #endif
23153
23154 if (NILP (Vmouse_highlight)
23155 || !f->glyphs_initialized_p)
23156 return;
23157
23158 dpyinfo->mouse_face_mouse_x = x;
23159 dpyinfo->mouse_face_mouse_y = y;
23160 dpyinfo->mouse_face_mouse_frame = f;
23161
23162 if (dpyinfo->mouse_face_defer)
23163 return;
23164
23165 if (gc_in_progress)
23166 {
23167 dpyinfo->mouse_face_deferred_gc = 1;
23168 return;
23169 }
23170
23171 /* Which window is that in? */
23172 window = window_from_coordinates (f, x, y, &part, 0, 0, 1);
23173
23174 /* If we were displaying active text in another window, clear that.
23175 Also clear if we move out of text area in same window. */
23176 if (! EQ (window, dpyinfo->mouse_face_window)
23177 || (part != ON_TEXT && part != ON_MODE_LINE && part != ON_HEADER_LINE
23178 && !NILP (dpyinfo->mouse_face_window)))
23179 clear_mouse_face (dpyinfo);
23180
23181 /* Not on a window -> return. */
23182 if (!WINDOWP (window))
23183 return;
23184
23185 /* Reset help_echo_string. It will get recomputed below. */
23186 help_echo_string = Qnil;
23187
23188 /* Convert to window-relative pixel coordinates. */
23189 w = XWINDOW (window);
23190 frame_to_window_pixel_xy (w, &x, &y);
23191
23192 /* Handle tool-bar window differently since it doesn't display a
23193 buffer. */
23194 if (EQ (window, f->tool_bar_window))
23195 {
23196 note_tool_bar_highlight (f, x, y);
23197 return;
23198 }
23199
23200 /* Mouse is on the mode, header line or margin? */
23201 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
23202 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
23203 {
23204 note_mode_line_or_margin_highlight (window, x, y, part);
23205 return;
23206 }
23207
23208 if (part == ON_VERTICAL_BORDER)
23209 {
23210 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
23211 help_echo_string = build_string ("drag-mouse-1: resize");
23212 }
23213 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
23214 || part == ON_SCROLL_BAR)
23215 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
23216 else
23217 cursor = FRAME_X_OUTPUT (f)->text_cursor;
23218
23219 /* Are we in a window whose display is up to date?
23220 And verify the buffer's text has not changed. */
23221 b = XBUFFER (w->buffer);
23222 if (part == ON_TEXT
23223 && EQ (w->window_end_valid, w->buffer)
23224 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
23225 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
23226 {
23227 int hpos, vpos, pos, i, dx, dy, area;
23228 struct glyph *glyph;
23229 Lisp_Object object;
23230 Lisp_Object mouse_face = Qnil, overlay = Qnil, position;
23231 Lisp_Object *overlay_vec = NULL;
23232 int noverlays;
23233 struct buffer *obuf;
23234 int obegv, ozv, same_region;
23235
23236 /* Find the glyph under X/Y. */
23237 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
23238
23239 /* Look for :pointer property on image. */
23240 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
23241 {
23242 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
23243 if (img != NULL && IMAGEP (img->spec))
23244 {
23245 Lisp_Object image_map, hotspot;
23246 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
23247 !NILP (image_map))
23248 && (hotspot = find_hot_spot (image_map,
23249 glyph->slice.x + dx,
23250 glyph->slice.y + dy),
23251 CONSP (hotspot))
23252 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
23253 {
23254 Lisp_Object area_id, plist;
23255
23256 area_id = XCAR (hotspot);
23257 /* Could check AREA_ID to see if we enter/leave this hot-spot.
23258 If so, we could look for mouse-enter, mouse-leave
23259 properties in PLIST (and do something...). */
23260 hotspot = XCDR (hotspot);
23261 if (CONSP (hotspot)
23262 && (plist = XCAR (hotspot), CONSP (plist)))
23263 {
23264 pointer = Fplist_get (plist, Qpointer);
23265 if (NILP (pointer))
23266 pointer = Qhand;
23267 help_echo_string = Fplist_get (plist, Qhelp_echo);
23268 if (!NILP (help_echo_string))
23269 {
23270 help_echo_window = window;
23271 help_echo_object = glyph->object;
23272 help_echo_pos = glyph->charpos;
23273 }
23274 }
23275 }
23276 if (NILP (pointer))
23277 pointer = Fplist_get (XCDR (img->spec), QCpointer);
23278 }
23279 }
23280
23281 /* Clear mouse face if X/Y not over text. */
23282 if (glyph == NULL
23283 || area != TEXT_AREA
23284 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p)
23285 {
23286 if (clear_mouse_face (dpyinfo))
23287 cursor = No_Cursor;
23288 if (NILP (pointer))
23289 {
23290 if (area != TEXT_AREA)
23291 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
23292 else
23293 pointer = Vvoid_text_area_pointer;
23294 }
23295 goto set_cursor;
23296 }
23297
23298 pos = glyph->charpos;
23299 object = glyph->object;
23300 if (!STRINGP (object) && !BUFFERP (object))
23301 goto set_cursor;
23302
23303 /* If we get an out-of-range value, return now; avoid an error. */
23304 if (BUFFERP (object) && pos > BUF_Z (b))
23305 goto set_cursor;
23306
23307 /* Make the window's buffer temporarily current for
23308 overlays_at and compute_char_face. */
23309 obuf = current_buffer;
23310 current_buffer = b;
23311 obegv = BEGV;
23312 ozv = ZV;
23313 BEGV = BEG;
23314 ZV = Z;
23315
23316 /* Is this char mouse-active or does it have help-echo? */
23317 position = make_number (pos);
23318
23319 if (BUFFERP (object))
23320 {
23321 /* Put all the overlays we want in a vector in overlay_vec. */
23322 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
23323 /* Sort overlays into increasing priority order. */
23324 noverlays = sort_overlays (overlay_vec, noverlays, w);
23325 }
23326 else
23327 noverlays = 0;
23328
23329 same_region = (EQ (window, dpyinfo->mouse_face_window)
23330 && vpos >= dpyinfo->mouse_face_beg_row
23331 && vpos <= dpyinfo->mouse_face_end_row
23332 && (vpos > dpyinfo->mouse_face_beg_row
23333 || hpos >= dpyinfo->mouse_face_beg_col)
23334 && (vpos < dpyinfo->mouse_face_end_row
23335 || hpos < dpyinfo->mouse_face_end_col
23336 || dpyinfo->mouse_face_past_end));
23337
23338 if (same_region)
23339 cursor = No_Cursor;
23340
23341 /* Check mouse-face highlighting. */
23342 if (! same_region
23343 /* If there exists an overlay with mouse-face overlapping
23344 the one we are currently highlighting, we have to
23345 check if we enter the overlapping overlay, and then
23346 highlight only that. */
23347 || (OVERLAYP (dpyinfo->mouse_face_overlay)
23348 && mouse_face_overlay_overlaps (dpyinfo->mouse_face_overlay)))
23349 {
23350 /* Find the highest priority overlay that has a mouse-face
23351 property. */
23352 overlay = Qnil;
23353 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
23354 {
23355 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
23356 if (!NILP (mouse_face))
23357 overlay = overlay_vec[i];
23358 }
23359
23360 /* If we're actually highlighting the same overlay as
23361 before, there's no need to do that again. */
23362 if (!NILP (overlay)
23363 && EQ (overlay, dpyinfo->mouse_face_overlay))
23364 goto check_help_echo;
23365
23366 dpyinfo->mouse_face_overlay = overlay;
23367
23368 /* Clear the display of the old active region, if any. */
23369 if (clear_mouse_face (dpyinfo))
23370 cursor = No_Cursor;
23371
23372 /* If no overlay applies, get a text property. */
23373 if (NILP (overlay))
23374 mouse_face = Fget_text_property (position, Qmouse_face, object);
23375
23376 /* Handle the overlay case. */
23377 if (!NILP (overlay))
23378 {
23379 /* Find the range of text around this char that
23380 should be active. */
23381 Lisp_Object before, after;
23382 int ignore;
23383
23384 before = Foverlay_start (overlay);
23385 after = Foverlay_end (overlay);
23386 /* Record this as the current active region. */
23387 fast_find_position (w, XFASTINT (before),
23388 &dpyinfo->mouse_face_beg_col,
23389 &dpyinfo->mouse_face_beg_row,
23390 &dpyinfo->mouse_face_beg_x,
23391 &dpyinfo->mouse_face_beg_y, Qnil);
23392
23393 dpyinfo->mouse_face_past_end
23394 = !fast_find_position (w, XFASTINT (after),
23395 &dpyinfo->mouse_face_end_col,
23396 &dpyinfo->mouse_face_end_row,
23397 &dpyinfo->mouse_face_end_x,
23398 &dpyinfo->mouse_face_end_y, Qnil);
23399 dpyinfo->mouse_face_window = window;
23400
23401 dpyinfo->mouse_face_face_id
23402 = face_at_buffer_position (w, pos, 0, 0,
23403 &ignore, pos + 1,
23404 !dpyinfo->mouse_face_hidden);
23405
23406 /* Display it as active. */
23407 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
23408 cursor = No_Cursor;
23409 }
23410 /* Handle the text property case. */
23411 else if (!NILP (mouse_face) && BUFFERP (object))
23412 {
23413 /* Find the range of text around this char that
23414 should be active. */
23415 Lisp_Object before, after, beginning, end;
23416 int ignore;
23417
23418 beginning = Fmarker_position (w->start);
23419 end = make_number (BUF_Z (XBUFFER (object))
23420 - XFASTINT (w->window_end_pos));
23421 before
23422 = Fprevious_single_property_change (make_number (pos + 1),
23423 Qmouse_face,
23424 object, beginning);
23425 after
23426 = Fnext_single_property_change (position, Qmouse_face,
23427 object, end);
23428
23429 /* Record this as the current active region. */
23430 fast_find_position (w, XFASTINT (before),
23431 &dpyinfo->mouse_face_beg_col,
23432 &dpyinfo->mouse_face_beg_row,
23433 &dpyinfo->mouse_face_beg_x,
23434 &dpyinfo->mouse_face_beg_y, Qnil);
23435 dpyinfo->mouse_face_past_end
23436 = !fast_find_position (w, XFASTINT (after),
23437 &dpyinfo->mouse_face_end_col,
23438 &dpyinfo->mouse_face_end_row,
23439 &dpyinfo->mouse_face_end_x,
23440 &dpyinfo->mouse_face_end_y, Qnil);
23441 dpyinfo->mouse_face_window = window;
23442
23443 if (BUFFERP (object))
23444 dpyinfo->mouse_face_face_id
23445 = face_at_buffer_position (w, pos, 0, 0,
23446 &ignore, pos + 1,
23447 !dpyinfo->mouse_face_hidden);
23448
23449 /* Display it as active. */
23450 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
23451 cursor = No_Cursor;
23452 }
23453 else if (!NILP (mouse_face) && STRINGP (object))
23454 {
23455 Lisp_Object b, e;
23456 int ignore;
23457
23458 b = Fprevious_single_property_change (make_number (pos + 1),
23459 Qmouse_face,
23460 object, Qnil);
23461 e = Fnext_single_property_change (position, Qmouse_face,
23462 object, Qnil);
23463 if (NILP (b))
23464 b = make_number (0);
23465 if (NILP (e))
23466 e = make_number (SCHARS (object) - 1);
23467
23468 fast_find_string_pos (w, XINT (b), object,
23469 &dpyinfo->mouse_face_beg_col,
23470 &dpyinfo->mouse_face_beg_row,
23471 &dpyinfo->mouse_face_beg_x,
23472 &dpyinfo->mouse_face_beg_y, 0);
23473 fast_find_string_pos (w, XINT (e), object,
23474 &dpyinfo->mouse_face_end_col,
23475 &dpyinfo->mouse_face_end_row,
23476 &dpyinfo->mouse_face_end_x,
23477 &dpyinfo->mouse_face_end_y, 1);
23478 dpyinfo->mouse_face_past_end = 0;
23479 dpyinfo->mouse_face_window = window;
23480 dpyinfo->mouse_face_face_id
23481 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
23482 glyph->face_id, 1);
23483 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
23484 cursor = No_Cursor;
23485 }
23486 else if (STRINGP (object) && NILP (mouse_face))
23487 {
23488 /* A string which doesn't have mouse-face, but
23489 the text ``under'' it might have. */
23490 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
23491 int start = MATRIX_ROW_START_CHARPOS (r);
23492
23493 pos = string_buffer_position (w, object, start);
23494 if (pos > 0)
23495 mouse_face = get_char_property_and_overlay (make_number (pos),
23496 Qmouse_face,
23497 w->buffer,
23498 &overlay);
23499 if (!NILP (mouse_face) && !NILP (overlay))
23500 {
23501 Lisp_Object before = Foverlay_start (overlay);
23502 Lisp_Object after = Foverlay_end (overlay);
23503 int ignore;
23504
23505 /* Note that we might not be able to find position
23506 BEFORE in the glyph matrix if the overlay is
23507 entirely covered by a `display' property. In
23508 this case, we overshoot. So let's stop in
23509 the glyph matrix before glyphs for OBJECT. */
23510 fast_find_position (w, XFASTINT (before),
23511 &dpyinfo->mouse_face_beg_col,
23512 &dpyinfo->mouse_face_beg_row,
23513 &dpyinfo->mouse_face_beg_x,
23514 &dpyinfo->mouse_face_beg_y,
23515 object);
23516
23517 dpyinfo->mouse_face_past_end
23518 = !fast_find_position (w, XFASTINT (after),
23519 &dpyinfo->mouse_face_end_col,
23520 &dpyinfo->mouse_face_end_row,
23521 &dpyinfo->mouse_face_end_x,
23522 &dpyinfo->mouse_face_end_y,
23523 Qnil);
23524 dpyinfo->mouse_face_window = window;
23525 dpyinfo->mouse_face_face_id
23526 = face_at_buffer_position (w, pos, 0, 0,
23527 &ignore, pos + 1,
23528 !dpyinfo->mouse_face_hidden);
23529
23530 /* Display it as active. */
23531 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
23532 cursor = No_Cursor;
23533 }
23534 }
23535 }
23536
23537 check_help_echo:
23538
23539 /* Look for a `help-echo' property. */
23540 if (NILP (help_echo_string)) {
23541 Lisp_Object help, overlay;
23542
23543 /* Check overlays first. */
23544 help = overlay = Qnil;
23545 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
23546 {
23547 overlay = overlay_vec[i];
23548 help = Foverlay_get (overlay, Qhelp_echo);
23549 }
23550
23551 if (!NILP (help))
23552 {
23553 help_echo_string = help;
23554 help_echo_window = window;
23555 help_echo_object = overlay;
23556 help_echo_pos = pos;
23557 }
23558 else
23559 {
23560 Lisp_Object object = glyph->object;
23561 int charpos = glyph->charpos;
23562
23563 /* Try text properties. */
23564 if (STRINGP (object)
23565 && charpos >= 0
23566 && charpos < SCHARS (object))
23567 {
23568 help = Fget_text_property (make_number (charpos),
23569 Qhelp_echo, object);
23570 if (NILP (help))
23571 {
23572 /* If the string itself doesn't specify a help-echo,
23573 see if the buffer text ``under'' it does. */
23574 struct glyph_row *r
23575 = MATRIX_ROW (w->current_matrix, vpos);
23576 int start = MATRIX_ROW_START_CHARPOS (r);
23577 int pos = string_buffer_position (w, object, start);
23578 if (pos > 0)
23579 {
23580 help = Fget_char_property (make_number (pos),
23581 Qhelp_echo, w->buffer);
23582 if (!NILP (help))
23583 {
23584 charpos = pos;
23585 object = w->buffer;
23586 }
23587 }
23588 }
23589 }
23590 else if (BUFFERP (object)
23591 && charpos >= BEGV
23592 && charpos < ZV)
23593 help = Fget_text_property (make_number (charpos), Qhelp_echo,
23594 object);
23595
23596 if (!NILP (help))
23597 {
23598 help_echo_string = help;
23599 help_echo_window = window;
23600 help_echo_object = object;
23601 help_echo_pos = charpos;
23602 }
23603 }
23604 }
23605
23606 /* Look for a `pointer' property. */
23607 if (NILP (pointer))
23608 {
23609 /* Check overlays first. */
23610 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
23611 pointer = Foverlay_get (overlay_vec[i], Qpointer);
23612
23613 if (NILP (pointer))
23614 {
23615 Lisp_Object object = glyph->object;
23616 int charpos = glyph->charpos;
23617
23618 /* Try text properties. */
23619 if (STRINGP (object)
23620 && charpos >= 0
23621 && charpos < SCHARS (object))
23622 {
23623 pointer = Fget_text_property (make_number (charpos),
23624 Qpointer, object);
23625 if (NILP (pointer))
23626 {
23627 /* If the string itself doesn't specify a pointer,
23628 see if the buffer text ``under'' it does. */
23629 struct glyph_row *r
23630 = MATRIX_ROW (w->current_matrix, vpos);
23631 int start = MATRIX_ROW_START_CHARPOS (r);
23632 int pos = string_buffer_position (w, object, start);
23633 if (pos > 0)
23634 pointer = Fget_char_property (make_number (pos),
23635 Qpointer, w->buffer);
23636 }
23637 }
23638 else if (BUFFERP (object)
23639 && charpos >= BEGV
23640 && charpos < ZV)
23641 pointer = Fget_text_property (make_number (charpos),
23642 Qpointer, object);
23643 }
23644 }
23645
23646 BEGV = obegv;
23647 ZV = ozv;
23648 current_buffer = obuf;
23649 }
23650
23651 set_cursor:
23652
23653 define_frame_cursor1 (f, cursor, pointer);
23654 }
23655
23656
23657 /* EXPORT for RIF:
23658 Clear any mouse-face on window W. This function is part of the
23659 redisplay interface, and is called from try_window_id and similar
23660 functions to ensure the mouse-highlight is off. */
23661
23662 void
23663 x_clear_window_mouse_face (w)
23664 struct window *w;
23665 {
23666 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
23667 Lisp_Object window;
23668
23669 BLOCK_INPUT;
23670 XSETWINDOW (window, w);
23671 if (EQ (window, dpyinfo->mouse_face_window))
23672 clear_mouse_face (dpyinfo);
23673 UNBLOCK_INPUT;
23674 }
23675
23676
23677 /* EXPORT:
23678 Just discard the mouse face information for frame F, if any.
23679 This is used when the size of F is changed. */
23680
23681 void
23682 cancel_mouse_face (f)
23683 struct frame *f;
23684 {
23685 Lisp_Object window;
23686 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
23687
23688 window = dpyinfo->mouse_face_window;
23689 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
23690 {
23691 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
23692 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
23693 dpyinfo->mouse_face_window = Qnil;
23694 }
23695 }
23696
23697
23698 #endif /* HAVE_WINDOW_SYSTEM */
23699
23700 \f
23701 /***********************************************************************
23702 Exposure Events
23703 ***********************************************************************/
23704
23705 #ifdef HAVE_WINDOW_SYSTEM
23706
23707 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
23708 which intersects rectangle R. R is in window-relative coordinates. */
23709
23710 static void
23711 expose_area (w, row, r, area)
23712 struct window *w;
23713 struct glyph_row *row;
23714 XRectangle *r;
23715 enum glyph_row_area area;
23716 {
23717 struct glyph *first = row->glyphs[area];
23718 struct glyph *end = row->glyphs[area] + row->used[area];
23719 struct glyph *last;
23720 int first_x, start_x, x;
23721
23722 if (area == TEXT_AREA && row->fill_line_p)
23723 /* If row extends face to end of line write the whole line. */
23724 draw_glyphs (w, 0, row, area,
23725 0, row->used[area],
23726 DRAW_NORMAL_TEXT, 0);
23727 else
23728 {
23729 /* Set START_X to the window-relative start position for drawing glyphs of
23730 AREA. The first glyph of the text area can be partially visible.
23731 The first glyphs of other areas cannot. */
23732 start_x = window_box_left_offset (w, area);
23733 x = start_x;
23734 if (area == TEXT_AREA)
23735 x += row->x;
23736
23737 /* Find the first glyph that must be redrawn. */
23738 while (first < end
23739 && x + first->pixel_width < r->x)
23740 {
23741 x += first->pixel_width;
23742 ++first;
23743 }
23744
23745 /* Find the last one. */
23746 last = first;
23747 first_x = x;
23748 while (last < end
23749 && x < r->x + r->width)
23750 {
23751 x += last->pixel_width;
23752 ++last;
23753 }
23754
23755 /* Repaint. */
23756 if (last > first)
23757 draw_glyphs (w, first_x - start_x, row, area,
23758 first - row->glyphs[area], last - row->glyphs[area],
23759 DRAW_NORMAL_TEXT, 0);
23760 }
23761 }
23762
23763
23764 /* Redraw the parts of the glyph row ROW on window W intersecting
23765 rectangle R. R is in window-relative coordinates. Value is
23766 non-zero if mouse-face was overwritten. */
23767
23768 static int
23769 expose_line (w, row, r)
23770 struct window *w;
23771 struct glyph_row *row;
23772 XRectangle *r;
23773 {
23774 xassert (row->enabled_p);
23775
23776 if (row->mode_line_p || w->pseudo_window_p)
23777 draw_glyphs (w, 0, row, TEXT_AREA,
23778 0, row->used[TEXT_AREA],
23779 DRAW_NORMAL_TEXT, 0);
23780 else
23781 {
23782 if (row->used[LEFT_MARGIN_AREA])
23783 expose_area (w, row, r, LEFT_MARGIN_AREA);
23784 if (row->used[TEXT_AREA])
23785 expose_area (w, row, r, TEXT_AREA);
23786 if (row->used[RIGHT_MARGIN_AREA])
23787 expose_area (w, row, r, RIGHT_MARGIN_AREA);
23788 draw_row_fringe_bitmaps (w, row);
23789 }
23790
23791 return row->mouse_face_p;
23792 }
23793
23794
23795 /* Redraw those parts of glyphs rows during expose event handling that
23796 overlap other rows. Redrawing of an exposed line writes over parts
23797 of lines overlapping that exposed line; this function fixes that.
23798
23799 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
23800 row in W's current matrix that is exposed and overlaps other rows.
23801 LAST_OVERLAPPING_ROW is the last such row. */
23802
23803 static void
23804 expose_overlaps (w, first_overlapping_row, last_overlapping_row, r)
23805 struct window *w;
23806 struct glyph_row *first_overlapping_row;
23807 struct glyph_row *last_overlapping_row;
23808 XRectangle *r;
23809 {
23810 struct glyph_row *row;
23811
23812 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
23813 if (row->overlapping_p)
23814 {
23815 xassert (row->enabled_p && !row->mode_line_p);
23816
23817 row->clip = r;
23818 if (row->used[LEFT_MARGIN_AREA])
23819 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
23820
23821 if (row->used[TEXT_AREA])
23822 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
23823
23824 if (row->used[RIGHT_MARGIN_AREA])
23825 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
23826 row->clip = NULL;
23827 }
23828 }
23829
23830
23831 /* Return non-zero if W's cursor intersects rectangle R. */
23832
23833 static int
23834 phys_cursor_in_rect_p (w, r)
23835 struct window *w;
23836 XRectangle *r;
23837 {
23838 XRectangle cr, result;
23839 struct glyph *cursor_glyph;
23840 struct glyph_row *row;
23841
23842 if (w->phys_cursor.vpos >= 0
23843 && w->phys_cursor.vpos < w->current_matrix->nrows
23844 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
23845 row->enabled_p)
23846 && row->cursor_in_fringe_p)
23847 {
23848 /* Cursor is in the fringe. */
23849 cr.x = window_box_right_offset (w,
23850 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
23851 ? RIGHT_MARGIN_AREA
23852 : TEXT_AREA));
23853 cr.y = row->y;
23854 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
23855 cr.height = row->height;
23856 return x_intersect_rectangles (&cr, r, &result);
23857 }
23858
23859 cursor_glyph = get_phys_cursor_glyph (w);
23860 if (cursor_glyph)
23861 {
23862 /* r is relative to W's box, but w->phys_cursor.x is relative
23863 to left edge of W's TEXT area. Adjust it. */
23864 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
23865 cr.y = w->phys_cursor.y;
23866 cr.width = cursor_glyph->pixel_width;
23867 cr.height = w->phys_cursor_height;
23868 /* ++KFS: W32 version used W32-specific IntersectRect here, but
23869 I assume the effect is the same -- and this is portable. */
23870 return x_intersect_rectangles (&cr, r, &result);
23871 }
23872 /* If we don't understand the format, pretend we're not in the hot-spot. */
23873 return 0;
23874 }
23875
23876
23877 /* EXPORT:
23878 Draw a vertical window border to the right of window W if W doesn't
23879 have vertical scroll bars. */
23880
23881 void
23882 x_draw_vertical_border (w)
23883 struct window *w;
23884 {
23885 struct frame *f = XFRAME (WINDOW_FRAME (w));
23886
23887 /* We could do better, if we knew what type of scroll-bar the adjacent
23888 windows (on either side) have... But we don't :-(
23889 However, I think this works ok. ++KFS 2003-04-25 */
23890
23891 /* Redraw borders between horizontally adjacent windows. Don't
23892 do it for frames with vertical scroll bars because either the
23893 right scroll bar of a window, or the left scroll bar of its
23894 neighbor will suffice as a border. */
23895 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
23896 return;
23897
23898 if (!WINDOW_RIGHTMOST_P (w)
23899 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
23900 {
23901 int x0, x1, y0, y1;
23902
23903 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
23904 y1 -= 1;
23905
23906 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
23907 x1 -= 1;
23908
23909 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
23910 }
23911 else if (!WINDOW_LEFTMOST_P (w)
23912 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
23913 {
23914 int x0, x1, y0, y1;
23915
23916 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
23917 y1 -= 1;
23918
23919 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
23920 x0 -= 1;
23921
23922 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
23923 }
23924 }
23925
23926
23927 /* Redraw the part of window W intersection rectangle FR. Pixel
23928 coordinates in FR are frame-relative. Call this function with
23929 input blocked. Value is non-zero if the exposure overwrites
23930 mouse-face. */
23931
23932 static int
23933 expose_window (w, fr)
23934 struct window *w;
23935 XRectangle *fr;
23936 {
23937 struct frame *f = XFRAME (w->frame);
23938 XRectangle wr, r;
23939 int mouse_face_overwritten_p = 0;
23940
23941 /* If window is not yet fully initialized, do nothing. This can
23942 happen when toolkit scroll bars are used and a window is split.
23943 Reconfiguring the scroll bar will generate an expose for a newly
23944 created window. */
23945 if (w->current_matrix == NULL)
23946 return 0;
23947
23948 /* When we're currently updating the window, display and current
23949 matrix usually don't agree. Arrange for a thorough display
23950 later. */
23951 if (w == updated_window)
23952 {
23953 SET_FRAME_GARBAGED (f);
23954 return 0;
23955 }
23956
23957 /* Frame-relative pixel rectangle of W. */
23958 wr.x = WINDOW_LEFT_EDGE_X (w);
23959 wr.y = WINDOW_TOP_EDGE_Y (w);
23960 wr.width = WINDOW_TOTAL_WIDTH (w);
23961 wr.height = WINDOW_TOTAL_HEIGHT (w);
23962
23963 if (x_intersect_rectangles (fr, &wr, &r))
23964 {
23965 int yb = window_text_bottom_y (w);
23966 struct glyph_row *row;
23967 int cursor_cleared_p;
23968 struct glyph_row *first_overlapping_row, *last_overlapping_row;
23969
23970 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
23971 r.x, r.y, r.width, r.height));
23972
23973 /* Convert to window coordinates. */
23974 r.x -= WINDOW_LEFT_EDGE_X (w);
23975 r.y -= WINDOW_TOP_EDGE_Y (w);
23976
23977 /* Turn off the cursor. */
23978 if (!w->pseudo_window_p
23979 && phys_cursor_in_rect_p (w, &r))
23980 {
23981 x_clear_cursor (w);
23982 cursor_cleared_p = 1;
23983 }
23984 else
23985 cursor_cleared_p = 0;
23986
23987 /* Update lines intersecting rectangle R. */
23988 first_overlapping_row = last_overlapping_row = NULL;
23989 for (row = w->current_matrix->rows;
23990 row->enabled_p;
23991 ++row)
23992 {
23993 int y0 = row->y;
23994 int y1 = MATRIX_ROW_BOTTOM_Y (row);
23995
23996 if ((y0 >= r.y && y0 < r.y + r.height)
23997 || (y1 > r.y && y1 < r.y + r.height)
23998 || (r.y >= y0 && r.y < y1)
23999 || (r.y + r.height > y0 && r.y + r.height < y1))
24000 {
24001 /* A header line may be overlapping, but there is no need
24002 to fix overlapping areas for them. KFS 2005-02-12 */
24003 if (row->overlapping_p && !row->mode_line_p)
24004 {
24005 if (first_overlapping_row == NULL)
24006 first_overlapping_row = row;
24007 last_overlapping_row = row;
24008 }
24009
24010 row->clip = fr;
24011 if (expose_line (w, row, &r))
24012 mouse_face_overwritten_p = 1;
24013 row->clip = NULL;
24014 }
24015 else if (row->overlapping_p)
24016 {
24017 /* We must redraw a row overlapping the exposed area. */
24018 if (y0 < r.y
24019 ? y0 + row->phys_height > r.y
24020 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
24021 {
24022 if (first_overlapping_row == NULL)
24023 first_overlapping_row = row;
24024 last_overlapping_row = row;
24025 }
24026 }
24027
24028 if (y1 >= yb)
24029 break;
24030 }
24031
24032 /* Display the mode line if there is one. */
24033 if (WINDOW_WANTS_MODELINE_P (w)
24034 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
24035 row->enabled_p)
24036 && row->y < r.y + r.height)
24037 {
24038 if (expose_line (w, row, &r))
24039 mouse_face_overwritten_p = 1;
24040 }
24041
24042 if (!w->pseudo_window_p)
24043 {
24044 /* Fix the display of overlapping rows. */
24045 if (first_overlapping_row)
24046 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
24047 fr);
24048
24049 /* Draw border between windows. */
24050 x_draw_vertical_border (w);
24051
24052 /* Turn the cursor on again. */
24053 if (cursor_cleared_p)
24054 update_window_cursor (w, 1);
24055 }
24056 }
24057
24058 return mouse_face_overwritten_p;
24059 }
24060
24061
24062
24063 /* Redraw (parts) of all windows in the window tree rooted at W that
24064 intersect R. R contains frame pixel coordinates. Value is
24065 non-zero if the exposure overwrites mouse-face. */
24066
24067 static int
24068 expose_window_tree (w, r)
24069 struct window *w;
24070 XRectangle *r;
24071 {
24072 struct frame *f = XFRAME (w->frame);
24073 int mouse_face_overwritten_p = 0;
24074
24075 while (w && !FRAME_GARBAGED_P (f))
24076 {
24077 if (!NILP (w->hchild))
24078 mouse_face_overwritten_p
24079 |= expose_window_tree (XWINDOW (w->hchild), r);
24080 else if (!NILP (w->vchild))
24081 mouse_face_overwritten_p
24082 |= expose_window_tree (XWINDOW (w->vchild), r);
24083 else
24084 mouse_face_overwritten_p |= expose_window (w, r);
24085
24086 w = NILP (w->next) ? NULL : XWINDOW (w->next);
24087 }
24088
24089 return mouse_face_overwritten_p;
24090 }
24091
24092
24093 /* EXPORT:
24094 Redisplay an exposed area of frame F. X and Y are the upper-left
24095 corner of the exposed rectangle. W and H are width and height of
24096 the exposed area. All are pixel values. W or H zero means redraw
24097 the entire frame. */
24098
24099 void
24100 expose_frame (f, x, y, w, h)
24101 struct frame *f;
24102 int x, y, w, h;
24103 {
24104 XRectangle r;
24105 int mouse_face_overwritten_p = 0;
24106
24107 TRACE ((stderr, "expose_frame "));
24108
24109 /* No need to redraw if frame will be redrawn soon. */
24110 if (FRAME_GARBAGED_P (f))
24111 {
24112 TRACE ((stderr, " garbaged\n"));
24113 return;
24114 }
24115
24116 /* If basic faces haven't been realized yet, there is no point in
24117 trying to redraw anything. This can happen when we get an expose
24118 event while Emacs is starting, e.g. by moving another window. */
24119 if (FRAME_FACE_CACHE (f) == NULL
24120 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
24121 {
24122 TRACE ((stderr, " no faces\n"));
24123 return;
24124 }
24125
24126 if (w == 0 || h == 0)
24127 {
24128 r.x = r.y = 0;
24129 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
24130 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
24131 }
24132 else
24133 {
24134 r.x = x;
24135 r.y = y;
24136 r.width = w;
24137 r.height = h;
24138 }
24139
24140 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
24141 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
24142
24143 if (WINDOWP (f->tool_bar_window))
24144 mouse_face_overwritten_p
24145 |= expose_window (XWINDOW (f->tool_bar_window), &r);
24146
24147 #ifdef HAVE_X_WINDOWS
24148 #ifndef MSDOS
24149 #ifndef USE_X_TOOLKIT
24150 if (WINDOWP (f->menu_bar_window))
24151 mouse_face_overwritten_p
24152 |= expose_window (XWINDOW (f->menu_bar_window), &r);
24153 #endif /* not USE_X_TOOLKIT */
24154 #endif
24155 #endif
24156
24157 /* Some window managers support a focus-follows-mouse style with
24158 delayed raising of frames. Imagine a partially obscured frame,
24159 and moving the mouse into partially obscured mouse-face on that
24160 frame. The visible part of the mouse-face will be highlighted,
24161 then the WM raises the obscured frame. With at least one WM, KDE
24162 2.1, Emacs is not getting any event for the raising of the frame
24163 (even tried with SubstructureRedirectMask), only Expose events.
24164 These expose events will draw text normally, i.e. not
24165 highlighted. Which means we must redo the highlight here.
24166 Subsume it under ``we love X''. --gerd 2001-08-15 */
24167 /* Included in Windows version because Windows most likely does not
24168 do the right thing if any third party tool offers
24169 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
24170 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
24171 {
24172 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
24173 if (f == dpyinfo->mouse_face_mouse_frame)
24174 {
24175 int x = dpyinfo->mouse_face_mouse_x;
24176 int y = dpyinfo->mouse_face_mouse_y;
24177 clear_mouse_face (dpyinfo);
24178 note_mouse_highlight (f, x, y);
24179 }
24180 }
24181 }
24182
24183
24184 /* EXPORT:
24185 Determine the intersection of two rectangles R1 and R2. Return
24186 the intersection in *RESULT. Value is non-zero if RESULT is not
24187 empty. */
24188
24189 int
24190 x_intersect_rectangles (r1, r2, result)
24191 XRectangle *r1, *r2, *result;
24192 {
24193 XRectangle *left, *right;
24194 XRectangle *upper, *lower;
24195 int intersection_p = 0;
24196
24197 /* Rearrange so that R1 is the left-most rectangle. */
24198 if (r1->x < r2->x)
24199 left = r1, right = r2;
24200 else
24201 left = r2, right = r1;
24202
24203 /* X0 of the intersection is right.x0, if this is inside R1,
24204 otherwise there is no intersection. */
24205 if (right->x <= left->x + left->width)
24206 {
24207 result->x = right->x;
24208
24209 /* The right end of the intersection is the minimum of the
24210 the right ends of left and right. */
24211 result->width = (min (left->x + left->width, right->x + right->width)
24212 - result->x);
24213
24214 /* Same game for Y. */
24215 if (r1->y < r2->y)
24216 upper = r1, lower = r2;
24217 else
24218 upper = r2, lower = r1;
24219
24220 /* The upper end of the intersection is lower.y0, if this is inside
24221 of upper. Otherwise, there is no intersection. */
24222 if (lower->y <= upper->y + upper->height)
24223 {
24224 result->y = lower->y;
24225
24226 /* The lower end of the intersection is the minimum of the lower
24227 ends of upper and lower. */
24228 result->height = (min (lower->y + lower->height,
24229 upper->y + upper->height)
24230 - result->y);
24231 intersection_p = 1;
24232 }
24233 }
24234
24235 return intersection_p;
24236 }
24237
24238 #endif /* HAVE_WINDOW_SYSTEM */
24239
24240 \f
24241 /***********************************************************************
24242 Initialization
24243 ***********************************************************************/
24244
24245 void
24246 syms_of_xdisp ()
24247 {
24248 Vwith_echo_area_save_vector = Qnil;
24249 staticpro (&Vwith_echo_area_save_vector);
24250
24251 Vmessage_stack = Qnil;
24252 staticpro (&Vmessage_stack);
24253
24254 Qinhibit_redisplay = intern ("inhibit-redisplay");
24255 staticpro (&Qinhibit_redisplay);
24256
24257 message_dolog_marker1 = Fmake_marker ();
24258 staticpro (&message_dolog_marker1);
24259 message_dolog_marker2 = Fmake_marker ();
24260 staticpro (&message_dolog_marker2);
24261 message_dolog_marker3 = Fmake_marker ();
24262 staticpro (&message_dolog_marker3);
24263
24264 #if GLYPH_DEBUG
24265 defsubr (&Sdump_frame_glyph_matrix);
24266 defsubr (&Sdump_glyph_matrix);
24267 defsubr (&Sdump_glyph_row);
24268 defsubr (&Sdump_tool_bar_row);
24269 defsubr (&Strace_redisplay);
24270 defsubr (&Strace_to_stderr);
24271 #endif
24272 #ifdef HAVE_WINDOW_SYSTEM
24273 defsubr (&Stool_bar_lines_needed);
24274 defsubr (&Slookup_image_map);
24275 #endif
24276 defsubr (&Sformat_mode_line);
24277 defsubr (&Sinvisible_p);
24278
24279 staticpro (&Qmenu_bar_update_hook);
24280 Qmenu_bar_update_hook = intern ("menu-bar-update-hook");
24281
24282 staticpro (&Qoverriding_terminal_local_map);
24283 Qoverriding_terminal_local_map = intern ("overriding-terminal-local-map");
24284
24285 staticpro (&Qoverriding_local_map);
24286 Qoverriding_local_map = intern ("overriding-local-map");
24287
24288 staticpro (&Qwindow_scroll_functions);
24289 Qwindow_scroll_functions = intern ("window-scroll-functions");
24290
24291 staticpro (&Qredisplay_end_trigger_functions);
24292 Qredisplay_end_trigger_functions = intern ("redisplay-end-trigger-functions");
24293
24294 staticpro (&Qinhibit_point_motion_hooks);
24295 Qinhibit_point_motion_hooks = intern ("inhibit-point-motion-hooks");
24296
24297 QCdata = intern (":data");
24298 staticpro (&QCdata);
24299 Qdisplay = intern ("display");
24300 staticpro (&Qdisplay);
24301 Qspace_width = intern ("space-width");
24302 staticpro (&Qspace_width);
24303 Qraise = intern ("raise");
24304 staticpro (&Qraise);
24305 Qslice = intern ("slice");
24306 staticpro (&Qslice);
24307 Qspace = intern ("space");
24308 staticpro (&Qspace);
24309 Qmargin = intern ("margin");
24310 staticpro (&Qmargin);
24311 Qpointer = intern ("pointer");
24312 staticpro (&Qpointer);
24313 Qleft_margin = intern ("left-margin");
24314 staticpro (&Qleft_margin);
24315 Qright_margin = intern ("right-margin");
24316 staticpro (&Qright_margin);
24317 Qcenter = intern ("center");
24318 staticpro (&Qcenter);
24319 Qline_height = intern ("line-height");
24320 staticpro (&Qline_height);
24321 QCalign_to = intern (":align-to");
24322 staticpro (&QCalign_to);
24323 QCrelative_width = intern (":relative-width");
24324 staticpro (&QCrelative_width);
24325 QCrelative_height = intern (":relative-height");
24326 staticpro (&QCrelative_height);
24327 QCeval = intern (":eval");
24328 staticpro (&QCeval);
24329 QCpropertize = intern (":propertize");
24330 staticpro (&QCpropertize);
24331 QCfile = intern (":file");
24332 staticpro (&QCfile);
24333 Qfontified = intern ("fontified");
24334 staticpro (&Qfontified);
24335 Qfontification_functions = intern ("fontification-functions");
24336 staticpro (&Qfontification_functions);
24337 Qtrailing_whitespace = intern ("trailing-whitespace");
24338 staticpro (&Qtrailing_whitespace);
24339 Qescape_glyph = intern ("escape-glyph");
24340 staticpro (&Qescape_glyph);
24341 Qnobreak_space = intern ("nobreak-space");
24342 staticpro (&Qnobreak_space);
24343 Qimage = intern ("image");
24344 staticpro (&Qimage);
24345 QCmap = intern (":map");
24346 staticpro (&QCmap);
24347 QCpointer = intern (":pointer");
24348 staticpro (&QCpointer);
24349 Qrect = intern ("rect");
24350 staticpro (&Qrect);
24351 Qcircle = intern ("circle");
24352 staticpro (&Qcircle);
24353 Qpoly = intern ("poly");
24354 staticpro (&Qpoly);
24355 Qmessage_truncate_lines = intern ("message-truncate-lines");
24356 staticpro (&Qmessage_truncate_lines);
24357 Qgrow_only = intern ("grow-only");
24358 staticpro (&Qgrow_only);
24359 Qinhibit_menubar_update = intern ("inhibit-menubar-update");
24360 staticpro (&Qinhibit_menubar_update);
24361 Qinhibit_eval_during_redisplay = intern ("inhibit-eval-during-redisplay");
24362 staticpro (&Qinhibit_eval_during_redisplay);
24363 Qposition = intern ("position");
24364 staticpro (&Qposition);
24365 Qbuffer_position = intern ("buffer-position");
24366 staticpro (&Qbuffer_position);
24367 Qobject = intern ("object");
24368 staticpro (&Qobject);
24369 Qbar = intern ("bar");
24370 staticpro (&Qbar);
24371 Qhbar = intern ("hbar");
24372 staticpro (&Qhbar);
24373 Qbox = intern ("box");
24374 staticpro (&Qbox);
24375 Qhollow = intern ("hollow");
24376 staticpro (&Qhollow);
24377 Qhand = intern ("hand");
24378 staticpro (&Qhand);
24379 Qarrow = intern ("arrow");
24380 staticpro (&Qarrow);
24381 Qtext = intern ("text");
24382 staticpro (&Qtext);
24383 Qrisky_local_variable = intern ("risky-local-variable");
24384 staticpro (&Qrisky_local_variable);
24385 Qinhibit_free_realized_faces = intern ("inhibit-free-realized-faces");
24386 staticpro (&Qinhibit_free_realized_faces);
24387
24388 list_of_error = Fcons (Fcons (intern ("error"),
24389 Fcons (intern ("void-variable"), Qnil)),
24390 Qnil);
24391 staticpro (&list_of_error);
24392
24393 Qlast_arrow_position = intern ("last-arrow-position");
24394 staticpro (&Qlast_arrow_position);
24395 Qlast_arrow_string = intern ("last-arrow-string");
24396 staticpro (&Qlast_arrow_string);
24397
24398 Qoverlay_arrow_string = intern ("overlay-arrow-string");
24399 staticpro (&Qoverlay_arrow_string);
24400 Qoverlay_arrow_bitmap = intern ("overlay-arrow-bitmap");
24401 staticpro (&Qoverlay_arrow_bitmap);
24402
24403 echo_buffer[0] = echo_buffer[1] = Qnil;
24404 staticpro (&echo_buffer[0]);
24405 staticpro (&echo_buffer[1]);
24406
24407 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
24408 staticpro (&echo_area_buffer[0]);
24409 staticpro (&echo_area_buffer[1]);
24410
24411 Vmessages_buffer_name = build_string ("*Messages*");
24412 staticpro (&Vmessages_buffer_name);
24413
24414 mode_line_proptrans_alist = Qnil;
24415 staticpro (&mode_line_proptrans_alist);
24416 mode_line_string_list = Qnil;
24417 staticpro (&mode_line_string_list);
24418 mode_line_string_face = Qnil;
24419 staticpro (&mode_line_string_face);
24420 mode_line_string_face_prop = Qnil;
24421 staticpro (&mode_line_string_face_prop);
24422 Vmode_line_unwind_vector = Qnil;
24423 staticpro (&Vmode_line_unwind_vector);
24424
24425 help_echo_string = Qnil;
24426 staticpro (&help_echo_string);
24427 help_echo_object = Qnil;
24428 staticpro (&help_echo_object);
24429 help_echo_window = Qnil;
24430 staticpro (&help_echo_window);
24431 previous_help_echo_string = Qnil;
24432 staticpro (&previous_help_echo_string);
24433 help_echo_pos = -1;
24434
24435 #ifdef HAVE_WINDOW_SYSTEM
24436 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
24437 doc: /* *Non-nil means draw block cursor as wide as the glyph under it.
24438 For example, if a block cursor is over a tab, it will be drawn as
24439 wide as that tab on the display. */);
24440 x_stretch_cursor_p = 0;
24441 #endif
24442
24443 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
24444 doc: /* *Non-nil means highlight trailing whitespace.
24445 The face used for trailing whitespace is `trailing-whitespace'. */);
24446 Vshow_trailing_whitespace = Qnil;
24447
24448 DEFVAR_LISP ("nobreak-char-display", &Vnobreak_char_display,
24449 doc: /* *Control highlighting of nobreak space and soft hyphen.
24450 A value of t means highlight the character itself (for nobreak space,
24451 use face `nobreak-space').
24452 A value of nil means no highlighting.
24453 Other values mean display the escape glyph followed by an ordinary
24454 space or ordinary hyphen. */);
24455 Vnobreak_char_display = Qt;
24456
24457 DEFVAR_LISP ("void-text-area-pointer", &Vvoid_text_area_pointer,
24458 doc: /* *The pointer shape to show in void text areas.
24459 A value of nil means to show the text pointer. Other options are `arrow',
24460 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
24461 Vvoid_text_area_pointer = Qarrow;
24462
24463 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
24464 doc: /* Non-nil means don't actually do any redisplay.
24465 This is used for internal purposes. */);
24466 Vinhibit_redisplay = Qnil;
24467
24468 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
24469 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
24470 Vglobal_mode_string = Qnil;
24471
24472 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
24473 doc: /* Marker for where to display an arrow on top of the buffer text.
24474 This must be the beginning of a line in order to work.
24475 See also `overlay-arrow-string'. */);
24476 Voverlay_arrow_position = Qnil;
24477
24478 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
24479 doc: /* String to display as an arrow in non-window frames.
24480 See also `overlay-arrow-position'. */);
24481 Voverlay_arrow_string = build_string ("=>");
24482
24483 DEFVAR_LISP ("overlay-arrow-variable-list", &Voverlay_arrow_variable_list,
24484 doc: /* List of variables (symbols) which hold markers for overlay arrows.
24485 The symbols on this list are examined during redisplay to determine
24486 where to display overlay arrows. */);
24487 Voverlay_arrow_variable_list
24488 = Fcons (intern ("overlay-arrow-position"), Qnil);
24489
24490 DEFVAR_INT ("scroll-step", &scroll_step,
24491 doc: /* *The number of lines to try scrolling a window by when point moves out.
24492 If that fails to bring point back on frame, point is centered instead.
24493 If this is zero, point is always centered after it moves off frame.
24494 If you want scrolling to always be a line at a time, you should set
24495 `scroll-conservatively' to a large value rather than set this to 1. */);
24496
24497 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
24498 doc: /* *Scroll up to this many lines, to bring point back on screen.
24499 If point moves off-screen, redisplay will scroll by up to
24500 `scroll-conservatively' lines in order to bring point just barely
24501 onto the screen again. If that cannot be done, then redisplay
24502 recenters point as usual.
24503
24504 A value of zero means always recenter point if it moves off screen. */);
24505 scroll_conservatively = 0;
24506
24507 DEFVAR_INT ("scroll-margin", &scroll_margin,
24508 doc: /* *Number of lines of margin at the top and bottom of a window.
24509 Recenter the window whenever point gets within this many lines
24510 of the top or bottom of the window. */);
24511 scroll_margin = 0;
24512
24513 DEFVAR_LISP ("display-pixels-per-inch", &Vdisplay_pixels_per_inch,
24514 doc: /* Pixels per inch value for non-window system displays.
24515 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
24516 Vdisplay_pixels_per_inch = make_float (72.0);
24517
24518 #if GLYPH_DEBUG
24519 DEFVAR_INT ("debug-end-pos", &debug_end_pos, doc: /* Don't ask. */);
24520 #endif
24521
24522 DEFVAR_BOOL ("truncate-partial-width-windows",
24523 &truncate_partial_width_windows,
24524 doc: /* *Non-nil means truncate lines in all windows less than full frame wide. */);
24525 truncate_partial_width_windows = 1;
24526
24527 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
24528 doc: /* When nil, display the mode-line/header-line/menu-bar in the default face.
24529 Any other value means to use the appropriate face, `mode-line',
24530 `header-line', or `menu' respectively. */);
24531 mode_line_inverse_video = 1;
24532
24533 DEFVAR_LISP ("line-number-display-limit", &Vline_number_display_limit,
24534 doc: /* *Maximum buffer size for which line number should be displayed.
24535 If the buffer is bigger than this, the line number does not appear
24536 in the mode line. A value of nil means no limit. */);
24537 Vline_number_display_limit = Qnil;
24538
24539 DEFVAR_INT ("line-number-display-limit-width",
24540 &line_number_display_limit_width,
24541 doc: /* *Maximum line width (in characters) for line number display.
24542 If the average length of the lines near point is bigger than this, then the
24543 line number may be omitted from the mode line. */);
24544 line_number_display_limit_width = 200;
24545
24546 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
24547 doc: /* *Non-nil means highlight region even in nonselected windows. */);
24548 highlight_nonselected_windows = 0;
24549
24550 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
24551 doc: /* Non-nil if more than one frame is visible on this display.
24552 Minibuffer-only frames don't count, but iconified frames do.
24553 This variable is not guaranteed to be accurate except while processing
24554 `frame-title-format' and `icon-title-format'. */);
24555
24556 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
24557 doc: /* Template for displaying the title bar of visible frames.
24558 \(Assuming the window manager supports this feature.)
24559
24560 This variable has the same structure as `mode-line-format', except that
24561 the %c and %l constructs are ignored. It is used only on frames for
24562 which no explicit name has been set \(see `modify-frame-parameters'). */);
24563
24564 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
24565 doc: /* Template for displaying the title bar of an iconified frame.
24566 \(Assuming the window manager supports this feature.)
24567 This variable has the same structure as `mode-line-format' (which see),
24568 and is used only on frames for which no explicit name has been set
24569 \(see `modify-frame-parameters'). */);
24570 Vicon_title_format
24571 = Vframe_title_format
24572 = Fcons (intern ("multiple-frames"),
24573 Fcons (build_string ("%b"),
24574 Fcons (Fcons (empty_unibyte_string,
24575 Fcons (intern ("invocation-name"),
24576 Fcons (build_string ("@"),
24577 Fcons (intern ("system-name"),
24578 Qnil)))),
24579 Qnil)));
24580
24581 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
24582 doc: /* Maximum number of lines to keep in the message log buffer.
24583 If nil, disable message logging. If t, log messages but don't truncate
24584 the buffer when it becomes large. */);
24585 Vmessage_log_max = make_number (100);
24586
24587 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
24588 doc: /* Functions called before redisplay, if window sizes have changed.
24589 The value should be a list of functions that take one argument.
24590 Just before redisplay, for each frame, if any of its windows have changed
24591 size since the last redisplay, or have been split or deleted,
24592 all the functions in the list are called, with the frame as argument. */);
24593 Vwindow_size_change_functions = Qnil;
24594
24595 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
24596 doc: /* List of functions to call before redisplaying a window with scrolling.
24597 Each function is called with two arguments, the window
24598 and its new display-start position. Note that the value of `window-end'
24599 is not valid when these functions are called. */);
24600 Vwindow_scroll_functions = Qnil;
24601
24602 DEFVAR_LISP ("redisplay-end-trigger-functions", &Vredisplay_end_trigger_functions,
24603 doc: /* Functions called when redisplay of a window reaches the end trigger.
24604 Each function is called with two arguments, the window and the end trigger value.
24605 See `set-window-redisplay-end-trigger'. */);
24606 Vredisplay_end_trigger_functions = Qnil;
24607
24608 DEFVAR_LISP ("mouse-autoselect-window", &Vmouse_autoselect_window,
24609 doc: /* *Non-nil means autoselect window with mouse pointer.
24610 If nil, do not autoselect windows.
24611 A positive number means delay autoselection by that many seconds: a
24612 window is autoselected only after the mouse has remained in that
24613 window for the duration of the delay.
24614 A negative number has a similar effect, but causes windows to be
24615 autoselected only after the mouse has stopped moving. \(Because of
24616 the way Emacs compares mouse events, you will occasionally wait twice
24617 that time before the window gets selected.\)
24618 Any other value means to autoselect window instantaneously when the
24619 mouse pointer enters it.
24620
24621 Autoselection selects the minibuffer only if it is active, and never
24622 unselects the minibuffer if it is active.
24623
24624 When customizing this variable make sure that the actual value of
24625 `focus-follows-mouse' matches the behavior of your window manager. */);
24626 Vmouse_autoselect_window = Qnil;
24627
24628 DEFVAR_LISP ("auto-resize-tool-bars", &Vauto_resize_tool_bars,
24629 doc: /* *Non-nil means automatically resize tool-bars.
24630 This dynamically changes the tool-bar's height to the minimum height
24631 that is needed to make all tool-bar items visible.
24632 If value is `grow-only', the tool-bar's height is only increased
24633 automatically; to decrease the tool-bar height, use \\[recenter]. */);
24634 Vauto_resize_tool_bars = Qt;
24635
24636 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
24637 doc: /* *Non-nil means raise tool-bar buttons when the mouse moves over them. */);
24638 auto_raise_tool_bar_buttons_p = 1;
24639
24640 DEFVAR_BOOL ("make-cursor-line-fully-visible", &make_cursor_line_fully_visible_p,
24641 doc: /* *Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
24642 make_cursor_line_fully_visible_p = 1;
24643
24644 DEFVAR_LISP ("tool-bar-border", &Vtool_bar_border,
24645 doc: /* *Border below tool-bar in pixels.
24646 If an integer, use it as the height of the border.
24647 If it is one of `internal-border-width' or `border-width', use the
24648 value of the corresponding frame parameter.
24649 Otherwise, no border is added below the tool-bar. */);
24650 Vtool_bar_border = Qinternal_border_width;
24651
24652 DEFVAR_LISP ("tool-bar-button-margin", &Vtool_bar_button_margin,
24653 doc: /* *Margin around tool-bar buttons in pixels.
24654 If an integer, use that for both horizontal and vertical margins.
24655 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
24656 HORZ specifying the horizontal margin, and VERT specifying the
24657 vertical margin. */);
24658 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
24659
24660 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
24661 doc: /* *Relief thickness of tool-bar buttons. */);
24662 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
24663
24664 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
24665 doc: /* List of functions to call to fontify regions of text.
24666 Each function is called with one argument POS. Functions must
24667 fontify a region starting at POS in the current buffer, and give
24668 fontified regions the property `fontified'. */);
24669 Vfontification_functions = Qnil;
24670 Fmake_variable_buffer_local (Qfontification_functions);
24671
24672 DEFVAR_BOOL ("unibyte-display-via-language-environment",
24673 &unibyte_display_via_language_environment,
24674 doc: /* *Non-nil means display unibyte text according to language environment.
24675 Specifically this means that unibyte non-ASCII characters
24676 are displayed by converting them to the equivalent multibyte characters
24677 according to the current language environment. As a result, they are
24678 displayed according to the current fontset. */);
24679 unibyte_display_via_language_environment = 0;
24680
24681 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
24682 doc: /* *Maximum height for resizing mini-windows.
24683 If a float, it specifies a fraction of the mini-window frame's height.
24684 If an integer, it specifies a number of lines. */);
24685 Vmax_mini_window_height = make_float (0.25);
24686
24687 DEFVAR_LISP ("resize-mini-windows", &Vresize_mini_windows,
24688 doc: /* *How to resize mini-windows.
24689 A value of nil means don't automatically resize mini-windows.
24690 A value of t means resize them to fit the text displayed in them.
24691 A value of `grow-only', the default, means let mini-windows grow
24692 only, until their display becomes empty, at which point the windows
24693 go back to their normal size. */);
24694 Vresize_mini_windows = Qgrow_only;
24695
24696 DEFVAR_LISP ("blink-cursor-alist", &Vblink_cursor_alist,
24697 doc: /* Alist specifying how to blink the cursor off.
24698 Each element has the form (ON-STATE . OFF-STATE). Whenever the
24699 `cursor-type' frame-parameter or variable equals ON-STATE,
24700 comparing using `equal', Emacs uses OFF-STATE to specify
24701 how to blink it off. ON-STATE and OFF-STATE are values for
24702 the `cursor-type' frame parameter.
24703
24704 If a frame's ON-STATE has no entry in this list,
24705 the frame's other specifications determine how to blink the cursor off. */);
24706 Vblink_cursor_alist = Qnil;
24707
24708 DEFVAR_BOOL ("auto-hscroll-mode", &automatic_hscrolling_p,
24709 doc: /* *Non-nil means scroll the display automatically to make point visible. */);
24710 automatic_hscrolling_p = 1;
24711
24712 DEFVAR_INT ("hscroll-margin", &hscroll_margin,
24713 doc: /* *How many columns away from the window edge point is allowed to get
24714 before automatic hscrolling will horizontally scroll the window. */);
24715 hscroll_margin = 5;
24716
24717 DEFVAR_LISP ("hscroll-step", &Vhscroll_step,
24718 doc: /* *How many columns to scroll the window when point gets too close to the edge.
24719 When point is less than `hscroll-margin' columns from the window
24720 edge, automatic hscrolling will scroll the window by the amount of columns
24721 determined by this variable. If its value is a positive integer, scroll that
24722 many columns. If it's a positive floating-point number, it specifies the
24723 fraction of the window's width to scroll. If it's nil or zero, point will be
24724 centered horizontally after the scroll. Any other value, including negative
24725 numbers, are treated as if the value were zero.
24726
24727 Automatic hscrolling always moves point outside the scroll margin, so if
24728 point was more than scroll step columns inside the margin, the window will
24729 scroll more than the value given by the scroll step.
24730
24731 Note that the lower bound for automatic hscrolling specified by `scroll-left'
24732 and `scroll-right' overrides this variable's effect. */);
24733 Vhscroll_step = make_number (0);
24734
24735 DEFVAR_BOOL ("message-truncate-lines", &message_truncate_lines,
24736 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
24737 Bind this around calls to `message' to let it take effect. */);
24738 message_truncate_lines = 0;
24739
24740 DEFVAR_LISP ("menu-bar-update-hook", &Vmenu_bar_update_hook,
24741 doc: /* Normal hook run to update the menu bar definitions.
24742 Redisplay runs this hook before it redisplays the menu bar.
24743 This is used to update submenus such as Buffers,
24744 whose contents depend on various data. */);
24745 Vmenu_bar_update_hook = Qnil;
24746
24747 DEFVAR_LISP ("menu-updating-frame", &Vmenu_updating_frame,
24748 doc: /* Frame for which we are updating a menu.
24749 The enable predicate for a menu binding should check this variable. */);
24750 Vmenu_updating_frame = Qnil;
24751
24752 DEFVAR_BOOL ("inhibit-menubar-update", &inhibit_menubar_update,
24753 doc: /* Non-nil means don't update menu bars. Internal use only. */);
24754 inhibit_menubar_update = 0;
24755
24756 DEFVAR_BOOL ("inhibit-eval-during-redisplay", &inhibit_eval_during_redisplay,
24757 doc: /* Non-nil means don't eval Lisp during redisplay. */);
24758 inhibit_eval_during_redisplay = 0;
24759
24760 DEFVAR_BOOL ("inhibit-free-realized-faces", &inhibit_free_realized_faces,
24761 doc: /* Non-nil means don't free realized faces. Internal use only. */);
24762 inhibit_free_realized_faces = 0;
24763
24764 #if GLYPH_DEBUG
24765 DEFVAR_BOOL ("inhibit-try-window-id", &inhibit_try_window_id,
24766 doc: /* Inhibit try_window_id display optimization. */);
24767 inhibit_try_window_id = 0;
24768
24769 DEFVAR_BOOL ("inhibit-try-window-reusing", &inhibit_try_window_reusing,
24770 doc: /* Inhibit try_window_reusing display optimization. */);
24771 inhibit_try_window_reusing = 0;
24772
24773 DEFVAR_BOOL ("inhibit-try-cursor-movement", &inhibit_try_cursor_movement,
24774 doc: /* Inhibit try_cursor_movement display optimization. */);
24775 inhibit_try_cursor_movement = 0;
24776 #endif /* GLYPH_DEBUG */
24777
24778 DEFVAR_INT ("overline-margin", &overline_margin,
24779 doc: /* *Space between overline and text, in pixels.
24780 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
24781 margin to the caracter height. */);
24782 overline_margin = 2;
24783 }
24784
24785
24786 /* Initialize this module when Emacs starts. */
24787
24788 void
24789 init_xdisp ()
24790 {
24791 Lisp_Object root_window;
24792 struct window *mini_w;
24793
24794 current_header_line_height = current_mode_line_height = -1;
24795
24796 CHARPOS (this_line_start_pos) = 0;
24797
24798 mini_w = XWINDOW (minibuf_window);
24799 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
24800
24801 if (!noninteractive)
24802 {
24803 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
24804 int i;
24805
24806 XWINDOW (root_window)->top_line = make_number (FRAME_TOP_MARGIN (f));
24807 set_window_height (root_window,
24808 FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f),
24809 0);
24810 mini_w->top_line = make_number (FRAME_LINES (f) - 1);
24811 set_window_height (minibuf_window, 1, 0);
24812
24813 XWINDOW (root_window)->total_cols = make_number (FRAME_COLS (f));
24814 mini_w->total_cols = make_number (FRAME_COLS (f));
24815
24816 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
24817 scratch_glyph_row.glyphs[TEXT_AREA + 1]
24818 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
24819
24820 /* The default ellipsis glyphs `...'. */
24821 for (i = 0; i < 3; ++i)
24822 default_invis_vector[i] = make_number ('.');
24823 }
24824
24825 {
24826 /* Allocate the buffer for frame titles.
24827 Also used for `format-mode-line'. */
24828 int size = 100;
24829 mode_line_noprop_buf = (char *) xmalloc (size);
24830 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
24831 mode_line_noprop_ptr = mode_line_noprop_buf;
24832 mode_line_target = MODE_LINE_DISPLAY;
24833 }
24834
24835 help_echo_showing_p = 0;
24836 }
24837
24838
24839 /* arch-tag: eacc864d-bb6a-4b74-894a-1a4399a1358b
24840 (do not change this comment) */