]> code.delx.au - gnu-emacs/blob - src/xdisp.c
Merge from emacs--devo--0
[gnu-emacs] / src / xdisp.c
1 /* Display generation from window structure and buffer text.
2 Copyright (C) 1985, 1986, 1987, 1988, 1993, 1994, 1995,
3 1997, 1998, 1999, 2000, 2001, 2002, 2003,
4 2004, 2005, 2006, 2007 Free Software Foundation, Inc.
5
6 This file is part of GNU Emacs.
7
8 GNU Emacs is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
12
13 GNU Emacs is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GNU Emacs; see the file COPYING. If not, write to
20 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 Boston, MA 02110-1301, USA. */
22
23 /* New redisplay written by Gerd Moellmann <gerd@gnu.org>.
24
25 Redisplay.
26
27 Emacs separates the task of updating the display from code
28 modifying global state, e.g. buffer text. This way functions
29 operating on buffers don't also have to be concerned with updating
30 the display.
31
32 Updating the display is triggered by the Lisp interpreter when it
33 decides it's time to do it. This is done either automatically for
34 you as part of the interpreter's command loop or as the result of
35 calling Lisp functions like `sit-for'. The C function `redisplay'
36 in xdisp.c is the only entry into the inner redisplay code. (Or,
37 let's say almost---see the description of direct update
38 operations, below.)
39
40 The following diagram shows how redisplay code is invoked. As you
41 can see, Lisp calls redisplay and vice versa. Under window systems
42 like X, some portions of the redisplay code are also called
43 asynchronously during mouse movement or expose events. It is very
44 important that these code parts do NOT use the C library (malloc,
45 free) because many C libraries under Unix are not reentrant. They
46 may also NOT call functions of the Lisp interpreter which could
47 change the interpreter's state. If you don't follow these rules,
48 you will encounter bugs which are very hard to explain.
49
50 (Direct functions, see below)
51 direct_output_for_insert,
52 direct_forward_char (dispnew.c)
53 +---------------------------------+
54 | |
55 | V
56 +--------------+ redisplay +----------------+
57 | Lisp machine |---------------->| Redisplay code |<--+
58 +--------------+ (xdisp.c) +----------------+ |
59 ^ | |
60 +----------------------------------+ |
61 Don't use this path when called |
62 asynchronously! |
63 |
64 expose_window (asynchronous) |
65 |
66 X expose events -----+
67
68 What does redisplay do? Obviously, it has to figure out somehow what
69 has been changed since the last time the display has been updated,
70 and to make these changes visible. Preferably it would do that in
71 a moderately intelligent way, i.e. fast.
72
73 Changes in buffer text can be deduced from window and buffer
74 structures, and from some global variables like `beg_unchanged' and
75 `end_unchanged'. The contents of the display are additionally
76 recorded in a `glyph matrix', a two-dimensional matrix of glyph
77 structures. Each row in such a matrix corresponds to a line on the
78 display, and each glyph in a row corresponds to a column displaying
79 a character, an image, or what else. This matrix is called the
80 `current glyph matrix' or `current matrix' in redisplay
81 terminology.
82
83 For buffer parts that have been changed since the last update, a
84 second glyph matrix is constructed, the so called `desired glyph
85 matrix' or short `desired matrix'. Current and desired matrix are
86 then compared to find a cheap way to update the display, e.g. by
87 reusing part of the display by scrolling lines.
88
89
90 Direct operations.
91
92 You will find a lot of redisplay optimizations when you start
93 looking at the innards of redisplay. The overall goal of all these
94 optimizations is to make redisplay fast because it is done
95 frequently.
96
97 Two optimizations are not found in xdisp.c. These are the direct
98 operations mentioned above. As the name suggests they follow a
99 different principle than the rest of redisplay. Instead of
100 building a desired matrix and then comparing it with the current
101 display, they perform their actions directly on the display and on
102 the current matrix.
103
104 One direct operation updates the display after one character has
105 been entered. The other one moves the cursor by one position
106 forward or backward. You find these functions under the names
107 `direct_output_for_insert' and `direct_output_forward_char' in
108 dispnew.c.
109
110
111 Desired matrices.
112
113 Desired matrices are always built per Emacs window. The function
114 `display_line' is the central function to look at if you are
115 interested. It constructs one row in a desired matrix given an
116 iterator structure containing both a buffer position and a
117 description of the environment in which the text is to be
118 displayed. But this is too early, read on.
119
120 Characters and pixmaps displayed for a range of buffer text depend
121 on various settings of buffers and windows, on overlays and text
122 properties, on display tables, on selective display. The good news
123 is that all this hairy stuff is hidden behind a small set of
124 interface functions taking an iterator structure (struct it)
125 argument.
126
127 Iteration over things to be displayed is then simple. It is
128 started by initializing an iterator with a call to init_iterator.
129 Calls to get_next_display_element fill the iterator structure with
130 relevant information about the next thing to display. Calls to
131 set_iterator_to_next move the iterator to the next thing.
132
133 Besides this, an iterator also contains information about the
134 display environment in which glyphs for display elements are to be
135 produced. It has fields for the width and height of the display,
136 the information whether long lines are truncated or continued, a
137 current X and Y position, and lots of other stuff you can better
138 see in dispextern.h.
139
140 Glyphs in a desired matrix are normally constructed in a loop
141 calling get_next_display_element and then produce_glyphs. The call
142 to produce_glyphs will fill the iterator structure with pixel
143 information about the element being displayed and at the same time
144 produce glyphs for it. If the display element fits on the line
145 being displayed, set_iterator_to_next is called next, otherwise the
146 glyphs produced are discarded.
147
148
149 Frame matrices.
150
151 That just couldn't be all, could it? What about terminal types not
152 supporting operations on sub-windows of the screen? To update the
153 display on such a terminal, window-based glyph matrices are not
154 well suited. To be able to reuse part of the display (scrolling
155 lines up and down), we must instead have a view of the whole
156 screen. This is what `frame matrices' are for. They are a trick.
157
158 Frames on terminals like above have a glyph pool. Windows on such
159 a frame sub-allocate their glyph memory from their frame's glyph
160 pool. The frame itself is given its own glyph matrices. By
161 coincidence---or maybe something else---rows in window glyph
162 matrices are slices of corresponding rows in frame matrices. Thus
163 writing to window matrices implicitly updates a frame matrix which
164 provides us with the view of the whole screen that we originally
165 wanted to have without having to move many bytes around. To be
166 honest, there is a little bit more done, but not much more. If you
167 plan to extend that code, take a look at dispnew.c. The function
168 build_frame_matrix is a good starting point. */
169
170 #include <config.h>
171 #include <stdio.h>
172
173 #include "lisp.h"
174 #include "keyboard.h"
175 #include "frame.h"
176 #include "window.h"
177 #include "termchar.h"
178 #include "dispextern.h"
179 #include "buffer.h"
180 #include "character.h"
181 #include "charset.h"
182 #include "indent.h"
183 #include "commands.h"
184 #include "keymap.h"
185 #include "macros.h"
186 #include "disptab.h"
187 #include "termhooks.h"
188 #include "intervals.h"
189 #include "coding.h"
190 #include "process.h"
191 #include "region-cache.h"
192 #include "fontset.h"
193 #include "blockinput.h"
194
195 #ifdef HAVE_X_WINDOWS
196 #include "xterm.h"
197 #endif
198 #ifdef WINDOWSNT
199 #include "w32term.h"
200 #endif
201 #ifdef MAC_OS
202 #include "macterm.h"
203 #endif
204
205 #ifdef HAVE_WINDOW_SYSTEM
206 #ifdef USE_FONT_BACKEND
207 #include "font.h"
208 #endif /* USE_FONT_BACKEND */
209 #endif /* HAVE_WINDOW_SYSTEM */
210
211 #ifndef FRAME_X_OUTPUT
212 #define FRAME_X_OUTPUT(f) ((f)->output_data.x)
213 #endif
214
215 #define INFINITY 10000000
216
217 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
218 || defined (USE_GTK)
219 extern void set_frame_menubar P_ ((struct frame *f, int, int));
220 extern int pending_menu_activation;
221 #endif
222
223 extern int interrupt_input;
224 extern int command_loop_level;
225
226 extern Lisp_Object do_mouse_tracking;
227
228 extern int minibuffer_auto_raise;
229 extern Lisp_Object Vminibuffer_list;
230
231 extern Lisp_Object Qface;
232 extern Lisp_Object Qmode_line, Qmode_line_inactive, Qheader_line;
233
234 extern Lisp_Object Voverriding_local_map;
235 extern Lisp_Object Voverriding_local_map_menu_flag;
236 extern Lisp_Object Qmenu_item;
237 extern Lisp_Object Qwhen;
238 extern Lisp_Object Qhelp_echo;
239
240 Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
241 Lisp_Object Qwindow_scroll_functions, Vwindow_scroll_functions;
242 Lisp_Object Qredisplay_end_trigger_functions, Vredisplay_end_trigger_functions;
243 Lisp_Object Qinhibit_point_motion_hooks;
244 Lisp_Object QCeval, QCfile, QCdata, QCpropertize;
245 Lisp_Object Qfontified;
246 Lisp_Object Qgrow_only;
247 Lisp_Object Qinhibit_eval_during_redisplay;
248 Lisp_Object Qbuffer_position, Qposition, Qobject;
249
250 /* Cursor shapes */
251 Lisp_Object Qbar, Qhbar, Qbox, Qhollow;
252
253 /* Pointer shapes */
254 Lisp_Object Qarrow, Qhand, Qtext;
255
256 Lisp_Object Qrisky_local_variable;
257
258 /* Holds the list (error). */
259 Lisp_Object list_of_error;
260
261 /* Functions called to fontify regions of text. */
262
263 Lisp_Object Vfontification_functions;
264 Lisp_Object Qfontification_functions;
265
266 /* Non-nil means automatically select any window when the mouse
267 cursor moves into it. */
268 Lisp_Object Vmouse_autoselect_window;
269
270 /* Non-zero means draw tool bar buttons raised when the mouse moves
271 over them. */
272
273 int auto_raise_tool_bar_buttons_p;
274
275 /* Non-zero means to reposition window if cursor line is only partially visible. */
276
277 int make_cursor_line_fully_visible_p;
278
279 /* Margin below tool bar in pixels. 0 or nil means no margin.
280 If value is `internal-border-width' or `border-width',
281 the corresponding frame parameter is used. */
282
283 Lisp_Object Vtool_bar_border;
284
285 /* Margin around tool bar buttons in pixels. */
286
287 Lisp_Object Vtool_bar_button_margin;
288
289 /* Thickness of shadow to draw around tool bar buttons. */
290
291 EMACS_INT tool_bar_button_relief;
292
293 /* Non-nil means automatically resize tool-bars so that all tool-bar
294 items are visible, and no blank lines remain.
295
296 If value is `grow-only', only make tool-bar bigger. */
297
298 Lisp_Object Vauto_resize_tool_bars;
299
300 /* Non-zero means draw block and hollow cursor as wide as the glyph
301 under it. For example, if a block cursor is over a tab, it will be
302 drawn as wide as that tab on the display. */
303
304 int x_stretch_cursor_p;
305
306 /* Non-nil means don't actually do any redisplay. */
307
308 Lisp_Object Vinhibit_redisplay, Qinhibit_redisplay;
309
310 /* Non-zero means Lisp evaluation during redisplay is inhibited. */
311
312 int inhibit_eval_during_redisplay;
313
314 /* Names of text properties relevant for redisplay. */
315
316 Lisp_Object Qdisplay;
317 extern Lisp_Object Qface, Qinvisible, Qwidth;
318
319 /* Symbols used in text property values. */
320
321 Lisp_Object Vdisplay_pixels_per_inch;
322 Lisp_Object Qspace, QCalign_to, QCrelative_width, QCrelative_height;
323 Lisp_Object Qleft_margin, Qright_margin, Qspace_width, Qraise;
324 Lisp_Object Qslice;
325 Lisp_Object Qcenter;
326 Lisp_Object Qmargin, Qpointer;
327 Lisp_Object Qline_height;
328 extern Lisp_Object Qheight;
329 extern Lisp_Object QCwidth, QCheight, QCascent;
330 extern Lisp_Object Qscroll_bar;
331 extern Lisp_Object Qcursor;
332
333 /* Non-nil means highlight trailing whitespace. */
334
335 Lisp_Object Vshow_trailing_whitespace;
336
337 /* Non-nil means escape non-break space and hyphens. */
338
339 Lisp_Object Vnobreak_char_display;
340
341 #ifdef HAVE_WINDOW_SYSTEM
342 extern Lisp_Object Voverflow_newline_into_fringe;
343
344 /* Test if overflow newline into fringe. Called with iterator IT
345 at or past right window margin, and with IT->current_x set. */
346
347 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(it) \
348 (!NILP (Voverflow_newline_into_fringe) \
349 && FRAME_WINDOW_P (it->f) \
350 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) > 0 \
351 && it->current_x == it->last_visible_x)
352
353 #endif /* HAVE_WINDOW_SYSTEM */
354
355 /* Non-nil means show the text cursor in void text areas
356 i.e. in blank areas after eol and eob. This used to be
357 the default in 21.3. */
358
359 Lisp_Object Vvoid_text_area_pointer;
360
361 /* Name of the face used to highlight trailing whitespace. */
362
363 Lisp_Object Qtrailing_whitespace;
364
365 /* Name and number of the face used to highlight escape glyphs. */
366
367 Lisp_Object Qescape_glyph;
368
369 /* Name and number of the face used to highlight non-breaking spaces. */
370
371 Lisp_Object Qnobreak_space;
372
373 /* The symbol `image' which is the car of the lists used to represent
374 images in Lisp. */
375
376 Lisp_Object Qimage;
377
378 /* The image map types. */
379 Lisp_Object QCmap, QCpointer;
380 Lisp_Object Qrect, Qcircle, Qpoly;
381
382 /* Non-zero means print newline to stdout before next mini-buffer
383 message. */
384
385 int noninteractive_need_newline;
386
387 /* Non-zero means print newline to message log before next message. */
388
389 static int message_log_need_newline;
390
391 /* Three markers that message_dolog uses.
392 It could allocate them itself, but that causes trouble
393 in handling memory-full errors. */
394 static Lisp_Object message_dolog_marker1;
395 static Lisp_Object message_dolog_marker2;
396 static Lisp_Object message_dolog_marker3;
397 \f
398 /* The buffer position of the first character appearing entirely or
399 partially on the line of the selected window which contains the
400 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
401 redisplay optimization in redisplay_internal. */
402
403 static struct text_pos this_line_start_pos;
404
405 /* Number of characters past the end of the line above, including the
406 terminating newline. */
407
408 static struct text_pos this_line_end_pos;
409
410 /* The vertical positions and the height of this line. */
411
412 static int this_line_vpos;
413 static int this_line_y;
414 static int this_line_pixel_height;
415
416 /* X position at which this display line starts. Usually zero;
417 negative if first character is partially visible. */
418
419 static int this_line_start_x;
420
421 /* Buffer that this_line_.* variables are referring to. */
422
423 static struct buffer *this_line_buffer;
424
425 /* Nonzero means truncate lines in all windows less wide than the
426 frame. */
427
428 int truncate_partial_width_windows;
429
430 /* A flag to control how to display unibyte 8-bit character. */
431
432 int unibyte_display_via_language_environment;
433
434 /* Nonzero means we have more than one non-mini-buffer-only frame.
435 Not guaranteed to be accurate except while parsing
436 frame-title-format. */
437
438 int multiple_frames;
439
440 Lisp_Object Vglobal_mode_string;
441
442
443 /* List of variables (symbols) which hold markers for overlay arrows.
444 The symbols on this list are examined during redisplay to determine
445 where to display overlay arrows. */
446
447 Lisp_Object Voverlay_arrow_variable_list;
448
449 /* Marker for where to display an arrow on top of the buffer text. */
450
451 Lisp_Object Voverlay_arrow_position;
452
453 /* String to display for the arrow. Only used on terminal frames. */
454
455 Lisp_Object Voverlay_arrow_string;
456
457 /* Values of those variables at last redisplay are stored as
458 properties on `overlay-arrow-position' symbol. However, if
459 Voverlay_arrow_position is a marker, last-arrow-position is its
460 numerical position. */
461
462 Lisp_Object Qlast_arrow_position, Qlast_arrow_string;
463
464 /* Alternative overlay-arrow-string and overlay-arrow-bitmap
465 properties on a symbol in overlay-arrow-variable-list. */
466
467 Lisp_Object Qoverlay_arrow_string, Qoverlay_arrow_bitmap;
468
469 /* Like mode-line-format, but for the title bar on a visible frame. */
470
471 Lisp_Object Vframe_title_format;
472
473 /* Like mode-line-format, but for the title bar on an iconified frame. */
474
475 Lisp_Object Vicon_title_format;
476
477 /* List of functions to call when a window's size changes. These
478 functions get one arg, a frame on which one or more windows' sizes
479 have changed. */
480
481 static Lisp_Object Vwindow_size_change_functions;
482
483 Lisp_Object Qmenu_bar_update_hook, Vmenu_bar_update_hook;
484
485 /* Nonzero if an overlay arrow has been displayed in this window. */
486
487 static int overlay_arrow_seen;
488
489 /* Nonzero means highlight the region even in nonselected windows. */
490
491 int highlight_nonselected_windows;
492
493 /* If cursor motion alone moves point off frame, try scrolling this
494 many lines up or down if that will bring it back. */
495
496 static EMACS_INT scroll_step;
497
498 /* Nonzero means scroll just far enough to bring point back on the
499 screen, when appropriate. */
500
501 static EMACS_INT scroll_conservatively;
502
503 /* Recenter the window whenever point gets within this many lines of
504 the top or bottom of the window. This value is translated into a
505 pixel value by multiplying it with FRAME_LINE_HEIGHT, which means
506 that there is really a fixed pixel height scroll margin. */
507
508 EMACS_INT scroll_margin;
509
510 /* Number of windows showing the buffer of the selected window (or
511 another buffer with the same base buffer). keyboard.c refers to
512 this. */
513
514 int buffer_shared;
515
516 /* Vector containing glyphs for an ellipsis `...'. */
517
518 static Lisp_Object default_invis_vector[3];
519
520 /* Zero means display the mode-line/header-line/menu-bar in the default face
521 (this slightly odd definition is for compatibility with previous versions
522 of emacs), non-zero means display them using their respective faces.
523
524 This variable is deprecated. */
525
526 int mode_line_inverse_video;
527
528 /* Prompt to display in front of the mini-buffer contents. */
529
530 Lisp_Object minibuf_prompt;
531
532 /* Width of current mini-buffer prompt. Only set after display_line
533 of the line that contains the prompt. */
534
535 int minibuf_prompt_width;
536
537 /* This is the window where the echo area message was displayed. It
538 is always a mini-buffer window, but it may not be the same window
539 currently active as a mini-buffer. */
540
541 Lisp_Object echo_area_window;
542
543 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
544 pushes the current message and the value of
545 message_enable_multibyte on the stack, the function restore_message
546 pops the stack and displays MESSAGE again. */
547
548 Lisp_Object Vmessage_stack;
549
550 /* Nonzero means multibyte characters were enabled when the echo area
551 message was specified. */
552
553 int message_enable_multibyte;
554
555 /* Nonzero if we should redraw the mode lines on the next redisplay. */
556
557 int update_mode_lines;
558
559 /* Nonzero if window sizes or contents have changed since last
560 redisplay that finished. */
561
562 int windows_or_buffers_changed;
563
564 /* Nonzero means a frame's cursor type has been changed. */
565
566 int cursor_type_changed;
567
568 /* Nonzero after display_mode_line if %l was used and it displayed a
569 line number. */
570
571 int line_number_displayed;
572
573 /* Maximum buffer size for which to display line numbers. */
574
575 Lisp_Object Vline_number_display_limit;
576
577 /* Line width to consider when repositioning for line number display. */
578
579 static EMACS_INT line_number_display_limit_width;
580
581 /* Number of lines to keep in the message log buffer. t means
582 infinite. nil means don't log at all. */
583
584 Lisp_Object Vmessage_log_max;
585
586 /* The name of the *Messages* buffer, a string. */
587
588 static Lisp_Object Vmessages_buffer_name;
589
590 /* Current, index 0, and last displayed echo area message. Either
591 buffers from echo_buffers, or nil to indicate no message. */
592
593 Lisp_Object echo_area_buffer[2];
594
595 /* The buffers referenced from echo_area_buffer. */
596
597 static Lisp_Object echo_buffer[2];
598
599 /* A vector saved used in with_area_buffer to reduce consing. */
600
601 static Lisp_Object Vwith_echo_area_save_vector;
602
603 /* Non-zero means display_echo_area should display the last echo area
604 message again. Set by redisplay_preserve_echo_area. */
605
606 static int display_last_displayed_message_p;
607
608 /* Nonzero if echo area is being used by print; zero if being used by
609 message. */
610
611 int message_buf_print;
612
613 /* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
614
615 Lisp_Object Qinhibit_menubar_update;
616 int inhibit_menubar_update;
617
618 /* When evaluating expressions from menu bar items (enable conditions,
619 for instance), this is the frame they are being processed for. */
620
621 Lisp_Object Vmenu_updating_frame;
622
623 /* Maximum height for resizing mini-windows. Either a float
624 specifying a fraction of the available height, or an integer
625 specifying a number of lines. */
626
627 Lisp_Object Vmax_mini_window_height;
628
629 /* Non-zero means messages should be displayed with truncated
630 lines instead of being continued. */
631
632 int message_truncate_lines;
633 Lisp_Object Qmessage_truncate_lines;
634
635 /* Set to 1 in clear_message to make redisplay_internal aware
636 of an emptied echo area. */
637
638 static int message_cleared_p;
639
640 /* How to blink the default frame cursor off. */
641 Lisp_Object Vblink_cursor_alist;
642
643 /* A scratch glyph row with contents used for generating truncation
644 glyphs. Also used in direct_output_for_insert. */
645
646 #define MAX_SCRATCH_GLYPHS 100
647 struct glyph_row scratch_glyph_row;
648 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
649
650 /* Ascent and height of the last line processed by move_it_to. */
651
652 static int last_max_ascent, last_height;
653
654 /* Non-zero if there's a help-echo in the echo area. */
655
656 int help_echo_showing_p;
657
658 /* If >= 0, computed, exact values of mode-line and header-line height
659 to use in the macros CURRENT_MODE_LINE_HEIGHT and
660 CURRENT_HEADER_LINE_HEIGHT. */
661
662 int current_mode_line_height, current_header_line_height;
663
664 /* The maximum distance to look ahead for text properties. Values
665 that are too small let us call compute_char_face and similar
666 functions too often which is expensive. Values that are too large
667 let us call compute_char_face and alike too often because we
668 might not be interested in text properties that far away. */
669
670 #define TEXT_PROP_DISTANCE_LIMIT 100
671
672 #if GLYPH_DEBUG
673
674 /* Variables to turn off display optimizations from Lisp. */
675
676 int inhibit_try_window_id, inhibit_try_window_reusing;
677 int inhibit_try_cursor_movement;
678
679 /* Non-zero means print traces of redisplay if compiled with
680 GLYPH_DEBUG != 0. */
681
682 int trace_redisplay_p;
683
684 #endif /* GLYPH_DEBUG */
685
686 #ifdef DEBUG_TRACE_MOVE
687 /* Non-zero means trace with TRACE_MOVE to stderr. */
688 int trace_move;
689
690 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
691 #else
692 #define TRACE_MOVE(x) (void) 0
693 #endif
694
695 /* Non-zero means automatically scroll windows horizontally to make
696 point visible. */
697
698 int automatic_hscrolling_p;
699
700 /* How close to the margin can point get before the window is scrolled
701 horizontally. */
702 EMACS_INT hscroll_margin;
703
704 /* How much to scroll horizontally when point is inside the above margin. */
705 Lisp_Object Vhscroll_step;
706
707 /* The variable `resize-mini-windows'. If nil, don't resize
708 mini-windows. If t, always resize them to fit the text they
709 display. If `grow-only', let mini-windows grow only until they
710 become empty. */
711
712 Lisp_Object Vresize_mini_windows;
713
714 /* Buffer being redisplayed -- for redisplay_window_error. */
715
716 struct buffer *displayed_buffer;
717
718 /* Space between overline and text. */
719
720 EMACS_INT overline_margin;
721
722 /* Value returned from text property handlers (see below). */
723
724 enum prop_handled
725 {
726 HANDLED_NORMALLY,
727 HANDLED_RECOMPUTE_PROPS,
728 HANDLED_OVERLAY_STRING_CONSUMED,
729 HANDLED_RETURN
730 };
731
732 /* A description of text properties that redisplay is interested
733 in. */
734
735 struct props
736 {
737 /* The name of the property. */
738 Lisp_Object *name;
739
740 /* A unique index for the property. */
741 enum prop_idx idx;
742
743 /* A handler function called to set up iterator IT from the property
744 at IT's current position. Value is used to steer handle_stop. */
745 enum prop_handled (*handler) P_ ((struct it *it));
746 };
747
748 static enum prop_handled handle_face_prop P_ ((struct it *));
749 static enum prop_handled handle_invisible_prop P_ ((struct it *));
750 static enum prop_handled handle_display_prop P_ ((struct it *));
751 static enum prop_handled handle_composition_prop P_ ((struct it *));
752 static enum prop_handled handle_overlay_change P_ ((struct it *));
753 static enum prop_handled handle_fontified_prop P_ ((struct it *));
754 static enum prop_handled handle_auto_composed_prop P_ ((struct it *));
755
756 /* Properties handled by iterators. */
757
758 static struct props it_props[] =
759 {
760 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
761 /* Handle `face' before `display' because some sub-properties of
762 `display' need to know the face. */
763 {&Qface, FACE_PROP_IDX, handle_face_prop},
764 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
765 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
766 {&Qauto_composed, AUTO_COMPOSED_PROP_IDX, handle_auto_composed_prop},
767 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
768 {NULL, 0, NULL}
769 };
770
771 /* Value is the position described by X. If X is a marker, value is
772 the marker_position of X. Otherwise, value is X. */
773
774 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
775
776 /* Enumeration returned by some move_it_.* functions internally. */
777
778 enum move_it_result
779 {
780 /* Not used. Undefined value. */
781 MOVE_UNDEFINED,
782
783 /* Move ended at the requested buffer position or ZV. */
784 MOVE_POS_MATCH_OR_ZV,
785
786 /* Move ended at the requested X pixel position. */
787 MOVE_X_REACHED,
788
789 /* Move within a line ended at the end of a line that must be
790 continued. */
791 MOVE_LINE_CONTINUED,
792
793 /* Move within a line ended at the end of a line that would
794 be displayed truncated. */
795 MOVE_LINE_TRUNCATED,
796
797 /* Move within a line ended at a line end. */
798 MOVE_NEWLINE_OR_CR
799 };
800
801 /* This counter is used to clear the face cache every once in a while
802 in redisplay_internal. It is incremented for each redisplay.
803 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
804 cleared. */
805
806 #define CLEAR_FACE_CACHE_COUNT 500
807 static int clear_face_cache_count;
808
809 /* Similarly for the image cache. */
810
811 #ifdef HAVE_WINDOW_SYSTEM
812 #define CLEAR_IMAGE_CACHE_COUNT 101
813 static int clear_image_cache_count;
814 #endif
815
816 /* Non-zero while redisplay_internal is in progress. */
817
818 int redisplaying_p;
819
820 /* Non-zero means don't free realized faces. Bound while freeing
821 realized faces is dangerous because glyph matrices might still
822 reference them. */
823
824 int inhibit_free_realized_faces;
825 Lisp_Object Qinhibit_free_realized_faces;
826
827 /* If a string, XTread_socket generates an event to display that string.
828 (The display is done in read_char.) */
829
830 Lisp_Object help_echo_string;
831 Lisp_Object help_echo_window;
832 Lisp_Object help_echo_object;
833 int help_echo_pos;
834
835 /* Temporary variable for XTread_socket. */
836
837 Lisp_Object previous_help_echo_string;
838
839 /* Null glyph slice */
840
841 static struct glyph_slice null_glyph_slice = { 0, 0, 0, 0 };
842
843 \f
844 /* Function prototypes. */
845
846 static void setup_for_ellipsis P_ ((struct it *, int));
847 static void mark_window_display_accurate_1 P_ ((struct window *, int));
848 static int single_display_spec_string_p P_ ((Lisp_Object, Lisp_Object));
849 static int display_prop_string_p P_ ((Lisp_Object, Lisp_Object));
850 static int cursor_row_p P_ ((struct window *, struct glyph_row *));
851 static int redisplay_mode_lines P_ ((Lisp_Object, int));
852 static char *decode_mode_spec_coding P_ ((Lisp_Object, char *, int));
853
854 #if 0
855 static int invisible_text_between_p P_ ((struct it *, int, int));
856 #endif
857
858 static void pint2str P_ ((char *, int, int));
859 static void pint2hrstr P_ ((char *, int, int));
860 static struct text_pos run_window_scroll_functions P_ ((Lisp_Object,
861 struct text_pos));
862 static void reconsider_clip_changes P_ ((struct window *, struct buffer *));
863 static int text_outside_line_unchanged_p P_ ((struct window *, int, int));
864 static void store_mode_line_noprop_char P_ ((char));
865 static int store_mode_line_noprop P_ ((const unsigned char *, int, int));
866 static void x_consider_frame_title P_ ((Lisp_Object));
867 static void handle_stop P_ ((struct it *));
868 static int tool_bar_lines_needed P_ ((struct frame *, int *));
869 static int single_display_spec_intangible_p P_ ((Lisp_Object));
870 static void ensure_echo_area_buffers P_ ((void));
871 static Lisp_Object unwind_with_echo_area_buffer P_ ((Lisp_Object));
872 static Lisp_Object with_echo_area_buffer_unwind_data P_ ((struct window *));
873 static int with_echo_area_buffer P_ ((struct window *, int,
874 int (*) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
875 EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
876 static void clear_garbaged_frames P_ ((void));
877 static int current_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
878 static int truncate_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
879 static int set_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
880 static int display_echo_area P_ ((struct window *));
881 static int display_echo_area_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
882 static int resize_mini_window_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
883 static Lisp_Object unwind_redisplay P_ ((Lisp_Object));
884 static int string_char_and_length P_ ((const unsigned char *, int, int *));
885 static struct text_pos display_prop_end P_ ((struct it *, Lisp_Object,
886 struct text_pos));
887 static int compute_window_start_on_continuation_line P_ ((struct window *));
888 static Lisp_Object safe_eval_handler P_ ((Lisp_Object));
889 static void insert_left_trunc_glyphs P_ ((struct it *));
890 static struct glyph_row *get_overlay_arrow_glyph_row P_ ((struct window *,
891 Lisp_Object));
892 static void extend_face_to_end_of_line P_ ((struct it *));
893 static int append_space_for_newline P_ ((struct it *, int));
894 static int cursor_row_fully_visible_p P_ ((struct window *, int, int));
895 static int try_scrolling P_ ((Lisp_Object, int, EMACS_INT, EMACS_INT, int, int));
896 static int try_cursor_movement P_ ((Lisp_Object, struct text_pos, int *));
897 static int trailing_whitespace_p P_ ((int));
898 static int message_log_check_duplicate P_ ((int, int, int, int));
899 static void push_it P_ ((struct it *));
900 static void pop_it P_ ((struct it *));
901 static void sync_frame_with_window_matrix_rows P_ ((struct window *));
902 static void select_frame_for_redisplay P_ ((Lisp_Object));
903 static void redisplay_internal P_ ((int));
904 static int echo_area_display P_ ((int));
905 static void redisplay_windows P_ ((Lisp_Object));
906 static void redisplay_window P_ ((Lisp_Object, int));
907 static Lisp_Object redisplay_window_error ();
908 static Lisp_Object redisplay_window_0 P_ ((Lisp_Object));
909 static Lisp_Object redisplay_window_1 P_ ((Lisp_Object));
910 static int update_menu_bar P_ ((struct frame *, int, int));
911 static int try_window_reusing_current_matrix P_ ((struct window *));
912 static int try_window_id P_ ((struct window *));
913 static int display_line P_ ((struct it *));
914 static int display_mode_lines P_ ((struct window *));
915 static int display_mode_line P_ ((struct window *, enum face_id, Lisp_Object));
916 static int display_mode_element P_ ((struct it *, int, int, int, Lisp_Object, Lisp_Object, int));
917 static int store_mode_line_string P_ ((char *, Lisp_Object, int, int, int, Lisp_Object));
918 static char *decode_mode_spec P_ ((struct window *, int, int, int, int *));
919 static void display_menu_bar P_ ((struct window *));
920 static int display_count_lines P_ ((int, int, int, int, int *));
921 static int display_string P_ ((unsigned char *, Lisp_Object, Lisp_Object,
922 int, int, struct it *, int, int, int, int));
923 static void compute_line_metrics P_ ((struct it *));
924 static void run_redisplay_end_trigger_hook P_ ((struct it *));
925 static int get_overlay_strings P_ ((struct it *, int));
926 static int get_overlay_strings_1 P_ ((struct it *, int, int));
927 static void next_overlay_string P_ ((struct it *));
928 static void reseat P_ ((struct it *, struct text_pos, int));
929 static void reseat_1 P_ ((struct it *, struct text_pos, int));
930 static void back_to_previous_visible_line_start P_ ((struct it *));
931 void reseat_at_previous_visible_line_start P_ ((struct it *));
932 static void reseat_at_next_visible_line_start P_ ((struct it *, int));
933 static int next_element_from_ellipsis P_ ((struct it *));
934 static int next_element_from_display_vector P_ ((struct it *));
935 static int next_element_from_string P_ ((struct it *));
936 static int next_element_from_c_string P_ ((struct it *));
937 static int next_element_from_buffer P_ ((struct it *));
938 static int next_element_from_composition P_ ((struct it *));
939 static int next_element_from_image P_ ((struct it *));
940 static int next_element_from_stretch P_ ((struct it *));
941 static void load_overlay_strings P_ ((struct it *, int));
942 static int init_from_display_pos P_ ((struct it *, struct window *,
943 struct display_pos *));
944 static void reseat_to_string P_ ((struct it *, unsigned char *,
945 Lisp_Object, int, int, int, int));
946 static enum move_it_result move_it_in_display_line_to P_ ((struct it *,
947 int, int, int));
948 void move_it_vertically_backward P_ ((struct it *, int));
949 static void init_to_row_start P_ ((struct it *, struct window *,
950 struct glyph_row *));
951 static int init_to_row_end P_ ((struct it *, struct window *,
952 struct glyph_row *));
953 static void back_to_previous_line_start P_ ((struct it *));
954 static int forward_to_next_line_start P_ ((struct it *, int *));
955 static struct text_pos string_pos_nchars_ahead P_ ((struct text_pos,
956 Lisp_Object, int));
957 static struct text_pos string_pos P_ ((int, Lisp_Object));
958 static struct text_pos c_string_pos P_ ((int, unsigned char *, int));
959 static int number_of_chars P_ ((unsigned char *, int));
960 static void compute_stop_pos P_ ((struct it *));
961 static void compute_string_pos P_ ((struct text_pos *, struct text_pos,
962 Lisp_Object));
963 static int face_before_or_after_it_pos P_ ((struct it *, int));
964 static int next_overlay_change P_ ((int));
965 static int handle_single_display_spec P_ ((struct it *, Lisp_Object,
966 Lisp_Object, 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;
4557 EMACS_INT pos, this_pos;
4558
4559 if (STRINGP (it->string))
4560 pos = IT_STRING_CHARPOS (*it);
4561 else
4562 pos = IT_CHARPOS (*it);
4563 this_pos = pos;
4564
4565 val =Fget_char_property (make_number (pos), Qauto_composed, it->string);
4566 if (! NILP (val))
4567 {
4568 Lisp_Object limit = Qnil, next;
4569
4570 /* As Fnext_single_char_property_change is very slow, we
4571 limit the search to the current line. */
4572 if (STRINGP (it->string))
4573 limit = make_number (SCHARS (it->string));
4574 else
4575 limit = make_number (find_next_newline_no_quit (pos, 1));
4576
4577 next = (Fnext_single_property_change
4578 (make_number (pos), Qauto_composed, it->string, limit));
4579 if (XINT (next) < XINT (limit))
4580 {
4581 /* The current point is auto-composed, but there exist
4582 characters not yet composed beyond the auto-composed
4583 region. There's a possiblity that the last
4584 characters in the region may be newly composed. */
4585 int charpos = XINT (next) - 1, bytepos, c;
4586
4587 if (STRINGP (it->string))
4588 {
4589 bytepos = string_char_to_byte (it->string, charpos);
4590 c = SDATA (it->string)[bytepos];
4591 }
4592 else
4593 {
4594 bytepos = CHAR_TO_BYTE (charpos);
4595 c = FETCH_BYTE (bytepos);
4596 }
4597 if (c != '\n')
4598 /* If the last character is not newline, it may be
4599 composed with the following characters. */
4600 val = Qnil, pos = charpos + 1;
4601 }
4602 }
4603 if (NILP (val))
4604 {
4605 int count = SPECPDL_INDEX ();
4606 Lisp_Object args[4];
4607
4608 args[0] = Vauto_composition_function;
4609 specbind (Qauto_composition_function, Qnil);
4610 args[1] = make_number (pos);
4611 args[2] = it->string;
4612 #ifdef USE_FONT_BACKEND
4613 if (enable_font_backend)
4614 {
4615 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4616 int c;
4617
4618 if (STRINGP (it->string))
4619 {
4620 EMACS_INT pos_byte = IT_STRING_BYTEPOS (*it);
4621 const unsigned char *s = SDATA (it->string) + pos_byte;
4622
4623 if (STRING_MULTIBYTE (it->string))
4624 it->c = STRING_CHAR (s, 0);
4625 else
4626 it->c = *s;
4627 }
4628 else
4629 {
4630 EMACS_INT pos_byte = IT_BYTEPOS (*it);
4631
4632 it->c = FETCH_CHAR (pos_byte);
4633 }
4634 args[3] = font_at (it->c, this_pos, face, it->w, it->string);
4635 }
4636 else
4637 #endif /* USE_FONT_BACKEND */
4638 args[3] = Qnil;
4639 safe_call (4, args);
4640 unbind_to (count, Qnil);
4641
4642 if (this_pos == pos)
4643 {
4644 val = Fget_char_property (args[1], Qauto_composed, it->string);
4645 /* Return HANDLED_RECOMPUTE_PROPS only if function composed
4646 something. This avoids an endless loop if they failed to
4647 fontify the text for which reason ever. */
4648 if (! NILP (val))
4649 handled = HANDLED_RECOMPUTE_PROPS;
4650 }
4651 else
4652 handled = HANDLED_RECOMPUTE_PROPS;
4653 }
4654 }
4655
4656 return handled;
4657 }
4658
4659 /* Set up iterator IT from `composition' property at its current
4660 position. Called from handle_stop. */
4661
4662 static enum prop_handled
4663 handle_composition_prop (it)
4664 struct it *it;
4665 {
4666 Lisp_Object prop, string;
4667 EMACS_INT pos, pos_byte, start, end;
4668 enum prop_handled handled = HANDLED_NORMALLY;
4669
4670 if (STRINGP (it->string))
4671 {
4672 pos = IT_STRING_CHARPOS (*it);
4673 pos_byte = IT_STRING_BYTEPOS (*it);
4674 string = it->string;
4675 }
4676 else
4677 {
4678 pos = IT_CHARPOS (*it);
4679 pos_byte = IT_BYTEPOS (*it);
4680 string = Qnil;
4681 }
4682
4683 /* If there's a valid composition and point is not inside of the
4684 composition (in the case that the composition is from the current
4685 buffer), draw a glyph composed from the composition components. */
4686 if (find_composition (pos, -1, &start, &end, &prop, string)
4687 && COMPOSITION_VALID_P (start, end, prop)
4688 && (STRINGP (it->string) || (PT <= start || PT >= end)))
4689 {
4690 int id;
4691
4692 if (start != pos)
4693 {
4694 if (STRINGP (it->string))
4695 pos_byte = string_char_to_byte (it->string, start);
4696 else
4697 pos_byte = CHAR_TO_BYTE (start);
4698 }
4699 id = get_composition_id (start, pos_byte, end - start, prop, string);
4700
4701 if (id >= 0)
4702 {
4703 struct composition *cmp = composition_table[id];
4704
4705 if (cmp->glyph_len == 0)
4706 {
4707 /* No glyph. */
4708 if (STRINGP (it->string))
4709 {
4710 IT_STRING_CHARPOS (*it) = end;
4711 IT_STRING_BYTEPOS (*it) = string_char_to_byte (it->string,
4712 end);
4713 }
4714 else
4715 {
4716 IT_CHARPOS (*it) = end;
4717 IT_BYTEPOS (*it) = CHAR_TO_BYTE (end);
4718 }
4719 return HANDLED_RECOMPUTE_PROPS;
4720 }
4721
4722 it->stop_charpos = end;
4723 push_it (it);
4724
4725 it->method = GET_FROM_COMPOSITION;
4726 it->cmp_id = id;
4727 it->cmp_len = COMPOSITION_LENGTH (prop);
4728 /* For a terminal, draw only the first (non-TAB) character
4729 of the components. */
4730 #ifdef USE_FONT_BACKEND
4731 if (composition_table[id]->method == COMPOSITION_WITH_GLYPH_STRING)
4732 {
4733 Lisp_Object lgstring = AREF (XHASH_TABLE (composition_hash_table)
4734 ->key_and_value,
4735 cmp->hash_index * 2);
4736
4737 it->c = XINT (LGLYPH_CHAR (LGSTRING_GLYPH (lgstring, 0)));
4738 }
4739 else
4740 #endif /* USE_FONT_BACKEND */
4741 {
4742 int i;
4743
4744 for (i = 0; i < cmp->glyph_len; i++)
4745 if ((it->c = COMPOSITION_GLYPH (composition_table[id], i))
4746 != '\t')
4747 break;
4748 }
4749 if (it->c == '\t')
4750 it->c = ' ';
4751 it->len = (STRINGP (it->string)
4752 ? string_char_to_byte (it->string, end)
4753 : CHAR_TO_BYTE (end)) - pos_byte;
4754 handled = HANDLED_RETURN;
4755 }
4756 }
4757
4758 return handled;
4759 }
4760
4761
4762 \f
4763 /***********************************************************************
4764 Overlay strings
4765 ***********************************************************************/
4766
4767 /* The following structure is used to record overlay strings for
4768 later sorting in load_overlay_strings. */
4769
4770 struct overlay_entry
4771 {
4772 Lisp_Object overlay;
4773 Lisp_Object string;
4774 int priority;
4775 int after_string_p;
4776 };
4777
4778
4779 /* Set up iterator IT from overlay strings at its current position.
4780 Called from handle_stop. */
4781
4782 static enum prop_handled
4783 handle_overlay_change (it)
4784 struct it *it;
4785 {
4786 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
4787 return HANDLED_RECOMPUTE_PROPS;
4788 else
4789 return HANDLED_NORMALLY;
4790 }
4791
4792
4793 /* Set up the next overlay string for delivery by IT, if there is an
4794 overlay string to deliver. Called by set_iterator_to_next when the
4795 end of the current overlay string is reached. If there are more
4796 overlay strings to display, IT->string and
4797 IT->current.overlay_string_index are set appropriately here.
4798 Otherwise IT->string is set to nil. */
4799
4800 static void
4801 next_overlay_string (it)
4802 struct it *it;
4803 {
4804 ++it->current.overlay_string_index;
4805 if (it->current.overlay_string_index == it->n_overlay_strings)
4806 {
4807 /* No more overlay strings. Restore IT's settings to what
4808 they were before overlay strings were processed, and
4809 continue to deliver from current_buffer. */
4810 int display_ellipsis_p = it->stack[it->sp - 1].display_ellipsis_p;
4811
4812 pop_it (it);
4813 xassert (it->sp > 0
4814 || it->method == GET_FROM_COMPOSITION
4815 || (NILP (it->string)
4816 && it->method == GET_FROM_BUFFER
4817 && it->stop_charpos >= BEGV
4818 && it->stop_charpos <= it->end_charpos));
4819 it->current.overlay_string_index = -1;
4820 it->n_overlay_strings = 0;
4821
4822 /* If we're at the end of the buffer, record that we have
4823 processed the overlay strings there already, so that
4824 next_element_from_buffer doesn't try it again. */
4825 if (IT_CHARPOS (*it) >= it->end_charpos)
4826 it->overlay_strings_at_end_processed_p = 1;
4827
4828 /* If we have to display `...' for invisible text, set
4829 the iterator up for that. */
4830 if (display_ellipsis_p)
4831 setup_for_ellipsis (it, 0);
4832 }
4833 else
4834 {
4835 /* There are more overlay strings to process. If
4836 IT->current.overlay_string_index has advanced to a position
4837 where we must load IT->overlay_strings with more strings, do
4838 it. */
4839 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
4840
4841 if (it->current.overlay_string_index && i == 0)
4842 load_overlay_strings (it, 0);
4843
4844 /* Initialize IT to deliver display elements from the overlay
4845 string. */
4846 it->string = it->overlay_strings[i];
4847 it->multibyte_p = STRING_MULTIBYTE (it->string);
4848 SET_TEXT_POS (it->current.string_pos, 0, 0);
4849 it->method = GET_FROM_STRING;
4850 it->stop_charpos = 0;
4851 }
4852
4853 CHECK_IT (it);
4854 }
4855
4856
4857 /* Compare two overlay_entry structures E1 and E2. Used as a
4858 comparison function for qsort in load_overlay_strings. Overlay
4859 strings for the same position are sorted so that
4860
4861 1. All after-strings come in front of before-strings, except
4862 when they come from the same overlay.
4863
4864 2. Within after-strings, strings are sorted so that overlay strings
4865 from overlays with higher priorities come first.
4866
4867 2. Within before-strings, strings are sorted so that overlay
4868 strings from overlays with higher priorities come last.
4869
4870 Value is analogous to strcmp. */
4871
4872
4873 static int
4874 compare_overlay_entries (e1, e2)
4875 void *e1, *e2;
4876 {
4877 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
4878 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
4879 int result;
4880
4881 if (entry1->after_string_p != entry2->after_string_p)
4882 {
4883 /* Let after-strings appear in front of before-strings if
4884 they come from different overlays. */
4885 if (EQ (entry1->overlay, entry2->overlay))
4886 result = entry1->after_string_p ? 1 : -1;
4887 else
4888 result = entry1->after_string_p ? -1 : 1;
4889 }
4890 else if (entry1->after_string_p)
4891 /* After-strings sorted in order of decreasing priority. */
4892 result = entry2->priority - entry1->priority;
4893 else
4894 /* Before-strings sorted in order of increasing priority. */
4895 result = entry1->priority - entry2->priority;
4896
4897 return result;
4898 }
4899
4900
4901 /* Load the vector IT->overlay_strings with overlay strings from IT's
4902 current buffer position, or from CHARPOS if that is > 0. Set
4903 IT->n_overlays to the total number of overlay strings found.
4904
4905 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
4906 a time. On entry into load_overlay_strings,
4907 IT->current.overlay_string_index gives the number of overlay
4908 strings that have already been loaded by previous calls to this
4909 function.
4910
4911 IT->add_overlay_start contains an additional overlay start
4912 position to consider for taking overlay strings from, if non-zero.
4913 This position comes into play when the overlay has an `invisible'
4914 property, and both before and after-strings. When we've skipped to
4915 the end of the overlay, because of its `invisible' property, we
4916 nevertheless want its before-string to appear.
4917 IT->add_overlay_start will contain the overlay start position
4918 in this case.
4919
4920 Overlay strings are sorted so that after-string strings come in
4921 front of before-string strings. Within before and after-strings,
4922 strings are sorted by overlay priority. See also function
4923 compare_overlay_entries. */
4924
4925 static void
4926 load_overlay_strings (it, charpos)
4927 struct it *it;
4928 int charpos;
4929 {
4930 extern Lisp_Object Qafter_string, Qbefore_string, Qwindow, Qpriority;
4931 Lisp_Object overlay, window, str, invisible;
4932 struct Lisp_Overlay *ov;
4933 int start, end;
4934 int size = 20;
4935 int n = 0, i, j, invis_p;
4936 struct overlay_entry *entries
4937 = (struct overlay_entry *) alloca (size * sizeof *entries);
4938
4939 if (charpos <= 0)
4940 charpos = IT_CHARPOS (*it);
4941
4942 /* Append the overlay string STRING of overlay OVERLAY to vector
4943 `entries' which has size `size' and currently contains `n'
4944 elements. AFTER_P non-zero means STRING is an after-string of
4945 OVERLAY. */
4946 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
4947 do \
4948 { \
4949 Lisp_Object priority; \
4950 \
4951 if (n == size) \
4952 { \
4953 int new_size = 2 * size; \
4954 struct overlay_entry *old = entries; \
4955 entries = \
4956 (struct overlay_entry *) alloca (new_size \
4957 * sizeof *entries); \
4958 bcopy (old, entries, size * sizeof *entries); \
4959 size = new_size; \
4960 } \
4961 \
4962 entries[n].string = (STRING); \
4963 entries[n].overlay = (OVERLAY); \
4964 priority = Foverlay_get ((OVERLAY), Qpriority); \
4965 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
4966 entries[n].after_string_p = (AFTER_P); \
4967 ++n; \
4968 } \
4969 while (0)
4970
4971 /* Process overlay before the overlay center. */
4972 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
4973 {
4974 XSETMISC (overlay, ov);
4975 xassert (OVERLAYP (overlay));
4976 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4977 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4978
4979 if (end < charpos)
4980 break;
4981
4982 /* Skip this overlay if it doesn't start or end at IT's current
4983 position. */
4984 if (end != charpos && start != charpos)
4985 continue;
4986
4987 /* Skip this overlay if it doesn't apply to IT->w. */
4988 window = Foverlay_get (overlay, Qwindow);
4989 if (WINDOWP (window) && XWINDOW (window) != it->w)
4990 continue;
4991
4992 /* If the text ``under'' the overlay is invisible, both before-
4993 and after-strings from this overlay are visible; start and
4994 end position are indistinguishable. */
4995 invisible = Foverlay_get (overlay, Qinvisible);
4996 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4997
4998 /* If overlay has a non-empty before-string, record it. */
4999 if ((start == charpos || (end == charpos && invis_p))
5000 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5001 && SCHARS (str))
5002 RECORD_OVERLAY_STRING (overlay, str, 0);
5003
5004 /* If overlay has a non-empty after-string, record it. */
5005 if ((end == charpos || (start == charpos && invis_p))
5006 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5007 && SCHARS (str))
5008 RECORD_OVERLAY_STRING (overlay, str, 1);
5009 }
5010
5011 /* Process overlays after the overlay center. */
5012 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
5013 {
5014 XSETMISC (overlay, ov);
5015 xassert (OVERLAYP (overlay));
5016 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5017 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5018
5019 if (start > charpos)
5020 break;
5021
5022 /* Skip this overlay if it doesn't start or end at IT's current
5023 position. */
5024 if (end != charpos && start != charpos)
5025 continue;
5026
5027 /* Skip this overlay if it doesn't apply to IT->w. */
5028 window = Foverlay_get (overlay, Qwindow);
5029 if (WINDOWP (window) && XWINDOW (window) != it->w)
5030 continue;
5031
5032 /* If the text ``under'' the overlay is invisible, it has a zero
5033 dimension, and both before- and after-strings apply. */
5034 invisible = Foverlay_get (overlay, Qinvisible);
5035 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5036
5037 /* If overlay has a non-empty before-string, record it. */
5038 if ((start == charpos || (end == charpos && invis_p))
5039 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5040 && SCHARS (str))
5041 RECORD_OVERLAY_STRING (overlay, str, 0);
5042
5043 /* If overlay has a non-empty after-string, record it. */
5044 if ((end == charpos || (start == charpos && invis_p))
5045 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5046 && SCHARS (str))
5047 RECORD_OVERLAY_STRING (overlay, str, 1);
5048 }
5049
5050 #undef RECORD_OVERLAY_STRING
5051
5052 /* Sort entries. */
5053 if (n > 1)
5054 qsort (entries, n, sizeof *entries, compare_overlay_entries);
5055
5056 /* Record the total number of strings to process. */
5057 it->n_overlay_strings = n;
5058
5059 /* IT->current.overlay_string_index is the number of overlay strings
5060 that have already been consumed by IT. Copy some of the
5061 remaining overlay strings to IT->overlay_strings. */
5062 i = 0;
5063 j = it->current.overlay_string_index;
5064 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
5065 {
5066 it->overlay_strings[i] = entries[j].string;
5067 it->string_overlays[i++] = entries[j++].overlay;
5068 }
5069
5070 CHECK_IT (it);
5071 }
5072
5073
5074 /* Get the first chunk of overlay strings at IT's current buffer
5075 position, or at CHARPOS if that is > 0. Value is non-zero if at
5076 least one overlay string was found. */
5077
5078 static int
5079 get_overlay_strings_1 (it, charpos, compute_stop_p)
5080 struct it *it;
5081 int charpos;
5082 {
5083 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
5084 process. This fills IT->overlay_strings with strings, and sets
5085 IT->n_overlay_strings to the total number of strings to process.
5086 IT->pos.overlay_string_index has to be set temporarily to zero
5087 because load_overlay_strings needs this; it must be set to -1
5088 when no overlay strings are found because a zero value would
5089 indicate a position in the first overlay string. */
5090 it->current.overlay_string_index = 0;
5091 load_overlay_strings (it, charpos);
5092
5093 /* If we found overlay strings, set up IT to deliver display
5094 elements from the first one. Otherwise set up IT to deliver
5095 from current_buffer. */
5096 if (it->n_overlay_strings)
5097 {
5098 /* Make sure we know settings in current_buffer, so that we can
5099 restore meaningful values when we're done with the overlay
5100 strings. */
5101 if (compute_stop_p)
5102 compute_stop_pos (it);
5103 xassert (it->face_id >= 0);
5104
5105 /* Save IT's settings. They are restored after all overlay
5106 strings have been processed. */
5107 xassert (!compute_stop_p || it->sp == 0);
5108 push_it (it);
5109
5110 /* Set up IT to deliver display elements from the first overlay
5111 string. */
5112 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5113 it->string = it->overlay_strings[0];
5114 it->from_overlay = Qnil;
5115 it->stop_charpos = 0;
5116 xassert (STRINGP (it->string));
5117 it->end_charpos = SCHARS (it->string);
5118 it->multibyte_p = STRING_MULTIBYTE (it->string);
5119 it->method = GET_FROM_STRING;
5120 return 1;
5121 }
5122
5123 it->current.overlay_string_index = -1;
5124 return 0;
5125 }
5126
5127 static int
5128 get_overlay_strings (it, charpos)
5129 struct it *it;
5130 int charpos;
5131 {
5132 it->string = Qnil;
5133 it->method = GET_FROM_BUFFER;
5134
5135 (void) get_overlay_strings_1 (it, charpos, 1);
5136
5137 CHECK_IT (it);
5138
5139 /* Value is non-zero if we found at least one overlay string. */
5140 return STRINGP (it->string);
5141 }
5142
5143
5144 \f
5145 /***********************************************************************
5146 Saving and restoring state
5147 ***********************************************************************/
5148
5149 /* Save current settings of IT on IT->stack. Called, for example,
5150 before setting up IT for an overlay string, to be able to restore
5151 IT's settings to what they were after the overlay string has been
5152 processed. */
5153
5154 static void
5155 push_it (it)
5156 struct it *it;
5157 {
5158 struct iterator_stack_entry *p;
5159
5160 xassert (it->sp < IT_STACK_SIZE);
5161 p = it->stack + it->sp;
5162
5163 p->stop_charpos = it->stop_charpos;
5164 xassert (it->face_id >= 0);
5165 p->face_id = it->face_id;
5166 p->string = it->string;
5167 p->method = it->method;
5168 p->from_overlay = it->from_overlay;
5169 switch (p->method)
5170 {
5171 case GET_FROM_IMAGE:
5172 p->u.image.object = it->object;
5173 p->u.image.image_id = it->image_id;
5174 p->u.image.slice = it->slice;
5175 break;
5176 case GET_FROM_COMPOSITION:
5177 p->u.comp.object = it->object;
5178 p->u.comp.c = it->c;
5179 p->u.comp.len = it->len;
5180 p->u.comp.cmp_id = it->cmp_id;
5181 p->u.comp.cmp_len = it->cmp_len;
5182 break;
5183 case GET_FROM_STRETCH:
5184 p->u.stretch.object = it->object;
5185 break;
5186 }
5187 p->position = it->position;
5188 p->current = it->current;
5189 p->end_charpos = it->end_charpos;
5190 p->string_nchars = it->string_nchars;
5191 p->area = it->area;
5192 p->multibyte_p = it->multibyte_p;
5193 p->space_width = it->space_width;
5194 p->font_height = it->font_height;
5195 p->voffset = it->voffset;
5196 p->string_from_display_prop_p = it->string_from_display_prop_p;
5197 p->display_ellipsis_p = 0;
5198 ++it->sp;
5199 }
5200
5201
5202 /* Restore IT's settings from IT->stack. Called, for example, when no
5203 more overlay strings must be processed, and we return to delivering
5204 display elements from a buffer, or when the end of a string from a
5205 `display' property is reached and we return to delivering display
5206 elements from an overlay string, or from a buffer. */
5207
5208 static void
5209 pop_it (it)
5210 struct it *it;
5211 {
5212 struct iterator_stack_entry *p;
5213
5214 xassert (it->sp > 0);
5215 --it->sp;
5216 p = it->stack + it->sp;
5217 it->stop_charpos = p->stop_charpos;
5218 it->face_id = p->face_id;
5219 it->current = p->current;
5220 it->position = p->position;
5221 it->string = p->string;
5222 it->from_overlay = p->from_overlay;
5223 if (NILP (it->string))
5224 SET_TEXT_POS (it->current.string_pos, -1, -1);
5225 it->method = p->method;
5226 switch (it->method)
5227 {
5228 case GET_FROM_IMAGE:
5229 it->image_id = p->u.image.image_id;
5230 it->object = p->u.image.object;
5231 it->slice = p->u.image.slice;
5232 break;
5233 case GET_FROM_COMPOSITION:
5234 it->object = p->u.comp.object;
5235 it->c = p->u.comp.c;
5236 it->len = p->u.comp.len;
5237 it->cmp_id = p->u.comp.cmp_id;
5238 it->cmp_len = p->u.comp.cmp_len;
5239 break;
5240 case GET_FROM_STRETCH:
5241 it->object = p->u.comp.object;
5242 break;
5243 case GET_FROM_BUFFER:
5244 it->object = it->w->buffer;
5245 break;
5246 case GET_FROM_STRING:
5247 it->object = it->string;
5248 break;
5249 }
5250 it->end_charpos = p->end_charpos;
5251 it->string_nchars = p->string_nchars;
5252 it->area = p->area;
5253 it->multibyte_p = p->multibyte_p;
5254 it->space_width = p->space_width;
5255 it->font_height = p->font_height;
5256 it->voffset = p->voffset;
5257 it->string_from_display_prop_p = p->string_from_display_prop_p;
5258 }
5259
5260
5261 \f
5262 /***********************************************************************
5263 Moving over lines
5264 ***********************************************************************/
5265
5266 /* Set IT's current position to the previous line start. */
5267
5268 static void
5269 back_to_previous_line_start (it)
5270 struct it *it;
5271 {
5272 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
5273 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
5274 }
5275
5276
5277 /* Move IT to the next line start.
5278
5279 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
5280 we skipped over part of the text (as opposed to moving the iterator
5281 continuously over the text). Otherwise, don't change the value
5282 of *SKIPPED_P.
5283
5284 Newlines may come from buffer text, overlay strings, or strings
5285 displayed via the `display' property. That's the reason we can't
5286 simply use find_next_newline_no_quit.
5287
5288 Note that this function may not skip over invisible text that is so
5289 because of text properties and immediately follows a newline. If
5290 it would, function reseat_at_next_visible_line_start, when called
5291 from set_iterator_to_next, would effectively make invisible
5292 characters following a newline part of the wrong glyph row, which
5293 leads to wrong cursor motion. */
5294
5295 static int
5296 forward_to_next_line_start (it, skipped_p)
5297 struct it *it;
5298 int *skipped_p;
5299 {
5300 int old_selective, newline_found_p, n;
5301 const int MAX_NEWLINE_DISTANCE = 500;
5302
5303 /* If already on a newline, just consume it to avoid unintended
5304 skipping over invisible text below. */
5305 if (it->what == IT_CHARACTER
5306 && it->c == '\n'
5307 && CHARPOS (it->position) == IT_CHARPOS (*it))
5308 {
5309 set_iterator_to_next (it, 0);
5310 it->c = 0;
5311 return 1;
5312 }
5313
5314 /* Don't handle selective display in the following. It's (a)
5315 unnecessary because it's done by the caller, and (b) leads to an
5316 infinite recursion because next_element_from_ellipsis indirectly
5317 calls this function. */
5318 old_selective = it->selective;
5319 it->selective = 0;
5320
5321 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
5322 from buffer text. */
5323 for (n = newline_found_p = 0;
5324 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
5325 n += STRINGP (it->string) ? 0 : 1)
5326 {
5327 if (!get_next_display_element (it))
5328 return 0;
5329 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
5330 set_iterator_to_next (it, 0);
5331 }
5332
5333 /* If we didn't find a newline near enough, see if we can use a
5334 short-cut. */
5335 if (!newline_found_p)
5336 {
5337 int start = IT_CHARPOS (*it);
5338 int limit = find_next_newline_no_quit (start, 1);
5339 Lisp_Object pos;
5340
5341 xassert (!STRINGP (it->string));
5342
5343 /* If there isn't any `display' property in sight, and no
5344 overlays, we can just use the position of the newline in
5345 buffer text. */
5346 if (it->stop_charpos >= limit
5347 || ((pos = Fnext_single_property_change (make_number (start),
5348 Qdisplay,
5349 Qnil, make_number (limit)),
5350 NILP (pos))
5351 && next_overlay_change (start) == ZV))
5352 {
5353 IT_CHARPOS (*it) = limit;
5354 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
5355 *skipped_p = newline_found_p = 1;
5356 }
5357 else
5358 {
5359 while (get_next_display_element (it)
5360 && !newline_found_p)
5361 {
5362 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
5363 set_iterator_to_next (it, 0);
5364 }
5365 }
5366 }
5367
5368 it->selective = old_selective;
5369 return newline_found_p;
5370 }
5371
5372
5373 /* Set IT's current position to the previous visible line start. Skip
5374 invisible text that is so either due to text properties or due to
5375 selective display. Caution: this does not change IT->current_x and
5376 IT->hpos. */
5377
5378 static void
5379 back_to_previous_visible_line_start (it)
5380 struct it *it;
5381 {
5382 while (IT_CHARPOS (*it) > BEGV)
5383 {
5384 back_to_previous_line_start (it);
5385
5386 if (IT_CHARPOS (*it) <= BEGV)
5387 break;
5388
5389 /* If selective > 0, then lines indented more than that values
5390 are invisible. */
5391 if (it->selective > 0
5392 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5393 (double) it->selective)) /* iftc */
5394 continue;
5395
5396 /* Check the newline before point for invisibility. */
5397 {
5398 Lisp_Object prop;
5399 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
5400 Qinvisible, it->window);
5401 if (TEXT_PROP_MEANS_INVISIBLE (prop))
5402 continue;
5403 }
5404
5405 if (IT_CHARPOS (*it) <= BEGV)
5406 break;
5407
5408 {
5409 struct it it2;
5410 int pos;
5411 int beg, end;
5412 Lisp_Object val, overlay;
5413
5414 /* If newline is part of a composition, continue from start of composition */
5415 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
5416 && beg < IT_CHARPOS (*it))
5417 goto replaced;
5418
5419 /* If newline is replaced by a display property, find start of overlay
5420 or interval and continue search from that point. */
5421 it2 = *it;
5422 pos = --IT_CHARPOS (it2);
5423 --IT_BYTEPOS (it2);
5424 it2.sp = 0;
5425 if (handle_display_prop (&it2) == HANDLED_RETURN
5426 && !NILP (val = get_char_property_and_overlay
5427 (make_number (pos), Qdisplay, Qnil, &overlay))
5428 && (OVERLAYP (overlay)
5429 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
5430 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
5431 goto replaced;
5432
5433 /* Newline is not replaced by anything -- so we are done. */
5434 break;
5435
5436 replaced:
5437 if (beg < BEGV)
5438 beg = BEGV;
5439 IT_CHARPOS (*it) = beg;
5440 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
5441 }
5442 }
5443
5444 it->continuation_lines_width = 0;
5445
5446 xassert (IT_CHARPOS (*it) >= BEGV);
5447 xassert (IT_CHARPOS (*it) == BEGV
5448 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5449 CHECK_IT (it);
5450 }
5451
5452
5453 /* Reseat iterator IT at the previous visible line start. Skip
5454 invisible text that is so either due to text properties or due to
5455 selective display. At the end, update IT's overlay information,
5456 face information etc. */
5457
5458 void
5459 reseat_at_previous_visible_line_start (it)
5460 struct it *it;
5461 {
5462 back_to_previous_visible_line_start (it);
5463 reseat (it, it->current.pos, 1);
5464 CHECK_IT (it);
5465 }
5466
5467
5468 /* Reseat iterator IT on the next visible line start in the current
5469 buffer. ON_NEWLINE_P non-zero means position IT on the newline
5470 preceding the line start. Skip over invisible text that is so
5471 because of selective display. Compute faces, overlays etc at the
5472 new position. Note that this function does not skip over text that
5473 is invisible because of text properties. */
5474
5475 static void
5476 reseat_at_next_visible_line_start (it, on_newline_p)
5477 struct it *it;
5478 int on_newline_p;
5479 {
5480 int newline_found_p, skipped_p = 0;
5481
5482 newline_found_p = forward_to_next_line_start (it, &skipped_p);
5483
5484 /* Skip over lines that are invisible because they are indented
5485 more than the value of IT->selective. */
5486 if (it->selective > 0)
5487 while (IT_CHARPOS (*it) < ZV
5488 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5489 (double) it->selective)) /* iftc */
5490 {
5491 xassert (IT_BYTEPOS (*it) == BEGV
5492 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5493 newline_found_p = forward_to_next_line_start (it, &skipped_p);
5494 }
5495
5496 /* Position on the newline if that's what's requested. */
5497 if (on_newline_p && newline_found_p)
5498 {
5499 if (STRINGP (it->string))
5500 {
5501 if (IT_STRING_CHARPOS (*it) > 0)
5502 {
5503 --IT_STRING_CHARPOS (*it);
5504 --IT_STRING_BYTEPOS (*it);
5505 }
5506 }
5507 else if (IT_CHARPOS (*it) > BEGV)
5508 {
5509 --IT_CHARPOS (*it);
5510 --IT_BYTEPOS (*it);
5511 reseat (it, it->current.pos, 0);
5512 }
5513 }
5514 else if (skipped_p)
5515 reseat (it, it->current.pos, 0);
5516
5517 CHECK_IT (it);
5518 }
5519
5520
5521 \f
5522 /***********************************************************************
5523 Changing an iterator's position
5524 ***********************************************************************/
5525
5526 /* Change IT's current position to POS in current_buffer. If FORCE_P
5527 is non-zero, always check for text properties at the new position.
5528 Otherwise, text properties are only looked up if POS >=
5529 IT->check_charpos of a property. */
5530
5531 static void
5532 reseat (it, pos, force_p)
5533 struct it *it;
5534 struct text_pos pos;
5535 int force_p;
5536 {
5537 int original_pos = IT_CHARPOS (*it);
5538
5539 reseat_1 (it, pos, 0);
5540
5541 /* Determine where to check text properties. Avoid doing it
5542 where possible because text property lookup is very expensive. */
5543 if (force_p
5544 || CHARPOS (pos) > it->stop_charpos
5545 || CHARPOS (pos) < original_pos)
5546 handle_stop (it);
5547
5548 CHECK_IT (it);
5549 }
5550
5551
5552 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
5553 IT->stop_pos to POS, also. */
5554
5555 static void
5556 reseat_1 (it, pos, set_stop_p)
5557 struct it *it;
5558 struct text_pos pos;
5559 int set_stop_p;
5560 {
5561 /* Don't call this function when scanning a C string. */
5562 xassert (it->s == NULL);
5563
5564 /* POS must be a reasonable value. */
5565 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
5566
5567 it->current.pos = it->position = pos;
5568 it->end_charpos = ZV;
5569 it->dpvec = NULL;
5570 it->current.dpvec_index = -1;
5571 it->current.overlay_string_index = -1;
5572 IT_STRING_CHARPOS (*it) = -1;
5573 IT_STRING_BYTEPOS (*it) = -1;
5574 it->string = Qnil;
5575 it->method = GET_FROM_BUFFER;
5576 it->object = it->w->buffer;
5577 it->area = TEXT_AREA;
5578 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
5579 it->sp = 0;
5580 it->string_from_display_prop_p = 0;
5581 it->face_before_selective_p = 0;
5582
5583 if (set_stop_p)
5584 it->stop_charpos = CHARPOS (pos);
5585 }
5586
5587
5588 /* Set up IT for displaying a string, starting at CHARPOS in window W.
5589 If S is non-null, it is a C string to iterate over. Otherwise,
5590 STRING gives a Lisp string to iterate over.
5591
5592 If PRECISION > 0, don't return more then PRECISION number of
5593 characters from the string.
5594
5595 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
5596 characters have been returned. FIELD_WIDTH < 0 means an infinite
5597 field width.
5598
5599 MULTIBYTE = 0 means disable processing of multibyte characters,
5600 MULTIBYTE > 0 means enable it,
5601 MULTIBYTE < 0 means use IT->multibyte_p.
5602
5603 IT must be initialized via a prior call to init_iterator before
5604 calling this function. */
5605
5606 static void
5607 reseat_to_string (it, s, string, charpos, precision, field_width, multibyte)
5608 struct it *it;
5609 unsigned char *s;
5610 Lisp_Object string;
5611 int charpos;
5612 int precision, field_width, multibyte;
5613 {
5614 /* No region in strings. */
5615 it->region_beg_charpos = it->region_end_charpos = -1;
5616
5617 /* No text property checks performed by default, but see below. */
5618 it->stop_charpos = -1;
5619
5620 /* Set iterator position and end position. */
5621 bzero (&it->current, sizeof it->current);
5622 it->current.overlay_string_index = -1;
5623 it->current.dpvec_index = -1;
5624 xassert (charpos >= 0);
5625
5626 /* If STRING is specified, use its multibyteness, otherwise use the
5627 setting of MULTIBYTE, if specified. */
5628 if (multibyte >= 0)
5629 it->multibyte_p = multibyte > 0;
5630
5631 if (s == NULL)
5632 {
5633 xassert (STRINGP (string));
5634 it->string = string;
5635 it->s = NULL;
5636 it->end_charpos = it->string_nchars = SCHARS (string);
5637 it->method = GET_FROM_STRING;
5638 it->current.string_pos = string_pos (charpos, string);
5639 }
5640 else
5641 {
5642 it->s = s;
5643 it->string = Qnil;
5644
5645 /* Note that we use IT->current.pos, not it->current.string_pos,
5646 for displaying C strings. */
5647 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
5648 if (it->multibyte_p)
5649 {
5650 it->current.pos = c_string_pos (charpos, s, 1);
5651 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
5652 }
5653 else
5654 {
5655 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
5656 it->end_charpos = it->string_nchars = strlen (s);
5657 }
5658
5659 it->method = GET_FROM_C_STRING;
5660 }
5661
5662 /* PRECISION > 0 means don't return more than PRECISION characters
5663 from the string. */
5664 if (precision > 0 && it->end_charpos - charpos > precision)
5665 it->end_charpos = it->string_nchars = charpos + precision;
5666
5667 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
5668 characters have been returned. FIELD_WIDTH == 0 means don't pad,
5669 FIELD_WIDTH < 0 means infinite field width. This is useful for
5670 padding with `-' at the end of a mode line. */
5671 if (field_width < 0)
5672 field_width = INFINITY;
5673 if (field_width > it->end_charpos - charpos)
5674 it->end_charpos = charpos + field_width;
5675
5676 /* Use the standard display table for displaying strings. */
5677 if (DISP_TABLE_P (Vstandard_display_table))
5678 it->dp = XCHAR_TABLE (Vstandard_display_table);
5679
5680 it->stop_charpos = charpos;
5681 CHECK_IT (it);
5682 }
5683
5684
5685 \f
5686 /***********************************************************************
5687 Iteration
5688 ***********************************************************************/
5689
5690 /* Map enum it_method value to corresponding next_element_from_* function. */
5691
5692 static int (* get_next_element[NUM_IT_METHODS]) P_ ((struct it *it)) =
5693 {
5694 next_element_from_buffer,
5695 next_element_from_display_vector,
5696 next_element_from_composition,
5697 next_element_from_string,
5698 next_element_from_c_string,
5699 next_element_from_image,
5700 next_element_from_stretch
5701 };
5702
5703
5704 /* Load IT's display element fields with information about the next
5705 display element from the current position of IT. Value is zero if
5706 end of buffer (or C string) is reached. */
5707
5708 static struct frame *last_escape_glyph_frame = NULL;
5709 static unsigned last_escape_glyph_face_id = (1 << FACE_ID_BITS);
5710 static int last_escape_glyph_merged_face_id = 0;
5711
5712 int
5713 get_next_display_element (it)
5714 struct it *it;
5715 {
5716 /* Non-zero means that we found a display element. Zero means that
5717 we hit the end of what we iterate over. Performance note: the
5718 function pointer `method' used here turns out to be faster than
5719 using a sequence of if-statements. */
5720 int success_p;
5721
5722 get_next:
5723 success_p = (*get_next_element[it->method]) (it);
5724
5725 if (it->what == IT_CHARACTER)
5726 {
5727 /* Map via display table or translate control characters.
5728 IT->c, IT->len etc. have been set to the next character by
5729 the function call above. If we have a display table, and it
5730 contains an entry for IT->c, translate it. Don't do this if
5731 IT->c itself comes from a display table, otherwise we could
5732 end up in an infinite recursion. (An alternative could be to
5733 count the recursion depth of this function and signal an
5734 error when a certain maximum depth is reached.) Is it worth
5735 it? */
5736 if (success_p && it->dpvec == NULL)
5737 {
5738 Lisp_Object dv;
5739
5740 if (it->dp
5741 && (dv = DISP_CHAR_VECTOR (it->dp, it->c),
5742 VECTORP (dv)))
5743 {
5744 struct Lisp_Vector *v = XVECTOR (dv);
5745
5746 /* Return the first character from the display table
5747 entry, if not empty. If empty, don't display the
5748 current character. */
5749 if (v->size)
5750 {
5751 it->dpvec_char_len = it->len;
5752 it->dpvec = v->contents;
5753 it->dpend = v->contents + v->size;
5754 it->current.dpvec_index = 0;
5755 it->dpvec_face_id = -1;
5756 it->saved_face_id = it->face_id;
5757 it->method = GET_FROM_DISPLAY_VECTOR;
5758 it->ellipsis_p = 0;
5759 }
5760 else
5761 {
5762 set_iterator_to_next (it, 0);
5763 }
5764 goto get_next;
5765 }
5766
5767 /* Translate control characters into `\003' or `^C' form.
5768 Control characters coming from a display table entry are
5769 currently not translated because we use IT->dpvec to hold
5770 the translation. This could easily be changed but I
5771 don't believe that it is worth doing.
5772
5773 If it->multibyte_p is nonzero, non-printable non-ASCII
5774 characters are also translated to octal form.
5775
5776 If it->multibyte_p is zero, eight-bit characters that
5777 don't have corresponding multibyte char code are also
5778 translated to octal form. */
5779 else if ((it->c < ' '
5780 ? (it->area != TEXT_AREA
5781 /* In mode line, treat \n, \t like other crl chars. */
5782 || (it->c != '\t'
5783 && it->glyph_row && it->glyph_row->mode_line_p)
5784 || (it->c != '\n' && it->c != '\t'))
5785 : (it->multibyte_p
5786 ? (!CHAR_PRINTABLE_P (it->c)
5787 || (!NILP (Vnobreak_char_display)
5788 && (it->c == 0xA0 /* NO-BREAK SPACE */
5789 || it->c == 0xAD /* SOFT HYPHEN */)))
5790 : (it->c >= 127
5791 && (! unibyte_display_via_language_environment
5792 || (UNIBYTE_CHAR_HAS_MULTIBYTE_P (it->c)))))))
5793 {
5794 /* IT->c is a control character which must be displayed
5795 either as '\003' or as `^C' where the '\\' and '^'
5796 can be defined in the display table. Fill
5797 IT->ctl_chars with glyphs for what we have to
5798 display. Then, set IT->dpvec to these glyphs. */
5799 GLYPH g;
5800 int ctl_len;
5801 int face_id, lface_id = 0 ;
5802 GLYPH escape_glyph;
5803
5804 /* Handle control characters with ^. */
5805
5806 if (it->c < 128 && it->ctl_arrow_p)
5807 {
5808 g = '^'; /* default glyph for Control */
5809 /* Set IT->ctl_chars[0] to the glyph for `^'. */
5810 if (it->dp
5811 && INTEGERP (DISP_CTRL_GLYPH (it->dp))
5812 && GLYPH_CHAR_VALID_P (XINT (DISP_CTRL_GLYPH (it->dp))))
5813 {
5814 g = XINT (DISP_CTRL_GLYPH (it->dp));
5815 lface_id = FAST_GLYPH_FACE (g);
5816 }
5817 if (lface_id)
5818 {
5819 g = FAST_GLYPH_CHAR (g);
5820 face_id = merge_faces (it->f, Qt, lface_id,
5821 it->face_id);
5822 }
5823 else if (it->f == last_escape_glyph_frame
5824 && it->face_id == last_escape_glyph_face_id)
5825 {
5826 face_id = last_escape_glyph_merged_face_id;
5827 }
5828 else
5829 {
5830 /* Merge the escape-glyph face into the current face. */
5831 face_id = merge_faces (it->f, Qescape_glyph, 0,
5832 it->face_id);
5833 last_escape_glyph_frame = it->f;
5834 last_escape_glyph_face_id = it->face_id;
5835 last_escape_glyph_merged_face_id = face_id;
5836 }
5837
5838 XSETINT (it->ctl_chars[0], g);
5839 g = it->c ^ 0100;
5840 XSETINT (it->ctl_chars[1], g);
5841 ctl_len = 2;
5842 goto display_control;
5843 }
5844
5845 /* Handle non-break space in the mode where it only gets
5846 highlighting. */
5847
5848 if (EQ (Vnobreak_char_display, Qt)
5849 && it->c == 0xA0)
5850 {
5851 /* Merge the no-break-space face into the current face. */
5852 face_id = merge_faces (it->f, Qnobreak_space, 0,
5853 it->face_id);
5854
5855 g = it->c = ' ';
5856 XSETINT (it->ctl_chars[0], g);
5857 ctl_len = 1;
5858 goto display_control;
5859 }
5860
5861 /* Handle sequences that start with the "escape glyph". */
5862
5863 /* the default escape glyph is \. */
5864 escape_glyph = '\\';
5865
5866 if (it->dp
5867 && INTEGERP (DISP_ESCAPE_GLYPH (it->dp))
5868 && GLYPH_CHAR_VALID_P (XFASTINT (DISP_ESCAPE_GLYPH (it->dp))))
5869 {
5870 escape_glyph = XFASTINT (DISP_ESCAPE_GLYPH (it->dp));
5871 lface_id = FAST_GLYPH_FACE (escape_glyph);
5872 }
5873 if (lface_id)
5874 {
5875 /* The display table specified a face.
5876 Merge it into face_id and also into escape_glyph. */
5877 escape_glyph = FAST_GLYPH_CHAR (escape_glyph);
5878 face_id = merge_faces (it->f, Qt, lface_id,
5879 it->face_id);
5880 }
5881 else if (it->f == last_escape_glyph_frame
5882 && it->face_id == last_escape_glyph_face_id)
5883 {
5884 face_id = last_escape_glyph_merged_face_id;
5885 }
5886 else
5887 {
5888 /* Merge the escape-glyph face into the current face. */
5889 face_id = merge_faces (it->f, Qescape_glyph, 0,
5890 it->face_id);
5891 last_escape_glyph_frame = it->f;
5892 last_escape_glyph_face_id = it->face_id;
5893 last_escape_glyph_merged_face_id = face_id;
5894 }
5895
5896 /* Handle soft hyphens in the mode where they only get
5897 highlighting. */
5898
5899 if (EQ (Vnobreak_char_display, Qt)
5900 && it->c == 0xAD)
5901 {
5902 g = it->c = '-';
5903 XSETINT (it->ctl_chars[0], g);
5904 ctl_len = 1;
5905 goto display_control;
5906 }
5907
5908 /* Handle non-break space and soft hyphen
5909 with the escape glyph. */
5910
5911 if (it->c == 0xA0 || it->c == 0xAD)
5912 {
5913 XSETINT (it->ctl_chars[0], escape_glyph);
5914 g = it->c = (it->c == 0xA0 ? ' ' : '-');
5915 XSETINT (it->ctl_chars[1], g);
5916 ctl_len = 2;
5917 goto display_control;
5918 }
5919
5920 {
5921 unsigned char str[MAX_MULTIBYTE_LENGTH];
5922 int len;
5923 int i;
5924
5925 /* Set IT->ctl_chars[0] to the glyph for `\\'. */
5926 if (CHAR_BYTE8_P (it->c))
5927 {
5928 str[0] = CHAR_TO_BYTE8 (it->c);
5929 len = 1;
5930 }
5931 else if (it->c < 256)
5932 {
5933 str[0] = it->c;
5934 len = 1;
5935 }
5936 else
5937 {
5938 /* It's an invalid character, which shouldn't
5939 happen actually, but due to bugs it may
5940 happen. Let's print the char as is, there's
5941 not much meaningful we can do with it. */
5942 str[0] = it->c;
5943 str[1] = it->c >> 8;
5944 str[2] = it->c >> 16;
5945 str[3] = it->c >> 24;
5946 len = 4;
5947 }
5948
5949 for (i = 0; i < len; i++)
5950 {
5951 XSETINT (it->ctl_chars[i * 4], escape_glyph);
5952 /* Insert three more glyphs into IT->ctl_chars for
5953 the octal display of the character. */
5954 g = ((str[i] >> 6) & 7) + '0';
5955 XSETINT (it->ctl_chars[i * 4 + 1], g);
5956 g = ((str[i] >> 3) & 7) + '0';
5957 XSETINT (it->ctl_chars[i * 4 + 2], g);
5958 g = (str[i] & 7) + '0';
5959 XSETINT (it->ctl_chars[i * 4 + 3], g);
5960 }
5961 ctl_len = len * 4;
5962 }
5963
5964 display_control:
5965 /* Set up IT->dpvec and return first character from it. */
5966 it->dpvec_char_len = it->len;
5967 it->dpvec = it->ctl_chars;
5968 it->dpend = it->dpvec + ctl_len;
5969 it->current.dpvec_index = 0;
5970 it->dpvec_face_id = face_id;
5971 it->saved_face_id = it->face_id;
5972 it->method = GET_FROM_DISPLAY_VECTOR;
5973 it->ellipsis_p = 0;
5974 goto get_next;
5975 }
5976 }
5977 }
5978
5979 /* Adjust face id for a multibyte character. There are no multibyte
5980 character in unibyte text. */
5981 if ((it->what == IT_CHARACTER || it->what == IT_COMPOSITION)
5982 && it->multibyte_p
5983 && success_p
5984 && FRAME_WINDOW_P (it->f))
5985 {
5986 struct face *face = FACE_FROM_ID (it->f, it->face_id);
5987 int pos = (it->s ? -1
5988 : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
5989 : IT_CHARPOS (*it));
5990
5991 it->face_id = FACE_FOR_CHAR (it->f, face, it->c, pos, it->string);
5992 }
5993
5994 /* Is this character the last one of a run of characters with
5995 box? If yes, set IT->end_of_box_run_p to 1. */
5996 if (it->face_box_p
5997 && it->s == NULL)
5998 {
5999 int face_id;
6000 struct face *face;
6001
6002 it->end_of_box_run_p
6003 = ((face_id = face_after_it_pos (it),
6004 face_id != it->face_id)
6005 && (face = FACE_FROM_ID (it->f, face_id),
6006 face->box == FACE_NO_BOX));
6007 }
6008
6009 /* Value is 0 if end of buffer or string reached. */
6010 return success_p;
6011 }
6012
6013
6014 /* Move IT to the next display element.
6015
6016 RESEAT_P non-zero means if called on a newline in buffer text,
6017 skip to the next visible line start.
6018
6019 Functions get_next_display_element and set_iterator_to_next are
6020 separate because I find this arrangement easier to handle than a
6021 get_next_display_element function that also increments IT's
6022 position. The way it is we can first look at an iterator's current
6023 display element, decide whether it fits on a line, and if it does,
6024 increment the iterator position. The other way around we probably
6025 would either need a flag indicating whether the iterator has to be
6026 incremented the next time, or we would have to implement a
6027 decrement position function which would not be easy to write. */
6028
6029 void
6030 set_iterator_to_next (it, reseat_p)
6031 struct it *it;
6032 int reseat_p;
6033 {
6034 /* Reset flags indicating start and end of a sequence of characters
6035 with box. Reset them at the start of this function because
6036 moving the iterator to a new position might set them. */
6037 it->start_of_box_run_p = it->end_of_box_run_p = 0;
6038
6039 switch (it->method)
6040 {
6041 case GET_FROM_BUFFER:
6042 /* The current display element of IT is a character from
6043 current_buffer. Advance in the buffer, and maybe skip over
6044 invisible lines that are so because of selective display. */
6045 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
6046 reseat_at_next_visible_line_start (it, 0);
6047 else
6048 {
6049 xassert (it->len != 0);
6050 IT_BYTEPOS (*it) += it->len;
6051 IT_CHARPOS (*it) += 1;
6052 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
6053 }
6054 break;
6055
6056 case GET_FROM_COMPOSITION:
6057 xassert (it->cmp_id >= 0 && it->cmp_id < n_compositions);
6058 xassert (it->sp > 0);
6059 pop_it (it);
6060 if (it->method == GET_FROM_STRING)
6061 {
6062 IT_STRING_BYTEPOS (*it) += it->len;
6063 IT_STRING_CHARPOS (*it) += it->cmp_len;
6064 goto consider_string_end;
6065 }
6066 else if (it->method == GET_FROM_BUFFER)
6067 {
6068 IT_BYTEPOS (*it) += it->len;
6069 IT_CHARPOS (*it) += it->cmp_len;
6070 }
6071 break;
6072
6073 case GET_FROM_C_STRING:
6074 /* Current display element of IT is from a C string. */
6075 IT_BYTEPOS (*it) += it->len;
6076 IT_CHARPOS (*it) += 1;
6077 break;
6078
6079 case GET_FROM_DISPLAY_VECTOR:
6080 /* Current display element of IT is from a display table entry.
6081 Advance in the display table definition. Reset it to null if
6082 end reached, and continue with characters from buffers/
6083 strings. */
6084 ++it->current.dpvec_index;
6085
6086 /* Restore face of the iterator to what they were before the
6087 display vector entry (these entries may contain faces). */
6088 it->face_id = it->saved_face_id;
6089
6090 if (it->dpvec + it->current.dpvec_index == it->dpend)
6091 {
6092 int recheck_faces = it->ellipsis_p;
6093
6094 if (it->s)
6095 it->method = GET_FROM_C_STRING;
6096 else if (STRINGP (it->string))
6097 it->method = GET_FROM_STRING;
6098 else
6099 {
6100 it->method = GET_FROM_BUFFER;
6101 it->object = it->w->buffer;
6102 }
6103
6104 it->dpvec = NULL;
6105 it->current.dpvec_index = -1;
6106
6107 /* Skip over characters which were displayed via IT->dpvec. */
6108 if (it->dpvec_char_len < 0)
6109 reseat_at_next_visible_line_start (it, 1);
6110 else if (it->dpvec_char_len > 0)
6111 {
6112 if (it->method == GET_FROM_STRING
6113 && it->n_overlay_strings > 0)
6114 it->ignore_overlay_strings_at_pos_p = 1;
6115 it->len = it->dpvec_char_len;
6116 set_iterator_to_next (it, reseat_p);
6117 }
6118
6119 /* Maybe recheck faces after display vector */
6120 if (recheck_faces)
6121 it->stop_charpos = IT_CHARPOS (*it);
6122 }
6123 break;
6124
6125 case GET_FROM_STRING:
6126 /* Current display element is a character from a Lisp string. */
6127 xassert (it->s == NULL && STRINGP (it->string));
6128 IT_STRING_BYTEPOS (*it) += it->len;
6129 IT_STRING_CHARPOS (*it) += 1;
6130
6131 consider_string_end:
6132
6133 if (it->current.overlay_string_index >= 0)
6134 {
6135 /* IT->string is an overlay string. Advance to the
6136 next, if there is one. */
6137 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
6138 next_overlay_string (it);
6139 }
6140 else
6141 {
6142 /* IT->string is not an overlay string. If we reached
6143 its end, and there is something on IT->stack, proceed
6144 with what is on the stack. This can be either another
6145 string, this time an overlay string, or a buffer. */
6146 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
6147 && it->sp > 0)
6148 {
6149 pop_it (it);
6150 if (it->method == GET_FROM_STRING)
6151 goto consider_string_end;
6152 }
6153 }
6154 break;
6155
6156 case GET_FROM_IMAGE:
6157 case GET_FROM_STRETCH:
6158 /* The position etc with which we have to proceed are on
6159 the stack. The position may be at the end of a string,
6160 if the `display' property takes up the whole string. */
6161 xassert (it->sp > 0);
6162 pop_it (it);
6163 if (it->method == GET_FROM_STRING)
6164 goto consider_string_end;
6165 break;
6166
6167 default:
6168 /* There are no other methods defined, so this should be a bug. */
6169 abort ();
6170 }
6171
6172 xassert (it->method != GET_FROM_STRING
6173 || (STRINGP (it->string)
6174 && IT_STRING_CHARPOS (*it) >= 0));
6175 }
6176
6177 /* Load IT's display element fields with information about the next
6178 display element which comes from a display table entry or from the
6179 result of translating a control character to one of the forms `^C'
6180 or `\003'.
6181
6182 IT->dpvec holds the glyphs to return as characters.
6183 IT->saved_face_id holds the face id before the display vector--
6184 it is restored into IT->face_idin set_iterator_to_next. */
6185
6186 static int
6187 next_element_from_display_vector (it)
6188 struct it *it;
6189 {
6190 /* Precondition. */
6191 xassert (it->dpvec && it->current.dpvec_index >= 0);
6192
6193 it->face_id = it->saved_face_id;
6194
6195 if (INTEGERP (*it->dpvec)
6196 && GLYPH_CHAR_VALID_P (XFASTINT (*it->dpvec)))
6197 {
6198 GLYPH g;
6199
6200 g = XFASTINT (it->dpvec[it->current.dpvec_index]);
6201 it->c = FAST_GLYPH_CHAR (g);
6202 it->len = CHAR_BYTES (it->c);
6203
6204 /* The entry may contain a face id to use. Such a face id is
6205 the id of a Lisp face, not a realized face. A face id of
6206 zero means no face is specified. */
6207 if (it->dpvec_face_id >= 0)
6208 it->face_id = it->dpvec_face_id;
6209 else
6210 {
6211 int lface_id = FAST_GLYPH_FACE (g);
6212 if (lface_id > 0)
6213 it->face_id = merge_faces (it->f, Qt, lface_id,
6214 it->saved_face_id);
6215 }
6216 }
6217 else
6218 /* Display table entry is invalid. Return a space. */
6219 it->c = ' ', it->len = 1;
6220
6221 /* Don't change position and object of the iterator here. They are
6222 still the values of the character that had this display table
6223 entry or was translated, and that's what we want. */
6224 it->what = IT_CHARACTER;
6225 return 1;
6226 }
6227
6228
6229 /* Load IT with the next display element from Lisp string IT->string.
6230 IT->current.string_pos is the current position within the string.
6231 If IT->current.overlay_string_index >= 0, the Lisp string is an
6232 overlay string. */
6233
6234 static int
6235 next_element_from_string (it)
6236 struct it *it;
6237 {
6238 struct text_pos position;
6239
6240 xassert (STRINGP (it->string));
6241 xassert (IT_STRING_CHARPOS (*it) >= 0);
6242 position = it->current.string_pos;
6243
6244 /* Time to check for invisible text? */
6245 if (IT_STRING_CHARPOS (*it) < it->end_charpos
6246 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
6247 {
6248 handle_stop (it);
6249
6250 /* Since a handler may have changed IT->method, we must
6251 recurse here. */
6252 return get_next_display_element (it);
6253 }
6254
6255 if (it->current.overlay_string_index >= 0)
6256 {
6257 /* Get the next character from an overlay string. In overlay
6258 strings, There is no field width or padding with spaces to
6259 do. */
6260 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
6261 {
6262 it->what = IT_EOB;
6263 return 0;
6264 }
6265 else if (STRING_MULTIBYTE (it->string))
6266 {
6267 int remaining = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
6268 const unsigned char *s = (SDATA (it->string)
6269 + IT_STRING_BYTEPOS (*it));
6270 it->c = string_char_and_length (s, remaining, &it->len);
6271 }
6272 else
6273 {
6274 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
6275 it->len = 1;
6276 }
6277 }
6278 else
6279 {
6280 /* Get the next character from a Lisp string that is not an
6281 overlay string. Such strings come from the mode line, for
6282 example. We may have to pad with spaces, or truncate the
6283 string. See also next_element_from_c_string. */
6284 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
6285 {
6286 it->what = IT_EOB;
6287 return 0;
6288 }
6289 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
6290 {
6291 /* Pad with spaces. */
6292 it->c = ' ', it->len = 1;
6293 CHARPOS (position) = BYTEPOS (position) = -1;
6294 }
6295 else if (STRING_MULTIBYTE (it->string))
6296 {
6297 int maxlen = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
6298 const unsigned char *s = (SDATA (it->string)
6299 + IT_STRING_BYTEPOS (*it));
6300 it->c = string_char_and_length (s, maxlen, &it->len);
6301 }
6302 else
6303 {
6304 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
6305 it->len = 1;
6306 }
6307 }
6308
6309 /* Record what we have and where it came from. */
6310 it->what = IT_CHARACTER;
6311 it->object = it->string;
6312 it->position = position;
6313 return 1;
6314 }
6315
6316
6317 /* Load IT with next display element from C string IT->s.
6318 IT->string_nchars is the maximum number of characters to return
6319 from the string. IT->end_charpos may be greater than
6320 IT->string_nchars when this function is called, in which case we
6321 may have to return padding spaces. Value is zero if end of string
6322 reached, including padding spaces. */
6323
6324 static int
6325 next_element_from_c_string (it)
6326 struct it *it;
6327 {
6328 int success_p = 1;
6329
6330 xassert (it->s);
6331 it->what = IT_CHARACTER;
6332 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
6333 it->object = Qnil;
6334
6335 /* IT's position can be greater IT->string_nchars in case a field
6336 width or precision has been specified when the iterator was
6337 initialized. */
6338 if (IT_CHARPOS (*it) >= it->end_charpos)
6339 {
6340 /* End of the game. */
6341 it->what = IT_EOB;
6342 success_p = 0;
6343 }
6344 else if (IT_CHARPOS (*it) >= it->string_nchars)
6345 {
6346 /* Pad with spaces. */
6347 it->c = ' ', it->len = 1;
6348 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
6349 }
6350 else if (it->multibyte_p)
6351 {
6352 /* Implementation note: The calls to strlen apparently aren't a
6353 performance problem because there is no noticeable performance
6354 difference between Emacs running in unibyte or multibyte mode. */
6355 int maxlen = strlen (it->s) - IT_BYTEPOS (*it);
6356 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it),
6357 maxlen, &it->len);
6358 }
6359 else
6360 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
6361
6362 return success_p;
6363 }
6364
6365
6366 /* Set up IT to return characters from an ellipsis, if appropriate.
6367 The definition of the ellipsis glyphs may come from a display table
6368 entry. This function Fills IT with the first glyph from the
6369 ellipsis if an ellipsis is to be displayed. */
6370
6371 static int
6372 next_element_from_ellipsis (it)
6373 struct it *it;
6374 {
6375 if (it->selective_display_ellipsis_p)
6376 setup_for_ellipsis (it, it->len);
6377 else
6378 {
6379 /* The face at the current position may be different from the
6380 face we find after the invisible text. Remember what it
6381 was in IT->saved_face_id, and signal that it's there by
6382 setting face_before_selective_p. */
6383 it->saved_face_id = it->face_id;
6384 it->method = GET_FROM_BUFFER;
6385 it->object = it->w->buffer;
6386 reseat_at_next_visible_line_start (it, 1);
6387 it->face_before_selective_p = 1;
6388 }
6389
6390 return get_next_display_element (it);
6391 }
6392
6393
6394 /* Deliver an image display element. The iterator IT is already
6395 filled with image information (done in handle_display_prop). Value
6396 is always 1. */
6397
6398
6399 static int
6400 next_element_from_image (it)
6401 struct it *it;
6402 {
6403 it->what = IT_IMAGE;
6404 return 1;
6405 }
6406
6407
6408 /* Fill iterator IT with next display element from a stretch glyph
6409 property. IT->object is the value of the text property. Value is
6410 always 1. */
6411
6412 static int
6413 next_element_from_stretch (it)
6414 struct it *it;
6415 {
6416 it->what = IT_STRETCH;
6417 return 1;
6418 }
6419
6420
6421 /* Load IT with the next display element from current_buffer. Value
6422 is zero if end of buffer reached. IT->stop_charpos is the next
6423 position at which to stop and check for text properties or buffer
6424 end. */
6425
6426 static int
6427 next_element_from_buffer (it)
6428 struct it *it;
6429 {
6430 int success_p = 1;
6431
6432 /* Check this assumption, otherwise, we would never enter the
6433 if-statement, below. */
6434 xassert (IT_CHARPOS (*it) >= BEGV
6435 && IT_CHARPOS (*it) <= it->stop_charpos);
6436
6437 if (IT_CHARPOS (*it) >= it->stop_charpos)
6438 {
6439 if (IT_CHARPOS (*it) >= it->end_charpos)
6440 {
6441 int overlay_strings_follow_p;
6442
6443 /* End of the game, except when overlay strings follow that
6444 haven't been returned yet. */
6445 if (it->overlay_strings_at_end_processed_p)
6446 overlay_strings_follow_p = 0;
6447 else
6448 {
6449 it->overlay_strings_at_end_processed_p = 1;
6450 overlay_strings_follow_p = get_overlay_strings (it, 0);
6451 }
6452
6453 if (overlay_strings_follow_p)
6454 success_p = get_next_display_element (it);
6455 else
6456 {
6457 it->what = IT_EOB;
6458 it->position = it->current.pos;
6459 success_p = 0;
6460 }
6461 }
6462 else
6463 {
6464 handle_stop (it);
6465 return get_next_display_element (it);
6466 }
6467 }
6468 else
6469 {
6470 /* No face changes, overlays etc. in sight, so just return a
6471 character from current_buffer. */
6472 unsigned char *p;
6473
6474 /* Maybe run the redisplay end trigger hook. Performance note:
6475 This doesn't seem to cost measurable time. */
6476 if (it->redisplay_end_trigger_charpos
6477 && it->glyph_row
6478 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
6479 run_redisplay_end_trigger_hook (it);
6480
6481 /* Get the next character, maybe multibyte. */
6482 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
6483 if (it->multibyte_p && !ASCII_BYTE_P (*p))
6484 {
6485 int maxlen = ((IT_BYTEPOS (*it) >= GPT_BYTE ? ZV_BYTE : GPT_BYTE)
6486 - IT_BYTEPOS (*it));
6487 it->c = string_char_and_length (p, maxlen, &it->len);
6488 }
6489 else
6490 it->c = *p, it->len = 1;
6491
6492 /* Record what we have and where it came from. */
6493 it->what = IT_CHARACTER;
6494 it->object = it->w->buffer;
6495 it->position = it->current.pos;
6496
6497 /* Normally we return the character found above, except when we
6498 really want to return an ellipsis for selective display. */
6499 if (it->selective)
6500 {
6501 if (it->c == '\n')
6502 {
6503 /* A value of selective > 0 means hide lines indented more
6504 than that number of columns. */
6505 if (it->selective > 0
6506 && IT_CHARPOS (*it) + 1 < ZV
6507 && indented_beyond_p (IT_CHARPOS (*it) + 1,
6508 IT_BYTEPOS (*it) + 1,
6509 (double) it->selective)) /* iftc */
6510 {
6511 success_p = next_element_from_ellipsis (it);
6512 it->dpvec_char_len = -1;
6513 }
6514 }
6515 else if (it->c == '\r' && it->selective == -1)
6516 {
6517 /* A value of selective == -1 means that everything from the
6518 CR to the end of the line is invisible, with maybe an
6519 ellipsis displayed for it. */
6520 success_p = next_element_from_ellipsis (it);
6521 it->dpvec_char_len = -1;
6522 }
6523 }
6524 }
6525
6526 /* Value is zero if end of buffer reached. */
6527 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
6528 return success_p;
6529 }
6530
6531
6532 /* Run the redisplay end trigger hook for IT. */
6533
6534 static void
6535 run_redisplay_end_trigger_hook (it)
6536 struct it *it;
6537 {
6538 Lisp_Object args[3];
6539
6540 /* IT->glyph_row should be non-null, i.e. we should be actually
6541 displaying something, or otherwise we should not run the hook. */
6542 xassert (it->glyph_row);
6543
6544 /* Set up hook arguments. */
6545 args[0] = Qredisplay_end_trigger_functions;
6546 args[1] = it->window;
6547 XSETINT (args[2], it->redisplay_end_trigger_charpos);
6548 it->redisplay_end_trigger_charpos = 0;
6549
6550 /* Since we are *trying* to run these functions, don't try to run
6551 them again, even if they get an error. */
6552 it->w->redisplay_end_trigger = Qnil;
6553 Frun_hook_with_args (3, args);
6554
6555 /* Notice if it changed the face of the character we are on. */
6556 handle_face_prop (it);
6557 }
6558
6559
6560 /* Deliver a composition display element. The iterator IT is already
6561 filled with composition information (done in
6562 handle_composition_prop). Value is always 1. */
6563
6564 static int
6565 next_element_from_composition (it)
6566 struct it *it;
6567 {
6568 it->what = IT_COMPOSITION;
6569 it->position = (STRINGP (it->string)
6570 ? it->current.string_pos
6571 : it->current.pos);
6572 if (STRINGP (it->string))
6573 it->object = it->string;
6574 else
6575 it->object = it->w->buffer;
6576 return 1;
6577 }
6578
6579
6580 \f
6581 /***********************************************************************
6582 Moving an iterator without producing glyphs
6583 ***********************************************************************/
6584
6585 /* Check if iterator is at a position corresponding to a valid buffer
6586 position after some move_it_ call. */
6587
6588 #define IT_POS_VALID_AFTER_MOVE_P(it) \
6589 ((it)->method == GET_FROM_STRING \
6590 ? IT_STRING_CHARPOS (*it) == 0 \
6591 : 1)
6592
6593
6594 /* Move iterator IT to a specified buffer or X position within one
6595 line on the display without producing glyphs.
6596
6597 OP should be a bit mask including some or all of these bits:
6598 MOVE_TO_X: Stop on reaching x-position TO_X.
6599 MOVE_TO_POS: Stop on reaching buffer or string position TO_CHARPOS.
6600 Regardless of OP's value, stop in reaching the end of the display line.
6601
6602 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
6603 This means, in particular, that TO_X includes window's horizontal
6604 scroll amount.
6605
6606 The return value has several possible values that
6607 say what condition caused the scan to stop:
6608
6609 MOVE_POS_MATCH_OR_ZV
6610 - when TO_POS or ZV was reached.
6611
6612 MOVE_X_REACHED
6613 -when TO_X was reached before TO_POS or ZV were reached.
6614
6615 MOVE_LINE_CONTINUED
6616 - when we reached the end of the display area and the line must
6617 be continued.
6618
6619 MOVE_LINE_TRUNCATED
6620 - when we reached the end of the display area and the line is
6621 truncated.
6622
6623 MOVE_NEWLINE_OR_CR
6624 - when we stopped at a line end, i.e. a newline or a CR and selective
6625 display is on. */
6626
6627 static enum move_it_result
6628 move_it_in_display_line_to (it, to_charpos, to_x, op)
6629 struct it *it;
6630 int to_charpos, to_x, op;
6631 {
6632 enum move_it_result result = MOVE_UNDEFINED;
6633 struct glyph_row *saved_glyph_row;
6634
6635 /* Don't produce glyphs in produce_glyphs. */
6636 saved_glyph_row = it->glyph_row;
6637 it->glyph_row = NULL;
6638
6639 #define BUFFER_POS_REACHED_P() \
6640 ((op & MOVE_TO_POS) != 0 \
6641 && BUFFERP (it->object) \
6642 && IT_CHARPOS (*it) >= to_charpos \
6643 && (it->method == GET_FROM_BUFFER \
6644 || (it->method == GET_FROM_DISPLAY_VECTOR \
6645 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
6646
6647
6648 while (1)
6649 {
6650 int x, i, ascent = 0, descent = 0;
6651
6652 /* Stop if we move beyond TO_CHARPOS (after an image or stretch glyph). */
6653 if ((op & MOVE_TO_POS) != 0
6654 && BUFFERP (it->object)
6655 && it->method == GET_FROM_BUFFER
6656 && IT_CHARPOS (*it) > to_charpos)
6657 {
6658 result = MOVE_POS_MATCH_OR_ZV;
6659 break;
6660 }
6661
6662 /* Stop when ZV reached.
6663 We used to stop here when TO_CHARPOS reached as well, but that is
6664 too soon if this glyph does not fit on this line. So we handle it
6665 explicitly below. */
6666 if (!get_next_display_element (it)
6667 || (it->truncate_lines_p
6668 && BUFFER_POS_REACHED_P ()))
6669 {
6670 result = MOVE_POS_MATCH_OR_ZV;
6671 break;
6672 }
6673
6674 /* The call to produce_glyphs will get the metrics of the
6675 display element IT is loaded with. We record in x the
6676 x-position before this display element in case it does not
6677 fit on the line. */
6678 x = it->current_x;
6679
6680 /* Remember the line height so far in case the next element doesn't
6681 fit on the line. */
6682 if (!it->truncate_lines_p)
6683 {
6684 ascent = it->max_ascent;
6685 descent = it->max_descent;
6686 }
6687
6688 PRODUCE_GLYPHS (it);
6689
6690 if (it->area != TEXT_AREA)
6691 {
6692 set_iterator_to_next (it, 1);
6693 continue;
6694 }
6695
6696 /* The number of glyphs we get back in IT->nglyphs will normally
6697 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
6698 character on a terminal frame, or (iii) a line end. For the
6699 second case, IT->nglyphs - 1 padding glyphs will be present
6700 (on X frames, there is only one glyph produced for a
6701 composite character.
6702
6703 The behavior implemented below means, for continuation lines,
6704 that as many spaces of a TAB as fit on the current line are
6705 displayed there. For terminal frames, as many glyphs of a
6706 multi-glyph character are displayed in the current line, too.
6707 This is what the old redisplay code did, and we keep it that
6708 way. Under X, the whole shape of a complex character must
6709 fit on the line or it will be completely displayed in the
6710 next line.
6711
6712 Note that both for tabs and padding glyphs, all glyphs have
6713 the same width. */
6714 if (it->nglyphs)
6715 {
6716 /* More than one glyph or glyph doesn't fit on line. All
6717 glyphs have the same width. */
6718 int single_glyph_width = it->pixel_width / it->nglyphs;
6719 int new_x;
6720 int x_before_this_char = x;
6721 int hpos_before_this_char = it->hpos;
6722
6723 for (i = 0; i < it->nglyphs; ++i, x = new_x)
6724 {
6725 new_x = x + single_glyph_width;
6726
6727 /* We want to leave anything reaching TO_X to the caller. */
6728 if ((op & MOVE_TO_X) && new_x > to_x)
6729 {
6730 if (BUFFER_POS_REACHED_P ())
6731 goto buffer_pos_reached;
6732 it->current_x = x;
6733 result = MOVE_X_REACHED;
6734 break;
6735 }
6736 else if (/* Lines are continued. */
6737 !it->truncate_lines_p
6738 && (/* And glyph doesn't fit on the line. */
6739 new_x > it->last_visible_x
6740 /* Or it fits exactly and we're on a window
6741 system frame. */
6742 || (new_x == it->last_visible_x
6743 && FRAME_WINDOW_P (it->f))))
6744 {
6745 if (/* IT->hpos == 0 means the very first glyph
6746 doesn't fit on the line, e.g. a wide image. */
6747 it->hpos == 0
6748 || (new_x == it->last_visible_x
6749 && FRAME_WINDOW_P (it->f)))
6750 {
6751 ++it->hpos;
6752 it->current_x = new_x;
6753
6754 /* The character's last glyph just barely fits
6755 in this row. */
6756 if (i == it->nglyphs - 1)
6757 {
6758 /* If this is the destination position,
6759 return a position *before* it in this row,
6760 now that we know it fits in this row. */
6761 if (BUFFER_POS_REACHED_P ())
6762 {
6763 it->hpos = hpos_before_this_char;
6764 it->current_x = x_before_this_char;
6765 result = MOVE_POS_MATCH_OR_ZV;
6766 break;
6767 }
6768
6769 set_iterator_to_next (it, 1);
6770 #ifdef HAVE_WINDOW_SYSTEM
6771 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
6772 {
6773 if (!get_next_display_element (it))
6774 {
6775 result = MOVE_POS_MATCH_OR_ZV;
6776 break;
6777 }
6778 if (BUFFER_POS_REACHED_P ())
6779 {
6780 if (ITERATOR_AT_END_OF_LINE_P (it))
6781 result = MOVE_POS_MATCH_OR_ZV;
6782 else
6783 result = MOVE_LINE_CONTINUED;
6784 break;
6785 }
6786 if (ITERATOR_AT_END_OF_LINE_P (it))
6787 {
6788 result = MOVE_NEWLINE_OR_CR;
6789 break;
6790 }
6791 }
6792 #endif /* HAVE_WINDOW_SYSTEM */
6793 }
6794 }
6795 else
6796 {
6797 it->current_x = x;
6798 it->max_ascent = ascent;
6799 it->max_descent = descent;
6800 }
6801
6802 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
6803 IT_CHARPOS (*it)));
6804 result = MOVE_LINE_CONTINUED;
6805 break;
6806 }
6807 else if (BUFFER_POS_REACHED_P ())
6808 goto buffer_pos_reached;
6809 else if (new_x > it->first_visible_x)
6810 {
6811 /* Glyph is visible. Increment number of glyphs that
6812 would be displayed. */
6813 ++it->hpos;
6814 }
6815 else
6816 {
6817 /* Glyph is completely off the left margin of the display
6818 area. Nothing to do. */
6819 }
6820 }
6821
6822 if (result != MOVE_UNDEFINED)
6823 break;
6824 }
6825 else if (BUFFER_POS_REACHED_P ())
6826 {
6827 buffer_pos_reached:
6828 it->current_x = x;
6829 it->max_ascent = ascent;
6830 it->max_descent = descent;
6831 result = MOVE_POS_MATCH_OR_ZV;
6832 break;
6833 }
6834 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
6835 {
6836 /* Stop when TO_X specified and reached. This check is
6837 necessary here because of lines consisting of a line end,
6838 only. The line end will not produce any glyphs and we
6839 would never get MOVE_X_REACHED. */
6840 xassert (it->nglyphs == 0);
6841 result = MOVE_X_REACHED;
6842 break;
6843 }
6844
6845 /* Is this a line end? If yes, we're done. */
6846 if (ITERATOR_AT_END_OF_LINE_P (it))
6847 {
6848 result = MOVE_NEWLINE_OR_CR;
6849 break;
6850 }
6851
6852 /* The current display element has been consumed. Advance
6853 to the next. */
6854 set_iterator_to_next (it, 1);
6855
6856 /* Stop if lines are truncated and IT's current x-position is
6857 past the right edge of the window now. */
6858 if (it->truncate_lines_p
6859 && it->current_x >= it->last_visible_x)
6860 {
6861 #ifdef HAVE_WINDOW_SYSTEM
6862 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
6863 {
6864 if (!get_next_display_element (it)
6865 || BUFFER_POS_REACHED_P ())
6866 {
6867 result = MOVE_POS_MATCH_OR_ZV;
6868 break;
6869 }
6870 if (ITERATOR_AT_END_OF_LINE_P (it))
6871 {
6872 result = MOVE_NEWLINE_OR_CR;
6873 break;
6874 }
6875 }
6876 #endif /* HAVE_WINDOW_SYSTEM */
6877 result = MOVE_LINE_TRUNCATED;
6878 break;
6879 }
6880 }
6881
6882 #undef BUFFER_POS_REACHED_P
6883
6884 /* Restore the iterator settings altered at the beginning of this
6885 function. */
6886 it->glyph_row = saved_glyph_row;
6887 return result;
6888 }
6889
6890
6891 /* Move IT forward until it satisfies one or more of the criteria in
6892 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
6893
6894 OP is a bit-mask that specifies where to stop, and in particular,
6895 which of those four position arguments makes a difference. See the
6896 description of enum move_operation_enum.
6897
6898 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
6899 screen line, this function will set IT to the next position >
6900 TO_CHARPOS. */
6901
6902 void
6903 move_it_to (it, to_charpos, to_x, to_y, to_vpos, op)
6904 struct it *it;
6905 int to_charpos, to_x, to_y, to_vpos;
6906 int op;
6907 {
6908 enum move_it_result skip, skip2 = MOVE_X_REACHED;
6909 int line_height;
6910 int reached = 0;
6911
6912 for (;;)
6913 {
6914 if (op & MOVE_TO_VPOS)
6915 {
6916 /* If no TO_CHARPOS and no TO_X specified, stop at the
6917 start of the line TO_VPOS. */
6918 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
6919 {
6920 if (it->vpos == to_vpos)
6921 {
6922 reached = 1;
6923 break;
6924 }
6925 else
6926 skip = move_it_in_display_line_to (it, -1, -1, 0);
6927 }
6928 else
6929 {
6930 /* TO_VPOS >= 0 means stop at TO_X in the line at
6931 TO_VPOS, or at TO_POS, whichever comes first. */
6932 if (it->vpos == to_vpos)
6933 {
6934 reached = 2;
6935 break;
6936 }
6937
6938 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
6939
6940 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
6941 {
6942 reached = 3;
6943 break;
6944 }
6945 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
6946 {
6947 /* We have reached TO_X but not in the line we want. */
6948 skip = move_it_in_display_line_to (it, to_charpos,
6949 -1, MOVE_TO_POS);
6950 if (skip == MOVE_POS_MATCH_OR_ZV)
6951 {
6952 reached = 4;
6953 break;
6954 }
6955 }
6956 }
6957 }
6958 else if (op & MOVE_TO_Y)
6959 {
6960 struct it it_backup;
6961
6962 /* TO_Y specified means stop at TO_X in the line containing
6963 TO_Y---or at TO_CHARPOS if this is reached first. The
6964 problem is that we can't really tell whether the line
6965 contains TO_Y before we have completely scanned it, and
6966 this may skip past TO_X. What we do is to first scan to
6967 TO_X.
6968
6969 If TO_X is not specified, use a TO_X of zero. The reason
6970 is to make the outcome of this function more predictable.
6971 If we didn't use TO_X == 0, we would stop at the end of
6972 the line which is probably not what a caller would expect
6973 to happen. */
6974 skip = move_it_in_display_line_to (it, to_charpos,
6975 ((op & MOVE_TO_X)
6976 ? to_x : 0),
6977 (MOVE_TO_X
6978 | (op & MOVE_TO_POS)));
6979
6980 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
6981 if (skip == MOVE_POS_MATCH_OR_ZV)
6982 {
6983 reached = 5;
6984 break;
6985 }
6986
6987 /* If TO_X was reached, we would like to know whether TO_Y
6988 is in the line. This can only be said if we know the
6989 total line height which requires us to scan the rest of
6990 the line. */
6991 if (skip == MOVE_X_REACHED)
6992 {
6993 /* Wait! We can conclude that TO_Y is in the line if
6994 the already scanned glyphs make the line tall enough
6995 because further scanning doesn't make it shorter. */
6996 line_height = it->max_ascent + it->max_descent;
6997 if (to_y >= it->current_y
6998 && to_y < it->current_y + line_height)
6999 {
7000 reached = 6;
7001 break;
7002 }
7003 it_backup = *it;
7004 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
7005 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
7006 op & MOVE_TO_POS);
7007 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
7008 }
7009
7010 /* Now, decide whether TO_Y is in this line. */
7011 line_height = it->max_ascent + it->max_descent;
7012 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
7013
7014 if (to_y >= it->current_y
7015 && to_y < it->current_y + line_height)
7016 {
7017 if (skip == MOVE_X_REACHED)
7018 /* If TO_Y is in this line and TO_X was reached above,
7019 we scanned too far. We have to restore IT's settings
7020 to the ones before skipping. */
7021 *it = it_backup;
7022 reached = 6;
7023 }
7024 else if (skip == MOVE_X_REACHED)
7025 {
7026 skip = skip2;
7027 if (skip == MOVE_POS_MATCH_OR_ZV)
7028 reached = 7;
7029 }
7030
7031 if (reached)
7032 break;
7033 }
7034 else if (BUFFERP (it->object)
7035 && it->method == GET_FROM_BUFFER
7036 && IT_CHARPOS (*it) >= to_charpos)
7037 skip = MOVE_POS_MATCH_OR_ZV;
7038 else
7039 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
7040
7041 switch (skip)
7042 {
7043 case MOVE_POS_MATCH_OR_ZV:
7044 reached = 8;
7045 goto out;
7046
7047 case MOVE_NEWLINE_OR_CR:
7048 set_iterator_to_next (it, 1);
7049 it->continuation_lines_width = 0;
7050 break;
7051
7052 case MOVE_LINE_TRUNCATED:
7053 it->continuation_lines_width = 0;
7054 reseat_at_next_visible_line_start (it, 0);
7055 if ((op & MOVE_TO_POS) != 0
7056 && IT_CHARPOS (*it) > to_charpos)
7057 {
7058 reached = 9;
7059 goto out;
7060 }
7061 break;
7062
7063 case MOVE_LINE_CONTINUED:
7064 /* For continued lines ending in a tab, some of the glyphs
7065 associated with the tab are displayed on the current
7066 line. Since it->current_x does not include these glyphs,
7067 we use it->last_visible_x instead. */
7068 it->continuation_lines_width +=
7069 (it->c == '\t') ? it->last_visible_x : it->current_x;
7070 break;
7071
7072 default:
7073 abort ();
7074 }
7075
7076 /* Reset/increment for the next run. */
7077 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
7078 it->current_x = it->hpos = 0;
7079 it->current_y += it->max_ascent + it->max_descent;
7080 ++it->vpos;
7081 last_height = it->max_ascent + it->max_descent;
7082 last_max_ascent = it->max_ascent;
7083 it->max_ascent = it->max_descent = 0;
7084 }
7085
7086 out:
7087
7088 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
7089 }
7090
7091
7092 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
7093
7094 If DY > 0, move IT backward at least that many pixels. DY = 0
7095 means move IT backward to the preceding line start or BEGV. This
7096 function may move over more than DY pixels if IT->current_y - DY
7097 ends up in the middle of a line; in this case IT->current_y will be
7098 set to the top of the line moved to. */
7099
7100 void
7101 move_it_vertically_backward (it, dy)
7102 struct it *it;
7103 int dy;
7104 {
7105 int nlines, h;
7106 struct it it2, it3;
7107 int start_pos;
7108
7109 move_further_back:
7110 xassert (dy >= 0);
7111
7112 start_pos = IT_CHARPOS (*it);
7113
7114 /* Estimate how many newlines we must move back. */
7115 nlines = max (1, dy / FRAME_LINE_HEIGHT (it->f));
7116
7117 /* Set the iterator's position that many lines back. */
7118 while (nlines-- && IT_CHARPOS (*it) > BEGV)
7119 back_to_previous_visible_line_start (it);
7120
7121 /* Reseat the iterator here. When moving backward, we don't want
7122 reseat to skip forward over invisible text, set up the iterator
7123 to deliver from overlay strings at the new position etc. So,
7124 use reseat_1 here. */
7125 reseat_1 (it, it->current.pos, 1);
7126
7127 /* We are now surely at a line start. */
7128 it->current_x = it->hpos = 0;
7129 it->continuation_lines_width = 0;
7130
7131 /* Move forward and see what y-distance we moved. First move to the
7132 start of the next line so that we get its height. We need this
7133 height to be able to tell whether we reached the specified
7134 y-distance. */
7135 it2 = *it;
7136 it2.max_ascent = it2.max_descent = 0;
7137 do
7138 {
7139 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
7140 MOVE_TO_POS | MOVE_TO_VPOS);
7141 }
7142 while (!IT_POS_VALID_AFTER_MOVE_P (&it2));
7143 xassert (IT_CHARPOS (*it) >= BEGV);
7144 it3 = it2;
7145
7146 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
7147 xassert (IT_CHARPOS (*it) >= BEGV);
7148 /* H is the actual vertical distance from the position in *IT
7149 and the starting position. */
7150 h = it2.current_y - it->current_y;
7151 /* NLINES is the distance in number of lines. */
7152 nlines = it2.vpos - it->vpos;
7153
7154 /* Correct IT's y and vpos position
7155 so that they are relative to the starting point. */
7156 it->vpos -= nlines;
7157 it->current_y -= h;
7158
7159 if (dy == 0)
7160 {
7161 /* DY == 0 means move to the start of the screen line. The
7162 value of nlines is > 0 if continuation lines were involved. */
7163 if (nlines > 0)
7164 move_it_by_lines (it, nlines, 1);
7165 #if 0
7166 /* I think this assert is bogus if buffer contains
7167 invisible text or images. KFS. */
7168 xassert (IT_CHARPOS (*it) <= start_pos);
7169 #endif
7170 }
7171 else
7172 {
7173 /* The y-position we try to reach, relative to *IT.
7174 Note that H has been subtracted in front of the if-statement. */
7175 int target_y = it->current_y + h - dy;
7176 int y0 = it3.current_y;
7177 int y1 = line_bottom_y (&it3);
7178 int line_height = y1 - y0;
7179
7180 /* If we did not reach target_y, try to move further backward if
7181 we can. If we moved too far backward, try to move forward. */
7182 if (target_y < it->current_y
7183 /* This is heuristic. In a window that's 3 lines high, with
7184 a line height of 13 pixels each, recentering with point
7185 on the bottom line will try to move -39/2 = 19 pixels
7186 backward. Try to avoid moving into the first line. */
7187 && (it->current_y - target_y
7188 > min (window_box_height (it->w), line_height * 2 / 3))
7189 && IT_CHARPOS (*it) > BEGV)
7190 {
7191 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
7192 target_y - it->current_y));
7193 dy = it->current_y - target_y;
7194 goto move_further_back;
7195 }
7196 else if (target_y >= it->current_y + line_height
7197 && IT_CHARPOS (*it) < ZV)
7198 {
7199 /* Should move forward by at least one line, maybe more.
7200
7201 Note: Calling move_it_by_lines can be expensive on
7202 terminal frames, where compute_motion is used (via
7203 vmotion) to do the job, when there are very long lines
7204 and truncate-lines is nil. That's the reason for
7205 treating terminal frames specially here. */
7206
7207 if (!FRAME_WINDOW_P (it->f))
7208 move_it_vertically (it, target_y - (it->current_y + line_height));
7209 else
7210 {
7211 do
7212 {
7213 move_it_by_lines (it, 1, 1);
7214 }
7215 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
7216 }
7217
7218 #if 0
7219 /* I think this assert is bogus if buffer contains
7220 invisible text or images. KFS. */
7221 xassert (IT_CHARPOS (*it) >= BEGV);
7222 #endif
7223 }
7224 }
7225 }
7226
7227
7228 /* Move IT by a specified amount of pixel lines DY. DY negative means
7229 move backwards. DY = 0 means move to start of screen line. At the
7230 end, IT will be on the start of a screen line. */
7231
7232 void
7233 move_it_vertically (it, dy)
7234 struct it *it;
7235 int dy;
7236 {
7237 if (dy <= 0)
7238 move_it_vertically_backward (it, -dy);
7239 else
7240 {
7241 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
7242 move_it_to (it, ZV, -1, it->current_y + dy, -1,
7243 MOVE_TO_POS | MOVE_TO_Y);
7244 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
7245
7246 /* If buffer ends in ZV without a newline, move to the start of
7247 the line to satisfy the post-condition. */
7248 if (IT_CHARPOS (*it) == ZV
7249 && ZV > BEGV
7250 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
7251 move_it_by_lines (it, 0, 0);
7252 }
7253 }
7254
7255
7256 /* Move iterator IT past the end of the text line it is in. */
7257
7258 void
7259 move_it_past_eol (it)
7260 struct it *it;
7261 {
7262 enum move_it_result rc;
7263
7264 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
7265 if (rc == MOVE_NEWLINE_OR_CR)
7266 set_iterator_to_next (it, 0);
7267 }
7268
7269
7270 #if 0 /* Currently not used. */
7271
7272 /* Return non-zero if some text between buffer positions START_CHARPOS
7273 and END_CHARPOS is invisible. IT->window is the window for text
7274 property lookup. */
7275
7276 static int
7277 invisible_text_between_p (it, start_charpos, end_charpos)
7278 struct it *it;
7279 int start_charpos, end_charpos;
7280 {
7281 Lisp_Object prop, limit;
7282 int invisible_found_p;
7283
7284 xassert (it != NULL && start_charpos <= end_charpos);
7285
7286 /* Is text at START invisible? */
7287 prop = Fget_char_property (make_number (start_charpos), Qinvisible,
7288 it->window);
7289 if (TEXT_PROP_MEANS_INVISIBLE (prop))
7290 invisible_found_p = 1;
7291 else
7292 {
7293 limit = Fnext_single_char_property_change (make_number (start_charpos),
7294 Qinvisible, Qnil,
7295 make_number (end_charpos));
7296 invisible_found_p = XFASTINT (limit) < end_charpos;
7297 }
7298
7299 return invisible_found_p;
7300 }
7301
7302 #endif /* 0 */
7303
7304
7305 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
7306 negative means move up. DVPOS == 0 means move to the start of the
7307 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
7308 NEED_Y_P is zero, IT->current_y will be left unchanged.
7309
7310 Further optimization ideas: If we would know that IT->f doesn't use
7311 a face with proportional font, we could be faster for
7312 truncate-lines nil. */
7313
7314 void
7315 move_it_by_lines (it, dvpos, need_y_p)
7316 struct it *it;
7317 int dvpos, need_y_p;
7318 {
7319 struct position pos;
7320
7321 /* The commented-out optimization uses vmotion on terminals. This
7322 gives bad results, because elements like it->what, on which
7323 callers such as pos_visible_p rely, aren't updated. */
7324 /* if (!FRAME_WINDOW_P (it->f))
7325 {
7326 struct text_pos textpos;
7327
7328 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
7329 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
7330 reseat (it, textpos, 1);
7331 it->vpos += pos.vpos;
7332 it->current_y += pos.vpos;
7333 }
7334 else */
7335
7336 if (dvpos == 0)
7337 {
7338 /* DVPOS == 0 means move to the start of the screen line. */
7339 move_it_vertically_backward (it, 0);
7340 xassert (it->current_x == 0 && it->hpos == 0);
7341 /* Let next call to line_bottom_y calculate real line height */
7342 last_height = 0;
7343 }
7344 else if (dvpos > 0)
7345 {
7346 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
7347 if (!IT_POS_VALID_AFTER_MOVE_P (it))
7348 move_it_to (it, IT_CHARPOS (*it) + 1, -1, -1, -1, MOVE_TO_POS);
7349 }
7350 else
7351 {
7352 struct it it2;
7353 int start_charpos, i;
7354
7355 /* Start at the beginning of the screen line containing IT's
7356 position. This may actually move vertically backwards,
7357 in case of overlays, so adjust dvpos accordingly. */
7358 dvpos += it->vpos;
7359 move_it_vertically_backward (it, 0);
7360 dvpos -= it->vpos;
7361
7362 /* Go back -DVPOS visible lines and reseat the iterator there. */
7363 start_charpos = IT_CHARPOS (*it);
7364 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > BEGV; --i)
7365 back_to_previous_visible_line_start (it);
7366 reseat (it, it->current.pos, 1);
7367
7368 /* Move further back if we end up in a string or an image. */
7369 while (!IT_POS_VALID_AFTER_MOVE_P (it))
7370 {
7371 /* First try to move to start of display line. */
7372 dvpos += it->vpos;
7373 move_it_vertically_backward (it, 0);
7374 dvpos -= it->vpos;
7375 if (IT_POS_VALID_AFTER_MOVE_P (it))
7376 break;
7377 /* If start of line is still in string or image,
7378 move further back. */
7379 back_to_previous_visible_line_start (it);
7380 reseat (it, it->current.pos, 1);
7381 dvpos--;
7382 }
7383
7384 it->current_x = it->hpos = 0;
7385
7386 /* Above call may have moved too far if continuation lines
7387 are involved. Scan forward and see if it did. */
7388 it2 = *it;
7389 it2.vpos = it2.current_y = 0;
7390 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
7391 it->vpos -= it2.vpos;
7392 it->current_y -= it2.current_y;
7393 it->current_x = it->hpos = 0;
7394
7395 /* If we moved too far back, move IT some lines forward. */
7396 if (it2.vpos > -dvpos)
7397 {
7398 int delta = it2.vpos + dvpos;
7399 it2 = *it;
7400 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
7401 /* Move back again if we got too far ahead. */
7402 if (IT_CHARPOS (*it) >= start_charpos)
7403 *it = it2;
7404 }
7405 }
7406 }
7407
7408 /* Return 1 if IT points into the middle of a display vector. */
7409
7410 int
7411 in_display_vector_p (it)
7412 struct it *it;
7413 {
7414 return (it->method == GET_FROM_DISPLAY_VECTOR
7415 && it->current.dpvec_index > 0
7416 && it->dpvec + it->current.dpvec_index != it->dpend);
7417 }
7418
7419 \f
7420 /***********************************************************************
7421 Messages
7422 ***********************************************************************/
7423
7424
7425 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
7426 to *Messages*. */
7427
7428 void
7429 add_to_log (format, arg1, arg2)
7430 char *format;
7431 Lisp_Object arg1, arg2;
7432 {
7433 Lisp_Object args[3];
7434 Lisp_Object msg, fmt;
7435 char *buffer;
7436 int len;
7437 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
7438 USE_SAFE_ALLOCA;
7439
7440 /* Do nothing if called asynchronously. Inserting text into
7441 a buffer may call after-change-functions and alike and
7442 that would means running Lisp asynchronously. */
7443 if (handling_signal)
7444 return;
7445
7446 fmt = msg = Qnil;
7447 GCPRO4 (fmt, msg, arg1, arg2);
7448
7449 args[0] = fmt = build_string (format);
7450 args[1] = arg1;
7451 args[2] = arg2;
7452 msg = Fformat (3, args);
7453
7454 len = SBYTES (msg) + 1;
7455 SAFE_ALLOCA (buffer, char *, len);
7456 bcopy (SDATA (msg), buffer, len);
7457
7458 message_dolog (buffer, len - 1, 1, 0);
7459 SAFE_FREE ();
7460
7461 UNGCPRO;
7462 }
7463
7464
7465 /* Output a newline in the *Messages* buffer if "needs" one. */
7466
7467 void
7468 message_log_maybe_newline ()
7469 {
7470 if (message_log_need_newline)
7471 message_dolog ("", 0, 1, 0);
7472 }
7473
7474
7475 /* Add a string M of length NBYTES to the message log, optionally
7476 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
7477 nonzero, means interpret the contents of M as multibyte. This
7478 function calls low-level routines in order to bypass text property
7479 hooks, etc. which might not be safe to run.
7480
7481 This may GC (insert may run before/after change hooks),
7482 so the buffer M must NOT point to a Lisp string. */
7483
7484 void
7485 message_dolog (m, nbytes, nlflag, multibyte)
7486 const char *m;
7487 int nbytes, nlflag, multibyte;
7488 {
7489 if (!NILP (Vmemory_full))
7490 return;
7491
7492 if (!NILP (Vmessage_log_max))
7493 {
7494 struct buffer *oldbuf;
7495 Lisp_Object oldpoint, oldbegv, oldzv;
7496 int old_windows_or_buffers_changed = windows_or_buffers_changed;
7497 int point_at_end = 0;
7498 int zv_at_end = 0;
7499 Lisp_Object old_deactivate_mark, tem;
7500 struct gcpro gcpro1;
7501
7502 old_deactivate_mark = Vdeactivate_mark;
7503 oldbuf = current_buffer;
7504 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
7505 current_buffer->undo_list = Qt;
7506
7507 oldpoint = message_dolog_marker1;
7508 set_marker_restricted (oldpoint, make_number (PT), Qnil);
7509 oldbegv = message_dolog_marker2;
7510 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
7511 oldzv = message_dolog_marker3;
7512 set_marker_restricted (oldzv, make_number (ZV), Qnil);
7513 GCPRO1 (old_deactivate_mark);
7514
7515 if (PT == Z)
7516 point_at_end = 1;
7517 if (ZV == Z)
7518 zv_at_end = 1;
7519
7520 BEGV = BEG;
7521 BEGV_BYTE = BEG_BYTE;
7522 ZV = Z;
7523 ZV_BYTE = Z_BYTE;
7524 TEMP_SET_PT_BOTH (Z, Z_BYTE);
7525
7526 /* Insert the string--maybe converting multibyte to single byte
7527 or vice versa, so that all the text fits the buffer. */
7528 if (multibyte
7529 && NILP (current_buffer->enable_multibyte_characters))
7530 {
7531 int i, c, char_bytes;
7532 unsigned char work[1];
7533
7534 /* Convert a multibyte string to single-byte
7535 for the *Message* buffer. */
7536 for (i = 0; i < nbytes; i += char_bytes)
7537 {
7538 c = string_char_and_length (m + i, nbytes - i, &char_bytes);
7539 work[0] = (ASCII_CHAR_P (c)
7540 ? c
7541 : multibyte_char_to_unibyte (c, Qnil));
7542 insert_1_both (work, 1, 1, 1, 0, 0);
7543 }
7544 }
7545 else if (! multibyte
7546 && ! NILP (current_buffer->enable_multibyte_characters))
7547 {
7548 int i, c, char_bytes;
7549 unsigned char *msg = (unsigned char *) m;
7550 unsigned char str[MAX_MULTIBYTE_LENGTH];
7551 /* Convert a single-byte string to multibyte
7552 for the *Message* buffer. */
7553 for (i = 0; i < nbytes; i++)
7554 {
7555 c = msg[i];
7556 c = unibyte_char_to_multibyte (c);
7557 char_bytes = CHAR_STRING (c, str);
7558 insert_1_both (str, 1, char_bytes, 1, 0, 0);
7559 }
7560 }
7561 else if (nbytes)
7562 insert_1 (m, nbytes, 1, 0, 0);
7563
7564 if (nlflag)
7565 {
7566 int this_bol, this_bol_byte, prev_bol, prev_bol_byte, dup;
7567 insert_1 ("\n", 1, 1, 0, 0);
7568
7569 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
7570 this_bol = PT;
7571 this_bol_byte = PT_BYTE;
7572
7573 /* See if this line duplicates the previous one.
7574 If so, combine duplicates. */
7575 if (this_bol > BEG)
7576 {
7577 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
7578 prev_bol = PT;
7579 prev_bol_byte = PT_BYTE;
7580
7581 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
7582 this_bol, this_bol_byte);
7583 if (dup)
7584 {
7585 del_range_both (prev_bol, prev_bol_byte,
7586 this_bol, this_bol_byte, 0);
7587 if (dup > 1)
7588 {
7589 char dupstr[40];
7590 int duplen;
7591
7592 /* If you change this format, don't forget to also
7593 change message_log_check_duplicate. */
7594 sprintf (dupstr, " [%d times]", dup);
7595 duplen = strlen (dupstr);
7596 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
7597 insert_1 (dupstr, duplen, 1, 0, 1);
7598 }
7599 }
7600 }
7601
7602 /* If we have more than the desired maximum number of lines
7603 in the *Messages* buffer now, delete the oldest ones.
7604 This is safe because we don't have undo in this buffer. */
7605
7606 if (NATNUMP (Vmessage_log_max))
7607 {
7608 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
7609 -XFASTINT (Vmessage_log_max) - 1, 0);
7610 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
7611 }
7612 }
7613 BEGV = XMARKER (oldbegv)->charpos;
7614 BEGV_BYTE = marker_byte_position (oldbegv);
7615
7616 if (zv_at_end)
7617 {
7618 ZV = Z;
7619 ZV_BYTE = Z_BYTE;
7620 }
7621 else
7622 {
7623 ZV = XMARKER (oldzv)->charpos;
7624 ZV_BYTE = marker_byte_position (oldzv);
7625 }
7626
7627 if (point_at_end)
7628 TEMP_SET_PT_BOTH (Z, Z_BYTE);
7629 else
7630 /* We can't do Fgoto_char (oldpoint) because it will run some
7631 Lisp code. */
7632 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
7633 XMARKER (oldpoint)->bytepos);
7634
7635 UNGCPRO;
7636 unchain_marker (XMARKER (oldpoint));
7637 unchain_marker (XMARKER (oldbegv));
7638 unchain_marker (XMARKER (oldzv));
7639
7640 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
7641 set_buffer_internal (oldbuf);
7642 if (NILP (tem))
7643 windows_or_buffers_changed = old_windows_or_buffers_changed;
7644 message_log_need_newline = !nlflag;
7645 Vdeactivate_mark = old_deactivate_mark;
7646 }
7647 }
7648
7649
7650 /* We are at the end of the buffer after just having inserted a newline.
7651 (Note: We depend on the fact we won't be crossing the gap.)
7652 Check to see if the most recent message looks a lot like the previous one.
7653 Return 0 if different, 1 if the new one should just replace it, or a
7654 value N > 1 if we should also append " [N times]". */
7655
7656 static int
7657 message_log_check_duplicate (prev_bol, prev_bol_byte, this_bol, this_bol_byte)
7658 int prev_bol, this_bol;
7659 int prev_bol_byte, this_bol_byte;
7660 {
7661 int i;
7662 int len = Z_BYTE - 1 - this_bol_byte;
7663 int seen_dots = 0;
7664 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
7665 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
7666
7667 for (i = 0; i < len; i++)
7668 {
7669 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
7670 seen_dots = 1;
7671 if (p1[i] != p2[i])
7672 return seen_dots;
7673 }
7674 p1 += len;
7675 if (*p1 == '\n')
7676 return 2;
7677 if (*p1++ == ' ' && *p1++ == '[')
7678 {
7679 int n = 0;
7680 while (*p1 >= '0' && *p1 <= '9')
7681 n = n * 10 + *p1++ - '0';
7682 if (strncmp (p1, " times]\n", 8) == 0)
7683 return n+1;
7684 }
7685 return 0;
7686 }
7687 \f
7688
7689 /* Display an echo area message M with a specified length of NBYTES
7690 bytes. The string may include null characters. If M is 0, clear
7691 out any existing message, and let the mini-buffer text show
7692 through.
7693
7694 This may GC, so the buffer M must NOT point to a Lisp string. */
7695
7696 void
7697 message2 (m, nbytes, multibyte)
7698 const char *m;
7699 int nbytes;
7700 int multibyte;
7701 {
7702 /* First flush out any partial line written with print. */
7703 message_log_maybe_newline ();
7704 if (m)
7705 message_dolog (m, nbytes, 1, multibyte);
7706 message2_nolog (m, nbytes, multibyte);
7707 }
7708
7709
7710 /* The non-logging counterpart of message2. */
7711
7712 void
7713 message2_nolog (m, nbytes, multibyte)
7714 const char *m;
7715 int nbytes, multibyte;
7716 {
7717 struct frame *sf = SELECTED_FRAME ();
7718 message_enable_multibyte = multibyte;
7719
7720 if (noninteractive)
7721 {
7722 if (noninteractive_need_newline)
7723 putc ('\n', stderr);
7724 noninteractive_need_newline = 0;
7725 if (m)
7726 fwrite (m, nbytes, 1, stderr);
7727 if (cursor_in_echo_area == 0)
7728 fprintf (stderr, "\n");
7729 fflush (stderr);
7730 }
7731 /* A null message buffer means that the frame hasn't really been
7732 initialized yet. Error messages get reported properly by
7733 cmd_error, so this must be just an informative message; toss it. */
7734 else if (INTERACTIVE
7735 && sf->glyphs_initialized_p
7736 && FRAME_MESSAGE_BUF (sf))
7737 {
7738 Lisp_Object mini_window;
7739 struct frame *f;
7740
7741 /* Get the frame containing the mini-buffer
7742 that the selected frame is using. */
7743 mini_window = FRAME_MINIBUF_WINDOW (sf);
7744 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7745
7746 FRAME_SAMPLE_VISIBILITY (f);
7747 if (FRAME_VISIBLE_P (sf)
7748 && ! FRAME_VISIBLE_P (f))
7749 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
7750
7751 if (m)
7752 {
7753 set_message (m, Qnil, nbytes, multibyte);
7754 if (minibuffer_auto_raise)
7755 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
7756 }
7757 else
7758 clear_message (1, 1);
7759
7760 do_pending_window_change (0);
7761 echo_area_display (1);
7762 do_pending_window_change (0);
7763 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
7764 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
7765 }
7766 }
7767
7768
7769 /* Display an echo area message M with a specified length of NBYTES
7770 bytes. The string may include null characters. If M is not a
7771 string, clear out any existing message, and let the mini-buffer
7772 text show through.
7773
7774 This function cancels echoing. */
7775
7776 void
7777 message3 (m, nbytes, multibyte)
7778 Lisp_Object m;
7779 int nbytes;
7780 int multibyte;
7781 {
7782 struct gcpro gcpro1;
7783
7784 GCPRO1 (m);
7785 clear_message (1,1);
7786 cancel_echoing ();
7787
7788 /* First flush out any partial line written with print. */
7789 message_log_maybe_newline ();
7790 if (STRINGP (m))
7791 {
7792 char *buffer;
7793 USE_SAFE_ALLOCA;
7794
7795 SAFE_ALLOCA (buffer, char *, nbytes);
7796 bcopy (SDATA (m), buffer, nbytes);
7797 message_dolog (buffer, nbytes, 1, multibyte);
7798 SAFE_FREE ();
7799 }
7800 message3_nolog (m, nbytes, multibyte);
7801
7802 UNGCPRO;
7803 }
7804
7805
7806 /* The non-logging version of message3.
7807 This does not cancel echoing, because it is used for echoing.
7808 Perhaps we need to make a separate function for echoing
7809 and make this cancel echoing. */
7810
7811 void
7812 message3_nolog (m, nbytes, multibyte)
7813 Lisp_Object m;
7814 int nbytes, multibyte;
7815 {
7816 struct frame *sf = SELECTED_FRAME ();
7817 message_enable_multibyte = multibyte;
7818
7819 if (noninteractive)
7820 {
7821 if (noninteractive_need_newline)
7822 putc ('\n', stderr);
7823 noninteractive_need_newline = 0;
7824 if (STRINGP (m))
7825 fwrite (SDATA (m), nbytes, 1, stderr);
7826 if (cursor_in_echo_area == 0)
7827 fprintf (stderr, "\n");
7828 fflush (stderr);
7829 }
7830 /* A null message buffer means that the frame hasn't really been
7831 initialized yet. Error messages get reported properly by
7832 cmd_error, so this must be just an informative message; toss it. */
7833 else if (INTERACTIVE
7834 && sf->glyphs_initialized_p
7835 && FRAME_MESSAGE_BUF (sf))
7836 {
7837 Lisp_Object mini_window;
7838 Lisp_Object frame;
7839 struct frame *f;
7840
7841 /* Get the frame containing the mini-buffer
7842 that the selected frame is using. */
7843 mini_window = FRAME_MINIBUF_WINDOW (sf);
7844 frame = XWINDOW (mini_window)->frame;
7845 f = XFRAME (frame);
7846
7847 FRAME_SAMPLE_VISIBILITY (f);
7848 if (FRAME_VISIBLE_P (sf)
7849 && !FRAME_VISIBLE_P (f))
7850 Fmake_frame_visible (frame);
7851
7852 if (STRINGP (m) && SCHARS (m) > 0)
7853 {
7854 set_message (NULL, m, nbytes, multibyte);
7855 if (minibuffer_auto_raise)
7856 Fraise_frame (frame);
7857 /* Assume we are not echoing.
7858 (If we are, echo_now will override this.) */
7859 echo_message_buffer = Qnil;
7860 }
7861 else
7862 clear_message (1, 1);
7863
7864 do_pending_window_change (0);
7865 echo_area_display (1);
7866 do_pending_window_change (0);
7867 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
7868 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
7869 }
7870 }
7871
7872
7873 /* Display a null-terminated echo area message M. If M is 0, clear
7874 out any existing message, and let the mini-buffer text show through.
7875
7876 The buffer M must continue to exist until after the echo area gets
7877 cleared or some other message gets displayed there. Do not pass
7878 text that is stored in a Lisp string. Do not pass text in a buffer
7879 that was alloca'd. */
7880
7881 void
7882 message1 (m)
7883 char *m;
7884 {
7885 message2 (m, (m ? strlen (m) : 0), 0);
7886 }
7887
7888
7889 /* The non-logging counterpart of message1. */
7890
7891 void
7892 message1_nolog (m)
7893 char *m;
7894 {
7895 message2_nolog (m, (m ? strlen (m) : 0), 0);
7896 }
7897
7898 /* Display a message M which contains a single %s
7899 which gets replaced with STRING. */
7900
7901 void
7902 message_with_string (m, string, log)
7903 char *m;
7904 Lisp_Object string;
7905 int log;
7906 {
7907 CHECK_STRING (string);
7908
7909 if (noninteractive)
7910 {
7911 if (m)
7912 {
7913 if (noninteractive_need_newline)
7914 putc ('\n', stderr);
7915 noninteractive_need_newline = 0;
7916 fprintf (stderr, m, SDATA (string));
7917 if (cursor_in_echo_area == 0)
7918 fprintf (stderr, "\n");
7919 fflush (stderr);
7920 }
7921 }
7922 else if (INTERACTIVE)
7923 {
7924 /* The frame whose minibuffer we're going to display the message on.
7925 It may be larger than the selected frame, so we need
7926 to use its buffer, not the selected frame's buffer. */
7927 Lisp_Object mini_window;
7928 struct frame *f, *sf = SELECTED_FRAME ();
7929
7930 /* Get the frame containing the minibuffer
7931 that the selected frame is using. */
7932 mini_window = FRAME_MINIBUF_WINDOW (sf);
7933 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7934
7935 /* A null message buffer means that the frame hasn't really been
7936 initialized yet. Error messages get reported properly by
7937 cmd_error, so this must be just an informative message; toss it. */
7938 if (FRAME_MESSAGE_BUF (f))
7939 {
7940 Lisp_Object args[2], message;
7941 struct gcpro gcpro1, gcpro2;
7942
7943 args[0] = build_string (m);
7944 args[1] = message = string;
7945 GCPRO2 (args[0], message);
7946 gcpro1.nvars = 2;
7947
7948 message = Fformat (2, args);
7949
7950 if (log)
7951 message3 (message, SBYTES (message), STRING_MULTIBYTE (message));
7952 else
7953 message3_nolog (message, SBYTES (message), STRING_MULTIBYTE (message));
7954
7955 UNGCPRO;
7956
7957 /* Print should start at the beginning of the message
7958 buffer next time. */
7959 message_buf_print = 0;
7960 }
7961 }
7962 }
7963
7964
7965 /* Dump an informative message to the minibuf. If M is 0, clear out
7966 any existing message, and let the mini-buffer text show through. */
7967
7968 /* VARARGS 1 */
7969 void
7970 message (m, a1, a2, a3)
7971 char *m;
7972 EMACS_INT a1, a2, a3;
7973 {
7974 if (noninteractive)
7975 {
7976 if (m)
7977 {
7978 if (noninteractive_need_newline)
7979 putc ('\n', stderr);
7980 noninteractive_need_newline = 0;
7981 fprintf (stderr, m, a1, a2, a3);
7982 if (cursor_in_echo_area == 0)
7983 fprintf (stderr, "\n");
7984 fflush (stderr);
7985 }
7986 }
7987 else if (INTERACTIVE)
7988 {
7989 /* The frame whose mini-buffer we're going to display the message
7990 on. It may be larger than the selected frame, so we need to
7991 use its buffer, not the selected frame's buffer. */
7992 Lisp_Object mini_window;
7993 struct frame *f, *sf = SELECTED_FRAME ();
7994
7995 /* Get the frame containing the mini-buffer
7996 that the selected frame is using. */
7997 mini_window = FRAME_MINIBUF_WINDOW (sf);
7998 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7999
8000 /* A null message buffer means that the frame hasn't really been
8001 initialized yet. Error messages get reported properly by
8002 cmd_error, so this must be just an informative message; toss
8003 it. */
8004 if (FRAME_MESSAGE_BUF (f))
8005 {
8006 if (m)
8007 {
8008 int len;
8009 #ifdef NO_ARG_ARRAY
8010 char *a[3];
8011 a[0] = (char *) a1;
8012 a[1] = (char *) a2;
8013 a[2] = (char *) a3;
8014
8015 len = doprnt (FRAME_MESSAGE_BUF (f),
8016 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
8017 #else
8018 len = doprnt (FRAME_MESSAGE_BUF (f),
8019 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3,
8020 (char **) &a1);
8021 #endif /* NO_ARG_ARRAY */
8022
8023 message2 (FRAME_MESSAGE_BUF (f), len, 0);
8024 }
8025 else
8026 message1 (0);
8027
8028 /* Print should start at the beginning of the message
8029 buffer next time. */
8030 message_buf_print = 0;
8031 }
8032 }
8033 }
8034
8035
8036 /* The non-logging version of message. */
8037
8038 void
8039 message_nolog (m, a1, a2, a3)
8040 char *m;
8041 EMACS_INT a1, a2, a3;
8042 {
8043 Lisp_Object old_log_max;
8044 old_log_max = Vmessage_log_max;
8045 Vmessage_log_max = Qnil;
8046 message (m, a1, a2, a3);
8047 Vmessage_log_max = old_log_max;
8048 }
8049
8050
8051 /* Display the current message in the current mini-buffer. This is
8052 only called from error handlers in process.c, and is not time
8053 critical. */
8054
8055 void
8056 update_echo_area ()
8057 {
8058 if (!NILP (echo_area_buffer[0]))
8059 {
8060 Lisp_Object string;
8061 string = Fcurrent_message ();
8062 message3 (string, SBYTES (string),
8063 !NILP (current_buffer->enable_multibyte_characters));
8064 }
8065 }
8066
8067
8068 /* Make sure echo area buffers in `echo_buffers' are live.
8069 If they aren't, make new ones. */
8070
8071 static void
8072 ensure_echo_area_buffers ()
8073 {
8074 int i;
8075
8076 for (i = 0; i < 2; ++i)
8077 if (!BUFFERP (echo_buffer[i])
8078 || NILP (XBUFFER (echo_buffer[i])->name))
8079 {
8080 char name[30];
8081 Lisp_Object old_buffer;
8082 int j;
8083
8084 old_buffer = echo_buffer[i];
8085 sprintf (name, " *Echo Area %d*", i);
8086 echo_buffer[i] = Fget_buffer_create (build_string (name));
8087 XBUFFER (echo_buffer[i])->truncate_lines = Qnil;
8088
8089 for (j = 0; j < 2; ++j)
8090 if (EQ (old_buffer, echo_area_buffer[j]))
8091 echo_area_buffer[j] = echo_buffer[i];
8092 }
8093 }
8094
8095
8096 /* Call FN with args A1..A4 with either the current or last displayed
8097 echo_area_buffer as current buffer.
8098
8099 WHICH zero means use the current message buffer
8100 echo_area_buffer[0]. If that is nil, choose a suitable buffer
8101 from echo_buffer[] and clear it.
8102
8103 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
8104 suitable buffer from echo_buffer[] and clear it.
8105
8106 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
8107 that the current message becomes the last displayed one, make
8108 choose a suitable buffer for echo_area_buffer[0], and clear it.
8109
8110 Value is what FN returns. */
8111
8112 static int
8113 with_echo_area_buffer (w, which, fn, a1, a2, a3, a4)
8114 struct window *w;
8115 int which;
8116 int (*fn) P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
8117 EMACS_INT a1;
8118 Lisp_Object a2;
8119 EMACS_INT a3, a4;
8120 {
8121 Lisp_Object buffer;
8122 int this_one, the_other, clear_buffer_p, rc;
8123 int count = SPECPDL_INDEX ();
8124
8125 /* If buffers aren't live, make new ones. */
8126 ensure_echo_area_buffers ();
8127
8128 clear_buffer_p = 0;
8129
8130 if (which == 0)
8131 this_one = 0, the_other = 1;
8132 else if (which > 0)
8133 this_one = 1, the_other = 0;
8134 else
8135 {
8136 this_one = 0, the_other = 1;
8137 clear_buffer_p = 1;
8138
8139 /* We need a fresh one in case the current echo buffer equals
8140 the one containing the last displayed echo area message. */
8141 if (!NILP (echo_area_buffer[this_one])
8142 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
8143 echo_area_buffer[this_one] = Qnil;
8144 }
8145
8146 /* Choose a suitable buffer from echo_buffer[] is we don't
8147 have one. */
8148 if (NILP (echo_area_buffer[this_one]))
8149 {
8150 echo_area_buffer[this_one]
8151 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
8152 ? echo_buffer[the_other]
8153 : echo_buffer[this_one]);
8154 clear_buffer_p = 1;
8155 }
8156
8157 buffer = echo_area_buffer[this_one];
8158
8159 /* Don't get confused by reusing the buffer used for echoing
8160 for a different purpose. */
8161 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
8162 cancel_echoing ();
8163
8164 record_unwind_protect (unwind_with_echo_area_buffer,
8165 with_echo_area_buffer_unwind_data (w));
8166
8167 /* Make the echo area buffer current. Note that for display
8168 purposes, it is not necessary that the displayed window's buffer
8169 == current_buffer, except for text property lookup. So, let's
8170 only set that buffer temporarily here without doing a full
8171 Fset_window_buffer. We must also change w->pointm, though,
8172 because otherwise an assertions in unshow_buffer fails, and Emacs
8173 aborts. */
8174 set_buffer_internal_1 (XBUFFER (buffer));
8175 if (w)
8176 {
8177 w->buffer = buffer;
8178 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
8179 }
8180
8181 current_buffer->undo_list = Qt;
8182 current_buffer->read_only = Qnil;
8183 specbind (Qinhibit_read_only, Qt);
8184 specbind (Qinhibit_modification_hooks, Qt);
8185
8186 if (clear_buffer_p && Z > BEG)
8187 del_range (BEG, Z);
8188
8189 xassert (BEGV >= BEG);
8190 xassert (ZV <= Z && ZV >= BEGV);
8191
8192 rc = fn (a1, a2, a3, a4);
8193
8194 xassert (BEGV >= BEG);
8195 xassert (ZV <= Z && ZV >= BEGV);
8196
8197 unbind_to (count, Qnil);
8198 return rc;
8199 }
8200
8201
8202 /* Save state that should be preserved around the call to the function
8203 FN called in with_echo_area_buffer. */
8204
8205 static Lisp_Object
8206 with_echo_area_buffer_unwind_data (w)
8207 struct window *w;
8208 {
8209 int i = 0;
8210 Lisp_Object vector;
8211
8212 /* Reduce consing by keeping one vector in
8213 Vwith_echo_area_save_vector. */
8214 vector = Vwith_echo_area_save_vector;
8215 Vwith_echo_area_save_vector = Qnil;
8216
8217 if (NILP (vector))
8218 vector = Fmake_vector (make_number (7), Qnil);
8219
8220 XSETBUFFER (AREF (vector, i), current_buffer); ++i;
8221 AREF (vector, i) = Vdeactivate_mark, ++i;
8222 AREF (vector, i) = make_number (windows_or_buffers_changed), ++i;
8223
8224 if (w)
8225 {
8226 XSETWINDOW (AREF (vector, i), w); ++i;
8227 AREF (vector, i) = w->buffer; ++i;
8228 AREF (vector, i) = make_number (XMARKER (w->pointm)->charpos); ++i;
8229 AREF (vector, i) = make_number (XMARKER (w->pointm)->bytepos); ++i;
8230 }
8231 else
8232 {
8233 int end = i + 4;
8234 for (; i < end; ++i)
8235 AREF (vector, i) = Qnil;
8236 }
8237
8238 xassert (i == ASIZE (vector));
8239 return vector;
8240 }
8241
8242
8243 /* Restore global state from VECTOR which was created by
8244 with_echo_area_buffer_unwind_data. */
8245
8246 static Lisp_Object
8247 unwind_with_echo_area_buffer (vector)
8248 Lisp_Object vector;
8249 {
8250 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
8251 Vdeactivate_mark = AREF (vector, 1);
8252 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
8253
8254 if (WINDOWP (AREF (vector, 3)))
8255 {
8256 struct window *w;
8257 Lisp_Object buffer, charpos, bytepos;
8258
8259 w = XWINDOW (AREF (vector, 3));
8260 buffer = AREF (vector, 4);
8261 charpos = AREF (vector, 5);
8262 bytepos = AREF (vector, 6);
8263
8264 w->buffer = buffer;
8265 set_marker_both (w->pointm, buffer,
8266 XFASTINT (charpos), XFASTINT (bytepos));
8267 }
8268
8269 Vwith_echo_area_save_vector = vector;
8270 return Qnil;
8271 }
8272
8273
8274 /* Set up the echo area for use by print functions. MULTIBYTE_P
8275 non-zero means we will print multibyte. */
8276
8277 void
8278 setup_echo_area_for_printing (multibyte_p)
8279 int multibyte_p;
8280 {
8281 /* If we can't find an echo area any more, exit. */
8282 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
8283 Fkill_emacs (Qnil);
8284
8285 ensure_echo_area_buffers ();
8286
8287 if (!message_buf_print)
8288 {
8289 /* A message has been output since the last time we printed.
8290 Choose a fresh echo area buffer. */
8291 if (EQ (echo_area_buffer[1], echo_buffer[0]))
8292 echo_area_buffer[0] = echo_buffer[1];
8293 else
8294 echo_area_buffer[0] = echo_buffer[0];
8295
8296 /* Switch to that buffer and clear it. */
8297 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
8298 current_buffer->truncate_lines = Qnil;
8299
8300 if (Z > BEG)
8301 {
8302 int count = SPECPDL_INDEX ();
8303 specbind (Qinhibit_read_only, Qt);
8304 /* Note that undo recording is always disabled. */
8305 del_range (BEG, Z);
8306 unbind_to (count, Qnil);
8307 }
8308 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
8309
8310 /* Set up the buffer for the multibyteness we need. */
8311 if (multibyte_p
8312 != !NILP (current_buffer->enable_multibyte_characters))
8313 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
8314
8315 /* Raise the frame containing the echo area. */
8316 if (minibuffer_auto_raise)
8317 {
8318 struct frame *sf = SELECTED_FRAME ();
8319 Lisp_Object mini_window;
8320 mini_window = FRAME_MINIBUF_WINDOW (sf);
8321 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
8322 }
8323
8324 message_log_maybe_newline ();
8325 message_buf_print = 1;
8326 }
8327 else
8328 {
8329 if (NILP (echo_area_buffer[0]))
8330 {
8331 if (EQ (echo_area_buffer[1], echo_buffer[0]))
8332 echo_area_buffer[0] = echo_buffer[1];
8333 else
8334 echo_area_buffer[0] = echo_buffer[0];
8335 }
8336
8337 if (current_buffer != XBUFFER (echo_area_buffer[0]))
8338 {
8339 /* Someone switched buffers between print requests. */
8340 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
8341 current_buffer->truncate_lines = Qnil;
8342 }
8343 }
8344 }
8345
8346
8347 /* Display an echo area message in window W. Value is non-zero if W's
8348 height is changed. If display_last_displayed_message_p is
8349 non-zero, display the message that was last displayed, otherwise
8350 display the current message. */
8351
8352 static int
8353 display_echo_area (w)
8354 struct window *w;
8355 {
8356 int i, no_message_p, window_height_changed_p, count;
8357
8358 /* Temporarily disable garbage collections while displaying the echo
8359 area. This is done because a GC can print a message itself.
8360 That message would modify the echo area buffer's contents while a
8361 redisplay of the buffer is going on, and seriously confuse
8362 redisplay. */
8363 count = inhibit_garbage_collection ();
8364
8365 /* If there is no message, we must call display_echo_area_1
8366 nevertheless because it resizes the window. But we will have to
8367 reset the echo_area_buffer in question to nil at the end because
8368 with_echo_area_buffer will sets it to an empty buffer. */
8369 i = display_last_displayed_message_p ? 1 : 0;
8370 no_message_p = NILP (echo_area_buffer[i]);
8371
8372 window_height_changed_p
8373 = with_echo_area_buffer (w, display_last_displayed_message_p,
8374 display_echo_area_1,
8375 (EMACS_INT) w, Qnil, 0, 0);
8376
8377 if (no_message_p)
8378 echo_area_buffer[i] = Qnil;
8379
8380 unbind_to (count, Qnil);
8381 return window_height_changed_p;
8382 }
8383
8384
8385 /* Helper for display_echo_area. Display the current buffer which
8386 contains the current echo area message in window W, a mini-window,
8387 a pointer to which is passed in A1. A2..A4 are currently not used.
8388 Change the height of W so that all of the message is displayed.
8389 Value is non-zero if height of W was changed. */
8390
8391 static int
8392 display_echo_area_1 (a1, a2, a3, a4)
8393 EMACS_INT a1;
8394 Lisp_Object a2;
8395 EMACS_INT a3, a4;
8396 {
8397 struct window *w = (struct window *) a1;
8398 Lisp_Object window;
8399 struct text_pos start;
8400 int window_height_changed_p = 0;
8401
8402 /* Do this before displaying, so that we have a large enough glyph
8403 matrix for the display. If we can't get enough space for the
8404 whole text, display the last N lines. That works by setting w->start. */
8405 window_height_changed_p = resize_mini_window (w, 0);
8406
8407 /* Use the starting position chosen by resize_mini_window. */
8408 SET_TEXT_POS_FROM_MARKER (start, w->start);
8409
8410 /* Display. */
8411 clear_glyph_matrix (w->desired_matrix);
8412 XSETWINDOW (window, w);
8413 try_window (window, start, 0);
8414
8415 return window_height_changed_p;
8416 }
8417
8418
8419 /* Resize the echo area window to exactly the size needed for the
8420 currently displayed message, if there is one. If a mini-buffer
8421 is active, don't shrink it. */
8422
8423 void
8424 resize_echo_area_exactly ()
8425 {
8426 if (BUFFERP (echo_area_buffer[0])
8427 && WINDOWP (echo_area_window))
8428 {
8429 struct window *w = XWINDOW (echo_area_window);
8430 int resized_p;
8431 Lisp_Object resize_exactly;
8432
8433 if (minibuf_level == 0)
8434 resize_exactly = Qt;
8435 else
8436 resize_exactly = Qnil;
8437
8438 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
8439 (EMACS_INT) w, resize_exactly, 0, 0);
8440 if (resized_p)
8441 {
8442 ++windows_or_buffers_changed;
8443 ++update_mode_lines;
8444 redisplay_internal (0);
8445 }
8446 }
8447 }
8448
8449
8450 /* Callback function for with_echo_area_buffer, when used from
8451 resize_echo_area_exactly. A1 contains a pointer to the window to
8452 resize, EXACTLY non-nil means resize the mini-window exactly to the
8453 size of the text displayed. A3 and A4 are not used. Value is what
8454 resize_mini_window returns. */
8455
8456 static int
8457 resize_mini_window_1 (a1, exactly, a3, a4)
8458 EMACS_INT a1;
8459 Lisp_Object exactly;
8460 EMACS_INT a3, a4;
8461 {
8462 return resize_mini_window ((struct window *) a1, !NILP (exactly));
8463 }
8464
8465
8466 /* Resize mini-window W to fit the size of its contents. EXACT:P
8467 means size the window exactly to the size needed. Otherwise, it's
8468 only enlarged until W's buffer is empty.
8469
8470 Set W->start to the right place to begin display. If the whole
8471 contents fit, start at the beginning. Otherwise, start so as
8472 to make the end of the contents appear. This is particularly
8473 important for y-or-n-p, but seems desirable generally.
8474
8475 Value is non-zero if the window height has been changed. */
8476
8477 int
8478 resize_mini_window (w, exact_p)
8479 struct window *w;
8480 int exact_p;
8481 {
8482 struct frame *f = XFRAME (w->frame);
8483 int window_height_changed_p = 0;
8484
8485 xassert (MINI_WINDOW_P (w));
8486
8487 /* By default, start display at the beginning. */
8488 set_marker_both (w->start, w->buffer,
8489 BUF_BEGV (XBUFFER (w->buffer)),
8490 BUF_BEGV_BYTE (XBUFFER (w->buffer)));
8491
8492 /* Don't resize windows while redisplaying a window; it would
8493 confuse redisplay functions when the size of the window they are
8494 displaying changes from under them. Such a resizing can happen,
8495 for instance, when which-func prints a long message while
8496 we are running fontification-functions. We're running these
8497 functions with safe_call which binds inhibit-redisplay to t. */
8498 if (!NILP (Vinhibit_redisplay))
8499 return 0;
8500
8501 /* Nil means don't try to resize. */
8502 if (NILP (Vresize_mini_windows)
8503 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
8504 return 0;
8505
8506 if (!FRAME_MINIBUF_ONLY_P (f))
8507 {
8508 struct it it;
8509 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
8510 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
8511 int height, max_height;
8512 int unit = FRAME_LINE_HEIGHT (f);
8513 struct text_pos start;
8514 struct buffer *old_current_buffer = NULL;
8515
8516 if (current_buffer != XBUFFER (w->buffer))
8517 {
8518 old_current_buffer = current_buffer;
8519 set_buffer_internal (XBUFFER (w->buffer));
8520 }
8521
8522 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
8523
8524 /* Compute the max. number of lines specified by the user. */
8525 if (FLOATP (Vmax_mini_window_height))
8526 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
8527 else if (INTEGERP (Vmax_mini_window_height))
8528 max_height = XINT (Vmax_mini_window_height);
8529 else
8530 max_height = total_height / 4;
8531
8532 /* Correct that max. height if it's bogus. */
8533 max_height = max (1, max_height);
8534 max_height = min (total_height, max_height);
8535
8536 /* Find out the height of the text in the window. */
8537 if (it.truncate_lines_p)
8538 height = 1;
8539 else
8540 {
8541 last_height = 0;
8542 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
8543 if (it.max_ascent == 0 && it.max_descent == 0)
8544 height = it.current_y + last_height;
8545 else
8546 height = it.current_y + it.max_ascent + it.max_descent;
8547 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
8548 height = (height + unit - 1) / unit;
8549 }
8550
8551 /* Compute a suitable window start. */
8552 if (height > max_height)
8553 {
8554 height = max_height;
8555 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
8556 move_it_vertically_backward (&it, (height - 1) * unit);
8557 start = it.current.pos;
8558 }
8559 else
8560 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
8561 SET_MARKER_FROM_TEXT_POS (w->start, start);
8562
8563 if (EQ (Vresize_mini_windows, Qgrow_only))
8564 {
8565 /* Let it grow only, until we display an empty message, in which
8566 case the window shrinks again. */
8567 if (height > WINDOW_TOTAL_LINES (w))
8568 {
8569 int old_height = WINDOW_TOTAL_LINES (w);
8570 freeze_window_starts (f, 1);
8571 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8572 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8573 }
8574 else if (height < WINDOW_TOTAL_LINES (w)
8575 && (exact_p || BEGV == ZV))
8576 {
8577 int old_height = WINDOW_TOTAL_LINES (w);
8578 freeze_window_starts (f, 0);
8579 shrink_mini_window (w);
8580 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8581 }
8582 }
8583 else
8584 {
8585 /* Always resize to exact size needed. */
8586 if (height > WINDOW_TOTAL_LINES (w))
8587 {
8588 int old_height = WINDOW_TOTAL_LINES (w);
8589 freeze_window_starts (f, 1);
8590 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8591 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8592 }
8593 else if (height < WINDOW_TOTAL_LINES (w))
8594 {
8595 int old_height = WINDOW_TOTAL_LINES (w);
8596 freeze_window_starts (f, 0);
8597 shrink_mini_window (w);
8598
8599 if (height)
8600 {
8601 freeze_window_starts (f, 1);
8602 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8603 }
8604
8605 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8606 }
8607 }
8608
8609 if (old_current_buffer)
8610 set_buffer_internal (old_current_buffer);
8611 }
8612
8613 return window_height_changed_p;
8614 }
8615
8616
8617 /* Value is the current message, a string, or nil if there is no
8618 current message. */
8619
8620 Lisp_Object
8621 current_message ()
8622 {
8623 Lisp_Object msg;
8624
8625 if (NILP (echo_area_buffer[0]))
8626 msg = Qnil;
8627 else
8628 {
8629 with_echo_area_buffer (0, 0, current_message_1,
8630 (EMACS_INT) &msg, Qnil, 0, 0);
8631 if (NILP (msg))
8632 echo_area_buffer[0] = Qnil;
8633 }
8634
8635 return msg;
8636 }
8637
8638
8639 static int
8640 current_message_1 (a1, a2, a3, a4)
8641 EMACS_INT a1;
8642 Lisp_Object a2;
8643 EMACS_INT a3, a4;
8644 {
8645 Lisp_Object *msg = (Lisp_Object *) a1;
8646
8647 if (Z > BEG)
8648 *msg = make_buffer_string (BEG, Z, 1);
8649 else
8650 *msg = Qnil;
8651 return 0;
8652 }
8653
8654
8655 /* Push the current message on Vmessage_stack for later restauration
8656 by restore_message. Value is non-zero if the current message isn't
8657 empty. This is a relatively infrequent operation, so it's not
8658 worth optimizing. */
8659
8660 int
8661 push_message ()
8662 {
8663 Lisp_Object msg;
8664 msg = current_message ();
8665 Vmessage_stack = Fcons (msg, Vmessage_stack);
8666 return STRINGP (msg);
8667 }
8668
8669
8670 /* Restore message display from the top of Vmessage_stack. */
8671
8672 void
8673 restore_message ()
8674 {
8675 Lisp_Object msg;
8676
8677 xassert (CONSP (Vmessage_stack));
8678 msg = XCAR (Vmessage_stack);
8679 if (STRINGP (msg))
8680 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
8681 else
8682 message3_nolog (msg, 0, 0);
8683 }
8684
8685
8686 /* Handler for record_unwind_protect calling pop_message. */
8687
8688 Lisp_Object
8689 pop_message_unwind (dummy)
8690 Lisp_Object dummy;
8691 {
8692 pop_message ();
8693 return Qnil;
8694 }
8695
8696 /* Pop the top-most entry off Vmessage_stack. */
8697
8698 void
8699 pop_message ()
8700 {
8701 xassert (CONSP (Vmessage_stack));
8702 Vmessage_stack = XCDR (Vmessage_stack);
8703 }
8704
8705
8706 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
8707 exits. If the stack is not empty, we have a missing pop_message
8708 somewhere. */
8709
8710 void
8711 check_message_stack ()
8712 {
8713 if (!NILP (Vmessage_stack))
8714 abort ();
8715 }
8716
8717
8718 /* Truncate to NCHARS what will be displayed in the echo area the next
8719 time we display it---but don't redisplay it now. */
8720
8721 void
8722 truncate_echo_area (nchars)
8723 int nchars;
8724 {
8725 if (nchars == 0)
8726 echo_area_buffer[0] = Qnil;
8727 /* A null message buffer means that the frame hasn't really been
8728 initialized yet. Error messages get reported properly by
8729 cmd_error, so this must be just an informative message; toss it. */
8730 else if (!noninteractive
8731 && INTERACTIVE
8732 && !NILP (echo_area_buffer[0]))
8733 {
8734 struct frame *sf = SELECTED_FRAME ();
8735 if (FRAME_MESSAGE_BUF (sf))
8736 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
8737 }
8738 }
8739
8740
8741 /* Helper function for truncate_echo_area. Truncate the current
8742 message to at most NCHARS characters. */
8743
8744 static int
8745 truncate_message_1 (nchars, a2, a3, a4)
8746 EMACS_INT nchars;
8747 Lisp_Object a2;
8748 EMACS_INT a3, a4;
8749 {
8750 if (BEG + nchars < Z)
8751 del_range (BEG + nchars, Z);
8752 if (Z == BEG)
8753 echo_area_buffer[0] = Qnil;
8754 return 0;
8755 }
8756
8757
8758 /* Set the current message to a substring of S or STRING.
8759
8760 If STRING is a Lisp string, set the message to the first NBYTES
8761 bytes from STRING. NBYTES zero means use the whole string. If
8762 STRING is multibyte, the message will be displayed multibyte.
8763
8764 If S is not null, set the message to the first LEN bytes of S. LEN
8765 zero means use the whole string. MULTIBYTE_P non-zero means S is
8766 multibyte. Display the message multibyte in that case.
8767
8768 Doesn't GC, as with_echo_area_buffer binds Qinhibit_modification_hooks
8769 to t before calling set_message_1 (which calls insert).
8770 */
8771
8772 void
8773 set_message (s, string, nbytes, multibyte_p)
8774 const char *s;
8775 Lisp_Object string;
8776 int nbytes, multibyte_p;
8777 {
8778 message_enable_multibyte
8779 = ((s && multibyte_p)
8780 || (STRINGP (string) && STRING_MULTIBYTE (string)));
8781
8782 with_echo_area_buffer (0, -1, set_message_1,
8783 (EMACS_INT) s, string, nbytes, multibyte_p);
8784 message_buf_print = 0;
8785 help_echo_showing_p = 0;
8786 }
8787
8788
8789 /* Helper function for set_message. Arguments have the same meaning
8790 as there, with A1 corresponding to S and A2 corresponding to STRING
8791 This function is called with the echo area buffer being
8792 current. */
8793
8794 static int
8795 set_message_1 (a1, a2, nbytes, multibyte_p)
8796 EMACS_INT a1;
8797 Lisp_Object a2;
8798 EMACS_INT nbytes, multibyte_p;
8799 {
8800 const char *s = (const char *) a1;
8801 Lisp_Object string = a2;
8802
8803 /* Change multibyteness of the echo buffer appropriately. */
8804 if (message_enable_multibyte
8805 != !NILP (current_buffer->enable_multibyte_characters))
8806 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
8807
8808 current_buffer->truncate_lines = message_truncate_lines ? Qt : Qnil;
8809
8810 /* Insert new message at BEG. */
8811 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
8812
8813 if (STRINGP (string))
8814 {
8815 int nchars;
8816
8817 if (nbytes == 0)
8818 nbytes = SBYTES (string);
8819 nchars = string_byte_to_char (string, nbytes);
8820
8821 /* This function takes care of single/multibyte conversion. We
8822 just have to ensure that the echo area buffer has the right
8823 setting of enable_multibyte_characters. */
8824 insert_from_string (string, 0, 0, nchars, nbytes, 1);
8825 }
8826 else if (s)
8827 {
8828 if (nbytes == 0)
8829 nbytes = strlen (s);
8830
8831 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
8832 {
8833 /* Convert from multi-byte to single-byte. */
8834 int i, c, n;
8835 unsigned char work[1];
8836
8837 /* Convert a multibyte string to single-byte. */
8838 for (i = 0; i < nbytes; i += n)
8839 {
8840 c = string_char_and_length (s + i, nbytes - i, &n);
8841 work[0] = (ASCII_CHAR_P (c)
8842 ? c
8843 : multibyte_char_to_unibyte (c, Qnil));
8844 insert_1_both (work, 1, 1, 1, 0, 0);
8845 }
8846 }
8847 else if (!multibyte_p
8848 && !NILP (current_buffer->enable_multibyte_characters))
8849 {
8850 /* Convert from single-byte to multi-byte. */
8851 int i, c, n;
8852 const unsigned char *msg = (const unsigned char *) s;
8853 unsigned char str[MAX_MULTIBYTE_LENGTH];
8854
8855 /* Convert a single-byte string to multibyte. */
8856 for (i = 0; i < nbytes; i++)
8857 {
8858 c = msg[i];
8859 c = unibyte_char_to_multibyte (c);
8860 n = CHAR_STRING (c, str);
8861 insert_1_both (str, 1, n, 1, 0, 0);
8862 }
8863 }
8864 else
8865 insert_1 (s, nbytes, 1, 0, 0);
8866 }
8867
8868 return 0;
8869 }
8870
8871
8872 /* Clear messages. CURRENT_P non-zero means clear the current
8873 message. LAST_DISPLAYED_P non-zero means clear the message
8874 last displayed. */
8875
8876 void
8877 clear_message (current_p, last_displayed_p)
8878 int current_p, last_displayed_p;
8879 {
8880 if (current_p)
8881 {
8882 echo_area_buffer[0] = Qnil;
8883 message_cleared_p = 1;
8884 }
8885
8886 if (last_displayed_p)
8887 echo_area_buffer[1] = Qnil;
8888
8889 message_buf_print = 0;
8890 }
8891
8892 /* Clear garbaged frames.
8893
8894 This function is used where the old redisplay called
8895 redraw_garbaged_frames which in turn called redraw_frame which in
8896 turn called clear_frame. The call to clear_frame was a source of
8897 flickering. I believe a clear_frame is not necessary. It should
8898 suffice in the new redisplay to invalidate all current matrices,
8899 and ensure a complete redisplay of all windows. */
8900
8901 static void
8902 clear_garbaged_frames ()
8903 {
8904 if (frame_garbaged)
8905 {
8906 Lisp_Object tail, frame;
8907 int changed_count = 0;
8908
8909 FOR_EACH_FRAME (tail, frame)
8910 {
8911 struct frame *f = XFRAME (frame);
8912
8913 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
8914 {
8915 if (f->resized_p)
8916 {
8917 Fredraw_frame (frame);
8918 f->force_flush_display_p = 1;
8919 }
8920 clear_current_matrices (f);
8921 changed_count++;
8922 f->garbaged = 0;
8923 f->resized_p = 0;
8924 }
8925 }
8926
8927 frame_garbaged = 0;
8928 if (changed_count)
8929 ++windows_or_buffers_changed;
8930 }
8931 }
8932
8933
8934 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
8935 is non-zero update selected_frame. Value is non-zero if the
8936 mini-windows height has been changed. */
8937
8938 static int
8939 echo_area_display (update_frame_p)
8940 int update_frame_p;
8941 {
8942 Lisp_Object mini_window;
8943 struct window *w;
8944 struct frame *f;
8945 int window_height_changed_p = 0;
8946 struct frame *sf = SELECTED_FRAME ();
8947
8948 mini_window = FRAME_MINIBUF_WINDOW (sf);
8949 w = XWINDOW (mini_window);
8950 f = XFRAME (WINDOW_FRAME (w));
8951
8952 /* Don't display if frame is invisible or not yet initialized. */
8953 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
8954 return 0;
8955
8956 /* The terminal frame is used as the first Emacs frame on the Mac OS. */
8957 #ifndef MAC_OS8
8958 #ifdef HAVE_WINDOW_SYSTEM
8959 /* When Emacs starts, selected_frame may be the initial terminal
8960 frame. If we let this through, a message would be displayed on
8961 the terminal. */
8962 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
8963 return 0;
8964 #endif /* HAVE_WINDOW_SYSTEM */
8965 #endif
8966
8967 /* Redraw garbaged frames. */
8968 if (frame_garbaged)
8969 clear_garbaged_frames ();
8970
8971 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
8972 {
8973 echo_area_window = mini_window;
8974 window_height_changed_p = display_echo_area (w);
8975 w->must_be_updated_p = 1;
8976
8977 /* Update the display, unless called from redisplay_internal.
8978 Also don't update the screen during redisplay itself. The
8979 update will happen at the end of redisplay, and an update
8980 here could cause confusion. */
8981 if (update_frame_p && !redisplaying_p)
8982 {
8983 int n = 0;
8984
8985 /* If the display update has been interrupted by pending
8986 input, update mode lines in the frame. Due to the
8987 pending input, it might have been that redisplay hasn't
8988 been called, so that mode lines above the echo area are
8989 garbaged. This looks odd, so we prevent it here. */
8990 if (!display_completed)
8991 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
8992
8993 if (window_height_changed_p
8994 /* Don't do this if Emacs is shutting down. Redisplay
8995 needs to run hooks. */
8996 && !NILP (Vrun_hooks))
8997 {
8998 /* Must update other windows. Likewise as in other
8999 cases, don't let this update be interrupted by
9000 pending input. */
9001 int count = SPECPDL_INDEX ();
9002 specbind (Qredisplay_dont_pause, Qt);
9003 windows_or_buffers_changed = 1;
9004 redisplay_internal (0);
9005 unbind_to (count, Qnil);
9006 }
9007 else if (FRAME_WINDOW_P (f) && n == 0)
9008 {
9009 /* Window configuration is the same as before.
9010 Can do with a display update of the echo area,
9011 unless we displayed some mode lines. */
9012 update_single_window (w, 1);
9013 FRAME_RIF (f)->flush_display (f);
9014 }
9015 else
9016 update_frame (f, 1, 1);
9017
9018 /* If cursor is in the echo area, make sure that the next
9019 redisplay displays the minibuffer, so that the cursor will
9020 be replaced with what the minibuffer wants. */
9021 if (cursor_in_echo_area)
9022 ++windows_or_buffers_changed;
9023 }
9024 }
9025 else if (!EQ (mini_window, selected_window))
9026 windows_or_buffers_changed++;
9027
9028 /* Last displayed message is now the current message. */
9029 echo_area_buffer[1] = echo_area_buffer[0];
9030 /* Inform read_char that we're not echoing. */
9031 echo_message_buffer = Qnil;
9032
9033 /* Prevent redisplay optimization in redisplay_internal by resetting
9034 this_line_start_pos. This is done because the mini-buffer now
9035 displays the message instead of its buffer text. */
9036 if (EQ (mini_window, selected_window))
9037 CHARPOS (this_line_start_pos) = 0;
9038
9039 return window_height_changed_p;
9040 }
9041
9042
9043 \f
9044 /***********************************************************************
9045 Mode Lines and Frame Titles
9046 ***********************************************************************/
9047
9048 /* A buffer for constructing non-propertized mode-line strings and
9049 frame titles in it; allocated from the heap in init_xdisp and
9050 resized as needed in store_mode_line_noprop_char. */
9051
9052 static char *mode_line_noprop_buf;
9053
9054 /* The buffer's end, and a current output position in it. */
9055
9056 static char *mode_line_noprop_buf_end;
9057 static char *mode_line_noprop_ptr;
9058
9059 #define MODE_LINE_NOPROP_LEN(start) \
9060 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
9061
9062 static enum {
9063 MODE_LINE_DISPLAY = 0,
9064 MODE_LINE_TITLE,
9065 MODE_LINE_NOPROP,
9066 MODE_LINE_STRING
9067 } mode_line_target;
9068
9069 /* Alist that caches the results of :propertize.
9070 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
9071 static Lisp_Object mode_line_proptrans_alist;
9072
9073 /* List of strings making up the mode-line. */
9074 static Lisp_Object mode_line_string_list;
9075
9076 /* Base face property when building propertized mode line string. */
9077 static Lisp_Object mode_line_string_face;
9078 static Lisp_Object mode_line_string_face_prop;
9079
9080
9081 /* Unwind data for mode line strings */
9082
9083 static Lisp_Object Vmode_line_unwind_vector;
9084
9085 static Lisp_Object
9086 format_mode_line_unwind_data (obuf, save_proptrans)
9087 struct buffer *obuf;
9088 {
9089 Lisp_Object vector;
9090
9091 /* Reduce consing by keeping one vector in
9092 Vwith_echo_area_save_vector. */
9093 vector = Vmode_line_unwind_vector;
9094 Vmode_line_unwind_vector = Qnil;
9095
9096 if (NILP (vector))
9097 vector = Fmake_vector (make_number (7), Qnil);
9098
9099 AREF (vector, 0) = make_number (mode_line_target);
9100 AREF (vector, 1) = make_number (MODE_LINE_NOPROP_LEN (0));
9101 AREF (vector, 2) = mode_line_string_list;
9102 AREF (vector, 3) = (save_proptrans ? mode_line_proptrans_alist : Qt);
9103 AREF (vector, 4) = mode_line_string_face;
9104 AREF (vector, 5) = mode_line_string_face_prop;
9105
9106 if (obuf)
9107 XSETBUFFER (AREF (vector, 6), obuf);
9108 else
9109 AREF (vector, 6) = Qnil;
9110
9111 return vector;
9112 }
9113
9114 static Lisp_Object
9115 unwind_format_mode_line (vector)
9116 Lisp_Object vector;
9117 {
9118 mode_line_target = XINT (AREF (vector, 0));
9119 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
9120 mode_line_string_list = AREF (vector, 2);
9121 if (! EQ (AREF (vector, 3), Qt))
9122 mode_line_proptrans_alist = AREF (vector, 3);
9123 mode_line_string_face = AREF (vector, 4);
9124 mode_line_string_face_prop = AREF (vector, 5);
9125
9126 if (!NILP (AREF (vector, 6)))
9127 {
9128 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
9129 AREF (vector, 6) = Qnil;
9130 }
9131
9132 Vmode_line_unwind_vector = vector;
9133 return Qnil;
9134 }
9135
9136
9137 /* Store a single character C for the frame title in mode_line_noprop_buf.
9138 Re-allocate mode_line_noprop_buf if necessary. */
9139
9140 static void
9141 #ifdef PROTOTYPES
9142 store_mode_line_noprop_char (char c)
9143 #else
9144 store_mode_line_noprop_char (c)
9145 char c;
9146 #endif
9147 {
9148 /* If output position has reached the end of the allocated buffer,
9149 double the buffer's size. */
9150 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
9151 {
9152 int len = MODE_LINE_NOPROP_LEN (0);
9153 int new_size = 2 * len * sizeof *mode_line_noprop_buf;
9154 mode_line_noprop_buf = (char *) xrealloc (mode_line_noprop_buf, new_size);
9155 mode_line_noprop_buf_end = mode_line_noprop_buf + new_size;
9156 mode_line_noprop_ptr = mode_line_noprop_buf + len;
9157 }
9158
9159 *mode_line_noprop_ptr++ = c;
9160 }
9161
9162
9163 /* Store part of a frame title in mode_line_noprop_buf, beginning at
9164 mode_line_noprop_ptr. STR is the string to store. Do not copy
9165 characters that yield more columns than PRECISION; PRECISION <= 0
9166 means copy the whole string. Pad with spaces until FIELD_WIDTH
9167 number of characters have been copied; FIELD_WIDTH <= 0 means don't
9168 pad. Called from display_mode_element when it is used to build a
9169 frame title. */
9170
9171 static int
9172 store_mode_line_noprop (str, field_width, precision)
9173 const unsigned char *str;
9174 int field_width, precision;
9175 {
9176 int n = 0;
9177 int dummy, nbytes;
9178
9179 /* Copy at most PRECISION chars from STR. */
9180 nbytes = strlen (str);
9181 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
9182 while (nbytes--)
9183 store_mode_line_noprop_char (*str++);
9184
9185 /* Fill up with spaces until FIELD_WIDTH reached. */
9186 while (field_width > 0
9187 && n < field_width)
9188 {
9189 store_mode_line_noprop_char (' ');
9190 ++n;
9191 }
9192
9193 return n;
9194 }
9195
9196 /***********************************************************************
9197 Frame Titles
9198 ***********************************************************************/
9199
9200 #ifdef HAVE_WINDOW_SYSTEM
9201
9202 /* Set the title of FRAME, if it has changed. The title format is
9203 Vicon_title_format if FRAME is iconified, otherwise it is
9204 frame_title_format. */
9205
9206 static void
9207 x_consider_frame_title (frame)
9208 Lisp_Object frame;
9209 {
9210 struct frame *f = XFRAME (frame);
9211
9212 if (FRAME_WINDOW_P (f)
9213 || FRAME_MINIBUF_ONLY_P (f)
9214 || f->explicit_name)
9215 {
9216 /* Do we have more than one visible frame on this X display? */
9217 Lisp_Object tail;
9218 Lisp_Object fmt;
9219 int title_start;
9220 char *title;
9221 int len;
9222 struct it it;
9223 int count = SPECPDL_INDEX ();
9224
9225 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
9226 {
9227 Lisp_Object other_frame = XCAR (tail);
9228 struct frame *tf = XFRAME (other_frame);
9229
9230 if (tf != f
9231 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
9232 && !FRAME_MINIBUF_ONLY_P (tf)
9233 && !EQ (other_frame, tip_frame)
9234 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
9235 break;
9236 }
9237
9238 /* Set global variable indicating that multiple frames exist. */
9239 multiple_frames = CONSP (tail);
9240
9241 /* Switch to the buffer of selected window of the frame. Set up
9242 mode_line_target so that display_mode_element will output into
9243 mode_line_noprop_buf; then display the title. */
9244 record_unwind_protect (unwind_format_mode_line,
9245 format_mode_line_unwind_data (current_buffer, 0));
9246
9247 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
9248 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
9249
9250 mode_line_target = MODE_LINE_TITLE;
9251 title_start = MODE_LINE_NOPROP_LEN (0);
9252 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
9253 NULL, DEFAULT_FACE_ID);
9254 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
9255 len = MODE_LINE_NOPROP_LEN (title_start);
9256 title = mode_line_noprop_buf + title_start;
9257 unbind_to (count, Qnil);
9258
9259 /* Set the title only if it's changed. This avoids consing in
9260 the common case where it hasn't. (If it turns out that we've
9261 already wasted too much time by walking through the list with
9262 display_mode_element, then we might need to optimize at a
9263 higher level than this.) */
9264 if (! STRINGP (f->name)
9265 || SBYTES (f->name) != len
9266 || bcmp (title, SDATA (f->name), len) != 0)
9267 x_implicitly_set_name (f, make_string (title, len), Qnil);
9268 }
9269 }
9270
9271 #endif /* not HAVE_WINDOW_SYSTEM */
9272
9273
9274
9275 \f
9276 /***********************************************************************
9277 Menu Bars
9278 ***********************************************************************/
9279
9280
9281 /* Prepare for redisplay by updating menu-bar item lists when
9282 appropriate. This can call eval. */
9283
9284 void
9285 prepare_menu_bars ()
9286 {
9287 int all_windows;
9288 struct gcpro gcpro1, gcpro2;
9289 struct frame *f;
9290 Lisp_Object tooltip_frame;
9291
9292 #ifdef HAVE_WINDOW_SYSTEM
9293 tooltip_frame = tip_frame;
9294 #else
9295 tooltip_frame = Qnil;
9296 #endif
9297
9298 /* Update all frame titles based on their buffer names, etc. We do
9299 this before the menu bars so that the buffer-menu will show the
9300 up-to-date frame titles. */
9301 #ifdef HAVE_WINDOW_SYSTEM
9302 if (windows_or_buffers_changed || update_mode_lines)
9303 {
9304 Lisp_Object tail, frame;
9305
9306 FOR_EACH_FRAME (tail, frame)
9307 {
9308 f = XFRAME (frame);
9309 if (!EQ (frame, tooltip_frame)
9310 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
9311 x_consider_frame_title (frame);
9312 }
9313 }
9314 #endif /* HAVE_WINDOW_SYSTEM */
9315
9316 /* Update the menu bar item lists, if appropriate. This has to be
9317 done before any actual redisplay or generation of display lines. */
9318 all_windows = (update_mode_lines
9319 || buffer_shared > 1
9320 || windows_or_buffers_changed);
9321 if (all_windows)
9322 {
9323 Lisp_Object tail, frame;
9324 int count = SPECPDL_INDEX ();
9325 /* 1 means that update_menu_bar has run its hooks
9326 so any further calls to update_menu_bar shouldn't do so again. */
9327 int menu_bar_hooks_run = 0;
9328
9329 record_unwind_save_match_data ();
9330
9331 FOR_EACH_FRAME (tail, frame)
9332 {
9333 f = XFRAME (frame);
9334
9335 /* Ignore tooltip frame. */
9336 if (EQ (frame, tooltip_frame))
9337 continue;
9338
9339 /* If a window on this frame changed size, report that to
9340 the user and clear the size-change flag. */
9341 if (FRAME_WINDOW_SIZES_CHANGED (f))
9342 {
9343 Lisp_Object functions;
9344
9345 /* Clear flag first in case we get an error below. */
9346 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
9347 functions = Vwindow_size_change_functions;
9348 GCPRO2 (tail, functions);
9349
9350 while (CONSP (functions))
9351 {
9352 call1 (XCAR (functions), frame);
9353 functions = XCDR (functions);
9354 }
9355 UNGCPRO;
9356 }
9357
9358 GCPRO1 (tail);
9359 menu_bar_hooks_run = update_menu_bar (f, 0, menu_bar_hooks_run);
9360 #ifdef HAVE_WINDOW_SYSTEM
9361 update_tool_bar (f, 0);
9362 #ifdef MAC_OS
9363 mac_update_title_bar (f, 0);
9364 #endif
9365 #endif
9366 UNGCPRO;
9367 }
9368
9369 unbind_to (count, Qnil);
9370 }
9371 else
9372 {
9373 struct frame *sf = SELECTED_FRAME ();
9374 update_menu_bar (sf, 1, 0);
9375 #ifdef HAVE_WINDOW_SYSTEM
9376 update_tool_bar (sf, 1);
9377 #ifdef MAC_OS
9378 mac_update_title_bar (sf, 1);
9379 #endif
9380 #endif
9381 }
9382
9383 /* Motif needs this. See comment in xmenu.c. Turn it off when
9384 pending_menu_activation is not defined. */
9385 #ifdef USE_X_TOOLKIT
9386 pending_menu_activation = 0;
9387 #endif
9388 }
9389
9390
9391 /* Update the menu bar item list for frame F. This has to be done
9392 before we start to fill in any display lines, because it can call
9393 eval.
9394
9395 If SAVE_MATCH_DATA is non-zero, we must save and restore it here.
9396
9397 If HOOKS_RUN is 1, that means a previous call to update_menu_bar
9398 already ran the menu bar hooks for this redisplay, so there
9399 is no need to run them again. The return value is the
9400 updated value of this flag, to pass to the next call. */
9401
9402 static int
9403 update_menu_bar (f, save_match_data, hooks_run)
9404 struct frame *f;
9405 int save_match_data;
9406 int hooks_run;
9407 {
9408 Lisp_Object window;
9409 register struct window *w;
9410
9411 /* If called recursively during a menu update, do nothing. This can
9412 happen when, for instance, an activate-menubar-hook causes a
9413 redisplay. */
9414 if (inhibit_menubar_update)
9415 return hooks_run;
9416
9417 window = FRAME_SELECTED_WINDOW (f);
9418 w = XWINDOW (window);
9419
9420 #if 0 /* The if statement below this if statement used to include the
9421 condition !NILP (w->update_mode_line), rather than using
9422 update_mode_lines directly, and this if statement may have
9423 been added to make that condition work. Now the if
9424 statement below matches its comment, this isn't needed. */
9425 if (update_mode_lines)
9426 w->update_mode_line = Qt;
9427 #endif
9428
9429 if (FRAME_WINDOW_P (f)
9430 ?
9431 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
9432 || defined (USE_GTK)
9433 FRAME_EXTERNAL_MENU_BAR (f)
9434 #else
9435 FRAME_MENU_BAR_LINES (f) > 0
9436 #endif
9437 : FRAME_MENU_BAR_LINES (f) > 0)
9438 {
9439 /* If the user has switched buffers or windows, we need to
9440 recompute to reflect the new bindings. But we'll
9441 recompute when update_mode_lines is set too; that means
9442 that people can use force-mode-line-update to request
9443 that the menu bar be recomputed. The adverse effect on
9444 the rest of the redisplay algorithm is about the same as
9445 windows_or_buffers_changed anyway. */
9446 if (windows_or_buffers_changed
9447 /* This used to test w->update_mode_line, but we believe
9448 there is no need to recompute the menu in that case. */
9449 || update_mode_lines
9450 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
9451 < BUF_MODIFF (XBUFFER (w->buffer)))
9452 != !NILP (w->last_had_star))
9453 || ((!NILP (Vtransient_mark_mode)
9454 && !NILP (XBUFFER (w->buffer)->mark_active))
9455 != !NILP (w->region_showing)))
9456 {
9457 struct buffer *prev = current_buffer;
9458 int count = SPECPDL_INDEX ();
9459
9460 specbind (Qinhibit_menubar_update, Qt);
9461
9462 set_buffer_internal_1 (XBUFFER (w->buffer));
9463 if (save_match_data)
9464 record_unwind_save_match_data ();
9465 if (NILP (Voverriding_local_map_menu_flag))
9466 {
9467 specbind (Qoverriding_terminal_local_map, Qnil);
9468 specbind (Qoverriding_local_map, Qnil);
9469 }
9470
9471 if (!hooks_run)
9472 {
9473 /* Run the Lucid hook. */
9474 safe_run_hooks (Qactivate_menubar_hook);
9475
9476 /* If it has changed current-menubar from previous value,
9477 really recompute the menu-bar from the value. */
9478 if (! NILP (Vlucid_menu_bar_dirty_flag))
9479 call0 (Qrecompute_lucid_menubar);
9480
9481 safe_run_hooks (Qmenu_bar_update_hook);
9482
9483 hooks_run = 1;
9484 }
9485
9486 XSETFRAME (Vmenu_updating_frame, f);
9487 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
9488
9489 /* Redisplay the menu bar in case we changed it. */
9490 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
9491 || defined (USE_GTK)
9492 if (FRAME_WINDOW_P (f))
9493 {
9494 #ifdef MAC_OS
9495 /* All frames on Mac OS share the same menubar. So only
9496 the selected frame should be allowed to set it. */
9497 if (f == SELECTED_FRAME ())
9498 #endif
9499 set_frame_menubar (f, 0, 0);
9500 }
9501 else
9502 /* On a terminal screen, the menu bar is an ordinary screen
9503 line, and this makes it get updated. */
9504 w->update_mode_line = Qt;
9505 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
9506 /* In the non-toolkit version, the menu bar is an ordinary screen
9507 line, and this makes it get updated. */
9508 w->update_mode_line = Qt;
9509 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
9510
9511 unbind_to (count, Qnil);
9512 set_buffer_internal_1 (prev);
9513 }
9514 }
9515
9516 return hooks_run;
9517 }
9518
9519
9520 \f
9521 /***********************************************************************
9522 Output Cursor
9523 ***********************************************************************/
9524
9525 #ifdef HAVE_WINDOW_SYSTEM
9526
9527 /* EXPORT:
9528 Nominal cursor position -- where to draw output.
9529 HPOS and VPOS are window relative glyph matrix coordinates.
9530 X and Y are window relative pixel coordinates. */
9531
9532 struct cursor_pos output_cursor;
9533
9534
9535 /* EXPORT:
9536 Set the global variable output_cursor to CURSOR. All cursor
9537 positions are relative to updated_window. */
9538
9539 void
9540 set_output_cursor (cursor)
9541 struct cursor_pos *cursor;
9542 {
9543 output_cursor.hpos = cursor->hpos;
9544 output_cursor.vpos = cursor->vpos;
9545 output_cursor.x = cursor->x;
9546 output_cursor.y = cursor->y;
9547 }
9548
9549
9550 /* EXPORT for RIF:
9551 Set a nominal cursor position.
9552
9553 HPOS and VPOS are column/row positions in a window glyph matrix. X
9554 and Y are window text area relative pixel positions.
9555
9556 If this is done during an update, updated_window will contain the
9557 window that is being updated and the position is the future output
9558 cursor position for that window. If updated_window is null, use
9559 selected_window and display the cursor at the given position. */
9560
9561 void
9562 x_cursor_to (vpos, hpos, y, x)
9563 int vpos, hpos, y, x;
9564 {
9565 struct window *w;
9566
9567 /* If updated_window is not set, work on selected_window. */
9568 if (updated_window)
9569 w = updated_window;
9570 else
9571 w = XWINDOW (selected_window);
9572
9573 /* Set the output cursor. */
9574 output_cursor.hpos = hpos;
9575 output_cursor.vpos = vpos;
9576 output_cursor.x = x;
9577 output_cursor.y = y;
9578
9579 /* If not called as part of an update, really display the cursor.
9580 This will also set the cursor position of W. */
9581 if (updated_window == NULL)
9582 {
9583 BLOCK_INPUT;
9584 display_and_set_cursor (w, 1, hpos, vpos, x, y);
9585 if (FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
9586 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (SELECTED_FRAME ());
9587 UNBLOCK_INPUT;
9588 }
9589 }
9590
9591 #endif /* HAVE_WINDOW_SYSTEM */
9592
9593 \f
9594 /***********************************************************************
9595 Tool-bars
9596 ***********************************************************************/
9597
9598 #ifdef HAVE_WINDOW_SYSTEM
9599
9600 /* Where the mouse was last time we reported a mouse event. */
9601
9602 FRAME_PTR last_mouse_frame;
9603
9604 /* Tool-bar item index of the item on which a mouse button was pressed
9605 or -1. */
9606
9607 int last_tool_bar_item;
9608
9609
9610 /* Update the tool-bar item list for frame F. This has to be done
9611 before we start to fill in any display lines. Called from
9612 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
9613 and restore it here. */
9614
9615 static void
9616 update_tool_bar (f, save_match_data)
9617 struct frame *f;
9618 int save_match_data;
9619 {
9620 #if defined (USE_GTK) || USE_MAC_TOOLBAR
9621 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
9622 #else
9623 int do_update = WINDOWP (f->tool_bar_window)
9624 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
9625 #endif
9626
9627 if (do_update)
9628 {
9629 Lisp_Object window;
9630 struct window *w;
9631
9632 window = FRAME_SELECTED_WINDOW (f);
9633 w = XWINDOW (window);
9634
9635 /* If the user has switched buffers or windows, we need to
9636 recompute to reflect the new bindings. But we'll
9637 recompute when update_mode_lines is set too; that means
9638 that people can use force-mode-line-update to request
9639 that the menu bar be recomputed. The adverse effect on
9640 the rest of the redisplay algorithm is about the same as
9641 windows_or_buffers_changed anyway. */
9642 if (windows_or_buffers_changed
9643 || !NILP (w->update_mode_line)
9644 || update_mode_lines
9645 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
9646 < BUF_MODIFF (XBUFFER (w->buffer)))
9647 != !NILP (w->last_had_star))
9648 || ((!NILP (Vtransient_mark_mode)
9649 && !NILP (XBUFFER (w->buffer)->mark_active))
9650 != !NILP (w->region_showing)))
9651 {
9652 struct buffer *prev = current_buffer;
9653 int count = SPECPDL_INDEX ();
9654 Lisp_Object new_tool_bar;
9655 int new_n_tool_bar;
9656 struct gcpro gcpro1;
9657
9658 /* Set current_buffer to the buffer of the selected
9659 window of the frame, so that we get the right local
9660 keymaps. */
9661 set_buffer_internal_1 (XBUFFER (w->buffer));
9662
9663 /* Save match data, if we must. */
9664 if (save_match_data)
9665 record_unwind_save_match_data ();
9666
9667 /* Make sure that we don't accidentally use bogus keymaps. */
9668 if (NILP (Voverriding_local_map_menu_flag))
9669 {
9670 specbind (Qoverriding_terminal_local_map, Qnil);
9671 specbind (Qoverriding_local_map, Qnil);
9672 }
9673
9674 GCPRO1 (new_tool_bar);
9675
9676 /* Build desired tool-bar items from keymaps. */
9677 new_tool_bar = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
9678 &new_n_tool_bar);
9679
9680 /* Redisplay the tool-bar if we changed it. */
9681 if (new_n_tool_bar != f->n_tool_bar_items
9682 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
9683 {
9684 /* Redisplay that happens asynchronously due to an expose event
9685 may access f->tool_bar_items. Make sure we update both
9686 variables within BLOCK_INPUT so no such event interrupts. */
9687 BLOCK_INPUT;
9688 f->tool_bar_items = new_tool_bar;
9689 f->n_tool_bar_items = new_n_tool_bar;
9690 w->update_mode_line = Qt;
9691 UNBLOCK_INPUT;
9692 }
9693
9694 UNGCPRO;
9695
9696 unbind_to (count, Qnil);
9697 set_buffer_internal_1 (prev);
9698 }
9699 }
9700 }
9701
9702
9703 /* Set F->desired_tool_bar_string to a Lisp string representing frame
9704 F's desired tool-bar contents. F->tool_bar_items must have
9705 been set up previously by calling prepare_menu_bars. */
9706
9707 static void
9708 build_desired_tool_bar_string (f)
9709 struct frame *f;
9710 {
9711 int i, size, size_needed;
9712 struct gcpro gcpro1, gcpro2, gcpro3;
9713 Lisp_Object image, plist, props;
9714
9715 image = plist = props = Qnil;
9716 GCPRO3 (image, plist, props);
9717
9718 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
9719 Otherwise, make a new string. */
9720
9721 /* The size of the string we might be able to reuse. */
9722 size = (STRINGP (f->desired_tool_bar_string)
9723 ? SCHARS (f->desired_tool_bar_string)
9724 : 0);
9725
9726 /* We need one space in the string for each image. */
9727 size_needed = f->n_tool_bar_items;
9728
9729 /* Reuse f->desired_tool_bar_string, if possible. */
9730 if (size < size_needed || NILP (f->desired_tool_bar_string))
9731 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
9732 make_number (' '));
9733 else
9734 {
9735 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
9736 Fremove_text_properties (make_number (0), make_number (size),
9737 props, f->desired_tool_bar_string);
9738 }
9739
9740 /* Put a `display' property on the string for the images to display,
9741 put a `menu_item' property on tool-bar items with a value that
9742 is the index of the item in F's tool-bar item vector. */
9743 for (i = 0; i < f->n_tool_bar_items; ++i)
9744 {
9745 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
9746
9747 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
9748 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
9749 int hmargin, vmargin, relief, idx, end;
9750 extern Lisp_Object QCrelief, QCmargin, QCconversion;
9751
9752 /* If image is a vector, choose the image according to the
9753 button state. */
9754 image = PROP (TOOL_BAR_ITEM_IMAGES);
9755 if (VECTORP (image))
9756 {
9757 if (enabled_p)
9758 idx = (selected_p
9759 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
9760 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
9761 else
9762 idx = (selected_p
9763 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
9764 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
9765
9766 xassert (ASIZE (image) >= idx);
9767 image = AREF (image, idx);
9768 }
9769 else
9770 idx = -1;
9771
9772 /* Ignore invalid image specifications. */
9773 if (!valid_image_p (image))
9774 continue;
9775
9776 /* Display the tool-bar button pressed, or depressed. */
9777 plist = Fcopy_sequence (XCDR (image));
9778
9779 /* Compute margin and relief to draw. */
9780 relief = (tool_bar_button_relief >= 0
9781 ? tool_bar_button_relief
9782 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
9783 hmargin = vmargin = relief;
9784
9785 if (INTEGERP (Vtool_bar_button_margin)
9786 && XINT (Vtool_bar_button_margin) > 0)
9787 {
9788 hmargin += XFASTINT (Vtool_bar_button_margin);
9789 vmargin += XFASTINT (Vtool_bar_button_margin);
9790 }
9791 else if (CONSP (Vtool_bar_button_margin))
9792 {
9793 if (INTEGERP (XCAR (Vtool_bar_button_margin))
9794 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
9795 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
9796
9797 if (INTEGERP (XCDR (Vtool_bar_button_margin))
9798 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
9799 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
9800 }
9801
9802 if (auto_raise_tool_bar_buttons_p)
9803 {
9804 /* Add a `:relief' property to the image spec if the item is
9805 selected. */
9806 if (selected_p)
9807 {
9808 plist = Fplist_put (plist, QCrelief, make_number (-relief));
9809 hmargin -= relief;
9810 vmargin -= relief;
9811 }
9812 }
9813 else
9814 {
9815 /* If image is selected, display it pressed, i.e. with a
9816 negative relief. If it's not selected, display it with a
9817 raised relief. */
9818 plist = Fplist_put (plist, QCrelief,
9819 (selected_p
9820 ? make_number (-relief)
9821 : make_number (relief)));
9822 hmargin -= relief;
9823 vmargin -= relief;
9824 }
9825
9826 /* Put a margin around the image. */
9827 if (hmargin || vmargin)
9828 {
9829 if (hmargin == vmargin)
9830 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
9831 else
9832 plist = Fplist_put (plist, QCmargin,
9833 Fcons (make_number (hmargin),
9834 make_number (vmargin)));
9835 }
9836
9837 /* If button is not enabled, and we don't have special images
9838 for the disabled state, make the image appear disabled by
9839 applying an appropriate algorithm to it. */
9840 if (!enabled_p && idx < 0)
9841 plist = Fplist_put (plist, QCconversion, Qdisabled);
9842
9843 /* Put a `display' text property on the string for the image to
9844 display. Put a `menu-item' property on the string that gives
9845 the start of this item's properties in the tool-bar items
9846 vector. */
9847 image = Fcons (Qimage, plist);
9848 props = list4 (Qdisplay, image,
9849 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
9850
9851 /* Let the last image hide all remaining spaces in the tool bar
9852 string. The string can be longer than needed when we reuse a
9853 previous string. */
9854 if (i + 1 == f->n_tool_bar_items)
9855 end = SCHARS (f->desired_tool_bar_string);
9856 else
9857 end = i + 1;
9858 Fadd_text_properties (make_number (i), make_number (end),
9859 props, f->desired_tool_bar_string);
9860 #undef PROP
9861 }
9862
9863 UNGCPRO;
9864 }
9865
9866
9867 /* Display one line of the tool-bar of frame IT->f.
9868
9869 HEIGHT specifies the desired height of the tool-bar line.
9870 If the actual height of the glyph row is less than HEIGHT, the
9871 row's height is increased to HEIGHT, and the icons are centered
9872 vertically in the new height.
9873
9874 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
9875 count a final empty row in case the tool-bar width exactly matches
9876 the window width.
9877 */
9878
9879 static void
9880 display_tool_bar_line (it, height)
9881 struct it *it;
9882 int height;
9883 {
9884 struct glyph_row *row = it->glyph_row;
9885 int max_x = it->last_visible_x;
9886 struct glyph *last;
9887
9888 prepare_desired_row (row);
9889 row->y = it->current_y;
9890
9891 /* Note that this isn't made use of if the face hasn't a box,
9892 so there's no need to check the face here. */
9893 it->start_of_box_run_p = 1;
9894
9895 while (it->current_x < max_x)
9896 {
9897 int x, n_glyphs_before, i, nglyphs;
9898 struct it it_before;
9899
9900 /* Get the next display element. */
9901 if (!get_next_display_element (it))
9902 {
9903 /* Don't count empty row if we are counting needed tool-bar lines. */
9904 if (height < 0 && !it->hpos)
9905 return;
9906 break;
9907 }
9908
9909 /* Produce glyphs. */
9910 n_glyphs_before = row->used[TEXT_AREA];
9911 it_before = *it;
9912
9913 PRODUCE_GLYPHS (it);
9914
9915 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
9916 i = 0;
9917 x = it_before.current_x;
9918 while (i < nglyphs)
9919 {
9920 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
9921
9922 if (x + glyph->pixel_width > max_x)
9923 {
9924 /* Glyph doesn't fit on line. Backtrack. */
9925 row->used[TEXT_AREA] = n_glyphs_before;
9926 *it = it_before;
9927 /* If this is the only glyph on this line, it will never fit on the
9928 toolbar, so skip it. But ensure there is at least one glyph,
9929 so we don't accidentally disable the tool-bar. */
9930 if (n_glyphs_before == 0
9931 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
9932 break;
9933 goto out;
9934 }
9935
9936 ++it->hpos;
9937 x += glyph->pixel_width;
9938 ++i;
9939 }
9940
9941 /* Stop at line ends. */
9942 if (ITERATOR_AT_END_OF_LINE_P (it))
9943 break;
9944
9945 set_iterator_to_next (it, 1);
9946 }
9947
9948 out:;
9949
9950 row->displays_text_p = row->used[TEXT_AREA] != 0;
9951
9952 /* Use default face for the border below the tool bar.
9953
9954 FIXME: When auto-resize-tool-bars is grow-only, there is
9955 no additional border below the possibly empty tool-bar lines.
9956 So to make the extra empty lines look "normal", we have to
9957 use the tool-bar face for the border too. */
9958 if (!row->displays_text_p && !EQ (Vauto_resize_tool_bars, Qgrow_only))
9959 it->face_id = DEFAULT_FACE_ID;
9960
9961 extend_face_to_end_of_line (it);
9962 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
9963 last->right_box_line_p = 1;
9964 if (last == row->glyphs[TEXT_AREA])
9965 last->left_box_line_p = 1;
9966
9967 /* Make line the desired height and center it vertically. */
9968 if ((height -= it->max_ascent + it->max_descent) > 0)
9969 {
9970 /* Don't add more than one line height. */
9971 height %= FRAME_LINE_HEIGHT (it->f);
9972 it->max_ascent += height / 2;
9973 it->max_descent += (height + 1) / 2;
9974 }
9975
9976 compute_line_metrics (it);
9977
9978 /* If line is empty, make it occupy the rest of the tool-bar. */
9979 if (!row->displays_text_p)
9980 {
9981 row->height = row->phys_height = it->last_visible_y - row->y;
9982 row->visible_height = row->height;
9983 row->ascent = row->phys_ascent = 0;
9984 row->extra_line_spacing = 0;
9985 }
9986
9987 row->full_width_p = 1;
9988 row->continued_p = 0;
9989 row->truncated_on_left_p = 0;
9990 row->truncated_on_right_p = 0;
9991
9992 it->current_x = it->hpos = 0;
9993 it->current_y += row->height;
9994 ++it->vpos;
9995 ++it->glyph_row;
9996 }
9997
9998
9999 /* Max tool-bar height. */
10000
10001 #define MAX_FRAME_TOOL_BAR_HEIGHT(f) \
10002 ((FRAME_LINE_HEIGHT (f) * FRAME_LINES (f)))
10003
10004 /* Value is the number of screen lines needed to make all tool-bar
10005 items of frame F visible. The number of actual rows needed is
10006 returned in *N_ROWS if non-NULL. */
10007
10008 static int
10009 tool_bar_lines_needed (f, n_rows)
10010 struct frame *f;
10011 int *n_rows;
10012 {
10013 struct window *w = XWINDOW (f->tool_bar_window);
10014 struct it it;
10015 /* tool_bar_lines_needed is called from redisplay_tool_bar after building
10016 the desired matrix, so use (unused) mode-line row as temporary row to
10017 avoid destroying the first tool-bar row. */
10018 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
10019
10020 /* Initialize an iterator for iteration over
10021 F->desired_tool_bar_string in the tool-bar window of frame F. */
10022 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
10023 it.first_visible_x = 0;
10024 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
10025 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
10026
10027 while (!ITERATOR_AT_END_P (&it))
10028 {
10029 clear_glyph_row (temp_row);
10030 it.glyph_row = temp_row;
10031 display_tool_bar_line (&it, -1);
10032 }
10033 clear_glyph_row (temp_row);
10034
10035 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
10036 if (n_rows)
10037 *n_rows = it.vpos > 0 ? it.vpos : -1;
10038
10039 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
10040 }
10041
10042
10043 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
10044 0, 1, 0,
10045 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
10046 (frame)
10047 Lisp_Object frame;
10048 {
10049 struct frame *f;
10050 struct window *w;
10051 int nlines = 0;
10052
10053 if (NILP (frame))
10054 frame = selected_frame;
10055 else
10056 CHECK_FRAME (frame);
10057 f = XFRAME (frame);
10058
10059 if (WINDOWP (f->tool_bar_window)
10060 || (w = XWINDOW (f->tool_bar_window),
10061 WINDOW_TOTAL_LINES (w) > 0))
10062 {
10063 update_tool_bar (f, 1);
10064 if (f->n_tool_bar_items)
10065 {
10066 build_desired_tool_bar_string (f);
10067 nlines = tool_bar_lines_needed (f, NULL);
10068 }
10069 }
10070
10071 return make_number (nlines);
10072 }
10073
10074
10075 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
10076 height should be changed. */
10077
10078 static int
10079 redisplay_tool_bar (f)
10080 struct frame *f;
10081 {
10082 struct window *w;
10083 struct it it;
10084 struct glyph_row *row;
10085
10086 #if defined (USE_GTK) || USE_MAC_TOOLBAR
10087 if (FRAME_EXTERNAL_TOOL_BAR (f))
10088 update_frame_tool_bar (f);
10089 return 0;
10090 #endif
10091
10092 /* If frame hasn't a tool-bar window or if it is zero-height, don't
10093 do anything. This means you must start with tool-bar-lines
10094 non-zero to get the auto-sizing effect. Or in other words, you
10095 can turn off tool-bars by specifying tool-bar-lines zero. */
10096 if (!WINDOWP (f->tool_bar_window)
10097 || (w = XWINDOW (f->tool_bar_window),
10098 WINDOW_TOTAL_LINES (w) == 0))
10099 return 0;
10100
10101 /* Set up an iterator for the tool-bar window. */
10102 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
10103 it.first_visible_x = 0;
10104 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
10105 row = it.glyph_row;
10106
10107 /* Build a string that represents the contents of the tool-bar. */
10108 build_desired_tool_bar_string (f);
10109 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
10110
10111 if (f->n_tool_bar_rows == 0)
10112 {
10113 int nlines;
10114
10115 if ((nlines = tool_bar_lines_needed (f, &f->n_tool_bar_rows),
10116 nlines != WINDOW_TOTAL_LINES (w)))
10117 {
10118 extern Lisp_Object Qtool_bar_lines;
10119 Lisp_Object frame;
10120 int old_height = WINDOW_TOTAL_LINES (w);
10121
10122 XSETFRAME (frame, f);
10123 Fmodify_frame_parameters (frame,
10124 Fcons (Fcons (Qtool_bar_lines,
10125 make_number (nlines)),
10126 Qnil));
10127 if (WINDOW_TOTAL_LINES (w) != old_height)
10128 {
10129 clear_glyph_matrix (w->desired_matrix);
10130 fonts_changed_p = 1;
10131 return 1;
10132 }
10133 }
10134 }
10135
10136 /* Display as many lines as needed to display all tool-bar items. */
10137
10138 if (f->n_tool_bar_rows > 0)
10139 {
10140 int border, rows, height, extra;
10141
10142 if (INTEGERP (Vtool_bar_border))
10143 border = XINT (Vtool_bar_border);
10144 else if (EQ (Vtool_bar_border, Qinternal_border_width))
10145 border = FRAME_INTERNAL_BORDER_WIDTH (f);
10146 else if (EQ (Vtool_bar_border, Qborder_width))
10147 border = f->border_width;
10148 else
10149 border = 0;
10150 if (border < 0)
10151 border = 0;
10152
10153 rows = f->n_tool_bar_rows;
10154 height = max (1, (it.last_visible_y - border) / rows);
10155 extra = it.last_visible_y - border - height * rows;
10156
10157 while (it.current_y < it.last_visible_y)
10158 {
10159 int h = 0;
10160 if (extra > 0 && rows-- > 0)
10161 {
10162 h = (extra + rows - 1) / rows;
10163 extra -= h;
10164 }
10165 display_tool_bar_line (&it, height + h);
10166 }
10167 }
10168 else
10169 {
10170 while (it.current_y < it.last_visible_y)
10171 display_tool_bar_line (&it, 0);
10172 }
10173
10174 /* It doesn't make much sense to try scrolling in the tool-bar
10175 window, so don't do it. */
10176 w->desired_matrix->no_scrolling_p = 1;
10177 w->must_be_updated_p = 1;
10178
10179 if (!NILP (Vauto_resize_tool_bars))
10180 {
10181 int max_tool_bar_height = MAX_FRAME_TOOL_BAR_HEIGHT (f);
10182 int change_height_p = 0;
10183
10184 /* If we couldn't display everything, change the tool-bar's
10185 height if there is room for more. */
10186 if (IT_STRING_CHARPOS (it) < it.end_charpos
10187 && it.current_y < max_tool_bar_height)
10188 change_height_p = 1;
10189
10190 row = it.glyph_row - 1;
10191
10192 /* If there are blank lines at the end, except for a partially
10193 visible blank line at the end that is smaller than
10194 FRAME_LINE_HEIGHT, change the tool-bar's height. */
10195 if (!row->displays_text_p
10196 && row->height >= FRAME_LINE_HEIGHT (f))
10197 change_height_p = 1;
10198
10199 /* If row displays tool-bar items, but is partially visible,
10200 change the tool-bar's height. */
10201 if (row->displays_text_p
10202 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y
10203 && MATRIX_ROW_BOTTOM_Y (row) < max_tool_bar_height)
10204 change_height_p = 1;
10205
10206 /* Resize windows as needed by changing the `tool-bar-lines'
10207 frame parameter. */
10208 if (change_height_p)
10209 {
10210 extern Lisp_Object Qtool_bar_lines;
10211 Lisp_Object frame;
10212 int old_height = WINDOW_TOTAL_LINES (w);
10213 int nrows;
10214 int nlines = tool_bar_lines_needed (f, &nrows);
10215
10216 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
10217 && !f->minimize_tool_bar_window_p)
10218 ? (nlines > old_height)
10219 : (nlines != old_height));
10220 f->minimize_tool_bar_window_p = 0;
10221
10222 if (change_height_p)
10223 {
10224 XSETFRAME (frame, f);
10225 Fmodify_frame_parameters (frame,
10226 Fcons (Fcons (Qtool_bar_lines,
10227 make_number (nlines)),
10228 Qnil));
10229 if (WINDOW_TOTAL_LINES (w) != old_height)
10230 {
10231 clear_glyph_matrix (w->desired_matrix);
10232 f->n_tool_bar_rows = nrows;
10233 fonts_changed_p = 1;
10234 return 1;
10235 }
10236 }
10237 }
10238 }
10239
10240 f->minimize_tool_bar_window_p = 0;
10241 return 0;
10242 }
10243
10244
10245 /* Get information about the tool-bar item which is displayed in GLYPH
10246 on frame F. Return in *PROP_IDX the index where tool-bar item
10247 properties start in F->tool_bar_items. Value is zero if
10248 GLYPH doesn't display a tool-bar item. */
10249
10250 static int
10251 tool_bar_item_info (f, glyph, prop_idx)
10252 struct frame *f;
10253 struct glyph *glyph;
10254 int *prop_idx;
10255 {
10256 Lisp_Object prop;
10257 int success_p;
10258 int charpos;
10259
10260 /* This function can be called asynchronously, which means we must
10261 exclude any possibility that Fget_text_property signals an
10262 error. */
10263 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
10264 charpos = max (0, charpos);
10265
10266 /* Get the text property `menu-item' at pos. The value of that
10267 property is the start index of this item's properties in
10268 F->tool_bar_items. */
10269 prop = Fget_text_property (make_number (charpos),
10270 Qmenu_item, f->current_tool_bar_string);
10271 if (INTEGERP (prop))
10272 {
10273 *prop_idx = XINT (prop);
10274 success_p = 1;
10275 }
10276 else
10277 success_p = 0;
10278
10279 return success_p;
10280 }
10281
10282 \f
10283 /* Get information about the tool-bar item at position X/Y on frame F.
10284 Return in *GLYPH a pointer to the glyph of the tool-bar item in
10285 the current matrix of the tool-bar window of F, or NULL if not
10286 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
10287 item in F->tool_bar_items. Value is
10288
10289 -1 if X/Y is not on a tool-bar item
10290 0 if X/Y is on the same item that was highlighted before.
10291 1 otherwise. */
10292
10293 static int
10294 get_tool_bar_item (f, x, y, glyph, hpos, vpos, prop_idx)
10295 struct frame *f;
10296 int x, y;
10297 struct glyph **glyph;
10298 int *hpos, *vpos, *prop_idx;
10299 {
10300 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10301 struct window *w = XWINDOW (f->tool_bar_window);
10302 int area;
10303
10304 /* Find the glyph under X/Y. */
10305 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
10306 if (*glyph == NULL)
10307 return -1;
10308
10309 /* Get the start of this tool-bar item's properties in
10310 f->tool_bar_items. */
10311 if (!tool_bar_item_info (f, *glyph, prop_idx))
10312 return -1;
10313
10314 /* Is mouse on the highlighted item? */
10315 if (EQ (f->tool_bar_window, dpyinfo->mouse_face_window)
10316 && *vpos >= dpyinfo->mouse_face_beg_row
10317 && *vpos <= dpyinfo->mouse_face_end_row
10318 && (*vpos > dpyinfo->mouse_face_beg_row
10319 || *hpos >= dpyinfo->mouse_face_beg_col)
10320 && (*vpos < dpyinfo->mouse_face_end_row
10321 || *hpos < dpyinfo->mouse_face_end_col
10322 || dpyinfo->mouse_face_past_end))
10323 return 0;
10324
10325 return 1;
10326 }
10327
10328
10329 /* EXPORT:
10330 Handle mouse button event on the tool-bar of frame F, at
10331 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
10332 0 for button release. MODIFIERS is event modifiers for button
10333 release. */
10334
10335 void
10336 handle_tool_bar_click (f, x, y, down_p, modifiers)
10337 struct frame *f;
10338 int x, y, down_p;
10339 unsigned int modifiers;
10340 {
10341 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10342 struct window *w = XWINDOW (f->tool_bar_window);
10343 int hpos, vpos, prop_idx;
10344 struct glyph *glyph;
10345 Lisp_Object enabled_p;
10346
10347 /* If not on the highlighted tool-bar item, return. */
10348 frame_to_window_pixel_xy (w, &x, &y);
10349 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
10350 return;
10351
10352 /* If item is disabled, do nothing. */
10353 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
10354 if (NILP (enabled_p))
10355 return;
10356
10357 if (down_p)
10358 {
10359 /* Show item in pressed state. */
10360 show_mouse_face (dpyinfo, DRAW_IMAGE_SUNKEN);
10361 dpyinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
10362 last_tool_bar_item = prop_idx;
10363 }
10364 else
10365 {
10366 Lisp_Object key, frame;
10367 struct input_event event;
10368 EVENT_INIT (event);
10369
10370 /* Show item in released state. */
10371 show_mouse_face (dpyinfo, DRAW_IMAGE_RAISED);
10372 dpyinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
10373
10374 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
10375
10376 XSETFRAME (frame, f);
10377 event.kind = TOOL_BAR_EVENT;
10378 event.frame_or_window = frame;
10379 event.arg = frame;
10380 kbd_buffer_store_event (&event);
10381
10382 event.kind = TOOL_BAR_EVENT;
10383 event.frame_or_window = frame;
10384 event.arg = key;
10385 event.modifiers = modifiers;
10386 kbd_buffer_store_event (&event);
10387 last_tool_bar_item = -1;
10388 }
10389 }
10390
10391
10392 /* Possibly highlight a tool-bar item on frame F when mouse moves to
10393 tool-bar window-relative coordinates X/Y. Called from
10394 note_mouse_highlight. */
10395
10396 static void
10397 note_tool_bar_highlight (f, x, y)
10398 struct frame *f;
10399 int x, y;
10400 {
10401 Lisp_Object window = f->tool_bar_window;
10402 struct window *w = XWINDOW (window);
10403 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10404 int hpos, vpos;
10405 struct glyph *glyph;
10406 struct glyph_row *row;
10407 int i;
10408 Lisp_Object enabled_p;
10409 int prop_idx;
10410 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
10411 int mouse_down_p, rc;
10412
10413 /* Function note_mouse_highlight is called with negative x(y
10414 values when mouse moves outside of the frame. */
10415 if (x <= 0 || y <= 0)
10416 {
10417 clear_mouse_face (dpyinfo);
10418 return;
10419 }
10420
10421 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
10422 if (rc < 0)
10423 {
10424 /* Not on tool-bar item. */
10425 clear_mouse_face (dpyinfo);
10426 return;
10427 }
10428 else if (rc == 0)
10429 /* On same tool-bar item as before. */
10430 goto set_help_echo;
10431
10432 clear_mouse_face (dpyinfo);
10433
10434 /* Mouse is down, but on different tool-bar item? */
10435 mouse_down_p = (dpyinfo->grabbed
10436 && f == last_mouse_frame
10437 && FRAME_LIVE_P (f));
10438 if (mouse_down_p
10439 && last_tool_bar_item != prop_idx)
10440 return;
10441
10442 dpyinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
10443 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
10444
10445 /* If tool-bar item is not enabled, don't highlight it. */
10446 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
10447 if (!NILP (enabled_p))
10448 {
10449 /* Compute the x-position of the glyph. In front and past the
10450 image is a space. We include this in the highlighted area. */
10451 row = MATRIX_ROW (w->current_matrix, vpos);
10452 for (i = x = 0; i < hpos; ++i)
10453 x += row->glyphs[TEXT_AREA][i].pixel_width;
10454
10455 /* Record this as the current active region. */
10456 dpyinfo->mouse_face_beg_col = hpos;
10457 dpyinfo->mouse_face_beg_row = vpos;
10458 dpyinfo->mouse_face_beg_x = x;
10459 dpyinfo->mouse_face_beg_y = row->y;
10460 dpyinfo->mouse_face_past_end = 0;
10461
10462 dpyinfo->mouse_face_end_col = hpos + 1;
10463 dpyinfo->mouse_face_end_row = vpos;
10464 dpyinfo->mouse_face_end_x = x + glyph->pixel_width;
10465 dpyinfo->mouse_face_end_y = row->y;
10466 dpyinfo->mouse_face_window = window;
10467 dpyinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
10468
10469 /* Display it as active. */
10470 show_mouse_face (dpyinfo, draw);
10471 dpyinfo->mouse_face_image_state = draw;
10472 }
10473
10474 set_help_echo:
10475
10476 /* Set help_echo_string to a help string to display for this tool-bar item.
10477 XTread_socket does the rest. */
10478 help_echo_object = help_echo_window = Qnil;
10479 help_echo_pos = -1;
10480 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
10481 if (NILP (help_echo_string))
10482 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
10483 }
10484
10485 #endif /* HAVE_WINDOW_SYSTEM */
10486
10487
10488 \f
10489 /************************************************************************
10490 Horizontal scrolling
10491 ************************************************************************/
10492
10493 static int hscroll_window_tree P_ ((Lisp_Object));
10494 static int hscroll_windows P_ ((Lisp_Object));
10495
10496 /* For all leaf windows in the window tree rooted at WINDOW, set their
10497 hscroll value so that PT is (i) visible in the window, and (ii) so
10498 that it is not within a certain margin at the window's left and
10499 right border. Value is non-zero if any window's hscroll has been
10500 changed. */
10501
10502 static int
10503 hscroll_window_tree (window)
10504 Lisp_Object window;
10505 {
10506 int hscrolled_p = 0;
10507 int hscroll_relative_p = FLOATP (Vhscroll_step);
10508 int hscroll_step_abs = 0;
10509 double hscroll_step_rel = 0;
10510
10511 if (hscroll_relative_p)
10512 {
10513 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
10514 if (hscroll_step_rel < 0)
10515 {
10516 hscroll_relative_p = 0;
10517 hscroll_step_abs = 0;
10518 }
10519 }
10520 else if (INTEGERP (Vhscroll_step))
10521 {
10522 hscroll_step_abs = XINT (Vhscroll_step);
10523 if (hscroll_step_abs < 0)
10524 hscroll_step_abs = 0;
10525 }
10526 else
10527 hscroll_step_abs = 0;
10528
10529 while (WINDOWP (window))
10530 {
10531 struct window *w = XWINDOW (window);
10532
10533 if (WINDOWP (w->hchild))
10534 hscrolled_p |= hscroll_window_tree (w->hchild);
10535 else if (WINDOWP (w->vchild))
10536 hscrolled_p |= hscroll_window_tree (w->vchild);
10537 else if (w->cursor.vpos >= 0)
10538 {
10539 int h_margin;
10540 int text_area_width;
10541 struct glyph_row *current_cursor_row
10542 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
10543 struct glyph_row *desired_cursor_row
10544 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
10545 struct glyph_row *cursor_row
10546 = (desired_cursor_row->enabled_p
10547 ? desired_cursor_row
10548 : current_cursor_row);
10549
10550 text_area_width = window_box_width (w, TEXT_AREA);
10551
10552 /* Scroll when cursor is inside this scroll margin. */
10553 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
10554
10555 if ((XFASTINT (w->hscroll)
10556 && w->cursor.x <= h_margin)
10557 || (cursor_row->enabled_p
10558 && cursor_row->truncated_on_right_p
10559 && (w->cursor.x >= text_area_width - h_margin)))
10560 {
10561 struct it it;
10562 int hscroll;
10563 struct buffer *saved_current_buffer;
10564 int pt;
10565 int wanted_x;
10566
10567 /* Find point in a display of infinite width. */
10568 saved_current_buffer = current_buffer;
10569 current_buffer = XBUFFER (w->buffer);
10570
10571 if (w == XWINDOW (selected_window))
10572 pt = BUF_PT (current_buffer);
10573 else
10574 {
10575 pt = marker_position (w->pointm);
10576 pt = max (BEGV, pt);
10577 pt = min (ZV, pt);
10578 }
10579
10580 /* Move iterator to pt starting at cursor_row->start in
10581 a line with infinite width. */
10582 init_to_row_start (&it, w, cursor_row);
10583 it.last_visible_x = INFINITY;
10584 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
10585 current_buffer = saved_current_buffer;
10586
10587 /* Position cursor in window. */
10588 if (!hscroll_relative_p && hscroll_step_abs == 0)
10589 hscroll = max (0, (it.current_x
10590 - (ITERATOR_AT_END_OF_LINE_P (&it)
10591 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
10592 : (text_area_width / 2))))
10593 / FRAME_COLUMN_WIDTH (it.f);
10594 else if (w->cursor.x >= text_area_width - h_margin)
10595 {
10596 if (hscroll_relative_p)
10597 wanted_x = text_area_width * (1 - hscroll_step_rel)
10598 - h_margin;
10599 else
10600 wanted_x = text_area_width
10601 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
10602 - h_margin;
10603 hscroll
10604 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
10605 }
10606 else
10607 {
10608 if (hscroll_relative_p)
10609 wanted_x = text_area_width * hscroll_step_rel
10610 + h_margin;
10611 else
10612 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
10613 + h_margin;
10614 hscroll
10615 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
10616 }
10617 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
10618
10619 /* Don't call Fset_window_hscroll if value hasn't
10620 changed because it will prevent redisplay
10621 optimizations. */
10622 if (XFASTINT (w->hscroll) != hscroll)
10623 {
10624 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
10625 w->hscroll = make_number (hscroll);
10626 hscrolled_p = 1;
10627 }
10628 }
10629 }
10630
10631 window = w->next;
10632 }
10633
10634 /* Value is non-zero if hscroll of any leaf window has been changed. */
10635 return hscrolled_p;
10636 }
10637
10638
10639 /* Set hscroll so that cursor is visible and not inside horizontal
10640 scroll margins for all windows in the tree rooted at WINDOW. See
10641 also hscroll_window_tree above. Value is non-zero if any window's
10642 hscroll has been changed. If it has, desired matrices on the frame
10643 of WINDOW are cleared. */
10644
10645 static int
10646 hscroll_windows (window)
10647 Lisp_Object window;
10648 {
10649 int hscrolled_p;
10650
10651 if (automatic_hscrolling_p)
10652 {
10653 hscrolled_p = hscroll_window_tree (window);
10654 if (hscrolled_p)
10655 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
10656 }
10657 else
10658 hscrolled_p = 0;
10659 return hscrolled_p;
10660 }
10661
10662
10663 \f
10664 /************************************************************************
10665 Redisplay
10666 ************************************************************************/
10667
10668 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
10669 to a non-zero value. This is sometimes handy to have in a debugger
10670 session. */
10671
10672 #if GLYPH_DEBUG
10673
10674 /* First and last unchanged row for try_window_id. */
10675
10676 int debug_first_unchanged_at_end_vpos;
10677 int debug_last_unchanged_at_beg_vpos;
10678
10679 /* Delta vpos and y. */
10680
10681 int debug_dvpos, debug_dy;
10682
10683 /* Delta in characters and bytes for try_window_id. */
10684
10685 int debug_delta, debug_delta_bytes;
10686
10687 /* Values of window_end_pos and window_end_vpos at the end of
10688 try_window_id. */
10689
10690 EMACS_INT debug_end_pos, debug_end_vpos;
10691
10692 /* Append a string to W->desired_matrix->method. FMT is a printf
10693 format string. A1...A9 are a supplement for a variable-length
10694 argument list. If trace_redisplay_p is non-zero also printf the
10695 resulting string to stderr. */
10696
10697 static void
10698 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
10699 struct window *w;
10700 char *fmt;
10701 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
10702 {
10703 char buffer[512];
10704 char *method = w->desired_matrix->method;
10705 int len = strlen (method);
10706 int size = sizeof w->desired_matrix->method;
10707 int remaining = size - len - 1;
10708
10709 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
10710 if (len && remaining)
10711 {
10712 method[len] = '|';
10713 --remaining, ++len;
10714 }
10715
10716 strncpy (method + len, buffer, remaining);
10717
10718 if (trace_redisplay_p)
10719 fprintf (stderr, "%p (%s): %s\n",
10720 w,
10721 ((BUFFERP (w->buffer)
10722 && STRINGP (XBUFFER (w->buffer)->name))
10723 ? (char *) SDATA (XBUFFER (w->buffer)->name)
10724 : "no buffer"),
10725 buffer);
10726 }
10727
10728 #endif /* GLYPH_DEBUG */
10729
10730
10731 /* Value is non-zero if all changes in window W, which displays
10732 current_buffer, are in the text between START and END. START is a
10733 buffer position, END is given as a distance from Z. Used in
10734 redisplay_internal for display optimization. */
10735
10736 static INLINE int
10737 text_outside_line_unchanged_p (w, start, end)
10738 struct window *w;
10739 int start, end;
10740 {
10741 int unchanged_p = 1;
10742
10743 /* If text or overlays have changed, see where. */
10744 if (XFASTINT (w->last_modified) < MODIFF
10745 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
10746 {
10747 /* Gap in the line? */
10748 if (GPT < start || Z - GPT < end)
10749 unchanged_p = 0;
10750
10751 /* Changes start in front of the line, or end after it? */
10752 if (unchanged_p
10753 && (BEG_UNCHANGED < start - 1
10754 || END_UNCHANGED < end))
10755 unchanged_p = 0;
10756
10757 /* If selective display, can't optimize if changes start at the
10758 beginning of the line. */
10759 if (unchanged_p
10760 && INTEGERP (current_buffer->selective_display)
10761 && XINT (current_buffer->selective_display) > 0
10762 && (BEG_UNCHANGED < start || GPT <= start))
10763 unchanged_p = 0;
10764
10765 /* If there are overlays at the start or end of the line, these
10766 may have overlay strings with newlines in them. A change at
10767 START, for instance, may actually concern the display of such
10768 overlay strings as well, and they are displayed on different
10769 lines. So, quickly rule out this case. (For the future, it
10770 might be desirable to implement something more telling than
10771 just BEG/END_UNCHANGED.) */
10772 if (unchanged_p)
10773 {
10774 if (BEG + BEG_UNCHANGED == start
10775 && overlay_touches_p (start))
10776 unchanged_p = 0;
10777 if (END_UNCHANGED == end
10778 && overlay_touches_p (Z - end))
10779 unchanged_p = 0;
10780 }
10781 }
10782
10783 return unchanged_p;
10784 }
10785
10786
10787 /* Do a frame update, taking possible shortcuts into account. This is
10788 the main external entry point for redisplay.
10789
10790 If the last redisplay displayed an echo area message and that message
10791 is no longer requested, we clear the echo area or bring back the
10792 mini-buffer if that is in use. */
10793
10794 void
10795 redisplay ()
10796 {
10797 redisplay_internal (0);
10798 }
10799
10800
10801 static Lisp_Object
10802 overlay_arrow_string_or_property (var)
10803 Lisp_Object var;
10804 {
10805 Lisp_Object val;
10806
10807 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
10808 return val;
10809
10810 return Voverlay_arrow_string;
10811 }
10812
10813 /* Return 1 if there are any overlay-arrows in current_buffer. */
10814 static int
10815 overlay_arrow_in_current_buffer_p ()
10816 {
10817 Lisp_Object vlist;
10818
10819 for (vlist = Voverlay_arrow_variable_list;
10820 CONSP (vlist);
10821 vlist = XCDR (vlist))
10822 {
10823 Lisp_Object var = XCAR (vlist);
10824 Lisp_Object val;
10825
10826 if (!SYMBOLP (var))
10827 continue;
10828 val = find_symbol_value (var);
10829 if (MARKERP (val)
10830 && current_buffer == XMARKER (val)->buffer)
10831 return 1;
10832 }
10833 return 0;
10834 }
10835
10836
10837 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
10838 has changed. */
10839
10840 static int
10841 overlay_arrows_changed_p ()
10842 {
10843 Lisp_Object vlist;
10844
10845 for (vlist = Voverlay_arrow_variable_list;
10846 CONSP (vlist);
10847 vlist = XCDR (vlist))
10848 {
10849 Lisp_Object var = XCAR (vlist);
10850 Lisp_Object val, pstr;
10851
10852 if (!SYMBOLP (var))
10853 continue;
10854 val = find_symbol_value (var);
10855 if (!MARKERP (val))
10856 continue;
10857 if (! EQ (COERCE_MARKER (val),
10858 Fget (var, Qlast_arrow_position))
10859 || ! (pstr = overlay_arrow_string_or_property (var),
10860 EQ (pstr, Fget (var, Qlast_arrow_string))))
10861 return 1;
10862 }
10863 return 0;
10864 }
10865
10866 /* Mark overlay arrows to be updated on next redisplay. */
10867
10868 static void
10869 update_overlay_arrows (up_to_date)
10870 int up_to_date;
10871 {
10872 Lisp_Object vlist;
10873
10874 for (vlist = Voverlay_arrow_variable_list;
10875 CONSP (vlist);
10876 vlist = XCDR (vlist))
10877 {
10878 Lisp_Object var = XCAR (vlist);
10879
10880 if (!SYMBOLP (var))
10881 continue;
10882
10883 if (up_to_date > 0)
10884 {
10885 Lisp_Object val = find_symbol_value (var);
10886 Fput (var, Qlast_arrow_position,
10887 COERCE_MARKER (val));
10888 Fput (var, Qlast_arrow_string,
10889 overlay_arrow_string_or_property (var));
10890 }
10891 else if (up_to_date < 0
10892 || !NILP (Fget (var, Qlast_arrow_position)))
10893 {
10894 Fput (var, Qlast_arrow_position, Qt);
10895 Fput (var, Qlast_arrow_string, Qt);
10896 }
10897 }
10898 }
10899
10900
10901 /* Return overlay arrow string to display at row.
10902 Return integer (bitmap number) for arrow bitmap in left fringe.
10903 Return nil if no overlay arrow. */
10904
10905 static Lisp_Object
10906 overlay_arrow_at_row (it, row)
10907 struct it *it;
10908 struct glyph_row *row;
10909 {
10910 Lisp_Object vlist;
10911
10912 for (vlist = Voverlay_arrow_variable_list;
10913 CONSP (vlist);
10914 vlist = XCDR (vlist))
10915 {
10916 Lisp_Object var = XCAR (vlist);
10917 Lisp_Object val;
10918
10919 if (!SYMBOLP (var))
10920 continue;
10921
10922 val = find_symbol_value (var);
10923
10924 if (MARKERP (val)
10925 && current_buffer == XMARKER (val)->buffer
10926 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
10927 {
10928 if (FRAME_WINDOW_P (it->f)
10929 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
10930 {
10931 #ifdef HAVE_WINDOW_SYSTEM
10932 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
10933 {
10934 int fringe_bitmap;
10935 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
10936 return make_number (fringe_bitmap);
10937 }
10938 #endif
10939 return make_number (-1); /* Use default arrow bitmap */
10940 }
10941 return overlay_arrow_string_or_property (var);
10942 }
10943 }
10944
10945 return Qnil;
10946 }
10947
10948 /* Return 1 if point moved out of or into a composition. Otherwise
10949 return 0. PREV_BUF and PREV_PT are the last point buffer and
10950 position. BUF and PT are the current point buffer and position. */
10951
10952 int
10953 check_point_in_composition (prev_buf, prev_pt, buf, pt)
10954 struct buffer *prev_buf, *buf;
10955 int prev_pt, pt;
10956 {
10957 EMACS_INT start, end;
10958 Lisp_Object prop;
10959 Lisp_Object buffer;
10960
10961 XSETBUFFER (buffer, buf);
10962 /* Check a composition at the last point if point moved within the
10963 same buffer. */
10964 if (prev_buf == buf)
10965 {
10966 if (prev_pt == pt)
10967 /* Point didn't move. */
10968 return 0;
10969
10970 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
10971 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
10972 && COMPOSITION_VALID_P (start, end, prop)
10973 && start < prev_pt && end > prev_pt)
10974 /* The last point was within the composition. Return 1 iff
10975 point moved out of the composition. */
10976 return (pt <= start || pt >= end);
10977 }
10978
10979 /* Check a composition at the current point. */
10980 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
10981 && find_composition (pt, -1, &start, &end, &prop, buffer)
10982 && COMPOSITION_VALID_P (start, end, prop)
10983 && start < pt && end > pt);
10984 }
10985
10986
10987 /* Reconsider the setting of B->clip_changed which is displayed
10988 in window W. */
10989
10990 static INLINE void
10991 reconsider_clip_changes (w, b)
10992 struct window *w;
10993 struct buffer *b;
10994 {
10995 if (b->clip_changed
10996 && !NILP (w->window_end_valid)
10997 && w->current_matrix->buffer == b
10998 && w->current_matrix->zv == BUF_ZV (b)
10999 && w->current_matrix->begv == BUF_BEGV (b))
11000 b->clip_changed = 0;
11001
11002 /* If display wasn't paused, and W is not a tool bar window, see if
11003 point has been moved into or out of a composition. In that case,
11004 we set b->clip_changed to 1 to force updating the screen. If
11005 b->clip_changed has already been set to 1, we can skip this
11006 check. */
11007 if (!b->clip_changed
11008 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
11009 {
11010 int pt;
11011
11012 if (w == XWINDOW (selected_window))
11013 pt = BUF_PT (current_buffer);
11014 else
11015 pt = marker_position (w->pointm);
11016
11017 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
11018 || pt != XINT (w->last_point))
11019 && check_point_in_composition (w->current_matrix->buffer,
11020 XINT (w->last_point),
11021 XBUFFER (w->buffer), pt))
11022 b->clip_changed = 1;
11023 }
11024 }
11025 \f
11026
11027 /* Select FRAME to forward the values of frame-local variables into C
11028 variables so that the redisplay routines can access those values
11029 directly. */
11030
11031 static void
11032 select_frame_for_redisplay (frame)
11033 Lisp_Object frame;
11034 {
11035 Lisp_Object tail, sym, val;
11036 Lisp_Object old = selected_frame;
11037
11038 xassert (FRAMEP (frame) && FRAME_LIVE_P (XFRAME (frame)));
11039
11040 selected_frame = frame;
11041
11042 for (tail = XFRAME (frame)->param_alist; CONSP (tail); tail = XCDR (tail))
11043 if (CONSP (XCAR (tail))
11044 && (sym = XCAR (XCAR (tail)),
11045 SYMBOLP (sym))
11046 && (sym = indirect_variable (sym),
11047 val = SYMBOL_VALUE (sym),
11048 (BUFFER_LOCAL_VALUEP (val)))
11049 && XBUFFER_LOCAL_VALUE (val)->check_frame)
11050 /* Use find_symbol_value rather than Fsymbol_value
11051 to avoid an error if it is void. */
11052 find_symbol_value (sym);
11053
11054 for (tail = XFRAME (old)->param_alist; CONSP (tail); tail = XCDR (tail))
11055 if (CONSP (XCAR (tail))
11056 && (sym = XCAR (XCAR (tail)),
11057 SYMBOLP (sym))
11058 && (sym = indirect_variable (sym),
11059 val = SYMBOL_VALUE (sym),
11060 (BUFFER_LOCAL_VALUEP (val)))
11061 && XBUFFER_LOCAL_VALUE (val)->check_frame)
11062 find_symbol_value (sym);
11063 }
11064
11065
11066 #define STOP_POLLING \
11067 do { if (! polling_stopped_here) stop_polling (); \
11068 polling_stopped_here = 1; } while (0)
11069
11070 #define RESUME_POLLING \
11071 do { if (polling_stopped_here) start_polling (); \
11072 polling_stopped_here = 0; } while (0)
11073
11074
11075 /* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
11076 response to any user action; therefore, we should preserve the echo
11077 area. (Actually, our caller does that job.) Perhaps in the future
11078 avoid recentering windows if it is not necessary; currently that
11079 causes some problems. */
11080
11081 static void
11082 redisplay_internal (preserve_echo_area)
11083 int preserve_echo_area;
11084 {
11085 struct window *w = XWINDOW (selected_window);
11086 struct frame *f;
11087 int pause;
11088 int must_finish = 0;
11089 struct text_pos tlbufpos, tlendpos;
11090 int number_of_visible_frames;
11091 int count, count1;
11092 struct frame *sf;
11093 int polling_stopped_here = 0;
11094 Lisp_Object old_frame = selected_frame;
11095
11096 /* Non-zero means redisplay has to consider all windows on all
11097 frames. Zero means, only selected_window is considered. */
11098 int consider_all_windows_p;
11099
11100 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
11101
11102 /* No redisplay if running in batch mode or frame is not yet fully
11103 initialized, or redisplay is explicitly turned off by setting
11104 Vinhibit_redisplay. */
11105 if (noninteractive
11106 || !NILP (Vinhibit_redisplay))
11107 return;
11108
11109 /* Don't examine these until after testing Vinhibit_redisplay.
11110 When Emacs is shutting down, perhaps because its connection to
11111 X has dropped, we should not look at them at all. */
11112 f = XFRAME (w->frame);
11113 sf = SELECTED_FRAME ();
11114
11115 if (!f->glyphs_initialized_p)
11116 return;
11117
11118 /* The flag redisplay_performed_directly_p is set by
11119 direct_output_for_insert when it already did the whole screen
11120 update necessary. */
11121 if (redisplay_performed_directly_p)
11122 {
11123 redisplay_performed_directly_p = 0;
11124 if (!hscroll_windows (selected_window))
11125 return;
11126 }
11127
11128 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (MAC_OS)
11129 if (popup_activated ())
11130 return;
11131 #endif
11132
11133 /* I don't think this happens but let's be paranoid. */
11134 if (redisplaying_p)
11135 return;
11136
11137 /* Record a function that resets redisplaying_p to its old value
11138 when we leave this function. */
11139 count = SPECPDL_INDEX ();
11140 record_unwind_protect (unwind_redisplay,
11141 Fcons (make_number (redisplaying_p), selected_frame));
11142 ++redisplaying_p;
11143 specbind (Qinhibit_free_realized_faces, Qnil);
11144
11145 {
11146 Lisp_Object tail, frame;
11147
11148 FOR_EACH_FRAME (tail, frame)
11149 {
11150 struct frame *f = XFRAME (frame);
11151 f->already_hscrolled_p = 0;
11152 }
11153 }
11154
11155 retry:
11156 if (!EQ (old_frame, selected_frame)
11157 && FRAME_LIVE_P (XFRAME (old_frame)))
11158 /* When running redisplay, we play a bit fast-and-loose and allow e.g.
11159 selected_frame and selected_window to be temporarily out-of-sync so
11160 when we come back here via `goto retry', we need to resync because we
11161 may need to run Elisp code (via prepare_menu_bars). */
11162 select_frame_for_redisplay (old_frame);
11163
11164 pause = 0;
11165 reconsider_clip_changes (w, current_buffer);
11166 last_escape_glyph_frame = NULL;
11167 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
11168
11169 /* If new fonts have been loaded that make a glyph matrix adjustment
11170 necessary, do it. */
11171 if (fonts_changed_p)
11172 {
11173 adjust_glyphs (NULL);
11174 ++windows_or_buffers_changed;
11175 fonts_changed_p = 0;
11176 }
11177
11178 /* If face_change_count is non-zero, init_iterator will free all
11179 realized faces, which includes the faces referenced from current
11180 matrices. So, we can't reuse current matrices in this case. */
11181 if (face_change_count)
11182 ++windows_or_buffers_changed;
11183
11184 if (FRAME_TERMCAP_P (sf)
11185 && FRAME_TTY (sf)->previous_frame != sf)
11186 {
11187 /* Since frames on a single ASCII terminal share the same
11188 display area, displaying a different frame means redisplay
11189 the whole thing. */
11190 windows_or_buffers_changed++;
11191 SET_FRAME_GARBAGED (sf);
11192 FRAME_TTY (sf)->previous_frame = sf;
11193 }
11194
11195 /* Set the visible flags for all frames. Do this before checking
11196 for resized or garbaged frames; they want to know if their frames
11197 are visible. See the comment in frame.h for
11198 FRAME_SAMPLE_VISIBILITY. */
11199 {
11200 Lisp_Object tail, frame;
11201
11202 number_of_visible_frames = 0;
11203
11204 FOR_EACH_FRAME (tail, frame)
11205 {
11206 struct frame *f = XFRAME (frame);
11207
11208 FRAME_SAMPLE_VISIBILITY (f);
11209 if (FRAME_VISIBLE_P (f))
11210 ++number_of_visible_frames;
11211 clear_desired_matrices (f);
11212 }
11213 }
11214
11215
11216 /* Notice any pending interrupt request to change frame size. */
11217 do_pending_window_change (1);
11218
11219 /* Clear frames marked as garbaged. */
11220 if (frame_garbaged)
11221 clear_garbaged_frames ();
11222
11223 /* Build menubar and tool-bar items. */
11224 if (NILP (Vmemory_full))
11225 prepare_menu_bars ();
11226
11227 if (windows_or_buffers_changed)
11228 update_mode_lines++;
11229
11230 /* Detect case that we need to write or remove a star in the mode line. */
11231 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
11232 {
11233 w->update_mode_line = Qt;
11234 if (buffer_shared > 1)
11235 update_mode_lines++;
11236 }
11237
11238 /* Avoid invocation of point motion hooks by `current_column' below. */
11239 count1 = SPECPDL_INDEX ();
11240 specbind (Qinhibit_point_motion_hooks, Qt);
11241
11242 /* If %c is in the mode line, update it if needed. */
11243 if (!NILP (w->column_number_displayed)
11244 /* This alternative quickly identifies a common case
11245 where no change is needed. */
11246 && !(PT == XFASTINT (w->last_point)
11247 && XFASTINT (w->last_modified) >= MODIFF
11248 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
11249 && (XFASTINT (w->column_number_displayed)
11250 != (int) current_column ())) /* iftc */
11251 w->update_mode_line = Qt;
11252
11253 unbind_to (count1, Qnil);
11254
11255 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
11256
11257 /* The variable buffer_shared is set in redisplay_window and
11258 indicates that we redisplay a buffer in different windows. See
11259 there. */
11260 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
11261 || cursor_type_changed);
11262
11263 /* If specs for an arrow have changed, do thorough redisplay
11264 to ensure we remove any arrow that should no longer exist. */
11265 if (overlay_arrows_changed_p ())
11266 consider_all_windows_p = windows_or_buffers_changed = 1;
11267
11268 /* Normally the message* functions will have already displayed and
11269 updated the echo area, but the frame may have been trashed, or
11270 the update may have been preempted, so display the echo area
11271 again here. Checking message_cleared_p captures the case that
11272 the echo area should be cleared. */
11273 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
11274 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
11275 || (message_cleared_p
11276 && minibuf_level == 0
11277 /* If the mini-window is currently selected, this means the
11278 echo-area doesn't show through. */
11279 && !MINI_WINDOW_P (XWINDOW (selected_window))))
11280 {
11281 int window_height_changed_p = echo_area_display (0);
11282 must_finish = 1;
11283
11284 /* If we don't display the current message, don't clear the
11285 message_cleared_p flag, because, if we did, we wouldn't clear
11286 the echo area in the next redisplay which doesn't preserve
11287 the echo area. */
11288 if (!display_last_displayed_message_p)
11289 message_cleared_p = 0;
11290
11291 if (fonts_changed_p)
11292 goto retry;
11293 else if (window_height_changed_p)
11294 {
11295 consider_all_windows_p = 1;
11296 ++update_mode_lines;
11297 ++windows_or_buffers_changed;
11298
11299 /* If window configuration was changed, frames may have been
11300 marked garbaged. Clear them or we will experience
11301 surprises wrt scrolling. */
11302 if (frame_garbaged)
11303 clear_garbaged_frames ();
11304 }
11305 }
11306 else if (EQ (selected_window, minibuf_window)
11307 && (current_buffer->clip_changed
11308 || XFASTINT (w->last_modified) < MODIFF
11309 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
11310 && resize_mini_window (w, 0))
11311 {
11312 /* Resized active mini-window to fit the size of what it is
11313 showing if its contents might have changed. */
11314 must_finish = 1;
11315 consider_all_windows_p = 1;
11316 ++windows_or_buffers_changed;
11317 ++update_mode_lines;
11318
11319 /* If window configuration was changed, frames may have been
11320 marked garbaged. Clear them or we will experience
11321 surprises wrt scrolling. */
11322 if (frame_garbaged)
11323 clear_garbaged_frames ();
11324 }
11325
11326
11327 /* If showing the region, and mark has changed, we must redisplay
11328 the whole window. The assignment to this_line_start_pos prevents
11329 the optimization directly below this if-statement. */
11330 if (((!NILP (Vtransient_mark_mode)
11331 && !NILP (XBUFFER (w->buffer)->mark_active))
11332 != !NILP (w->region_showing))
11333 || (!NILP (w->region_showing)
11334 && !EQ (w->region_showing,
11335 Fmarker_position (XBUFFER (w->buffer)->mark))))
11336 CHARPOS (this_line_start_pos) = 0;
11337
11338 /* Optimize the case that only the line containing the cursor in the
11339 selected window has changed. Variables starting with this_ are
11340 set in display_line and record information about the line
11341 containing the cursor. */
11342 tlbufpos = this_line_start_pos;
11343 tlendpos = this_line_end_pos;
11344 if (!consider_all_windows_p
11345 && CHARPOS (tlbufpos) > 0
11346 && NILP (w->update_mode_line)
11347 && !current_buffer->clip_changed
11348 && !current_buffer->prevent_redisplay_optimizations_p
11349 && FRAME_VISIBLE_P (XFRAME (w->frame))
11350 && !FRAME_OBSCURED_P (XFRAME (w->frame))
11351 /* Make sure recorded data applies to current buffer, etc. */
11352 && this_line_buffer == current_buffer
11353 && current_buffer == XBUFFER (w->buffer)
11354 && NILP (w->force_start)
11355 && NILP (w->optional_new_start)
11356 /* Point must be on the line that we have info recorded about. */
11357 && PT >= CHARPOS (tlbufpos)
11358 && PT <= Z - CHARPOS (tlendpos)
11359 /* All text outside that line, including its final newline,
11360 must be unchanged */
11361 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
11362 CHARPOS (tlendpos)))
11363 {
11364 if (CHARPOS (tlbufpos) > BEGV
11365 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
11366 && (CHARPOS (tlbufpos) == ZV
11367 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
11368 /* Former continuation line has disappeared by becoming empty */
11369 goto cancel;
11370 else if (XFASTINT (w->last_modified) < MODIFF
11371 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
11372 || MINI_WINDOW_P (w))
11373 {
11374 /* We have to handle the case of continuation around a
11375 wide-column character (See the comment in indent.c around
11376 line 885).
11377
11378 For instance, in the following case:
11379
11380 -------- Insert --------
11381 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
11382 J_I_ ==> J_I_ `^^' are cursors.
11383 ^^ ^^
11384 -------- --------
11385
11386 As we have to redraw the line above, we should goto cancel. */
11387
11388 struct it it;
11389 int line_height_before = this_line_pixel_height;
11390
11391 /* Note that start_display will handle the case that the
11392 line starting at tlbufpos is a continuation lines. */
11393 start_display (&it, w, tlbufpos);
11394
11395 /* Implementation note: It this still necessary? */
11396 if (it.current_x != this_line_start_x)
11397 goto cancel;
11398
11399 TRACE ((stderr, "trying display optimization 1\n"));
11400 w->cursor.vpos = -1;
11401 overlay_arrow_seen = 0;
11402 it.vpos = this_line_vpos;
11403 it.current_y = this_line_y;
11404 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
11405 display_line (&it);
11406
11407 /* If line contains point, is not continued,
11408 and ends at same distance from eob as before, we win */
11409 if (w->cursor.vpos >= 0
11410 /* Line is not continued, otherwise this_line_start_pos
11411 would have been set to 0 in display_line. */
11412 && CHARPOS (this_line_start_pos)
11413 /* Line ends as before. */
11414 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
11415 /* Line has same height as before. Otherwise other lines
11416 would have to be shifted up or down. */
11417 && this_line_pixel_height == line_height_before)
11418 {
11419 /* If this is not the window's last line, we must adjust
11420 the charstarts of the lines below. */
11421 if (it.current_y < it.last_visible_y)
11422 {
11423 struct glyph_row *row
11424 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
11425 int delta, delta_bytes;
11426
11427 if (Z - CHARPOS (tlendpos) == ZV)
11428 {
11429 /* This line ends at end of (accessible part of)
11430 buffer. There is no newline to count. */
11431 delta = (Z
11432 - CHARPOS (tlendpos)
11433 - MATRIX_ROW_START_CHARPOS (row));
11434 delta_bytes = (Z_BYTE
11435 - BYTEPOS (tlendpos)
11436 - MATRIX_ROW_START_BYTEPOS (row));
11437 }
11438 else
11439 {
11440 /* This line ends in a newline. Must take
11441 account of the newline and the rest of the
11442 text that follows. */
11443 delta = (Z
11444 - CHARPOS (tlendpos)
11445 - MATRIX_ROW_START_CHARPOS (row));
11446 delta_bytes = (Z_BYTE
11447 - BYTEPOS (tlendpos)
11448 - MATRIX_ROW_START_BYTEPOS (row));
11449 }
11450
11451 increment_matrix_positions (w->current_matrix,
11452 this_line_vpos + 1,
11453 w->current_matrix->nrows,
11454 delta, delta_bytes);
11455 }
11456
11457 /* If this row displays text now but previously didn't,
11458 or vice versa, w->window_end_vpos may have to be
11459 adjusted. */
11460 if ((it.glyph_row - 1)->displays_text_p)
11461 {
11462 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
11463 XSETINT (w->window_end_vpos, this_line_vpos);
11464 }
11465 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
11466 && this_line_vpos > 0)
11467 XSETINT (w->window_end_vpos, this_line_vpos - 1);
11468 w->window_end_valid = Qnil;
11469
11470 /* Update hint: No need to try to scroll in update_window. */
11471 w->desired_matrix->no_scrolling_p = 1;
11472
11473 #if GLYPH_DEBUG
11474 *w->desired_matrix->method = 0;
11475 debug_method_add (w, "optimization 1");
11476 #endif
11477 #ifdef HAVE_WINDOW_SYSTEM
11478 update_window_fringes (w, 0);
11479 #endif
11480 goto update;
11481 }
11482 else
11483 goto cancel;
11484 }
11485 else if (/* Cursor position hasn't changed. */
11486 PT == XFASTINT (w->last_point)
11487 /* Make sure the cursor was last displayed
11488 in this window. Otherwise we have to reposition it. */
11489 && 0 <= w->cursor.vpos
11490 && WINDOW_TOTAL_LINES (w) > w->cursor.vpos)
11491 {
11492 if (!must_finish)
11493 {
11494 do_pending_window_change (1);
11495
11496 /* We used to always goto end_of_redisplay here, but this
11497 isn't enough if we have a blinking cursor. */
11498 if (w->cursor_off_p == w->last_cursor_off_p)
11499 goto end_of_redisplay;
11500 }
11501 goto update;
11502 }
11503 /* If highlighting the region, or if the cursor is in the echo area,
11504 then we can't just move the cursor. */
11505 else if (! (!NILP (Vtransient_mark_mode)
11506 && !NILP (current_buffer->mark_active))
11507 && (EQ (selected_window, current_buffer->last_selected_window)
11508 || highlight_nonselected_windows)
11509 && NILP (w->region_showing)
11510 && NILP (Vshow_trailing_whitespace)
11511 && !cursor_in_echo_area)
11512 {
11513 struct it it;
11514 struct glyph_row *row;
11515
11516 /* Skip from tlbufpos to PT and see where it is. Note that
11517 PT may be in invisible text. If so, we will end at the
11518 next visible position. */
11519 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
11520 NULL, DEFAULT_FACE_ID);
11521 it.current_x = this_line_start_x;
11522 it.current_y = this_line_y;
11523 it.vpos = this_line_vpos;
11524
11525 /* The call to move_it_to stops in front of PT, but
11526 moves over before-strings. */
11527 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
11528
11529 if (it.vpos == this_line_vpos
11530 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
11531 row->enabled_p))
11532 {
11533 xassert (this_line_vpos == it.vpos);
11534 xassert (this_line_y == it.current_y);
11535 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11536 #if GLYPH_DEBUG
11537 *w->desired_matrix->method = 0;
11538 debug_method_add (w, "optimization 3");
11539 #endif
11540 goto update;
11541 }
11542 else
11543 goto cancel;
11544 }
11545
11546 cancel:
11547 /* Text changed drastically or point moved off of line. */
11548 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
11549 }
11550
11551 CHARPOS (this_line_start_pos) = 0;
11552 consider_all_windows_p |= buffer_shared > 1;
11553 ++clear_face_cache_count;
11554 #ifdef HAVE_WINDOW_SYSTEM
11555 ++clear_image_cache_count;
11556 #endif
11557
11558 /* Build desired matrices, and update the display. If
11559 consider_all_windows_p is non-zero, do it for all windows on all
11560 frames. Otherwise do it for selected_window, only. */
11561
11562 if (consider_all_windows_p)
11563 {
11564 Lisp_Object tail, frame;
11565
11566 FOR_EACH_FRAME (tail, frame)
11567 XFRAME (frame)->updated_p = 0;
11568
11569 /* Recompute # windows showing selected buffer. This will be
11570 incremented each time such a window is displayed. */
11571 buffer_shared = 0;
11572
11573 FOR_EACH_FRAME (tail, frame)
11574 {
11575 struct frame *f = XFRAME (frame);
11576
11577 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
11578 {
11579 if (! EQ (frame, selected_frame))
11580 /* Select the frame, for the sake of frame-local
11581 variables. */
11582 select_frame_for_redisplay (frame);
11583
11584 /* Mark all the scroll bars to be removed; we'll redeem
11585 the ones we want when we redisplay their windows. */
11586 if (FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
11587 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
11588
11589 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
11590 redisplay_windows (FRAME_ROOT_WINDOW (f));
11591
11592 /* Any scroll bars which redisplay_windows should have
11593 nuked should now go away. */
11594 if (FRAME_TERMINAL (f)->judge_scroll_bars_hook)
11595 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
11596
11597 /* If fonts changed, display again. */
11598 /* ??? rms: I suspect it is a mistake to jump all the way
11599 back to retry here. It should just retry this frame. */
11600 if (fonts_changed_p)
11601 goto retry;
11602
11603 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
11604 {
11605 /* See if we have to hscroll. */
11606 if (!f->already_hscrolled_p)
11607 {
11608 f->already_hscrolled_p = 1;
11609 if (hscroll_windows (f->root_window))
11610 goto retry;
11611 }
11612
11613 /* Prevent various kinds of signals during display
11614 update. stdio is not robust about handling
11615 signals, which can cause an apparent I/O
11616 error. */
11617 if (interrupt_input)
11618 unrequest_sigio ();
11619 STOP_POLLING;
11620
11621 /* Update the display. */
11622 set_window_update_flags (XWINDOW (f->root_window), 1);
11623 pause |= update_frame (f, 0, 0);
11624 #if 0 /* Exiting the loop can leave the wrong value for buffer_shared. */
11625 if (pause)
11626 break;
11627 #endif
11628
11629 f->updated_p = 1;
11630 }
11631 }
11632 }
11633
11634 if (!pause)
11635 {
11636 /* Do the mark_window_display_accurate after all windows have
11637 been redisplayed because this call resets flags in buffers
11638 which are needed for proper redisplay. */
11639 FOR_EACH_FRAME (tail, frame)
11640 {
11641 struct frame *f = XFRAME (frame);
11642 if (f->updated_p)
11643 {
11644 mark_window_display_accurate (f->root_window, 1);
11645 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
11646 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
11647 }
11648 }
11649 }
11650 }
11651 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
11652 {
11653 Lisp_Object mini_window;
11654 struct frame *mini_frame;
11655
11656 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
11657 /* Use list_of_error, not Qerror, so that
11658 we catch only errors and don't run the debugger. */
11659 internal_condition_case_1 (redisplay_window_1, selected_window,
11660 list_of_error,
11661 redisplay_window_error);
11662
11663 /* Compare desired and current matrices, perform output. */
11664
11665 update:
11666 /* If fonts changed, display again. */
11667 if (fonts_changed_p)
11668 goto retry;
11669
11670 /* Prevent various kinds of signals during display update.
11671 stdio is not robust about handling signals,
11672 which can cause an apparent I/O error. */
11673 if (interrupt_input)
11674 unrequest_sigio ();
11675 STOP_POLLING;
11676
11677 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
11678 {
11679 if (hscroll_windows (selected_window))
11680 goto retry;
11681
11682 XWINDOW (selected_window)->must_be_updated_p = 1;
11683 pause = update_frame (sf, 0, 0);
11684 }
11685
11686 /* We may have called echo_area_display at the top of this
11687 function. If the echo area is on another frame, that may
11688 have put text on a frame other than the selected one, so the
11689 above call to update_frame would not have caught it. Catch
11690 it here. */
11691 mini_window = FRAME_MINIBUF_WINDOW (sf);
11692 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
11693
11694 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
11695 {
11696 XWINDOW (mini_window)->must_be_updated_p = 1;
11697 pause |= update_frame (mini_frame, 0, 0);
11698 if (!pause && hscroll_windows (mini_window))
11699 goto retry;
11700 }
11701 }
11702
11703 /* If display was paused because of pending input, make sure we do a
11704 thorough update the next time. */
11705 if (pause)
11706 {
11707 /* Prevent the optimization at the beginning of
11708 redisplay_internal that tries a single-line update of the
11709 line containing the cursor in the selected window. */
11710 CHARPOS (this_line_start_pos) = 0;
11711
11712 /* Let the overlay arrow be updated the next time. */
11713 update_overlay_arrows (0);
11714
11715 /* If we pause after scrolling, some rows in the current
11716 matrices of some windows are not valid. */
11717 if (!WINDOW_FULL_WIDTH_P (w)
11718 && !FRAME_WINDOW_P (XFRAME (w->frame)))
11719 update_mode_lines = 1;
11720 }
11721 else
11722 {
11723 if (!consider_all_windows_p)
11724 {
11725 /* This has already been done above if
11726 consider_all_windows_p is set. */
11727 mark_window_display_accurate_1 (w, 1);
11728
11729 /* Say overlay arrows are up to date. */
11730 update_overlay_arrows (1);
11731
11732 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
11733 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
11734 }
11735
11736 update_mode_lines = 0;
11737 windows_or_buffers_changed = 0;
11738 cursor_type_changed = 0;
11739 }
11740
11741 /* Start SIGIO interrupts coming again. Having them off during the
11742 code above makes it less likely one will discard output, but not
11743 impossible, since there might be stuff in the system buffer here.
11744 But it is much hairier to try to do anything about that. */
11745 if (interrupt_input)
11746 request_sigio ();
11747 RESUME_POLLING;
11748
11749 /* If a frame has become visible which was not before, redisplay
11750 again, so that we display it. Expose events for such a frame
11751 (which it gets when becoming visible) don't call the parts of
11752 redisplay constructing glyphs, so simply exposing a frame won't
11753 display anything in this case. So, we have to display these
11754 frames here explicitly. */
11755 if (!pause)
11756 {
11757 Lisp_Object tail, frame;
11758 int new_count = 0;
11759
11760 FOR_EACH_FRAME (tail, frame)
11761 {
11762 int this_is_visible = 0;
11763
11764 if (XFRAME (frame)->visible)
11765 this_is_visible = 1;
11766 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
11767 if (XFRAME (frame)->visible)
11768 this_is_visible = 1;
11769
11770 if (this_is_visible)
11771 new_count++;
11772 }
11773
11774 if (new_count != number_of_visible_frames)
11775 windows_or_buffers_changed++;
11776 }
11777
11778 /* Change frame size now if a change is pending. */
11779 do_pending_window_change (1);
11780
11781 /* If we just did a pending size change, or have additional
11782 visible frames, redisplay again. */
11783 if (windows_or_buffers_changed && !pause)
11784 goto retry;
11785
11786 /* Clear the face cache eventually. */
11787 if (consider_all_windows_p)
11788 {
11789 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
11790 {
11791 clear_face_cache (0);
11792 clear_face_cache_count = 0;
11793 }
11794 #ifdef HAVE_WINDOW_SYSTEM
11795 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
11796 {
11797 Lisp_Object tail, frame;
11798 FOR_EACH_FRAME (tail, frame)
11799 {
11800 struct frame *f = XFRAME (frame);
11801 if (FRAME_WINDOW_P (f))
11802 clear_image_cache (f, 0);
11803 }
11804 clear_image_cache_count = 0;
11805 }
11806 #endif /* HAVE_WINDOW_SYSTEM */
11807 }
11808
11809 end_of_redisplay:
11810 unbind_to (count, Qnil);
11811 RESUME_POLLING;
11812 }
11813
11814
11815 /* Redisplay, but leave alone any recent echo area message unless
11816 another message has been requested in its place.
11817
11818 This is useful in situations where you need to redisplay but no
11819 user action has occurred, making it inappropriate for the message
11820 area to be cleared. See tracking_off and
11821 wait_reading_process_output for examples of these situations.
11822
11823 FROM_WHERE is an integer saying from where this function was
11824 called. This is useful for debugging. */
11825
11826 void
11827 redisplay_preserve_echo_area (from_where)
11828 int from_where;
11829 {
11830 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
11831
11832 if (!NILP (echo_area_buffer[1]))
11833 {
11834 /* We have a previously displayed message, but no current
11835 message. Redisplay the previous message. */
11836 display_last_displayed_message_p = 1;
11837 redisplay_internal (1);
11838 display_last_displayed_message_p = 0;
11839 }
11840 else
11841 redisplay_internal (1);
11842
11843 if (FRAME_RIF (SELECTED_FRAME ()) != NULL
11844 && FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
11845 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (NULL);
11846 }
11847
11848
11849 /* Function registered with record_unwind_protect in
11850 redisplay_internal. Reset redisplaying_p to the value it had
11851 before redisplay_internal was called, and clear
11852 prevent_freeing_realized_faces_p. It also selects the previously
11853 selected frame, unless it has been deleted (by an X connection
11854 failure during redisplay, for example). */
11855
11856 static Lisp_Object
11857 unwind_redisplay (val)
11858 Lisp_Object val;
11859 {
11860 Lisp_Object old_redisplaying_p, old_frame;
11861
11862 old_redisplaying_p = XCAR (val);
11863 redisplaying_p = XFASTINT (old_redisplaying_p);
11864 old_frame = XCDR (val);
11865 if (! EQ (old_frame, selected_frame)
11866 && FRAME_LIVE_P (XFRAME (old_frame)))
11867 select_frame_for_redisplay (old_frame);
11868 return Qnil;
11869 }
11870
11871
11872 /* Mark the display of window W as accurate or inaccurate. If
11873 ACCURATE_P is non-zero mark display of W as accurate. If
11874 ACCURATE_P is zero, arrange for W to be redisplayed the next time
11875 redisplay_internal is called. */
11876
11877 static void
11878 mark_window_display_accurate_1 (w, accurate_p)
11879 struct window *w;
11880 int accurate_p;
11881 {
11882 if (BUFFERP (w->buffer))
11883 {
11884 struct buffer *b = XBUFFER (w->buffer);
11885
11886 w->last_modified
11887 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
11888 w->last_overlay_modified
11889 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
11890 w->last_had_star
11891 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
11892
11893 if (accurate_p)
11894 {
11895 b->clip_changed = 0;
11896 b->prevent_redisplay_optimizations_p = 0;
11897
11898 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
11899 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
11900 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
11901 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
11902
11903 w->current_matrix->buffer = b;
11904 w->current_matrix->begv = BUF_BEGV (b);
11905 w->current_matrix->zv = BUF_ZV (b);
11906
11907 w->last_cursor = w->cursor;
11908 w->last_cursor_off_p = w->cursor_off_p;
11909
11910 if (w == XWINDOW (selected_window))
11911 w->last_point = make_number (BUF_PT (b));
11912 else
11913 w->last_point = make_number (XMARKER (w->pointm)->charpos);
11914 }
11915 }
11916
11917 if (accurate_p)
11918 {
11919 w->window_end_valid = w->buffer;
11920 #if 0 /* This is incorrect with variable-height lines. */
11921 xassert (XINT (w->window_end_vpos)
11922 < (WINDOW_TOTAL_LINES (w)
11923 - (WINDOW_WANTS_MODELINE_P (w) ? 1 : 0)));
11924 #endif
11925 w->update_mode_line = Qnil;
11926 }
11927 }
11928
11929
11930 /* Mark the display of windows in the window tree rooted at WINDOW as
11931 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
11932 windows as accurate. If ACCURATE_P is zero, arrange for windows to
11933 be redisplayed the next time redisplay_internal is called. */
11934
11935 void
11936 mark_window_display_accurate (window, accurate_p)
11937 Lisp_Object window;
11938 int accurate_p;
11939 {
11940 struct window *w;
11941
11942 for (; !NILP (window); window = w->next)
11943 {
11944 w = XWINDOW (window);
11945 mark_window_display_accurate_1 (w, accurate_p);
11946
11947 if (!NILP (w->vchild))
11948 mark_window_display_accurate (w->vchild, accurate_p);
11949 if (!NILP (w->hchild))
11950 mark_window_display_accurate (w->hchild, accurate_p);
11951 }
11952
11953 if (accurate_p)
11954 {
11955 update_overlay_arrows (1);
11956 }
11957 else
11958 {
11959 /* Force a thorough redisplay the next time by setting
11960 last_arrow_position and last_arrow_string to t, which is
11961 unequal to any useful value of Voverlay_arrow_... */
11962 update_overlay_arrows (-1);
11963 }
11964 }
11965
11966
11967 /* Return value in display table DP (Lisp_Char_Table *) for character
11968 C. Since a display table doesn't have any parent, we don't have to
11969 follow parent. Do not call this function directly but use the
11970 macro DISP_CHAR_VECTOR. */
11971
11972 Lisp_Object
11973 disp_char_vector (dp, c)
11974 struct Lisp_Char_Table *dp;
11975 int c;
11976 {
11977 Lisp_Object val;
11978
11979 if (ASCII_CHAR_P (c))
11980 {
11981 val = dp->ascii;
11982 if (SUB_CHAR_TABLE_P (val))
11983 val = XSUB_CHAR_TABLE (val)->contents[c];
11984 }
11985 else
11986 {
11987 Lisp_Object table;
11988
11989 XSETCHAR_TABLE (table, dp);
11990 val = char_table_ref (table, c);
11991 }
11992 if (NILP (val))
11993 val = dp->defalt;
11994 return val;
11995 }
11996
11997
11998 \f
11999 /***********************************************************************
12000 Window Redisplay
12001 ***********************************************************************/
12002
12003 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
12004
12005 static void
12006 redisplay_windows (window)
12007 Lisp_Object window;
12008 {
12009 while (!NILP (window))
12010 {
12011 struct window *w = XWINDOW (window);
12012
12013 if (!NILP (w->hchild))
12014 redisplay_windows (w->hchild);
12015 else if (!NILP (w->vchild))
12016 redisplay_windows (w->vchild);
12017 else
12018 {
12019 displayed_buffer = XBUFFER (w->buffer);
12020 /* Use list_of_error, not Qerror, so that
12021 we catch only errors and don't run the debugger. */
12022 internal_condition_case_1 (redisplay_window_0, window,
12023 list_of_error,
12024 redisplay_window_error);
12025 }
12026
12027 window = w->next;
12028 }
12029 }
12030
12031 static Lisp_Object
12032 redisplay_window_error ()
12033 {
12034 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
12035 return Qnil;
12036 }
12037
12038 static Lisp_Object
12039 redisplay_window_0 (window)
12040 Lisp_Object window;
12041 {
12042 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
12043 redisplay_window (window, 0);
12044 return Qnil;
12045 }
12046
12047 static Lisp_Object
12048 redisplay_window_1 (window)
12049 Lisp_Object window;
12050 {
12051 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
12052 redisplay_window (window, 1);
12053 return Qnil;
12054 }
12055 \f
12056
12057 /* Increment GLYPH until it reaches END or CONDITION fails while
12058 adding (GLYPH)->pixel_width to X. */
12059
12060 #define SKIP_GLYPHS(glyph, end, x, condition) \
12061 do \
12062 { \
12063 (x) += (glyph)->pixel_width; \
12064 ++(glyph); \
12065 } \
12066 while ((glyph) < (end) && (condition))
12067
12068
12069 /* Set cursor position of W. PT is assumed to be displayed in ROW.
12070 DELTA is the number of bytes by which positions recorded in ROW
12071 differ from current buffer positions.
12072
12073 Return 0 if cursor is not on this row. 1 otherwise. */
12074
12075 int
12076 set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
12077 struct window *w;
12078 struct glyph_row *row;
12079 struct glyph_matrix *matrix;
12080 int delta, delta_bytes, dy, dvpos;
12081 {
12082 struct glyph *glyph = row->glyphs[TEXT_AREA];
12083 struct glyph *end = glyph + row->used[TEXT_AREA];
12084 struct glyph *cursor = NULL;
12085 /* The first glyph that starts a sequence of glyphs from string. */
12086 struct glyph *string_start;
12087 /* The X coordinate of string_start. */
12088 int string_start_x;
12089 /* The last known character position. */
12090 int last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
12091 /* The last known character position before string_start. */
12092 int string_before_pos;
12093 int x = row->x;
12094 int cursor_x = x;
12095 int cursor_from_overlay_pos = 0;
12096 int pt_old = PT - delta;
12097
12098 /* Skip over glyphs not having an object at the start of the row.
12099 These are special glyphs like truncation marks on terminal
12100 frames. */
12101 if (row->displays_text_p)
12102 while (glyph < end
12103 && INTEGERP (glyph->object)
12104 && glyph->charpos < 0)
12105 {
12106 x += glyph->pixel_width;
12107 ++glyph;
12108 }
12109
12110 string_start = NULL;
12111 while (glyph < end
12112 && !INTEGERP (glyph->object)
12113 && (!BUFFERP (glyph->object)
12114 || (last_pos = glyph->charpos) < pt_old))
12115 {
12116 if (! STRINGP (glyph->object))
12117 {
12118 string_start = NULL;
12119 x += glyph->pixel_width;
12120 ++glyph;
12121 if (cursor_from_overlay_pos
12122 && last_pos >= cursor_from_overlay_pos)
12123 {
12124 cursor_from_overlay_pos = 0;
12125 cursor = 0;
12126 }
12127 }
12128 else
12129 {
12130 if (string_start == NULL)
12131 {
12132 string_before_pos = last_pos;
12133 string_start = glyph;
12134 string_start_x = x;
12135 }
12136 /* Skip all glyphs from string. */
12137 do
12138 {
12139 Lisp_Object cprop;
12140 int pos;
12141 if ((cursor == NULL || glyph > cursor)
12142 && (cprop = Fget_char_property (make_number ((glyph)->charpos),
12143 Qcursor, (glyph)->object),
12144 !NILP (cprop))
12145 && (pos = string_buffer_position (w, glyph->object,
12146 string_before_pos),
12147 (pos == 0 /* From overlay */
12148 || pos == pt_old)))
12149 {
12150 /* Estimate overlay buffer position from the buffer
12151 positions of the glyphs before and after the overlay.
12152 Add 1 to last_pos so that if point corresponds to the
12153 glyph right after the overlay, we still use a 'cursor'
12154 property found in that overlay. */
12155 cursor_from_overlay_pos = (pos ? 0 : last_pos
12156 + (INTEGERP (cprop) ? XINT (cprop) : 0));
12157 cursor = glyph;
12158 cursor_x = x;
12159 }
12160 x += glyph->pixel_width;
12161 ++glyph;
12162 }
12163 while (glyph < end && EQ (glyph->object, string_start->object));
12164 }
12165 }
12166
12167 if (cursor != NULL)
12168 {
12169 glyph = cursor;
12170 x = cursor_x;
12171 }
12172 else if (row->ends_in_ellipsis_p && glyph == end)
12173 {
12174 /* Scan back over the ellipsis glyphs, decrementing positions. */
12175 while (glyph > row->glyphs[TEXT_AREA]
12176 && (glyph - 1)->charpos == last_pos)
12177 glyph--, x -= glyph->pixel_width;
12178 /* That loop always goes one position too far,
12179 including the glyph before the ellipsis.
12180 So scan forward over that one. */
12181 x += glyph->pixel_width;
12182 glyph++;
12183 }
12184 else if (string_start
12185 && (glyph == end || !BUFFERP (glyph->object) || last_pos > pt_old))
12186 {
12187 /* We may have skipped over point because the previous glyphs
12188 are from string. As there's no easy way to know the
12189 character position of the current glyph, find the correct
12190 glyph on point by scanning from string_start again. */
12191 Lisp_Object limit;
12192 Lisp_Object string;
12193 struct glyph *stop = glyph;
12194 int pos;
12195
12196 limit = make_number (pt_old + 1);
12197 glyph = string_start;
12198 x = string_start_x;
12199 string = glyph->object;
12200 pos = string_buffer_position (w, string, string_before_pos);
12201 /* If STRING is from overlay, LAST_POS == 0. We skip such glyphs
12202 because we always put cursor after overlay strings. */
12203 while (pos == 0 && glyph < stop)
12204 {
12205 string = glyph->object;
12206 SKIP_GLYPHS (glyph, stop, x, EQ (glyph->object, string));
12207 if (glyph < stop)
12208 pos = string_buffer_position (w, glyph->object, string_before_pos);
12209 }
12210
12211 while (glyph < stop)
12212 {
12213 pos = XINT (Fnext_single_char_property_change
12214 (make_number (pos), Qdisplay, Qnil, limit));
12215 if (pos > pt_old)
12216 break;
12217 /* Skip glyphs from the same string. */
12218 string = glyph->object;
12219 SKIP_GLYPHS (glyph, stop, x, EQ (glyph->object, string));
12220 /* Skip glyphs from an overlay. */
12221 while (glyph < stop
12222 && ! string_buffer_position (w, glyph->object, pos))
12223 {
12224 string = glyph->object;
12225 SKIP_GLYPHS (glyph, stop, x, EQ (glyph->object, string));
12226 }
12227 }
12228
12229 /* If we reached the end of the line, and end was from a string,
12230 cursor is not on this line. */
12231 if (glyph == end && row->continued_p)
12232 return 0;
12233 }
12234
12235 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
12236 w->cursor.x = x;
12237 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
12238 w->cursor.y = row->y + dy;
12239
12240 if (w == XWINDOW (selected_window))
12241 {
12242 if (!row->continued_p
12243 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
12244 && row->x == 0)
12245 {
12246 this_line_buffer = XBUFFER (w->buffer);
12247
12248 CHARPOS (this_line_start_pos)
12249 = MATRIX_ROW_START_CHARPOS (row) + delta;
12250 BYTEPOS (this_line_start_pos)
12251 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
12252
12253 CHARPOS (this_line_end_pos)
12254 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
12255 BYTEPOS (this_line_end_pos)
12256 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
12257
12258 this_line_y = w->cursor.y;
12259 this_line_pixel_height = row->height;
12260 this_line_vpos = w->cursor.vpos;
12261 this_line_start_x = row->x;
12262 }
12263 else
12264 CHARPOS (this_line_start_pos) = 0;
12265 }
12266
12267 return 1;
12268 }
12269
12270
12271 /* Run window scroll functions, if any, for WINDOW with new window
12272 start STARTP. Sets the window start of WINDOW to that position.
12273
12274 We assume that the window's buffer is really current. */
12275
12276 static INLINE struct text_pos
12277 run_window_scroll_functions (window, startp)
12278 Lisp_Object window;
12279 struct text_pos startp;
12280 {
12281 struct window *w = XWINDOW (window);
12282 SET_MARKER_FROM_TEXT_POS (w->start, startp);
12283
12284 if (current_buffer != XBUFFER (w->buffer))
12285 abort ();
12286
12287 if (!NILP (Vwindow_scroll_functions))
12288 {
12289 run_hook_with_args_2 (Qwindow_scroll_functions, window,
12290 make_number (CHARPOS (startp)));
12291 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12292 /* In case the hook functions switch buffers. */
12293 if (current_buffer != XBUFFER (w->buffer))
12294 set_buffer_internal_1 (XBUFFER (w->buffer));
12295 }
12296
12297 return startp;
12298 }
12299
12300
12301 /* Make sure the line containing the cursor is fully visible.
12302 A value of 1 means there is nothing to be done.
12303 (Either the line is fully visible, or it cannot be made so,
12304 or we cannot tell.)
12305
12306 If FORCE_P is non-zero, return 0 even if partial visible cursor row
12307 is higher than window.
12308
12309 A value of 0 means the caller should do scrolling
12310 as if point had gone off the screen. */
12311
12312 static int
12313 cursor_row_fully_visible_p (w, force_p, current_matrix_p)
12314 struct window *w;
12315 int force_p;
12316 int current_matrix_p;
12317 {
12318 struct glyph_matrix *matrix;
12319 struct glyph_row *row;
12320 int window_height;
12321
12322 if (!make_cursor_line_fully_visible_p)
12323 return 1;
12324
12325 /* It's not always possible to find the cursor, e.g, when a window
12326 is full of overlay strings. Don't do anything in that case. */
12327 if (w->cursor.vpos < 0)
12328 return 1;
12329
12330 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
12331 row = MATRIX_ROW (matrix, w->cursor.vpos);
12332
12333 /* If the cursor row is not partially visible, there's nothing to do. */
12334 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
12335 return 1;
12336
12337 /* If the row the cursor is in is taller than the window's height,
12338 it's not clear what to do, so do nothing. */
12339 window_height = window_box_height (w);
12340 if (row->height >= window_height)
12341 {
12342 if (!force_p || MINI_WINDOW_P (w)
12343 || w->vscroll || w->cursor.vpos == 0)
12344 return 1;
12345 }
12346 return 0;
12347
12348 #if 0
12349 /* This code used to try to scroll the window just enough to make
12350 the line visible. It returned 0 to say that the caller should
12351 allocate larger glyph matrices. */
12352
12353 if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (w, row))
12354 {
12355 int dy = row->height - row->visible_height;
12356 w->vscroll = 0;
12357 w->cursor.y += dy;
12358 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
12359 }
12360 else /* MATRIX_ROW_PARTIALLY_VISIBLE_AT_BOTTOM_P (w, row)) */
12361 {
12362 int dy = - (row->height - row->visible_height);
12363 w->vscroll = dy;
12364 w->cursor.y += dy;
12365 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
12366 }
12367
12368 /* When we change the cursor y-position of the selected window,
12369 change this_line_y as well so that the display optimization for
12370 the cursor line of the selected window in redisplay_internal uses
12371 the correct y-position. */
12372 if (w == XWINDOW (selected_window))
12373 this_line_y = w->cursor.y;
12374
12375 /* If vscrolling requires a larger glyph matrix, arrange for a fresh
12376 redisplay with larger matrices. */
12377 if (matrix->nrows < required_matrix_height (w))
12378 {
12379 fonts_changed_p = 1;
12380 return 0;
12381 }
12382
12383 return 1;
12384 #endif /* 0 */
12385 }
12386
12387
12388 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
12389 non-zero means only WINDOW is redisplayed in redisplay_internal.
12390 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
12391 in redisplay_window to bring a partially visible line into view in
12392 the case that only the cursor has moved.
12393
12394 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
12395 last screen line's vertical height extends past the end of the screen.
12396
12397 Value is
12398
12399 1 if scrolling succeeded
12400
12401 0 if scrolling didn't find point.
12402
12403 -1 if new fonts have been loaded so that we must interrupt
12404 redisplay, adjust glyph matrices, and try again. */
12405
12406 enum
12407 {
12408 SCROLLING_SUCCESS,
12409 SCROLLING_FAILED,
12410 SCROLLING_NEED_LARGER_MATRICES
12411 };
12412
12413 static int
12414 try_scrolling (window, just_this_one_p, scroll_conservatively,
12415 scroll_step, temp_scroll_step, last_line_misfit)
12416 Lisp_Object window;
12417 int just_this_one_p;
12418 EMACS_INT scroll_conservatively, scroll_step;
12419 int temp_scroll_step;
12420 int last_line_misfit;
12421 {
12422 struct window *w = XWINDOW (window);
12423 struct frame *f = XFRAME (w->frame);
12424 struct text_pos scroll_margin_pos;
12425 struct text_pos pos;
12426 struct text_pos startp;
12427 struct it it;
12428 Lisp_Object window_end;
12429 int this_scroll_margin;
12430 int dy = 0;
12431 int scroll_max;
12432 int rc;
12433 int amount_to_scroll = 0;
12434 Lisp_Object aggressive;
12435 int height;
12436 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
12437
12438 #if GLYPH_DEBUG
12439 debug_method_add (w, "try_scrolling");
12440 #endif
12441
12442 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12443
12444 /* Compute scroll margin height in pixels. We scroll when point is
12445 within this distance from the top or bottom of the window. */
12446 if (scroll_margin > 0)
12447 {
12448 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
12449 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
12450 }
12451 else
12452 this_scroll_margin = 0;
12453
12454 /* Force scroll_conservatively to have a reasonable value so it doesn't
12455 cause an overflow while computing how much to scroll. */
12456 if (scroll_conservatively)
12457 scroll_conservatively = min (scroll_conservatively,
12458 MOST_POSITIVE_FIXNUM / FRAME_LINE_HEIGHT (f));
12459
12460 /* Compute how much we should try to scroll maximally to bring point
12461 into view. */
12462 if (scroll_step || scroll_conservatively || temp_scroll_step)
12463 scroll_max = max (scroll_step,
12464 max (scroll_conservatively, temp_scroll_step));
12465 else if (NUMBERP (current_buffer->scroll_down_aggressively)
12466 || NUMBERP (current_buffer->scroll_up_aggressively))
12467 /* We're trying to scroll because of aggressive scrolling
12468 but no scroll_step is set. Choose an arbitrary one. Maybe
12469 there should be a variable for this. */
12470 scroll_max = 10;
12471 else
12472 scroll_max = 0;
12473 scroll_max *= FRAME_LINE_HEIGHT (f);
12474
12475 /* Decide whether we have to scroll down. Start at the window end
12476 and move this_scroll_margin up to find the position of the scroll
12477 margin. */
12478 window_end = Fwindow_end (window, Qt);
12479
12480 too_near_end:
12481
12482 CHARPOS (scroll_margin_pos) = XINT (window_end);
12483 BYTEPOS (scroll_margin_pos) = CHAR_TO_BYTE (CHARPOS (scroll_margin_pos));
12484
12485 if (this_scroll_margin || extra_scroll_margin_lines)
12486 {
12487 start_display (&it, w, scroll_margin_pos);
12488 if (this_scroll_margin)
12489 move_it_vertically_backward (&it, this_scroll_margin);
12490 if (extra_scroll_margin_lines)
12491 move_it_by_lines (&it, - extra_scroll_margin_lines, 0);
12492 scroll_margin_pos = it.current.pos;
12493 }
12494
12495 if (PT >= CHARPOS (scroll_margin_pos))
12496 {
12497 int y0;
12498
12499 /* Point is in the scroll margin at the bottom of the window, or
12500 below. Compute a new window start that makes point visible. */
12501
12502 /* Compute the distance from the scroll margin to PT.
12503 Give up if the distance is greater than scroll_max. */
12504 start_display (&it, w, scroll_margin_pos);
12505 y0 = it.current_y;
12506 move_it_to (&it, PT, 0, it.last_visible_y, -1,
12507 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
12508
12509 /* To make point visible, we have to move the window start
12510 down so that the line the cursor is in is visible, which
12511 means we have to add in the height of the cursor line. */
12512 dy = line_bottom_y (&it) - y0;
12513
12514 if (dy > scroll_max)
12515 return SCROLLING_FAILED;
12516
12517 /* Move the window start down. If scrolling conservatively,
12518 move it just enough down to make point visible. If
12519 scroll_step is set, move it down by scroll_step. */
12520 start_display (&it, w, startp);
12521
12522 if (scroll_conservatively)
12523 /* Set AMOUNT_TO_SCROLL to at least one line,
12524 and at most scroll_conservatively lines. */
12525 amount_to_scroll
12526 = min (max (dy, FRAME_LINE_HEIGHT (f)),
12527 FRAME_LINE_HEIGHT (f) * scroll_conservatively);
12528 else if (scroll_step || temp_scroll_step)
12529 amount_to_scroll = scroll_max;
12530 else
12531 {
12532 aggressive = current_buffer->scroll_up_aggressively;
12533 height = WINDOW_BOX_TEXT_HEIGHT (w);
12534 if (NUMBERP (aggressive))
12535 {
12536 double float_amount = XFLOATINT (aggressive) * height;
12537 amount_to_scroll = float_amount;
12538 if (amount_to_scroll == 0 && float_amount > 0)
12539 amount_to_scroll = 1;
12540 }
12541 }
12542
12543 if (amount_to_scroll <= 0)
12544 return SCROLLING_FAILED;
12545
12546 /* If moving by amount_to_scroll leaves STARTP unchanged,
12547 move it down one screen line. */
12548
12549 move_it_vertically (&it, amount_to_scroll);
12550 if (CHARPOS (it.current.pos) == CHARPOS (startp))
12551 move_it_by_lines (&it, 1, 1);
12552 startp = it.current.pos;
12553 }
12554 else
12555 {
12556 /* See if point is inside the scroll margin at the top of the
12557 window. */
12558 scroll_margin_pos = startp;
12559 if (this_scroll_margin)
12560 {
12561 start_display (&it, w, startp);
12562 move_it_vertically (&it, this_scroll_margin);
12563 scroll_margin_pos = it.current.pos;
12564 }
12565
12566 if (PT < CHARPOS (scroll_margin_pos))
12567 {
12568 /* Point is in the scroll margin at the top of the window or
12569 above what is displayed in the window. */
12570 int y0;
12571
12572 /* Compute the vertical distance from PT to the scroll
12573 margin position. Give up if distance is greater than
12574 scroll_max. */
12575 SET_TEXT_POS (pos, PT, PT_BYTE);
12576 start_display (&it, w, pos);
12577 y0 = it.current_y;
12578 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
12579 it.last_visible_y, -1,
12580 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
12581 dy = it.current_y - y0;
12582 if (dy > scroll_max)
12583 return SCROLLING_FAILED;
12584
12585 /* Compute new window start. */
12586 start_display (&it, w, startp);
12587
12588 if (scroll_conservatively)
12589 amount_to_scroll
12590 = max (dy, FRAME_LINE_HEIGHT (f) * max (scroll_step, temp_scroll_step));
12591 else if (scroll_step || temp_scroll_step)
12592 amount_to_scroll = scroll_max;
12593 else
12594 {
12595 aggressive = current_buffer->scroll_down_aggressively;
12596 height = WINDOW_BOX_TEXT_HEIGHT (w);
12597 if (NUMBERP (aggressive))
12598 {
12599 double float_amount = XFLOATINT (aggressive) * height;
12600 amount_to_scroll = float_amount;
12601 if (amount_to_scroll == 0 && float_amount > 0)
12602 amount_to_scroll = 1;
12603 }
12604 }
12605
12606 if (amount_to_scroll <= 0)
12607 return SCROLLING_FAILED;
12608
12609 move_it_vertically_backward (&it, amount_to_scroll);
12610 startp = it.current.pos;
12611 }
12612 }
12613
12614 /* Run window scroll functions. */
12615 startp = run_window_scroll_functions (window, startp);
12616
12617 /* Display the window. Give up if new fonts are loaded, or if point
12618 doesn't appear. */
12619 if (!try_window (window, startp, 0))
12620 rc = SCROLLING_NEED_LARGER_MATRICES;
12621 else if (w->cursor.vpos < 0)
12622 {
12623 clear_glyph_matrix (w->desired_matrix);
12624 rc = SCROLLING_FAILED;
12625 }
12626 else
12627 {
12628 /* Maybe forget recorded base line for line number display. */
12629 if (!just_this_one_p
12630 || current_buffer->clip_changed
12631 || BEG_UNCHANGED < CHARPOS (startp))
12632 w->base_line_number = Qnil;
12633
12634 /* If cursor ends up on a partially visible line,
12635 treat that as being off the bottom of the screen. */
12636 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0))
12637 {
12638 clear_glyph_matrix (w->desired_matrix);
12639 ++extra_scroll_margin_lines;
12640 goto too_near_end;
12641 }
12642 rc = SCROLLING_SUCCESS;
12643 }
12644
12645 return rc;
12646 }
12647
12648
12649 /* Compute a suitable window start for window W if display of W starts
12650 on a continuation line. Value is non-zero if a new window start
12651 was computed.
12652
12653 The new window start will be computed, based on W's width, starting
12654 from the start of the continued line. It is the start of the
12655 screen line with the minimum distance from the old start W->start. */
12656
12657 static int
12658 compute_window_start_on_continuation_line (w)
12659 struct window *w;
12660 {
12661 struct text_pos pos, start_pos;
12662 int window_start_changed_p = 0;
12663
12664 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
12665
12666 /* If window start is on a continuation line... Window start may be
12667 < BEGV in case there's invisible text at the start of the
12668 buffer (M-x rmail, for example). */
12669 if (CHARPOS (start_pos) > BEGV
12670 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
12671 {
12672 struct it it;
12673 struct glyph_row *row;
12674
12675 /* Handle the case that the window start is out of range. */
12676 if (CHARPOS (start_pos) < BEGV)
12677 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
12678 else if (CHARPOS (start_pos) > ZV)
12679 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
12680
12681 /* Find the start of the continued line. This should be fast
12682 because scan_buffer is fast (newline cache). */
12683 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
12684 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
12685 row, DEFAULT_FACE_ID);
12686 reseat_at_previous_visible_line_start (&it);
12687
12688 /* If the line start is "too far" away from the window start,
12689 say it takes too much time to compute a new window start. */
12690 if (CHARPOS (start_pos) - IT_CHARPOS (it)
12691 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
12692 {
12693 int min_distance, distance;
12694
12695 /* Move forward by display lines to find the new window
12696 start. If window width was enlarged, the new start can
12697 be expected to be > the old start. If window width was
12698 decreased, the new window start will be < the old start.
12699 So, we're looking for the display line start with the
12700 minimum distance from the old window start. */
12701 pos = it.current.pos;
12702 min_distance = INFINITY;
12703 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
12704 distance < min_distance)
12705 {
12706 min_distance = distance;
12707 pos = it.current.pos;
12708 move_it_by_lines (&it, 1, 0);
12709 }
12710
12711 /* Set the window start there. */
12712 SET_MARKER_FROM_TEXT_POS (w->start, pos);
12713 window_start_changed_p = 1;
12714 }
12715 }
12716
12717 return window_start_changed_p;
12718 }
12719
12720
12721 /* Try cursor movement in case text has not changed in window WINDOW,
12722 with window start STARTP. Value is
12723
12724 CURSOR_MOVEMENT_SUCCESS if successful
12725
12726 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
12727
12728 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
12729 display. *SCROLL_STEP is set to 1, under certain circumstances, if
12730 we want to scroll as if scroll-step were set to 1. See the code.
12731
12732 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
12733 which case we have to abort this redisplay, and adjust matrices
12734 first. */
12735
12736 enum
12737 {
12738 CURSOR_MOVEMENT_SUCCESS,
12739 CURSOR_MOVEMENT_CANNOT_BE_USED,
12740 CURSOR_MOVEMENT_MUST_SCROLL,
12741 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
12742 };
12743
12744 static int
12745 try_cursor_movement (window, startp, scroll_step)
12746 Lisp_Object window;
12747 struct text_pos startp;
12748 int *scroll_step;
12749 {
12750 struct window *w = XWINDOW (window);
12751 struct frame *f = XFRAME (w->frame);
12752 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
12753
12754 #if GLYPH_DEBUG
12755 if (inhibit_try_cursor_movement)
12756 return rc;
12757 #endif
12758
12759 /* Handle case where text has not changed, only point, and it has
12760 not moved off the frame. */
12761 if (/* Point may be in this window. */
12762 PT >= CHARPOS (startp)
12763 /* Selective display hasn't changed. */
12764 && !current_buffer->clip_changed
12765 /* Function force-mode-line-update is used to force a thorough
12766 redisplay. It sets either windows_or_buffers_changed or
12767 update_mode_lines. So don't take a shortcut here for these
12768 cases. */
12769 && !update_mode_lines
12770 && !windows_or_buffers_changed
12771 && !cursor_type_changed
12772 /* Can't use this case if highlighting a region. When a
12773 region exists, cursor movement has to do more than just
12774 set the cursor. */
12775 && !(!NILP (Vtransient_mark_mode)
12776 && !NILP (current_buffer->mark_active))
12777 && NILP (w->region_showing)
12778 && NILP (Vshow_trailing_whitespace)
12779 /* Right after splitting windows, last_point may be nil. */
12780 && INTEGERP (w->last_point)
12781 /* This code is not used for mini-buffer for the sake of the case
12782 of redisplaying to replace an echo area message; since in
12783 that case the mini-buffer contents per se are usually
12784 unchanged. This code is of no real use in the mini-buffer
12785 since the handling of this_line_start_pos, etc., in redisplay
12786 handles the same cases. */
12787 && !EQ (window, minibuf_window)
12788 /* When splitting windows or for new windows, it happens that
12789 redisplay is called with a nil window_end_vpos or one being
12790 larger than the window. This should really be fixed in
12791 window.c. I don't have this on my list, now, so we do
12792 approximately the same as the old redisplay code. --gerd. */
12793 && INTEGERP (w->window_end_vpos)
12794 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
12795 && (FRAME_WINDOW_P (f)
12796 || !overlay_arrow_in_current_buffer_p ()))
12797 {
12798 int this_scroll_margin, top_scroll_margin;
12799 struct glyph_row *row = NULL;
12800
12801 #if GLYPH_DEBUG
12802 debug_method_add (w, "cursor movement");
12803 #endif
12804
12805 /* Scroll if point within this distance from the top or bottom
12806 of the window. This is a pixel value. */
12807 this_scroll_margin = max (0, scroll_margin);
12808 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
12809 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
12810
12811 top_scroll_margin = this_scroll_margin;
12812 if (WINDOW_WANTS_HEADER_LINE_P (w))
12813 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
12814
12815 /* Start with the row the cursor was displayed during the last
12816 not paused redisplay. Give up if that row is not valid. */
12817 if (w->last_cursor.vpos < 0
12818 || w->last_cursor.vpos >= w->current_matrix->nrows)
12819 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12820 else
12821 {
12822 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
12823 if (row->mode_line_p)
12824 ++row;
12825 if (!row->enabled_p)
12826 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12827 }
12828
12829 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
12830 {
12831 int scroll_p = 0;
12832 int last_y = window_text_bottom_y (w) - this_scroll_margin;
12833
12834 if (PT > XFASTINT (w->last_point))
12835 {
12836 /* Point has moved forward. */
12837 while (MATRIX_ROW_END_CHARPOS (row) < PT
12838 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
12839 {
12840 xassert (row->enabled_p);
12841 ++row;
12842 }
12843
12844 /* The end position of a row equals the start position
12845 of the next row. If PT is there, we would rather
12846 display it in the next line. */
12847 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
12848 && MATRIX_ROW_END_CHARPOS (row) == PT
12849 && !cursor_row_p (w, row))
12850 ++row;
12851
12852 /* If within the scroll margin, scroll. Note that
12853 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
12854 the next line would be drawn, and that
12855 this_scroll_margin can be zero. */
12856 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
12857 || PT > MATRIX_ROW_END_CHARPOS (row)
12858 /* Line is completely visible last line in window
12859 and PT is to be set in the next line. */
12860 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
12861 && PT == MATRIX_ROW_END_CHARPOS (row)
12862 && !row->ends_at_zv_p
12863 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
12864 scroll_p = 1;
12865 }
12866 else if (PT < XFASTINT (w->last_point))
12867 {
12868 /* Cursor has to be moved backward. Note that PT >=
12869 CHARPOS (startp) because of the outer if-statement. */
12870 while (!row->mode_line_p
12871 && (MATRIX_ROW_START_CHARPOS (row) > PT
12872 || (MATRIX_ROW_START_CHARPOS (row) == PT
12873 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
12874 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
12875 row > w->current_matrix->rows
12876 && (row-1)->ends_in_newline_from_string_p))))
12877 && (row->y > top_scroll_margin
12878 || CHARPOS (startp) == BEGV))
12879 {
12880 xassert (row->enabled_p);
12881 --row;
12882 }
12883
12884 /* Consider the following case: Window starts at BEGV,
12885 there is invisible, intangible text at BEGV, so that
12886 display starts at some point START > BEGV. It can
12887 happen that we are called with PT somewhere between
12888 BEGV and START. Try to handle that case. */
12889 if (row < w->current_matrix->rows
12890 || row->mode_line_p)
12891 {
12892 row = w->current_matrix->rows;
12893 if (row->mode_line_p)
12894 ++row;
12895 }
12896
12897 /* Due to newlines in overlay strings, we may have to
12898 skip forward over overlay strings. */
12899 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
12900 && MATRIX_ROW_END_CHARPOS (row) == PT
12901 && !cursor_row_p (w, row))
12902 ++row;
12903
12904 /* If within the scroll margin, scroll. */
12905 if (row->y < top_scroll_margin
12906 && CHARPOS (startp) != BEGV)
12907 scroll_p = 1;
12908 }
12909 else
12910 {
12911 /* Cursor did not move. So don't scroll even if cursor line
12912 is partially visible, as it was so before. */
12913 rc = CURSOR_MOVEMENT_SUCCESS;
12914 }
12915
12916 if (PT < MATRIX_ROW_START_CHARPOS (row)
12917 || PT > MATRIX_ROW_END_CHARPOS (row))
12918 {
12919 /* if PT is not in the glyph row, give up. */
12920 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12921 }
12922 else if (rc != CURSOR_MOVEMENT_SUCCESS
12923 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
12924 && make_cursor_line_fully_visible_p)
12925 {
12926 if (PT == MATRIX_ROW_END_CHARPOS (row)
12927 && !row->ends_at_zv_p
12928 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
12929 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12930 else if (row->height > window_box_height (w))
12931 {
12932 /* If we end up in a partially visible line, let's
12933 make it fully visible, except when it's taller
12934 than the window, in which case we can't do much
12935 about it. */
12936 *scroll_step = 1;
12937 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12938 }
12939 else
12940 {
12941 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
12942 if (!cursor_row_fully_visible_p (w, 0, 1))
12943 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12944 else
12945 rc = CURSOR_MOVEMENT_SUCCESS;
12946 }
12947 }
12948 else if (scroll_p)
12949 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12950 else
12951 {
12952 do
12953 {
12954 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
12955 {
12956 rc = CURSOR_MOVEMENT_SUCCESS;
12957 break;
12958 }
12959 ++row;
12960 }
12961 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
12962 && MATRIX_ROW_START_CHARPOS (row) == PT
12963 && cursor_row_p (w, row));
12964 }
12965 }
12966 }
12967
12968 return rc;
12969 }
12970
12971 void
12972 set_vertical_scroll_bar (w)
12973 struct window *w;
12974 {
12975 int start, end, whole;
12976
12977 /* Calculate the start and end positions for the current window.
12978 At some point, it would be nice to choose between scrollbars
12979 which reflect the whole buffer size, with special markers
12980 indicating narrowing, and scrollbars which reflect only the
12981 visible region.
12982
12983 Note that mini-buffers sometimes aren't displaying any text. */
12984 if (!MINI_WINDOW_P (w)
12985 || (w == XWINDOW (minibuf_window)
12986 && NILP (echo_area_buffer[0])))
12987 {
12988 struct buffer *buf = XBUFFER (w->buffer);
12989 whole = BUF_ZV (buf) - BUF_BEGV (buf);
12990 start = marker_position (w->start) - BUF_BEGV (buf);
12991 /* I don't think this is guaranteed to be right. For the
12992 moment, we'll pretend it is. */
12993 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
12994
12995 if (end < start)
12996 end = start;
12997 if (whole < (end - start))
12998 whole = end - start;
12999 }
13000 else
13001 start = end = whole = 0;
13002
13003 /* Indicate what this scroll bar ought to be displaying now. */
13004 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
13005 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
13006 (w, end - start, whole, start);
13007 }
13008
13009
13010 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
13011 selected_window is redisplayed.
13012
13013 We can return without actually redisplaying the window if
13014 fonts_changed_p is nonzero. In that case, redisplay_internal will
13015 retry. */
13016
13017 static void
13018 redisplay_window (window, just_this_one_p)
13019 Lisp_Object window;
13020 int just_this_one_p;
13021 {
13022 struct window *w = XWINDOW (window);
13023 struct frame *f = XFRAME (w->frame);
13024 struct buffer *buffer = XBUFFER (w->buffer);
13025 struct buffer *old = current_buffer;
13026 struct text_pos lpoint, opoint, startp;
13027 int update_mode_line;
13028 int tem;
13029 struct it it;
13030 /* Record it now because it's overwritten. */
13031 int current_matrix_up_to_date_p = 0;
13032 int used_current_matrix_p = 0;
13033 /* This is less strict than current_matrix_up_to_date_p.
13034 It indictes that the buffer contents and narrowing are unchanged. */
13035 int buffer_unchanged_p = 0;
13036 int temp_scroll_step = 0;
13037 int count = SPECPDL_INDEX ();
13038 int rc;
13039 int centering_position = -1;
13040 int last_line_misfit = 0;
13041 int beg_unchanged, end_unchanged;
13042
13043 SET_TEXT_POS (lpoint, PT, PT_BYTE);
13044 opoint = lpoint;
13045
13046 /* W must be a leaf window here. */
13047 xassert (!NILP (w->buffer));
13048 #if GLYPH_DEBUG
13049 *w->desired_matrix->method = 0;
13050 #endif
13051
13052 specbind (Qinhibit_point_motion_hooks, Qt);
13053
13054 reconsider_clip_changes (w, buffer);
13055
13056 /* Has the mode line to be updated? */
13057 update_mode_line = (!NILP (w->update_mode_line)
13058 || update_mode_lines
13059 || buffer->clip_changed
13060 || buffer->prevent_redisplay_optimizations_p);
13061
13062 if (MINI_WINDOW_P (w))
13063 {
13064 if (w == XWINDOW (echo_area_window)
13065 && !NILP (echo_area_buffer[0]))
13066 {
13067 if (update_mode_line)
13068 /* We may have to update a tty frame's menu bar or a
13069 tool-bar. Example `M-x C-h C-h C-g'. */
13070 goto finish_menu_bars;
13071 else
13072 /* We've already displayed the echo area glyphs in this window. */
13073 goto finish_scroll_bars;
13074 }
13075 else if ((w != XWINDOW (minibuf_window)
13076 || minibuf_level == 0)
13077 /* When buffer is nonempty, redisplay window normally. */
13078 && BUF_Z (XBUFFER (w->buffer)) == BUF_BEG (XBUFFER (w->buffer))
13079 /* Quail displays non-mini buffers in minibuffer window.
13080 In that case, redisplay the window normally. */
13081 && !NILP (Fmemq (w->buffer, Vminibuffer_list)))
13082 {
13083 /* W is a mini-buffer window, but it's not active, so clear
13084 it. */
13085 int yb = window_text_bottom_y (w);
13086 struct glyph_row *row;
13087 int y;
13088
13089 for (y = 0, row = w->desired_matrix->rows;
13090 y < yb;
13091 y += row->height, ++row)
13092 blank_row (w, row, y);
13093 goto finish_scroll_bars;
13094 }
13095
13096 clear_glyph_matrix (w->desired_matrix);
13097 }
13098
13099 /* Otherwise set up data on this window; select its buffer and point
13100 value. */
13101 /* Really select the buffer, for the sake of buffer-local
13102 variables. */
13103 set_buffer_internal_1 (XBUFFER (w->buffer));
13104 SET_TEXT_POS (opoint, PT, PT_BYTE);
13105
13106 beg_unchanged = BEG_UNCHANGED;
13107 end_unchanged = END_UNCHANGED;
13108
13109 current_matrix_up_to_date_p
13110 = (!NILP (w->window_end_valid)
13111 && !current_buffer->clip_changed
13112 && !current_buffer->prevent_redisplay_optimizations_p
13113 && XFASTINT (w->last_modified) >= MODIFF
13114 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
13115
13116 buffer_unchanged_p
13117 = (!NILP (w->window_end_valid)
13118 && !current_buffer->clip_changed
13119 && XFASTINT (w->last_modified) >= MODIFF
13120 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
13121
13122 /* When windows_or_buffers_changed is non-zero, we can't rely on
13123 the window end being valid, so set it to nil there. */
13124 if (windows_or_buffers_changed)
13125 {
13126 /* If window starts on a continuation line, maybe adjust the
13127 window start in case the window's width changed. */
13128 if (XMARKER (w->start)->buffer == current_buffer)
13129 compute_window_start_on_continuation_line (w);
13130
13131 w->window_end_valid = Qnil;
13132 }
13133
13134 /* Some sanity checks. */
13135 CHECK_WINDOW_END (w);
13136 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
13137 abort ();
13138 if (BYTEPOS (opoint) < CHARPOS (opoint))
13139 abort ();
13140
13141 /* If %c is in mode line, update it if needed. */
13142 if (!NILP (w->column_number_displayed)
13143 /* This alternative quickly identifies a common case
13144 where no change is needed. */
13145 && !(PT == XFASTINT (w->last_point)
13146 && XFASTINT (w->last_modified) >= MODIFF
13147 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
13148 && (XFASTINT (w->column_number_displayed)
13149 != (int) current_column ())) /* iftc */
13150 update_mode_line = 1;
13151
13152 /* Count number of windows showing the selected buffer. An indirect
13153 buffer counts as its base buffer. */
13154 if (!just_this_one_p)
13155 {
13156 struct buffer *current_base, *window_base;
13157 current_base = current_buffer;
13158 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
13159 if (current_base->base_buffer)
13160 current_base = current_base->base_buffer;
13161 if (window_base->base_buffer)
13162 window_base = window_base->base_buffer;
13163 if (current_base == window_base)
13164 buffer_shared++;
13165 }
13166
13167 /* Point refers normally to the selected window. For any other
13168 window, set up appropriate value. */
13169 if (!EQ (window, selected_window))
13170 {
13171 int new_pt = XMARKER (w->pointm)->charpos;
13172 int new_pt_byte = marker_byte_position (w->pointm);
13173 if (new_pt < BEGV)
13174 {
13175 new_pt = BEGV;
13176 new_pt_byte = BEGV_BYTE;
13177 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
13178 }
13179 else if (new_pt > (ZV - 1))
13180 {
13181 new_pt = ZV;
13182 new_pt_byte = ZV_BYTE;
13183 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
13184 }
13185
13186 /* We don't use SET_PT so that the point-motion hooks don't run. */
13187 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
13188 }
13189
13190 /* If any of the character widths specified in the display table
13191 have changed, invalidate the width run cache. It's true that
13192 this may be a bit late to catch such changes, but the rest of
13193 redisplay goes (non-fatally) haywire when the display table is
13194 changed, so why should we worry about doing any better? */
13195 if (current_buffer->width_run_cache)
13196 {
13197 struct Lisp_Char_Table *disptab = buffer_display_table ();
13198
13199 if (! disptab_matches_widthtab (disptab,
13200 XVECTOR (current_buffer->width_table)))
13201 {
13202 invalidate_region_cache (current_buffer,
13203 current_buffer->width_run_cache,
13204 BEG, Z);
13205 recompute_width_table (current_buffer, disptab);
13206 }
13207 }
13208
13209 /* If window-start is screwed up, choose a new one. */
13210 if (XMARKER (w->start)->buffer != current_buffer)
13211 goto recenter;
13212
13213 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13214
13215 /* If someone specified a new starting point but did not insist,
13216 check whether it can be used. */
13217 if (!NILP (w->optional_new_start)
13218 && CHARPOS (startp) >= BEGV
13219 && CHARPOS (startp) <= ZV)
13220 {
13221 w->optional_new_start = Qnil;
13222 start_display (&it, w, startp);
13223 move_it_to (&it, PT, 0, it.last_visible_y, -1,
13224 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
13225 if (IT_CHARPOS (it) == PT)
13226 w->force_start = Qt;
13227 /* IT may overshoot PT if text at PT is invisible. */
13228 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
13229 w->force_start = Qt;
13230 }
13231
13232 force_start:
13233
13234 /* Handle case where place to start displaying has been specified,
13235 unless the specified location is outside the accessible range. */
13236 if (!NILP (w->force_start)
13237 || w->frozen_window_start_p)
13238 {
13239 /* We set this later on if we have to adjust point. */
13240 int new_vpos = -1;
13241 int val;
13242
13243 w->force_start = Qnil;
13244 w->vscroll = 0;
13245 w->window_end_valid = Qnil;
13246
13247 /* Forget any recorded base line for line number display. */
13248 if (!buffer_unchanged_p)
13249 w->base_line_number = Qnil;
13250
13251 /* Redisplay the mode line. Select the buffer properly for that.
13252 Also, run the hook window-scroll-functions
13253 because we have scrolled. */
13254 /* Note, we do this after clearing force_start because
13255 if there's an error, it is better to forget about force_start
13256 than to get into an infinite loop calling the hook functions
13257 and having them get more errors. */
13258 if (!update_mode_line
13259 || ! NILP (Vwindow_scroll_functions))
13260 {
13261 update_mode_line = 1;
13262 w->update_mode_line = Qt;
13263 startp = run_window_scroll_functions (window, startp);
13264 }
13265
13266 w->last_modified = make_number (0);
13267 w->last_overlay_modified = make_number (0);
13268 if (CHARPOS (startp) < BEGV)
13269 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
13270 else if (CHARPOS (startp) > ZV)
13271 SET_TEXT_POS (startp, ZV, ZV_BYTE);
13272
13273 /* Redisplay, then check if cursor has been set during the
13274 redisplay. Give up if new fonts were loaded. */
13275 val = try_window (window, startp, 1);
13276 if (!val)
13277 {
13278 w->force_start = Qt;
13279 clear_glyph_matrix (w->desired_matrix);
13280 goto need_larger_matrices;
13281 }
13282 /* Point was outside the scroll margins. */
13283 if (val < 0)
13284 new_vpos = window_box_height (w) / 2;
13285
13286 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
13287 {
13288 /* If point does not appear, try to move point so it does
13289 appear. The desired matrix has been built above, so we
13290 can use it here. */
13291 new_vpos = window_box_height (w) / 2;
13292 }
13293
13294 if (!cursor_row_fully_visible_p (w, 0, 0))
13295 {
13296 /* Point does appear, but on a line partly visible at end of window.
13297 Move it back to a fully-visible line. */
13298 new_vpos = window_box_height (w);
13299 }
13300
13301 /* If we need to move point for either of the above reasons,
13302 now actually do it. */
13303 if (new_vpos >= 0)
13304 {
13305 struct glyph_row *row;
13306
13307 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
13308 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
13309 ++row;
13310
13311 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
13312 MATRIX_ROW_START_BYTEPOS (row));
13313
13314 if (w != XWINDOW (selected_window))
13315 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
13316 else if (current_buffer == old)
13317 SET_TEXT_POS (lpoint, PT, PT_BYTE);
13318
13319 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
13320
13321 /* If we are highlighting the region, then we just changed
13322 the region, so redisplay to show it. */
13323 if (!NILP (Vtransient_mark_mode)
13324 && !NILP (current_buffer->mark_active))
13325 {
13326 clear_glyph_matrix (w->desired_matrix);
13327 if (!try_window (window, startp, 0))
13328 goto need_larger_matrices;
13329 }
13330 }
13331
13332 #if GLYPH_DEBUG
13333 debug_method_add (w, "forced window start");
13334 #endif
13335 goto done;
13336 }
13337
13338 /* Handle case where text has not changed, only point, and it has
13339 not moved off the frame, and we are not retrying after hscroll.
13340 (current_matrix_up_to_date_p is nonzero when retrying.) */
13341 if (current_matrix_up_to_date_p
13342 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
13343 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
13344 {
13345 switch (rc)
13346 {
13347 case CURSOR_MOVEMENT_SUCCESS:
13348 used_current_matrix_p = 1;
13349 goto done;
13350
13351 #if 0 /* try_cursor_movement never returns this value. */
13352 case CURSOR_MOVEMENT_NEED_LARGER_MATRICES:
13353 goto need_larger_matrices;
13354 #endif
13355
13356 case CURSOR_MOVEMENT_MUST_SCROLL:
13357 goto try_to_scroll;
13358
13359 default:
13360 abort ();
13361 }
13362 }
13363 /* If current starting point was originally the beginning of a line
13364 but no longer is, find a new starting point. */
13365 else if (!NILP (w->start_at_line_beg)
13366 && !(CHARPOS (startp) <= BEGV
13367 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
13368 {
13369 #if GLYPH_DEBUG
13370 debug_method_add (w, "recenter 1");
13371 #endif
13372 goto recenter;
13373 }
13374
13375 /* Try scrolling with try_window_id. Value is > 0 if update has
13376 been done, it is -1 if we know that the same window start will
13377 not work. It is 0 if unsuccessful for some other reason. */
13378 else if ((tem = try_window_id (w)) != 0)
13379 {
13380 #if GLYPH_DEBUG
13381 debug_method_add (w, "try_window_id %d", tem);
13382 #endif
13383
13384 if (fonts_changed_p)
13385 goto need_larger_matrices;
13386 if (tem > 0)
13387 goto done;
13388
13389 /* Otherwise try_window_id has returned -1 which means that we
13390 don't want the alternative below this comment to execute. */
13391 }
13392 else if (CHARPOS (startp) >= BEGV
13393 && CHARPOS (startp) <= ZV
13394 && PT >= CHARPOS (startp)
13395 && (CHARPOS (startp) < ZV
13396 /* Avoid starting at end of buffer. */
13397 || CHARPOS (startp) == BEGV
13398 || (XFASTINT (w->last_modified) >= MODIFF
13399 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
13400 {
13401
13402 /* If first window line is a continuation line, and window start
13403 is inside the modified region, but the first change is before
13404 current window start, we must select a new window start.
13405
13406 However, if this is the result of a down-mouse event (e.g. by
13407 extending the mouse-drag-overlay), we don't want to select a
13408 new window start, since that would change the position under
13409 the mouse, resulting in an unwanted mouse-movement rather
13410 than a simple mouse-click. */
13411 if (NILP (w->start_at_line_beg)
13412 && NILP (do_mouse_tracking)
13413 && CHARPOS (startp) > BEGV
13414 && CHARPOS (startp) > BEG + beg_unchanged
13415 && CHARPOS (startp) <= Z - end_unchanged)
13416 {
13417 w->force_start = Qt;
13418 if (XMARKER (w->start)->buffer == current_buffer)
13419 compute_window_start_on_continuation_line (w);
13420 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13421 goto force_start;
13422 }
13423
13424 #if GLYPH_DEBUG
13425 debug_method_add (w, "same window start");
13426 #endif
13427
13428 /* Try to redisplay starting at same place as before.
13429 If point has not moved off frame, accept the results. */
13430 if (!current_matrix_up_to_date_p
13431 /* Don't use try_window_reusing_current_matrix in this case
13432 because a window scroll function can have changed the
13433 buffer. */
13434 || !NILP (Vwindow_scroll_functions)
13435 || MINI_WINDOW_P (w)
13436 || !(used_current_matrix_p
13437 = try_window_reusing_current_matrix (w)))
13438 {
13439 IF_DEBUG (debug_method_add (w, "1"));
13440 if (try_window (window, startp, 1) < 0)
13441 /* -1 means we need to scroll.
13442 0 means we need new matrices, but fonts_changed_p
13443 is set in that case, so we will detect it below. */
13444 goto try_to_scroll;
13445 }
13446
13447 if (fonts_changed_p)
13448 goto need_larger_matrices;
13449
13450 if (w->cursor.vpos >= 0)
13451 {
13452 if (!just_this_one_p
13453 || current_buffer->clip_changed
13454 || BEG_UNCHANGED < CHARPOS (startp))
13455 /* Forget any recorded base line for line number display. */
13456 w->base_line_number = Qnil;
13457
13458 if (!cursor_row_fully_visible_p (w, 1, 0))
13459 {
13460 clear_glyph_matrix (w->desired_matrix);
13461 last_line_misfit = 1;
13462 }
13463 /* Drop through and scroll. */
13464 else
13465 goto done;
13466 }
13467 else
13468 clear_glyph_matrix (w->desired_matrix);
13469 }
13470
13471 try_to_scroll:
13472
13473 w->last_modified = make_number (0);
13474 w->last_overlay_modified = make_number (0);
13475
13476 /* Redisplay the mode line. Select the buffer properly for that. */
13477 if (!update_mode_line)
13478 {
13479 update_mode_line = 1;
13480 w->update_mode_line = Qt;
13481 }
13482
13483 /* Try to scroll by specified few lines. */
13484 if ((scroll_conservatively
13485 || scroll_step
13486 || temp_scroll_step
13487 || NUMBERP (current_buffer->scroll_up_aggressively)
13488 || NUMBERP (current_buffer->scroll_down_aggressively))
13489 && !current_buffer->clip_changed
13490 && CHARPOS (startp) >= BEGV
13491 && CHARPOS (startp) <= ZV)
13492 {
13493 /* The function returns -1 if new fonts were loaded, 1 if
13494 successful, 0 if not successful. */
13495 int rc = try_scrolling (window, just_this_one_p,
13496 scroll_conservatively,
13497 scroll_step,
13498 temp_scroll_step, last_line_misfit);
13499 switch (rc)
13500 {
13501 case SCROLLING_SUCCESS:
13502 goto done;
13503
13504 case SCROLLING_NEED_LARGER_MATRICES:
13505 goto need_larger_matrices;
13506
13507 case SCROLLING_FAILED:
13508 break;
13509
13510 default:
13511 abort ();
13512 }
13513 }
13514
13515 /* Finally, just choose place to start which centers point */
13516
13517 recenter:
13518 if (centering_position < 0)
13519 centering_position = window_box_height (w) / 2;
13520
13521 #if GLYPH_DEBUG
13522 debug_method_add (w, "recenter");
13523 #endif
13524
13525 /* w->vscroll = 0; */
13526
13527 /* Forget any previously recorded base line for line number display. */
13528 if (!buffer_unchanged_p)
13529 w->base_line_number = Qnil;
13530
13531 /* Move backward half the height of the window. */
13532 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
13533 it.current_y = it.last_visible_y;
13534 move_it_vertically_backward (&it, centering_position);
13535 xassert (IT_CHARPOS (it) >= BEGV);
13536
13537 /* The function move_it_vertically_backward may move over more
13538 than the specified y-distance. If it->w is small, e.g. a
13539 mini-buffer window, we may end up in front of the window's
13540 display area. Start displaying at the start of the line
13541 containing PT in this case. */
13542 if (it.current_y <= 0)
13543 {
13544 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
13545 move_it_vertically_backward (&it, 0);
13546 #if 0
13547 /* I think this assert is bogus if buffer contains
13548 invisible text or images. KFS. */
13549 xassert (IT_CHARPOS (it) <= PT);
13550 #endif
13551 it.current_y = 0;
13552 }
13553
13554 it.current_x = it.hpos = 0;
13555
13556 /* Set startp here explicitly in case that helps avoid an infinite loop
13557 in case the window-scroll-functions functions get errors. */
13558 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
13559
13560 /* Run scroll hooks. */
13561 startp = run_window_scroll_functions (window, it.current.pos);
13562
13563 /* Redisplay the window. */
13564 if (!current_matrix_up_to_date_p
13565 || windows_or_buffers_changed
13566 || cursor_type_changed
13567 /* Don't use try_window_reusing_current_matrix in this case
13568 because it can have changed the buffer. */
13569 || !NILP (Vwindow_scroll_functions)
13570 || !just_this_one_p
13571 || MINI_WINDOW_P (w)
13572 || !(used_current_matrix_p
13573 = try_window_reusing_current_matrix (w)))
13574 try_window (window, startp, 0);
13575
13576 /* If new fonts have been loaded (due to fontsets), give up. We
13577 have to start a new redisplay since we need to re-adjust glyph
13578 matrices. */
13579 if (fonts_changed_p)
13580 goto need_larger_matrices;
13581
13582 /* If cursor did not appear assume that the middle of the window is
13583 in the first line of the window. Do it again with the next line.
13584 (Imagine a window of height 100, displaying two lines of height
13585 60. Moving back 50 from it->last_visible_y will end in the first
13586 line.) */
13587 if (w->cursor.vpos < 0)
13588 {
13589 if (!NILP (w->window_end_valid)
13590 && PT >= Z - XFASTINT (w->window_end_pos))
13591 {
13592 clear_glyph_matrix (w->desired_matrix);
13593 move_it_by_lines (&it, 1, 0);
13594 try_window (window, it.current.pos, 0);
13595 }
13596 else if (PT < IT_CHARPOS (it))
13597 {
13598 clear_glyph_matrix (w->desired_matrix);
13599 move_it_by_lines (&it, -1, 0);
13600 try_window (window, it.current.pos, 0);
13601 }
13602 else
13603 {
13604 /* Not much we can do about it. */
13605 }
13606 }
13607
13608 /* Consider the following case: Window starts at BEGV, there is
13609 invisible, intangible text at BEGV, so that display starts at
13610 some point START > BEGV. It can happen that we are called with
13611 PT somewhere between BEGV and START. Try to handle that case. */
13612 if (w->cursor.vpos < 0)
13613 {
13614 struct glyph_row *row = w->current_matrix->rows;
13615 if (row->mode_line_p)
13616 ++row;
13617 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13618 }
13619
13620 if (!cursor_row_fully_visible_p (w, 0, 0))
13621 {
13622 /* If vscroll is enabled, disable it and try again. */
13623 if (w->vscroll)
13624 {
13625 w->vscroll = 0;
13626 clear_glyph_matrix (w->desired_matrix);
13627 goto recenter;
13628 }
13629
13630 /* If centering point failed to make the whole line visible,
13631 put point at the top instead. That has to make the whole line
13632 visible, if it can be done. */
13633 if (centering_position == 0)
13634 goto done;
13635
13636 clear_glyph_matrix (w->desired_matrix);
13637 centering_position = 0;
13638 goto recenter;
13639 }
13640
13641 done:
13642
13643 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13644 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
13645 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
13646 ? Qt : Qnil);
13647
13648 /* Display the mode line, if we must. */
13649 if ((update_mode_line
13650 /* If window not full width, must redo its mode line
13651 if (a) the window to its side is being redone and
13652 (b) we do a frame-based redisplay. This is a consequence
13653 of how inverted lines are drawn in frame-based redisplay. */
13654 || (!just_this_one_p
13655 && !FRAME_WINDOW_P (f)
13656 && !WINDOW_FULL_WIDTH_P (w))
13657 /* Line number to display. */
13658 || INTEGERP (w->base_line_pos)
13659 /* Column number is displayed and different from the one displayed. */
13660 || (!NILP (w->column_number_displayed)
13661 && (XFASTINT (w->column_number_displayed)
13662 != (int) current_column ()))) /* iftc */
13663 /* This means that the window has a mode line. */
13664 && (WINDOW_WANTS_MODELINE_P (w)
13665 || WINDOW_WANTS_HEADER_LINE_P (w)))
13666 {
13667 display_mode_lines (w);
13668
13669 /* If mode line height has changed, arrange for a thorough
13670 immediate redisplay using the correct mode line height. */
13671 if (WINDOW_WANTS_MODELINE_P (w)
13672 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
13673 {
13674 fonts_changed_p = 1;
13675 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
13676 = DESIRED_MODE_LINE_HEIGHT (w);
13677 }
13678
13679 /* If top line height has changed, arrange for a thorough
13680 immediate redisplay using the correct mode line height. */
13681 if (WINDOW_WANTS_HEADER_LINE_P (w)
13682 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
13683 {
13684 fonts_changed_p = 1;
13685 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
13686 = DESIRED_HEADER_LINE_HEIGHT (w);
13687 }
13688
13689 if (fonts_changed_p)
13690 goto need_larger_matrices;
13691 }
13692
13693 if (!line_number_displayed
13694 && !BUFFERP (w->base_line_pos))
13695 {
13696 w->base_line_pos = Qnil;
13697 w->base_line_number = Qnil;
13698 }
13699
13700 finish_menu_bars:
13701
13702 /* When we reach a frame's selected window, redo the frame's menu bar. */
13703 if (update_mode_line
13704 && EQ (FRAME_SELECTED_WINDOW (f), window))
13705 {
13706 int redisplay_menu_p = 0;
13707 int redisplay_tool_bar_p = 0;
13708
13709 if (FRAME_WINDOW_P (f))
13710 {
13711 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
13712 || defined (USE_GTK)
13713 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
13714 #else
13715 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
13716 #endif
13717 }
13718 else
13719 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
13720
13721 if (redisplay_menu_p)
13722 display_menu_bar (w);
13723
13724 #ifdef HAVE_WINDOW_SYSTEM
13725 if (FRAME_WINDOW_P (f))
13726 {
13727 #if defined (USE_GTK) || USE_MAC_TOOLBAR
13728 redisplay_tool_bar_p = FRAME_EXTERNAL_TOOL_BAR (f);
13729 #else
13730 redisplay_tool_bar_p = WINDOWP (f->tool_bar_window)
13731 && (FRAME_TOOL_BAR_LINES (f) > 0
13732 || !NILP (Vauto_resize_tool_bars));
13733 #endif
13734
13735 if (redisplay_tool_bar_p && redisplay_tool_bar (f))
13736 {
13737 extern int ignore_mouse_drag_p;
13738 ignore_mouse_drag_p = 1;
13739 }
13740 }
13741 #endif
13742 }
13743
13744 #ifdef HAVE_WINDOW_SYSTEM
13745 if (FRAME_WINDOW_P (f)
13746 && update_window_fringes (w, (just_this_one_p
13747 || (!used_current_matrix_p && !overlay_arrow_seen)
13748 || w->pseudo_window_p)))
13749 {
13750 update_begin (f);
13751 BLOCK_INPUT;
13752 if (draw_window_fringes (w, 1))
13753 x_draw_vertical_border (w);
13754 UNBLOCK_INPUT;
13755 update_end (f);
13756 }
13757 #endif /* HAVE_WINDOW_SYSTEM */
13758
13759 /* We go to this label, with fonts_changed_p nonzero,
13760 if it is necessary to try again using larger glyph matrices.
13761 We have to redeem the scroll bar even in this case,
13762 because the loop in redisplay_internal expects that. */
13763 need_larger_matrices:
13764 ;
13765 finish_scroll_bars:
13766
13767 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
13768 {
13769 /* Set the thumb's position and size. */
13770 set_vertical_scroll_bar (w);
13771
13772 /* Note that we actually used the scroll bar attached to this
13773 window, so it shouldn't be deleted at the end of redisplay. */
13774 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
13775 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
13776 }
13777
13778 /* Restore current_buffer and value of point in it. */
13779 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
13780 set_buffer_internal_1 (old);
13781 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
13782 shorter. This can be caused by log truncation in *Messages*. */
13783 if (CHARPOS (lpoint) <= ZV)
13784 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
13785
13786 unbind_to (count, Qnil);
13787 }
13788
13789
13790 /* Build the complete desired matrix of WINDOW with a window start
13791 buffer position POS.
13792
13793 Value is 1 if successful. It is zero if fonts were loaded during
13794 redisplay which makes re-adjusting glyph matrices necessary, and -1
13795 if point would appear in the scroll margins.
13796 (We check that only if CHECK_MARGINS is nonzero. */
13797
13798 int
13799 try_window (window, pos, check_margins)
13800 Lisp_Object window;
13801 struct text_pos pos;
13802 int check_margins;
13803 {
13804 struct window *w = XWINDOW (window);
13805 struct it it;
13806 struct glyph_row *last_text_row = NULL;
13807 struct frame *f = XFRAME (w->frame);
13808
13809 /* Make POS the new window start. */
13810 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
13811
13812 /* Mark cursor position as unknown. No overlay arrow seen. */
13813 w->cursor.vpos = -1;
13814 overlay_arrow_seen = 0;
13815
13816 /* Initialize iterator and info to start at POS. */
13817 start_display (&it, w, pos);
13818
13819 /* Display all lines of W. */
13820 while (it.current_y < it.last_visible_y)
13821 {
13822 if (display_line (&it))
13823 last_text_row = it.glyph_row - 1;
13824 if (fonts_changed_p)
13825 return 0;
13826 }
13827
13828 /* Don't let the cursor end in the scroll margins. */
13829 if (check_margins
13830 && !MINI_WINDOW_P (w))
13831 {
13832 int this_scroll_margin;
13833
13834 this_scroll_margin = max (0, scroll_margin);
13835 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
13836 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
13837
13838 if ((w->cursor.y >= 0 /* not vscrolled */
13839 && w->cursor.y < this_scroll_margin
13840 && CHARPOS (pos) > BEGV
13841 && IT_CHARPOS (it) < ZV)
13842 /* rms: considering make_cursor_line_fully_visible_p here
13843 seems to give wrong results. We don't want to recenter
13844 when the last line is partly visible, we want to allow
13845 that case to be handled in the usual way. */
13846 || (w->cursor.y + 1) > it.last_visible_y)
13847 {
13848 w->cursor.vpos = -1;
13849 clear_glyph_matrix (w->desired_matrix);
13850 return -1;
13851 }
13852 }
13853
13854 /* If bottom moved off end of frame, change mode line percentage. */
13855 if (XFASTINT (w->window_end_pos) <= 0
13856 && Z != IT_CHARPOS (it))
13857 w->update_mode_line = Qt;
13858
13859 /* Set window_end_pos to the offset of the last character displayed
13860 on the window from the end of current_buffer. Set
13861 window_end_vpos to its row number. */
13862 if (last_text_row)
13863 {
13864 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
13865 w->window_end_bytepos
13866 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
13867 w->window_end_pos
13868 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
13869 w->window_end_vpos
13870 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
13871 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
13872 ->displays_text_p);
13873 }
13874 else
13875 {
13876 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
13877 w->window_end_pos = make_number (Z - ZV);
13878 w->window_end_vpos = make_number (0);
13879 }
13880
13881 /* But that is not valid info until redisplay finishes. */
13882 w->window_end_valid = Qnil;
13883 return 1;
13884 }
13885
13886
13887 \f
13888 /************************************************************************
13889 Window redisplay reusing current matrix when buffer has not changed
13890 ************************************************************************/
13891
13892 /* Try redisplay of window W showing an unchanged buffer with a
13893 different window start than the last time it was displayed by
13894 reusing its current matrix. Value is non-zero if successful.
13895 W->start is the new window start. */
13896
13897 static int
13898 try_window_reusing_current_matrix (w)
13899 struct window *w;
13900 {
13901 struct frame *f = XFRAME (w->frame);
13902 struct glyph_row *row, *bottom_row;
13903 struct it it;
13904 struct run run;
13905 struct text_pos start, new_start;
13906 int nrows_scrolled, i;
13907 struct glyph_row *last_text_row;
13908 struct glyph_row *last_reused_text_row;
13909 struct glyph_row *start_row;
13910 int start_vpos, min_y, max_y;
13911
13912 #if GLYPH_DEBUG
13913 if (inhibit_try_window_reusing)
13914 return 0;
13915 #endif
13916
13917 if (/* This function doesn't handle terminal frames. */
13918 !FRAME_WINDOW_P (f)
13919 /* Don't try to reuse the display if windows have been split
13920 or such. */
13921 || windows_or_buffers_changed
13922 || cursor_type_changed)
13923 return 0;
13924
13925 /* Can't do this if region may have changed. */
13926 if ((!NILP (Vtransient_mark_mode)
13927 && !NILP (current_buffer->mark_active))
13928 || !NILP (w->region_showing)
13929 || !NILP (Vshow_trailing_whitespace))
13930 return 0;
13931
13932 /* If top-line visibility has changed, give up. */
13933 if (WINDOW_WANTS_HEADER_LINE_P (w)
13934 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
13935 return 0;
13936
13937 /* Give up if old or new display is scrolled vertically. We could
13938 make this function handle this, but right now it doesn't. */
13939 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
13940 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
13941 return 0;
13942
13943 /* The variable new_start now holds the new window start. The old
13944 start `start' can be determined from the current matrix. */
13945 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
13946 start = start_row->start.pos;
13947 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
13948
13949 /* Clear the desired matrix for the display below. */
13950 clear_glyph_matrix (w->desired_matrix);
13951
13952 if (CHARPOS (new_start) <= CHARPOS (start))
13953 {
13954 int first_row_y;
13955
13956 /* Don't use this method if the display starts with an ellipsis
13957 displayed for invisible text. It's not easy to handle that case
13958 below, and it's certainly not worth the effort since this is
13959 not a frequent case. */
13960 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
13961 return 0;
13962
13963 IF_DEBUG (debug_method_add (w, "twu1"));
13964
13965 /* Display up to a row that can be reused. The variable
13966 last_text_row is set to the last row displayed that displays
13967 text. Note that it.vpos == 0 if or if not there is a
13968 header-line; it's not the same as the MATRIX_ROW_VPOS! */
13969 start_display (&it, w, new_start);
13970 first_row_y = it.current_y;
13971 w->cursor.vpos = -1;
13972 last_text_row = last_reused_text_row = NULL;
13973
13974 while (it.current_y < it.last_visible_y
13975 && !fonts_changed_p)
13976 {
13977 /* If we have reached into the characters in the START row,
13978 that means the line boundaries have changed. So we
13979 can't start copying with the row START. Maybe it will
13980 work to start copying with the following row. */
13981 while (IT_CHARPOS (it) > CHARPOS (start))
13982 {
13983 /* Advance to the next row as the "start". */
13984 start_row++;
13985 start = start_row->start.pos;
13986 /* If there are no more rows to try, or just one, give up. */
13987 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
13988 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
13989 || CHARPOS (start) == ZV)
13990 {
13991 clear_glyph_matrix (w->desired_matrix);
13992 return 0;
13993 }
13994
13995 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
13996 }
13997 /* If we have reached alignment,
13998 we can copy the rest of the rows. */
13999 if (IT_CHARPOS (it) == CHARPOS (start))
14000 break;
14001
14002 if (display_line (&it))
14003 last_text_row = it.glyph_row - 1;
14004 }
14005
14006 /* A value of current_y < last_visible_y means that we stopped
14007 at the previous window start, which in turn means that we
14008 have at least one reusable row. */
14009 if (it.current_y < it.last_visible_y)
14010 {
14011 /* IT.vpos always starts from 0; it counts text lines. */
14012 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
14013
14014 /* Find PT if not already found in the lines displayed. */
14015 if (w->cursor.vpos < 0)
14016 {
14017 int dy = it.current_y - start_row->y;
14018
14019 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14020 row = row_containing_pos (w, PT, row, NULL, dy);
14021 if (row)
14022 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
14023 dy, nrows_scrolled);
14024 else
14025 {
14026 clear_glyph_matrix (w->desired_matrix);
14027 return 0;
14028 }
14029 }
14030
14031 /* Scroll the display. Do it before the current matrix is
14032 changed. The problem here is that update has not yet
14033 run, i.e. part of the current matrix is not up to date.
14034 scroll_run_hook will clear the cursor, and use the
14035 current matrix to get the height of the row the cursor is
14036 in. */
14037 run.current_y = start_row->y;
14038 run.desired_y = it.current_y;
14039 run.height = it.last_visible_y - it.current_y;
14040
14041 if (run.height > 0 && run.current_y != run.desired_y)
14042 {
14043 update_begin (f);
14044 FRAME_RIF (f)->update_window_begin_hook (w);
14045 FRAME_RIF (f)->clear_window_mouse_face (w);
14046 FRAME_RIF (f)->scroll_run_hook (w, &run);
14047 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
14048 update_end (f);
14049 }
14050
14051 /* Shift current matrix down by nrows_scrolled lines. */
14052 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
14053 rotate_matrix (w->current_matrix,
14054 start_vpos,
14055 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
14056 nrows_scrolled);
14057
14058 /* Disable lines that must be updated. */
14059 for (i = 0; i < nrows_scrolled; ++i)
14060 (start_row + i)->enabled_p = 0;
14061
14062 /* Re-compute Y positions. */
14063 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
14064 max_y = it.last_visible_y;
14065 for (row = start_row + nrows_scrolled;
14066 row < bottom_row;
14067 ++row)
14068 {
14069 row->y = it.current_y;
14070 row->visible_height = row->height;
14071
14072 if (row->y < min_y)
14073 row->visible_height -= min_y - row->y;
14074 if (row->y + row->height > max_y)
14075 row->visible_height -= row->y + row->height - max_y;
14076 row->redraw_fringe_bitmaps_p = 1;
14077
14078 it.current_y += row->height;
14079
14080 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14081 last_reused_text_row = row;
14082 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
14083 break;
14084 }
14085
14086 /* Disable lines in the current matrix which are now
14087 below the window. */
14088 for (++row; row < bottom_row; ++row)
14089 row->enabled_p = row->mode_line_p = 0;
14090 }
14091
14092 /* Update window_end_pos etc.; last_reused_text_row is the last
14093 reused row from the current matrix containing text, if any.
14094 The value of last_text_row is the last displayed line
14095 containing text. */
14096 if (last_reused_text_row)
14097 {
14098 w->window_end_bytepos
14099 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
14100 w->window_end_pos
14101 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
14102 w->window_end_vpos
14103 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
14104 w->current_matrix));
14105 }
14106 else if (last_text_row)
14107 {
14108 w->window_end_bytepos
14109 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
14110 w->window_end_pos
14111 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
14112 w->window_end_vpos
14113 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
14114 }
14115 else
14116 {
14117 /* This window must be completely empty. */
14118 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
14119 w->window_end_pos = make_number (Z - ZV);
14120 w->window_end_vpos = make_number (0);
14121 }
14122 w->window_end_valid = Qnil;
14123
14124 /* Update hint: don't try scrolling again in update_window. */
14125 w->desired_matrix->no_scrolling_p = 1;
14126
14127 #if GLYPH_DEBUG
14128 debug_method_add (w, "try_window_reusing_current_matrix 1");
14129 #endif
14130 return 1;
14131 }
14132 else if (CHARPOS (new_start) > CHARPOS (start))
14133 {
14134 struct glyph_row *pt_row, *row;
14135 struct glyph_row *first_reusable_row;
14136 struct glyph_row *first_row_to_display;
14137 int dy;
14138 int yb = window_text_bottom_y (w);
14139
14140 /* Find the row starting at new_start, if there is one. Don't
14141 reuse a partially visible line at the end. */
14142 first_reusable_row = start_row;
14143 while (first_reusable_row->enabled_p
14144 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
14145 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
14146 < CHARPOS (new_start)))
14147 ++first_reusable_row;
14148
14149 /* Give up if there is no row to reuse. */
14150 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
14151 || !first_reusable_row->enabled_p
14152 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
14153 != CHARPOS (new_start)))
14154 return 0;
14155
14156 /* We can reuse fully visible rows beginning with
14157 first_reusable_row to the end of the window. Set
14158 first_row_to_display to the first row that cannot be reused.
14159 Set pt_row to the row containing point, if there is any. */
14160 pt_row = NULL;
14161 for (first_row_to_display = first_reusable_row;
14162 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
14163 ++first_row_to_display)
14164 {
14165 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
14166 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
14167 pt_row = first_row_to_display;
14168 }
14169
14170 /* Start displaying at the start of first_row_to_display. */
14171 xassert (first_row_to_display->y < yb);
14172 init_to_row_start (&it, w, first_row_to_display);
14173
14174 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
14175 - start_vpos);
14176 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
14177 - nrows_scrolled);
14178 it.current_y = (first_row_to_display->y - first_reusable_row->y
14179 + WINDOW_HEADER_LINE_HEIGHT (w));
14180
14181 /* Display lines beginning with first_row_to_display in the
14182 desired matrix. Set last_text_row to the last row displayed
14183 that displays text. */
14184 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
14185 if (pt_row == NULL)
14186 w->cursor.vpos = -1;
14187 last_text_row = NULL;
14188 while (it.current_y < it.last_visible_y && !fonts_changed_p)
14189 if (display_line (&it))
14190 last_text_row = it.glyph_row - 1;
14191
14192 /* Give up If point isn't in a row displayed or reused. */
14193 if (w->cursor.vpos < 0)
14194 {
14195 clear_glyph_matrix (w->desired_matrix);
14196 return 0;
14197 }
14198
14199 /* If point is in a reused row, adjust y and vpos of the cursor
14200 position. */
14201 if (pt_row)
14202 {
14203 w->cursor.vpos -= nrows_scrolled;
14204 w->cursor.y -= first_reusable_row->y - start_row->y;
14205 }
14206
14207 /* Scroll the display. */
14208 run.current_y = first_reusable_row->y;
14209 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
14210 run.height = it.last_visible_y - run.current_y;
14211 dy = run.current_y - run.desired_y;
14212
14213 if (run.height)
14214 {
14215 update_begin (f);
14216 FRAME_RIF (f)->update_window_begin_hook (w);
14217 FRAME_RIF (f)->clear_window_mouse_face (w);
14218 FRAME_RIF (f)->scroll_run_hook (w, &run);
14219 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
14220 update_end (f);
14221 }
14222
14223 /* Adjust Y positions of reused rows. */
14224 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
14225 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
14226 max_y = it.last_visible_y;
14227 for (row = first_reusable_row; row < first_row_to_display; ++row)
14228 {
14229 row->y -= dy;
14230 row->visible_height = row->height;
14231 if (row->y < min_y)
14232 row->visible_height -= min_y - row->y;
14233 if (row->y + row->height > max_y)
14234 row->visible_height -= row->y + row->height - max_y;
14235 row->redraw_fringe_bitmaps_p = 1;
14236 }
14237
14238 /* Scroll the current matrix. */
14239 xassert (nrows_scrolled > 0);
14240 rotate_matrix (w->current_matrix,
14241 start_vpos,
14242 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
14243 -nrows_scrolled);
14244
14245 /* Disable rows not reused. */
14246 for (row -= nrows_scrolled; row < bottom_row; ++row)
14247 row->enabled_p = 0;
14248
14249 /* Point may have moved to a different line, so we cannot assume that
14250 the previous cursor position is valid; locate the correct row. */
14251 if (pt_row)
14252 {
14253 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
14254 row < bottom_row && PT >= MATRIX_ROW_END_CHARPOS (row);
14255 row++)
14256 {
14257 w->cursor.vpos++;
14258 w->cursor.y = row->y;
14259 }
14260 if (row < bottom_row)
14261 {
14262 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
14263 while (glyph->charpos < PT)
14264 {
14265 w->cursor.hpos++;
14266 w->cursor.x += glyph->pixel_width;
14267 glyph++;
14268 }
14269 }
14270 }
14271
14272 /* Adjust window end. A null value of last_text_row means that
14273 the window end is in reused rows which in turn means that
14274 only its vpos can have changed. */
14275 if (last_text_row)
14276 {
14277 w->window_end_bytepos
14278 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
14279 w->window_end_pos
14280 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
14281 w->window_end_vpos
14282 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
14283 }
14284 else
14285 {
14286 w->window_end_vpos
14287 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
14288 }
14289
14290 w->window_end_valid = Qnil;
14291 w->desired_matrix->no_scrolling_p = 1;
14292
14293 #if GLYPH_DEBUG
14294 debug_method_add (w, "try_window_reusing_current_matrix 2");
14295 #endif
14296 return 1;
14297 }
14298
14299 return 0;
14300 }
14301
14302
14303 \f
14304 /************************************************************************
14305 Window redisplay reusing current matrix when buffer has changed
14306 ************************************************************************/
14307
14308 static struct glyph_row *find_last_unchanged_at_beg_row P_ ((struct window *));
14309 static struct glyph_row *find_first_unchanged_at_end_row P_ ((struct window *,
14310 int *, int *));
14311 static struct glyph_row *
14312 find_last_row_displaying_text P_ ((struct glyph_matrix *, struct it *,
14313 struct glyph_row *));
14314
14315
14316 /* Return the last row in MATRIX displaying text. If row START is
14317 non-null, start searching with that row. IT gives the dimensions
14318 of the display. Value is null if matrix is empty; otherwise it is
14319 a pointer to the row found. */
14320
14321 static struct glyph_row *
14322 find_last_row_displaying_text (matrix, it, start)
14323 struct glyph_matrix *matrix;
14324 struct it *it;
14325 struct glyph_row *start;
14326 {
14327 struct glyph_row *row, *row_found;
14328
14329 /* Set row_found to the last row in IT->w's current matrix
14330 displaying text. The loop looks funny but think of partially
14331 visible lines. */
14332 row_found = NULL;
14333 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
14334 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14335 {
14336 xassert (row->enabled_p);
14337 row_found = row;
14338 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
14339 break;
14340 ++row;
14341 }
14342
14343 return row_found;
14344 }
14345
14346
14347 /* Return the last row in the current matrix of W that is not affected
14348 by changes at the start of current_buffer that occurred since W's
14349 current matrix was built. Value is null if no such row exists.
14350
14351 BEG_UNCHANGED us the number of characters unchanged at the start of
14352 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
14353 first changed character in current_buffer. Characters at positions <
14354 BEG + BEG_UNCHANGED are at the same buffer positions as they were
14355 when the current matrix was built. */
14356
14357 static struct glyph_row *
14358 find_last_unchanged_at_beg_row (w)
14359 struct window *w;
14360 {
14361 int first_changed_pos = BEG + BEG_UNCHANGED;
14362 struct glyph_row *row;
14363 struct glyph_row *row_found = NULL;
14364 int yb = window_text_bottom_y (w);
14365
14366 /* Find the last row displaying unchanged text. */
14367 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14368 while (MATRIX_ROW_DISPLAYS_TEXT_P (row)
14369 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos)
14370 {
14371 if (/* If row ends before first_changed_pos, it is unchanged,
14372 except in some case. */
14373 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
14374 /* When row ends in ZV and we write at ZV it is not
14375 unchanged. */
14376 && !row->ends_at_zv_p
14377 /* When first_changed_pos is the end of a continued line,
14378 row is not unchanged because it may be no longer
14379 continued. */
14380 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
14381 && (row->continued_p
14382 || row->exact_window_width_line_p)))
14383 row_found = row;
14384
14385 /* Stop if last visible row. */
14386 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
14387 break;
14388
14389 ++row;
14390 }
14391
14392 return row_found;
14393 }
14394
14395
14396 /* Find the first glyph row in the current matrix of W that is not
14397 affected by changes at the end of current_buffer since the
14398 time W's current matrix was built.
14399
14400 Return in *DELTA the number of chars by which buffer positions in
14401 unchanged text at the end of current_buffer must be adjusted.
14402
14403 Return in *DELTA_BYTES the corresponding number of bytes.
14404
14405 Value is null if no such row exists, i.e. all rows are affected by
14406 changes. */
14407
14408 static struct glyph_row *
14409 find_first_unchanged_at_end_row (w, delta, delta_bytes)
14410 struct window *w;
14411 int *delta, *delta_bytes;
14412 {
14413 struct glyph_row *row;
14414 struct glyph_row *row_found = NULL;
14415
14416 *delta = *delta_bytes = 0;
14417
14418 /* Display must not have been paused, otherwise the current matrix
14419 is not up to date. */
14420 if (NILP (w->window_end_valid))
14421 abort ();
14422
14423 /* A value of window_end_pos >= END_UNCHANGED means that the window
14424 end is in the range of changed text. If so, there is no
14425 unchanged row at the end of W's current matrix. */
14426 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
14427 return NULL;
14428
14429 /* Set row to the last row in W's current matrix displaying text. */
14430 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
14431
14432 /* If matrix is entirely empty, no unchanged row exists. */
14433 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14434 {
14435 /* The value of row is the last glyph row in the matrix having a
14436 meaningful buffer position in it. The end position of row
14437 corresponds to window_end_pos. This allows us to translate
14438 buffer positions in the current matrix to current buffer
14439 positions for characters not in changed text. */
14440 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
14441 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
14442 int last_unchanged_pos, last_unchanged_pos_old;
14443 struct glyph_row *first_text_row
14444 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14445
14446 *delta = Z - Z_old;
14447 *delta_bytes = Z_BYTE - Z_BYTE_old;
14448
14449 /* Set last_unchanged_pos to the buffer position of the last
14450 character in the buffer that has not been changed. Z is the
14451 index + 1 of the last character in current_buffer, i.e. by
14452 subtracting END_UNCHANGED we get the index of the last
14453 unchanged character, and we have to add BEG to get its buffer
14454 position. */
14455 last_unchanged_pos = Z - END_UNCHANGED + BEG;
14456 last_unchanged_pos_old = last_unchanged_pos - *delta;
14457
14458 /* Search backward from ROW for a row displaying a line that
14459 starts at a minimum position >= last_unchanged_pos_old. */
14460 for (; row > first_text_row; --row)
14461 {
14462 /* This used to abort, but it can happen.
14463 It is ok to just stop the search instead here. KFS. */
14464 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
14465 break;
14466
14467 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
14468 row_found = row;
14469 }
14470 }
14471
14472 if (row_found && !MATRIX_ROW_DISPLAYS_TEXT_P (row_found))
14473 abort ();
14474
14475 return row_found;
14476 }
14477
14478
14479 /* Make sure that glyph rows in the current matrix of window W
14480 reference the same glyph memory as corresponding rows in the
14481 frame's frame matrix. This function is called after scrolling W's
14482 current matrix on a terminal frame in try_window_id and
14483 try_window_reusing_current_matrix. */
14484
14485 static void
14486 sync_frame_with_window_matrix_rows (w)
14487 struct window *w;
14488 {
14489 struct frame *f = XFRAME (w->frame);
14490 struct glyph_row *window_row, *window_row_end, *frame_row;
14491
14492 /* Preconditions: W must be a leaf window and full-width. Its frame
14493 must have a frame matrix. */
14494 xassert (NILP (w->hchild) && NILP (w->vchild));
14495 xassert (WINDOW_FULL_WIDTH_P (w));
14496 xassert (!FRAME_WINDOW_P (f));
14497
14498 /* If W is a full-width window, glyph pointers in W's current matrix
14499 have, by definition, to be the same as glyph pointers in the
14500 corresponding frame matrix. Note that frame matrices have no
14501 marginal areas (see build_frame_matrix). */
14502 window_row = w->current_matrix->rows;
14503 window_row_end = window_row + w->current_matrix->nrows;
14504 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
14505 while (window_row < window_row_end)
14506 {
14507 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
14508 struct glyph *end = window_row->glyphs[LAST_AREA];
14509
14510 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
14511 frame_row->glyphs[TEXT_AREA] = start;
14512 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
14513 frame_row->glyphs[LAST_AREA] = end;
14514
14515 /* Disable frame rows whose corresponding window rows have
14516 been disabled in try_window_id. */
14517 if (!window_row->enabled_p)
14518 frame_row->enabled_p = 0;
14519
14520 ++window_row, ++frame_row;
14521 }
14522 }
14523
14524
14525 /* Find the glyph row in window W containing CHARPOS. Consider all
14526 rows between START and END (not inclusive). END null means search
14527 all rows to the end of the display area of W. Value is the row
14528 containing CHARPOS or null. */
14529
14530 struct glyph_row *
14531 row_containing_pos (w, charpos, start, end, dy)
14532 struct window *w;
14533 int charpos;
14534 struct glyph_row *start, *end;
14535 int dy;
14536 {
14537 struct glyph_row *row = start;
14538 int last_y;
14539
14540 /* If we happen to start on a header-line, skip that. */
14541 if (row->mode_line_p)
14542 ++row;
14543
14544 if ((end && row >= end) || !row->enabled_p)
14545 return NULL;
14546
14547 last_y = window_text_bottom_y (w) - dy;
14548
14549 while (1)
14550 {
14551 /* Give up if we have gone too far. */
14552 if (end && row >= end)
14553 return NULL;
14554 /* This formerly returned if they were equal.
14555 I think that both quantities are of a "last plus one" type;
14556 if so, when they are equal, the row is within the screen. -- rms. */
14557 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
14558 return NULL;
14559
14560 /* If it is in this row, return this row. */
14561 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
14562 || (MATRIX_ROW_END_CHARPOS (row) == charpos
14563 /* The end position of a row equals the start
14564 position of the next row. If CHARPOS is there, we
14565 would rather display it in the next line, except
14566 when this line ends in ZV. */
14567 && !row->ends_at_zv_p
14568 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
14569 && charpos >= MATRIX_ROW_START_CHARPOS (row))
14570 return row;
14571 ++row;
14572 }
14573 }
14574
14575
14576 /* Try to redisplay window W by reusing its existing display. W's
14577 current matrix must be up to date when this function is called,
14578 i.e. window_end_valid must not be nil.
14579
14580 Value is
14581
14582 1 if display has been updated
14583 0 if otherwise unsuccessful
14584 -1 if redisplay with same window start is known not to succeed
14585
14586 The following steps are performed:
14587
14588 1. Find the last row in the current matrix of W that is not
14589 affected by changes at the start of current_buffer. If no such row
14590 is found, give up.
14591
14592 2. Find the first row in W's current matrix that is not affected by
14593 changes at the end of current_buffer. Maybe there is no such row.
14594
14595 3. Display lines beginning with the row + 1 found in step 1 to the
14596 row found in step 2 or, if step 2 didn't find a row, to the end of
14597 the window.
14598
14599 4. If cursor is not known to appear on the window, give up.
14600
14601 5. If display stopped at the row found in step 2, scroll the
14602 display and current matrix as needed.
14603
14604 6. Maybe display some lines at the end of W, if we must. This can
14605 happen under various circumstances, like a partially visible line
14606 becoming fully visible, or because newly displayed lines are displayed
14607 in smaller font sizes.
14608
14609 7. Update W's window end information. */
14610
14611 static int
14612 try_window_id (w)
14613 struct window *w;
14614 {
14615 struct frame *f = XFRAME (w->frame);
14616 struct glyph_matrix *current_matrix = w->current_matrix;
14617 struct glyph_matrix *desired_matrix = w->desired_matrix;
14618 struct glyph_row *last_unchanged_at_beg_row;
14619 struct glyph_row *first_unchanged_at_end_row;
14620 struct glyph_row *row;
14621 struct glyph_row *bottom_row;
14622 int bottom_vpos;
14623 struct it it;
14624 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
14625 struct text_pos start_pos;
14626 struct run run;
14627 int first_unchanged_at_end_vpos = 0;
14628 struct glyph_row *last_text_row, *last_text_row_at_end;
14629 struct text_pos start;
14630 int first_changed_charpos, last_changed_charpos;
14631
14632 #if GLYPH_DEBUG
14633 if (inhibit_try_window_id)
14634 return 0;
14635 #endif
14636
14637 /* This is handy for debugging. */
14638 #if 0
14639 #define GIVE_UP(X) \
14640 do { \
14641 fprintf (stderr, "try_window_id give up %d\n", (X)); \
14642 return 0; \
14643 } while (0)
14644 #else
14645 #define GIVE_UP(X) return 0
14646 #endif
14647
14648 SET_TEXT_POS_FROM_MARKER (start, w->start);
14649
14650 /* Don't use this for mini-windows because these can show
14651 messages and mini-buffers, and we don't handle that here. */
14652 if (MINI_WINDOW_P (w))
14653 GIVE_UP (1);
14654
14655 /* This flag is used to prevent redisplay optimizations. */
14656 if (windows_or_buffers_changed || cursor_type_changed)
14657 GIVE_UP (2);
14658
14659 /* Verify that narrowing has not changed.
14660 Also verify that we were not told to prevent redisplay optimizations.
14661 It would be nice to further
14662 reduce the number of cases where this prevents try_window_id. */
14663 if (current_buffer->clip_changed
14664 || current_buffer->prevent_redisplay_optimizations_p)
14665 GIVE_UP (3);
14666
14667 /* Window must either use window-based redisplay or be full width. */
14668 if (!FRAME_WINDOW_P (f)
14669 && (!FRAME_LINE_INS_DEL_OK (f)
14670 || !WINDOW_FULL_WIDTH_P (w)))
14671 GIVE_UP (4);
14672
14673 /* Give up if point is not known NOT to appear in W. */
14674 if (PT < CHARPOS (start))
14675 GIVE_UP (5);
14676
14677 /* Another way to prevent redisplay optimizations. */
14678 if (XFASTINT (w->last_modified) == 0)
14679 GIVE_UP (6);
14680
14681 /* Verify that window is not hscrolled. */
14682 if (XFASTINT (w->hscroll) != 0)
14683 GIVE_UP (7);
14684
14685 /* Verify that display wasn't paused. */
14686 if (NILP (w->window_end_valid))
14687 GIVE_UP (8);
14688
14689 /* Can't use this if highlighting a region because a cursor movement
14690 will do more than just set the cursor. */
14691 if (!NILP (Vtransient_mark_mode)
14692 && !NILP (current_buffer->mark_active))
14693 GIVE_UP (9);
14694
14695 /* Likewise if highlighting trailing whitespace. */
14696 if (!NILP (Vshow_trailing_whitespace))
14697 GIVE_UP (11);
14698
14699 /* Likewise if showing a region. */
14700 if (!NILP (w->region_showing))
14701 GIVE_UP (10);
14702
14703 /* Can use this if overlay arrow position and or string have changed. */
14704 if (overlay_arrows_changed_p ())
14705 GIVE_UP (12);
14706
14707
14708 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
14709 only if buffer has really changed. The reason is that the gap is
14710 initially at Z for freshly visited files. The code below would
14711 set end_unchanged to 0 in that case. */
14712 if (MODIFF > SAVE_MODIFF
14713 /* This seems to happen sometimes after saving a buffer. */
14714 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
14715 {
14716 if (GPT - BEG < BEG_UNCHANGED)
14717 BEG_UNCHANGED = GPT - BEG;
14718 if (Z - GPT < END_UNCHANGED)
14719 END_UNCHANGED = Z - GPT;
14720 }
14721
14722 /* The position of the first and last character that has been changed. */
14723 first_changed_charpos = BEG + BEG_UNCHANGED;
14724 last_changed_charpos = Z - END_UNCHANGED;
14725
14726 /* If window starts after a line end, and the last change is in
14727 front of that newline, then changes don't affect the display.
14728 This case happens with stealth-fontification. Note that although
14729 the display is unchanged, glyph positions in the matrix have to
14730 be adjusted, of course. */
14731 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
14732 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
14733 && ((last_changed_charpos < CHARPOS (start)
14734 && CHARPOS (start) == BEGV)
14735 || (last_changed_charpos < CHARPOS (start) - 1
14736 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
14737 {
14738 int Z_old, delta, Z_BYTE_old, delta_bytes;
14739 struct glyph_row *r0;
14740
14741 /* Compute how many chars/bytes have been added to or removed
14742 from the buffer. */
14743 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
14744 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
14745 delta = Z - Z_old;
14746 delta_bytes = Z_BYTE - Z_BYTE_old;
14747
14748 /* Give up if PT is not in the window. Note that it already has
14749 been checked at the start of try_window_id that PT is not in
14750 front of the window start. */
14751 if (PT >= MATRIX_ROW_END_CHARPOS (row) + delta)
14752 GIVE_UP (13);
14753
14754 /* If window start is unchanged, we can reuse the whole matrix
14755 as is, after adjusting glyph positions. No need to compute
14756 the window end again, since its offset from Z hasn't changed. */
14757 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
14758 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + delta
14759 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + delta_bytes
14760 /* PT must not be in a partially visible line. */
14761 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + delta
14762 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
14763 {
14764 /* Adjust positions in the glyph matrix. */
14765 if (delta || delta_bytes)
14766 {
14767 struct glyph_row *r1
14768 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
14769 increment_matrix_positions (w->current_matrix,
14770 MATRIX_ROW_VPOS (r0, current_matrix),
14771 MATRIX_ROW_VPOS (r1, current_matrix),
14772 delta, delta_bytes);
14773 }
14774
14775 /* Set the cursor. */
14776 row = row_containing_pos (w, PT, r0, NULL, 0);
14777 if (row)
14778 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
14779 else
14780 abort ();
14781 return 1;
14782 }
14783 }
14784
14785 /* Handle the case that changes are all below what is displayed in
14786 the window, and that PT is in the window. This shortcut cannot
14787 be taken if ZV is visible in the window, and text has been added
14788 there that is visible in the window. */
14789 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
14790 /* ZV is not visible in the window, or there are no
14791 changes at ZV, actually. */
14792 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
14793 || first_changed_charpos == last_changed_charpos))
14794 {
14795 struct glyph_row *r0;
14796
14797 /* Give up if PT is not in the window. Note that it already has
14798 been checked at the start of try_window_id that PT is not in
14799 front of the window start. */
14800 if (PT >= MATRIX_ROW_END_CHARPOS (row))
14801 GIVE_UP (14);
14802
14803 /* If window start is unchanged, we can reuse the whole matrix
14804 as is, without changing glyph positions since no text has
14805 been added/removed in front of the window end. */
14806 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
14807 if (TEXT_POS_EQUAL_P (start, r0->start.pos)
14808 /* PT must not be in a partially visible line. */
14809 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
14810 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
14811 {
14812 /* We have to compute the window end anew since text
14813 can have been added/removed after it. */
14814 w->window_end_pos
14815 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
14816 w->window_end_bytepos
14817 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
14818
14819 /* Set the cursor. */
14820 row = row_containing_pos (w, PT, r0, NULL, 0);
14821 if (row)
14822 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
14823 else
14824 abort ();
14825 return 2;
14826 }
14827 }
14828
14829 /* Give up if window start is in the changed area.
14830
14831 The condition used to read
14832
14833 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
14834
14835 but why that was tested escapes me at the moment. */
14836 if (CHARPOS (start) >= first_changed_charpos
14837 && CHARPOS (start) <= last_changed_charpos)
14838 GIVE_UP (15);
14839
14840 /* Check that window start agrees with the start of the first glyph
14841 row in its current matrix. Check this after we know the window
14842 start is not in changed text, otherwise positions would not be
14843 comparable. */
14844 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
14845 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
14846 GIVE_UP (16);
14847
14848 /* Give up if the window ends in strings. Overlay strings
14849 at the end are difficult to handle, so don't try. */
14850 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
14851 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
14852 GIVE_UP (20);
14853
14854 /* Compute the position at which we have to start displaying new
14855 lines. Some of the lines at the top of the window might be
14856 reusable because they are not displaying changed text. Find the
14857 last row in W's current matrix not affected by changes at the
14858 start of current_buffer. Value is null if changes start in the
14859 first line of window. */
14860 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
14861 if (last_unchanged_at_beg_row)
14862 {
14863 /* Avoid starting to display in the moddle of a character, a TAB
14864 for instance. This is easier than to set up the iterator
14865 exactly, and it's not a frequent case, so the additional
14866 effort wouldn't really pay off. */
14867 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
14868 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
14869 && last_unchanged_at_beg_row > w->current_matrix->rows)
14870 --last_unchanged_at_beg_row;
14871
14872 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
14873 GIVE_UP (17);
14874
14875 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
14876 GIVE_UP (18);
14877 start_pos = it.current.pos;
14878
14879 /* Start displaying new lines in the desired matrix at the same
14880 vpos we would use in the current matrix, i.e. below
14881 last_unchanged_at_beg_row. */
14882 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
14883 current_matrix);
14884 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
14885 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
14886
14887 xassert (it.hpos == 0 && it.current_x == 0);
14888 }
14889 else
14890 {
14891 /* There are no reusable lines at the start of the window.
14892 Start displaying in the first text line. */
14893 start_display (&it, w, start);
14894 it.vpos = it.first_vpos;
14895 start_pos = it.current.pos;
14896 }
14897
14898 /* Find the first row that is not affected by changes at the end of
14899 the buffer. Value will be null if there is no unchanged row, in
14900 which case we must redisplay to the end of the window. delta
14901 will be set to the value by which buffer positions beginning with
14902 first_unchanged_at_end_row have to be adjusted due to text
14903 changes. */
14904 first_unchanged_at_end_row
14905 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
14906 IF_DEBUG (debug_delta = delta);
14907 IF_DEBUG (debug_delta_bytes = delta_bytes);
14908
14909 /* Set stop_pos to the buffer position up to which we will have to
14910 display new lines. If first_unchanged_at_end_row != NULL, this
14911 is the buffer position of the start of the line displayed in that
14912 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
14913 that we don't stop at a buffer position. */
14914 stop_pos = 0;
14915 if (first_unchanged_at_end_row)
14916 {
14917 xassert (last_unchanged_at_beg_row == NULL
14918 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
14919
14920 /* If this is a continuation line, move forward to the next one
14921 that isn't. Changes in lines above affect this line.
14922 Caution: this may move first_unchanged_at_end_row to a row
14923 not displaying text. */
14924 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
14925 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
14926 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
14927 < it.last_visible_y))
14928 ++first_unchanged_at_end_row;
14929
14930 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
14931 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
14932 >= it.last_visible_y))
14933 first_unchanged_at_end_row = NULL;
14934 else
14935 {
14936 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
14937 + delta);
14938 first_unchanged_at_end_vpos
14939 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
14940 xassert (stop_pos >= Z - END_UNCHANGED);
14941 }
14942 }
14943 else if (last_unchanged_at_beg_row == NULL)
14944 GIVE_UP (19);
14945
14946
14947 #if GLYPH_DEBUG
14948
14949 /* Either there is no unchanged row at the end, or the one we have
14950 now displays text. This is a necessary condition for the window
14951 end pos calculation at the end of this function. */
14952 xassert (first_unchanged_at_end_row == NULL
14953 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
14954
14955 debug_last_unchanged_at_beg_vpos
14956 = (last_unchanged_at_beg_row
14957 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
14958 : -1);
14959 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
14960
14961 #endif /* GLYPH_DEBUG != 0 */
14962
14963
14964 /* Display new lines. Set last_text_row to the last new line
14965 displayed which has text on it, i.e. might end up as being the
14966 line where the window_end_vpos is. */
14967 w->cursor.vpos = -1;
14968 last_text_row = NULL;
14969 overlay_arrow_seen = 0;
14970 while (it.current_y < it.last_visible_y
14971 && !fonts_changed_p
14972 && (first_unchanged_at_end_row == NULL
14973 || IT_CHARPOS (it) < stop_pos))
14974 {
14975 if (display_line (&it))
14976 last_text_row = it.glyph_row - 1;
14977 }
14978
14979 if (fonts_changed_p)
14980 return -1;
14981
14982
14983 /* Compute differences in buffer positions, y-positions etc. for
14984 lines reused at the bottom of the window. Compute what we can
14985 scroll. */
14986 if (first_unchanged_at_end_row
14987 /* No lines reused because we displayed everything up to the
14988 bottom of the window. */
14989 && it.current_y < it.last_visible_y)
14990 {
14991 dvpos = (it.vpos
14992 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
14993 current_matrix));
14994 dy = it.current_y - first_unchanged_at_end_row->y;
14995 run.current_y = first_unchanged_at_end_row->y;
14996 run.desired_y = run.current_y + dy;
14997 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
14998 }
14999 else
15000 {
15001 delta = dvpos = dy = run.current_y = run.desired_y = run.height = 0;
15002 first_unchanged_at_end_row = NULL;
15003 }
15004 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
15005
15006
15007 /* Find the cursor if not already found. We have to decide whether
15008 PT will appear on this window (it sometimes doesn't, but this is
15009 not a very frequent case.) This decision has to be made before
15010 the current matrix is altered. A value of cursor.vpos < 0 means
15011 that PT is either in one of the lines beginning at
15012 first_unchanged_at_end_row or below the window. Don't care for
15013 lines that might be displayed later at the window end; as
15014 mentioned, this is not a frequent case. */
15015 if (w->cursor.vpos < 0)
15016 {
15017 /* Cursor in unchanged rows at the top? */
15018 if (PT < CHARPOS (start_pos)
15019 && last_unchanged_at_beg_row)
15020 {
15021 row = row_containing_pos (w, PT,
15022 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
15023 last_unchanged_at_beg_row + 1, 0);
15024 if (row)
15025 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15026 }
15027
15028 /* Start from first_unchanged_at_end_row looking for PT. */
15029 else if (first_unchanged_at_end_row)
15030 {
15031 row = row_containing_pos (w, PT - delta,
15032 first_unchanged_at_end_row, NULL, 0);
15033 if (row)
15034 set_cursor_from_row (w, row, w->current_matrix, delta,
15035 delta_bytes, dy, dvpos);
15036 }
15037
15038 /* Give up if cursor was not found. */
15039 if (w->cursor.vpos < 0)
15040 {
15041 clear_glyph_matrix (w->desired_matrix);
15042 return -1;
15043 }
15044 }
15045
15046 /* Don't let the cursor end in the scroll margins. */
15047 {
15048 int this_scroll_margin, cursor_height;
15049
15050 this_scroll_margin = max (0, scroll_margin);
15051 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
15052 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
15053 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
15054
15055 if ((w->cursor.y < this_scroll_margin
15056 && CHARPOS (start) > BEGV)
15057 /* Old redisplay didn't take scroll margin into account at the bottom,
15058 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
15059 || (w->cursor.y + (make_cursor_line_fully_visible_p
15060 ? cursor_height + this_scroll_margin
15061 : 1)) > it.last_visible_y)
15062 {
15063 w->cursor.vpos = -1;
15064 clear_glyph_matrix (w->desired_matrix);
15065 return -1;
15066 }
15067 }
15068
15069 /* Scroll the display. Do it before changing the current matrix so
15070 that xterm.c doesn't get confused about where the cursor glyph is
15071 found. */
15072 if (dy && run.height)
15073 {
15074 update_begin (f);
15075
15076 if (FRAME_WINDOW_P (f))
15077 {
15078 FRAME_RIF (f)->update_window_begin_hook (w);
15079 FRAME_RIF (f)->clear_window_mouse_face (w);
15080 FRAME_RIF (f)->scroll_run_hook (w, &run);
15081 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
15082 }
15083 else
15084 {
15085 /* Terminal frame. In this case, dvpos gives the number of
15086 lines to scroll by; dvpos < 0 means scroll up. */
15087 int first_unchanged_at_end_vpos
15088 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
15089 int from = WINDOW_TOP_EDGE_LINE (w) + first_unchanged_at_end_vpos;
15090 int end = (WINDOW_TOP_EDGE_LINE (w)
15091 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
15092 + window_internal_height (w));
15093
15094 /* Perform the operation on the screen. */
15095 if (dvpos > 0)
15096 {
15097 /* Scroll last_unchanged_at_beg_row to the end of the
15098 window down dvpos lines. */
15099 set_terminal_window (f, end);
15100
15101 /* On dumb terminals delete dvpos lines at the end
15102 before inserting dvpos empty lines. */
15103 if (!FRAME_SCROLL_REGION_OK (f))
15104 ins_del_lines (f, end - dvpos, -dvpos);
15105
15106 /* Insert dvpos empty lines in front of
15107 last_unchanged_at_beg_row. */
15108 ins_del_lines (f, from, dvpos);
15109 }
15110 else if (dvpos < 0)
15111 {
15112 /* Scroll up last_unchanged_at_beg_vpos to the end of
15113 the window to last_unchanged_at_beg_vpos - |dvpos|. */
15114 set_terminal_window (f, end);
15115
15116 /* Delete dvpos lines in front of
15117 last_unchanged_at_beg_vpos. ins_del_lines will set
15118 the cursor to the given vpos and emit |dvpos| delete
15119 line sequences. */
15120 ins_del_lines (f, from + dvpos, dvpos);
15121
15122 /* On a dumb terminal insert dvpos empty lines at the
15123 end. */
15124 if (!FRAME_SCROLL_REGION_OK (f))
15125 ins_del_lines (f, end + dvpos, -dvpos);
15126 }
15127
15128 set_terminal_window (f, 0);
15129 }
15130
15131 update_end (f);
15132 }
15133
15134 /* Shift reused rows of the current matrix to the right position.
15135 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
15136 text. */
15137 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
15138 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
15139 if (dvpos < 0)
15140 {
15141 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
15142 bottom_vpos, dvpos);
15143 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
15144 bottom_vpos, 0);
15145 }
15146 else if (dvpos > 0)
15147 {
15148 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
15149 bottom_vpos, dvpos);
15150 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
15151 first_unchanged_at_end_vpos + dvpos, 0);
15152 }
15153
15154 /* For frame-based redisplay, make sure that current frame and window
15155 matrix are in sync with respect to glyph memory. */
15156 if (!FRAME_WINDOW_P (f))
15157 sync_frame_with_window_matrix_rows (w);
15158
15159 /* Adjust buffer positions in reused rows. */
15160 if (delta || delta_bytes)
15161 increment_matrix_positions (current_matrix,
15162 first_unchanged_at_end_vpos + dvpos,
15163 bottom_vpos, delta, delta_bytes);
15164
15165 /* Adjust Y positions. */
15166 if (dy)
15167 shift_glyph_matrix (w, current_matrix,
15168 first_unchanged_at_end_vpos + dvpos,
15169 bottom_vpos, dy);
15170
15171 if (first_unchanged_at_end_row)
15172 {
15173 first_unchanged_at_end_row += dvpos;
15174 if (first_unchanged_at_end_row->y >= it.last_visible_y
15175 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
15176 first_unchanged_at_end_row = NULL;
15177 }
15178
15179 /* If scrolling up, there may be some lines to display at the end of
15180 the window. */
15181 last_text_row_at_end = NULL;
15182 if (dy < 0)
15183 {
15184 /* Scrolling up can leave for example a partially visible line
15185 at the end of the window to be redisplayed. */
15186 /* Set last_row to the glyph row in the current matrix where the
15187 window end line is found. It has been moved up or down in
15188 the matrix by dvpos. */
15189 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
15190 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
15191
15192 /* If last_row is the window end line, it should display text. */
15193 xassert (last_row->displays_text_p);
15194
15195 /* If window end line was partially visible before, begin
15196 displaying at that line. Otherwise begin displaying with the
15197 line following it. */
15198 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
15199 {
15200 init_to_row_start (&it, w, last_row);
15201 it.vpos = last_vpos;
15202 it.current_y = last_row->y;
15203 }
15204 else
15205 {
15206 init_to_row_end (&it, w, last_row);
15207 it.vpos = 1 + last_vpos;
15208 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
15209 ++last_row;
15210 }
15211
15212 /* We may start in a continuation line. If so, we have to
15213 get the right continuation_lines_width and current_x. */
15214 it.continuation_lines_width = last_row->continuation_lines_width;
15215 it.hpos = it.current_x = 0;
15216
15217 /* Display the rest of the lines at the window end. */
15218 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
15219 while (it.current_y < it.last_visible_y
15220 && !fonts_changed_p)
15221 {
15222 /* Is it always sure that the display agrees with lines in
15223 the current matrix? I don't think so, so we mark rows
15224 displayed invalid in the current matrix by setting their
15225 enabled_p flag to zero. */
15226 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
15227 if (display_line (&it))
15228 last_text_row_at_end = it.glyph_row - 1;
15229 }
15230 }
15231
15232 /* Update window_end_pos and window_end_vpos. */
15233 if (first_unchanged_at_end_row
15234 && !last_text_row_at_end)
15235 {
15236 /* Window end line if one of the preserved rows from the current
15237 matrix. Set row to the last row displaying text in current
15238 matrix starting at first_unchanged_at_end_row, after
15239 scrolling. */
15240 xassert (first_unchanged_at_end_row->displays_text_p);
15241 row = find_last_row_displaying_text (w->current_matrix, &it,
15242 first_unchanged_at_end_row);
15243 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
15244
15245 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
15246 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
15247 w->window_end_vpos
15248 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
15249 xassert (w->window_end_bytepos >= 0);
15250 IF_DEBUG (debug_method_add (w, "A"));
15251 }
15252 else if (last_text_row_at_end)
15253 {
15254 w->window_end_pos
15255 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
15256 w->window_end_bytepos
15257 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
15258 w->window_end_vpos
15259 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
15260 xassert (w->window_end_bytepos >= 0);
15261 IF_DEBUG (debug_method_add (w, "B"));
15262 }
15263 else if (last_text_row)
15264 {
15265 /* We have displayed either to the end of the window or at the
15266 end of the window, i.e. the last row with text is to be found
15267 in the desired matrix. */
15268 w->window_end_pos
15269 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
15270 w->window_end_bytepos
15271 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
15272 w->window_end_vpos
15273 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
15274 xassert (w->window_end_bytepos >= 0);
15275 }
15276 else if (first_unchanged_at_end_row == NULL
15277 && last_text_row == NULL
15278 && last_text_row_at_end == NULL)
15279 {
15280 /* Displayed to end of window, but no line containing text was
15281 displayed. Lines were deleted at the end of the window. */
15282 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
15283 int vpos = XFASTINT (w->window_end_vpos);
15284 struct glyph_row *current_row = current_matrix->rows + vpos;
15285 struct glyph_row *desired_row = desired_matrix->rows + vpos;
15286
15287 for (row = NULL;
15288 row == NULL && vpos >= first_vpos;
15289 --vpos, --current_row, --desired_row)
15290 {
15291 if (desired_row->enabled_p)
15292 {
15293 if (desired_row->displays_text_p)
15294 row = desired_row;
15295 }
15296 else if (current_row->displays_text_p)
15297 row = current_row;
15298 }
15299
15300 xassert (row != NULL);
15301 w->window_end_vpos = make_number (vpos + 1);
15302 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
15303 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
15304 xassert (w->window_end_bytepos >= 0);
15305 IF_DEBUG (debug_method_add (w, "C"));
15306 }
15307 else
15308 abort ();
15309
15310 #if 0 /* This leads to problems, for instance when the cursor is
15311 at ZV, and the cursor line displays no text. */
15312 /* Disable rows below what's displayed in the window. This makes
15313 debugging easier. */
15314 enable_glyph_matrix_rows (current_matrix,
15315 XFASTINT (w->window_end_vpos) + 1,
15316 bottom_vpos, 0);
15317 #endif
15318
15319 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
15320 debug_end_vpos = XFASTINT (w->window_end_vpos));
15321
15322 /* Record that display has not been completed. */
15323 w->window_end_valid = Qnil;
15324 w->desired_matrix->no_scrolling_p = 1;
15325 return 3;
15326
15327 #undef GIVE_UP
15328 }
15329
15330
15331 \f
15332 /***********************************************************************
15333 More debugging support
15334 ***********************************************************************/
15335
15336 #if GLYPH_DEBUG
15337
15338 void dump_glyph_row P_ ((struct glyph_row *, int, int));
15339 void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
15340 void dump_glyph P_ ((struct glyph_row *, struct glyph *, int));
15341
15342
15343 /* Dump the contents of glyph matrix MATRIX on stderr.
15344
15345 GLYPHS 0 means don't show glyph contents.
15346 GLYPHS 1 means show glyphs in short form
15347 GLYPHS > 1 means show glyphs in long form. */
15348
15349 void
15350 dump_glyph_matrix (matrix, glyphs)
15351 struct glyph_matrix *matrix;
15352 int glyphs;
15353 {
15354 int i;
15355 for (i = 0; i < matrix->nrows; ++i)
15356 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
15357 }
15358
15359
15360 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
15361 the glyph row and area where the glyph comes from. */
15362
15363 void
15364 dump_glyph (row, glyph, area)
15365 struct glyph_row *row;
15366 struct glyph *glyph;
15367 int area;
15368 {
15369 if (glyph->type == CHAR_GLYPH)
15370 {
15371 fprintf (stderr,
15372 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15373 glyph - row->glyphs[TEXT_AREA],
15374 'C',
15375 glyph->charpos,
15376 (BUFFERP (glyph->object)
15377 ? 'B'
15378 : (STRINGP (glyph->object)
15379 ? 'S'
15380 : '-')),
15381 glyph->pixel_width,
15382 glyph->u.ch,
15383 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
15384 ? glyph->u.ch
15385 : '.'),
15386 glyph->face_id,
15387 glyph->left_box_line_p,
15388 glyph->right_box_line_p);
15389 }
15390 else if (glyph->type == STRETCH_GLYPH)
15391 {
15392 fprintf (stderr,
15393 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15394 glyph - row->glyphs[TEXT_AREA],
15395 'S',
15396 glyph->charpos,
15397 (BUFFERP (glyph->object)
15398 ? 'B'
15399 : (STRINGP (glyph->object)
15400 ? 'S'
15401 : '-')),
15402 glyph->pixel_width,
15403 0,
15404 '.',
15405 glyph->face_id,
15406 glyph->left_box_line_p,
15407 glyph->right_box_line_p);
15408 }
15409 else if (glyph->type == IMAGE_GLYPH)
15410 {
15411 fprintf (stderr,
15412 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15413 glyph - row->glyphs[TEXT_AREA],
15414 'I',
15415 glyph->charpos,
15416 (BUFFERP (glyph->object)
15417 ? 'B'
15418 : (STRINGP (glyph->object)
15419 ? 'S'
15420 : '-')),
15421 glyph->pixel_width,
15422 glyph->u.img_id,
15423 '.',
15424 glyph->face_id,
15425 glyph->left_box_line_p,
15426 glyph->right_box_line_p);
15427 }
15428 else if (glyph->type == COMPOSITE_GLYPH)
15429 {
15430 fprintf (stderr,
15431 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15432 glyph - row->glyphs[TEXT_AREA],
15433 '+',
15434 glyph->charpos,
15435 (BUFFERP (glyph->object)
15436 ? 'B'
15437 : (STRINGP (glyph->object)
15438 ? 'S'
15439 : '-')),
15440 glyph->pixel_width,
15441 glyph->u.cmp_id,
15442 '.',
15443 glyph->face_id,
15444 glyph->left_box_line_p,
15445 glyph->right_box_line_p);
15446 }
15447 }
15448
15449
15450 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
15451 GLYPHS 0 means don't show glyph contents.
15452 GLYPHS 1 means show glyphs in short form
15453 GLYPHS > 1 means show glyphs in long form. */
15454
15455 void
15456 dump_glyph_row (row, vpos, glyphs)
15457 struct glyph_row *row;
15458 int vpos, glyphs;
15459 {
15460 if (glyphs != 1)
15461 {
15462 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
15463 fprintf (stderr, "======================================================================\n");
15464
15465 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d\
15466 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
15467 vpos,
15468 MATRIX_ROW_START_CHARPOS (row),
15469 MATRIX_ROW_END_CHARPOS (row),
15470 row->used[TEXT_AREA],
15471 row->contains_overlapping_glyphs_p,
15472 row->enabled_p,
15473 row->truncated_on_left_p,
15474 row->truncated_on_right_p,
15475 row->continued_p,
15476 MATRIX_ROW_CONTINUATION_LINE_P (row),
15477 row->displays_text_p,
15478 row->ends_at_zv_p,
15479 row->fill_line_p,
15480 row->ends_in_middle_of_char_p,
15481 row->starts_in_middle_of_char_p,
15482 row->mouse_face_p,
15483 row->x,
15484 row->y,
15485 row->pixel_width,
15486 row->height,
15487 row->visible_height,
15488 row->ascent,
15489 row->phys_ascent);
15490 fprintf (stderr, "%9d %5d\t%5d\n", row->start.overlay_string_index,
15491 row->end.overlay_string_index,
15492 row->continuation_lines_width);
15493 fprintf (stderr, "%9d %5d\n",
15494 CHARPOS (row->start.string_pos),
15495 CHARPOS (row->end.string_pos));
15496 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
15497 row->end.dpvec_index);
15498 }
15499
15500 if (glyphs > 1)
15501 {
15502 int area;
15503
15504 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
15505 {
15506 struct glyph *glyph = row->glyphs[area];
15507 struct glyph *glyph_end = glyph + row->used[area];
15508
15509 /* Glyph for a line end in text. */
15510 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
15511 ++glyph_end;
15512
15513 if (glyph < glyph_end)
15514 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
15515
15516 for (; glyph < glyph_end; ++glyph)
15517 dump_glyph (row, glyph, area);
15518 }
15519 }
15520 else if (glyphs == 1)
15521 {
15522 int area;
15523
15524 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
15525 {
15526 char *s = (char *) alloca (row->used[area] + 1);
15527 int i;
15528
15529 for (i = 0; i < row->used[area]; ++i)
15530 {
15531 struct glyph *glyph = row->glyphs[area] + i;
15532 if (glyph->type == CHAR_GLYPH
15533 && glyph->u.ch < 0x80
15534 && glyph->u.ch >= ' ')
15535 s[i] = glyph->u.ch;
15536 else
15537 s[i] = '.';
15538 }
15539
15540 s[i] = '\0';
15541 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
15542 }
15543 }
15544 }
15545
15546
15547 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
15548 Sdump_glyph_matrix, 0, 1, "p",
15549 doc: /* Dump the current matrix of the selected window to stderr.
15550 Shows contents of glyph row structures. With non-nil
15551 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
15552 glyphs in short form, otherwise show glyphs in long form. */)
15553 (glyphs)
15554 Lisp_Object glyphs;
15555 {
15556 struct window *w = XWINDOW (selected_window);
15557 struct buffer *buffer = XBUFFER (w->buffer);
15558
15559 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
15560 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
15561 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
15562 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
15563 fprintf (stderr, "=============================================\n");
15564 dump_glyph_matrix (w->current_matrix,
15565 NILP (glyphs) ? 0 : XINT (glyphs));
15566 return Qnil;
15567 }
15568
15569
15570 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
15571 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
15572 ()
15573 {
15574 struct frame *f = XFRAME (selected_frame);
15575 dump_glyph_matrix (f->current_matrix, 1);
15576 return Qnil;
15577 }
15578
15579
15580 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
15581 doc: /* Dump glyph row ROW to stderr.
15582 GLYPH 0 means don't dump glyphs.
15583 GLYPH 1 means dump glyphs in short form.
15584 GLYPH > 1 or omitted means dump glyphs in long form. */)
15585 (row, glyphs)
15586 Lisp_Object row, glyphs;
15587 {
15588 struct glyph_matrix *matrix;
15589 int vpos;
15590
15591 CHECK_NUMBER (row);
15592 matrix = XWINDOW (selected_window)->current_matrix;
15593 vpos = XINT (row);
15594 if (vpos >= 0 && vpos < matrix->nrows)
15595 dump_glyph_row (MATRIX_ROW (matrix, vpos),
15596 vpos,
15597 INTEGERP (glyphs) ? XINT (glyphs) : 2);
15598 return Qnil;
15599 }
15600
15601
15602 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
15603 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
15604 GLYPH 0 means don't dump glyphs.
15605 GLYPH 1 means dump glyphs in short form.
15606 GLYPH > 1 or omitted means dump glyphs in long form. */)
15607 (row, glyphs)
15608 Lisp_Object row, glyphs;
15609 {
15610 struct frame *sf = SELECTED_FRAME ();
15611 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
15612 int vpos;
15613
15614 CHECK_NUMBER (row);
15615 vpos = XINT (row);
15616 if (vpos >= 0 && vpos < m->nrows)
15617 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
15618 INTEGERP (glyphs) ? XINT (glyphs) : 2);
15619 return Qnil;
15620 }
15621
15622
15623 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
15624 doc: /* Toggle tracing of redisplay.
15625 With ARG, turn tracing on if and only if ARG is positive. */)
15626 (arg)
15627 Lisp_Object arg;
15628 {
15629 if (NILP (arg))
15630 trace_redisplay_p = !trace_redisplay_p;
15631 else
15632 {
15633 arg = Fprefix_numeric_value (arg);
15634 trace_redisplay_p = XINT (arg) > 0;
15635 }
15636
15637 return Qnil;
15638 }
15639
15640
15641 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
15642 doc: /* Like `format', but print result to stderr.
15643 usage: (trace-to-stderr STRING &rest OBJECTS) */)
15644 (nargs, args)
15645 int nargs;
15646 Lisp_Object *args;
15647 {
15648 Lisp_Object s = Fformat (nargs, args);
15649 fprintf (stderr, "%s", SDATA (s));
15650 return Qnil;
15651 }
15652
15653 #endif /* GLYPH_DEBUG */
15654
15655
15656 \f
15657 /***********************************************************************
15658 Building Desired Matrix Rows
15659 ***********************************************************************/
15660
15661 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
15662 Used for non-window-redisplay windows, and for windows w/o left fringe. */
15663
15664 static struct glyph_row *
15665 get_overlay_arrow_glyph_row (w, overlay_arrow_string)
15666 struct window *w;
15667 Lisp_Object overlay_arrow_string;
15668 {
15669 struct frame *f = XFRAME (WINDOW_FRAME (w));
15670 struct buffer *buffer = XBUFFER (w->buffer);
15671 struct buffer *old = current_buffer;
15672 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
15673 int arrow_len = SCHARS (overlay_arrow_string);
15674 const unsigned char *arrow_end = arrow_string + arrow_len;
15675 const unsigned char *p;
15676 struct it it;
15677 int multibyte_p;
15678 int n_glyphs_before;
15679
15680 set_buffer_temp (buffer);
15681 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
15682 it.glyph_row->used[TEXT_AREA] = 0;
15683 SET_TEXT_POS (it.position, 0, 0);
15684
15685 multibyte_p = !NILP (buffer->enable_multibyte_characters);
15686 p = arrow_string;
15687 while (p < arrow_end)
15688 {
15689 Lisp_Object face, ilisp;
15690
15691 /* Get the next character. */
15692 if (multibyte_p)
15693 it.c = string_char_and_length (p, arrow_len, &it.len);
15694 else
15695 it.c = *p, it.len = 1;
15696 p += it.len;
15697
15698 /* Get its face. */
15699 ilisp = make_number (p - arrow_string);
15700 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
15701 it.face_id = compute_char_face (f, it.c, face);
15702
15703 /* Compute its width, get its glyphs. */
15704 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
15705 SET_TEXT_POS (it.position, -1, -1);
15706 PRODUCE_GLYPHS (&it);
15707
15708 /* If this character doesn't fit any more in the line, we have
15709 to remove some glyphs. */
15710 if (it.current_x > it.last_visible_x)
15711 {
15712 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
15713 break;
15714 }
15715 }
15716
15717 set_buffer_temp (old);
15718 return it.glyph_row;
15719 }
15720
15721
15722 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
15723 glyphs are only inserted for terminal frames since we can't really
15724 win with truncation glyphs when partially visible glyphs are
15725 involved. Which glyphs to insert is determined by
15726 produce_special_glyphs. */
15727
15728 static void
15729 insert_left_trunc_glyphs (it)
15730 struct it *it;
15731 {
15732 struct it truncate_it;
15733 struct glyph *from, *end, *to, *toend;
15734
15735 xassert (!FRAME_WINDOW_P (it->f));
15736
15737 /* Get the truncation glyphs. */
15738 truncate_it = *it;
15739 truncate_it.current_x = 0;
15740 truncate_it.face_id = DEFAULT_FACE_ID;
15741 truncate_it.glyph_row = &scratch_glyph_row;
15742 truncate_it.glyph_row->used[TEXT_AREA] = 0;
15743 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
15744 truncate_it.object = make_number (0);
15745 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
15746
15747 /* Overwrite glyphs from IT with truncation glyphs. */
15748 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
15749 end = from + truncate_it.glyph_row->used[TEXT_AREA];
15750 to = it->glyph_row->glyphs[TEXT_AREA];
15751 toend = to + it->glyph_row->used[TEXT_AREA];
15752
15753 while (from < end)
15754 *to++ = *from++;
15755
15756 /* There may be padding glyphs left over. Overwrite them too. */
15757 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
15758 {
15759 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
15760 while (from < end)
15761 *to++ = *from++;
15762 }
15763
15764 if (to > toend)
15765 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
15766 }
15767
15768
15769 /* Compute the pixel height and width of IT->glyph_row.
15770
15771 Most of the time, ascent and height of a display line will be equal
15772 to the max_ascent and max_height values of the display iterator
15773 structure. This is not the case if
15774
15775 1. We hit ZV without displaying anything. In this case, max_ascent
15776 and max_height will be zero.
15777
15778 2. We have some glyphs that don't contribute to the line height.
15779 (The glyph row flag contributes_to_line_height_p is for future
15780 pixmap extensions).
15781
15782 The first case is easily covered by using default values because in
15783 these cases, the line height does not really matter, except that it
15784 must not be zero. */
15785
15786 static void
15787 compute_line_metrics (it)
15788 struct it *it;
15789 {
15790 struct glyph_row *row = it->glyph_row;
15791 int area, i;
15792
15793 if (FRAME_WINDOW_P (it->f))
15794 {
15795 int i, min_y, max_y;
15796
15797 /* The line may consist of one space only, that was added to
15798 place the cursor on it. If so, the row's height hasn't been
15799 computed yet. */
15800 if (row->height == 0)
15801 {
15802 if (it->max_ascent + it->max_descent == 0)
15803 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
15804 row->ascent = it->max_ascent;
15805 row->height = it->max_ascent + it->max_descent;
15806 row->phys_ascent = it->max_phys_ascent;
15807 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
15808 row->extra_line_spacing = it->max_extra_line_spacing;
15809 }
15810
15811 /* Compute the width of this line. */
15812 row->pixel_width = row->x;
15813 for (i = 0; i < row->used[TEXT_AREA]; ++i)
15814 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
15815
15816 xassert (row->pixel_width >= 0);
15817 xassert (row->ascent >= 0 && row->height > 0);
15818
15819 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
15820 || MATRIX_ROW_OVERLAPS_PRED_P (row));
15821
15822 /* If first line's physical ascent is larger than its logical
15823 ascent, use the physical ascent, and make the row taller.
15824 This makes accented characters fully visible. */
15825 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
15826 && row->phys_ascent > row->ascent)
15827 {
15828 row->height += row->phys_ascent - row->ascent;
15829 row->ascent = row->phys_ascent;
15830 }
15831
15832 /* Compute how much of the line is visible. */
15833 row->visible_height = row->height;
15834
15835 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
15836 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
15837
15838 if (row->y < min_y)
15839 row->visible_height -= min_y - row->y;
15840 if (row->y + row->height > max_y)
15841 row->visible_height -= row->y + row->height - max_y;
15842 }
15843 else
15844 {
15845 row->pixel_width = row->used[TEXT_AREA];
15846 if (row->continued_p)
15847 row->pixel_width -= it->continuation_pixel_width;
15848 else if (row->truncated_on_right_p)
15849 row->pixel_width -= it->truncation_pixel_width;
15850 row->ascent = row->phys_ascent = 0;
15851 row->height = row->phys_height = row->visible_height = 1;
15852 row->extra_line_spacing = 0;
15853 }
15854
15855 /* Compute a hash code for this row. */
15856 row->hash = 0;
15857 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
15858 for (i = 0; i < row->used[area]; ++i)
15859 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
15860 + row->glyphs[area][i].u.val
15861 + row->glyphs[area][i].face_id
15862 + row->glyphs[area][i].padding_p
15863 + (row->glyphs[area][i].type << 2));
15864
15865 it->max_ascent = it->max_descent = 0;
15866 it->max_phys_ascent = it->max_phys_descent = 0;
15867 }
15868
15869
15870 /* Append one space to the glyph row of iterator IT if doing a
15871 window-based redisplay. The space has the same face as
15872 IT->face_id. Value is non-zero if a space was added.
15873
15874 This function is called to make sure that there is always one glyph
15875 at the end of a glyph row that the cursor can be set on under
15876 window-systems. (If there weren't such a glyph we would not know
15877 how wide and tall a box cursor should be displayed).
15878
15879 At the same time this space let's a nicely handle clearing to the
15880 end of the line if the row ends in italic text. */
15881
15882 static int
15883 append_space_for_newline (it, default_face_p)
15884 struct it *it;
15885 int default_face_p;
15886 {
15887 if (FRAME_WINDOW_P (it->f))
15888 {
15889 int n = it->glyph_row->used[TEXT_AREA];
15890
15891 if (it->glyph_row->glyphs[TEXT_AREA] + n
15892 < it->glyph_row->glyphs[1 + TEXT_AREA])
15893 {
15894 /* Save some values that must not be changed.
15895 Must save IT->c and IT->len because otherwise
15896 ITERATOR_AT_END_P wouldn't work anymore after
15897 append_space_for_newline has been called. */
15898 enum display_element_type saved_what = it->what;
15899 int saved_c = it->c, saved_len = it->len;
15900 int saved_x = it->current_x;
15901 int saved_face_id = it->face_id;
15902 struct text_pos saved_pos;
15903 Lisp_Object saved_object;
15904 struct face *face;
15905
15906 saved_object = it->object;
15907 saved_pos = it->position;
15908
15909 it->what = IT_CHARACTER;
15910 bzero (&it->position, sizeof it->position);
15911 it->object = make_number (0);
15912 it->c = ' ';
15913 it->len = 1;
15914
15915 if (default_face_p)
15916 it->face_id = DEFAULT_FACE_ID;
15917 else if (it->face_before_selective_p)
15918 it->face_id = it->saved_face_id;
15919 face = FACE_FROM_ID (it->f, it->face_id);
15920 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
15921
15922 PRODUCE_GLYPHS (it);
15923
15924 it->override_ascent = -1;
15925 it->constrain_row_ascent_descent_p = 0;
15926 it->current_x = saved_x;
15927 it->object = saved_object;
15928 it->position = saved_pos;
15929 it->what = saved_what;
15930 it->face_id = saved_face_id;
15931 it->len = saved_len;
15932 it->c = saved_c;
15933 return 1;
15934 }
15935 }
15936
15937 return 0;
15938 }
15939
15940
15941 /* Extend the face of the last glyph in the text area of IT->glyph_row
15942 to the end of the display line. Called from display_line.
15943 If the glyph row is empty, add a space glyph to it so that we
15944 know the face to draw. Set the glyph row flag fill_line_p. */
15945
15946 static void
15947 extend_face_to_end_of_line (it)
15948 struct it *it;
15949 {
15950 struct face *face;
15951 struct frame *f = it->f;
15952
15953 /* If line is already filled, do nothing. */
15954 if (it->current_x >= it->last_visible_x)
15955 return;
15956
15957 /* Face extension extends the background and box of IT->face_id
15958 to the end of the line. If the background equals the background
15959 of the frame, we don't have to do anything. */
15960 if (it->face_before_selective_p)
15961 face = FACE_FROM_ID (it->f, it->saved_face_id);
15962 else
15963 face = FACE_FROM_ID (f, it->face_id);
15964
15965 if (FRAME_WINDOW_P (f)
15966 && it->glyph_row->displays_text_p
15967 && face->box == FACE_NO_BOX
15968 && face->background == FRAME_BACKGROUND_PIXEL (f)
15969 && !face->stipple)
15970 return;
15971
15972 /* Set the glyph row flag indicating that the face of the last glyph
15973 in the text area has to be drawn to the end of the text area. */
15974 it->glyph_row->fill_line_p = 1;
15975
15976 /* If current character of IT is not ASCII, make sure we have the
15977 ASCII face. This will be automatically undone the next time
15978 get_next_display_element returns a multibyte character. Note
15979 that the character will always be single byte in unibyte text. */
15980 if (!ASCII_CHAR_P (it->c))
15981 {
15982 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
15983 }
15984
15985 if (FRAME_WINDOW_P (f))
15986 {
15987 /* If the row is empty, add a space with the current face of IT,
15988 so that we know which face to draw. */
15989 if (it->glyph_row->used[TEXT_AREA] == 0)
15990 {
15991 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
15992 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
15993 it->glyph_row->used[TEXT_AREA] = 1;
15994 }
15995 }
15996 else
15997 {
15998 /* Save some values that must not be changed. */
15999 int saved_x = it->current_x;
16000 struct text_pos saved_pos;
16001 Lisp_Object saved_object;
16002 enum display_element_type saved_what = it->what;
16003 int saved_face_id = it->face_id;
16004
16005 saved_object = it->object;
16006 saved_pos = it->position;
16007
16008 it->what = IT_CHARACTER;
16009 bzero (&it->position, sizeof it->position);
16010 it->object = make_number (0);
16011 it->c = ' ';
16012 it->len = 1;
16013 it->face_id = face->id;
16014
16015 PRODUCE_GLYPHS (it);
16016
16017 while (it->current_x <= it->last_visible_x)
16018 PRODUCE_GLYPHS (it);
16019
16020 /* Don't count these blanks really. It would let us insert a left
16021 truncation glyph below and make us set the cursor on them, maybe. */
16022 it->current_x = saved_x;
16023 it->object = saved_object;
16024 it->position = saved_pos;
16025 it->what = saved_what;
16026 it->face_id = saved_face_id;
16027 }
16028 }
16029
16030
16031 /* Value is non-zero if text starting at CHARPOS in current_buffer is
16032 trailing whitespace. */
16033
16034 static int
16035 trailing_whitespace_p (charpos)
16036 int charpos;
16037 {
16038 int bytepos = CHAR_TO_BYTE (charpos);
16039 int c = 0;
16040
16041 while (bytepos < ZV_BYTE
16042 && (c = FETCH_CHAR (bytepos),
16043 c == ' ' || c == '\t'))
16044 ++bytepos;
16045
16046 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
16047 {
16048 if (bytepos != PT_BYTE)
16049 return 1;
16050 }
16051 return 0;
16052 }
16053
16054
16055 /* Highlight trailing whitespace, if any, in ROW. */
16056
16057 void
16058 highlight_trailing_whitespace (f, row)
16059 struct frame *f;
16060 struct glyph_row *row;
16061 {
16062 int used = row->used[TEXT_AREA];
16063
16064 if (used)
16065 {
16066 struct glyph *start = row->glyphs[TEXT_AREA];
16067 struct glyph *glyph = start + used - 1;
16068
16069 /* Skip over glyphs inserted to display the cursor at the
16070 end of a line, for extending the face of the last glyph
16071 to the end of the line on terminals, and for truncation
16072 and continuation glyphs. */
16073 while (glyph >= start
16074 && glyph->type == CHAR_GLYPH
16075 && INTEGERP (glyph->object))
16076 --glyph;
16077
16078 /* If last glyph is a space or stretch, and it's trailing
16079 whitespace, set the face of all trailing whitespace glyphs in
16080 IT->glyph_row to `trailing-whitespace'. */
16081 if (glyph >= start
16082 && BUFFERP (glyph->object)
16083 && (glyph->type == STRETCH_GLYPH
16084 || (glyph->type == CHAR_GLYPH
16085 && glyph->u.ch == ' '))
16086 && trailing_whitespace_p (glyph->charpos))
16087 {
16088 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
16089 if (face_id < 0)
16090 return;
16091
16092 while (glyph >= start
16093 && BUFFERP (glyph->object)
16094 && (glyph->type == STRETCH_GLYPH
16095 || (glyph->type == CHAR_GLYPH
16096 && glyph->u.ch == ' ')))
16097 (glyph--)->face_id = face_id;
16098 }
16099 }
16100 }
16101
16102
16103 /* Value is non-zero if glyph row ROW in window W should be
16104 used to hold the cursor. */
16105
16106 static int
16107 cursor_row_p (w, row)
16108 struct window *w;
16109 struct glyph_row *row;
16110 {
16111 int cursor_row_p = 1;
16112
16113 if (PT == MATRIX_ROW_END_CHARPOS (row))
16114 {
16115 /* Suppose the row ends on a string.
16116 Unless the row is continued, that means it ends on a newline
16117 in the string. If it's anything other than a display string
16118 (e.g. a before-string from an overlay), we don't want the
16119 cursor there. (This heuristic seems to give the optimal
16120 behavior for the various types of multi-line strings.) */
16121 if (CHARPOS (row->end.string_pos) >= 0)
16122 {
16123 if (row->continued_p)
16124 cursor_row_p = 1;
16125 else
16126 {
16127 /* Check for `display' property. */
16128 struct glyph *beg = row->glyphs[TEXT_AREA];
16129 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
16130 struct glyph *glyph;
16131
16132 cursor_row_p = 0;
16133 for (glyph = end; glyph >= beg; --glyph)
16134 if (STRINGP (glyph->object))
16135 {
16136 Lisp_Object prop
16137 = Fget_char_property (make_number (PT),
16138 Qdisplay, Qnil);
16139 cursor_row_p =
16140 (!NILP (prop)
16141 && display_prop_string_p (prop, glyph->object));
16142 break;
16143 }
16144 }
16145 }
16146 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
16147 {
16148 /* If the row ends in middle of a real character,
16149 and the line is continued, we want the cursor here.
16150 That's because MATRIX_ROW_END_CHARPOS would equal
16151 PT if PT is before the character. */
16152 if (!row->ends_in_ellipsis_p)
16153 cursor_row_p = row->continued_p;
16154 else
16155 /* If the row ends in an ellipsis, then
16156 MATRIX_ROW_END_CHARPOS will equal point after the invisible text.
16157 We want that position to be displayed after the ellipsis. */
16158 cursor_row_p = 0;
16159 }
16160 /* If the row ends at ZV, display the cursor at the end of that
16161 row instead of at the start of the row below. */
16162 else if (row->ends_at_zv_p)
16163 cursor_row_p = 1;
16164 else
16165 cursor_row_p = 0;
16166 }
16167
16168 return cursor_row_p;
16169 }
16170
16171
16172 /* Construct the glyph row IT->glyph_row in the desired matrix of
16173 IT->w from text at the current position of IT. See dispextern.h
16174 for an overview of struct it. Value is non-zero if
16175 IT->glyph_row displays text, as opposed to a line displaying ZV
16176 only. */
16177
16178 static int
16179 display_line (it)
16180 struct it *it;
16181 {
16182 struct glyph_row *row = it->glyph_row;
16183 Lisp_Object overlay_arrow_string;
16184
16185 /* We always start displaying at hpos zero even if hscrolled. */
16186 xassert (it->hpos == 0 && it->current_x == 0);
16187
16188 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
16189 >= it->w->desired_matrix->nrows)
16190 {
16191 it->w->nrows_scale_factor++;
16192 fonts_changed_p = 1;
16193 return 0;
16194 }
16195
16196 /* Is IT->w showing the region? */
16197 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
16198
16199 /* Clear the result glyph row and enable it. */
16200 prepare_desired_row (row);
16201
16202 row->y = it->current_y;
16203 row->start = it->start;
16204 row->continuation_lines_width = it->continuation_lines_width;
16205 row->displays_text_p = 1;
16206 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
16207 it->starts_in_middle_of_char_p = 0;
16208
16209 /* Arrange the overlays nicely for our purposes. Usually, we call
16210 display_line on only one line at a time, in which case this
16211 can't really hurt too much, or we call it on lines which appear
16212 one after another in the buffer, in which case all calls to
16213 recenter_overlay_lists but the first will be pretty cheap. */
16214 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
16215
16216 /* Move over display elements that are not visible because we are
16217 hscrolled. This may stop at an x-position < IT->first_visible_x
16218 if the first glyph is partially visible or if we hit a line end. */
16219 if (it->current_x < it->first_visible_x)
16220 {
16221 move_it_in_display_line_to (it, ZV, it->first_visible_x,
16222 MOVE_TO_POS | MOVE_TO_X);
16223 }
16224
16225 /* Get the initial row height. This is either the height of the
16226 text hscrolled, if there is any, or zero. */
16227 row->ascent = it->max_ascent;
16228 row->height = it->max_ascent + it->max_descent;
16229 row->phys_ascent = it->max_phys_ascent;
16230 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
16231 row->extra_line_spacing = it->max_extra_line_spacing;
16232
16233 /* Loop generating characters. The loop is left with IT on the next
16234 character to display. */
16235 while (1)
16236 {
16237 int n_glyphs_before, hpos_before, x_before;
16238 int x, i, nglyphs;
16239 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
16240
16241 /* Retrieve the next thing to display. Value is zero if end of
16242 buffer reached. */
16243 if (!get_next_display_element (it))
16244 {
16245 /* Maybe add a space at the end of this line that is used to
16246 display the cursor there under X. Set the charpos of the
16247 first glyph of blank lines not corresponding to any text
16248 to -1. */
16249 #ifdef HAVE_WINDOW_SYSTEM
16250 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16251 row->exact_window_width_line_p = 1;
16252 else
16253 #endif /* HAVE_WINDOW_SYSTEM */
16254 if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
16255 || row->used[TEXT_AREA] == 0)
16256 {
16257 row->glyphs[TEXT_AREA]->charpos = -1;
16258 row->displays_text_p = 0;
16259
16260 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines)
16261 && (!MINI_WINDOW_P (it->w)
16262 || (minibuf_level && EQ (it->window, minibuf_window))))
16263 row->indicate_empty_line_p = 1;
16264 }
16265
16266 it->continuation_lines_width = 0;
16267 row->ends_at_zv_p = 1;
16268 break;
16269 }
16270
16271 /* Now, get the metrics of what we want to display. This also
16272 generates glyphs in `row' (which is IT->glyph_row). */
16273 n_glyphs_before = row->used[TEXT_AREA];
16274 x = it->current_x;
16275
16276 /* Remember the line height so far in case the next element doesn't
16277 fit on the line. */
16278 if (!it->truncate_lines_p)
16279 {
16280 ascent = it->max_ascent;
16281 descent = it->max_descent;
16282 phys_ascent = it->max_phys_ascent;
16283 phys_descent = it->max_phys_descent;
16284 }
16285
16286 PRODUCE_GLYPHS (it);
16287
16288 /* If this display element was in marginal areas, continue with
16289 the next one. */
16290 if (it->area != TEXT_AREA)
16291 {
16292 row->ascent = max (row->ascent, it->max_ascent);
16293 row->height = max (row->height, it->max_ascent + it->max_descent);
16294 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
16295 row->phys_height = max (row->phys_height,
16296 it->max_phys_ascent + it->max_phys_descent);
16297 row->extra_line_spacing = max (row->extra_line_spacing,
16298 it->max_extra_line_spacing);
16299 set_iterator_to_next (it, 1);
16300 continue;
16301 }
16302
16303 /* Does the display element fit on the line? If we truncate
16304 lines, we should draw past the right edge of the window. If
16305 we don't truncate, we want to stop so that we can display the
16306 continuation glyph before the right margin. If lines are
16307 continued, there are two possible strategies for characters
16308 resulting in more than 1 glyph (e.g. tabs): Display as many
16309 glyphs as possible in this line and leave the rest for the
16310 continuation line, or display the whole element in the next
16311 line. Original redisplay did the former, so we do it also. */
16312 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
16313 hpos_before = it->hpos;
16314 x_before = x;
16315
16316 if (/* Not a newline. */
16317 nglyphs > 0
16318 /* Glyphs produced fit entirely in the line. */
16319 && it->current_x < it->last_visible_x)
16320 {
16321 it->hpos += nglyphs;
16322 row->ascent = max (row->ascent, it->max_ascent);
16323 row->height = max (row->height, it->max_ascent + it->max_descent);
16324 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
16325 row->phys_height = max (row->phys_height,
16326 it->max_phys_ascent + it->max_phys_descent);
16327 row->extra_line_spacing = max (row->extra_line_spacing,
16328 it->max_extra_line_spacing);
16329 if (it->current_x - it->pixel_width < it->first_visible_x)
16330 row->x = x - it->first_visible_x;
16331 }
16332 else
16333 {
16334 int new_x;
16335 struct glyph *glyph;
16336
16337 for (i = 0; i < nglyphs; ++i, x = new_x)
16338 {
16339 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
16340 new_x = x + glyph->pixel_width;
16341
16342 if (/* Lines are continued. */
16343 !it->truncate_lines_p
16344 && (/* Glyph doesn't fit on the line. */
16345 new_x > it->last_visible_x
16346 /* Or it fits exactly on a window system frame. */
16347 || (new_x == it->last_visible_x
16348 && FRAME_WINDOW_P (it->f))))
16349 {
16350 /* End of a continued line. */
16351
16352 if (it->hpos == 0
16353 || (new_x == it->last_visible_x
16354 && FRAME_WINDOW_P (it->f)))
16355 {
16356 /* Current glyph is the only one on the line or
16357 fits exactly on the line. We must continue
16358 the line because we can't draw the cursor
16359 after the glyph. */
16360 row->continued_p = 1;
16361 it->current_x = new_x;
16362 it->continuation_lines_width += new_x;
16363 ++it->hpos;
16364 if (i == nglyphs - 1)
16365 {
16366 set_iterator_to_next (it, 1);
16367 #ifdef HAVE_WINDOW_SYSTEM
16368 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16369 {
16370 if (!get_next_display_element (it))
16371 {
16372 row->exact_window_width_line_p = 1;
16373 it->continuation_lines_width = 0;
16374 row->continued_p = 0;
16375 row->ends_at_zv_p = 1;
16376 }
16377 else if (ITERATOR_AT_END_OF_LINE_P (it))
16378 {
16379 row->continued_p = 0;
16380 row->exact_window_width_line_p = 1;
16381 }
16382 }
16383 #endif /* HAVE_WINDOW_SYSTEM */
16384 }
16385 }
16386 else if (CHAR_GLYPH_PADDING_P (*glyph)
16387 && !FRAME_WINDOW_P (it->f))
16388 {
16389 /* A padding glyph that doesn't fit on this line.
16390 This means the whole character doesn't fit
16391 on the line. */
16392 row->used[TEXT_AREA] = n_glyphs_before;
16393
16394 /* Fill the rest of the row with continuation
16395 glyphs like in 20.x. */
16396 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
16397 < row->glyphs[1 + TEXT_AREA])
16398 produce_special_glyphs (it, IT_CONTINUATION);
16399
16400 row->continued_p = 1;
16401 it->current_x = x_before;
16402 it->continuation_lines_width += x_before;
16403
16404 /* Restore the height to what it was before the
16405 element not fitting on the line. */
16406 it->max_ascent = ascent;
16407 it->max_descent = descent;
16408 it->max_phys_ascent = phys_ascent;
16409 it->max_phys_descent = phys_descent;
16410 }
16411 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
16412 {
16413 /* A TAB that extends past the right edge of the
16414 window. This produces a single glyph on
16415 window system frames. We leave the glyph in
16416 this row and let it fill the row, but don't
16417 consume the TAB. */
16418 it->continuation_lines_width += it->last_visible_x;
16419 row->ends_in_middle_of_char_p = 1;
16420 row->continued_p = 1;
16421 glyph->pixel_width = it->last_visible_x - x;
16422 it->starts_in_middle_of_char_p = 1;
16423 }
16424 else
16425 {
16426 /* Something other than a TAB that draws past
16427 the right edge of the window. Restore
16428 positions to values before the element. */
16429 row->used[TEXT_AREA] = n_glyphs_before + i;
16430
16431 /* Display continuation glyphs. */
16432 if (!FRAME_WINDOW_P (it->f))
16433 produce_special_glyphs (it, IT_CONTINUATION);
16434 row->continued_p = 1;
16435
16436 it->current_x = x_before;
16437 it->continuation_lines_width += x;
16438 extend_face_to_end_of_line (it);
16439
16440 if (nglyphs > 1 && i > 0)
16441 {
16442 row->ends_in_middle_of_char_p = 1;
16443 it->starts_in_middle_of_char_p = 1;
16444 }
16445
16446 /* Restore the height to what it was before the
16447 element not fitting on the line. */
16448 it->max_ascent = ascent;
16449 it->max_descent = descent;
16450 it->max_phys_ascent = phys_ascent;
16451 it->max_phys_descent = phys_descent;
16452 }
16453
16454 break;
16455 }
16456 else if (new_x > it->first_visible_x)
16457 {
16458 /* Increment number of glyphs actually displayed. */
16459 ++it->hpos;
16460
16461 if (x < it->first_visible_x)
16462 /* Glyph is partially visible, i.e. row starts at
16463 negative X position. */
16464 row->x = x - it->first_visible_x;
16465 }
16466 else
16467 {
16468 /* Glyph is completely off the left margin of the
16469 window. This should not happen because of the
16470 move_it_in_display_line at the start of this
16471 function, unless the text display area of the
16472 window is empty. */
16473 xassert (it->first_visible_x <= it->last_visible_x);
16474 }
16475 }
16476
16477 row->ascent = max (row->ascent, it->max_ascent);
16478 row->height = max (row->height, it->max_ascent + it->max_descent);
16479 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
16480 row->phys_height = max (row->phys_height,
16481 it->max_phys_ascent + it->max_phys_descent);
16482 row->extra_line_spacing = max (row->extra_line_spacing,
16483 it->max_extra_line_spacing);
16484
16485 /* End of this display line if row is continued. */
16486 if (row->continued_p || row->ends_at_zv_p)
16487 break;
16488 }
16489
16490 at_end_of_line:
16491 /* Is this a line end? If yes, we're also done, after making
16492 sure that a non-default face is extended up to the right
16493 margin of the window. */
16494 if (ITERATOR_AT_END_OF_LINE_P (it))
16495 {
16496 int used_before = row->used[TEXT_AREA];
16497
16498 row->ends_in_newline_from_string_p = STRINGP (it->object);
16499
16500 #ifdef HAVE_WINDOW_SYSTEM
16501 /* Add a space at the end of the line that is used to
16502 display the cursor there. */
16503 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16504 append_space_for_newline (it, 0);
16505 #endif /* HAVE_WINDOW_SYSTEM */
16506
16507 /* Extend the face to the end of the line. */
16508 extend_face_to_end_of_line (it);
16509
16510 /* Make sure we have the position. */
16511 if (used_before == 0)
16512 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
16513
16514 /* Consume the line end. This skips over invisible lines. */
16515 set_iterator_to_next (it, 1);
16516 it->continuation_lines_width = 0;
16517 break;
16518 }
16519
16520 /* Proceed with next display element. Note that this skips
16521 over lines invisible because of selective display. */
16522 set_iterator_to_next (it, 1);
16523
16524 /* If we truncate lines, we are done when the last displayed
16525 glyphs reach past the right margin of the window. */
16526 if (it->truncate_lines_p
16527 && (FRAME_WINDOW_P (it->f)
16528 ? (it->current_x >= it->last_visible_x)
16529 : (it->current_x > it->last_visible_x)))
16530 {
16531 /* Maybe add truncation glyphs. */
16532 if (!FRAME_WINDOW_P (it->f))
16533 {
16534 int i, n;
16535
16536 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
16537 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
16538 break;
16539
16540 for (n = row->used[TEXT_AREA]; i < n; ++i)
16541 {
16542 row->used[TEXT_AREA] = i;
16543 produce_special_glyphs (it, IT_TRUNCATION);
16544 }
16545 }
16546 #ifdef HAVE_WINDOW_SYSTEM
16547 else
16548 {
16549 /* Don't truncate if we can overflow newline into fringe. */
16550 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16551 {
16552 if (!get_next_display_element (it))
16553 {
16554 it->continuation_lines_width = 0;
16555 row->ends_at_zv_p = 1;
16556 row->exact_window_width_line_p = 1;
16557 break;
16558 }
16559 if (ITERATOR_AT_END_OF_LINE_P (it))
16560 {
16561 row->exact_window_width_line_p = 1;
16562 goto at_end_of_line;
16563 }
16564 }
16565 }
16566 #endif /* HAVE_WINDOW_SYSTEM */
16567
16568 row->truncated_on_right_p = 1;
16569 it->continuation_lines_width = 0;
16570 reseat_at_next_visible_line_start (it, 0);
16571 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
16572 it->hpos = hpos_before;
16573 it->current_x = x_before;
16574 break;
16575 }
16576 }
16577
16578 /* If line is not empty and hscrolled, maybe insert truncation glyphs
16579 at the left window margin. */
16580 if (it->first_visible_x
16581 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
16582 {
16583 if (!FRAME_WINDOW_P (it->f))
16584 insert_left_trunc_glyphs (it);
16585 row->truncated_on_left_p = 1;
16586 }
16587
16588 /* If the start of this line is the overlay arrow-position, then
16589 mark this glyph row as the one containing the overlay arrow.
16590 This is clearly a mess with variable size fonts. It would be
16591 better to let it be displayed like cursors under X. */
16592 if ((row->displays_text_p || !overlay_arrow_seen)
16593 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
16594 !NILP (overlay_arrow_string)))
16595 {
16596 /* Overlay arrow in window redisplay is a fringe bitmap. */
16597 if (STRINGP (overlay_arrow_string))
16598 {
16599 struct glyph_row *arrow_row
16600 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
16601 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
16602 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
16603 struct glyph *p = row->glyphs[TEXT_AREA];
16604 struct glyph *p2, *end;
16605
16606 /* Copy the arrow glyphs. */
16607 while (glyph < arrow_end)
16608 *p++ = *glyph++;
16609
16610 /* Throw away padding glyphs. */
16611 p2 = p;
16612 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
16613 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
16614 ++p2;
16615 if (p2 > p)
16616 {
16617 while (p2 < end)
16618 *p++ = *p2++;
16619 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
16620 }
16621 }
16622 else
16623 {
16624 xassert (INTEGERP (overlay_arrow_string));
16625 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
16626 }
16627 overlay_arrow_seen = 1;
16628 }
16629
16630 /* Compute pixel dimensions of this line. */
16631 compute_line_metrics (it);
16632
16633 /* Remember the position at which this line ends. */
16634 row->end = it->current;
16635
16636 /* Record whether this row ends inside an ellipsis. */
16637 row->ends_in_ellipsis_p
16638 = (it->method == GET_FROM_DISPLAY_VECTOR
16639 && it->ellipsis_p);
16640
16641 /* Save fringe bitmaps in this row. */
16642 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
16643 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
16644 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
16645 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
16646
16647 it->left_user_fringe_bitmap = 0;
16648 it->left_user_fringe_face_id = 0;
16649 it->right_user_fringe_bitmap = 0;
16650 it->right_user_fringe_face_id = 0;
16651
16652 /* Maybe set the cursor. */
16653 if (it->w->cursor.vpos < 0
16654 && PT >= MATRIX_ROW_START_CHARPOS (row)
16655 && PT <= MATRIX_ROW_END_CHARPOS (row)
16656 && cursor_row_p (it->w, row))
16657 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
16658
16659 /* Highlight trailing whitespace. */
16660 if (!NILP (Vshow_trailing_whitespace))
16661 highlight_trailing_whitespace (it->f, it->glyph_row);
16662
16663 /* Prepare for the next line. This line starts horizontally at (X
16664 HPOS) = (0 0). Vertical positions are incremented. As a
16665 convenience for the caller, IT->glyph_row is set to the next
16666 row to be used. */
16667 it->current_x = it->hpos = 0;
16668 it->current_y += row->height;
16669 ++it->vpos;
16670 ++it->glyph_row;
16671 it->start = it->current;
16672 return row->displays_text_p;
16673 }
16674
16675
16676 \f
16677 /***********************************************************************
16678 Menu Bar
16679 ***********************************************************************/
16680
16681 /* Redisplay the menu bar in the frame for window W.
16682
16683 The menu bar of X frames that don't have X toolkit support is
16684 displayed in a special window W->frame->menu_bar_window.
16685
16686 The menu bar of terminal frames is treated specially as far as
16687 glyph matrices are concerned. Menu bar lines are not part of
16688 windows, so the update is done directly on the frame matrix rows
16689 for the menu bar. */
16690
16691 static void
16692 display_menu_bar (w)
16693 struct window *w;
16694 {
16695 struct frame *f = XFRAME (WINDOW_FRAME (w));
16696 struct it it;
16697 Lisp_Object items;
16698 int i;
16699
16700 /* Don't do all this for graphical frames. */
16701 #ifdef HAVE_NTGUI
16702 if (FRAME_W32_P (f))
16703 return;
16704 #endif
16705 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
16706 if (FRAME_X_P (f))
16707 return;
16708 #endif
16709 #ifdef MAC_OS
16710 if (FRAME_MAC_P (f))
16711 return;
16712 #endif
16713
16714 #ifdef USE_X_TOOLKIT
16715 xassert (!FRAME_WINDOW_P (f));
16716 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
16717 it.first_visible_x = 0;
16718 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
16719 #else /* not USE_X_TOOLKIT */
16720 if (FRAME_WINDOW_P (f))
16721 {
16722 /* Menu bar lines are displayed in the desired matrix of the
16723 dummy window menu_bar_window. */
16724 struct window *menu_w;
16725 xassert (WINDOWP (f->menu_bar_window));
16726 menu_w = XWINDOW (f->menu_bar_window);
16727 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
16728 MENU_FACE_ID);
16729 it.first_visible_x = 0;
16730 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
16731 }
16732 else
16733 {
16734 /* This is a TTY frame, i.e. character hpos/vpos are used as
16735 pixel x/y. */
16736 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
16737 MENU_FACE_ID);
16738 it.first_visible_x = 0;
16739 it.last_visible_x = FRAME_COLS (f);
16740 }
16741 #endif /* not USE_X_TOOLKIT */
16742
16743 if (! mode_line_inverse_video)
16744 /* Force the menu-bar to be displayed in the default face. */
16745 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
16746
16747 /* Clear all rows of the menu bar. */
16748 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
16749 {
16750 struct glyph_row *row = it.glyph_row + i;
16751 clear_glyph_row (row);
16752 row->enabled_p = 1;
16753 row->full_width_p = 1;
16754 }
16755
16756 /* Display all items of the menu bar. */
16757 items = FRAME_MENU_BAR_ITEMS (it.f);
16758 for (i = 0; i < XVECTOR (items)->size; i += 4)
16759 {
16760 Lisp_Object string;
16761
16762 /* Stop at nil string. */
16763 string = AREF (items, i + 1);
16764 if (NILP (string))
16765 break;
16766
16767 /* Remember where item was displayed. */
16768 AREF (items, i + 3) = make_number (it.hpos);
16769
16770 /* Display the item, pad with one space. */
16771 if (it.current_x < it.last_visible_x)
16772 display_string (NULL, string, Qnil, 0, 0, &it,
16773 SCHARS (string) + 1, 0, 0, -1);
16774 }
16775
16776 /* Fill out the line with spaces. */
16777 if (it.current_x < it.last_visible_x)
16778 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
16779
16780 /* Compute the total height of the lines. */
16781 compute_line_metrics (&it);
16782 }
16783
16784
16785 \f
16786 /***********************************************************************
16787 Mode Line
16788 ***********************************************************************/
16789
16790 /* Redisplay mode lines in the window tree whose root is WINDOW. If
16791 FORCE is non-zero, redisplay mode lines unconditionally.
16792 Otherwise, redisplay only mode lines that are garbaged. Value is
16793 the number of windows whose mode lines were redisplayed. */
16794
16795 static int
16796 redisplay_mode_lines (window, force)
16797 Lisp_Object window;
16798 int force;
16799 {
16800 int nwindows = 0;
16801
16802 while (!NILP (window))
16803 {
16804 struct window *w = XWINDOW (window);
16805
16806 if (WINDOWP (w->hchild))
16807 nwindows += redisplay_mode_lines (w->hchild, force);
16808 else if (WINDOWP (w->vchild))
16809 nwindows += redisplay_mode_lines (w->vchild, force);
16810 else if (force
16811 || FRAME_GARBAGED_P (XFRAME (w->frame))
16812 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
16813 {
16814 struct text_pos lpoint;
16815 struct buffer *old = current_buffer;
16816
16817 /* Set the window's buffer for the mode line display. */
16818 SET_TEXT_POS (lpoint, PT, PT_BYTE);
16819 set_buffer_internal_1 (XBUFFER (w->buffer));
16820
16821 /* Point refers normally to the selected window. For any
16822 other window, set up appropriate value. */
16823 if (!EQ (window, selected_window))
16824 {
16825 struct text_pos pt;
16826
16827 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
16828 if (CHARPOS (pt) < BEGV)
16829 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
16830 else if (CHARPOS (pt) > (ZV - 1))
16831 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
16832 else
16833 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
16834 }
16835
16836 /* Display mode lines. */
16837 clear_glyph_matrix (w->desired_matrix);
16838 if (display_mode_lines (w))
16839 {
16840 ++nwindows;
16841 w->must_be_updated_p = 1;
16842 }
16843
16844 /* Restore old settings. */
16845 set_buffer_internal_1 (old);
16846 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
16847 }
16848
16849 window = w->next;
16850 }
16851
16852 return nwindows;
16853 }
16854
16855
16856 /* Display the mode and/or top line of window W. Value is the number
16857 of mode lines displayed. */
16858
16859 static int
16860 display_mode_lines (w)
16861 struct window *w;
16862 {
16863 Lisp_Object old_selected_window, old_selected_frame;
16864 int n = 0;
16865
16866 old_selected_frame = selected_frame;
16867 selected_frame = w->frame;
16868 old_selected_window = selected_window;
16869 XSETWINDOW (selected_window, w);
16870
16871 /* These will be set while the mode line specs are processed. */
16872 line_number_displayed = 0;
16873 w->column_number_displayed = Qnil;
16874
16875 if (WINDOW_WANTS_MODELINE_P (w))
16876 {
16877 struct window *sel_w = XWINDOW (old_selected_window);
16878
16879 /* Select mode line face based on the real selected window. */
16880 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
16881 current_buffer->mode_line_format);
16882 ++n;
16883 }
16884
16885 if (WINDOW_WANTS_HEADER_LINE_P (w))
16886 {
16887 display_mode_line (w, HEADER_LINE_FACE_ID,
16888 current_buffer->header_line_format);
16889 ++n;
16890 }
16891
16892 selected_frame = old_selected_frame;
16893 selected_window = old_selected_window;
16894 return n;
16895 }
16896
16897
16898 /* Display mode or top line of window W. FACE_ID specifies which line
16899 to display; it is either MODE_LINE_FACE_ID or HEADER_LINE_FACE_ID.
16900 FORMAT is the mode line format to display. Value is the pixel
16901 height of the mode line displayed. */
16902
16903 static int
16904 display_mode_line (w, face_id, format)
16905 struct window *w;
16906 enum face_id face_id;
16907 Lisp_Object format;
16908 {
16909 struct it it;
16910 struct face *face;
16911 int count = SPECPDL_INDEX ();
16912
16913 init_iterator (&it, w, -1, -1, NULL, face_id);
16914 /* Don't extend on a previously drawn mode-line.
16915 This may happen if called from pos_visible_p. */
16916 it.glyph_row->enabled_p = 0;
16917 prepare_desired_row (it.glyph_row);
16918
16919 it.glyph_row->mode_line_p = 1;
16920
16921 if (! mode_line_inverse_video)
16922 /* Force the mode-line to be displayed in the default face. */
16923 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
16924
16925 record_unwind_protect (unwind_format_mode_line,
16926 format_mode_line_unwind_data (NULL, 0));
16927
16928 mode_line_target = MODE_LINE_DISPLAY;
16929
16930 /* Temporarily make frame's keyboard the current kboard so that
16931 kboard-local variables in the mode_line_format will get the right
16932 values. */
16933 push_kboard (FRAME_KBOARD (it.f));
16934 record_unwind_save_match_data ();
16935 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
16936 pop_kboard ();
16937
16938 unbind_to (count, Qnil);
16939
16940 /* Fill up with spaces. */
16941 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
16942
16943 compute_line_metrics (&it);
16944 it.glyph_row->full_width_p = 1;
16945 it.glyph_row->continued_p = 0;
16946 it.glyph_row->truncated_on_left_p = 0;
16947 it.glyph_row->truncated_on_right_p = 0;
16948
16949 /* Make a 3D mode-line have a shadow at its right end. */
16950 face = FACE_FROM_ID (it.f, face_id);
16951 extend_face_to_end_of_line (&it);
16952 if (face->box != FACE_NO_BOX)
16953 {
16954 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
16955 + it.glyph_row->used[TEXT_AREA] - 1);
16956 last->right_box_line_p = 1;
16957 }
16958
16959 return it.glyph_row->height;
16960 }
16961
16962 /* Move element ELT in LIST to the front of LIST.
16963 Return the updated list. */
16964
16965 static Lisp_Object
16966 move_elt_to_front (elt, list)
16967 Lisp_Object elt, list;
16968 {
16969 register Lisp_Object tail, prev;
16970 register Lisp_Object tem;
16971
16972 tail = list;
16973 prev = Qnil;
16974 while (CONSP (tail))
16975 {
16976 tem = XCAR (tail);
16977
16978 if (EQ (elt, tem))
16979 {
16980 /* Splice out the link TAIL. */
16981 if (NILP (prev))
16982 list = XCDR (tail);
16983 else
16984 Fsetcdr (prev, XCDR (tail));
16985
16986 /* Now make it the first. */
16987 Fsetcdr (tail, list);
16988 return tail;
16989 }
16990 else
16991 prev = tail;
16992 tail = XCDR (tail);
16993 QUIT;
16994 }
16995
16996 /* Not found--return unchanged LIST. */
16997 return list;
16998 }
16999
17000 /* Contribute ELT to the mode line for window IT->w. How it
17001 translates into text depends on its data type.
17002
17003 IT describes the display environment in which we display, as usual.
17004
17005 DEPTH is the depth in recursion. It is used to prevent
17006 infinite recursion here.
17007
17008 FIELD_WIDTH is the number of characters the display of ELT should
17009 occupy in the mode line, and PRECISION is the maximum number of
17010 characters to display from ELT's representation. See
17011 display_string for details.
17012
17013 Returns the hpos of the end of the text generated by ELT.
17014
17015 PROPS is a property list to add to any string we encounter.
17016
17017 If RISKY is nonzero, remove (disregard) any properties in any string
17018 we encounter, and ignore :eval and :propertize.
17019
17020 The global variable `mode_line_target' determines whether the
17021 output is passed to `store_mode_line_noprop',
17022 `store_mode_line_string', or `display_string'. */
17023
17024 static int
17025 display_mode_element (it, depth, field_width, precision, elt, props, risky)
17026 struct it *it;
17027 int depth;
17028 int field_width, precision;
17029 Lisp_Object elt, props;
17030 int risky;
17031 {
17032 int n = 0, field, prec;
17033 int literal = 0;
17034
17035 tail_recurse:
17036 if (depth > 100)
17037 elt = build_string ("*too-deep*");
17038
17039 depth++;
17040
17041 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
17042 {
17043 case Lisp_String:
17044 {
17045 /* A string: output it and check for %-constructs within it. */
17046 unsigned char c;
17047 int offset = 0;
17048
17049 if (SCHARS (elt) > 0
17050 && (!NILP (props) || risky))
17051 {
17052 Lisp_Object oprops, aelt;
17053 oprops = Ftext_properties_at (make_number (0), elt);
17054
17055 /* If the starting string's properties are not what
17056 we want, translate the string. Also, if the string
17057 is risky, do that anyway. */
17058
17059 if (NILP (Fequal (props, oprops)) || risky)
17060 {
17061 /* If the starting string has properties,
17062 merge the specified ones onto the existing ones. */
17063 if (! NILP (oprops) && !risky)
17064 {
17065 Lisp_Object tem;
17066
17067 oprops = Fcopy_sequence (oprops);
17068 tem = props;
17069 while (CONSP (tem))
17070 {
17071 oprops = Fplist_put (oprops, XCAR (tem),
17072 XCAR (XCDR (tem)));
17073 tem = XCDR (XCDR (tem));
17074 }
17075 props = oprops;
17076 }
17077
17078 aelt = Fassoc (elt, mode_line_proptrans_alist);
17079 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
17080 {
17081 /* AELT is what we want. Move it to the front
17082 without consing. */
17083 elt = XCAR (aelt);
17084 mode_line_proptrans_alist
17085 = move_elt_to_front (aelt, mode_line_proptrans_alist);
17086 }
17087 else
17088 {
17089 Lisp_Object tem;
17090
17091 /* If AELT has the wrong props, it is useless.
17092 so get rid of it. */
17093 if (! NILP (aelt))
17094 mode_line_proptrans_alist
17095 = Fdelq (aelt, mode_line_proptrans_alist);
17096
17097 elt = Fcopy_sequence (elt);
17098 Fset_text_properties (make_number (0), Flength (elt),
17099 props, elt);
17100 /* Add this item to mode_line_proptrans_alist. */
17101 mode_line_proptrans_alist
17102 = Fcons (Fcons (elt, props),
17103 mode_line_proptrans_alist);
17104 /* Truncate mode_line_proptrans_alist
17105 to at most 50 elements. */
17106 tem = Fnthcdr (make_number (50),
17107 mode_line_proptrans_alist);
17108 if (! NILP (tem))
17109 XSETCDR (tem, Qnil);
17110 }
17111 }
17112 }
17113
17114 offset = 0;
17115
17116 if (literal)
17117 {
17118 prec = precision - n;
17119 switch (mode_line_target)
17120 {
17121 case MODE_LINE_NOPROP:
17122 case MODE_LINE_TITLE:
17123 n += store_mode_line_noprop (SDATA (elt), -1, prec);
17124 break;
17125 case MODE_LINE_STRING:
17126 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
17127 break;
17128 case MODE_LINE_DISPLAY:
17129 n += display_string (NULL, elt, Qnil, 0, 0, it,
17130 0, prec, 0, STRING_MULTIBYTE (elt));
17131 break;
17132 }
17133
17134 break;
17135 }
17136
17137 /* Handle the non-literal case. */
17138
17139 while ((precision <= 0 || n < precision)
17140 && SREF (elt, offset) != 0
17141 && (mode_line_target != MODE_LINE_DISPLAY
17142 || it->current_x < it->last_visible_x))
17143 {
17144 int last_offset = offset;
17145
17146 /* Advance to end of string or next format specifier. */
17147 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
17148 ;
17149
17150 if (offset - 1 != last_offset)
17151 {
17152 int nchars, nbytes;
17153
17154 /* Output to end of string or up to '%'. Field width
17155 is length of string. Don't output more than
17156 PRECISION allows us. */
17157 offset--;
17158
17159 prec = c_string_width (SDATA (elt) + last_offset,
17160 offset - last_offset, precision - n,
17161 &nchars, &nbytes);
17162
17163 switch (mode_line_target)
17164 {
17165 case MODE_LINE_NOPROP:
17166 case MODE_LINE_TITLE:
17167 n += store_mode_line_noprop (SDATA (elt) + last_offset, 0, prec);
17168 break;
17169 case MODE_LINE_STRING:
17170 {
17171 int bytepos = last_offset;
17172 int charpos = string_byte_to_char (elt, bytepos);
17173 int endpos = (precision <= 0
17174 ? string_byte_to_char (elt, offset)
17175 : charpos + nchars);
17176
17177 n += store_mode_line_string (NULL,
17178 Fsubstring (elt, make_number (charpos),
17179 make_number (endpos)),
17180 0, 0, 0, Qnil);
17181 }
17182 break;
17183 case MODE_LINE_DISPLAY:
17184 {
17185 int bytepos = last_offset;
17186 int charpos = string_byte_to_char (elt, bytepos);
17187
17188 if (precision <= 0)
17189 nchars = string_byte_to_char (elt, offset) - charpos;
17190 n += display_string (NULL, elt, Qnil, 0, charpos,
17191 it, 0, nchars, 0,
17192 STRING_MULTIBYTE (elt));
17193 }
17194 break;
17195 }
17196 }
17197 else /* c == '%' */
17198 {
17199 int percent_position = offset;
17200
17201 /* Get the specified minimum width. Zero means
17202 don't pad. */
17203 field = 0;
17204 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
17205 field = field * 10 + c - '0';
17206
17207 /* Don't pad beyond the total padding allowed. */
17208 if (field_width - n > 0 && field > field_width - n)
17209 field = field_width - n;
17210
17211 /* Note that either PRECISION <= 0 or N < PRECISION. */
17212 prec = precision - n;
17213
17214 if (c == 'M')
17215 n += display_mode_element (it, depth, field, prec,
17216 Vglobal_mode_string, props,
17217 risky);
17218 else if (c != 0)
17219 {
17220 int multibyte;
17221 int bytepos, charpos;
17222 unsigned char *spec;
17223
17224 bytepos = percent_position;
17225 charpos = (STRING_MULTIBYTE (elt)
17226 ? string_byte_to_char (elt, bytepos)
17227 : bytepos);
17228
17229 spec
17230 = decode_mode_spec (it->w, c, field, prec, &multibyte);
17231
17232 switch (mode_line_target)
17233 {
17234 case MODE_LINE_NOPROP:
17235 case MODE_LINE_TITLE:
17236 n += store_mode_line_noprop (spec, field, prec);
17237 break;
17238 case MODE_LINE_STRING:
17239 {
17240 int len = strlen (spec);
17241 Lisp_Object tem = make_string (spec, len);
17242 props = Ftext_properties_at (make_number (charpos), elt);
17243 /* Should only keep face property in props */
17244 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
17245 }
17246 break;
17247 case MODE_LINE_DISPLAY:
17248 {
17249 int nglyphs_before, nwritten;
17250
17251 nglyphs_before = it->glyph_row->used[TEXT_AREA];
17252 nwritten = display_string (spec, Qnil, elt,
17253 charpos, 0, it,
17254 field, prec, 0,
17255 multibyte);
17256
17257 /* Assign to the glyphs written above the
17258 string where the `%x' came from, position
17259 of the `%'. */
17260 if (nwritten > 0)
17261 {
17262 struct glyph *glyph
17263 = (it->glyph_row->glyphs[TEXT_AREA]
17264 + nglyphs_before);
17265 int i;
17266
17267 for (i = 0; i < nwritten; ++i)
17268 {
17269 glyph[i].object = elt;
17270 glyph[i].charpos = charpos;
17271 }
17272
17273 n += nwritten;
17274 }
17275 }
17276 break;
17277 }
17278 }
17279 else /* c == 0 */
17280 break;
17281 }
17282 }
17283 }
17284 break;
17285
17286 case Lisp_Symbol:
17287 /* A symbol: process the value of the symbol recursively
17288 as if it appeared here directly. Avoid error if symbol void.
17289 Special case: if value of symbol is a string, output the string
17290 literally. */
17291 {
17292 register Lisp_Object tem;
17293
17294 /* If the variable is not marked as risky to set
17295 then its contents are risky to use. */
17296 if (NILP (Fget (elt, Qrisky_local_variable)))
17297 risky = 1;
17298
17299 tem = Fboundp (elt);
17300 if (!NILP (tem))
17301 {
17302 tem = Fsymbol_value (elt);
17303 /* If value is a string, output that string literally:
17304 don't check for % within it. */
17305 if (STRINGP (tem))
17306 literal = 1;
17307
17308 if (!EQ (tem, elt))
17309 {
17310 /* Give up right away for nil or t. */
17311 elt = tem;
17312 goto tail_recurse;
17313 }
17314 }
17315 }
17316 break;
17317
17318 case Lisp_Cons:
17319 {
17320 register Lisp_Object car, tem;
17321
17322 /* A cons cell: five distinct cases.
17323 If first element is :eval or :propertize, do something special.
17324 If first element is a string or a cons, process all the elements
17325 and effectively concatenate them.
17326 If first element is a negative number, truncate displaying cdr to
17327 at most that many characters. If positive, pad (with spaces)
17328 to at least that many characters.
17329 If first element is a symbol, process the cadr or caddr recursively
17330 according to whether the symbol's value is non-nil or nil. */
17331 car = XCAR (elt);
17332 if (EQ (car, QCeval))
17333 {
17334 /* An element of the form (:eval FORM) means evaluate FORM
17335 and use the result as mode line elements. */
17336
17337 if (risky)
17338 break;
17339
17340 if (CONSP (XCDR (elt)))
17341 {
17342 Lisp_Object spec;
17343 spec = safe_eval (XCAR (XCDR (elt)));
17344 n += display_mode_element (it, depth, field_width - n,
17345 precision - n, spec, props,
17346 risky);
17347 }
17348 }
17349 else if (EQ (car, QCpropertize))
17350 {
17351 /* An element of the form (:propertize ELT PROPS...)
17352 means display ELT but applying properties PROPS. */
17353
17354 if (risky)
17355 break;
17356
17357 if (CONSP (XCDR (elt)))
17358 n += display_mode_element (it, depth, field_width - n,
17359 precision - n, XCAR (XCDR (elt)),
17360 XCDR (XCDR (elt)), risky);
17361 }
17362 else if (SYMBOLP (car))
17363 {
17364 tem = Fboundp (car);
17365 elt = XCDR (elt);
17366 if (!CONSP (elt))
17367 goto invalid;
17368 /* elt is now the cdr, and we know it is a cons cell.
17369 Use its car if CAR has a non-nil value. */
17370 if (!NILP (tem))
17371 {
17372 tem = Fsymbol_value (car);
17373 if (!NILP (tem))
17374 {
17375 elt = XCAR (elt);
17376 goto tail_recurse;
17377 }
17378 }
17379 /* Symbol's value is nil (or symbol is unbound)
17380 Get the cddr of the original list
17381 and if possible find the caddr and use that. */
17382 elt = XCDR (elt);
17383 if (NILP (elt))
17384 break;
17385 else if (!CONSP (elt))
17386 goto invalid;
17387 elt = XCAR (elt);
17388 goto tail_recurse;
17389 }
17390 else if (INTEGERP (car))
17391 {
17392 register int lim = XINT (car);
17393 elt = XCDR (elt);
17394 if (lim < 0)
17395 {
17396 /* Negative int means reduce maximum width. */
17397 if (precision <= 0)
17398 precision = -lim;
17399 else
17400 precision = min (precision, -lim);
17401 }
17402 else if (lim > 0)
17403 {
17404 /* Padding specified. Don't let it be more than
17405 current maximum. */
17406 if (precision > 0)
17407 lim = min (precision, lim);
17408
17409 /* If that's more padding than already wanted, queue it.
17410 But don't reduce padding already specified even if
17411 that is beyond the current truncation point. */
17412 field_width = max (lim, field_width);
17413 }
17414 goto tail_recurse;
17415 }
17416 else if (STRINGP (car) || CONSP (car))
17417 {
17418 register int limit = 50;
17419 /* Limit is to protect against circular lists. */
17420 while (CONSP (elt)
17421 && --limit > 0
17422 && (precision <= 0 || n < precision))
17423 {
17424 n += display_mode_element (it, depth,
17425 /* Do padding only after the last
17426 element in the list. */
17427 (! CONSP (XCDR (elt))
17428 ? field_width - n
17429 : 0),
17430 precision - n, XCAR (elt),
17431 props, risky);
17432 elt = XCDR (elt);
17433 }
17434 }
17435 }
17436 break;
17437
17438 default:
17439 invalid:
17440 elt = build_string ("*invalid*");
17441 goto tail_recurse;
17442 }
17443
17444 /* Pad to FIELD_WIDTH. */
17445 if (field_width > 0 && n < field_width)
17446 {
17447 switch (mode_line_target)
17448 {
17449 case MODE_LINE_NOPROP:
17450 case MODE_LINE_TITLE:
17451 n += store_mode_line_noprop ("", field_width - n, 0);
17452 break;
17453 case MODE_LINE_STRING:
17454 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
17455 break;
17456 case MODE_LINE_DISPLAY:
17457 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
17458 0, 0, 0);
17459 break;
17460 }
17461 }
17462
17463 return n;
17464 }
17465
17466 /* Store a mode-line string element in mode_line_string_list.
17467
17468 If STRING is non-null, display that C string. Otherwise, the Lisp
17469 string LISP_STRING is displayed.
17470
17471 FIELD_WIDTH is the minimum number of output glyphs to produce.
17472 If STRING has fewer characters than FIELD_WIDTH, pad to the right
17473 with spaces. FIELD_WIDTH <= 0 means don't pad.
17474
17475 PRECISION is the maximum number of characters to output from
17476 STRING. PRECISION <= 0 means don't truncate the string.
17477
17478 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
17479 properties to the string.
17480
17481 PROPS are the properties to add to the string.
17482 The mode_line_string_face face property is always added to the string.
17483 */
17484
17485 static int
17486 store_mode_line_string (string, lisp_string, copy_string, field_width, precision, props)
17487 char *string;
17488 Lisp_Object lisp_string;
17489 int copy_string;
17490 int field_width;
17491 int precision;
17492 Lisp_Object props;
17493 {
17494 int len;
17495 int n = 0;
17496
17497 if (string != NULL)
17498 {
17499 len = strlen (string);
17500 if (precision > 0 && len > precision)
17501 len = precision;
17502 lisp_string = make_string (string, len);
17503 if (NILP (props))
17504 props = mode_line_string_face_prop;
17505 else if (!NILP (mode_line_string_face))
17506 {
17507 Lisp_Object face = Fplist_get (props, Qface);
17508 props = Fcopy_sequence (props);
17509 if (NILP (face))
17510 face = mode_line_string_face;
17511 else
17512 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
17513 props = Fplist_put (props, Qface, face);
17514 }
17515 Fadd_text_properties (make_number (0), make_number (len),
17516 props, lisp_string);
17517 }
17518 else
17519 {
17520 len = XFASTINT (Flength (lisp_string));
17521 if (precision > 0 && len > precision)
17522 {
17523 len = precision;
17524 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
17525 precision = -1;
17526 }
17527 if (!NILP (mode_line_string_face))
17528 {
17529 Lisp_Object face;
17530 if (NILP (props))
17531 props = Ftext_properties_at (make_number (0), lisp_string);
17532 face = Fplist_get (props, Qface);
17533 if (NILP (face))
17534 face = mode_line_string_face;
17535 else
17536 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
17537 props = Fcons (Qface, Fcons (face, Qnil));
17538 if (copy_string)
17539 lisp_string = Fcopy_sequence (lisp_string);
17540 }
17541 if (!NILP (props))
17542 Fadd_text_properties (make_number (0), make_number (len),
17543 props, lisp_string);
17544 }
17545
17546 if (len > 0)
17547 {
17548 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
17549 n += len;
17550 }
17551
17552 if (field_width > len)
17553 {
17554 field_width -= len;
17555 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
17556 if (!NILP (props))
17557 Fadd_text_properties (make_number (0), make_number (field_width),
17558 props, lisp_string);
17559 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
17560 n += field_width;
17561 }
17562
17563 return n;
17564 }
17565
17566
17567 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
17568 1, 4, 0,
17569 doc: /* Format a string out of a mode line format specification.
17570 First arg FORMAT specifies the mode line format (see `mode-line-format'
17571 for details) to use.
17572
17573 Optional second arg FACE specifies the face property to put
17574 on all characters for which no face is specified.
17575 The value t means whatever face the window's mode line currently uses
17576 \(either `mode-line' or `mode-line-inactive', depending).
17577 A value of nil means the default is no face property.
17578 If FACE is an integer, the value string has no text properties.
17579
17580 Optional third and fourth args WINDOW and BUFFER specify the window
17581 and buffer to use as the context for the formatting (defaults
17582 are the selected window and the window's buffer). */)
17583 (format, face, window, buffer)
17584 Lisp_Object format, face, window, buffer;
17585 {
17586 struct it it;
17587 int len;
17588 struct window *w;
17589 struct buffer *old_buffer = NULL;
17590 int face_id = -1;
17591 int no_props = INTEGERP (face);
17592 int count = SPECPDL_INDEX ();
17593 Lisp_Object str;
17594 int string_start = 0;
17595
17596 if (NILP (window))
17597 window = selected_window;
17598 CHECK_WINDOW (window);
17599 w = XWINDOW (window);
17600
17601 if (NILP (buffer))
17602 buffer = w->buffer;
17603 CHECK_BUFFER (buffer);
17604
17605 /* Make formatting the modeline a non-op when noninteractive, otherwise
17606 there will be problems later caused by a partially initialized frame. */
17607 if (NILP (format) || noninteractive)
17608 return empty_unibyte_string;
17609
17610 if (no_props)
17611 face = Qnil;
17612
17613 if (!NILP (face))
17614 {
17615 if (EQ (face, Qt))
17616 face = (EQ (window, selected_window) ? Qmode_line : Qmode_line_inactive);
17617 face_id = lookup_named_face (XFRAME (WINDOW_FRAME (w)), face, 0);
17618 }
17619
17620 if (face_id < 0)
17621 face_id = DEFAULT_FACE_ID;
17622
17623 if (XBUFFER (buffer) != current_buffer)
17624 old_buffer = current_buffer;
17625
17626 /* Save things including mode_line_proptrans_alist,
17627 and set that to nil so that we don't alter the outer value. */
17628 record_unwind_protect (unwind_format_mode_line,
17629 format_mode_line_unwind_data (old_buffer, 1));
17630 mode_line_proptrans_alist = Qnil;
17631
17632 if (old_buffer)
17633 set_buffer_internal_1 (XBUFFER (buffer));
17634
17635 init_iterator (&it, w, -1, -1, NULL, face_id);
17636
17637 if (no_props)
17638 {
17639 mode_line_target = MODE_LINE_NOPROP;
17640 mode_line_string_face_prop = Qnil;
17641 mode_line_string_list = Qnil;
17642 string_start = MODE_LINE_NOPROP_LEN (0);
17643 }
17644 else
17645 {
17646 mode_line_target = MODE_LINE_STRING;
17647 mode_line_string_list = Qnil;
17648 mode_line_string_face = face;
17649 mode_line_string_face_prop
17650 = (NILP (face) ? Qnil : Fcons (Qface, Fcons (face, Qnil)));
17651 }
17652
17653 push_kboard (FRAME_KBOARD (it.f));
17654 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
17655 pop_kboard ();
17656
17657 if (no_props)
17658 {
17659 len = MODE_LINE_NOPROP_LEN (string_start);
17660 str = make_string (mode_line_noprop_buf + string_start, len);
17661 }
17662 else
17663 {
17664 mode_line_string_list = Fnreverse (mode_line_string_list);
17665 str = Fmapconcat (intern ("identity"), mode_line_string_list,
17666 empty_unibyte_string);
17667 }
17668
17669 unbind_to (count, Qnil);
17670 return str;
17671 }
17672
17673 /* Write a null-terminated, right justified decimal representation of
17674 the positive integer D to BUF using a minimal field width WIDTH. */
17675
17676 static void
17677 pint2str (buf, width, d)
17678 register char *buf;
17679 register int width;
17680 register int d;
17681 {
17682 register char *p = buf;
17683
17684 if (d <= 0)
17685 *p++ = '0';
17686 else
17687 {
17688 while (d > 0)
17689 {
17690 *p++ = d % 10 + '0';
17691 d /= 10;
17692 }
17693 }
17694
17695 for (width -= (int) (p - buf); width > 0; --width)
17696 *p++ = ' ';
17697 *p-- = '\0';
17698 while (p > buf)
17699 {
17700 d = *buf;
17701 *buf++ = *p;
17702 *p-- = d;
17703 }
17704 }
17705
17706 /* Write a null-terminated, right justified decimal and "human
17707 readable" representation of the nonnegative integer D to BUF using
17708 a minimal field width WIDTH. D should be smaller than 999.5e24. */
17709
17710 static const char power_letter[] =
17711 {
17712 0, /* not used */
17713 'k', /* kilo */
17714 'M', /* mega */
17715 'G', /* giga */
17716 'T', /* tera */
17717 'P', /* peta */
17718 'E', /* exa */
17719 'Z', /* zetta */
17720 'Y' /* yotta */
17721 };
17722
17723 static void
17724 pint2hrstr (buf, width, d)
17725 char *buf;
17726 int width;
17727 int d;
17728 {
17729 /* We aim to represent the nonnegative integer D as
17730 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
17731 int quotient = d;
17732 int remainder = 0;
17733 /* -1 means: do not use TENTHS. */
17734 int tenths = -1;
17735 int exponent = 0;
17736
17737 /* Length of QUOTIENT.TENTHS as a string. */
17738 int length;
17739
17740 char * psuffix;
17741 char * p;
17742
17743 if (1000 <= quotient)
17744 {
17745 /* Scale to the appropriate EXPONENT. */
17746 do
17747 {
17748 remainder = quotient % 1000;
17749 quotient /= 1000;
17750 exponent++;
17751 }
17752 while (1000 <= quotient);
17753
17754 /* Round to nearest and decide whether to use TENTHS or not. */
17755 if (quotient <= 9)
17756 {
17757 tenths = remainder / 100;
17758 if (50 <= remainder % 100)
17759 {
17760 if (tenths < 9)
17761 tenths++;
17762 else
17763 {
17764 quotient++;
17765 if (quotient == 10)
17766 tenths = -1;
17767 else
17768 tenths = 0;
17769 }
17770 }
17771 }
17772 else
17773 if (500 <= remainder)
17774 {
17775 if (quotient < 999)
17776 quotient++;
17777 else
17778 {
17779 quotient = 1;
17780 exponent++;
17781 tenths = 0;
17782 }
17783 }
17784 }
17785
17786 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
17787 if (tenths == -1 && quotient <= 99)
17788 if (quotient <= 9)
17789 length = 1;
17790 else
17791 length = 2;
17792 else
17793 length = 3;
17794 p = psuffix = buf + max (width, length);
17795
17796 /* Print EXPONENT. */
17797 if (exponent)
17798 *psuffix++ = power_letter[exponent];
17799 *psuffix = '\0';
17800
17801 /* Print TENTHS. */
17802 if (tenths >= 0)
17803 {
17804 *--p = '0' + tenths;
17805 *--p = '.';
17806 }
17807
17808 /* Print QUOTIENT. */
17809 do
17810 {
17811 int digit = quotient % 10;
17812 *--p = '0' + digit;
17813 }
17814 while ((quotient /= 10) != 0);
17815
17816 /* Print leading spaces. */
17817 while (buf < p)
17818 *--p = ' ';
17819 }
17820
17821 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
17822 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
17823 type of CODING_SYSTEM. Return updated pointer into BUF. */
17824
17825 static unsigned char invalid_eol_type[] = "(*invalid*)";
17826
17827 static char *
17828 decode_mode_spec_coding (coding_system, buf, eol_flag)
17829 Lisp_Object coding_system;
17830 register char *buf;
17831 int eol_flag;
17832 {
17833 Lisp_Object val;
17834 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
17835 const unsigned char *eol_str;
17836 int eol_str_len;
17837 /* The EOL conversion we are using. */
17838 Lisp_Object eoltype;
17839
17840 val = CODING_SYSTEM_SPEC (coding_system);
17841 eoltype = Qnil;
17842
17843 if (!VECTORP (val)) /* Not yet decided. */
17844 {
17845 if (multibyte)
17846 *buf++ = '-';
17847 if (eol_flag)
17848 eoltype = eol_mnemonic_undecided;
17849 /* Don't mention EOL conversion if it isn't decided. */
17850 }
17851 else
17852 {
17853 Lisp_Object attrs;
17854 Lisp_Object eolvalue;
17855
17856 attrs = AREF (val, 0);
17857 eolvalue = AREF (val, 2);
17858
17859 if (multibyte)
17860 *buf++ = XFASTINT (CODING_ATTR_MNEMONIC (attrs));
17861
17862 if (eol_flag)
17863 {
17864 /* The EOL conversion that is normal on this system. */
17865
17866 if (NILP (eolvalue)) /* Not yet decided. */
17867 eoltype = eol_mnemonic_undecided;
17868 else if (VECTORP (eolvalue)) /* Not yet decided. */
17869 eoltype = eol_mnemonic_undecided;
17870 else /* eolvalue is Qunix, Qdos, or Qmac. */
17871 eoltype = (EQ (eolvalue, Qunix)
17872 ? eol_mnemonic_unix
17873 : (EQ (eolvalue, Qdos) == 1
17874 ? eol_mnemonic_dos : eol_mnemonic_mac));
17875 }
17876 }
17877
17878 if (eol_flag)
17879 {
17880 /* Mention the EOL conversion if it is not the usual one. */
17881 if (STRINGP (eoltype))
17882 {
17883 eol_str = SDATA (eoltype);
17884 eol_str_len = SBYTES (eoltype);
17885 }
17886 else if (CHARACTERP (eoltype))
17887 {
17888 unsigned char *tmp = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
17889 eol_str_len = CHAR_STRING (XINT (eoltype), tmp);
17890 eol_str = tmp;
17891 }
17892 else
17893 {
17894 eol_str = invalid_eol_type;
17895 eol_str_len = sizeof (invalid_eol_type) - 1;
17896 }
17897 bcopy (eol_str, buf, eol_str_len);
17898 buf += eol_str_len;
17899 }
17900
17901 return buf;
17902 }
17903
17904 /* Return a string for the output of a mode line %-spec for window W,
17905 generated by character C. PRECISION >= 0 means don't return a
17906 string longer than that value. FIELD_WIDTH > 0 means pad the
17907 string returned with spaces to that value. Return 1 in *MULTIBYTE
17908 if the result is multibyte text.
17909
17910 Note we operate on the current buffer for most purposes,
17911 the exception being w->base_line_pos. */
17912
17913 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
17914
17915 static char *
17916 decode_mode_spec (w, c, field_width, precision, multibyte)
17917 struct window *w;
17918 register int c;
17919 int field_width, precision;
17920 int *multibyte;
17921 {
17922 Lisp_Object obj;
17923 struct frame *f = XFRAME (WINDOW_FRAME (w));
17924 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
17925 struct buffer *b = current_buffer;
17926
17927 obj = Qnil;
17928 *multibyte = 0;
17929
17930 switch (c)
17931 {
17932 case '*':
17933 if (!NILP (b->read_only))
17934 return "%";
17935 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
17936 return "*";
17937 return "-";
17938
17939 case '+':
17940 /* This differs from %* only for a modified read-only buffer. */
17941 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
17942 return "*";
17943 if (!NILP (b->read_only))
17944 return "%";
17945 return "-";
17946
17947 case '&':
17948 /* This differs from %* in ignoring read-only-ness. */
17949 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
17950 return "*";
17951 return "-";
17952
17953 case '%':
17954 return "%";
17955
17956 case '[':
17957 {
17958 int i;
17959 char *p;
17960
17961 if (command_loop_level > 5)
17962 return "[[[... ";
17963 p = decode_mode_spec_buf;
17964 for (i = 0; i < command_loop_level; i++)
17965 *p++ = '[';
17966 *p = 0;
17967 return decode_mode_spec_buf;
17968 }
17969
17970 case ']':
17971 {
17972 int i;
17973 char *p;
17974
17975 if (command_loop_level > 5)
17976 return " ...]]]";
17977 p = decode_mode_spec_buf;
17978 for (i = 0; i < command_loop_level; i++)
17979 *p++ = ']';
17980 *p = 0;
17981 return decode_mode_spec_buf;
17982 }
17983
17984 case '-':
17985 {
17986 register int i;
17987
17988 /* Let lots_of_dashes be a string of infinite length. */
17989 if (mode_line_target == MODE_LINE_NOPROP ||
17990 mode_line_target == MODE_LINE_STRING)
17991 return "--";
17992 if (field_width <= 0
17993 || field_width > sizeof (lots_of_dashes))
17994 {
17995 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
17996 decode_mode_spec_buf[i] = '-';
17997 decode_mode_spec_buf[i] = '\0';
17998 return decode_mode_spec_buf;
17999 }
18000 else
18001 return lots_of_dashes;
18002 }
18003
18004 case 'b':
18005 obj = b->name;
18006 break;
18007
18008 case 'c':
18009 /* %c and %l are ignored in `frame-title-format'.
18010 (In redisplay_internal, the frame title is drawn _before_ the
18011 windows are updated, so the stuff which depends on actual
18012 window contents (such as %l) may fail to render properly, or
18013 even crash emacs.) */
18014 if (mode_line_target == MODE_LINE_TITLE)
18015 return "";
18016 else
18017 {
18018 int col = (int) current_column (); /* iftc */
18019 w->column_number_displayed = make_number (col);
18020 pint2str (decode_mode_spec_buf, field_width, col);
18021 return decode_mode_spec_buf;
18022 }
18023
18024 case 'e':
18025 #ifndef SYSTEM_MALLOC
18026 {
18027 if (NILP (Vmemory_full))
18028 return "";
18029 else
18030 return "!MEM FULL! ";
18031 }
18032 #else
18033 return "";
18034 #endif
18035
18036 case 'F':
18037 /* %F displays the frame name. */
18038 if (!NILP (f->title))
18039 return (char *) SDATA (f->title);
18040 if (f->explicit_name || ! FRAME_WINDOW_P (f))
18041 return (char *) SDATA (f->name);
18042 return "Emacs";
18043
18044 case 'f':
18045 obj = b->filename;
18046 break;
18047
18048 case 'i':
18049 {
18050 int size = ZV - BEGV;
18051 pint2str (decode_mode_spec_buf, field_width, size);
18052 return decode_mode_spec_buf;
18053 }
18054
18055 case 'I':
18056 {
18057 int size = ZV - BEGV;
18058 pint2hrstr (decode_mode_spec_buf, field_width, size);
18059 return decode_mode_spec_buf;
18060 }
18061
18062 case 'l':
18063 {
18064 int startpos, startpos_byte, line, linepos, linepos_byte;
18065 int topline, nlines, junk, height;
18066
18067 /* %c and %l are ignored in `frame-title-format'. */
18068 if (mode_line_target == MODE_LINE_TITLE)
18069 return "";
18070
18071 startpos = XMARKER (w->start)->charpos;
18072 startpos_byte = marker_byte_position (w->start);
18073 height = WINDOW_TOTAL_LINES (w);
18074
18075 /* If we decided that this buffer isn't suitable for line numbers,
18076 don't forget that too fast. */
18077 if (EQ (w->base_line_pos, w->buffer))
18078 goto no_value;
18079 /* But do forget it, if the window shows a different buffer now. */
18080 else if (BUFFERP (w->base_line_pos))
18081 w->base_line_pos = Qnil;
18082
18083 /* If the buffer is very big, don't waste time. */
18084 if (INTEGERP (Vline_number_display_limit)
18085 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
18086 {
18087 w->base_line_pos = Qnil;
18088 w->base_line_number = Qnil;
18089 goto no_value;
18090 }
18091
18092 if (!NILP (w->base_line_number)
18093 && !NILP (w->base_line_pos)
18094 && XFASTINT (w->base_line_pos) <= startpos)
18095 {
18096 line = XFASTINT (w->base_line_number);
18097 linepos = XFASTINT (w->base_line_pos);
18098 linepos_byte = buf_charpos_to_bytepos (b, linepos);
18099 }
18100 else
18101 {
18102 line = 1;
18103 linepos = BUF_BEGV (b);
18104 linepos_byte = BUF_BEGV_BYTE (b);
18105 }
18106
18107 /* Count lines from base line to window start position. */
18108 nlines = display_count_lines (linepos, linepos_byte,
18109 startpos_byte,
18110 startpos, &junk);
18111
18112 topline = nlines + line;
18113
18114 /* Determine a new base line, if the old one is too close
18115 or too far away, or if we did not have one.
18116 "Too close" means it's plausible a scroll-down would
18117 go back past it. */
18118 if (startpos == BUF_BEGV (b))
18119 {
18120 w->base_line_number = make_number (topline);
18121 w->base_line_pos = make_number (BUF_BEGV (b));
18122 }
18123 else if (nlines < height + 25 || nlines > height * 3 + 50
18124 || linepos == BUF_BEGV (b))
18125 {
18126 int limit = BUF_BEGV (b);
18127 int limit_byte = BUF_BEGV_BYTE (b);
18128 int position;
18129 int distance = (height * 2 + 30) * line_number_display_limit_width;
18130
18131 if (startpos - distance > limit)
18132 {
18133 limit = startpos - distance;
18134 limit_byte = CHAR_TO_BYTE (limit);
18135 }
18136
18137 nlines = display_count_lines (startpos, startpos_byte,
18138 limit_byte,
18139 - (height * 2 + 30),
18140 &position);
18141 /* If we couldn't find the lines we wanted within
18142 line_number_display_limit_width chars per line,
18143 give up on line numbers for this window. */
18144 if (position == limit_byte && limit == startpos - distance)
18145 {
18146 w->base_line_pos = w->buffer;
18147 w->base_line_number = Qnil;
18148 goto no_value;
18149 }
18150
18151 w->base_line_number = make_number (topline - nlines);
18152 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
18153 }
18154
18155 /* Now count lines from the start pos to point. */
18156 nlines = display_count_lines (startpos, startpos_byte,
18157 PT_BYTE, PT, &junk);
18158
18159 /* Record that we did display the line number. */
18160 line_number_displayed = 1;
18161
18162 /* Make the string to show. */
18163 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
18164 return decode_mode_spec_buf;
18165 no_value:
18166 {
18167 char* p = decode_mode_spec_buf;
18168 int pad = field_width - 2;
18169 while (pad-- > 0)
18170 *p++ = ' ';
18171 *p++ = '?';
18172 *p++ = '?';
18173 *p = '\0';
18174 return decode_mode_spec_buf;
18175 }
18176 }
18177 break;
18178
18179 case 'm':
18180 obj = b->mode_name;
18181 break;
18182
18183 case 'n':
18184 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
18185 return " Narrow";
18186 break;
18187
18188 case 'p':
18189 {
18190 int pos = marker_position (w->start);
18191 int total = BUF_ZV (b) - BUF_BEGV (b);
18192
18193 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
18194 {
18195 if (pos <= BUF_BEGV (b))
18196 return "All";
18197 else
18198 return "Bottom";
18199 }
18200 else if (pos <= BUF_BEGV (b))
18201 return "Top";
18202 else
18203 {
18204 if (total > 1000000)
18205 /* Do it differently for a large value, to avoid overflow. */
18206 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
18207 else
18208 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
18209 /* We can't normally display a 3-digit number,
18210 so get us a 2-digit number that is close. */
18211 if (total == 100)
18212 total = 99;
18213 sprintf (decode_mode_spec_buf, "%2d%%", total);
18214 return decode_mode_spec_buf;
18215 }
18216 }
18217
18218 /* Display percentage of size above the bottom of the screen. */
18219 case 'P':
18220 {
18221 int toppos = marker_position (w->start);
18222 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
18223 int total = BUF_ZV (b) - BUF_BEGV (b);
18224
18225 if (botpos >= BUF_ZV (b))
18226 {
18227 if (toppos <= BUF_BEGV (b))
18228 return "All";
18229 else
18230 return "Bottom";
18231 }
18232 else
18233 {
18234 if (total > 1000000)
18235 /* Do it differently for a large value, to avoid overflow. */
18236 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
18237 else
18238 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
18239 /* We can't normally display a 3-digit number,
18240 so get us a 2-digit number that is close. */
18241 if (total == 100)
18242 total = 99;
18243 if (toppos <= BUF_BEGV (b))
18244 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
18245 else
18246 sprintf (decode_mode_spec_buf, "%2d%%", total);
18247 return decode_mode_spec_buf;
18248 }
18249 }
18250
18251 case 's':
18252 /* status of process */
18253 obj = Fget_buffer_process (Fcurrent_buffer ());
18254 if (NILP (obj))
18255 return "no process";
18256 #ifdef subprocesses
18257 obj = Fsymbol_name (Fprocess_status (obj));
18258 #endif
18259 break;
18260
18261 case '@':
18262 {
18263 Lisp_Object val;
18264 val = call1 (intern ("file-remote-p"), current_buffer->directory);
18265 if (NILP (val))
18266 return "-";
18267 else
18268 return "@";
18269 }
18270
18271 case 't': /* indicate TEXT or BINARY */
18272 #ifdef MODE_LINE_BINARY_TEXT
18273 return MODE_LINE_BINARY_TEXT (b);
18274 #else
18275 return "T";
18276 #endif
18277
18278 case 'z':
18279 /* coding-system (not including end-of-line format) */
18280 case 'Z':
18281 /* coding-system (including end-of-line type) */
18282 {
18283 int eol_flag = (c == 'Z');
18284 char *p = decode_mode_spec_buf;
18285
18286 if (! FRAME_WINDOW_P (f))
18287 {
18288 /* No need to mention EOL here--the terminal never needs
18289 to do EOL conversion. */
18290 p = decode_mode_spec_coding (CODING_ID_NAME
18291 (FRAME_KEYBOARD_CODING (f)->id),
18292 p, 0);
18293 p = decode_mode_spec_coding (CODING_ID_NAME
18294 (FRAME_TERMINAL_CODING (f)->id),
18295 p, 0);
18296 }
18297 p = decode_mode_spec_coding (b->buffer_file_coding_system,
18298 p, eol_flag);
18299
18300 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
18301 #ifdef subprocesses
18302 obj = Fget_buffer_process (Fcurrent_buffer ());
18303 if (PROCESSP (obj))
18304 {
18305 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
18306 p, eol_flag);
18307 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
18308 p, eol_flag);
18309 }
18310 #endif /* subprocesses */
18311 #endif /* 0 */
18312 *p = 0;
18313 return decode_mode_spec_buf;
18314 }
18315 }
18316
18317 if (STRINGP (obj))
18318 {
18319 *multibyte = STRING_MULTIBYTE (obj);
18320 return (char *) SDATA (obj);
18321 }
18322 else
18323 return "";
18324 }
18325
18326
18327 /* Count up to COUNT lines starting from START / START_BYTE.
18328 But don't go beyond LIMIT_BYTE.
18329 Return the number of lines thus found (always nonnegative).
18330
18331 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
18332
18333 static int
18334 display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
18335 int start, start_byte, limit_byte, count;
18336 int *byte_pos_ptr;
18337 {
18338 register unsigned char *cursor;
18339 unsigned char *base;
18340
18341 register int ceiling;
18342 register unsigned char *ceiling_addr;
18343 int orig_count = count;
18344
18345 /* If we are not in selective display mode,
18346 check only for newlines. */
18347 int selective_display = (!NILP (current_buffer->selective_display)
18348 && !INTEGERP (current_buffer->selective_display));
18349
18350 if (count > 0)
18351 {
18352 while (start_byte < limit_byte)
18353 {
18354 ceiling = BUFFER_CEILING_OF (start_byte);
18355 ceiling = min (limit_byte - 1, ceiling);
18356 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
18357 base = (cursor = BYTE_POS_ADDR (start_byte));
18358 while (1)
18359 {
18360 if (selective_display)
18361 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
18362 ;
18363 else
18364 while (*cursor != '\n' && ++cursor != ceiling_addr)
18365 ;
18366
18367 if (cursor != ceiling_addr)
18368 {
18369 if (--count == 0)
18370 {
18371 start_byte += cursor - base + 1;
18372 *byte_pos_ptr = start_byte;
18373 return orig_count;
18374 }
18375 else
18376 if (++cursor == ceiling_addr)
18377 break;
18378 }
18379 else
18380 break;
18381 }
18382 start_byte += cursor - base;
18383 }
18384 }
18385 else
18386 {
18387 while (start_byte > limit_byte)
18388 {
18389 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
18390 ceiling = max (limit_byte, ceiling);
18391 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
18392 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
18393 while (1)
18394 {
18395 if (selective_display)
18396 while (--cursor != ceiling_addr
18397 && *cursor != '\n' && *cursor != 015)
18398 ;
18399 else
18400 while (--cursor != ceiling_addr && *cursor != '\n')
18401 ;
18402
18403 if (cursor != ceiling_addr)
18404 {
18405 if (++count == 0)
18406 {
18407 start_byte += cursor - base + 1;
18408 *byte_pos_ptr = start_byte;
18409 /* When scanning backwards, we should
18410 not count the newline posterior to which we stop. */
18411 return - orig_count - 1;
18412 }
18413 }
18414 else
18415 break;
18416 }
18417 /* Here we add 1 to compensate for the last decrement
18418 of CURSOR, which took it past the valid range. */
18419 start_byte += cursor - base + 1;
18420 }
18421 }
18422
18423 *byte_pos_ptr = limit_byte;
18424
18425 if (count < 0)
18426 return - orig_count + count;
18427 return orig_count - count;
18428
18429 }
18430
18431
18432 \f
18433 /***********************************************************************
18434 Displaying strings
18435 ***********************************************************************/
18436
18437 /* Display a NUL-terminated string, starting with index START.
18438
18439 If STRING is non-null, display that C string. Otherwise, the Lisp
18440 string LISP_STRING is displayed.
18441
18442 If FACE_STRING is not nil, FACE_STRING_POS is a position in
18443 FACE_STRING. Display STRING or LISP_STRING with the face at
18444 FACE_STRING_POS in FACE_STRING:
18445
18446 Display the string in the environment given by IT, but use the
18447 standard display table, temporarily.
18448
18449 FIELD_WIDTH is the minimum number of output glyphs to produce.
18450 If STRING has fewer characters than FIELD_WIDTH, pad to the right
18451 with spaces. If STRING has more characters, more than FIELD_WIDTH
18452 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
18453
18454 PRECISION is the maximum number of characters to output from
18455 STRING. PRECISION < 0 means don't truncate the string.
18456
18457 This is roughly equivalent to printf format specifiers:
18458
18459 FIELD_WIDTH PRECISION PRINTF
18460 ----------------------------------------
18461 -1 -1 %s
18462 -1 10 %.10s
18463 10 -1 %10s
18464 20 10 %20.10s
18465
18466 MULTIBYTE zero means do not display multibyte chars, > 0 means do
18467 display them, and < 0 means obey the current buffer's value of
18468 enable_multibyte_characters.
18469
18470 Value is the number of columns displayed. */
18471
18472 static int
18473 display_string (string, lisp_string, face_string, face_string_pos,
18474 start, it, field_width, precision, max_x, multibyte)
18475 unsigned char *string;
18476 Lisp_Object lisp_string;
18477 Lisp_Object face_string;
18478 int face_string_pos;
18479 int start;
18480 struct it *it;
18481 int field_width, precision, max_x;
18482 int multibyte;
18483 {
18484 int hpos_at_start = it->hpos;
18485 int saved_face_id = it->face_id;
18486 struct glyph_row *row = it->glyph_row;
18487
18488 /* Initialize the iterator IT for iteration over STRING beginning
18489 with index START. */
18490 reseat_to_string (it, string, lisp_string, start,
18491 precision, field_width, multibyte);
18492
18493 /* If displaying STRING, set up the face of the iterator
18494 from LISP_STRING, if that's given. */
18495 if (STRINGP (face_string))
18496 {
18497 int endptr;
18498 struct face *face;
18499
18500 it->face_id
18501 = face_at_string_position (it->w, face_string, face_string_pos,
18502 0, it->region_beg_charpos,
18503 it->region_end_charpos,
18504 &endptr, it->base_face_id, 0);
18505 face = FACE_FROM_ID (it->f, it->face_id);
18506 it->face_box_p = face->box != FACE_NO_BOX;
18507 }
18508
18509 /* Set max_x to the maximum allowed X position. Don't let it go
18510 beyond the right edge of the window. */
18511 if (max_x <= 0)
18512 max_x = it->last_visible_x;
18513 else
18514 max_x = min (max_x, it->last_visible_x);
18515
18516 /* Skip over display elements that are not visible. because IT->w is
18517 hscrolled. */
18518 if (it->current_x < it->first_visible_x)
18519 move_it_in_display_line_to (it, 100000, it->first_visible_x,
18520 MOVE_TO_POS | MOVE_TO_X);
18521
18522 row->ascent = it->max_ascent;
18523 row->height = it->max_ascent + it->max_descent;
18524 row->phys_ascent = it->max_phys_ascent;
18525 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
18526 row->extra_line_spacing = it->max_extra_line_spacing;
18527
18528 /* This condition is for the case that we are called with current_x
18529 past last_visible_x. */
18530 while (it->current_x < max_x)
18531 {
18532 int x_before, x, n_glyphs_before, i, nglyphs;
18533
18534 /* Get the next display element. */
18535 if (!get_next_display_element (it))
18536 break;
18537
18538 /* Produce glyphs. */
18539 x_before = it->current_x;
18540 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
18541 PRODUCE_GLYPHS (it);
18542
18543 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
18544 i = 0;
18545 x = x_before;
18546 while (i < nglyphs)
18547 {
18548 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
18549
18550 if (!it->truncate_lines_p
18551 && x + glyph->pixel_width > max_x)
18552 {
18553 /* End of continued line or max_x reached. */
18554 if (CHAR_GLYPH_PADDING_P (*glyph))
18555 {
18556 /* A wide character is unbreakable. */
18557 it->glyph_row->used[TEXT_AREA] = n_glyphs_before;
18558 it->current_x = x_before;
18559 }
18560 else
18561 {
18562 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
18563 it->current_x = x;
18564 }
18565 break;
18566 }
18567 else if (x + glyph->pixel_width >= it->first_visible_x)
18568 {
18569 /* Glyph is at least partially visible. */
18570 ++it->hpos;
18571 if (x < it->first_visible_x)
18572 it->glyph_row->x = x - it->first_visible_x;
18573 }
18574 else
18575 {
18576 /* Glyph is off the left margin of the display area.
18577 Should not happen. */
18578 abort ();
18579 }
18580
18581 row->ascent = max (row->ascent, it->max_ascent);
18582 row->height = max (row->height, it->max_ascent + it->max_descent);
18583 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
18584 row->phys_height = max (row->phys_height,
18585 it->max_phys_ascent + it->max_phys_descent);
18586 row->extra_line_spacing = max (row->extra_line_spacing,
18587 it->max_extra_line_spacing);
18588 x += glyph->pixel_width;
18589 ++i;
18590 }
18591
18592 /* Stop if max_x reached. */
18593 if (i < nglyphs)
18594 break;
18595
18596 /* Stop at line ends. */
18597 if (ITERATOR_AT_END_OF_LINE_P (it))
18598 {
18599 it->continuation_lines_width = 0;
18600 break;
18601 }
18602
18603 set_iterator_to_next (it, 1);
18604
18605 /* Stop if truncating at the right edge. */
18606 if (it->truncate_lines_p
18607 && it->current_x >= it->last_visible_x)
18608 {
18609 /* Add truncation mark, but don't do it if the line is
18610 truncated at a padding space. */
18611 if (IT_CHARPOS (*it) < it->string_nchars)
18612 {
18613 if (!FRAME_WINDOW_P (it->f))
18614 {
18615 int i, n;
18616
18617 if (it->current_x > it->last_visible_x)
18618 {
18619 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
18620 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
18621 break;
18622 for (n = row->used[TEXT_AREA]; i < n; ++i)
18623 {
18624 row->used[TEXT_AREA] = i;
18625 produce_special_glyphs (it, IT_TRUNCATION);
18626 }
18627 }
18628 produce_special_glyphs (it, IT_TRUNCATION);
18629 }
18630 it->glyph_row->truncated_on_right_p = 1;
18631 }
18632 break;
18633 }
18634 }
18635
18636 /* Maybe insert a truncation at the left. */
18637 if (it->first_visible_x
18638 && IT_CHARPOS (*it) > 0)
18639 {
18640 if (!FRAME_WINDOW_P (it->f))
18641 insert_left_trunc_glyphs (it);
18642 it->glyph_row->truncated_on_left_p = 1;
18643 }
18644
18645 it->face_id = saved_face_id;
18646
18647 /* Value is number of columns displayed. */
18648 return it->hpos - hpos_at_start;
18649 }
18650
18651
18652 \f
18653 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
18654 appears as an element of LIST or as the car of an element of LIST.
18655 If PROPVAL is a list, compare each element against LIST in that
18656 way, and return 1/2 if any element of PROPVAL is found in LIST.
18657 Otherwise return 0. This function cannot quit.
18658 The return value is 2 if the text is invisible but with an ellipsis
18659 and 1 if it's invisible and without an ellipsis. */
18660
18661 int
18662 invisible_p (propval, list)
18663 register Lisp_Object propval;
18664 Lisp_Object list;
18665 {
18666 register Lisp_Object tail, proptail;
18667
18668 for (tail = list; CONSP (tail); tail = XCDR (tail))
18669 {
18670 register Lisp_Object tem;
18671 tem = XCAR (tail);
18672 if (EQ (propval, tem))
18673 return 1;
18674 if (CONSP (tem) && EQ (propval, XCAR (tem)))
18675 return NILP (XCDR (tem)) ? 1 : 2;
18676 }
18677
18678 if (CONSP (propval))
18679 {
18680 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
18681 {
18682 Lisp_Object propelt;
18683 propelt = XCAR (proptail);
18684 for (tail = list; CONSP (tail); tail = XCDR (tail))
18685 {
18686 register Lisp_Object tem;
18687 tem = XCAR (tail);
18688 if (EQ (propelt, tem))
18689 return 1;
18690 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
18691 return NILP (XCDR (tem)) ? 1 : 2;
18692 }
18693 }
18694 }
18695
18696 return 0;
18697 }
18698
18699 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
18700 doc: /* Non-nil if the property makes the text invisible.
18701 POS-OR-PROP can be a marker or number, in which case it is taken to be
18702 a position in the current buffer and the value of the `invisible' property
18703 is checked; or it can be some other value, which is then presumed to be the
18704 value of the `invisible' property of the text of interest.
18705 The non-nil value returned can be t for truly invisible text or something
18706 else if the text is replaced by an ellipsis. */)
18707 (pos_or_prop)
18708 Lisp_Object pos_or_prop;
18709 {
18710 Lisp_Object prop
18711 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
18712 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
18713 : pos_or_prop);
18714 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
18715 return (invis == 0 ? Qnil
18716 : invis == 1 ? Qt
18717 : make_number (invis));
18718 }
18719
18720 /* Calculate a width or height in pixels from a specification using
18721 the following elements:
18722
18723 SPEC ::=
18724 NUM - a (fractional) multiple of the default font width/height
18725 (NUM) - specifies exactly NUM pixels
18726 UNIT - a fixed number of pixels, see below.
18727 ELEMENT - size of a display element in pixels, see below.
18728 (NUM . SPEC) - equals NUM * SPEC
18729 (+ SPEC SPEC ...) - add pixel values
18730 (- SPEC SPEC ...) - subtract pixel values
18731 (- SPEC) - negate pixel value
18732
18733 NUM ::=
18734 INT or FLOAT - a number constant
18735 SYMBOL - use symbol's (buffer local) variable binding.
18736
18737 UNIT ::=
18738 in - pixels per inch *)
18739 mm - pixels per 1/1000 meter *)
18740 cm - pixels per 1/100 meter *)
18741 width - width of current font in pixels.
18742 height - height of current font in pixels.
18743
18744 *) using the ratio(s) defined in display-pixels-per-inch.
18745
18746 ELEMENT ::=
18747
18748 left-fringe - left fringe width in pixels
18749 right-fringe - right fringe width in pixels
18750
18751 left-margin - left margin width in pixels
18752 right-margin - right margin width in pixels
18753
18754 scroll-bar - scroll-bar area width in pixels
18755
18756 Examples:
18757
18758 Pixels corresponding to 5 inches:
18759 (5 . in)
18760
18761 Total width of non-text areas on left side of window (if scroll-bar is on left):
18762 '(space :width (+ left-fringe left-margin scroll-bar))
18763
18764 Align to first text column (in header line):
18765 '(space :align-to 0)
18766
18767 Align to middle of text area minus half the width of variable `my-image'
18768 containing a loaded image:
18769 '(space :align-to (0.5 . (- text my-image)))
18770
18771 Width of left margin minus width of 1 character in the default font:
18772 '(space :width (- left-margin 1))
18773
18774 Width of left margin minus width of 2 characters in the current font:
18775 '(space :width (- left-margin (2 . width)))
18776
18777 Center 1 character over left-margin (in header line):
18778 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
18779
18780 Different ways to express width of left fringe plus left margin minus one pixel:
18781 '(space :width (- (+ left-fringe left-margin) (1)))
18782 '(space :width (+ left-fringe left-margin (- (1))))
18783 '(space :width (+ left-fringe left-margin (-1)))
18784
18785 */
18786
18787 #define NUMVAL(X) \
18788 ((INTEGERP (X) || FLOATP (X)) \
18789 ? XFLOATINT (X) \
18790 : - 1)
18791
18792 int
18793 calc_pixel_width_or_height (res, it, prop, font, width_p, align_to)
18794 double *res;
18795 struct it *it;
18796 Lisp_Object prop;
18797 void *font;
18798 int width_p, *align_to;
18799 {
18800 double pixels;
18801
18802 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
18803 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
18804
18805 if (NILP (prop))
18806 return OK_PIXELS (0);
18807
18808 xassert (FRAME_LIVE_P (it->f));
18809
18810 if (SYMBOLP (prop))
18811 {
18812 if (SCHARS (SYMBOL_NAME (prop)) == 2)
18813 {
18814 char *unit = SDATA (SYMBOL_NAME (prop));
18815
18816 if (unit[0] == 'i' && unit[1] == 'n')
18817 pixels = 1.0;
18818 else if (unit[0] == 'm' && unit[1] == 'm')
18819 pixels = 25.4;
18820 else if (unit[0] == 'c' && unit[1] == 'm')
18821 pixels = 2.54;
18822 else
18823 pixels = 0;
18824 if (pixels > 0)
18825 {
18826 double ppi;
18827 #ifdef HAVE_WINDOW_SYSTEM
18828 if (FRAME_WINDOW_P (it->f)
18829 && (ppi = (width_p
18830 ? FRAME_X_DISPLAY_INFO (it->f)->resx
18831 : FRAME_X_DISPLAY_INFO (it->f)->resy),
18832 ppi > 0))
18833 return OK_PIXELS (ppi / pixels);
18834 #endif
18835
18836 if ((ppi = NUMVAL (Vdisplay_pixels_per_inch), ppi > 0)
18837 || (CONSP (Vdisplay_pixels_per_inch)
18838 && (ppi = (width_p
18839 ? NUMVAL (XCAR (Vdisplay_pixels_per_inch))
18840 : NUMVAL (XCDR (Vdisplay_pixels_per_inch))),
18841 ppi > 0)))
18842 return OK_PIXELS (ppi / pixels);
18843
18844 return 0;
18845 }
18846 }
18847
18848 #ifdef HAVE_WINDOW_SYSTEM
18849 if (EQ (prop, Qheight))
18850 return OK_PIXELS (font ? FONT_HEIGHT ((XFontStruct *)font) : FRAME_LINE_HEIGHT (it->f));
18851 if (EQ (prop, Qwidth))
18852 return OK_PIXELS (font ? FONT_WIDTH ((XFontStruct *)font) : FRAME_COLUMN_WIDTH (it->f));
18853 #else
18854 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
18855 return OK_PIXELS (1);
18856 #endif
18857
18858 if (EQ (prop, Qtext))
18859 return OK_PIXELS (width_p
18860 ? window_box_width (it->w, TEXT_AREA)
18861 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
18862
18863 if (align_to && *align_to < 0)
18864 {
18865 *res = 0;
18866 if (EQ (prop, Qleft))
18867 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
18868 if (EQ (prop, Qright))
18869 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
18870 if (EQ (prop, Qcenter))
18871 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
18872 + window_box_width (it->w, TEXT_AREA) / 2);
18873 if (EQ (prop, Qleft_fringe))
18874 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
18875 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
18876 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
18877 if (EQ (prop, Qright_fringe))
18878 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
18879 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
18880 : window_box_right_offset (it->w, TEXT_AREA));
18881 if (EQ (prop, Qleft_margin))
18882 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
18883 if (EQ (prop, Qright_margin))
18884 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
18885 if (EQ (prop, Qscroll_bar))
18886 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
18887 ? 0
18888 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
18889 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
18890 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
18891 : 0)));
18892 }
18893 else
18894 {
18895 if (EQ (prop, Qleft_fringe))
18896 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
18897 if (EQ (prop, Qright_fringe))
18898 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
18899 if (EQ (prop, Qleft_margin))
18900 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
18901 if (EQ (prop, Qright_margin))
18902 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
18903 if (EQ (prop, Qscroll_bar))
18904 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
18905 }
18906
18907 prop = Fbuffer_local_value (prop, it->w->buffer);
18908 }
18909
18910 if (INTEGERP (prop) || FLOATP (prop))
18911 {
18912 int base_unit = (width_p
18913 ? FRAME_COLUMN_WIDTH (it->f)
18914 : FRAME_LINE_HEIGHT (it->f));
18915 return OK_PIXELS (XFLOATINT (prop) * base_unit);
18916 }
18917
18918 if (CONSP (prop))
18919 {
18920 Lisp_Object car = XCAR (prop);
18921 Lisp_Object cdr = XCDR (prop);
18922
18923 if (SYMBOLP (car))
18924 {
18925 #ifdef HAVE_WINDOW_SYSTEM
18926 if (FRAME_WINDOW_P (it->f)
18927 && valid_image_p (prop))
18928 {
18929 int id = lookup_image (it->f, prop);
18930 struct image *img = IMAGE_FROM_ID (it->f, id);
18931
18932 return OK_PIXELS (width_p ? img->width : img->height);
18933 }
18934 #endif
18935 if (EQ (car, Qplus) || EQ (car, Qminus))
18936 {
18937 int first = 1;
18938 double px;
18939
18940 pixels = 0;
18941 while (CONSP (cdr))
18942 {
18943 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
18944 font, width_p, align_to))
18945 return 0;
18946 if (first)
18947 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
18948 else
18949 pixels += px;
18950 cdr = XCDR (cdr);
18951 }
18952 if (EQ (car, Qminus))
18953 pixels = -pixels;
18954 return OK_PIXELS (pixels);
18955 }
18956
18957 car = Fbuffer_local_value (car, it->w->buffer);
18958 }
18959
18960 if (INTEGERP (car) || FLOATP (car))
18961 {
18962 double fact;
18963 pixels = XFLOATINT (car);
18964 if (NILP (cdr))
18965 return OK_PIXELS (pixels);
18966 if (calc_pixel_width_or_height (&fact, it, cdr,
18967 font, width_p, align_to))
18968 return OK_PIXELS (pixels * fact);
18969 return 0;
18970 }
18971
18972 return 0;
18973 }
18974
18975 return 0;
18976 }
18977
18978 \f
18979 /***********************************************************************
18980 Glyph Display
18981 ***********************************************************************/
18982
18983 #ifdef HAVE_WINDOW_SYSTEM
18984
18985 #if GLYPH_DEBUG
18986
18987 void
18988 dump_glyph_string (s)
18989 struct glyph_string *s;
18990 {
18991 fprintf (stderr, "glyph string\n");
18992 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
18993 s->x, s->y, s->width, s->height);
18994 fprintf (stderr, " ybase = %d\n", s->ybase);
18995 fprintf (stderr, " hl = %d\n", s->hl);
18996 fprintf (stderr, " left overhang = %d, right = %d\n",
18997 s->left_overhang, s->right_overhang);
18998 fprintf (stderr, " nchars = %d\n", s->nchars);
18999 fprintf (stderr, " extends to end of line = %d\n",
19000 s->extends_to_end_of_line_p);
19001 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
19002 fprintf (stderr, " bg width = %d\n", s->background_width);
19003 }
19004
19005 #endif /* GLYPH_DEBUG */
19006
19007 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
19008 of XChar2b structures for S; it can't be allocated in
19009 init_glyph_string because it must be allocated via `alloca'. W
19010 is the window on which S is drawn. ROW and AREA are the glyph row
19011 and area within the row from which S is constructed. START is the
19012 index of the first glyph structure covered by S. HL is a
19013 face-override for drawing S. */
19014
19015 #ifdef HAVE_NTGUI
19016 #define OPTIONAL_HDC(hdc) hdc,
19017 #define DECLARE_HDC(hdc) HDC hdc;
19018 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
19019 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
19020 #endif
19021
19022 #ifndef OPTIONAL_HDC
19023 #define OPTIONAL_HDC(hdc)
19024 #define DECLARE_HDC(hdc)
19025 #define ALLOCATE_HDC(hdc, f)
19026 #define RELEASE_HDC(hdc, f)
19027 #endif
19028
19029 static void
19030 init_glyph_string (s, OPTIONAL_HDC (hdc) char2b, w, row, area, start, hl)
19031 struct glyph_string *s;
19032 DECLARE_HDC (hdc)
19033 XChar2b *char2b;
19034 struct window *w;
19035 struct glyph_row *row;
19036 enum glyph_row_area area;
19037 int start;
19038 enum draw_glyphs_face hl;
19039 {
19040 bzero (s, sizeof *s);
19041 s->w = w;
19042 s->f = XFRAME (w->frame);
19043 #ifdef HAVE_NTGUI
19044 s->hdc = hdc;
19045 #endif
19046 s->display = FRAME_X_DISPLAY (s->f);
19047 s->window = FRAME_X_WINDOW (s->f);
19048 s->char2b = char2b;
19049 s->hl = hl;
19050 s->row = row;
19051 s->area = area;
19052 s->first_glyph = row->glyphs[area] + start;
19053 s->height = row->height;
19054 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
19055
19056 /* Display the internal border below the tool-bar window. */
19057 if (WINDOWP (s->f->tool_bar_window)
19058 && s->w == XWINDOW (s->f->tool_bar_window))
19059 s->y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
19060
19061 s->ybase = s->y + row->ascent;
19062 }
19063
19064
19065 /* Append the list of glyph strings with head H and tail T to the list
19066 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
19067
19068 static INLINE void
19069 append_glyph_string_lists (head, tail, h, t)
19070 struct glyph_string **head, **tail;
19071 struct glyph_string *h, *t;
19072 {
19073 if (h)
19074 {
19075 if (*head)
19076 (*tail)->next = h;
19077 else
19078 *head = h;
19079 h->prev = *tail;
19080 *tail = t;
19081 }
19082 }
19083
19084
19085 /* Prepend the list of glyph strings with head H and tail T to the
19086 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
19087 result. */
19088
19089 static INLINE void
19090 prepend_glyph_string_lists (head, tail, h, t)
19091 struct glyph_string **head, **tail;
19092 struct glyph_string *h, *t;
19093 {
19094 if (h)
19095 {
19096 if (*head)
19097 (*head)->prev = t;
19098 else
19099 *tail = t;
19100 t->next = *head;
19101 *head = h;
19102 }
19103 }
19104
19105
19106 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
19107 Set *HEAD and *TAIL to the resulting list. */
19108
19109 static INLINE void
19110 append_glyph_string (head, tail, s)
19111 struct glyph_string **head, **tail;
19112 struct glyph_string *s;
19113 {
19114 s->next = s->prev = NULL;
19115 append_glyph_string_lists (head, tail, s, s);
19116 }
19117
19118
19119 /* Get face and two-byte form of character C in face FACE_ID on frame
19120 F. The encoding of C is returned in *CHAR2B. MULTIBYTE_P non-zero
19121 means we want to display multibyte text. DISPLAY_P non-zero means
19122 make sure that X resources for the face returned are allocated.
19123 Value is a pointer to a realized face that is ready for display if
19124 DISPLAY_P is non-zero. */
19125
19126 static INLINE struct face *
19127 get_char_face_and_encoding (f, c, face_id, char2b, multibyte_p, display_p)
19128 struct frame *f;
19129 int c, face_id;
19130 XChar2b *char2b;
19131 int multibyte_p, display_p;
19132 {
19133 struct face *face = FACE_FROM_ID (f, face_id);
19134
19135 #ifdef USE_FONT_BACKEND
19136 if (enable_font_backend)
19137 {
19138 struct font *font = (struct font *) face->font_info;
19139
19140 if (font)
19141 {
19142 unsigned code = font->driver->encode_char (font, c);
19143
19144 if (code != FONT_INVALID_CODE)
19145 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
19146 else
19147 STORE_XCHAR2B (char2b, 0, 0);
19148 }
19149 }
19150 else
19151 #endif /* USE_FONT_BACKEND */
19152 if (!multibyte_p)
19153 {
19154 /* Unibyte case. We don't have to encode, but we have to make
19155 sure to use a face suitable for unibyte. */
19156 STORE_XCHAR2B (char2b, 0, c);
19157 face_id = FACE_FOR_CHAR (f, face, c, -1, Qnil);
19158 face = FACE_FROM_ID (f, face_id);
19159 }
19160 else if (c < 128)
19161 {
19162 /* Case of ASCII in a face known to fit ASCII. */
19163 STORE_XCHAR2B (char2b, 0, c);
19164 }
19165 else if (face->font != NULL)
19166 {
19167 struct font_info *font_info
19168 = FONT_INFO_FROM_ID (f, face->font_info_id);
19169 struct charset *charset = CHARSET_FROM_ID (font_info->charset);
19170 unsigned code = ENCODE_CHAR (charset, c);
19171
19172 if (CHARSET_DIMENSION (charset) == 1)
19173 STORE_XCHAR2B (char2b, 0, code);
19174 else
19175 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
19176 /* Maybe encode the character in *CHAR2B. */
19177 FRAME_RIF (f)->encode_char (c, char2b, font_info, charset, NULL);
19178 }
19179
19180 /* Make sure X resources of the face are allocated. */
19181 #ifdef HAVE_X_WINDOWS
19182 if (display_p)
19183 #endif
19184 {
19185 xassert (face != NULL);
19186 PREPARE_FACE_FOR_DISPLAY (f, face);
19187 }
19188
19189 return face;
19190 }
19191
19192
19193 /* Get face and two-byte form of character glyph GLYPH on frame F.
19194 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
19195 a pointer to a realized face that is ready for display. */
19196
19197 static INLINE struct face *
19198 get_glyph_face_and_encoding (f, glyph, char2b, two_byte_p)
19199 struct frame *f;
19200 struct glyph *glyph;
19201 XChar2b *char2b;
19202 int *two_byte_p;
19203 {
19204 struct face *face;
19205
19206 xassert (glyph->type == CHAR_GLYPH);
19207 face = FACE_FROM_ID (f, glyph->face_id);
19208
19209 if (two_byte_p)
19210 *two_byte_p = 0;
19211
19212 #ifdef USE_FONT_BACKEND
19213 if (enable_font_backend)
19214 {
19215 struct font *font = (struct font *) face->font_info;
19216
19217 if (font)
19218 {
19219 unsigned code = font->driver->encode_char (font, glyph->u.ch);
19220
19221 if (code != FONT_INVALID_CODE)
19222 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
19223 else
19224 STORE_XCHAR2B (char2b, 0, code);
19225 }
19226 }
19227 else
19228 #endif /* USE_FONT_BACKEND */
19229 if (!glyph->multibyte_p)
19230 {
19231 /* Unibyte case. We don't have to encode, but we have to make
19232 sure to use a face suitable for unibyte. */
19233 STORE_XCHAR2B (char2b, 0, glyph->u.ch);
19234 }
19235 else if (glyph->u.ch < 128)
19236 {
19237 /* Case of ASCII in a face known to fit ASCII. */
19238 STORE_XCHAR2B (char2b, 0, glyph->u.ch);
19239 }
19240 else
19241 {
19242 struct font_info *font_info
19243 = FONT_INFO_FROM_ID (f, face->font_info_id);
19244 if (font_info)
19245 {
19246 struct charset *charset = CHARSET_FROM_ID (font_info->charset);
19247 unsigned code = ENCODE_CHAR (charset, glyph->u.ch);
19248
19249 if (CHARSET_DIMENSION (charset) == 1)
19250 STORE_XCHAR2B (char2b, 0, code);
19251 else
19252 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
19253
19254 /* Maybe encode the character in *CHAR2B. */
19255 if (CHARSET_ID (charset) != charset_ascii)
19256 {
19257 glyph->font_type
19258 = FRAME_RIF (f)->encode_char (glyph->u.ch, char2b, font_info,
19259 charset, two_byte_p);
19260 }
19261 }
19262 }
19263
19264 /* Make sure X resources of the face are allocated. */
19265 xassert (face != NULL);
19266 PREPARE_FACE_FOR_DISPLAY (f, face);
19267 return face;
19268 }
19269
19270
19271 /* Fill glyph string S with composition components specified by S->cmp.
19272
19273 BASE_FACE is the base face of the composition.
19274 S->gidx is the index of the first component for S.
19275
19276 OVERLAPS non-zero means S should draw the foreground only, and use
19277 its physical height for clipping. See also draw_glyphs.
19278
19279 Value is the index of a component not in S. */
19280
19281 static int
19282 fill_composite_glyph_string (s, base_face, overlaps)
19283 struct glyph_string *s;
19284 struct face *base_face;
19285 int overlaps;
19286 {
19287 int i;
19288
19289 xassert (s);
19290
19291 s->for_overlaps = overlaps;
19292
19293 #ifdef USE_FONT_BACKEND
19294 if (enable_font_backend && s->cmp->method == COMPOSITION_WITH_GLYPH_STRING)
19295 {
19296 Lisp_Object gstring
19297 = AREF (XHASH_TABLE (composition_hash_table)->key_and_value,
19298 s->cmp->hash_index * 2);
19299
19300 s->face = base_face;
19301 s->font_info = s->cmp->font;
19302 s->font = s->font_info->font;
19303 for (i = 0, s->nchars = 0; i < s->cmp->glyph_len; i++, s->nchars++)
19304 {
19305 Lisp_Object g = LGSTRING_GLYPH (gstring, i);
19306 unsigned code;
19307 XChar2b * store_pos;
19308 if (NILP (LGLYPH_FROM (g)))
19309 break;
19310 code = XUINT (LGLYPH_CODE (g));
19311 store_pos = s->char2b + i;
19312 STORE_XCHAR2B (store_pos, code >> 8, code & 0xFF);
19313 }
19314 s->width = s->cmp->pixel_width;
19315 }
19316 else
19317 #endif /* USE_FONT_BACKEND */
19318 {
19319 /* For all glyphs of this composition, starting at the offset
19320 S->gidx, until we reach the end of the definition or encounter a
19321 glyph that requires the different face, add it to S. */
19322 struct face *face;
19323
19324 s->face = NULL;
19325 s->font = NULL;
19326 s->font_info = NULL;
19327 for (i = s->gidx; i < s->cmp->glyph_len; i++)
19328 {
19329 int c = COMPOSITION_GLYPH (s->cmp, i);
19330
19331 if (c != '\t')
19332 {
19333 int face_id = FACE_FOR_CHAR (s->f, base_face, c, -1, Qnil);
19334
19335 face = get_char_face_and_encoding (s->f, c, face_id,
19336 s->char2b + i, 1, 1);
19337 if (face)
19338 {
19339 if (! s->face)
19340 {
19341 s->face = face;
19342 s->font = s->face->font;
19343 s->font_info = FONT_INFO_FROM_FACE (s->f, s->face);
19344 }
19345 else if (s->face != face)
19346 break;
19347 }
19348 }
19349 ++s->nchars;
19350 }
19351
19352 /* All glyph strings for the same composition has the same width,
19353 i.e. the width set for the first component of the composition. */
19354 s->width = s->first_glyph->pixel_width;
19355 }
19356
19357 /* If the specified font could not be loaded, use the frame's
19358 default font, but record the fact that we couldn't load it in
19359 the glyph string so that we can draw rectangles for the
19360 characters of the glyph string. */
19361 if (s->font == NULL)
19362 {
19363 s->font_not_found_p = 1;
19364 s->font = FRAME_FONT (s->f);
19365 }
19366
19367 /* Adjust base line for subscript/superscript text. */
19368 s->ybase += s->first_glyph->voffset;
19369
19370 /* This glyph string must always be drawn with 16-bit functions. */
19371 s->two_byte_p = 1;
19372
19373 return s->gidx + s->nchars;
19374 }
19375
19376
19377 /* Fill glyph string S from a sequence of character glyphs.
19378
19379 FACE_ID is the face id of the string. START is the index of the
19380 first glyph to consider, END is the index of the last + 1.
19381 OVERLAPS non-zero means S should draw the foreground only, and use
19382 its physical height for clipping. See also draw_glyphs.
19383
19384 Value is the index of the first glyph not in S. */
19385
19386 static int
19387 fill_glyph_string (s, face_id, start, end, overlaps)
19388 struct glyph_string *s;
19389 int face_id;
19390 int start, end, overlaps;
19391 {
19392 struct glyph *glyph, *last;
19393 int voffset;
19394 int glyph_not_available_p;
19395
19396 xassert (s->f == XFRAME (s->w->frame));
19397 xassert (s->nchars == 0);
19398 xassert (start >= 0 && end > start);
19399
19400 s->for_overlaps = overlaps,
19401 glyph = s->row->glyphs[s->area] + start;
19402 last = s->row->glyphs[s->area] + end;
19403 voffset = glyph->voffset;
19404
19405 glyph_not_available_p = glyph->glyph_not_available_p;
19406
19407 while (glyph < last
19408 && glyph->type == CHAR_GLYPH
19409 && glyph->voffset == voffset
19410 /* Same face id implies same font, nowadays. */
19411 && glyph->face_id == face_id
19412 && glyph->glyph_not_available_p == glyph_not_available_p)
19413 {
19414 int two_byte_p;
19415
19416 s->face = get_glyph_face_and_encoding (s->f, glyph,
19417 s->char2b + s->nchars,
19418 &two_byte_p);
19419 s->two_byte_p = two_byte_p;
19420 ++s->nchars;
19421 xassert (s->nchars <= end - start);
19422 s->width += glyph->pixel_width;
19423 ++glyph;
19424 }
19425
19426 s->font = s->face->font;
19427 s->font_info = FONT_INFO_FROM_FACE (s->f, s->face);
19428
19429 /* If the specified font could not be loaded, use the frame's font,
19430 but record the fact that we couldn't load it in
19431 S->font_not_found_p so that we can draw rectangles for the
19432 characters of the glyph string. */
19433 if (s->font == NULL || glyph_not_available_p)
19434 {
19435 s->font_not_found_p = 1;
19436 s->font = FRAME_FONT (s->f);
19437 }
19438
19439 /* Adjust base line for subscript/superscript text. */
19440 s->ybase += voffset;
19441
19442 xassert (s->face && s->face->gc);
19443 return glyph - s->row->glyphs[s->area];
19444 }
19445
19446
19447 /* Fill glyph string S from image glyph S->first_glyph. */
19448
19449 static void
19450 fill_image_glyph_string (s)
19451 struct glyph_string *s;
19452 {
19453 xassert (s->first_glyph->type == IMAGE_GLYPH);
19454 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
19455 xassert (s->img);
19456 s->slice = s->first_glyph->slice;
19457 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
19458 s->font = s->face->font;
19459 s->width = s->first_glyph->pixel_width;
19460
19461 /* Adjust base line for subscript/superscript text. */
19462 s->ybase += s->first_glyph->voffset;
19463 }
19464
19465
19466 /* Fill glyph string S from a sequence of stretch glyphs.
19467
19468 ROW is the glyph row in which the glyphs are found, AREA is the
19469 area within the row. START is the index of the first glyph to
19470 consider, END is the index of the last + 1.
19471
19472 Value is the index of the first glyph not in S. */
19473
19474 static int
19475 fill_stretch_glyph_string (s, row, area, start, end)
19476 struct glyph_string *s;
19477 struct glyph_row *row;
19478 enum glyph_row_area area;
19479 int start, end;
19480 {
19481 struct glyph *glyph, *last;
19482 int voffset, face_id;
19483
19484 xassert (s->first_glyph->type == STRETCH_GLYPH);
19485
19486 glyph = s->row->glyphs[s->area] + start;
19487 last = s->row->glyphs[s->area] + end;
19488 face_id = glyph->face_id;
19489 s->face = FACE_FROM_ID (s->f, face_id);
19490 s->font = s->face->font;
19491 s->font_info = FONT_INFO_FROM_FACE (s->f, s->face);
19492 s->width = glyph->pixel_width;
19493 s->nchars = 1;
19494 voffset = glyph->voffset;
19495
19496 for (++glyph;
19497 (glyph < last
19498 && glyph->type == STRETCH_GLYPH
19499 && glyph->voffset == voffset
19500 && glyph->face_id == face_id);
19501 ++glyph)
19502 s->width += glyph->pixel_width;
19503
19504 /* Adjust base line for subscript/superscript text. */
19505 s->ybase += voffset;
19506
19507 /* The case that face->gc == 0 is handled when drawing the glyph
19508 string by calling PREPARE_FACE_FOR_DISPLAY. */
19509 xassert (s->face);
19510 return glyph - s->row->glyphs[s->area];
19511 }
19512
19513 static XCharStruct *
19514 get_per_char_metric (f, font, font_info, char2b, font_type)
19515 struct frame *f;
19516 XFontStruct *font;
19517 struct font_info *font_info;
19518 XChar2b *char2b;
19519 int font_type;
19520 {
19521 #ifdef USE_FONT_BACKEND
19522 if (enable_font_backend)
19523 {
19524 static XCharStruct pcm_value;
19525 unsigned code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
19526 struct font *fontp;
19527 struct font_metrics metrics;
19528
19529 if (! font_info || code == FONT_INVALID_CODE)
19530 return NULL;
19531 fontp = (struct font *) font_info;
19532 fontp->driver->text_extents (fontp, &code, 1, &metrics);
19533 pcm_value.lbearing = metrics.lbearing;
19534 pcm_value.rbearing = metrics.rbearing;
19535 pcm_value.ascent = metrics.ascent;
19536 pcm_value.descent = metrics.descent;
19537 pcm_value.width = metrics.width;
19538 return &pcm_value;
19539 }
19540 #endif /* USE_FONT_BACKEND */
19541 return FRAME_RIF (f)->per_char_metric (font, char2b, font_type);
19542 }
19543
19544 /* EXPORT for RIF:
19545 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
19546 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
19547 assumed to be zero. */
19548
19549 void
19550 x_get_glyph_overhangs (glyph, f, left, right)
19551 struct glyph *glyph;
19552 struct frame *f;
19553 int *left, *right;
19554 {
19555 *left = *right = 0;
19556
19557 if (glyph->type == CHAR_GLYPH)
19558 {
19559 XFontStruct *font;
19560 struct face *face;
19561 struct font_info *font_info;
19562 XChar2b char2b;
19563 XCharStruct *pcm;
19564
19565 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
19566 font = face->font;
19567 font_info = FONT_INFO_FROM_FACE (f, face);
19568 if (font /* ++KFS: Should this be font_info ? */
19569 && (pcm = get_per_char_metric (f, font, font_info, &char2b, glyph->font_type)))
19570 {
19571 if (pcm->rbearing > pcm->width)
19572 *right = pcm->rbearing - pcm->width;
19573 if (pcm->lbearing < 0)
19574 *left = -pcm->lbearing;
19575 }
19576 }
19577 else if (glyph->type == COMPOSITE_GLYPH)
19578 {
19579 struct composition *cmp = composition_table[glyph->u.cmp_id];
19580
19581 *right = cmp->rbearing - cmp->pixel_width;
19582 *left = - cmp->lbearing;
19583 }
19584 }
19585
19586
19587 /* Return the index of the first glyph preceding glyph string S that
19588 is overwritten by S because of S's left overhang. Value is -1
19589 if no glyphs are overwritten. */
19590
19591 static int
19592 left_overwritten (s)
19593 struct glyph_string *s;
19594 {
19595 int k;
19596
19597 if (s->left_overhang)
19598 {
19599 int x = 0, i;
19600 struct glyph *glyphs = s->row->glyphs[s->area];
19601 int first = s->first_glyph - glyphs;
19602
19603 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
19604 x -= glyphs[i].pixel_width;
19605
19606 k = i + 1;
19607 }
19608 else
19609 k = -1;
19610
19611 return k;
19612 }
19613
19614
19615 /* Return the index of the first glyph preceding glyph string S that
19616 is overwriting S because of its right overhang. Value is -1 if no
19617 glyph in front of S overwrites S. */
19618
19619 static int
19620 left_overwriting (s)
19621 struct glyph_string *s;
19622 {
19623 int i, k, x;
19624 struct glyph *glyphs = s->row->glyphs[s->area];
19625 int first = s->first_glyph - glyphs;
19626
19627 k = -1;
19628 x = 0;
19629 for (i = first - 1; i >= 0; --i)
19630 {
19631 int left, right;
19632 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
19633 if (x + right > 0)
19634 k = i;
19635 x -= glyphs[i].pixel_width;
19636 }
19637
19638 return k;
19639 }
19640
19641
19642 /* Return the index of the last glyph following glyph string S that is
19643 not overwritten by S because of S's right overhang. Value is -1 if
19644 no such glyph is found. */
19645
19646 static int
19647 right_overwritten (s)
19648 struct glyph_string *s;
19649 {
19650 int k = -1;
19651
19652 if (s->right_overhang)
19653 {
19654 int x = 0, i;
19655 struct glyph *glyphs = s->row->glyphs[s->area];
19656 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
19657 int end = s->row->used[s->area];
19658
19659 for (i = first; i < end && s->right_overhang > x; ++i)
19660 x += glyphs[i].pixel_width;
19661
19662 k = i;
19663 }
19664
19665 return k;
19666 }
19667
19668
19669 /* Return the index of the last glyph following glyph string S that
19670 overwrites S because of its left overhang. Value is negative
19671 if no such glyph is found. */
19672
19673 static int
19674 right_overwriting (s)
19675 struct glyph_string *s;
19676 {
19677 int i, k, x;
19678 int end = s->row->used[s->area];
19679 struct glyph *glyphs = s->row->glyphs[s->area];
19680 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
19681
19682 k = -1;
19683 x = 0;
19684 for (i = first; i < end; ++i)
19685 {
19686 int left, right;
19687 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
19688 if (x - left < 0)
19689 k = i;
19690 x += glyphs[i].pixel_width;
19691 }
19692
19693 return k;
19694 }
19695
19696
19697 /* Set background width of glyph string S. START is the index of the
19698 first glyph following S. LAST_X is the right-most x-position + 1
19699 in the drawing area. */
19700
19701 static INLINE void
19702 set_glyph_string_background_width (s, start, last_x)
19703 struct glyph_string *s;
19704 int start;
19705 int last_x;
19706 {
19707 /* If the face of this glyph string has to be drawn to the end of
19708 the drawing area, set S->extends_to_end_of_line_p. */
19709
19710 if (start == s->row->used[s->area]
19711 && s->area == TEXT_AREA
19712 && ((s->row->fill_line_p
19713 && (s->hl == DRAW_NORMAL_TEXT
19714 || s->hl == DRAW_IMAGE_RAISED
19715 || s->hl == DRAW_IMAGE_SUNKEN))
19716 || s->hl == DRAW_MOUSE_FACE))
19717 s->extends_to_end_of_line_p = 1;
19718
19719 /* If S extends its face to the end of the line, set its
19720 background_width to the distance to the right edge of the drawing
19721 area. */
19722 if (s->extends_to_end_of_line_p)
19723 s->background_width = last_x - s->x + 1;
19724 else
19725 s->background_width = s->width;
19726 }
19727
19728
19729 /* Compute overhangs and x-positions for glyph string S and its
19730 predecessors, or successors. X is the starting x-position for S.
19731 BACKWARD_P non-zero means process predecessors. */
19732
19733 static void
19734 compute_overhangs_and_x (s, x, backward_p)
19735 struct glyph_string *s;
19736 int x;
19737 int backward_p;
19738 {
19739 if (backward_p)
19740 {
19741 while (s)
19742 {
19743 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
19744 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
19745 x -= s->width;
19746 s->x = x;
19747 s = s->prev;
19748 }
19749 }
19750 else
19751 {
19752 while (s)
19753 {
19754 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
19755 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
19756 s->x = x;
19757 x += s->width;
19758 s = s->next;
19759 }
19760 }
19761 }
19762
19763
19764
19765 /* The following macros are only called from draw_glyphs below.
19766 They reference the following parameters of that function directly:
19767 `w', `row', `area', and `overlap_p'
19768 as well as the following local variables:
19769 `s', `f', and `hdc' (in W32) */
19770
19771 #ifdef HAVE_NTGUI
19772 /* On W32, silently add local `hdc' variable to argument list of
19773 init_glyph_string. */
19774 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
19775 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
19776 #else
19777 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
19778 init_glyph_string (s, char2b, w, row, area, start, hl)
19779 #endif
19780
19781 /* Add a glyph string for a stretch glyph to the list of strings
19782 between HEAD and TAIL. START is the index of the stretch glyph in
19783 row area AREA of glyph row ROW. END is the index of the last glyph
19784 in that glyph row area. X is the current output position assigned
19785 to the new glyph string constructed. HL overrides that face of the
19786 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
19787 is the right-most x-position of the drawing area. */
19788
19789 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
19790 and below -- keep them on one line. */
19791 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
19792 do \
19793 { \
19794 s = (struct glyph_string *) alloca (sizeof *s); \
19795 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
19796 START = fill_stretch_glyph_string (s, row, area, START, END); \
19797 append_glyph_string (&HEAD, &TAIL, s); \
19798 s->x = (X); \
19799 } \
19800 while (0)
19801
19802
19803 /* Add a glyph string for an image glyph to the list of strings
19804 between HEAD and TAIL. START is the index of the image glyph in
19805 row area AREA of glyph row ROW. END is the index of the last glyph
19806 in that glyph row area. X is the current output position assigned
19807 to the new glyph string constructed. HL overrides that face of the
19808 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
19809 is the right-most x-position of the drawing area. */
19810
19811 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
19812 do \
19813 { \
19814 s = (struct glyph_string *) alloca (sizeof *s); \
19815 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
19816 fill_image_glyph_string (s); \
19817 append_glyph_string (&HEAD, &TAIL, s); \
19818 ++START; \
19819 s->x = (X); \
19820 } \
19821 while (0)
19822
19823
19824 /* Add a glyph string for a sequence of character glyphs to the list
19825 of strings between HEAD and TAIL. START is the index of the first
19826 glyph in row area AREA of glyph row ROW that is part of the new
19827 glyph string. END is the index of the last glyph in that glyph row
19828 area. X is the current output position assigned to the new glyph
19829 string constructed. HL overrides that face of the glyph; e.g. it
19830 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
19831 right-most x-position of the drawing area. */
19832
19833 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
19834 do \
19835 { \
19836 int face_id; \
19837 XChar2b *char2b; \
19838 \
19839 face_id = (row)->glyphs[area][START].face_id; \
19840 \
19841 s = (struct glyph_string *) alloca (sizeof *s); \
19842 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
19843 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
19844 append_glyph_string (&HEAD, &TAIL, s); \
19845 s->x = (X); \
19846 START = fill_glyph_string (s, face_id, START, END, overlaps); \
19847 } \
19848 while (0)
19849
19850
19851 /* Add a glyph string for a composite sequence to the list of strings
19852 between HEAD and TAIL. START is the index of the first glyph in
19853 row area AREA of glyph row ROW that is part of the new glyph
19854 string. END is the index of the last glyph in that glyph row area.
19855 X is the current output position assigned to the new glyph string
19856 constructed. HL overrides that face of the glyph; e.g. it is
19857 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
19858 x-position of the drawing area. */
19859
19860 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
19861 do { \
19862 int face_id = (row)->glyphs[area][START].face_id; \
19863 struct face *base_face = FACE_FROM_ID (f, face_id); \
19864 int cmp_id = (row)->glyphs[area][START].u.cmp_id; \
19865 struct composition *cmp = composition_table[cmp_id]; \
19866 XChar2b *char2b; \
19867 struct glyph_string *first_s; \
19868 int n; \
19869 \
19870 char2b = (XChar2b *) alloca ((sizeof *char2b) * cmp->glyph_len); \
19871 base_face = base_face->ascii_face; \
19872 \
19873 /* Make glyph_strings for each glyph sequence that is drawable by \
19874 the same face, and append them to HEAD/TAIL. */ \
19875 for (n = 0; n < cmp->glyph_len;) \
19876 { \
19877 s = (struct glyph_string *) alloca (sizeof *s); \
19878 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
19879 append_glyph_string (&(HEAD), &(TAIL), s); \
19880 s->cmp = cmp; \
19881 s->gidx = n; \
19882 s->x = (X); \
19883 if (n == 0) \
19884 first_s = s; \
19885 n = fill_composite_glyph_string (s, base_face, overlaps); \
19886 } \
19887 \
19888 ++START; \
19889 s = first_s; \
19890 } while (0)
19891
19892
19893 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
19894 of AREA of glyph row ROW on window W between indices START and END.
19895 HL overrides the face for drawing glyph strings, e.g. it is
19896 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
19897 x-positions of the drawing area.
19898
19899 This is an ugly monster macro construct because we must use alloca
19900 to allocate glyph strings (because draw_glyphs can be called
19901 asynchronously). */
19902
19903 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
19904 do \
19905 { \
19906 HEAD = TAIL = NULL; \
19907 while (START < END) \
19908 { \
19909 struct glyph *first_glyph = (row)->glyphs[area] + START; \
19910 switch (first_glyph->type) \
19911 { \
19912 case CHAR_GLYPH: \
19913 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
19914 HL, X, LAST_X); \
19915 break; \
19916 \
19917 case COMPOSITE_GLYPH: \
19918 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
19919 HL, X, LAST_X); \
19920 break; \
19921 \
19922 case STRETCH_GLYPH: \
19923 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
19924 HL, X, LAST_X); \
19925 break; \
19926 \
19927 case IMAGE_GLYPH: \
19928 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
19929 HL, X, LAST_X); \
19930 break; \
19931 \
19932 default: \
19933 abort (); \
19934 } \
19935 \
19936 if (s) \
19937 { \
19938 set_glyph_string_background_width (s, START, LAST_X); \
19939 (X) += s->width; \
19940 } \
19941 } \
19942 } \
19943 while (0)
19944
19945
19946 /* Draw glyphs between START and END in AREA of ROW on window W,
19947 starting at x-position X. X is relative to AREA in W. HL is a
19948 face-override with the following meaning:
19949
19950 DRAW_NORMAL_TEXT draw normally
19951 DRAW_CURSOR draw in cursor face
19952 DRAW_MOUSE_FACE draw in mouse face.
19953 DRAW_INVERSE_VIDEO draw in mode line face
19954 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
19955 DRAW_IMAGE_RAISED draw an image with a raised relief around it
19956
19957 If OVERLAPS is non-zero, draw only the foreground of characters and
19958 clip to the physical height of ROW. Non-zero value also defines
19959 the overlapping part to be drawn:
19960
19961 OVERLAPS_PRED overlap with preceding rows
19962 OVERLAPS_SUCC overlap with succeeding rows
19963 OVERLAPS_BOTH overlap with both preceding/succeeding rows
19964 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
19965
19966 Value is the x-position reached, relative to AREA of W. */
19967
19968 static int
19969 draw_glyphs (w, x, row, area, start, end, hl, overlaps)
19970 struct window *w;
19971 int x;
19972 struct glyph_row *row;
19973 enum glyph_row_area area;
19974 EMACS_INT start, end;
19975 enum draw_glyphs_face hl;
19976 int overlaps;
19977 {
19978 struct glyph_string *head, *tail;
19979 struct glyph_string *s;
19980 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
19981 int last_x, area_width;
19982 int x_reached;
19983 int i, j;
19984 struct frame *f = XFRAME (WINDOW_FRAME (w));
19985 DECLARE_HDC (hdc);
19986
19987 ALLOCATE_HDC (hdc, f);
19988
19989 /* Let's rather be paranoid than getting a SEGV. */
19990 end = min (end, row->used[area]);
19991 start = max (0, start);
19992 start = min (end, start);
19993
19994 /* Translate X to frame coordinates. Set last_x to the right
19995 end of the drawing area. */
19996 if (row->full_width_p)
19997 {
19998 /* X is relative to the left edge of W, without scroll bars
19999 or fringes. */
20000 x += WINDOW_LEFT_EDGE_X (w);
20001 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
20002 }
20003 else
20004 {
20005 int area_left = window_box_left (w, area);
20006 x += area_left;
20007 area_width = window_box_width (w, area);
20008 last_x = area_left + area_width;
20009 }
20010
20011 /* Build a doubly-linked list of glyph_string structures between
20012 head and tail from what we have to draw. Note that the macro
20013 BUILD_GLYPH_STRINGS will modify its start parameter. That's
20014 the reason we use a separate variable `i'. */
20015 i = start;
20016 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
20017 if (tail)
20018 x_reached = tail->x + tail->background_width;
20019 else
20020 x_reached = x;
20021
20022 /* If there are any glyphs with lbearing < 0 or rbearing > width in
20023 the row, redraw some glyphs in front or following the glyph
20024 strings built above. */
20025 if (head && !overlaps && row->contains_overlapping_glyphs_p)
20026 {
20027 int dummy_x = 0;
20028 struct glyph_string *h, *t;
20029
20030 /* Compute overhangs for all glyph strings. */
20031 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
20032 for (s = head; s; s = s->next)
20033 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
20034
20035 /* Prepend glyph strings for glyphs in front of the first glyph
20036 string that are overwritten because of the first glyph
20037 string's left overhang. The background of all strings
20038 prepended must be drawn because the first glyph string
20039 draws over it. */
20040 i = left_overwritten (head);
20041 if (i >= 0)
20042 {
20043 j = i;
20044 BUILD_GLYPH_STRINGS (j, start, h, t,
20045 DRAW_NORMAL_TEXT, dummy_x, last_x);
20046 start = i;
20047 compute_overhangs_and_x (t, head->x, 1);
20048 prepend_glyph_string_lists (&head, &tail, h, t);
20049 clip_head = head;
20050 }
20051
20052 /* Prepend glyph strings for glyphs in front of the first glyph
20053 string that overwrite that glyph string because of their
20054 right overhang. For these strings, only the foreground must
20055 be drawn, because it draws over the glyph string at `head'.
20056 The background must not be drawn because this would overwrite
20057 right overhangs of preceding glyphs for which no glyph
20058 strings exist. */
20059 i = left_overwriting (head);
20060 if (i >= 0)
20061 {
20062 clip_head = head;
20063 BUILD_GLYPH_STRINGS (i, start, h, t,
20064 DRAW_NORMAL_TEXT, dummy_x, last_x);
20065 for (s = h; s; s = s->next)
20066 s->background_filled_p = 1;
20067 compute_overhangs_and_x (t, head->x, 1);
20068 prepend_glyph_string_lists (&head, &tail, h, t);
20069 }
20070
20071 /* Append glyphs strings for glyphs following the last glyph
20072 string tail that are overwritten by tail. The background of
20073 these strings has to be drawn because tail's foreground draws
20074 over it. */
20075 i = right_overwritten (tail);
20076 if (i >= 0)
20077 {
20078 BUILD_GLYPH_STRINGS (end, i, h, t,
20079 DRAW_NORMAL_TEXT, x, last_x);
20080 compute_overhangs_and_x (h, tail->x + tail->width, 0);
20081 append_glyph_string_lists (&head, &tail, h, t);
20082 clip_tail = tail;
20083 }
20084
20085 /* Append glyph strings for glyphs following the last glyph
20086 string tail that overwrite tail. The foreground of such
20087 glyphs has to be drawn because it writes into the background
20088 of tail. The background must not be drawn because it could
20089 paint over the foreground of following glyphs. */
20090 i = right_overwriting (tail);
20091 if (i >= 0)
20092 {
20093 clip_tail = tail;
20094 i++; /* We must include the Ith glyph. */
20095 BUILD_GLYPH_STRINGS (end, i, h, t,
20096 DRAW_NORMAL_TEXT, x, last_x);
20097 for (s = h; s; s = s->next)
20098 s->background_filled_p = 1;
20099 compute_overhangs_and_x (h, tail->x + tail->width, 0);
20100 append_glyph_string_lists (&head, &tail, h, t);
20101 }
20102 if (clip_head || clip_tail)
20103 for (s = head; s; s = s->next)
20104 {
20105 s->clip_head = clip_head;
20106 s->clip_tail = clip_tail;
20107 }
20108 }
20109
20110 /* Draw all strings. */
20111 for (s = head; s; s = s->next)
20112 FRAME_RIF (f)->draw_glyph_string (s);
20113
20114 if (area == TEXT_AREA
20115 && !row->full_width_p
20116 /* When drawing overlapping rows, only the glyph strings'
20117 foreground is drawn, which doesn't erase a cursor
20118 completely. */
20119 && !overlaps)
20120 {
20121 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
20122 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
20123 : (tail ? tail->x + tail->background_width : x));
20124
20125 int text_left = window_box_left (w, TEXT_AREA);
20126 x0 -= text_left;
20127 x1 -= text_left;
20128
20129 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
20130 row->y, MATRIX_ROW_BOTTOM_Y (row));
20131 }
20132
20133 /* Value is the x-position up to which drawn, relative to AREA of W.
20134 This doesn't include parts drawn because of overhangs. */
20135 if (row->full_width_p)
20136 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
20137 else
20138 x_reached -= window_box_left (w, area);
20139
20140 RELEASE_HDC (hdc, f);
20141
20142 return x_reached;
20143 }
20144
20145 /* Expand row matrix if too narrow. Don't expand if area
20146 is not present. */
20147
20148 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
20149 { \
20150 if (!fonts_changed_p \
20151 && (it->glyph_row->glyphs[area] \
20152 < it->glyph_row->glyphs[area + 1])) \
20153 { \
20154 it->w->ncols_scale_factor++; \
20155 fonts_changed_p = 1; \
20156 } \
20157 }
20158
20159 /* Store one glyph for IT->char_to_display in IT->glyph_row.
20160 Called from x_produce_glyphs when IT->glyph_row is non-null. */
20161
20162 static INLINE void
20163 append_glyph (it)
20164 struct it *it;
20165 {
20166 struct glyph *glyph;
20167 enum glyph_row_area area = it->area;
20168
20169 xassert (it->glyph_row);
20170 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
20171
20172 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
20173 if (glyph < it->glyph_row->glyphs[area + 1])
20174 {
20175 glyph->charpos = CHARPOS (it->position);
20176 glyph->object = it->object;
20177 glyph->pixel_width = it->pixel_width;
20178 glyph->ascent = it->ascent;
20179 glyph->descent = it->descent;
20180 glyph->voffset = it->voffset;
20181 glyph->type = CHAR_GLYPH;
20182 glyph->multibyte_p = it->multibyte_p;
20183 glyph->left_box_line_p = it->start_of_box_run_p;
20184 glyph->right_box_line_p = it->end_of_box_run_p;
20185 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
20186 || it->phys_descent > it->descent);
20187 glyph->padding_p = 0;
20188 glyph->glyph_not_available_p = it->glyph_not_available_p;
20189 glyph->face_id = it->face_id;
20190 glyph->u.ch = it->char_to_display;
20191 glyph->slice = null_glyph_slice;
20192 glyph->font_type = FONT_TYPE_UNKNOWN;
20193 ++it->glyph_row->used[area];
20194 }
20195 else
20196 IT_EXPAND_MATRIX_WIDTH (it, area);
20197 }
20198
20199 /* Store one glyph for the composition IT->cmp_id in IT->glyph_row.
20200 Called from x_produce_glyphs when IT->glyph_row is non-null. */
20201
20202 static INLINE void
20203 append_composite_glyph (it)
20204 struct it *it;
20205 {
20206 struct glyph *glyph;
20207 enum glyph_row_area area = it->area;
20208
20209 xassert (it->glyph_row);
20210
20211 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
20212 if (glyph < it->glyph_row->glyphs[area + 1])
20213 {
20214 glyph->charpos = CHARPOS (it->position);
20215 glyph->object = it->object;
20216 glyph->pixel_width = it->pixel_width;
20217 glyph->ascent = it->ascent;
20218 glyph->descent = it->descent;
20219 glyph->voffset = it->voffset;
20220 glyph->type = COMPOSITE_GLYPH;
20221 glyph->multibyte_p = it->multibyte_p;
20222 glyph->left_box_line_p = it->start_of_box_run_p;
20223 glyph->right_box_line_p = it->end_of_box_run_p;
20224 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
20225 || it->phys_descent > it->descent);
20226 glyph->padding_p = 0;
20227 glyph->glyph_not_available_p = 0;
20228 glyph->face_id = it->face_id;
20229 glyph->u.cmp_id = it->cmp_id;
20230 glyph->slice = null_glyph_slice;
20231 glyph->font_type = FONT_TYPE_UNKNOWN;
20232 ++it->glyph_row->used[area];
20233 }
20234 else
20235 IT_EXPAND_MATRIX_WIDTH (it, area);
20236 }
20237
20238
20239 /* Change IT->ascent and IT->height according to the setting of
20240 IT->voffset. */
20241
20242 static INLINE void
20243 take_vertical_position_into_account (it)
20244 struct it *it;
20245 {
20246 if (it->voffset)
20247 {
20248 if (it->voffset < 0)
20249 /* Increase the ascent so that we can display the text higher
20250 in the line. */
20251 it->ascent -= it->voffset;
20252 else
20253 /* Increase the descent so that we can display the text lower
20254 in the line. */
20255 it->descent += it->voffset;
20256 }
20257 }
20258
20259
20260 /* Produce glyphs/get display metrics for the image IT is loaded with.
20261 See the description of struct display_iterator in dispextern.h for
20262 an overview of struct display_iterator. */
20263
20264 static void
20265 produce_image_glyph (it)
20266 struct it *it;
20267 {
20268 struct image *img;
20269 struct face *face;
20270 int glyph_ascent, crop;
20271 struct glyph_slice slice;
20272
20273 xassert (it->what == IT_IMAGE);
20274
20275 face = FACE_FROM_ID (it->f, it->face_id);
20276 xassert (face);
20277 /* Make sure X resources of the face is loaded. */
20278 PREPARE_FACE_FOR_DISPLAY (it->f, face);
20279
20280 if (it->image_id < 0)
20281 {
20282 /* Fringe bitmap. */
20283 it->ascent = it->phys_ascent = 0;
20284 it->descent = it->phys_descent = 0;
20285 it->pixel_width = 0;
20286 it->nglyphs = 0;
20287 return;
20288 }
20289
20290 img = IMAGE_FROM_ID (it->f, it->image_id);
20291 xassert (img);
20292 /* Make sure X resources of the image is loaded. */
20293 prepare_image_for_display (it->f, img);
20294
20295 slice.x = slice.y = 0;
20296 slice.width = img->width;
20297 slice.height = img->height;
20298
20299 if (INTEGERP (it->slice.x))
20300 slice.x = XINT (it->slice.x);
20301 else if (FLOATP (it->slice.x))
20302 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
20303
20304 if (INTEGERP (it->slice.y))
20305 slice.y = XINT (it->slice.y);
20306 else if (FLOATP (it->slice.y))
20307 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
20308
20309 if (INTEGERP (it->slice.width))
20310 slice.width = XINT (it->slice.width);
20311 else if (FLOATP (it->slice.width))
20312 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
20313
20314 if (INTEGERP (it->slice.height))
20315 slice.height = XINT (it->slice.height);
20316 else if (FLOATP (it->slice.height))
20317 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
20318
20319 if (slice.x >= img->width)
20320 slice.x = img->width;
20321 if (slice.y >= img->height)
20322 slice.y = img->height;
20323 if (slice.x + slice.width >= img->width)
20324 slice.width = img->width - slice.x;
20325 if (slice.y + slice.height > img->height)
20326 slice.height = img->height - slice.y;
20327
20328 if (slice.width == 0 || slice.height == 0)
20329 return;
20330
20331 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
20332
20333 it->descent = slice.height - glyph_ascent;
20334 if (slice.y == 0)
20335 it->descent += img->vmargin;
20336 if (slice.y + slice.height == img->height)
20337 it->descent += img->vmargin;
20338 it->phys_descent = it->descent;
20339
20340 it->pixel_width = slice.width;
20341 if (slice.x == 0)
20342 it->pixel_width += img->hmargin;
20343 if (slice.x + slice.width == img->width)
20344 it->pixel_width += img->hmargin;
20345
20346 /* It's quite possible for images to have an ascent greater than
20347 their height, so don't get confused in that case. */
20348 if (it->descent < 0)
20349 it->descent = 0;
20350
20351 #if 0 /* this breaks image tiling */
20352 /* If this glyph is alone on the last line, adjust it.ascent to minimum row ascent. */
20353 int face_ascent = face->font ? FONT_BASE (face->font) : FRAME_BASELINE_OFFSET (it->f);
20354 if (face_ascent > it->ascent)
20355 it->ascent = it->phys_ascent = face_ascent;
20356 #endif
20357
20358 it->nglyphs = 1;
20359
20360 if (face->box != FACE_NO_BOX)
20361 {
20362 if (face->box_line_width > 0)
20363 {
20364 if (slice.y == 0)
20365 it->ascent += face->box_line_width;
20366 if (slice.y + slice.height == img->height)
20367 it->descent += face->box_line_width;
20368 }
20369
20370 if (it->start_of_box_run_p && slice.x == 0)
20371 it->pixel_width += eabs (face->box_line_width);
20372 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
20373 it->pixel_width += eabs (face->box_line_width);
20374 }
20375
20376 take_vertical_position_into_account (it);
20377
20378 /* Automatically crop wide image glyphs at right edge so we can
20379 draw the cursor on same display row. */
20380 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
20381 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
20382 {
20383 it->pixel_width -= crop;
20384 slice.width -= crop;
20385 }
20386
20387 if (it->glyph_row)
20388 {
20389 struct glyph *glyph;
20390 enum glyph_row_area area = it->area;
20391
20392 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
20393 if (glyph < it->glyph_row->glyphs[area + 1])
20394 {
20395 glyph->charpos = CHARPOS (it->position);
20396 glyph->object = it->object;
20397 glyph->pixel_width = it->pixel_width;
20398 glyph->ascent = glyph_ascent;
20399 glyph->descent = it->descent;
20400 glyph->voffset = it->voffset;
20401 glyph->type = IMAGE_GLYPH;
20402 glyph->multibyte_p = it->multibyte_p;
20403 glyph->left_box_line_p = it->start_of_box_run_p;
20404 glyph->right_box_line_p = it->end_of_box_run_p;
20405 glyph->overlaps_vertically_p = 0;
20406 glyph->padding_p = 0;
20407 glyph->glyph_not_available_p = 0;
20408 glyph->face_id = it->face_id;
20409 glyph->u.img_id = img->id;
20410 glyph->slice = slice;
20411 glyph->font_type = FONT_TYPE_UNKNOWN;
20412 ++it->glyph_row->used[area];
20413 }
20414 else
20415 IT_EXPAND_MATRIX_WIDTH (it, area);
20416 }
20417 }
20418
20419
20420 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
20421 of the glyph, WIDTH and HEIGHT are the width and height of the
20422 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
20423
20424 static void
20425 append_stretch_glyph (it, object, width, height, ascent)
20426 struct it *it;
20427 Lisp_Object object;
20428 int width, height;
20429 int ascent;
20430 {
20431 struct glyph *glyph;
20432 enum glyph_row_area area = it->area;
20433
20434 xassert (ascent >= 0 && ascent <= height);
20435
20436 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
20437 if (glyph < it->glyph_row->glyphs[area + 1])
20438 {
20439 glyph->charpos = CHARPOS (it->position);
20440 glyph->object = object;
20441 glyph->pixel_width = width;
20442 glyph->ascent = ascent;
20443 glyph->descent = height - ascent;
20444 glyph->voffset = it->voffset;
20445 glyph->type = STRETCH_GLYPH;
20446 glyph->multibyte_p = it->multibyte_p;
20447 glyph->left_box_line_p = it->start_of_box_run_p;
20448 glyph->right_box_line_p = it->end_of_box_run_p;
20449 glyph->overlaps_vertically_p = 0;
20450 glyph->padding_p = 0;
20451 glyph->glyph_not_available_p = 0;
20452 glyph->face_id = it->face_id;
20453 glyph->u.stretch.ascent = ascent;
20454 glyph->u.stretch.height = height;
20455 glyph->slice = null_glyph_slice;
20456 glyph->font_type = FONT_TYPE_UNKNOWN;
20457 ++it->glyph_row->used[area];
20458 }
20459 else
20460 IT_EXPAND_MATRIX_WIDTH (it, area);
20461 }
20462
20463
20464 /* Produce a stretch glyph for iterator IT. IT->object is the value
20465 of the glyph property displayed. The value must be a list
20466 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
20467 being recognized:
20468
20469 1. `:width WIDTH' specifies that the space should be WIDTH *
20470 canonical char width wide. WIDTH may be an integer or floating
20471 point number.
20472
20473 2. `:relative-width FACTOR' specifies that the width of the stretch
20474 should be computed from the width of the first character having the
20475 `glyph' property, and should be FACTOR times that width.
20476
20477 3. `:align-to HPOS' specifies that the space should be wide enough
20478 to reach HPOS, a value in canonical character units.
20479
20480 Exactly one of the above pairs must be present.
20481
20482 4. `:height HEIGHT' specifies that the height of the stretch produced
20483 should be HEIGHT, measured in canonical character units.
20484
20485 5. `:relative-height FACTOR' specifies that the height of the
20486 stretch should be FACTOR times the height of the characters having
20487 the glyph property.
20488
20489 Either none or exactly one of 4 or 5 must be present.
20490
20491 6. `:ascent ASCENT' specifies that ASCENT percent of the height
20492 of the stretch should be used for the ascent of the stretch.
20493 ASCENT must be in the range 0 <= ASCENT <= 100. */
20494
20495 static void
20496 produce_stretch_glyph (it)
20497 struct it *it;
20498 {
20499 /* (space :width WIDTH :height HEIGHT ...) */
20500 Lisp_Object prop, plist;
20501 int width = 0, height = 0, align_to = -1;
20502 int zero_width_ok_p = 0, zero_height_ok_p = 0;
20503 int ascent = 0;
20504 double tem;
20505 struct face *face = FACE_FROM_ID (it->f, it->face_id);
20506 XFontStruct *font = face->font ? face->font : FRAME_FONT (it->f);
20507
20508 PREPARE_FACE_FOR_DISPLAY (it->f, face);
20509
20510 /* List should start with `space'. */
20511 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
20512 plist = XCDR (it->object);
20513
20514 /* Compute the width of the stretch. */
20515 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
20516 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
20517 {
20518 /* Absolute width `:width WIDTH' specified and valid. */
20519 zero_width_ok_p = 1;
20520 width = (int)tem;
20521 }
20522 else if (prop = Fplist_get (plist, QCrelative_width),
20523 NUMVAL (prop) > 0)
20524 {
20525 /* Relative width `:relative-width FACTOR' specified and valid.
20526 Compute the width of the characters having the `glyph'
20527 property. */
20528 struct it it2;
20529 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
20530
20531 it2 = *it;
20532 if (it->multibyte_p)
20533 {
20534 int maxlen = ((IT_BYTEPOS (*it) >= GPT ? ZV : GPT)
20535 - IT_BYTEPOS (*it));
20536 it2.c = STRING_CHAR_AND_LENGTH (p, maxlen, it2.len);
20537 }
20538 else
20539 it2.c = *p, it2.len = 1;
20540
20541 it2.glyph_row = NULL;
20542 it2.what = IT_CHARACTER;
20543 x_produce_glyphs (&it2);
20544 width = NUMVAL (prop) * it2.pixel_width;
20545 }
20546 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
20547 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
20548 {
20549 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
20550 align_to = (align_to < 0
20551 ? 0
20552 : align_to - window_box_left_offset (it->w, TEXT_AREA));
20553 else if (align_to < 0)
20554 align_to = window_box_left_offset (it->w, TEXT_AREA);
20555 width = max (0, (int)tem + align_to - it->current_x);
20556 zero_width_ok_p = 1;
20557 }
20558 else
20559 /* Nothing specified -> width defaults to canonical char width. */
20560 width = FRAME_COLUMN_WIDTH (it->f);
20561
20562 if (width <= 0 && (width < 0 || !zero_width_ok_p))
20563 width = 1;
20564
20565 /* Compute height. */
20566 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
20567 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
20568 {
20569 height = (int)tem;
20570 zero_height_ok_p = 1;
20571 }
20572 else if (prop = Fplist_get (plist, QCrelative_height),
20573 NUMVAL (prop) > 0)
20574 height = FONT_HEIGHT (font) * NUMVAL (prop);
20575 else
20576 height = FONT_HEIGHT (font);
20577
20578 if (height <= 0 && (height < 0 || !zero_height_ok_p))
20579 height = 1;
20580
20581 /* Compute percentage of height used for ascent. If
20582 `:ascent ASCENT' is present and valid, use that. Otherwise,
20583 derive the ascent from the font in use. */
20584 if (prop = Fplist_get (plist, QCascent),
20585 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
20586 ascent = height * NUMVAL (prop) / 100.0;
20587 else if (!NILP (prop)
20588 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
20589 ascent = min (max (0, (int)tem), height);
20590 else
20591 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
20592
20593 if (width > 0 && !it->truncate_lines_p
20594 && it->current_x + width > it->last_visible_x)
20595 width = it->last_visible_x - it->current_x - 1;
20596
20597 if (width > 0 && height > 0 && it->glyph_row)
20598 {
20599 Lisp_Object object = it->stack[it->sp - 1].string;
20600 if (!STRINGP (object))
20601 object = it->w->buffer;
20602 append_stretch_glyph (it, object, width, height, ascent);
20603 }
20604
20605 it->pixel_width = width;
20606 it->ascent = it->phys_ascent = ascent;
20607 it->descent = it->phys_descent = height - it->ascent;
20608 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
20609
20610 take_vertical_position_into_account (it);
20611 }
20612
20613 /* Get line-height and line-spacing property at point.
20614 If line-height has format (HEIGHT TOTAL), return TOTAL
20615 in TOTAL_HEIGHT. */
20616
20617 static Lisp_Object
20618 get_line_height_property (it, prop)
20619 struct it *it;
20620 Lisp_Object prop;
20621 {
20622 Lisp_Object position;
20623
20624 if (STRINGP (it->object))
20625 position = make_number (IT_STRING_CHARPOS (*it));
20626 else if (BUFFERP (it->object))
20627 position = make_number (IT_CHARPOS (*it));
20628 else
20629 return Qnil;
20630
20631 return Fget_char_property (position, prop, it->object);
20632 }
20633
20634 /* Calculate line-height and line-spacing properties.
20635 An integer value specifies explicit pixel value.
20636 A float value specifies relative value to current face height.
20637 A cons (float . face-name) specifies relative value to
20638 height of specified face font.
20639
20640 Returns height in pixels, or nil. */
20641
20642
20643 static Lisp_Object
20644 calc_line_height_property (it, val, font, boff, override)
20645 struct it *it;
20646 Lisp_Object val;
20647 XFontStruct *font;
20648 int boff, override;
20649 {
20650 Lisp_Object face_name = Qnil;
20651 int ascent, descent, height;
20652
20653 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
20654 return val;
20655
20656 if (CONSP (val))
20657 {
20658 face_name = XCAR (val);
20659 val = XCDR (val);
20660 if (!NUMBERP (val))
20661 val = make_number (1);
20662 if (NILP (face_name))
20663 {
20664 height = it->ascent + it->descent;
20665 goto scale;
20666 }
20667 }
20668
20669 if (NILP (face_name))
20670 {
20671 font = FRAME_FONT (it->f);
20672 boff = FRAME_BASELINE_OFFSET (it->f);
20673 }
20674 else if (EQ (face_name, Qt))
20675 {
20676 override = 0;
20677 }
20678 else
20679 {
20680 int face_id;
20681 struct face *face;
20682 struct font_info *font_info;
20683
20684 face_id = lookup_named_face (it->f, face_name, 0);
20685 if (face_id < 0)
20686 return make_number (-1);
20687
20688 face = FACE_FROM_ID (it->f, face_id);
20689 font = face->font;
20690 if (font == NULL)
20691 return make_number (-1);
20692
20693 font_info = FONT_INFO_FROM_FACE (it->f, face);
20694 boff = font_info->baseline_offset;
20695 if (font_info->vertical_centering)
20696 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
20697 }
20698
20699 ascent = FONT_BASE (font) + boff;
20700 descent = FONT_DESCENT (font) - boff;
20701
20702 if (override)
20703 {
20704 it->override_ascent = ascent;
20705 it->override_descent = descent;
20706 it->override_boff = boff;
20707 }
20708
20709 height = ascent + descent;
20710
20711 scale:
20712 if (FLOATP (val))
20713 height = (int)(XFLOAT_DATA (val) * height);
20714 else if (INTEGERP (val))
20715 height *= XINT (val);
20716
20717 return make_number (height);
20718 }
20719
20720
20721 /* RIF:
20722 Produce glyphs/get display metrics for the display element IT is
20723 loaded with. See the description of struct it in dispextern.h
20724 for an overview of struct it. */
20725
20726 void
20727 x_produce_glyphs (it)
20728 struct it *it;
20729 {
20730 int extra_line_spacing = it->extra_line_spacing;
20731
20732 it->glyph_not_available_p = 0;
20733
20734 if (it->what == IT_CHARACTER)
20735 {
20736 XChar2b char2b;
20737 XFontStruct *font;
20738 struct face *face = FACE_FROM_ID (it->f, it->face_id);
20739 XCharStruct *pcm;
20740 int font_not_found_p;
20741 struct font_info *font_info;
20742 int boff; /* baseline offset */
20743 /* We may change it->multibyte_p upon unibyte<->multibyte
20744 conversion. So, save the current value now and restore it
20745 later.
20746
20747 Note: It seems that we don't have to record multibyte_p in
20748 struct glyph because the character code itself tells if or
20749 not the character is multibyte. Thus, in the future, we must
20750 consider eliminating the field `multibyte_p' in the struct
20751 glyph. */
20752 int saved_multibyte_p = it->multibyte_p;
20753
20754 /* Maybe translate single-byte characters to multibyte, or the
20755 other way. */
20756 it->char_to_display = it->c;
20757 if (!ASCII_BYTE_P (it->c)
20758 && ! it->multibyte_p)
20759 {
20760 if (SINGLE_BYTE_CHAR_P (it->c)
20761 && unibyte_display_via_language_environment)
20762 it->char_to_display = unibyte_char_to_multibyte (it->c);
20763 if (! SINGLE_BYTE_CHAR_P (it->char_to_display))
20764 {
20765 it->multibyte_p = 1;
20766 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display,
20767 -1, Qnil);
20768 face = FACE_FROM_ID (it->f, it->face_id);
20769 }
20770 }
20771
20772 /* Get font to use. Encode IT->char_to_display. */
20773 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
20774 &char2b, it->multibyte_p, 0);
20775 font = face->font;
20776
20777 /* When no suitable font found, use the default font. */
20778 font_not_found_p = font == NULL;
20779 if (font_not_found_p)
20780 {
20781 font = FRAME_FONT (it->f);
20782 boff = FRAME_BASELINE_OFFSET (it->f);
20783 font_info = NULL;
20784 }
20785 else
20786 {
20787 font_info = FONT_INFO_FROM_FACE (it->f, face);
20788 boff = font_info->baseline_offset;
20789 if (font_info->vertical_centering)
20790 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
20791 }
20792
20793 if (it->char_to_display >= ' '
20794 && (!it->multibyte_p || it->char_to_display < 128))
20795 {
20796 /* Either unibyte or ASCII. */
20797 int stretched_p;
20798
20799 it->nglyphs = 1;
20800
20801 pcm = get_per_char_metric (it->f, font, font_info, &char2b,
20802 FONT_TYPE_FOR_UNIBYTE (font, it->char_to_display));
20803
20804 if (it->override_ascent >= 0)
20805 {
20806 it->ascent = it->override_ascent;
20807 it->descent = it->override_descent;
20808 boff = it->override_boff;
20809 }
20810 else
20811 {
20812 it->ascent = FONT_BASE (font) + boff;
20813 it->descent = FONT_DESCENT (font) - boff;
20814 }
20815
20816 if (pcm)
20817 {
20818 it->phys_ascent = pcm->ascent + boff;
20819 it->phys_descent = pcm->descent - boff;
20820 it->pixel_width = pcm->width;
20821 }
20822 else
20823 {
20824 it->glyph_not_available_p = 1;
20825 it->phys_ascent = it->ascent;
20826 it->phys_descent = it->descent;
20827 it->pixel_width = FONT_WIDTH (font);
20828 }
20829
20830 if (it->constrain_row_ascent_descent_p)
20831 {
20832 if (it->descent > it->max_descent)
20833 {
20834 it->ascent += it->descent - it->max_descent;
20835 it->descent = it->max_descent;
20836 }
20837 if (it->ascent > it->max_ascent)
20838 {
20839 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
20840 it->ascent = it->max_ascent;
20841 }
20842 it->phys_ascent = min (it->phys_ascent, it->ascent);
20843 it->phys_descent = min (it->phys_descent, it->descent);
20844 extra_line_spacing = 0;
20845 }
20846
20847 /* If this is a space inside a region of text with
20848 `space-width' property, change its width. */
20849 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
20850 if (stretched_p)
20851 it->pixel_width *= XFLOATINT (it->space_width);
20852
20853 /* If face has a box, add the box thickness to the character
20854 height. If character has a box line to the left and/or
20855 right, add the box line width to the character's width. */
20856 if (face->box != FACE_NO_BOX)
20857 {
20858 int thick = face->box_line_width;
20859
20860 if (thick > 0)
20861 {
20862 it->ascent += thick;
20863 it->descent += thick;
20864 }
20865 else
20866 thick = -thick;
20867
20868 if (it->start_of_box_run_p)
20869 it->pixel_width += thick;
20870 if (it->end_of_box_run_p)
20871 it->pixel_width += thick;
20872 }
20873
20874 /* If face has an overline, add the height of the overline
20875 (1 pixel) and a 1 pixel margin to the character height. */
20876 if (face->overline_p)
20877 it->ascent += overline_margin;
20878
20879 if (it->constrain_row_ascent_descent_p)
20880 {
20881 if (it->ascent > it->max_ascent)
20882 it->ascent = it->max_ascent;
20883 if (it->descent > it->max_descent)
20884 it->descent = it->max_descent;
20885 }
20886
20887 take_vertical_position_into_account (it);
20888
20889 /* If we have to actually produce glyphs, do it. */
20890 if (it->glyph_row)
20891 {
20892 if (stretched_p)
20893 {
20894 /* Translate a space with a `space-width' property
20895 into a stretch glyph. */
20896 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
20897 / FONT_HEIGHT (font));
20898 append_stretch_glyph (it, it->object, it->pixel_width,
20899 it->ascent + it->descent, ascent);
20900 }
20901 else
20902 append_glyph (it);
20903
20904 /* If characters with lbearing or rbearing are displayed
20905 in this line, record that fact in a flag of the
20906 glyph row. This is used to optimize X output code. */
20907 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
20908 it->glyph_row->contains_overlapping_glyphs_p = 1;
20909 }
20910 }
20911 else if (it->char_to_display == '\n')
20912 {
20913 /* A newline has no width but we need the height of the line.
20914 But if previous part of the line set a height, don't
20915 increase that height */
20916
20917 Lisp_Object height;
20918 Lisp_Object total_height = Qnil;
20919
20920 it->override_ascent = -1;
20921 it->pixel_width = 0;
20922 it->nglyphs = 0;
20923
20924 height = get_line_height_property(it, Qline_height);
20925 /* Split (line-height total-height) list */
20926 if (CONSP (height)
20927 && CONSP (XCDR (height))
20928 && NILP (XCDR (XCDR (height))))
20929 {
20930 total_height = XCAR (XCDR (height));
20931 height = XCAR (height);
20932 }
20933 height = calc_line_height_property(it, height, font, boff, 1);
20934
20935 if (it->override_ascent >= 0)
20936 {
20937 it->ascent = it->override_ascent;
20938 it->descent = it->override_descent;
20939 boff = it->override_boff;
20940 }
20941 else
20942 {
20943 it->ascent = FONT_BASE (font) + boff;
20944 it->descent = FONT_DESCENT (font) - boff;
20945 }
20946
20947 if (EQ (height, Qt))
20948 {
20949 if (it->descent > it->max_descent)
20950 {
20951 it->ascent += it->descent - it->max_descent;
20952 it->descent = it->max_descent;
20953 }
20954 if (it->ascent > it->max_ascent)
20955 {
20956 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
20957 it->ascent = it->max_ascent;
20958 }
20959 it->phys_ascent = min (it->phys_ascent, it->ascent);
20960 it->phys_descent = min (it->phys_descent, it->descent);
20961 it->constrain_row_ascent_descent_p = 1;
20962 extra_line_spacing = 0;
20963 }
20964 else
20965 {
20966 Lisp_Object spacing;
20967
20968 it->phys_ascent = it->ascent;
20969 it->phys_descent = it->descent;
20970
20971 if ((it->max_ascent > 0 || it->max_descent > 0)
20972 && face->box != FACE_NO_BOX
20973 && face->box_line_width > 0)
20974 {
20975 it->ascent += face->box_line_width;
20976 it->descent += face->box_line_width;
20977 }
20978 if (!NILP (height)
20979 && XINT (height) > it->ascent + it->descent)
20980 it->ascent = XINT (height) - it->descent;
20981
20982 if (!NILP (total_height))
20983 spacing = calc_line_height_property(it, total_height, font, boff, 0);
20984 else
20985 {
20986 spacing = get_line_height_property(it, Qline_spacing);
20987 spacing = calc_line_height_property(it, spacing, font, boff, 0);
20988 }
20989 if (INTEGERP (spacing))
20990 {
20991 extra_line_spacing = XINT (spacing);
20992 if (!NILP (total_height))
20993 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
20994 }
20995 }
20996 }
20997 else if (it->char_to_display == '\t')
20998 {
20999 int tab_width = it->tab_width * FRAME_SPACE_WIDTH (it->f);
21000 int x = it->current_x + it->continuation_lines_width;
21001 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
21002
21003 /* If the distance from the current position to the next tab
21004 stop is less than a space character width, use the
21005 tab stop after that. */
21006 if (next_tab_x - x < FRAME_SPACE_WIDTH (it->f))
21007 next_tab_x += tab_width;
21008
21009 it->pixel_width = next_tab_x - x;
21010 it->nglyphs = 1;
21011 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
21012 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
21013
21014 if (it->glyph_row)
21015 {
21016 append_stretch_glyph (it, it->object, it->pixel_width,
21017 it->ascent + it->descent, it->ascent);
21018 }
21019 }
21020 else
21021 {
21022 /* A multi-byte character. Assume that the display width of the
21023 character is the width of the character multiplied by the
21024 width of the font. */
21025
21026 /* If we found a font, this font should give us the right
21027 metrics. If we didn't find a font, use the frame's
21028 default font and calculate the width of the character by
21029 multiplying the width of font by the width of the
21030 character. */
21031
21032 pcm = get_per_char_metric (it->f, font, font_info, &char2b,
21033 FONT_TYPE_FOR_MULTIBYTE (font, it->c));
21034
21035 if (font_not_found_p || !pcm)
21036 {
21037 int char_width = CHAR_WIDTH (it->char_to_display);
21038
21039 if (char_width == 0)
21040 /* This is a non spacing character. But, as we are
21041 going to display an empty box, the box must occupy
21042 at least one column. */
21043 char_width = 1;
21044 it->glyph_not_available_p = 1;
21045 it->pixel_width = FRAME_COLUMN_WIDTH (it->f) * char_width;
21046 it->phys_ascent = FONT_BASE (font) + boff;
21047 it->phys_descent = FONT_DESCENT (font) - boff;
21048 }
21049 else
21050 {
21051 it->pixel_width = pcm->width;
21052 it->phys_ascent = pcm->ascent + boff;
21053 it->phys_descent = pcm->descent - boff;
21054 if (it->glyph_row
21055 && (pcm->lbearing < 0
21056 || pcm->rbearing > pcm->width))
21057 it->glyph_row->contains_overlapping_glyphs_p = 1;
21058 }
21059 it->nglyphs = 1;
21060 it->ascent = FONT_BASE (font) + boff;
21061 it->descent = FONT_DESCENT (font) - boff;
21062 if (face->box != FACE_NO_BOX)
21063 {
21064 int thick = face->box_line_width;
21065
21066 if (thick > 0)
21067 {
21068 it->ascent += thick;
21069 it->descent += thick;
21070 }
21071 else
21072 thick = - thick;
21073
21074 if (it->start_of_box_run_p)
21075 it->pixel_width += thick;
21076 if (it->end_of_box_run_p)
21077 it->pixel_width += thick;
21078 }
21079
21080 /* If face has an overline, add the height of the overline
21081 (1 pixel) and a 1 pixel margin to the character height. */
21082 if (face->overline_p)
21083 it->ascent += overline_margin;
21084
21085 take_vertical_position_into_account (it);
21086
21087 if (it->glyph_row)
21088 append_glyph (it);
21089 }
21090 it->multibyte_p = saved_multibyte_p;
21091 }
21092 else if (it->what == IT_COMPOSITION)
21093 {
21094 /* Note: A composition is represented as one glyph in the
21095 glyph matrix. There are no padding glyphs.
21096
21097 Important is that pixel_width, ascent, and descent are the
21098 values of what is drawn by draw_glyphs (i.e. the values of
21099 the overall glyphs composed). */
21100 struct face *face = FACE_FROM_ID (it->f, it->face_id);
21101 int boff; /* baseline offset */
21102 struct composition *cmp = composition_table[it->cmp_id];
21103 int glyph_len = cmp->glyph_len;
21104 XFontStruct *font = face->font;
21105
21106 it->nglyphs = 1;
21107
21108 #ifdef USE_FONT_BACKEND
21109 if (cmp->method == COMPOSITION_WITH_GLYPH_STRING)
21110 {
21111 if (! cmp->font || cmp->font != font)
21112 font_prepare_composition (cmp);
21113 }
21114 else
21115 #endif /* USE_FONT_BACKEND */
21116 /* If we have not yet calculated pixel size data of glyphs of
21117 the composition for the current face font, calculate them
21118 now. Theoretically, we have to check all fonts for the
21119 glyphs, but that requires much time and memory space. So,
21120 here we check only the font of the first glyph. This leads
21121 to incorrect display, but it's very rare, and C-l (recenter)
21122 can correct the display anyway. */
21123 if (! cmp->font || cmp->font != font)
21124 {
21125 /* Ascent and descent of the font of the first character
21126 of this composition (adjusted by baseline offset).
21127 Ascent and descent of overall glyphs should not be less
21128 than them respectively. */
21129 int font_ascent, font_descent, font_height;
21130 /* Bounding box of the overall glyphs. */
21131 int leftmost, rightmost, lowest, highest;
21132 int lbearing, rbearing;
21133 int i, width, ascent, descent;
21134 int left_padded = 0, right_padded = 0;
21135 int face_id;
21136 int c;
21137 XChar2b char2b;
21138 XCharStruct *pcm;
21139 int font_not_found_p;
21140 struct font_info *font_info;
21141 int pos;
21142
21143 for (glyph_len = cmp->glyph_len; glyph_len > 0; glyph_len--)
21144 if ((c = COMPOSITION_GLYPH (cmp, glyph_len - 1)) != '\t')
21145 break;
21146 if (glyph_len < cmp->glyph_len)
21147 right_padded = 1;
21148 for (i = 0; i < glyph_len; i++)
21149 {
21150 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
21151 break;
21152 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
21153 }
21154 if (i > 0)
21155 left_padded = 1;
21156
21157 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
21158 : IT_CHARPOS (*it));
21159 /* When no suitable font found, use the default font. */
21160 font_not_found_p = font == NULL;
21161 if (font_not_found_p)
21162 {
21163 face = face->ascii_face;
21164 font = face->font;
21165 }
21166 font_info = FONT_INFO_FROM_FACE (it->f, face);
21167 boff = font_info->baseline_offset;
21168 if (font_info->vertical_centering)
21169 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
21170 font_ascent = FONT_BASE (font) + boff;
21171 font_descent = FONT_DESCENT (font) - boff;
21172 font_height = FONT_HEIGHT (font);
21173
21174 cmp->font = (void *) font;
21175
21176 pcm = NULL;
21177 if (! font_not_found_p)
21178 {
21179 get_char_face_and_encoding (it->f, c, it->face_id,
21180 &char2b, it->multibyte_p, 0);
21181 pcm = get_per_char_metric (it->f, font, font_info, &char2b,
21182 FONT_TYPE_FOR_MULTIBYTE (font, c));
21183 }
21184
21185 /* Initialize the bounding box. */
21186 if (pcm)
21187 {
21188 width = pcm->width;
21189 ascent = pcm->ascent;
21190 descent = pcm->descent;
21191 lbearing = pcm->lbearing;
21192 rbearing = pcm->rbearing;
21193 }
21194 else
21195 {
21196 width = FONT_WIDTH (font);
21197 ascent = FONT_BASE (font);
21198 descent = FONT_DESCENT (font);
21199 lbearing = 0;
21200 rbearing = width;
21201 }
21202
21203 rightmost = width;
21204 leftmost = 0;
21205 lowest = - descent + boff;
21206 highest = ascent + boff;
21207
21208 if (! font_not_found_p
21209 && font_info->default_ascent
21210 && CHAR_TABLE_P (Vuse_default_ascent)
21211 && !NILP (Faref (Vuse_default_ascent,
21212 make_number (it->char_to_display))))
21213 highest = font_info->default_ascent + boff;
21214
21215 /* Draw the first glyph at the normal position. It may be
21216 shifted to right later if some other glyphs are drawn
21217 at the left. */
21218 cmp->offsets[i * 2] = 0;
21219 cmp->offsets[i * 2 + 1] = boff;
21220 cmp->lbearing = lbearing;
21221 cmp->rbearing = rbearing;
21222
21223 /* Set cmp->offsets for the remaining glyphs. */
21224 for (i++; i < glyph_len; i++)
21225 {
21226 int left, right, btm, top;
21227 int ch = COMPOSITION_GLYPH (cmp, i);
21228 int face_id;
21229 struct face *this_face;
21230 int this_boff;
21231
21232 if (ch == '\t')
21233 ch = ' ';
21234 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
21235 this_face = FACE_FROM_ID (it->f, face_id);
21236 font = this_face->font;
21237
21238 if (font == NULL)
21239 pcm = NULL;
21240 else
21241 {
21242 font_info = FONT_INFO_FROM_FACE (it->f, this_face);
21243 this_boff = font_info->baseline_offset;
21244 if (font_info->vertical_centering)
21245 this_boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
21246 get_char_face_and_encoding (it->f, ch, face_id,
21247 &char2b, it->multibyte_p, 0);
21248 pcm = get_per_char_metric (it->f, font, font_info, &char2b,
21249 FONT_TYPE_FOR_MULTIBYTE (font,
21250 ch));
21251 }
21252 if (! pcm)
21253 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
21254 else
21255 {
21256 width = pcm->width;
21257 ascent = pcm->ascent;
21258 descent = pcm->descent;
21259 lbearing = pcm->lbearing;
21260 rbearing = pcm->rbearing;
21261 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
21262 {
21263 /* Relative composition with or without
21264 alternate chars. */
21265 left = (leftmost + rightmost - width) / 2;
21266 btm = - descent + boff;
21267 if (font_info->relative_compose
21268 && (! CHAR_TABLE_P (Vignore_relative_composition)
21269 || NILP (Faref (Vignore_relative_composition,
21270 make_number (ch)))))
21271 {
21272
21273 if (- descent >= font_info->relative_compose)
21274 /* One extra pixel between two glyphs. */
21275 btm = highest + 1;
21276 else if (ascent <= 0)
21277 /* One extra pixel between two glyphs. */
21278 btm = lowest - 1 - ascent - descent;
21279 }
21280 }
21281 else
21282 {
21283 /* A composition rule is specified by an integer
21284 value that encodes global and new reference
21285 points (GREF and NREF). GREF and NREF are
21286 specified by numbers as below:
21287
21288 0---1---2 -- ascent
21289 | |
21290 | |
21291 | |
21292 9--10--11 -- center
21293 | |
21294 ---3---4---5--- baseline
21295 | |
21296 6---7---8 -- descent
21297 */
21298 int rule = COMPOSITION_RULE (cmp, i);
21299 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
21300
21301 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
21302 grefx = gref % 3, nrefx = nref % 3;
21303 grefy = gref / 3, nrefy = nref / 3;
21304 if (xoff)
21305 xoff = font_height * (xoff - 128) / 256;
21306 if (yoff)
21307 yoff = font_height * (yoff - 128) / 256;
21308
21309 left = (leftmost
21310 + grefx * (rightmost - leftmost) / 2
21311 - nrefx * width / 2
21312 + xoff);
21313
21314 btm = ((grefy == 0 ? highest
21315 : grefy == 1 ? 0
21316 : grefy == 2 ? lowest
21317 : (highest + lowest) / 2)
21318 - (nrefy == 0 ? ascent + descent
21319 : nrefy == 1 ? descent - boff
21320 : nrefy == 2 ? 0
21321 : (ascent + descent) / 2)
21322 + yoff);
21323 }
21324
21325 cmp->offsets[i * 2] = left;
21326 cmp->offsets[i * 2 + 1] = btm + descent;
21327
21328 /* Update the bounding box of the overall glyphs. */
21329 if (width > 0)
21330 {
21331 right = left + width;
21332 if (left < leftmost)
21333 leftmost = left;
21334 if (right > rightmost)
21335 rightmost = right;
21336 }
21337 top = btm + descent + ascent;
21338 if (top > highest)
21339 highest = top;
21340 if (btm < lowest)
21341 lowest = btm;
21342
21343 if (cmp->lbearing > left + lbearing)
21344 cmp->lbearing = left + lbearing;
21345 if (cmp->rbearing < left + rbearing)
21346 cmp->rbearing = left + rbearing;
21347 }
21348 }
21349
21350 /* If there are glyphs whose x-offsets are negative,
21351 shift all glyphs to the right and make all x-offsets
21352 non-negative. */
21353 if (leftmost < 0)
21354 {
21355 for (i = 0; i < cmp->glyph_len; i++)
21356 cmp->offsets[i * 2] -= leftmost;
21357 rightmost -= leftmost;
21358 cmp->lbearing -= leftmost;
21359 cmp->rbearing -= leftmost;
21360 }
21361
21362 if (left_padded && cmp->lbearing < 0)
21363 {
21364 for (i = 0; i < cmp->glyph_len; i++)
21365 cmp->offsets[i * 2] -= cmp->lbearing;
21366 rightmost -= cmp->lbearing;
21367 cmp->rbearing -= cmp->lbearing;
21368 cmp->lbearing = 0;
21369 }
21370 if (right_padded && rightmost < cmp->rbearing)
21371 {
21372 rightmost = cmp->rbearing;
21373 }
21374
21375 cmp->pixel_width = rightmost;
21376 cmp->ascent = highest;
21377 cmp->descent = - lowest;
21378 if (cmp->ascent < font_ascent)
21379 cmp->ascent = font_ascent;
21380 if (cmp->descent < font_descent)
21381 cmp->descent = font_descent;
21382 }
21383
21384 if (it->glyph_row
21385 && (cmp->lbearing < 0
21386 || cmp->rbearing > cmp->pixel_width))
21387 it->glyph_row->contains_overlapping_glyphs_p = 1;
21388
21389 it->pixel_width = cmp->pixel_width;
21390 it->ascent = it->phys_ascent = cmp->ascent;
21391 it->descent = it->phys_descent = cmp->descent;
21392
21393 if (face->box != FACE_NO_BOX)
21394 {
21395 int thick = face->box_line_width;
21396
21397 if (thick > 0)
21398 {
21399 it->ascent += thick;
21400 it->descent += thick;
21401 }
21402 else
21403 thick = - thick;
21404
21405 if (it->start_of_box_run_p)
21406 it->pixel_width += thick;
21407 if (it->end_of_box_run_p)
21408 it->pixel_width += thick;
21409 }
21410
21411 /* If face has an overline, add the height of the overline
21412 (1 pixel) and a 1 pixel margin to the character height. */
21413 if (face->overline_p)
21414 it->ascent += overline_margin;
21415
21416 take_vertical_position_into_account (it);
21417
21418 if (it->glyph_row)
21419 append_composite_glyph (it);
21420 }
21421 else if (it->what == IT_IMAGE)
21422 produce_image_glyph (it);
21423 else if (it->what == IT_STRETCH)
21424 produce_stretch_glyph (it);
21425
21426 /* Accumulate dimensions. Note: can't assume that it->descent > 0
21427 because this isn't true for images with `:ascent 100'. */
21428 xassert (it->ascent >= 0 && it->descent >= 0);
21429 if (it->area == TEXT_AREA)
21430 it->current_x += it->pixel_width;
21431
21432 if (extra_line_spacing > 0)
21433 {
21434 it->descent += extra_line_spacing;
21435 if (extra_line_spacing > it->max_extra_line_spacing)
21436 it->max_extra_line_spacing = extra_line_spacing;
21437 }
21438
21439 it->max_ascent = max (it->max_ascent, it->ascent);
21440 it->max_descent = max (it->max_descent, it->descent);
21441 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
21442 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
21443 }
21444
21445 /* EXPORT for RIF:
21446 Output LEN glyphs starting at START at the nominal cursor position.
21447 Advance the nominal cursor over the text. The global variable
21448 updated_window contains the window being updated, updated_row is
21449 the glyph row being updated, and updated_area is the area of that
21450 row being updated. */
21451
21452 void
21453 x_write_glyphs (start, len)
21454 struct glyph *start;
21455 int len;
21456 {
21457 int x, hpos;
21458
21459 xassert (updated_window && updated_row);
21460 BLOCK_INPUT;
21461
21462 /* Write glyphs. */
21463
21464 hpos = start - updated_row->glyphs[updated_area];
21465 x = draw_glyphs (updated_window, output_cursor.x,
21466 updated_row, updated_area,
21467 hpos, hpos + len,
21468 DRAW_NORMAL_TEXT, 0);
21469
21470 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
21471 if (updated_area == TEXT_AREA
21472 && updated_window->phys_cursor_on_p
21473 && updated_window->phys_cursor.vpos == output_cursor.vpos
21474 && updated_window->phys_cursor.hpos >= hpos
21475 && updated_window->phys_cursor.hpos < hpos + len)
21476 updated_window->phys_cursor_on_p = 0;
21477
21478 UNBLOCK_INPUT;
21479
21480 /* Advance the output cursor. */
21481 output_cursor.hpos += len;
21482 output_cursor.x = x;
21483 }
21484
21485
21486 /* EXPORT for RIF:
21487 Insert LEN glyphs from START at the nominal cursor position. */
21488
21489 void
21490 x_insert_glyphs (start, len)
21491 struct glyph *start;
21492 int len;
21493 {
21494 struct frame *f;
21495 struct window *w;
21496 int line_height, shift_by_width, shifted_region_width;
21497 struct glyph_row *row;
21498 struct glyph *glyph;
21499 int frame_x, frame_y;
21500 EMACS_INT hpos;
21501
21502 xassert (updated_window && updated_row);
21503 BLOCK_INPUT;
21504 w = updated_window;
21505 f = XFRAME (WINDOW_FRAME (w));
21506
21507 /* Get the height of the line we are in. */
21508 row = updated_row;
21509 line_height = row->height;
21510
21511 /* Get the width of the glyphs to insert. */
21512 shift_by_width = 0;
21513 for (glyph = start; glyph < start + len; ++glyph)
21514 shift_by_width += glyph->pixel_width;
21515
21516 /* Get the width of the region to shift right. */
21517 shifted_region_width = (window_box_width (w, updated_area)
21518 - output_cursor.x
21519 - shift_by_width);
21520
21521 /* Shift right. */
21522 frame_x = window_box_left (w, updated_area) + output_cursor.x;
21523 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
21524
21525 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
21526 line_height, shift_by_width);
21527
21528 /* Write the glyphs. */
21529 hpos = start - row->glyphs[updated_area];
21530 draw_glyphs (w, output_cursor.x, row, updated_area,
21531 hpos, hpos + len,
21532 DRAW_NORMAL_TEXT, 0);
21533
21534 /* Advance the output cursor. */
21535 output_cursor.hpos += len;
21536 output_cursor.x += shift_by_width;
21537 UNBLOCK_INPUT;
21538 }
21539
21540
21541 /* EXPORT for RIF:
21542 Erase the current text line from the nominal cursor position
21543 (inclusive) to pixel column TO_X (exclusive). The idea is that
21544 everything from TO_X onward is already erased.
21545
21546 TO_X is a pixel position relative to updated_area of
21547 updated_window. TO_X == -1 means clear to the end of this area. */
21548
21549 void
21550 x_clear_end_of_line (to_x)
21551 int to_x;
21552 {
21553 struct frame *f;
21554 struct window *w = updated_window;
21555 int max_x, min_y, max_y;
21556 int from_x, from_y, to_y;
21557
21558 xassert (updated_window && updated_row);
21559 f = XFRAME (w->frame);
21560
21561 if (updated_row->full_width_p)
21562 max_x = WINDOW_TOTAL_WIDTH (w);
21563 else
21564 max_x = window_box_width (w, updated_area);
21565 max_y = window_text_bottom_y (w);
21566
21567 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
21568 of window. For TO_X > 0, truncate to end of drawing area. */
21569 if (to_x == 0)
21570 return;
21571 else if (to_x < 0)
21572 to_x = max_x;
21573 else
21574 to_x = min (to_x, max_x);
21575
21576 to_y = min (max_y, output_cursor.y + updated_row->height);
21577
21578 /* Notice if the cursor will be cleared by this operation. */
21579 if (!updated_row->full_width_p)
21580 notice_overwritten_cursor (w, updated_area,
21581 output_cursor.x, -1,
21582 updated_row->y,
21583 MATRIX_ROW_BOTTOM_Y (updated_row));
21584
21585 from_x = output_cursor.x;
21586
21587 /* Translate to frame coordinates. */
21588 if (updated_row->full_width_p)
21589 {
21590 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
21591 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
21592 }
21593 else
21594 {
21595 int area_left = window_box_left (w, updated_area);
21596 from_x += area_left;
21597 to_x += area_left;
21598 }
21599
21600 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
21601 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
21602 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
21603
21604 /* Prevent inadvertently clearing to end of the X window. */
21605 if (to_x > from_x && to_y > from_y)
21606 {
21607 BLOCK_INPUT;
21608 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
21609 to_x - from_x, to_y - from_y);
21610 UNBLOCK_INPUT;
21611 }
21612 }
21613
21614 #endif /* HAVE_WINDOW_SYSTEM */
21615
21616
21617 \f
21618 /***********************************************************************
21619 Cursor types
21620 ***********************************************************************/
21621
21622 /* Value is the internal representation of the specified cursor type
21623 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
21624 of the bar cursor. */
21625
21626 static enum text_cursor_kinds
21627 get_specified_cursor_type (arg, width)
21628 Lisp_Object arg;
21629 int *width;
21630 {
21631 enum text_cursor_kinds type;
21632
21633 if (NILP (arg))
21634 return NO_CURSOR;
21635
21636 if (EQ (arg, Qbox))
21637 return FILLED_BOX_CURSOR;
21638
21639 if (EQ (arg, Qhollow))
21640 return HOLLOW_BOX_CURSOR;
21641
21642 if (EQ (arg, Qbar))
21643 {
21644 *width = 2;
21645 return BAR_CURSOR;
21646 }
21647
21648 if (CONSP (arg)
21649 && EQ (XCAR (arg), Qbar)
21650 && INTEGERP (XCDR (arg))
21651 && XINT (XCDR (arg)) >= 0)
21652 {
21653 *width = XINT (XCDR (arg));
21654 return BAR_CURSOR;
21655 }
21656
21657 if (EQ (arg, Qhbar))
21658 {
21659 *width = 2;
21660 return HBAR_CURSOR;
21661 }
21662
21663 if (CONSP (arg)
21664 && EQ (XCAR (arg), Qhbar)
21665 && INTEGERP (XCDR (arg))
21666 && XINT (XCDR (arg)) >= 0)
21667 {
21668 *width = XINT (XCDR (arg));
21669 return HBAR_CURSOR;
21670 }
21671
21672 /* Treat anything unknown as "hollow box cursor".
21673 It was bad to signal an error; people have trouble fixing
21674 .Xdefaults with Emacs, when it has something bad in it. */
21675 type = HOLLOW_BOX_CURSOR;
21676
21677 return type;
21678 }
21679
21680 /* Set the default cursor types for specified frame. */
21681 void
21682 set_frame_cursor_types (f, arg)
21683 struct frame *f;
21684 Lisp_Object arg;
21685 {
21686 int width;
21687 Lisp_Object tem;
21688
21689 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
21690 FRAME_CURSOR_WIDTH (f) = width;
21691
21692 /* By default, set up the blink-off state depending on the on-state. */
21693
21694 tem = Fassoc (arg, Vblink_cursor_alist);
21695 if (!NILP (tem))
21696 {
21697 FRAME_BLINK_OFF_CURSOR (f)
21698 = get_specified_cursor_type (XCDR (tem), &width);
21699 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
21700 }
21701 else
21702 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
21703 }
21704
21705
21706 /* Return the cursor we want to be displayed in window W. Return
21707 width of bar/hbar cursor through WIDTH arg. Return with
21708 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
21709 (i.e. if the `system caret' should track this cursor).
21710
21711 In a mini-buffer window, we want the cursor only to appear if we
21712 are reading input from this window. For the selected window, we
21713 want the cursor type given by the frame parameter or buffer local
21714 setting of cursor-type. If explicitly marked off, draw no cursor.
21715 In all other cases, we want a hollow box cursor. */
21716
21717 static enum text_cursor_kinds
21718 get_window_cursor_type (w, glyph, width, active_cursor)
21719 struct window *w;
21720 struct glyph *glyph;
21721 int *width;
21722 int *active_cursor;
21723 {
21724 struct frame *f = XFRAME (w->frame);
21725 struct buffer *b = XBUFFER (w->buffer);
21726 int cursor_type = DEFAULT_CURSOR;
21727 Lisp_Object alt_cursor;
21728 int non_selected = 0;
21729
21730 *active_cursor = 1;
21731
21732 /* Echo area */
21733 if (cursor_in_echo_area
21734 && FRAME_HAS_MINIBUF_P (f)
21735 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
21736 {
21737 if (w == XWINDOW (echo_area_window))
21738 {
21739 if (EQ (b->cursor_type, Qt) || NILP (b->cursor_type))
21740 {
21741 *width = FRAME_CURSOR_WIDTH (f);
21742 return FRAME_DESIRED_CURSOR (f);
21743 }
21744 else
21745 return get_specified_cursor_type (b->cursor_type, width);
21746 }
21747
21748 *active_cursor = 0;
21749 non_selected = 1;
21750 }
21751
21752 /* Detect a nonselected window or nonselected frame. */
21753 else if (w != XWINDOW (f->selected_window)
21754 #ifdef HAVE_WINDOW_SYSTEM
21755 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame
21756 #endif
21757 )
21758 {
21759 *active_cursor = 0;
21760
21761 if (MINI_WINDOW_P (w) && minibuf_level == 0)
21762 return NO_CURSOR;
21763
21764 non_selected = 1;
21765 }
21766
21767 /* Never display a cursor in a window in which cursor-type is nil. */
21768 if (NILP (b->cursor_type))
21769 return NO_CURSOR;
21770
21771 /* Get the normal cursor type for this window. */
21772 if (EQ (b->cursor_type, Qt))
21773 {
21774 cursor_type = FRAME_DESIRED_CURSOR (f);
21775 *width = FRAME_CURSOR_WIDTH (f);
21776 }
21777 else
21778 cursor_type = get_specified_cursor_type (b->cursor_type, width);
21779
21780 /* Use cursor-in-non-selected-windows instead
21781 for non-selected window or frame. */
21782 if (non_selected)
21783 {
21784 alt_cursor = b->cursor_in_non_selected_windows;
21785 if (!EQ (Qt, alt_cursor))
21786 return get_specified_cursor_type (alt_cursor, width);
21787 /* t means modify the normal cursor type. */
21788 if (cursor_type == FILLED_BOX_CURSOR)
21789 cursor_type = HOLLOW_BOX_CURSOR;
21790 else if (cursor_type == BAR_CURSOR && *width > 1)
21791 --*width;
21792 return cursor_type;
21793 }
21794
21795 /* Use normal cursor if not blinked off. */
21796 if (!w->cursor_off_p)
21797 {
21798 #ifdef HAVE_WINDOW_SYSTEM
21799 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
21800 {
21801 if (cursor_type == FILLED_BOX_CURSOR)
21802 {
21803 /* Using a block cursor on large images can be very annoying.
21804 So use a hollow cursor for "large" images.
21805 If image is not transparent (no mask), also use hollow cursor. */
21806 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
21807 if (img != NULL && IMAGEP (img->spec))
21808 {
21809 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
21810 where N = size of default frame font size.
21811 This should cover most of the "tiny" icons people may use. */
21812 if (!img->mask
21813 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
21814 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
21815 cursor_type = HOLLOW_BOX_CURSOR;
21816 }
21817 }
21818 else if (cursor_type != NO_CURSOR)
21819 {
21820 /* Display current only supports BOX and HOLLOW cursors for images.
21821 So for now, unconditionally use a HOLLOW cursor when cursor is
21822 not a solid box cursor. */
21823 cursor_type = HOLLOW_BOX_CURSOR;
21824 }
21825 }
21826 #endif
21827 return cursor_type;
21828 }
21829
21830 /* Cursor is blinked off, so determine how to "toggle" it. */
21831
21832 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
21833 if ((alt_cursor = Fassoc (b->cursor_type, Vblink_cursor_alist), !NILP (alt_cursor)))
21834 return get_specified_cursor_type (XCDR (alt_cursor), width);
21835
21836 /* Then see if frame has specified a specific blink off cursor type. */
21837 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
21838 {
21839 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
21840 return FRAME_BLINK_OFF_CURSOR (f);
21841 }
21842
21843 #if 0
21844 /* Some people liked having a permanently visible blinking cursor,
21845 while others had very strong opinions against it. So it was
21846 decided to remove it. KFS 2003-09-03 */
21847
21848 /* Finally perform built-in cursor blinking:
21849 filled box <-> hollow box
21850 wide [h]bar <-> narrow [h]bar
21851 narrow [h]bar <-> no cursor
21852 other type <-> no cursor */
21853
21854 if (cursor_type == FILLED_BOX_CURSOR)
21855 return HOLLOW_BOX_CURSOR;
21856
21857 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
21858 {
21859 *width = 1;
21860 return cursor_type;
21861 }
21862 #endif
21863
21864 return NO_CURSOR;
21865 }
21866
21867
21868 #ifdef HAVE_WINDOW_SYSTEM
21869
21870 /* Notice when the text cursor of window W has been completely
21871 overwritten by a drawing operation that outputs glyphs in AREA
21872 starting at X0 and ending at X1 in the line starting at Y0 and
21873 ending at Y1. X coordinates are area-relative. X1 < 0 means all
21874 the rest of the line after X0 has been written. Y coordinates
21875 are window-relative. */
21876
21877 static void
21878 notice_overwritten_cursor (w, area, x0, x1, y0, y1)
21879 struct window *w;
21880 enum glyph_row_area area;
21881 int x0, y0, x1, y1;
21882 {
21883 int cx0, cx1, cy0, cy1;
21884 struct glyph_row *row;
21885
21886 if (!w->phys_cursor_on_p)
21887 return;
21888 if (area != TEXT_AREA)
21889 return;
21890
21891 if (w->phys_cursor.vpos < 0
21892 || w->phys_cursor.vpos >= w->current_matrix->nrows
21893 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
21894 !(row->enabled_p && row->displays_text_p)))
21895 return;
21896
21897 if (row->cursor_in_fringe_p)
21898 {
21899 row->cursor_in_fringe_p = 0;
21900 draw_fringe_bitmap (w, row, 0);
21901 w->phys_cursor_on_p = 0;
21902 return;
21903 }
21904
21905 cx0 = w->phys_cursor.x;
21906 cx1 = cx0 + w->phys_cursor_width;
21907 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
21908 return;
21909
21910 /* The cursor image will be completely removed from the
21911 screen if the output area intersects the cursor area in
21912 y-direction. When we draw in [y0 y1[, and some part of
21913 the cursor is at y < y0, that part must have been drawn
21914 before. When scrolling, the cursor is erased before
21915 actually scrolling, so we don't come here. When not
21916 scrolling, the rows above the old cursor row must have
21917 changed, and in this case these rows must have written
21918 over the cursor image.
21919
21920 Likewise if part of the cursor is below y1, with the
21921 exception of the cursor being in the first blank row at
21922 the buffer and window end because update_text_area
21923 doesn't draw that row. (Except when it does, but
21924 that's handled in update_text_area.) */
21925
21926 cy0 = w->phys_cursor.y;
21927 cy1 = cy0 + w->phys_cursor_height;
21928 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
21929 return;
21930
21931 w->phys_cursor_on_p = 0;
21932 }
21933
21934 #endif /* HAVE_WINDOW_SYSTEM */
21935
21936 \f
21937 /************************************************************************
21938 Mouse Face
21939 ************************************************************************/
21940
21941 #ifdef HAVE_WINDOW_SYSTEM
21942
21943 /* EXPORT for RIF:
21944 Fix the display of area AREA of overlapping row ROW in window W
21945 with respect to the overlapping part OVERLAPS. */
21946
21947 void
21948 x_fix_overlapping_area (w, row, area, overlaps)
21949 struct window *w;
21950 struct glyph_row *row;
21951 enum glyph_row_area area;
21952 int overlaps;
21953 {
21954 int i, x;
21955
21956 BLOCK_INPUT;
21957
21958 x = 0;
21959 for (i = 0; i < row->used[area];)
21960 {
21961 if (row->glyphs[area][i].overlaps_vertically_p)
21962 {
21963 int start = i, start_x = x;
21964
21965 do
21966 {
21967 x += row->glyphs[area][i].pixel_width;
21968 ++i;
21969 }
21970 while (i < row->used[area]
21971 && row->glyphs[area][i].overlaps_vertically_p);
21972
21973 draw_glyphs (w, start_x, row, area,
21974 start, i,
21975 DRAW_NORMAL_TEXT, overlaps);
21976 }
21977 else
21978 {
21979 x += row->glyphs[area][i].pixel_width;
21980 ++i;
21981 }
21982 }
21983
21984 UNBLOCK_INPUT;
21985 }
21986
21987
21988 /* EXPORT:
21989 Draw the cursor glyph of window W in glyph row ROW. See the
21990 comment of draw_glyphs for the meaning of HL. */
21991
21992 void
21993 draw_phys_cursor_glyph (w, row, hl)
21994 struct window *w;
21995 struct glyph_row *row;
21996 enum draw_glyphs_face hl;
21997 {
21998 /* If cursor hpos is out of bounds, don't draw garbage. This can
21999 happen in mini-buffer windows when switching between echo area
22000 glyphs and mini-buffer. */
22001 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
22002 {
22003 int on_p = w->phys_cursor_on_p;
22004 int x1;
22005 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
22006 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
22007 hl, 0);
22008 w->phys_cursor_on_p = on_p;
22009
22010 if (hl == DRAW_CURSOR)
22011 w->phys_cursor_width = x1 - w->phys_cursor.x;
22012 /* When we erase the cursor, and ROW is overlapped by other
22013 rows, make sure that these overlapping parts of other rows
22014 are redrawn. */
22015 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
22016 {
22017 w->phys_cursor_width = x1 - w->phys_cursor.x;
22018
22019 if (row > w->current_matrix->rows
22020 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
22021 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
22022 OVERLAPS_ERASED_CURSOR);
22023
22024 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
22025 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
22026 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
22027 OVERLAPS_ERASED_CURSOR);
22028 }
22029 }
22030 }
22031
22032
22033 /* EXPORT:
22034 Erase the image of a cursor of window W from the screen. */
22035
22036 void
22037 erase_phys_cursor (w)
22038 struct window *w;
22039 {
22040 struct frame *f = XFRAME (w->frame);
22041 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
22042 int hpos = w->phys_cursor.hpos;
22043 int vpos = w->phys_cursor.vpos;
22044 int mouse_face_here_p = 0;
22045 struct glyph_matrix *active_glyphs = w->current_matrix;
22046 struct glyph_row *cursor_row;
22047 struct glyph *cursor_glyph;
22048 enum draw_glyphs_face hl;
22049
22050 /* No cursor displayed or row invalidated => nothing to do on the
22051 screen. */
22052 if (w->phys_cursor_type == NO_CURSOR)
22053 goto mark_cursor_off;
22054
22055 /* VPOS >= active_glyphs->nrows means that window has been resized.
22056 Don't bother to erase the cursor. */
22057 if (vpos >= active_glyphs->nrows)
22058 goto mark_cursor_off;
22059
22060 /* If row containing cursor is marked invalid, there is nothing we
22061 can do. */
22062 cursor_row = MATRIX_ROW (active_glyphs, vpos);
22063 if (!cursor_row->enabled_p)
22064 goto mark_cursor_off;
22065
22066 /* If line spacing is > 0, old cursor may only be partially visible in
22067 window after split-window. So adjust visible height. */
22068 cursor_row->visible_height = min (cursor_row->visible_height,
22069 window_text_bottom_y (w) - cursor_row->y);
22070
22071 /* If row is completely invisible, don't attempt to delete a cursor which
22072 isn't there. This can happen if cursor is at top of a window, and
22073 we switch to a buffer with a header line in that window. */
22074 if (cursor_row->visible_height <= 0)
22075 goto mark_cursor_off;
22076
22077 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
22078 if (cursor_row->cursor_in_fringe_p)
22079 {
22080 cursor_row->cursor_in_fringe_p = 0;
22081 draw_fringe_bitmap (w, cursor_row, 0);
22082 goto mark_cursor_off;
22083 }
22084
22085 /* This can happen when the new row is shorter than the old one.
22086 In this case, either draw_glyphs or clear_end_of_line
22087 should have cleared the cursor. Note that we wouldn't be
22088 able to erase the cursor in this case because we don't have a
22089 cursor glyph at hand. */
22090 if (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])
22091 goto mark_cursor_off;
22092
22093 /* If the cursor is in the mouse face area, redisplay that when
22094 we clear the cursor. */
22095 if (! NILP (dpyinfo->mouse_face_window)
22096 && w == XWINDOW (dpyinfo->mouse_face_window)
22097 && (vpos > dpyinfo->mouse_face_beg_row
22098 || (vpos == dpyinfo->mouse_face_beg_row
22099 && hpos >= dpyinfo->mouse_face_beg_col))
22100 && (vpos < dpyinfo->mouse_face_end_row
22101 || (vpos == dpyinfo->mouse_face_end_row
22102 && hpos < dpyinfo->mouse_face_end_col))
22103 /* Don't redraw the cursor's spot in mouse face if it is at the
22104 end of a line (on a newline). The cursor appears there, but
22105 mouse highlighting does not. */
22106 && cursor_row->used[TEXT_AREA] > hpos)
22107 mouse_face_here_p = 1;
22108
22109 /* Maybe clear the display under the cursor. */
22110 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
22111 {
22112 int x, y, left_x;
22113 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
22114 int width;
22115
22116 cursor_glyph = get_phys_cursor_glyph (w);
22117 if (cursor_glyph == NULL)
22118 goto mark_cursor_off;
22119
22120 width = cursor_glyph->pixel_width;
22121 left_x = window_box_left_offset (w, TEXT_AREA);
22122 x = w->phys_cursor.x;
22123 if (x < left_x)
22124 width -= left_x - x;
22125 width = min (width, window_box_width (w, TEXT_AREA) - x);
22126 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
22127 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, max (x, left_x));
22128
22129 if (width > 0)
22130 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
22131 }
22132
22133 /* Erase the cursor by redrawing the character underneath it. */
22134 if (mouse_face_here_p)
22135 hl = DRAW_MOUSE_FACE;
22136 else
22137 hl = DRAW_NORMAL_TEXT;
22138 draw_phys_cursor_glyph (w, cursor_row, hl);
22139
22140 mark_cursor_off:
22141 w->phys_cursor_on_p = 0;
22142 w->phys_cursor_type = NO_CURSOR;
22143 }
22144
22145
22146 /* EXPORT:
22147 Display or clear cursor of window W. If ON is zero, clear the
22148 cursor. If it is non-zero, display the cursor. If ON is nonzero,
22149 where to put the cursor is specified by HPOS, VPOS, X and Y. */
22150
22151 void
22152 display_and_set_cursor (w, on, hpos, vpos, x, y)
22153 struct window *w;
22154 int on, hpos, vpos, x, y;
22155 {
22156 struct frame *f = XFRAME (w->frame);
22157 int new_cursor_type;
22158 int new_cursor_width;
22159 int active_cursor;
22160 struct glyph_row *glyph_row;
22161 struct glyph *glyph;
22162
22163 /* This is pointless on invisible frames, and dangerous on garbaged
22164 windows and frames; in the latter case, the frame or window may
22165 be in the midst of changing its size, and x and y may be off the
22166 window. */
22167 if (! FRAME_VISIBLE_P (f)
22168 || FRAME_GARBAGED_P (f)
22169 || vpos >= w->current_matrix->nrows
22170 || hpos >= w->current_matrix->matrix_w)
22171 return;
22172
22173 /* If cursor is off and we want it off, return quickly. */
22174 if (!on && !w->phys_cursor_on_p)
22175 return;
22176
22177 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
22178 /* If cursor row is not enabled, we don't really know where to
22179 display the cursor. */
22180 if (!glyph_row->enabled_p)
22181 {
22182 w->phys_cursor_on_p = 0;
22183 return;
22184 }
22185
22186 glyph = NULL;
22187 if (!glyph_row->exact_window_width_line_p
22188 || hpos < glyph_row->used[TEXT_AREA])
22189 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
22190
22191 xassert (interrupt_input_blocked);
22192
22193 /* Set new_cursor_type to the cursor we want to be displayed. */
22194 new_cursor_type = get_window_cursor_type (w, glyph,
22195 &new_cursor_width, &active_cursor);
22196
22197 /* If cursor is currently being shown and we don't want it to be or
22198 it is in the wrong place, or the cursor type is not what we want,
22199 erase it. */
22200 if (w->phys_cursor_on_p
22201 && (!on
22202 || w->phys_cursor.x != x
22203 || w->phys_cursor.y != y
22204 || new_cursor_type != w->phys_cursor_type
22205 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
22206 && new_cursor_width != w->phys_cursor_width)))
22207 erase_phys_cursor (w);
22208
22209 /* Don't check phys_cursor_on_p here because that flag is only set
22210 to zero in some cases where we know that the cursor has been
22211 completely erased, to avoid the extra work of erasing the cursor
22212 twice. In other words, phys_cursor_on_p can be 1 and the cursor
22213 still not be visible, or it has only been partly erased. */
22214 if (on)
22215 {
22216 w->phys_cursor_ascent = glyph_row->ascent;
22217 w->phys_cursor_height = glyph_row->height;
22218
22219 /* Set phys_cursor_.* before x_draw_.* is called because some
22220 of them may need the information. */
22221 w->phys_cursor.x = x;
22222 w->phys_cursor.y = glyph_row->y;
22223 w->phys_cursor.hpos = hpos;
22224 w->phys_cursor.vpos = vpos;
22225 }
22226
22227 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
22228 new_cursor_type, new_cursor_width,
22229 on, active_cursor);
22230 }
22231
22232
22233 /* Switch the display of W's cursor on or off, according to the value
22234 of ON. */
22235
22236 static void
22237 update_window_cursor (w, on)
22238 struct window *w;
22239 int on;
22240 {
22241 /* Don't update cursor in windows whose frame is in the process
22242 of being deleted. */
22243 if (w->current_matrix)
22244 {
22245 BLOCK_INPUT;
22246 display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
22247 w->phys_cursor.x, w->phys_cursor.y);
22248 UNBLOCK_INPUT;
22249 }
22250 }
22251
22252
22253 /* Call update_window_cursor with parameter ON_P on all leaf windows
22254 in the window tree rooted at W. */
22255
22256 static void
22257 update_cursor_in_window_tree (w, on_p)
22258 struct window *w;
22259 int on_p;
22260 {
22261 while (w)
22262 {
22263 if (!NILP (w->hchild))
22264 update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
22265 else if (!NILP (w->vchild))
22266 update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
22267 else
22268 update_window_cursor (w, on_p);
22269
22270 w = NILP (w->next) ? 0 : XWINDOW (w->next);
22271 }
22272 }
22273
22274
22275 /* EXPORT:
22276 Display the cursor on window W, or clear it, according to ON_P.
22277 Don't change the cursor's position. */
22278
22279 void
22280 x_update_cursor (f, on_p)
22281 struct frame *f;
22282 int on_p;
22283 {
22284 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
22285 }
22286
22287
22288 /* EXPORT:
22289 Clear the cursor of window W to background color, and mark the
22290 cursor as not shown. This is used when the text where the cursor
22291 is is about to be rewritten. */
22292
22293 void
22294 x_clear_cursor (w)
22295 struct window *w;
22296 {
22297 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
22298 update_window_cursor (w, 0);
22299 }
22300
22301
22302 /* EXPORT:
22303 Display the active region described by mouse_face_* according to DRAW. */
22304
22305 void
22306 show_mouse_face (dpyinfo, draw)
22307 Display_Info *dpyinfo;
22308 enum draw_glyphs_face draw;
22309 {
22310 struct window *w = XWINDOW (dpyinfo->mouse_face_window);
22311 struct frame *f = XFRAME (WINDOW_FRAME (w));
22312
22313 if (/* If window is in the process of being destroyed, don't bother
22314 to do anything. */
22315 w->current_matrix != NULL
22316 /* Don't update mouse highlight if hidden */
22317 && (draw != DRAW_MOUSE_FACE || !dpyinfo->mouse_face_hidden)
22318 /* Recognize when we are called to operate on rows that don't exist
22319 anymore. This can happen when a window is split. */
22320 && dpyinfo->mouse_face_end_row < w->current_matrix->nrows)
22321 {
22322 int phys_cursor_on_p = w->phys_cursor_on_p;
22323 struct glyph_row *row, *first, *last;
22324
22325 first = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_beg_row);
22326 last = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_end_row);
22327
22328 for (row = first; row <= last && row->enabled_p; ++row)
22329 {
22330 int start_hpos, end_hpos, start_x;
22331
22332 /* For all but the first row, the highlight starts at column 0. */
22333 if (row == first)
22334 {
22335 start_hpos = dpyinfo->mouse_face_beg_col;
22336 start_x = dpyinfo->mouse_face_beg_x;
22337 }
22338 else
22339 {
22340 start_hpos = 0;
22341 start_x = 0;
22342 }
22343
22344 if (row == last)
22345 end_hpos = dpyinfo->mouse_face_end_col;
22346 else
22347 {
22348 end_hpos = row->used[TEXT_AREA];
22349 if (draw == DRAW_NORMAL_TEXT)
22350 row->fill_line_p = 1; /* Clear to end of line */
22351 }
22352
22353 if (end_hpos > start_hpos)
22354 {
22355 draw_glyphs (w, start_x, row, TEXT_AREA,
22356 start_hpos, end_hpos,
22357 draw, 0);
22358
22359 row->mouse_face_p
22360 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
22361 }
22362 }
22363
22364 /* When we've written over the cursor, arrange for it to
22365 be displayed again. */
22366 if (phys_cursor_on_p && !w->phys_cursor_on_p)
22367 {
22368 BLOCK_INPUT;
22369 display_and_set_cursor (w, 1,
22370 w->phys_cursor.hpos, w->phys_cursor.vpos,
22371 w->phys_cursor.x, w->phys_cursor.y);
22372 UNBLOCK_INPUT;
22373 }
22374 }
22375
22376 /* Change the mouse cursor. */
22377 if (draw == DRAW_NORMAL_TEXT && !EQ (dpyinfo->mouse_face_window, f->tool_bar_window))
22378 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
22379 else if (draw == DRAW_MOUSE_FACE)
22380 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
22381 else
22382 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
22383 }
22384
22385 /* EXPORT:
22386 Clear out the mouse-highlighted active region.
22387 Redraw it un-highlighted first. Value is non-zero if mouse
22388 face was actually drawn unhighlighted. */
22389
22390 int
22391 clear_mouse_face (dpyinfo)
22392 Display_Info *dpyinfo;
22393 {
22394 int cleared = 0;
22395
22396 if (!dpyinfo->mouse_face_hidden && !NILP (dpyinfo->mouse_face_window))
22397 {
22398 show_mouse_face (dpyinfo, DRAW_NORMAL_TEXT);
22399 cleared = 1;
22400 }
22401
22402 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
22403 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
22404 dpyinfo->mouse_face_window = Qnil;
22405 dpyinfo->mouse_face_overlay = Qnil;
22406 return cleared;
22407 }
22408
22409
22410 /* EXPORT:
22411 Non-zero if physical cursor of window W is within mouse face. */
22412
22413 int
22414 cursor_in_mouse_face_p (w)
22415 struct window *w;
22416 {
22417 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
22418 int in_mouse_face = 0;
22419
22420 if (WINDOWP (dpyinfo->mouse_face_window)
22421 && XWINDOW (dpyinfo->mouse_face_window) == w)
22422 {
22423 int hpos = w->phys_cursor.hpos;
22424 int vpos = w->phys_cursor.vpos;
22425
22426 if (vpos >= dpyinfo->mouse_face_beg_row
22427 && vpos <= dpyinfo->mouse_face_end_row
22428 && (vpos > dpyinfo->mouse_face_beg_row
22429 || hpos >= dpyinfo->mouse_face_beg_col)
22430 && (vpos < dpyinfo->mouse_face_end_row
22431 || hpos < dpyinfo->mouse_face_end_col
22432 || dpyinfo->mouse_face_past_end))
22433 in_mouse_face = 1;
22434 }
22435
22436 return in_mouse_face;
22437 }
22438
22439
22440
22441 \f
22442 /* Find the glyph matrix position of buffer position CHARPOS in window
22443 *W. HPOS, *VPOS, *X, and *Y are set to the positions found. W's
22444 current glyphs must be up to date. If CHARPOS is above window
22445 start return (0, 0, 0, 0). If CHARPOS is after end of W, return end
22446 of last line in W. In the row containing CHARPOS, stop before glyphs
22447 having STOP as object. */
22448
22449 #if 1 /* This is a version of fast_find_position that's more correct
22450 in the presence of hscrolling, for example. I didn't install
22451 it right away because the problem fixed is minor, it failed
22452 in 20.x as well, and I think it's too risky to install
22453 so near the release of 21.1. 2001-09-25 gerd. */
22454
22455 static int
22456 fast_find_position (w, charpos, hpos, vpos, x, y, stop)
22457 struct window *w;
22458 EMACS_INT charpos;
22459 int *hpos, *vpos, *x, *y;
22460 Lisp_Object stop;
22461 {
22462 struct glyph_row *row, *first;
22463 struct glyph *glyph, *end;
22464 int past_end = 0;
22465
22466 first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
22467 if (charpos < MATRIX_ROW_START_CHARPOS (first))
22468 {
22469 *x = first->x;
22470 *y = first->y;
22471 *hpos = 0;
22472 *vpos = MATRIX_ROW_VPOS (first, w->current_matrix);
22473 return 1;
22474 }
22475
22476 row = row_containing_pos (w, charpos, first, NULL, 0);
22477 if (row == NULL)
22478 {
22479 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
22480 past_end = 1;
22481 }
22482
22483 /* If whole rows or last part of a row came from a display overlay,
22484 row_containing_pos will skip over such rows because their end pos
22485 equals the start pos of the overlay or interval.
22486
22487 Move back if we have a STOP object and previous row's
22488 end glyph came from STOP. */
22489 if (!NILP (stop))
22490 {
22491 struct glyph_row *prev;
22492 while ((prev = row - 1, prev >= first)
22493 && MATRIX_ROW_END_CHARPOS (prev) == charpos
22494 && prev->used[TEXT_AREA] > 0)
22495 {
22496 struct glyph *beg = prev->glyphs[TEXT_AREA];
22497 glyph = beg + prev->used[TEXT_AREA];
22498 while (--glyph >= beg
22499 && INTEGERP (glyph->object));
22500 if (glyph < beg
22501 || !EQ (stop, glyph->object))
22502 break;
22503 row = prev;
22504 }
22505 }
22506
22507 *x = row->x;
22508 *y = row->y;
22509 *vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
22510
22511 glyph = row->glyphs[TEXT_AREA];
22512 end = glyph + row->used[TEXT_AREA];
22513
22514 /* Skip over glyphs not having an object at the start of the row.
22515 These are special glyphs like truncation marks on terminal
22516 frames. */
22517 if (row->displays_text_p)
22518 while (glyph < end
22519 && INTEGERP (glyph->object)
22520 && !EQ (stop, glyph->object)
22521 && glyph->charpos < 0)
22522 {
22523 *x += glyph->pixel_width;
22524 ++glyph;
22525 }
22526
22527 while (glyph < end
22528 && !INTEGERP (glyph->object)
22529 && !EQ (stop, glyph->object)
22530 && (!BUFFERP (glyph->object)
22531 || glyph->charpos < charpos))
22532 {
22533 *x += glyph->pixel_width;
22534 ++glyph;
22535 }
22536
22537 *hpos = glyph - row->glyphs[TEXT_AREA];
22538 return !past_end;
22539 }
22540
22541 #else /* not 1 */
22542
22543 static int
22544 fast_find_position (w, pos, hpos, vpos, x, y, stop)
22545 struct window *w;
22546 EMACS_INT pos;
22547 int *hpos, *vpos, *x, *y;
22548 Lisp_Object stop;
22549 {
22550 int i;
22551 int lastcol;
22552 int maybe_next_line_p = 0;
22553 int line_start_position;
22554 int yb = window_text_bottom_y (w);
22555 struct glyph_row *row, *best_row;
22556 int row_vpos, best_row_vpos;
22557 int current_x;
22558
22559 row = best_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
22560 row_vpos = best_row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
22561
22562 while (row->y < yb)
22563 {
22564 if (row->used[TEXT_AREA])
22565 line_start_position = row->glyphs[TEXT_AREA]->charpos;
22566 else
22567 line_start_position = 0;
22568
22569 if (line_start_position > pos)
22570 break;
22571 /* If the position sought is the end of the buffer,
22572 don't include the blank lines at the bottom of the window. */
22573 else if (line_start_position == pos
22574 && pos == BUF_ZV (XBUFFER (w->buffer)))
22575 {
22576 maybe_next_line_p = 1;
22577 break;
22578 }
22579 else if (line_start_position > 0)
22580 {
22581 best_row = row;
22582 best_row_vpos = row_vpos;
22583 }
22584
22585 if (row->y + row->height >= yb)
22586 break;
22587
22588 ++row;
22589 ++row_vpos;
22590 }
22591
22592 /* Find the right column within BEST_ROW. */
22593 lastcol = 0;
22594 current_x = best_row->x;
22595 for (i = 0; i < best_row->used[TEXT_AREA]; i++)
22596 {
22597 struct glyph *glyph = best_row->glyphs[TEXT_AREA] + i;
22598 int charpos = glyph->charpos;
22599
22600 if (BUFFERP (glyph->object))
22601 {
22602 if (charpos == pos)
22603 {
22604 *hpos = i;
22605 *vpos = best_row_vpos;
22606 *x = current_x;
22607 *y = best_row->y;
22608 return 1;
22609 }
22610 else if (charpos > pos)
22611 break;
22612 }
22613 else if (EQ (glyph->object, stop))
22614 break;
22615
22616 if (charpos > 0)
22617 lastcol = i;
22618 current_x += glyph->pixel_width;
22619 }
22620
22621 /* If we're looking for the end of the buffer,
22622 and we didn't find it in the line we scanned,
22623 use the start of the following line. */
22624 if (maybe_next_line_p)
22625 {
22626 ++best_row;
22627 ++best_row_vpos;
22628 lastcol = 0;
22629 current_x = best_row->x;
22630 }
22631
22632 *vpos = best_row_vpos;
22633 *hpos = lastcol + 1;
22634 *x = current_x;
22635 *y = best_row->y;
22636 return 0;
22637 }
22638
22639 #endif /* not 1 */
22640
22641
22642 /* Find the position of the glyph for position POS in OBJECT in
22643 window W's current matrix, and return in *X, *Y the pixel
22644 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
22645
22646 RIGHT_P non-zero means return the position of the right edge of the
22647 glyph, RIGHT_P zero means return the left edge position.
22648
22649 If no glyph for POS exists in the matrix, return the position of
22650 the glyph with the next smaller position that is in the matrix, if
22651 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
22652 exists in the matrix, return the position of the glyph with the
22653 next larger position in OBJECT.
22654
22655 Value is non-zero if a glyph was found. */
22656
22657 static int
22658 fast_find_string_pos (w, pos, object, hpos, vpos, x, y, right_p)
22659 struct window *w;
22660 EMACS_INT pos;
22661 Lisp_Object object;
22662 int *hpos, *vpos, *x, *y;
22663 int right_p;
22664 {
22665 int yb = window_text_bottom_y (w);
22666 struct glyph_row *r;
22667 struct glyph *best_glyph = NULL;
22668 struct glyph_row *best_row = NULL;
22669 int best_x = 0;
22670
22671 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
22672 r->enabled_p && r->y < yb;
22673 ++r)
22674 {
22675 struct glyph *g = r->glyphs[TEXT_AREA];
22676 struct glyph *e = g + r->used[TEXT_AREA];
22677 int gx;
22678
22679 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
22680 if (EQ (g->object, object))
22681 {
22682 if (g->charpos == pos)
22683 {
22684 best_glyph = g;
22685 best_x = gx;
22686 best_row = r;
22687 goto found;
22688 }
22689 else if (best_glyph == NULL
22690 || ((eabs (g->charpos - pos)
22691 < eabs (best_glyph->charpos - pos))
22692 && (right_p
22693 ? g->charpos < pos
22694 : g->charpos > pos)))
22695 {
22696 best_glyph = g;
22697 best_x = gx;
22698 best_row = r;
22699 }
22700 }
22701 }
22702
22703 found:
22704
22705 if (best_glyph)
22706 {
22707 *x = best_x;
22708 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
22709
22710 if (right_p)
22711 {
22712 *x += best_glyph->pixel_width;
22713 ++*hpos;
22714 }
22715
22716 *y = best_row->y;
22717 *vpos = best_row - w->current_matrix->rows;
22718 }
22719
22720 return best_glyph != NULL;
22721 }
22722
22723
22724 /* See if position X, Y is within a hot-spot of an image. */
22725
22726 static int
22727 on_hot_spot_p (hot_spot, x, y)
22728 Lisp_Object hot_spot;
22729 int x, y;
22730 {
22731 if (!CONSP (hot_spot))
22732 return 0;
22733
22734 if (EQ (XCAR (hot_spot), Qrect))
22735 {
22736 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
22737 Lisp_Object rect = XCDR (hot_spot);
22738 Lisp_Object tem;
22739 if (!CONSP (rect))
22740 return 0;
22741 if (!CONSP (XCAR (rect)))
22742 return 0;
22743 if (!CONSP (XCDR (rect)))
22744 return 0;
22745 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
22746 return 0;
22747 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
22748 return 0;
22749 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
22750 return 0;
22751 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
22752 return 0;
22753 return 1;
22754 }
22755 else if (EQ (XCAR (hot_spot), Qcircle))
22756 {
22757 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
22758 Lisp_Object circ = XCDR (hot_spot);
22759 Lisp_Object lr, lx0, ly0;
22760 if (CONSP (circ)
22761 && CONSP (XCAR (circ))
22762 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
22763 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
22764 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
22765 {
22766 double r = XFLOATINT (lr);
22767 double dx = XINT (lx0) - x;
22768 double dy = XINT (ly0) - y;
22769 return (dx * dx + dy * dy <= r * r);
22770 }
22771 }
22772 else if (EQ (XCAR (hot_spot), Qpoly))
22773 {
22774 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
22775 if (VECTORP (XCDR (hot_spot)))
22776 {
22777 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
22778 Lisp_Object *poly = v->contents;
22779 int n = v->size;
22780 int i;
22781 int inside = 0;
22782 Lisp_Object lx, ly;
22783 int x0, y0;
22784
22785 /* Need an even number of coordinates, and at least 3 edges. */
22786 if (n < 6 || n & 1)
22787 return 0;
22788
22789 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
22790 If count is odd, we are inside polygon. Pixels on edges
22791 may or may not be included depending on actual geometry of the
22792 polygon. */
22793 if ((lx = poly[n-2], !INTEGERP (lx))
22794 || (ly = poly[n-1], !INTEGERP (lx)))
22795 return 0;
22796 x0 = XINT (lx), y0 = XINT (ly);
22797 for (i = 0; i < n; i += 2)
22798 {
22799 int x1 = x0, y1 = y0;
22800 if ((lx = poly[i], !INTEGERP (lx))
22801 || (ly = poly[i+1], !INTEGERP (ly)))
22802 return 0;
22803 x0 = XINT (lx), y0 = XINT (ly);
22804
22805 /* Does this segment cross the X line? */
22806 if (x0 >= x)
22807 {
22808 if (x1 >= x)
22809 continue;
22810 }
22811 else if (x1 < x)
22812 continue;
22813 if (y > y0 && y > y1)
22814 continue;
22815 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
22816 inside = !inside;
22817 }
22818 return inside;
22819 }
22820 }
22821 return 0;
22822 }
22823
22824 Lisp_Object
22825 find_hot_spot (map, x, y)
22826 Lisp_Object map;
22827 int x, y;
22828 {
22829 while (CONSP (map))
22830 {
22831 if (CONSP (XCAR (map))
22832 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
22833 return XCAR (map);
22834 map = XCDR (map);
22835 }
22836
22837 return Qnil;
22838 }
22839
22840 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
22841 3, 3, 0,
22842 doc: /* Lookup in image map MAP coordinates X and Y.
22843 An image map is an alist where each element has the format (AREA ID PLIST).
22844 An AREA is specified as either a rectangle, a circle, or a polygon:
22845 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
22846 pixel coordinates of the upper left and bottom right corners.
22847 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
22848 and the radius of the circle; r may be a float or integer.
22849 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
22850 vector describes one corner in the polygon.
22851 Returns the alist element for the first matching AREA in MAP. */)
22852 (map, x, y)
22853 Lisp_Object map;
22854 Lisp_Object x, y;
22855 {
22856 if (NILP (map))
22857 return Qnil;
22858
22859 CHECK_NUMBER (x);
22860 CHECK_NUMBER (y);
22861
22862 return find_hot_spot (map, XINT (x), XINT (y));
22863 }
22864
22865
22866 /* Display frame CURSOR, optionally using shape defined by POINTER. */
22867 static void
22868 define_frame_cursor1 (f, cursor, pointer)
22869 struct frame *f;
22870 Cursor cursor;
22871 Lisp_Object pointer;
22872 {
22873 /* Do not change cursor shape while dragging mouse. */
22874 if (!NILP (do_mouse_tracking))
22875 return;
22876
22877 if (!NILP (pointer))
22878 {
22879 if (EQ (pointer, Qarrow))
22880 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
22881 else if (EQ (pointer, Qhand))
22882 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
22883 else if (EQ (pointer, Qtext))
22884 cursor = FRAME_X_OUTPUT (f)->text_cursor;
22885 else if (EQ (pointer, intern ("hdrag")))
22886 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
22887 #ifdef HAVE_X_WINDOWS
22888 else if (EQ (pointer, intern ("vdrag")))
22889 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
22890 #endif
22891 else if (EQ (pointer, intern ("hourglass")))
22892 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
22893 else if (EQ (pointer, Qmodeline))
22894 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
22895 else
22896 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
22897 }
22898
22899 if (cursor != No_Cursor)
22900 FRAME_RIF (f)->define_frame_cursor (f, cursor);
22901 }
22902
22903 /* Take proper action when mouse has moved to the mode or header line
22904 or marginal area AREA of window W, x-position X and y-position Y.
22905 X is relative to the start of the text display area of W, so the
22906 width of bitmap areas and scroll bars must be subtracted to get a
22907 position relative to the start of the mode line. */
22908
22909 static void
22910 note_mode_line_or_margin_highlight (window, x, y, area)
22911 Lisp_Object window;
22912 int x, y;
22913 enum window_part area;
22914 {
22915 struct window *w = XWINDOW (window);
22916 struct frame *f = XFRAME (w->frame);
22917 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
22918 Cursor cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
22919 Lisp_Object pointer = Qnil;
22920 int charpos, dx, dy, width, height;
22921 Lisp_Object string, object = Qnil;
22922 Lisp_Object pos, help;
22923
22924 Lisp_Object mouse_face;
22925 int original_x_pixel = x;
22926 struct glyph * glyph = NULL, * row_start_glyph = NULL;
22927 struct glyph_row *row;
22928
22929 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
22930 {
22931 int x0;
22932 struct glyph *end;
22933
22934 string = mode_line_string (w, area, &x, &y, &charpos,
22935 &object, &dx, &dy, &width, &height);
22936
22937 row = (area == ON_MODE_LINE
22938 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
22939 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
22940
22941 /* Find glyph */
22942 if (row->mode_line_p && row->enabled_p)
22943 {
22944 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
22945 end = glyph + row->used[TEXT_AREA];
22946
22947 for (x0 = original_x_pixel;
22948 glyph < end && x0 >= glyph->pixel_width;
22949 ++glyph)
22950 x0 -= glyph->pixel_width;
22951
22952 if (glyph >= end)
22953 glyph = NULL;
22954 }
22955 }
22956 else
22957 {
22958 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
22959 string = marginal_area_string (w, area, &x, &y, &charpos,
22960 &object, &dx, &dy, &width, &height);
22961 }
22962
22963 help = Qnil;
22964
22965 if (IMAGEP (object))
22966 {
22967 Lisp_Object image_map, hotspot;
22968 if ((image_map = Fplist_get (XCDR (object), QCmap),
22969 !NILP (image_map))
22970 && (hotspot = find_hot_spot (image_map, dx, dy),
22971 CONSP (hotspot))
22972 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
22973 {
22974 Lisp_Object area_id, plist;
22975
22976 area_id = XCAR (hotspot);
22977 /* Could check AREA_ID to see if we enter/leave this hot-spot.
22978 If so, we could look for mouse-enter, mouse-leave
22979 properties in PLIST (and do something...). */
22980 hotspot = XCDR (hotspot);
22981 if (CONSP (hotspot)
22982 && (plist = XCAR (hotspot), CONSP (plist)))
22983 {
22984 pointer = Fplist_get (plist, Qpointer);
22985 if (NILP (pointer))
22986 pointer = Qhand;
22987 help = Fplist_get (plist, Qhelp_echo);
22988 if (!NILP (help))
22989 {
22990 help_echo_string = help;
22991 /* Is this correct? ++kfs */
22992 XSETWINDOW (help_echo_window, w);
22993 help_echo_object = w->buffer;
22994 help_echo_pos = charpos;
22995 }
22996 }
22997 }
22998 if (NILP (pointer))
22999 pointer = Fplist_get (XCDR (object), QCpointer);
23000 }
23001
23002 if (STRINGP (string))
23003 {
23004 pos = make_number (charpos);
23005 /* If we're on a string with `help-echo' text property, arrange
23006 for the help to be displayed. This is done by setting the
23007 global variable help_echo_string to the help string. */
23008 if (NILP (help))
23009 {
23010 help = Fget_text_property (pos, Qhelp_echo, string);
23011 if (!NILP (help))
23012 {
23013 help_echo_string = help;
23014 XSETWINDOW (help_echo_window, w);
23015 help_echo_object = string;
23016 help_echo_pos = charpos;
23017 }
23018 }
23019
23020 if (NILP (pointer))
23021 pointer = Fget_text_property (pos, Qpointer, string);
23022
23023 /* Change the mouse pointer according to what is under X/Y. */
23024 if (NILP (pointer) && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
23025 {
23026 Lisp_Object map;
23027 map = Fget_text_property (pos, Qlocal_map, string);
23028 if (!KEYMAPP (map))
23029 map = Fget_text_property (pos, Qkeymap, string);
23030 if (!KEYMAPP (map))
23031 cursor = dpyinfo->vertical_scroll_bar_cursor;
23032 }
23033
23034 /* Change the mouse face according to what is under X/Y. */
23035 mouse_face = Fget_text_property (pos, Qmouse_face, string);
23036 if (!NILP (mouse_face)
23037 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
23038 && glyph)
23039 {
23040 Lisp_Object b, e;
23041
23042 struct glyph * tmp_glyph;
23043
23044 int gpos;
23045 int gseq_length;
23046 int total_pixel_width;
23047 int ignore;
23048
23049 int vpos, hpos;
23050
23051 b = Fprevious_single_property_change (make_number (charpos + 1),
23052 Qmouse_face, string, Qnil);
23053 if (NILP (b))
23054 b = make_number (0);
23055
23056 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
23057 if (NILP (e))
23058 e = make_number (SCHARS (string));
23059
23060 /* Calculate the position(glyph position: GPOS) of GLYPH in
23061 displayed string. GPOS is different from CHARPOS.
23062
23063 CHARPOS is the position of glyph in internal string
23064 object. A mode line string format has structures which
23065 is converted to a flatten by emacs lisp interpreter.
23066 The internal string is an element of the structures.
23067 The displayed string is the flatten string. */
23068 gpos = 0;
23069 if (glyph > row_start_glyph)
23070 {
23071 tmp_glyph = glyph - 1;
23072 while (tmp_glyph >= row_start_glyph
23073 && tmp_glyph->charpos >= XINT (b)
23074 && EQ (tmp_glyph->object, glyph->object))
23075 {
23076 tmp_glyph--;
23077 gpos++;
23078 }
23079 }
23080
23081 /* Calculate the lenght(glyph sequence length: GSEQ_LENGTH) of
23082 displayed string holding GLYPH.
23083
23084 GSEQ_LENGTH is different from SCHARS (STRING).
23085 SCHARS (STRING) returns the length of the internal string. */
23086 for (tmp_glyph = glyph, gseq_length = gpos;
23087 tmp_glyph->charpos < XINT (e);
23088 tmp_glyph++, gseq_length++)
23089 {
23090 if (!EQ (tmp_glyph->object, glyph->object))
23091 break;
23092 }
23093
23094 total_pixel_width = 0;
23095 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
23096 total_pixel_width += tmp_glyph->pixel_width;
23097
23098 /* Pre calculation of re-rendering position */
23099 vpos = (x - gpos);
23100 hpos = (area == ON_MODE_LINE
23101 ? (w->current_matrix)->nrows - 1
23102 : 0);
23103
23104 /* If the re-rendering position is included in the last
23105 re-rendering area, we should do nothing. */
23106 if ( EQ (window, dpyinfo->mouse_face_window)
23107 && dpyinfo->mouse_face_beg_col <= vpos
23108 && vpos < dpyinfo->mouse_face_end_col
23109 && dpyinfo->mouse_face_beg_row == hpos )
23110 return;
23111
23112 if (clear_mouse_face (dpyinfo))
23113 cursor = No_Cursor;
23114
23115 dpyinfo->mouse_face_beg_col = vpos;
23116 dpyinfo->mouse_face_beg_row = hpos;
23117
23118 dpyinfo->mouse_face_beg_x = original_x_pixel - (total_pixel_width + dx);
23119 dpyinfo->mouse_face_beg_y = 0;
23120
23121 dpyinfo->mouse_face_end_col = vpos + gseq_length;
23122 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_beg_row;
23123
23124 dpyinfo->mouse_face_end_x = 0;
23125 dpyinfo->mouse_face_end_y = 0;
23126
23127 dpyinfo->mouse_face_past_end = 0;
23128 dpyinfo->mouse_face_window = window;
23129
23130 dpyinfo->mouse_face_face_id = face_at_string_position (w, string,
23131 charpos,
23132 0, 0, 0, &ignore,
23133 glyph->face_id, 1);
23134 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
23135
23136 if (NILP (pointer))
23137 pointer = Qhand;
23138 }
23139 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
23140 clear_mouse_face (dpyinfo);
23141 }
23142 define_frame_cursor1 (f, cursor, pointer);
23143 }
23144
23145
23146 /* EXPORT:
23147 Take proper action when the mouse has moved to position X, Y on
23148 frame F as regards highlighting characters that have mouse-face
23149 properties. Also de-highlighting chars where the mouse was before.
23150 X and Y can be negative or out of range. */
23151
23152 void
23153 note_mouse_highlight (f, x, y)
23154 struct frame *f;
23155 int x, y;
23156 {
23157 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
23158 enum window_part part;
23159 Lisp_Object window;
23160 struct window *w;
23161 Cursor cursor = No_Cursor;
23162 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
23163 struct buffer *b;
23164
23165 /* When a menu is active, don't highlight because this looks odd. */
23166 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (MAC_OS)
23167 if (popup_activated ())
23168 return;
23169 #endif
23170
23171 if (NILP (Vmouse_highlight)
23172 || !f->glyphs_initialized_p)
23173 return;
23174
23175 dpyinfo->mouse_face_mouse_x = x;
23176 dpyinfo->mouse_face_mouse_y = y;
23177 dpyinfo->mouse_face_mouse_frame = f;
23178
23179 if (dpyinfo->mouse_face_defer)
23180 return;
23181
23182 if (gc_in_progress)
23183 {
23184 dpyinfo->mouse_face_deferred_gc = 1;
23185 return;
23186 }
23187
23188 /* Which window is that in? */
23189 window = window_from_coordinates (f, x, y, &part, 0, 0, 1);
23190
23191 /* If we were displaying active text in another window, clear that.
23192 Also clear if we move out of text area in same window. */
23193 if (! EQ (window, dpyinfo->mouse_face_window)
23194 || (part != ON_TEXT && part != ON_MODE_LINE && part != ON_HEADER_LINE
23195 && !NILP (dpyinfo->mouse_face_window)))
23196 clear_mouse_face (dpyinfo);
23197
23198 /* Not on a window -> return. */
23199 if (!WINDOWP (window))
23200 return;
23201
23202 /* Reset help_echo_string. It will get recomputed below. */
23203 help_echo_string = Qnil;
23204
23205 /* Convert to window-relative pixel coordinates. */
23206 w = XWINDOW (window);
23207 frame_to_window_pixel_xy (w, &x, &y);
23208
23209 /* Handle tool-bar window differently since it doesn't display a
23210 buffer. */
23211 if (EQ (window, f->tool_bar_window))
23212 {
23213 note_tool_bar_highlight (f, x, y);
23214 return;
23215 }
23216
23217 /* Mouse is on the mode, header line or margin? */
23218 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
23219 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
23220 {
23221 note_mode_line_or_margin_highlight (window, x, y, part);
23222 return;
23223 }
23224
23225 if (part == ON_VERTICAL_BORDER)
23226 {
23227 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
23228 help_echo_string = build_string ("drag-mouse-1: resize");
23229 }
23230 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
23231 || part == ON_SCROLL_BAR)
23232 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
23233 else
23234 cursor = FRAME_X_OUTPUT (f)->text_cursor;
23235
23236 /* Are we in a window whose display is up to date?
23237 And verify the buffer's text has not changed. */
23238 b = XBUFFER (w->buffer);
23239 if (part == ON_TEXT
23240 && EQ (w->window_end_valid, w->buffer)
23241 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
23242 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
23243 {
23244 int hpos, vpos, pos, i, dx, dy, area;
23245 struct glyph *glyph;
23246 Lisp_Object object;
23247 Lisp_Object mouse_face = Qnil, overlay = Qnil, position;
23248 Lisp_Object *overlay_vec = NULL;
23249 int noverlays;
23250 struct buffer *obuf;
23251 int obegv, ozv, same_region;
23252
23253 /* Find the glyph under X/Y. */
23254 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
23255
23256 /* Look for :pointer property on image. */
23257 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
23258 {
23259 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
23260 if (img != NULL && IMAGEP (img->spec))
23261 {
23262 Lisp_Object image_map, hotspot;
23263 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
23264 !NILP (image_map))
23265 && (hotspot = find_hot_spot (image_map,
23266 glyph->slice.x + dx,
23267 glyph->slice.y + dy),
23268 CONSP (hotspot))
23269 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
23270 {
23271 Lisp_Object area_id, plist;
23272
23273 area_id = XCAR (hotspot);
23274 /* Could check AREA_ID to see if we enter/leave this hot-spot.
23275 If so, we could look for mouse-enter, mouse-leave
23276 properties in PLIST (and do something...). */
23277 hotspot = XCDR (hotspot);
23278 if (CONSP (hotspot)
23279 && (plist = XCAR (hotspot), CONSP (plist)))
23280 {
23281 pointer = Fplist_get (plist, Qpointer);
23282 if (NILP (pointer))
23283 pointer = Qhand;
23284 help_echo_string = Fplist_get (plist, Qhelp_echo);
23285 if (!NILP (help_echo_string))
23286 {
23287 help_echo_window = window;
23288 help_echo_object = glyph->object;
23289 help_echo_pos = glyph->charpos;
23290 }
23291 }
23292 }
23293 if (NILP (pointer))
23294 pointer = Fplist_get (XCDR (img->spec), QCpointer);
23295 }
23296 }
23297
23298 /* Clear mouse face if X/Y not over text. */
23299 if (glyph == NULL
23300 || area != TEXT_AREA
23301 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p)
23302 {
23303 if (clear_mouse_face (dpyinfo))
23304 cursor = No_Cursor;
23305 if (NILP (pointer))
23306 {
23307 if (area != TEXT_AREA)
23308 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
23309 else
23310 pointer = Vvoid_text_area_pointer;
23311 }
23312 goto set_cursor;
23313 }
23314
23315 pos = glyph->charpos;
23316 object = glyph->object;
23317 if (!STRINGP (object) && !BUFFERP (object))
23318 goto set_cursor;
23319
23320 /* If we get an out-of-range value, return now; avoid an error. */
23321 if (BUFFERP (object) && pos > BUF_Z (b))
23322 goto set_cursor;
23323
23324 /* Make the window's buffer temporarily current for
23325 overlays_at and compute_char_face. */
23326 obuf = current_buffer;
23327 current_buffer = b;
23328 obegv = BEGV;
23329 ozv = ZV;
23330 BEGV = BEG;
23331 ZV = Z;
23332
23333 /* Is this char mouse-active or does it have help-echo? */
23334 position = make_number (pos);
23335
23336 if (BUFFERP (object))
23337 {
23338 /* Put all the overlays we want in a vector in overlay_vec. */
23339 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
23340 /* Sort overlays into increasing priority order. */
23341 noverlays = sort_overlays (overlay_vec, noverlays, w);
23342 }
23343 else
23344 noverlays = 0;
23345
23346 same_region = (EQ (window, dpyinfo->mouse_face_window)
23347 && vpos >= dpyinfo->mouse_face_beg_row
23348 && vpos <= dpyinfo->mouse_face_end_row
23349 && (vpos > dpyinfo->mouse_face_beg_row
23350 || hpos >= dpyinfo->mouse_face_beg_col)
23351 && (vpos < dpyinfo->mouse_face_end_row
23352 || hpos < dpyinfo->mouse_face_end_col
23353 || dpyinfo->mouse_face_past_end));
23354
23355 if (same_region)
23356 cursor = No_Cursor;
23357
23358 /* Check mouse-face highlighting. */
23359 if (! same_region
23360 /* If there exists an overlay with mouse-face overlapping
23361 the one we are currently highlighting, we have to
23362 check if we enter the overlapping overlay, and then
23363 highlight only that. */
23364 || (OVERLAYP (dpyinfo->mouse_face_overlay)
23365 && mouse_face_overlay_overlaps (dpyinfo->mouse_face_overlay)))
23366 {
23367 /* Find the highest priority overlay that has a mouse-face
23368 property. */
23369 overlay = Qnil;
23370 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
23371 {
23372 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
23373 if (!NILP (mouse_face))
23374 overlay = overlay_vec[i];
23375 }
23376
23377 /* If we're actually highlighting the same overlay as
23378 before, there's no need to do that again. */
23379 if (!NILP (overlay)
23380 && EQ (overlay, dpyinfo->mouse_face_overlay))
23381 goto check_help_echo;
23382
23383 dpyinfo->mouse_face_overlay = overlay;
23384
23385 /* Clear the display of the old active region, if any. */
23386 if (clear_mouse_face (dpyinfo))
23387 cursor = No_Cursor;
23388
23389 /* If no overlay applies, get a text property. */
23390 if (NILP (overlay))
23391 mouse_face = Fget_text_property (position, Qmouse_face, object);
23392
23393 /* Handle the overlay case. */
23394 if (!NILP (overlay))
23395 {
23396 /* Find the range of text around this char that
23397 should be active. */
23398 Lisp_Object before, after;
23399 int ignore;
23400
23401 before = Foverlay_start (overlay);
23402 after = Foverlay_end (overlay);
23403 /* Record this as the current active region. */
23404 fast_find_position (w, XFASTINT (before),
23405 &dpyinfo->mouse_face_beg_col,
23406 &dpyinfo->mouse_face_beg_row,
23407 &dpyinfo->mouse_face_beg_x,
23408 &dpyinfo->mouse_face_beg_y, Qnil);
23409
23410 dpyinfo->mouse_face_past_end
23411 = !fast_find_position (w, XFASTINT (after),
23412 &dpyinfo->mouse_face_end_col,
23413 &dpyinfo->mouse_face_end_row,
23414 &dpyinfo->mouse_face_end_x,
23415 &dpyinfo->mouse_face_end_y, Qnil);
23416 dpyinfo->mouse_face_window = window;
23417
23418 dpyinfo->mouse_face_face_id
23419 = face_at_buffer_position (w, pos, 0, 0,
23420 &ignore, pos + 1,
23421 !dpyinfo->mouse_face_hidden);
23422
23423 /* Display it as active. */
23424 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
23425 cursor = No_Cursor;
23426 }
23427 /* Handle the text property case. */
23428 else if (!NILP (mouse_face) && BUFFERP (object))
23429 {
23430 /* Find the range of text around this char that
23431 should be active. */
23432 Lisp_Object before, after, beginning, end;
23433 int ignore;
23434
23435 beginning = Fmarker_position (w->start);
23436 end = make_number (BUF_Z (XBUFFER (object))
23437 - XFASTINT (w->window_end_pos));
23438 before
23439 = Fprevious_single_property_change (make_number (pos + 1),
23440 Qmouse_face,
23441 object, beginning);
23442 after
23443 = Fnext_single_property_change (position, Qmouse_face,
23444 object, end);
23445
23446 /* Record this as the current active region. */
23447 fast_find_position (w, XFASTINT (before),
23448 &dpyinfo->mouse_face_beg_col,
23449 &dpyinfo->mouse_face_beg_row,
23450 &dpyinfo->mouse_face_beg_x,
23451 &dpyinfo->mouse_face_beg_y, Qnil);
23452 dpyinfo->mouse_face_past_end
23453 = !fast_find_position (w, XFASTINT (after),
23454 &dpyinfo->mouse_face_end_col,
23455 &dpyinfo->mouse_face_end_row,
23456 &dpyinfo->mouse_face_end_x,
23457 &dpyinfo->mouse_face_end_y, Qnil);
23458 dpyinfo->mouse_face_window = window;
23459
23460 if (BUFFERP (object))
23461 dpyinfo->mouse_face_face_id
23462 = face_at_buffer_position (w, pos, 0, 0,
23463 &ignore, pos + 1,
23464 !dpyinfo->mouse_face_hidden);
23465
23466 /* Display it as active. */
23467 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
23468 cursor = No_Cursor;
23469 }
23470 else if (!NILP (mouse_face) && STRINGP (object))
23471 {
23472 Lisp_Object b, e;
23473 int ignore;
23474
23475 b = Fprevious_single_property_change (make_number (pos + 1),
23476 Qmouse_face,
23477 object, Qnil);
23478 e = Fnext_single_property_change (position, Qmouse_face,
23479 object, Qnil);
23480 if (NILP (b))
23481 b = make_number (0);
23482 if (NILP (e))
23483 e = make_number (SCHARS (object) - 1);
23484
23485 fast_find_string_pos (w, XINT (b), object,
23486 &dpyinfo->mouse_face_beg_col,
23487 &dpyinfo->mouse_face_beg_row,
23488 &dpyinfo->mouse_face_beg_x,
23489 &dpyinfo->mouse_face_beg_y, 0);
23490 fast_find_string_pos (w, XINT (e), object,
23491 &dpyinfo->mouse_face_end_col,
23492 &dpyinfo->mouse_face_end_row,
23493 &dpyinfo->mouse_face_end_x,
23494 &dpyinfo->mouse_face_end_y, 1);
23495 dpyinfo->mouse_face_past_end = 0;
23496 dpyinfo->mouse_face_window = window;
23497 dpyinfo->mouse_face_face_id
23498 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
23499 glyph->face_id, 1);
23500 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
23501 cursor = No_Cursor;
23502 }
23503 else if (STRINGP (object) && NILP (mouse_face))
23504 {
23505 /* A string which doesn't have mouse-face, but
23506 the text ``under'' it might have. */
23507 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
23508 int start = MATRIX_ROW_START_CHARPOS (r);
23509
23510 pos = string_buffer_position (w, object, start);
23511 if (pos > 0)
23512 mouse_face = get_char_property_and_overlay (make_number (pos),
23513 Qmouse_face,
23514 w->buffer,
23515 &overlay);
23516 if (!NILP (mouse_face) && !NILP (overlay))
23517 {
23518 Lisp_Object before = Foverlay_start (overlay);
23519 Lisp_Object after = Foverlay_end (overlay);
23520 int ignore;
23521
23522 /* Note that we might not be able to find position
23523 BEFORE in the glyph matrix if the overlay is
23524 entirely covered by a `display' property. In
23525 this case, we overshoot. So let's stop in
23526 the glyph matrix before glyphs for OBJECT. */
23527 fast_find_position (w, XFASTINT (before),
23528 &dpyinfo->mouse_face_beg_col,
23529 &dpyinfo->mouse_face_beg_row,
23530 &dpyinfo->mouse_face_beg_x,
23531 &dpyinfo->mouse_face_beg_y,
23532 object);
23533
23534 dpyinfo->mouse_face_past_end
23535 = !fast_find_position (w, XFASTINT (after),
23536 &dpyinfo->mouse_face_end_col,
23537 &dpyinfo->mouse_face_end_row,
23538 &dpyinfo->mouse_face_end_x,
23539 &dpyinfo->mouse_face_end_y,
23540 Qnil);
23541 dpyinfo->mouse_face_window = window;
23542 dpyinfo->mouse_face_face_id
23543 = face_at_buffer_position (w, pos, 0, 0,
23544 &ignore, pos + 1,
23545 !dpyinfo->mouse_face_hidden);
23546
23547 /* Display it as active. */
23548 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
23549 cursor = No_Cursor;
23550 }
23551 }
23552 }
23553
23554 check_help_echo:
23555
23556 /* Look for a `help-echo' property. */
23557 if (NILP (help_echo_string)) {
23558 Lisp_Object help, overlay;
23559
23560 /* Check overlays first. */
23561 help = overlay = Qnil;
23562 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
23563 {
23564 overlay = overlay_vec[i];
23565 help = Foverlay_get (overlay, Qhelp_echo);
23566 }
23567
23568 if (!NILP (help))
23569 {
23570 help_echo_string = help;
23571 help_echo_window = window;
23572 help_echo_object = overlay;
23573 help_echo_pos = pos;
23574 }
23575 else
23576 {
23577 Lisp_Object object = glyph->object;
23578 int charpos = glyph->charpos;
23579
23580 /* Try text properties. */
23581 if (STRINGP (object)
23582 && charpos >= 0
23583 && charpos < SCHARS (object))
23584 {
23585 help = Fget_text_property (make_number (charpos),
23586 Qhelp_echo, object);
23587 if (NILP (help))
23588 {
23589 /* If the string itself doesn't specify a help-echo,
23590 see if the buffer text ``under'' it does. */
23591 struct glyph_row *r
23592 = MATRIX_ROW (w->current_matrix, vpos);
23593 int start = MATRIX_ROW_START_CHARPOS (r);
23594 int pos = string_buffer_position (w, object, start);
23595 if (pos > 0)
23596 {
23597 help = Fget_char_property (make_number (pos),
23598 Qhelp_echo, w->buffer);
23599 if (!NILP (help))
23600 {
23601 charpos = pos;
23602 object = w->buffer;
23603 }
23604 }
23605 }
23606 }
23607 else if (BUFFERP (object)
23608 && charpos >= BEGV
23609 && charpos < ZV)
23610 help = Fget_text_property (make_number (charpos), Qhelp_echo,
23611 object);
23612
23613 if (!NILP (help))
23614 {
23615 help_echo_string = help;
23616 help_echo_window = window;
23617 help_echo_object = object;
23618 help_echo_pos = charpos;
23619 }
23620 }
23621 }
23622
23623 /* Look for a `pointer' property. */
23624 if (NILP (pointer))
23625 {
23626 /* Check overlays first. */
23627 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
23628 pointer = Foverlay_get (overlay_vec[i], Qpointer);
23629
23630 if (NILP (pointer))
23631 {
23632 Lisp_Object object = glyph->object;
23633 int charpos = glyph->charpos;
23634
23635 /* Try text properties. */
23636 if (STRINGP (object)
23637 && charpos >= 0
23638 && charpos < SCHARS (object))
23639 {
23640 pointer = Fget_text_property (make_number (charpos),
23641 Qpointer, object);
23642 if (NILP (pointer))
23643 {
23644 /* If the string itself doesn't specify a pointer,
23645 see if the buffer text ``under'' it does. */
23646 struct glyph_row *r
23647 = MATRIX_ROW (w->current_matrix, vpos);
23648 int start = MATRIX_ROW_START_CHARPOS (r);
23649 int pos = string_buffer_position (w, object, start);
23650 if (pos > 0)
23651 pointer = Fget_char_property (make_number (pos),
23652 Qpointer, w->buffer);
23653 }
23654 }
23655 else if (BUFFERP (object)
23656 && charpos >= BEGV
23657 && charpos < ZV)
23658 pointer = Fget_text_property (make_number (charpos),
23659 Qpointer, object);
23660 }
23661 }
23662
23663 BEGV = obegv;
23664 ZV = ozv;
23665 current_buffer = obuf;
23666 }
23667
23668 set_cursor:
23669
23670 define_frame_cursor1 (f, cursor, pointer);
23671 }
23672
23673
23674 /* EXPORT for RIF:
23675 Clear any mouse-face on window W. This function is part of the
23676 redisplay interface, and is called from try_window_id and similar
23677 functions to ensure the mouse-highlight is off. */
23678
23679 void
23680 x_clear_window_mouse_face (w)
23681 struct window *w;
23682 {
23683 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
23684 Lisp_Object window;
23685
23686 BLOCK_INPUT;
23687 XSETWINDOW (window, w);
23688 if (EQ (window, dpyinfo->mouse_face_window))
23689 clear_mouse_face (dpyinfo);
23690 UNBLOCK_INPUT;
23691 }
23692
23693
23694 /* EXPORT:
23695 Just discard the mouse face information for frame F, if any.
23696 This is used when the size of F is changed. */
23697
23698 void
23699 cancel_mouse_face (f)
23700 struct frame *f;
23701 {
23702 Lisp_Object window;
23703 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
23704
23705 window = dpyinfo->mouse_face_window;
23706 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
23707 {
23708 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
23709 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
23710 dpyinfo->mouse_face_window = Qnil;
23711 }
23712 }
23713
23714
23715 #endif /* HAVE_WINDOW_SYSTEM */
23716
23717 \f
23718 /***********************************************************************
23719 Exposure Events
23720 ***********************************************************************/
23721
23722 #ifdef HAVE_WINDOW_SYSTEM
23723
23724 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
23725 which intersects rectangle R. R is in window-relative coordinates. */
23726
23727 static void
23728 expose_area (w, row, r, area)
23729 struct window *w;
23730 struct glyph_row *row;
23731 XRectangle *r;
23732 enum glyph_row_area area;
23733 {
23734 struct glyph *first = row->glyphs[area];
23735 struct glyph *end = row->glyphs[area] + row->used[area];
23736 struct glyph *last;
23737 int first_x, start_x, x;
23738
23739 if (area == TEXT_AREA && row->fill_line_p)
23740 /* If row extends face to end of line write the whole line. */
23741 draw_glyphs (w, 0, row, area,
23742 0, row->used[area],
23743 DRAW_NORMAL_TEXT, 0);
23744 else
23745 {
23746 /* Set START_X to the window-relative start position for drawing glyphs of
23747 AREA. The first glyph of the text area can be partially visible.
23748 The first glyphs of other areas cannot. */
23749 start_x = window_box_left_offset (w, area);
23750 x = start_x;
23751 if (area == TEXT_AREA)
23752 x += row->x;
23753
23754 /* Find the first glyph that must be redrawn. */
23755 while (first < end
23756 && x + first->pixel_width < r->x)
23757 {
23758 x += first->pixel_width;
23759 ++first;
23760 }
23761
23762 /* Find the last one. */
23763 last = first;
23764 first_x = x;
23765 while (last < end
23766 && x < r->x + r->width)
23767 {
23768 x += last->pixel_width;
23769 ++last;
23770 }
23771
23772 /* Repaint. */
23773 if (last > first)
23774 draw_glyphs (w, first_x - start_x, row, area,
23775 first - row->glyphs[area], last - row->glyphs[area],
23776 DRAW_NORMAL_TEXT, 0);
23777 }
23778 }
23779
23780
23781 /* Redraw the parts of the glyph row ROW on window W intersecting
23782 rectangle R. R is in window-relative coordinates. Value is
23783 non-zero if mouse-face was overwritten. */
23784
23785 static int
23786 expose_line (w, row, r)
23787 struct window *w;
23788 struct glyph_row *row;
23789 XRectangle *r;
23790 {
23791 xassert (row->enabled_p);
23792
23793 if (row->mode_line_p || w->pseudo_window_p)
23794 draw_glyphs (w, 0, row, TEXT_AREA,
23795 0, row->used[TEXT_AREA],
23796 DRAW_NORMAL_TEXT, 0);
23797 else
23798 {
23799 if (row->used[LEFT_MARGIN_AREA])
23800 expose_area (w, row, r, LEFT_MARGIN_AREA);
23801 if (row->used[TEXT_AREA])
23802 expose_area (w, row, r, TEXT_AREA);
23803 if (row->used[RIGHT_MARGIN_AREA])
23804 expose_area (w, row, r, RIGHT_MARGIN_AREA);
23805 draw_row_fringe_bitmaps (w, row);
23806 }
23807
23808 return row->mouse_face_p;
23809 }
23810
23811
23812 /* Redraw those parts of glyphs rows during expose event handling that
23813 overlap other rows. Redrawing of an exposed line writes over parts
23814 of lines overlapping that exposed line; this function fixes that.
23815
23816 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
23817 row in W's current matrix that is exposed and overlaps other rows.
23818 LAST_OVERLAPPING_ROW is the last such row. */
23819
23820 static void
23821 expose_overlaps (w, first_overlapping_row, last_overlapping_row, r)
23822 struct window *w;
23823 struct glyph_row *first_overlapping_row;
23824 struct glyph_row *last_overlapping_row;
23825 XRectangle *r;
23826 {
23827 struct glyph_row *row;
23828
23829 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
23830 if (row->overlapping_p)
23831 {
23832 xassert (row->enabled_p && !row->mode_line_p);
23833
23834 row->clip = r;
23835 if (row->used[LEFT_MARGIN_AREA])
23836 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
23837
23838 if (row->used[TEXT_AREA])
23839 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
23840
23841 if (row->used[RIGHT_MARGIN_AREA])
23842 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
23843 row->clip = NULL;
23844 }
23845 }
23846
23847
23848 /* Return non-zero if W's cursor intersects rectangle R. */
23849
23850 static int
23851 phys_cursor_in_rect_p (w, r)
23852 struct window *w;
23853 XRectangle *r;
23854 {
23855 XRectangle cr, result;
23856 struct glyph *cursor_glyph;
23857
23858 cursor_glyph = get_phys_cursor_glyph (w);
23859 if (cursor_glyph)
23860 {
23861 /* r is relative to W's box, but w->phys_cursor.x is relative
23862 to left edge of W's TEXT area. Adjust it. */
23863 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
23864 cr.y = w->phys_cursor.y;
23865 cr.width = cursor_glyph->pixel_width;
23866 cr.height = w->phys_cursor_height;
23867 /* ++KFS: W32 version used W32-specific IntersectRect here, but
23868 I assume the effect is the same -- and this is portable. */
23869 return x_intersect_rectangles (&cr, r, &result);
23870 }
23871 /* If we don't understand the format, pretend we're not in the hot-spot. */
23872 return 0;
23873 }
23874
23875
23876 /* EXPORT:
23877 Draw a vertical window border to the right of window W if W doesn't
23878 have vertical scroll bars. */
23879
23880 void
23881 x_draw_vertical_border (w)
23882 struct window *w;
23883 {
23884 struct frame *f = XFRAME (WINDOW_FRAME (w));
23885
23886 /* We could do better, if we knew what type of scroll-bar the adjacent
23887 windows (on either side) have... But we don't :-(
23888 However, I think this works ok. ++KFS 2003-04-25 */
23889
23890 /* Redraw borders between horizontally adjacent windows. Don't
23891 do it for frames with vertical scroll bars because either the
23892 right scroll bar of a window, or the left scroll bar of its
23893 neighbor will suffice as a border. */
23894 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
23895 return;
23896
23897 if (!WINDOW_RIGHTMOST_P (w)
23898 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
23899 {
23900 int x0, x1, y0, y1;
23901
23902 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
23903 y1 -= 1;
23904
23905 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
23906 x1 -= 1;
23907
23908 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
23909 }
23910 else if (!WINDOW_LEFTMOST_P (w)
23911 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
23912 {
23913 int x0, x1, y0, y1;
23914
23915 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
23916 y1 -= 1;
23917
23918 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
23919 x0 -= 1;
23920
23921 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
23922 }
23923 }
23924
23925
23926 /* Redraw the part of window W intersection rectangle FR. Pixel
23927 coordinates in FR are frame-relative. Call this function with
23928 input blocked. Value is non-zero if the exposure overwrites
23929 mouse-face. */
23930
23931 static int
23932 expose_window (w, fr)
23933 struct window *w;
23934 XRectangle *fr;
23935 {
23936 struct frame *f = XFRAME (w->frame);
23937 XRectangle wr, r;
23938 int mouse_face_overwritten_p = 0;
23939
23940 /* If window is not yet fully initialized, do nothing. This can
23941 happen when toolkit scroll bars are used and a window is split.
23942 Reconfiguring the scroll bar will generate an expose for a newly
23943 created window. */
23944 if (w->current_matrix == NULL)
23945 return 0;
23946
23947 /* When we're currently updating the window, display and current
23948 matrix usually don't agree. Arrange for a thorough display
23949 later. */
23950 if (w == updated_window)
23951 {
23952 SET_FRAME_GARBAGED (f);
23953 return 0;
23954 }
23955
23956 /* Frame-relative pixel rectangle of W. */
23957 wr.x = WINDOW_LEFT_EDGE_X (w);
23958 wr.y = WINDOW_TOP_EDGE_Y (w);
23959 wr.width = WINDOW_TOTAL_WIDTH (w);
23960 wr.height = WINDOW_TOTAL_HEIGHT (w);
23961
23962 if (x_intersect_rectangles (fr, &wr, &r))
23963 {
23964 int yb = window_text_bottom_y (w);
23965 struct glyph_row *row;
23966 int cursor_cleared_p;
23967 struct glyph_row *first_overlapping_row, *last_overlapping_row;
23968
23969 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
23970 r.x, r.y, r.width, r.height));
23971
23972 /* Convert to window coordinates. */
23973 r.x -= WINDOW_LEFT_EDGE_X (w);
23974 r.y -= WINDOW_TOP_EDGE_Y (w);
23975
23976 /* Turn off the cursor. */
23977 if (!w->pseudo_window_p
23978 && phys_cursor_in_rect_p (w, &r))
23979 {
23980 x_clear_cursor (w);
23981 cursor_cleared_p = 1;
23982 }
23983 else
23984 cursor_cleared_p = 0;
23985
23986 /* Update lines intersecting rectangle R. */
23987 first_overlapping_row = last_overlapping_row = NULL;
23988 for (row = w->current_matrix->rows;
23989 row->enabled_p;
23990 ++row)
23991 {
23992 int y0 = row->y;
23993 int y1 = MATRIX_ROW_BOTTOM_Y (row);
23994
23995 if ((y0 >= r.y && y0 < r.y + r.height)
23996 || (y1 > r.y && y1 < r.y + r.height)
23997 || (r.y >= y0 && r.y < y1)
23998 || (r.y + r.height > y0 && r.y + r.height < y1))
23999 {
24000 /* A header line may be overlapping, but there is no need
24001 to fix overlapping areas for them. KFS 2005-02-12 */
24002 if (row->overlapping_p && !row->mode_line_p)
24003 {
24004 if (first_overlapping_row == NULL)
24005 first_overlapping_row = row;
24006 last_overlapping_row = row;
24007 }
24008
24009 row->clip = fr;
24010 if (expose_line (w, row, &r))
24011 mouse_face_overwritten_p = 1;
24012 row->clip = NULL;
24013 }
24014 else if (row->overlapping_p)
24015 {
24016 /* We must redraw a row overlapping the exposed area. */
24017 if (y0 < r.y
24018 ? y0 + row->phys_height > r.y
24019 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
24020 {
24021 if (first_overlapping_row == NULL)
24022 first_overlapping_row = row;
24023 last_overlapping_row = row;
24024 }
24025 }
24026
24027 if (y1 >= yb)
24028 break;
24029 }
24030
24031 /* Display the mode line if there is one. */
24032 if (WINDOW_WANTS_MODELINE_P (w)
24033 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
24034 row->enabled_p)
24035 && row->y < r.y + r.height)
24036 {
24037 if (expose_line (w, row, &r))
24038 mouse_face_overwritten_p = 1;
24039 }
24040
24041 if (!w->pseudo_window_p)
24042 {
24043 /* Fix the display of overlapping rows. */
24044 if (first_overlapping_row)
24045 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
24046 fr);
24047
24048 /* Draw border between windows. */
24049 x_draw_vertical_border (w);
24050
24051 /* Turn the cursor on again. */
24052 if (cursor_cleared_p)
24053 update_window_cursor (w, 1);
24054 }
24055 }
24056
24057 return mouse_face_overwritten_p;
24058 }
24059
24060
24061
24062 /* Redraw (parts) of all windows in the window tree rooted at W that
24063 intersect R. R contains frame pixel coordinates. Value is
24064 non-zero if the exposure overwrites mouse-face. */
24065
24066 static int
24067 expose_window_tree (w, r)
24068 struct window *w;
24069 XRectangle *r;
24070 {
24071 struct frame *f = XFRAME (w->frame);
24072 int mouse_face_overwritten_p = 0;
24073
24074 while (w && !FRAME_GARBAGED_P (f))
24075 {
24076 if (!NILP (w->hchild))
24077 mouse_face_overwritten_p
24078 |= expose_window_tree (XWINDOW (w->hchild), r);
24079 else if (!NILP (w->vchild))
24080 mouse_face_overwritten_p
24081 |= expose_window_tree (XWINDOW (w->vchild), r);
24082 else
24083 mouse_face_overwritten_p |= expose_window (w, r);
24084
24085 w = NILP (w->next) ? NULL : XWINDOW (w->next);
24086 }
24087
24088 return mouse_face_overwritten_p;
24089 }
24090
24091
24092 /* EXPORT:
24093 Redisplay an exposed area of frame F. X and Y are the upper-left
24094 corner of the exposed rectangle. W and H are width and height of
24095 the exposed area. All are pixel values. W or H zero means redraw
24096 the entire frame. */
24097
24098 void
24099 expose_frame (f, x, y, w, h)
24100 struct frame *f;
24101 int x, y, w, h;
24102 {
24103 XRectangle r;
24104 int mouse_face_overwritten_p = 0;
24105
24106 TRACE ((stderr, "expose_frame "));
24107
24108 /* No need to redraw if frame will be redrawn soon. */
24109 if (FRAME_GARBAGED_P (f))
24110 {
24111 TRACE ((stderr, " garbaged\n"));
24112 return;
24113 }
24114
24115 /* If basic faces haven't been realized yet, there is no point in
24116 trying to redraw anything. This can happen when we get an expose
24117 event while Emacs is starting, e.g. by moving another window. */
24118 if (FRAME_FACE_CACHE (f) == NULL
24119 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
24120 {
24121 TRACE ((stderr, " no faces\n"));
24122 return;
24123 }
24124
24125 if (w == 0 || h == 0)
24126 {
24127 r.x = r.y = 0;
24128 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
24129 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
24130 }
24131 else
24132 {
24133 r.x = x;
24134 r.y = y;
24135 r.width = w;
24136 r.height = h;
24137 }
24138
24139 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
24140 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
24141
24142 if (WINDOWP (f->tool_bar_window))
24143 mouse_face_overwritten_p
24144 |= expose_window (XWINDOW (f->tool_bar_window), &r);
24145
24146 #ifdef HAVE_X_WINDOWS
24147 #ifndef MSDOS
24148 #ifndef USE_X_TOOLKIT
24149 if (WINDOWP (f->menu_bar_window))
24150 mouse_face_overwritten_p
24151 |= expose_window (XWINDOW (f->menu_bar_window), &r);
24152 #endif /* not USE_X_TOOLKIT */
24153 #endif
24154 #endif
24155
24156 /* Some window managers support a focus-follows-mouse style with
24157 delayed raising of frames. Imagine a partially obscured frame,
24158 and moving the mouse into partially obscured mouse-face on that
24159 frame. The visible part of the mouse-face will be highlighted,
24160 then the WM raises the obscured frame. With at least one WM, KDE
24161 2.1, Emacs is not getting any event for the raising of the frame
24162 (even tried with SubstructureRedirectMask), only Expose events.
24163 These expose events will draw text normally, i.e. not
24164 highlighted. Which means we must redo the highlight here.
24165 Subsume it under ``we love X''. --gerd 2001-08-15 */
24166 /* Included in Windows version because Windows most likely does not
24167 do the right thing if any third party tool offers
24168 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
24169 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
24170 {
24171 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
24172 if (f == dpyinfo->mouse_face_mouse_frame)
24173 {
24174 int x = dpyinfo->mouse_face_mouse_x;
24175 int y = dpyinfo->mouse_face_mouse_y;
24176 clear_mouse_face (dpyinfo);
24177 note_mouse_highlight (f, x, y);
24178 }
24179 }
24180 }
24181
24182
24183 /* EXPORT:
24184 Determine the intersection of two rectangles R1 and R2. Return
24185 the intersection in *RESULT. Value is non-zero if RESULT is not
24186 empty. */
24187
24188 int
24189 x_intersect_rectangles (r1, r2, result)
24190 XRectangle *r1, *r2, *result;
24191 {
24192 XRectangle *left, *right;
24193 XRectangle *upper, *lower;
24194 int intersection_p = 0;
24195
24196 /* Rearrange so that R1 is the left-most rectangle. */
24197 if (r1->x < r2->x)
24198 left = r1, right = r2;
24199 else
24200 left = r2, right = r1;
24201
24202 /* X0 of the intersection is right.x0, if this is inside R1,
24203 otherwise there is no intersection. */
24204 if (right->x <= left->x + left->width)
24205 {
24206 result->x = right->x;
24207
24208 /* The right end of the intersection is the minimum of the
24209 the right ends of left and right. */
24210 result->width = (min (left->x + left->width, right->x + right->width)
24211 - result->x);
24212
24213 /* Same game for Y. */
24214 if (r1->y < r2->y)
24215 upper = r1, lower = r2;
24216 else
24217 upper = r2, lower = r1;
24218
24219 /* The upper end of the intersection is lower.y0, if this is inside
24220 of upper. Otherwise, there is no intersection. */
24221 if (lower->y <= upper->y + upper->height)
24222 {
24223 result->y = lower->y;
24224
24225 /* The lower end of the intersection is the minimum of the lower
24226 ends of upper and lower. */
24227 result->height = (min (lower->y + lower->height,
24228 upper->y + upper->height)
24229 - result->y);
24230 intersection_p = 1;
24231 }
24232 }
24233
24234 return intersection_p;
24235 }
24236
24237 #endif /* HAVE_WINDOW_SYSTEM */
24238
24239 \f
24240 /***********************************************************************
24241 Initialization
24242 ***********************************************************************/
24243
24244 void
24245 syms_of_xdisp ()
24246 {
24247 Vwith_echo_area_save_vector = Qnil;
24248 staticpro (&Vwith_echo_area_save_vector);
24249
24250 Vmessage_stack = Qnil;
24251 staticpro (&Vmessage_stack);
24252
24253 Qinhibit_redisplay = intern ("inhibit-redisplay");
24254 staticpro (&Qinhibit_redisplay);
24255
24256 message_dolog_marker1 = Fmake_marker ();
24257 staticpro (&message_dolog_marker1);
24258 message_dolog_marker2 = Fmake_marker ();
24259 staticpro (&message_dolog_marker2);
24260 message_dolog_marker3 = Fmake_marker ();
24261 staticpro (&message_dolog_marker3);
24262
24263 #if GLYPH_DEBUG
24264 defsubr (&Sdump_frame_glyph_matrix);
24265 defsubr (&Sdump_glyph_matrix);
24266 defsubr (&Sdump_glyph_row);
24267 defsubr (&Sdump_tool_bar_row);
24268 defsubr (&Strace_redisplay);
24269 defsubr (&Strace_to_stderr);
24270 #endif
24271 #ifdef HAVE_WINDOW_SYSTEM
24272 defsubr (&Stool_bar_lines_needed);
24273 defsubr (&Slookup_image_map);
24274 #endif
24275 defsubr (&Sformat_mode_line);
24276 defsubr (&Sinvisible_p);
24277
24278 staticpro (&Qmenu_bar_update_hook);
24279 Qmenu_bar_update_hook = intern ("menu-bar-update-hook");
24280
24281 staticpro (&Qoverriding_terminal_local_map);
24282 Qoverriding_terminal_local_map = intern ("overriding-terminal-local-map");
24283
24284 staticpro (&Qoverriding_local_map);
24285 Qoverriding_local_map = intern ("overriding-local-map");
24286
24287 staticpro (&Qwindow_scroll_functions);
24288 Qwindow_scroll_functions = intern ("window-scroll-functions");
24289
24290 staticpro (&Qredisplay_end_trigger_functions);
24291 Qredisplay_end_trigger_functions = intern ("redisplay-end-trigger-functions");
24292
24293 staticpro (&Qinhibit_point_motion_hooks);
24294 Qinhibit_point_motion_hooks = intern ("inhibit-point-motion-hooks");
24295
24296 QCdata = intern (":data");
24297 staticpro (&QCdata);
24298 Qdisplay = intern ("display");
24299 staticpro (&Qdisplay);
24300 Qspace_width = intern ("space-width");
24301 staticpro (&Qspace_width);
24302 Qraise = intern ("raise");
24303 staticpro (&Qraise);
24304 Qslice = intern ("slice");
24305 staticpro (&Qslice);
24306 Qspace = intern ("space");
24307 staticpro (&Qspace);
24308 Qmargin = intern ("margin");
24309 staticpro (&Qmargin);
24310 Qpointer = intern ("pointer");
24311 staticpro (&Qpointer);
24312 Qleft_margin = intern ("left-margin");
24313 staticpro (&Qleft_margin);
24314 Qright_margin = intern ("right-margin");
24315 staticpro (&Qright_margin);
24316 Qcenter = intern ("center");
24317 staticpro (&Qcenter);
24318 Qline_height = intern ("line-height");
24319 staticpro (&Qline_height);
24320 QCalign_to = intern (":align-to");
24321 staticpro (&QCalign_to);
24322 QCrelative_width = intern (":relative-width");
24323 staticpro (&QCrelative_width);
24324 QCrelative_height = intern (":relative-height");
24325 staticpro (&QCrelative_height);
24326 QCeval = intern (":eval");
24327 staticpro (&QCeval);
24328 QCpropertize = intern (":propertize");
24329 staticpro (&QCpropertize);
24330 QCfile = intern (":file");
24331 staticpro (&QCfile);
24332 Qfontified = intern ("fontified");
24333 staticpro (&Qfontified);
24334 Qfontification_functions = intern ("fontification-functions");
24335 staticpro (&Qfontification_functions);
24336 Qtrailing_whitespace = intern ("trailing-whitespace");
24337 staticpro (&Qtrailing_whitespace);
24338 Qescape_glyph = intern ("escape-glyph");
24339 staticpro (&Qescape_glyph);
24340 Qnobreak_space = intern ("nobreak-space");
24341 staticpro (&Qnobreak_space);
24342 Qimage = intern ("image");
24343 staticpro (&Qimage);
24344 QCmap = intern (":map");
24345 staticpro (&QCmap);
24346 QCpointer = intern (":pointer");
24347 staticpro (&QCpointer);
24348 Qrect = intern ("rect");
24349 staticpro (&Qrect);
24350 Qcircle = intern ("circle");
24351 staticpro (&Qcircle);
24352 Qpoly = intern ("poly");
24353 staticpro (&Qpoly);
24354 Qmessage_truncate_lines = intern ("message-truncate-lines");
24355 staticpro (&Qmessage_truncate_lines);
24356 Qgrow_only = intern ("grow-only");
24357 staticpro (&Qgrow_only);
24358 Qinhibit_menubar_update = intern ("inhibit-menubar-update");
24359 staticpro (&Qinhibit_menubar_update);
24360 Qinhibit_eval_during_redisplay = intern ("inhibit-eval-during-redisplay");
24361 staticpro (&Qinhibit_eval_during_redisplay);
24362 Qposition = intern ("position");
24363 staticpro (&Qposition);
24364 Qbuffer_position = intern ("buffer-position");
24365 staticpro (&Qbuffer_position);
24366 Qobject = intern ("object");
24367 staticpro (&Qobject);
24368 Qbar = intern ("bar");
24369 staticpro (&Qbar);
24370 Qhbar = intern ("hbar");
24371 staticpro (&Qhbar);
24372 Qbox = intern ("box");
24373 staticpro (&Qbox);
24374 Qhollow = intern ("hollow");
24375 staticpro (&Qhollow);
24376 Qhand = intern ("hand");
24377 staticpro (&Qhand);
24378 Qarrow = intern ("arrow");
24379 staticpro (&Qarrow);
24380 Qtext = intern ("text");
24381 staticpro (&Qtext);
24382 Qrisky_local_variable = intern ("risky-local-variable");
24383 staticpro (&Qrisky_local_variable);
24384 Qinhibit_free_realized_faces = intern ("inhibit-free-realized-faces");
24385 staticpro (&Qinhibit_free_realized_faces);
24386
24387 list_of_error = Fcons (Fcons (intern ("error"),
24388 Fcons (intern ("void-variable"), Qnil)),
24389 Qnil);
24390 staticpro (&list_of_error);
24391
24392 Qlast_arrow_position = intern ("last-arrow-position");
24393 staticpro (&Qlast_arrow_position);
24394 Qlast_arrow_string = intern ("last-arrow-string");
24395 staticpro (&Qlast_arrow_string);
24396
24397 Qoverlay_arrow_string = intern ("overlay-arrow-string");
24398 staticpro (&Qoverlay_arrow_string);
24399 Qoverlay_arrow_bitmap = intern ("overlay-arrow-bitmap");
24400 staticpro (&Qoverlay_arrow_bitmap);
24401
24402 echo_buffer[0] = echo_buffer[1] = Qnil;
24403 staticpro (&echo_buffer[0]);
24404 staticpro (&echo_buffer[1]);
24405
24406 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
24407 staticpro (&echo_area_buffer[0]);
24408 staticpro (&echo_area_buffer[1]);
24409
24410 Vmessages_buffer_name = build_string ("*Messages*");
24411 staticpro (&Vmessages_buffer_name);
24412
24413 mode_line_proptrans_alist = Qnil;
24414 staticpro (&mode_line_proptrans_alist);
24415 mode_line_string_list = Qnil;
24416 staticpro (&mode_line_string_list);
24417 mode_line_string_face = Qnil;
24418 staticpro (&mode_line_string_face);
24419 mode_line_string_face_prop = Qnil;
24420 staticpro (&mode_line_string_face_prop);
24421 Vmode_line_unwind_vector = Qnil;
24422 staticpro (&Vmode_line_unwind_vector);
24423
24424 help_echo_string = Qnil;
24425 staticpro (&help_echo_string);
24426 help_echo_object = Qnil;
24427 staticpro (&help_echo_object);
24428 help_echo_window = Qnil;
24429 staticpro (&help_echo_window);
24430 previous_help_echo_string = Qnil;
24431 staticpro (&previous_help_echo_string);
24432 help_echo_pos = -1;
24433
24434 #ifdef HAVE_WINDOW_SYSTEM
24435 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
24436 doc: /* *Non-nil means draw block cursor as wide as the glyph under it.
24437 For example, if a block cursor is over a tab, it will be drawn as
24438 wide as that tab on the display. */);
24439 x_stretch_cursor_p = 0;
24440 #endif
24441
24442 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
24443 doc: /* *Non-nil means highlight trailing whitespace.
24444 The face used for trailing whitespace is `trailing-whitespace'. */);
24445 Vshow_trailing_whitespace = Qnil;
24446
24447 DEFVAR_LISP ("nobreak-char-display", &Vnobreak_char_display,
24448 doc: /* *Control highlighting of nobreak space and soft hyphen.
24449 A value of t means highlight the character itself (for nobreak space,
24450 use face `nobreak-space').
24451 A value of nil means no highlighting.
24452 Other values mean display the escape glyph followed by an ordinary
24453 space or ordinary hyphen. */);
24454 Vnobreak_char_display = Qt;
24455
24456 DEFVAR_LISP ("void-text-area-pointer", &Vvoid_text_area_pointer,
24457 doc: /* *The pointer shape to show in void text areas.
24458 A value of nil means to show the text pointer. Other options are `arrow',
24459 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
24460 Vvoid_text_area_pointer = Qarrow;
24461
24462 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
24463 doc: /* Non-nil means don't actually do any redisplay.
24464 This is used for internal purposes. */);
24465 Vinhibit_redisplay = Qnil;
24466
24467 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
24468 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
24469 Vglobal_mode_string = Qnil;
24470
24471 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
24472 doc: /* Marker for where to display an arrow on top of the buffer text.
24473 This must be the beginning of a line in order to work.
24474 See also `overlay-arrow-string'. */);
24475 Voverlay_arrow_position = Qnil;
24476
24477 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
24478 doc: /* String to display as an arrow in non-window frames.
24479 See also `overlay-arrow-position'. */);
24480 Voverlay_arrow_string = build_string ("=>");
24481
24482 DEFVAR_LISP ("overlay-arrow-variable-list", &Voverlay_arrow_variable_list,
24483 doc: /* List of variables (symbols) which hold markers for overlay arrows.
24484 The symbols on this list are examined during redisplay to determine
24485 where to display overlay arrows. */);
24486 Voverlay_arrow_variable_list
24487 = Fcons (intern ("overlay-arrow-position"), Qnil);
24488
24489 DEFVAR_INT ("scroll-step", &scroll_step,
24490 doc: /* *The number of lines to try scrolling a window by when point moves out.
24491 If that fails to bring point back on frame, point is centered instead.
24492 If this is zero, point is always centered after it moves off frame.
24493 If you want scrolling to always be a line at a time, you should set
24494 `scroll-conservatively' to a large value rather than set this to 1. */);
24495
24496 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
24497 doc: /* *Scroll up to this many lines, to bring point back on screen.
24498 A value of zero means to scroll the text to center point vertically
24499 in the window. */);
24500 scroll_conservatively = 0;
24501
24502 DEFVAR_INT ("scroll-margin", &scroll_margin,
24503 doc: /* *Number of lines of margin at the top and bottom of a window.
24504 Recenter the window whenever point gets within this many lines
24505 of the top or bottom of the window. */);
24506 scroll_margin = 0;
24507
24508 DEFVAR_LISP ("display-pixels-per-inch", &Vdisplay_pixels_per_inch,
24509 doc: /* Pixels per inch value for non-window system displays.
24510 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
24511 Vdisplay_pixels_per_inch = make_float (72.0);
24512
24513 #if GLYPH_DEBUG
24514 DEFVAR_INT ("debug-end-pos", &debug_end_pos, doc: /* Don't ask. */);
24515 #endif
24516
24517 DEFVAR_BOOL ("truncate-partial-width-windows",
24518 &truncate_partial_width_windows,
24519 doc: /* *Non-nil means truncate lines in all windows less than full frame wide. */);
24520 truncate_partial_width_windows = 1;
24521
24522 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
24523 doc: /* When nil, display the mode-line/header-line/menu-bar in the default face.
24524 Any other value means to use the appropriate face, `mode-line',
24525 `header-line', or `menu' respectively. */);
24526 mode_line_inverse_video = 1;
24527
24528 DEFVAR_LISP ("line-number-display-limit", &Vline_number_display_limit,
24529 doc: /* *Maximum buffer size for which line number should be displayed.
24530 If the buffer is bigger than this, the line number does not appear
24531 in the mode line. A value of nil means no limit. */);
24532 Vline_number_display_limit = Qnil;
24533
24534 DEFVAR_INT ("line-number-display-limit-width",
24535 &line_number_display_limit_width,
24536 doc: /* *Maximum line width (in characters) for line number display.
24537 If the average length of the lines near point is bigger than this, then the
24538 line number may be omitted from the mode line. */);
24539 line_number_display_limit_width = 200;
24540
24541 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
24542 doc: /* *Non-nil means highlight region even in nonselected windows. */);
24543 highlight_nonselected_windows = 0;
24544
24545 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
24546 doc: /* Non-nil if more than one frame is visible on this display.
24547 Minibuffer-only frames don't count, but iconified frames do.
24548 This variable is not guaranteed to be accurate except while processing
24549 `frame-title-format' and `icon-title-format'. */);
24550
24551 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
24552 doc: /* Template for displaying the title bar of visible frames.
24553 \(Assuming the window manager supports this feature.)
24554
24555 This variable has the same structure as `mode-line-format', except that
24556 the %c and %l constructs are ignored. It is used only on frames for
24557 which no explicit name has been set \(see `modify-frame-parameters'). */);
24558
24559 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
24560 doc: /* Template for displaying the title bar of an iconified frame.
24561 \(Assuming the window manager supports this feature.)
24562 This variable has the same structure as `mode-line-format' (which see),
24563 and is used only on frames for which no explicit name has been set
24564 \(see `modify-frame-parameters'). */);
24565 Vicon_title_format
24566 = Vframe_title_format
24567 = Fcons (intern ("multiple-frames"),
24568 Fcons (build_string ("%b"),
24569 Fcons (Fcons (empty_unibyte_string,
24570 Fcons (intern ("invocation-name"),
24571 Fcons (build_string ("@"),
24572 Fcons (intern ("system-name"),
24573 Qnil)))),
24574 Qnil)));
24575
24576 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
24577 doc: /* Maximum number of lines to keep in the message log buffer.
24578 If nil, disable message logging. If t, log messages but don't truncate
24579 the buffer when it becomes large. */);
24580 Vmessage_log_max = make_number (100);
24581
24582 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
24583 doc: /* Functions called before redisplay, if window sizes have changed.
24584 The value should be a list of functions that take one argument.
24585 Just before redisplay, for each frame, if any of its windows have changed
24586 size since the last redisplay, or have been split or deleted,
24587 all the functions in the list are called, with the frame as argument. */);
24588 Vwindow_size_change_functions = Qnil;
24589
24590 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
24591 doc: /* List of functions to call before redisplaying a window with scrolling.
24592 Each function is called with two arguments, the window
24593 and its new display-start position. Note that the value of `window-end'
24594 is not valid when these functions are called. */);
24595 Vwindow_scroll_functions = Qnil;
24596
24597 DEFVAR_LISP ("redisplay-end-trigger-functions", &Vredisplay_end_trigger_functions,
24598 doc: /* Functions called when redisplay of a window reaches the end trigger.
24599 Each function is called with two arguments, the window and the end trigger value.
24600 See `set-window-redisplay-end-trigger'. */);
24601 Vredisplay_end_trigger_functions = Qnil;
24602
24603 DEFVAR_LISP ("mouse-autoselect-window", &Vmouse_autoselect_window,
24604 doc: /* *Non-nil means autoselect window with mouse pointer.
24605 If nil, do not autoselect windows.
24606 A positive number means delay autoselection by that many seconds: a
24607 window is autoselected only after the mouse has remained in that
24608 window for the duration of the delay.
24609 A negative number has a similar effect, but causes windows to be
24610 autoselected only after the mouse has stopped moving. \(Because of
24611 the way Emacs compares mouse events, you will occasionally wait twice
24612 that time before the window gets selected.\)
24613 Any other value means to autoselect window instantaneously when the
24614 mouse pointer enters it.
24615
24616 Autoselection selects the minibuffer only if it is active, and never
24617 unselects the minibuffer if it is active.
24618
24619 When customizing this variable make sure that the actual value of
24620 `focus-follows-mouse' matches the behavior of your window manager. */);
24621 Vmouse_autoselect_window = Qnil;
24622
24623 DEFVAR_LISP ("auto-resize-tool-bars", &Vauto_resize_tool_bars,
24624 doc: /* *Non-nil means automatically resize tool-bars.
24625 This dynamically changes the tool-bar's height to the minimum height
24626 that is needed to make all tool-bar items visible.
24627 If value is `grow-only', the tool-bar's height is only increased
24628 automatically; to decrease the tool-bar height, use \\[recenter]. */);
24629 Vauto_resize_tool_bars = Qt;
24630
24631 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
24632 doc: /* *Non-nil means raise tool-bar buttons when the mouse moves over them. */);
24633 auto_raise_tool_bar_buttons_p = 1;
24634
24635 DEFVAR_BOOL ("make-cursor-line-fully-visible", &make_cursor_line_fully_visible_p,
24636 doc: /* *Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
24637 make_cursor_line_fully_visible_p = 1;
24638
24639 DEFVAR_LISP ("tool-bar-border", &Vtool_bar_border,
24640 doc: /* *Border below tool-bar in pixels.
24641 If an integer, use it as the height of the border.
24642 If it is one of `internal-border-width' or `border-width', use the
24643 value of the corresponding frame parameter.
24644 Otherwise, no border is added below the tool-bar. */);
24645 Vtool_bar_border = Qinternal_border_width;
24646
24647 DEFVAR_LISP ("tool-bar-button-margin", &Vtool_bar_button_margin,
24648 doc: /* *Margin around tool-bar buttons in pixels.
24649 If an integer, use that for both horizontal and vertical margins.
24650 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
24651 HORZ specifying the horizontal margin, and VERT specifying the
24652 vertical margin. */);
24653 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
24654
24655 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
24656 doc: /* *Relief thickness of tool-bar buttons. */);
24657 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
24658
24659 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
24660 doc: /* List of functions to call to fontify regions of text.
24661 Each function is called with one argument POS. Functions must
24662 fontify a region starting at POS in the current buffer, and give
24663 fontified regions the property `fontified'. */);
24664 Vfontification_functions = Qnil;
24665 Fmake_variable_buffer_local (Qfontification_functions);
24666
24667 DEFVAR_BOOL ("unibyte-display-via-language-environment",
24668 &unibyte_display_via_language_environment,
24669 doc: /* *Non-nil means display unibyte text according to language environment.
24670 Specifically this means that unibyte non-ASCII characters
24671 are displayed by converting them to the equivalent multibyte characters
24672 according to the current language environment. As a result, they are
24673 displayed according to the current fontset. */);
24674 unibyte_display_via_language_environment = 0;
24675
24676 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
24677 doc: /* *Maximum height for resizing mini-windows.
24678 If a float, it specifies a fraction of the mini-window frame's height.
24679 If an integer, it specifies a number of lines. */);
24680 Vmax_mini_window_height = make_float (0.25);
24681
24682 DEFVAR_LISP ("resize-mini-windows", &Vresize_mini_windows,
24683 doc: /* *How to resize mini-windows.
24684 A value of nil means don't automatically resize mini-windows.
24685 A value of t means resize them to fit the text displayed in them.
24686 A value of `grow-only', the default, means let mini-windows grow
24687 only, until their display becomes empty, at which point the windows
24688 go back to their normal size. */);
24689 Vresize_mini_windows = Qgrow_only;
24690
24691 DEFVAR_LISP ("blink-cursor-alist", &Vblink_cursor_alist,
24692 doc: /* Alist specifying how to blink the cursor off.
24693 Each element has the form (ON-STATE . OFF-STATE). Whenever the
24694 `cursor-type' frame-parameter or variable equals ON-STATE,
24695 comparing using `equal', Emacs uses OFF-STATE to specify
24696 how to blink it off. ON-STATE and OFF-STATE are values for
24697 the `cursor-type' frame parameter.
24698
24699 If a frame's ON-STATE has no entry in this list,
24700 the frame's other specifications determine how to blink the cursor off. */);
24701 Vblink_cursor_alist = Qnil;
24702
24703 DEFVAR_BOOL ("auto-hscroll-mode", &automatic_hscrolling_p,
24704 doc: /* *Non-nil means scroll the display automatically to make point visible. */);
24705 automatic_hscrolling_p = 1;
24706
24707 DEFVAR_INT ("hscroll-margin", &hscroll_margin,
24708 doc: /* *How many columns away from the window edge point is allowed to get
24709 before automatic hscrolling will horizontally scroll the window. */);
24710 hscroll_margin = 5;
24711
24712 DEFVAR_LISP ("hscroll-step", &Vhscroll_step,
24713 doc: /* *How many columns to scroll the window when point gets too close to the edge.
24714 When point is less than `hscroll-margin' columns from the window
24715 edge, automatic hscrolling will scroll the window by the amount of columns
24716 determined by this variable. If its value is a positive integer, scroll that
24717 many columns. If it's a positive floating-point number, it specifies the
24718 fraction of the window's width to scroll. If it's nil or zero, point will be
24719 centered horizontally after the scroll. Any other value, including negative
24720 numbers, are treated as if the value were zero.
24721
24722 Automatic hscrolling always moves point outside the scroll margin, so if
24723 point was more than scroll step columns inside the margin, the window will
24724 scroll more than the value given by the scroll step.
24725
24726 Note that the lower bound for automatic hscrolling specified by `scroll-left'
24727 and `scroll-right' overrides this variable's effect. */);
24728 Vhscroll_step = make_number (0);
24729
24730 DEFVAR_BOOL ("message-truncate-lines", &message_truncate_lines,
24731 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
24732 Bind this around calls to `message' to let it take effect. */);
24733 message_truncate_lines = 0;
24734
24735 DEFVAR_LISP ("menu-bar-update-hook", &Vmenu_bar_update_hook,
24736 doc: /* Normal hook run to update the menu bar definitions.
24737 Redisplay runs this hook before it redisplays the menu bar.
24738 This is used to update submenus such as Buffers,
24739 whose contents depend on various data. */);
24740 Vmenu_bar_update_hook = Qnil;
24741
24742 DEFVAR_LISP ("menu-updating-frame", &Vmenu_updating_frame,
24743 doc: /* Frame for which we are updating a menu.
24744 The enable predicate for a menu binding should check this variable. */);
24745 Vmenu_updating_frame = Qnil;
24746
24747 DEFVAR_BOOL ("inhibit-menubar-update", &inhibit_menubar_update,
24748 doc: /* Non-nil means don't update menu bars. Internal use only. */);
24749 inhibit_menubar_update = 0;
24750
24751 DEFVAR_BOOL ("inhibit-eval-during-redisplay", &inhibit_eval_during_redisplay,
24752 doc: /* Non-nil means don't eval Lisp during redisplay. */);
24753 inhibit_eval_during_redisplay = 0;
24754
24755 DEFVAR_BOOL ("inhibit-free-realized-faces", &inhibit_free_realized_faces,
24756 doc: /* Non-nil means don't free realized faces. Internal use only. */);
24757 inhibit_free_realized_faces = 0;
24758
24759 #if GLYPH_DEBUG
24760 DEFVAR_BOOL ("inhibit-try-window-id", &inhibit_try_window_id,
24761 doc: /* Inhibit try_window_id display optimization. */);
24762 inhibit_try_window_id = 0;
24763
24764 DEFVAR_BOOL ("inhibit-try-window-reusing", &inhibit_try_window_reusing,
24765 doc: /* Inhibit try_window_reusing display optimization. */);
24766 inhibit_try_window_reusing = 0;
24767
24768 DEFVAR_BOOL ("inhibit-try-cursor-movement", &inhibit_try_cursor_movement,
24769 doc: /* Inhibit try_cursor_movement display optimization. */);
24770 inhibit_try_cursor_movement = 0;
24771 #endif /* GLYPH_DEBUG */
24772
24773 DEFVAR_INT ("overline-margin", &overline_margin,
24774 doc: /* *Space between overline and text, in pixels.
24775 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
24776 margin to the caracter height. */);
24777 overline_margin = 2;
24778 }
24779
24780
24781 /* Initialize this module when Emacs starts. */
24782
24783 void
24784 init_xdisp ()
24785 {
24786 Lisp_Object root_window;
24787 struct window *mini_w;
24788
24789 current_header_line_height = current_mode_line_height = -1;
24790
24791 CHARPOS (this_line_start_pos) = 0;
24792
24793 mini_w = XWINDOW (minibuf_window);
24794 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
24795
24796 if (!noninteractive)
24797 {
24798 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
24799 int i;
24800
24801 XWINDOW (root_window)->top_line = make_number (FRAME_TOP_MARGIN (f));
24802 set_window_height (root_window,
24803 FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f),
24804 0);
24805 mini_w->top_line = make_number (FRAME_LINES (f) - 1);
24806 set_window_height (minibuf_window, 1, 0);
24807
24808 XWINDOW (root_window)->total_cols = make_number (FRAME_COLS (f));
24809 mini_w->total_cols = make_number (FRAME_COLS (f));
24810
24811 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
24812 scratch_glyph_row.glyphs[TEXT_AREA + 1]
24813 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
24814
24815 /* The default ellipsis glyphs `...'. */
24816 for (i = 0; i < 3; ++i)
24817 default_invis_vector[i] = make_number ('.');
24818 }
24819
24820 {
24821 /* Allocate the buffer for frame titles.
24822 Also used for `format-mode-line'. */
24823 int size = 100;
24824 mode_line_noprop_buf = (char *) xmalloc (size);
24825 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
24826 mode_line_noprop_ptr = mode_line_noprop_buf;
24827 mode_line_target = MODE_LINE_DISPLAY;
24828 }
24829
24830 help_echo_showing_p = 0;
24831 }
24832
24833
24834 /* arch-tag: eacc864d-bb6a-4b74-894a-1a4399a1358b
24835 (do not change this comment) */