]> code.delx.au - gnu-emacs/blob - src/xdisp.c
(handle_auto_composed_prop): Fix previous change.
[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 = Qnil;
4557 EMACS_INT pos, pos_byte, this_pos, start, end;
4558 int c;
4559
4560 if (STRINGP (it->string))
4561 {
4562 const unsigned char *s;
4563
4564 pos = IT_STRING_CHARPOS (*it);
4565 pos_byte = IT_STRING_BYTEPOS (*it);
4566 s = SDATA (it->string) + pos_byte;
4567 if (STRING_MULTIBYTE (it->string))
4568 c = STRING_CHAR (s, 0);
4569 else
4570 c = *s;
4571 }
4572 else
4573 {
4574 pos = IT_CHARPOS (*it);
4575 pos_byte = IT_BYTEPOS (*it);
4576 c = FETCH_CHAR (pos_byte);
4577 }
4578 this_pos = pos;
4579
4580 if (get_property_and_range (pos, Qauto_composed, &val, &start, &end,
4581 it->string))
4582 {
4583 Lisp_Object cmp_prop;
4584 EMACS_INT cmp_start, cmp_end;
4585
4586 #ifdef USE_FONT_BACKEND
4587 if (enable_font_backend
4588 && get_property_and_range (pos, Qcomposition, &cmp_prop,
4589 &cmp_start, &cmp_end, it->string)
4590 && cmp_start == pos
4591 && COMPOSITION_METHOD (cmp_prop) == COMPOSITION_WITH_GLYPH_STRING)
4592 {
4593 Lisp_Object gstring = COMPOSITION_COMPONENTS (cmp_prop);
4594 Lisp_Object font_object = LGSTRING_FONT (gstring);
4595
4596 if (! EQ (font_object,
4597 font_at (c, pos, FACE_FROM_ID (it->f, it->face_id),
4598 it->w, it->string)))
4599 /* We must re-compute the composition for the
4600 different font. */
4601 val = Qnil;
4602 }
4603 #endif
4604 if (! NILP (val))
4605 {
4606 EMACS_INT limit;
4607
4608 /* As Fnext_single_char_property_change is very slow, we
4609 limit the search to the current line. */
4610 if (STRINGP (it->string))
4611 limit = SCHARS (it->string);
4612 else
4613 limit = find_next_newline_no_quit (pos, 1);
4614
4615 if (end < limit)
4616 {
4617 /* The current point is auto-composed, but there
4618 exist characters not yet composed beyond the
4619 auto-composed region. There's a possiblity that
4620 the last characters in the region may be newly
4621 composed. */
4622 if (pos < end - 1)
4623 {
4624 if (get_property_and_range (end - 1, Qcomposition,
4625 &cmp_prop, &cmp_start,
4626 &cmp_end, it->string))
4627 pos = cmp_start;
4628 else
4629 pos = end - 1;
4630 }
4631 val = Qnil;
4632 }
4633 }
4634 }
4635 if (NILP (val))
4636 {
4637 int count = SPECPDL_INDEX ();
4638 Lisp_Object args[4];
4639
4640 args[0] = Vauto_composition_function;
4641 specbind (Qauto_composition_function, Qnil);
4642 args[1] = make_number (pos);
4643 args[2] = it->string;
4644 #ifdef USE_FONT_BACKEND
4645 if (enable_font_backend)
4646 args[3] = it->window;
4647 else
4648 #endif /* USE_FONT_BACKEND */
4649 args[3] = Qnil;
4650 safe_call (4, args);
4651 unbind_to (count, Qnil);
4652 }
4653 }
4654
4655 return handled;
4656 }
4657
4658 /* Set up iterator IT from `composition' property at its current
4659 position. Called from handle_stop. */
4660
4661 static enum prop_handled
4662 handle_composition_prop (it)
4663 struct it *it;
4664 {
4665 Lisp_Object prop, string;
4666 EMACS_INT pos, pos_byte, start, end;
4667 enum prop_handled handled = HANDLED_NORMALLY;
4668
4669 if (STRINGP (it->string))
4670 {
4671 pos = IT_STRING_CHARPOS (*it);
4672 pos_byte = IT_STRING_BYTEPOS (*it);
4673 string = it->string;
4674 }
4675 else
4676 {
4677 pos = IT_CHARPOS (*it);
4678 pos_byte = IT_BYTEPOS (*it);
4679 string = Qnil;
4680 }
4681
4682 /* If there's a valid composition and point is not inside of the
4683 composition (in the case that the composition is from the current
4684 buffer), draw a glyph composed from the composition components. */
4685 if (find_composition (pos, -1, &start, &end, &prop, string)
4686 && COMPOSITION_VALID_P (start, end, prop)
4687 && (STRINGP (it->string) || (PT <= start || PT >= end)))
4688 {
4689 int id;
4690
4691 if (start != pos)
4692 {
4693 if (STRINGP (it->string))
4694 pos_byte = string_char_to_byte (it->string, start);
4695 else
4696 pos_byte = CHAR_TO_BYTE (start);
4697 }
4698 id = get_composition_id (start, pos_byte, end - start, prop, string);
4699
4700 if (id >= 0)
4701 {
4702 struct composition *cmp = composition_table[id];
4703
4704 if (cmp->glyph_len == 0)
4705 {
4706 /* No glyph. */
4707 if (STRINGP (it->string))
4708 {
4709 IT_STRING_CHARPOS (*it) = end;
4710 IT_STRING_BYTEPOS (*it) = string_char_to_byte (it->string,
4711 end);
4712 }
4713 else
4714 {
4715 IT_CHARPOS (*it) = end;
4716 IT_BYTEPOS (*it) = CHAR_TO_BYTE (end);
4717 }
4718 return HANDLED_RECOMPUTE_PROPS;
4719 }
4720
4721 it->stop_charpos = end;
4722 push_it (it);
4723
4724 it->method = GET_FROM_COMPOSITION;
4725 it->cmp_id = id;
4726 it->cmp_len = COMPOSITION_LENGTH (prop);
4727 /* For a terminal, draw only the first (non-TAB) character
4728 of the components. */
4729 #ifdef USE_FONT_BACKEND
4730 if (composition_table[id]->method == COMPOSITION_WITH_GLYPH_STRING)
4731 {
4732 Lisp_Object lgstring = AREF (XHASH_TABLE (composition_hash_table)
4733 ->key_and_value,
4734 cmp->hash_index * 2);
4735
4736 it->c = LGLYPH_CHAR (LGSTRING_GLYPH (lgstring, 0));
4737 }
4738 else
4739 #endif /* USE_FONT_BACKEND */
4740 {
4741 int i;
4742
4743 for (i = 0; i < cmp->glyph_len; i++)
4744 if ((it->c = COMPOSITION_GLYPH (composition_table[id], i))
4745 != '\t')
4746 break;
4747 }
4748 if (it->c == '\t')
4749 it->c = ' ';
4750 it->len = (STRINGP (it->string)
4751 ? string_char_to_byte (it->string, end)
4752 : CHAR_TO_BYTE (end)) - pos_byte;
4753 handled = HANDLED_RETURN;
4754 }
4755 }
4756
4757 return handled;
4758 }
4759
4760
4761 \f
4762 /***********************************************************************
4763 Overlay strings
4764 ***********************************************************************/
4765
4766 /* The following structure is used to record overlay strings for
4767 later sorting in load_overlay_strings. */
4768
4769 struct overlay_entry
4770 {
4771 Lisp_Object overlay;
4772 Lisp_Object string;
4773 int priority;
4774 int after_string_p;
4775 };
4776
4777
4778 /* Set up iterator IT from overlay strings at its current position.
4779 Called from handle_stop. */
4780
4781 static enum prop_handled
4782 handle_overlay_change (it)
4783 struct it *it;
4784 {
4785 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
4786 return HANDLED_RECOMPUTE_PROPS;
4787 else
4788 return HANDLED_NORMALLY;
4789 }
4790
4791
4792 /* Set up the next overlay string for delivery by IT, if there is an
4793 overlay string to deliver. Called by set_iterator_to_next when the
4794 end of the current overlay string is reached. If there are more
4795 overlay strings to display, IT->string and
4796 IT->current.overlay_string_index are set appropriately here.
4797 Otherwise IT->string is set to nil. */
4798
4799 static void
4800 next_overlay_string (it)
4801 struct it *it;
4802 {
4803 ++it->current.overlay_string_index;
4804 if (it->current.overlay_string_index == it->n_overlay_strings)
4805 {
4806 /* No more overlay strings. Restore IT's settings to what
4807 they were before overlay strings were processed, and
4808 continue to deliver from current_buffer. */
4809 int display_ellipsis_p = it->stack[it->sp - 1].display_ellipsis_p;
4810
4811 pop_it (it);
4812 xassert (it->sp > 0
4813 || it->method == GET_FROM_COMPOSITION
4814 || (NILP (it->string)
4815 && it->method == GET_FROM_BUFFER
4816 && it->stop_charpos >= BEGV
4817 && it->stop_charpos <= it->end_charpos));
4818 it->current.overlay_string_index = -1;
4819 it->n_overlay_strings = 0;
4820
4821 /* If we're at the end of the buffer, record that we have
4822 processed the overlay strings there already, so that
4823 next_element_from_buffer doesn't try it again. */
4824 if (IT_CHARPOS (*it) >= it->end_charpos)
4825 it->overlay_strings_at_end_processed_p = 1;
4826
4827 /* If we have to display `...' for invisible text, set
4828 the iterator up for that. */
4829 if (display_ellipsis_p)
4830 setup_for_ellipsis (it, 0);
4831 }
4832 else
4833 {
4834 /* There are more overlay strings to process. If
4835 IT->current.overlay_string_index has advanced to a position
4836 where we must load IT->overlay_strings with more strings, do
4837 it. */
4838 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
4839
4840 if (it->current.overlay_string_index && i == 0)
4841 load_overlay_strings (it, 0);
4842
4843 /* Initialize IT to deliver display elements from the overlay
4844 string. */
4845 it->string = it->overlay_strings[i];
4846 it->multibyte_p = STRING_MULTIBYTE (it->string);
4847 SET_TEXT_POS (it->current.string_pos, 0, 0);
4848 it->method = GET_FROM_STRING;
4849 it->stop_charpos = 0;
4850 }
4851
4852 CHECK_IT (it);
4853 }
4854
4855
4856 /* Compare two overlay_entry structures E1 and E2. Used as a
4857 comparison function for qsort in load_overlay_strings. Overlay
4858 strings for the same position are sorted so that
4859
4860 1. All after-strings come in front of before-strings, except
4861 when they come from the same overlay.
4862
4863 2. Within after-strings, strings are sorted so that overlay strings
4864 from overlays with higher priorities come first.
4865
4866 2. Within before-strings, strings are sorted so that overlay
4867 strings from overlays with higher priorities come last.
4868
4869 Value is analogous to strcmp. */
4870
4871
4872 static int
4873 compare_overlay_entries (e1, e2)
4874 void *e1, *e2;
4875 {
4876 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
4877 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
4878 int result;
4879
4880 if (entry1->after_string_p != entry2->after_string_p)
4881 {
4882 /* Let after-strings appear in front of before-strings if
4883 they come from different overlays. */
4884 if (EQ (entry1->overlay, entry2->overlay))
4885 result = entry1->after_string_p ? 1 : -1;
4886 else
4887 result = entry1->after_string_p ? -1 : 1;
4888 }
4889 else if (entry1->after_string_p)
4890 /* After-strings sorted in order of decreasing priority. */
4891 result = entry2->priority - entry1->priority;
4892 else
4893 /* Before-strings sorted in order of increasing priority. */
4894 result = entry1->priority - entry2->priority;
4895
4896 return result;
4897 }
4898
4899
4900 /* Load the vector IT->overlay_strings with overlay strings from IT's
4901 current buffer position, or from CHARPOS if that is > 0. Set
4902 IT->n_overlays to the total number of overlay strings found.
4903
4904 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
4905 a time. On entry into load_overlay_strings,
4906 IT->current.overlay_string_index gives the number of overlay
4907 strings that have already been loaded by previous calls to this
4908 function.
4909
4910 IT->add_overlay_start contains an additional overlay start
4911 position to consider for taking overlay strings from, if non-zero.
4912 This position comes into play when the overlay has an `invisible'
4913 property, and both before and after-strings. When we've skipped to
4914 the end of the overlay, because of its `invisible' property, we
4915 nevertheless want its before-string to appear.
4916 IT->add_overlay_start will contain the overlay start position
4917 in this case.
4918
4919 Overlay strings are sorted so that after-string strings come in
4920 front of before-string strings. Within before and after-strings,
4921 strings are sorted by overlay priority. See also function
4922 compare_overlay_entries. */
4923
4924 static void
4925 load_overlay_strings (it, charpos)
4926 struct it *it;
4927 int charpos;
4928 {
4929 extern Lisp_Object Qafter_string, Qbefore_string, Qwindow, Qpriority;
4930 Lisp_Object overlay, window, str, invisible;
4931 struct Lisp_Overlay *ov;
4932 int start, end;
4933 int size = 20;
4934 int n = 0, i, j, invis_p;
4935 struct overlay_entry *entries
4936 = (struct overlay_entry *) alloca (size * sizeof *entries);
4937
4938 if (charpos <= 0)
4939 charpos = IT_CHARPOS (*it);
4940
4941 /* Append the overlay string STRING of overlay OVERLAY to vector
4942 `entries' which has size `size' and currently contains `n'
4943 elements. AFTER_P non-zero means STRING is an after-string of
4944 OVERLAY. */
4945 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
4946 do \
4947 { \
4948 Lisp_Object priority; \
4949 \
4950 if (n == size) \
4951 { \
4952 int new_size = 2 * size; \
4953 struct overlay_entry *old = entries; \
4954 entries = \
4955 (struct overlay_entry *) alloca (new_size \
4956 * sizeof *entries); \
4957 bcopy (old, entries, size * sizeof *entries); \
4958 size = new_size; \
4959 } \
4960 \
4961 entries[n].string = (STRING); \
4962 entries[n].overlay = (OVERLAY); \
4963 priority = Foverlay_get ((OVERLAY), Qpriority); \
4964 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
4965 entries[n].after_string_p = (AFTER_P); \
4966 ++n; \
4967 } \
4968 while (0)
4969
4970 /* Process overlay before the overlay center. */
4971 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
4972 {
4973 XSETMISC (overlay, ov);
4974 xassert (OVERLAYP (overlay));
4975 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4976 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4977
4978 if (end < charpos)
4979 break;
4980
4981 /* Skip this overlay if it doesn't start or end at IT's current
4982 position. */
4983 if (end != charpos && start != charpos)
4984 continue;
4985
4986 /* Skip this overlay if it doesn't apply to IT->w. */
4987 window = Foverlay_get (overlay, Qwindow);
4988 if (WINDOWP (window) && XWINDOW (window) != it->w)
4989 continue;
4990
4991 /* If the text ``under'' the overlay is invisible, both before-
4992 and after-strings from this overlay are visible; start and
4993 end position are indistinguishable. */
4994 invisible = Foverlay_get (overlay, Qinvisible);
4995 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4996
4997 /* If overlay has a non-empty before-string, record it. */
4998 if ((start == charpos || (end == charpos && invis_p))
4999 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5000 && SCHARS (str))
5001 RECORD_OVERLAY_STRING (overlay, str, 0);
5002
5003 /* If overlay has a non-empty after-string, record it. */
5004 if ((end == charpos || (start == charpos && invis_p))
5005 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5006 && SCHARS (str))
5007 RECORD_OVERLAY_STRING (overlay, str, 1);
5008 }
5009
5010 /* Process overlays after the overlay center. */
5011 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
5012 {
5013 XSETMISC (overlay, ov);
5014 xassert (OVERLAYP (overlay));
5015 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5016 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5017
5018 if (start > charpos)
5019 break;
5020
5021 /* Skip this overlay if it doesn't start or end at IT's current
5022 position. */
5023 if (end != charpos && start != charpos)
5024 continue;
5025
5026 /* Skip this overlay if it doesn't apply to IT->w. */
5027 window = Foverlay_get (overlay, Qwindow);
5028 if (WINDOWP (window) && XWINDOW (window) != it->w)
5029 continue;
5030
5031 /* If the text ``under'' the overlay is invisible, it has a zero
5032 dimension, and both before- and after-strings apply. */
5033 invisible = Foverlay_get (overlay, Qinvisible);
5034 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5035
5036 /* If overlay has a non-empty before-string, record it. */
5037 if ((start == charpos || (end == charpos && invis_p))
5038 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5039 && SCHARS (str))
5040 RECORD_OVERLAY_STRING (overlay, str, 0);
5041
5042 /* If overlay has a non-empty after-string, record it. */
5043 if ((end == charpos || (start == charpos && invis_p))
5044 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5045 && SCHARS (str))
5046 RECORD_OVERLAY_STRING (overlay, str, 1);
5047 }
5048
5049 #undef RECORD_OVERLAY_STRING
5050
5051 /* Sort entries. */
5052 if (n > 1)
5053 qsort (entries, n, sizeof *entries, compare_overlay_entries);
5054
5055 /* Record the total number of strings to process. */
5056 it->n_overlay_strings = n;
5057
5058 /* IT->current.overlay_string_index is the number of overlay strings
5059 that have already been consumed by IT. Copy some of the
5060 remaining overlay strings to IT->overlay_strings. */
5061 i = 0;
5062 j = it->current.overlay_string_index;
5063 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
5064 {
5065 it->overlay_strings[i] = entries[j].string;
5066 it->string_overlays[i++] = entries[j++].overlay;
5067 }
5068
5069 CHECK_IT (it);
5070 }
5071
5072
5073 /* Get the first chunk of overlay strings at IT's current buffer
5074 position, or at CHARPOS if that is > 0. Value is non-zero if at
5075 least one overlay string was found. */
5076
5077 static int
5078 get_overlay_strings_1 (it, charpos, compute_stop_p)
5079 struct it *it;
5080 int charpos;
5081 {
5082 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
5083 process. This fills IT->overlay_strings with strings, and sets
5084 IT->n_overlay_strings to the total number of strings to process.
5085 IT->pos.overlay_string_index has to be set temporarily to zero
5086 because load_overlay_strings needs this; it must be set to -1
5087 when no overlay strings are found because a zero value would
5088 indicate a position in the first overlay string. */
5089 it->current.overlay_string_index = 0;
5090 load_overlay_strings (it, charpos);
5091
5092 /* If we found overlay strings, set up IT to deliver display
5093 elements from the first one. Otherwise set up IT to deliver
5094 from current_buffer. */
5095 if (it->n_overlay_strings)
5096 {
5097 /* Make sure we know settings in current_buffer, so that we can
5098 restore meaningful values when we're done with the overlay
5099 strings. */
5100 if (compute_stop_p)
5101 compute_stop_pos (it);
5102 xassert (it->face_id >= 0);
5103
5104 /* Save IT's settings. They are restored after all overlay
5105 strings have been processed. */
5106 xassert (!compute_stop_p || it->sp == 0);
5107 push_it (it);
5108
5109 /* Set up IT to deliver display elements from the first overlay
5110 string. */
5111 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5112 it->string = it->overlay_strings[0];
5113 it->from_overlay = Qnil;
5114 it->stop_charpos = 0;
5115 xassert (STRINGP (it->string));
5116 it->end_charpos = SCHARS (it->string);
5117 it->multibyte_p = STRING_MULTIBYTE (it->string);
5118 it->method = GET_FROM_STRING;
5119 return 1;
5120 }
5121
5122 it->current.overlay_string_index = -1;
5123 return 0;
5124 }
5125
5126 static int
5127 get_overlay_strings (it, charpos)
5128 struct it *it;
5129 int charpos;
5130 {
5131 it->string = Qnil;
5132 it->method = GET_FROM_BUFFER;
5133
5134 (void) get_overlay_strings_1 (it, charpos, 1);
5135
5136 CHECK_IT (it);
5137
5138 /* Value is non-zero if we found at least one overlay string. */
5139 return STRINGP (it->string);
5140 }
5141
5142
5143 \f
5144 /***********************************************************************
5145 Saving and restoring state
5146 ***********************************************************************/
5147
5148 /* Save current settings of IT on IT->stack. Called, for example,
5149 before setting up IT for an overlay string, to be able to restore
5150 IT's settings to what they were after the overlay string has been
5151 processed. */
5152
5153 static void
5154 push_it (it)
5155 struct it *it;
5156 {
5157 struct iterator_stack_entry *p;
5158
5159 xassert (it->sp < IT_STACK_SIZE);
5160 p = it->stack + it->sp;
5161
5162 p->stop_charpos = it->stop_charpos;
5163 xassert (it->face_id >= 0);
5164 p->face_id = it->face_id;
5165 p->string = it->string;
5166 p->method = it->method;
5167 p->from_overlay = it->from_overlay;
5168 switch (p->method)
5169 {
5170 case GET_FROM_IMAGE:
5171 p->u.image.object = it->object;
5172 p->u.image.image_id = it->image_id;
5173 p->u.image.slice = it->slice;
5174 break;
5175 case GET_FROM_COMPOSITION:
5176 p->u.comp.object = it->object;
5177 p->u.comp.c = it->c;
5178 p->u.comp.len = it->len;
5179 p->u.comp.cmp_id = it->cmp_id;
5180 p->u.comp.cmp_len = it->cmp_len;
5181 break;
5182 case GET_FROM_STRETCH:
5183 p->u.stretch.object = it->object;
5184 break;
5185 }
5186 p->position = it->position;
5187 p->current = it->current;
5188 p->end_charpos = it->end_charpos;
5189 p->string_nchars = it->string_nchars;
5190 p->area = it->area;
5191 p->multibyte_p = it->multibyte_p;
5192 p->space_width = it->space_width;
5193 p->font_height = it->font_height;
5194 p->voffset = it->voffset;
5195 p->string_from_display_prop_p = it->string_from_display_prop_p;
5196 p->display_ellipsis_p = 0;
5197 ++it->sp;
5198 }
5199
5200
5201 /* Restore IT's settings from IT->stack. Called, for example, when no
5202 more overlay strings must be processed, and we return to delivering
5203 display elements from a buffer, or when the end of a string from a
5204 `display' property is reached and we return to delivering display
5205 elements from an overlay string, or from a buffer. */
5206
5207 static void
5208 pop_it (it)
5209 struct it *it;
5210 {
5211 struct iterator_stack_entry *p;
5212
5213 xassert (it->sp > 0);
5214 --it->sp;
5215 p = it->stack + it->sp;
5216 it->stop_charpos = p->stop_charpos;
5217 it->face_id = p->face_id;
5218 it->current = p->current;
5219 it->position = p->position;
5220 it->string = p->string;
5221 it->from_overlay = p->from_overlay;
5222 if (NILP (it->string))
5223 SET_TEXT_POS (it->current.string_pos, -1, -1);
5224 it->method = p->method;
5225 switch (it->method)
5226 {
5227 case GET_FROM_IMAGE:
5228 it->image_id = p->u.image.image_id;
5229 it->object = p->u.image.object;
5230 it->slice = p->u.image.slice;
5231 break;
5232 case GET_FROM_COMPOSITION:
5233 it->object = p->u.comp.object;
5234 it->c = p->u.comp.c;
5235 it->len = p->u.comp.len;
5236 it->cmp_id = p->u.comp.cmp_id;
5237 it->cmp_len = p->u.comp.cmp_len;
5238 break;
5239 case GET_FROM_STRETCH:
5240 it->object = p->u.comp.object;
5241 break;
5242 case GET_FROM_BUFFER:
5243 it->object = it->w->buffer;
5244 break;
5245 case GET_FROM_STRING:
5246 it->object = it->string;
5247 break;
5248 }
5249 it->end_charpos = p->end_charpos;
5250 it->string_nchars = p->string_nchars;
5251 it->area = p->area;
5252 it->multibyte_p = p->multibyte_p;
5253 it->space_width = p->space_width;
5254 it->font_height = p->font_height;
5255 it->voffset = p->voffset;
5256 it->string_from_display_prop_p = p->string_from_display_prop_p;
5257 }
5258
5259
5260 \f
5261 /***********************************************************************
5262 Moving over lines
5263 ***********************************************************************/
5264
5265 /* Set IT's current position to the previous line start. */
5266
5267 static void
5268 back_to_previous_line_start (it)
5269 struct it *it;
5270 {
5271 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
5272 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
5273 }
5274
5275
5276 /* Move IT to the next line start.
5277
5278 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
5279 we skipped over part of the text (as opposed to moving the iterator
5280 continuously over the text). Otherwise, don't change the value
5281 of *SKIPPED_P.
5282
5283 Newlines may come from buffer text, overlay strings, or strings
5284 displayed via the `display' property. That's the reason we can't
5285 simply use find_next_newline_no_quit.
5286
5287 Note that this function may not skip over invisible text that is so
5288 because of text properties and immediately follows a newline. If
5289 it would, function reseat_at_next_visible_line_start, when called
5290 from set_iterator_to_next, would effectively make invisible
5291 characters following a newline part of the wrong glyph row, which
5292 leads to wrong cursor motion. */
5293
5294 static int
5295 forward_to_next_line_start (it, skipped_p)
5296 struct it *it;
5297 int *skipped_p;
5298 {
5299 int old_selective, newline_found_p, n;
5300 const int MAX_NEWLINE_DISTANCE = 500;
5301
5302 /* If already on a newline, just consume it to avoid unintended
5303 skipping over invisible text below. */
5304 if (it->what == IT_CHARACTER
5305 && it->c == '\n'
5306 && CHARPOS (it->position) == IT_CHARPOS (*it))
5307 {
5308 set_iterator_to_next (it, 0);
5309 it->c = 0;
5310 return 1;
5311 }
5312
5313 /* Don't handle selective display in the following. It's (a)
5314 unnecessary because it's done by the caller, and (b) leads to an
5315 infinite recursion because next_element_from_ellipsis indirectly
5316 calls this function. */
5317 old_selective = it->selective;
5318 it->selective = 0;
5319
5320 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
5321 from buffer text. */
5322 for (n = newline_found_p = 0;
5323 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
5324 n += STRINGP (it->string) ? 0 : 1)
5325 {
5326 if (!get_next_display_element (it))
5327 return 0;
5328 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
5329 set_iterator_to_next (it, 0);
5330 }
5331
5332 /* If we didn't find a newline near enough, see if we can use a
5333 short-cut. */
5334 if (!newline_found_p)
5335 {
5336 int start = IT_CHARPOS (*it);
5337 int limit = find_next_newline_no_quit (start, 1);
5338 Lisp_Object pos;
5339
5340 xassert (!STRINGP (it->string));
5341
5342 /* If there isn't any `display' property in sight, and no
5343 overlays, we can just use the position of the newline in
5344 buffer text. */
5345 if (it->stop_charpos >= limit
5346 || ((pos = Fnext_single_property_change (make_number (start),
5347 Qdisplay,
5348 Qnil, make_number (limit)),
5349 NILP (pos))
5350 && next_overlay_change (start) == ZV))
5351 {
5352 IT_CHARPOS (*it) = limit;
5353 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
5354 *skipped_p = newline_found_p = 1;
5355 }
5356 else
5357 {
5358 while (get_next_display_element (it)
5359 && !newline_found_p)
5360 {
5361 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
5362 set_iterator_to_next (it, 0);
5363 }
5364 }
5365 }
5366
5367 it->selective = old_selective;
5368 return newline_found_p;
5369 }
5370
5371
5372 /* Set IT's current position to the previous visible line start. Skip
5373 invisible text that is so either due to text properties or due to
5374 selective display. Caution: this does not change IT->current_x and
5375 IT->hpos. */
5376
5377 static void
5378 back_to_previous_visible_line_start (it)
5379 struct it *it;
5380 {
5381 while (IT_CHARPOS (*it) > BEGV)
5382 {
5383 back_to_previous_line_start (it);
5384
5385 if (IT_CHARPOS (*it) <= BEGV)
5386 break;
5387
5388 /* If selective > 0, then lines indented more than that values
5389 are invisible. */
5390 if (it->selective > 0
5391 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5392 (double) it->selective)) /* iftc */
5393 continue;
5394
5395 /* Check the newline before point for invisibility. */
5396 {
5397 Lisp_Object prop;
5398 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
5399 Qinvisible, it->window);
5400 if (TEXT_PROP_MEANS_INVISIBLE (prop))
5401 continue;
5402 }
5403
5404 if (IT_CHARPOS (*it) <= BEGV)
5405 break;
5406
5407 {
5408 struct it it2;
5409 int pos;
5410 EMACS_INT beg, end;
5411 Lisp_Object val, overlay;
5412
5413 /* If newline is part of a composition, continue from start of composition */
5414 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
5415 && beg < IT_CHARPOS (*it))
5416 goto replaced;
5417
5418 /* If newline is replaced by a display property, find start of overlay
5419 or interval and continue search from that point. */
5420 it2 = *it;
5421 pos = --IT_CHARPOS (it2);
5422 --IT_BYTEPOS (it2);
5423 it2.sp = 0;
5424 if (handle_display_prop (&it2) == HANDLED_RETURN
5425 && !NILP (val = get_char_property_and_overlay
5426 (make_number (pos), Qdisplay, Qnil, &overlay))
5427 && (OVERLAYP (overlay)
5428 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
5429 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
5430 goto replaced;
5431
5432 /* Newline is not replaced by anything -- so we are done. */
5433 break;
5434
5435 replaced:
5436 if (beg < BEGV)
5437 beg = BEGV;
5438 IT_CHARPOS (*it) = beg;
5439 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
5440 }
5441 }
5442
5443 it->continuation_lines_width = 0;
5444
5445 xassert (IT_CHARPOS (*it) >= BEGV);
5446 xassert (IT_CHARPOS (*it) == BEGV
5447 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5448 CHECK_IT (it);
5449 }
5450
5451
5452 /* Reseat iterator IT at the previous visible line start. Skip
5453 invisible text that is so either due to text properties or due to
5454 selective display. At the end, update IT's overlay information,
5455 face information etc. */
5456
5457 void
5458 reseat_at_previous_visible_line_start (it)
5459 struct it *it;
5460 {
5461 back_to_previous_visible_line_start (it);
5462 reseat (it, it->current.pos, 1);
5463 CHECK_IT (it);
5464 }
5465
5466
5467 /* Reseat iterator IT on the next visible line start in the current
5468 buffer. ON_NEWLINE_P non-zero means position IT on the newline
5469 preceding the line start. Skip over invisible text that is so
5470 because of selective display. Compute faces, overlays etc at the
5471 new position. Note that this function does not skip over text that
5472 is invisible because of text properties. */
5473
5474 static void
5475 reseat_at_next_visible_line_start (it, on_newline_p)
5476 struct it *it;
5477 int on_newline_p;
5478 {
5479 int newline_found_p, skipped_p = 0;
5480
5481 newline_found_p = forward_to_next_line_start (it, &skipped_p);
5482
5483 /* Skip over lines that are invisible because they are indented
5484 more than the value of IT->selective. */
5485 if (it->selective > 0)
5486 while (IT_CHARPOS (*it) < ZV
5487 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5488 (double) it->selective)) /* iftc */
5489 {
5490 xassert (IT_BYTEPOS (*it) == BEGV
5491 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5492 newline_found_p = forward_to_next_line_start (it, &skipped_p);
5493 }
5494
5495 /* Position on the newline if that's what's requested. */
5496 if (on_newline_p && newline_found_p)
5497 {
5498 if (STRINGP (it->string))
5499 {
5500 if (IT_STRING_CHARPOS (*it) > 0)
5501 {
5502 --IT_STRING_CHARPOS (*it);
5503 --IT_STRING_BYTEPOS (*it);
5504 }
5505 }
5506 else if (IT_CHARPOS (*it) > BEGV)
5507 {
5508 --IT_CHARPOS (*it);
5509 --IT_BYTEPOS (*it);
5510 reseat (it, it->current.pos, 0);
5511 }
5512 }
5513 else if (skipped_p)
5514 reseat (it, it->current.pos, 0);
5515
5516 CHECK_IT (it);
5517 }
5518
5519
5520 \f
5521 /***********************************************************************
5522 Changing an iterator's position
5523 ***********************************************************************/
5524
5525 /* Change IT's current position to POS in current_buffer. If FORCE_P
5526 is non-zero, always check for text properties at the new position.
5527 Otherwise, text properties are only looked up if POS >=
5528 IT->check_charpos of a property. */
5529
5530 static void
5531 reseat (it, pos, force_p)
5532 struct it *it;
5533 struct text_pos pos;
5534 int force_p;
5535 {
5536 int original_pos = IT_CHARPOS (*it);
5537
5538 reseat_1 (it, pos, 0);
5539
5540 /* Determine where to check text properties. Avoid doing it
5541 where possible because text property lookup is very expensive. */
5542 if (force_p
5543 || CHARPOS (pos) > it->stop_charpos
5544 || CHARPOS (pos) < original_pos)
5545 handle_stop (it);
5546
5547 CHECK_IT (it);
5548 }
5549
5550
5551 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
5552 IT->stop_pos to POS, also. */
5553
5554 static void
5555 reseat_1 (it, pos, set_stop_p)
5556 struct it *it;
5557 struct text_pos pos;
5558 int set_stop_p;
5559 {
5560 /* Don't call this function when scanning a C string. */
5561 xassert (it->s == NULL);
5562
5563 /* POS must be a reasonable value. */
5564 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
5565
5566 it->current.pos = it->position = pos;
5567 it->end_charpos = ZV;
5568 it->dpvec = NULL;
5569 it->current.dpvec_index = -1;
5570 it->current.overlay_string_index = -1;
5571 IT_STRING_CHARPOS (*it) = -1;
5572 IT_STRING_BYTEPOS (*it) = -1;
5573 it->string = Qnil;
5574 it->method = GET_FROM_BUFFER;
5575 it->object = it->w->buffer;
5576 it->area = TEXT_AREA;
5577 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
5578 it->sp = 0;
5579 it->string_from_display_prop_p = 0;
5580 it->face_before_selective_p = 0;
5581
5582 if (set_stop_p)
5583 it->stop_charpos = CHARPOS (pos);
5584 }
5585
5586
5587 /* Set up IT for displaying a string, starting at CHARPOS in window W.
5588 If S is non-null, it is a C string to iterate over. Otherwise,
5589 STRING gives a Lisp string to iterate over.
5590
5591 If PRECISION > 0, don't return more then PRECISION number of
5592 characters from the string.
5593
5594 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
5595 characters have been returned. FIELD_WIDTH < 0 means an infinite
5596 field width.
5597
5598 MULTIBYTE = 0 means disable processing of multibyte characters,
5599 MULTIBYTE > 0 means enable it,
5600 MULTIBYTE < 0 means use IT->multibyte_p.
5601
5602 IT must be initialized via a prior call to init_iterator before
5603 calling this function. */
5604
5605 static void
5606 reseat_to_string (it, s, string, charpos, precision, field_width, multibyte)
5607 struct it *it;
5608 unsigned char *s;
5609 Lisp_Object string;
5610 int charpos;
5611 int precision, field_width, multibyte;
5612 {
5613 /* No region in strings. */
5614 it->region_beg_charpos = it->region_end_charpos = -1;
5615
5616 /* No text property checks performed by default, but see below. */
5617 it->stop_charpos = -1;
5618
5619 /* Set iterator position and end position. */
5620 bzero (&it->current, sizeof it->current);
5621 it->current.overlay_string_index = -1;
5622 it->current.dpvec_index = -1;
5623 xassert (charpos >= 0);
5624
5625 /* If STRING is specified, use its multibyteness, otherwise use the
5626 setting of MULTIBYTE, if specified. */
5627 if (multibyte >= 0)
5628 it->multibyte_p = multibyte > 0;
5629
5630 if (s == NULL)
5631 {
5632 xassert (STRINGP (string));
5633 it->string = string;
5634 it->s = NULL;
5635 it->end_charpos = it->string_nchars = SCHARS (string);
5636 it->method = GET_FROM_STRING;
5637 it->current.string_pos = string_pos (charpos, string);
5638 }
5639 else
5640 {
5641 it->s = s;
5642 it->string = Qnil;
5643
5644 /* Note that we use IT->current.pos, not it->current.string_pos,
5645 for displaying C strings. */
5646 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
5647 if (it->multibyte_p)
5648 {
5649 it->current.pos = c_string_pos (charpos, s, 1);
5650 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
5651 }
5652 else
5653 {
5654 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
5655 it->end_charpos = it->string_nchars = strlen (s);
5656 }
5657
5658 it->method = GET_FROM_C_STRING;
5659 }
5660
5661 /* PRECISION > 0 means don't return more than PRECISION characters
5662 from the string. */
5663 if (precision > 0 && it->end_charpos - charpos > precision)
5664 it->end_charpos = it->string_nchars = charpos + precision;
5665
5666 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
5667 characters have been returned. FIELD_WIDTH == 0 means don't pad,
5668 FIELD_WIDTH < 0 means infinite field width. This is useful for
5669 padding with `-' at the end of a mode line. */
5670 if (field_width < 0)
5671 field_width = INFINITY;
5672 if (field_width > it->end_charpos - charpos)
5673 it->end_charpos = charpos + field_width;
5674
5675 /* Use the standard display table for displaying strings. */
5676 if (DISP_TABLE_P (Vstandard_display_table))
5677 it->dp = XCHAR_TABLE (Vstandard_display_table);
5678
5679 it->stop_charpos = charpos;
5680 CHECK_IT (it);
5681 }
5682
5683
5684 \f
5685 /***********************************************************************
5686 Iteration
5687 ***********************************************************************/
5688
5689 /* Map enum it_method value to corresponding next_element_from_* function. */
5690
5691 static int (* get_next_element[NUM_IT_METHODS]) P_ ((struct it *it)) =
5692 {
5693 next_element_from_buffer,
5694 next_element_from_display_vector,
5695 next_element_from_composition,
5696 next_element_from_string,
5697 next_element_from_c_string,
5698 next_element_from_image,
5699 next_element_from_stretch
5700 };
5701
5702
5703 /* Load IT's display element fields with information about the next
5704 display element from the current position of IT. Value is zero if
5705 end of buffer (or C string) is reached. */
5706
5707 static struct frame *last_escape_glyph_frame = NULL;
5708 static unsigned last_escape_glyph_face_id = (1 << FACE_ID_BITS);
5709 static int last_escape_glyph_merged_face_id = 0;
5710
5711 int
5712 get_next_display_element (it)
5713 struct it *it;
5714 {
5715 /* Non-zero means that we found a display element. Zero means that
5716 we hit the end of what we iterate over. Performance note: the
5717 function pointer `method' used here turns out to be faster than
5718 using a sequence of if-statements. */
5719 int success_p;
5720
5721 get_next:
5722 success_p = (*get_next_element[it->method]) (it);
5723
5724 if (it->what == IT_CHARACTER)
5725 {
5726 /* Map via display table or translate control characters.
5727 IT->c, IT->len etc. have been set to the next character by
5728 the function call above. If we have a display table, and it
5729 contains an entry for IT->c, translate it. Don't do this if
5730 IT->c itself comes from a display table, otherwise we could
5731 end up in an infinite recursion. (An alternative could be to
5732 count the recursion depth of this function and signal an
5733 error when a certain maximum depth is reached.) Is it worth
5734 it? */
5735 if (success_p && it->dpvec == NULL)
5736 {
5737 Lisp_Object dv;
5738
5739 if (it->dp
5740 && (dv = DISP_CHAR_VECTOR (it->dp, it->c),
5741 VECTORP (dv)))
5742 {
5743 struct Lisp_Vector *v = XVECTOR (dv);
5744
5745 /* Return the first character from the display table
5746 entry, if not empty. If empty, don't display the
5747 current character. */
5748 if (v->size)
5749 {
5750 it->dpvec_char_len = it->len;
5751 it->dpvec = v->contents;
5752 it->dpend = v->contents + v->size;
5753 it->current.dpvec_index = 0;
5754 it->dpvec_face_id = -1;
5755 it->saved_face_id = it->face_id;
5756 it->method = GET_FROM_DISPLAY_VECTOR;
5757 it->ellipsis_p = 0;
5758 }
5759 else
5760 {
5761 set_iterator_to_next (it, 0);
5762 }
5763 goto get_next;
5764 }
5765
5766 /* Translate control characters into `\003' or `^C' form.
5767 Control characters coming from a display table entry are
5768 currently not translated because we use IT->dpvec to hold
5769 the translation. This could easily be changed but I
5770 don't believe that it is worth doing.
5771
5772 If it->multibyte_p is nonzero, non-printable non-ASCII
5773 characters are also translated to octal form.
5774
5775 If it->multibyte_p is zero, eight-bit characters that
5776 don't have corresponding multibyte char code are also
5777 translated to octal form. */
5778 else if ((it->c < ' '
5779 ? (it->area != TEXT_AREA
5780 /* In mode line, treat \n, \t like other crl chars. */
5781 || (it->c != '\t'
5782 && it->glyph_row && it->glyph_row->mode_line_p)
5783 || (it->c != '\n' && it->c != '\t'))
5784 : (it->multibyte_p
5785 ? (!CHAR_PRINTABLE_P (it->c)
5786 || (!NILP (Vnobreak_char_display)
5787 && (it->c == 0xA0 /* NO-BREAK SPACE */
5788 || it->c == 0xAD /* SOFT HYPHEN */)))
5789 : (it->c >= 127
5790 && (! unibyte_display_via_language_environment
5791 || (UNIBYTE_CHAR_HAS_MULTIBYTE_P (it->c)))))))
5792 {
5793 /* IT->c is a control character which must be displayed
5794 either as '\003' or as `^C' where the '\\' and '^'
5795 can be defined in the display table. Fill
5796 IT->ctl_chars with glyphs for what we have to
5797 display. Then, set IT->dpvec to these glyphs. */
5798 GLYPH g;
5799 int ctl_len;
5800 int face_id, lface_id = 0 ;
5801 GLYPH escape_glyph;
5802
5803 /* Handle control characters with ^. */
5804
5805 if (it->c < 128 && it->ctl_arrow_p)
5806 {
5807 g = '^'; /* default glyph for Control */
5808 /* Set IT->ctl_chars[0] to the glyph for `^'. */
5809 if (it->dp
5810 && INTEGERP (DISP_CTRL_GLYPH (it->dp))
5811 && GLYPH_CHAR_VALID_P (XINT (DISP_CTRL_GLYPH (it->dp))))
5812 {
5813 g = XINT (DISP_CTRL_GLYPH (it->dp));
5814 lface_id = FAST_GLYPH_FACE (g);
5815 }
5816 if (lface_id)
5817 {
5818 g = FAST_GLYPH_CHAR (g);
5819 face_id = merge_faces (it->f, Qt, lface_id,
5820 it->face_id);
5821 }
5822 else if (it->f == last_escape_glyph_frame
5823 && it->face_id == last_escape_glyph_face_id)
5824 {
5825 face_id = last_escape_glyph_merged_face_id;
5826 }
5827 else
5828 {
5829 /* Merge the escape-glyph face into the current face. */
5830 face_id = merge_faces (it->f, Qescape_glyph, 0,
5831 it->face_id);
5832 last_escape_glyph_frame = it->f;
5833 last_escape_glyph_face_id = it->face_id;
5834 last_escape_glyph_merged_face_id = face_id;
5835 }
5836
5837 XSETINT (it->ctl_chars[0], g);
5838 g = it->c ^ 0100;
5839 XSETINT (it->ctl_chars[1], g);
5840 ctl_len = 2;
5841 goto display_control;
5842 }
5843
5844 /* Handle non-break space in the mode where it only gets
5845 highlighting. */
5846
5847 if (EQ (Vnobreak_char_display, Qt)
5848 && it->c == 0xA0)
5849 {
5850 /* Merge the no-break-space face into the current face. */
5851 face_id = merge_faces (it->f, Qnobreak_space, 0,
5852 it->face_id);
5853
5854 g = it->c = ' ';
5855 XSETINT (it->ctl_chars[0], g);
5856 ctl_len = 1;
5857 goto display_control;
5858 }
5859
5860 /* Handle sequences that start with the "escape glyph". */
5861
5862 /* the default escape glyph is \. */
5863 escape_glyph = '\\';
5864
5865 if (it->dp
5866 && INTEGERP (DISP_ESCAPE_GLYPH (it->dp))
5867 && GLYPH_CHAR_VALID_P (XFASTINT (DISP_ESCAPE_GLYPH (it->dp))))
5868 {
5869 escape_glyph = XFASTINT (DISP_ESCAPE_GLYPH (it->dp));
5870 lface_id = FAST_GLYPH_FACE (escape_glyph);
5871 }
5872 if (lface_id)
5873 {
5874 /* The display table specified a face.
5875 Merge it into face_id and also into escape_glyph. */
5876 escape_glyph = FAST_GLYPH_CHAR (escape_glyph);
5877 face_id = merge_faces (it->f, Qt, lface_id,
5878 it->face_id);
5879 }
5880 else if (it->f == last_escape_glyph_frame
5881 && it->face_id == last_escape_glyph_face_id)
5882 {
5883 face_id = last_escape_glyph_merged_face_id;
5884 }
5885 else
5886 {
5887 /* Merge the escape-glyph face into the current face. */
5888 face_id = merge_faces (it->f, Qescape_glyph, 0,
5889 it->face_id);
5890 last_escape_glyph_frame = it->f;
5891 last_escape_glyph_face_id = it->face_id;
5892 last_escape_glyph_merged_face_id = face_id;
5893 }
5894
5895 /* Handle soft hyphens in the mode where they only get
5896 highlighting. */
5897
5898 if (EQ (Vnobreak_char_display, Qt)
5899 && it->c == 0xAD)
5900 {
5901 g = it->c = '-';
5902 XSETINT (it->ctl_chars[0], g);
5903 ctl_len = 1;
5904 goto display_control;
5905 }
5906
5907 /* Handle non-break space and soft hyphen
5908 with the escape glyph. */
5909
5910 if (it->c == 0xA0 || it->c == 0xAD)
5911 {
5912 XSETINT (it->ctl_chars[0], escape_glyph);
5913 g = it->c = (it->c == 0xA0 ? ' ' : '-');
5914 XSETINT (it->ctl_chars[1], g);
5915 ctl_len = 2;
5916 goto display_control;
5917 }
5918
5919 {
5920 unsigned char str[MAX_MULTIBYTE_LENGTH];
5921 int len;
5922 int i;
5923
5924 /* Set IT->ctl_chars[0] to the glyph for `\\'. */
5925 if (CHAR_BYTE8_P (it->c))
5926 {
5927 str[0] = CHAR_TO_BYTE8 (it->c);
5928 len = 1;
5929 }
5930 else if (it->c < 256)
5931 {
5932 str[0] = it->c;
5933 len = 1;
5934 }
5935 else
5936 {
5937 /* It's an invalid character, which shouldn't
5938 happen actually, but due to bugs it may
5939 happen. Let's print the char as is, there's
5940 not much meaningful we can do with it. */
5941 str[0] = it->c;
5942 str[1] = it->c >> 8;
5943 str[2] = it->c >> 16;
5944 str[3] = it->c >> 24;
5945 len = 4;
5946 }
5947
5948 for (i = 0; i < len; i++)
5949 {
5950 XSETINT (it->ctl_chars[i * 4], escape_glyph);
5951 /* Insert three more glyphs into IT->ctl_chars for
5952 the octal display of the character. */
5953 g = ((str[i] >> 6) & 7) + '0';
5954 XSETINT (it->ctl_chars[i * 4 + 1], g);
5955 g = ((str[i] >> 3) & 7) + '0';
5956 XSETINT (it->ctl_chars[i * 4 + 2], g);
5957 g = (str[i] & 7) + '0';
5958 XSETINT (it->ctl_chars[i * 4 + 3], g);
5959 }
5960 ctl_len = len * 4;
5961 }
5962
5963 display_control:
5964 /* Set up IT->dpvec and return first character from it. */
5965 it->dpvec_char_len = it->len;
5966 it->dpvec = it->ctl_chars;
5967 it->dpend = it->dpvec + ctl_len;
5968 it->current.dpvec_index = 0;
5969 it->dpvec_face_id = face_id;
5970 it->saved_face_id = it->face_id;
5971 it->method = GET_FROM_DISPLAY_VECTOR;
5972 it->ellipsis_p = 0;
5973 goto get_next;
5974 }
5975 }
5976 }
5977
5978 /* Adjust face id for a multibyte character. There are no multibyte
5979 character in unibyte text. */
5980 if ((it->what == IT_CHARACTER || it->what == IT_COMPOSITION)
5981 && it->multibyte_p
5982 && success_p
5983 && FRAME_WINDOW_P (it->f))
5984 {
5985 struct face *face = FACE_FROM_ID (it->f, it->face_id);
5986 int pos = (it->s ? -1
5987 : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
5988 : IT_CHARPOS (*it));
5989
5990 it->face_id = FACE_FOR_CHAR (it->f, face, it->c, pos, it->string);
5991 }
5992
5993 /* Is this character the last one of a run of characters with
5994 box? If yes, set IT->end_of_box_run_p to 1. */
5995 if (it->face_box_p
5996 && it->s == NULL)
5997 {
5998 int face_id;
5999 struct face *face;
6000
6001 it->end_of_box_run_p
6002 = ((face_id = face_after_it_pos (it),
6003 face_id != it->face_id)
6004 && (face = FACE_FROM_ID (it->f, face_id),
6005 face->box == FACE_NO_BOX));
6006 }
6007
6008 /* Value is 0 if end of buffer or string reached. */
6009 return success_p;
6010 }
6011
6012
6013 /* Move IT to the next display element.
6014
6015 RESEAT_P non-zero means if called on a newline in buffer text,
6016 skip to the next visible line start.
6017
6018 Functions get_next_display_element and set_iterator_to_next are
6019 separate because I find this arrangement easier to handle than a
6020 get_next_display_element function that also increments IT's
6021 position. The way it is we can first look at an iterator's current
6022 display element, decide whether it fits on a line, and if it does,
6023 increment the iterator position. The other way around we probably
6024 would either need a flag indicating whether the iterator has to be
6025 incremented the next time, or we would have to implement a
6026 decrement position function which would not be easy to write. */
6027
6028 void
6029 set_iterator_to_next (it, reseat_p)
6030 struct it *it;
6031 int reseat_p;
6032 {
6033 /* Reset flags indicating start and end of a sequence of characters
6034 with box. Reset them at the start of this function because
6035 moving the iterator to a new position might set them. */
6036 it->start_of_box_run_p = it->end_of_box_run_p = 0;
6037
6038 switch (it->method)
6039 {
6040 case GET_FROM_BUFFER:
6041 /* The current display element of IT is a character from
6042 current_buffer. Advance in the buffer, and maybe skip over
6043 invisible lines that are so because of selective display. */
6044 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
6045 reseat_at_next_visible_line_start (it, 0);
6046 else
6047 {
6048 xassert (it->len != 0);
6049 IT_BYTEPOS (*it) += it->len;
6050 IT_CHARPOS (*it) += 1;
6051 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
6052 }
6053 break;
6054
6055 case GET_FROM_COMPOSITION:
6056 xassert (it->cmp_id >= 0 && it->cmp_id < n_compositions);
6057 xassert (it->sp > 0);
6058 pop_it (it);
6059 if (it->method == GET_FROM_STRING)
6060 {
6061 IT_STRING_BYTEPOS (*it) += it->len;
6062 IT_STRING_CHARPOS (*it) += it->cmp_len;
6063 goto consider_string_end;
6064 }
6065 else if (it->method == GET_FROM_BUFFER)
6066 {
6067 IT_BYTEPOS (*it) += it->len;
6068 IT_CHARPOS (*it) += it->cmp_len;
6069 }
6070 break;
6071
6072 case GET_FROM_C_STRING:
6073 /* Current display element of IT is from a C string. */
6074 IT_BYTEPOS (*it) += it->len;
6075 IT_CHARPOS (*it) += 1;
6076 break;
6077
6078 case GET_FROM_DISPLAY_VECTOR:
6079 /* Current display element of IT is from a display table entry.
6080 Advance in the display table definition. Reset it to null if
6081 end reached, and continue with characters from buffers/
6082 strings. */
6083 ++it->current.dpvec_index;
6084
6085 /* Restore face of the iterator to what they were before the
6086 display vector entry (these entries may contain faces). */
6087 it->face_id = it->saved_face_id;
6088
6089 if (it->dpvec + it->current.dpvec_index == it->dpend)
6090 {
6091 int recheck_faces = it->ellipsis_p;
6092
6093 if (it->s)
6094 it->method = GET_FROM_C_STRING;
6095 else if (STRINGP (it->string))
6096 it->method = GET_FROM_STRING;
6097 else
6098 {
6099 it->method = GET_FROM_BUFFER;
6100 it->object = it->w->buffer;
6101 }
6102
6103 it->dpvec = NULL;
6104 it->current.dpvec_index = -1;
6105
6106 /* Skip over characters which were displayed via IT->dpvec. */
6107 if (it->dpvec_char_len < 0)
6108 reseat_at_next_visible_line_start (it, 1);
6109 else if (it->dpvec_char_len > 0)
6110 {
6111 if (it->method == GET_FROM_STRING
6112 && it->n_overlay_strings > 0)
6113 it->ignore_overlay_strings_at_pos_p = 1;
6114 it->len = it->dpvec_char_len;
6115 set_iterator_to_next (it, reseat_p);
6116 }
6117
6118 /* Maybe recheck faces after display vector */
6119 if (recheck_faces)
6120 it->stop_charpos = IT_CHARPOS (*it);
6121 }
6122 break;
6123
6124 case GET_FROM_STRING:
6125 /* Current display element is a character from a Lisp string. */
6126 xassert (it->s == NULL && STRINGP (it->string));
6127 IT_STRING_BYTEPOS (*it) += it->len;
6128 IT_STRING_CHARPOS (*it) += 1;
6129
6130 consider_string_end:
6131
6132 if (it->current.overlay_string_index >= 0)
6133 {
6134 /* IT->string is an overlay string. Advance to the
6135 next, if there is one. */
6136 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
6137 next_overlay_string (it);
6138 }
6139 else
6140 {
6141 /* IT->string is not an overlay string. If we reached
6142 its end, and there is something on IT->stack, proceed
6143 with what is on the stack. This can be either another
6144 string, this time an overlay string, or a buffer. */
6145 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
6146 && it->sp > 0)
6147 {
6148 pop_it (it);
6149 if (it->method == GET_FROM_STRING)
6150 goto consider_string_end;
6151 }
6152 }
6153 break;
6154
6155 case GET_FROM_IMAGE:
6156 case GET_FROM_STRETCH:
6157 /* The position etc with which we have to proceed are on
6158 the stack. The position may be at the end of a string,
6159 if the `display' property takes up the whole string. */
6160 xassert (it->sp > 0);
6161 pop_it (it);
6162 if (it->method == GET_FROM_STRING)
6163 goto consider_string_end;
6164 break;
6165
6166 default:
6167 /* There are no other methods defined, so this should be a bug. */
6168 abort ();
6169 }
6170
6171 xassert (it->method != GET_FROM_STRING
6172 || (STRINGP (it->string)
6173 && IT_STRING_CHARPOS (*it) >= 0));
6174 }
6175
6176 /* Load IT's display element fields with information about the next
6177 display element which comes from a display table entry or from the
6178 result of translating a control character to one of the forms `^C'
6179 or `\003'.
6180
6181 IT->dpvec holds the glyphs to return as characters.
6182 IT->saved_face_id holds the face id before the display vector--
6183 it is restored into IT->face_idin set_iterator_to_next. */
6184
6185 static int
6186 next_element_from_display_vector (it)
6187 struct it *it;
6188 {
6189 /* Precondition. */
6190 xassert (it->dpvec && it->current.dpvec_index >= 0);
6191
6192 it->face_id = it->saved_face_id;
6193
6194 if (INTEGERP (*it->dpvec)
6195 && GLYPH_CHAR_VALID_P (XFASTINT (*it->dpvec)))
6196 {
6197 GLYPH g;
6198
6199 g = XFASTINT (it->dpvec[it->current.dpvec_index]);
6200 it->c = FAST_GLYPH_CHAR (g);
6201 it->len = CHAR_BYTES (it->c);
6202
6203 /* The entry may contain a face id to use. Such a face id is
6204 the id of a Lisp face, not a realized face. A face id of
6205 zero means no face is specified. */
6206 if (it->dpvec_face_id >= 0)
6207 it->face_id = it->dpvec_face_id;
6208 else
6209 {
6210 int lface_id = FAST_GLYPH_FACE (g);
6211 if (lface_id > 0)
6212 it->face_id = merge_faces (it->f, Qt, lface_id,
6213 it->saved_face_id);
6214 }
6215 }
6216 else
6217 /* Display table entry is invalid. Return a space. */
6218 it->c = ' ', it->len = 1;
6219
6220 /* Don't change position and object of the iterator here. They are
6221 still the values of the character that had this display table
6222 entry or was translated, and that's what we want. */
6223 it->what = IT_CHARACTER;
6224 return 1;
6225 }
6226
6227
6228 /* Load IT with the next display element from Lisp string IT->string.
6229 IT->current.string_pos is the current position within the string.
6230 If IT->current.overlay_string_index >= 0, the Lisp string is an
6231 overlay string. */
6232
6233 static int
6234 next_element_from_string (it)
6235 struct it *it;
6236 {
6237 struct text_pos position;
6238
6239 xassert (STRINGP (it->string));
6240 xassert (IT_STRING_CHARPOS (*it) >= 0);
6241 position = it->current.string_pos;
6242
6243 /* Time to check for invisible text? */
6244 if (IT_STRING_CHARPOS (*it) < it->end_charpos
6245 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
6246 {
6247 handle_stop (it);
6248
6249 /* Since a handler may have changed IT->method, we must
6250 recurse here. */
6251 return get_next_display_element (it);
6252 }
6253
6254 if (it->current.overlay_string_index >= 0)
6255 {
6256 /* Get the next character from an overlay string. In overlay
6257 strings, There is no field width or padding with spaces to
6258 do. */
6259 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
6260 {
6261 it->what = IT_EOB;
6262 return 0;
6263 }
6264 else if (STRING_MULTIBYTE (it->string))
6265 {
6266 int remaining = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
6267 const unsigned char *s = (SDATA (it->string)
6268 + IT_STRING_BYTEPOS (*it));
6269 it->c = string_char_and_length (s, remaining, &it->len);
6270 }
6271 else
6272 {
6273 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
6274 it->len = 1;
6275 }
6276 }
6277 else
6278 {
6279 /* Get the next character from a Lisp string that is not an
6280 overlay string. Such strings come from the mode line, for
6281 example. We may have to pad with spaces, or truncate the
6282 string. See also next_element_from_c_string. */
6283 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
6284 {
6285 it->what = IT_EOB;
6286 return 0;
6287 }
6288 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
6289 {
6290 /* Pad with spaces. */
6291 it->c = ' ', it->len = 1;
6292 CHARPOS (position) = BYTEPOS (position) = -1;
6293 }
6294 else if (STRING_MULTIBYTE (it->string))
6295 {
6296 int maxlen = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
6297 const unsigned char *s = (SDATA (it->string)
6298 + IT_STRING_BYTEPOS (*it));
6299 it->c = string_char_and_length (s, maxlen, &it->len);
6300 }
6301 else
6302 {
6303 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
6304 it->len = 1;
6305 }
6306 }
6307
6308 /* Record what we have and where it came from. */
6309 it->what = IT_CHARACTER;
6310 it->object = it->string;
6311 it->position = position;
6312 return 1;
6313 }
6314
6315
6316 /* Load IT with next display element from C string IT->s.
6317 IT->string_nchars is the maximum number of characters to return
6318 from the string. IT->end_charpos may be greater than
6319 IT->string_nchars when this function is called, in which case we
6320 may have to return padding spaces. Value is zero if end of string
6321 reached, including padding spaces. */
6322
6323 static int
6324 next_element_from_c_string (it)
6325 struct it *it;
6326 {
6327 int success_p = 1;
6328
6329 xassert (it->s);
6330 it->what = IT_CHARACTER;
6331 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
6332 it->object = Qnil;
6333
6334 /* IT's position can be greater IT->string_nchars in case a field
6335 width or precision has been specified when the iterator was
6336 initialized. */
6337 if (IT_CHARPOS (*it) >= it->end_charpos)
6338 {
6339 /* End of the game. */
6340 it->what = IT_EOB;
6341 success_p = 0;
6342 }
6343 else if (IT_CHARPOS (*it) >= it->string_nchars)
6344 {
6345 /* Pad with spaces. */
6346 it->c = ' ', it->len = 1;
6347 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
6348 }
6349 else if (it->multibyte_p)
6350 {
6351 /* Implementation note: The calls to strlen apparently aren't a
6352 performance problem because there is no noticeable performance
6353 difference between Emacs running in unibyte or multibyte mode. */
6354 int maxlen = strlen (it->s) - IT_BYTEPOS (*it);
6355 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it),
6356 maxlen, &it->len);
6357 }
6358 else
6359 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
6360
6361 return success_p;
6362 }
6363
6364
6365 /* Set up IT to return characters from an ellipsis, if appropriate.
6366 The definition of the ellipsis glyphs may come from a display table
6367 entry. This function Fills IT with the first glyph from the
6368 ellipsis if an ellipsis is to be displayed. */
6369
6370 static int
6371 next_element_from_ellipsis (it)
6372 struct it *it;
6373 {
6374 if (it->selective_display_ellipsis_p)
6375 setup_for_ellipsis (it, it->len);
6376 else
6377 {
6378 /* The face at the current position may be different from the
6379 face we find after the invisible text. Remember what it
6380 was in IT->saved_face_id, and signal that it's there by
6381 setting face_before_selective_p. */
6382 it->saved_face_id = it->face_id;
6383 it->method = GET_FROM_BUFFER;
6384 it->object = it->w->buffer;
6385 reseat_at_next_visible_line_start (it, 1);
6386 it->face_before_selective_p = 1;
6387 }
6388
6389 return get_next_display_element (it);
6390 }
6391
6392
6393 /* Deliver an image display element. The iterator IT is already
6394 filled with image information (done in handle_display_prop). Value
6395 is always 1. */
6396
6397
6398 static int
6399 next_element_from_image (it)
6400 struct it *it;
6401 {
6402 it->what = IT_IMAGE;
6403 return 1;
6404 }
6405
6406
6407 /* Fill iterator IT with next display element from a stretch glyph
6408 property. IT->object is the value of the text property. Value is
6409 always 1. */
6410
6411 static int
6412 next_element_from_stretch (it)
6413 struct it *it;
6414 {
6415 it->what = IT_STRETCH;
6416 return 1;
6417 }
6418
6419
6420 /* Load IT with the next display element from current_buffer. Value
6421 is zero if end of buffer reached. IT->stop_charpos is the next
6422 position at which to stop and check for text properties or buffer
6423 end. */
6424
6425 static int
6426 next_element_from_buffer (it)
6427 struct it *it;
6428 {
6429 int success_p = 1;
6430
6431 /* Check this assumption, otherwise, we would never enter the
6432 if-statement, below. */
6433 xassert (IT_CHARPOS (*it) >= BEGV
6434 && IT_CHARPOS (*it) <= it->stop_charpos);
6435
6436 if (IT_CHARPOS (*it) >= it->stop_charpos)
6437 {
6438 if (IT_CHARPOS (*it) >= it->end_charpos)
6439 {
6440 int overlay_strings_follow_p;
6441
6442 /* End of the game, except when overlay strings follow that
6443 haven't been returned yet. */
6444 if (it->overlay_strings_at_end_processed_p)
6445 overlay_strings_follow_p = 0;
6446 else
6447 {
6448 it->overlay_strings_at_end_processed_p = 1;
6449 overlay_strings_follow_p = get_overlay_strings (it, 0);
6450 }
6451
6452 if (overlay_strings_follow_p)
6453 success_p = get_next_display_element (it);
6454 else
6455 {
6456 it->what = IT_EOB;
6457 it->position = it->current.pos;
6458 success_p = 0;
6459 }
6460 }
6461 else
6462 {
6463 handle_stop (it);
6464 return get_next_display_element (it);
6465 }
6466 }
6467 else
6468 {
6469 /* No face changes, overlays etc. in sight, so just return a
6470 character from current_buffer. */
6471 unsigned char *p;
6472
6473 /* Maybe run the redisplay end trigger hook. Performance note:
6474 This doesn't seem to cost measurable time. */
6475 if (it->redisplay_end_trigger_charpos
6476 && it->glyph_row
6477 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
6478 run_redisplay_end_trigger_hook (it);
6479
6480 /* Get the next character, maybe multibyte. */
6481 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
6482 if (it->multibyte_p && !ASCII_BYTE_P (*p))
6483 {
6484 int maxlen = ((IT_BYTEPOS (*it) >= GPT_BYTE ? ZV_BYTE : GPT_BYTE)
6485 - IT_BYTEPOS (*it));
6486 it->c = string_char_and_length (p, maxlen, &it->len);
6487 }
6488 else
6489 it->c = *p, it->len = 1;
6490
6491 /* Record what we have and where it came from. */
6492 it->what = IT_CHARACTER;
6493 it->object = it->w->buffer;
6494 it->position = it->current.pos;
6495
6496 /* Normally we return the character found above, except when we
6497 really want to return an ellipsis for selective display. */
6498 if (it->selective)
6499 {
6500 if (it->c == '\n')
6501 {
6502 /* A value of selective > 0 means hide lines indented more
6503 than that number of columns. */
6504 if (it->selective > 0
6505 && IT_CHARPOS (*it) + 1 < ZV
6506 && indented_beyond_p (IT_CHARPOS (*it) + 1,
6507 IT_BYTEPOS (*it) + 1,
6508 (double) it->selective)) /* iftc */
6509 {
6510 success_p = next_element_from_ellipsis (it);
6511 it->dpvec_char_len = -1;
6512 }
6513 }
6514 else if (it->c == '\r' && it->selective == -1)
6515 {
6516 /* A value of selective == -1 means that everything from the
6517 CR to the end of the line is invisible, with maybe an
6518 ellipsis displayed for it. */
6519 success_p = next_element_from_ellipsis (it);
6520 it->dpvec_char_len = -1;
6521 }
6522 }
6523 }
6524
6525 /* Value is zero if end of buffer reached. */
6526 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
6527 return success_p;
6528 }
6529
6530
6531 /* Run the redisplay end trigger hook for IT. */
6532
6533 static void
6534 run_redisplay_end_trigger_hook (it)
6535 struct it *it;
6536 {
6537 Lisp_Object args[3];
6538
6539 /* IT->glyph_row should be non-null, i.e. we should be actually
6540 displaying something, or otherwise we should not run the hook. */
6541 xassert (it->glyph_row);
6542
6543 /* Set up hook arguments. */
6544 args[0] = Qredisplay_end_trigger_functions;
6545 args[1] = it->window;
6546 XSETINT (args[2], it->redisplay_end_trigger_charpos);
6547 it->redisplay_end_trigger_charpos = 0;
6548
6549 /* Since we are *trying* to run these functions, don't try to run
6550 them again, even if they get an error. */
6551 it->w->redisplay_end_trigger = Qnil;
6552 Frun_hook_with_args (3, args);
6553
6554 /* Notice if it changed the face of the character we are on. */
6555 handle_face_prop (it);
6556 }
6557
6558
6559 /* Deliver a composition display element. The iterator IT is already
6560 filled with composition information (done in
6561 handle_composition_prop). Value is always 1. */
6562
6563 static int
6564 next_element_from_composition (it)
6565 struct it *it;
6566 {
6567 it->what = IT_COMPOSITION;
6568 it->position = (STRINGP (it->string)
6569 ? it->current.string_pos
6570 : it->current.pos);
6571 if (STRINGP (it->string))
6572 it->object = it->string;
6573 else
6574 it->object = it->w->buffer;
6575 return 1;
6576 }
6577
6578
6579 \f
6580 /***********************************************************************
6581 Moving an iterator without producing glyphs
6582 ***********************************************************************/
6583
6584 /* Check if iterator is at a position corresponding to a valid buffer
6585 position after some move_it_ call. */
6586
6587 #define IT_POS_VALID_AFTER_MOVE_P(it) \
6588 ((it)->method == GET_FROM_STRING \
6589 ? IT_STRING_CHARPOS (*it) == 0 \
6590 : 1)
6591
6592
6593 /* Move iterator IT to a specified buffer or X position within one
6594 line on the display without producing glyphs.
6595
6596 OP should be a bit mask including some or all of these bits:
6597 MOVE_TO_X: Stop on reaching x-position TO_X.
6598 MOVE_TO_POS: Stop on reaching buffer or string position TO_CHARPOS.
6599 Regardless of OP's value, stop in reaching the end of the display line.
6600
6601 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
6602 This means, in particular, that TO_X includes window's horizontal
6603 scroll amount.
6604
6605 The return value has several possible values that
6606 say what condition caused the scan to stop:
6607
6608 MOVE_POS_MATCH_OR_ZV
6609 - when TO_POS or ZV was reached.
6610
6611 MOVE_X_REACHED
6612 -when TO_X was reached before TO_POS or ZV were reached.
6613
6614 MOVE_LINE_CONTINUED
6615 - when we reached the end of the display area and the line must
6616 be continued.
6617
6618 MOVE_LINE_TRUNCATED
6619 - when we reached the end of the display area and the line is
6620 truncated.
6621
6622 MOVE_NEWLINE_OR_CR
6623 - when we stopped at a line end, i.e. a newline or a CR and selective
6624 display is on. */
6625
6626 static enum move_it_result
6627 move_it_in_display_line_to (it, to_charpos, to_x, op)
6628 struct it *it;
6629 int to_charpos, to_x, op;
6630 {
6631 enum move_it_result result = MOVE_UNDEFINED;
6632 struct glyph_row *saved_glyph_row;
6633
6634 /* Don't produce glyphs in produce_glyphs. */
6635 saved_glyph_row = it->glyph_row;
6636 it->glyph_row = NULL;
6637
6638 #define BUFFER_POS_REACHED_P() \
6639 ((op & MOVE_TO_POS) != 0 \
6640 && BUFFERP (it->object) \
6641 && IT_CHARPOS (*it) >= to_charpos \
6642 && (it->method == GET_FROM_BUFFER \
6643 || (it->method == GET_FROM_DISPLAY_VECTOR \
6644 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
6645
6646
6647 while (1)
6648 {
6649 int x, i, ascent = 0, descent = 0;
6650
6651 /* Stop if we move beyond TO_CHARPOS (after an image or stretch glyph). */
6652 if ((op & MOVE_TO_POS) != 0
6653 && BUFFERP (it->object)
6654 && it->method == GET_FROM_BUFFER
6655 && IT_CHARPOS (*it) > to_charpos)
6656 {
6657 result = MOVE_POS_MATCH_OR_ZV;
6658 break;
6659 }
6660
6661 /* Stop when ZV reached.
6662 We used to stop here when TO_CHARPOS reached as well, but that is
6663 too soon if this glyph does not fit on this line. So we handle it
6664 explicitly below. */
6665 if (!get_next_display_element (it)
6666 || (it->truncate_lines_p
6667 && BUFFER_POS_REACHED_P ()))
6668 {
6669 result = MOVE_POS_MATCH_OR_ZV;
6670 break;
6671 }
6672
6673 /* The call to produce_glyphs will get the metrics of the
6674 display element IT is loaded with. We record in x the
6675 x-position before this display element in case it does not
6676 fit on the line. */
6677 x = it->current_x;
6678
6679 /* Remember the line height so far in case the next element doesn't
6680 fit on the line. */
6681 if (!it->truncate_lines_p)
6682 {
6683 ascent = it->max_ascent;
6684 descent = it->max_descent;
6685 }
6686
6687 PRODUCE_GLYPHS (it);
6688
6689 if (it->area != TEXT_AREA)
6690 {
6691 set_iterator_to_next (it, 1);
6692 continue;
6693 }
6694
6695 /* The number of glyphs we get back in IT->nglyphs will normally
6696 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
6697 character on a terminal frame, or (iii) a line end. For the
6698 second case, IT->nglyphs - 1 padding glyphs will be present
6699 (on X frames, there is only one glyph produced for a
6700 composite character.
6701
6702 The behavior implemented below means, for continuation lines,
6703 that as many spaces of a TAB as fit on the current line are
6704 displayed there. For terminal frames, as many glyphs of a
6705 multi-glyph character are displayed in the current line, too.
6706 This is what the old redisplay code did, and we keep it that
6707 way. Under X, the whole shape of a complex character must
6708 fit on the line or it will be completely displayed in the
6709 next line.
6710
6711 Note that both for tabs and padding glyphs, all glyphs have
6712 the same width. */
6713 if (it->nglyphs)
6714 {
6715 /* More than one glyph or glyph doesn't fit on line. All
6716 glyphs have the same width. */
6717 int single_glyph_width = it->pixel_width / it->nglyphs;
6718 int new_x;
6719 int x_before_this_char = x;
6720 int hpos_before_this_char = it->hpos;
6721
6722 for (i = 0; i < it->nglyphs; ++i, x = new_x)
6723 {
6724 new_x = x + single_glyph_width;
6725
6726 /* We want to leave anything reaching TO_X to the caller. */
6727 if ((op & MOVE_TO_X) && new_x > to_x)
6728 {
6729 if (BUFFER_POS_REACHED_P ())
6730 goto buffer_pos_reached;
6731 it->current_x = x;
6732 result = MOVE_X_REACHED;
6733 break;
6734 }
6735 else if (/* Lines are continued. */
6736 !it->truncate_lines_p
6737 && (/* And glyph doesn't fit on the line. */
6738 new_x > it->last_visible_x
6739 /* Or it fits exactly and we're on a window
6740 system frame. */
6741 || (new_x == it->last_visible_x
6742 && FRAME_WINDOW_P (it->f))))
6743 {
6744 if (/* IT->hpos == 0 means the very first glyph
6745 doesn't fit on the line, e.g. a wide image. */
6746 it->hpos == 0
6747 || (new_x == it->last_visible_x
6748 && FRAME_WINDOW_P (it->f)))
6749 {
6750 ++it->hpos;
6751 it->current_x = new_x;
6752
6753 /* The character's last glyph just barely fits
6754 in this row. */
6755 if (i == it->nglyphs - 1)
6756 {
6757 /* If this is the destination position,
6758 return a position *before* it in this row,
6759 now that we know it fits in this row. */
6760 if (BUFFER_POS_REACHED_P ())
6761 {
6762 it->hpos = hpos_before_this_char;
6763 it->current_x = x_before_this_char;
6764 result = MOVE_POS_MATCH_OR_ZV;
6765 break;
6766 }
6767
6768 set_iterator_to_next (it, 1);
6769 #ifdef HAVE_WINDOW_SYSTEM
6770 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
6771 {
6772 if (!get_next_display_element (it))
6773 {
6774 result = MOVE_POS_MATCH_OR_ZV;
6775 break;
6776 }
6777 if (BUFFER_POS_REACHED_P ())
6778 {
6779 if (ITERATOR_AT_END_OF_LINE_P (it))
6780 result = MOVE_POS_MATCH_OR_ZV;
6781 else
6782 result = MOVE_LINE_CONTINUED;
6783 break;
6784 }
6785 if (ITERATOR_AT_END_OF_LINE_P (it))
6786 {
6787 result = MOVE_NEWLINE_OR_CR;
6788 break;
6789 }
6790 }
6791 #endif /* HAVE_WINDOW_SYSTEM */
6792 }
6793 }
6794 else
6795 {
6796 it->current_x = x;
6797 it->max_ascent = ascent;
6798 it->max_descent = descent;
6799 }
6800
6801 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
6802 IT_CHARPOS (*it)));
6803 result = MOVE_LINE_CONTINUED;
6804 break;
6805 }
6806 else if (BUFFER_POS_REACHED_P ())
6807 goto buffer_pos_reached;
6808 else if (new_x > it->first_visible_x)
6809 {
6810 /* Glyph is visible. Increment number of glyphs that
6811 would be displayed. */
6812 ++it->hpos;
6813 }
6814 else
6815 {
6816 /* Glyph is completely off the left margin of the display
6817 area. Nothing to do. */
6818 }
6819 }
6820
6821 if (result != MOVE_UNDEFINED)
6822 break;
6823 }
6824 else if (BUFFER_POS_REACHED_P ())
6825 {
6826 buffer_pos_reached:
6827 it->current_x = x;
6828 it->max_ascent = ascent;
6829 it->max_descent = descent;
6830 result = MOVE_POS_MATCH_OR_ZV;
6831 break;
6832 }
6833 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
6834 {
6835 /* Stop when TO_X specified and reached. This check is
6836 necessary here because of lines consisting of a line end,
6837 only. The line end will not produce any glyphs and we
6838 would never get MOVE_X_REACHED. */
6839 xassert (it->nglyphs == 0);
6840 result = MOVE_X_REACHED;
6841 break;
6842 }
6843
6844 /* Is this a line end? If yes, we're done. */
6845 if (ITERATOR_AT_END_OF_LINE_P (it))
6846 {
6847 result = MOVE_NEWLINE_OR_CR;
6848 break;
6849 }
6850
6851 /* The current display element has been consumed. Advance
6852 to the next. */
6853 set_iterator_to_next (it, 1);
6854
6855 /* Stop if lines are truncated and IT's current x-position is
6856 past the right edge of the window now. */
6857 if (it->truncate_lines_p
6858 && it->current_x >= it->last_visible_x)
6859 {
6860 #ifdef HAVE_WINDOW_SYSTEM
6861 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
6862 {
6863 if (!get_next_display_element (it)
6864 || BUFFER_POS_REACHED_P ())
6865 {
6866 result = MOVE_POS_MATCH_OR_ZV;
6867 break;
6868 }
6869 if (ITERATOR_AT_END_OF_LINE_P (it))
6870 {
6871 result = MOVE_NEWLINE_OR_CR;
6872 break;
6873 }
6874 }
6875 #endif /* HAVE_WINDOW_SYSTEM */
6876 result = MOVE_LINE_TRUNCATED;
6877 break;
6878 }
6879 }
6880
6881 #undef BUFFER_POS_REACHED_P
6882
6883 /* Restore the iterator settings altered at the beginning of this
6884 function. */
6885 it->glyph_row = saved_glyph_row;
6886 return result;
6887 }
6888
6889
6890 /* Move IT forward until it satisfies one or more of the criteria in
6891 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
6892
6893 OP is a bit-mask that specifies where to stop, and in particular,
6894 which of those four position arguments makes a difference. See the
6895 description of enum move_operation_enum.
6896
6897 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
6898 screen line, this function will set IT to the next position >
6899 TO_CHARPOS. */
6900
6901 void
6902 move_it_to (it, to_charpos, to_x, to_y, to_vpos, op)
6903 struct it *it;
6904 int to_charpos, to_x, to_y, to_vpos;
6905 int op;
6906 {
6907 enum move_it_result skip, skip2 = MOVE_X_REACHED;
6908 int line_height;
6909 int reached = 0;
6910
6911 for (;;)
6912 {
6913 if (op & MOVE_TO_VPOS)
6914 {
6915 /* If no TO_CHARPOS and no TO_X specified, stop at the
6916 start of the line TO_VPOS. */
6917 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
6918 {
6919 if (it->vpos == to_vpos)
6920 {
6921 reached = 1;
6922 break;
6923 }
6924 else
6925 skip = move_it_in_display_line_to (it, -1, -1, 0);
6926 }
6927 else
6928 {
6929 /* TO_VPOS >= 0 means stop at TO_X in the line at
6930 TO_VPOS, or at TO_POS, whichever comes first. */
6931 if (it->vpos == to_vpos)
6932 {
6933 reached = 2;
6934 break;
6935 }
6936
6937 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
6938
6939 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
6940 {
6941 reached = 3;
6942 break;
6943 }
6944 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
6945 {
6946 /* We have reached TO_X but not in the line we want. */
6947 skip = move_it_in_display_line_to (it, to_charpos,
6948 -1, MOVE_TO_POS);
6949 if (skip == MOVE_POS_MATCH_OR_ZV)
6950 {
6951 reached = 4;
6952 break;
6953 }
6954 }
6955 }
6956 }
6957 else if (op & MOVE_TO_Y)
6958 {
6959 struct it it_backup;
6960
6961 /* TO_Y specified means stop at TO_X in the line containing
6962 TO_Y---or at TO_CHARPOS if this is reached first. The
6963 problem is that we can't really tell whether the line
6964 contains TO_Y before we have completely scanned it, and
6965 this may skip past TO_X. What we do is to first scan to
6966 TO_X.
6967
6968 If TO_X is not specified, use a TO_X of zero. The reason
6969 is to make the outcome of this function more predictable.
6970 If we didn't use TO_X == 0, we would stop at the end of
6971 the line which is probably not what a caller would expect
6972 to happen. */
6973 skip = move_it_in_display_line_to (it, to_charpos,
6974 ((op & MOVE_TO_X)
6975 ? to_x : 0),
6976 (MOVE_TO_X
6977 | (op & MOVE_TO_POS)));
6978
6979 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
6980 if (skip == MOVE_POS_MATCH_OR_ZV)
6981 {
6982 reached = 5;
6983 break;
6984 }
6985
6986 /* If TO_X was reached, we would like to know whether TO_Y
6987 is in the line. This can only be said if we know the
6988 total line height which requires us to scan the rest of
6989 the line. */
6990 if (skip == MOVE_X_REACHED)
6991 {
6992 /* Wait! We can conclude that TO_Y is in the line if
6993 the already scanned glyphs make the line tall enough
6994 because further scanning doesn't make it shorter. */
6995 line_height = it->max_ascent + it->max_descent;
6996 if (to_y >= it->current_y
6997 && to_y < it->current_y + line_height)
6998 {
6999 reached = 6;
7000 break;
7001 }
7002 it_backup = *it;
7003 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
7004 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
7005 op & MOVE_TO_POS);
7006 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
7007 }
7008
7009 /* Now, decide whether TO_Y is in this line. */
7010 line_height = it->max_ascent + it->max_descent;
7011 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
7012
7013 if (to_y >= it->current_y
7014 && to_y < it->current_y + line_height)
7015 {
7016 if (skip == MOVE_X_REACHED)
7017 /* If TO_Y is in this line and TO_X was reached above,
7018 we scanned too far. We have to restore IT's settings
7019 to the ones before skipping. */
7020 *it = it_backup;
7021 reached = 6;
7022 }
7023 else if (skip == MOVE_X_REACHED)
7024 {
7025 skip = skip2;
7026 if (skip == MOVE_POS_MATCH_OR_ZV)
7027 reached = 7;
7028 }
7029
7030 if (reached)
7031 break;
7032 }
7033 else if (BUFFERP (it->object)
7034 && it->method == GET_FROM_BUFFER
7035 && IT_CHARPOS (*it) >= to_charpos)
7036 skip = MOVE_POS_MATCH_OR_ZV;
7037 else
7038 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
7039
7040 switch (skip)
7041 {
7042 case MOVE_POS_MATCH_OR_ZV:
7043 reached = 8;
7044 goto out;
7045
7046 case MOVE_NEWLINE_OR_CR:
7047 set_iterator_to_next (it, 1);
7048 it->continuation_lines_width = 0;
7049 break;
7050
7051 case MOVE_LINE_TRUNCATED:
7052 it->continuation_lines_width = 0;
7053 reseat_at_next_visible_line_start (it, 0);
7054 if ((op & MOVE_TO_POS) != 0
7055 && IT_CHARPOS (*it) > to_charpos)
7056 {
7057 reached = 9;
7058 goto out;
7059 }
7060 break;
7061
7062 case MOVE_LINE_CONTINUED:
7063 /* For continued lines ending in a tab, some of the glyphs
7064 associated with the tab are displayed on the current
7065 line. Since it->current_x does not include these glyphs,
7066 we use it->last_visible_x instead. */
7067 it->continuation_lines_width +=
7068 (it->c == '\t') ? it->last_visible_x : it->current_x;
7069 break;
7070
7071 default:
7072 abort ();
7073 }
7074
7075 /* Reset/increment for the next run. */
7076 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
7077 it->current_x = it->hpos = 0;
7078 it->current_y += it->max_ascent + it->max_descent;
7079 ++it->vpos;
7080 last_height = it->max_ascent + it->max_descent;
7081 last_max_ascent = it->max_ascent;
7082 it->max_ascent = it->max_descent = 0;
7083 }
7084
7085 out:
7086
7087 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
7088 }
7089
7090
7091 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
7092
7093 If DY > 0, move IT backward at least that many pixels. DY = 0
7094 means move IT backward to the preceding line start or BEGV. This
7095 function may move over more than DY pixels if IT->current_y - DY
7096 ends up in the middle of a line; in this case IT->current_y will be
7097 set to the top of the line moved to. */
7098
7099 void
7100 move_it_vertically_backward (it, dy)
7101 struct it *it;
7102 int dy;
7103 {
7104 int nlines, h;
7105 struct it it2, it3;
7106 int start_pos;
7107
7108 move_further_back:
7109 xassert (dy >= 0);
7110
7111 start_pos = IT_CHARPOS (*it);
7112
7113 /* Estimate how many newlines we must move back. */
7114 nlines = max (1, dy / FRAME_LINE_HEIGHT (it->f));
7115
7116 /* Set the iterator's position that many lines back. */
7117 while (nlines-- && IT_CHARPOS (*it) > BEGV)
7118 back_to_previous_visible_line_start (it);
7119
7120 /* Reseat the iterator here. When moving backward, we don't want
7121 reseat to skip forward over invisible text, set up the iterator
7122 to deliver from overlay strings at the new position etc. So,
7123 use reseat_1 here. */
7124 reseat_1 (it, it->current.pos, 1);
7125
7126 /* We are now surely at a line start. */
7127 it->current_x = it->hpos = 0;
7128 it->continuation_lines_width = 0;
7129
7130 /* Move forward and see what y-distance we moved. First move to the
7131 start of the next line so that we get its height. We need this
7132 height to be able to tell whether we reached the specified
7133 y-distance. */
7134 it2 = *it;
7135 it2.max_ascent = it2.max_descent = 0;
7136 do
7137 {
7138 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
7139 MOVE_TO_POS | MOVE_TO_VPOS);
7140 }
7141 while (!IT_POS_VALID_AFTER_MOVE_P (&it2));
7142 xassert (IT_CHARPOS (*it) >= BEGV);
7143 it3 = it2;
7144
7145 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
7146 xassert (IT_CHARPOS (*it) >= BEGV);
7147 /* H is the actual vertical distance from the position in *IT
7148 and the starting position. */
7149 h = it2.current_y - it->current_y;
7150 /* NLINES is the distance in number of lines. */
7151 nlines = it2.vpos - it->vpos;
7152
7153 /* Correct IT's y and vpos position
7154 so that they are relative to the starting point. */
7155 it->vpos -= nlines;
7156 it->current_y -= h;
7157
7158 if (dy == 0)
7159 {
7160 /* DY == 0 means move to the start of the screen line. The
7161 value of nlines is > 0 if continuation lines were involved. */
7162 if (nlines > 0)
7163 move_it_by_lines (it, nlines, 1);
7164 #if 0
7165 /* I think this assert is bogus if buffer contains
7166 invisible text or images. KFS. */
7167 xassert (IT_CHARPOS (*it) <= start_pos);
7168 #endif
7169 }
7170 else
7171 {
7172 /* The y-position we try to reach, relative to *IT.
7173 Note that H has been subtracted in front of the if-statement. */
7174 int target_y = it->current_y + h - dy;
7175 int y0 = it3.current_y;
7176 int y1 = line_bottom_y (&it3);
7177 int line_height = y1 - y0;
7178
7179 /* If we did not reach target_y, try to move further backward if
7180 we can. If we moved too far backward, try to move forward. */
7181 if (target_y < it->current_y
7182 /* This is heuristic. In a window that's 3 lines high, with
7183 a line height of 13 pixels each, recentering with point
7184 on the bottom line will try to move -39/2 = 19 pixels
7185 backward. Try to avoid moving into the first line. */
7186 && (it->current_y - target_y
7187 > min (window_box_height (it->w), line_height * 2 / 3))
7188 && IT_CHARPOS (*it) > BEGV)
7189 {
7190 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
7191 target_y - it->current_y));
7192 dy = it->current_y - target_y;
7193 goto move_further_back;
7194 }
7195 else if (target_y >= it->current_y + line_height
7196 && IT_CHARPOS (*it) < ZV)
7197 {
7198 /* Should move forward by at least one line, maybe more.
7199
7200 Note: Calling move_it_by_lines can be expensive on
7201 terminal frames, where compute_motion is used (via
7202 vmotion) to do the job, when there are very long lines
7203 and truncate-lines is nil. That's the reason for
7204 treating terminal frames specially here. */
7205
7206 if (!FRAME_WINDOW_P (it->f))
7207 move_it_vertically (it, target_y - (it->current_y + line_height));
7208 else
7209 {
7210 do
7211 {
7212 move_it_by_lines (it, 1, 1);
7213 }
7214 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
7215 }
7216
7217 #if 0
7218 /* I think this assert is bogus if buffer contains
7219 invisible text or images. KFS. */
7220 xassert (IT_CHARPOS (*it) >= BEGV);
7221 #endif
7222 }
7223 }
7224 }
7225
7226
7227 /* Move IT by a specified amount of pixel lines DY. DY negative means
7228 move backwards. DY = 0 means move to start of screen line. At the
7229 end, IT will be on the start of a screen line. */
7230
7231 void
7232 move_it_vertically (it, dy)
7233 struct it *it;
7234 int dy;
7235 {
7236 if (dy <= 0)
7237 move_it_vertically_backward (it, -dy);
7238 else
7239 {
7240 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
7241 move_it_to (it, ZV, -1, it->current_y + dy, -1,
7242 MOVE_TO_POS | MOVE_TO_Y);
7243 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
7244
7245 /* If buffer ends in ZV without a newline, move to the start of
7246 the line to satisfy the post-condition. */
7247 if (IT_CHARPOS (*it) == ZV
7248 && ZV > BEGV
7249 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
7250 move_it_by_lines (it, 0, 0);
7251 }
7252 }
7253
7254
7255 /* Move iterator IT past the end of the text line it is in. */
7256
7257 void
7258 move_it_past_eol (it)
7259 struct it *it;
7260 {
7261 enum move_it_result rc;
7262
7263 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
7264 if (rc == MOVE_NEWLINE_OR_CR)
7265 set_iterator_to_next (it, 0);
7266 }
7267
7268
7269 #if 0 /* Currently not used. */
7270
7271 /* Return non-zero if some text between buffer positions START_CHARPOS
7272 and END_CHARPOS is invisible. IT->window is the window for text
7273 property lookup. */
7274
7275 static int
7276 invisible_text_between_p (it, start_charpos, end_charpos)
7277 struct it *it;
7278 int start_charpos, end_charpos;
7279 {
7280 Lisp_Object prop, limit;
7281 int invisible_found_p;
7282
7283 xassert (it != NULL && start_charpos <= end_charpos);
7284
7285 /* Is text at START invisible? */
7286 prop = Fget_char_property (make_number (start_charpos), Qinvisible,
7287 it->window);
7288 if (TEXT_PROP_MEANS_INVISIBLE (prop))
7289 invisible_found_p = 1;
7290 else
7291 {
7292 limit = Fnext_single_char_property_change (make_number (start_charpos),
7293 Qinvisible, Qnil,
7294 make_number (end_charpos));
7295 invisible_found_p = XFASTINT (limit) < end_charpos;
7296 }
7297
7298 return invisible_found_p;
7299 }
7300
7301 #endif /* 0 */
7302
7303
7304 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
7305 negative means move up. DVPOS == 0 means move to the start of the
7306 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
7307 NEED_Y_P is zero, IT->current_y will be left unchanged.
7308
7309 Further optimization ideas: If we would know that IT->f doesn't use
7310 a face with proportional font, we could be faster for
7311 truncate-lines nil. */
7312
7313 void
7314 move_it_by_lines (it, dvpos, need_y_p)
7315 struct it *it;
7316 int dvpos, need_y_p;
7317 {
7318 struct position pos;
7319
7320 /* The commented-out optimization uses vmotion on terminals. This
7321 gives bad results, because elements like it->what, on which
7322 callers such as pos_visible_p rely, aren't updated. */
7323 /* if (!FRAME_WINDOW_P (it->f))
7324 {
7325 struct text_pos textpos;
7326
7327 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
7328 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
7329 reseat (it, textpos, 1);
7330 it->vpos += pos.vpos;
7331 it->current_y += pos.vpos;
7332 }
7333 else */
7334
7335 if (dvpos == 0)
7336 {
7337 /* DVPOS == 0 means move to the start of the screen line. */
7338 move_it_vertically_backward (it, 0);
7339 xassert (it->current_x == 0 && it->hpos == 0);
7340 /* Let next call to line_bottom_y calculate real line height */
7341 last_height = 0;
7342 }
7343 else if (dvpos > 0)
7344 {
7345 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
7346 if (!IT_POS_VALID_AFTER_MOVE_P (it))
7347 move_it_to (it, IT_CHARPOS (*it) + 1, -1, -1, -1, MOVE_TO_POS);
7348 }
7349 else
7350 {
7351 struct it it2;
7352 int start_charpos, i;
7353
7354 /* Start at the beginning of the screen line containing IT's
7355 position. This may actually move vertically backwards,
7356 in case of overlays, so adjust dvpos accordingly. */
7357 dvpos += it->vpos;
7358 move_it_vertically_backward (it, 0);
7359 dvpos -= it->vpos;
7360
7361 /* Go back -DVPOS visible lines and reseat the iterator there. */
7362 start_charpos = IT_CHARPOS (*it);
7363 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > BEGV; --i)
7364 back_to_previous_visible_line_start (it);
7365 reseat (it, it->current.pos, 1);
7366
7367 /* Move further back if we end up in a string or an image. */
7368 while (!IT_POS_VALID_AFTER_MOVE_P (it))
7369 {
7370 /* First try to move to start of display line. */
7371 dvpos += it->vpos;
7372 move_it_vertically_backward (it, 0);
7373 dvpos -= it->vpos;
7374 if (IT_POS_VALID_AFTER_MOVE_P (it))
7375 break;
7376 /* If start of line is still in string or image,
7377 move further back. */
7378 back_to_previous_visible_line_start (it);
7379 reseat (it, it->current.pos, 1);
7380 dvpos--;
7381 }
7382
7383 it->current_x = it->hpos = 0;
7384
7385 /* Above call may have moved too far if continuation lines
7386 are involved. Scan forward and see if it did. */
7387 it2 = *it;
7388 it2.vpos = it2.current_y = 0;
7389 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
7390 it->vpos -= it2.vpos;
7391 it->current_y -= it2.current_y;
7392 it->current_x = it->hpos = 0;
7393
7394 /* If we moved too far back, move IT some lines forward. */
7395 if (it2.vpos > -dvpos)
7396 {
7397 int delta = it2.vpos + dvpos;
7398 it2 = *it;
7399 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
7400 /* Move back again if we got too far ahead. */
7401 if (IT_CHARPOS (*it) >= start_charpos)
7402 *it = it2;
7403 }
7404 }
7405 }
7406
7407 /* Return 1 if IT points into the middle of a display vector. */
7408
7409 int
7410 in_display_vector_p (it)
7411 struct it *it;
7412 {
7413 return (it->method == GET_FROM_DISPLAY_VECTOR
7414 && it->current.dpvec_index > 0
7415 && it->dpvec + it->current.dpvec_index != it->dpend);
7416 }
7417
7418 \f
7419 /***********************************************************************
7420 Messages
7421 ***********************************************************************/
7422
7423
7424 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
7425 to *Messages*. */
7426
7427 void
7428 add_to_log (format, arg1, arg2)
7429 char *format;
7430 Lisp_Object arg1, arg2;
7431 {
7432 Lisp_Object args[3];
7433 Lisp_Object msg, fmt;
7434 char *buffer;
7435 int len;
7436 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
7437 USE_SAFE_ALLOCA;
7438
7439 /* Do nothing if called asynchronously. Inserting text into
7440 a buffer may call after-change-functions and alike and
7441 that would means running Lisp asynchronously. */
7442 if (handling_signal)
7443 return;
7444
7445 fmt = msg = Qnil;
7446 GCPRO4 (fmt, msg, arg1, arg2);
7447
7448 args[0] = fmt = build_string (format);
7449 args[1] = arg1;
7450 args[2] = arg2;
7451 msg = Fformat (3, args);
7452
7453 len = SBYTES (msg) + 1;
7454 SAFE_ALLOCA (buffer, char *, len);
7455 bcopy (SDATA (msg), buffer, len);
7456
7457 message_dolog (buffer, len - 1, 1, 0);
7458 SAFE_FREE ();
7459
7460 UNGCPRO;
7461 }
7462
7463
7464 /* Output a newline in the *Messages* buffer if "needs" one. */
7465
7466 void
7467 message_log_maybe_newline ()
7468 {
7469 if (message_log_need_newline)
7470 message_dolog ("", 0, 1, 0);
7471 }
7472
7473
7474 /* Add a string M of length NBYTES to the message log, optionally
7475 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
7476 nonzero, means interpret the contents of M as multibyte. This
7477 function calls low-level routines in order to bypass text property
7478 hooks, etc. which might not be safe to run.
7479
7480 This may GC (insert may run before/after change hooks),
7481 so the buffer M must NOT point to a Lisp string. */
7482
7483 void
7484 message_dolog (m, nbytes, nlflag, multibyte)
7485 const char *m;
7486 int nbytes, nlflag, multibyte;
7487 {
7488 if (!NILP (Vmemory_full))
7489 return;
7490
7491 if (!NILP (Vmessage_log_max))
7492 {
7493 struct buffer *oldbuf;
7494 Lisp_Object oldpoint, oldbegv, oldzv;
7495 int old_windows_or_buffers_changed = windows_or_buffers_changed;
7496 int point_at_end = 0;
7497 int zv_at_end = 0;
7498 Lisp_Object old_deactivate_mark, tem;
7499 struct gcpro gcpro1;
7500
7501 old_deactivate_mark = Vdeactivate_mark;
7502 oldbuf = current_buffer;
7503 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
7504 current_buffer->undo_list = Qt;
7505
7506 oldpoint = message_dolog_marker1;
7507 set_marker_restricted (oldpoint, make_number (PT), Qnil);
7508 oldbegv = message_dolog_marker2;
7509 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
7510 oldzv = message_dolog_marker3;
7511 set_marker_restricted (oldzv, make_number (ZV), Qnil);
7512 GCPRO1 (old_deactivate_mark);
7513
7514 if (PT == Z)
7515 point_at_end = 1;
7516 if (ZV == Z)
7517 zv_at_end = 1;
7518
7519 BEGV = BEG;
7520 BEGV_BYTE = BEG_BYTE;
7521 ZV = Z;
7522 ZV_BYTE = Z_BYTE;
7523 TEMP_SET_PT_BOTH (Z, Z_BYTE);
7524
7525 /* Insert the string--maybe converting multibyte to single byte
7526 or vice versa, so that all the text fits the buffer. */
7527 if (multibyte
7528 && NILP (current_buffer->enable_multibyte_characters))
7529 {
7530 int i, c, char_bytes;
7531 unsigned char work[1];
7532
7533 /* Convert a multibyte string to single-byte
7534 for the *Message* buffer. */
7535 for (i = 0; i < nbytes; i += char_bytes)
7536 {
7537 c = string_char_and_length (m + i, nbytes - i, &char_bytes);
7538 work[0] = (ASCII_CHAR_P (c)
7539 ? c
7540 : multibyte_char_to_unibyte (c, Qnil));
7541 insert_1_both (work, 1, 1, 1, 0, 0);
7542 }
7543 }
7544 else if (! multibyte
7545 && ! NILP (current_buffer->enable_multibyte_characters))
7546 {
7547 int i, c, char_bytes;
7548 unsigned char *msg = (unsigned char *) m;
7549 unsigned char str[MAX_MULTIBYTE_LENGTH];
7550 /* Convert a single-byte string to multibyte
7551 for the *Message* buffer. */
7552 for (i = 0; i < nbytes; i++)
7553 {
7554 c = msg[i];
7555 c = unibyte_char_to_multibyte (c);
7556 char_bytes = CHAR_STRING (c, str);
7557 insert_1_both (str, 1, char_bytes, 1, 0, 0);
7558 }
7559 }
7560 else if (nbytes)
7561 insert_1 (m, nbytes, 1, 0, 0);
7562
7563 if (nlflag)
7564 {
7565 int this_bol, this_bol_byte, prev_bol, prev_bol_byte, dup;
7566 insert_1 ("\n", 1, 1, 0, 0);
7567
7568 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
7569 this_bol = PT;
7570 this_bol_byte = PT_BYTE;
7571
7572 /* See if this line duplicates the previous one.
7573 If so, combine duplicates. */
7574 if (this_bol > BEG)
7575 {
7576 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
7577 prev_bol = PT;
7578 prev_bol_byte = PT_BYTE;
7579
7580 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
7581 this_bol, this_bol_byte);
7582 if (dup)
7583 {
7584 del_range_both (prev_bol, prev_bol_byte,
7585 this_bol, this_bol_byte, 0);
7586 if (dup > 1)
7587 {
7588 char dupstr[40];
7589 int duplen;
7590
7591 /* If you change this format, don't forget to also
7592 change message_log_check_duplicate. */
7593 sprintf (dupstr, " [%d times]", dup);
7594 duplen = strlen (dupstr);
7595 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
7596 insert_1 (dupstr, duplen, 1, 0, 1);
7597 }
7598 }
7599 }
7600
7601 /* If we have more than the desired maximum number of lines
7602 in the *Messages* buffer now, delete the oldest ones.
7603 This is safe because we don't have undo in this buffer. */
7604
7605 if (NATNUMP (Vmessage_log_max))
7606 {
7607 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
7608 -XFASTINT (Vmessage_log_max) - 1, 0);
7609 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
7610 }
7611 }
7612 BEGV = XMARKER (oldbegv)->charpos;
7613 BEGV_BYTE = marker_byte_position (oldbegv);
7614
7615 if (zv_at_end)
7616 {
7617 ZV = Z;
7618 ZV_BYTE = Z_BYTE;
7619 }
7620 else
7621 {
7622 ZV = XMARKER (oldzv)->charpos;
7623 ZV_BYTE = marker_byte_position (oldzv);
7624 }
7625
7626 if (point_at_end)
7627 TEMP_SET_PT_BOTH (Z, Z_BYTE);
7628 else
7629 /* We can't do Fgoto_char (oldpoint) because it will run some
7630 Lisp code. */
7631 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
7632 XMARKER (oldpoint)->bytepos);
7633
7634 UNGCPRO;
7635 unchain_marker (XMARKER (oldpoint));
7636 unchain_marker (XMARKER (oldbegv));
7637 unchain_marker (XMARKER (oldzv));
7638
7639 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
7640 set_buffer_internal (oldbuf);
7641 if (NILP (tem))
7642 windows_or_buffers_changed = old_windows_or_buffers_changed;
7643 message_log_need_newline = !nlflag;
7644 Vdeactivate_mark = old_deactivate_mark;
7645 }
7646 }
7647
7648
7649 /* We are at the end of the buffer after just having inserted a newline.
7650 (Note: We depend on the fact we won't be crossing the gap.)
7651 Check to see if the most recent message looks a lot like the previous one.
7652 Return 0 if different, 1 if the new one should just replace it, or a
7653 value N > 1 if we should also append " [N times]". */
7654
7655 static int
7656 message_log_check_duplicate (prev_bol, prev_bol_byte, this_bol, this_bol_byte)
7657 int prev_bol, this_bol;
7658 int prev_bol_byte, this_bol_byte;
7659 {
7660 int i;
7661 int len = Z_BYTE - 1 - this_bol_byte;
7662 int seen_dots = 0;
7663 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
7664 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
7665
7666 for (i = 0; i < len; i++)
7667 {
7668 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
7669 seen_dots = 1;
7670 if (p1[i] != p2[i])
7671 return seen_dots;
7672 }
7673 p1 += len;
7674 if (*p1 == '\n')
7675 return 2;
7676 if (*p1++ == ' ' && *p1++ == '[')
7677 {
7678 int n = 0;
7679 while (*p1 >= '0' && *p1 <= '9')
7680 n = n * 10 + *p1++ - '0';
7681 if (strncmp (p1, " times]\n", 8) == 0)
7682 return n+1;
7683 }
7684 return 0;
7685 }
7686 \f
7687
7688 /* Display an echo area message M with a specified length of NBYTES
7689 bytes. The string may include null characters. If M is 0, clear
7690 out any existing message, and let the mini-buffer text show
7691 through.
7692
7693 This may GC, so the buffer M must NOT point to a Lisp string. */
7694
7695 void
7696 message2 (m, nbytes, multibyte)
7697 const char *m;
7698 int nbytes;
7699 int multibyte;
7700 {
7701 /* First flush out any partial line written with print. */
7702 message_log_maybe_newline ();
7703 if (m)
7704 message_dolog (m, nbytes, 1, multibyte);
7705 message2_nolog (m, nbytes, multibyte);
7706 }
7707
7708
7709 /* The non-logging counterpart of message2. */
7710
7711 void
7712 message2_nolog (m, nbytes, multibyte)
7713 const char *m;
7714 int nbytes, multibyte;
7715 {
7716 struct frame *sf = SELECTED_FRAME ();
7717 message_enable_multibyte = multibyte;
7718
7719 if (noninteractive)
7720 {
7721 if (noninteractive_need_newline)
7722 putc ('\n', stderr);
7723 noninteractive_need_newline = 0;
7724 if (m)
7725 fwrite (m, nbytes, 1, stderr);
7726 if (cursor_in_echo_area == 0)
7727 fprintf (stderr, "\n");
7728 fflush (stderr);
7729 }
7730 /* A null message buffer means that the frame hasn't really been
7731 initialized yet. Error messages get reported properly by
7732 cmd_error, so this must be just an informative message; toss it. */
7733 else if (INTERACTIVE
7734 && sf->glyphs_initialized_p
7735 && FRAME_MESSAGE_BUF (sf))
7736 {
7737 Lisp_Object mini_window;
7738 struct frame *f;
7739
7740 /* Get the frame containing the mini-buffer
7741 that the selected frame is using. */
7742 mini_window = FRAME_MINIBUF_WINDOW (sf);
7743 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7744
7745 FRAME_SAMPLE_VISIBILITY (f);
7746 if (FRAME_VISIBLE_P (sf)
7747 && ! FRAME_VISIBLE_P (f))
7748 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
7749
7750 if (m)
7751 {
7752 set_message (m, Qnil, nbytes, multibyte);
7753 if (minibuffer_auto_raise)
7754 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
7755 }
7756 else
7757 clear_message (1, 1);
7758
7759 do_pending_window_change (0);
7760 echo_area_display (1);
7761 do_pending_window_change (0);
7762 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
7763 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
7764 }
7765 }
7766
7767
7768 /* Display an echo area message M with a specified length of NBYTES
7769 bytes. The string may include null characters. If M is not a
7770 string, clear out any existing message, and let the mini-buffer
7771 text show through.
7772
7773 This function cancels echoing. */
7774
7775 void
7776 message3 (m, nbytes, multibyte)
7777 Lisp_Object m;
7778 int nbytes;
7779 int multibyte;
7780 {
7781 struct gcpro gcpro1;
7782
7783 GCPRO1 (m);
7784 clear_message (1,1);
7785 cancel_echoing ();
7786
7787 /* First flush out any partial line written with print. */
7788 message_log_maybe_newline ();
7789 if (STRINGP (m))
7790 {
7791 char *buffer;
7792 USE_SAFE_ALLOCA;
7793
7794 SAFE_ALLOCA (buffer, char *, nbytes);
7795 bcopy (SDATA (m), buffer, nbytes);
7796 message_dolog (buffer, nbytes, 1, multibyte);
7797 SAFE_FREE ();
7798 }
7799 message3_nolog (m, nbytes, multibyte);
7800
7801 UNGCPRO;
7802 }
7803
7804
7805 /* The non-logging version of message3.
7806 This does not cancel echoing, because it is used for echoing.
7807 Perhaps we need to make a separate function for echoing
7808 and make this cancel echoing. */
7809
7810 void
7811 message3_nolog (m, nbytes, multibyte)
7812 Lisp_Object m;
7813 int nbytes, multibyte;
7814 {
7815 struct frame *sf = SELECTED_FRAME ();
7816 message_enable_multibyte = multibyte;
7817
7818 if (noninteractive)
7819 {
7820 if (noninteractive_need_newline)
7821 putc ('\n', stderr);
7822 noninteractive_need_newline = 0;
7823 if (STRINGP (m))
7824 fwrite (SDATA (m), nbytes, 1, stderr);
7825 if (cursor_in_echo_area == 0)
7826 fprintf (stderr, "\n");
7827 fflush (stderr);
7828 }
7829 /* A null message buffer means that the frame hasn't really been
7830 initialized yet. Error messages get reported properly by
7831 cmd_error, so this must be just an informative message; toss it. */
7832 else if (INTERACTIVE
7833 && sf->glyphs_initialized_p
7834 && FRAME_MESSAGE_BUF (sf))
7835 {
7836 Lisp_Object mini_window;
7837 Lisp_Object frame;
7838 struct frame *f;
7839
7840 /* Get the frame containing the mini-buffer
7841 that the selected frame is using. */
7842 mini_window = FRAME_MINIBUF_WINDOW (sf);
7843 frame = XWINDOW (mini_window)->frame;
7844 f = XFRAME (frame);
7845
7846 FRAME_SAMPLE_VISIBILITY (f);
7847 if (FRAME_VISIBLE_P (sf)
7848 && !FRAME_VISIBLE_P (f))
7849 Fmake_frame_visible (frame);
7850
7851 if (STRINGP (m) && SCHARS (m) > 0)
7852 {
7853 set_message (NULL, m, nbytes, multibyte);
7854 if (minibuffer_auto_raise)
7855 Fraise_frame (frame);
7856 /* Assume we are not echoing.
7857 (If we are, echo_now will override this.) */
7858 echo_message_buffer = Qnil;
7859 }
7860 else
7861 clear_message (1, 1);
7862
7863 do_pending_window_change (0);
7864 echo_area_display (1);
7865 do_pending_window_change (0);
7866 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
7867 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
7868 }
7869 }
7870
7871
7872 /* Display a null-terminated echo area message M. If M is 0, clear
7873 out any existing message, and let the mini-buffer text show through.
7874
7875 The buffer M must continue to exist until after the echo area gets
7876 cleared or some other message gets displayed there. Do not pass
7877 text that is stored in a Lisp string. Do not pass text in a buffer
7878 that was alloca'd. */
7879
7880 void
7881 message1 (m)
7882 char *m;
7883 {
7884 message2 (m, (m ? strlen (m) : 0), 0);
7885 }
7886
7887
7888 /* The non-logging counterpart of message1. */
7889
7890 void
7891 message1_nolog (m)
7892 char *m;
7893 {
7894 message2_nolog (m, (m ? strlen (m) : 0), 0);
7895 }
7896
7897 /* Display a message M which contains a single %s
7898 which gets replaced with STRING. */
7899
7900 void
7901 message_with_string (m, string, log)
7902 char *m;
7903 Lisp_Object string;
7904 int log;
7905 {
7906 CHECK_STRING (string);
7907
7908 if (noninteractive)
7909 {
7910 if (m)
7911 {
7912 if (noninteractive_need_newline)
7913 putc ('\n', stderr);
7914 noninteractive_need_newline = 0;
7915 fprintf (stderr, m, SDATA (string));
7916 if (cursor_in_echo_area == 0)
7917 fprintf (stderr, "\n");
7918 fflush (stderr);
7919 }
7920 }
7921 else if (INTERACTIVE)
7922 {
7923 /* The frame whose minibuffer we're going to display the message on.
7924 It may be larger than the selected frame, so we need
7925 to use its buffer, not the selected frame's buffer. */
7926 Lisp_Object mini_window;
7927 struct frame *f, *sf = SELECTED_FRAME ();
7928
7929 /* Get the frame containing the minibuffer
7930 that the selected frame is using. */
7931 mini_window = FRAME_MINIBUF_WINDOW (sf);
7932 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7933
7934 /* A null message buffer means that the frame hasn't really been
7935 initialized yet. Error messages get reported properly by
7936 cmd_error, so this must be just an informative message; toss it. */
7937 if (FRAME_MESSAGE_BUF (f))
7938 {
7939 Lisp_Object args[2], message;
7940 struct gcpro gcpro1, gcpro2;
7941
7942 args[0] = build_string (m);
7943 args[1] = message = string;
7944 GCPRO2 (args[0], message);
7945 gcpro1.nvars = 2;
7946
7947 message = Fformat (2, args);
7948
7949 if (log)
7950 message3 (message, SBYTES (message), STRING_MULTIBYTE (message));
7951 else
7952 message3_nolog (message, SBYTES (message), STRING_MULTIBYTE (message));
7953
7954 UNGCPRO;
7955
7956 /* Print should start at the beginning of the message
7957 buffer next time. */
7958 message_buf_print = 0;
7959 }
7960 }
7961 }
7962
7963
7964 /* Dump an informative message to the minibuf. If M is 0, clear out
7965 any existing message, and let the mini-buffer text show through. */
7966
7967 /* VARARGS 1 */
7968 void
7969 message (m, a1, a2, a3)
7970 char *m;
7971 EMACS_INT a1, a2, a3;
7972 {
7973 if (noninteractive)
7974 {
7975 if (m)
7976 {
7977 if (noninteractive_need_newline)
7978 putc ('\n', stderr);
7979 noninteractive_need_newline = 0;
7980 fprintf (stderr, m, a1, a2, a3);
7981 if (cursor_in_echo_area == 0)
7982 fprintf (stderr, "\n");
7983 fflush (stderr);
7984 }
7985 }
7986 else if (INTERACTIVE)
7987 {
7988 /* The frame whose mini-buffer we're going to display the message
7989 on. It may be larger than the selected frame, so we need to
7990 use its buffer, not the selected frame's buffer. */
7991 Lisp_Object mini_window;
7992 struct frame *f, *sf = SELECTED_FRAME ();
7993
7994 /* Get the frame containing the mini-buffer
7995 that the selected frame is using. */
7996 mini_window = FRAME_MINIBUF_WINDOW (sf);
7997 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7998
7999 /* A null message buffer means that the frame hasn't really been
8000 initialized yet. Error messages get reported properly by
8001 cmd_error, so this must be just an informative message; toss
8002 it. */
8003 if (FRAME_MESSAGE_BUF (f))
8004 {
8005 if (m)
8006 {
8007 int len;
8008 #ifdef NO_ARG_ARRAY
8009 char *a[3];
8010 a[0] = (char *) a1;
8011 a[1] = (char *) a2;
8012 a[2] = (char *) a3;
8013
8014 len = doprnt (FRAME_MESSAGE_BUF (f),
8015 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
8016 #else
8017 len = doprnt (FRAME_MESSAGE_BUF (f),
8018 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3,
8019 (char **) &a1);
8020 #endif /* NO_ARG_ARRAY */
8021
8022 message2 (FRAME_MESSAGE_BUF (f), len, 0);
8023 }
8024 else
8025 message1 (0);
8026
8027 /* Print should start at the beginning of the message
8028 buffer next time. */
8029 message_buf_print = 0;
8030 }
8031 }
8032 }
8033
8034
8035 /* The non-logging version of message. */
8036
8037 void
8038 message_nolog (m, a1, a2, a3)
8039 char *m;
8040 EMACS_INT a1, a2, a3;
8041 {
8042 Lisp_Object old_log_max;
8043 old_log_max = Vmessage_log_max;
8044 Vmessage_log_max = Qnil;
8045 message (m, a1, a2, a3);
8046 Vmessage_log_max = old_log_max;
8047 }
8048
8049
8050 /* Display the current message in the current mini-buffer. This is
8051 only called from error handlers in process.c, and is not time
8052 critical. */
8053
8054 void
8055 update_echo_area ()
8056 {
8057 if (!NILP (echo_area_buffer[0]))
8058 {
8059 Lisp_Object string;
8060 string = Fcurrent_message ();
8061 message3 (string, SBYTES (string),
8062 !NILP (current_buffer->enable_multibyte_characters));
8063 }
8064 }
8065
8066
8067 /* Make sure echo area buffers in `echo_buffers' are live.
8068 If they aren't, make new ones. */
8069
8070 static void
8071 ensure_echo_area_buffers ()
8072 {
8073 int i;
8074
8075 for (i = 0; i < 2; ++i)
8076 if (!BUFFERP (echo_buffer[i])
8077 || NILP (XBUFFER (echo_buffer[i])->name))
8078 {
8079 char name[30];
8080 Lisp_Object old_buffer;
8081 int j;
8082
8083 old_buffer = echo_buffer[i];
8084 sprintf (name, " *Echo Area %d*", i);
8085 echo_buffer[i] = Fget_buffer_create (build_string (name));
8086 XBUFFER (echo_buffer[i])->truncate_lines = Qnil;
8087
8088 for (j = 0; j < 2; ++j)
8089 if (EQ (old_buffer, echo_area_buffer[j]))
8090 echo_area_buffer[j] = echo_buffer[i];
8091 }
8092 }
8093
8094
8095 /* Call FN with args A1..A4 with either the current or last displayed
8096 echo_area_buffer as current buffer.
8097
8098 WHICH zero means use the current message buffer
8099 echo_area_buffer[0]. If that is nil, choose a suitable buffer
8100 from echo_buffer[] and clear it.
8101
8102 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
8103 suitable buffer from echo_buffer[] and clear it.
8104
8105 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
8106 that the current message becomes the last displayed one, make
8107 choose a suitable buffer for echo_area_buffer[0], and clear it.
8108
8109 Value is what FN returns. */
8110
8111 static int
8112 with_echo_area_buffer (w, which, fn, a1, a2, a3, a4)
8113 struct window *w;
8114 int which;
8115 int (*fn) P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
8116 EMACS_INT a1;
8117 Lisp_Object a2;
8118 EMACS_INT a3, a4;
8119 {
8120 Lisp_Object buffer;
8121 int this_one, the_other, clear_buffer_p, rc;
8122 int count = SPECPDL_INDEX ();
8123
8124 /* If buffers aren't live, make new ones. */
8125 ensure_echo_area_buffers ();
8126
8127 clear_buffer_p = 0;
8128
8129 if (which == 0)
8130 this_one = 0, the_other = 1;
8131 else if (which > 0)
8132 this_one = 1, the_other = 0;
8133 else
8134 {
8135 this_one = 0, the_other = 1;
8136 clear_buffer_p = 1;
8137
8138 /* We need a fresh one in case the current echo buffer equals
8139 the one containing the last displayed echo area message. */
8140 if (!NILP (echo_area_buffer[this_one])
8141 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
8142 echo_area_buffer[this_one] = Qnil;
8143 }
8144
8145 /* Choose a suitable buffer from echo_buffer[] is we don't
8146 have one. */
8147 if (NILP (echo_area_buffer[this_one]))
8148 {
8149 echo_area_buffer[this_one]
8150 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
8151 ? echo_buffer[the_other]
8152 : echo_buffer[this_one]);
8153 clear_buffer_p = 1;
8154 }
8155
8156 buffer = echo_area_buffer[this_one];
8157
8158 /* Don't get confused by reusing the buffer used for echoing
8159 for a different purpose. */
8160 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
8161 cancel_echoing ();
8162
8163 record_unwind_protect (unwind_with_echo_area_buffer,
8164 with_echo_area_buffer_unwind_data (w));
8165
8166 /* Make the echo area buffer current. Note that for display
8167 purposes, it is not necessary that the displayed window's buffer
8168 == current_buffer, except for text property lookup. So, let's
8169 only set that buffer temporarily here without doing a full
8170 Fset_window_buffer. We must also change w->pointm, though,
8171 because otherwise an assertions in unshow_buffer fails, and Emacs
8172 aborts. */
8173 set_buffer_internal_1 (XBUFFER (buffer));
8174 if (w)
8175 {
8176 w->buffer = buffer;
8177 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
8178 }
8179
8180 current_buffer->undo_list = Qt;
8181 current_buffer->read_only = Qnil;
8182 specbind (Qinhibit_read_only, Qt);
8183 specbind (Qinhibit_modification_hooks, Qt);
8184
8185 if (clear_buffer_p && Z > BEG)
8186 del_range (BEG, Z);
8187
8188 xassert (BEGV >= BEG);
8189 xassert (ZV <= Z && ZV >= BEGV);
8190
8191 rc = fn (a1, a2, a3, a4);
8192
8193 xassert (BEGV >= BEG);
8194 xassert (ZV <= Z && ZV >= BEGV);
8195
8196 unbind_to (count, Qnil);
8197 return rc;
8198 }
8199
8200
8201 /* Save state that should be preserved around the call to the function
8202 FN called in with_echo_area_buffer. */
8203
8204 static Lisp_Object
8205 with_echo_area_buffer_unwind_data (w)
8206 struct window *w;
8207 {
8208 int i = 0;
8209 Lisp_Object vector;
8210
8211 /* Reduce consing by keeping one vector in
8212 Vwith_echo_area_save_vector. */
8213 vector = Vwith_echo_area_save_vector;
8214 Vwith_echo_area_save_vector = Qnil;
8215
8216 if (NILP (vector))
8217 vector = Fmake_vector (make_number (7), Qnil);
8218
8219 XSETBUFFER (AREF (vector, i), current_buffer); ++i;
8220 AREF (vector, i) = Vdeactivate_mark, ++i;
8221 AREF (vector, i) = make_number (windows_or_buffers_changed), ++i;
8222
8223 if (w)
8224 {
8225 XSETWINDOW (AREF (vector, i), w); ++i;
8226 AREF (vector, i) = w->buffer; ++i;
8227 AREF (vector, i) = make_number (XMARKER (w->pointm)->charpos); ++i;
8228 AREF (vector, i) = make_number (XMARKER (w->pointm)->bytepos); ++i;
8229 }
8230 else
8231 {
8232 int end = i + 4;
8233 for (; i < end; ++i)
8234 AREF (vector, i) = Qnil;
8235 }
8236
8237 xassert (i == ASIZE (vector));
8238 return vector;
8239 }
8240
8241
8242 /* Restore global state from VECTOR which was created by
8243 with_echo_area_buffer_unwind_data. */
8244
8245 static Lisp_Object
8246 unwind_with_echo_area_buffer (vector)
8247 Lisp_Object vector;
8248 {
8249 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
8250 Vdeactivate_mark = AREF (vector, 1);
8251 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
8252
8253 if (WINDOWP (AREF (vector, 3)))
8254 {
8255 struct window *w;
8256 Lisp_Object buffer, charpos, bytepos;
8257
8258 w = XWINDOW (AREF (vector, 3));
8259 buffer = AREF (vector, 4);
8260 charpos = AREF (vector, 5);
8261 bytepos = AREF (vector, 6);
8262
8263 w->buffer = buffer;
8264 set_marker_both (w->pointm, buffer,
8265 XFASTINT (charpos), XFASTINT (bytepos));
8266 }
8267
8268 Vwith_echo_area_save_vector = vector;
8269 return Qnil;
8270 }
8271
8272
8273 /* Set up the echo area for use by print functions. MULTIBYTE_P
8274 non-zero means we will print multibyte. */
8275
8276 void
8277 setup_echo_area_for_printing (multibyte_p)
8278 int multibyte_p;
8279 {
8280 /* If we can't find an echo area any more, exit. */
8281 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
8282 Fkill_emacs (Qnil);
8283
8284 ensure_echo_area_buffers ();
8285
8286 if (!message_buf_print)
8287 {
8288 /* A message has been output since the last time we printed.
8289 Choose a fresh echo area buffer. */
8290 if (EQ (echo_area_buffer[1], echo_buffer[0]))
8291 echo_area_buffer[0] = echo_buffer[1];
8292 else
8293 echo_area_buffer[0] = echo_buffer[0];
8294
8295 /* Switch to that buffer and clear it. */
8296 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
8297 current_buffer->truncate_lines = Qnil;
8298
8299 if (Z > BEG)
8300 {
8301 int count = SPECPDL_INDEX ();
8302 specbind (Qinhibit_read_only, Qt);
8303 /* Note that undo recording is always disabled. */
8304 del_range (BEG, Z);
8305 unbind_to (count, Qnil);
8306 }
8307 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
8308
8309 /* Set up the buffer for the multibyteness we need. */
8310 if (multibyte_p
8311 != !NILP (current_buffer->enable_multibyte_characters))
8312 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
8313
8314 /* Raise the frame containing the echo area. */
8315 if (minibuffer_auto_raise)
8316 {
8317 struct frame *sf = SELECTED_FRAME ();
8318 Lisp_Object mini_window;
8319 mini_window = FRAME_MINIBUF_WINDOW (sf);
8320 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
8321 }
8322
8323 message_log_maybe_newline ();
8324 message_buf_print = 1;
8325 }
8326 else
8327 {
8328 if (NILP (echo_area_buffer[0]))
8329 {
8330 if (EQ (echo_area_buffer[1], echo_buffer[0]))
8331 echo_area_buffer[0] = echo_buffer[1];
8332 else
8333 echo_area_buffer[0] = echo_buffer[0];
8334 }
8335
8336 if (current_buffer != XBUFFER (echo_area_buffer[0]))
8337 {
8338 /* Someone switched buffers between print requests. */
8339 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
8340 current_buffer->truncate_lines = Qnil;
8341 }
8342 }
8343 }
8344
8345
8346 /* Display an echo area message in window W. Value is non-zero if W's
8347 height is changed. If display_last_displayed_message_p is
8348 non-zero, display the message that was last displayed, otherwise
8349 display the current message. */
8350
8351 static int
8352 display_echo_area (w)
8353 struct window *w;
8354 {
8355 int i, no_message_p, window_height_changed_p, count;
8356
8357 /* Temporarily disable garbage collections while displaying the echo
8358 area. This is done because a GC can print a message itself.
8359 That message would modify the echo area buffer's contents while a
8360 redisplay of the buffer is going on, and seriously confuse
8361 redisplay. */
8362 count = inhibit_garbage_collection ();
8363
8364 /* If there is no message, we must call display_echo_area_1
8365 nevertheless because it resizes the window. But we will have to
8366 reset the echo_area_buffer in question to nil at the end because
8367 with_echo_area_buffer will sets it to an empty buffer. */
8368 i = display_last_displayed_message_p ? 1 : 0;
8369 no_message_p = NILP (echo_area_buffer[i]);
8370
8371 window_height_changed_p
8372 = with_echo_area_buffer (w, display_last_displayed_message_p,
8373 display_echo_area_1,
8374 (EMACS_INT) w, Qnil, 0, 0);
8375
8376 if (no_message_p)
8377 echo_area_buffer[i] = Qnil;
8378
8379 unbind_to (count, Qnil);
8380 return window_height_changed_p;
8381 }
8382
8383
8384 /* Helper for display_echo_area. Display the current buffer which
8385 contains the current echo area message in window W, a mini-window,
8386 a pointer to which is passed in A1. A2..A4 are currently not used.
8387 Change the height of W so that all of the message is displayed.
8388 Value is non-zero if height of W was changed. */
8389
8390 static int
8391 display_echo_area_1 (a1, a2, a3, a4)
8392 EMACS_INT a1;
8393 Lisp_Object a2;
8394 EMACS_INT a3, a4;
8395 {
8396 struct window *w = (struct window *) a1;
8397 Lisp_Object window;
8398 struct text_pos start;
8399 int window_height_changed_p = 0;
8400
8401 /* Do this before displaying, so that we have a large enough glyph
8402 matrix for the display. If we can't get enough space for the
8403 whole text, display the last N lines. That works by setting w->start. */
8404 window_height_changed_p = resize_mini_window (w, 0);
8405
8406 /* Use the starting position chosen by resize_mini_window. */
8407 SET_TEXT_POS_FROM_MARKER (start, w->start);
8408
8409 /* Display. */
8410 clear_glyph_matrix (w->desired_matrix);
8411 XSETWINDOW (window, w);
8412 try_window (window, start, 0);
8413
8414 return window_height_changed_p;
8415 }
8416
8417
8418 /* Resize the echo area window to exactly the size needed for the
8419 currently displayed message, if there is one. If a mini-buffer
8420 is active, don't shrink it. */
8421
8422 void
8423 resize_echo_area_exactly ()
8424 {
8425 if (BUFFERP (echo_area_buffer[0])
8426 && WINDOWP (echo_area_window))
8427 {
8428 struct window *w = XWINDOW (echo_area_window);
8429 int resized_p;
8430 Lisp_Object resize_exactly;
8431
8432 if (minibuf_level == 0)
8433 resize_exactly = Qt;
8434 else
8435 resize_exactly = Qnil;
8436
8437 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
8438 (EMACS_INT) w, resize_exactly, 0, 0);
8439 if (resized_p)
8440 {
8441 ++windows_or_buffers_changed;
8442 ++update_mode_lines;
8443 redisplay_internal (0);
8444 }
8445 }
8446 }
8447
8448
8449 /* Callback function for with_echo_area_buffer, when used from
8450 resize_echo_area_exactly. A1 contains a pointer to the window to
8451 resize, EXACTLY non-nil means resize the mini-window exactly to the
8452 size of the text displayed. A3 and A4 are not used. Value is what
8453 resize_mini_window returns. */
8454
8455 static int
8456 resize_mini_window_1 (a1, exactly, a3, a4)
8457 EMACS_INT a1;
8458 Lisp_Object exactly;
8459 EMACS_INT a3, a4;
8460 {
8461 return resize_mini_window ((struct window *) a1, !NILP (exactly));
8462 }
8463
8464
8465 /* Resize mini-window W to fit the size of its contents. EXACT:P
8466 means size the window exactly to the size needed. Otherwise, it's
8467 only enlarged until W's buffer is empty.
8468
8469 Set W->start to the right place to begin display. If the whole
8470 contents fit, start at the beginning. Otherwise, start so as
8471 to make the end of the contents appear. This is particularly
8472 important for y-or-n-p, but seems desirable generally.
8473
8474 Value is non-zero if the window height has been changed. */
8475
8476 int
8477 resize_mini_window (w, exact_p)
8478 struct window *w;
8479 int exact_p;
8480 {
8481 struct frame *f = XFRAME (w->frame);
8482 int window_height_changed_p = 0;
8483
8484 xassert (MINI_WINDOW_P (w));
8485
8486 /* By default, start display at the beginning. */
8487 set_marker_both (w->start, w->buffer,
8488 BUF_BEGV (XBUFFER (w->buffer)),
8489 BUF_BEGV_BYTE (XBUFFER (w->buffer)));
8490
8491 /* Don't resize windows while redisplaying a window; it would
8492 confuse redisplay functions when the size of the window they are
8493 displaying changes from under them. Such a resizing can happen,
8494 for instance, when which-func prints a long message while
8495 we are running fontification-functions. We're running these
8496 functions with safe_call which binds inhibit-redisplay to t. */
8497 if (!NILP (Vinhibit_redisplay))
8498 return 0;
8499
8500 /* Nil means don't try to resize. */
8501 if (NILP (Vresize_mini_windows)
8502 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
8503 return 0;
8504
8505 if (!FRAME_MINIBUF_ONLY_P (f))
8506 {
8507 struct it it;
8508 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
8509 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
8510 int height, max_height;
8511 int unit = FRAME_LINE_HEIGHT (f);
8512 struct text_pos start;
8513 struct buffer *old_current_buffer = NULL;
8514
8515 if (current_buffer != XBUFFER (w->buffer))
8516 {
8517 old_current_buffer = current_buffer;
8518 set_buffer_internal (XBUFFER (w->buffer));
8519 }
8520
8521 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
8522
8523 /* Compute the max. number of lines specified by the user. */
8524 if (FLOATP (Vmax_mini_window_height))
8525 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
8526 else if (INTEGERP (Vmax_mini_window_height))
8527 max_height = XINT (Vmax_mini_window_height);
8528 else
8529 max_height = total_height / 4;
8530
8531 /* Correct that max. height if it's bogus. */
8532 max_height = max (1, max_height);
8533 max_height = min (total_height, max_height);
8534
8535 /* Find out the height of the text in the window. */
8536 if (it.truncate_lines_p)
8537 height = 1;
8538 else
8539 {
8540 last_height = 0;
8541 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
8542 if (it.max_ascent == 0 && it.max_descent == 0)
8543 height = it.current_y + last_height;
8544 else
8545 height = it.current_y + it.max_ascent + it.max_descent;
8546 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
8547 height = (height + unit - 1) / unit;
8548 }
8549
8550 /* Compute a suitable window start. */
8551 if (height > max_height)
8552 {
8553 height = max_height;
8554 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
8555 move_it_vertically_backward (&it, (height - 1) * unit);
8556 start = it.current.pos;
8557 }
8558 else
8559 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
8560 SET_MARKER_FROM_TEXT_POS (w->start, start);
8561
8562 if (EQ (Vresize_mini_windows, Qgrow_only))
8563 {
8564 /* Let it grow only, until we display an empty message, in which
8565 case the window shrinks again. */
8566 if (height > WINDOW_TOTAL_LINES (w))
8567 {
8568 int old_height = WINDOW_TOTAL_LINES (w);
8569 freeze_window_starts (f, 1);
8570 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8571 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8572 }
8573 else if (height < WINDOW_TOTAL_LINES (w)
8574 && (exact_p || BEGV == ZV))
8575 {
8576 int old_height = WINDOW_TOTAL_LINES (w);
8577 freeze_window_starts (f, 0);
8578 shrink_mini_window (w);
8579 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8580 }
8581 }
8582 else
8583 {
8584 /* Always resize to exact size needed. */
8585 if (height > WINDOW_TOTAL_LINES (w))
8586 {
8587 int old_height = WINDOW_TOTAL_LINES (w);
8588 freeze_window_starts (f, 1);
8589 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8590 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8591 }
8592 else if (height < WINDOW_TOTAL_LINES (w))
8593 {
8594 int old_height = WINDOW_TOTAL_LINES (w);
8595 freeze_window_starts (f, 0);
8596 shrink_mini_window (w);
8597
8598 if (height)
8599 {
8600 freeze_window_starts (f, 1);
8601 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8602 }
8603
8604 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8605 }
8606 }
8607
8608 if (old_current_buffer)
8609 set_buffer_internal (old_current_buffer);
8610 }
8611
8612 return window_height_changed_p;
8613 }
8614
8615
8616 /* Value is the current message, a string, or nil if there is no
8617 current message. */
8618
8619 Lisp_Object
8620 current_message ()
8621 {
8622 Lisp_Object msg;
8623
8624 if (NILP (echo_area_buffer[0]))
8625 msg = Qnil;
8626 else
8627 {
8628 with_echo_area_buffer (0, 0, current_message_1,
8629 (EMACS_INT) &msg, Qnil, 0, 0);
8630 if (NILP (msg))
8631 echo_area_buffer[0] = Qnil;
8632 }
8633
8634 return msg;
8635 }
8636
8637
8638 static int
8639 current_message_1 (a1, a2, a3, a4)
8640 EMACS_INT a1;
8641 Lisp_Object a2;
8642 EMACS_INT a3, a4;
8643 {
8644 Lisp_Object *msg = (Lisp_Object *) a1;
8645
8646 if (Z > BEG)
8647 *msg = make_buffer_string (BEG, Z, 1);
8648 else
8649 *msg = Qnil;
8650 return 0;
8651 }
8652
8653
8654 /* Push the current message on Vmessage_stack for later restauration
8655 by restore_message. Value is non-zero if the current message isn't
8656 empty. This is a relatively infrequent operation, so it's not
8657 worth optimizing. */
8658
8659 int
8660 push_message ()
8661 {
8662 Lisp_Object msg;
8663 msg = current_message ();
8664 Vmessage_stack = Fcons (msg, Vmessage_stack);
8665 return STRINGP (msg);
8666 }
8667
8668
8669 /* Restore message display from the top of Vmessage_stack. */
8670
8671 void
8672 restore_message ()
8673 {
8674 Lisp_Object msg;
8675
8676 xassert (CONSP (Vmessage_stack));
8677 msg = XCAR (Vmessage_stack);
8678 if (STRINGP (msg))
8679 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
8680 else
8681 message3_nolog (msg, 0, 0);
8682 }
8683
8684
8685 /* Handler for record_unwind_protect calling pop_message. */
8686
8687 Lisp_Object
8688 pop_message_unwind (dummy)
8689 Lisp_Object dummy;
8690 {
8691 pop_message ();
8692 return Qnil;
8693 }
8694
8695 /* Pop the top-most entry off Vmessage_stack. */
8696
8697 void
8698 pop_message ()
8699 {
8700 xassert (CONSP (Vmessage_stack));
8701 Vmessage_stack = XCDR (Vmessage_stack);
8702 }
8703
8704
8705 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
8706 exits. If the stack is not empty, we have a missing pop_message
8707 somewhere. */
8708
8709 void
8710 check_message_stack ()
8711 {
8712 if (!NILP (Vmessage_stack))
8713 abort ();
8714 }
8715
8716
8717 /* Truncate to NCHARS what will be displayed in the echo area the next
8718 time we display it---but don't redisplay it now. */
8719
8720 void
8721 truncate_echo_area (nchars)
8722 int nchars;
8723 {
8724 if (nchars == 0)
8725 echo_area_buffer[0] = Qnil;
8726 /* A null message buffer means that the frame hasn't really been
8727 initialized yet. Error messages get reported properly by
8728 cmd_error, so this must be just an informative message; toss it. */
8729 else if (!noninteractive
8730 && INTERACTIVE
8731 && !NILP (echo_area_buffer[0]))
8732 {
8733 struct frame *sf = SELECTED_FRAME ();
8734 if (FRAME_MESSAGE_BUF (sf))
8735 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
8736 }
8737 }
8738
8739
8740 /* Helper function for truncate_echo_area. Truncate the current
8741 message to at most NCHARS characters. */
8742
8743 static int
8744 truncate_message_1 (nchars, a2, a3, a4)
8745 EMACS_INT nchars;
8746 Lisp_Object a2;
8747 EMACS_INT a3, a4;
8748 {
8749 if (BEG + nchars < Z)
8750 del_range (BEG + nchars, Z);
8751 if (Z == BEG)
8752 echo_area_buffer[0] = Qnil;
8753 return 0;
8754 }
8755
8756
8757 /* Set the current message to a substring of S or STRING.
8758
8759 If STRING is a Lisp string, set the message to the first NBYTES
8760 bytes from STRING. NBYTES zero means use the whole string. If
8761 STRING is multibyte, the message will be displayed multibyte.
8762
8763 If S is not null, set the message to the first LEN bytes of S. LEN
8764 zero means use the whole string. MULTIBYTE_P non-zero means S is
8765 multibyte. Display the message multibyte in that case.
8766
8767 Doesn't GC, as with_echo_area_buffer binds Qinhibit_modification_hooks
8768 to t before calling set_message_1 (which calls insert).
8769 */
8770
8771 void
8772 set_message (s, string, nbytes, multibyte_p)
8773 const char *s;
8774 Lisp_Object string;
8775 int nbytes, multibyte_p;
8776 {
8777 message_enable_multibyte
8778 = ((s && multibyte_p)
8779 || (STRINGP (string) && STRING_MULTIBYTE (string)));
8780
8781 with_echo_area_buffer (0, -1, set_message_1,
8782 (EMACS_INT) s, string, nbytes, multibyte_p);
8783 message_buf_print = 0;
8784 help_echo_showing_p = 0;
8785 }
8786
8787
8788 /* Helper function for set_message. Arguments have the same meaning
8789 as there, with A1 corresponding to S and A2 corresponding to STRING
8790 This function is called with the echo area buffer being
8791 current. */
8792
8793 static int
8794 set_message_1 (a1, a2, nbytes, multibyte_p)
8795 EMACS_INT a1;
8796 Lisp_Object a2;
8797 EMACS_INT nbytes, multibyte_p;
8798 {
8799 const char *s = (const char *) a1;
8800 Lisp_Object string = a2;
8801
8802 /* Change multibyteness of the echo buffer appropriately. */
8803 if (message_enable_multibyte
8804 != !NILP (current_buffer->enable_multibyte_characters))
8805 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
8806
8807 current_buffer->truncate_lines = message_truncate_lines ? Qt : Qnil;
8808
8809 /* Insert new message at BEG. */
8810 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
8811
8812 if (STRINGP (string))
8813 {
8814 int nchars;
8815
8816 if (nbytes == 0)
8817 nbytes = SBYTES (string);
8818 nchars = string_byte_to_char (string, nbytes);
8819
8820 /* This function takes care of single/multibyte conversion. We
8821 just have to ensure that the echo area buffer has the right
8822 setting of enable_multibyte_characters. */
8823 insert_from_string (string, 0, 0, nchars, nbytes, 1);
8824 }
8825 else if (s)
8826 {
8827 if (nbytes == 0)
8828 nbytes = strlen (s);
8829
8830 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
8831 {
8832 /* Convert from multi-byte to single-byte. */
8833 int i, c, n;
8834 unsigned char work[1];
8835
8836 /* Convert a multibyte string to single-byte. */
8837 for (i = 0; i < nbytes; i += n)
8838 {
8839 c = string_char_and_length (s + i, nbytes - i, &n);
8840 work[0] = (ASCII_CHAR_P (c)
8841 ? c
8842 : multibyte_char_to_unibyte (c, Qnil));
8843 insert_1_both (work, 1, 1, 1, 0, 0);
8844 }
8845 }
8846 else if (!multibyte_p
8847 && !NILP (current_buffer->enable_multibyte_characters))
8848 {
8849 /* Convert from single-byte to multi-byte. */
8850 int i, c, n;
8851 const unsigned char *msg = (const unsigned char *) s;
8852 unsigned char str[MAX_MULTIBYTE_LENGTH];
8853
8854 /* Convert a single-byte string to multibyte. */
8855 for (i = 0; i < nbytes; i++)
8856 {
8857 c = msg[i];
8858 c = unibyte_char_to_multibyte (c);
8859 n = CHAR_STRING (c, str);
8860 insert_1_both (str, 1, n, 1, 0, 0);
8861 }
8862 }
8863 else
8864 insert_1 (s, nbytes, 1, 0, 0);
8865 }
8866
8867 return 0;
8868 }
8869
8870
8871 /* Clear messages. CURRENT_P non-zero means clear the current
8872 message. LAST_DISPLAYED_P non-zero means clear the message
8873 last displayed. */
8874
8875 void
8876 clear_message (current_p, last_displayed_p)
8877 int current_p, last_displayed_p;
8878 {
8879 if (current_p)
8880 {
8881 echo_area_buffer[0] = Qnil;
8882 message_cleared_p = 1;
8883 }
8884
8885 if (last_displayed_p)
8886 echo_area_buffer[1] = Qnil;
8887
8888 message_buf_print = 0;
8889 }
8890
8891 /* Clear garbaged frames.
8892
8893 This function is used where the old redisplay called
8894 redraw_garbaged_frames which in turn called redraw_frame which in
8895 turn called clear_frame. The call to clear_frame was a source of
8896 flickering. I believe a clear_frame is not necessary. It should
8897 suffice in the new redisplay to invalidate all current matrices,
8898 and ensure a complete redisplay of all windows. */
8899
8900 static void
8901 clear_garbaged_frames ()
8902 {
8903 if (frame_garbaged)
8904 {
8905 Lisp_Object tail, frame;
8906 int changed_count = 0;
8907
8908 FOR_EACH_FRAME (tail, frame)
8909 {
8910 struct frame *f = XFRAME (frame);
8911
8912 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
8913 {
8914 if (f->resized_p)
8915 {
8916 Fredraw_frame (frame);
8917 f->force_flush_display_p = 1;
8918 }
8919 clear_current_matrices (f);
8920 changed_count++;
8921 f->garbaged = 0;
8922 f->resized_p = 0;
8923 }
8924 }
8925
8926 frame_garbaged = 0;
8927 if (changed_count)
8928 ++windows_or_buffers_changed;
8929 }
8930 }
8931
8932
8933 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
8934 is non-zero update selected_frame. Value is non-zero if the
8935 mini-windows height has been changed. */
8936
8937 static int
8938 echo_area_display (update_frame_p)
8939 int update_frame_p;
8940 {
8941 Lisp_Object mini_window;
8942 struct window *w;
8943 struct frame *f;
8944 int window_height_changed_p = 0;
8945 struct frame *sf = SELECTED_FRAME ();
8946
8947 mini_window = FRAME_MINIBUF_WINDOW (sf);
8948 w = XWINDOW (mini_window);
8949 f = XFRAME (WINDOW_FRAME (w));
8950
8951 /* Don't display if frame is invisible or not yet initialized. */
8952 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
8953 return 0;
8954
8955 /* The terminal frame is used as the first Emacs frame on the Mac OS. */
8956 #ifndef MAC_OS8
8957 #ifdef HAVE_WINDOW_SYSTEM
8958 /* When Emacs starts, selected_frame may be the initial terminal
8959 frame. If we let this through, a message would be displayed on
8960 the terminal. */
8961 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
8962 return 0;
8963 #endif /* HAVE_WINDOW_SYSTEM */
8964 #endif
8965
8966 /* Redraw garbaged frames. */
8967 if (frame_garbaged)
8968 clear_garbaged_frames ();
8969
8970 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
8971 {
8972 echo_area_window = mini_window;
8973 window_height_changed_p = display_echo_area (w);
8974 w->must_be_updated_p = 1;
8975
8976 /* Update the display, unless called from redisplay_internal.
8977 Also don't update the screen during redisplay itself. The
8978 update will happen at the end of redisplay, and an update
8979 here could cause confusion. */
8980 if (update_frame_p && !redisplaying_p)
8981 {
8982 int n = 0;
8983
8984 /* If the display update has been interrupted by pending
8985 input, update mode lines in the frame. Due to the
8986 pending input, it might have been that redisplay hasn't
8987 been called, so that mode lines above the echo area are
8988 garbaged. This looks odd, so we prevent it here. */
8989 if (!display_completed)
8990 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
8991
8992 if (window_height_changed_p
8993 /* Don't do this if Emacs is shutting down. Redisplay
8994 needs to run hooks. */
8995 && !NILP (Vrun_hooks))
8996 {
8997 /* Must update other windows. Likewise as in other
8998 cases, don't let this update be interrupted by
8999 pending input. */
9000 int count = SPECPDL_INDEX ();
9001 specbind (Qredisplay_dont_pause, Qt);
9002 windows_or_buffers_changed = 1;
9003 redisplay_internal (0);
9004 unbind_to (count, Qnil);
9005 }
9006 else if (FRAME_WINDOW_P (f) && n == 0)
9007 {
9008 /* Window configuration is the same as before.
9009 Can do with a display update of the echo area,
9010 unless we displayed some mode lines. */
9011 update_single_window (w, 1);
9012 FRAME_RIF (f)->flush_display (f);
9013 }
9014 else
9015 update_frame (f, 1, 1);
9016
9017 /* If cursor is in the echo area, make sure that the next
9018 redisplay displays the minibuffer, so that the cursor will
9019 be replaced with what the minibuffer wants. */
9020 if (cursor_in_echo_area)
9021 ++windows_or_buffers_changed;
9022 }
9023 }
9024 else if (!EQ (mini_window, selected_window))
9025 windows_or_buffers_changed++;
9026
9027 /* Last displayed message is now the current message. */
9028 echo_area_buffer[1] = echo_area_buffer[0];
9029 /* Inform read_char that we're not echoing. */
9030 echo_message_buffer = Qnil;
9031
9032 /* Prevent redisplay optimization in redisplay_internal by resetting
9033 this_line_start_pos. This is done because the mini-buffer now
9034 displays the message instead of its buffer text. */
9035 if (EQ (mini_window, selected_window))
9036 CHARPOS (this_line_start_pos) = 0;
9037
9038 return window_height_changed_p;
9039 }
9040
9041
9042 \f
9043 /***********************************************************************
9044 Mode Lines and Frame Titles
9045 ***********************************************************************/
9046
9047 /* A buffer for constructing non-propertized mode-line strings and
9048 frame titles in it; allocated from the heap in init_xdisp and
9049 resized as needed in store_mode_line_noprop_char. */
9050
9051 static char *mode_line_noprop_buf;
9052
9053 /* The buffer's end, and a current output position in it. */
9054
9055 static char *mode_line_noprop_buf_end;
9056 static char *mode_line_noprop_ptr;
9057
9058 #define MODE_LINE_NOPROP_LEN(start) \
9059 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
9060
9061 static enum {
9062 MODE_LINE_DISPLAY = 0,
9063 MODE_LINE_TITLE,
9064 MODE_LINE_NOPROP,
9065 MODE_LINE_STRING
9066 } mode_line_target;
9067
9068 /* Alist that caches the results of :propertize.
9069 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
9070 static Lisp_Object mode_line_proptrans_alist;
9071
9072 /* List of strings making up the mode-line. */
9073 static Lisp_Object mode_line_string_list;
9074
9075 /* Base face property when building propertized mode line string. */
9076 static Lisp_Object mode_line_string_face;
9077 static Lisp_Object mode_line_string_face_prop;
9078
9079
9080 /* Unwind data for mode line strings */
9081
9082 static Lisp_Object Vmode_line_unwind_vector;
9083
9084 static Lisp_Object
9085 format_mode_line_unwind_data (obuf, save_proptrans)
9086 struct buffer *obuf;
9087 {
9088 Lisp_Object vector;
9089
9090 /* Reduce consing by keeping one vector in
9091 Vwith_echo_area_save_vector. */
9092 vector = Vmode_line_unwind_vector;
9093 Vmode_line_unwind_vector = Qnil;
9094
9095 if (NILP (vector))
9096 vector = Fmake_vector (make_number (7), Qnil);
9097
9098 AREF (vector, 0) = make_number (mode_line_target);
9099 AREF (vector, 1) = make_number (MODE_LINE_NOPROP_LEN (0));
9100 AREF (vector, 2) = mode_line_string_list;
9101 AREF (vector, 3) = (save_proptrans ? mode_line_proptrans_alist : Qt);
9102 AREF (vector, 4) = mode_line_string_face;
9103 AREF (vector, 5) = mode_line_string_face_prop;
9104
9105 if (obuf)
9106 XSETBUFFER (AREF (vector, 6), obuf);
9107 else
9108 AREF (vector, 6) = Qnil;
9109
9110 return vector;
9111 }
9112
9113 static Lisp_Object
9114 unwind_format_mode_line (vector)
9115 Lisp_Object vector;
9116 {
9117 mode_line_target = XINT (AREF (vector, 0));
9118 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
9119 mode_line_string_list = AREF (vector, 2);
9120 if (! EQ (AREF (vector, 3), Qt))
9121 mode_line_proptrans_alist = AREF (vector, 3);
9122 mode_line_string_face = AREF (vector, 4);
9123 mode_line_string_face_prop = AREF (vector, 5);
9124
9125 if (!NILP (AREF (vector, 6)))
9126 {
9127 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
9128 AREF (vector, 6) = Qnil;
9129 }
9130
9131 Vmode_line_unwind_vector = vector;
9132 return Qnil;
9133 }
9134
9135
9136 /* Store a single character C for the frame title in mode_line_noprop_buf.
9137 Re-allocate mode_line_noprop_buf if necessary. */
9138
9139 static void
9140 #ifdef PROTOTYPES
9141 store_mode_line_noprop_char (char c)
9142 #else
9143 store_mode_line_noprop_char (c)
9144 char c;
9145 #endif
9146 {
9147 /* If output position has reached the end of the allocated buffer,
9148 double the buffer's size. */
9149 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
9150 {
9151 int len = MODE_LINE_NOPROP_LEN (0);
9152 int new_size = 2 * len * sizeof *mode_line_noprop_buf;
9153 mode_line_noprop_buf = (char *) xrealloc (mode_line_noprop_buf, new_size);
9154 mode_line_noprop_buf_end = mode_line_noprop_buf + new_size;
9155 mode_line_noprop_ptr = mode_line_noprop_buf + len;
9156 }
9157
9158 *mode_line_noprop_ptr++ = c;
9159 }
9160
9161
9162 /* Store part of a frame title in mode_line_noprop_buf, beginning at
9163 mode_line_noprop_ptr. STR is the string to store. Do not copy
9164 characters that yield more columns than PRECISION; PRECISION <= 0
9165 means copy the whole string. Pad with spaces until FIELD_WIDTH
9166 number of characters have been copied; FIELD_WIDTH <= 0 means don't
9167 pad. Called from display_mode_element when it is used to build a
9168 frame title. */
9169
9170 static int
9171 store_mode_line_noprop (str, field_width, precision)
9172 const unsigned char *str;
9173 int field_width, precision;
9174 {
9175 int n = 0;
9176 int dummy, nbytes;
9177
9178 /* Copy at most PRECISION chars from STR. */
9179 nbytes = strlen (str);
9180 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
9181 while (nbytes--)
9182 store_mode_line_noprop_char (*str++);
9183
9184 /* Fill up with spaces until FIELD_WIDTH reached. */
9185 while (field_width > 0
9186 && n < field_width)
9187 {
9188 store_mode_line_noprop_char (' ');
9189 ++n;
9190 }
9191
9192 return n;
9193 }
9194
9195 /***********************************************************************
9196 Frame Titles
9197 ***********************************************************************/
9198
9199 #ifdef HAVE_WINDOW_SYSTEM
9200
9201 /* Set the title of FRAME, if it has changed. The title format is
9202 Vicon_title_format if FRAME is iconified, otherwise it is
9203 frame_title_format. */
9204
9205 static void
9206 x_consider_frame_title (frame)
9207 Lisp_Object frame;
9208 {
9209 struct frame *f = XFRAME (frame);
9210
9211 if (FRAME_WINDOW_P (f)
9212 || FRAME_MINIBUF_ONLY_P (f)
9213 || f->explicit_name)
9214 {
9215 /* Do we have more than one visible frame on this X display? */
9216 Lisp_Object tail;
9217 Lisp_Object fmt;
9218 int title_start;
9219 char *title;
9220 int len;
9221 struct it it;
9222 int count = SPECPDL_INDEX ();
9223
9224 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
9225 {
9226 Lisp_Object other_frame = XCAR (tail);
9227 struct frame *tf = XFRAME (other_frame);
9228
9229 if (tf != f
9230 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
9231 && !FRAME_MINIBUF_ONLY_P (tf)
9232 && !EQ (other_frame, tip_frame)
9233 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
9234 break;
9235 }
9236
9237 /* Set global variable indicating that multiple frames exist. */
9238 multiple_frames = CONSP (tail);
9239
9240 /* Switch to the buffer of selected window of the frame. Set up
9241 mode_line_target so that display_mode_element will output into
9242 mode_line_noprop_buf; then display the title. */
9243 record_unwind_protect (unwind_format_mode_line,
9244 format_mode_line_unwind_data (current_buffer, 0));
9245
9246 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
9247 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
9248
9249 mode_line_target = MODE_LINE_TITLE;
9250 title_start = MODE_LINE_NOPROP_LEN (0);
9251 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
9252 NULL, DEFAULT_FACE_ID);
9253 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
9254 len = MODE_LINE_NOPROP_LEN (title_start);
9255 title = mode_line_noprop_buf + title_start;
9256 unbind_to (count, Qnil);
9257
9258 /* Set the title only if it's changed. This avoids consing in
9259 the common case where it hasn't. (If it turns out that we've
9260 already wasted too much time by walking through the list with
9261 display_mode_element, then we might need to optimize at a
9262 higher level than this.) */
9263 if (! STRINGP (f->name)
9264 || SBYTES (f->name) != len
9265 || bcmp (title, SDATA (f->name), len) != 0)
9266 x_implicitly_set_name (f, make_string (title, len), Qnil);
9267 }
9268 }
9269
9270 #endif /* not HAVE_WINDOW_SYSTEM */
9271
9272
9273
9274 \f
9275 /***********************************************************************
9276 Menu Bars
9277 ***********************************************************************/
9278
9279
9280 /* Prepare for redisplay by updating menu-bar item lists when
9281 appropriate. This can call eval. */
9282
9283 void
9284 prepare_menu_bars ()
9285 {
9286 int all_windows;
9287 struct gcpro gcpro1, gcpro2;
9288 struct frame *f;
9289 Lisp_Object tooltip_frame;
9290
9291 #ifdef HAVE_WINDOW_SYSTEM
9292 tooltip_frame = tip_frame;
9293 #else
9294 tooltip_frame = Qnil;
9295 #endif
9296
9297 /* Update all frame titles based on their buffer names, etc. We do
9298 this before the menu bars so that the buffer-menu will show the
9299 up-to-date frame titles. */
9300 #ifdef HAVE_WINDOW_SYSTEM
9301 if (windows_or_buffers_changed || update_mode_lines)
9302 {
9303 Lisp_Object tail, frame;
9304
9305 FOR_EACH_FRAME (tail, frame)
9306 {
9307 f = XFRAME (frame);
9308 if (!EQ (frame, tooltip_frame)
9309 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
9310 x_consider_frame_title (frame);
9311 }
9312 }
9313 #endif /* HAVE_WINDOW_SYSTEM */
9314
9315 /* Update the menu bar item lists, if appropriate. This has to be
9316 done before any actual redisplay or generation of display lines. */
9317 all_windows = (update_mode_lines
9318 || buffer_shared > 1
9319 || windows_or_buffers_changed);
9320 if (all_windows)
9321 {
9322 Lisp_Object tail, frame;
9323 int count = SPECPDL_INDEX ();
9324 /* 1 means that update_menu_bar has run its hooks
9325 so any further calls to update_menu_bar shouldn't do so again. */
9326 int menu_bar_hooks_run = 0;
9327
9328 record_unwind_save_match_data ();
9329
9330 FOR_EACH_FRAME (tail, frame)
9331 {
9332 f = XFRAME (frame);
9333
9334 /* Ignore tooltip frame. */
9335 if (EQ (frame, tooltip_frame))
9336 continue;
9337
9338 /* If a window on this frame changed size, report that to
9339 the user and clear the size-change flag. */
9340 if (FRAME_WINDOW_SIZES_CHANGED (f))
9341 {
9342 Lisp_Object functions;
9343
9344 /* Clear flag first in case we get an error below. */
9345 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
9346 functions = Vwindow_size_change_functions;
9347 GCPRO2 (tail, functions);
9348
9349 while (CONSP (functions))
9350 {
9351 call1 (XCAR (functions), frame);
9352 functions = XCDR (functions);
9353 }
9354 UNGCPRO;
9355 }
9356
9357 GCPRO1 (tail);
9358 menu_bar_hooks_run = update_menu_bar (f, 0, menu_bar_hooks_run);
9359 #ifdef HAVE_WINDOW_SYSTEM
9360 update_tool_bar (f, 0);
9361 #ifdef MAC_OS
9362 mac_update_title_bar (f, 0);
9363 #endif
9364 #endif
9365 UNGCPRO;
9366 }
9367
9368 unbind_to (count, Qnil);
9369 }
9370 else
9371 {
9372 struct frame *sf = SELECTED_FRAME ();
9373 update_menu_bar (sf, 1, 0);
9374 #ifdef HAVE_WINDOW_SYSTEM
9375 update_tool_bar (sf, 1);
9376 #ifdef MAC_OS
9377 mac_update_title_bar (sf, 1);
9378 #endif
9379 #endif
9380 }
9381
9382 /* Motif needs this. See comment in xmenu.c. Turn it off when
9383 pending_menu_activation is not defined. */
9384 #ifdef USE_X_TOOLKIT
9385 pending_menu_activation = 0;
9386 #endif
9387 }
9388
9389
9390 /* Update the menu bar item list for frame F. This has to be done
9391 before we start to fill in any display lines, because it can call
9392 eval.
9393
9394 If SAVE_MATCH_DATA is non-zero, we must save and restore it here.
9395
9396 If HOOKS_RUN is 1, that means a previous call to update_menu_bar
9397 already ran the menu bar hooks for this redisplay, so there
9398 is no need to run them again. The return value is the
9399 updated value of this flag, to pass to the next call. */
9400
9401 static int
9402 update_menu_bar (f, save_match_data, hooks_run)
9403 struct frame *f;
9404 int save_match_data;
9405 int hooks_run;
9406 {
9407 Lisp_Object window;
9408 register struct window *w;
9409
9410 /* If called recursively during a menu update, do nothing. This can
9411 happen when, for instance, an activate-menubar-hook causes a
9412 redisplay. */
9413 if (inhibit_menubar_update)
9414 return hooks_run;
9415
9416 window = FRAME_SELECTED_WINDOW (f);
9417 w = XWINDOW (window);
9418
9419 #if 0 /* The if statement below this if statement used to include the
9420 condition !NILP (w->update_mode_line), rather than using
9421 update_mode_lines directly, and this if statement may have
9422 been added to make that condition work. Now the if
9423 statement below matches its comment, this isn't needed. */
9424 if (update_mode_lines)
9425 w->update_mode_line = Qt;
9426 #endif
9427
9428 if (FRAME_WINDOW_P (f)
9429 ?
9430 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
9431 || defined (USE_GTK)
9432 FRAME_EXTERNAL_MENU_BAR (f)
9433 #else
9434 FRAME_MENU_BAR_LINES (f) > 0
9435 #endif
9436 : FRAME_MENU_BAR_LINES (f) > 0)
9437 {
9438 /* If the user has switched buffers or windows, we need to
9439 recompute to reflect the new bindings. But we'll
9440 recompute when update_mode_lines is set too; that means
9441 that people can use force-mode-line-update to request
9442 that the menu bar be recomputed. The adverse effect on
9443 the rest of the redisplay algorithm is about the same as
9444 windows_or_buffers_changed anyway. */
9445 if (windows_or_buffers_changed
9446 /* This used to test w->update_mode_line, but we believe
9447 there is no need to recompute the menu in that case. */
9448 || update_mode_lines
9449 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
9450 < BUF_MODIFF (XBUFFER (w->buffer)))
9451 != !NILP (w->last_had_star))
9452 || ((!NILP (Vtransient_mark_mode)
9453 && !NILP (XBUFFER (w->buffer)->mark_active))
9454 != !NILP (w->region_showing)))
9455 {
9456 struct buffer *prev = current_buffer;
9457 int count = SPECPDL_INDEX ();
9458
9459 specbind (Qinhibit_menubar_update, Qt);
9460
9461 set_buffer_internal_1 (XBUFFER (w->buffer));
9462 if (save_match_data)
9463 record_unwind_save_match_data ();
9464 if (NILP (Voverriding_local_map_menu_flag))
9465 {
9466 specbind (Qoverriding_terminal_local_map, Qnil);
9467 specbind (Qoverriding_local_map, Qnil);
9468 }
9469
9470 if (!hooks_run)
9471 {
9472 /* Run the Lucid hook. */
9473 safe_run_hooks (Qactivate_menubar_hook);
9474
9475 /* If it has changed current-menubar from previous value,
9476 really recompute the menu-bar from the value. */
9477 if (! NILP (Vlucid_menu_bar_dirty_flag))
9478 call0 (Qrecompute_lucid_menubar);
9479
9480 safe_run_hooks (Qmenu_bar_update_hook);
9481
9482 hooks_run = 1;
9483 }
9484
9485 XSETFRAME (Vmenu_updating_frame, f);
9486 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
9487
9488 /* Redisplay the menu bar in case we changed it. */
9489 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
9490 || defined (USE_GTK)
9491 if (FRAME_WINDOW_P (f))
9492 {
9493 #ifdef MAC_OS
9494 /* All frames on Mac OS share the same menubar. So only
9495 the selected frame should be allowed to set it. */
9496 if (f == SELECTED_FRAME ())
9497 #endif
9498 set_frame_menubar (f, 0, 0);
9499 }
9500 else
9501 /* On a terminal screen, the menu bar is an ordinary screen
9502 line, and this makes it get updated. */
9503 w->update_mode_line = Qt;
9504 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
9505 /* In the non-toolkit version, the menu bar is an ordinary screen
9506 line, and this makes it get updated. */
9507 w->update_mode_line = Qt;
9508 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
9509
9510 unbind_to (count, Qnil);
9511 set_buffer_internal_1 (prev);
9512 }
9513 }
9514
9515 return hooks_run;
9516 }
9517
9518
9519 \f
9520 /***********************************************************************
9521 Output Cursor
9522 ***********************************************************************/
9523
9524 #ifdef HAVE_WINDOW_SYSTEM
9525
9526 /* EXPORT:
9527 Nominal cursor position -- where to draw output.
9528 HPOS and VPOS are window relative glyph matrix coordinates.
9529 X and Y are window relative pixel coordinates. */
9530
9531 struct cursor_pos output_cursor;
9532
9533
9534 /* EXPORT:
9535 Set the global variable output_cursor to CURSOR. All cursor
9536 positions are relative to updated_window. */
9537
9538 void
9539 set_output_cursor (cursor)
9540 struct cursor_pos *cursor;
9541 {
9542 output_cursor.hpos = cursor->hpos;
9543 output_cursor.vpos = cursor->vpos;
9544 output_cursor.x = cursor->x;
9545 output_cursor.y = cursor->y;
9546 }
9547
9548
9549 /* EXPORT for RIF:
9550 Set a nominal cursor position.
9551
9552 HPOS and VPOS are column/row positions in a window glyph matrix. X
9553 and Y are window text area relative pixel positions.
9554
9555 If this is done during an update, updated_window will contain the
9556 window that is being updated and the position is the future output
9557 cursor position for that window. If updated_window is null, use
9558 selected_window and display the cursor at the given position. */
9559
9560 void
9561 x_cursor_to (vpos, hpos, y, x)
9562 int vpos, hpos, y, x;
9563 {
9564 struct window *w;
9565
9566 /* If updated_window is not set, work on selected_window. */
9567 if (updated_window)
9568 w = updated_window;
9569 else
9570 w = XWINDOW (selected_window);
9571
9572 /* Set the output cursor. */
9573 output_cursor.hpos = hpos;
9574 output_cursor.vpos = vpos;
9575 output_cursor.x = x;
9576 output_cursor.y = y;
9577
9578 /* If not called as part of an update, really display the cursor.
9579 This will also set the cursor position of W. */
9580 if (updated_window == NULL)
9581 {
9582 BLOCK_INPUT;
9583 display_and_set_cursor (w, 1, hpos, vpos, x, y);
9584 if (FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
9585 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (SELECTED_FRAME ());
9586 UNBLOCK_INPUT;
9587 }
9588 }
9589
9590 #endif /* HAVE_WINDOW_SYSTEM */
9591
9592 \f
9593 /***********************************************************************
9594 Tool-bars
9595 ***********************************************************************/
9596
9597 #ifdef HAVE_WINDOW_SYSTEM
9598
9599 /* Where the mouse was last time we reported a mouse event. */
9600
9601 FRAME_PTR last_mouse_frame;
9602
9603 /* Tool-bar item index of the item on which a mouse button was pressed
9604 or -1. */
9605
9606 int last_tool_bar_item;
9607
9608
9609 /* Update the tool-bar item list for frame F. This has to be done
9610 before we start to fill in any display lines. Called from
9611 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
9612 and restore it here. */
9613
9614 static void
9615 update_tool_bar (f, save_match_data)
9616 struct frame *f;
9617 int save_match_data;
9618 {
9619 #if defined (USE_GTK) || USE_MAC_TOOLBAR
9620 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
9621 #else
9622 int do_update = WINDOWP (f->tool_bar_window)
9623 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
9624 #endif
9625
9626 if (do_update)
9627 {
9628 Lisp_Object window;
9629 struct window *w;
9630
9631 window = FRAME_SELECTED_WINDOW (f);
9632 w = XWINDOW (window);
9633
9634 /* If the user has switched buffers or windows, we need to
9635 recompute to reflect the new bindings. But we'll
9636 recompute when update_mode_lines is set too; that means
9637 that people can use force-mode-line-update to request
9638 that the menu bar be recomputed. The adverse effect on
9639 the rest of the redisplay algorithm is about the same as
9640 windows_or_buffers_changed anyway. */
9641 if (windows_or_buffers_changed
9642 || !NILP (w->update_mode_line)
9643 || update_mode_lines
9644 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
9645 < BUF_MODIFF (XBUFFER (w->buffer)))
9646 != !NILP (w->last_had_star))
9647 || ((!NILP (Vtransient_mark_mode)
9648 && !NILP (XBUFFER (w->buffer)->mark_active))
9649 != !NILP (w->region_showing)))
9650 {
9651 struct buffer *prev = current_buffer;
9652 int count = SPECPDL_INDEX ();
9653 Lisp_Object new_tool_bar;
9654 int new_n_tool_bar;
9655 struct gcpro gcpro1;
9656
9657 /* Set current_buffer to the buffer of the selected
9658 window of the frame, so that we get the right local
9659 keymaps. */
9660 set_buffer_internal_1 (XBUFFER (w->buffer));
9661
9662 /* Save match data, if we must. */
9663 if (save_match_data)
9664 record_unwind_save_match_data ();
9665
9666 /* Make sure that we don't accidentally use bogus keymaps. */
9667 if (NILP (Voverriding_local_map_menu_flag))
9668 {
9669 specbind (Qoverriding_terminal_local_map, Qnil);
9670 specbind (Qoverriding_local_map, Qnil);
9671 }
9672
9673 GCPRO1 (new_tool_bar);
9674
9675 /* Build desired tool-bar items from keymaps. */
9676 new_tool_bar = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
9677 &new_n_tool_bar);
9678
9679 /* Redisplay the tool-bar if we changed it. */
9680 if (new_n_tool_bar != f->n_tool_bar_items
9681 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
9682 {
9683 /* Redisplay that happens asynchronously due to an expose event
9684 may access f->tool_bar_items. Make sure we update both
9685 variables within BLOCK_INPUT so no such event interrupts. */
9686 BLOCK_INPUT;
9687 f->tool_bar_items = new_tool_bar;
9688 f->n_tool_bar_items = new_n_tool_bar;
9689 w->update_mode_line = Qt;
9690 UNBLOCK_INPUT;
9691 }
9692
9693 UNGCPRO;
9694
9695 unbind_to (count, Qnil);
9696 set_buffer_internal_1 (prev);
9697 }
9698 }
9699 }
9700
9701
9702 /* Set F->desired_tool_bar_string to a Lisp string representing frame
9703 F's desired tool-bar contents. F->tool_bar_items must have
9704 been set up previously by calling prepare_menu_bars. */
9705
9706 static void
9707 build_desired_tool_bar_string (f)
9708 struct frame *f;
9709 {
9710 int i, size, size_needed;
9711 struct gcpro gcpro1, gcpro2, gcpro3;
9712 Lisp_Object image, plist, props;
9713
9714 image = plist = props = Qnil;
9715 GCPRO3 (image, plist, props);
9716
9717 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
9718 Otherwise, make a new string. */
9719
9720 /* The size of the string we might be able to reuse. */
9721 size = (STRINGP (f->desired_tool_bar_string)
9722 ? SCHARS (f->desired_tool_bar_string)
9723 : 0);
9724
9725 /* We need one space in the string for each image. */
9726 size_needed = f->n_tool_bar_items;
9727
9728 /* Reuse f->desired_tool_bar_string, if possible. */
9729 if (size < size_needed || NILP (f->desired_tool_bar_string))
9730 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
9731 make_number (' '));
9732 else
9733 {
9734 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
9735 Fremove_text_properties (make_number (0), make_number (size),
9736 props, f->desired_tool_bar_string);
9737 }
9738
9739 /* Put a `display' property on the string for the images to display,
9740 put a `menu_item' property on tool-bar items with a value that
9741 is the index of the item in F's tool-bar item vector. */
9742 for (i = 0; i < f->n_tool_bar_items; ++i)
9743 {
9744 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
9745
9746 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
9747 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
9748 int hmargin, vmargin, relief, idx, end;
9749 extern Lisp_Object QCrelief, QCmargin, QCconversion;
9750
9751 /* If image is a vector, choose the image according to the
9752 button state. */
9753 image = PROP (TOOL_BAR_ITEM_IMAGES);
9754 if (VECTORP (image))
9755 {
9756 if (enabled_p)
9757 idx = (selected_p
9758 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
9759 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
9760 else
9761 idx = (selected_p
9762 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
9763 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
9764
9765 xassert (ASIZE (image) >= idx);
9766 image = AREF (image, idx);
9767 }
9768 else
9769 idx = -1;
9770
9771 /* Ignore invalid image specifications. */
9772 if (!valid_image_p (image))
9773 continue;
9774
9775 /* Display the tool-bar button pressed, or depressed. */
9776 plist = Fcopy_sequence (XCDR (image));
9777
9778 /* Compute margin and relief to draw. */
9779 relief = (tool_bar_button_relief >= 0
9780 ? tool_bar_button_relief
9781 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
9782 hmargin = vmargin = relief;
9783
9784 if (INTEGERP (Vtool_bar_button_margin)
9785 && XINT (Vtool_bar_button_margin) > 0)
9786 {
9787 hmargin += XFASTINT (Vtool_bar_button_margin);
9788 vmargin += XFASTINT (Vtool_bar_button_margin);
9789 }
9790 else if (CONSP (Vtool_bar_button_margin))
9791 {
9792 if (INTEGERP (XCAR (Vtool_bar_button_margin))
9793 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
9794 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
9795
9796 if (INTEGERP (XCDR (Vtool_bar_button_margin))
9797 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
9798 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
9799 }
9800
9801 if (auto_raise_tool_bar_buttons_p)
9802 {
9803 /* Add a `:relief' property to the image spec if the item is
9804 selected. */
9805 if (selected_p)
9806 {
9807 plist = Fplist_put (plist, QCrelief, make_number (-relief));
9808 hmargin -= relief;
9809 vmargin -= relief;
9810 }
9811 }
9812 else
9813 {
9814 /* If image is selected, display it pressed, i.e. with a
9815 negative relief. If it's not selected, display it with a
9816 raised relief. */
9817 plist = Fplist_put (plist, QCrelief,
9818 (selected_p
9819 ? make_number (-relief)
9820 : make_number (relief)));
9821 hmargin -= relief;
9822 vmargin -= relief;
9823 }
9824
9825 /* Put a margin around the image. */
9826 if (hmargin || vmargin)
9827 {
9828 if (hmargin == vmargin)
9829 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
9830 else
9831 plist = Fplist_put (plist, QCmargin,
9832 Fcons (make_number (hmargin),
9833 make_number (vmargin)));
9834 }
9835
9836 /* If button is not enabled, and we don't have special images
9837 for the disabled state, make the image appear disabled by
9838 applying an appropriate algorithm to it. */
9839 if (!enabled_p && idx < 0)
9840 plist = Fplist_put (plist, QCconversion, Qdisabled);
9841
9842 /* Put a `display' text property on the string for the image to
9843 display. Put a `menu-item' property on the string that gives
9844 the start of this item's properties in the tool-bar items
9845 vector. */
9846 image = Fcons (Qimage, plist);
9847 props = list4 (Qdisplay, image,
9848 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
9849
9850 /* Let the last image hide all remaining spaces in the tool bar
9851 string. The string can be longer than needed when we reuse a
9852 previous string. */
9853 if (i + 1 == f->n_tool_bar_items)
9854 end = SCHARS (f->desired_tool_bar_string);
9855 else
9856 end = i + 1;
9857 Fadd_text_properties (make_number (i), make_number (end),
9858 props, f->desired_tool_bar_string);
9859 #undef PROP
9860 }
9861
9862 UNGCPRO;
9863 }
9864
9865
9866 /* Display one line of the tool-bar of frame IT->f.
9867
9868 HEIGHT specifies the desired height of the tool-bar line.
9869 If the actual height of the glyph row is less than HEIGHT, the
9870 row's height is increased to HEIGHT, and the icons are centered
9871 vertically in the new height.
9872
9873 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
9874 count a final empty row in case the tool-bar width exactly matches
9875 the window width.
9876 */
9877
9878 static void
9879 display_tool_bar_line (it, height)
9880 struct it *it;
9881 int height;
9882 {
9883 struct glyph_row *row = it->glyph_row;
9884 int max_x = it->last_visible_x;
9885 struct glyph *last;
9886
9887 prepare_desired_row (row);
9888 row->y = it->current_y;
9889
9890 /* Note that this isn't made use of if the face hasn't a box,
9891 so there's no need to check the face here. */
9892 it->start_of_box_run_p = 1;
9893
9894 while (it->current_x < max_x)
9895 {
9896 int x, n_glyphs_before, i, nglyphs;
9897 struct it it_before;
9898
9899 /* Get the next display element. */
9900 if (!get_next_display_element (it))
9901 {
9902 /* Don't count empty row if we are counting needed tool-bar lines. */
9903 if (height < 0 && !it->hpos)
9904 return;
9905 break;
9906 }
9907
9908 /* Produce glyphs. */
9909 n_glyphs_before = row->used[TEXT_AREA];
9910 it_before = *it;
9911
9912 PRODUCE_GLYPHS (it);
9913
9914 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
9915 i = 0;
9916 x = it_before.current_x;
9917 while (i < nglyphs)
9918 {
9919 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
9920
9921 if (x + glyph->pixel_width > max_x)
9922 {
9923 /* Glyph doesn't fit on line. Backtrack. */
9924 row->used[TEXT_AREA] = n_glyphs_before;
9925 *it = it_before;
9926 /* If this is the only glyph on this line, it will never fit on the
9927 toolbar, so skip it. But ensure there is at least one glyph,
9928 so we don't accidentally disable the tool-bar. */
9929 if (n_glyphs_before == 0
9930 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
9931 break;
9932 goto out;
9933 }
9934
9935 ++it->hpos;
9936 x += glyph->pixel_width;
9937 ++i;
9938 }
9939
9940 /* Stop at line ends. */
9941 if (ITERATOR_AT_END_OF_LINE_P (it))
9942 break;
9943
9944 set_iterator_to_next (it, 1);
9945 }
9946
9947 out:;
9948
9949 row->displays_text_p = row->used[TEXT_AREA] != 0;
9950
9951 /* Use default face for the border below the tool bar.
9952
9953 FIXME: When auto-resize-tool-bars is grow-only, there is
9954 no additional border below the possibly empty tool-bar lines.
9955 So to make the extra empty lines look "normal", we have to
9956 use the tool-bar face for the border too. */
9957 if (!row->displays_text_p && !EQ (Vauto_resize_tool_bars, Qgrow_only))
9958 it->face_id = DEFAULT_FACE_ID;
9959
9960 extend_face_to_end_of_line (it);
9961 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
9962 last->right_box_line_p = 1;
9963 if (last == row->glyphs[TEXT_AREA])
9964 last->left_box_line_p = 1;
9965
9966 /* Make line the desired height and center it vertically. */
9967 if ((height -= it->max_ascent + it->max_descent) > 0)
9968 {
9969 /* Don't add more than one line height. */
9970 height %= FRAME_LINE_HEIGHT (it->f);
9971 it->max_ascent += height / 2;
9972 it->max_descent += (height + 1) / 2;
9973 }
9974
9975 compute_line_metrics (it);
9976
9977 /* If line is empty, make it occupy the rest of the tool-bar. */
9978 if (!row->displays_text_p)
9979 {
9980 row->height = row->phys_height = it->last_visible_y - row->y;
9981 row->visible_height = row->height;
9982 row->ascent = row->phys_ascent = 0;
9983 row->extra_line_spacing = 0;
9984 }
9985
9986 row->full_width_p = 1;
9987 row->continued_p = 0;
9988 row->truncated_on_left_p = 0;
9989 row->truncated_on_right_p = 0;
9990
9991 it->current_x = it->hpos = 0;
9992 it->current_y += row->height;
9993 ++it->vpos;
9994 ++it->glyph_row;
9995 }
9996
9997
9998 /* Max tool-bar height. */
9999
10000 #define MAX_FRAME_TOOL_BAR_HEIGHT(f) \
10001 ((FRAME_LINE_HEIGHT (f) * FRAME_LINES (f)))
10002
10003 /* Value is the number of screen lines needed to make all tool-bar
10004 items of frame F visible. The number of actual rows needed is
10005 returned in *N_ROWS if non-NULL. */
10006
10007 static int
10008 tool_bar_lines_needed (f, n_rows)
10009 struct frame *f;
10010 int *n_rows;
10011 {
10012 struct window *w = XWINDOW (f->tool_bar_window);
10013 struct it it;
10014 /* tool_bar_lines_needed is called from redisplay_tool_bar after building
10015 the desired matrix, so use (unused) mode-line row as temporary row to
10016 avoid destroying the first tool-bar row. */
10017 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
10018
10019 /* Initialize an iterator for iteration over
10020 F->desired_tool_bar_string in the tool-bar window of frame F. */
10021 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
10022 it.first_visible_x = 0;
10023 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
10024 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
10025
10026 while (!ITERATOR_AT_END_P (&it))
10027 {
10028 clear_glyph_row (temp_row);
10029 it.glyph_row = temp_row;
10030 display_tool_bar_line (&it, -1);
10031 }
10032 clear_glyph_row (temp_row);
10033
10034 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
10035 if (n_rows)
10036 *n_rows = it.vpos > 0 ? it.vpos : -1;
10037
10038 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
10039 }
10040
10041
10042 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
10043 0, 1, 0,
10044 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
10045 (frame)
10046 Lisp_Object frame;
10047 {
10048 struct frame *f;
10049 struct window *w;
10050 int nlines = 0;
10051
10052 if (NILP (frame))
10053 frame = selected_frame;
10054 else
10055 CHECK_FRAME (frame);
10056 f = XFRAME (frame);
10057
10058 if (WINDOWP (f->tool_bar_window)
10059 || (w = XWINDOW (f->tool_bar_window),
10060 WINDOW_TOTAL_LINES (w) > 0))
10061 {
10062 update_tool_bar (f, 1);
10063 if (f->n_tool_bar_items)
10064 {
10065 build_desired_tool_bar_string (f);
10066 nlines = tool_bar_lines_needed (f, NULL);
10067 }
10068 }
10069
10070 return make_number (nlines);
10071 }
10072
10073
10074 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
10075 height should be changed. */
10076
10077 static int
10078 redisplay_tool_bar (f)
10079 struct frame *f;
10080 {
10081 struct window *w;
10082 struct it it;
10083 struct glyph_row *row;
10084
10085 #if defined (USE_GTK) || USE_MAC_TOOLBAR
10086 if (FRAME_EXTERNAL_TOOL_BAR (f))
10087 update_frame_tool_bar (f);
10088 return 0;
10089 #endif
10090
10091 /* If frame hasn't a tool-bar window or if it is zero-height, don't
10092 do anything. This means you must start with tool-bar-lines
10093 non-zero to get the auto-sizing effect. Or in other words, you
10094 can turn off tool-bars by specifying tool-bar-lines zero. */
10095 if (!WINDOWP (f->tool_bar_window)
10096 || (w = XWINDOW (f->tool_bar_window),
10097 WINDOW_TOTAL_LINES (w) == 0))
10098 return 0;
10099
10100 /* Set up an iterator for the tool-bar window. */
10101 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
10102 it.first_visible_x = 0;
10103 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
10104 row = it.glyph_row;
10105
10106 /* Build a string that represents the contents of the tool-bar. */
10107 build_desired_tool_bar_string (f);
10108 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
10109
10110 if (f->n_tool_bar_rows == 0)
10111 {
10112 int nlines;
10113
10114 if ((nlines = tool_bar_lines_needed (f, &f->n_tool_bar_rows),
10115 nlines != WINDOW_TOTAL_LINES (w)))
10116 {
10117 extern Lisp_Object Qtool_bar_lines;
10118 Lisp_Object frame;
10119 int old_height = WINDOW_TOTAL_LINES (w);
10120
10121 XSETFRAME (frame, f);
10122 Fmodify_frame_parameters (frame,
10123 Fcons (Fcons (Qtool_bar_lines,
10124 make_number (nlines)),
10125 Qnil));
10126 if (WINDOW_TOTAL_LINES (w) != old_height)
10127 {
10128 clear_glyph_matrix (w->desired_matrix);
10129 fonts_changed_p = 1;
10130 return 1;
10131 }
10132 }
10133 }
10134
10135 /* Display as many lines as needed to display all tool-bar items. */
10136
10137 if (f->n_tool_bar_rows > 0)
10138 {
10139 int border, rows, height, extra;
10140
10141 if (INTEGERP (Vtool_bar_border))
10142 border = XINT (Vtool_bar_border);
10143 else if (EQ (Vtool_bar_border, Qinternal_border_width))
10144 border = FRAME_INTERNAL_BORDER_WIDTH (f);
10145 else if (EQ (Vtool_bar_border, Qborder_width))
10146 border = f->border_width;
10147 else
10148 border = 0;
10149 if (border < 0)
10150 border = 0;
10151
10152 rows = f->n_tool_bar_rows;
10153 height = max (1, (it.last_visible_y - border) / rows);
10154 extra = it.last_visible_y - border - height * rows;
10155
10156 while (it.current_y < it.last_visible_y)
10157 {
10158 int h = 0;
10159 if (extra > 0 && rows-- > 0)
10160 {
10161 h = (extra + rows - 1) / rows;
10162 extra -= h;
10163 }
10164 display_tool_bar_line (&it, height + h);
10165 }
10166 }
10167 else
10168 {
10169 while (it.current_y < it.last_visible_y)
10170 display_tool_bar_line (&it, 0);
10171 }
10172
10173 /* It doesn't make much sense to try scrolling in the tool-bar
10174 window, so don't do it. */
10175 w->desired_matrix->no_scrolling_p = 1;
10176 w->must_be_updated_p = 1;
10177
10178 if (!NILP (Vauto_resize_tool_bars))
10179 {
10180 int max_tool_bar_height = MAX_FRAME_TOOL_BAR_HEIGHT (f);
10181 int change_height_p = 0;
10182
10183 /* If we couldn't display everything, change the tool-bar's
10184 height if there is room for more. */
10185 if (IT_STRING_CHARPOS (it) < it.end_charpos
10186 && it.current_y < max_tool_bar_height)
10187 change_height_p = 1;
10188
10189 row = it.glyph_row - 1;
10190
10191 /* If there are blank lines at the end, except for a partially
10192 visible blank line at the end that is smaller than
10193 FRAME_LINE_HEIGHT, change the tool-bar's height. */
10194 if (!row->displays_text_p
10195 && row->height >= FRAME_LINE_HEIGHT (f))
10196 change_height_p = 1;
10197
10198 /* If row displays tool-bar items, but is partially visible,
10199 change the tool-bar's height. */
10200 if (row->displays_text_p
10201 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y
10202 && MATRIX_ROW_BOTTOM_Y (row) < max_tool_bar_height)
10203 change_height_p = 1;
10204
10205 /* Resize windows as needed by changing the `tool-bar-lines'
10206 frame parameter. */
10207 if (change_height_p)
10208 {
10209 extern Lisp_Object Qtool_bar_lines;
10210 Lisp_Object frame;
10211 int old_height = WINDOW_TOTAL_LINES (w);
10212 int nrows;
10213 int nlines = tool_bar_lines_needed (f, &nrows);
10214
10215 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
10216 && !f->minimize_tool_bar_window_p)
10217 ? (nlines > old_height)
10218 : (nlines != old_height));
10219 f->minimize_tool_bar_window_p = 0;
10220
10221 if (change_height_p)
10222 {
10223 XSETFRAME (frame, f);
10224 Fmodify_frame_parameters (frame,
10225 Fcons (Fcons (Qtool_bar_lines,
10226 make_number (nlines)),
10227 Qnil));
10228 if (WINDOW_TOTAL_LINES (w) != old_height)
10229 {
10230 clear_glyph_matrix (w->desired_matrix);
10231 f->n_tool_bar_rows = nrows;
10232 fonts_changed_p = 1;
10233 return 1;
10234 }
10235 }
10236 }
10237 }
10238
10239 f->minimize_tool_bar_window_p = 0;
10240 return 0;
10241 }
10242
10243
10244 /* Get information about the tool-bar item which is displayed in GLYPH
10245 on frame F. Return in *PROP_IDX the index where tool-bar item
10246 properties start in F->tool_bar_items. Value is zero if
10247 GLYPH doesn't display a tool-bar item. */
10248
10249 static int
10250 tool_bar_item_info (f, glyph, prop_idx)
10251 struct frame *f;
10252 struct glyph *glyph;
10253 int *prop_idx;
10254 {
10255 Lisp_Object prop;
10256 int success_p;
10257 int charpos;
10258
10259 /* This function can be called asynchronously, which means we must
10260 exclude any possibility that Fget_text_property signals an
10261 error. */
10262 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
10263 charpos = max (0, charpos);
10264
10265 /* Get the text property `menu-item' at pos. The value of that
10266 property is the start index of this item's properties in
10267 F->tool_bar_items. */
10268 prop = Fget_text_property (make_number (charpos),
10269 Qmenu_item, f->current_tool_bar_string);
10270 if (INTEGERP (prop))
10271 {
10272 *prop_idx = XINT (prop);
10273 success_p = 1;
10274 }
10275 else
10276 success_p = 0;
10277
10278 return success_p;
10279 }
10280
10281 \f
10282 /* Get information about the tool-bar item at position X/Y on frame F.
10283 Return in *GLYPH a pointer to the glyph of the tool-bar item in
10284 the current matrix of the tool-bar window of F, or NULL if not
10285 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
10286 item in F->tool_bar_items. Value is
10287
10288 -1 if X/Y is not on a tool-bar item
10289 0 if X/Y is on the same item that was highlighted before.
10290 1 otherwise. */
10291
10292 static int
10293 get_tool_bar_item (f, x, y, glyph, hpos, vpos, prop_idx)
10294 struct frame *f;
10295 int x, y;
10296 struct glyph **glyph;
10297 int *hpos, *vpos, *prop_idx;
10298 {
10299 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10300 struct window *w = XWINDOW (f->tool_bar_window);
10301 int area;
10302
10303 /* Find the glyph under X/Y. */
10304 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
10305 if (*glyph == NULL)
10306 return -1;
10307
10308 /* Get the start of this tool-bar item's properties in
10309 f->tool_bar_items. */
10310 if (!tool_bar_item_info (f, *glyph, prop_idx))
10311 return -1;
10312
10313 /* Is mouse on the highlighted item? */
10314 if (EQ (f->tool_bar_window, dpyinfo->mouse_face_window)
10315 && *vpos >= dpyinfo->mouse_face_beg_row
10316 && *vpos <= dpyinfo->mouse_face_end_row
10317 && (*vpos > dpyinfo->mouse_face_beg_row
10318 || *hpos >= dpyinfo->mouse_face_beg_col)
10319 && (*vpos < dpyinfo->mouse_face_end_row
10320 || *hpos < dpyinfo->mouse_face_end_col
10321 || dpyinfo->mouse_face_past_end))
10322 return 0;
10323
10324 return 1;
10325 }
10326
10327
10328 /* EXPORT:
10329 Handle mouse button event on the tool-bar of frame F, at
10330 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
10331 0 for button release. MODIFIERS is event modifiers for button
10332 release. */
10333
10334 void
10335 handle_tool_bar_click (f, x, y, down_p, modifiers)
10336 struct frame *f;
10337 int x, y, down_p;
10338 unsigned int modifiers;
10339 {
10340 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10341 struct window *w = XWINDOW (f->tool_bar_window);
10342 int hpos, vpos, prop_idx;
10343 struct glyph *glyph;
10344 Lisp_Object enabled_p;
10345
10346 /* If not on the highlighted tool-bar item, return. */
10347 frame_to_window_pixel_xy (w, &x, &y);
10348 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
10349 return;
10350
10351 /* If item is disabled, do nothing. */
10352 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
10353 if (NILP (enabled_p))
10354 return;
10355
10356 if (down_p)
10357 {
10358 /* Show item in pressed state. */
10359 show_mouse_face (dpyinfo, DRAW_IMAGE_SUNKEN);
10360 dpyinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
10361 last_tool_bar_item = prop_idx;
10362 }
10363 else
10364 {
10365 Lisp_Object key, frame;
10366 struct input_event event;
10367 EVENT_INIT (event);
10368
10369 /* Show item in released state. */
10370 show_mouse_face (dpyinfo, DRAW_IMAGE_RAISED);
10371 dpyinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
10372
10373 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
10374
10375 XSETFRAME (frame, f);
10376 event.kind = TOOL_BAR_EVENT;
10377 event.frame_or_window = frame;
10378 event.arg = frame;
10379 kbd_buffer_store_event (&event);
10380
10381 event.kind = TOOL_BAR_EVENT;
10382 event.frame_or_window = frame;
10383 event.arg = key;
10384 event.modifiers = modifiers;
10385 kbd_buffer_store_event (&event);
10386 last_tool_bar_item = -1;
10387 }
10388 }
10389
10390
10391 /* Possibly highlight a tool-bar item on frame F when mouse moves to
10392 tool-bar window-relative coordinates X/Y. Called from
10393 note_mouse_highlight. */
10394
10395 static void
10396 note_tool_bar_highlight (f, x, y)
10397 struct frame *f;
10398 int x, y;
10399 {
10400 Lisp_Object window = f->tool_bar_window;
10401 struct window *w = XWINDOW (window);
10402 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10403 int hpos, vpos;
10404 struct glyph *glyph;
10405 struct glyph_row *row;
10406 int i;
10407 Lisp_Object enabled_p;
10408 int prop_idx;
10409 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
10410 int mouse_down_p, rc;
10411
10412 /* Function note_mouse_highlight is called with negative x(y
10413 values when mouse moves outside of the frame. */
10414 if (x <= 0 || y <= 0)
10415 {
10416 clear_mouse_face (dpyinfo);
10417 return;
10418 }
10419
10420 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
10421 if (rc < 0)
10422 {
10423 /* Not on tool-bar item. */
10424 clear_mouse_face (dpyinfo);
10425 return;
10426 }
10427 else if (rc == 0)
10428 /* On same tool-bar item as before. */
10429 goto set_help_echo;
10430
10431 clear_mouse_face (dpyinfo);
10432
10433 /* Mouse is down, but on different tool-bar item? */
10434 mouse_down_p = (dpyinfo->grabbed
10435 && f == last_mouse_frame
10436 && FRAME_LIVE_P (f));
10437 if (mouse_down_p
10438 && last_tool_bar_item != prop_idx)
10439 return;
10440
10441 dpyinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
10442 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
10443
10444 /* If tool-bar item is not enabled, don't highlight it. */
10445 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
10446 if (!NILP (enabled_p))
10447 {
10448 /* Compute the x-position of the glyph. In front and past the
10449 image is a space. We include this in the highlighted area. */
10450 row = MATRIX_ROW (w->current_matrix, vpos);
10451 for (i = x = 0; i < hpos; ++i)
10452 x += row->glyphs[TEXT_AREA][i].pixel_width;
10453
10454 /* Record this as the current active region. */
10455 dpyinfo->mouse_face_beg_col = hpos;
10456 dpyinfo->mouse_face_beg_row = vpos;
10457 dpyinfo->mouse_face_beg_x = x;
10458 dpyinfo->mouse_face_beg_y = row->y;
10459 dpyinfo->mouse_face_past_end = 0;
10460
10461 dpyinfo->mouse_face_end_col = hpos + 1;
10462 dpyinfo->mouse_face_end_row = vpos;
10463 dpyinfo->mouse_face_end_x = x + glyph->pixel_width;
10464 dpyinfo->mouse_face_end_y = row->y;
10465 dpyinfo->mouse_face_window = window;
10466 dpyinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
10467
10468 /* Display it as active. */
10469 show_mouse_face (dpyinfo, draw);
10470 dpyinfo->mouse_face_image_state = draw;
10471 }
10472
10473 set_help_echo:
10474
10475 /* Set help_echo_string to a help string to display for this tool-bar item.
10476 XTread_socket does the rest. */
10477 help_echo_object = help_echo_window = Qnil;
10478 help_echo_pos = -1;
10479 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
10480 if (NILP (help_echo_string))
10481 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
10482 }
10483
10484 #endif /* HAVE_WINDOW_SYSTEM */
10485
10486
10487 \f
10488 /************************************************************************
10489 Horizontal scrolling
10490 ************************************************************************/
10491
10492 static int hscroll_window_tree P_ ((Lisp_Object));
10493 static int hscroll_windows P_ ((Lisp_Object));
10494
10495 /* For all leaf windows in the window tree rooted at WINDOW, set their
10496 hscroll value so that PT is (i) visible in the window, and (ii) so
10497 that it is not within a certain margin at the window's left and
10498 right border. Value is non-zero if any window's hscroll has been
10499 changed. */
10500
10501 static int
10502 hscroll_window_tree (window)
10503 Lisp_Object window;
10504 {
10505 int hscrolled_p = 0;
10506 int hscroll_relative_p = FLOATP (Vhscroll_step);
10507 int hscroll_step_abs = 0;
10508 double hscroll_step_rel = 0;
10509
10510 if (hscroll_relative_p)
10511 {
10512 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
10513 if (hscroll_step_rel < 0)
10514 {
10515 hscroll_relative_p = 0;
10516 hscroll_step_abs = 0;
10517 }
10518 }
10519 else if (INTEGERP (Vhscroll_step))
10520 {
10521 hscroll_step_abs = XINT (Vhscroll_step);
10522 if (hscroll_step_abs < 0)
10523 hscroll_step_abs = 0;
10524 }
10525 else
10526 hscroll_step_abs = 0;
10527
10528 while (WINDOWP (window))
10529 {
10530 struct window *w = XWINDOW (window);
10531
10532 if (WINDOWP (w->hchild))
10533 hscrolled_p |= hscroll_window_tree (w->hchild);
10534 else if (WINDOWP (w->vchild))
10535 hscrolled_p |= hscroll_window_tree (w->vchild);
10536 else if (w->cursor.vpos >= 0)
10537 {
10538 int h_margin;
10539 int text_area_width;
10540 struct glyph_row *current_cursor_row
10541 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
10542 struct glyph_row *desired_cursor_row
10543 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
10544 struct glyph_row *cursor_row
10545 = (desired_cursor_row->enabled_p
10546 ? desired_cursor_row
10547 : current_cursor_row);
10548
10549 text_area_width = window_box_width (w, TEXT_AREA);
10550
10551 /* Scroll when cursor is inside this scroll margin. */
10552 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
10553
10554 if ((XFASTINT (w->hscroll)
10555 && w->cursor.x <= h_margin)
10556 || (cursor_row->enabled_p
10557 && cursor_row->truncated_on_right_p
10558 && (w->cursor.x >= text_area_width - h_margin)))
10559 {
10560 struct it it;
10561 int hscroll;
10562 struct buffer *saved_current_buffer;
10563 int pt;
10564 int wanted_x;
10565
10566 /* Find point in a display of infinite width. */
10567 saved_current_buffer = current_buffer;
10568 current_buffer = XBUFFER (w->buffer);
10569
10570 if (w == XWINDOW (selected_window))
10571 pt = BUF_PT (current_buffer);
10572 else
10573 {
10574 pt = marker_position (w->pointm);
10575 pt = max (BEGV, pt);
10576 pt = min (ZV, pt);
10577 }
10578
10579 /* Move iterator to pt starting at cursor_row->start in
10580 a line with infinite width. */
10581 init_to_row_start (&it, w, cursor_row);
10582 it.last_visible_x = INFINITY;
10583 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
10584 current_buffer = saved_current_buffer;
10585
10586 /* Position cursor in window. */
10587 if (!hscroll_relative_p && hscroll_step_abs == 0)
10588 hscroll = max (0, (it.current_x
10589 - (ITERATOR_AT_END_OF_LINE_P (&it)
10590 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
10591 : (text_area_width / 2))))
10592 / FRAME_COLUMN_WIDTH (it.f);
10593 else if (w->cursor.x >= text_area_width - h_margin)
10594 {
10595 if (hscroll_relative_p)
10596 wanted_x = text_area_width * (1 - hscroll_step_rel)
10597 - h_margin;
10598 else
10599 wanted_x = text_area_width
10600 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
10601 - h_margin;
10602 hscroll
10603 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
10604 }
10605 else
10606 {
10607 if (hscroll_relative_p)
10608 wanted_x = text_area_width * hscroll_step_rel
10609 + h_margin;
10610 else
10611 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
10612 + h_margin;
10613 hscroll
10614 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
10615 }
10616 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
10617
10618 /* Don't call Fset_window_hscroll if value hasn't
10619 changed because it will prevent redisplay
10620 optimizations. */
10621 if (XFASTINT (w->hscroll) != hscroll)
10622 {
10623 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
10624 w->hscroll = make_number (hscroll);
10625 hscrolled_p = 1;
10626 }
10627 }
10628 }
10629
10630 window = w->next;
10631 }
10632
10633 /* Value is non-zero if hscroll of any leaf window has been changed. */
10634 return hscrolled_p;
10635 }
10636
10637
10638 /* Set hscroll so that cursor is visible and not inside horizontal
10639 scroll margins for all windows in the tree rooted at WINDOW. See
10640 also hscroll_window_tree above. Value is non-zero if any window's
10641 hscroll has been changed. If it has, desired matrices on the frame
10642 of WINDOW are cleared. */
10643
10644 static int
10645 hscroll_windows (window)
10646 Lisp_Object window;
10647 {
10648 int hscrolled_p;
10649
10650 if (automatic_hscrolling_p)
10651 {
10652 hscrolled_p = hscroll_window_tree (window);
10653 if (hscrolled_p)
10654 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
10655 }
10656 else
10657 hscrolled_p = 0;
10658 return hscrolled_p;
10659 }
10660
10661
10662 \f
10663 /************************************************************************
10664 Redisplay
10665 ************************************************************************/
10666
10667 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
10668 to a non-zero value. This is sometimes handy to have in a debugger
10669 session. */
10670
10671 #if GLYPH_DEBUG
10672
10673 /* First and last unchanged row for try_window_id. */
10674
10675 int debug_first_unchanged_at_end_vpos;
10676 int debug_last_unchanged_at_beg_vpos;
10677
10678 /* Delta vpos and y. */
10679
10680 int debug_dvpos, debug_dy;
10681
10682 /* Delta in characters and bytes for try_window_id. */
10683
10684 int debug_delta, debug_delta_bytes;
10685
10686 /* Values of window_end_pos and window_end_vpos at the end of
10687 try_window_id. */
10688
10689 EMACS_INT debug_end_pos, debug_end_vpos;
10690
10691 /* Append a string to W->desired_matrix->method. FMT is a printf
10692 format string. A1...A9 are a supplement for a variable-length
10693 argument list. If trace_redisplay_p is non-zero also printf the
10694 resulting string to stderr. */
10695
10696 static void
10697 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
10698 struct window *w;
10699 char *fmt;
10700 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
10701 {
10702 char buffer[512];
10703 char *method = w->desired_matrix->method;
10704 int len = strlen (method);
10705 int size = sizeof w->desired_matrix->method;
10706 int remaining = size - len - 1;
10707
10708 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
10709 if (len && remaining)
10710 {
10711 method[len] = '|';
10712 --remaining, ++len;
10713 }
10714
10715 strncpy (method + len, buffer, remaining);
10716
10717 if (trace_redisplay_p)
10718 fprintf (stderr, "%p (%s): %s\n",
10719 w,
10720 ((BUFFERP (w->buffer)
10721 && STRINGP (XBUFFER (w->buffer)->name))
10722 ? (char *) SDATA (XBUFFER (w->buffer)->name)
10723 : "no buffer"),
10724 buffer);
10725 }
10726
10727 #endif /* GLYPH_DEBUG */
10728
10729
10730 /* Value is non-zero if all changes in window W, which displays
10731 current_buffer, are in the text between START and END. START is a
10732 buffer position, END is given as a distance from Z. Used in
10733 redisplay_internal for display optimization. */
10734
10735 static INLINE int
10736 text_outside_line_unchanged_p (w, start, end)
10737 struct window *w;
10738 int start, end;
10739 {
10740 int unchanged_p = 1;
10741
10742 /* If text or overlays have changed, see where. */
10743 if (XFASTINT (w->last_modified) < MODIFF
10744 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
10745 {
10746 /* Gap in the line? */
10747 if (GPT < start || Z - GPT < end)
10748 unchanged_p = 0;
10749
10750 /* Changes start in front of the line, or end after it? */
10751 if (unchanged_p
10752 && (BEG_UNCHANGED < start - 1
10753 || END_UNCHANGED < end))
10754 unchanged_p = 0;
10755
10756 /* If selective display, can't optimize if changes start at the
10757 beginning of the line. */
10758 if (unchanged_p
10759 && INTEGERP (current_buffer->selective_display)
10760 && XINT (current_buffer->selective_display) > 0
10761 && (BEG_UNCHANGED < start || GPT <= start))
10762 unchanged_p = 0;
10763
10764 /* If there are overlays at the start or end of the line, these
10765 may have overlay strings with newlines in them. A change at
10766 START, for instance, may actually concern the display of such
10767 overlay strings as well, and they are displayed on different
10768 lines. So, quickly rule out this case. (For the future, it
10769 might be desirable to implement something more telling than
10770 just BEG/END_UNCHANGED.) */
10771 if (unchanged_p)
10772 {
10773 if (BEG + BEG_UNCHANGED == start
10774 && overlay_touches_p (start))
10775 unchanged_p = 0;
10776 if (END_UNCHANGED == end
10777 && overlay_touches_p (Z - end))
10778 unchanged_p = 0;
10779 }
10780 }
10781
10782 return unchanged_p;
10783 }
10784
10785
10786 /* Do a frame update, taking possible shortcuts into account. This is
10787 the main external entry point for redisplay.
10788
10789 If the last redisplay displayed an echo area message and that message
10790 is no longer requested, we clear the echo area or bring back the
10791 mini-buffer if that is in use. */
10792
10793 void
10794 redisplay ()
10795 {
10796 redisplay_internal (0);
10797 }
10798
10799
10800 static Lisp_Object
10801 overlay_arrow_string_or_property (var)
10802 Lisp_Object var;
10803 {
10804 Lisp_Object val;
10805
10806 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
10807 return val;
10808
10809 return Voverlay_arrow_string;
10810 }
10811
10812 /* Return 1 if there are any overlay-arrows in current_buffer. */
10813 static int
10814 overlay_arrow_in_current_buffer_p ()
10815 {
10816 Lisp_Object vlist;
10817
10818 for (vlist = Voverlay_arrow_variable_list;
10819 CONSP (vlist);
10820 vlist = XCDR (vlist))
10821 {
10822 Lisp_Object var = XCAR (vlist);
10823 Lisp_Object val;
10824
10825 if (!SYMBOLP (var))
10826 continue;
10827 val = find_symbol_value (var);
10828 if (MARKERP (val)
10829 && current_buffer == XMARKER (val)->buffer)
10830 return 1;
10831 }
10832 return 0;
10833 }
10834
10835
10836 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
10837 has changed. */
10838
10839 static int
10840 overlay_arrows_changed_p ()
10841 {
10842 Lisp_Object vlist;
10843
10844 for (vlist = Voverlay_arrow_variable_list;
10845 CONSP (vlist);
10846 vlist = XCDR (vlist))
10847 {
10848 Lisp_Object var = XCAR (vlist);
10849 Lisp_Object val, pstr;
10850
10851 if (!SYMBOLP (var))
10852 continue;
10853 val = find_symbol_value (var);
10854 if (!MARKERP (val))
10855 continue;
10856 if (! EQ (COERCE_MARKER (val),
10857 Fget (var, Qlast_arrow_position))
10858 || ! (pstr = overlay_arrow_string_or_property (var),
10859 EQ (pstr, Fget (var, Qlast_arrow_string))))
10860 return 1;
10861 }
10862 return 0;
10863 }
10864
10865 /* Mark overlay arrows to be updated on next redisplay. */
10866
10867 static void
10868 update_overlay_arrows (up_to_date)
10869 int up_to_date;
10870 {
10871 Lisp_Object vlist;
10872
10873 for (vlist = Voverlay_arrow_variable_list;
10874 CONSP (vlist);
10875 vlist = XCDR (vlist))
10876 {
10877 Lisp_Object var = XCAR (vlist);
10878
10879 if (!SYMBOLP (var))
10880 continue;
10881
10882 if (up_to_date > 0)
10883 {
10884 Lisp_Object val = find_symbol_value (var);
10885 Fput (var, Qlast_arrow_position,
10886 COERCE_MARKER (val));
10887 Fput (var, Qlast_arrow_string,
10888 overlay_arrow_string_or_property (var));
10889 }
10890 else if (up_to_date < 0
10891 || !NILP (Fget (var, Qlast_arrow_position)))
10892 {
10893 Fput (var, Qlast_arrow_position, Qt);
10894 Fput (var, Qlast_arrow_string, Qt);
10895 }
10896 }
10897 }
10898
10899
10900 /* Return overlay arrow string to display at row.
10901 Return integer (bitmap number) for arrow bitmap in left fringe.
10902 Return nil if no overlay arrow. */
10903
10904 static Lisp_Object
10905 overlay_arrow_at_row (it, row)
10906 struct it *it;
10907 struct glyph_row *row;
10908 {
10909 Lisp_Object vlist;
10910
10911 for (vlist = Voverlay_arrow_variable_list;
10912 CONSP (vlist);
10913 vlist = XCDR (vlist))
10914 {
10915 Lisp_Object var = XCAR (vlist);
10916 Lisp_Object val;
10917
10918 if (!SYMBOLP (var))
10919 continue;
10920
10921 val = find_symbol_value (var);
10922
10923 if (MARKERP (val)
10924 && current_buffer == XMARKER (val)->buffer
10925 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
10926 {
10927 if (FRAME_WINDOW_P (it->f)
10928 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
10929 {
10930 #ifdef HAVE_WINDOW_SYSTEM
10931 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
10932 {
10933 int fringe_bitmap;
10934 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
10935 return make_number (fringe_bitmap);
10936 }
10937 #endif
10938 return make_number (-1); /* Use default arrow bitmap */
10939 }
10940 return overlay_arrow_string_or_property (var);
10941 }
10942 }
10943
10944 return Qnil;
10945 }
10946
10947 /* Return 1 if point moved out of or into a composition. Otherwise
10948 return 0. PREV_BUF and PREV_PT are the last point buffer and
10949 position. BUF and PT are the current point buffer and position. */
10950
10951 int
10952 check_point_in_composition (prev_buf, prev_pt, buf, pt)
10953 struct buffer *prev_buf, *buf;
10954 int prev_pt, pt;
10955 {
10956 EMACS_INT start, end;
10957 Lisp_Object prop;
10958 Lisp_Object buffer;
10959
10960 XSETBUFFER (buffer, buf);
10961 /* Check a composition at the last point if point moved within the
10962 same buffer. */
10963 if (prev_buf == buf)
10964 {
10965 if (prev_pt == pt)
10966 /* Point didn't move. */
10967 return 0;
10968
10969 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
10970 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
10971 && COMPOSITION_VALID_P (start, end, prop)
10972 && start < prev_pt && end > prev_pt)
10973 /* The last point was within the composition. Return 1 iff
10974 point moved out of the composition. */
10975 return (pt <= start || pt >= end);
10976 }
10977
10978 /* Check a composition at the current point. */
10979 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
10980 && find_composition (pt, -1, &start, &end, &prop, buffer)
10981 && COMPOSITION_VALID_P (start, end, prop)
10982 && start < pt && end > pt);
10983 }
10984
10985
10986 /* Reconsider the setting of B->clip_changed which is displayed
10987 in window W. */
10988
10989 static INLINE void
10990 reconsider_clip_changes (w, b)
10991 struct window *w;
10992 struct buffer *b;
10993 {
10994 if (b->clip_changed
10995 && !NILP (w->window_end_valid)
10996 && w->current_matrix->buffer == b
10997 && w->current_matrix->zv == BUF_ZV (b)
10998 && w->current_matrix->begv == BUF_BEGV (b))
10999 b->clip_changed = 0;
11000
11001 /* If display wasn't paused, and W is not a tool bar window, see if
11002 point has been moved into or out of a composition. In that case,
11003 we set b->clip_changed to 1 to force updating the screen. If
11004 b->clip_changed has already been set to 1, we can skip this
11005 check. */
11006 if (!b->clip_changed
11007 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
11008 {
11009 int pt;
11010
11011 if (w == XWINDOW (selected_window))
11012 pt = BUF_PT (current_buffer);
11013 else
11014 pt = marker_position (w->pointm);
11015
11016 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
11017 || pt != XINT (w->last_point))
11018 && check_point_in_composition (w->current_matrix->buffer,
11019 XINT (w->last_point),
11020 XBUFFER (w->buffer), pt))
11021 b->clip_changed = 1;
11022 }
11023 }
11024 \f
11025
11026 /* Select FRAME to forward the values of frame-local variables into C
11027 variables so that the redisplay routines can access those values
11028 directly. */
11029
11030 static void
11031 select_frame_for_redisplay (frame)
11032 Lisp_Object frame;
11033 {
11034 Lisp_Object tail, sym, val;
11035 Lisp_Object old = selected_frame;
11036
11037 xassert (FRAMEP (frame) && FRAME_LIVE_P (XFRAME (frame)));
11038
11039 selected_frame = frame;
11040
11041 for (tail = XFRAME (frame)->param_alist; CONSP (tail); tail = XCDR (tail))
11042 if (CONSP (XCAR (tail))
11043 && (sym = XCAR (XCAR (tail)),
11044 SYMBOLP (sym))
11045 && (sym = indirect_variable (sym),
11046 val = SYMBOL_VALUE (sym),
11047 (BUFFER_LOCAL_VALUEP (val)))
11048 && XBUFFER_LOCAL_VALUE (val)->check_frame)
11049 /* Use find_symbol_value rather than Fsymbol_value
11050 to avoid an error if it is void. */
11051 find_symbol_value (sym);
11052
11053 for (tail = XFRAME (old)->param_alist; CONSP (tail); tail = XCDR (tail))
11054 if (CONSP (XCAR (tail))
11055 && (sym = XCAR (XCAR (tail)),
11056 SYMBOLP (sym))
11057 && (sym = indirect_variable (sym),
11058 val = SYMBOL_VALUE (sym),
11059 (BUFFER_LOCAL_VALUEP (val)))
11060 && XBUFFER_LOCAL_VALUE (val)->check_frame)
11061 find_symbol_value (sym);
11062 }
11063
11064
11065 #define STOP_POLLING \
11066 do { if (! polling_stopped_here) stop_polling (); \
11067 polling_stopped_here = 1; } while (0)
11068
11069 #define RESUME_POLLING \
11070 do { if (polling_stopped_here) start_polling (); \
11071 polling_stopped_here = 0; } while (0)
11072
11073
11074 /* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
11075 response to any user action; therefore, we should preserve the echo
11076 area. (Actually, our caller does that job.) Perhaps in the future
11077 avoid recentering windows if it is not necessary; currently that
11078 causes some problems. */
11079
11080 static void
11081 redisplay_internal (preserve_echo_area)
11082 int preserve_echo_area;
11083 {
11084 struct window *w = XWINDOW (selected_window);
11085 struct frame *f;
11086 int pause;
11087 int must_finish = 0;
11088 struct text_pos tlbufpos, tlendpos;
11089 int number_of_visible_frames;
11090 int count, count1;
11091 struct frame *sf;
11092 int polling_stopped_here = 0;
11093 Lisp_Object old_frame = selected_frame;
11094
11095 /* Non-zero means redisplay has to consider all windows on all
11096 frames. Zero means, only selected_window is considered. */
11097 int consider_all_windows_p;
11098
11099 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
11100
11101 /* No redisplay if running in batch mode or frame is not yet fully
11102 initialized, or redisplay is explicitly turned off by setting
11103 Vinhibit_redisplay. */
11104 if (noninteractive
11105 || !NILP (Vinhibit_redisplay))
11106 return;
11107
11108 /* Don't examine these until after testing Vinhibit_redisplay.
11109 When Emacs is shutting down, perhaps because its connection to
11110 X has dropped, we should not look at them at all. */
11111 f = XFRAME (w->frame);
11112 sf = SELECTED_FRAME ();
11113
11114 if (!f->glyphs_initialized_p)
11115 return;
11116
11117 /* The flag redisplay_performed_directly_p is set by
11118 direct_output_for_insert when it already did the whole screen
11119 update necessary. */
11120 if (redisplay_performed_directly_p)
11121 {
11122 redisplay_performed_directly_p = 0;
11123 if (!hscroll_windows (selected_window))
11124 return;
11125 }
11126
11127 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (MAC_OS)
11128 if (popup_activated ())
11129 return;
11130 #endif
11131
11132 /* I don't think this happens but let's be paranoid. */
11133 if (redisplaying_p)
11134 return;
11135
11136 /* Record a function that resets redisplaying_p to its old value
11137 when we leave this function. */
11138 count = SPECPDL_INDEX ();
11139 record_unwind_protect (unwind_redisplay,
11140 Fcons (make_number (redisplaying_p), selected_frame));
11141 ++redisplaying_p;
11142 specbind (Qinhibit_free_realized_faces, Qnil);
11143
11144 {
11145 Lisp_Object tail, frame;
11146
11147 FOR_EACH_FRAME (tail, frame)
11148 {
11149 struct frame *f = XFRAME (frame);
11150 f->already_hscrolled_p = 0;
11151 }
11152 }
11153
11154 retry:
11155 if (!EQ (old_frame, selected_frame)
11156 && FRAME_LIVE_P (XFRAME (old_frame)))
11157 /* When running redisplay, we play a bit fast-and-loose and allow e.g.
11158 selected_frame and selected_window to be temporarily out-of-sync so
11159 when we come back here via `goto retry', we need to resync because we
11160 may need to run Elisp code (via prepare_menu_bars). */
11161 select_frame_for_redisplay (old_frame);
11162
11163 pause = 0;
11164 reconsider_clip_changes (w, current_buffer);
11165 last_escape_glyph_frame = NULL;
11166 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
11167
11168 /* If new fonts have been loaded that make a glyph matrix adjustment
11169 necessary, do it. */
11170 if (fonts_changed_p)
11171 {
11172 adjust_glyphs (NULL);
11173 ++windows_or_buffers_changed;
11174 fonts_changed_p = 0;
11175 }
11176
11177 /* If face_change_count is non-zero, init_iterator will free all
11178 realized faces, which includes the faces referenced from current
11179 matrices. So, we can't reuse current matrices in this case. */
11180 if (face_change_count)
11181 ++windows_or_buffers_changed;
11182
11183 if (FRAME_TERMCAP_P (sf)
11184 && FRAME_TTY (sf)->previous_frame != sf)
11185 {
11186 /* Since frames on a single ASCII terminal share the same
11187 display area, displaying a different frame means redisplay
11188 the whole thing. */
11189 windows_or_buffers_changed++;
11190 SET_FRAME_GARBAGED (sf);
11191 FRAME_TTY (sf)->previous_frame = sf;
11192 }
11193
11194 /* Set the visible flags for all frames. Do this before checking
11195 for resized or garbaged frames; they want to know if their frames
11196 are visible. See the comment in frame.h for
11197 FRAME_SAMPLE_VISIBILITY. */
11198 {
11199 Lisp_Object tail, frame;
11200
11201 number_of_visible_frames = 0;
11202
11203 FOR_EACH_FRAME (tail, frame)
11204 {
11205 struct frame *f = XFRAME (frame);
11206
11207 FRAME_SAMPLE_VISIBILITY (f);
11208 if (FRAME_VISIBLE_P (f))
11209 ++number_of_visible_frames;
11210 clear_desired_matrices (f);
11211 }
11212 }
11213
11214
11215 /* Notice any pending interrupt request to change frame size. */
11216 do_pending_window_change (1);
11217
11218 /* Clear frames marked as garbaged. */
11219 if (frame_garbaged)
11220 clear_garbaged_frames ();
11221
11222 /* Build menubar and tool-bar items. */
11223 if (NILP (Vmemory_full))
11224 prepare_menu_bars ();
11225
11226 if (windows_or_buffers_changed)
11227 update_mode_lines++;
11228
11229 /* Detect case that we need to write or remove a star in the mode line. */
11230 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
11231 {
11232 w->update_mode_line = Qt;
11233 if (buffer_shared > 1)
11234 update_mode_lines++;
11235 }
11236
11237 /* Avoid invocation of point motion hooks by `current_column' below. */
11238 count1 = SPECPDL_INDEX ();
11239 specbind (Qinhibit_point_motion_hooks, Qt);
11240
11241 /* If %c is in the mode line, update it if needed. */
11242 if (!NILP (w->column_number_displayed)
11243 /* This alternative quickly identifies a common case
11244 where no change is needed. */
11245 && !(PT == XFASTINT (w->last_point)
11246 && XFASTINT (w->last_modified) >= MODIFF
11247 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
11248 && (XFASTINT (w->column_number_displayed)
11249 != (int) current_column ())) /* iftc */
11250 w->update_mode_line = Qt;
11251
11252 unbind_to (count1, Qnil);
11253
11254 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
11255
11256 /* The variable buffer_shared is set in redisplay_window and
11257 indicates that we redisplay a buffer in different windows. See
11258 there. */
11259 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
11260 || cursor_type_changed);
11261
11262 /* If specs for an arrow have changed, do thorough redisplay
11263 to ensure we remove any arrow that should no longer exist. */
11264 if (overlay_arrows_changed_p ())
11265 consider_all_windows_p = windows_or_buffers_changed = 1;
11266
11267 /* Normally the message* functions will have already displayed and
11268 updated the echo area, but the frame may have been trashed, or
11269 the update may have been preempted, so display the echo area
11270 again here. Checking message_cleared_p captures the case that
11271 the echo area should be cleared. */
11272 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
11273 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
11274 || (message_cleared_p
11275 && minibuf_level == 0
11276 /* If the mini-window is currently selected, this means the
11277 echo-area doesn't show through. */
11278 && !MINI_WINDOW_P (XWINDOW (selected_window))))
11279 {
11280 int window_height_changed_p = echo_area_display (0);
11281 must_finish = 1;
11282
11283 /* If we don't display the current message, don't clear the
11284 message_cleared_p flag, because, if we did, we wouldn't clear
11285 the echo area in the next redisplay which doesn't preserve
11286 the echo area. */
11287 if (!display_last_displayed_message_p)
11288 message_cleared_p = 0;
11289
11290 if (fonts_changed_p)
11291 goto retry;
11292 else if (window_height_changed_p)
11293 {
11294 consider_all_windows_p = 1;
11295 ++update_mode_lines;
11296 ++windows_or_buffers_changed;
11297
11298 /* If window configuration was changed, frames may have been
11299 marked garbaged. Clear them or we will experience
11300 surprises wrt scrolling. */
11301 if (frame_garbaged)
11302 clear_garbaged_frames ();
11303 }
11304 }
11305 else if (EQ (selected_window, minibuf_window)
11306 && (current_buffer->clip_changed
11307 || XFASTINT (w->last_modified) < MODIFF
11308 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
11309 && resize_mini_window (w, 0))
11310 {
11311 /* Resized active mini-window to fit the size of what it is
11312 showing if its contents might have changed. */
11313 must_finish = 1;
11314 consider_all_windows_p = 1;
11315 ++windows_or_buffers_changed;
11316 ++update_mode_lines;
11317
11318 /* If window configuration was changed, frames may have been
11319 marked garbaged. Clear them or we will experience
11320 surprises wrt scrolling. */
11321 if (frame_garbaged)
11322 clear_garbaged_frames ();
11323 }
11324
11325
11326 /* If showing the region, and mark has changed, we must redisplay
11327 the whole window. The assignment to this_line_start_pos prevents
11328 the optimization directly below this if-statement. */
11329 if (((!NILP (Vtransient_mark_mode)
11330 && !NILP (XBUFFER (w->buffer)->mark_active))
11331 != !NILP (w->region_showing))
11332 || (!NILP (w->region_showing)
11333 && !EQ (w->region_showing,
11334 Fmarker_position (XBUFFER (w->buffer)->mark))))
11335 CHARPOS (this_line_start_pos) = 0;
11336
11337 /* Optimize the case that only the line containing the cursor in the
11338 selected window has changed. Variables starting with this_ are
11339 set in display_line and record information about the line
11340 containing the cursor. */
11341 tlbufpos = this_line_start_pos;
11342 tlendpos = this_line_end_pos;
11343 if (!consider_all_windows_p
11344 && CHARPOS (tlbufpos) > 0
11345 && NILP (w->update_mode_line)
11346 && !current_buffer->clip_changed
11347 && !current_buffer->prevent_redisplay_optimizations_p
11348 && FRAME_VISIBLE_P (XFRAME (w->frame))
11349 && !FRAME_OBSCURED_P (XFRAME (w->frame))
11350 /* Make sure recorded data applies to current buffer, etc. */
11351 && this_line_buffer == current_buffer
11352 && current_buffer == XBUFFER (w->buffer)
11353 && NILP (w->force_start)
11354 && NILP (w->optional_new_start)
11355 /* Point must be on the line that we have info recorded about. */
11356 && PT >= CHARPOS (tlbufpos)
11357 && PT <= Z - CHARPOS (tlendpos)
11358 /* All text outside that line, including its final newline,
11359 must be unchanged */
11360 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
11361 CHARPOS (tlendpos)))
11362 {
11363 if (CHARPOS (tlbufpos) > BEGV
11364 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
11365 && (CHARPOS (tlbufpos) == ZV
11366 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
11367 /* Former continuation line has disappeared by becoming empty */
11368 goto cancel;
11369 else if (XFASTINT (w->last_modified) < MODIFF
11370 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
11371 || MINI_WINDOW_P (w))
11372 {
11373 /* We have to handle the case of continuation around a
11374 wide-column character (See the comment in indent.c around
11375 line 885).
11376
11377 For instance, in the following case:
11378
11379 -------- Insert --------
11380 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
11381 J_I_ ==> J_I_ `^^' are cursors.
11382 ^^ ^^
11383 -------- --------
11384
11385 As we have to redraw the line above, we should goto cancel. */
11386
11387 struct it it;
11388 int line_height_before = this_line_pixel_height;
11389
11390 /* Note that start_display will handle the case that the
11391 line starting at tlbufpos is a continuation lines. */
11392 start_display (&it, w, tlbufpos);
11393
11394 /* Implementation note: It this still necessary? */
11395 if (it.current_x != this_line_start_x)
11396 goto cancel;
11397
11398 TRACE ((stderr, "trying display optimization 1\n"));
11399 w->cursor.vpos = -1;
11400 overlay_arrow_seen = 0;
11401 it.vpos = this_line_vpos;
11402 it.current_y = this_line_y;
11403 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
11404 display_line (&it);
11405
11406 /* If line contains point, is not continued,
11407 and ends at same distance from eob as before, we win */
11408 if (w->cursor.vpos >= 0
11409 /* Line is not continued, otherwise this_line_start_pos
11410 would have been set to 0 in display_line. */
11411 && CHARPOS (this_line_start_pos)
11412 /* Line ends as before. */
11413 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
11414 /* Line has same height as before. Otherwise other lines
11415 would have to be shifted up or down. */
11416 && this_line_pixel_height == line_height_before)
11417 {
11418 /* If this is not the window's last line, we must adjust
11419 the charstarts of the lines below. */
11420 if (it.current_y < it.last_visible_y)
11421 {
11422 struct glyph_row *row
11423 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
11424 int delta, delta_bytes;
11425
11426 if (Z - CHARPOS (tlendpos) == ZV)
11427 {
11428 /* This line ends at end of (accessible part of)
11429 buffer. There is no newline to count. */
11430 delta = (Z
11431 - CHARPOS (tlendpos)
11432 - MATRIX_ROW_START_CHARPOS (row));
11433 delta_bytes = (Z_BYTE
11434 - BYTEPOS (tlendpos)
11435 - MATRIX_ROW_START_BYTEPOS (row));
11436 }
11437 else
11438 {
11439 /* This line ends in a newline. Must take
11440 account of the newline and the rest of the
11441 text that follows. */
11442 delta = (Z
11443 - CHARPOS (tlendpos)
11444 - MATRIX_ROW_START_CHARPOS (row));
11445 delta_bytes = (Z_BYTE
11446 - BYTEPOS (tlendpos)
11447 - MATRIX_ROW_START_BYTEPOS (row));
11448 }
11449
11450 increment_matrix_positions (w->current_matrix,
11451 this_line_vpos + 1,
11452 w->current_matrix->nrows,
11453 delta, delta_bytes);
11454 }
11455
11456 /* If this row displays text now but previously didn't,
11457 or vice versa, w->window_end_vpos may have to be
11458 adjusted. */
11459 if ((it.glyph_row - 1)->displays_text_p)
11460 {
11461 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
11462 XSETINT (w->window_end_vpos, this_line_vpos);
11463 }
11464 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
11465 && this_line_vpos > 0)
11466 XSETINT (w->window_end_vpos, this_line_vpos - 1);
11467 w->window_end_valid = Qnil;
11468
11469 /* Update hint: No need to try to scroll in update_window. */
11470 w->desired_matrix->no_scrolling_p = 1;
11471
11472 #if GLYPH_DEBUG
11473 *w->desired_matrix->method = 0;
11474 debug_method_add (w, "optimization 1");
11475 #endif
11476 #ifdef HAVE_WINDOW_SYSTEM
11477 update_window_fringes (w, 0);
11478 #endif
11479 goto update;
11480 }
11481 else
11482 goto cancel;
11483 }
11484 else if (/* Cursor position hasn't changed. */
11485 PT == XFASTINT (w->last_point)
11486 /* Make sure the cursor was last displayed
11487 in this window. Otherwise we have to reposition it. */
11488 && 0 <= w->cursor.vpos
11489 && WINDOW_TOTAL_LINES (w) > w->cursor.vpos)
11490 {
11491 if (!must_finish)
11492 {
11493 do_pending_window_change (1);
11494
11495 /* We used to always goto end_of_redisplay here, but this
11496 isn't enough if we have a blinking cursor. */
11497 if (w->cursor_off_p == w->last_cursor_off_p)
11498 goto end_of_redisplay;
11499 }
11500 goto update;
11501 }
11502 /* If highlighting the region, or if the cursor is in the echo area,
11503 then we can't just move the cursor. */
11504 else if (! (!NILP (Vtransient_mark_mode)
11505 && !NILP (current_buffer->mark_active))
11506 && (EQ (selected_window, current_buffer->last_selected_window)
11507 || highlight_nonselected_windows)
11508 && NILP (w->region_showing)
11509 && NILP (Vshow_trailing_whitespace)
11510 && !cursor_in_echo_area)
11511 {
11512 struct it it;
11513 struct glyph_row *row;
11514
11515 /* Skip from tlbufpos to PT and see where it is. Note that
11516 PT may be in invisible text. If so, we will end at the
11517 next visible position. */
11518 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
11519 NULL, DEFAULT_FACE_ID);
11520 it.current_x = this_line_start_x;
11521 it.current_y = this_line_y;
11522 it.vpos = this_line_vpos;
11523
11524 /* The call to move_it_to stops in front of PT, but
11525 moves over before-strings. */
11526 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
11527
11528 if (it.vpos == this_line_vpos
11529 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
11530 row->enabled_p))
11531 {
11532 xassert (this_line_vpos == it.vpos);
11533 xassert (this_line_y == it.current_y);
11534 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11535 #if GLYPH_DEBUG
11536 *w->desired_matrix->method = 0;
11537 debug_method_add (w, "optimization 3");
11538 #endif
11539 goto update;
11540 }
11541 else
11542 goto cancel;
11543 }
11544
11545 cancel:
11546 /* Text changed drastically or point moved off of line. */
11547 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
11548 }
11549
11550 CHARPOS (this_line_start_pos) = 0;
11551 consider_all_windows_p |= buffer_shared > 1;
11552 ++clear_face_cache_count;
11553 #ifdef HAVE_WINDOW_SYSTEM
11554 ++clear_image_cache_count;
11555 #endif
11556
11557 /* Build desired matrices, and update the display. If
11558 consider_all_windows_p is non-zero, do it for all windows on all
11559 frames. Otherwise do it for selected_window, only. */
11560
11561 if (consider_all_windows_p)
11562 {
11563 Lisp_Object tail, frame;
11564
11565 FOR_EACH_FRAME (tail, frame)
11566 XFRAME (frame)->updated_p = 0;
11567
11568 /* Recompute # windows showing selected buffer. This will be
11569 incremented each time such a window is displayed. */
11570 buffer_shared = 0;
11571
11572 FOR_EACH_FRAME (tail, frame)
11573 {
11574 struct frame *f = XFRAME (frame);
11575
11576 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
11577 {
11578 if (! EQ (frame, selected_frame))
11579 /* Select the frame, for the sake of frame-local
11580 variables. */
11581 select_frame_for_redisplay (frame);
11582
11583 /* Mark all the scroll bars to be removed; we'll redeem
11584 the ones we want when we redisplay their windows. */
11585 if (FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
11586 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
11587
11588 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
11589 redisplay_windows (FRAME_ROOT_WINDOW (f));
11590
11591 /* Any scroll bars which redisplay_windows should have
11592 nuked should now go away. */
11593 if (FRAME_TERMINAL (f)->judge_scroll_bars_hook)
11594 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
11595
11596 /* If fonts changed, display again. */
11597 /* ??? rms: I suspect it is a mistake to jump all the way
11598 back to retry here. It should just retry this frame. */
11599 if (fonts_changed_p)
11600 goto retry;
11601
11602 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
11603 {
11604 /* See if we have to hscroll. */
11605 if (!f->already_hscrolled_p)
11606 {
11607 f->already_hscrolled_p = 1;
11608 if (hscroll_windows (f->root_window))
11609 goto retry;
11610 }
11611
11612 /* Prevent various kinds of signals during display
11613 update. stdio is not robust about handling
11614 signals, which can cause an apparent I/O
11615 error. */
11616 if (interrupt_input)
11617 unrequest_sigio ();
11618 STOP_POLLING;
11619
11620 /* Update the display. */
11621 set_window_update_flags (XWINDOW (f->root_window), 1);
11622 pause |= update_frame (f, 0, 0);
11623 #if 0 /* Exiting the loop can leave the wrong value for buffer_shared. */
11624 if (pause)
11625 break;
11626 #endif
11627
11628 f->updated_p = 1;
11629 }
11630 }
11631 }
11632
11633 if (!pause)
11634 {
11635 /* Do the mark_window_display_accurate after all windows have
11636 been redisplayed because this call resets flags in buffers
11637 which are needed for proper redisplay. */
11638 FOR_EACH_FRAME (tail, frame)
11639 {
11640 struct frame *f = XFRAME (frame);
11641 if (f->updated_p)
11642 {
11643 mark_window_display_accurate (f->root_window, 1);
11644 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
11645 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
11646 }
11647 }
11648 }
11649 }
11650 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
11651 {
11652 Lisp_Object mini_window;
11653 struct frame *mini_frame;
11654
11655 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
11656 /* Use list_of_error, not Qerror, so that
11657 we catch only errors and don't run the debugger. */
11658 internal_condition_case_1 (redisplay_window_1, selected_window,
11659 list_of_error,
11660 redisplay_window_error);
11661
11662 /* Compare desired and current matrices, perform output. */
11663
11664 update:
11665 /* If fonts changed, display again. */
11666 if (fonts_changed_p)
11667 goto retry;
11668
11669 /* Prevent various kinds of signals during display update.
11670 stdio is not robust about handling signals,
11671 which can cause an apparent I/O error. */
11672 if (interrupt_input)
11673 unrequest_sigio ();
11674 STOP_POLLING;
11675
11676 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
11677 {
11678 if (hscroll_windows (selected_window))
11679 goto retry;
11680
11681 XWINDOW (selected_window)->must_be_updated_p = 1;
11682 pause = update_frame (sf, 0, 0);
11683 }
11684
11685 /* We may have called echo_area_display at the top of this
11686 function. If the echo area is on another frame, that may
11687 have put text on a frame other than the selected one, so the
11688 above call to update_frame would not have caught it. Catch
11689 it here. */
11690 mini_window = FRAME_MINIBUF_WINDOW (sf);
11691 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
11692
11693 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
11694 {
11695 XWINDOW (mini_window)->must_be_updated_p = 1;
11696 pause |= update_frame (mini_frame, 0, 0);
11697 if (!pause && hscroll_windows (mini_window))
11698 goto retry;
11699 }
11700 }
11701
11702 /* If display was paused because of pending input, make sure we do a
11703 thorough update the next time. */
11704 if (pause)
11705 {
11706 /* Prevent the optimization at the beginning of
11707 redisplay_internal that tries a single-line update of the
11708 line containing the cursor in the selected window. */
11709 CHARPOS (this_line_start_pos) = 0;
11710
11711 /* Let the overlay arrow be updated the next time. */
11712 update_overlay_arrows (0);
11713
11714 /* If we pause after scrolling, some rows in the current
11715 matrices of some windows are not valid. */
11716 if (!WINDOW_FULL_WIDTH_P (w)
11717 && !FRAME_WINDOW_P (XFRAME (w->frame)))
11718 update_mode_lines = 1;
11719 }
11720 else
11721 {
11722 if (!consider_all_windows_p)
11723 {
11724 /* This has already been done above if
11725 consider_all_windows_p is set. */
11726 mark_window_display_accurate_1 (w, 1);
11727
11728 /* Say overlay arrows are up to date. */
11729 update_overlay_arrows (1);
11730
11731 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
11732 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
11733 }
11734
11735 update_mode_lines = 0;
11736 windows_or_buffers_changed = 0;
11737 cursor_type_changed = 0;
11738 }
11739
11740 /* Start SIGIO interrupts coming again. Having them off during the
11741 code above makes it less likely one will discard output, but not
11742 impossible, since there might be stuff in the system buffer here.
11743 But it is much hairier to try to do anything about that. */
11744 if (interrupt_input)
11745 request_sigio ();
11746 RESUME_POLLING;
11747
11748 /* If a frame has become visible which was not before, redisplay
11749 again, so that we display it. Expose events for such a frame
11750 (which it gets when becoming visible) don't call the parts of
11751 redisplay constructing glyphs, so simply exposing a frame won't
11752 display anything in this case. So, we have to display these
11753 frames here explicitly. */
11754 if (!pause)
11755 {
11756 Lisp_Object tail, frame;
11757 int new_count = 0;
11758
11759 FOR_EACH_FRAME (tail, frame)
11760 {
11761 int this_is_visible = 0;
11762
11763 if (XFRAME (frame)->visible)
11764 this_is_visible = 1;
11765 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
11766 if (XFRAME (frame)->visible)
11767 this_is_visible = 1;
11768
11769 if (this_is_visible)
11770 new_count++;
11771 }
11772
11773 if (new_count != number_of_visible_frames)
11774 windows_or_buffers_changed++;
11775 }
11776
11777 /* Change frame size now if a change is pending. */
11778 do_pending_window_change (1);
11779
11780 /* If we just did a pending size change, or have additional
11781 visible frames, redisplay again. */
11782 if (windows_or_buffers_changed && !pause)
11783 goto retry;
11784
11785 /* Clear the face cache eventually. */
11786 if (consider_all_windows_p)
11787 {
11788 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
11789 {
11790 clear_face_cache (0);
11791 clear_face_cache_count = 0;
11792 }
11793 #ifdef HAVE_WINDOW_SYSTEM
11794 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
11795 {
11796 Lisp_Object tail, frame;
11797 FOR_EACH_FRAME (tail, frame)
11798 {
11799 struct frame *f = XFRAME (frame);
11800 if (FRAME_WINDOW_P (f))
11801 clear_image_cache (f, 0);
11802 }
11803 clear_image_cache_count = 0;
11804 }
11805 #endif /* HAVE_WINDOW_SYSTEM */
11806 }
11807
11808 end_of_redisplay:
11809 unbind_to (count, Qnil);
11810 RESUME_POLLING;
11811 }
11812
11813
11814 /* Redisplay, but leave alone any recent echo area message unless
11815 another message has been requested in its place.
11816
11817 This is useful in situations where you need to redisplay but no
11818 user action has occurred, making it inappropriate for the message
11819 area to be cleared. See tracking_off and
11820 wait_reading_process_output for examples of these situations.
11821
11822 FROM_WHERE is an integer saying from where this function was
11823 called. This is useful for debugging. */
11824
11825 void
11826 redisplay_preserve_echo_area (from_where)
11827 int from_where;
11828 {
11829 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
11830
11831 if (!NILP (echo_area_buffer[1]))
11832 {
11833 /* We have a previously displayed message, but no current
11834 message. Redisplay the previous message. */
11835 display_last_displayed_message_p = 1;
11836 redisplay_internal (1);
11837 display_last_displayed_message_p = 0;
11838 }
11839 else
11840 redisplay_internal (1);
11841
11842 if (FRAME_RIF (SELECTED_FRAME ()) != NULL
11843 && FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
11844 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (NULL);
11845 }
11846
11847
11848 /* Function registered with record_unwind_protect in
11849 redisplay_internal. Reset redisplaying_p to the value it had
11850 before redisplay_internal was called, and clear
11851 prevent_freeing_realized_faces_p. It also selects the previously
11852 selected frame, unless it has been deleted (by an X connection
11853 failure during redisplay, for example). */
11854
11855 static Lisp_Object
11856 unwind_redisplay (val)
11857 Lisp_Object val;
11858 {
11859 Lisp_Object old_redisplaying_p, old_frame;
11860
11861 old_redisplaying_p = XCAR (val);
11862 redisplaying_p = XFASTINT (old_redisplaying_p);
11863 old_frame = XCDR (val);
11864 if (! EQ (old_frame, selected_frame)
11865 && FRAME_LIVE_P (XFRAME (old_frame)))
11866 select_frame_for_redisplay (old_frame);
11867 return Qnil;
11868 }
11869
11870
11871 /* Mark the display of window W as accurate or inaccurate. If
11872 ACCURATE_P is non-zero mark display of W as accurate. If
11873 ACCURATE_P is zero, arrange for W to be redisplayed the next time
11874 redisplay_internal is called. */
11875
11876 static void
11877 mark_window_display_accurate_1 (w, accurate_p)
11878 struct window *w;
11879 int accurate_p;
11880 {
11881 if (BUFFERP (w->buffer))
11882 {
11883 struct buffer *b = XBUFFER (w->buffer);
11884
11885 w->last_modified
11886 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
11887 w->last_overlay_modified
11888 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
11889 w->last_had_star
11890 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
11891
11892 if (accurate_p)
11893 {
11894 b->clip_changed = 0;
11895 b->prevent_redisplay_optimizations_p = 0;
11896
11897 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
11898 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
11899 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
11900 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
11901
11902 w->current_matrix->buffer = b;
11903 w->current_matrix->begv = BUF_BEGV (b);
11904 w->current_matrix->zv = BUF_ZV (b);
11905
11906 w->last_cursor = w->cursor;
11907 w->last_cursor_off_p = w->cursor_off_p;
11908
11909 if (w == XWINDOW (selected_window))
11910 w->last_point = make_number (BUF_PT (b));
11911 else
11912 w->last_point = make_number (XMARKER (w->pointm)->charpos);
11913 }
11914 }
11915
11916 if (accurate_p)
11917 {
11918 w->window_end_valid = w->buffer;
11919 #if 0 /* This is incorrect with variable-height lines. */
11920 xassert (XINT (w->window_end_vpos)
11921 < (WINDOW_TOTAL_LINES (w)
11922 - (WINDOW_WANTS_MODELINE_P (w) ? 1 : 0)));
11923 #endif
11924 w->update_mode_line = Qnil;
11925 }
11926 }
11927
11928
11929 /* Mark the display of windows in the window tree rooted at WINDOW as
11930 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
11931 windows as accurate. If ACCURATE_P is zero, arrange for windows to
11932 be redisplayed the next time redisplay_internal is called. */
11933
11934 void
11935 mark_window_display_accurate (window, accurate_p)
11936 Lisp_Object window;
11937 int accurate_p;
11938 {
11939 struct window *w;
11940
11941 for (; !NILP (window); window = w->next)
11942 {
11943 w = XWINDOW (window);
11944 mark_window_display_accurate_1 (w, accurate_p);
11945
11946 if (!NILP (w->vchild))
11947 mark_window_display_accurate (w->vchild, accurate_p);
11948 if (!NILP (w->hchild))
11949 mark_window_display_accurate (w->hchild, accurate_p);
11950 }
11951
11952 if (accurate_p)
11953 {
11954 update_overlay_arrows (1);
11955 }
11956 else
11957 {
11958 /* Force a thorough redisplay the next time by setting
11959 last_arrow_position and last_arrow_string to t, which is
11960 unequal to any useful value of Voverlay_arrow_... */
11961 update_overlay_arrows (-1);
11962 }
11963 }
11964
11965
11966 /* Return value in display table DP (Lisp_Char_Table *) for character
11967 C. Since a display table doesn't have any parent, we don't have to
11968 follow parent. Do not call this function directly but use the
11969 macro DISP_CHAR_VECTOR. */
11970
11971 Lisp_Object
11972 disp_char_vector (dp, c)
11973 struct Lisp_Char_Table *dp;
11974 int c;
11975 {
11976 Lisp_Object val;
11977
11978 if (ASCII_CHAR_P (c))
11979 {
11980 val = dp->ascii;
11981 if (SUB_CHAR_TABLE_P (val))
11982 val = XSUB_CHAR_TABLE (val)->contents[c];
11983 }
11984 else
11985 {
11986 Lisp_Object table;
11987
11988 XSETCHAR_TABLE (table, dp);
11989 val = char_table_ref (table, c);
11990 }
11991 if (NILP (val))
11992 val = dp->defalt;
11993 return val;
11994 }
11995
11996
11997 \f
11998 /***********************************************************************
11999 Window Redisplay
12000 ***********************************************************************/
12001
12002 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
12003
12004 static void
12005 redisplay_windows (window)
12006 Lisp_Object window;
12007 {
12008 while (!NILP (window))
12009 {
12010 struct window *w = XWINDOW (window);
12011
12012 if (!NILP (w->hchild))
12013 redisplay_windows (w->hchild);
12014 else if (!NILP (w->vchild))
12015 redisplay_windows (w->vchild);
12016 else
12017 {
12018 displayed_buffer = XBUFFER (w->buffer);
12019 /* Use list_of_error, not Qerror, so that
12020 we catch only errors and don't run the debugger. */
12021 internal_condition_case_1 (redisplay_window_0, window,
12022 list_of_error,
12023 redisplay_window_error);
12024 }
12025
12026 window = w->next;
12027 }
12028 }
12029
12030 static Lisp_Object
12031 redisplay_window_error ()
12032 {
12033 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
12034 return Qnil;
12035 }
12036
12037 static Lisp_Object
12038 redisplay_window_0 (window)
12039 Lisp_Object window;
12040 {
12041 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
12042 redisplay_window (window, 0);
12043 return Qnil;
12044 }
12045
12046 static Lisp_Object
12047 redisplay_window_1 (window)
12048 Lisp_Object window;
12049 {
12050 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
12051 redisplay_window (window, 1);
12052 return Qnil;
12053 }
12054 \f
12055
12056 /* Increment GLYPH until it reaches END or CONDITION fails while
12057 adding (GLYPH)->pixel_width to X. */
12058
12059 #define SKIP_GLYPHS(glyph, end, x, condition) \
12060 do \
12061 { \
12062 (x) += (glyph)->pixel_width; \
12063 ++(glyph); \
12064 } \
12065 while ((glyph) < (end) && (condition))
12066
12067
12068 /* Set cursor position of W. PT is assumed to be displayed in ROW.
12069 DELTA is the number of bytes by which positions recorded in ROW
12070 differ from current buffer positions.
12071
12072 Return 0 if cursor is not on this row. 1 otherwise. */
12073
12074 int
12075 set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
12076 struct window *w;
12077 struct glyph_row *row;
12078 struct glyph_matrix *matrix;
12079 int delta, delta_bytes, dy, dvpos;
12080 {
12081 struct glyph *glyph = row->glyphs[TEXT_AREA];
12082 struct glyph *end = glyph + row->used[TEXT_AREA];
12083 struct glyph *cursor = NULL;
12084 /* The first glyph that starts a sequence of glyphs from string. */
12085 struct glyph *string_start;
12086 /* The X coordinate of string_start. */
12087 int string_start_x;
12088 /* The last known character position. */
12089 int last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
12090 /* The last known character position before string_start. */
12091 int string_before_pos;
12092 int x = row->x;
12093 int cursor_x = x;
12094 int cursor_from_overlay_pos = 0;
12095 int pt_old = PT - delta;
12096
12097 /* Skip over glyphs not having an object at the start of the row.
12098 These are special glyphs like truncation marks on terminal
12099 frames. */
12100 if (row->displays_text_p)
12101 while (glyph < end
12102 && INTEGERP (glyph->object)
12103 && glyph->charpos < 0)
12104 {
12105 x += glyph->pixel_width;
12106 ++glyph;
12107 }
12108
12109 string_start = NULL;
12110 while (glyph < end
12111 && !INTEGERP (glyph->object)
12112 && (!BUFFERP (glyph->object)
12113 || (last_pos = glyph->charpos) < pt_old))
12114 {
12115 if (! STRINGP (glyph->object))
12116 {
12117 string_start = NULL;
12118 x += glyph->pixel_width;
12119 ++glyph;
12120 if (cursor_from_overlay_pos
12121 && last_pos >= cursor_from_overlay_pos)
12122 {
12123 cursor_from_overlay_pos = 0;
12124 cursor = 0;
12125 }
12126 }
12127 else
12128 {
12129 if (string_start == NULL)
12130 {
12131 string_before_pos = last_pos;
12132 string_start = glyph;
12133 string_start_x = x;
12134 }
12135 /* Skip all glyphs from string. */
12136 do
12137 {
12138 Lisp_Object cprop;
12139 int pos;
12140 if ((cursor == NULL || glyph > cursor)
12141 && (cprop = Fget_char_property (make_number ((glyph)->charpos),
12142 Qcursor, (glyph)->object),
12143 !NILP (cprop))
12144 && (pos = string_buffer_position (w, glyph->object,
12145 string_before_pos),
12146 (pos == 0 /* From overlay */
12147 || pos == pt_old)))
12148 {
12149 /* Estimate overlay buffer position from the buffer
12150 positions of the glyphs before and after the overlay.
12151 Add 1 to last_pos so that if point corresponds to the
12152 glyph right after the overlay, we still use a 'cursor'
12153 property found in that overlay. */
12154 cursor_from_overlay_pos = (pos ? 0 : last_pos
12155 + (INTEGERP (cprop) ? XINT (cprop) : 0));
12156 cursor = glyph;
12157 cursor_x = x;
12158 }
12159 x += glyph->pixel_width;
12160 ++glyph;
12161 }
12162 while (glyph < end && EQ (glyph->object, string_start->object));
12163 }
12164 }
12165
12166 if (cursor != NULL)
12167 {
12168 glyph = cursor;
12169 x = cursor_x;
12170 }
12171 else if (row->ends_in_ellipsis_p && glyph == end)
12172 {
12173 /* Scan back over the ellipsis glyphs, decrementing positions. */
12174 while (glyph > row->glyphs[TEXT_AREA]
12175 && (glyph - 1)->charpos == last_pos)
12176 glyph--, x -= glyph->pixel_width;
12177 /* That loop always goes one position too far,
12178 including the glyph before the ellipsis.
12179 So scan forward over that one. */
12180 x += glyph->pixel_width;
12181 glyph++;
12182 }
12183 else if (string_start
12184 && (glyph == end || !BUFFERP (glyph->object) || last_pos > pt_old))
12185 {
12186 /* We may have skipped over point because the previous glyphs
12187 are from string. As there's no easy way to know the
12188 character position of the current glyph, find the correct
12189 glyph on point by scanning from string_start again. */
12190 Lisp_Object limit;
12191 Lisp_Object string;
12192 struct glyph *stop = glyph;
12193 int pos;
12194
12195 limit = make_number (pt_old + 1);
12196 glyph = string_start;
12197 x = string_start_x;
12198 string = glyph->object;
12199 pos = string_buffer_position (w, string, string_before_pos);
12200 /* If STRING is from overlay, LAST_POS == 0. We skip such glyphs
12201 because we always put cursor after overlay strings. */
12202 while (pos == 0 && glyph < stop)
12203 {
12204 string = glyph->object;
12205 SKIP_GLYPHS (glyph, stop, x, EQ (glyph->object, string));
12206 if (glyph < stop)
12207 pos = string_buffer_position (w, glyph->object, string_before_pos);
12208 }
12209
12210 while (glyph < stop)
12211 {
12212 pos = XINT (Fnext_single_char_property_change
12213 (make_number (pos), Qdisplay, Qnil, limit));
12214 if (pos > pt_old)
12215 break;
12216 /* Skip glyphs from the same string. */
12217 string = glyph->object;
12218 SKIP_GLYPHS (glyph, stop, x, EQ (glyph->object, string));
12219 /* Skip glyphs from an overlay. */
12220 while (glyph < stop
12221 && ! string_buffer_position (w, glyph->object, pos))
12222 {
12223 string = glyph->object;
12224 SKIP_GLYPHS (glyph, stop, x, EQ (glyph->object, string));
12225 }
12226 }
12227
12228 /* If we reached the end of the line, and end was from a string,
12229 cursor is not on this line. */
12230 if (glyph == end && row->continued_p)
12231 return 0;
12232 }
12233
12234 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
12235 w->cursor.x = x;
12236 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
12237 w->cursor.y = row->y + dy;
12238
12239 if (w == XWINDOW (selected_window))
12240 {
12241 if (!row->continued_p
12242 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
12243 && row->x == 0)
12244 {
12245 this_line_buffer = XBUFFER (w->buffer);
12246
12247 CHARPOS (this_line_start_pos)
12248 = MATRIX_ROW_START_CHARPOS (row) + delta;
12249 BYTEPOS (this_line_start_pos)
12250 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
12251
12252 CHARPOS (this_line_end_pos)
12253 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
12254 BYTEPOS (this_line_end_pos)
12255 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
12256
12257 this_line_y = w->cursor.y;
12258 this_line_pixel_height = row->height;
12259 this_line_vpos = w->cursor.vpos;
12260 this_line_start_x = row->x;
12261 }
12262 else
12263 CHARPOS (this_line_start_pos) = 0;
12264 }
12265
12266 return 1;
12267 }
12268
12269
12270 /* Run window scroll functions, if any, for WINDOW with new window
12271 start STARTP. Sets the window start of WINDOW to that position.
12272
12273 We assume that the window's buffer is really current. */
12274
12275 static INLINE struct text_pos
12276 run_window_scroll_functions (window, startp)
12277 Lisp_Object window;
12278 struct text_pos startp;
12279 {
12280 struct window *w = XWINDOW (window);
12281 SET_MARKER_FROM_TEXT_POS (w->start, startp);
12282
12283 if (current_buffer != XBUFFER (w->buffer))
12284 abort ();
12285
12286 if (!NILP (Vwindow_scroll_functions))
12287 {
12288 run_hook_with_args_2 (Qwindow_scroll_functions, window,
12289 make_number (CHARPOS (startp)));
12290 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12291 /* In case the hook functions switch buffers. */
12292 if (current_buffer != XBUFFER (w->buffer))
12293 set_buffer_internal_1 (XBUFFER (w->buffer));
12294 }
12295
12296 return startp;
12297 }
12298
12299
12300 /* Make sure the line containing the cursor is fully visible.
12301 A value of 1 means there is nothing to be done.
12302 (Either the line is fully visible, or it cannot be made so,
12303 or we cannot tell.)
12304
12305 If FORCE_P is non-zero, return 0 even if partial visible cursor row
12306 is higher than window.
12307
12308 A value of 0 means the caller should do scrolling
12309 as if point had gone off the screen. */
12310
12311 static int
12312 cursor_row_fully_visible_p (w, force_p, current_matrix_p)
12313 struct window *w;
12314 int force_p;
12315 int current_matrix_p;
12316 {
12317 struct glyph_matrix *matrix;
12318 struct glyph_row *row;
12319 int window_height;
12320
12321 if (!make_cursor_line_fully_visible_p)
12322 return 1;
12323
12324 /* It's not always possible to find the cursor, e.g, when a window
12325 is full of overlay strings. Don't do anything in that case. */
12326 if (w->cursor.vpos < 0)
12327 return 1;
12328
12329 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
12330 row = MATRIX_ROW (matrix, w->cursor.vpos);
12331
12332 /* If the cursor row is not partially visible, there's nothing to do. */
12333 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
12334 return 1;
12335
12336 /* If the row the cursor is in is taller than the window's height,
12337 it's not clear what to do, so do nothing. */
12338 window_height = window_box_height (w);
12339 if (row->height >= window_height)
12340 {
12341 if (!force_p || MINI_WINDOW_P (w)
12342 || w->vscroll || w->cursor.vpos == 0)
12343 return 1;
12344 }
12345 return 0;
12346
12347 #if 0
12348 /* This code used to try to scroll the window just enough to make
12349 the line visible. It returned 0 to say that the caller should
12350 allocate larger glyph matrices. */
12351
12352 if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (w, row))
12353 {
12354 int dy = row->height - row->visible_height;
12355 w->vscroll = 0;
12356 w->cursor.y += dy;
12357 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
12358 }
12359 else /* MATRIX_ROW_PARTIALLY_VISIBLE_AT_BOTTOM_P (w, row)) */
12360 {
12361 int dy = - (row->height - row->visible_height);
12362 w->vscroll = dy;
12363 w->cursor.y += dy;
12364 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
12365 }
12366
12367 /* When we change the cursor y-position of the selected window,
12368 change this_line_y as well so that the display optimization for
12369 the cursor line of the selected window in redisplay_internal uses
12370 the correct y-position. */
12371 if (w == XWINDOW (selected_window))
12372 this_line_y = w->cursor.y;
12373
12374 /* If vscrolling requires a larger glyph matrix, arrange for a fresh
12375 redisplay with larger matrices. */
12376 if (matrix->nrows < required_matrix_height (w))
12377 {
12378 fonts_changed_p = 1;
12379 return 0;
12380 }
12381
12382 return 1;
12383 #endif /* 0 */
12384 }
12385
12386
12387 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
12388 non-zero means only WINDOW is redisplayed in redisplay_internal.
12389 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
12390 in redisplay_window to bring a partially visible line into view in
12391 the case that only the cursor has moved.
12392
12393 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
12394 last screen line's vertical height extends past the end of the screen.
12395
12396 Value is
12397
12398 1 if scrolling succeeded
12399
12400 0 if scrolling didn't find point.
12401
12402 -1 if new fonts have been loaded so that we must interrupt
12403 redisplay, adjust glyph matrices, and try again. */
12404
12405 enum
12406 {
12407 SCROLLING_SUCCESS,
12408 SCROLLING_FAILED,
12409 SCROLLING_NEED_LARGER_MATRICES
12410 };
12411
12412 static int
12413 try_scrolling (window, just_this_one_p, scroll_conservatively,
12414 scroll_step, temp_scroll_step, last_line_misfit)
12415 Lisp_Object window;
12416 int just_this_one_p;
12417 EMACS_INT scroll_conservatively, scroll_step;
12418 int temp_scroll_step;
12419 int last_line_misfit;
12420 {
12421 struct window *w = XWINDOW (window);
12422 struct frame *f = XFRAME (w->frame);
12423 struct text_pos scroll_margin_pos;
12424 struct text_pos pos;
12425 struct text_pos startp;
12426 struct it it;
12427 Lisp_Object window_end;
12428 int this_scroll_margin;
12429 int dy = 0;
12430 int scroll_max;
12431 int rc;
12432 int amount_to_scroll = 0;
12433 Lisp_Object aggressive;
12434 int height;
12435 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
12436
12437 #if GLYPH_DEBUG
12438 debug_method_add (w, "try_scrolling");
12439 #endif
12440
12441 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12442
12443 /* Compute scroll margin height in pixels. We scroll when point is
12444 within this distance from the top or bottom of the window. */
12445 if (scroll_margin > 0)
12446 {
12447 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
12448 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
12449 }
12450 else
12451 this_scroll_margin = 0;
12452
12453 /* Force scroll_conservatively to have a reasonable value so it doesn't
12454 cause an overflow while computing how much to scroll. */
12455 if (scroll_conservatively)
12456 scroll_conservatively = min (scroll_conservatively,
12457 MOST_POSITIVE_FIXNUM / FRAME_LINE_HEIGHT (f));
12458
12459 /* Compute how much we should try to scroll maximally to bring point
12460 into view. */
12461 if (scroll_step || scroll_conservatively || temp_scroll_step)
12462 scroll_max = max (scroll_step,
12463 max (scroll_conservatively, temp_scroll_step));
12464 else if (NUMBERP (current_buffer->scroll_down_aggressively)
12465 || NUMBERP (current_buffer->scroll_up_aggressively))
12466 /* We're trying to scroll because of aggressive scrolling
12467 but no scroll_step is set. Choose an arbitrary one. Maybe
12468 there should be a variable for this. */
12469 scroll_max = 10;
12470 else
12471 scroll_max = 0;
12472 scroll_max *= FRAME_LINE_HEIGHT (f);
12473
12474 /* Decide whether we have to scroll down. Start at the window end
12475 and move this_scroll_margin up to find the position of the scroll
12476 margin. */
12477 window_end = Fwindow_end (window, Qt);
12478
12479 too_near_end:
12480
12481 CHARPOS (scroll_margin_pos) = XINT (window_end);
12482 BYTEPOS (scroll_margin_pos) = CHAR_TO_BYTE (CHARPOS (scroll_margin_pos));
12483
12484 if (this_scroll_margin || extra_scroll_margin_lines)
12485 {
12486 start_display (&it, w, scroll_margin_pos);
12487 if (this_scroll_margin)
12488 move_it_vertically_backward (&it, this_scroll_margin);
12489 if (extra_scroll_margin_lines)
12490 move_it_by_lines (&it, - extra_scroll_margin_lines, 0);
12491 scroll_margin_pos = it.current.pos;
12492 }
12493
12494 if (PT >= CHARPOS (scroll_margin_pos))
12495 {
12496 int y0;
12497
12498 /* Point is in the scroll margin at the bottom of the window, or
12499 below. Compute a new window start that makes point visible. */
12500
12501 /* Compute the distance from the scroll margin to PT.
12502 Give up if the distance is greater than scroll_max. */
12503 start_display (&it, w, scroll_margin_pos);
12504 y0 = it.current_y;
12505 move_it_to (&it, PT, 0, it.last_visible_y, -1,
12506 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
12507
12508 /* To make point visible, we have to move the window start
12509 down so that the line the cursor is in is visible, which
12510 means we have to add in the height of the cursor line. */
12511 dy = line_bottom_y (&it) - y0;
12512
12513 if (dy > scroll_max)
12514 return SCROLLING_FAILED;
12515
12516 /* Move the window start down. If scrolling conservatively,
12517 move it just enough down to make point visible. If
12518 scroll_step is set, move it down by scroll_step. */
12519 start_display (&it, w, startp);
12520
12521 if (scroll_conservatively)
12522 /* Set AMOUNT_TO_SCROLL to at least one line,
12523 and at most scroll_conservatively lines. */
12524 amount_to_scroll
12525 = min (max (dy, FRAME_LINE_HEIGHT (f)),
12526 FRAME_LINE_HEIGHT (f) * scroll_conservatively);
12527 else if (scroll_step || temp_scroll_step)
12528 amount_to_scroll = scroll_max;
12529 else
12530 {
12531 aggressive = current_buffer->scroll_up_aggressively;
12532 height = WINDOW_BOX_TEXT_HEIGHT (w);
12533 if (NUMBERP (aggressive))
12534 {
12535 double float_amount = XFLOATINT (aggressive) * height;
12536 amount_to_scroll = float_amount;
12537 if (amount_to_scroll == 0 && float_amount > 0)
12538 amount_to_scroll = 1;
12539 }
12540 }
12541
12542 if (amount_to_scroll <= 0)
12543 return SCROLLING_FAILED;
12544
12545 /* If moving by amount_to_scroll leaves STARTP unchanged,
12546 move it down one screen line. */
12547
12548 move_it_vertically (&it, amount_to_scroll);
12549 if (CHARPOS (it.current.pos) == CHARPOS (startp))
12550 move_it_by_lines (&it, 1, 1);
12551 startp = it.current.pos;
12552 }
12553 else
12554 {
12555 /* See if point is inside the scroll margin at the top of the
12556 window. */
12557 scroll_margin_pos = startp;
12558 if (this_scroll_margin)
12559 {
12560 start_display (&it, w, startp);
12561 move_it_vertically (&it, this_scroll_margin);
12562 scroll_margin_pos = it.current.pos;
12563 }
12564
12565 if (PT < CHARPOS (scroll_margin_pos))
12566 {
12567 /* Point is in the scroll margin at the top of the window or
12568 above what is displayed in the window. */
12569 int y0;
12570
12571 /* Compute the vertical distance from PT to the scroll
12572 margin position. Give up if distance is greater than
12573 scroll_max. */
12574 SET_TEXT_POS (pos, PT, PT_BYTE);
12575 start_display (&it, w, pos);
12576 y0 = it.current_y;
12577 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
12578 it.last_visible_y, -1,
12579 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
12580 dy = it.current_y - y0;
12581 if (dy > scroll_max)
12582 return SCROLLING_FAILED;
12583
12584 /* Compute new window start. */
12585 start_display (&it, w, startp);
12586
12587 if (scroll_conservatively)
12588 amount_to_scroll
12589 = max (dy, FRAME_LINE_HEIGHT (f) * max (scroll_step, temp_scroll_step));
12590 else if (scroll_step || temp_scroll_step)
12591 amount_to_scroll = scroll_max;
12592 else
12593 {
12594 aggressive = current_buffer->scroll_down_aggressively;
12595 height = WINDOW_BOX_TEXT_HEIGHT (w);
12596 if (NUMBERP (aggressive))
12597 {
12598 double float_amount = XFLOATINT (aggressive) * height;
12599 amount_to_scroll = float_amount;
12600 if (amount_to_scroll == 0 && float_amount > 0)
12601 amount_to_scroll = 1;
12602 }
12603 }
12604
12605 if (amount_to_scroll <= 0)
12606 return SCROLLING_FAILED;
12607
12608 move_it_vertically_backward (&it, amount_to_scroll);
12609 startp = it.current.pos;
12610 }
12611 }
12612
12613 /* Run window scroll functions. */
12614 startp = run_window_scroll_functions (window, startp);
12615
12616 /* Display the window. Give up if new fonts are loaded, or if point
12617 doesn't appear. */
12618 if (!try_window (window, startp, 0))
12619 rc = SCROLLING_NEED_LARGER_MATRICES;
12620 else if (w->cursor.vpos < 0)
12621 {
12622 clear_glyph_matrix (w->desired_matrix);
12623 rc = SCROLLING_FAILED;
12624 }
12625 else
12626 {
12627 /* Maybe forget recorded base line for line number display. */
12628 if (!just_this_one_p
12629 || current_buffer->clip_changed
12630 || BEG_UNCHANGED < CHARPOS (startp))
12631 w->base_line_number = Qnil;
12632
12633 /* If cursor ends up on a partially visible line,
12634 treat that as being off the bottom of the screen. */
12635 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0))
12636 {
12637 clear_glyph_matrix (w->desired_matrix);
12638 ++extra_scroll_margin_lines;
12639 goto too_near_end;
12640 }
12641 rc = SCROLLING_SUCCESS;
12642 }
12643
12644 return rc;
12645 }
12646
12647
12648 /* Compute a suitable window start for window W if display of W starts
12649 on a continuation line. Value is non-zero if a new window start
12650 was computed.
12651
12652 The new window start will be computed, based on W's width, starting
12653 from the start of the continued line. It is the start of the
12654 screen line with the minimum distance from the old start W->start. */
12655
12656 static int
12657 compute_window_start_on_continuation_line (w)
12658 struct window *w;
12659 {
12660 struct text_pos pos, start_pos;
12661 int window_start_changed_p = 0;
12662
12663 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
12664
12665 /* If window start is on a continuation line... Window start may be
12666 < BEGV in case there's invisible text at the start of the
12667 buffer (M-x rmail, for example). */
12668 if (CHARPOS (start_pos) > BEGV
12669 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
12670 {
12671 struct it it;
12672 struct glyph_row *row;
12673
12674 /* Handle the case that the window start is out of range. */
12675 if (CHARPOS (start_pos) < BEGV)
12676 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
12677 else if (CHARPOS (start_pos) > ZV)
12678 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
12679
12680 /* Find the start of the continued line. This should be fast
12681 because scan_buffer is fast (newline cache). */
12682 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
12683 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
12684 row, DEFAULT_FACE_ID);
12685 reseat_at_previous_visible_line_start (&it);
12686
12687 /* If the line start is "too far" away from the window start,
12688 say it takes too much time to compute a new window start. */
12689 if (CHARPOS (start_pos) - IT_CHARPOS (it)
12690 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
12691 {
12692 int min_distance, distance;
12693
12694 /* Move forward by display lines to find the new window
12695 start. If window width was enlarged, the new start can
12696 be expected to be > the old start. If window width was
12697 decreased, the new window start will be < the old start.
12698 So, we're looking for the display line start with the
12699 minimum distance from the old window start. */
12700 pos = it.current.pos;
12701 min_distance = INFINITY;
12702 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
12703 distance < min_distance)
12704 {
12705 min_distance = distance;
12706 pos = it.current.pos;
12707 move_it_by_lines (&it, 1, 0);
12708 }
12709
12710 /* Set the window start there. */
12711 SET_MARKER_FROM_TEXT_POS (w->start, pos);
12712 window_start_changed_p = 1;
12713 }
12714 }
12715
12716 return window_start_changed_p;
12717 }
12718
12719
12720 /* Try cursor movement in case text has not changed in window WINDOW,
12721 with window start STARTP. Value is
12722
12723 CURSOR_MOVEMENT_SUCCESS if successful
12724
12725 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
12726
12727 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
12728 display. *SCROLL_STEP is set to 1, under certain circumstances, if
12729 we want to scroll as if scroll-step were set to 1. See the code.
12730
12731 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
12732 which case we have to abort this redisplay, and adjust matrices
12733 first. */
12734
12735 enum
12736 {
12737 CURSOR_MOVEMENT_SUCCESS,
12738 CURSOR_MOVEMENT_CANNOT_BE_USED,
12739 CURSOR_MOVEMENT_MUST_SCROLL,
12740 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
12741 };
12742
12743 static int
12744 try_cursor_movement (window, startp, scroll_step)
12745 Lisp_Object window;
12746 struct text_pos startp;
12747 int *scroll_step;
12748 {
12749 struct window *w = XWINDOW (window);
12750 struct frame *f = XFRAME (w->frame);
12751 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
12752
12753 #if GLYPH_DEBUG
12754 if (inhibit_try_cursor_movement)
12755 return rc;
12756 #endif
12757
12758 /* Handle case where text has not changed, only point, and it has
12759 not moved off the frame. */
12760 if (/* Point may be in this window. */
12761 PT >= CHARPOS (startp)
12762 /* Selective display hasn't changed. */
12763 && !current_buffer->clip_changed
12764 /* Function force-mode-line-update is used to force a thorough
12765 redisplay. It sets either windows_or_buffers_changed or
12766 update_mode_lines. So don't take a shortcut here for these
12767 cases. */
12768 && !update_mode_lines
12769 && !windows_or_buffers_changed
12770 && !cursor_type_changed
12771 /* Can't use this case if highlighting a region. When a
12772 region exists, cursor movement has to do more than just
12773 set the cursor. */
12774 && !(!NILP (Vtransient_mark_mode)
12775 && !NILP (current_buffer->mark_active))
12776 && NILP (w->region_showing)
12777 && NILP (Vshow_trailing_whitespace)
12778 /* Right after splitting windows, last_point may be nil. */
12779 && INTEGERP (w->last_point)
12780 /* This code is not used for mini-buffer for the sake of the case
12781 of redisplaying to replace an echo area message; since in
12782 that case the mini-buffer contents per se are usually
12783 unchanged. This code is of no real use in the mini-buffer
12784 since the handling of this_line_start_pos, etc., in redisplay
12785 handles the same cases. */
12786 && !EQ (window, minibuf_window)
12787 /* When splitting windows or for new windows, it happens that
12788 redisplay is called with a nil window_end_vpos or one being
12789 larger than the window. This should really be fixed in
12790 window.c. I don't have this on my list, now, so we do
12791 approximately the same as the old redisplay code. --gerd. */
12792 && INTEGERP (w->window_end_vpos)
12793 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
12794 && (FRAME_WINDOW_P (f)
12795 || !overlay_arrow_in_current_buffer_p ()))
12796 {
12797 int this_scroll_margin, top_scroll_margin;
12798 struct glyph_row *row = NULL;
12799
12800 #if GLYPH_DEBUG
12801 debug_method_add (w, "cursor movement");
12802 #endif
12803
12804 /* Scroll if point within this distance from the top or bottom
12805 of the window. This is a pixel value. */
12806 this_scroll_margin = max (0, scroll_margin);
12807 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
12808 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
12809
12810 top_scroll_margin = this_scroll_margin;
12811 if (WINDOW_WANTS_HEADER_LINE_P (w))
12812 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
12813
12814 /* Start with the row the cursor was displayed during the last
12815 not paused redisplay. Give up if that row is not valid. */
12816 if (w->last_cursor.vpos < 0
12817 || w->last_cursor.vpos >= w->current_matrix->nrows)
12818 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12819 else
12820 {
12821 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
12822 if (row->mode_line_p)
12823 ++row;
12824 if (!row->enabled_p)
12825 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12826 }
12827
12828 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
12829 {
12830 int scroll_p = 0;
12831 int last_y = window_text_bottom_y (w) - this_scroll_margin;
12832
12833 if (PT > XFASTINT (w->last_point))
12834 {
12835 /* Point has moved forward. */
12836 while (MATRIX_ROW_END_CHARPOS (row) < PT
12837 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
12838 {
12839 xassert (row->enabled_p);
12840 ++row;
12841 }
12842
12843 /* The end position of a row equals the start position
12844 of the next row. If PT is there, we would rather
12845 display it in the next line. */
12846 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
12847 && MATRIX_ROW_END_CHARPOS (row) == PT
12848 && !cursor_row_p (w, row))
12849 ++row;
12850
12851 /* If within the scroll margin, scroll. Note that
12852 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
12853 the next line would be drawn, and that
12854 this_scroll_margin can be zero. */
12855 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
12856 || PT > MATRIX_ROW_END_CHARPOS (row)
12857 /* Line is completely visible last line in window
12858 and PT is to be set in the next line. */
12859 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
12860 && PT == MATRIX_ROW_END_CHARPOS (row)
12861 && !row->ends_at_zv_p
12862 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
12863 scroll_p = 1;
12864 }
12865 else if (PT < XFASTINT (w->last_point))
12866 {
12867 /* Cursor has to be moved backward. Note that PT >=
12868 CHARPOS (startp) because of the outer if-statement. */
12869 while (!row->mode_line_p
12870 && (MATRIX_ROW_START_CHARPOS (row) > PT
12871 || (MATRIX_ROW_START_CHARPOS (row) == PT
12872 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
12873 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
12874 row > w->current_matrix->rows
12875 && (row-1)->ends_in_newline_from_string_p))))
12876 && (row->y > top_scroll_margin
12877 || CHARPOS (startp) == BEGV))
12878 {
12879 xassert (row->enabled_p);
12880 --row;
12881 }
12882
12883 /* Consider the following case: Window starts at BEGV,
12884 there is invisible, intangible text at BEGV, so that
12885 display starts at some point START > BEGV. It can
12886 happen that we are called with PT somewhere between
12887 BEGV and START. Try to handle that case. */
12888 if (row < w->current_matrix->rows
12889 || row->mode_line_p)
12890 {
12891 row = w->current_matrix->rows;
12892 if (row->mode_line_p)
12893 ++row;
12894 }
12895
12896 /* Due to newlines in overlay strings, we may have to
12897 skip forward over overlay strings. */
12898 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
12899 && MATRIX_ROW_END_CHARPOS (row) == PT
12900 && !cursor_row_p (w, row))
12901 ++row;
12902
12903 /* If within the scroll margin, scroll. */
12904 if (row->y < top_scroll_margin
12905 && CHARPOS (startp) != BEGV)
12906 scroll_p = 1;
12907 }
12908 else
12909 {
12910 /* Cursor did not move. So don't scroll even if cursor line
12911 is partially visible, as it was so before. */
12912 rc = CURSOR_MOVEMENT_SUCCESS;
12913 }
12914
12915 if (PT < MATRIX_ROW_START_CHARPOS (row)
12916 || PT > MATRIX_ROW_END_CHARPOS (row))
12917 {
12918 /* if PT is not in the glyph row, give up. */
12919 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12920 }
12921 else if (rc != CURSOR_MOVEMENT_SUCCESS
12922 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
12923 && make_cursor_line_fully_visible_p)
12924 {
12925 if (PT == MATRIX_ROW_END_CHARPOS (row)
12926 && !row->ends_at_zv_p
12927 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
12928 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12929 else if (row->height > window_box_height (w))
12930 {
12931 /* If we end up in a partially visible line, let's
12932 make it fully visible, except when it's taller
12933 than the window, in which case we can't do much
12934 about it. */
12935 *scroll_step = 1;
12936 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12937 }
12938 else
12939 {
12940 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
12941 if (!cursor_row_fully_visible_p (w, 0, 1))
12942 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12943 else
12944 rc = CURSOR_MOVEMENT_SUCCESS;
12945 }
12946 }
12947 else if (scroll_p)
12948 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12949 else
12950 {
12951 do
12952 {
12953 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
12954 {
12955 rc = CURSOR_MOVEMENT_SUCCESS;
12956 break;
12957 }
12958 ++row;
12959 }
12960 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
12961 && MATRIX_ROW_START_CHARPOS (row) == PT
12962 && cursor_row_p (w, row));
12963 }
12964 }
12965 }
12966
12967 return rc;
12968 }
12969
12970 void
12971 set_vertical_scroll_bar (w)
12972 struct window *w;
12973 {
12974 int start, end, whole;
12975
12976 /* Calculate the start and end positions for the current window.
12977 At some point, it would be nice to choose between scrollbars
12978 which reflect the whole buffer size, with special markers
12979 indicating narrowing, and scrollbars which reflect only the
12980 visible region.
12981
12982 Note that mini-buffers sometimes aren't displaying any text. */
12983 if (!MINI_WINDOW_P (w)
12984 || (w == XWINDOW (minibuf_window)
12985 && NILP (echo_area_buffer[0])))
12986 {
12987 struct buffer *buf = XBUFFER (w->buffer);
12988 whole = BUF_ZV (buf) - BUF_BEGV (buf);
12989 start = marker_position (w->start) - BUF_BEGV (buf);
12990 /* I don't think this is guaranteed to be right. For the
12991 moment, we'll pretend it is. */
12992 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
12993
12994 if (end < start)
12995 end = start;
12996 if (whole < (end - start))
12997 whole = end - start;
12998 }
12999 else
13000 start = end = whole = 0;
13001
13002 /* Indicate what this scroll bar ought to be displaying now. */
13003 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
13004 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
13005 (w, end - start, whole, start);
13006 }
13007
13008
13009 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
13010 selected_window is redisplayed.
13011
13012 We can return without actually redisplaying the window if
13013 fonts_changed_p is nonzero. In that case, redisplay_internal will
13014 retry. */
13015
13016 static void
13017 redisplay_window (window, just_this_one_p)
13018 Lisp_Object window;
13019 int just_this_one_p;
13020 {
13021 struct window *w = XWINDOW (window);
13022 struct frame *f = XFRAME (w->frame);
13023 struct buffer *buffer = XBUFFER (w->buffer);
13024 struct buffer *old = current_buffer;
13025 struct text_pos lpoint, opoint, startp;
13026 int update_mode_line;
13027 int tem;
13028 struct it it;
13029 /* Record it now because it's overwritten. */
13030 int current_matrix_up_to_date_p = 0;
13031 int used_current_matrix_p = 0;
13032 /* This is less strict than current_matrix_up_to_date_p.
13033 It indictes that the buffer contents and narrowing are unchanged. */
13034 int buffer_unchanged_p = 0;
13035 int temp_scroll_step = 0;
13036 int count = SPECPDL_INDEX ();
13037 int rc;
13038 int centering_position = -1;
13039 int last_line_misfit = 0;
13040 int beg_unchanged, end_unchanged;
13041
13042 SET_TEXT_POS (lpoint, PT, PT_BYTE);
13043 opoint = lpoint;
13044
13045 /* W must be a leaf window here. */
13046 xassert (!NILP (w->buffer));
13047 #if GLYPH_DEBUG
13048 *w->desired_matrix->method = 0;
13049 #endif
13050
13051 specbind (Qinhibit_point_motion_hooks, Qt);
13052
13053 reconsider_clip_changes (w, buffer);
13054
13055 /* Has the mode line to be updated? */
13056 update_mode_line = (!NILP (w->update_mode_line)
13057 || update_mode_lines
13058 || buffer->clip_changed
13059 || buffer->prevent_redisplay_optimizations_p);
13060
13061 if (MINI_WINDOW_P (w))
13062 {
13063 if (w == XWINDOW (echo_area_window)
13064 && !NILP (echo_area_buffer[0]))
13065 {
13066 if (update_mode_line)
13067 /* We may have to update a tty frame's menu bar or a
13068 tool-bar. Example `M-x C-h C-h C-g'. */
13069 goto finish_menu_bars;
13070 else
13071 /* We've already displayed the echo area glyphs in this window. */
13072 goto finish_scroll_bars;
13073 }
13074 else if ((w != XWINDOW (minibuf_window)
13075 || minibuf_level == 0)
13076 /* When buffer is nonempty, redisplay window normally. */
13077 && BUF_Z (XBUFFER (w->buffer)) == BUF_BEG (XBUFFER (w->buffer))
13078 /* Quail displays non-mini buffers in minibuffer window.
13079 In that case, redisplay the window normally. */
13080 && !NILP (Fmemq (w->buffer, Vminibuffer_list)))
13081 {
13082 /* W is a mini-buffer window, but it's not active, so clear
13083 it. */
13084 int yb = window_text_bottom_y (w);
13085 struct glyph_row *row;
13086 int y;
13087
13088 for (y = 0, row = w->desired_matrix->rows;
13089 y < yb;
13090 y += row->height, ++row)
13091 blank_row (w, row, y);
13092 goto finish_scroll_bars;
13093 }
13094
13095 clear_glyph_matrix (w->desired_matrix);
13096 }
13097
13098 /* Otherwise set up data on this window; select its buffer and point
13099 value. */
13100 /* Really select the buffer, for the sake of buffer-local
13101 variables. */
13102 set_buffer_internal_1 (XBUFFER (w->buffer));
13103 SET_TEXT_POS (opoint, PT, PT_BYTE);
13104
13105 beg_unchanged = BEG_UNCHANGED;
13106 end_unchanged = END_UNCHANGED;
13107
13108 current_matrix_up_to_date_p
13109 = (!NILP (w->window_end_valid)
13110 && !current_buffer->clip_changed
13111 && !current_buffer->prevent_redisplay_optimizations_p
13112 && XFASTINT (w->last_modified) >= MODIFF
13113 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
13114
13115 buffer_unchanged_p
13116 = (!NILP (w->window_end_valid)
13117 && !current_buffer->clip_changed
13118 && XFASTINT (w->last_modified) >= MODIFF
13119 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
13120
13121 /* When windows_or_buffers_changed is non-zero, we can't rely on
13122 the window end being valid, so set it to nil there. */
13123 if (windows_or_buffers_changed)
13124 {
13125 /* If window starts on a continuation line, maybe adjust the
13126 window start in case the window's width changed. */
13127 if (XMARKER (w->start)->buffer == current_buffer)
13128 compute_window_start_on_continuation_line (w);
13129
13130 w->window_end_valid = Qnil;
13131 }
13132
13133 /* Some sanity checks. */
13134 CHECK_WINDOW_END (w);
13135 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
13136 abort ();
13137 if (BYTEPOS (opoint) < CHARPOS (opoint))
13138 abort ();
13139
13140 /* If %c is in mode line, update it if needed. */
13141 if (!NILP (w->column_number_displayed)
13142 /* This alternative quickly identifies a common case
13143 where no change is needed. */
13144 && !(PT == XFASTINT (w->last_point)
13145 && XFASTINT (w->last_modified) >= MODIFF
13146 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
13147 && (XFASTINT (w->column_number_displayed)
13148 != (int) current_column ())) /* iftc */
13149 update_mode_line = 1;
13150
13151 /* Count number of windows showing the selected buffer. An indirect
13152 buffer counts as its base buffer. */
13153 if (!just_this_one_p)
13154 {
13155 struct buffer *current_base, *window_base;
13156 current_base = current_buffer;
13157 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
13158 if (current_base->base_buffer)
13159 current_base = current_base->base_buffer;
13160 if (window_base->base_buffer)
13161 window_base = window_base->base_buffer;
13162 if (current_base == window_base)
13163 buffer_shared++;
13164 }
13165
13166 /* Point refers normally to the selected window. For any other
13167 window, set up appropriate value. */
13168 if (!EQ (window, selected_window))
13169 {
13170 int new_pt = XMARKER (w->pointm)->charpos;
13171 int new_pt_byte = marker_byte_position (w->pointm);
13172 if (new_pt < BEGV)
13173 {
13174 new_pt = BEGV;
13175 new_pt_byte = BEGV_BYTE;
13176 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
13177 }
13178 else if (new_pt > (ZV - 1))
13179 {
13180 new_pt = ZV;
13181 new_pt_byte = ZV_BYTE;
13182 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
13183 }
13184
13185 /* We don't use SET_PT so that the point-motion hooks don't run. */
13186 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
13187 }
13188
13189 /* If any of the character widths specified in the display table
13190 have changed, invalidate the width run cache. It's true that
13191 this may be a bit late to catch such changes, but the rest of
13192 redisplay goes (non-fatally) haywire when the display table is
13193 changed, so why should we worry about doing any better? */
13194 if (current_buffer->width_run_cache)
13195 {
13196 struct Lisp_Char_Table *disptab = buffer_display_table ();
13197
13198 if (! disptab_matches_widthtab (disptab,
13199 XVECTOR (current_buffer->width_table)))
13200 {
13201 invalidate_region_cache (current_buffer,
13202 current_buffer->width_run_cache,
13203 BEG, Z);
13204 recompute_width_table (current_buffer, disptab);
13205 }
13206 }
13207
13208 /* If window-start is screwed up, choose a new one. */
13209 if (XMARKER (w->start)->buffer != current_buffer)
13210 goto recenter;
13211
13212 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13213
13214 /* If someone specified a new starting point but did not insist,
13215 check whether it can be used. */
13216 if (!NILP (w->optional_new_start)
13217 && CHARPOS (startp) >= BEGV
13218 && CHARPOS (startp) <= ZV)
13219 {
13220 w->optional_new_start = Qnil;
13221 start_display (&it, w, startp);
13222 move_it_to (&it, PT, 0, it.last_visible_y, -1,
13223 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
13224 if (IT_CHARPOS (it) == PT)
13225 w->force_start = Qt;
13226 /* IT may overshoot PT if text at PT is invisible. */
13227 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
13228 w->force_start = Qt;
13229 }
13230
13231 force_start:
13232
13233 /* Handle case where place to start displaying has been specified,
13234 unless the specified location is outside the accessible range. */
13235 if (!NILP (w->force_start)
13236 || w->frozen_window_start_p)
13237 {
13238 /* We set this later on if we have to adjust point. */
13239 int new_vpos = -1;
13240 int val;
13241
13242 w->force_start = Qnil;
13243 w->vscroll = 0;
13244 w->window_end_valid = Qnil;
13245
13246 /* Forget any recorded base line for line number display. */
13247 if (!buffer_unchanged_p)
13248 w->base_line_number = Qnil;
13249
13250 /* Redisplay the mode line. Select the buffer properly for that.
13251 Also, run the hook window-scroll-functions
13252 because we have scrolled. */
13253 /* Note, we do this after clearing force_start because
13254 if there's an error, it is better to forget about force_start
13255 than to get into an infinite loop calling the hook functions
13256 and having them get more errors. */
13257 if (!update_mode_line
13258 || ! NILP (Vwindow_scroll_functions))
13259 {
13260 update_mode_line = 1;
13261 w->update_mode_line = Qt;
13262 startp = run_window_scroll_functions (window, startp);
13263 }
13264
13265 w->last_modified = make_number (0);
13266 w->last_overlay_modified = make_number (0);
13267 if (CHARPOS (startp) < BEGV)
13268 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
13269 else if (CHARPOS (startp) > ZV)
13270 SET_TEXT_POS (startp, ZV, ZV_BYTE);
13271
13272 /* Redisplay, then check if cursor has been set during the
13273 redisplay. Give up if new fonts were loaded. */
13274 val = try_window (window, startp, 1);
13275 if (!val)
13276 {
13277 w->force_start = Qt;
13278 clear_glyph_matrix (w->desired_matrix);
13279 goto need_larger_matrices;
13280 }
13281 /* Point was outside the scroll margins. */
13282 if (val < 0)
13283 new_vpos = window_box_height (w) / 2;
13284
13285 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
13286 {
13287 /* If point does not appear, try to move point so it does
13288 appear. The desired matrix has been built above, so we
13289 can use it here. */
13290 new_vpos = window_box_height (w) / 2;
13291 }
13292
13293 if (!cursor_row_fully_visible_p (w, 0, 0))
13294 {
13295 /* Point does appear, but on a line partly visible at end of window.
13296 Move it back to a fully-visible line. */
13297 new_vpos = window_box_height (w);
13298 }
13299
13300 /* If we need to move point for either of the above reasons,
13301 now actually do it. */
13302 if (new_vpos >= 0)
13303 {
13304 struct glyph_row *row;
13305
13306 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
13307 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
13308 ++row;
13309
13310 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
13311 MATRIX_ROW_START_BYTEPOS (row));
13312
13313 if (w != XWINDOW (selected_window))
13314 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
13315 else if (current_buffer == old)
13316 SET_TEXT_POS (lpoint, PT, PT_BYTE);
13317
13318 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
13319
13320 /* If we are highlighting the region, then we just changed
13321 the region, so redisplay to show it. */
13322 if (!NILP (Vtransient_mark_mode)
13323 && !NILP (current_buffer->mark_active))
13324 {
13325 clear_glyph_matrix (w->desired_matrix);
13326 if (!try_window (window, startp, 0))
13327 goto need_larger_matrices;
13328 }
13329 }
13330
13331 #if GLYPH_DEBUG
13332 debug_method_add (w, "forced window start");
13333 #endif
13334 goto done;
13335 }
13336
13337 /* Handle case where text has not changed, only point, and it has
13338 not moved off the frame, and we are not retrying after hscroll.
13339 (current_matrix_up_to_date_p is nonzero when retrying.) */
13340 if (current_matrix_up_to_date_p
13341 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
13342 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
13343 {
13344 switch (rc)
13345 {
13346 case CURSOR_MOVEMENT_SUCCESS:
13347 used_current_matrix_p = 1;
13348 goto done;
13349
13350 #if 0 /* try_cursor_movement never returns this value. */
13351 case CURSOR_MOVEMENT_NEED_LARGER_MATRICES:
13352 goto need_larger_matrices;
13353 #endif
13354
13355 case CURSOR_MOVEMENT_MUST_SCROLL:
13356 goto try_to_scroll;
13357
13358 default:
13359 abort ();
13360 }
13361 }
13362 /* If current starting point was originally the beginning of a line
13363 but no longer is, find a new starting point. */
13364 else if (!NILP (w->start_at_line_beg)
13365 && !(CHARPOS (startp) <= BEGV
13366 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
13367 {
13368 #if GLYPH_DEBUG
13369 debug_method_add (w, "recenter 1");
13370 #endif
13371 goto recenter;
13372 }
13373
13374 /* Try scrolling with try_window_id. Value is > 0 if update has
13375 been done, it is -1 if we know that the same window start will
13376 not work. It is 0 if unsuccessful for some other reason. */
13377 else if ((tem = try_window_id (w)) != 0)
13378 {
13379 #if GLYPH_DEBUG
13380 debug_method_add (w, "try_window_id %d", tem);
13381 #endif
13382
13383 if (fonts_changed_p)
13384 goto need_larger_matrices;
13385 if (tem > 0)
13386 goto done;
13387
13388 /* Otherwise try_window_id has returned -1 which means that we
13389 don't want the alternative below this comment to execute. */
13390 }
13391 else if (CHARPOS (startp) >= BEGV
13392 && CHARPOS (startp) <= ZV
13393 && PT >= CHARPOS (startp)
13394 && (CHARPOS (startp) < ZV
13395 /* Avoid starting at end of buffer. */
13396 || CHARPOS (startp) == BEGV
13397 || (XFASTINT (w->last_modified) >= MODIFF
13398 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
13399 {
13400
13401 /* If first window line is a continuation line, and window start
13402 is inside the modified region, but the first change is before
13403 current window start, we must select a new window start.
13404
13405 However, if this is the result of a down-mouse event (e.g. by
13406 extending the mouse-drag-overlay), we don't want to select a
13407 new window start, since that would change the position under
13408 the mouse, resulting in an unwanted mouse-movement rather
13409 than a simple mouse-click. */
13410 if (NILP (w->start_at_line_beg)
13411 && NILP (do_mouse_tracking)
13412 && CHARPOS (startp) > BEGV
13413 && CHARPOS (startp) > BEG + beg_unchanged
13414 && CHARPOS (startp) <= Z - end_unchanged)
13415 {
13416 w->force_start = Qt;
13417 if (XMARKER (w->start)->buffer == current_buffer)
13418 compute_window_start_on_continuation_line (w);
13419 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13420 goto force_start;
13421 }
13422
13423 #if GLYPH_DEBUG
13424 debug_method_add (w, "same window start");
13425 #endif
13426
13427 /* Try to redisplay starting at same place as before.
13428 If point has not moved off frame, accept the results. */
13429 if (!current_matrix_up_to_date_p
13430 /* Don't use try_window_reusing_current_matrix in this case
13431 because a window scroll function can have changed the
13432 buffer. */
13433 || !NILP (Vwindow_scroll_functions)
13434 || MINI_WINDOW_P (w)
13435 || !(used_current_matrix_p
13436 = try_window_reusing_current_matrix (w)))
13437 {
13438 IF_DEBUG (debug_method_add (w, "1"));
13439 if (try_window (window, startp, 1) < 0)
13440 /* -1 means we need to scroll.
13441 0 means we need new matrices, but fonts_changed_p
13442 is set in that case, so we will detect it below. */
13443 goto try_to_scroll;
13444 }
13445
13446 if (fonts_changed_p)
13447 goto need_larger_matrices;
13448
13449 if (w->cursor.vpos >= 0)
13450 {
13451 if (!just_this_one_p
13452 || current_buffer->clip_changed
13453 || BEG_UNCHANGED < CHARPOS (startp))
13454 /* Forget any recorded base line for line number display. */
13455 w->base_line_number = Qnil;
13456
13457 if (!cursor_row_fully_visible_p (w, 1, 0))
13458 {
13459 clear_glyph_matrix (w->desired_matrix);
13460 last_line_misfit = 1;
13461 }
13462 /* Drop through and scroll. */
13463 else
13464 goto done;
13465 }
13466 else
13467 clear_glyph_matrix (w->desired_matrix);
13468 }
13469
13470 try_to_scroll:
13471
13472 w->last_modified = make_number (0);
13473 w->last_overlay_modified = make_number (0);
13474
13475 /* Redisplay the mode line. Select the buffer properly for that. */
13476 if (!update_mode_line)
13477 {
13478 update_mode_line = 1;
13479 w->update_mode_line = Qt;
13480 }
13481
13482 /* Try to scroll by specified few lines. */
13483 if ((scroll_conservatively
13484 || scroll_step
13485 || temp_scroll_step
13486 || NUMBERP (current_buffer->scroll_up_aggressively)
13487 || NUMBERP (current_buffer->scroll_down_aggressively))
13488 && !current_buffer->clip_changed
13489 && CHARPOS (startp) >= BEGV
13490 && CHARPOS (startp) <= ZV)
13491 {
13492 /* The function returns -1 if new fonts were loaded, 1 if
13493 successful, 0 if not successful. */
13494 int rc = try_scrolling (window, just_this_one_p,
13495 scroll_conservatively,
13496 scroll_step,
13497 temp_scroll_step, last_line_misfit);
13498 switch (rc)
13499 {
13500 case SCROLLING_SUCCESS:
13501 goto done;
13502
13503 case SCROLLING_NEED_LARGER_MATRICES:
13504 goto need_larger_matrices;
13505
13506 case SCROLLING_FAILED:
13507 break;
13508
13509 default:
13510 abort ();
13511 }
13512 }
13513
13514 /* Finally, just choose place to start which centers point */
13515
13516 recenter:
13517 if (centering_position < 0)
13518 centering_position = window_box_height (w) / 2;
13519
13520 #if GLYPH_DEBUG
13521 debug_method_add (w, "recenter");
13522 #endif
13523
13524 /* w->vscroll = 0; */
13525
13526 /* Forget any previously recorded base line for line number display. */
13527 if (!buffer_unchanged_p)
13528 w->base_line_number = Qnil;
13529
13530 /* Move backward half the height of the window. */
13531 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
13532 it.current_y = it.last_visible_y;
13533 move_it_vertically_backward (&it, centering_position);
13534 xassert (IT_CHARPOS (it) >= BEGV);
13535
13536 /* The function move_it_vertically_backward may move over more
13537 than the specified y-distance. If it->w is small, e.g. a
13538 mini-buffer window, we may end up in front of the window's
13539 display area. Start displaying at the start of the line
13540 containing PT in this case. */
13541 if (it.current_y <= 0)
13542 {
13543 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
13544 move_it_vertically_backward (&it, 0);
13545 #if 0
13546 /* I think this assert is bogus if buffer contains
13547 invisible text or images. KFS. */
13548 xassert (IT_CHARPOS (it) <= PT);
13549 #endif
13550 it.current_y = 0;
13551 }
13552
13553 it.current_x = it.hpos = 0;
13554
13555 /* Set startp here explicitly in case that helps avoid an infinite loop
13556 in case the window-scroll-functions functions get errors. */
13557 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
13558
13559 /* Run scroll hooks. */
13560 startp = run_window_scroll_functions (window, it.current.pos);
13561
13562 /* Redisplay the window. */
13563 if (!current_matrix_up_to_date_p
13564 || windows_or_buffers_changed
13565 || cursor_type_changed
13566 /* Don't use try_window_reusing_current_matrix in this case
13567 because it can have changed the buffer. */
13568 || !NILP (Vwindow_scroll_functions)
13569 || !just_this_one_p
13570 || MINI_WINDOW_P (w)
13571 || !(used_current_matrix_p
13572 = try_window_reusing_current_matrix (w)))
13573 try_window (window, startp, 0);
13574
13575 /* If new fonts have been loaded (due to fontsets), give up. We
13576 have to start a new redisplay since we need to re-adjust glyph
13577 matrices. */
13578 if (fonts_changed_p)
13579 goto need_larger_matrices;
13580
13581 /* If cursor did not appear assume that the middle of the window is
13582 in the first line of the window. Do it again with the next line.
13583 (Imagine a window of height 100, displaying two lines of height
13584 60. Moving back 50 from it->last_visible_y will end in the first
13585 line.) */
13586 if (w->cursor.vpos < 0)
13587 {
13588 if (!NILP (w->window_end_valid)
13589 && PT >= Z - XFASTINT (w->window_end_pos))
13590 {
13591 clear_glyph_matrix (w->desired_matrix);
13592 move_it_by_lines (&it, 1, 0);
13593 try_window (window, it.current.pos, 0);
13594 }
13595 else if (PT < IT_CHARPOS (it))
13596 {
13597 clear_glyph_matrix (w->desired_matrix);
13598 move_it_by_lines (&it, -1, 0);
13599 try_window (window, it.current.pos, 0);
13600 }
13601 else
13602 {
13603 /* Not much we can do about it. */
13604 }
13605 }
13606
13607 /* Consider the following case: Window starts at BEGV, there is
13608 invisible, intangible text at BEGV, so that display starts at
13609 some point START > BEGV. It can happen that we are called with
13610 PT somewhere between BEGV and START. Try to handle that case. */
13611 if (w->cursor.vpos < 0)
13612 {
13613 struct glyph_row *row = w->current_matrix->rows;
13614 if (row->mode_line_p)
13615 ++row;
13616 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13617 }
13618
13619 if (!cursor_row_fully_visible_p (w, 0, 0))
13620 {
13621 /* If vscroll is enabled, disable it and try again. */
13622 if (w->vscroll)
13623 {
13624 w->vscroll = 0;
13625 clear_glyph_matrix (w->desired_matrix);
13626 goto recenter;
13627 }
13628
13629 /* If centering point failed to make the whole line visible,
13630 put point at the top instead. That has to make the whole line
13631 visible, if it can be done. */
13632 if (centering_position == 0)
13633 goto done;
13634
13635 clear_glyph_matrix (w->desired_matrix);
13636 centering_position = 0;
13637 goto recenter;
13638 }
13639
13640 done:
13641
13642 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13643 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
13644 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
13645 ? Qt : Qnil);
13646
13647 /* Display the mode line, if we must. */
13648 if ((update_mode_line
13649 /* If window not full width, must redo its mode line
13650 if (a) the window to its side is being redone and
13651 (b) we do a frame-based redisplay. This is a consequence
13652 of how inverted lines are drawn in frame-based redisplay. */
13653 || (!just_this_one_p
13654 && !FRAME_WINDOW_P (f)
13655 && !WINDOW_FULL_WIDTH_P (w))
13656 /* Line number to display. */
13657 || INTEGERP (w->base_line_pos)
13658 /* Column number is displayed and different from the one displayed. */
13659 || (!NILP (w->column_number_displayed)
13660 && (XFASTINT (w->column_number_displayed)
13661 != (int) current_column ()))) /* iftc */
13662 /* This means that the window has a mode line. */
13663 && (WINDOW_WANTS_MODELINE_P (w)
13664 || WINDOW_WANTS_HEADER_LINE_P (w)))
13665 {
13666 display_mode_lines (w);
13667
13668 /* If mode line height has changed, arrange for a thorough
13669 immediate redisplay using the correct mode line height. */
13670 if (WINDOW_WANTS_MODELINE_P (w)
13671 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
13672 {
13673 fonts_changed_p = 1;
13674 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
13675 = DESIRED_MODE_LINE_HEIGHT (w);
13676 }
13677
13678 /* If top line height has changed, arrange for a thorough
13679 immediate redisplay using the correct mode line height. */
13680 if (WINDOW_WANTS_HEADER_LINE_P (w)
13681 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
13682 {
13683 fonts_changed_p = 1;
13684 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
13685 = DESIRED_HEADER_LINE_HEIGHT (w);
13686 }
13687
13688 if (fonts_changed_p)
13689 goto need_larger_matrices;
13690 }
13691
13692 if (!line_number_displayed
13693 && !BUFFERP (w->base_line_pos))
13694 {
13695 w->base_line_pos = Qnil;
13696 w->base_line_number = Qnil;
13697 }
13698
13699 finish_menu_bars:
13700
13701 /* When we reach a frame's selected window, redo the frame's menu bar. */
13702 if (update_mode_line
13703 && EQ (FRAME_SELECTED_WINDOW (f), window))
13704 {
13705 int redisplay_menu_p = 0;
13706 int redisplay_tool_bar_p = 0;
13707
13708 if (FRAME_WINDOW_P (f))
13709 {
13710 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
13711 || defined (USE_GTK)
13712 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
13713 #else
13714 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
13715 #endif
13716 }
13717 else
13718 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
13719
13720 if (redisplay_menu_p)
13721 display_menu_bar (w);
13722
13723 #ifdef HAVE_WINDOW_SYSTEM
13724 if (FRAME_WINDOW_P (f))
13725 {
13726 #if defined (USE_GTK) || USE_MAC_TOOLBAR
13727 redisplay_tool_bar_p = FRAME_EXTERNAL_TOOL_BAR (f);
13728 #else
13729 redisplay_tool_bar_p = WINDOWP (f->tool_bar_window)
13730 && (FRAME_TOOL_BAR_LINES (f) > 0
13731 || !NILP (Vauto_resize_tool_bars));
13732 #endif
13733
13734 if (redisplay_tool_bar_p && redisplay_tool_bar (f))
13735 {
13736 extern int ignore_mouse_drag_p;
13737 ignore_mouse_drag_p = 1;
13738 }
13739 }
13740 #endif
13741 }
13742
13743 #ifdef HAVE_WINDOW_SYSTEM
13744 if (FRAME_WINDOW_P (f)
13745 && update_window_fringes (w, (just_this_one_p
13746 || (!used_current_matrix_p && !overlay_arrow_seen)
13747 || w->pseudo_window_p)))
13748 {
13749 update_begin (f);
13750 BLOCK_INPUT;
13751 if (draw_window_fringes (w, 1))
13752 x_draw_vertical_border (w);
13753 UNBLOCK_INPUT;
13754 update_end (f);
13755 }
13756 #endif /* HAVE_WINDOW_SYSTEM */
13757
13758 /* We go to this label, with fonts_changed_p nonzero,
13759 if it is necessary to try again using larger glyph matrices.
13760 We have to redeem the scroll bar even in this case,
13761 because the loop in redisplay_internal expects that. */
13762 need_larger_matrices:
13763 ;
13764 finish_scroll_bars:
13765
13766 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
13767 {
13768 /* Set the thumb's position and size. */
13769 set_vertical_scroll_bar (w);
13770
13771 /* Note that we actually used the scroll bar attached to this
13772 window, so it shouldn't be deleted at the end of redisplay. */
13773 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
13774 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
13775 }
13776
13777 /* Restore current_buffer and value of point in it. */
13778 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
13779 set_buffer_internal_1 (old);
13780 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
13781 shorter. This can be caused by log truncation in *Messages*. */
13782 if (CHARPOS (lpoint) <= ZV)
13783 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
13784
13785 unbind_to (count, Qnil);
13786 }
13787
13788
13789 /* Build the complete desired matrix of WINDOW with a window start
13790 buffer position POS.
13791
13792 Value is 1 if successful. It is zero if fonts were loaded during
13793 redisplay which makes re-adjusting glyph matrices necessary, and -1
13794 if point would appear in the scroll margins.
13795 (We check that only if CHECK_MARGINS is nonzero. */
13796
13797 int
13798 try_window (window, pos, check_margins)
13799 Lisp_Object window;
13800 struct text_pos pos;
13801 int check_margins;
13802 {
13803 struct window *w = XWINDOW (window);
13804 struct it it;
13805 struct glyph_row *last_text_row = NULL;
13806 struct frame *f = XFRAME (w->frame);
13807
13808 /* Make POS the new window start. */
13809 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
13810
13811 /* Mark cursor position as unknown. No overlay arrow seen. */
13812 w->cursor.vpos = -1;
13813 overlay_arrow_seen = 0;
13814
13815 /* Initialize iterator and info to start at POS. */
13816 start_display (&it, w, pos);
13817
13818 /* Display all lines of W. */
13819 while (it.current_y < it.last_visible_y)
13820 {
13821 if (display_line (&it))
13822 last_text_row = it.glyph_row - 1;
13823 if (fonts_changed_p)
13824 return 0;
13825 }
13826
13827 /* Don't let the cursor end in the scroll margins. */
13828 if (check_margins
13829 && !MINI_WINDOW_P (w))
13830 {
13831 int this_scroll_margin;
13832
13833 this_scroll_margin = max (0, scroll_margin);
13834 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
13835 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
13836
13837 if ((w->cursor.y >= 0 /* not vscrolled */
13838 && w->cursor.y < this_scroll_margin
13839 && CHARPOS (pos) > BEGV
13840 && IT_CHARPOS (it) < ZV)
13841 /* rms: considering make_cursor_line_fully_visible_p here
13842 seems to give wrong results. We don't want to recenter
13843 when the last line is partly visible, we want to allow
13844 that case to be handled in the usual way. */
13845 || (w->cursor.y + 1) > it.last_visible_y)
13846 {
13847 w->cursor.vpos = -1;
13848 clear_glyph_matrix (w->desired_matrix);
13849 return -1;
13850 }
13851 }
13852
13853 /* If bottom moved off end of frame, change mode line percentage. */
13854 if (XFASTINT (w->window_end_pos) <= 0
13855 && Z != IT_CHARPOS (it))
13856 w->update_mode_line = Qt;
13857
13858 /* Set window_end_pos to the offset of the last character displayed
13859 on the window from the end of current_buffer. Set
13860 window_end_vpos to its row number. */
13861 if (last_text_row)
13862 {
13863 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
13864 w->window_end_bytepos
13865 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
13866 w->window_end_pos
13867 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
13868 w->window_end_vpos
13869 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
13870 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
13871 ->displays_text_p);
13872 }
13873 else
13874 {
13875 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
13876 w->window_end_pos = make_number (Z - ZV);
13877 w->window_end_vpos = make_number (0);
13878 }
13879
13880 /* But that is not valid info until redisplay finishes. */
13881 w->window_end_valid = Qnil;
13882 return 1;
13883 }
13884
13885
13886 \f
13887 /************************************************************************
13888 Window redisplay reusing current matrix when buffer has not changed
13889 ************************************************************************/
13890
13891 /* Try redisplay of window W showing an unchanged buffer with a
13892 different window start than the last time it was displayed by
13893 reusing its current matrix. Value is non-zero if successful.
13894 W->start is the new window start. */
13895
13896 static int
13897 try_window_reusing_current_matrix (w)
13898 struct window *w;
13899 {
13900 struct frame *f = XFRAME (w->frame);
13901 struct glyph_row *row, *bottom_row;
13902 struct it it;
13903 struct run run;
13904 struct text_pos start, new_start;
13905 int nrows_scrolled, i;
13906 struct glyph_row *last_text_row;
13907 struct glyph_row *last_reused_text_row;
13908 struct glyph_row *start_row;
13909 int start_vpos, min_y, max_y;
13910
13911 #if GLYPH_DEBUG
13912 if (inhibit_try_window_reusing)
13913 return 0;
13914 #endif
13915
13916 if (/* This function doesn't handle terminal frames. */
13917 !FRAME_WINDOW_P (f)
13918 /* Don't try to reuse the display if windows have been split
13919 or such. */
13920 || windows_or_buffers_changed
13921 || cursor_type_changed)
13922 return 0;
13923
13924 /* Can't do this if region may have changed. */
13925 if ((!NILP (Vtransient_mark_mode)
13926 && !NILP (current_buffer->mark_active))
13927 || !NILP (w->region_showing)
13928 || !NILP (Vshow_trailing_whitespace))
13929 return 0;
13930
13931 /* If top-line visibility has changed, give up. */
13932 if (WINDOW_WANTS_HEADER_LINE_P (w)
13933 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
13934 return 0;
13935
13936 /* Give up if old or new display is scrolled vertically. We could
13937 make this function handle this, but right now it doesn't. */
13938 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
13939 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
13940 return 0;
13941
13942 /* The variable new_start now holds the new window start. The old
13943 start `start' can be determined from the current matrix. */
13944 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
13945 start = start_row->start.pos;
13946 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
13947
13948 /* Clear the desired matrix for the display below. */
13949 clear_glyph_matrix (w->desired_matrix);
13950
13951 if (CHARPOS (new_start) <= CHARPOS (start))
13952 {
13953 int first_row_y;
13954
13955 /* Don't use this method if the display starts with an ellipsis
13956 displayed for invisible text. It's not easy to handle that case
13957 below, and it's certainly not worth the effort since this is
13958 not a frequent case. */
13959 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
13960 return 0;
13961
13962 IF_DEBUG (debug_method_add (w, "twu1"));
13963
13964 /* Display up to a row that can be reused. The variable
13965 last_text_row is set to the last row displayed that displays
13966 text. Note that it.vpos == 0 if or if not there is a
13967 header-line; it's not the same as the MATRIX_ROW_VPOS! */
13968 start_display (&it, w, new_start);
13969 first_row_y = it.current_y;
13970 w->cursor.vpos = -1;
13971 last_text_row = last_reused_text_row = NULL;
13972
13973 while (it.current_y < it.last_visible_y
13974 && !fonts_changed_p)
13975 {
13976 /* If we have reached into the characters in the START row,
13977 that means the line boundaries have changed. So we
13978 can't start copying with the row START. Maybe it will
13979 work to start copying with the following row. */
13980 while (IT_CHARPOS (it) > CHARPOS (start))
13981 {
13982 /* Advance to the next row as the "start". */
13983 start_row++;
13984 start = start_row->start.pos;
13985 /* If there are no more rows to try, or just one, give up. */
13986 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
13987 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
13988 || CHARPOS (start) == ZV)
13989 {
13990 clear_glyph_matrix (w->desired_matrix);
13991 return 0;
13992 }
13993
13994 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
13995 }
13996 /* If we have reached alignment,
13997 we can copy the rest of the rows. */
13998 if (IT_CHARPOS (it) == CHARPOS (start))
13999 break;
14000
14001 if (display_line (&it))
14002 last_text_row = it.glyph_row - 1;
14003 }
14004
14005 /* A value of current_y < last_visible_y means that we stopped
14006 at the previous window start, which in turn means that we
14007 have at least one reusable row. */
14008 if (it.current_y < it.last_visible_y)
14009 {
14010 /* IT.vpos always starts from 0; it counts text lines. */
14011 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
14012
14013 /* Find PT if not already found in the lines displayed. */
14014 if (w->cursor.vpos < 0)
14015 {
14016 int dy = it.current_y - start_row->y;
14017
14018 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14019 row = row_containing_pos (w, PT, row, NULL, dy);
14020 if (row)
14021 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
14022 dy, nrows_scrolled);
14023 else
14024 {
14025 clear_glyph_matrix (w->desired_matrix);
14026 return 0;
14027 }
14028 }
14029
14030 /* Scroll the display. Do it before the current matrix is
14031 changed. The problem here is that update has not yet
14032 run, i.e. part of the current matrix is not up to date.
14033 scroll_run_hook will clear the cursor, and use the
14034 current matrix to get the height of the row the cursor is
14035 in. */
14036 run.current_y = start_row->y;
14037 run.desired_y = it.current_y;
14038 run.height = it.last_visible_y - it.current_y;
14039
14040 if (run.height > 0 && run.current_y != run.desired_y)
14041 {
14042 update_begin (f);
14043 FRAME_RIF (f)->update_window_begin_hook (w);
14044 FRAME_RIF (f)->clear_window_mouse_face (w);
14045 FRAME_RIF (f)->scroll_run_hook (w, &run);
14046 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
14047 update_end (f);
14048 }
14049
14050 /* Shift current matrix down by nrows_scrolled lines. */
14051 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
14052 rotate_matrix (w->current_matrix,
14053 start_vpos,
14054 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
14055 nrows_scrolled);
14056
14057 /* Disable lines that must be updated. */
14058 for (i = 0; i < nrows_scrolled; ++i)
14059 (start_row + i)->enabled_p = 0;
14060
14061 /* Re-compute Y positions. */
14062 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
14063 max_y = it.last_visible_y;
14064 for (row = start_row + nrows_scrolled;
14065 row < bottom_row;
14066 ++row)
14067 {
14068 row->y = it.current_y;
14069 row->visible_height = row->height;
14070
14071 if (row->y < min_y)
14072 row->visible_height -= min_y - row->y;
14073 if (row->y + row->height > max_y)
14074 row->visible_height -= row->y + row->height - max_y;
14075 row->redraw_fringe_bitmaps_p = 1;
14076
14077 it.current_y += row->height;
14078
14079 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14080 last_reused_text_row = row;
14081 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
14082 break;
14083 }
14084
14085 /* Disable lines in the current matrix which are now
14086 below the window. */
14087 for (++row; row < bottom_row; ++row)
14088 row->enabled_p = row->mode_line_p = 0;
14089 }
14090
14091 /* Update window_end_pos etc.; last_reused_text_row is the last
14092 reused row from the current matrix containing text, if any.
14093 The value of last_text_row is the last displayed line
14094 containing text. */
14095 if (last_reused_text_row)
14096 {
14097 w->window_end_bytepos
14098 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
14099 w->window_end_pos
14100 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
14101 w->window_end_vpos
14102 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
14103 w->current_matrix));
14104 }
14105 else if (last_text_row)
14106 {
14107 w->window_end_bytepos
14108 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
14109 w->window_end_pos
14110 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
14111 w->window_end_vpos
14112 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
14113 }
14114 else
14115 {
14116 /* This window must be completely empty. */
14117 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
14118 w->window_end_pos = make_number (Z - ZV);
14119 w->window_end_vpos = make_number (0);
14120 }
14121 w->window_end_valid = Qnil;
14122
14123 /* Update hint: don't try scrolling again in update_window. */
14124 w->desired_matrix->no_scrolling_p = 1;
14125
14126 #if GLYPH_DEBUG
14127 debug_method_add (w, "try_window_reusing_current_matrix 1");
14128 #endif
14129 return 1;
14130 }
14131 else if (CHARPOS (new_start) > CHARPOS (start))
14132 {
14133 struct glyph_row *pt_row, *row;
14134 struct glyph_row *first_reusable_row;
14135 struct glyph_row *first_row_to_display;
14136 int dy;
14137 int yb = window_text_bottom_y (w);
14138
14139 /* Find the row starting at new_start, if there is one. Don't
14140 reuse a partially visible line at the end. */
14141 first_reusable_row = start_row;
14142 while (first_reusable_row->enabled_p
14143 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
14144 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
14145 < CHARPOS (new_start)))
14146 ++first_reusable_row;
14147
14148 /* Give up if there is no row to reuse. */
14149 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
14150 || !first_reusable_row->enabled_p
14151 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
14152 != CHARPOS (new_start)))
14153 return 0;
14154
14155 /* We can reuse fully visible rows beginning with
14156 first_reusable_row to the end of the window. Set
14157 first_row_to_display to the first row that cannot be reused.
14158 Set pt_row to the row containing point, if there is any. */
14159 pt_row = NULL;
14160 for (first_row_to_display = first_reusable_row;
14161 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
14162 ++first_row_to_display)
14163 {
14164 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
14165 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
14166 pt_row = first_row_to_display;
14167 }
14168
14169 /* Start displaying at the start of first_row_to_display. */
14170 xassert (first_row_to_display->y < yb);
14171 init_to_row_start (&it, w, first_row_to_display);
14172
14173 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
14174 - start_vpos);
14175 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
14176 - nrows_scrolled);
14177 it.current_y = (first_row_to_display->y - first_reusable_row->y
14178 + WINDOW_HEADER_LINE_HEIGHT (w));
14179
14180 /* Display lines beginning with first_row_to_display in the
14181 desired matrix. Set last_text_row to the last row displayed
14182 that displays text. */
14183 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
14184 if (pt_row == NULL)
14185 w->cursor.vpos = -1;
14186 last_text_row = NULL;
14187 while (it.current_y < it.last_visible_y && !fonts_changed_p)
14188 if (display_line (&it))
14189 last_text_row = it.glyph_row - 1;
14190
14191 /* Give up If point isn't in a row displayed or reused. */
14192 if (w->cursor.vpos < 0)
14193 {
14194 clear_glyph_matrix (w->desired_matrix);
14195 return 0;
14196 }
14197
14198 /* If point is in a reused row, adjust y and vpos of the cursor
14199 position. */
14200 if (pt_row)
14201 {
14202 w->cursor.vpos -= nrows_scrolled;
14203 w->cursor.y -= first_reusable_row->y - start_row->y;
14204 }
14205
14206 /* Scroll the display. */
14207 run.current_y = first_reusable_row->y;
14208 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
14209 run.height = it.last_visible_y - run.current_y;
14210 dy = run.current_y - run.desired_y;
14211
14212 if (run.height)
14213 {
14214 update_begin (f);
14215 FRAME_RIF (f)->update_window_begin_hook (w);
14216 FRAME_RIF (f)->clear_window_mouse_face (w);
14217 FRAME_RIF (f)->scroll_run_hook (w, &run);
14218 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
14219 update_end (f);
14220 }
14221
14222 /* Adjust Y positions of reused rows. */
14223 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
14224 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
14225 max_y = it.last_visible_y;
14226 for (row = first_reusable_row; row < first_row_to_display; ++row)
14227 {
14228 row->y -= dy;
14229 row->visible_height = row->height;
14230 if (row->y < min_y)
14231 row->visible_height -= min_y - row->y;
14232 if (row->y + row->height > max_y)
14233 row->visible_height -= row->y + row->height - max_y;
14234 row->redraw_fringe_bitmaps_p = 1;
14235 }
14236
14237 /* Scroll the current matrix. */
14238 xassert (nrows_scrolled > 0);
14239 rotate_matrix (w->current_matrix,
14240 start_vpos,
14241 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
14242 -nrows_scrolled);
14243
14244 /* Disable rows not reused. */
14245 for (row -= nrows_scrolled; row < bottom_row; ++row)
14246 row->enabled_p = 0;
14247
14248 /* Point may have moved to a different line, so we cannot assume that
14249 the previous cursor position is valid; locate the correct row. */
14250 if (pt_row)
14251 {
14252 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
14253 row < bottom_row && PT >= MATRIX_ROW_END_CHARPOS (row);
14254 row++)
14255 {
14256 w->cursor.vpos++;
14257 w->cursor.y = row->y;
14258 }
14259 if (row < bottom_row)
14260 {
14261 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
14262 while (glyph->charpos < PT)
14263 {
14264 w->cursor.hpos++;
14265 w->cursor.x += glyph->pixel_width;
14266 glyph++;
14267 }
14268 }
14269 }
14270
14271 /* Adjust window end. A null value of last_text_row means that
14272 the window end is in reused rows which in turn means that
14273 only its vpos can have changed. */
14274 if (last_text_row)
14275 {
14276 w->window_end_bytepos
14277 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
14278 w->window_end_pos
14279 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
14280 w->window_end_vpos
14281 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
14282 }
14283 else
14284 {
14285 w->window_end_vpos
14286 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
14287 }
14288
14289 w->window_end_valid = Qnil;
14290 w->desired_matrix->no_scrolling_p = 1;
14291
14292 #if GLYPH_DEBUG
14293 debug_method_add (w, "try_window_reusing_current_matrix 2");
14294 #endif
14295 return 1;
14296 }
14297
14298 return 0;
14299 }
14300
14301
14302 \f
14303 /************************************************************************
14304 Window redisplay reusing current matrix when buffer has changed
14305 ************************************************************************/
14306
14307 static struct glyph_row *find_last_unchanged_at_beg_row P_ ((struct window *));
14308 static struct glyph_row *find_first_unchanged_at_end_row P_ ((struct window *,
14309 int *, int *));
14310 static struct glyph_row *
14311 find_last_row_displaying_text P_ ((struct glyph_matrix *, struct it *,
14312 struct glyph_row *));
14313
14314
14315 /* Return the last row in MATRIX displaying text. If row START is
14316 non-null, start searching with that row. IT gives the dimensions
14317 of the display. Value is null if matrix is empty; otherwise it is
14318 a pointer to the row found. */
14319
14320 static struct glyph_row *
14321 find_last_row_displaying_text (matrix, it, start)
14322 struct glyph_matrix *matrix;
14323 struct it *it;
14324 struct glyph_row *start;
14325 {
14326 struct glyph_row *row, *row_found;
14327
14328 /* Set row_found to the last row in IT->w's current matrix
14329 displaying text. The loop looks funny but think of partially
14330 visible lines. */
14331 row_found = NULL;
14332 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
14333 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14334 {
14335 xassert (row->enabled_p);
14336 row_found = row;
14337 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
14338 break;
14339 ++row;
14340 }
14341
14342 return row_found;
14343 }
14344
14345
14346 /* Return the last row in the current matrix of W that is not affected
14347 by changes at the start of current_buffer that occurred since W's
14348 current matrix was built. Value is null if no such row exists.
14349
14350 BEG_UNCHANGED us the number of characters unchanged at the start of
14351 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
14352 first changed character in current_buffer. Characters at positions <
14353 BEG + BEG_UNCHANGED are at the same buffer positions as they were
14354 when the current matrix was built. */
14355
14356 static struct glyph_row *
14357 find_last_unchanged_at_beg_row (w)
14358 struct window *w;
14359 {
14360 int first_changed_pos = BEG + BEG_UNCHANGED;
14361 struct glyph_row *row;
14362 struct glyph_row *row_found = NULL;
14363 int yb = window_text_bottom_y (w);
14364
14365 /* Find the last row displaying unchanged text. */
14366 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14367 while (MATRIX_ROW_DISPLAYS_TEXT_P (row)
14368 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos)
14369 {
14370 if (/* If row ends before first_changed_pos, it is unchanged,
14371 except in some case. */
14372 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
14373 /* When row ends in ZV and we write at ZV it is not
14374 unchanged. */
14375 && !row->ends_at_zv_p
14376 /* When first_changed_pos is the end of a continued line,
14377 row is not unchanged because it may be no longer
14378 continued. */
14379 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
14380 && (row->continued_p
14381 || row->exact_window_width_line_p)))
14382 row_found = row;
14383
14384 /* Stop if last visible row. */
14385 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
14386 break;
14387
14388 ++row;
14389 }
14390
14391 return row_found;
14392 }
14393
14394
14395 /* Find the first glyph row in the current matrix of W that is not
14396 affected by changes at the end of current_buffer since the
14397 time W's current matrix was built.
14398
14399 Return in *DELTA the number of chars by which buffer positions in
14400 unchanged text at the end of current_buffer must be adjusted.
14401
14402 Return in *DELTA_BYTES the corresponding number of bytes.
14403
14404 Value is null if no such row exists, i.e. all rows are affected by
14405 changes. */
14406
14407 static struct glyph_row *
14408 find_first_unchanged_at_end_row (w, delta, delta_bytes)
14409 struct window *w;
14410 int *delta, *delta_bytes;
14411 {
14412 struct glyph_row *row;
14413 struct glyph_row *row_found = NULL;
14414
14415 *delta = *delta_bytes = 0;
14416
14417 /* Display must not have been paused, otherwise the current matrix
14418 is not up to date. */
14419 if (NILP (w->window_end_valid))
14420 abort ();
14421
14422 /* A value of window_end_pos >= END_UNCHANGED means that the window
14423 end is in the range of changed text. If so, there is no
14424 unchanged row at the end of W's current matrix. */
14425 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
14426 return NULL;
14427
14428 /* Set row to the last row in W's current matrix displaying text. */
14429 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
14430
14431 /* If matrix is entirely empty, no unchanged row exists. */
14432 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14433 {
14434 /* The value of row is the last glyph row in the matrix having a
14435 meaningful buffer position in it. The end position of row
14436 corresponds to window_end_pos. This allows us to translate
14437 buffer positions in the current matrix to current buffer
14438 positions for characters not in changed text. */
14439 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
14440 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
14441 int last_unchanged_pos, last_unchanged_pos_old;
14442 struct glyph_row *first_text_row
14443 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14444
14445 *delta = Z - Z_old;
14446 *delta_bytes = Z_BYTE - Z_BYTE_old;
14447
14448 /* Set last_unchanged_pos to the buffer position of the last
14449 character in the buffer that has not been changed. Z is the
14450 index + 1 of the last character in current_buffer, i.e. by
14451 subtracting END_UNCHANGED we get the index of the last
14452 unchanged character, and we have to add BEG to get its buffer
14453 position. */
14454 last_unchanged_pos = Z - END_UNCHANGED + BEG;
14455 last_unchanged_pos_old = last_unchanged_pos - *delta;
14456
14457 /* Search backward from ROW for a row displaying a line that
14458 starts at a minimum position >= last_unchanged_pos_old. */
14459 for (; row > first_text_row; --row)
14460 {
14461 /* This used to abort, but it can happen.
14462 It is ok to just stop the search instead here. KFS. */
14463 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
14464 break;
14465
14466 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
14467 row_found = row;
14468 }
14469 }
14470
14471 if (row_found && !MATRIX_ROW_DISPLAYS_TEXT_P (row_found))
14472 abort ();
14473
14474 return row_found;
14475 }
14476
14477
14478 /* Make sure that glyph rows in the current matrix of window W
14479 reference the same glyph memory as corresponding rows in the
14480 frame's frame matrix. This function is called after scrolling W's
14481 current matrix on a terminal frame in try_window_id and
14482 try_window_reusing_current_matrix. */
14483
14484 static void
14485 sync_frame_with_window_matrix_rows (w)
14486 struct window *w;
14487 {
14488 struct frame *f = XFRAME (w->frame);
14489 struct glyph_row *window_row, *window_row_end, *frame_row;
14490
14491 /* Preconditions: W must be a leaf window and full-width. Its frame
14492 must have a frame matrix. */
14493 xassert (NILP (w->hchild) && NILP (w->vchild));
14494 xassert (WINDOW_FULL_WIDTH_P (w));
14495 xassert (!FRAME_WINDOW_P (f));
14496
14497 /* If W is a full-width window, glyph pointers in W's current matrix
14498 have, by definition, to be the same as glyph pointers in the
14499 corresponding frame matrix. Note that frame matrices have no
14500 marginal areas (see build_frame_matrix). */
14501 window_row = w->current_matrix->rows;
14502 window_row_end = window_row + w->current_matrix->nrows;
14503 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
14504 while (window_row < window_row_end)
14505 {
14506 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
14507 struct glyph *end = window_row->glyphs[LAST_AREA];
14508
14509 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
14510 frame_row->glyphs[TEXT_AREA] = start;
14511 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
14512 frame_row->glyphs[LAST_AREA] = end;
14513
14514 /* Disable frame rows whose corresponding window rows have
14515 been disabled in try_window_id. */
14516 if (!window_row->enabled_p)
14517 frame_row->enabled_p = 0;
14518
14519 ++window_row, ++frame_row;
14520 }
14521 }
14522
14523
14524 /* Find the glyph row in window W containing CHARPOS. Consider all
14525 rows between START and END (not inclusive). END null means search
14526 all rows to the end of the display area of W. Value is the row
14527 containing CHARPOS or null. */
14528
14529 struct glyph_row *
14530 row_containing_pos (w, charpos, start, end, dy)
14531 struct window *w;
14532 int charpos;
14533 struct glyph_row *start, *end;
14534 int dy;
14535 {
14536 struct glyph_row *row = start;
14537 int last_y;
14538
14539 /* If we happen to start on a header-line, skip that. */
14540 if (row->mode_line_p)
14541 ++row;
14542
14543 if ((end && row >= end) || !row->enabled_p)
14544 return NULL;
14545
14546 last_y = window_text_bottom_y (w) - dy;
14547
14548 while (1)
14549 {
14550 /* Give up if we have gone too far. */
14551 if (end && row >= end)
14552 return NULL;
14553 /* This formerly returned if they were equal.
14554 I think that both quantities are of a "last plus one" type;
14555 if so, when they are equal, the row is within the screen. -- rms. */
14556 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
14557 return NULL;
14558
14559 /* If it is in this row, return this row. */
14560 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
14561 || (MATRIX_ROW_END_CHARPOS (row) == charpos
14562 /* The end position of a row equals the start
14563 position of the next row. If CHARPOS is there, we
14564 would rather display it in the next line, except
14565 when this line ends in ZV. */
14566 && !row->ends_at_zv_p
14567 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
14568 && charpos >= MATRIX_ROW_START_CHARPOS (row))
14569 return row;
14570 ++row;
14571 }
14572 }
14573
14574
14575 /* Try to redisplay window W by reusing its existing display. W's
14576 current matrix must be up to date when this function is called,
14577 i.e. window_end_valid must not be nil.
14578
14579 Value is
14580
14581 1 if display has been updated
14582 0 if otherwise unsuccessful
14583 -1 if redisplay with same window start is known not to succeed
14584
14585 The following steps are performed:
14586
14587 1. Find the last row in the current matrix of W that is not
14588 affected by changes at the start of current_buffer. If no such row
14589 is found, give up.
14590
14591 2. Find the first row in W's current matrix that is not affected by
14592 changes at the end of current_buffer. Maybe there is no such row.
14593
14594 3. Display lines beginning with the row + 1 found in step 1 to the
14595 row found in step 2 or, if step 2 didn't find a row, to the end of
14596 the window.
14597
14598 4. If cursor is not known to appear on the window, give up.
14599
14600 5. If display stopped at the row found in step 2, scroll the
14601 display and current matrix as needed.
14602
14603 6. Maybe display some lines at the end of W, if we must. This can
14604 happen under various circumstances, like a partially visible line
14605 becoming fully visible, or because newly displayed lines are displayed
14606 in smaller font sizes.
14607
14608 7. Update W's window end information. */
14609
14610 static int
14611 try_window_id (w)
14612 struct window *w;
14613 {
14614 struct frame *f = XFRAME (w->frame);
14615 struct glyph_matrix *current_matrix = w->current_matrix;
14616 struct glyph_matrix *desired_matrix = w->desired_matrix;
14617 struct glyph_row *last_unchanged_at_beg_row;
14618 struct glyph_row *first_unchanged_at_end_row;
14619 struct glyph_row *row;
14620 struct glyph_row *bottom_row;
14621 int bottom_vpos;
14622 struct it it;
14623 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
14624 struct text_pos start_pos;
14625 struct run run;
14626 int first_unchanged_at_end_vpos = 0;
14627 struct glyph_row *last_text_row, *last_text_row_at_end;
14628 struct text_pos start;
14629 int first_changed_charpos, last_changed_charpos;
14630
14631 #if GLYPH_DEBUG
14632 if (inhibit_try_window_id)
14633 return 0;
14634 #endif
14635
14636 /* This is handy for debugging. */
14637 #if 0
14638 #define GIVE_UP(X) \
14639 do { \
14640 fprintf (stderr, "try_window_id give up %d\n", (X)); \
14641 return 0; \
14642 } while (0)
14643 #else
14644 #define GIVE_UP(X) return 0
14645 #endif
14646
14647 SET_TEXT_POS_FROM_MARKER (start, w->start);
14648
14649 /* Don't use this for mini-windows because these can show
14650 messages and mini-buffers, and we don't handle that here. */
14651 if (MINI_WINDOW_P (w))
14652 GIVE_UP (1);
14653
14654 /* This flag is used to prevent redisplay optimizations. */
14655 if (windows_or_buffers_changed || cursor_type_changed)
14656 GIVE_UP (2);
14657
14658 /* Verify that narrowing has not changed.
14659 Also verify that we were not told to prevent redisplay optimizations.
14660 It would be nice to further
14661 reduce the number of cases where this prevents try_window_id. */
14662 if (current_buffer->clip_changed
14663 || current_buffer->prevent_redisplay_optimizations_p)
14664 GIVE_UP (3);
14665
14666 /* Window must either use window-based redisplay or be full width. */
14667 if (!FRAME_WINDOW_P (f)
14668 && (!FRAME_LINE_INS_DEL_OK (f)
14669 || !WINDOW_FULL_WIDTH_P (w)))
14670 GIVE_UP (4);
14671
14672 /* Give up if point is not known NOT to appear in W. */
14673 if (PT < CHARPOS (start))
14674 GIVE_UP (5);
14675
14676 /* Another way to prevent redisplay optimizations. */
14677 if (XFASTINT (w->last_modified) == 0)
14678 GIVE_UP (6);
14679
14680 /* Verify that window is not hscrolled. */
14681 if (XFASTINT (w->hscroll) != 0)
14682 GIVE_UP (7);
14683
14684 /* Verify that display wasn't paused. */
14685 if (NILP (w->window_end_valid))
14686 GIVE_UP (8);
14687
14688 /* Can't use this if highlighting a region because a cursor movement
14689 will do more than just set the cursor. */
14690 if (!NILP (Vtransient_mark_mode)
14691 && !NILP (current_buffer->mark_active))
14692 GIVE_UP (9);
14693
14694 /* Likewise if highlighting trailing whitespace. */
14695 if (!NILP (Vshow_trailing_whitespace))
14696 GIVE_UP (11);
14697
14698 /* Likewise if showing a region. */
14699 if (!NILP (w->region_showing))
14700 GIVE_UP (10);
14701
14702 /* Can use this if overlay arrow position and or string have changed. */
14703 if (overlay_arrows_changed_p ())
14704 GIVE_UP (12);
14705
14706
14707 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
14708 only if buffer has really changed. The reason is that the gap is
14709 initially at Z for freshly visited files. The code below would
14710 set end_unchanged to 0 in that case. */
14711 if (MODIFF > SAVE_MODIFF
14712 /* This seems to happen sometimes after saving a buffer. */
14713 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
14714 {
14715 if (GPT - BEG < BEG_UNCHANGED)
14716 BEG_UNCHANGED = GPT - BEG;
14717 if (Z - GPT < END_UNCHANGED)
14718 END_UNCHANGED = Z - GPT;
14719 }
14720
14721 /* The position of the first and last character that has been changed. */
14722 first_changed_charpos = BEG + BEG_UNCHANGED;
14723 last_changed_charpos = Z - END_UNCHANGED;
14724
14725 /* If window starts after a line end, and the last change is in
14726 front of that newline, then changes don't affect the display.
14727 This case happens with stealth-fontification. Note that although
14728 the display is unchanged, glyph positions in the matrix have to
14729 be adjusted, of course. */
14730 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
14731 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
14732 && ((last_changed_charpos < CHARPOS (start)
14733 && CHARPOS (start) == BEGV)
14734 || (last_changed_charpos < CHARPOS (start) - 1
14735 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
14736 {
14737 int Z_old, delta, Z_BYTE_old, delta_bytes;
14738 struct glyph_row *r0;
14739
14740 /* Compute how many chars/bytes have been added to or removed
14741 from the buffer. */
14742 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
14743 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
14744 delta = Z - Z_old;
14745 delta_bytes = Z_BYTE - Z_BYTE_old;
14746
14747 /* Give up if PT is not in the window. Note that it already has
14748 been checked at the start of try_window_id that PT is not in
14749 front of the window start. */
14750 if (PT >= MATRIX_ROW_END_CHARPOS (row) + delta)
14751 GIVE_UP (13);
14752
14753 /* If window start is unchanged, we can reuse the whole matrix
14754 as is, after adjusting glyph positions. No need to compute
14755 the window end again, since its offset from Z hasn't changed. */
14756 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
14757 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + delta
14758 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + delta_bytes
14759 /* PT must not be in a partially visible line. */
14760 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + delta
14761 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
14762 {
14763 /* Adjust positions in the glyph matrix. */
14764 if (delta || delta_bytes)
14765 {
14766 struct glyph_row *r1
14767 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
14768 increment_matrix_positions (w->current_matrix,
14769 MATRIX_ROW_VPOS (r0, current_matrix),
14770 MATRIX_ROW_VPOS (r1, current_matrix),
14771 delta, delta_bytes);
14772 }
14773
14774 /* Set the cursor. */
14775 row = row_containing_pos (w, PT, r0, NULL, 0);
14776 if (row)
14777 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
14778 else
14779 abort ();
14780 return 1;
14781 }
14782 }
14783
14784 /* Handle the case that changes are all below what is displayed in
14785 the window, and that PT is in the window. This shortcut cannot
14786 be taken if ZV is visible in the window, and text has been added
14787 there that is visible in the window. */
14788 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
14789 /* ZV is not visible in the window, or there are no
14790 changes at ZV, actually. */
14791 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
14792 || first_changed_charpos == last_changed_charpos))
14793 {
14794 struct glyph_row *r0;
14795
14796 /* Give up if PT is not in the window. Note that it already has
14797 been checked at the start of try_window_id that PT is not in
14798 front of the window start. */
14799 if (PT >= MATRIX_ROW_END_CHARPOS (row))
14800 GIVE_UP (14);
14801
14802 /* If window start is unchanged, we can reuse the whole matrix
14803 as is, without changing glyph positions since no text has
14804 been added/removed in front of the window end. */
14805 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
14806 if (TEXT_POS_EQUAL_P (start, r0->start.pos)
14807 /* PT must not be in a partially visible line. */
14808 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
14809 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
14810 {
14811 /* We have to compute the window end anew since text
14812 can have been added/removed after it. */
14813 w->window_end_pos
14814 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
14815 w->window_end_bytepos
14816 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
14817
14818 /* Set the cursor. */
14819 row = row_containing_pos (w, PT, r0, NULL, 0);
14820 if (row)
14821 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
14822 else
14823 abort ();
14824 return 2;
14825 }
14826 }
14827
14828 /* Give up if window start is in the changed area.
14829
14830 The condition used to read
14831
14832 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
14833
14834 but why that was tested escapes me at the moment. */
14835 if (CHARPOS (start) >= first_changed_charpos
14836 && CHARPOS (start) <= last_changed_charpos)
14837 GIVE_UP (15);
14838
14839 /* Check that window start agrees with the start of the first glyph
14840 row in its current matrix. Check this after we know the window
14841 start is not in changed text, otherwise positions would not be
14842 comparable. */
14843 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
14844 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
14845 GIVE_UP (16);
14846
14847 /* Give up if the window ends in strings. Overlay strings
14848 at the end are difficult to handle, so don't try. */
14849 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
14850 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
14851 GIVE_UP (20);
14852
14853 /* Compute the position at which we have to start displaying new
14854 lines. Some of the lines at the top of the window might be
14855 reusable because they are not displaying changed text. Find the
14856 last row in W's current matrix not affected by changes at the
14857 start of current_buffer. Value is null if changes start in the
14858 first line of window. */
14859 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
14860 if (last_unchanged_at_beg_row)
14861 {
14862 /* Avoid starting to display in the moddle of a character, a TAB
14863 for instance. This is easier than to set up the iterator
14864 exactly, and it's not a frequent case, so the additional
14865 effort wouldn't really pay off. */
14866 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
14867 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
14868 && last_unchanged_at_beg_row > w->current_matrix->rows)
14869 --last_unchanged_at_beg_row;
14870
14871 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
14872 GIVE_UP (17);
14873
14874 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
14875 GIVE_UP (18);
14876 start_pos = it.current.pos;
14877
14878 /* Start displaying new lines in the desired matrix at the same
14879 vpos we would use in the current matrix, i.e. below
14880 last_unchanged_at_beg_row. */
14881 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
14882 current_matrix);
14883 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
14884 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
14885
14886 xassert (it.hpos == 0 && it.current_x == 0);
14887 }
14888 else
14889 {
14890 /* There are no reusable lines at the start of the window.
14891 Start displaying in the first text line. */
14892 start_display (&it, w, start);
14893 it.vpos = it.first_vpos;
14894 start_pos = it.current.pos;
14895 }
14896
14897 /* Find the first row that is not affected by changes at the end of
14898 the buffer. Value will be null if there is no unchanged row, in
14899 which case we must redisplay to the end of the window. delta
14900 will be set to the value by which buffer positions beginning with
14901 first_unchanged_at_end_row have to be adjusted due to text
14902 changes. */
14903 first_unchanged_at_end_row
14904 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
14905 IF_DEBUG (debug_delta = delta);
14906 IF_DEBUG (debug_delta_bytes = delta_bytes);
14907
14908 /* Set stop_pos to the buffer position up to which we will have to
14909 display new lines. If first_unchanged_at_end_row != NULL, this
14910 is the buffer position of the start of the line displayed in that
14911 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
14912 that we don't stop at a buffer position. */
14913 stop_pos = 0;
14914 if (first_unchanged_at_end_row)
14915 {
14916 xassert (last_unchanged_at_beg_row == NULL
14917 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
14918
14919 /* If this is a continuation line, move forward to the next one
14920 that isn't. Changes in lines above affect this line.
14921 Caution: this may move first_unchanged_at_end_row to a row
14922 not displaying text. */
14923 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
14924 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
14925 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
14926 < it.last_visible_y))
14927 ++first_unchanged_at_end_row;
14928
14929 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
14930 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
14931 >= it.last_visible_y))
14932 first_unchanged_at_end_row = NULL;
14933 else
14934 {
14935 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
14936 + delta);
14937 first_unchanged_at_end_vpos
14938 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
14939 xassert (stop_pos >= Z - END_UNCHANGED);
14940 }
14941 }
14942 else if (last_unchanged_at_beg_row == NULL)
14943 GIVE_UP (19);
14944
14945
14946 #if GLYPH_DEBUG
14947
14948 /* Either there is no unchanged row at the end, or the one we have
14949 now displays text. This is a necessary condition for the window
14950 end pos calculation at the end of this function. */
14951 xassert (first_unchanged_at_end_row == NULL
14952 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
14953
14954 debug_last_unchanged_at_beg_vpos
14955 = (last_unchanged_at_beg_row
14956 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
14957 : -1);
14958 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
14959
14960 #endif /* GLYPH_DEBUG != 0 */
14961
14962
14963 /* Display new lines. Set last_text_row to the last new line
14964 displayed which has text on it, i.e. might end up as being the
14965 line where the window_end_vpos is. */
14966 w->cursor.vpos = -1;
14967 last_text_row = NULL;
14968 overlay_arrow_seen = 0;
14969 while (it.current_y < it.last_visible_y
14970 && !fonts_changed_p
14971 && (first_unchanged_at_end_row == NULL
14972 || IT_CHARPOS (it) < stop_pos))
14973 {
14974 if (display_line (&it))
14975 last_text_row = it.glyph_row - 1;
14976 }
14977
14978 if (fonts_changed_p)
14979 return -1;
14980
14981
14982 /* Compute differences in buffer positions, y-positions etc. for
14983 lines reused at the bottom of the window. Compute what we can
14984 scroll. */
14985 if (first_unchanged_at_end_row
14986 /* No lines reused because we displayed everything up to the
14987 bottom of the window. */
14988 && it.current_y < it.last_visible_y)
14989 {
14990 dvpos = (it.vpos
14991 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
14992 current_matrix));
14993 dy = it.current_y - first_unchanged_at_end_row->y;
14994 run.current_y = first_unchanged_at_end_row->y;
14995 run.desired_y = run.current_y + dy;
14996 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
14997 }
14998 else
14999 {
15000 delta = dvpos = dy = run.current_y = run.desired_y = run.height = 0;
15001 first_unchanged_at_end_row = NULL;
15002 }
15003 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
15004
15005
15006 /* Find the cursor if not already found. We have to decide whether
15007 PT will appear on this window (it sometimes doesn't, but this is
15008 not a very frequent case.) This decision has to be made before
15009 the current matrix is altered. A value of cursor.vpos < 0 means
15010 that PT is either in one of the lines beginning at
15011 first_unchanged_at_end_row or below the window. Don't care for
15012 lines that might be displayed later at the window end; as
15013 mentioned, this is not a frequent case. */
15014 if (w->cursor.vpos < 0)
15015 {
15016 /* Cursor in unchanged rows at the top? */
15017 if (PT < CHARPOS (start_pos)
15018 && last_unchanged_at_beg_row)
15019 {
15020 row = row_containing_pos (w, PT,
15021 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
15022 last_unchanged_at_beg_row + 1, 0);
15023 if (row)
15024 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15025 }
15026
15027 /* Start from first_unchanged_at_end_row looking for PT. */
15028 else if (first_unchanged_at_end_row)
15029 {
15030 row = row_containing_pos (w, PT - delta,
15031 first_unchanged_at_end_row, NULL, 0);
15032 if (row)
15033 set_cursor_from_row (w, row, w->current_matrix, delta,
15034 delta_bytes, dy, dvpos);
15035 }
15036
15037 /* Give up if cursor was not found. */
15038 if (w->cursor.vpos < 0)
15039 {
15040 clear_glyph_matrix (w->desired_matrix);
15041 return -1;
15042 }
15043 }
15044
15045 /* Don't let the cursor end in the scroll margins. */
15046 {
15047 int this_scroll_margin, cursor_height;
15048
15049 this_scroll_margin = max (0, scroll_margin);
15050 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
15051 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
15052 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
15053
15054 if ((w->cursor.y < this_scroll_margin
15055 && CHARPOS (start) > BEGV)
15056 /* Old redisplay didn't take scroll margin into account at the bottom,
15057 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
15058 || (w->cursor.y + (make_cursor_line_fully_visible_p
15059 ? cursor_height + this_scroll_margin
15060 : 1)) > it.last_visible_y)
15061 {
15062 w->cursor.vpos = -1;
15063 clear_glyph_matrix (w->desired_matrix);
15064 return -1;
15065 }
15066 }
15067
15068 /* Scroll the display. Do it before changing the current matrix so
15069 that xterm.c doesn't get confused about where the cursor glyph is
15070 found. */
15071 if (dy && run.height)
15072 {
15073 update_begin (f);
15074
15075 if (FRAME_WINDOW_P (f))
15076 {
15077 FRAME_RIF (f)->update_window_begin_hook (w);
15078 FRAME_RIF (f)->clear_window_mouse_face (w);
15079 FRAME_RIF (f)->scroll_run_hook (w, &run);
15080 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
15081 }
15082 else
15083 {
15084 /* Terminal frame. In this case, dvpos gives the number of
15085 lines to scroll by; dvpos < 0 means scroll up. */
15086 int first_unchanged_at_end_vpos
15087 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
15088 int from = WINDOW_TOP_EDGE_LINE (w) + first_unchanged_at_end_vpos;
15089 int end = (WINDOW_TOP_EDGE_LINE (w)
15090 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
15091 + window_internal_height (w));
15092
15093 /* Perform the operation on the screen. */
15094 if (dvpos > 0)
15095 {
15096 /* Scroll last_unchanged_at_beg_row to the end of the
15097 window down dvpos lines. */
15098 set_terminal_window (f, end);
15099
15100 /* On dumb terminals delete dvpos lines at the end
15101 before inserting dvpos empty lines. */
15102 if (!FRAME_SCROLL_REGION_OK (f))
15103 ins_del_lines (f, end - dvpos, -dvpos);
15104
15105 /* Insert dvpos empty lines in front of
15106 last_unchanged_at_beg_row. */
15107 ins_del_lines (f, from, dvpos);
15108 }
15109 else if (dvpos < 0)
15110 {
15111 /* Scroll up last_unchanged_at_beg_vpos to the end of
15112 the window to last_unchanged_at_beg_vpos - |dvpos|. */
15113 set_terminal_window (f, end);
15114
15115 /* Delete dvpos lines in front of
15116 last_unchanged_at_beg_vpos. ins_del_lines will set
15117 the cursor to the given vpos and emit |dvpos| delete
15118 line sequences. */
15119 ins_del_lines (f, from + dvpos, dvpos);
15120
15121 /* On a dumb terminal insert dvpos empty lines at the
15122 end. */
15123 if (!FRAME_SCROLL_REGION_OK (f))
15124 ins_del_lines (f, end + dvpos, -dvpos);
15125 }
15126
15127 set_terminal_window (f, 0);
15128 }
15129
15130 update_end (f);
15131 }
15132
15133 /* Shift reused rows of the current matrix to the right position.
15134 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
15135 text. */
15136 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
15137 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
15138 if (dvpos < 0)
15139 {
15140 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
15141 bottom_vpos, dvpos);
15142 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
15143 bottom_vpos, 0);
15144 }
15145 else if (dvpos > 0)
15146 {
15147 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
15148 bottom_vpos, dvpos);
15149 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
15150 first_unchanged_at_end_vpos + dvpos, 0);
15151 }
15152
15153 /* For frame-based redisplay, make sure that current frame and window
15154 matrix are in sync with respect to glyph memory. */
15155 if (!FRAME_WINDOW_P (f))
15156 sync_frame_with_window_matrix_rows (w);
15157
15158 /* Adjust buffer positions in reused rows. */
15159 if (delta || delta_bytes)
15160 increment_matrix_positions (current_matrix,
15161 first_unchanged_at_end_vpos + dvpos,
15162 bottom_vpos, delta, delta_bytes);
15163
15164 /* Adjust Y positions. */
15165 if (dy)
15166 shift_glyph_matrix (w, current_matrix,
15167 first_unchanged_at_end_vpos + dvpos,
15168 bottom_vpos, dy);
15169
15170 if (first_unchanged_at_end_row)
15171 {
15172 first_unchanged_at_end_row += dvpos;
15173 if (first_unchanged_at_end_row->y >= it.last_visible_y
15174 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
15175 first_unchanged_at_end_row = NULL;
15176 }
15177
15178 /* If scrolling up, there may be some lines to display at the end of
15179 the window. */
15180 last_text_row_at_end = NULL;
15181 if (dy < 0)
15182 {
15183 /* Scrolling up can leave for example a partially visible line
15184 at the end of the window to be redisplayed. */
15185 /* Set last_row to the glyph row in the current matrix where the
15186 window end line is found. It has been moved up or down in
15187 the matrix by dvpos. */
15188 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
15189 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
15190
15191 /* If last_row is the window end line, it should display text. */
15192 xassert (last_row->displays_text_p);
15193
15194 /* If window end line was partially visible before, begin
15195 displaying at that line. Otherwise begin displaying with the
15196 line following it. */
15197 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
15198 {
15199 init_to_row_start (&it, w, last_row);
15200 it.vpos = last_vpos;
15201 it.current_y = last_row->y;
15202 }
15203 else
15204 {
15205 init_to_row_end (&it, w, last_row);
15206 it.vpos = 1 + last_vpos;
15207 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
15208 ++last_row;
15209 }
15210
15211 /* We may start in a continuation line. If so, we have to
15212 get the right continuation_lines_width and current_x. */
15213 it.continuation_lines_width = last_row->continuation_lines_width;
15214 it.hpos = it.current_x = 0;
15215
15216 /* Display the rest of the lines at the window end. */
15217 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
15218 while (it.current_y < it.last_visible_y
15219 && !fonts_changed_p)
15220 {
15221 /* Is it always sure that the display agrees with lines in
15222 the current matrix? I don't think so, so we mark rows
15223 displayed invalid in the current matrix by setting their
15224 enabled_p flag to zero. */
15225 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
15226 if (display_line (&it))
15227 last_text_row_at_end = it.glyph_row - 1;
15228 }
15229 }
15230
15231 /* Update window_end_pos and window_end_vpos. */
15232 if (first_unchanged_at_end_row
15233 && !last_text_row_at_end)
15234 {
15235 /* Window end line if one of the preserved rows from the current
15236 matrix. Set row to the last row displaying text in current
15237 matrix starting at first_unchanged_at_end_row, after
15238 scrolling. */
15239 xassert (first_unchanged_at_end_row->displays_text_p);
15240 row = find_last_row_displaying_text (w->current_matrix, &it,
15241 first_unchanged_at_end_row);
15242 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
15243
15244 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
15245 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
15246 w->window_end_vpos
15247 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
15248 xassert (w->window_end_bytepos >= 0);
15249 IF_DEBUG (debug_method_add (w, "A"));
15250 }
15251 else if (last_text_row_at_end)
15252 {
15253 w->window_end_pos
15254 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
15255 w->window_end_bytepos
15256 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
15257 w->window_end_vpos
15258 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
15259 xassert (w->window_end_bytepos >= 0);
15260 IF_DEBUG (debug_method_add (w, "B"));
15261 }
15262 else if (last_text_row)
15263 {
15264 /* We have displayed either to the end of the window or at the
15265 end of the window, i.e. the last row with text is to be found
15266 in the desired matrix. */
15267 w->window_end_pos
15268 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
15269 w->window_end_bytepos
15270 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
15271 w->window_end_vpos
15272 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
15273 xassert (w->window_end_bytepos >= 0);
15274 }
15275 else if (first_unchanged_at_end_row == NULL
15276 && last_text_row == NULL
15277 && last_text_row_at_end == NULL)
15278 {
15279 /* Displayed to end of window, but no line containing text was
15280 displayed. Lines were deleted at the end of the window. */
15281 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
15282 int vpos = XFASTINT (w->window_end_vpos);
15283 struct glyph_row *current_row = current_matrix->rows + vpos;
15284 struct glyph_row *desired_row = desired_matrix->rows + vpos;
15285
15286 for (row = NULL;
15287 row == NULL && vpos >= first_vpos;
15288 --vpos, --current_row, --desired_row)
15289 {
15290 if (desired_row->enabled_p)
15291 {
15292 if (desired_row->displays_text_p)
15293 row = desired_row;
15294 }
15295 else if (current_row->displays_text_p)
15296 row = current_row;
15297 }
15298
15299 xassert (row != NULL);
15300 w->window_end_vpos = make_number (vpos + 1);
15301 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
15302 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
15303 xassert (w->window_end_bytepos >= 0);
15304 IF_DEBUG (debug_method_add (w, "C"));
15305 }
15306 else
15307 abort ();
15308
15309 #if 0 /* This leads to problems, for instance when the cursor is
15310 at ZV, and the cursor line displays no text. */
15311 /* Disable rows below what's displayed in the window. This makes
15312 debugging easier. */
15313 enable_glyph_matrix_rows (current_matrix,
15314 XFASTINT (w->window_end_vpos) + 1,
15315 bottom_vpos, 0);
15316 #endif
15317
15318 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
15319 debug_end_vpos = XFASTINT (w->window_end_vpos));
15320
15321 /* Record that display has not been completed. */
15322 w->window_end_valid = Qnil;
15323 w->desired_matrix->no_scrolling_p = 1;
15324 return 3;
15325
15326 #undef GIVE_UP
15327 }
15328
15329
15330 \f
15331 /***********************************************************************
15332 More debugging support
15333 ***********************************************************************/
15334
15335 #if GLYPH_DEBUG
15336
15337 void dump_glyph_row P_ ((struct glyph_row *, int, int));
15338 void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
15339 void dump_glyph P_ ((struct glyph_row *, struct glyph *, int));
15340
15341
15342 /* Dump the contents of glyph matrix MATRIX on stderr.
15343
15344 GLYPHS 0 means don't show glyph contents.
15345 GLYPHS 1 means show glyphs in short form
15346 GLYPHS > 1 means show glyphs in long form. */
15347
15348 void
15349 dump_glyph_matrix (matrix, glyphs)
15350 struct glyph_matrix *matrix;
15351 int glyphs;
15352 {
15353 int i;
15354 for (i = 0; i < matrix->nrows; ++i)
15355 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
15356 }
15357
15358
15359 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
15360 the glyph row and area where the glyph comes from. */
15361
15362 void
15363 dump_glyph (row, glyph, area)
15364 struct glyph_row *row;
15365 struct glyph *glyph;
15366 int area;
15367 {
15368 if (glyph->type == CHAR_GLYPH)
15369 {
15370 fprintf (stderr,
15371 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15372 glyph - row->glyphs[TEXT_AREA],
15373 'C',
15374 glyph->charpos,
15375 (BUFFERP (glyph->object)
15376 ? 'B'
15377 : (STRINGP (glyph->object)
15378 ? 'S'
15379 : '-')),
15380 glyph->pixel_width,
15381 glyph->u.ch,
15382 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
15383 ? glyph->u.ch
15384 : '.'),
15385 glyph->face_id,
15386 glyph->left_box_line_p,
15387 glyph->right_box_line_p);
15388 }
15389 else if (glyph->type == STRETCH_GLYPH)
15390 {
15391 fprintf (stderr,
15392 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15393 glyph - row->glyphs[TEXT_AREA],
15394 'S',
15395 glyph->charpos,
15396 (BUFFERP (glyph->object)
15397 ? 'B'
15398 : (STRINGP (glyph->object)
15399 ? 'S'
15400 : '-')),
15401 glyph->pixel_width,
15402 0,
15403 '.',
15404 glyph->face_id,
15405 glyph->left_box_line_p,
15406 glyph->right_box_line_p);
15407 }
15408 else if (glyph->type == IMAGE_GLYPH)
15409 {
15410 fprintf (stderr,
15411 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15412 glyph - row->glyphs[TEXT_AREA],
15413 'I',
15414 glyph->charpos,
15415 (BUFFERP (glyph->object)
15416 ? 'B'
15417 : (STRINGP (glyph->object)
15418 ? 'S'
15419 : '-')),
15420 glyph->pixel_width,
15421 glyph->u.img_id,
15422 '.',
15423 glyph->face_id,
15424 glyph->left_box_line_p,
15425 glyph->right_box_line_p);
15426 }
15427 else if (glyph->type == COMPOSITE_GLYPH)
15428 {
15429 fprintf (stderr,
15430 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15431 glyph - row->glyphs[TEXT_AREA],
15432 '+',
15433 glyph->charpos,
15434 (BUFFERP (glyph->object)
15435 ? 'B'
15436 : (STRINGP (glyph->object)
15437 ? 'S'
15438 : '-')),
15439 glyph->pixel_width,
15440 glyph->u.cmp_id,
15441 '.',
15442 glyph->face_id,
15443 glyph->left_box_line_p,
15444 glyph->right_box_line_p);
15445 }
15446 }
15447
15448
15449 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
15450 GLYPHS 0 means don't show glyph contents.
15451 GLYPHS 1 means show glyphs in short form
15452 GLYPHS > 1 means show glyphs in long form. */
15453
15454 void
15455 dump_glyph_row (row, vpos, glyphs)
15456 struct glyph_row *row;
15457 int vpos, glyphs;
15458 {
15459 if (glyphs != 1)
15460 {
15461 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
15462 fprintf (stderr, "======================================================================\n");
15463
15464 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d\
15465 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
15466 vpos,
15467 MATRIX_ROW_START_CHARPOS (row),
15468 MATRIX_ROW_END_CHARPOS (row),
15469 row->used[TEXT_AREA],
15470 row->contains_overlapping_glyphs_p,
15471 row->enabled_p,
15472 row->truncated_on_left_p,
15473 row->truncated_on_right_p,
15474 row->continued_p,
15475 MATRIX_ROW_CONTINUATION_LINE_P (row),
15476 row->displays_text_p,
15477 row->ends_at_zv_p,
15478 row->fill_line_p,
15479 row->ends_in_middle_of_char_p,
15480 row->starts_in_middle_of_char_p,
15481 row->mouse_face_p,
15482 row->x,
15483 row->y,
15484 row->pixel_width,
15485 row->height,
15486 row->visible_height,
15487 row->ascent,
15488 row->phys_ascent);
15489 fprintf (stderr, "%9d %5d\t%5d\n", row->start.overlay_string_index,
15490 row->end.overlay_string_index,
15491 row->continuation_lines_width);
15492 fprintf (stderr, "%9d %5d\n",
15493 CHARPOS (row->start.string_pos),
15494 CHARPOS (row->end.string_pos));
15495 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
15496 row->end.dpvec_index);
15497 }
15498
15499 if (glyphs > 1)
15500 {
15501 int area;
15502
15503 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
15504 {
15505 struct glyph *glyph = row->glyphs[area];
15506 struct glyph *glyph_end = glyph + row->used[area];
15507
15508 /* Glyph for a line end in text. */
15509 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
15510 ++glyph_end;
15511
15512 if (glyph < glyph_end)
15513 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
15514
15515 for (; glyph < glyph_end; ++glyph)
15516 dump_glyph (row, glyph, area);
15517 }
15518 }
15519 else if (glyphs == 1)
15520 {
15521 int area;
15522
15523 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
15524 {
15525 char *s = (char *) alloca (row->used[area] + 1);
15526 int i;
15527
15528 for (i = 0; i < row->used[area]; ++i)
15529 {
15530 struct glyph *glyph = row->glyphs[area] + i;
15531 if (glyph->type == CHAR_GLYPH
15532 && glyph->u.ch < 0x80
15533 && glyph->u.ch >= ' ')
15534 s[i] = glyph->u.ch;
15535 else
15536 s[i] = '.';
15537 }
15538
15539 s[i] = '\0';
15540 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
15541 }
15542 }
15543 }
15544
15545
15546 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
15547 Sdump_glyph_matrix, 0, 1, "p",
15548 doc: /* Dump the current matrix of the selected window to stderr.
15549 Shows contents of glyph row structures. With non-nil
15550 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
15551 glyphs in short form, otherwise show glyphs in long form. */)
15552 (glyphs)
15553 Lisp_Object glyphs;
15554 {
15555 struct window *w = XWINDOW (selected_window);
15556 struct buffer *buffer = XBUFFER (w->buffer);
15557
15558 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
15559 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
15560 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
15561 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
15562 fprintf (stderr, "=============================================\n");
15563 dump_glyph_matrix (w->current_matrix,
15564 NILP (glyphs) ? 0 : XINT (glyphs));
15565 return Qnil;
15566 }
15567
15568
15569 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
15570 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
15571 ()
15572 {
15573 struct frame *f = XFRAME (selected_frame);
15574 dump_glyph_matrix (f->current_matrix, 1);
15575 return Qnil;
15576 }
15577
15578
15579 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
15580 doc: /* Dump glyph row ROW to stderr.
15581 GLYPH 0 means don't dump glyphs.
15582 GLYPH 1 means dump glyphs in short form.
15583 GLYPH > 1 or omitted means dump glyphs in long form. */)
15584 (row, glyphs)
15585 Lisp_Object row, glyphs;
15586 {
15587 struct glyph_matrix *matrix;
15588 int vpos;
15589
15590 CHECK_NUMBER (row);
15591 matrix = XWINDOW (selected_window)->current_matrix;
15592 vpos = XINT (row);
15593 if (vpos >= 0 && vpos < matrix->nrows)
15594 dump_glyph_row (MATRIX_ROW (matrix, vpos),
15595 vpos,
15596 INTEGERP (glyphs) ? XINT (glyphs) : 2);
15597 return Qnil;
15598 }
15599
15600
15601 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
15602 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
15603 GLYPH 0 means don't dump glyphs.
15604 GLYPH 1 means dump glyphs in short form.
15605 GLYPH > 1 or omitted means dump glyphs in long form. */)
15606 (row, glyphs)
15607 Lisp_Object row, glyphs;
15608 {
15609 struct frame *sf = SELECTED_FRAME ();
15610 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
15611 int vpos;
15612
15613 CHECK_NUMBER (row);
15614 vpos = XINT (row);
15615 if (vpos >= 0 && vpos < m->nrows)
15616 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
15617 INTEGERP (glyphs) ? XINT (glyphs) : 2);
15618 return Qnil;
15619 }
15620
15621
15622 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
15623 doc: /* Toggle tracing of redisplay.
15624 With ARG, turn tracing on if and only if ARG is positive. */)
15625 (arg)
15626 Lisp_Object arg;
15627 {
15628 if (NILP (arg))
15629 trace_redisplay_p = !trace_redisplay_p;
15630 else
15631 {
15632 arg = Fprefix_numeric_value (arg);
15633 trace_redisplay_p = XINT (arg) > 0;
15634 }
15635
15636 return Qnil;
15637 }
15638
15639
15640 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
15641 doc: /* Like `format', but print result to stderr.
15642 usage: (trace-to-stderr STRING &rest OBJECTS) */)
15643 (nargs, args)
15644 int nargs;
15645 Lisp_Object *args;
15646 {
15647 Lisp_Object s = Fformat (nargs, args);
15648 fprintf (stderr, "%s", SDATA (s));
15649 return Qnil;
15650 }
15651
15652 #endif /* GLYPH_DEBUG */
15653
15654
15655 \f
15656 /***********************************************************************
15657 Building Desired Matrix Rows
15658 ***********************************************************************/
15659
15660 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
15661 Used for non-window-redisplay windows, and for windows w/o left fringe. */
15662
15663 static struct glyph_row *
15664 get_overlay_arrow_glyph_row (w, overlay_arrow_string)
15665 struct window *w;
15666 Lisp_Object overlay_arrow_string;
15667 {
15668 struct frame *f = XFRAME (WINDOW_FRAME (w));
15669 struct buffer *buffer = XBUFFER (w->buffer);
15670 struct buffer *old = current_buffer;
15671 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
15672 int arrow_len = SCHARS (overlay_arrow_string);
15673 const unsigned char *arrow_end = arrow_string + arrow_len;
15674 const unsigned char *p;
15675 struct it it;
15676 int multibyte_p;
15677 int n_glyphs_before;
15678
15679 set_buffer_temp (buffer);
15680 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
15681 it.glyph_row->used[TEXT_AREA] = 0;
15682 SET_TEXT_POS (it.position, 0, 0);
15683
15684 multibyte_p = !NILP (buffer->enable_multibyte_characters);
15685 p = arrow_string;
15686 while (p < arrow_end)
15687 {
15688 Lisp_Object face, ilisp;
15689
15690 /* Get the next character. */
15691 if (multibyte_p)
15692 it.c = string_char_and_length (p, arrow_len, &it.len);
15693 else
15694 it.c = *p, it.len = 1;
15695 p += it.len;
15696
15697 /* Get its face. */
15698 ilisp = make_number (p - arrow_string);
15699 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
15700 it.face_id = compute_char_face (f, it.c, face);
15701
15702 /* Compute its width, get its glyphs. */
15703 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
15704 SET_TEXT_POS (it.position, -1, -1);
15705 PRODUCE_GLYPHS (&it);
15706
15707 /* If this character doesn't fit any more in the line, we have
15708 to remove some glyphs. */
15709 if (it.current_x > it.last_visible_x)
15710 {
15711 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
15712 break;
15713 }
15714 }
15715
15716 set_buffer_temp (old);
15717 return it.glyph_row;
15718 }
15719
15720
15721 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
15722 glyphs are only inserted for terminal frames since we can't really
15723 win with truncation glyphs when partially visible glyphs are
15724 involved. Which glyphs to insert is determined by
15725 produce_special_glyphs. */
15726
15727 static void
15728 insert_left_trunc_glyphs (it)
15729 struct it *it;
15730 {
15731 struct it truncate_it;
15732 struct glyph *from, *end, *to, *toend;
15733
15734 xassert (!FRAME_WINDOW_P (it->f));
15735
15736 /* Get the truncation glyphs. */
15737 truncate_it = *it;
15738 truncate_it.current_x = 0;
15739 truncate_it.face_id = DEFAULT_FACE_ID;
15740 truncate_it.glyph_row = &scratch_glyph_row;
15741 truncate_it.glyph_row->used[TEXT_AREA] = 0;
15742 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
15743 truncate_it.object = make_number (0);
15744 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
15745
15746 /* Overwrite glyphs from IT with truncation glyphs. */
15747 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
15748 end = from + truncate_it.glyph_row->used[TEXT_AREA];
15749 to = it->glyph_row->glyphs[TEXT_AREA];
15750 toend = to + it->glyph_row->used[TEXT_AREA];
15751
15752 while (from < end)
15753 *to++ = *from++;
15754
15755 /* There may be padding glyphs left over. Overwrite them too. */
15756 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
15757 {
15758 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
15759 while (from < end)
15760 *to++ = *from++;
15761 }
15762
15763 if (to > toend)
15764 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
15765 }
15766
15767
15768 /* Compute the pixel height and width of IT->glyph_row.
15769
15770 Most of the time, ascent and height of a display line will be equal
15771 to the max_ascent and max_height values of the display iterator
15772 structure. This is not the case if
15773
15774 1. We hit ZV without displaying anything. In this case, max_ascent
15775 and max_height will be zero.
15776
15777 2. We have some glyphs that don't contribute to the line height.
15778 (The glyph row flag contributes_to_line_height_p is for future
15779 pixmap extensions).
15780
15781 The first case is easily covered by using default values because in
15782 these cases, the line height does not really matter, except that it
15783 must not be zero. */
15784
15785 static void
15786 compute_line_metrics (it)
15787 struct it *it;
15788 {
15789 struct glyph_row *row = it->glyph_row;
15790 int area, i;
15791
15792 if (FRAME_WINDOW_P (it->f))
15793 {
15794 int i, min_y, max_y;
15795
15796 /* The line may consist of one space only, that was added to
15797 place the cursor on it. If so, the row's height hasn't been
15798 computed yet. */
15799 if (row->height == 0)
15800 {
15801 if (it->max_ascent + it->max_descent == 0)
15802 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
15803 row->ascent = it->max_ascent;
15804 row->height = it->max_ascent + it->max_descent;
15805 row->phys_ascent = it->max_phys_ascent;
15806 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
15807 row->extra_line_spacing = it->max_extra_line_spacing;
15808 }
15809
15810 /* Compute the width of this line. */
15811 row->pixel_width = row->x;
15812 for (i = 0; i < row->used[TEXT_AREA]; ++i)
15813 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
15814
15815 xassert (row->pixel_width >= 0);
15816 xassert (row->ascent >= 0 && row->height > 0);
15817
15818 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
15819 || MATRIX_ROW_OVERLAPS_PRED_P (row));
15820
15821 /* If first line's physical ascent is larger than its logical
15822 ascent, use the physical ascent, and make the row taller.
15823 This makes accented characters fully visible. */
15824 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
15825 && row->phys_ascent > row->ascent)
15826 {
15827 row->height += row->phys_ascent - row->ascent;
15828 row->ascent = row->phys_ascent;
15829 }
15830
15831 /* Compute how much of the line is visible. */
15832 row->visible_height = row->height;
15833
15834 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
15835 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
15836
15837 if (row->y < min_y)
15838 row->visible_height -= min_y - row->y;
15839 if (row->y + row->height > max_y)
15840 row->visible_height -= row->y + row->height - max_y;
15841 }
15842 else
15843 {
15844 row->pixel_width = row->used[TEXT_AREA];
15845 if (row->continued_p)
15846 row->pixel_width -= it->continuation_pixel_width;
15847 else if (row->truncated_on_right_p)
15848 row->pixel_width -= it->truncation_pixel_width;
15849 row->ascent = row->phys_ascent = 0;
15850 row->height = row->phys_height = row->visible_height = 1;
15851 row->extra_line_spacing = 0;
15852 }
15853
15854 /* Compute a hash code for this row. */
15855 row->hash = 0;
15856 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
15857 for (i = 0; i < row->used[area]; ++i)
15858 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
15859 + row->glyphs[area][i].u.val
15860 + row->glyphs[area][i].face_id
15861 + row->glyphs[area][i].padding_p
15862 + (row->glyphs[area][i].type << 2));
15863
15864 it->max_ascent = it->max_descent = 0;
15865 it->max_phys_ascent = it->max_phys_descent = 0;
15866 }
15867
15868
15869 /* Append one space to the glyph row of iterator IT if doing a
15870 window-based redisplay. The space has the same face as
15871 IT->face_id. Value is non-zero if a space was added.
15872
15873 This function is called to make sure that there is always one glyph
15874 at the end of a glyph row that the cursor can be set on under
15875 window-systems. (If there weren't such a glyph we would not know
15876 how wide and tall a box cursor should be displayed).
15877
15878 At the same time this space let's a nicely handle clearing to the
15879 end of the line if the row ends in italic text. */
15880
15881 static int
15882 append_space_for_newline (it, default_face_p)
15883 struct it *it;
15884 int default_face_p;
15885 {
15886 if (FRAME_WINDOW_P (it->f))
15887 {
15888 int n = it->glyph_row->used[TEXT_AREA];
15889
15890 if (it->glyph_row->glyphs[TEXT_AREA] + n
15891 < it->glyph_row->glyphs[1 + TEXT_AREA])
15892 {
15893 /* Save some values that must not be changed.
15894 Must save IT->c and IT->len because otherwise
15895 ITERATOR_AT_END_P wouldn't work anymore after
15896 append_space_for_newline has been called. */
15897 enum display_element_type saved_what = it->what;
15898 int saved_c = it->c, saved_len = it->len;
15899 int saved_x = it->current_x;
15900 int saved_face_id = it->face_id;
15901 struct text_pos saved_pos;
15902 Lisp_Object saved_object;
15903 struct face *face;
15904
15905 saved_object = it->object;
15906 saved_pos = it->position;
15907
15908 it->what = IT_CHARACTER;
15909 bzero (&it->position, sizeof it->position);
15910 it->object = make_number (0);
15911 it->c = ' ';
15912 it->len = 1;
15913
15914 if (default_face_p)
15915 it->face_id = DEFAULT_FACE_ID;
15916 else if (it->face_before_selective_p)
15917 it->face_id = it->saved_face_id;
15918 face = FACE_FROM_ID (it->f, it->face_id);
15919 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
15920
15921 PRODUCE_GLYPHS (it);
15922
15923 it->override_ascent = -1;
15924 it->constrain_row_ascent_descent_p = 0;
15925 it->current_x = saved_x;
15926 it->object = saved_object;
15927 it->position = saved_pos;
15928 it->what = saved_what;
15929 it->face_id = saved_face_id;
15930 it->len = saved_len;
15931 it->c = saved_c;
15932 return 1;
15933 }
15934 }
15935
15936 return 0;
15937 }
15938
15939
15940 /* Extend the face of the last glyph in the text area of IT->glyph_row
15941 to the end of the display line. Called from display_line.
15942 If the glyph row is empty, add a space glyph to it so that we
15943 know the face to draw. Set the glyph row flag fill_line_p. */
15944
15945 static void
15946 extend_face_to_end_of_line (it)
15947 struct it *it;
15948 {
15949 struct face *face;
15950 struct frame *f = it->f;
15951
15952 /* If line is already filled, do nothing. */
15953 if (it->current_x >= it->last_visible_x)
15954 return;
15955
15956 /* Face extension extends the background and box of IT->face_id
15957 to the end of the line. If the background equals the background
15958 of the frame, we don't have to do anything. */
15959 if (it->face_before_selective_p)
15960 face = FACE_FROM_ID (it->f, it->saved_face_id);
15961 else
15962 face = FACE_FROM_ID (f, it->face_id);
15963
15964 if (FRAME_WINDOW_P (f)
15965 && it->glyph_row->displays_text_p
15966 && face->box == FACE_NO_BOX
15967 && face->background == FRAME_BACKGROUND_PIXEL (f)
15968 && !face->stipple)
15969 return;
15970
15971 /* Set the glyph row flag indicating that the face of the last glyph
15972 in the text area has to be drawn to the end of the text area. */
15973 it->glyph_row->fill_line_p = 1;
15974
15975 /* If current character of IT is not ASCII, make sure we have the
15976 ASCII face. This will be automatically undone the next time
15977 get_next_display_element returns a multibyte character. Note
15978 that the character will always be single byte in unibyte text. */
15979 if (!ASCII_CHAR_P (it->c))
15980 {
15981 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
15982 }
15983
15984 if (FRAME_WINDOW_P (f))
15985 {
15986 /* If the row is empty, add a space with the current face of IT,
15987 so that we know which face to draw. */
15988 if (it->glyph_row->used[TEXT_AREA] == 0)
15989 {
15990 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
15991 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
15992 it->glyph_row->used[TEXT_AREA] = 1;
15993 }
15994 }
15995 else
15996 {
15997 /* Save some values that must not be changed. */
15998 int saved_x = it->current_x;
15999 struct text_pos saved_pos;
16000 Lisp_Object saved_object;
16001 enum display_element_type saved_what = it->what;
16002 int saved_face_id = it->face_id;
16003
16004 saved_object = it->object;
16005 saved_pos = it->position;
16006
16007 it->what = IT_CHARACTER;
16008 bzero (&it->position, sizeof it->position);
16009 it->object = make_number (0);
16010 it->c = ' ';
16011 it->len = 1;
16012 it->face_id = face->id;
16013
16014 PRODUCE_GLYPHS (it);
16015
16016 while (it->current_x <= it->last_visible_x)
16017 PRODUCE_GLYPHS (it);
16018
16019 /* Don't count these blanks really. It would let us insert a left
16020 truncation glyph below and make us set the cursor on them, maybe. */
16021 it->current_x = saved_x;
16022 it->object = saved_object;
16023 it->position = saved_pos;
16024 it->what = saved_what;
16025 it->face_id = saved_face_id;
16026 }
16027 }
16028
16029
16030 /* Value is non-zero if text starting at CHARPOS in current_buffer is
16031 trailing whitespace. */
16032
16033 static int
16034 trailing_whitespace_p (charpos)
16035 int charpos;
16036 {
16037 int bytepos = CHAR_TO_BYTE (charpos);
16038 int c = 0;
16039
16040 while (bytepos < ZV_BYTE
16041 && (c = FETCH_CHAR (bytepos),
16042 c == ' ' || c == '\t'))
16043 ++bytepos;
16044
16045 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
16046 {
16047 if (bytepos != PT_BYTE)
16048 return 1;
16049 }
16050 return 0;
16051 }
16052
16053
16054 /* Highlight trailing whitespace, if any, in ROW. */
16055
16056 void
16057 highlight_trailing_whitespace (f, row)
16058 struct frame *f;
16059 struct glyph_row *row;
16060 {
16061 int used = row->used[TEXT_AREA];
16062
16063 if (used)
16064 {
16065 struct glyph *start = row->glyphs[TEXT_AREA];
16066 struct glyph *glyph = start + used - 1;
16067
16068 /* Skip over glyphs inserted to display the cursor at the
16069 end of a line, for extending the face of the last glyph
16070 to the end of the line on terminals, and for truncation
16071 and continuation glyphs. */
16072 while (glyph >= start
16073 && glyph->type == CHAR_GLYPH
16074 && INTEGERP (glyph->object))
16075 --glyph;
16076
16077 /* If last glyph is a space or stretch, and it's trailing
16078 whitespace, set the face of all trailing whitespace glyphs in
16079 IT->glyph_row to `trailing-whitespace'. */
16080 if (glyph >= start
16081 && BUFFERP (glyph->object)
16082 && (glyph->type == STRETCH_GLYPH
16083 || (glyph->type == CHAR_GLYPH
16084 && glyph->u.ch == ' '))
16085 && trailing_whitespace_p (glyph->charpos))
16086 {
16087 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
16088 if (face_id < 0)
16089 return;
16090
16091 while (glyph >= start
16092 && BUFFERP (glyph->object)
16093 && (glyph->type == STRETCH_GLYPH
16094 || (glyph->type == CHAR_GLYPH
16095 && glyph->u.ch == ' ')))
16096 (glyph--)->face_id = face_id;
16097 }
16098 }
16099 }
16100
16101
16102 /* Value is non-zero if glyph row ROW in window W should be
16103 used to hold the cursor. */
16104
16105 static int
16106 cursor_row_p (w, row)
16107 struct window *w;
16108 struct glyph_row *row;
16109 {
16110 int cursor_row_p = 1;
16111
16112 if (PT == MATRIX_ROW_END_CHARPOS (row))
16113 {
16114 /* Suppose the row ends on a string.
16115 Unless the row is continued, that means it ends on a newline
16116 in the string. If it's anything other than a display string
16117 (e.g. a before-string from an overlay), we don't want the
16118 cursor there. (This heuristic seems to give the optimal
16119 behavior for the various types of multi-line strings.) */
16120 if (CHARPOS (row->end.string_pos) >= 0)
16121 {
16122 if (row->continued_p)
16123 cursor_row_p = 1;
16124 else
16125 {
16126 /* Check for `display' property. */
16127 struct glyph *beg = row->glyphs[TEXT_AREA];
16128 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
16129 struct glyph *glyph;
16130
16131 cursor_row_p = 0;
16132 for (glyph = end; glyph >= beg; --glyph)
16133 if (STRINGP (glyph->object))
16134 {
16135 Lisp_Object prop
16136 = Fget_char_property (make_number (PT),
16137 Qdisplay, Qnil);
16138 cursor_row_p =
16139 (!NILP (prop)
16140 && display_prop_string_p (prop, glyph->object));
16141 break;
16142 }
16143 }
16144 }
16145 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
16146 {
16147 /* If the row ends in middle of a real character,
16148 and the line is continued, we want the cursor here.
16149 That's because MATRIX_ROW_END_CHARPOS would equal
16150 PT if PT is before the character. */
16151 if (!row->ends_in_ellipsis_p)
16152 cursor_row_p = row->continued_p;
16153 else
16154 /* If the row ends in an ellipsis, then
16155 MATRIX_ROW_END_CHARPOS will equal point after the invisible text.
16156 We want that position to be displayed after the ellipsis. */
16157 cursor_row_p = 0;
16158 }
16159 /* If the row ends at ZV, display the cursor at the end of that
16160 row instead of at the start of the row below. */
16161 else if (row->ends_at_zv_p)
16162 cursor_row_p = 1;
16163 else
16164 cursor_row_p = 0;
16165 }
16166
16167 return cursor_row_p;
16168 }
16169
16170
16171 /* Construct the glyph row IT->glyph_row in the desired matrix of
16172 IT->w from text at the current position of IT. See dispextern.h
16173 for an overview of struct it. Value is non-zero if
16174 IT->glyph_row displays text, as opposed to a line displaying ZV
16175 only. */
16176
16177 static int
16178 display_line (it)
16179 struct it *it;
16180 {
16181 struct glyph_row *row = it->glyph_row;
16182 Lisp_Object overlay_arrow_string;
16183
16184 /* We always start displaying at hpos zero even if hscrolled. */
16185 xassert (it->hpos == 0 && it->current_x == 0);
16186
16187 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
16188 >= it->w->desired_matrix->nrows)
16189 {
16190 it->w->nrows_scale_factor++;
16191 fonts_changed_p = 1;
16192 return 0;
16193 }
16194
16195 /* Is IT->w showing the region? */
16196 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
16197
16198 /* Clear the result glyph row and enable it. */
16199 prepare_desired_row (row);
16200
16201 row->y = it->current_y;
16202 row->start = it->start;
16203 row->continuation_lines_width = it->continuation_lines_width;
16204 row->displays_text_p = 1;
16205 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
16206 it->starts_in_middle_of_char_p = 0;
16207
16208 /* Arrange the overlays nicely for our purposes. Usually, we call
16209 display_line on only one line at a time, in which case this
16210 can't really hurt too much, or we call it on lines which appear
16211 one after another in the buffer, in which case all calls to
16212 recenter_overlay_lists but the first will be pretty cheap. */
16213 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
16214
16215 /* Move over display elements that are not visible because we are
16216 hscrolled. This may stop at an x-position < IT->first_visible_x
16217 if the first glyph is partially visible or if we hit a line end. */
16218 if (it->current_x < it->first_visible_x)
16219 {
16220 move_it_in_display_line_to (it, ZV, it->first_visible_x,
16221 MOVE_TO_POS | MOVE_TO_X);
16222 }
16223
16224 /* Get the initial row height. This is either the height of the
16225 text hscrolled, if there is any, or zero. */
16226 row->ascent = it->max_ascent;
16227 row->height = it->max_ascent + it->max_descent;
16228 row->phys_ascent = it->max_phys_ascent;
16229 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
16230 row->extra_line_spacing = it->max_extra_line_spacing;
16231
16232 /* Loop generating characters. The loop is left with IT on the next
16233 character to display. */
16234 while (1)
16235 {
16236 int n_glyphs_before, hpos_before, x_before;
16237 int x, i, nglyphs;
16238 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
16239
16240 /* Retrieve the next thing to display. Value is zero if end of
16241 buffer reached. */
16242 if (!get_next_display_element (it))
16243 {
16244 /* Maybe add a space at the end of this line that is used to
16245 display the cursor there under X. Set the charpos of the
16246 first glyph of blank lines not corresponding to any text
16247 to -1. */
16248 #ifdef HAVE_WINDOW_SYSTEM
16249 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16250 row->exact_window_width_line_p = 1;
16251 else
16252 #endif /* HAVE_WINDOW_SYSTEM */
16253 if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
16254 || row->used[TEXT_AREA] == 0)
16255 {
16256 row->glyphs[TEXT_AREA]->charpos = -1;
16257 row->displays_text_p = 0;
16258
16259 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines)
16260 && (!MINI_WINDOW_P (it->w)
16261 || (minibuf_level && EQ (it->window, minibuf_window))))
16262 row->indicate_empty_line_p = 1;
16263 }
16264
16265 it->continuation_lines_width = 0;
16266 row->ends_at_zv_p = 1;
16267 break;
16268 }
16269
16270 /* Now, get the metrics of what we want to display. This also
16271 generates glyphs in `row' (which is IT->glyph_row). */
16272 n_glyphs_before = row->used[TEXT_AREA];
16273 x = it->current_x;
16274
16275 /* Remember the line height so far in case the next element doesn't
16276 fit on the line. */
16277 if (!it->truncate_lines_p)
16278 {
16279 ascent = it->max_ascent;
16280 descent = it->max_descent;
16281 phys_ascent = it->max_phys_ascent;
16282 phys_descent = it->max_phys_descent;
16283 }
16284
16285 PRODUCE_GLYPHS (it);
16286
16287 /* If this display element was in marginal areas, continue with
16288 the next one. */
16289 if (it->area != TEXT_AREA)
16290 {
16291 row->ascent = max (row->ascent, it->max_ascent);
16292 row->height = max (row->height, it->max_ascent + it->max_descent);
16293 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
16294 row->phys_height = max (row->phys_height,
16295 it->max_phys_ascent + it->max_phys_descent);
16296 row->extra_line_spacing = max (row->extra_line_spacing,
16297 it->max_extra_line_spacing);
16298 set_iterator_to_next (it, 1);
16299 continue;
16300 }
16301
16302 /* Does the display element fit on the line? If we truncate
16303 lines, we should draw past the right edge of the window. If
16304 we don't truncate, we want to stop so that we can display the
16305 continuation glyph before the right margin. If lines are
16306 continued, there are two possible strategies for characters
16307 resulting in more than 1 glyph (e.g. tabs): Display as many
16308 glyphs as possible in this line and leave the rest for the
16309 continuation line, or display the whole element in the next
16310 line. Original redisplay did the former, so we do it also. */
16311 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
16312 hpos_before = it->hpos;
16313 x_before = x;
16314
16315 if (/* Not a newline. */
16316 nglyphs > 0
16317 /* Glyphs produced fit entirely in the line. */
16318 && it->current_x < it->last_visible_x)
16319 {
16320 it->hpos += nglyphs;
16321 row->ascent = max (row->ascent, it->max_ascent);
16322 row->height = max (row->height, it->max_ascent + it->max_descent);
16323 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
16324 row->phys_height = max (row->phys_height,
16325 it->max_phys_ascent + it->max_phys_descent);
16326 row->extra_line_spacing = max (row->extra_line_spacing,
16327 it->max_extra_line_spacing);
16328 if (it->current_x - it->pixel_width < it->first_visible_x)
16329 row->x = x - it->first_visible_x;
16330 }
16331 else
16332 {
16333 int new_x;
16334 struct glyph *glyph;
16335
16336 for (i = 0; i < nglyphs; ++i, x = new_x)
16337 {
16338 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
16339 new_x = x + glyph->pixel_width;
16340
16341 if (/* Lines are continued. */
16342 !it->truncate_lines_p
16343 && (/* Glyph doesn't fit on the line. */
16344 new_x > it->last_visible_x
16345 /* Or it fits exactly on a window system frame. */
16346 || (new_x == it->last_visible_x
16347 && FRAME_WINDOW_P (it->f))))
16348 {
16349 /* End of a continued line. */
16350
16351 if (it->hpos == 0
16352 || (new_x == it->last_visible_x
16353 && FRAME_WINDOW_P (it->f)))
16354 {
16355 /* Current glyph is the only one on the line or
16356 fits exactly on the line. We must continue
16357 the line because we can't draw the cursor
16358 after the glyph. */
16359 row->continued_p = 1;
16360 it->current_x = new_x;
16361 it->continuation_lines_width += new_x;
16362 ++it->hpos;
16363 if (i == nglyphs - 1)
16364 {
16365 set_iterator_to_next (it, 1);
16366 #ifdef HAVE_WINDOW_SYSTEM
16367 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16368 {
16369 if (!get_next_display_element (it))
16370 {
16371 row->exact_window_width_line_p = 1;
16372 it->continuation_lines_width = 0;
16373 row->continued_p = 0;
16374 row->ends_at_zv_p = 1;
16375 }
16376 else if (ITERATOR_AT_END_OF_LINE_P (it))
16377 {
16378 row->continued_p = 0;
16379 row->exact_window_width_line_p = 1;
16380 }
16381 }
16382 #endif /* HAVE_WINDOW_SYSTEM */
16383 }
16384 }
16385 else if (CHAR_GLYPH_PADDING_P (*glyph)
16386 && !FRAME_WINDOW_P (it->f))
16387 {
16388 /* A padding glyph that doesn't fit on this line.
16389 This means the whole character doesn't fit
16390 on the line. */
16391 row->used[TEXT_AREA] = n_glyphs_before;
16392
16393 /* Fill the rest of the row with continuation
16394 glyphs like in 20.x. */
16395 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
16396 < row->glyphs[1 + TEXT_AREA])
16397 produce_special_glyphs (it, IT_CONTINUATION);
16398
16399 row->continued_p = 1;
16400 it->current_x = x_before;
16401 it->continuation_lines_width += x_before;
16402
16403 /* Restore the height to what it was before the
16404 element not fitting on the line. */
16405 it->max_ascent = ascent;
16406 it->max_descent = descent;
16407 it->max_phys_ascent = phys_ascent;
16408 it->max_phys_descent = phys_descent;
16409 }
16410 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
16411 {
16412 /* A TAB that extends past the right edge of the
16413 window. This produces a single glyph on
16414 window system frames. We leave the glyph in
16415 this row and let it fill the row, but don't
16416 consume the TAB. */
16417 it->continuation_lines_width += it->last_visible_x;
16418 row->ends_in_middle_of_char_p = 1;
16419 row->continued_p = 1;
16420 glyph->pixel_width = it->last_visible_x - x;
16421 it->starts_in_middle_of_char_p = 1;
16422 }
16423 else
16424 {
16425 /* Something other than a TAB that draws past
16426 the right edge of the window. Restore
16427 positions to values before the element. */
16428 row->used[TEXT_AREA] = n_glyphs_before + i;
16429
16430 /* Display continuation glyphs. */
16431 if (!FRAME_WINDOW_P (it->f))
16432 produce_special_glyphs (it, IT_CONTINUATION);
16433 row->continued_p = 1;
16434
16435 it->current_x = x_before;
16436 it->continuation_lines_width += x;
16437 extend_face_to_end_of_line (it);
16438
16439 if (nglyphs > 1 && i > 0)
16440 {
16441 row->ends_in_middle_of_char_p = 1;
16442 it->starts_in_middle_of_char_p = 1;
16443 }
16444
16445 /* Restore the height to what it was before the
16446 element not fitting on the line. */
16447 it->max_ascent = ascent;
16448 it->max_descent = descent;
16449 it->max_phys_ascent = phys_ascent;
16450 it->max_phys_descent = phys_descent;
16451 }
16452
16453 break;
16454 }
16455 else if (new_x > it->first_visible_x)
16456 {
16457 /* Increment number of glyphs actually displayed. */
16458 ++it->hpos;
16459
16460 if (x < it->first_visible_x)
16461 /* Glyph is partially visible, i.e. row starts at
16462 negative X position. */
16463 row->x = x - it->first_visible_x;
16464 }
16465 else
16466 {
16467 /* Glyph is completely off the left margin of the
16468 window. This should not happen because of the
16469 move_it_in_display_line at the start of this
16470 function, unless the text display area of the
16471 window is empty. */
16472 xassert (it->first_visible_x <= it->last_visible_x);
16473 }
16474 }
16475
16476 row->ascent = max (row->ascent, it->max_ascent);
16477 row->height = max (row->height, it->max_ascent + it->max_descent);
16478 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
16479 row->phys_height = max (row->phys_height,
16480 it->max_phys_ascent + it->max_phys_descent);
16481 row->extra_line_spacing = max (row->extra_line_spacing,
16482 it->max_extra_line_spacing);
16483
16484 /* End of this display line if row is continued. */
16485 if (row->continued_p || row->ends_at_zv_p)
16486 break;
16487 }
16488
16489 at_end_of_line:
16490 /* Is this a line end? If yes, we're also done, after making
16491 sure that a non-default face is extended up to the right
16492 margin of the window. */
16493 if (ITERATOR_AT_END_OF_LINE_P (it))
16494 {
16495 int used_before = row->used[TEXT_AREA];
16496
16497 row->ends_in_newline_from_string_p = STRINGP (it->object);
16498
16499 #ifdef HAVE_WINDOW_SYSTEM
16500 /* Add a space at the end of the line that is used to
16501 display the cursor there. */
16502 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16503 append_space_for_newline (it, 0);
16504 #endif /* HAVE_WINDOW_SYSTEM */
16505
16506 /* Extend the face to the end of the line. */
16507 extend_face_to_end_of_line (it);
16508
16509 /* Make sure we have the position. */
16510 if (used_before == 0)
16511 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
16512
16513 /* Consume the line end. This skips over invisible lines. */
16514 set_iterator_to_next (it, 1);
16515 it->continuation_lines_width = 0;
16516 break;
16517 }
16518
16519 /* Proceed with next display element. Note that this skips
16520 over lines invisible because of selective display. */
16521 set_iterator_to_next (it, 1);
16522
16523 /* If we truncate lines, we are done when the last displayed
16524 glyphs reach past the right margin of the window. */
16525 if (it->truncate_lines_p
16526 && (FRAME_WINDOW_P (it->f)
16527 ? (it->current_x >= it->last_visible_x)
16528 : (it->current_x > it->last_visible_x)))
16529 {
16530 /* Maybe add truncation glyphs. */
16531 if (!FRAME_WINDOW_P (it->f))
16532 {
16533 int i, n;
16534
16535 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
16536 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
16537 break;
16538
16539 for (n = row->used[TEXT_AREA]; i < n; ++i)
16540 {
16541 row->used[TEXT_AREA] = i;
16542 produce_special_glyphs (it, IT_TRUNCATION);
16543 }
16544 }
16545 #ifdef HAVE_WINDOW_SYSTEM
16546 else
16547 {
16548 /* Don't truncate if we can overflow newline into fringe. */
16549 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16550 {
16551 if (!get_next_display_element (it))
16552 {
16553 it->continuation_lines_width = 0;
16554 row->ends_at_zv_p = 1;
16555 row->exact_window_width_line_p = 1;
16556 break;
16557 }
16558 if (ITERATOR_AT_END_OF_LINE_P (it))
16559 {
16560 row->exact_window_width_line_p = 1;
16561 goto at_end_of_line;
16562 }
16563 }
16564 }
16565 #endif /* HAVE_WINDOW_SYSTEM */
16566
16567 row->truncated_on_right_p = 1;
16568 it->continuation_lines_width = 0;
16569 reseat_at_next_visible_line_start (it, 0);
16570 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
16571 it->hpos = hpos_before;
16572 it->current_x = x_before;
16573 break;
16574 }
16575 }
16576
16577 /* If line is not empty and hscrolled, maybe insert truncation glyphs
16578 at the left window margin. */
16579 if (it->first_visible_x
16580 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
16581 {
16582 if (!FRAME_WINDOW_P (it->f))
16583 insert_left_trunc_glyphs (it);
16584 row->truncated_on_left_p = 1;
16585 }
16586
16587 /* If the start of this line is the overlay arrow-position, then
16588 mark this glyph row as the one containing the overlay arrow.
16589 This is clearly a mess with variable size fonts. It would be
16590 better to let it be displayed like cursors under X. */
16591 if ((row->displays_text_p || !overlay_arrow_seen)
16592 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
16593 !NILP (overlay_arrow_string)))
16594 {
16595 /* Overlay arrow in window redisplay is a fringe bitmap. */
16596 if (STRINGP (overlay_arrow_string))
16597 {
16598 struct glyph_row *arrow_row
16599 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
16600 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
16601 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
16602 struct glyph *p = row->glyphs[TEXT_AREA];
16603 struct glyph *p2, *end;
16604
16605 /* Copy the arrow glyphs. */
16606 while (glyph < arrow_end)
16607 *p++ = *glyph++;
16608
16609 /* Throw away padding glyphs. */
16610 p2 = p;
16611 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
16612 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
16613 ++p2;
16614 if (p2 > p)
16615 {
16616 while (p2 < end)
16617 *p++ = *p2++;
16618 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
16619 }
16620 }
16621 else
16622 {
16623 xassert (INTEGERP (overlay_arrow_string));
16624 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
16625 }
16626 overlay_arrow_seen = 1;
16627 }
16628
16629 /* Compute pixel dimensions of this line. */
16630 compute_line_metrics (it);
16631
16632 /* Remember the position at which this line ends. */
16633 row->end = it->current;
16634
16635 /* Record whether this row ends inside an ellipsis. */
16636 row->ends_in_ellipsis_p
16637 = (it->method == GET_FROM_DISPLAY_VECTOR
16638 && it->ellipsis_p);
16639
16640 /* Save fringe bitmaps in this row. */
16641 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
16642 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
16643 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
16644 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
16645
16646 it->left_user_fringe_bitmap = 0;
16647 it->left_user_fringe_face_id = 0;
16648 it->right_user_fringe_bitmap = 0;
16649 it->right_user_fringe_face_id = 0;
16650
16651 /* Maybe set the cursor. */
16652 if (it->w->cursor.vpos < 0
16653 && PT >= MATRIX_ROW_START_CHARPOS (row)
16654 && PT <= MATRIX_ROW_END_CHARPOS (row)
16655 && cursor_row_p (it->w, row))
16656 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
16657
16658 /* Highlight trailing whitespace. */
16659 if (!NILP (Vshow_trailing_whitespace))
16660 highlight_trailing_whitespace (it->f, it->glyph_row);
16661
16662 /* Prepare for the next line. This line starts horizontally at (X
16663 HPOS) = (0 0). Vertical positions are incremented. As a
16664 convenience for the caller, IT->glyph_row is set to the next
16665 row to be used. */
16666 it->current_x = it->hpos = 0;
16667 it->current_y += row->height;
16668 ++it->vpos;
16669 ++it->glyph_row;
16670 it->start = it->current;
16671 return row->displays_text_p;
16672 }
16673
16674
16675 \f
16676 /***********************************************************************
16677 Menu Bar
16678 ***********************************************************************/
16679
16680 /* Redisplay the menu bar in the frame for window W.
16681
16682 The menu bar of X frames that don't have X toolkit support is
16683 displayed in a special window W->frame->menu_bar_window.
16684
16685 The menu bar of terminal frames is treated specially as far as
16686 glyph matrices are concerned. Menu bar lines are not part of
16687 windows, so the update is done directly on the frame matrix rows
16688 for the menu bar. */
16689
16690 static void
16691 display_menu_bar (w)
16692 struct window *w;
16693 {
16694 struct frame *f = XFRAME (WINDOW_FRAME (w));
16695 struct it it;
16696 Lisp_Object items;
16697 int i;
16698
16699 /* Don't do all this for graphical frames. */
16700 #ifdef HAVE_NTGUI
16701 if (FRAME_W32_P (f))
16702 return;
16703 #endif
16704 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
16705 if (FRAME_X_P (f))
16706 return;
16707 #endif
16708 #ifdef MAC_OS
16709 if (FRAME_MAC_P (f))
16710 return;
16711 #endif
16712
16713 #ifdef USE_X_TOOLKIT
16714 xassert (!FRAME_WINDOW_P (f));
16715 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
16716 it.first_visible_x = 0;
16717 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
16718 #else /* not USE_X_TOOLKIT */
16719 if (FRAME_WINDOW_P (f))
16720 {
16721 /* Menu bar lines are displayed in the desired matrix of the
16722 dummy window menu_bar_window. */
16723 struct window *menu_w;
16724 xassert (WINDOWP (f->menu_bar_window));
16725 menu_w = XWINDOW (f->menu_bar_window);
16726 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
16727 MENU_FACE_ID);
16728 it.first_visible_x = 0;
16729 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
16730 }
16731 else
16732 {
16733 /* This is a TTY frame, i.e. character hpos/vpos are used as
16734 pixel x/y. */
16735 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
16736 MENU_FACE_ID);
16737 it.first_visible_x = 0;
16738 it.last_visible_x = FRAME_COLS (f);
16739 }
16740 #endif /* not USE_X_TOOLKIT */
16741
16742 if (! mode_line_inverse_video)
16743 /* Force the menu-bar to be displayed in the default face. */
16744 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
16745
16746 /* Clear all rows of the menu bar. */
16747 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
16748 {
16749 struct glyph_row *row = it.glyph_row + i;
16750 clear_glyph_row (row);
16751 row->enabled_p = 1;
16752 row->full_width_p = 1;
16753 }
16754
16755 /* Display all items of the menu bar. */
16756 items = FRAME_MENU_BAR_ITEMS (it.f);
16757 for (i = 0; i < XVECTOR (items)->size; i += 4)
16758 {
16759 Lisp_Object string;
16760
16761 /* Stop at nil string. */
16762 string = AREF (items, i + 1);
16763 if (NILP (string))
16764 break;
16765
16766 /* Remember where item was displayed. */
16767 AREF (items, i + 3) = make_number (it.hpos);
16768
16769 /* Display the item, pad with one space. */
16770 if (it.current_x < it.last_visible_x)
16771 display_string (NULL, string, Qnil, 0, 0, &it,
16772 SCHARS (string) + 1, 0, 0, -1);
16773 }
16774
16775 /* Fill out the line with spaces. */
16776 if (it.current_x < it.last_visible_x)
16777 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
16778
16779 /* Compute the total height of the lines. */
16780 compute_line_metrics (&it);
16781 }
16782
16783
16784 \f
16785 /***********************************************************************
16786 Mode Line
16787 ***********************************************************************/
16788
16789 /* Redisplay mode lines in the window tree whose root is WINDOW. If
16790 FORCE is non-zero, redisplay mode lines unconditionally.
16791 Otherwise, redisplay only mode lines that are garbaged. Value is
16792 the number of windows whose mode lines were redisplayed. */
16793
16794 static int
16795 redisplay_mode_lines (window, force)
16796 Lisp_Object window;
16797 int force;
16798 {
16799 int nwindows = 0;
16800
16801 while (!NILP (window))
16802 {
16803 struct window *w = XWINDOW (window);
16804
16805 if (WINDOWP (w->hchild))
16806 nwindows += redisplay_mode_lines (w->hchild, force);
16807 else if (WINDOWP (w->vchild))
16808 nwindows += redisplay_mode_lines (w->vchild, force);
16809 else if (force
16810 || FRAME_GARBAGED_P (XFRAME (w->frame))
16811 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
16812 {
16813 struct text_pos lpoint;
16814 struct buffer *old = current_buffer;
16815
16816 /* Set the window's buffer for the mode line display. */
16817 SET_TEXT_POS (lpoint, PT, PT_BYTE);
16818 set_buffer_internal_1 (XBUFFER (w->buffer));
16819
16820 /* Point refers normally to the selected window. For any
16821 other window, set up appropriate value. */
16822 if (!EQ (window, selected_window))
16823 {
16824 struct text_pos pt;
16825
16826 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
16827 if (CHARPOS (pt) < BEGV)
16828 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
16829 else if (CHARPOS (pt) > (ZV - 1))
16830 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
16831 else
16832 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
16833 }
16834
16835 /* Display mode lines. */
16836 clear_glyph_matrix (w->desired_matrix);
16837 if (display_mode_lines (w))
16838 {
16839 ++nwindows;
16840 w->must_be_updated_p = 1;
16841 }
16842
16843 /* Restore old settings. */
16844 set_buffer_internal_1 (old);
16845 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
16846 }
16847
16848 window = w->next;
16849 }
16850
16851 return nwindows;
16852 }
16853
16854
16855 /* Display the mode and/or top line of window W. Value is the number
16856 of mode lines displayed. */
16857
16858 static int
16859 display_mode_lines (w)
16860 struct window *w;
16861 {
16862 Lisp_Object old_selected_window, old_selected_frame;
16863 int n = 0;
16864
16865 old_selected_frame = selected_frame;
16866 selected_frame = w->frame;
16867 old_selected_window = selected_window;
16868 XSETWINDOW (selected_window, w);
16869
16870 /* These will be set while the mode line specs are processed. */
16871 line_number_displayed = 0;
16872 w->column_number_displayed = Qnil;
16873
16874 if (WINDOW_WANTS_MODELINE_P (w))
16875 {
16876 struct window *sel_w = XWINDOW (old_selected_window);
16877
16878 /* Select mode line face based on the real selected window. */
16879 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
16880 current_buffer->mode_line_format);
16881 ++n;
16882 }
16883
16884 if (WINDOW_WANTS_HEADER_LINE_P (w))
16885 {
16886 display_mode_line (w, HEADER_LINE_FACE_ID,
16887 current_buffer->header_line_format);
16888 ++n;
16889 }
16890
16891 selected_frame = old_selected_frame;
16892 selected_window = old_selected_window;
16893 return n;
16894 }
16895
16896
16897 /* Display mode or top line of window W. FACE_ID specifies which line
16898 to display; it is either MODE_LINE_FACE_ID or HEADER_LINE_FACE_ID.
16899 FORMAT is the mode line format to display. Value is the pixel
16900 height of the mode line displayed. */
16901
16902 static int
16903 display_mode_line (w, face_id, format)
16904 struct window *w;
16905 enum face_id face_id;
16906 Lisp_Object format;
16907 {
16908 struct it it;
16909 struct face *face;
16910 int count = SPECPDL_INDEX ();
16911
16912 init_iterator (&it, w, -1, -1, NULL, face_id);
16913 /* Don't extend on a previously drawn mode-line.
16914 This may happen if called from pos_visible_p. */
16915 it.glyph_row->enabled_p = 0;
16916 prepare_desired_row (it.glyph_row);
16917
16918 it.glyph_row->mode_line_p = 1;
16919
16920 if (! mode_line_inverse_video)
16921 /* Force the mode-line to be displayed in the default face. */
16922 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
16923
16924 record_unwind_protect (unwind_format_mode_line,
16925 format_mode_line_unwind_data (NULL, 0));
16926
16927 mode_line_target = MODE_LINE_DISPLAY;
16928
16929 /* Temporarily make frame's keyboard the current kboard so that
16930 kboard-local variables in the mode_line_format will get the right
16931 values. */
16932 push_kboard (FRAME_KBOARD (it.f));
16933 record_unwind_save_match_data ();
16934 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
16935 pop_kboard ();
16936
16937 unbind_to (count, Qnil);
16938
16939 /* Fill up with spaces. */
16940 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
16941
16942 compute_line_metrics (&it);
16943 it.glyph_row->full_width_p = 1;
16944 it.glyph_row->continued_p = 0;
16945 it.glyph_row->truncated_on_left_p = 0;
16946 it.glyph_row->truncated_on_right_p = 0;
16947
16948 /* Make a 3D mode-line have a shadow at its right end. */
16949 face = FACE_FROM_ID (it.f, face_id);
16950 extend_face_to_end_of_line (&it);
16951 if (face->box != FACE_NO_BOX)
16952 {
16953 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
16954 + it.glyph_row->used[TEXT_AREA] - 1);
16955 last->right_box_line_p = 1;
16956 }
16957
16958 return it.glyph_row->height;
16959 }
16960
16961 /* Move element ELT in LIST to the front of LIST.
16962 Return the updated list. */
16963
16964 static Lisp_Object
16965 move_elt_to_front (elt, list)
16966 Lisp_Object elt, list;
16967 {
16968 register Lisp_Object tail, prev;
16969 register Lisp_Object tem;
16970
16971 tail = list;
16972 prev = Qnil;
16973 while (CONSP (tail))
16974 {
16975 tem = XCAR (tail);
16976
16977 if (EQ (elt, tem))
16978 {
16979 /* Splice out the link TAIL. */
16980 if (NILP (prev))
16981 list = XCDR (tail);
16982 else
16983 Fsetcdr (prev, XCDR (tail));
16984
16985 /* Now make it the first. */
16986 Fsetcdr (tail, list);
16987 return tail;
16988 }
16989 else
16990 prev = tail;
16991 tail = XCDR (tail);
16992 QUIT;
16993 }
16994
16995 /* Not found--return unchanged LIST. */
16996 return list;
16997 }
16998
16999 /* Contribute ELT to the mode line for window IT->w. How it
17000 translates into text depends on its data type.
17001
17002 IT describes the display environment in which we display, as usual.
17003
17004 DEPTH is the depth in recursion. It is used to prevent
17005 infinite recursion here.
17006
17007 FIELD_WIDTH is the number of characters the display of ELT should
17008 occupy in the mode line, and PRECISION is the maximum number of
17009 characters to display from ELT's representation. See
17010 display_string for details.
17011
17012 Returns the hpos of the end of the text generated by ELT.
17013
17014 PROPS is a property list to add to any string we encounter.
17015
17016 If RISKY is nonzero, remove (disregard) any properties in any string
17017 we encounter, and ignore :eval and :propertize.
17018
17019 The global variable `mode_line_target' determines whether the
17020 output is passed to `store_mode_line_noprop',
17021 `store_mode_line_string', or `display_string'. */
17022
17023 static int
17024 display_mode_element (it, depth, field_width, precision, elt, props, risky)
17025 struct it *it;
17026 int depth;
17027 int field_width, precision;
17028 Lisp_Object elt, props;
17029 int risky;
17030 {
17031 int n = 0, field, prec;
17032 int literal = 0;
17033
17034 tail_recurse:
17035 if (depth > 100)
17036 elt = build_string ("*too-deep*");
17037
17038 depth++;
17039
17040 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
17041 {
17042 case Lisp_String:
17043 {
17044 /* A string: output it and check for %-constructs within it. */
17045 unsigned char c;
17046 int offset = 0;
17047
17048 if (SCHARS (elt) > 0
17049 && (!NILP (props) || risky))
17050 {
17051 Lisp_Object oprops, aelt;
17052 oprops = Ftext_properties_at (make_number (0), elt);
17053
17054 /* If the starting string's properties are not what
17055 we want, translate the string. Also, if the string
17056 is risky, do that anyway. */
17057
17058 if (NILP (Fequal (props, oprops)) || risky)
17059 {
17060 /* If the starting string has properties,
17061 merge the specified ones onto the existing ones. */
17062 if (! NILP (oprops) && !risky)
17063 {
17064 Lisp_Object tem;
17065
17066 oprops = Fcopy_sequence (oprops);
17067 tem = props;
17068 while (CONSP (tem))
17069 {
17070 oprops = Fplist_put (oprops, XCAR (tem),
17071 XCAR (XCDR (tem)));
17072 tem = XCDR (XCDR (tem));
17073 }
17074 props = oprops;
17075 }
17076
17077 aelt = Fassoc (elt, mode_line_proptrans_alist);
17078 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
17079 {
17080 /* AELT is what we want. Move it to the front
17081 without consing. */
17082 elt = XCAR (aelt);
17083 mode_line_proptrans_alist
17084 = move_elt_to_front (aelt, mode_line_proptrans_alist);
17085 }
17086 else
17087 {
17088 Lisp_Object tem;
17089
17090 /* If AELT has the wrong props, it is useless.
17091 so get rid of it. */
17092 if (! NILP (aelt))
17093 mode_line_proptrans_alist
17094 = Fdelq (aelt, mode_line_proptrans_alist);
17095
17096 elt = Fcopy_sequence (elt);
17097 Fset_text_properties (make_number (0), Flength (elt),
17098 props, elt);
17099 /* Add this item to mode_line_proptrans_alist. */
17100 mode_line_proptrans_alist
17101 = Fcons (Fcons (elt, props),
17102 mode_line_proptrans_alist);
17103 /* Truncate mode_line_proptrans_alist
17104 to at most 50 elements. */
17105 tem = Fnthcdr (make_number (50),
17106 mode_line_proptrans_alist);
17107 if (! NILP (tem))
17108 XSETCDR (tem, Qnil);
17109 }
17110 }
17111 }
17112
17113 offset = 0;
17114
17115 if (literal)
17116 {
17117 prec = precision - n;
17118 switch (mode_line_target)
17119 {
17120 case MODE_LINE_NOPROP:
17121 case MODE_LINE_TITLE:
17122 n += store_mode_line_noprop (SDATA (elt), -1, prec);
17123 break;
17124 case MODE_LINE_STRING:
17125 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
17126 break;
17127 case MODE_LINE_DISPLAY:
17128 n += display_string (NULL, elt, Qnil, 0, 0, it,
17129 0, prec, 0, STRING_MULTIBYTE (elt));
17130 break;
17131 }
17132
17133 break;
17134 }
17135
17136 /* Handle the non-literal case. */
17137
17138 while ((precision <= 0 || n < precision)
17139 && SREF (elt, offset) != 0
17140 && (mode_line_target != MODE_LINE_DISPLAY
17141 || it->current_x < it->last_visible_x))
17142 {
17143 int last_offset = offset;
17144
17145 /* Advance to end of string or next format specifier. */
17146 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
17147 ;
17148
17149 if (offset - 1 != last_offset)
17150 {
17151 int nchars, nbytes;
17152
17153 /* Output to end of string or up to '%'. Field width
17154 is length of string. Don't output more than
17155 PRECISION allows us. */
17156 offset--;
17157
17158 prec = c_string_width (SDATA (elt) + last_offset,
17159 offset - last_offset, precision - n,
17160 &nchars, &nbytes);
17161
17162 switch (mode_line_target)
17163 {
17164 case MODE_LINE_NOPROP:
17165 case MODE_LINE_TITLE:
17166 n += store_mode_line_noprop (SDATA (elt) + last_offset, 0, prec);
17167 break;
17168 case MODE_LINE_STRING:
17169 {
17170 int bytepos = last_offset;
17171 int charpos = string_byte_to_char (elt, bytepos);
17172 int endpos = (precision <= 0
17173 ? string_byte_to_char (elt, offset)
17174 : charpos + nchars);
17175
17176 n += store_mode_line_string (NULL,
17177 Fsubstring (elt, make_number (charpos),
17178 make_number (endpos)),
17179 0, 0, 0, Qnil);
17180 }
17181 break;
17182 case MODE_LINE_DISPLAY:
17183 {
17184 int bytepos = last_offset;
17185 int charpos = string_byte_to_char (elt, bytepos);
17186
17187 if (precision <= 0)
17188 nchars = string_byte_to_char (elt, offset) - charpos;
17189 n += display_string (NULL, elt, Qnil, 0, charpos,
17190 it, 0, nchars, 0,
17191 STRING_MULTIBYTE (elt));
17192 }
17193 break;
17194 }
17195 }
17196 else /* c == '%' */
17197 {
17198 int percent_position = offset;
17199
17200 /* Get the specified minimum width. Zero means
17201 don't pad. */
17202 field = 0;
17203 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
17204 field = field * 10 + c - '0';
17205
17206 /* Don't pad beyond the total padding allowed. */
17207 if (field_width - n > 0 && field > field_width - n)
17208 field = field_width - n;
17209
17210 /* Note that either PRECISION <= 0 or N < PRECISION. */
17211 prec = precision - n;
17212
17213 if (c == 'M')
17214 n += display_mode_element (it, depth, field, prec,
17215 Vglobal_mode_string, props,
17216 risky);
17217 else if (c != 0)
17218 {
17219 int multibyte;
17220 int bytepos, charpos;
17221 unsigned char *spec;
17222
17223 bytepos = percent_position;
17224 charpos = (STRING_MULTIBYTE (elt)
17225 ? string_byte_to_char (elt, bytepos)
17226 : bytepos);
17227
17228 spec
17229 = decode_mode_spec (it->w, c, field, prec, &multibyte);
17230
17231 switch (mode_line_target)
17232 {
17233 case MODE_LINE_NOPROP:
17234 case MODE_LINE_TITLE:
17235 n += store_mode_line_noprop (spec, field, prec);
17236 break;
17237 case MODE_LINE_STRING:
17238 {
17239 int len = strlen (spec);
17240 Lisp_Object tem = make_string (spec, len);
17241 props = Ftext_properties_at (make_number (charpos), elt);
17242 /* Should only keep face property in props */
17243 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
17244 }
17245 break;
17246 case MODE_LINE_DISPLAY:
17247 {
17248 int nglyphs_before, nwritten;
17249
17250 nglyphs_before = it->glyph_row->used[TEXT_AREA];
17251 nwritten = display_string (spec, Qnil, elt,
17252 charpos, 0, it,
17253 field, prec, 0,
17254 multibyte);
17255
17256 /* Assign to the glyphs written above the
17257 string where the `%x' came from, position
17258 of the `%'. */
17259 if (nwritten > 0)
17260 {
17261 struct glyph *glyph
17262 = (it->glyph_row->glyphs[TEXT_AREA]
17263 + nglyphs_before);
17264 int i;
17265
17266 for (i = 0; i < nwritten; ++i)
17267 {
17268 glyph[i].object = elt;
17269 glyph[i].charpos = charpos;
17270 }
17271
17272 n += nwritten;
17273 }
17274 }
17275 break;
17276 }
17277 }
17278 else /* c == 0 */
17279 break;
17280 }
17281 }
17282 }
17283 break;
17284
17285 case Lisp_Symbol:
17286 /* A symbol: process the value of the symbol recursively
17287 as if it appeared here directly. Avoid error if symbol void.
17288 Special case: if value of symbol is a string, output the string
17289 literally. */
17290 {
17291 register Lisp_Object tem;
17292
17293 /* If the variable is not marked as risky to set
17294 then its contents are risky to use. */
17295 if (NILP (Fget (elt, Qrisky_local_variable)))
17296 risky = 1;
17297
17298 tem = Fboundp (elt);
17299 if (!NILP (tem))
17300 {
17301 tem = Fsymbol_value (elt);
17302 /* If value is a string, output that string literally:
17303 don't check for % within it. */
17304 if (STRINGP (tem))
17305 literal = 1;
17306
17307 if (!EQ (tem, elt))
17308 {
17309 /* Give up right away for nil or t. */
17310 elt = tem;
17311 goto tail_recurse;
17312 }
17313 }
17314 }
17315 break;
17316
17317 case Lisp_Cons:
17318 {
17319 register Lisp_Object car, tem;
17320
17321 /* A cons cell: five distinct cases.
17322 If first element is :eval or :propertize, do something special.
17323 If first element is a string or a cons, process all the elements
17324 and effectively concatenate them.
17325 If first element is a negative number, truncate displaying cdr to
17326 at most that many characters. If positive, pad (with spaces)
17327 to at least that many characters.
17328 If first element is a symbol, process the cadr or caddr recursively
17329 according to whether the symbol's value is non-nil or nil. */
17330 car = XCAR (elt);
17331 if (EQ (car, QCeval))
17332 {
17333 /* An element of the form (:eval FORM) means evaluate FORM
17334 and use the result as mode line elements. */
17335
17336 if (risky)
17337 break;
17338
17339 if (CONSP (XCDR (elt)))
17340 {
17341 Lisp_Object spec;
17342 spec = safe_eval (XCAR (XCDR (elt)));
17343 n += display_mode_element (it, depth, field_width - n,
17344 precision - n, spec, props,
17345 risky);
17346 }
17347 }
17348 else if (EQ (car, QCpropertize))
17349 {
17350 /* An element of the form (:propertize ELT PROPS...)
17351 means display ELT but applying properties PROPS. */
17352
17353 if (risky)
17354 break;
17355
17356 if (CONSP (XCDR (elt)))
17357 n += display_mode_element (it, depth, field_width - n,
17358 precision - n, XCAR (XCDR (elt)),
17359 XCDR (XCDR (elt)), risky);
17360 }
17361 else if (SYMBOLP (car))
17362 {
17363 tem = Fboundp (car);
17364 elt = XCDR (elt);
17365 if (!CONSP (elt))
17366 goto invalid;
17367 /* elt is now the cdr, and we know it is a cons cell.
17368 Use its car if CAR has a non-nil value. */
17369 if (!NILP (tem))
17370 {
17371 tem = Fsymbol_value (car);
17372 if (!NILP (tem))
17373 {
17374 elt = XCAR (elt);
17375 goto tail_recurse;
17376 }
17377 }
17378 /* Symbol's value is nil (or symbol is unbound)
17379 Get the cddr of the original list
17380 and if possible find the caddr and use that. */
17381 elt = XCDR (elt);
17382 if (NILP (elt))
17383 break;
17384 else if (!CONSP (elt))
17385 goto invalid;
17386 elt = XCAR (elt);
17387 goto tail_recurse;
17388 }
17389 else if (INTEGERP (car))
17390 {
17391 register int lim = XINT (car);
17392 elt = XCDR (elt);
17393 if (lim < 0)
17394 {
17395 /* Negative int means reduce maximum width. */
17396 if (precision <= 0)
17397 precision = -lim;
17398 else
17399 precision = min (precision, -lim);
17400 }
17401 else if (lim > 0)
17402 {
17403 /* Padding specified. Don't let it be more than
17404 current maximum. */
17405 if (precision > 0)
17406 lim = min (precision, lim);
17407
17408 /* If that's more padding than already wanted, queue it.
17409 But don't reduce padding already specified even if
17410 that is beyond the current truncation point. */
17411 field_width = max (lim, field_width);
17412 }
17413 goto tail_recurse;
17414 }
17415 else if (STRINGP (car) || CONSP (car))
17416 {
17417 register int limit = 50;
17418 /* Limit is to protect against circular lists. */
17419 while (CONSP (elt)
17420 && --limit > 0
17421 && (precision <= 0 || n < precision))
17422 {
17423 n += display_mode_element (it, depth,
17424 /* Do padding only after the last
17425 element in the list. */
17426 (! CONSP (XCDR (elt))
17427 ? field_width - n
17428 : 0),
17429 precision - n, XCAR (elt),
17430 props, risky);
17431 elt = XCDR (elt);
17432 }
17433 }
17434 }
17435 break;
17436
17437 default:
17438 invalid:
17439 elt = build_string ("*invalid*");
17440 goto tail_recurse;
17441 }
17442
17443 /* Pad to FIELD_WIDTH. */
17444 if (field_width > 0 && n < field_width)
17445 {
17446 switch (mode_line_target)
17447 {
17448 case MODE_LINE_NOPROP:
17449 case MODE_LINE_TITLE:
17450 n += store_mode_line_noprop ("", field_width - n, 0);
17451 break;
17452 case MODE_LINE_STRING:
17453 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
17454 break;
17455 case MODE_LINE_DISPLAY:
17456 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
17457 0, 0, 0);
17458 break;
17459 }
17460 }
17461
17462 return n;
17463 }
17464
17465 /* Store a mode-line string element in mode_line_string_list.
17466
17467 If STRING is non-null, display that C string. Otherwise, the Lisp
17468 string LISP_STRING is displayed.
17469
17470 FIELD_WIDTH is the minimum number of output glyphs to produce.
17471 If STRING has fewer characters than FIELD_WIDTH, pad to the right
17472 with spaces. FIELD_WIDTH <= 0 means don't pad.
17473
17474 PRECISION is the maximum number of characters to output from
17475 STRING. PRECISION <= 0 means don't truncate the string.
17476
17477 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
17478 properties to the string.
17479
17480 PROPS are the properties to add to the string.
17481 The mode_line_string_face face property is always added to the string.
17482 */
17483
17484 static int
17485 store_mode_line_string (string, lisp_string, copy_string, field_width, precision, props)
17486 char *string;
17487 Lisp_Object lisp_string;
17488 int copy_string;
17489 int field_width;
17490 int precision;
17491 Lisp_Object props;
17492 {
17493 int len;
17494 int n = 0;
17495
17496 if (string != NULL)
17497 {
17498 len = strlen (string);
17499 if (precision > 0 && len > precision)
17500 len = precision;
17501 lisp_string = make_string (string, len);
17502 if (NILP (props))
17503 props = mode_line_string_face_prop;
17504 else if (!NILP (mode_line_string_face))
17505 {
17506 Lisp_Object face = Fplist_get (props, Qface);
17507 props = Fcopy_sequence (props);
17508 if (NILP (face))
17509 face = mode_line_string_face;
17510 else
17511 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
17512 props = Fplist_put (props, Qface, face);
17513 }
17514 Fadd_text_properties (make_number (0), make_number (len),
17515 props, lisp_string);
17516 }
17517 else
17518 {
17519 len = XFASTINT (Flength (lisp_string));
17520 if (precision > 0 && len > precision)
17521 {
17522 len = precision;
17523 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
17524 precision = -1;
17525 }
17526 if (!NILP (mode_line_string_face))
17527 {
17528 Lisp_Object face;
17529 if (NILP (props))
17530 props = Ftext_properties_at (make_number (0), lisp_string);
17531 face = Fplist_get (props, Qface);
17532 if (NILP (face))
17533 face = mode_line_string_face;
17534 else
17535 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
17536 props = Fcons (Qface, Fcons (face, Qnil));
17537 if (copy_string)
17538 lisp_string = Fcopy_sequence (lisp_string);
17539 }
17540 if (!NILP (props))
17541 Fadd_text_properties (make_number (0), make_number (len),
17542 props, lisp_string);
17543 }
17544
17545 if (len > 0)
17546 {
17547 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
17548 n += len;
17549 }
17550
17551 if (field_width > len)
17552 {
17553 field_width -= len;
17554 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
17555 if (!NILP (props))
17556 Fadd_text_properties (make_number (0), make_number (field_width),
17557 props, lisp_string);
17558 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
17559 n += field_width;
17560 }
17561
17562 return n;
17563 }
17564
17565
17566 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
17567 1, 4, 0,
17568 doc: /* Format a string out of a mode line format specification.
17569 First arg FORMAT specifies the mode line format (see `mode-line-format'
17570 for details) to use.
17571
17572 Optional second arg FACE specifies the face property to put
17573 on all characters for which no face is specified.
17574 The value t means whatever face the window's mode line currently uses
17575 \(either `mode-line' or `mode-line-inactive', depending).
17576 A value of nil means the default is no face property.
17577 If FACE is an integer, the value string has no text properties.
17578
17579 Optional third and fourth args WINDOW and BUFFER specify the window
17580 and buffer to use as the context for the formatting (defaults
17581 are the selected window and the window's buffer). */)
17582 (format, face, window, buffer)
17583 Lisp_Object format, face, window, buffer;
17584 {
17585 struct it it;
17586 int len;
17587 struct window *w;
17588 struct buffer *old_buffer = NULL;
17589 int face_id = -1;
17590 int no_props = INTEGERP (face);
17591 int count = SPECPDL_INDEX ();
17592 Lisp_Object str;
17593 int string_start = 0;
17594
17595 if (NILP (window))
17596 window = selected_window;
17597 CHECK_WINDOW (window);
17598 w = XWINDOW (window);
17599
17600 if (NILP (buffer))
17601 buffer = w->buffer;
17602 CHECK_BUFFER (buffer);
17603
17604 /* Make formatting the modeline a non-op when noninteractive, otherwise
17605 there will be problems later caused by a partially initialized frame. */
17606 if (NILP (format) || noninteractive)
17607 return empty_unibyte_string;
17608
17609 if (no_props)
17610 face = Qnil;
17611
17612 if (!NILP (face))
17613 {
17614 if (EQ (face, Qt))
17615 face = (EQ (window, selected_window) ? Qmode_line : Qmode_line_inactive);
17616 face_id = lookup_named_face (XFRAME (WINDOW_FRAME (w)), face, 0);
17617 }
17618
17619 if (face_id < 0)
17620 face_id = DEFAULT_FACE_ID;
17621
17622 if (XBUFFER (buffer) != current_buffer)
17623 old_buffer = current_buffer;
17624
17625 /* Save things including mode_line_proptrans_alist,
17626 and set that to nil so that we don't alter the outer value. */
17627 record_unwind_protect (unwind_format_mode_line,
17628 format_mode_line_unwind_data (old_buffer, 1));
17629 mode_line_proptrans_alist = Qnil;
17630
17631 if (old_buffer)
17632 set_buffer_internal_1 (XBUFFER (buffer));
17633
17634 init_iterator (&it, w, -1, -1, NULL, face_id);
17635
17636 if (no_props)
17637 {
17638 mode_line_target = MODE_LINE_NOPROP;
17639 mode_line_string_face_prop = Qnil;
17640 mode_line_string_list = Qnil;
17641 string_start = MODE_LINE_NOPROP_LEN (0);
17642 }
17643 else
17644 {
17645 mode_line_target = MODE_LINE_STRING;
17646 mode_line_string_list = Qnil;
17647 mode_line_string_face = face;
17648 mode_line_string_face_prop
17649 = (NILP (face) ? Qnil : Fcons (Qface, Fcons (face, Qnil)));
17650 }
17651
17652 push_kboard (FRAME_KBOARD (it.f));
17653 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
17654 pop_kboard ();
17655
17656 if (no_props)
17657 {
17658 len = MODE_LINE_NOPROP_LEN (string_start);
17659 str = make_string (mode_line_noprop_buf + string_start, len);
17660 }
17661 else
17662 {
17663 mode_line_string_list = Fnreverse (mode_line_string_list);
17664 str = Fmapconcat (intern ("identity"), mode_line_string_list,
17665 empty_unibyte_string);
17666 }
17667
17668 unbind_to (count, Qnil);
17669 return str;
17670 }
17671
17672 /* Write a null-terminated, right justified decimal representation of
17673 the positive integer D to BUF using a minimal field width WIDTH. */
17674
17675 static void
17676 pint2str (buf, width, d)
17677 register char *buf;
17678 register int width;
17679 register int d;
17680 {
17681 register char *p = buf;
17682
17683 if (d <= 0)
17684 *p++ = '0';
17685 else
17686 {
17687 while (d > 0)
17688 {
17689 *p++ = d % 10 + '0';
17690 d /= 10;
17691 }
17692 }
17693
17694 for (width -= (int) (p - buf); width > 0; --width)
17695 *p++ = ' ';
17696 *p-- = '\0';
17697 while (p > buf)
17698 {
17699 d = *buf;
17700 *buf++ = *p;
17701 *p-- = d;
17702 }
17703 }
17704
17705 /* Write a null-terminated, right justified decimal and "human
17706 readable" representation of the nonnegative integer D to BUF using
17707 a minimal field width WIDTH. D should be smaller than 999.5e24. */
17708
17709 static const char power_letter[] =
17710 {
17711 0, /* not used */
17712 'k', /* kilo */
17713 'M', /* mega */
17714 'G', /* giga */
17715 'T', /* tera */
17716 'P', /* peta */
17717 'E', /* exa */
17718 'Z', /* zetta */
17719 'Y' /* yotta */
17720 };
17721
17722 static void
17723 pint2hrstr (buf, width, d)
17724 char *buf;
17725 int width;
17726 int d;
17727 {
17728 /* We aim to represent the nonnegative integer D as
17729 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
17730 int quotient = d;
17731 int remainder = 0;
17732 /* -1 means: do not use TENTHS. */
17733 int tenths = -1;
17734 int exponent = 0;
17735
17736 /* Length of QUOTIENT.TENTHS as a string. */
17737 int length;
17738
17739 char * psuffix;
17740 char * p;
17741
17742 if (1000 <= quotient)
17743 {
17744 /* Scale to the appropriate EXPONENT. */
17745 do
17746 {
17747 remainder = quotient % 1000;
17748 quotient /= 1000;
17749 exponent++;
17750 }
17751 while (1000 <= quotient);
17752
17753 /* Round to nearest and decide whether to use TENTHS or not. */
17754 if (quotient <= 9)
17755 {
17756 tenths = remainder / 100;
17757 if (50 <= remainder % 100)
17758 {
17759 if (tenths < 9)
17760 tenths++;
17761 else
17762 {
17763 quotient++;
17764 if (quotient == 10)
17765 tenths = -1;
17766 else
17767 tenths = 0;
17768 }
17769 }
17770 }
17771 else
17772 if (500 <= remainder)
17773 {
17774 if (quotient < 999)
17775 quotient++;
17776 else
17777 {
17778 quotient = 1;
17779 exponent++;
17780 tenths = 0;
17781 }
17782 }
17783 }
17784
17785 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
17786 if (tenths == -1 && quotient <= 99)
17787 if (quotient <= 9)
17788 length = 1;
17789 else
17790 length = 2;
17791 else
17792 length = 3;
17793 p = psuffix = buf + max (width, length);
17794
17795 /* Print EXPONENT. */
17796 if (exponent)
17797 *psuffix++ = power_letter[exponent];
17798 *psuffix = '\0';
17799
17800 /* Print TENTHS. */
17801 if (tenths >= 0)
17802 {
17803 *--p = '0' + tenths;
17804 *--p = '.';
17805 }
17806
17807 /* Print QUOTIENT. */
17808 do
17809 {
17810 int digit = quotient % 10;
17811 *--p = '0' + digit;
17812 }
17813 while ((quotient /= 10) != 0);
17814
17815 /* Print leading spaces. */
17816 while (buf < p)
17817 *--p = ' ';
17818 }
17819
17820 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
17821 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
17822 type of CODING_SYSTEM. Return updated pointer into BUF. */
17823
17824 static unsigned char invalid_eol_type[] = "(*invalid*)";
17825
17826 static char *
17827 decode_mode_spec_coding (coding_system, buf, eol_flag)
17828 Lisp_Object coding_system;
17829 register char *buf;
17830 int eol_flag;
17831 {
17832 Lisp_Object val;
17833 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
17834 const unsigned char *eol_str;
17835 int eol_str_len;
17836 /* The EOL conversion we are using. */
17837 Lisp_Object eoltype;
17838
17839 val = CODING_SYSTEM_SPEC (coding_system);
17840 eoltype = Qnil;
17841
17842 if (!VECTORP (val)) /* Not yet decided. */
17843 {
17844 if (multibyte)
17845 *buf++ = '-';
17846 if (eol_flag)
17847 eoltype = eol_mnemonic_undecided;
17848 /* Don't mention EOL conversion if it isn't decided. */
17849 }
17850 else
17851 {
17852 Lisp_Object attrs;
17853 Lisp_Object eolvalue;
17854
17855 attrs = AREF (val, 0);
17856 eolvalue = AREF (val, 2);
17857
17858 if (multibyte)
17859 *buf++ = XFASTINT (CODING_ATTR_MNEMONIC (attrs));
17860
17861 if (eol_flag)
17862 {
17863 /* The EOL conversion that is normal on this system. */
17864
17865 if (NILP (eolvalue)) /* Not yet decided. */
17866 eoltype = eol_mnemonic_undecided;
17867 else if (VECTORP (eolvalue)) /* Not yet decided. */
17868 eoltype = eol_mnemonic_undecided;
17869 else /* eolvalue is Qunix, Qdos, or Qmac. */
17870 eoltype = (EQ (eolvalue, Qunix)
17871 ? eol_mnemonic_unix
17872 : (EQ (eolvalue, Qdos) == 1
17873 ? eol_mnemonic_dos : eol_mnemonic_mac));
17874 }
17875 }
17876
17877 if (eol_flag)
17878 {
17879 /* Mention the EOL conversion if it is not the usual one. */
17880 if (STRINGP (eoltype))
17881 {
17882 eol_str = SDATA (eoltype);
17883 eol_str_len = SBYTES (eoltype);
17884 }
17885 else if (CHARACTERP (eoltype))
17886 {
17887 unsigned char *tmp = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
17888 eol_str_len = CHAR_STRING (XINT (eoltype), tmp);
17889 eol_str = tmp;
17890 }
17891 else
17892 {
17893 eol_str = invalid_eol_type;
17894 eol_str_len = sizeof (invalid_eol_type) - 1;
17895 }
17896 bcopy (eol_str, buf, eol_str_len);
17897 buf += eol_str_len;
17898 }
17899
17900 return buf;
17901 }
17902
17903 /* Return a string for the output of a mode line %-spec for window W,
17904 generated by character C. PRECISION >= 0 means don't return a
17905 string longer than that value. FIELD_WIDTH > 0 means pad the
17906 string returned with spaces to that value. Return 1 in *MULTIBYTE
17907 if the result is multibyte text.
17908
17909 Note we operate on the current buffer for most purposes,
17910 the exception being w->base_line_pos. */
17911
17912 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
17913
17914 static char *
17915 decode_mode_spec (w, c, field_width, precision, multibyte)
17916 struct window *w;
17917 register int c;
17918 int field_width, precision;
17919 int *multibyte;
17920 {
17921 Lisp_Object obj;
17922 struct frame *f = XFRAME (WINDOW_FRAME (w));
17923 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
17924 struct buffer *b = current_buffer;
17925
17926 obj = Qnil;
17927 *multibyte = 0;
17928
17929 switch (c)
17930 {
17931 case '*':
17932 if (!NILP (b->read_only))
17933 return "%";
17934 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
17935 return "*";
17936 return "-";
17937
17938 case '+':
17939 /* This differs from %* only for a modified read-only buffer. */
17940 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
17941 return "*";
17942 if (!NILP (b->read_only))
17943 return "%";
17944 return "-";
17945
17946 case '&':
17947 /* This differs from %* in ignoring read-only-ness. */
17948 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
17949 return "*";
17950 return "-";
17951
17952 case '%':
17953 return "%";
17954
17955 case '[':
17956 {
17957 int i;
17958 char *p;
17959
17960 if (command_loop_level > 5)
17961 return "[[[... ";
17962 p = decode_mode_spec_buf;
17963 for (i = 0; i < command_loop_level; i++)
17964 *p++ = '[';
17965 *p = 0;
17966 return decode_mode_spec_buf;
17967 }
17968
17969 case ']':
17970 {
17971 int i;
17972 char *p;
17973
17974 if (command_loop_level > 5)
17975 return " ...]]]";
17976 p = decode_mode_spec_buf;
17977 for (i = 0; i < command_loop_level; i++)
17978 *p++ = ']';
17979 *p = 0;
17980 return decode_mode_spec_buf;
17981 }
17982
17983 case '-':
17984 {
17985 register int i;
17986
17987 /* Let lots_of_dashes be a string of infinite length. */
17988 if (mode_line_target == MODE_LINE_NOPROP ||
17989 mode_line_target == MODE_LINE_STRING)
17990 return "--";
17991 if (field_width <= 0
17992 || field_width > sizeof (lots_of_dashes))
17993 {
17994 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
17995 decode_mode_spec_buf[i] = '-';
17996 decode_mode_spec_buf[i] = '\0';
17997 return decode_mode_spec_buf;
17998 }
17999 else
18000 return lots_of_dashes;
18001 }
18002
18003 case 'b':
18004 obj = b->name;
18005 break;
18006
18007 case 'c':
18008 /* %c and %l are ignored in `frame-title-format'.
18009 (In redisplay_internal, the frame title is drawn _before_ the
18010 windows are updated, so the stuff which depends on actual
18011 window contents (such as %l) may fail to render properly, or
18012 even crash emacs.) */
18013 if (mode_line_target == MODE_LINE_TITLE)
18014 return "";
18015 else
18016 {
18017 int col = (int) current_column (); /* iftc */
18018 w->column_number_displayed = make_number (col);
18019 pint2str (decode_mode_spec_buf, field_width, col);
18020 return decode_mode_spec_buf;
18021 }
18022
18023 case 'e':
18024 #ifndef SYSTEM_MALLOC
18025 {
18026 if (NILP (Vmemory_full))
18027 return "";
18028 else
18029 return "!MEM FULL! ";
18030 }
18031 #else
18032 return "";
18033 #endif
18034
18035 case 'F':
18036 /* %F displays the frame name. */
18037 if (!NILP (f->title))
18038 return (char *) SDATA (f->title);
18039 if (f->explicit_name || ! FRAME_WINDOW_P (f))
18040 return (char *) SDATA (f->name);
18041 return "Emacs";
18042
18043 case 'f':
18044 obj = b->filename;
18045 break;
18046
18047 case 'i':
18048 {
18049 int size = ZV - BEGV;
18050 pint2str (decode_mode_spec_buf, field_width, size);
18051 return decode_mode_spec_buf;
18052 }
18053
18054 case 'I':
18055 {
18056 int size = ZV - BEGV;
18057 pint2hrstr (decode_mode_spec_buf, field_width, size);
18058 return decode_mode_spec_buf;
18059 }
18060
18061 case 'l':
18062 {
18063 int startpos, startpos_byte, line, linepos, linepos_byte;
18064 int topline, nlines, junk, height;
18065
18066 /* %c and %l are ignored in `frame-title-format'. */
18067 if (mode_line_target == MODE_LINE_TITLE)
18068 return "";
18069
18070 startpos = XMARKER (w->start)->charpos;
18071 startpos_byte = marker_byte_position (w->start);
18072 height = WINDOW_TOTAL_LINES (w);
18073
18074 /* If we decided that this buffer isn't suitable for line numbers,
18075 don't forget that too fast. */
18076 if (EQ (w->base_line_pos, w->buffer))
18077 goto no_value;
18078 /* But do forget it, if the window shows a different buffer now. */
18079 else if (BUFFERP (w->base_line_pos))
18080 w->base_line_pos = Qnil;
18081
18082 /* If the buffer is very big, don't waste time. */
18083 if (INTEGERP (Vline_number_display_limit)
18084 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
18085 {
18086 w->base_line_pos = Qnil;
18087 w->base_line_number = Qnil;
18088 goto no_value;
18089 }
18090
18091 if (!NILP (w->base_line_number)
18092 && !NILP (w->base_line_pos)
18093 && XFASTINT (w->base_line_pos) <= startpos)
18094 {
18095 line = XFASTINT (w->base_line_number);
18096 linepos = XFASTINT (w->base_line_pos);
18097 linepos_byte = buf_charpos_to_bytepos (b, linepos);
18098 }
18099 else
18100 {
18101 line = 1;
18102 linepos = BUF_BEGV (b);
18103 linepos_byte = BUF_BEGV_BYTE (b);
18104 }
18105
18106 /* Count lines from base line to window start position. */
18107 nlines = display_count_lines (linepos, linepos_byte,
18108 startpos_byte,
18109 startpos, &junk);
18110
18111 topline = nlines + line;
18112
18113 /* Determine a new base line, if the old one is too close
18114 or too far away, or if we did not have one.
18115 "Too close" means it's plausible a scroll-down would
18116 go back past it. */
18117 if (startpos == BUF_BEGV (b))
18118 {
18119 w->base_line_number = make_number (topline);
18120 w->base_line_pos = make_number (BUF_BEGV (b));
18121 }
18122 else if (nlines < height + 25 || nlines > height * 3 + 50
18123 || linepos == BUF_BEGV (b))
18124 {
18125 int limit = BUF_BEGV (b);
18126 int limit_byte = BUF_BEGV_BYTE (b);
18127 int position;
18128 int distance = (height * 2 + 30) * line_number_display_limit_width;
18129
18130 if (startpos - distance > limit)
18131 {
18132 limit = startpos - distance;
18133 limit_byte = CHAR_TO_BYTE (limit);
18134 }
18135
18136 nlines = display_count_lines (startpos, startpos_byte,
18137 limit_byte,
18138 - (height * 2 + 30),
18139 &position);
18140 /* If we couldn't find the lines we wanted within
18141 line_number_display_limit_width chars per line,
18142 give up on line numbers for this window. */
18143 if (position == limit_byte && limit == startpos - distance)
18144 {
18145 w->base_line_pos = w->buffer;
18146 w->base_line_number = Qnil;
18147 goto no_value;
18148 }
18149
18150 w->base_line_number = make_number (topline - nlines);
18151 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
18152 }
18153
18154 /* Now count lines from the start pos to point. */
18155 nlines = display_count_lines (startpos, startpos_byte,
18156 PT_BYTE, PT, &junk);
18157
18158 /* Record that we did display the line number. */
18159 line_number_displayed = 1;
18160
18161 /* Make the string to show. */
18162 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
18163 return decode_mode_spec_buf;
18164 no_value:
18165 {
18166 char* p = decode_mode_spec_buf;
18167 int pad = field_width - 2;
18168 while (pad-- > 0)
18169 *p++ = ' ';
18170 *p++ = '?';
18171 *p++ = '?';
18172 *p = '\0';
18173 return decode_mode_spec_buf;
18174 }
18175 }
18176 break;
18177
18178 case 'm':
18179 obj = b->mode_name;
18180 break;
18181
18182 case 'n':
18183 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
18184 return " Narrow";
18185 break;
18186
18187 case 'p':
18188 {
18189 int pos = marker_position (w->start);
18190 int total = BUF_ZV (b) - BUF_BEGV (b);
18191
18192 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
18193 {
18194 if (pos <= BUF_BEGV (b))
18195 return "All";
18196 else
18197 return "Bottom";
18198 }
18199 else if (pos <= BUF_BEGV (b))
18200 return "Top";
18201 else
18202 {
18203 if (total > 1000000)
18204 /* Do it differently for a large value, to avoid overflow. */
18205 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
18206 else
18207 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
18208 /* We can't normally display a 3-digit number,
18209 so get us a 2-digit number that is close. */
18210 if (total == 100)
18211 total = 99;
18212 sprintf (decode_mode_spec_buf, "%2d%%", total);
18213 return decode_mode_spec_buf;
18214 }
18215 }
18216
18217 /* Display percentage of size above the bottom of the screen. */
18218 case 'P':
18219 {
18220 int toppos = marker_position (w->start);
18221 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
18222 int total = BUF_ZV (b) - BUF_BEGV (b);
18223
18224 if (botpos >= BUF_ZV (b))
18225 {
18226 if (toppos <= BUF_BEGV (b))
18227 return "All";
18228 else
18229 return "Bottom";
18230 }
18231 else
18232 {
18233 if (total > 1000000)
18234 /* Do it differently for a large value, to avoid overflow. */
18235 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
18236 else
18237 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
18238 /* We can't normally display a 3-digit number,
18239 so get us a 2-digit number that is close. */
18240 if (total == 100)
18241 total = 99;
18242 if (toppos <= BUF_BEGV (b))
18243 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
18244 else
18245 sprintf (decode_mode_spec_buf, "%2d%%", total);
18246 return decode_mode_spec_buf;
18247 }
18248 }
18249
18250 case 's':
18251 /* status of process */
18252 obj = Fget_buffer_process (Fcurrent_buffer ());
18253 if (NILP (obj))
18254 return "no process";
18255 #ifdef subprocesses
18256 obj = Fsymbol_name (Fprocess_status (obj));
18257 #endif
18258 break;
18259
18260 case '@':
18261 {
18262 Lisp_Object val;
18263 val = call1 (intern ("file-remote-p"), current_buffer->directory);
18264 if (NILP (val))
18265 return "-";
18266 else
18267 return "@";
18268 }
18269
18270 case 't': /* indicate TEXT or BINARY */
18271 #ifdef MODE_LINE_BINARY_TEXT
18272 return MODE_LINE_BINARY_TEXT (b);
18273 #else
18274 return "T";
18275 #endif
18276
18277 case 'z':
18278 /* coding-system (not including end-of-line format) */
18279 case 'Z':
18280 /* coding-system (including end-of-line type) */
18281 {
18282 int eol_flag = (c == 'Z');
18283 char *p = decode_mode_spec_buf;
18284
18285 if (! FRAME_WINDOW_P (f))
18286 {
18287 /* No need to mention EOL here--the terminal never needs
18288 to do EOL conversion. */
18289 p = decode_mode_spec_coding (CODING_ID_NAME
18290 (FRAME_KEYBOARD_CODING (f)->id),
18291 p, 0);
18292 p = decode_mode_spec_coding (CODING_ID_NAME
18293 (FRAME_TERMINAL_CODING (f)->id),
18294 p, 0);
18295 }
18296 p = decode_mode_spec_coding (b->buffer_file_coding_system,
18297 p, eol_flag);
18298
18299 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
18300 #ifdef subprocesses
18301 obj = Fget_buffer_process (Fcurrent_buffer ());
18302 if (PROCESSP (obj))
18303 {
18304 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
18305 p, eol_flag);
18306 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
18307 p, eol_flag);
18308 }
18309 #endif /* subprocesses */
18310 #endif /* 0 */
18311 *p = 0;
18312 return decode_mode_spec_buf;
18313 }
18314 }
18315
18316 if (STRINGP (obj))
18317 {
18318 *multibyte = STRING_MULTIBYTE (obj);
18319 return (char *) SDATA (obj);
18320 }
18321 else
18322 return "";
18323 }
18324
18325
18326 /* Count up to COUNT lines starting from START / START_BYTE.
18327 But don't go beyond LIMIT_BYTE.
18328 Return the number of lines thus found (always nonnegative).
18329
18330 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
18331
18332 static int
18333 display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
18334 int start, start_byte, limit_byte, count;
18335 int *byte_pos_ptr;
18336 {
18337 register unsigned char *cursor;
18338 unsigned char *base;
18339
18340 register int ceiling;
18341 register unsigned char *ceiling_addr;
18342 int orig_count = count;
18343
18344 /* If we are not in selective display mode,
18345 check only for newlines. */
18346 int selective_display = (!NILP (current_buffer->selective_display)
18347 && !INTEGERP (current_buffer->selective_display));
18348
18349 if (count > 0)
18350 {
18351 while (start_byte < limit_byte)
18352 {
18353 ceiling = BUFFER_CEILING_OF (start_byte);
18354 ceiling = min (limit_byte - 1, ceiling);
18355 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
18356 base = (cursor = BYTE_POS_ADDR (start_byte));
18357 while (1)
18358 {
18359 if (selective_display)
18360 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
18361 ;
18362 else
18363 while (*cursor != '\n' && ++cursor != ceiling_addr)
18364 ;
18365
18366 if (cursor != ceiling_addr)
18367 {
18368 if (--count == 0)
18369 {
18370 start_byte += cursor - base + 1;
18371 *byte_pos_ptr = start_byte;
18372 return orig_count;
18373 }
18374 else
18375 if (++cursor == ceiling_addr)
18376 break;
18377 }
18378 else
18379 break;
18380 }
18381 start_byte += cursor - base;
18382 }
18383 }
18384 else
18385 {
18386 while (start_byte > limit_byte)
18387 {
18388 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
18389 ceiling = max (limit_byte, ceiling);
18390 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
18391 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
18392 while (1)
18393 {
18394 if (selective_display)
18395 while (--cursor != ceiling_addr
18396 && *cursor != '\n' && *cursor != 015)
18397 ;
18398 else
18399 while (--cursor != ceiling_addr && *cursor != '\n')
18400 ;
18401
18402 if (cursor != ceiling_addr)
18403 {
18404 if (++count == 0)
18405 {
18406 start_byte += cursor - base + 1;
18407 *byte_pos_ptr = start_byte;
18408 /* When scanning backwards, we should
18409 not count the newline posterior to which we stop. */
18410 return - orig_count - 1;
18411 }
18412 }
18413 else
18414 break;
18415 }
18416 /* Here we add 1 to compensate for the last decrement
18417 of CURSOR, which took it past the valid range. */
18418 start_byte += cursor - base + 1;
18419 }
18420 }
18421
18422 *byte_pos_ptr = limit_byte;
18423
18424 if (count < 0)
18425 return - orig_count + count;
18426 return orig_count - count;
18427
18428 }
18429
18430
18431 \f
18432 /***********************************************************************
18433 Displaying strings
18434 ***********************************************************************/
18435
18436 /* Display a NUL-terminated string, starting with index START.
18437
18438 If STRING is non-null, display that C string. Otherwise, the Lisp
18439 string LISP_STRING is displayed.
18440
18441 If FACE_STRING is not nil, FACE_STRING_POS is a position in
18442 FACE_STRING. Display STRING or LISP_STRING with the face at
18443 FACE_STRING_POS in FACE_STRING:
18444
18445 Display the string in the environment given by IT, but use the
18446 standard display table, temporarily.
18447
18448 FIELD_WIDTH is the minimum number of output glyphs to produce.
18449 If STRING has fewer characters than FIELD_WIDTH, pad to the right
18450 with spaces. If STRING has more characters, more than FIELD_WIDTH
18451 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
18452
18453 PRECISION is the maximum number of characters to output from
18454 STRING. PRECISION < 0 means don't truncate the string.
18455
18456 This is roughly equivalent to printf format specifiers:
18457
18458 FIELD_WIDTH PRECISION PRINTF
18459 ----------------------------------------
18460 -1 -1 %s
18461 -1 10 %.10s
18462 10 -1 %10s
18463 20 10 %20.10s
18464
18465 MULTIBYTE zero means do not display multibyte chars, > 0 means do
18466 display them, and < 0 means obey the current buffer's value of
18467 enable_multibyte_characters.
18468
18469 Value is the number of columns displayed. */
18470
18471 static int
18472 display_string (string, lisp_string, face_string, face_string_pos,
18473 start, it, field_width, precision, max_x, multibyte)
18474 unsigned char *string;
18475 Lisp_Object lisp_string;
18476 Lisp_Object face_string;
18477 int face_string_pos;
18478 int start;
18479 struct it *it;
18480 int field_width, precision, max_x;
18481 int multibyte;
18482 {
18483 int hpos_at_start = it->hpos;
18484 int saved_face_id = it->face_id;
18485 struct glyph_row *row = it->glyph_row;
18486
18487 /* Initialize the iterator IT for iteration over STRING beginning
18488 with index START. */
18489 reseat_to_string (it, string, lisp_string, start,
18490 precision, field_width, multibyte);
18491
18492 /* If displaying STRING, set up the face of the iterator
18493 from LISP_STRING, if that's given. */
18494 if (STRINGP (face_string))
18495 {
18496 int endptr;
18497 struct face *face;
18498
18499 it->face_id
18500 = face_at_string_position (it->w, face_string, face_string_pos,
18501 0, it->region_beg_charpos,
18502 it->region_end_charpos,
18503 &endptr, it->base_face_id, 0);
18504 face = FACE_FROM_ID (it->f, it->face_id);
18505 it->face_box_p = face->box != FACE_NO_BOX;
18506 }
18507
18508 /* Set max_x to the maximum allowed X position. Don't let it go
18509 beyond the right edge of the window. */
18510 if (max_x <= 0)
18511 max_x = it->last_visible_x;
18512 else
18513 max_x = min (max_x, it->last_visible_x);
18514
18515 /* Skip over display elements that are not visible. because IT->w is
18516 hscrolled. */
18517 if (it->current_x < it->first_visible_x)
18518 move_it_in_display_line_to (it, 100000, it->first_visible_x,
18519 MOVE_TO_POS | MOVE_TO_X);
18520
18521 row->ascent = it->max_ascent;
18522 row->height = it->max_ascent + it->max_descent;
18523 row->phys_ascent = it->max_phys_ascent;
18524 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
18525 row->extra_line_spacing = it->max_extra_line_spacing;
18526
18527 /* This condition is for the case that we are called with current_x
18528 past last_visible_x. */
18529 while (it->current_x < max_x)
18530 {
18531 int x_before, x, n_glyphs_before, i, nglyphs;
18532
18533 /* Get the next display element. */
18534 if (!get_next_display_element (it))
18535 break;
18536
18537 /* Produce glyphs. */
18538 x_before = it->current_x;
18539 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
18540 PRODUCE_GLYPHS (it);
18541
18542 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
18543 i = 0;
18544 x = x_before;
18545 while (i < nglyphs)
18546 {
18547 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
18548
18549 if (!it->truncate_lines_p
18550 && x + glyph->pixel_width > max_x)
18551 {
18552 /* End of continued line or max_x reached. */
18553 if (CHAR_GLYPH_PADDING_P (*glyph))
18554 {
18555 /* A wide character is unbreakable. */
18556 it->glyph_row->used[TEXT_AREA] = n_glyphs_before;
18557 it->current_x = x_before;
18558 }
18559 else
18560 {
18561 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
18562 it->current_x = x;
18563 }
18564 break;
18565 }
18566 else if (x + glyph->pixel_width >= it->first_visible_x)
18567 {
18568 /* Glyph is at least partially visible. */
18569 ++it->hpos;
18570 if (x < it->first_visible_x)
18571 it->glyph_row->x = x - it->first_visible_x;
18572 }
18573 else
18574 {
18575 /* Glyph is off the left margin of the display area.
18576 Should not happen. */
18577 abort ();
18578 }
18579
18580 row->ascent = max (row->ascent, it->max_ascent);
18581 row->height = max (row->height, it->max_ascent + it->max_descent);
18582 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
18583 row->phys_height = max (row->phys_height,
18584 it->max_phys_ascent + it->max_phys_descent);
18585 row->extra_line_spacing = max (row->extra_line_spacing,
18586 it->max_extra_line_spacing);
18587 x += glyph->pixel_width;
18588 ++i;
18589 }
18590
18591 /* Stop if max_x reached. */
18592 if (i < nglyphs)
18593 break;
18594
18595 /* Stop at line ends. */
18596 if (ITERATOR_AT_END_OF_LINE_P (it))
18597 {
18598 it->continuation_lines_width = 0;
18599 break;
18600 }
18601
18602 set_iterator_to_next (it, 1);
18603
18604 /* Stop if truncating at the right edge. */
18605 if (it->truncate_lines_p
18606 && it->current_x >= it->last_visible_x)
18607 {
18608 /* Add truncation mark, but don't do it if the line is
18609 truncated at a padding space. */
18610 if (IT_CHARPOS (*it) < it->string_nchars)
18611 {
18612 if (!FRAME_WINDOW_P (it->f))
18613 {
18614 int i, n;
18615
18616 if (it->current_x > it->last_visible_x)
18617 {
18618 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
18619 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
18620 break;
18621 for (n = row->used[TEXT_AREA]; i < n; ++i)
18622 {
18623 row->used[TEXT_AREA] = i;
18624 produce_special_glyphs (it, IT_TRUNCATION);
18625 }
18626 }
18627 produce_special_glyphs (it, IT_TRUNCATION);
18628 }
18629 it->glyph_row->truncated_on_right_p = 1;
18630 }
18631 break;
18632 }
18633 }
18634
18635 /* Maybe insert a truncation at the left. */
18636 if (it->first_visible_x
18637 && IT_CHARPOS (*it) > 0)
18638 {
18639 if (!FRAME_WINDOW_P (it->f))
18640 insert_left_trunc_glyphs (it);
18641 it->glyph_row->truncated_on_left_p = 1;
18642 }
18643
18644 it->face_id = saved_face_id;
18645
18646 /* Value is number of columns displayed. */
18647 return it->hpos - hpos_at_start;
18648 }
18649
18650
18651 \f
18652 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
18653 appears as an element of LIST or as the car of an element of LIST.
18654 If PROPVAL is a list, compare each element against LIST in that
18655 way, and return 1/2 if any element of PROPVAL is found in LIST.
18656 Otherwise return 0. This function cannot quit.
18657 The return value is 2 if the text is invisible but with an ellipsis
18658 and 1 if it's invisible and without an ellipsis. */
18659
18660 int
18661 invisible_p (propval, list)
18662 register Lisp_Object propval;
18663 Lisp_Object list;
18664 {
18665 register Lisp_Object tail, proptail;
18666
18667 for (tail = list; CONSP (tail); tail = XCDR (tail))
18668 {
18669 register Lisp_Object tem;
18670 tem = XCAR (tail);
18671 if (EQ (propval, tem))
18672 return 1;
18673 if (CONSP (tem) && EQ (propval, XCAR (tem)))
18674 return NILP (XCDR (tem)) ? 1 : 2;
18675 }
18676
18677 if (CONSP (propval))
18678 {
18679 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
18680 {
18681 Lisp_Object propelt;
18682 propelt = XCAR (proptail);
18683 for (tail = list; CONSP (tail); tail = XCDR (tail))
18684 {
18685 register Lisp_Object tem;
18686 tem = XCAR (tail);
18687 if (EQ (propelt, tem))
18688 return 1;
18689 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
18690 return NILP (XCDR (tem)) ? 1 : 2;
18691 }
18692 }
18693 }
18694
18695 return 0;
18696 }
18697
18698 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
18699 doc: /* Non-nil if the property makes the text invisible.
18700 POS-OR-PROP can be a marker or number, in which case it is taken to be
18701 a position in the current buffer and the value of the `invisible' property
18702 is checked; or it can be some other value, which is then presumed to be the
18703 value of the `invisible' property of the text of interest.
18704 The non-nil value returned can be t for truly invisible text or something
18705 else if the text is replaced by an ellipsis. */)
18706 (pos_or_prop)
18707 Lisp_Object pos_or_prop;
18708 {
18709 Lisp_Object prop
18710 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
18711 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
18712 : pos_or_prop);
18713 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
18714 return (invis == 0 ? Qnil
18715 : invis == 1 ? Qt
18716 : make_number (invis));
18717 }
18718
18719 /* Calculate a width or height in pixels from a specification using
18720 the following elements:
18721
18722 SPEC ::=
18723 NUM - a (fractional) multiple of the default font width/height
18724 (NUM) - specifies exactly NUM pixels
18725 UNIT - a fixed number of pixels, see below.
18726 ELEMENT - size of a display element in pixels, see below.
18727 (NUM . SPEC) - equals NUM * SPEC
18728 (+ SPEC SPEC ...) - add pixel values
18729 (- SPEC SPEC ...) - subtract pixel values
18730 (- SPEC) - negate pixel value
18731
18732 NUM ::=
18733 INT or FLOAT - a number constant
18734 SYMBOL - use symbol's (buffer local) variable binding.
18735
18736 UNIT ::=
18737 in - pixels per inch *)
18738 mm - pixels per 1/1000 meter *)
18739 cm - pixels per 1/100 meter *)
18740 width - width of current font in pixels.
18741 height - height of current font in pixels.
18742
18743 *) using the ratio(s) defined in display-pixels-per-inch.
18744
18745 ELEMENT ::=
18746
18747 left-fringe - left fringe width in pixels
18748 right-fringe - right fringe width in pixels
18749
18750 left-margin - left margin width in pixels
18751 right-margin - right margin width in pixels
18752
18753 scroll-bar - scroll-bar area width in pixels
18754
18755 Examples:
18756
18757 Pixels corresponding to 5 inches:
18758 (5 . in)
18759
18760 Total width of non-text areas on left side of window (if scroll-bar is on left):
18761 '(space :width (+ left-fringe left-margin scroll-bar))
18762
18763 Align to first text column (in header line):
18764 '(space :align-to 0)
18765
18766 Align to middle of text area minus half the width of variable `my-image'
18767 containing a loaded image:
18768 '(space :align-to (0.5 . (- text my-image)))
18769
18770 Width of left margin minus width of 1 character in the default font:
18771 '(space :width (- left-margin 1))
18772
18773 Width of left margin minus width of 2 characters in the current font:
18774 '(space :width (- left-margin (2 . width)))
18775
18776 Center 1 character over left-margin (in header line):
18777 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
18778
18779 Different ways to express width of left fringe plus left margin minus one pixel:
18780 '(space :width (- (+ left-fringe left-margin) (1)))
18781 '(space :width (+ left-fringe left-margin (- (1))))
18782 '(space :width (+ left-fringe left-margin (-1)))
18783
18784 */
18785
18786 #define NUMVAL(X) \
18787 ((INTEGERP (X) || FLOATP (X)) \
18788 ? XFLOATINT (X) \
18789 : - 1)
18790
18791 int
18792 calc_pixel_width_or_height (res, it, prop, font, width_p, align_to)
18793 double *res;
18794 struct it *it;
18795 Lisp_Object prop;
18796 void *font;
18797 int width_p, *align_to;
18798 {
18799 double pixels;
18800
18801 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
18802 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
18803
18804 if (NILP (prop))
18805 return OK_PIXELS (0);
18806
18807 xassert (FRAME_LIVE_P (it->f));
18808
18809 if (SYMBOLP (prop))
18810 {
18811 if (SCHARS (SYMBOL_NAME (prop)) == 2)
18812 {
18813 char *unit = SDATA (SYMBOL_NAME (prop));
18814
18815 if (unit[0] == 'i' && unit[1] == 'n')
18816 pixels = 1.0;
18817 else if (unit[0] == 'm' && unit[1] == 'm')
18818 pixels = 25.4;
18819 else if (unit[0] == 'c' && unit[1] == 'm')
18820 pixels = 2.54;
18821 else
18822 pixels = 0;
18823 if (pixels > 0)
18824 {
18825 double ppi;
18826 #ifdef HAVE_WINDOW_SYSTEM
18827 if (FRAME_WINDOW_P (it->f)
18828 && (ppi = (width_p
18829 ? FRAME_X_DISPLAY_INFO (it->f)->resx
18830 : FRAME_X_DISPLAY_INFO (it->f)->resy),
18831 ppi > 0))
18832 return OK_PIXELS (ppi / pixels);
18833 #endif
18834
18835 if ((ppi = NUMVAL (Vdisplay_pixels_per_inch), ppi > 0)
18836 || (CONSP (Vdisplay_pixels_per_inch)
18837 && (ppi = (width_p
18838 ? NUMVAL (XCAR (Vdisplay_pixels_per_inch))
18839 : NUMVAL (XCDR (Vdisplay_pixels_per_inch))),
18840 ppi > 0)))
18841 return OK_PIXELS (ppi / pixels);
18842
18843 return 0;
18844 }
18845 }
18846
18847 #ifdef HAVE_WINDOW_SYSTEM
18848 if (EQ (prop, Qheight))
18849 return OK_PIXELS (font ? FONT_HEIGHT ((XFontStruct *)font) : FRAME_LINE_HEIGHT (it->f));
18850 if (EQ (prop, Qwidth))
18851 return OK_PIXELS (font ? FONT_WIDTH ((XFontStruct *)font) : FRAME_COLUMN_WIDTH (it->f));
18852 #else
18853 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
18854 return OK_PIXELS (1);
18855 #endif
18856
18857 if (EQ (prop, Qtext))
18858 return OK_PIXELS (width_p
18859 ? window_box_width (it->w, TEXT_AREA)
18860 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
18861
18862 if (align_to && *align_to < 0)
18863 {
18864 *res = 0;
18865 if (EQ (prop, Qleft))
18866 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
18867 if (EQ (prop, Qright))
18868 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
18869 if (EQ (prop, Qcenter))
18870 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
18871 + window_box_width (it->w, TEXT_AREA) / 2);
18872 if (EQ (prop, Qleft_fringe))
18873 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
18874 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
18875 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
18876 if (EQ (prop, Qright_fringe))
18877 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
18878 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
18879 : window_box_right_offset (it->w, TEXT_AREA));
18880 if (EQ (prop, Qleft_margin))
18881 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
18882 if (EQ (prop, Qright_margin))
18883 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
18884 if (EQ (prop, Qscroll_bar))
18885 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
18886 ? 0
18887 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
18888 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
18889 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
18890 : 0)));
18891 }
18892 else
18893 {
18894 if (EQ (prop, Qleft_fringe))
18895 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
18896 if (EQ (prop, Qright_fringe))
18897 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
18898 if (EQ (prop, Qleft_margin))
18899 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
18900 if (EQ (prop, Qright_margin))
18901 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
18902 if (EQ (prop, Qscroll_bar))
18903 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
18904 }
18905
18906 prop = Fbuffer_local_value (prop, it->w->buffer);
18907 }
18908
18909 if (INTEGERP (prop) || FLOATP (prop))
18910 {
18911 int base_unit = (width_p
18912 ? FRAME_COLUMN_WIDTH (it->f)
18913 : FRAME_LINE_HEIGHT (it->f));
18914 return OK_PIXELS (XFLOATINT (prop) * base_unit);
18915 }
18916
18917 if (CONSP (prop))
18918 {
18919 Lisp_Object car = XCAR (prop);
18920 Lisp_Object cdr = XCDR (prop);
18921
18922 if (SYMBOLP (car))
18923 {
18924 #ifdef HAVE_WINDOW_SYSTEM
18925 if (FRAME_WINDOW_P (it->f)
18926 && valid_image_p (prop))
18927 {
18928 int id = lookup_image (it->f, prop);
18929 struct image *img = IMAGE_FROM_ID (it->f, id);
18930
18931 return OK_PIXELS (width_p ? img->width : img->height);
18932 }
18933 #endif
18934 if (EQ (car, Qplus) || EQ (car, Qminus))
18935 {
18936 int first = 1;
18937 double px;
18938
18939 pixels = 0;
18940 while (CONSP (cdr))
18941 {
18942 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
18943 font, width_p, align_to))
18944 return 0;
18945 if (first)
18946 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
18947 else
18948 pixels += px;
18949 cdr = XCDR (cdr);
18950 }
18951 if (EQ (car, Qminus))
18952 pixels = -pixels;
18953 return OK_PIXELS (pixels);
18954 }
18955
18956 car = Fbuffer_local_value (car, it->w->buffer);
18957 }
18958
18959 if (INTEGERP (car) || FLOATP (car))
18960 {
18961 double fact;
18962 pixels = XFLOATINT (car);
18963 if (NILP (cdr))
18964 return OK_PIXELS (pixels);
18965 if (calc_pixel_width_or_height (&fact, it, cdr,
18966 font, width_p, align_to))
18967 return OK_PIXELS (pixels * fact);
18968 return 0;
18969 }
18970
18971 return 0;
18972 }
18973
18974 return 0;
18975 }
18976
18977 \f
18978 /***********************************************************************
18979 Glyph Display
18980 ***********************************************************************/
18981
18982 #ifdef HAVE_WINDOW_SYSTEM
18983
18984 #if GLYPH_DEBUG
18985
18986 void
18987 dump_glyph_string (s)
18988 struct glyph_string *s;
18989 {
18990 fprintf (stderr, "glyph string\n");
18991 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
18992 s->x, s->y, s->width, s->height);
18993 fprintf (stderr, " ybase = %d\n", s->ybase);
18994 fprintf (stderr, " hl = %d\n", s->hl);
18995 fprintf (stderr, " left overhang = %d, right = %d\n",
18996 s->left_overhang, s->right_overhang);
18997 fprintf (stderr, " nchars = %d\n", s->nchars);
18998 fprintf (stderr, " extends to end of line = %d\n",
18999 s->extends_to_end_of_line_p);
19000 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
19001 fprintf (stderr, " bg width = %d\n", s->background_width);
19002 }
19003
19004 #endif /* GLYPH_DEBUG */
19005
19006 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
19007 of XChar2b structures for S; it can't be allocated in
19008 init_glyph_string because it must be allocated via `alloca'. W
19009 is the window on which S is drawn. ROW and AREA are the glyph row
19010 and area within the row from which S is constructed. START is the
19011 index of the first glyph structure covered by S. HL is a
19012 face-override for drawing S. */
19013
19014 #ifdef HAVE_NTGUI
19015 #define OPTIONAL_HDC(hdc) hdc,
19016 #define DECLARE_HDC(hdc) HDC hdc;
19017 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
19018 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
19019 #endif
19020
19021 #ifndef OPTIONAL_HDC
19022 #define OPTIONAL_HDC(hdc)
19023 #define DECLARE_HDC(hdc)
19024 #define ALLOCATE_HDC(hdc, f)
19025 #define RELEASE_HDC(hdc, f)
19026 #endif
19027
19028 static void
19029 init_glyph_string (s, OPTIONAL_HDC (hdc) char2b, w, row, area, start, hl)
19030 struct glyph_string *s;
19031 DECLARE_HDC (hdc)
19032 XChar2b *char2b;
19033 struct window *w;
19034 struct glyph_row *row;
19035 enum glyph_row_area area;
19036 int start;
19037 enum draw_glyphs_face hl;
19038 {
19039 bzero (s, sizeof *s);
19040 s->w = w;
19041 s->f = XFRAME (w->frame);
19042 #ifdef HAVE_NTGUI
19043 s->hdc = hdc;
19044 #endif
19045 s->display = FRAME_X_DISPLAY (s->f);
19046 s->window = FRAME_X_WINDOW (s->f);
19047 s->char2b = char2b;
19048 s->hl = hl;
19049 s->row = row;
19050 s->area = area;
19051 s->first_glyph = row->glyphs[area] + start;
19052 s->height = row->height;
19053 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
19054
19055 /* Display the internal border below the tool-bar window. */
19056 if (WINDOWP (s->f->tool_bar_window)
19057 && s->w == XWINDOW (s->f->tool_bar_window))
19058 s->y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
19059
19060 s->ybase = s->y + row->ascent;
19061 }
19062
19063
19064 /* Append the list of glyph strings with head H and tail T to the list
19065 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
19066
19067 static INLINE void
19068 append_glyph_string_lists (head, tail, h, t)
19069 struct glyph_string **head, **tail;
19070 struct glyph_string *h, *t;
19071 {
19072 if (h)
19073 {
19074 if (*head)
19075 (*tail)->next = h;
19076 else
19077 *head = h;
19078 h->prev = *tail;
19079 *tail = t;
19080 }
19081 }
19082
19083
19084 /* Prepend the list of glyph strings with head H and tail T to the
19085 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
19086 result. */
19087
19088 static INLINE void
19089 prepend_glyph_string_lists (head, tail, h, t)
19090 struct glyph_string **head, **tail;
19091 struct glyph_string *h, *t;
19092 {
19093 if (h)
19094 {
19095 if (*head)
19096 (*head)->prev = t;
19097 else
19098 *tail = t;
19099 t->next = *head;
19100 *head = h;
19101 }
19102 }
19103
19104
19105 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
19106 Set *HEAD and *TAIL to the resulting list. */
19107
19108 static INLINE void
19109 append_glyph_string (head, tail, s)
19110 struct glyph_string **head, **tail;
19111 struct glyph_string *s;
19112 {
19113 s->next = s->prev = NULL;
19114 append_glyph_string_lists (head, tail, s, s);
19115 }
19116
19117
19118 /* Get face and two-byte form of character C in face FACE_ID on frame
19119 F. The encoding of C is returned in *CHAR2B. MULTIBYTE_P non-zero
19120 means we want to display multibyte text. DISPLAY_P non-zero means
19121 make sure that X resources for the face returned are allocated.
19122 Value is a pointer to a realized face that is ready for display if
19123 DISPLAY_P is non-zero. */
19124
19125 static INLINE struct face *
19126 get_char_face_and_encoding (f, c, face_id, char2b, multibyte_p, display_p)
19127 struct frame *f;
19128 int c, face_id;
19129 XChar2b *char2b;
19130 int multibyte_p, display_p;
19131 {
19132 struct face *face = FACE_FROM_ID (f, face_id);
19133
19134 #ifdef USE_FONT_BACKEND
19135 if (enable_font_backend)
19136 {
19137 struct font *font = (struct font *) face->font_info;
19138
19139 if (font)
19140 {
19141 unsigned code = font->driver->encode_char (font, c);
19142
19143 if (code != FONT_INVALID_CODE)
19144 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
19145 else
19146 STORE_XCHAR2B (char2b, 0, 0);
19147 }
19148 }
19149 else
19150 #endif /* USE_FONT_BACKEND */
19151 if (!multibyte_p)
19152 {
19153 /* Unibyte case. We don't have to encode, but we have to make
19154 sure to use a face suitable for unibyte. */
19155 STORE_XCHAR2B (char2b, 0, c);
19156 face_id = FACE_FOR_CHAR (f, face, c, -1, Qnil);
19157 face = FACE_FROM_ID (f, face_id);
19158 }
19159 else if (c < 128)
19160 {
19161 /* Case of ASCII in a face known to fit ASCII. */
19162 STORE_XCHAR2B (char2b, 0, c);
19163 }
19164 else if (face->font != NULL)
19165 {
19166 struct font_info *font_info
19167 = FONT_INFO_FROM_ID (f, face->font_info_id);
19168 struct charset *charset = CHARSET_FROM_ID (font_info->charset);
19169 unsigned code = ENCODE_CHAR (charset, c);
19170
19171 if (CHARSET_DIMENSION (charset) == 1)
19172 STORE_XCHAR2B (char2b, 0, code);
19173 else
19174 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
19175 /* Maybe encode the character in *CHAR2B. */
19176 FRAME_RIF (f)->encode_char (c, char2b, font_info, charset, NULL);
19177 }
19178
19179 /* Make sure X resources of the face are allocated. */
19180 #ifdef HAVE_X_WINDOWS
19181 if (display_p)
19182 #endif
19183 {
19184 xassert (face != NULL);
19185 PREPARE_FACE_FOR_DISPLAY (f, face);
19186 }
19187
19188 return face;
19189 }
19190
19191
19192 /* Get face and two-byte form of character glyph GLYPH on frame F.
19193 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
19194 a pointer to a realized face that is ready for display. */
19195
19196 static INLINE struct face *
19197 get_glyph_face_and_encoding (f, glyph, char2b, two_byte_p)
19198 struct frame *f;
19199 struct glyph *glyph;
19200 XChar2b *char2b;
19201 int *two_byte_p;
19202 {
19203 struct face *face;
19204
19205 xassert (glyph->type == CHAR_GLYPH);
19206 face = FACE_FROM_ID (f, glyph->face_id);
19207
19208 if (two_byte_p)
19209 *two_byte_p = 0;
19210
19211 #ifdef USE_FONT_BACKEND
19212 if (enable_font_backend)
19213 {
19214 struct font *font = (struct font *) face->font_info;
19215
19216 if (font)
19217 {
19218 unsigned code = font->driver->encode_char (font, glyph->u.ch);
19219
19220 if (code != FONT_INVALID_CODE)
19221 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
19222 else
19223 STORE_XCHAR2B (char2b, 0, code);
19224 }
19225 }
19226 else
19227 #endif /* USE_FONT_BACKEND */
19228 if (!glyph->multibyte_p)
19229 {
19230 /* Unibyte case. We don't have to encode, but we have to make
19231 sure to use a face suitable for unibyte. */
19232 STORE_XCHAR2B (char2b, 0, glyph->u.ch);
19233 }
19234 else if (glyph->u.ch < 128)
19235 {
19236 /* Case of ASCII in a face known to fit ASCII. */
19237 STORE_XCHAR2B (char2b, 0, glyph->u.ch);
19238 }
19239 else
19240 {
19241 struct font_info *font_info
19242 = FONT_INFO_FROM_ID (f, face->font_info_id);
19243 if (font_info)
19244 {
19245 struct charset *charset = CHARSET_FROM_ID (font_info->charset);
19246 unsigned code = ENCODE_CHAR (charset, glyph->u.ch);
19247
19248 if (CHARSET_DIMENSION (charset) == 1)
19249 STORE_XCHAR2B (char2b, 0, code);
19250 else
19251 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
19252
19253 /* Maybe encode the character in *CHAR2B. */
19254 if (CHARSET_ID (charset) != charset_ascii)
19255 {
19256 glyph->font_type
19257 = FRAME_RIF (f)->encode_char (glyph->u.ch, char2b, font_info,
19258 charset, two_byte_p);
19259 }
19260 }
19261 }
19262
19263 /* Make sure X resources of the face are allocated. */
19264 xassert (face != NULL);
19265 PREPARE_FACE_FOR_DISPLAY (f, face);
19266 return face;
19267 }
19268
19269
19270 /* Fill glyph string S with composition components specified by S->cmp.
19271
19272 BASE_FACE is the base face of the composition.
19273 S->gidx is the index of the first component for S.
19274
19275 OVERLAPS non-zero means S should draw the foreground only, and use
19276 its physical height for clipping. See also draw_glyphs.
19277
19278 Value is the index of a component not in S. */
19279
19280 static int
19281 fill_composite_glyph_string (s, base_face, overlaps)
19282 struct glyph_string *s;
19283 struct face *base_face;
19284 int overlaps;
19285 {
19286 int i;
19287
19288 xassert (s);
19289
19290 s->for_overlaps = overlaps;
19291
19292 #ifdef USE_FONT_BACKEND
19293 if (enable_font_backend && s->cmp->method == COMPOSITION_WITH_GLYPH_STRING)
19294 {
19295 Lisp_Object gstring
19296 = AREF (XHASH_TABLE (composition_hash_table)->key_and_value,
19297 s->cmp->hash_index * 2);
19298
19299 s->face = base_face;
19300 s->font_info = s->cmp->font;
19301 s->font = s->font_info->font;
19302 for (i = 0, s->nchars = 0; i < s->cmp->glyph_len; i++, s->nchars++)
19303 {
19304 Lisp_Object g = LGSTRING_GLYPH (gstring, i);
19305 unsigned code;
19306 XChar2b * store_pos;
19307 if (NILP (g))
19308 break;
19309 code = LGLYPH_CODE (g);
19310 store_pos = s->char2b + i;
19311 STORE_XCHAR2B (store_pos, code >> 8, code & 0xFF);
19312 }
19313 s->width = s->cmp->pixel_width;
19314 }
19315 else
19316 #endif /* USE_FONT_BACKEND */
19317 {
19318 /* For all glyphs of this composition, starting at the offset
19319 S->gidx, until we reach the end of the definition or encounter a
19320 glyph that requires the different face, add it to S. */
19321 struct face *face;
19322
19323 s->face = NULL;
19324 s->font = NULL;
19325 s->font_info = NULL;
19326 for (i = s->gidx; i < s->cmp->glyph_len; i++)
19327 {
19328 int c = COMPOSITION_GLYPH (s->cmp, i);
19329
19330 if (c != '\t')
19331 {
19332 int face_id = FACE_FOR_CHAR (s->f, base_face, c, -1, Qnil);
19333
19334 face = get_char_face_and_encoding (s->f, c, face_id,
19335 s->char2b + i, 1, 1);
19336 if (face)
19337 {
19338 if (! s->face)
19339 {
19340 s->face = face;
19341 s->font = s->face->font;
19342 s->font_info = FONT_INFO_FROM_FACE (s->f, s->face);
19343 }
19344 else if (s->face != face)
19345 break;
19346 }
19347 }
19348 ++s->nchars;
19349 }
19350
19351 /* All glyph strings for the same composition has the same width,
19352 i.e. the width set for the first component of the composition. */
19353 s->width = s->first_glyph->pixel_width;
19354 }
19355
19356 /* If the specified font could not be loaded, use the frame's
19357 default font, but record the fact that we couldn't load it in
19358 the glyph string so that we can draw rectangles for the
19359 characters of the glyph string. */
19360 if (s->font == NULL)
19361 {
19362 s->font_not_found_p = 1;
19363 s->font = FRAME_FONT (s->f);
19364 }
19365
19366 /* Adjust base line for subscript/superscript text. */
19367 s->ybase += s->first_glyph->voffset;
19368
19369 /* This glyph string must always be drawn with 16-bit functions. */
19370 s->two_byte_p = 1;
19371
19372 return s->gidx + s->nchars;
19373 }
19374
19375
19376 /* Fill glyph string S from a sequence of character glyphs.
19377
19378 FACE_ID is the face id of the string. START is the index of the
19379 first glyph to consider, END is the index of the last + 1.
19380 OVERLAPS non-zero means S should draw the foreground only, and use
19381 its physical height for clipping. See also draw_glyphs.
19382
19383 Value is the index of the first glyph not in S. */
19384
19385 static int
19386 fill_glyph_string (s, face_id, start, end, overlaps)
19387 struct glyph_string *s;
19388 int face_id;
19389 int start, end, overlaps;
19390 {
19391 struct glyph *glyph, *last;
19392 int voffset;
19393 int glyph_not_available_p;
19394
19395 xassert (s->f == XFRAME (s->w->frame));
19396 xassert (s->nchars == 0);
19397 xassert (start >= 0 && end > start);
19398
19399 s->for_overlaps = overlaps,
19400 glyph = s->row->glyphs[s->area] + start;
19401 last = s->row->glyphs[s->area] + end;
19402 voffset = glyph->voffset;
19403
19404 glyph_not_available_p = glyph->glyph_not_available_p;
19405
19406 while (glyph < last
19407 && glyph->type == CHAR_GLYPH
19408 && glyph->voffset == voffset
19409 /* Same face id implies same font, nowadays. */
19410 && glyph->face_id == face_id
19411 && glyph->glyph_not_available_p == glyph_not_available_p)
19412 {
19413 int two_byte_p;
19414
19415 s->face = get_glyph_face_and_encoding (s->f, glyph,
19416 s->char2b + s->nchars,
19417 &two_byte_p);
19418 s->two_byte_p = two_byte_p;
19419 ++s->nchars;
19420 xassert (s->nchars <= end - start);
19421 s->width += glyph->pixel_width;
19422 ++glyph;
19423 }
19424
19425 s->font = s->face->font;
19426 s->font_info = FONT_INFO_FROM_FACE (s->f, s->face);
19427
19428 /* If the specified font could not be loaded, use the frame's font,
19429 but record the fact that we couldn't load it in
19430 S->font_not_found_p so that we can draw rectangles for the
19431 characters of the glyph string. */
19432 if (s->font == NULL || glyph_not_available_p)
19433 {
19434 s->font_not_found_p = 1;
19435 s->font = FRAME_FONT (s->f);
19436 }
19437
19438 /* Adjust base line for subscript/superscript text. */
19439 s->ybase += voffset;
19440
19441 xassert (s->face && s->face->gc);
19442 return glyph - s->row->glyphs[s->area];
19443 }
19444
19445
19446 /* Fill glyph string S from image glyph S->first_glyph. */
19447
19448 static void
19449 fill_image_glyph_string (s)
19450 struct glyph_string *s;
19451 {
19452 xassert (s->first_glyph->type == IMAGE_GLYPH);
19453 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
19454 xassert (s->img);
19455 s->slice = s->first_glyph->slice;
19456 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
19457 s->font = s->face->font;
19458 s->width = s->first_glyph->pixel_width;
19459
19460 /* Adjust base line for subscript/superscript text. */
19461 s->ybase += s->first_glyph->voffset;
19462 }
19463
19464
19465 /* Fill glyph string S from a sequence of stretch glyphs.
19466
19467 ROW is the glyph row in which the glyphs are found, AREA is the
19468 area within the row. START is the index of the first glyph to
19469 consider, END is the index of the last + 1.
19470
19471 Value is the index of the first glyph not in S. */
19472
19473 static int
19474 fill_stretch_glyph_string (s, row, area, start, end)
19475 struct glyph_string *s;
19476 struct glyph_row *row;
19477 enum glyph_row_area area;
19478 int start, end;
19479 {
19480 struct glyph *glyph, *last;
19481 int voffset, face_id;
19482
19483 xassert (s->first_glyph->type == STRETCH_GLYPH);
19484
19485 glyph = s->row->glyphs[s->area] + start;
19486 last = s->row->glyphs[s->area] + end;
19487 face_id = glyph->face_id;
19488 s->face = FACE_FROM_ID (s->f, face_id);
19489 s->font = s->face->font;
19490 s->font_info = FONT_INFO_FROM_FACE (s->f, s->face);
19491 s->width = glyph->pixel_width;
19492 s->nchars = 1;
19493 voffset = glyph->voffset;
19494
19495 for (++glyph;
19496 (glyph < last
19497 && glyph->type == STRETCH_GLYPH
19498 && glyph->voffset == voffset
19499 && glyph->face_id == face_id);
19500 ++glyph)
19501 s->width += glyph->pixel_width;
19502
19503 /* Adjust base line for subscript/superscript text. */
19504 s->ybase += voffset;
19505
19506 /* The case that face->gc == 0 is handled when drawing the glyph
19507 string by calling PREPARE_FACE_FOR_DISPLAY. */
19508 xassert (s->face);
19509 return glyph - s->row->glyphs[s->area];
19510 }
19511
19512 static XCharStruct *
19513 get_per_char_metric (f, font, font_info, char2b, font_type)
19514 struct frame *f;
19515 XFontStruct *font;
19516 struct font_info *font_info;
19517 XChar2b *char2b;
19518 int font_type;
19519 {
19520 #ifdef USE_FONT_BACKEND
19521 if (enable_font_backend)
19522 {
19523 static XCharStruct pcm_value;
19524 unsigned code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
19525 struct font *fontp;
19526 struct font_metrics metrics;
19527
19528 if (! font_info || code == FONT_INVALID_CODE)
19529 return NULL;
19530 fontp = (struct font *) font_info;
19531 fontp->driver->text_extents (fontp, &code, 1, &metrics);
19532 pcm_value.lbearing = metrics.lbearing;
19533 pcm_value.rbearing = metrics.rbearing;
19534 pcm_value.ascent = metrics.ascent;
19535 pcm_value.descent = metrics.descent;
19536 pcm_value.width = metrics.width;
19537 return &pcm_value;
19538 }
19539 #endif /* USE_FONT_BACKEND */
19540 return FRAME_RIF (f)->per_char_metric (font, char2b, font_type);
19541 }
19542
19543 /* EXPORT for RIF:
19544 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
19545 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
19546 assumed to be zero. */
19547
19548 void
19549 x_get_glyph_overhangs (glyph, f, left, right)
19550 struct glyph *glyph;
19551 struct frame *f;
19552 int *left, *right;
19553 {
19554 *left = *right = 0;
19555
19556 if (glyph->type == CHAR_GLYPH)
19557 {
19558 XFontStruct *font;
19559 struct face *face;
19560 struct font_info *font_info;
19561 XChar2b char2b;
19562 XCharStruct *pcm;
19563
19564 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
19565 font = face->font;
19566 font_info = FONT_INFO_FROM_FACE (f, face);
19567 if (font /* ++KFS: Should this be font_info ? */
19568 && (pcm = get_per_char_metric (f, font, font_info, &char2b, glyph->font_type)))
19569 {
19570 if (pcm->rbearing > pcm->width)
19571 *right = pcm->rbearing - pcm->width;
19572 if (pcm->lbearing < 0)
19573 *left = -pcm->lbearing;
19574 }
19575 }
19576 else if (glyph->type == COMPOSITE_GLYPH)
19577 {
19578 struct composition *cmp = composition_table[glyph->u.cmp_id];
19579
19580 *right = cmp->rbearing - cmp->pixel_width;
19581 *left = - cmp->lbearing;
19582 }
19583 }
19584
19585
19586 /* Return the index of the first glyph preceding glyph string S that
19587 is overwritten by S because of S's left overhang. Value is -1
19588 if no glyphs are overwritten. */
19589
19590 static int
19591 left_overwritten (s)
19592 struct glyph_string *s;
19593 {
19594 int k;
19595
19596 if (s->left_overhang)
19597 {
19598 int x = 0, i;
19599 struct glyph *glyphs = s->row->glyphs[s->area];
19600 int first = s->first_glyph - glyphs;
19601
19602 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
19603 x -= glyphs[i].pixel_width;
19604
19605 k = i + 1;
19606 }
19607 else
19608 k = -1;
19609
19610 return k;
19611 }
19612
19613
19614 /* Return the index of the first glyph preceding glyph string S that
19615 is overwriting S because of its right overhang. Value is -1 if no
19616 glyph in front of S overwrites S. */
19617
19618 static int
19619 left_overwriting (s)
19620 struct glyph_string *s;
19621 {
19622 int i, k, x;
19623 struct glyph *glyphs = s->row->glyphs[s->area];
19624 int first = s->first_glyph - glyphs;
19625
19626 k = -1;
19627 x = 0;
19628 for (i = first - 1; i >= 0; --i)
19629 {
19630 int left, right;
19631 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
19632 if (x + right > 0)
19633 k = i;
19634 x -= glyphs[i].pixel_width;
19635 }
19636
19637 return k;
19638 }
19639
19640
19641 /* Return the index of the last glyph following glyph string S that is
19642 not overwritten by S because of S's right overhang. Value is -1 if
19643 no such glyph is found. */
19644
19645 static int
19646 right_overwritten (s)
19647 struct glyph_string *s;
19648 {
19649 int k = -1;
19650
19651 if (s->right_overhang)
19652 {
19653 int x = 0, i;
19654 struct glyph *glyphs = s->row->glyphs[s->area];
19655 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
19656 int end = s->row->used[s->area];
19657
19658 for (i = first; i < end && s->right_overhang > x; ++i)
19659 x += glyphs[i].pixel_width;
19660
19661 k = i;
19662 }
19663
19664 return k;
19665 }
19666
19667
19668 /* Return the index of the last glyph following glyph string S that
19669 overwrites S because of its left overhang. Value is negative
19670 if no such glyph is found. */
19671
19672 static int
19673 right_overwriting (s)
19674 struct glyph_string *s;
19675 {
19676 int i, k, x;
19677 int end = s->row->used[s->area];
19678 struct glyph *glyphs = s->row->glyphs[s->area];
19679 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
19680
19681 k = -1;
19682 x = 0;
19683 for (i = first; i < end; ++i)
19684 {
19685 int left, right;
19686 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
19687 if (x - left < 0)
19688 k = i;
19689 x += glyphs[i].pixel_width;
19690 }
19691
19692 return k;
19693 }
19694
19695
19696 /* Set background width of glyph string S. START is the index of the
19697 first glyph following S. LAST_X is the right-most x-position + 1
19698 in the drawing area. */
19699
19700 static INLINE void
19701 set_glyph_string_background_width (s, start, last_x)
19702 struct glyph_string *s;
19703 int start;
19704 int last_x;
19705 {
19706 /* If the face of this glyph string has to be drawn to the end of
19707 the drawing area, set S->extends_to_end_of_line_p. */
19708
19709 if (start == s->row->used[s->area]
19710 && s->area == TEXT_AREA
19711 && ((s->row->fill_line_p
19712 && (s->hl == DRAW_NORMAL_TEXT
19713 || s->hl == DRAW_IMAGE_RAISED
19714 || s->hl == DRAW_IMAGE_SUNKEN))
19715 || s->hl == DRAW_MOUSE_FACE))
19716 s->extends_to_end_of_line_p = 1;
19717
19718 /* If S extends its face to the end of the line, set its
19719 background_width to the distance to the right edge of the drawing
19720 area. */
19721 if (s->extends_to_end_of_line_p)
19722 s->background_width = last_x - s->x + 1;
19723 else
19724 s->background_width = s->width;
19725 }
19726
19727
19728 /* Compute overhangs and x-positions for glyph string S and its
19729 predecessors, or successors. X is the starting x-position for S.
19730 BACKWARD_P non-zero means process predecessors. */
19731
19732 static void
19733 compute_overhangs_and_x (s, x, backward_p)
19734 struct glyph_string *s;
19735 int x;
19736 int backward_p;
19737 {
19738 if (backward_p)
19739 {
19740 while (s)
19741 {
19742 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
19743 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
19744 x -= s->width;
19745 s->x = x;
19746 s = s->prev;
19747 }
19748 }
19749 else
19750 {
19751 while (s)
19752 {
19753 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
19754 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
19755 s->x = x;
19756 x += s->width;
19757 s = s->next;
19758 }
19759 }
19760 }
19761
19762
19763
19764 /* The following macros are only called from draw_glyphs below.
19765 They reference the following parameters of that function directly:
19766 `w', `row', `area', and `overlap_p'
19767 as well as the following local variables:
19768 `s', `f', and `hdc' (in W32) */
19769
19770 #ifdef HAVE_NTGUI
19771 /* On W32, silently add local `hdc' variable to argument list of
19772 init_glyph_string. */
19773 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
19774 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
19775 #else
19776 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
19777 init_glyph_string (s, char2b, w, row, area, start, hl)
19778 #endif
19779
19780 /* Add a glyph string for a stretch glyph to the list of strings
19781 between HEAD and TAIL. START is the index of the stretch glyph in
19782 row area AREA of glyph row ROW. END is the index of the last glyph
19783 in that glyph row area. X is the current output position assigned
19784 to the new glyph string constructed. HL overrides that face of the
19785 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
19786 is the right-most x-position of the drawing area. */
19787
19788 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
19789 and below -- keep them on one line. */
19790 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
19791 do \
19792 { \
19793 s = (struct glyph_string *) alloca (sizeof *s); \
19794 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
19795 START = fill_stretch_glyph_string (s, row, area, START, END); \
19796 append_glyph_string (&HEAD, &TAIL, s); \
19797 s->x = (X); \
19798 } \
19799 while (0)
19800
19801
19802 /* Add a glyph string for an image glyph to the list of strings
19803 between HEAD and TAIL. START is the index of the image glyph in
19804 row area AREA of glyph row ROW. END is the index of the last glyph
19805 in that glyph row area. X is the current output position assigned
19806 to the new glyph string constructed. HL overrides that face of the
19807 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
19808 is the right-most x-position of the drawing area. */
19809
19810 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
19811 do \
19812 { \
19813 s = (struct glyph_string *) alloca (sizeof *s); \
19814 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
19815 fill_image_glyph_string (s); \
19816 append_glyph_string (&HEAD, &TAIL, s); \
19817 ++START; \
19818 s->x = (X); \
19819 } \
19820 while (0)
19821
19822
19823 /* Add a glyph string for a sequence of character glyphs to the list
19824 of strings between HEAD and TAIL. START is the index of the first
19825 glyph in row area AREA of glyph row ROW that is part of the new
19826 glyph string. END is the index of the last glyph in that glyph row
19827 area. X is the current output position assigned to the new glyph
19828 string constructed. HL overrides that face of the glyph; e.g. it
19829 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
19830 right-most x-position of the drawing area. */
19831
19832 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
19833 do \
19834 { \
19835 int face_id; \
19836 XChar2b *char2b; \
19837 \
19838 face_id = (row)->glyphs[area][START].face_id; \
19839 \
19840 s = (struct glyph_string *) alloca (sizeof *s); \
19841 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
19842 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
19843 append_glyph_string (&HEAD, &TAIL, s); \
19844 s->x = (X); \
19845 START = fill_glyph_string (s, face_id, START, END, overlaps); \
19846 } \
19847 while (0)
19848
19849
19850 /* Add a glyph string for a composite sequence to the list of strings
19851 between HEAD and TAIL. START is the index of the first glyph in
19852 row area AREA of glyph row ROW that is part of the new glyph
19853 string. END is the index of the last glyph in that glyph row area.
19854 X is the current output position assigned to the new glyph string
19855 constructed. HL overrides that face of the glyph; e.g. it is
19856 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
19857 x-position of the drawing area. */
19858
19859 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
19860 do { \
19861 int face_id = (row)->glyphs[area][START].face_id; \
19862 struct face *base_face = FACE_FROM_ID (f, face_id); \
19863 int cmp_id = (row)->glyphs[area][START].u.cmp_id; \
19864 struct composition *cmp = composition_table[cmp_id]; \
19865 XChar2b *char2b; \
19866 struct glyph_string *first_s; \
19867 int n; \
19868 \
19869 char2b = (XChar2b *) alloca ((sizeof *char2b) * cmp->glyph_len); \
19870 base_face = base_face->ascii_face; \
19871 \
19872 /* Make glyph_strings for each glyph sequence that is drawable by \
19873 the same face, and append them to HEAD/TAIL. */ \
19874 for (n = 0; n < cmp->glyph_len;) \
19875 { \
19876 s = (struct glyph_string *) alloca (sizeof *s); \
19877 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
19878 append_glyph_string (&(HEAD), &(TAIL), s); \
19879 s->cmp = cmp; \
19880 s->gidx = n; \
19881 s->x = (X); \
19882 if (n == 0) \
19883 first_s = s; \
19884 n = fill_composite_glyph_string (s, base_face, overlaps); \
19885 } \
19886 \
19887 ++START; \
19888 s = first_s; \
19889 } while (0)
19890
19891
19892 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
19893 of AREA of glyph row ROW on window W between indices START and END.
19894 HL overrides the face for drawing glyph strings, e.g. it is
19895 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
19896 x-positions of the drawing area.
19897
19898 This is an ugly monster macro construct because we must use alloca
19899 to allocate glyph strings (because draw_glyphs can be called
19900 asynchronously). */
19901
19902 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
19903 do \
19904 { \
19905 HEAD = TAIL = NULL; \
19906 while (START < END) \
19907 { \
19908 struct glyph *first_glyph = (row)->glyphs[area] + START; \
19909 switch (first_glyph->type) \
19910 { \
19911 case CHAR_GLYPH: \
19912 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
19913 HL, X, LAST_X); \
19914 break; \
19915 \
19916 case COMPOSITE_GLYPH: \
19917 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
19918 HL, X, LAST_X); \
19919 break; \
19920 \
19921 case STRETCH_GLYPH: \
19922 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
19923 HL, X, LAST_X); \
19924 break; \
19925 \
19926 case IMAGE_GLYPH: \
19927 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
19928 HL, X, LAST_X); \
19929 break; \
19930 \
19931 default: \
19932 abort (); \
19933 } \
19934 \
19935 if (s) \
19936 { \
19937 set_glyph_string_background_width (s, START, LAST_X); \
19938 (X) += s->width; \
19939 } \
19940 } \
19941 } \
19942 while (0)
19943
19944
19945 /* Draw glyphs between START and END in AREA of ROW on window W,
19946 starting at x-position X. X is relative to AREA in W. HL is a
19947 face-override with the following meaning:
19948
19949 DRAW_NORMAL_TEXT draw normally
19950 DRAW_CURSOR draw in cursor face
19951 DRAW_MOUSE_FACE draw in mouse face.
19952 DRAW_INVERSE_VIDEO draw in mode line face
19953 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
19954 DRAW_IMAGE_RAISED draw an image with a raised relief around it
19955
19956 If OVERLAPS is non-zero, draw only the foreground of characters and
19957 clip to the physical height of ROW. Non-zero value also defines
19958 the overlapping part to be drawn:
19959
19960 OVERLAPS_PRED overlap with preceding rows
19961 OVERLAPS_SUCC overlap with succeeding rows
19962 OVERLAPS_BOTH overlap with both preceding/succeeding rows
19963 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
19964
19965 Value is the x-position reached, relative to AREA of W. */
19966
19967 static int
19968 draw_glyphs (w, x, row, area, start, end, hl, overlaps)
19969 struct window *w;
19970 int x;
19971 struct glyph_row *row;
19972 enum glyph_row_area area;
19973 EMACS_INT start, end;
19974 enum draw_glyphs_face hl;
19975 int overlaps;
19976 {
19977 struct glyph_string *head, *tail;
19978 struct glyph_string *s;
19979 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
19980 int last_x, area_width;
19981 int x_reached;
19982 int i, j;
19983 struct frame *f = XFRAME (WINDOW_FRAME (w));
19984 DECLARE_HDC (hdc);
19985
19986 ALLOCATE_HDC (hdc, f);
19987
19988 /* Let's rather be paranoid than getting a SEGV. */
19989 end = min (end, row->used[area]);
19990 start = max (0, start);
19991 start = min (end, start);
19992
19993 /* Translate X to frame coordinates. Set last_x to the right
19994 end of the drawing area. */
19995 if (row->full_width_p)
19996 {
19997 /* X is relative to the left edge of W, without scroll bars
19998 or fringes. */
19999 x += WINDOW_LEFT_EDGE_X (w);
20000 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
20001 }
20002 else
20003 {
20004 int area_left = window_box_left (w, area);
20005 x += area_left;
20006 area_width = window_box_width (w, area);
20007 last_x = area_left + area_width;
20008 }
20009
20010 /* Build a doubly-linked list of glyph_string structures between
20011 head and tail from what we have to draw. Note that the macro
20012 BUILD_GLYPH_STRINGS will modify its start parameter. That's
20013 the reason we use a separate variable `i'. */
20014 i = start;
20015 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
20016 if (tail)
20017 x_reached = tail->x + tail->background_width;
20018 else
20019 x_reached = x;
20020
20021 /* If there are any glyphs with lbearing < 0 or rbearing > width in
20022 the row, redraw some glyphs in front or following the glyph
20023 strings built above. */
20024 if (head && !overlaps && row->contains_overlapping_glyphs_p)
20025 {
20026 int dummy_x = 0;
20027 struct glyph_string *h, *t;
20028
20029 /* Compute overhangs for all glyph strings. */
20030 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
20031 for (s = head; s; s = s->next)
20032 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
20033
20034 /* Prepend glyph strings for glyphs in front of the first glyph
20035 string that are overwritten because of the first glyph
20036 string's left overhang. The background of all strings
20037 prepended must be drawn because the first glyph string
20038 draws over it. */
20039 i = left_overwritten (head);
20040 if (i >= 0)
20041 {
20042 j = i;
20043 BUILD_GLYPH_STRINGS (j, start, h, t,
20044 DRAW_NORMAL_TEXT, dummy_x, last_x);
20045 start = i;
20046 compute_overhangs_and_x (t, head->x, 1);
20047 prepend_glyph_string_lists (&head, &tail, h, t);
20048 clip_head = head;
20049 }
20050
20051 /* Prepend glyph strings for glyphs in front of the first glyph
20052 string that overwrite that glyph string because of their
20053 right overhang. For these strings, only the foreground must
20054 be drawn, because it draws over the glyph string at `head'.
20055 The background must not be drawn because this would overwrite
20056 right overhangs of preceding glyphs for which no glyph
20057 strings exist. */
20058 i = left_overwriting (head);
20059 if (i >= 0)
20060 {
20061 clip_head = head;
20062 BUILD_GLYPH_STRINGS (i, start, h, t,
20063 DRAW_NORMAL_TEXT, dummy_x, last_x);
20064 for (s = h; s; s = s->next)
20065 s->background_filled_p = 1;
20066 compute_overhangs_and_x (t, head->x, 1);
20067 prepend_glyph_string_lists (&head, &tail, h, t);
20068 }
20069
20070 /* Append glyphs strings for glyphs following the last glyph
20071 string tail that are overwritten by tail. The background of
20072 these strings has to be drawn because tail's foreground draws
20073 over it. */
20074 i = right_overwritten (tail);
20075 if (i >= 0)
20076 {
20077 BUILD_GLYPH_STRINGS (end, i, h, t,
20078 DRAW_NORMAL_TEXT, x, last_x);
20079 compute_overhangs_and_x (h, tail->x + tail->width, 0);
20080 append_glyph_string_lists (&head, &tail, h, t);
20081 clip_tail = tail;
20082 }
20083
20084 /* Append glyph strings for glyphs following the last glyph
20085 string tail that overwrite tail. The foreground of such
20086 glyphs has to be drawn because it writes into the background
20087 of tail. The background must not be drawn because it could
20088 paint over the foreground of following glyphs. */
20089 i = right_overwriting (tail);
20090 if (i >= 0)
20091 {
20092 clip_tail = tail;
20093 i++; /* We must include the Ith glyph. */
20094 BUILD_GLYPH_STRINGS (end, i, h, t,
20095 DRAW_NORMAL_TEXT, x, last_x);
20096 for (s = h; s; s = s->next)
20097 s->background_filled_p = 1;
20098 compute_overhangs_and_x (h, tail->x + tail->width, 0);
20099 append_glyph_string_lists (&head, &tail, h, t);
20100 }
20101 if (clip_head || clip_tail)
20102 for (s = head; s; s = s->next)
20103 {
20104 s->clip_head = clip_head;
20105 s->clip_tail = clip_tail;
20106 }
20107 }
20108
20109 /* Draw all strings. */
20110 for (s = head; s; s = s->next)
20111 FRAME_RIF (f)->draw_glyph_string (s);
20112
20113 if (area == TEXT_AREA
20114 && !row->full_width_p
20115 /* When drawing overlapping rows, only the glyph strings'
20116 foreground is drawn, which doesn't erase a cursor
20117 completely. */
20118 && !overlaps)
20119 {
20120 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
20121 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
20122 : (tail ? tail->x + tail->background_width : x));
20123
20124 int text_left = window_box_left (w, TEXT_AREA);
20125 x0 -= text_left;
20126 x1 -= text_left;
20127
20128 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
20129 row->y, MATRIX_ROW_BOTTOM_Y (row));
20130 }
20131
20132 /* Value is the x-position up to which drawn, relative to AREA of W.
20133 This doesn't include parts drawn because of overhangs. */
20134 if (row->full_width_p)
20135 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
20136 else
20137 x_reached -= window_box_left (w, area);
20138
20139 RELEASE_HDC (hdc, f);
20140
20141 return x_reached;
20142 }
20143
20144 /* Expand row matrix if too narrow. Don't expand if area
20145 is not present. */
20146
20147 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
20148 { \
20149 if (!fonts_changed_p \
20150 && (it->glyph_row->glyphs[area] \
20151 < it->glyph_row->glyphs[area + 1])) \
20152 { \
20153 it->w->ncols_scale_factor++; \
20154 fonts_changed_p = 1; \
20155 } \
20156 }
20157
20158 /* Store one glyph for IT->char_to_display in IT->glyph_row.
20159 Called from x_produce_glyphs when IT->glyph_row is non-null. */
20160
20161 static INLINE void
20162 append_glyph (it)
20163 struct it *it;
20164 {
20165 struct glyph *glyph;
20166 enum glyph_row_area area = it->area;
20167
20168 xassert (it->glyph_row);
20169 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
20170
20171 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
20172 if (glyph < it->glyph_row->glyphs[area + 1])
20173 {
20174 glyph->charpos = CHARPOS (it->position);
20175 glyph->object = it->object;
20176 glyph->pixel_width = it->pixel_width;
20177 glyph->ascent = it->ascent;
20178 glyph->descent = it->descent;
20179 glyph->voffset = it->voffset;
20180 glyph->type = CHAR_GLYPH;
20181 glyph->multibyte_p = it->multibyte_p;
20182 glyph->left_box_line_p = it->start_of_box_run_p;
20183 glyph->right_box_line_p = it->end_of_box_run_p;
20184 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
20185 || it->phys_descent > it->descent);
20186 glyph->padding_p = 0;
20187 glyph->glyph_not_available_p = it->glyph_not_available_p;
20188 glyph->face_id = it->face_id;
20189 glyph->u.ch = it->char_to_display;
20190 glyph->slice = null_glyph_slice;
20191 glyph->font_type = FONT_TYPE_UNKNOWN;
20192 ++it->glyph_row->used[area];
20193 }
20194 else
20195 IT_EXPAND_MATRIX_WIDTH (it, area);
20196 }
20197
20198 /* Store one glyph for the composition IT->cmp_id in IT->glyph_row.
20199 Called from x_produce_glyphs when IT->glyph_row is non-null. */
20200
20201 static INLINE void
20202 append_composite_glyph (it)
20203 struct it *it;
20204 {
20205 struct glyph *glyph;
20206 enum glyph_row_area area = it->area;
20207
20208 xassert (it->glyph_row);
20209
20210 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
20211 if (glyph < it->glyph_row->glyphs[area + 1])
20212 {
20213 glyph->charpos = CHARPOS (it->position);
20214 glyph->object = it->object;
20215 glyph->pixel_width = it->pixel_width;
20216 glyph->ascent = it->ascent;
20217 glyph->descent = it->descent;
20218 glyph->voffset = it->voffset;
20219 glyph->type = COMPOSITE_GLYPH;
20220 glyph->multibyte_p = it->multibyte_p;
20221 glyph->left_box_line_p = it->start_of_box_run_p;
20222 glyph->right_box_line_p = it->end_of_box_run_p;
20223 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
20224 || it->phys_descent > it->descent);
20225 glyph->padding_p = 0;
20226 glyph->glyph_not_available_p = 0;
20227 glyph->face_id = it->face_id;
20228 glyph->u.cmp_id = it->cmp_id;
20229 glyph->slice = null_glyph_slice;
20230 glyph->font_type = FONT_TYPE_UNKNOWN;
20231 ++it->glyph_row->used[area];
20232 }
20233 else
20234 IT_EXPAND_MATRIX_WIDTH (it, area);
20235 }
20236
20237
20238 /* Change IT->ascent and IT->height according to the setting of
20239 IT->voffset. */
20240
20241 static INLINE void
20242 take_vertical_position_into_account (it)
20243 struct it *it;
20244 {
20245 if (it->voffset)
20246 {
20247 if (it->voffset < 0)
20248 /* Increase the ascent so that we can display the text higher
20249 in the line. */
20250 it->ascent -= it->voffset;
20251 else
20252 /* Increase the descent so that we can display the text lower
20253 in the line. */
20254 it->descent += it->voffset;
20255 }
20256 }
20257
20258
20259 /* Produce glyphs/get display metrics for the image IT is loaded with.
20260 See the description of struct display_iterator in dispextern.h for
20261 an overview of struct display_iterator. */
20262
20263 static void
20264 produce_image_glyph (it)
20265 struct it *it;
20266 {
20267 struct image *img;
20268 struct face *face;
20269 int glyph_ascent, crop;
20270 struct glyph_slice slice;
20271
20272 xassert (it->what == IT_IMAGE);
20273
20274 face = FACE_FROM_ID (it->f, it->face_id);
20275 xassert (face);
20276 /* Make sure X resources of the face is loaded. */
20277 PREPARE_FACE_FOR_DISPLAY (it->f, face);
20278
20279 if (it->image_id < 0)
20280 {
20281 /* Fringe bitmap. */
20282 it->ascent = it->phys_ascent = 0;
20283 it->descent = it->phys_descent = 0;
20284 it->pixel_width = 0;
20285 it->nglyphs = 0;
20286 return;
20287 }
20288
20289 img = IMAGE_FROM_ID (it->f, it->image_id);
20290 xassert (img);
20291 /* Make sure X resources of the image is loaded. */
20292 prepare_image_for_display (it->f, img);
20293
20294 slice.x = slice.y = 0;
20295 slice.width = img->width;
20296 slice.height = img->height;
20297
20298 if (INTEGERP (it->slice.x))
20299 slice.x = XINT (it->slice.x);
20300 else if (FLOATP (it->slice.x))
20301 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
20302
20303 if (INTEGERP (it->slice.y))
20304 slice.y = XINT (it->slice.y);
20305 else if (FLOATP (it->slice.y))
20306 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
20307
20308 if (INTEGERP (it->slice.width))
20309 slice.width = XINT (it->slice.width);
20310 else if (FLOATP (it->slice.width))
20311 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
20312
20313 if (INTEGERP (it->slice.height))
20314 slice.height = XINT (it->slice.height);
20315 else if (FLOATP (it->slice.height))
20316 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
20317
20318 if (slice.x >= img->width)
20319 slice.x = img->width;
20320 if (slice.y >= img->height)
20321 slice.y = img->height;
20322 if (slice.x + slice.width >= img->width)
20323 slice.width = img->width - slice.x;
20324 if (slice.y + slice.height > img->height)
20325 slice.height = img->height - slice.y;
20326
20327 if (slice.width == 0 || slice.height == 0)
20328 return;
20329
20330 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
20331
20332 it->descent = slice.height - glyph_ascent;
20333 if (slice.y == 0)
20334 it->descent += img->vmargin;
20335 if (slice.y + slice.height == img->height)
20336 it->descent += img->vmargin;
20337 it->phys_descent = it->descent;
20338
20339 it->pixel_width = slice.width;
20340 if (slice.x == 0)
20341 it->pixel_width += img->hmargin;
20342 if (slice.x + slice.width == img->width)
20343 it->pixel_width += img->hmargin;
20344
20345 /* It's quite possible for images to have an ascent greater than
20346 their height, so don't get confused in that case. */
20347 if (it->descent < 0)
20348 it->descent = 0;
20349
20350 #if 0 /* this breaks image tiling */
20351 /* If this glyph is alone on the last line, adjust it.ascent to minimum row ascent. */
20352 int face_ascent = face->font ? FONT_BASE (face->font) : FRAME_BASELINE_OFFSET (it->f);
20353 if (face_ascent > it->ascent)
20354 it->ascent = it->phys_ascent = face_ascent;
20355 #endif
20356
20357 it->nglyphs = 1;
20358
20359 if (face->box != FACE_NO_BOX)
20360 {
20361 if (face->box_line_width > 0)
20362 {
20363 if (slice.y == 0)
20364 it->ascent += face->box_line_width;
20365 if (slice.y + slice.height == img->height)
20366 it->descent += face->box_line_width;
20367 }
20368
20369 if (it->start_of_box_run_p && slice.x == 0)
20370 it->pixel_width += eabs (face->box_line_width);
20371 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
20372 it->pixel_width += eabs (face->box_line_width);
20373 }
20374
20375 take_vertical_position_into_account (it);
20376
20377 /* Automatically crop wide image glyphs at right edge so we can
20378 draw the cursor on same display row. */
20379 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
20380 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
20381 {
20382 it->pixel_width -= crop;
20383 slice.width -= crop;
20384 }
20385
20386 if (it->glyph_row)
20387 {
20388 struct glyph *glyph;
20389 enum glyph_row_area area = it->area;
20390
20391 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
20392 if (glyph < it->glyph_row->glyphs[area + 1])
20393 {
20394 glyph->charpos = CHARPOS (it->position);
20395 glyph->object = it->object;
20396 glyph->pixel_width = it->pixel_width;
20397 glyph->ascent = glyph_ascent;
20398 glyph->descent = it->descent;
20399 glyph->voffset = it->voffset;
20400 glyph->type = IMAGE_GLYPH;
20401 glyph->multibyte_p = it->multibyte_p;
20402 glyph->left_box_line_p = it->start_of_box_run_p;
20403 glyph->right_box_line_p = it->end_of_box_run_p;
20404 glyph->overlaps_vertically_p = 0;
20405 glyph->padding_p = 0;
20406 glyph->glyph_not_available_p = 0;
20407 glyph->face_id = it->face_id;
20408 glyph->u.img_id = img->id;
20409 glyph->slice = slice;
20410 glyph->font_type = FONT_TYPE_UNKNOWN;
20411 ++it->glyph_row->used[area];
20412 }
20413 else
20414 IT_EXPAND_MATRIX_WIDTH (it, area);
20415 }
20416 }
20417
20418
20419 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
20420 of the glyph, WIDTH and HEIGHT are the width and height of the
20421 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
20422
20423 static void
20424 append_stretch_glyph (it, object, width, height, ascent)
20425 struct it *it;
20426 Lisp_Object object;
20427 int width, height;
20428 int ascent;
20429 {
20430 struct glyph *glyph;
20431 enum glyph_row_area area = it->area;
20432
20433 xassert (ascent >= 0 && ascent <= height);
20434
20435 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
20436 if (glyph < it->glyph_row->glyphs[area + 1])
20437 {
20438 glyph->charpos = CHARPOS (it->position);
20439 glyph->object = object;
20440 glyph->pixel_width = width;
20441 glyph->ascent = ascent;
20442 glyph->descent = height - ascent;
20443 glyph->voffset = it->voffset;
20444 glyph->type = STRETCH_GLYPH;
20445 glyph->multibyte_p = it->multibyte_p;
20446 glyph->left_box_line_p = it->start_of_box_run_p;
20447 glyph->right_box_line_p = it->end_of_box_run_p;
20448 glyph->overlaps_vertically_p = 0;
20449 glyph->padding_p = 0;
20450 glyph->glyph_not_available_p = 0;
20451 glyph->face_id = it->face_id;
20452 glyph->u.stretch.ascent = ascent;
20453 glyph->u.stretch.height = height;
20454 glyph->slice = null_glyph_slice;
20455 glyph->font_type = FONT_TYPE_UNKNOWN;
20456 ++it->glyph_row->used[area];
20457 }
20458 else
20459 IT_EXPAND_MATRIX_WIDTH (it, area);
20460 }
20461
20462
20463 /* Produce a stretch glyph for iterator IT. IT->object is the value
20464 of the glyph property displayed. The value must be a list
20465 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
20466 being recognized:
20467
20468 1. `:width WIDTH' specifies that the space should be WIDTH *
20469 canonical char width wide. WIDTH may be an integer or floating
20470 point number.
20471
20472 2. `:relative-width FACTOR' specifies that the width of the stretch
20473 should be computed from the width of the first character having the
20474 `glyph' property, and should be FACTOR times that width.
20475
20476 3. `:align-to HPOS' specifies that the space should be wide enough
20477 to reach HPOS, a value in canonical character units.
20478
20479 Exactly one of the above pairs must be present.
20480
20481 4. `:height HEIGHT' specifies that the height of the stretch produced
20482 should be HEIGHT, measured in canonical character units.
20483
20484 5. `:relative-height FACTOR' specifies that the height of the
20485 stretch should be FACTOR times the height of the characters having
20486 the glyph property.
20487
20488 Either none or exactly one of 4 or 5 must be present.
20489
20490 6. `:ascent ASCENT' specifies that ASCENT percent of the height
20491 of the stretch should be used for the ascent of the stretch.
20492 ASCENT must be in the range 0 <= ASCENT <= 100. */
20493
20494 static void
20495 produce_stretch_glyph (it)
20496 struct it *it;
20497 {
20498 /* (space :width WIDTH :height HEIGHT ...) */
20499 Lisp_Object prop, plist;
20500 int width = 0, height = 0, align_to = -1;
20501 int zero_width_ok_p = 0, zero_height_ok_p = 0;
20502 int ascent = 0;
20503 double tem;
20504 struct face *face = FACE_FROM_ID (it->f, it->face_id);
20505 XFontStruct *font = face->font ? face->font : FRAME_FONT (it->f);
20506
20507 PREPARE_FACE_FOR_DISPLAY (it->f, face);
20508
20509 /* List should start with `space'. */
20510 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
20511 plist = XCDR (it->object);
20512
20513 /* Compute the width of the stretch. */
20514 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
20515 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
20516 {
20517 /* Absolute width `:width WIDTH' specified and valid. */
20518 zero_width_ok_p = 1;
20519 width = (int)tem;
20520 }
20521 else if (prop = Fplist_get (plist, QCrelative_width),
20522 NUMVAL (prop) > 0)
20523 {
20524 /* Relative width `:relative-width FACTOR' specified and valid.
20525 Compute the width of the characters having the `glyph'
20526 property. */
20527 struct it it2;
20528 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
20529
20530 it2 = *it;
20531 if (it->multibyte_p)
20532 {
20533 int maxlen = ((IT_BYTEPOS (*it) >= GPT ? ZV : GPT)
20534 - IT_BYTEPOS (*it));
20535 it2.c = STRING_CHAR_AND_LENGTH (p, maxlen, it2.len);
20536 }
20537 else
20538 it2.c = *p, it2.len = 1;
20539
20540 it2.glyph_row = NULL;
20541 it2.what = IT_CHARACTER;
20542 x_produce_glyphs (&it2);
20543 width = NUMVAL (prop) * it2.pixel_width;
20544 }
20545 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
20546 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
20547 {
20548 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
20549 align_to = (align_to < 0
20550 ? 0
20551 : align_to - window_box_left_offset (it->w, TEXT_AREA));
20552 else if (align_to < 0)
20553 align_to = window_box_left_offset (it->w, TEXT_AREA);
20554 width = max (0, (int)tem + align_to - it->current_x);
20555 zero_width_ok_p = 1;
20556 }
20557 else
20558 /* Nothing specified -> width defaults to canonical char width. */
20559 width = FRAME_COLUMN_WIDTH (it->f);
20560
20561 if (width <= 0 && (width < 0 || !zero_width_ok_p))
20562 width = 1;
20563
20564 /* Compute height. */
20565 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
20566 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
20567 {
20568 height = (int)tem;
20569 zero_height_ok_p = 1;
20570 }
20571 else if (prop = Fplist_get (plist, QCrelative_height),
20572 NUMVAL (prop) > 0)
20573 height = FONT_HEIGHT (font) * NUMVAL (prop);
20574 else
20575 height = FONT_HEIGHT (font);
20576
20577 if (height <= 0 && (height < 0 || !zero_height_ok_p))
20578 height = 1;
20579
20580 /* Compute percentage of height used for ascent. If
20581 `:ascent ASCENT' is present and valid, use that. Otherwise,
20582 derive the ascent from the font in use. */
20583 if (prop = Fplist_get (plist, QCascent),
20584 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
20585 ascent = height * NUMVAL (prop) / 100.0;
20586 else if (!NILP (prop)
20587 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
20588 ascent = min (max (0, (int)tem), height);
20589 else
20590 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
20591
20592 if (width > 0 && !it->truncate_lines_p
20593 && it->current_x + width > it->last_visible_x)
20594 width = it->last_visible_x - it->current_x - 1;
20595
20596 if (width > 0 && height > 0 && it->glyph_row)
20597 {
20598 Lisp_Object object = it->stack[it->sp - 1].string;
20599 if (!STRINGP (object))
20600 object = it->w->buffer;
20601 append_stretch_glyph (it, object, width, height, ascent);
20602 }
20603
20604 it->pixel_width = width;
20605 it->ascent = it->phys_ascent = ascent;
20606 it->descent = it->phys_descent = height - it->ascent;
20607 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
20608
20609 take_vertical_position_into_account (it);
20610 }
20611
20612 /* Get line-height and line-spacing property at point.
20613 If line-height has format (HEIGHT TOTAL), return TOTAL
20614 in TOTAL_HEIGHT. */
20615
20616 static Lisp_Object
20617 get_line_height_property (it, prop)
20618 struct it *it;
20619 Lisp_Object prop;
20620 {
20621 Lisp_Object position;
20622
20623 if (STRINGP (it->object))
20624 position = make_number (IT_STRING_CHARPOS (*it));
20625 else if (BUFFERP (it->object))
20626 position = make_number (IT_CHARPOS (*it));
20627 else
20628 return Qnil;
20629
20630 return Fget_char_property (position, prop, it->object);
20631 }
20632
20633 /* Calculate line-height and line-spacing properties.
20634 An integer value specifies explicit pixel value.
20635 A float value specifies relative value to current face height.
20636 A cons (float . face-name) specifies relative value to
20637 height of specified face font.
20638
20639 Returns height in pixels, or nil. */
20640
20641
20642 static Lisp_Object
20643 calc_line_height_property (it, val, font, boff, override)
20644 struct it *it;
20645 Lisp_Object val;
20646 XFontStruct *font;
20647 int boff, override;
20648 {
20649 Lisp_Object face_name = Qnil;
20650 int ascent, descent, height;
20651
20652 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
20653 return val;
20654
20655 if (CONSP (val))
20656 {
20657 face_name = XCAR (val);
20658 val = XCDR (val);
20659 if (!NUMBERP (val))
20660 val = make_number (1);
20661 if (NILP (face_name))
20662 {
20663 height = it->ascent + it->descent;
20664 goto scale;
20665 }
20666 }
20667
20668 if (NILP (face_name))
20669 {
20670 font = FRAME_FONT (it->f);
20671 boff = FRAME_BASELINE_OFFSET (it->f);
20672 }
20673 else if (EQ (face_name, Qt))
20674 {
20675 override = 0;
20676 }
20677 else
20678 {
20679 int face_id;
20680 struct face *face;
20681 struct font_info *font_info;
20682
20683 face_id = lookup_named_face (it->f, face_name, 0);
20684 if (face_id < 0)
20685 return make_number (-1);
20686
20687 face = FACE_FROM_ID (it->f, face_id);
20688 font = face->font;
20689 if (font == NULL)
20690 return make_number (-1);
20691
20692 font_info = FONT_INFO_FROM_FACE (it->f, face);
20693 boff = font_info->baseline_offset;
20694 if (font_info->vertical_centering)
20695 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
20696 }
20697
20698 ascent = FONT_BASE (font) + boff;
20699 descent = FONT_DESCENT (font) - boff;
20700
20701 if (override)
20702 {
20703 it->override_ascent = ascent;
20704 it->override_descent = descent;
20705 it->override_boff = boff;
20706 }
20707
20708 height = ascent + descent;
20709
20710 scale:
20711 if (FLOATP (val))
20712 height = (int)(XFLOAT_DATA (val) * height);
20713 else if (INTEGERP (val))
20714 height *= XINT (val);
20715
20716 return make_number (height);
20717 }
20718
20719
20720 /* RIF:
20721 Produce glyphs/get display metrics for the display element IT is
20722 loaded with. See the description of struct it in dispextern.h
20723 for an overview of struct it. */
20724
20725 void
20726 x_produce_glyphs (it)
20727 struct it *it;
20728 {
20729 int extra_line_spacing = it->extra_line_spacing;
20730
20731 it->glyph_not_available_p = 0;
20732
20733 if (it->what == IT_CHARACTER)
20734 {
20735 XChar2b char2b;
20736 XFontStruct *font;
20737 struct face *face = FACE_FROM_ID (it->f, it->face_id);
20738 XCharStruct *pcm;
20739 int font_not_found_p;
20740 struct font_info *font_info;
20741 int boff; /* baseline offset */
20742 /* We may change it->multibyte_p upon unibyte<->multibyte
20743 conversion. So, save the current value now and restore it
20744 later.
20745
20746 Note: It seems that we don't have to record multibyte_p in
20747 struct glyph because the character code itself tells if or
20748 not the character is multibyte. Thus, in the future, we must
20749 consider eliminating the field `multibyte_p' in the struct
20750 glyph. */
20751 int saved_multibyte_p = it->multibyte_p;
20752
20753 /* Maybe translate single-byte characters to multibyte, or the
20754 other way. */
20755 it->char_to_display = it->c;
20756 if (!ASCII_BYTE_P (it->c)
20757 && ! it->multibyte_p)
20758 {
20759 if (SINGLE_BYTE_CHAR_P (it->c)
20760 && unibyte_display_via_language_environment)
20761 it->char_to_display = unibyte_char_to_multibyte (it->c);
20762 if (! SINGLE_BYTE_CHAR_P (it->char_to_display))
20763 {
20764 it->multibyte_p = 1;
20765 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display,
20766 -1, Qnil);
20767 face = FACE_FROM_ID (it->f, it->face_id);
20768 }
20769 }
20770
20771 /* Get font to use. Encode IT->char_to_display. */
20772 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
20773 &char2b, it->multibyte_p, 0);
20774 font = face->font;
20775
20776 /* When no suitable font found, use the default font. */
20777 font_not_found_p = font == NULL;
20778 if (font_not_found_p)
20779 {
20780 font = FRAME_FONT (it->f);
20781 boff = FRAME_BASELINE_OFFSET (it->f);
20782 font_info = NULL;
20783 }
20784 else
20785 {
20786 font_info = FONT_INFO_FROM_FACE (it->f, face);
20787 boff = font_info->baseline_offset;
20788 if (font_info->vertical_centering)
20789 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
20790 }
20791
20792 if (it->char_to_display >= ' '
20793 && (!it->multibyte_p || it->char_to_display < 128))
20794 {
20795 /* Either unibyte or ASCII. */
20796 int stretched_p;
20797
20798 it->nglyphs = 1;
20799
20800 pcm = get_per_char_metric (it->f, font, font_info, &char2b,
20801 FONT_TYPE_FOR_UNIBYTE (font, it->char_to_display));
20802
20803 if (it->override_ascent >= 0)
20804 {
20805 it->ascent = it->override_ascent;
20806 it->descent = it->override_descent;
20807 boff = it->override_boff;
20808 }
20809 else
20810 {
20811 it->ascent = FONT_BASE (font) + boff;
20812 it->descent = FONT_DESCENT (font) - boff;
20813 }
20814
20815 if (pcm)
20816 {
20817 it->phys_ascent = pcm->ascent + boff;
20818 it->phys_descent = pcm->descent - boff;
20819 it->pixel_width = pcm->width;
20820 }
20821 else
20822 {
20823 it->glyph_not_available_p = 1;
20824 it->phys_ascent = it->ascent;
20825 it->phys_descent = it->descent;
20826 it->pixel_width = FONT_WIDTH (font);
20827 }
20828
20829 if (it->constrain_row_ascent_descent_p)
20830 {
20831 if (it->descent > it->max_descent)
20832 {
20833 it->ascent += it->descent - it->max_descent;
20834 it->descent = it->max_descent;
20835 }
20836 if (it->ascent > it->max_ascent)
20837 {
20838 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
20839 it->ascent = it->max_ascent;
20840 }
20841 it->phys_ascent = min (it->phys_ascent, it->ascent);
20842 it->phys_descent = min (it->phys_descent, it->descent);
20843 extra_line_spacing = 0;
20844 }
20845
20846 /* If this is a space inside a region of text with
20847 `space-width' property, change its width. */
20848 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
20849 if (stretched_p)
20850 it->pixel_width *= XFLOATINT (it->space_width);
20851
20852 /* If face has a box, add the box thickness to the character
20853 height. If character has a box line to the left and/or
20854 right, add the box line width to the character's width. */
20855 if (face->box != FACE_NO_BOX)
20856 {
20857 int thick = face->box_line_width;
20858
20859 if (thick > 0)
20860 {
20861 it->ascent += thick;
20862 it->descent += thick;
20863 }
20864 else
20865 thick = -thick;
20866
20867 if (it->start_of_box_run_p)
20868 it->pixel_width += thick;
20869 if (it->end_of_box_run_p)
20870 it->pixel_width += thick;
20871 }
20872
20873 /* If face has an overline, add the height of the overline
20874 (1 pixel) and a 1 pixel margin to the character height. */
20875 if (face->overline_p)
20876 it->ascent += overline_margin;
20877
20878 if (it->constrain_row_ascent_descent_p)
20879 {
20880 if (it->ascent > it->max_ascent)
20881 it->ascent = it->max_ascent;
20882 if (it->descent > it->max_descent)
20883 it->descent = it->max_descent;
20884 }
20885
20886 take_vertical_position_into_account (it);
20887
20888 /* If we have to actually produce glyphs, do it. */
20889 if (it->glyph_row)
20890 {
20891 if (stretched_p)
20892 {
20893 /* Translate a space with a `space-width' property
20894 into a stretch glyph. */
20895 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
20896 / FONT_HEIGHT (font));
20897 append_stretch_glyph (it, it->object, it->pixel_width,
20898 it->ascent + it->descent, ascent);
20899 }
20900 else
20901 append_glyph (it);
20902
20903 /* If characters with lbearing or rbearing are displayed
20904 in this line, record that fact in a flag of the
20905 glyph row. This is used to optimize X output code. */
20906 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
20907 it->glyph_row->contains_overlapping_glyphs_p = 1;
20908 }
20909 }
20910 else if (it->char_to_display == '\n')
20911 {
20912 /* A newline has no width but we need the height of the line.
20913 But if previous part of the line set a height, don't
20914 increase that height */
20915
20916 Lisp_Object height;
20917 Lisp_Object total_height = Qnil;
20918
20919 it->override_ascent = -1;
20920 it->pixel_width = 0;
20921 it->nglyphs = 0;
20922
20923 height = get_line_height_property(it, Qline_height);
20924 /* Split (line-height total-height) list */
20925 if (CONSP (height)
20926 && CONSP (XCDR (height))
20927 && NILP (XCDR (XCDR (height))))
20928 {
20929 total_height = XCAR (XCDR (height));
20930 height = XCAR (height);
20931 }
20932 height = calc_line_height_property(it, height, font, boff, 1);
20933
20934 if (it->override_ascent >= 0)
20935 {
20936 it->ascent = it->override_ascent;
20937 it->descent = it->override_descent;
20938 boff = it->override_boff;
20939 }
20940 else
20941 {
20942 it->ascent = FONT_BASE (font) + boff;
20943 it->descent = FONT_DESCENT (font) - boff;
20944 }
20945
20946 if (EQ (height, Qt))
20947 {
20948 if (it->descent > it->max_descent)
20949 {
20950 it->ascent += it->descent - it->max_descent;
20951 it->descent = it->max_descent;
20952 }
20953 if (it->ascent > it->max_ascent)
20954 {
20955 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
20956 it->ascent = it->max_ascent;
20957 }
20958 it->phys_ascent = min (it->phys_ascent, it->ascent);
20959 it->phys_descent = min (it->phys_descent, it->descent);
20960 it->constrain_row_ascent_descent_p = 1;
20961 extra_line_spacing = 0;
20962 }
20963 else
20964 {
20965 Lisp_Object spacing;
20966
20967 it->phys_ascent = it->ascent;
20968 it->phys_descent = it->descent;
20969
20970 if ((it->max_ascent > 0 || it->max_descent > 0)
20971 && face->box != FACE_NO_BOX
20972 && face->box_line_width > 0)
20973 {
20974 it->ascent += face->box_line_width;
20975 it->descent += face->box_line_width;
20976 }
20977 if (!NILP (height)
20978 && XINT (height) > it->ascent + it->descent)
20979 it->ascent = XINT (height) - it->descent;
20980
20981 if (!NILP (total_height))
20982 spacing = calc_line_height_property(it, total_height, font, boff, 0);
20983 else
20984 {
20985 spacing = get_line_height_property(it, Qline_spacing);
20986 spacing = calc_line_height_property(it, spacing, font, boff, 0);
20987 }
20988 if (INTEGERP (spacing))
20989 {
20990 extra_line_spacing = XINT (spacing);
20991 if (!NILP (total_height))
20992 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
20993 }
20994 }
20995 }
20996 else if (it->char_to_display == '\t')
20997 {
20998 int tab_width = it->tab_width * FRAME_SPACE_WIDTH (it->f);
20999 int x = it->current_x + it->continuation_lines_width;
21000 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
21001
21002 /* If the distance from the current position to the next tab
21003 stop is less than a space character width, use the
21004 tab stop after that. */
21005 if (next_tab_x - x < FRAME_SPACE_WIDTH (it->f))
21006 next_tab_x += tab_width;
21007
21008 it->pixel_width = next_tab_x - x;
21009 it->nglyphs = 1;
21010 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
21011 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
21012
21013 if (it->glyph_row)
21014 {
21015 append_stretch_glyph (it, it->object, it->pixel_width,
21016 it->ascent + it->descent, it->ascent);
21017 }
21018 }
21019 else
21020 {
21021 /* A multi-byte character. Assume that the display width of the
21022 character is the width of the character multiplied by the
21023 width of the font. */
21024
21025 /* If we found a font, this font should give us the right
21026 metrics. If we didn't find a font, use the frame's
21027 default font and calculate the width of the character by
21028 multiplying the width of font by the width of the
21029 character. */
21030
21031 pcm = get_per_char_metric (it->f, font, font_info, &char2b,
21032 FONT_TYPE_FOR_MULTIBYTE (font, it->c));
21033
21034 if (font_not_found_p || !pcm)
21035 {
21036 int char_width = CHAR_WIDTH (it->char_to_display);
21037
21038 if (char_width == 0)
21039 /* This is a non spacing character. But, as we are
21040 going to display an empty box, the box must occupy
21041 at least one column. */
21042 char_width = 1;
21043 it->glyph_not_available_p = 1;
21044 it->pixel_width = FRAME_COLUMN_WIDTH (it->f) * char_width;
21045 it->phys_ascent = FONT_BASE (font) + boff;
21046 it->phys_descent = FONT_DESCENT (font) - boff;
21047 }
21048 else
21049 {
21050 it->pixel_width = pcm->width;
21051 it->phys_ascent = pcm->ascent + boff;
21052 it->phys_descent = pcm->descent - boff;
21053 if (it->glyph_row
21054 && (pcm->lbearing < 0
21055 || pcm->rbearing > pcm->width))
21056 it->glyph_row->contains_overlapping_glyphs_p = 1;
21057 }
21058 it->nglyphs = 1;
21059 it->ascent = FONT_BASE (font) + boff;
21060 it->descent = FONT_DESCENT (font) - boff;
21061 if (face->box != FACE_NO_BOX)
21062 {
21063 int thick = face->box_line_width;
21064
21065 if (thick > 0)
21066 {
21067 it->ascent += thick;
21068 it->descent += thick;
21069 }
21070 else
21071 thick = - thick;
21072
21073 if (it->start_of_box_run_p)
21074 it->pixel_width += thick;
21075 if (it->end_of_box_run_p)
21076 it->pixel_width += thick;
21077 }
21078
21079 /* If face has an overline, add the height of the overline
21080 (1 pixel) and a 1 pixel margin to the character height. */
21081 if (face->overline_p)
21082 it->ascent += overline_margin;
21083
21084 take_vertical_position_into_account (it);
21085
21086 if (it->glyph_row)
21087 append_glyph (it);
21088 }
21089 it->multibyte_p = saved_multibyte_p;
21090 }
21091 else if (it->what == IT_COMPOSITION)
21092 {
21093 /* Note: A composition is represented as one glyph in the
21094 glyph matrix. There are no padding glyphs.
21095
21096 Important is that pixel_width, ascent, and descent are the
21097 values of what is drawn by draw_glyphs (i.e. the values of
21098 the overall glyphs composed). */
21099 struct face *face = FACE_FROM_ID (it->f, it->face_id);
21100 int boff; /* baseline offset */
21101 struct composition *cmp = composition_table[it->cmp_id];
21102 int glyph_len = cmp->glyph_len;
21103 XFontStruct *font = face->font;
21104
21105 it->nglyphs = 1;
21106
21107 #ifdef USE_FONT_BACKEND
21108 if (cmp->method == COMPOSITION_WITH_GLYPH_STRING)
21109 {
21110 if (! cmp->font || cmp->font != font)
21111 font_prepare_composition (cmp, it->f);
21112 }
21113 else
21114 #endif /* USE_FONT_BACKEND */
21115 /* If we have not yet calculated pixel size data of glyphs of
21116 the composition for the current face font, calculate them
21117 now. Theoretically, we have to check all fonts for the
21118 glyphs, but that requires much time and memory space. So,
21119 here we check only the font of the first glyph. This leads
21120 to incorrect display, but it's very rare, and C-l (recenter)
21121 can correct the display anyway. */
21122 if (! cmp->font || cmp->font != font)
21123 {
21124 /* Ascent and descent of the font of the first character
21125 of this composition (adjusted by baseline offset).
21126 Ascent and descent of overall glyphs should not be less
21127 than them respectively. */
21128 int font_ascent, font_descent, font_height;
21129 /* Bounding box of the overall glyphs. */
21130 int leftmost, rightmost, lowest, highest;
21131 int lbearing, rbearing;
21132 int i, width, ascent, descent;
21133 int left_padded = 0, right_padded = 0;
21134 int face_id;
21135 int c;
21136 XChar2b char2b;
21137 XCharStruct *pcm;
21138 int font_not_found_p;
21139 struct font_info *font_info;
21140 int pos;
21141
21142 for (glyph_len = cmp->glyph_len; glyph_len > 0; glyph_len--)
21143 if ((c = COMPOSITION_GLYPH (cmp, glyph_len - 1)) != '\t')
21144 break;
21145 if (glyph_len < cmp->glyph_len)
21146 right_padded = 1;
21147 for (i = 0; i < glyph_len; i++)
21148 {
21149 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
21150 break;
21151 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
21152 }
21153 if (i > 0)
21154 left_padded = 1;
21155
21156 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
21157 : IT_CHARPOS (*it));
21158 /* When no suitable font found, use the default font. */
21159 font_not_found_p = font == NULL;
21160 if (font_not_found_p)
21161 {
21162 face = face->ascii_face;
21163 font = face->font;
21164 }
21165 font_info = FONT_INFO_FROM_FACE (it->f, face);
21166 boff = font_info->baseline_offset;
21167 if (font_info->vertical_centering)
21168 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
21169 font_ascent = FONT_BASE (font) + boff;
21170 font_descent = FONT_DESCENT (font) - boff;
21171 font_height = FONT_HEIGHT (font);
21172
21173 cmp->font = (void *) font;
21174
21175 pcm = NULL;
21176 if (! font_not_found_p)
21177 {
21178 get_char_face_and_encoding (it->f, c, it->face_id,
21179 &char2b, it->multibyte_p, 0);
21180 pcm = get_per_char_metric (it->f, font, font_info, &char2b,
21181 FONT_TYPE_FOR_MULTIBYTE (font, c));
21182 }
21183
21184 /* Initialize the bounding box. */
21185 if (pcm)
21186 {
21187 width = pcm->width;
21188 ascent = pcm->ascent;
21189 descent = pcm->descent;
21190 lbearing = pcm->lbearing;
21191 rbearing = pcm->rbearing;
21192 }
21193 else
21194 {
21195 width = FONT_WIDTH (font);
21196 ascent = FONT_BASE (font);
21197 descent = FONT_DESCENT (font);
21198 lbearing = 0;
21199 rbearing = width;
21200 }
21201
21202 rightmost = width;
21203 leftmost = 0;
21204 lowest = - descent + boff;
21205 highest = ascent + boff;
21206
21207 if (! font_not_found_p
21208 && font_info->default_ascent
21209 && CHAR_TABLE_P (Vuse_default_ascent)
21210 && !NILP (Faref (Vuse_default_ascent,
21211 make_number (it->char_to_display))))
21212 highest = font_info->default_ascent + boff;
21213
21214 /* Draw the first glyph at the normal position. It may be
21215 shifted to right later if some other glyphs are drawn
21216 at the left. */
21217 cmp->offsets[i * 2] = 0;
21218 cmp->offsets[i * 2 + 1] = boff;
21219 cmp->lbearing = lbearing;
21220 cmp->rbearing = rbearing;
21221
21222 /* Set cmp->offsets for the remaining glyphs. */
21223 for (i++; i < glyph_len; i++)
21224 {
21225 int left, right, btm, top;
21226 int ch = COMPOSITION_GLYPH (cmp, i);
21227 int face_id;
21228 struct face *this_face;
21229 int this_boff;
21230
21231 if (ch == '\t')
21232 ch = ' ';
21233 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
21234 this_face = FACE_FROM_ID (it->f, face_id);
21235 font = this_face->font;
21236
21237 if (font == NULL)
21238 pcm = NULL;
21239 else
21240 {
21241 font_info = FONT_INFO_FROM_FACE (it->f, this_face);
21242 this_boff = font_info->baseline_offset;
21243 if (font_info->vertical_centering)
21244 this_boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
21245 get_char_face_and_encoding (it->f, ch, face_id,
21246 &char2b, it->multibyte_p, 0);
21247 pcm = get_per_char_metric (it->f, font, font_info, &char2b,
21248 FONT_TYPE_FOR_MULTIBYTE (font,
21249 ch));
21250 }
21251 if (! pcm)
21252 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
21253 else
21254 {
21255 width = pcm->width;
21256 ascent = pcm->ascent;
21257 descent = pcm->descent;
21258 lbearing = pcm->lbearing;
21259 rbearing = pcm->rbearing;
21260 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
21261 {
21262 /* Relative composition with or without
21263 alternate chars. */
21264 left = (leftmost + rightmost - width) / 2;
21265 btm = - descent + boff;
21266 if (font_info->relative_compose
21267 && (! CHAR_TABLE_P (Vignore_relative_composition)
21268 || NILP (Faref (Vignore_relative_composition,
21269 make_number (ch)))))
21270 {
21271
21272 if (- descent >= font_info->relative_compose)
21273 /* One extra pixel between two glyphs. */
21274 btm = highest + 1;
21275 else if (ascent <= 0)
21276 /* One extra pixel between two glyphs. */
21277 btm = lowest - 1 - ascent - descent;
21278 }
21279 }
21280 else
21281 {
21282 /* A composition rule is specified by an integer
21283 value that encodes global and new reference
21284 points (GREF and NREF). GREF and NREF are
21285 specified by numbers as below:
21286
21287 0---1---2 -- ascent
21288 | |
21289 | |
21290 | |
21291 9--10--11 -- center
21292 | |
21293 ---3---4---5--- baseline
21294 | |
21295 6---7---8 -- descent
21296 */
21297 int rule = COMPOSITION_RULE (cmp, i);
21298 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
21299
21300 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
21301 grefx = gref % 3, nrefx = nref % 3;
21302 grefy = gref / 3, nrefy = nref / 3;
21303 if (xoff)
21304 xoff = font_height * (xoff - 128) / 256;
21305 if (yoff)
21306 yoff = font_height * (yoff - 128) / 256;
21307
21308 left = (leftmost
21309 + grefx * (rightmost - leftmost) / 2
21310 - nrefx * width / 2
21311 + xoff);
21312
21313 btm = ((grefy == 0 ? highest
21314 : grefy == 1 ? 0
21315 : grefy == 2 ? lowest
21316 : (highest + lowest) / 2)
21317 - (nrefy == 0 ? ascent + descent
21318 : nrefy == 1 ? descent - boff
21319 : nrefy == 2 ? 0
21320 : (ascent + descent) / 2)
21321 + yoff);
21322 }
21323
21324 cmp->offsets[i * 2] = left;
21325 cmp->offsets[i * 2 + 1] = btm + descent;
21326
21327 /* Update the bounding box of the overall glyphs. */
21328 if (width > 0)
21329 {
21330 right = left + width;
21331 if (left < leftmost)
21332 leftmost = left;
21333 if (right > rightmost)
21334 rightmost = right;
21335 }
21336 top = btm + descent + ascent;
21337 if (top > highest)
21338 highest = top;
21339 if (btm < lowest)
21340 lowest = btm;
21341
21342 if (cmp->lbearing > left + lbearing)
21343 cmp->lbearing = left + lbearing;
21344 if (cmp->rbearing < left + rbearing)
21345 cmp->rbearing = left + rbearing;
21346 }
21347 }
21348
21349 /* If there are glyphs whose x-offsets are negative,
21350 shift all glyphs to the right and make all x-offsets
21351 non-negative. */
21352 if (leftmost < 0)
21353 {
21354 for (i = 0; i < cmp->glyph_len; i++)
21355 cmp->offsets[i * 2] -= leftmost;
21356 rightmost -= leftmost;
21357 cmp->lbearing -= leftmost;
21358 cmp->rbearing -= leftmost;
21359 }
21360
21361 if (left_padded && cmp->lbearing < 0)
21362 {
21363 for (i = 0; i < cmp->glyph_len; i++)
21364 cmp->offsets[i * 2] -= cmp->lbearing;
21365 rightmost -= cmp->lbearing;
21366 cmp->rbearing -= cmp->lbearing;
21367 cmp->lbearing = 0;
21368 }
21369 if (right_padded && rightmost < cmp->rbearing)
21370 {
21371 rightmost = cmp->rbearing;
21372 }
21373
21374 cmp->pixel_width = rightmost;
21375 cmp->ascent = highest;
21376 cmp->descent = - lowest;
21377 if (cmp->ascent < font_ascent)
21378 cmp->ascent = font_ascent;
21379 if (cmp->descent < font_descent)
21380 cmp->descent = font_descent;
21381 }
21382
21383 if (it->glyph_row
21384 && (cmp->lbearing < 0
21385 || cmp->rbearing > cmp->pixel_width))
21386 it->glyph_row->contains_overlapping_glyphs_p = 1;
21387
21388 it->pixel_width = cmp->pixel_width;
21389 it->ascent = it->phys_ascent = cmp->ascent;
21390 it->descent = it->phys_descent = cmp->descent;
21391
21392 if (face->box != FACE_NO_BOX)
21393 {
21394 int thick = face->box_line_width;
21395
21396 if (thick > 0)
21397 {
21398 it->ascent += thick;
21399 it->descent += thick;
21400 }
21401 else
21402 thick = - thick;
21403
21404 if (it->start_of_box_run_p)
21405 it->pixel_width += thick;
21406 if (it->end_of_box_run_p)
21407 it->pixel_width += thick;
21408 }
21409
21410 /* If face has an overline, add the height of the overline
21411 (1 pixel) and a 1 pixel margin to the character height. */
21412 if (face->overline_p)
21413 it->ascent += overline_margin;
21414
21415 take_vertical_position_into_account (it);
21416
21417 if (it->glyph_row)
21418 append_composite_glyph (it);
21419 }
21420 else if (it->what == IT_IMAGE)
21421 produce_image_glyph (it);
21422 else if (it->what == IT_STRETCH)
21423 produce_stretch_glyph (it);
21424
21425 /* Accumulate dimensions. Note: can't assume that it->descent > 0
21426 because this isn't true for images with `:ascent 100'. */
21427 xassert (it->ascent >= 0 && it->descent >= 0);
21428 if (it->area == TEXT_AREA)
21429 it->current_x += it->pixel_width;
21430
21431 if (extra_line_spacing > 0)
21432 {
21433 it->descent += extra_line_spacing;
21434 if (extra_line_spacing > it->max_extra_line_spacing)
21435 it->max_extra_line_spacing = extra_line_spacing;
21436 }
21437
21438 it->max_ascent = max (it->max_ascent, it->ascent);
21439 it->max_descent = max (it->max_descent, it->descent);
21440 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
21441 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
21442 }
21443
21444 /* EXPORT for RIF:
21445 Output LEN glyphs starting at START at the nominal cursor position.
21446 Advance the nominal cursor over the text. The global variable
21447 updated_window contains the window being updated, updated_row is
21448 the glyph row being updated, and updated_area is the area of that
21449 row being updated. */
21450
21451 void
21452 x_write_glyphs (start, len)
21453 struct glyph *start;
21454 int len;
21455 {
21456 int x, hpos;
21457
21458 xassert (updated_window && updated_row);
21459 BLOCK_INPUT;
21460
21461 /* Write glyphs. */
21462
21463 hpos = start - updated_row->glyphs[updated_area];
21464 x = draw_glyphs (updated_window, output_cursor.x,
21465 updated_row, updated_area,
21466 hpos, hpos + len,
21467 DRAW_NORMAL_TEXT, 0);
21468
21469 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
21470 if (updated_area == TEXT_AREA
21471 && updated_window->phys_cursor_on_p
21472 && updated_window->phys_cursor.vpos == output_cursor.vpos
21473 && updated_window->phys_cursor.hpos >= hpos
21474 && updated_window->phys_cursor.hpos < hpos + len)
21475 updated_window->phys_cursor_on_p = 0;
21476
21477 UNBLOCK_INPUT;
21478
21479 /* Advance the output cursor. */
21480 output_cursor.hpos += len;
21481 output_cursor.x = x;
21482 }
21483
21484
21485 /* EXPORT for RIF:
21486 Insert LEN glyphs from START at the nominal cursor position. */
21487
21488 void
21489 x_insert_glyphs (start, len)
21490 struct glyph *start;
21491 int len;
21492 {
21493 struct frame *f;
21494 struct window *w;
21495 int line_height, shift_by_width, shifted_region_width;
21496 struct glyph_row *row;
21497 struct glyph *glyph;
21498 int frame_x, frame_y;
21499 EMACS_INT hpos;
21500
21501 xassert (updated_window && updated_row);
21502 BLOCK_INPUT;
21503 w = updated_window;
21504 f = XFRAME (WINDOW_FRAME (w));
21505
21506 /* Get the height of the line we are in. */
21507 row = updated_row;
21508 line_height = row->height;
21509
21510 /* Get the width of the glyphs to insert. */
21511 shift_by_width = 0;
21512 for (glyph = start; glyph < start + len; ++glyph)
21513 shift_by_width += glyph->pixel_width;
21514
21515 /* Get the width of the region to shift right. */
21516 shifted_region_width = (window_box_width (w, updated_area)
21517 - output_cursor.x
21518 - shift_by_width);
21519
21520 /* Shift right. */
21521 frame_x = window_box_left (w, updated_area) + output_cursor.x;
21522 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
21523
21524 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
21525 line_height, shift_by_width);
21526
21527 /* Write the glyphs. */
21528 hpos = start - row->glyphs[updated_area];
21529 draw_glyphs (w, output_cursor.x, row, updated_area,
21530 hpos, hpos + len,
21531 DRAW_NORMAL_TEXT, 0);
21532
21533 /* Advance the output cursor. */
21534 output_cursor.hpos += len;
21535 output_cursor.x += shift_by_width;
21536 UNBLOCK_INPUT;
21537 }
21538
21539
21540 /* EXPORT for RIF:
21541 Erase the current text line from the nominal cursor position
21542 (inclusive) to pixel column TO_X (exclusive). The idea is that
21543 everything from TO_X onward is already erased.
21544
21545 TO_X is a pixel position relative to updated_area of
21546 updated_window. TO_X == -1 means clear to the end of this area. */
21547
21548 void
21549 x_clear_end_of_line (to_x)
21550 int to_x;
21551 {
21552 struct frame *f;
21553 struct window *w = updated_window;
21554 int max_x, min_y, max_y;
21555 int from_x, from_y, to_y;
21556
21557 xassert (updated_window && updated_row);
21558 f = XFRAME (w->frame);
21559
21560 if (updated_row->full_width_p)
21561 max_x = WINDOW_TOTAL_WIDTH (w);
21562 else
21563 max_x = window_box_width (w, updated_area);
21564 max_y = window_text_bottom_y (w);
21565
21566 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
21567 of window. For TO_X > 0, truncate to end of drawing area. */
21568 if (to_x == 0)
21569 return;
21570 else if (to_x < 0)
21571 to_x = max_x;
21572 else
21573 to_x = min (to_x, max_x);
21574
21575 to_y = min (max_y, output_cursor.y + updated_row->height);
21576
21577 /* Notice if the cursor will be cleared by this operation. */
21578 if (!updated_row->full_width_p)
21579 notice_overwritten_cursor (w, updated_area,
21580 output_cursor.x, -1,
21581 updated_row->y,
21582 MATRIX_ROW_BOTTOM_Y (updated_row));
21583
21584 from_x = output_cursor.x;
21585
21586 /* Translate to frame coordinates. */
21587 if (updated_row->full_width_p)
21588 {
21589 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
21590 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
21591 }
21592 else
21593 {
21594 int area_left = window_box_left (w, updated_area);
21595 from_x += area_left;
21596 to_x += area_left;
21597 }
21598
21599 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
21600 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
21601 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
21602
21603 /* Prevent inadvertently clearing to end of the X window. */
21604 if (to_x > from_x && to_y > from_y)
21605 {
21606 BLOCK_INPUT;
21607 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
21608 to_x - from_x, to_y - from_y);
21609 UNBLOCK_INPUT;
21610 }
21611 }
21612
21613 #endif /* HAVE_WINDOW_SYSTEM */
21614
21615
21616 \f
21617 /***********************************************************************
21618 Cursor types
21619 ***********************************************************************/
21620
21621 /* Value is the internal representation of the specified cursor type
21622 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
21623 of the bar cursor. */
21624
21625 static enum text_cursor_kinds
21626 get_specified_cursor_type (arg, width)
21627 Lisp_Object arg;
21628 int *width;
21629 {
21630 enum text_cursor_kinds type;
21631
21632 if (NILP (arg))
21633 return NO_CURSOR;
21634
21635 if (EQ (arg, Qbox))
21636 return FILLED_BOX_CURSOR;
21637
21638 if (EQ (arg, Qhollow))
21639 return HOLLOW_BOX_CURSOR;
21640
21641 if (EQ (arg, Qbar))
21642 {
21643 *width = 2;
21644 return BAR_CURSOR;
21645 }
21646
21647 if (CONSP (arg)
21648 && EQ (XCAR (arg), Qbar)
21649 && INTEGERP (XCDR (arg))
21650 && XINT (XCDR (arg)) >= 0)
21651 {
21652 *width = XINT (XCDR (arg));
21653 return BAR_CURSOR;
21654 }
21655
21656 if (EQ (arg, Qhbar))
21657 {
21658 *width = 2;
21659 return HBAR_CURSOR;
21660 }
21661
21662 if (CONSP (arg)
21663 && EQ (XCAR (arg), Qhbar)
21664 && INTEGERP (XCDR (arg))
21665 && XINT (XCDR (arg)) >= 0)
21666 {
21667 *width = XINT (XCDR (arg));
21668 return HBAR_CURSOR;
21669 }
21670
21671 /* Treat anything unknown as "hollow box cursor".
21672 It was bad to signal an error; people have trouble fixing
21673 .Xdefaults with Emacs, when it has something bad in it. */
21674 type = HOLLOW_BOX_CURSOR;
21675
21676 return type;
21677 }
21678
21679 /* Set the default cursor types for specified frame. */
21680 void
21681 set_frame_cursor_types (f, arg)
21682 struct frame *f;
21683 Lisp_Object arg;
21684 {
21685 int width;
21686 Lisp_Object tem;
21687
21688 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
21689 FRAME_CURSOR_WIDTH (f) = width;
21690
21691 /* By default, set up the blink-off state depending on the on-state. */
21692
21693 tem = Fassoc (arg, Vblink_cursor_alist);
21694 if (!NILP (tem))
21695 {
21696 FRAME_BLINK_OFF_CURSOR (f)
21697 = get_specified_cursor_type (XCDR (tem), &width);
21698 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
21699 }
21700 else
21701 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
21702 }
21703
21704
21705 /* Return the cursor we want to be displayed in window W. Return
21706 width of bar/hbar cursor through WIDTH arg. Return with
21707 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
21708 (i.e. if the `system caret' should track this cursor).
21709
21710 In a mini-buffer window, we want the cursor only to appear if we
21711 are reading input from this window. For the selected window, we
21712 want the cursor type given by the frame parameter or buffer local
21713 setting of cursor-type. If explicitly marked off, draw no cursor.
21714 In all other cases, we want a hollow box cursor. */
21715
21716 static enum text_cursor_kinds
21717 get_window_cursor_type (w, glyph, width, active_cursor)
21718 struct window *w;
21719 struct glyph *glyph;
21720 int *width;
21721 int *active_cursor;
21722 {
21723 struct frame *f = XFRAME (w->frame);
21724 struct buffer *b = XBUFFER (w->buffer);
21725 int cursor_type = DEFAULT_CURSOR;
21726 Lisp_Object alt_cursor;
21727 int non_selected = 0;
21728
21729 *active_cursor = 1;
21730
21731 /* Echo area */
21732 if (cursor_in_echo_area
21733 && FRAME_HAS_MINIBUF_P (f)
21734 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
21735 {
21736 if (w == XWINDOW (echo_area_window))
21737 {
21738 if (EQ (b->cursor_type, Qt) || NILP (b->cursor_type))
21739 {
21740 *width = FRAME_CURSOR_WIDTH (f);
21741 return FRAME_DESIRED_CURSOR (f);
21742 }
21743 else
21744 return get_specified_cursor_type (b->cursor_type, width);
21745 }
21746
21747 *active_cursor = 0;
21748 non_selected = 1;
21749 }
21750
21751 /* Detect a nonselected window or nonselected frame. */
21752 else if (w != XWINDOW (f->selected_window)
21753 #ifdef HAVE_WINDOW_SYSTEM
21754 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame
21755 #endif
21756 )
21757 {
21758 *active_cursor = 0;
21759
21760 if (MINI_WINDOW_P (w) && minibuf_level == 0)
21761 return NO_CURSOR;
21762
21763 non_selected = 1;
21764 }
21765
21766 /* Never display a cursor in a window in which cursor-type is nil. */
21767 if (NILP (b->cursor_type))
21768 return NO_CURSOR;
21769
21770 /* Get the normal cursor type for this window. */
21771 if (EQ (b->cursor_type, Qt))
21772 {
21773 cursor_type = FRAME_DESIRED_CURSOR (f);
21774 *width = FRAME_CURSOR_WIDTH (f);
21775 }
21776 else
21777 cursor_type = get_specified_cursor_type (b->cursor_type, width);
21778
21779 /* Use cursor-in-non-selected-windows instead
21780 for non-selected window or frame. */
21781 if (non_selected)
21782 {
21783 alt_cursor = b->cursor_in_non_selected_windows;
21784 if (!EQ (Qt, alt_cursor))
21785 return get_specified_cursor_type (alt_cursor, width);
21786 /* t means modify the normal cursor type. */
21787 if (cursor_type == FILLED_BOX_CURSOR)
21788 cursor_type = HOLLOW_BOX_CURSOR;
21789 else if (cursor_type == BAR_CURSOR && *width > 1)
21790 --*width;
21791 return cursor_type;
21792 }
21793
21794 /* Use normal cursor if not blinked off. */
21795 if (!w->cursor_off_p)
21796 {
21797 #ifdef HAVE_WINDOW_SYSTEM
21798 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
21799 {
21800 if (cursor_type == FILLED_BOX_CURSOR)
21801 {
21802 /* Using a block cursor on large images can be very annoying.
21803 So use a hollow cursor for "large" images.
21804 If image is not transparent (no mask), also use hollow cursor. */
21805 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
21806 if (img != NULL && IMAGEP (img->spec))
21807 {
21808 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
21809 where N = size of default frame font size.
21810 This should cover most of the "tiny" icons people may use. */
21811 if (!img->mask
21812 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
21813 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
21814 cursor_type = HOLLOW_BOX_CURSOR;
21815 }
21816 }
21817 else if (cursor_type != NO_CURSOR)
21818 {
21819 /* Display current only supports BOX and HOLLOW cursors for images.
21820 So for now, unconditionally use a HOLLOW cursor when cursor is
21821 not a solid box cursor. */
21822 cursor_type = HOLLOW_BOX_CURSOR;
21823 }
21824 }
21825 #endif
21826 return cursor_type;
21827 }
21828
21829 /* Cursor is blinked off, so determine how to "toggle" it. */
21830
21831 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
21832 if ((alt_cursor = Fassoc (b->cursor_type, Vblink_cursor_alist), !NILP (alt_cursor)))
21833 return get_specified_cursor_type (XCDR (alt_cursor), width);
21834
21835 /* Then see if frame has specified a specific blink off cursor type. */
21836 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
21837 {
21838 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
21839 return FRAME_BLINK_OFF_CURSOR (f);
21840 }
21841
21842 #if 0
21843 /* Some people liked having a permanently visible blinking cursor,
21844 while others had very strong opinions against it. So it was
21845 decided to remove it. KFS 2003-09-03 */
21846
21847 /* Finally perform built-in cursor blinking:
21848 filled box <-> hollow box
21849 wide [h]bar <-> narrow [h]bar
21850 narrow [h]bar <-> no cursor
21851 other type <-> no cursor */
21852
21853 if (cursor_type == FILLED_BOX_CURSOR)
21854 return HOLLOW_BOX_CURSOR;
21855
21856 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
21857 {
21858 *width = 1;
21859 return cursor_type;
21860 }
21861 #endif
21862
21863 return NO_CURSOR;
21864 }
21865
21866
21867 #ifdef HAVE_WINDOW_SYSTEM
21868
21869 /* Notice when the text cursor of window W has been completely
21870 overwritten by a drawing operation that outputs glyphs in AREA
21871 starting at X0 and ending at X1 in the line starting at Y0 and
21872 ending at Y1. X coordinates are area-relative. X1 < 0 means all
21873 the rest of the line after X0 has been written. Y coordinates
21874 are window-relative. */
21875
21876 static void
21877 notice_overwritten_cursor (w, area, x0, x1, y0, y1)
21878 struct window *w;
21879 enum glyph_row_area area;
21880 int x0, y0, x1, y1;
21881 {
21882 int cx0, cx1, cy0, cy1;
21883 struct glyph_row *row;
21884
21885 if (!w->phys_cursor_on_p)
21886 return;
21887 if (area != TEXT_AREA)
21888 return;
21889
21890 if (w->phys_cursor.vpos < 0
21891 || w->phys_cursor.vpos >= w->current_matrix->nrows
21892 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
21893 !(row->enabled_p && row->displays_text_p)))
21894 return;
21895
21896 if (row->cursor_in_fringe_p)
21897 {
21898 row->cursor_in_fringe_p = 0;
21899 draw_fringe_bitmap (w, row, 0);
21900 w->phys_cursor_on_p = 0;
21901 return;
21902 }
21903
21904 cx0 = w->phys_cursor.x;
21905 cx1 = cx0 + w->phys_cursor_width;
21906 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
21907 return;
21908
21909 /* The cursor image will be completely removed from the
21910 screen if the output area intersects the cursor area in
21911 y-direction. When we draw in [y0 y1[, and some part of
21912 the cursor is at y < y0, that part must have been drawn
21913 before. When scrolling, the cursor is erased before
21914 actually scrolling, so we don't come here. When not
21915 scrolling, the rows above the old cursor row must have
21916 changed, and in this case these rows must have written
21917 over the cursor image.
21918
21919 Likewise if part of the cursor is below y1, with the
21920 exception of the cursor being in the first blank row at
21921 the buffer and window end because update_text_area
21922 doesn't draw that row. (Except when it does, but
21923 that's handled in update_text_area.) */
21924
21925 cy0 = w->phys_cursor.y;
21926 cy1 = cy0 + w->phys_cursor_height;
21927 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
21928 return;
21929
21930 w->phys_cursor_on_p = 0;
21931 }
21932
21933 #endif /* HAVE_WINDOW_SYSTEM */
21934
21935 \f
21936 /************************************************************************
21937 Mouse Face
21938 ************************************************************************/
21939
21940 #ifdef HAVE_WINDOW_SYSTEM
21941
21942 /* EXPORT for RIF:
21943 Fix the display of area AREA of overlapping row ROW in window W
21944 with respect to the overlapping part OVERLAPS. */
21945
21946 void
21947 x_fix_overlapping_area (w, row, area, overlaps)
21948 struct window *w;
21949 struct glyph_row *row;
21950 enum glyph_row_area area;
21951 int overlaps;
21952 {
21953 int i, x;
21954
21955 BLOCK_INPUT;
21956
21957 x = 0;
21958 for (i = 0; i < row->used[area];)
21959 {
21960 if (row->glyphs[area][i].overlaps_vertically_p)
21961 {
21962 int start = i, start_x = x;
21963
21964 do
21965 {
21966 x += row->glyphs[area][i].pixel_width;
21967 ++i;
21968 }
21969 while (i < row->used[area]
21970 && row->glyphs[area][i].overlaps_vertically_p);
21971
21972 draw_glyphs (w, start_x, row, area,
21973 start, i,
21974 DRAW_NORMAL_TEXT, overlaps);
21975 }
21976 else
21977 {
21978 x += row->glyphs[area][i].pixel_width;
21979 ++i;
21980 }
21981 }
21982
21983 UNBLOCK_INPUT;
21984 }
21985
21986
21987 /* EXPORT:
21988 Draw the cursor glyph of window W in glyph row ROW. See the
21989 comment of draw_glyphs for the meaning of HL. */
21990
21991 void
21992 draw_phys_cursor_glyph (w, row, hl)
21993 struct window *w;
21994 struct glyph_row *row;
21995 enum draw_glyphs_face hl;
21996 {
21997 /* If cursor hpos is out of bounds, don't draw garbage. This can
21998 happen in mini-buffer windows when switching between echo area
21999 glyphs and mini-buffer. */
22000 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
22001 {
22002 int on_p = w->phys_cursor_on_p;
22003 int x1;
22004 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
22005 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
22006 hl, 0);
22007 w->phys_cursor_on_p = on_p;
22008
22009 if (hl == DRAW_CURSOR)
22010 w->phys_cursor_width = x1 - w->phys_cursor.x;
22011 /* When we erase the cursor, and ROW is overlapped by other
22012 rows, make sure that these overlapping parts of other rows
22013 are redrawn. */
22014 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
22015 {
22016 w->phys_cursor_width = x1 - w->phys_cursor.x;
22017
22018 if (row > w->current_matrix->rows
22019 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
22020 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
22021 OVERLAPS_ERASED_CURSOR);
22022
22023 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
22024 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
22025 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
22026 OVERLAPS_ERASED_CURSOR);
22027 }
22028 }
22029 }
22030
22031
22032 /* EXPORT:
22033 Erase the image of a cursor of window W from the screen. */
22034
22035 void
22036 erase_phys_cursor (w)
22037 struct window *w;
22038 {
22039 struct frame *f = XFRAME (w->frame);
22040 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
22041 int hpos = w->phys_cursor.hpos;
22042 int vpos = w->phys_cursor.vpos;
22043 int mouse_face_here_p = 0;
22044 struct glyph_matrix *active_glyphs = w->current_matrix;
22045 struct glyph_row *cursor_row;
22046 struct glyph *cursor_glyph;
22047 enum draw_glyphs_face hl;
22048
22049 /* No cursor displayed or row invalidated => nothing to do on the
22050 screen. */
22051 if (w->phys_cursor_type == NO_CURSOR)
22052 goto mark_cursor_off;
22053
22054 /* VPOS >= active_glyphs->nrows means that window has been resized.
22055 Don't bother to erase the cursor. */
22056 if (vpos >= active_glyphs->nrows)
22057 goto mark_cursor_off;
22058
22059 /* If row containing cursor is marked invalid, there is nothing we
22060 can do. */
22061 cursor_row = MATRIX_ROW (active_glyphs, vpos);
22062 if (!cursor_row->enabled_p)
22063 goto mark_cursor_off;
22064
22065 /* If line spacing is > 0, old cursor may only be partially visible in
22066 window after split-window. So adjust visible height. */
22067 cursor_row->visible_height = min (cursor_row->visible_height,
22068 window_text_bottom_y (w) - cursor_row->y);
22069
22070 /* If row is completely invisible, don't attempt to delete a cursor which
22071 isn't there. This can happen if cursor is at top of a window, and
22072 we switch to a buffer with a header line in that window. */
22073 if (cursor_row->visible_height <= 0)
22074 goto mark_cursor_off;
22075
22076 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
22077 if (cursor_row->cursor_in_fringe_p)
22078 {
22079 cursor_row->cursor_in_fringe_p = 0;
22080 draw_fringe_bitmap (w, cursor_row, 0);
22081 goto mark_cursor_off;
22082 }
22083
22084 /* This can happen when the new row is shorter than the old one.
22085 In this case, either draw_glyphs or clear_end_of_line
22086 should have cleared the cursor. Note that we wouldn't be
22087 able to erase the cursor in this case because we don't have a
22088 cursor glyph at hand. */
22089 if (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])
22090 goto mark_cursor_off;
22091
22092 /* If the cursor is in the mouse face area, redisplay that when
22093 we clear the cursor. */
22094 if (! NILP (dpyinfo->mouse_face_window)
22095 && w == XWINDOW (dpyinfo->mouse_face_window)
22096 && (vpos > dpyinfo->mouse_face_beg_row
22097 || (vpos == dpyinfo->mouse_face_beg_row
22098 && hpos >= dpyinfo->mouse_face_beg_col))
22099 && (vpos < dpyinfo->mouse_face_end_row
22100 || (vpos == dpyinfo->mouse_face_end_row
22101 && hpos < dpyinfo->mouse_face_end_col))
22102 /* Don't redraw the cursor's spot in mouse face if it is at the
22103 end of a line (on a newline). The cursor appears there, but
22104 mouse highlighting does not. */
22105 && cursor_row->used[TEXT_AREA] > hpos)
22106 mouse_face_here_p = 1;
22107
22108 /* Maybe clear the display under the cursor. */
22109 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
22110 {
22111 int x, y, left_x;
22112 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
22113 int width;
22114
22115 cursor_glyph = get_phys_cursor_glyph (w);
22116 if (cursor_glyph == NULL)
22117 goto mark_cursor_off;
22118
22119 width = cursor_glyph->pixel_width;
22120 left_x = window_box_left_offset (w, TEXT_AREA);
22121 x = w->phys_cursor.x;
22122 if (x < left_x)
22123 width -= left_x - x;
22124 width = min (width, window_box_width (w, TEXT_AREA) - x);
22125 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
22126 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, max (x, left_x));
22127
22128 if (width > 0)
22129 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
22130 }
22131
22132 /* Erase the cursor by redrawing the character underneath it. */
22133 if (mouse_face_here_p)
22134 hl = DRAW_MOUSE_FACE;
22135 else
22136 hl = DRAW_NORMAL_TEXT;
22137 draw_phys_cursor_glyph (w, cursor_row, hl);
22138
22139 mark_cursor_off:
22140 w->phys_cursor_on_p = 0;
22141 w->phys_cursor_type = NO_CURSOR;
22142 }
22143
22144
22145 /* EXPORT:
22146 Display or clear cursor of window W. If ON is zero, clear the
22147 cursor. If it is non-zero, display the cursor. If ON is nonzero,
22148 where to put the cursor is specified by HPOS, VPOS, X and Y. */
22149
22150 void
22151 display_and_set_cursor (w, on, hpos, vpos, x, y)
22152 struct window *w;
22153 int on, hpos, vpos, x, y;
22154 {
22155 struct frame *f = XFRAME (w->frame);
22156 int new_cursor_type;
22157 int new_cursor_width;
22158 int active_cursor;
22159 struct glyph_row *glyph_row;
22160 struct glyph *glyph;
22161
22162 /* This is pointless on invisible frames, and dangerous on garbaged
22163 windows and frames; in the latter case, the frame or window may
22164 be in the midst of changing its size, and x and y may be off the
22165 window. */
22166 if (! FRAME_VISIBLE_P (f)
22167 || FRAME_GARBAGED_P (f)
22168 || vpos >= w->current_matrix->nrows
22169 || hpos >= w->current_matrix->matrix_w)
22170 return;
22171
22172 /* If cursor is off and we want it off, return quickly. */
22173 if (!on && !w->phys_cursor_on_p)
22174 return;
22175
22176 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
22177 /* If cursor row is not enabled, we don't really know where to
22178 display the cursor. */
22179 if (!glyph_row->enabled_p)
22180 {
22181 w->phys_cursor_on_p = 0;
22182 return;
22183 }
22184
22185 glyph = NULL;
22186 if (!glyph_row->exact_window_width_line_p
22187 || hpos < glyph_row->used[TEXT_AREA])
22188 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
22189
22190 xassert (interrupt_input_blocked);
22191
22192 /* Set new_cursor_type to the cursor we want to be displayed. */
22193 new_cursor_type = get_window_cursor_type (w, glyph,
22194 &new_cursor_width, &active_cursor);
22195
22196 /* If cursor is currently being shown and we don't want it to be or
22197 it is in the wrong place, or the cursor type is not what we want,
22198 erase it. */
22199 if (w->phys_cursor_on_p
22200 && (!on
22201 || w->phys_cursor.x != x
22202 || w->phys_cursor.y != y
22203 || new_cursor_type != w->phys_cursor_type
22204 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
22205 && new_cursor_width != w->phys_cursor_width)))
22206 erase_phys_cursor (w);
22207
22208 /* Don't check phys_cursor_on_p here because that flag is only set
22209 to zero in some cases where we know that the cursor has been
22210 completely erased, to avoid the extra work of erasing the cursor
22211 twice. In other words, phys_cursor_on_p can be 1 and the cursor
22212 still not be visible, or it has only been partly erased. */
22213 if (on)
22214 {
22215 w->phys_cursor_ascent = glyph_row->ascent;
22216 w->phys_cursor_height = glyph_row->height;
22217
22218 /* Set phys_cursor_.* before x_draw_.* is called because some
22219 of them may need the information. */
22220 w->phys_cursor.x = x;
22221 w->phys_cursor.y = glyph_row->y;
22222 w->phys_cursor.hpos = hpos;
22223 w->phys_cursor.vpos = vpos;
22224 }
22225
22226 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
22227 new_cursor_type, new_cursor_width,
22228 on, active_cursor);
22229 }
22230
22231
22232 /* Switch the display of W's cursor on or off, according to the value
22233 of ON. */
22234
22235 static void
22236 update_window_cursor (w, on)
22237 struct window *w;
22238 int on;
22239 {
22240 /* Don't update cursor in windows whose frame is in the process
22241 of being deleted. */
22242 if (w->current_matrix)
22243 {
22244 BLOCK_INPUT;
22245 display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
22246 w->phys_cursor.x, w->phys_cursor.y);
22247 UNBLOCK_INPUT;
22248 }
22249 }
22250
22251
22252 /* Call update_window_cursor with parameter ON_P on all leaf windows
22253 in the window tree rooted at W. */
22254
22255 static void
22256 update_cursor_in_window_tree (w, on_p)
22257 struct window *w;
22258 int on_p;
22259 {
22260 while (w)
22261 {
22262 if (!NILP (w->hchild))
22263 update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
22264 else if (!NILP (w->vchild))
22265 update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
22266 else
22267 update_window_cursor (w, on_p);
22268
22269 w = NILP (w->next) ? 0 : XWINDOW (w->next);
22270 }
22271 }
22272
22273
22274 /* EXPORT:
22275 Display the cursor on window W, or clear it, according to ON_P.
22276 Don't change the cursor's position. */
22277
22278 void
22279 x_update_cursor (f, on_p)
22280 struct frame *f;
22281 int on_p;
22282 {
22283 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
22284 }
22285
22286
22287 /* EXPORT:
22288 Clear the cursor of window W to background color, and mark the
22289 cursor as not shown. This is used when the text where the cursor
22290 is is about to be rewritten. */
22291
22292 void
22293 x_clear_cursor (w)
22294 struct window *w;
22295 {
22296 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
22297 update_window_cursor (w, 0);
22298 }
22299
22300
22301 /* EXPORT:
22302 Display the active region described by mouse_face_* according to DRAW. */
22303
22304 void
22305 show_mouse_face (dpyinfo, draw)
22306 Display_Info *dpyinfo;
22307 enum draw_glyphs_face draw;
22308 {
22309 struct window *w = XWINDOW (dpyinfo->mouse_face_window);
22310 struct frame *f = XFRAME (WINDOW_FRAME (w));
22311
22312 if (/* If window is in the process of being destroyed, don't bother
22313 to do anything. */
22314 w->current_matrix != NULL
22315 /* Don't update mouse highlight if hidden */
22316 && (draw != DRAW_MOUSE_FACE || !dpyinfo->mouse_face_hidden)
22317 /* Recognize when we are called to operate on rows that don't exist
22318 anymore. This can happen when a window is split. */
22319 && dpyinfo->mouse_face_end_row < w->current_matrix->nrows)
22320 {
22321 int phys_cursor_on_p = w->phys_cursor_on_p;
22322 struct glyph_row *row, *first, *last;
22323
22324 first = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_beg_row);
22325 last = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_end_row);
22326
22327 for (row = first; row <= last && row->enabled_p; ++row)
22328 {
22329 int start_hpos, end_hpos, start_x;
22330
22331 /* For all but the first row, the highlight starts at column 0. */
22332 if (row == first)
22333 {
22334 start_hpos = dpyinfo->mouse_face_beg_col;
22335 start_x = dpyinfo->mouse_face_beg_x;
22336 }
22337 else
22338 {
22339 start_hpos = 0;
22340 start_x = 0;
22341 }
22342
22343 if (row == last)
22344 end_hpos = dpyinfo->mouse_face_end_col;
22345 else
22346 {
22347 end_hpos = row->used[TEXT_AREA];
22348 if (draw == DRAW_NORMAL_TEXT)
22349 row->fill_line_p = 1; /* Clear to end of line */
22350 }
22351
22352 if (end_hpos > start_hpos)
22353 {
22354 draw_glyphs (w, start_x, row, TEXT_AREA,
22355 start_hpos, end_hpos,
22356 draw, 0);
22357
22358 row->mouse_face_p
22359 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
22360 }
22361 }
22362
22363 /* When we've written over the cursor, arrange for it to
22364 be displayed again. */
22365 if (phys_cursor_on_p && !w->phys_cursor_on_p)
22366 {
22367 BLOCK_INPUT;
22368 display_and_set_cursor (w, 1,
22369 w->phys_cursor.hpos, w->phys_cursor.vpos,
22370 w->phys_cursor.x, w->phys_cursor.y);
22371 UNBLOCK_INPUT;
22372 }
22373 }
22374
22375 /* Change the mouse cursor. */
22376 if (draw == DRAW_NORMAL_TEXT && !EQ (dpyinfo->mouse_face_window, f->tool_bar_window))
22377 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
22378 else if (draw == DRAW_MOUSE_FACE)
22379 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
22380 else
22381 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
22382 }
22383
22384 /* EXPORT:
22385 Clear out the mouse-highlighted active region.
22386 Redraw it un-highlighted first. Value is non-zero if mouse
22387 face was actually drawn unhighlighted. */
22388
22389 int
22390 clear_mouse_face (dpyinfo)
22391 Display_Info *dpyinfo;
22392 {
22393 int cleared = 0;
22394
22395 if (!dpyinfo->mouse_face_hidden && !NILP (dpyinfo->mouse_face_window))
22396 {
22397 show_mouse_face (dpyinfo, DRAW_NORMAL_TEXT);
22398 cleared = 1;
22399 }
22400
22401 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
22402 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
22403 dpyinfo->mouse_face_window = Qnil;
22404 dpyinfo->mouse_face_overlay = Qnil;
22405 return cleared;
22406 }
22407
22408
22409 /* EXPORT:
22410 Non-zero if physical cursor of window W is within mouse face. */
22411
22412 int
22413 cursor_in_mouse_face_p (w)
22414 struct window *w;
22415 {
22416 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
22417 int in_mouse_face = 0;
22418
22419 if (WINDOWP (dpyinfo->mouse_face_window)
22420 && XWINDOW (dpyinfo->mouse_face_window) == w)
22421 {
22422 int hpos = w->phys_cursor.hpos;
22423 int vpos = w->phys_cursor.vpos;
22424
22425 if (vpos >= dpyinfo->mouse_face_beg_row
22426 && vpos <= dpyinfo->mouse_face_end_row
22427 && (vpos > dpyinfo->mouse_face_beg_row
22428 || hpos >= dpyinfo->mouse_face_beg_col)
22429 && (vpos < dpyinfo->mouse_face_end_row
22430 || hpos < dpyinfo->mouse_face_end_col
22431 || dpyinfo->mouse_face_past_end))
22432 in_mouse_face = 1;
22433 }
22434
22435 return in_mouse_face;
22436 }
22437
22438
22439
22440 \f
22441 /* Find the glyph matrix position of buffer position CHARPOS in window
22442 *W. HPOS, *VPOS, *X, and *Y are set to the positions found. W's
22443 current glyphs must be up to date. If CHARPOS is above window
22444 start return (0, 0, 0, 0). If CHARPOS is after end of W, return end
22445 of last line in W. In the row containing CHARPOS, stop before glyphs
22446 having STOP as object. */
22447
22448 #if 1 /* This is a version of fast_find_position that's more correct
22449 in the presence of hscrolling, for example. I didn't install
22450 it right away because the problem fixed is minor, it failed
22451 in 20.x as well, and I think it's too risky to install
22452 so near the release of 21.1. 2001-09-25 gerd. */
22453
22454 static int
22455 fast_find_position (w, charpos, hpos, vpos, x, y, stop)
22456 struct window *w;
22457 EMACS_INT charpos;
22458 int *hpos, *vpos, *x, *y;
22459 Lisp_Object stop;
22460 {
22461 struct glyph_row *row, *first;
22462 struct glyph *glyph, *end;
22463 int past_end = 0;
22464
22465 first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
22466 if (charpos < MATRIX_ROW_START_CHARPOS (first))
22467 {
22468 *x = first->x;
22469 *y = first->y;
22470 *hpos = 0;
22471 *vpos = MATRIX_ROW_VPOS (first, w->current_matrix);
22472 return 1;
22473 }
22474
22475 row = row_containing_pos (w, charpos, first, NULL, 0);
22476 if (row == NULL)
22477 {
22478 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
22479 past_end = 1;
22480 }
22481
22482 /* If whole rows or last part of a row came from a display overlay,
22483 row_containing_pos will skip over such rows because their end pos
22484 equals the start pos of the overlay or interval.
22485
22486 Move back if we have a STOP object and previous row's
22487 end glyph came from STOP. */
22488 if (!NILP (stop))
22489 {
22490 struct glyph_row *prev;
22491 while ((prev = row - 1, prev >= first)
22492 && MATRIX_ROW_END_CHARPOS (prev) == charpos
22493 && prev->used[TEXT_AREA] > 0)
22494 {
22495 struct glyph *beg = prev->glyphs[TEXT_AREA];
22496 glyph = beg + prev->used[TEXT_AREA];
22497 while (--glyph >= beg
22498 && INTEGERP (glyph->object));
22499 if (glyph < beg
22500 || !EQ (stop, glyph->object))
22501 break;
22502 row = prev;
22503 }
22504 }
22505
22506 *x = row->x;
22507 *y = row->y;
22508 *vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
22509
22510 glyph = row->glyphs[TEXT_AREA];
22511 end = glyph + row->used[TEXT_AREA];
22512
22513 /* Skip over glyphs not having an object at the start of the row.
22514 These are special glyphs like truncation marks on terminal
22515 frames. */
22516 if (row->displays_text_p)
22517 while (glyph < end
22518 && INTEGERP (glyph->object)
22519 && !EQ (stop, glyph->object)
22520 && glyph->charpos < 0)
22521 {
22522 *x += glyph->pixel_width;
22523 ++glyph;
22524 }
22525
22526 while (glyph < end
22527 && !INTEGERP (glyph->object)
22528 && !EQ (stop, glyph->object)
22529 && (!BUFFERP (glyph->object)
22530 || glyph->charpos < charpos))
22531 {
22532 *x += glyph->pixel_width;
22533 ++glyph;
22534 }
22535
22536 *hpos = glyph - row->glyphs[TEXT_AREA];
22537 return !past_end;
22538 }
22539
22540 #else /* not 1 */
22541
22542 static int
22543 fast_find_position (w, pos, hpos, vpos, x, y, stop)
22544 struct window *w;
22545 EMACS_INT pos;
22546 int *hpos, *vpos, *x, *y;
22547 Lisp_Object stop;
22548 {
22549 int i;
22550 int lastcol;
22551 int maybe_next_line_p = 0;
22552 int line_start_position;
22553 int yb = window_text_bottom_y (w);
22554 struct glyph_row *row, *best_row;
22555 int row_vpos, best_row_vpos;
22556 int current_x;
22557
22558 row = best_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
22559 row_vpos = best_row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
22560
22561 while (row->y < yb)
22562 {
22563 if (row->used[TEXT_AREA])
22564 line_start_position = row->glyphs[TEXT_AREA]->charpos;
22565 else
22566 line_start_position = 0;
22567
22568 if (line_start_position > pos)
22569 break;
22570 /* If the position sought is the end of the buffer,
22571 don't include the blank lines at the bottom of the window. */
22572 else if (line_start_position == pos
22573 && pos == BUF_ZV (XBUFFER (w->buffer)))
22574 {
22575 maybe_next_line_p = 1;
22576 break;
22577 }
22578 else if (line_start_position > 0)
22579 {
22580 best_row = row;
22581 best_row_vpos = row_vpos;
22582 }
22583
22584 if (row->y + row->height >= yb)
22585 break;
22586
22587 ++row;
22588 ++row_vpos;
22589 }
22590
22591 /* Find the right column within BEST_ROW. */
22592 lastcol = 0;
22593 current_x = best_row->x;
22594 for (i = 0; i < best_row->used[TEXT_AREA]; i++)
22595 {
22596 struct glyph *glyph = best_row->glyphs[TEXT_AREA] + i;
22597 int charpos = glyph->charpos;
22598
22599 if (BUFFERP (glyph->object))
22600 {
22601 if (charpos == pos)
22602 {
22603 *hpos = i;
22604 *vpos = best_row_vpos;
22605 *x = current_x;
22606 *y = best_row->y;
22607 return 1;
22608 }
22609 else if (charpos > pos)
22610 break;
22611 }
22612 else if (EQ (glyph->object, stop))
22613 break;
22614
22615 if (charpos > 0)
22616 lastcol = i;
22617 current_x += glyph->pixel_width;
22618 }
22619
22620 /* If we're looking for the end of the buffer,
22621 and we didn't find it in the line we scanned,
22622 use the start of the following line. */
22623 if (maybe_next_line_p)
22624 {
22625 ++best_row;
22626 ++best_row_vpos;
22627 lastcol = 0;
22628 current_x = best_row->x;
22629 }
22630
22631 *vpos = best_row_vpos;
22632 *hpos = lastcol + 1;
22633 *x = current_x;
22634 *y = best_row->y;
22635 return 0;
22636 }
22637
22638 #endif /* not 1 */
22639
22640
22641 /* Find the position of the glyph for position POS in OBJECT in
22642 window W's current matrix, and return in *X, *Y the pixel
22643 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
22644
22645 RIGHT_P non-zero means return the position of the right edge of the
22646 glyph, RIGHT_P zero means return the left edge position.
22647
22648 If no glyph for POS exists in the matrix, return the position of
22649 the glyph with the next smaller position that is in the matrix, if
22650 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
22651 exists in the matrix, return the position of the glyph with the
22652 next larger position in OBJECT.
22653
22654 Value is non-zero if a glyph was found. */
22655
22656 static int
22657 fast_find_string_pos (w, pos, object, hpos, vpos, x, y, right_p)
22658 struct window *w;
22659 EMACS_INT pos;
22660 Lisp_Object object;
22661 int *hpos, *vpos, *x, *y;
22662 int right_p;
22663 {
22664 int yb = window_text_bottom_y (w);
22665 struct glyph_row *r;
22666 struct glyph *best_glyph = NULL;
22667 struct glyph_row *best_row = NULL;
22668 int best_x = 0;
22669
22670 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
22671 r->enabled_p && r->y < yb;
22672 ++r)
22673 {
22674 struct glyph *g = r->glyphs[TEXT_AREA];
22675 struct glyph *e = g + r->used[TEXT_AREA];
22676 int gx;
22677
22678 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
22679 if (EQ (g->object, object))
22680 {
22681 if (g->charpos == pos)
22682 {
22683 best_glyph = g;
22684 best_x = gx;
22685 best_row = r;
22686 goto found;
22687 }
22688 else if (best_glyph == NULL
22689 || ((eabs (g->charpos - pos)
22690 < eabs (best_glyph->charpos - pos))
22691 && (right_p
22692 ? g->charpos < pos
22693 : g->charpos > pos)))
22694 {
22695 best_glyph = g;
22696 best_x = gx;
22697 best_row = r;
22698 }
22699 }
22700 }
22701
22702 found:
22703
22704 if (best_glyph)
22705 {
22706 *x = best_x;
22707 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
22708
22709 if (right_p)
22710 {
22711 *x += best_glyph->pixel_width;
22712 ++*hpos;
22713 }
22714
22715 *y = best_row->y;
22716 *vpos = best_row - w->current_matrix->rows;
22717 }
22718
22719 return best_glyph != NULL;
22720 }
22721
22722
22723 /* See if position X, Y is within a hot-spot of an image. */
22724
22725 static int
22726 on_hot_spot_p (hot_spot, x, y)
22727 Lisp_Object hot_spot;
22728 int x, y;
22729 {
22730 if (!CONSP (hot_spot))
22731 return 0;
22732
22733 if (EQ (XCAR (hot_spot), Qrect))
22734 {
22735 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
22736 Lisp_Object rect = XCDR (hot_spot);
22737 Lisp_Object tem;
22738 if (!CONSP (rect))
22739 return 0;
22740 if (!CONSP (XCAR (rect)))
22741 return 0;
22742 if (!CONSP (XCDR (rect)))
22743 return 0;
22744 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
22745 return 0;
22746 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
22747 return 0;
22748 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
22749 return 0;
22750 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
22751 return 0;
22752 return 1;
22753 }
22754 else if (EQ (XCAR (hot_spot), Qcircle))
22755 {
22756 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
22757 Lisp_Object circ = XCDR (hot_spot);
22758 Lisp_Object lr, lx0, ly0;
22759 if (CONSP (circ)
22760 && CONSP (XCAR (circ))
22761 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
22762 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
22763 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
22764 {
22765 double r = XFLOATINT (lr);
22766 double dx = XINT (lx0) - x;
22767 double dy = XINT (ly0) - y;
22768 return (dx * dx + dy * dy <= r * r);
22769 }
22770 }
22771 else if (EQ (XCAR (hot_spot), Qpoly))
22772 {
22773 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
22774 if (VECTORP (XCDR (hot_spot)))
22775 {
22776 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
22777 Lisp_Object *poly = v->contents;
22778 int n = v->size;
22779 int i;
22780 int inside = 0;
22781 Lisp_Object lx, ly;
22782 int x0, y0;
22783
22784 /* Need an even number of coordinates, and at least 3 edges. */
22785 if (n < 6 || n & 1)
22786 return 0;
22787
22788 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
22789 If count is odd, we are inside polygon. Pixels on edges
22790 may or may not be included depending on actual geometry of the
22791 polygon. */
22792 if ((lx = poly[n-2], !INTEGERP (lx))
22793 || (ly = poly[n-1], !INTEGERP (lx)))
22794 return 0;
22795 x0 = XINT (lx), y0 = XINT (ly);
22796 for (i = 0; i < n; i += 2)
22797 {
22798 int x1 = x0, y1 = y0;
22799 if ((lx = poly[i], !INTEGERP (lx))
22800 || (ly = poly[i+1], !INTEGERP (ly)))
22801 return 0;
22802 x0 = XINT (lx), y0 = XINT (ly);
22803
22804 /* Does this segment cross the X line? */
22805 if (x0 >= x)
22806 {
22807 if (x1 >= x)
22808 continue;
22809 }
22810 else if (x1 < x)
22811 continue;
22812 if (y > y0 && y > y1)
22813 continue;
22814 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
22815 inside = !inside;
22816 }
22817 return inside;
22818 }
22819 }
22820 return 0;
22821 }
22822
22823 Lisp_Object
22824 find_hot_spot (map, x, y)
22825 Lisp_Object map;
22826 int x, y;
22827 {
22828 while (CONSP (map))
22829 {
22830 if (CONSP (XCAR (map))
22831 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
22832 return XCAR (map);
22833 map = XCDR (map);
22834 }
22835
22836 return Qnil;
22837 }
22838
22839 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
22840 3, 3, 0,
22841 doc: /* Lookup in image map MAP coordinates X and Y.
22842 An image map is an alist where each element has the format (AREA ID PLIST).
22843 An AREA is specified as either a rectangle, a circle, or a polygon:
22844 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
22845 pixel coordinates of the upper left and bottom right corners.
22846 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
22847 and the radius of the circle; r may be a float or integer.
22848 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
22849 vector describes one corner in the polygon.
22850 Returns the alist element for the first matching AREA in MAP. */)
22851 (map, x, y)
22852 Lisp_Object map;
22853 Lisp_Object x, y;
22854 {
22855 if (NILP (map))
22856 return Qnil;
22857
22858 CHECK_NUMBER (x);
22859 CHECK_NUMBER (y);
22860
22861 return find_hot_spot (map, XINT (x), XINT (y));
22862 }
22863
22864
22865 /* Display frame CURSOR, optionally using shape defined by POINTER. */
22866 static void
22867 define_frame_cursor1 (f, cursor, pointer)
22868 struct frame *f;
22869 Cursor cursor;
22870 Lisp_Object pointer;
22871 {
22872 /* Do not change cursor shape while dragging mouse. */
22873 if (!NILP (do_mouse_tracking))
22874 return;
22875
22876 if (!NILP (pointer))
22877 {
22878 if (EQ (pointer, Qarrow))
22879 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
22880 else if (EQ (pointer, Qhand))
22881 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
22882 else if (EQ (pointer, Qtext))
22883 cursor = FRAME_X_OUTPUT (f)->text_cursor;
22884 else if (EQ (pointer, intern ("hdrag")))
22885 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
22886 #ifdef HAVE_X_WINDOWS
22887 else if (EQ (pointer, intern ("vdrag")))
22888 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
22889 #endif
22890 else if (EQ (pointer, intern ("hourglass")))
22891 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
22892 else if (EQ (pointer, Qmodeline))
22893 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
22894 else
22895 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
22896 }
22897
22898 if (cursor != No_Cursor)
22899 FRAME_RIF (f)->define_frame_cursor (f, cursor);
22900 }
22901
22902 /* Take proper action when mouse has moved to the mode or header line
22903 or marginal area AREA of window W, x-position X and y-position Y.
22904 X is relative to the start of the text display area of W, so the
22905 width of bitmap areas and scroll bars must be subtracted to get a
22906 position relative to the start of the mode line. */
22907
22908 static void
22909 note_mode_line_or_margin_highlight (window, x, y, area)
22910 Lisp_Object window;
22911 int x, y;
22912 enum window_part area;
22913 {
22914 struct window *w = XWINDOW (window);
22915 struct frame *f = XFRAME (w->frame);
22916 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
22917 Cursor cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
22918 Lisp_Object pointer = Qnil;
22919 int charpos, dx, dy, width, height;
22920 Lisp_Object string, object = Qnil;
22921 Lisp_Object pos, help;
22922
22923 Lisp_Object mouse_face;
22924 int original_x_pixel = x;
22925 struct glyph * glyph = NULL, * row_start_glyph = NULL;
22926 struct glyph_row *row;
22927
22928 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
22929 {
22930 int x0;
22931 struct glyph *end;
22932
22933 string = mode_line_string (w, area, &x, &y, &charpos,
22934 &object, &dx, &dy, &width, &height);
22935
22936 row = (area == ON_MODE_LINE
22937 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
22938 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
22939
22940 /* Find glyph */
22941 if (row->mode_line_p && row->enabled_p)
22942 {
22943 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
22944 end = glyph + row->used[TEXT_AREA];
22945
22946 for (x0 = original_x_pixel;
22947 glyph < end && x0 >= glyph->pixel_width;
22948 ++glyph)
22949 x0 -= glyph->pixel_width;
22950
22951 if (glyph >= end)
22952 glyph = NULL;
22953 }
22954 }
22955 else
22956 {
22957 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
22958 string = marginal_area_string (w, area, &x, &y, &charpos,
22959 &object, &dx, &dy, &width, &height);
22960 }
22961
22962 help = Qnil;
22963
22964 if (IMAGEP (object))
22965 {
22966 Lisp_Object image_map, hotspot;
22967 if ((image_map = Fplist_get (XCDR (object), QCmap),
22968 !NILP (image_map))
22969 && (hotspot = find_hot_spot (image_map, dx, dy),
22970 CONSP (hotspot))
22971 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
22972 {
22973 Lisp_Object area_id, plist;
22974
22975 area_id = XCAR (hotspot);
22976 /* Could check AREA_ID to see if we enter/leave this hot-spot.
22977 If so, we could look for mouse-enter, mouse-leave
22978 properties in PLIST (and do something...). */
22979 hotspot = XCDR (hotspot);
22980 if (CONSP (hotspot)
22981 && (plist = XCAR (hotspot), CONSP (plist)))
22982 {
22983 pointer = Fplist_get (plist, Qpointer);
22984 if (NILP (pointer))
22985 pointer = Qhand;
22986 help = Fplist_get (plist, Qhelp_echo);
22987 if (!NILP (help))
22988 {
22989 help_echo_string = help;
22990 /* Is this correct? ++kfs */
22991 XSETWINDOW (help_echo_window, w);
22992 help_echo_object = w->buffer;
22993 help_echo_pos = charpos;
22994 }
22995 }
22996 }
22997 if (NILP (pointer))
22998 pointer = Fplist_get (XCDR (object), QCpointer);
22999 }
23000
23001 if (STRINGP (string))
23002 {
23003 pos = make_number (charpos);
23004 /* If we're on a string with `help-echo' text property, arrange
23005 for the help to be displayed. This is done by setting the
23006 global variable help_echo_string to the help string. */
23007 if (NILP (help))
23008 {
23009 help = Fget_text_property (pos, Qhelp_echo, string);
23010 if (!NILP (help))
23011 {
23012 help_echo_string = help;
23013 XSETWINDOW (help_echo_window, w);
23014 help_echo_object = string;
23015 help_echo_pos = charpos;
23016 }
23017 }
23018
23019 if (NILP (pointer))
23020 pointer = Fget_text_property (pos, Qpointer, string);
23021
23022 /* Change the mouse pointer according to what is under X/Y. */
23023 if (NILP (pointer) && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
23024 {
23025 Lisp_Object map;
23026 map = Fget_text_property (pos, Qlocal_map, string);
23027 if (!KEYMAPP (map))
23028 map = Fget_text_property (pos, Qkeymap, string);
23029 if (!KEYMAPP (map))
23030 cursor = dpyinfo->vertical_scroll_bar_cursor;
23031 }
23032
23033 /* Change the mouse face according to what is under X/Y. */
23034 mouse_face = Fget_text_property (pos, Qmouse_face, string);
23035 if (!NILP (mouse_face)
23036 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
23037 && glyph)
23038 {
23039 Lisp_Object b, e;
23040
23041 struct glyph * tmp_glyph;
23042
23043 int gpos;
23044 int gseq_length;
23045 int total_pixel_width;
23046 int ignore;
23047
23048 int vpos, hpos;
23049
23050 b = Fprevious_single_property_change (make_number (charpos + 1),
23051 Qmouse_face, string, Qnil);
23052 if (NILP (b))
23053 b = make_number (0);
23054
23055 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
23056 if (NILP (e))
23057 e = make_number (SCHARS (string));
23058
23059 /* Calculate the position(glyph position: GPOS) of GLYPH in
23060 displayed string. GPOS is different from CHARPOS.
23061
23062 CHARPOS is the position of glyph in internal string
23063 object. A mode line string format has structures which
23064 is converted to a flatten by emacs lisp interpreter.
23065 The internal string is an element of the structures.
23066 The displayed string is the flatten string. */
23067 gpos = 0;
23068 if (glyph > row_start_glyph)
23069 {
23070 tmp_glyph = glyph - 1;
23071 while (tmp_glyph >= row_start_glyph
23072 && tmp_glyph->charpos >= XINT (b)
23073 && EQ (tmp_glyph->object, glyph->object))
23074 {
23075 tmp_glyph--;
23076 gpos++;
23077 }
23078 }
23079
23080 /* Calculate the lenght(glyph sequence length: GSEQ_LENGTH) of
23081 displayed string holding GLYPH.
23082
23083 GSEQ_LENGTH is different from SCHARS (STRING).
23084 SCHARS (STRING) returns the length of the internal string. */
23085 for (tmp_glyph = glyph, gseq_length = gpos;
23086 tmp_glyph->charpos < XINT (e);
23087 tmp_glyph++, gseq_length++)
23088 {
23089 if (!EQ (tmp_glyph->object, glyph->object))
23090 break;
23091 }
23092
23093 total_pixel_width = 0;
23094 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
23095 total_pixel_width += tmp_glyph->pixel_width;
23096
23097 /* Pre calculation of re-rendering position */
23098 vpos = (x - gpos);
23099 hpos = (area == ON_MODE_LINE
23100 ? (w->current_matrix)->nrows - 1
23101 : 0);
23102
23103 /* If the re-rendering position is included in the last
23104 re-rendering area, we should do nothing. */
23105 if ( EQ (window, dpyinfo->mouse_face_window)
23106 && dpyinfo->mouse_face_beg_col <= vpos
23107 && vpos < dpyinfo->mouse_face_end_col
23108 && dpyinfo->mouse_face_beg_row == hpos )
23109 return;
23110
23111 if (clear_mouse_face (dpyinfo))
23112 cursor = No_Cursor;
23113
23114 dpyinfo->mouse_face_beg_col = vpos;
23115 dpyinfo->mouse_face_beg_row = hpos;
23116
23117 dpyinfo->mouse_face_beg_x = original_x_pixel - (total_pixel_width + dx);
23118 dpyinfo->mouse_face_beg_y = 0;
23119
23120 dpyinfo->mouse_face_end_col = vpos + gseq_length;
23121 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_beg_row;
23122
23123 dpyinfo->mouse_face_end_x = 0;
23124 dpyinfo->mouse_face_end_y = 0;
23125
23126 dpyinfo->mouse_face_past_end = 0;
23127 dpyinfo->mouse_face_window = window;
23128
23129 dpyinfo->mouse_face_face_id = face_at_string_position (w, string,
23130 charpos,
23131 0, 0, 0, &ignore,
23132 glyph->face_id, 1);
23133 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
23134
23135 if (NILP (pointer))
23136 pointer = Qhand;
23137 }
23138 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
23139 clear_mouse_face (dpyinfo);
23140 }
23141 define_frame_cursor1 (f, cursor, pointer);
23142 }
23143
23144
23145 /* EXPORT:
23146 Take proper action when the mouse has moved to position X, Y on
23147 frame F as regards highlighting characters that have mouse-face
23148 properties. Also de-highlighting chars where the mouse was before.
23149 X and Y can be negative or out of range. */
23150
23151 void
23152 note_mouse_highlight (f, x, y)
23153 struct frame *f;
23154 int x, y;
23155 {
23156 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
23157 enum window_part part;
23158 Lisp_Object window;
23159 struct window *w;
23160 Cursor cursor = No_Cursor;
23161 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
23162 struct buffer *b;
23163
23164 /* When a menu is active, don't highlight because this looks odd. */
23165 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (MAC_OS)
23166 if (popup_activated ())
23167 return;
23168 #endif
23169
23170 if (NILP (Vmouse_highlight)
23171 || !f->glyphs_initialized_p)
23172 return;
23173
23174 dpyinfo->mouse_face_mouse_x = x;
23175 dpyinfo->mouse_face_mouse_y = y;
23176 dpyinfo->mouse_face_mouse_frame = f;
23177
23178 if (dpyinfo->mouse_face_defer)
23179 return;
23180
23181 if (gc_in_progress)
23182 {
23183 dpyinfo->mouse_face_deferred_gc = 1;
23184 return;
23185 }
23186
23187 /* Which window is that in? */
23188 window = window_from_coordinates (f, x, y, &part, 0, 0, 1);
23189
23190 /* If we were displaying active text in another window, clear that.
23191 Also clear if we move out of text area in same window. */
23192 if (! EQ (window, dpyinfo->mouse_face_window)
23193 || (part != ON_TEXT && part != ON_MODE_LINE && part != ON_HEADER_LINE
23194 && !NILP (dpyinfo->mouse_face_window)))
23195 clear_mouse_face (dpyinfo);
23196
23197 /* Not on a window -> return. */
23198 if (!WINDOWP (window))
23199 return;
23200
23201 /* Reset help_echo_string. It will get recomputed below. */
23202 help_echo_string = Qnil;
23203
23204 /* Convert to window-relative pixel coordinates. */
23205 w = XWINDOW (window);
23206 frame_to_window_pixel_xy (w, &x, &y);
23207
23208 /* Handle tool-bar window differently since it doesn't display a
23209 buffer. */
23210 if (EQ (window, f->tool_bar_window))
23211 {
23212 note_tool_bar_highlight (f, x, y);
23213 return;
23214 }
23215
23216 /* Mouse is on the mode, header line or margin? */
23217 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
23218 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
23219 {
23220 note_mode_line_or_margin_highlight (window, x, y, part);
23221 return;
23222 }
23223
23224 if (part == ON_VERTICAL_BORDER)
23225 {
23226 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
23227 help_echo_string = build_string ("drag-mouse-1: resize");
23228 }
23229 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
23230 || part == ON_SCROLL_BAR)
23231 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
23232 else
23233 cursor = FRAME_X_OUTPUT (f)->text_cursor;
23234
23235 /* Are we in a window whose display is up to date?
23236 And verify the buffer's text has not changed. */
23237 b = XBUFFER (w->buffer);
23238 if (part == ON_TEXT
23239 && EQ (w->window_end_valid, w->buffer)
23240 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
23241 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
23242 {
23243 int hpos, vpos, pos, i, dx, dy, area;
23244 struct glyph *glyph;
23245 Lisp_Object object;
23246 Lisp_Object mouse_face = Qnil, overlay = Qnil, position;
23247 Lisp_Object *overlay_vec = NULL;
23248 int noverlays;
23249 struct buffer *obuf;
23250 int obegv, ozv, same_region;
23251
23252 /* Find the glyph under X/Y. */
23253 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
23254
23255 /* Look for :pointer property on image. */
23256 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
23257 {
23258 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
23259 if (img != NULL && IMAGEP (img->spec))
23260 {
23261 Lisp_Object image_map, hotspot;
23262 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
23263 !NILP (image_map))
23264 && (hotspot = find_hot_spot (image_map,
23265 glyph->slice.x + dx,
23266 glyph->slice.y + dy),
23267 CONSP (hotspot))
23268 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
23269 {
23270 Lisp_Object area_id, plist;
23271
23272 area_id = XCAR (hotspot);
23273 /* Could check AREA_ID to see if we enter/leave this hot-spot.
23274 If so, we could look for mouse-enter, mouse-leave
23275 properties in PLIST (and do something...). */
23276 hotspot = XCDR (hotspot);
23277 if (CONSP (hotspot)
23278 && (plist = XCAR (hotspot), CONSP (plist)))
23279 {
23280 pointer = Fplist_get (plist, Qpointer);
23281 if (NILP (pointer))
23282 pointer = Qhand;
23283 help_echo_string = Fplist_get (plist, Qhelp_echo);
23284 if (!NILP (help_echo_string))
23285 {
23286 help_echo_window = window;
23287 help_echo_object = glyph->object;
23288 help_echo_pos = glyph->charpos;
23289 }
23290 }
23291 }
23292 if (NILP (pointer))
23293 pointer = Fplist_get (XCDR (img->spec), QCpointer);
23294 }
23295 }
23296
23297 /* Clear mouse face if X/Y not over text. */
23298 if (glyph == NULL
23299 || area != TEXT_AREA
23300 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p)
23301 {
23302 if (clear_mouse_face (dpyinfo))
23303 cursor = No_Cursor;
23304 if (NILP (pointer))
23305 {
23306 if (area != TEXT_AREA)
23307 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
23308 else
23309 pointer = Vvoid_text_area_pointer;
23310 }
23311 goto set_cursor;
23312 }
23313
23314 pos = glyph->charpos;
23315 object = glyph->object;
23316 if (!STRINGP (object) && !BUFFERP (object))
23317 goto set_cursor;
23318
23319 /* If we get an out-of-range value, return now; avoid an error. */
23320 if (BUFFERP (object) && pos > BUF_Z (b))
23321 goto set_cursor;
23322
23323 /* Make the window's buffer temporarily current for
23324 overlays_at and compute_char_face. */
23325 obuf = current_buffer;
23326 current_buffer = b;
23327 obegv = BEGV;
23328 ozv = ZV;
23329 BEGV = BEG;
23330 ZV = Z;
23331
23332 /* Is this char mouse-active or does it have help-echo? */
23333 position = make_number (pos);
23334
23335 if (BUFFERP (object))
23336 {
23337 /* Put all the overlays we want in a vector in overlay_vec. */
23338 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
23339 /* Sort overlays into increasing priority order. */
23340 noverlays = sort_overlays (overlay_vec, noverlays, w);
23341 }
23342 else
23343 noverlays = 0;
23344
23345 same_region = (EQ (window, dpyinfo->mouse_face_window)
23346 && vpos >= dpyinfo->mouse_face_beg_row
23347 && vpos <= dpyinfo->mouse_face_end_row
23348 && (vpos > dpyinfo->mouse_face_beg_row
23349 || hpos >= dpyinfo->mouse_face_beg_col)
23350 && (vpos < dpyinfo->mouse_face_end_row
23351 || hpos < dpyinfo->mouse_face_end_col
23352 || dpyinfo->mouse_face_past_end));
23353
23354 if (same_region)
23355 cursor = No_Cursor;
23356
23357 /* Check mouse-face highlighting. */
23358 if (! same_region
23359 /* If there exists an overlay with mouse-face overlapping
23360 the one we are currently highlighting, we have to
23361 check if we enter the overlapping overlay, and then
23362 highlight only that. */
23363 || (OVERLAYP (dpyinfo->mouse_face_overlay)
23364 && mouse_face_overlay_overlaps (dpyinfo->mouse_face_overlay)))
23365 {
23366 /* Find the highest priority overlay that has a mouse-face
23367 property. */
23368 overlay = Qnil;
23369 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
23370 {
23371 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
23372 if (!NILP (mouse_face))
23373 overlay = overlay_vec[i];
23374 }
23375
23376 /* If we're actually highlighting the same overlay as
23377 before, there's no need to do that again. */
23378 if (!NILP (overlay)
23379 && EQ (overlay, dpyinfo->mouse_face_overlay))
23380 goto check_help_echo;
23381
23382 dpyinfo->mouse_face_overlay = overlay;
23383
23384 /* Clear the display of the old active region, if any. */
23385 if (clear_mouse_face (dpyinfo))
23386 cursor = No_Cursor;
23387
23388 /* If no overlay applies, get a text property. */
23389 if (NILP (overlay))
23390 mouse_face = Fget_text_property (position, Qmouse_face, object);
23391
23392 /* Handle the overlay case. */
23393 if (!NILP (overlay))
23394 {
23395 /* Find the range of text around this char that
23396 should be active. */
23397 Lisp_Object before, after;
23398 int ignore;
23399
23400 before = Foverlay_start (overlay);
23401 after = Foverlay_end (overlay);
23402 /* Record this as the current active region. */
23403 fast_find_position (w, XFASTINT (before),
23404 &dpyinfo->mouse_face_beg_col,
23405 &dpyinfo->mouse_face_beg_row,
23406 &dpyinfo->mouse_face_beg_x,
23407 &dpyinfo->mouse_face_beg_y, Qnil);
23408
23409 dpyinfo->mouse_face_past_end
23410 = !fast_find_position (w, XFASTINT (after),
23411 &dpyinfo->mouse_face_end_col,
23412 &dpyinfo->mouse_face_end_row,
23413 &dpyinfo->mouse_face_end_x,
23414 &dpyinfo->mouse_face_end_y, Qnil);
23415 dpyinfo->mouse_face_window = window;
23416
23417 dpyinfo->mouse_face_face_id
23418 = face_at_buffer_position (w, pos, 0, 0,
23419 &ignore, pos + 1,
23420 !dpyinfo->mouse_face_hidden);
23421
23422 /* Display it as active. */
23423 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
23424 cursor = No_Cursor;
23425 }
23426 /* Handle the text property case. */
23427 else if (!NILP (mouse_face) && BUFFERP (object))
23428 {
23429 /* Find the range of text around this char that
23430 should be active. */
23431 Lisp_Object before, after, beginning, end;
23432 int ignore;
23433
23434 beginning = Fmarker_position (w->start);
23435 end = make_number (BUF_Z (XBUFFER (object))
23436 - XFASTINT (w->window_end_pos));
23437 before
23438 = Fprevious_single_property_change (make_number (pos + 1),
23439 Qmouse_face,
23440 object, beginning);
23441 after
23442 = Fnext_single_property_change (position, Qmouse_face,
23443 object, end);
23444
23445 /* Record this as the current active region. */
23446 fast_find_position (w, XFASTINT (before),
23447 &dpyinfo->mouse_face_beg_col,
23448 &dpyinfo->mouse_face_beg_row,
23449 &dpyinfo->mouse_face_beg_x,
23450 &dpyinfo->mouse_face_beg_y, Qnil);
23451 dpyinfo->mouse_face_past_end
23452 = !fast_find_position (w, XFASTINT (after),
23453 &dpyinfo->mouse_face_end_col,
23454 &dpyinfo->mouse_face_end_row,
23455 &dpyinfo->mouse_face_end_x,
23456 &dpyinfo->mouse_face_end_y, Qnil);
23457 dpyinfo->mouse_face_window = window;
23458
23459 if (BUFFERP (object))
23460 dpyinfo->mouse_face_face_id
23461 = face_at_buffer_position (w, pos, 0, 0,
23462 &ignore, pos + 1,
23463 !dpyinfo->mouse_face_hidden);
23464
23465 /* Display it as active. */
23466 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
23467 cursor = No_Cursor;
23468 }
23469 else if (!NILP (mouse_face) && STRINGP (object))
23470 {
23471 Lisp_Object b, e;
23472 int ignore;
23473
23474 b = Fprevious_single_property_change (make_number (pos + 1),
23475 Qmouse_face,
23476 object, Qnil);
23477 e = Fnext_single_property_change (position, Qmouse_face,
23478 object, Qnil);
23479 if (NILP (b))
23480 b = make_number (0);
23481 if (NILP (e))
23482 e = make_number (SCHARS (object) - 1);
23483
23484 fast_find_string_pos (w, XINT (b), object,
23485 &dpyinfo->mouse_face_beg_col,
23486 &dpyinfo->mouse_face_beg_row,
23487 &dpyinfo->mouse_face_beg_x,
23488 &dpyinfo->mouse_face_beg_y, 0);
23489 fast_find_string_pos (w, XINT (e), object,
23490 &dpyinfo->mouse_face_end_col,
23491 &dpyinfo->mouse_face_end_row,
23492 &dpyinfo->mouse_face_end_x,
23493 &dpyinfo->mouse_face_end_y, 1);
23494 dpyinfo->mouse_face_past_end = 0;
23495 dpyinfo->mouse_face_window = window;
23496 dpyinfo->mouse_face_face_id
23497 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
23498 glyph->face_id, 1);
23499 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
23500 cursor = No_Cursor;
23501 }
23502 else if (STRINGP (object) && NILP (mouse_face))
23503 {
23504 /* A string which doesn't have mouse-face, but
23505 the text ``under'' it might have. */
23506 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
23507 int start = MATRIX_ROW_START_CHARPOS (r);
23508
23509 pos = string_buffer_position (w, object, start);
23510 if (pos > 0)
23511 mouse_face = get_char_property_and_overlay (make_number (pos),
23512 Qmouse_face,
23513 w->buffer,
23514 &overlay);
23515 if (!NILP (mouse_face) && !NILP (overlay))
23516 {
23517 Lisp_Object before = Foverlay_start (overlay);
23518 Lisp_Object after = Foverlay_end (overlay);
23519 int ignore;
23520
23521 /* Note that we might not be able to find position
23522 BEFORE in the glyph matrix if the overlay is
23523 entirely covered by a `display' property. In
23524 this case, we overshoot. So let's stop in
23525 the glyph matrix before glyphs for OBJECT. */
23526 fast_find_position (w, XFASTINT (before),
23527 &dpyinfo->mouse_face_beg_col,
23528 &dpyinfo->mouse_face_beg_row,
23529 &dpyinfo->mouse_face_beg_x,
23530 &dpyinfo->mouse_face_beg_y,
23531 object);
23532
23533 dpyinfo->mouse_face_past_end
23534 = !fast_find_position (w, XFASTINT (after),
23535 &dpyinfo->mouse_face_end_col,
23536 &dpyinfo->mouse_face_end_row,
23537 &dpyinfo->mouse_face_end_x,
23538 &dpyinfo->mouse_face_end_y,
23539 Qnil);
23540 dpyinfo->mouse_face_window = window;
23541 dpyinfo->mouse_face_face_id
23542 = face_at_buffer_position (w, pos, 0, 0,
23543 &ignore, pos + 1,
23544 !dpyinfo->mouse_face_hidden);
23545
23546 /* Display it as active. */
23547 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
23548 cursor = No_Cursor;
23549 }
23550 }
23551 }
23552
23553 check_help_echo:
23554
23555 /* Look for a `help-echo' property. */
23556 if (NILP (help_echo_string)) {
23557 Lisp_Object help, overlay;
23558
23559 /* Check overlays first. */
23560 help = overlay = Qnil;
23561 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
23562 {
23563 overlay = overlay_vec[i];
23564 help = Foverlay_get (overlay, Qhelp_echo);
23565 }
23566
23567 if (!NILP (help))
23568 {
23569 help_echo_string = help;
23570 help_echo_window = window;
23571 help_echo_object = overlay;
23572 help_echo_pos = pos;
23573 }
23574 else
23575 {
23576 Lisp_Object object = glyph->object;
23577 int charpos = glyph->charpos;
23578
23579 /* Try text properties. */
23580 if (STRINGP (object)
23581 && charpos >= 0
23582 && charpos < SCHARS (object))
23583 {
23584 help = Fget_text_property (make_number (charpos),
23585 Qhelp_echo, object);
23586 if (NILP (help))
23587 {
23588 /* If the string itself doesn't specify a help-echo,
23589 see if the buffer text ``under'' it does. */
23590 struct glyph_row *r
23591 = MATRIX_ROW (w->current_matrix, vpos);
23592 int start = MATRIX_ROW_START_CHARPOS (r);
23593 int pos = string_buffer_position (w, object, start);
23594 if (pos > 0)
23595 {
23596 help = Fget_char_property (make_number (pos),
23597 Qhelp_echo, w->buffer);
23598 if (!NILP (help))
23599 {
23600 charpos = pos;
23601 object = w->buffer;
23602 }
23603 }
23604 }
23605 }
23606 else if (BUFFERP (object)
23607 && charpos >= BEGV
23608 && charpos < ZV)
23609 help = Fget_text_property (make_number (charpos), Qhelp_echo,
23610 object);
23611
23612 if (!NILP (help))
23613 {
23614 help_echo_string = help;
23615 help_echo_window = window;
23616 help_echo_object = object;
23617 help_echo_pos = charpos;
23618 }
23619 }
23620 }
23621
23622 /* Look for a `pointer' property. */
23623 if (NILP (pointer))
23624 {
23625 /* Check overlays first. */
23626 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
23627 pointer = Foverlay_get (overlay_vec[i], Qpointer);
23628
23629 if (NILP (pointer))
23630 {
23631 Lisp_Object object = glyph->object;
23632 int charpos = glyph->charpos;
23633
23634 /* Try text properties. */
23635 if (STRINGP (object)
23636 && charpos >= 0
23637 && charpos < SCHARS (object))
23638 {
23639 pointer = Fget_text_property (make_number (charpos),
23640 Qpointer, object);
23641 if (NILP (pointer))
23642 {
23643 /* If the string itself doesn't specify a pointer,
23644 see if the buffer text ``under'' it does. */
23645 struct glyph_row *r
23646 = MATRIX_ROW (w->current_matrix, vpos);
23647 int start = MATRIX_ROW_START_CHARPOS (r);
23648 int pos = string_buffer_position (w, object, start);
23649 if (pos > 0)
23650 pointer = Fget_char_property (make_number (pos),
23651 Qpointer, w->buffer);
23652 }
23653 }
23654 else if (BUFFERP (object)
23655 && charpos >= BEGV
23656 && charpos < ZV)
23657 pointer = Fget_text_property (make_number (charpos),
23658 Qpointer, object);
23659 }
23660 }
23661
23662 BEGV = obegv;
23663 ZV = ozv;
23664 current_buffer = obuf;
23665 }
23666
23667 set_cursor:
23668
23669 define_frame_cursor1 (f, cursor, pointer);
23670 }
23671
23672
23673 /* EXPORT for RIF:
23674 Clear any mouse-face on window W. This function is part of the
23675 redisplay interface, and is called from try_window_id and similar
23676 functions to ensure the mouse-highlight is off. */
23677
23678 void
23679 x_clear_window_mouse_face (w)
23680 struct window *w;
23681 {
23682 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
23683 Lisp_Object window;
23684
23685 BLOCK_INPUT;
23686 XSETWINDOW (window, w);
23687 if (EQ (window, dpyinfo->mouse_face_window))
23688 clear_mouse_face (dpyinfo);
23689 UNBLOCK_INPUT;
23690 }
23691
23692
23693 /* EXPORT:
23694 Just discard the mouse face information for frame F, if any.
23695 This is used when the size of F is changed. */
23696
23697 void
23698 cancel_mouse_face (f)
23699 struct frame *f;
23700 {
23701 Lisp_Object window;
23702 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
23703
23704 window = dpyinfo->mouse_face_window;
23705 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
23706 {
23707 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
23708 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
23709 dpyinfo->mouse_face_window = Qnil;
23710 }
23711 }
23712
23713
23714 #endif /* HAVE_WINDOW_SYSTEM */
23715
23716 \f
23717 /***********************************************************************
23718 Exposure Events
23719 ***********************************************************************/
23720
23721 #ifdef HAVE_WINDOW_SYSTEM
23722
23723 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
23724 which intersects rectangle R. R is in window-relative coordinates. */
23725
23726 static void
23727 expose_area (w, row, r, area)
23728 struct window *w;
23729 struct glyph_row *row;
23730 XRectangle *r;
23731 enum glyph_row_area area;
23732 {
23733 struct glyph *first = row->glyphs[area];
23734 struct glyph *end = row->glyphs[area] + row->used[area];
23735 struct glyph *last;
23736 int first_x, start_x, x;
23737
23738 if (area == TEXT_AREA && row->fill_line_p)
23739 /* If row extends face to end of line write the whole line. */
23740 draw_glyphs (w, 0, row, area,
23741 0, row->used[area],
23742 DRAW_NORMAL_TEXT, 0);
23743 else
23744 {
23745 /* Set START_X to the window-relative start position for drawing glyphs of
23746 AREA. The first glyph of the text area can be partially visible.
23747 The first glyphs of other areas cannot. */
23748 start_x = window_box_left_offset (w, area);
23749 x = start_x;
23750 if (area == TEXT_AREA)
23751 x += row->x;
23752
23753 /* Find the first glyph that must be redrawn. */
23754 while (first < end
23755 && x + first->pixel_width < r->x)
23756 {
23757 x += first->pixel_width;
23758 ++first;
23759 }
23760
23761 /* Find the last one. */
23762 last = first;
23763 first_x = x;
23764 while (last < end
23765 && x < r->x + r->width)
23766 {
23767 x += last->pixel_width;
23768 ++last;
23769 }
23770
23771 /* Repaint. */
23772 if (last > first)
23773 draw_glyphs (w, first_x - start_x, row, area,
23774 first - row->glyphs[area], last - row->glyphs[area],
23775 DRAW_NORMAL_TEXT, 0);
23776 }
23777 }
23778
23779
23780 /* Redraw the parts of the glyph row ROW on window W intersecting
23781 rectangle R. R is in window-relative coordinates. Value is
23782 non-zero if mouse-face was overwritten. */
23783
23784 static int
23785 expose_line (w, row, r)
23786 struct window *w;
23787 struct glyph_row *row;
23788 XRectangle *r;
23789 {
23790 xassert (row->enabled_p);
23791
23792 if (row->mode_line_p || w->pseudo_window_p)
23793 draw_glyphs (w, 0, row, TEXT_AREA,
23794 0, row->used[TEXT_AREA],
23795 DRAW_NORMAL_TEXT, 0);
23796 else
23797 {
23798 if (row->used[LEFT_MARGIN_AREA])
23799 expose_area (w, row, r, LEFT_MARGIN_AREA);
23800 if (row->used[TEXT_AREA])
23801 expose_area (w, row, r, TEXT_AREA);
23802 if (row->used[RIGHT_MARGIN_AREA])
23803 expose_area (w, row, r, RIGHT_MARGIN_AREA);
23804 draw_row_fringe_bitmaps (w, row);
23805 }
23806
23807 return row->mouse_face_p;
23808 }
23809
23810
23811 /* Redraw those parts of glyphs rows during expose event handling that
23812 overlap other rows. Redrawing of an exposed line writes over parts
23813 of lines overlapping that exposed line; this function fixes that.
23814
23815 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
23816 row in W's current matrix that is exposed and overlaps other rows.
23817 LAST_OVERLAPPING_ROW is the last such row. */
23818
23819 static void
23820 expose_overlaps (w, first_overlapping_row, last_overlapping_row, r)
23821 struct window *w;
23822 struct glyph_row *first_overlapping_row;
23823 struct glyph_row *last_overlapping_row;
23824 XRectangle *r;
23825 {
23826 struct glyph_row *row;
23827
23828 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
23829 if (row->overlapping_p)
23830 {
23831 xassert (row->enabled_p && !row->mode_line_p);
23832
23833 row->clip = r;
23834 if (row->used[LEFT_MARGIN_AREA])
23835 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
23836
23837 if (row->used[TEXT_AREA])
23838 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
23839
23840 if (row->used[RIGHT_MARGIN_AREA])
23841 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
23842 row->clip = NULL;
23843 }
23844 }
23845
23846
23847 /* Return non-zero if W's cursor intersects rectangle R. */
23848
23849 static int
23850 phys_cursor_in_rect_p (w, r)
23851 struct window *w;
23852 XRectangle *r;
23853 {
23854 XRectangle cr, result;
23855 struct glyph *cursor_glyph;
23856
23857 cursor_glyph = get_phys_cursor_glyph (w);
23858 if (cursor_glyph)
23859 {
23860 /* r is relative to W's box, but w->phys_cursor.x is relative
23861 to left edge of W's TEXT area. Adjust it. */
23862 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
23863 cr.y = w->phys_cursor.y;
23864 cr.width = cursor_glyph->pixel_width;
23865 cr.height = w->phys_cursor_height;
23866 /* ++KFS: W32 version used W32-specific IntersectRect here, but
23867 I assume the effect is the same -- and this is portable. */
23868 return x_intersect_rectangles (&cr, r, &result);
23869 }
23870 /* If we don't understand the format, pretend we're not in the hot-spot. */
23871 return 0;
23872 }
23873
23874
23875 /* EXPORT:
23876 Draw a vertical window border to the right of window W if W doesn't
23877 have vertical scroll bars. */
23878
23879 void
23880 x_draw_vertical_border (w)
23881 struct window *w;
23882 {
23883 struct frame *f = XFRAME (WINDOW_FRAME (w));
23884
23885 /* We could do better, if we knew what type of scroll-bar the adjacent
23886 windows (on either side) have... But we don't :-(
23887 However, I think this works ok. ++KFS 2003-04-25 */
23888
23889 /* Redraw borders between horizontally adjacent windows. Don't
23890 do it for frames with vertical scroll bars because either the
23891 right scroll bar of a window, or the left scroll bar of its
23892 neighbor will suffice as a border. */
23893 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
23894 return;
23895
23896 if (!WINDOW_RIGHTMOST_P (w)
23897 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
23898 {
23899 int x0, x1, y0, y1;
23900
23901 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
23902 y1 -= 1;
23903
23904 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
23905 x1 -= 1;
23906
23907 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
23908 }
23909 else if (!WINDOW_LEFTMOST_P (w)
23910 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
23911 {
23912 int x0, x1, y0, y1;
23913
23914 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
23915 y1 -= 1;
23916
23917 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
23918 x0 -= 1;
23919
23920 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
23921 }
23922 }
23923
23924
23925 /* Redraw the part of window W intersection rectangle FR. Pixel
23926 coordinates in FR are frame-relative. Call this function with
23927 input blocked. Value is non-zero if the exposure overwrites
23928 mouse-face. */
23929
23930 static int
23931 expose_window (w, fr)
23932 struct window *w;
23933 XRectangle *fr;
23934 {
23935 struct frame *f = XFRAME (w->frame);
23936 XRectangle wr, r;
23937 int mouse_face_overwritten_p = 0;
23938
23939 /* If window is not yet fully initialized, do nothing. This can
23940 happen when toolkit scroll bars are used and a window is split.
23941 Reconfiguring the scroll bar will generate an expose for a newly
23942 created window. */
23943 if (w->current_matrix == NULL)
23944 return 0;
23945
23946 /* When we're currently updating the window, display and current
23947 matrix usually don't agree. Arrange for a thorough display
23948 later. */
23949 if (w == updated_window)
23950 {
23951 SET_FRAME_GARBAGED (f);
23952 return 0;
23953 }
23954
23955 /* Frame-relative pixel rectangle of W. */
23956 wr.x = WINDOW_LEFT_EDGE_X (w);
23957 wr.y = WINDOW_TOP_EDGE_Y (w);
23958 wr.width = WINDOW_TOTAL_WIDTH (w);
23959 wr.height = WINDOW_TOTAL_HEIGHT (w);
23960
23961 if (x_intersect_rectangles (fr, &wr, &r))
23962 {
23963 int yb = window_text_bottom_y (w);
23964 struct glyph_row *row;
23965 int cursor_cleared_p;
23966 struct glyph_row *first_overlapping_row, *last_overlapping_row;
23967
23968 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
23969 r.x, r.y, r.width, r.height));
23970
23971 /* Convert to window coordinates. */
23972 r.x -= WINDOW_LEFT_EDGE_X (w);
23973 r.y -= WINDOW_TOP_EDGE_Y (w);
23974
23975 /* Turn off the cursor. */
23976 if (!w->pseudo_window_p
23977 && phys_cursor_in_rect_p (w, &r))
23978 {
23979 x_clear_cursor (w);
23980 cursor_cleared_p = 1;
23981 }
23982 else
23983 cursor_cleared_p = 0;
23984
23985 /* Update lines intersecting rectangle R. */
23986 first_overlapping_row = last_overlapping_row = NULL;
23987 for (row = w->current_matrix->rows;
23988 row->enabled_p;
23989 ++row)
23990 {
23991 int y0 = row->y;
23992 int y1 = MATRIX_ROW_BOTTOM_Y (row);
23993
23994 if ((y0 >= r.y && y0 < r.y + r.height)
23995 || (y1 > r.y && y1 < r.y + r.height)
23996 || (r.y >= y0 && r.y < y1)
23997 || (r.y + r.height > y0 && r.y + r.height < y1))
23998 {
23999 /* A header line may be overlapping, but there is no need
24000 to fix overlapping areas for them. KFS 2005-02-12 */
24001 if (row->overlapping_p && !row->mode_line_p)
24002 {
24003 if (first_overlapping_row == NULL)
24004 first_overlapping_row = row;
24005 last_overlapping_row = row;
24006 }
24007
24008 row->clip = fr;
24009 if (expose_line (w, row, &r))
24010 mouse_face_overwritten_p = 1;
24011 row->clip = NULL;
24012 }
24013 else if (row->overlapping_p)
24014 {
24015 /* We must redraw a row overlapping the exposed area. */
24016 if (y0 < r.y
24017 ? y0 + row->phys_height > r.y
24018 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
24019 {
24020 if (first_overlapping_row == NULL)
24021 first_overlapping_row = row;
24022 last_overlapping_row = row;
24023 }
24024 }
24025
24026 if (y1 >= yb)
24027 break;
24028 }
24029
24030 /* Display the mode line if there is one. */
24031 if (WINDOW_WANTS_MODELINE_P (w)
24032 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
24033 row->enabled_p)
24034 && row->y < r.y + r.height)
24035 {
24036 if (expose_line (w, row, &r))
24037 mouse_face_overwritten_p = 1;
24038 }
24039
24040 if (!w->pseudo_window_p)
24041 {
24042 /* Fix the display of overlapping rows. */
24043 if (first_overlapping_row)
24044 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
24045 fr);
24046
24047 /* Draw border between windows. */
24048 x_draw_vertical_border (w);
24049
24050 /* Turn the cursor on again. */
24051 if (cursor_cleared_p)
24052 update_window_cursor (w, 1);
24053 }
24054 }
24055
24056 return mouse_face_overwritten_p;
24057 }
24058
24059
24060
24061 /* Redraw (parts) of all windows in the window tree rooted at W that
24062 intersect R. R contains frame pixel coordinates. Value is
24063 non-zero if the exposure overwrites mouse-face. */
24064
24065 static int
24066 expose_window_tree (w, r)
24067 struct window *w;
24068 XRectangle *r;
24069 {
24070 struct frame *f = XFRAME (w->frame);
24071 int mouse_face_overwritten_p = 0;
24072
24073 while (w && !FRAME_GARBAGED_P (f))
24074 {
24075 if (!NILP (w->hchild))
24076 mouse_face_overwritten_p
24077 |= expose_window_tree (XWINDOW (w->hchild), r);
24078 else if (!NILP (w->vchild))
24079 mouse_face_overwritten_p
24080 |= expose_window_tree (XWINDOW (w->vchild), r);
24081 else
24082 mouse_face_overwritten_p |= expose_window (w, r);
24083
24084 w = NILP (w->next) ? NULL : XWINDOW (w->next);
24085 }
24086
24087 return mouse_face_overwritten_p;
24088 }
24089
24090
24091 /* EXPORT:
24092 Redisplay an exposed area of frame F. X and Y are the upper-left
24093 corner of the exposed rectangle. W and H are width and height of
24094 the exposed area. All are pixel values. W or H zero means redraw
24095 the entire frame. */
24096
24097 void
24098 expose_frame (f, x, y, w, h)
24099 struct frame *f;
24100 int x, y, w, h;
24101 {
24102 XRectangle r;
24103 int mouse_face_overwritten_p = 0;
24104
24105 TRACE ((stderr, "expose_frame "));
24106
24107 /* No need to redraw if frame will be redrawn soon. */
24108 if (FRAME_GARBAGED_P (f))
24109 {
24110 TRACE ((stderr, " garbaged\n"));
24111 return;
24112 }
24113
24114 /* If basic faces haven't been realized yet, there is no point in
24115 trying to redraw anything. This can happen when we get an expose
24116 event while Emacs is starting, e.g. by moving another window. */
24117 if (FRAME_FACE_CACHE (f) == NULL
24118 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
24119 {
24120 TRACE ((stderr, " no faces\n"));
24121 return;
24122 }
24123
24124 if (w == 0 || h == 0)
24125 {
24126 r.x = r.y = 0;
24127 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
24128 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
24129 }
24130 else
24131 {
24132 r.x = x;
24133 r.y = y;
24134 r.width = w;
24135 r.height = h;
24136 }
24137
24138 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
24139 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
24140
24141 if (WINDOWP (f->tool_bar_window))
24142 mouse_face_overwritten_p
24143 |= expose_window (XWINDOW (f->tool_bar_window), &r);
24144
24145 #ifdef HAVE_X_WINDOWS
24146 #ifndef MSDOS
24147 #ifndef USE_X_TOOLKIT
24148 if (WINDOWP (f->menu_bar_window))
24149 mouse_face_overwritten_p
24150 |= expose_window (XWINDOW (f->menu_bar_window), &r);
24151 #endif /* not USE_X_TOOLKIT */
24152 #endif
24153 #endif
24154
24155 /* Some window managers support a focus-follows-mouse style with
24156 delayed raising of frames. Imagine a partially obscured frame,
24157 and moving the mouse into partially obscured mouse-face on that
24158 frame. The visible part of the mouse-face will be highlighted,
24159 then the WM raises the obscured frame. With at least one WM, KDE
24160 2.1, Emacs is not getting any event for the raising of the frame
24161 (even tried with SubstructureRedirectMask), only Expose events.
24162 These expose events will draw text normally, i.e. not
24163 highlighted. Which means we must redo the highlight here.
24164 Subsume it under ``we love X''. --gerd 2001-08-15 */
24165 /* Included in Windows version because Windows most likely does not
24166 do the right thing if any third party tool offers
24167 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
24168 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
24169 {
24170 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
24171 if (f == dpyinfo->mouse_face_mouse_frame)
24172 {
24173 int x = dpyinfo->mouse_face_mouse_x;
24174 int y = dpyinfo->mouse_face_mouse_y;
24175 clear_mouse_face (dpyinfo);
24176 note_mouse_highlight (f, x, y);
24177 }
24178 }
24179 }
24180
24181
24182 /* EXPORT:
24183 Determine the intersection of two rectangles R1 and R2. Return
24184 the intersection in *RESULT. Value is non-zero if RESULT is not
24185 empty. */
24186
24187 int
24188 x_intersect_rectangles (r1, r2, result)
24189 XRectangle *r1, *r2, *result;
24190 {
24191 XRectangle *left, *right;
24192 XRectangle *upper, *lower;
24193 int intersection_p = 0;
24194
24195 /* Rearrange so that R1 is the left-most rectangle. */
24196 if (r1->x < r2->x)
24197 left = r1, right = r2;
24198 else
24199 left = r2, right = r1;
24200
24201 /* X0 of the intersection is right.x0, if this is inside R1,
24202 otherwise there is no intersection. */
24203 if (right->x <= left->x + left->width)
24204 {
24205 result->x = right->x;
24206
24207 /* The right end of the intersection is the minimum of the
24208 the right ends of left and right. */
24209 result->width = (min (left->x + left->width, right->x + right->width)
24210 - result->x);
24211
24212 /* Same game for Y. */
24213 if (r1->y < r2->y)
24214 upper = r1, lower = r2;
24215 else
24216 upper = r2, lower = r1;
24217
24218 /* The upper end of the intersection is lower.y0, if this is inside
24219 of upper. Otherwise, there is no intersection. */
24220 if (lower->y <= upper->y + upper->height)
24221 {
24222 result->y = lower->y;
24223
24224 /* The lower end of the intersection is the minimum of the lower
24225 ends of upper and lower. */
24226 result->height = (min (lower->y + lower->height,
24227 upper->y + upper->height)
24228 - result->y);
24229 intersection_p = 1;
24230 }
24231 }
24232
24233 return intersection_p;
24234 }
24235
24236 #endif /* HAVE_WINDOW_SYSTEM */
24237
24238 \f
24239 /***********************************************************************
24240 Initialization
24241 ***********************************************************************/
24242
24243 void
24244 syms_of_xdisp ()
24245 {
24246 Vwith_echo_area_save_vector = Qnil;
24247 staticpro (&Vwith_echo_area_save_vector);
24248
24249 Vmessage_stack = Qnil;
24250 staticpro (&Vmessage_stack);
24251
24252 Qinhibit_redisplay = intern ("inhibit-redisplay");
24253 staticpro (&Qinhibit_redisplay);
24254
24255 message_dolog_marker1 = Fmake_marker ();
24256 staticpro (&message_dolog_marker1);
24257 message_dolog_marker2 = Fmake_marker ();
24258 staticpro (&message_dolog_marker2);
24259 message_dolog_marker3 = Fmake_marker ();
24260 staticpro (&message_dolog_marker3);
24261
24262 #if GLYPH_DEBUG
24263 defsubr (&Sdump_frame_glyph_matrix);
24264 defsubr (&Sdump_glyph_matrix);
24265 defsubr (&Sdump_glyph_row);
24266 defsubr (&Sdump_tool_bar_row);
24267 defsubr (&Strace_redisplay);
24268 defsubr (&Strace_to_stderr);
24269 #endif
24270 #ifdef HAVE_WINDOW_SYSTEM
24271 defsubr (&Stool_bar_lines_needed);
24272 defsubr (&Slookup_image_map);
24273 #endif
24274 defsubr (&Sformat_mode_line);
24275 defsubr (&Sinvisible_p);
24276
24277 staticpro (&Qmenu_bar_update_hook);
24278 Qmenu_bar_update_hook = intern ("menu-bar-update-hook");
24279
24280 staticpro (&Qoverriding_terminal_local_map);
24281 Qoverriding_terminal_local_map = intern ("overriding-terminal-local-map");
24282
24283 staticpro (&Qoverriding_local_map);
24284 Qoverriding_local_map = intern ("overriding-local-map");
24285
24286 staticpro (&Qwindow_scroll_functions);
24287 Qwindow_scroll_functions = intern ("window-scroll-functions");
24288
24289 staticpro (&Qredisplay_end_trigger_functions);
24290 Qredisplay_end_trigger_functions = intern ("redisplay-end-trigger-functions");
24291
24292 staticpro (&Qinhibit_point_motion_hooks);
24293 Qinhibit_point_motion_hooks = intern ("inhibit-point-motion-hooks");
24294
24295 QCdata = intern (":data");
24296 staticpro (&QCdata);
24297 Qdisplay = intern ("display");
24298 staticpro (&Qdisplay);
24299 Qspace_width = intern ("space-width");
24300 staticpro (&Qspace_width);
24301 Qraise = intern ("raise");
24302 staticpro (&Qraise);
24303 Qslice = intern ("slice");
24304 staticpro (&Qslice);
24305 Qspace = intern ("space");
24306 staticpro (&Qspace);
24307 Qmargin = intern ("margin");
24308 staticpro (&Qmargin);
24309 Qpointer = intern ("pointer");
24310 staticpro (&Qpointer);
24311 Qleft_margin = intern ("left-margin");
24312 staticpro (&Qleft_margin);
24313 Qright_margin = intern ("right-margin");
24314 staticpro (&Qright_margin);
24315 Qcenter = intern ("center");
24316 staticpro (&Qcenter);
24317 Qline_height = intern ("line-height");
24318 staticpro (&Qline_height);
24319 QCalign_to = intern (":align-to");
24320 staticpro (&QCalign_to);
24321 QCrelative_width = intern (":relative-width");
24322 staticpro (&QCrelative_width);
24323 QCrelative_height = intern (":relative-height");
24324 staticpro (&QCrelative_height);
24325 QCeval = intern (":eval");
24326 staticpro (&QCeval);
24327 QCpropertize = intern (":propertize");
24328 staticpro (&QCpropertize);
24329 QCfile = intern (":file");
24330 staticpro (&QCfile);
24331 Qfontified = intern ("fontified");
24332 staticpro (&Qfontified);
24333 Qfontification_functions = intern ("fontification-functions");
24334 staticpro (&Qfontification_functions);
24335 Qtrailing_whitespace = intern ("trailing-whitespace");
24336 staticpro (&Qtrailing_whitespace);
24337 Qescape_glyph = intern ("escape-glyph");
24338 staticpro (&Qescape_glyph);
24339 Qnobreak_space = intern ("nobreak-space");
24340 staticpro (&Qnobreak_space);
24341 Qimage = intern ("image");
24342 staticpro (&Qimage);
24343 QCmap = intern (":map");
24344 staticpro (&QCmap);
24345 QCpointer = intern (":pointer");
24346 staticpro (&QCpointer);
24347 Qrect = intern ("rect");
24348 staticpro (&Qrect);
24349 Qcircle = intern ("circle");
24350 staticpro (&Qcircle);
24351 Qpoly = intern ("poly");
24352 staticpro (&Qpoly);
24353 Qmessage_truncate_lines = intern ("message-truncate-lines");
24354 staticpro (&Qmessage_truncate_lines);
24355 Qgrow_only = intern ("grow-only");
24356 staticpro (&Qgrow_only);
24357 Qinhibit_menubar_update = intern ("inhibit-menubar-update");
24358 staticpro (&Qinhibit_menubar_update);
24359 Qinhibit_eval_during_redisplay = intern ("inhibit-eval-during-redisplay");
24360 staticpro (&Qinhibit_eval_during_redisplay);
24361 Qposition = intern ("position");
24362 staticpro (&Qposition);
24363 Qbuffer_position = intern ("buffer-position");
24364 staticpro (&Qbuffer_position);
24365 Qobject = intern ("object");
24366 staticpro (&Qobject);
24367 Qbar = intern ("bar");
24368 staticpro (&Qbar);
24369 Qhbar = intern ("hbar");
24370 staticpro (&Qhbar);
24371 Qbox = intern ("box");
24372 staticpro (&Qbox);
24373 Qhollow = intern ("hollow");
24374 staticpro (&Qhollow);
24375 Qhand = intern ("hand");
24376 staticpro (&Qhand);
24377 Qarrow = intern ("arrow");
24378 staticpro (&Qarrow);
24379 Qtext = intern ("text");
24380 staticpro (&Qtext);
24381 Qrisky_local_variable = intern ("risky-local-variable");
24382 staticpro (&Qrisky_local_variable);
24383 Qinhibit_free_realized_faces = intern ("inhibit-free-realized-faces");
24384 staticpro (&Qinhibit_free_realized_faces);
24385
24386 list_of_error = Fcons (Fcons (intern ("error"),
24387 Fcons (intern ("void-variable"), Qnil)),
24388 Qnil);
24389 staticpro (&list_of_error);
24390
24391 Qlast_arrow_position = intern ("last-arrow-position");
24392 staticpro (&Qlast_arrow_position);
24393 Qlast_arrow_string = intern ("last-arrow-string");
24394 staticpro (&Qlast_arrow_string);
24395
24396 Qoverlay_arrow_string = intern ("overlay-arrow-string");
24397 staticpro (&Qoverlay_arrow_string);
24398 Qoverlay_arrow_bitmap = intern ("overlay-arrow-bitmap");
24399 staticpro (&Qoverlay_arrow_bitmap);
24400
24401 echo_buffer[0] = echo_buffer[1] = Qnil;
24402 staticpro (&echo_buffer[0]);
24403 staticpro (&echo_buffer[1]);
24404
24405 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
24406 staticpro (&echo_area_buffer[0]);
24407 staticpro (&echo_area_buffer[1]);
24408
24409 Vmessages_buffer_name = build_string ("*Messages*");
24410 staticpro (&Vmessages_buffer_name);
24411
24412 mode_line_proptrans_alist = Qnil;
24413 staticpro (&mode_line_proptrans_alist);
24414 mode_line_string_list = Qnil;
24415 staticpro (&mode_line_string_list);
24416 mode_line_string_face = Qnil;
24417 staticpro (&mode_line_string_face);
24418 mode_line_string_face_prop = Qnil;
24419 staticpro (&mode_line_string_face_prop);
24420 Vmode_line_unwind_vector = Qnil;
24421 staticpro (&Vmode_line_unwind_vector);
24422
24423 help_echo_string = Qnil;
24424 staticpro (&help_echo_string);
24425 help_echo_object = Qnil;
24426 staticpro (&help_echo_object);
24427 help_echo_window = Qnil;
24428 staticpro (&help_echo_window);
24429 previous_help_echo_string = Qnil;
24430 staticpro (&previous_help_echo_string);
24431 help_echo_pos = -1;
24432
24433 #ifdef HAVE_WINDOW_SYSTEM
24434 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
24435 doc: /* *Non-nil means draw block cursor as wide as the glyph under it.
24436 For example, if a block cursor is over a tab, it will be drawn as
24437 wide as that tab on the display. */);
24438 x_stretch_cursor_p = 0;
24439 #endif
24440
24441 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
24442 doc: /* *Non-nil means highlight trailing whitespace.
24443 The face used for trailing whitespace is `trailing-whitespace'. */);
24444 Vshow_trailing_whitespace = Qnil;
24445
24446 DEFVAR_LISP ("nobreak-char-display", &Vnobreak_char_display,
24447 doc: /* *Control highlighting of nobreak space and soft hyphen.
24448 A value of t means highlight the character itself (for nobreak space,
24449 use face `nobreak-space').
24450 A value of nil means no highlighting.
24451 Other values mean display the escape glyph followed by an ordinary
24452 space or ordinary hyphen. */);
24453 Vnobreak_char_display = Qt;
24454
24455 DEFVAR_LISP ("void-text-area-pointer", &Vvoid_text_area_pointer,
24456 doc: /* *The pointer shape to show in void text areas.
24457 A value of nil means to show the text pointer. Other options are `arrow',
24458 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
24459 Vvoid_text_area_pointer = Qarrow;
24460
24461 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
24462 doc: /* Non-nil means don't actually do any redisplay.
24463 This is used for internal purposes. */);
24464 Vinhibit_redisplay = Qnil;
24465
24466 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
24467 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
24468 Vglobal_mode_string = Qnil;
24469
24470 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
24471 doc: /* Marker for where to display an arrow on top of the buffer text.
24472 This must be the beginning of a line in order to work.
24473 See also `overlay-arrow-string'. */);
24474 Voverlay_arrow_position = Qnil;
24475
24476 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
24477 doc: /* String to display as an arrow in non-window frames.
24478 See also `overlay-arrow-position'. */);
24479 Voverlay_arrow_string = build_string ("=>");
24480
24481 DEFVAR_LISP ("overlay-arrow-variable-list", &Voverlay_arrow_variable_list,
24482 doc: /* List of variables (symbols) which hold markers for overlay arrows.
24483 The symbols on this list are examined during redisplay to determine
24484 where to display overlay arrows. */);
24485 Voverlay_arrow_variable_list
24486 = Fcons (intern ("overlay-arrow-position"), Qnil);
24487
24488 DEFVAR_INT ("scroll-step", &scroll_step,
24489 doc: /* *The number of lines to try scrolling a window by when point moves out.
24490 If that fails to bring point back on frame, point is centered instead.
24491 If this is zero, point is always centered after it moves off frame.
24492 If you want scrolling to always be a line at a time, you should set
24493 `scroll-conservatively' to a large value rather than set this to 1. */);
24494
24495 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
24496 doc: /* *Scroll up to this many lines, to bring point back on screen.
24497 A value of zero means to scroll the text to center point vertically
24498 in the window. */);
24499 scroll_conservatively = 0;
24500
24501 DEFVAR_INT ("scroll-margin", &scroll_margin,
24502 doc: /* *Number of lines of margin at the top and bottom of a window.
24503 Recenter the window whenever point gets within this many lines
24504 of the top or bottom of the window. */);
24505 scroll_margin = 0;
24506
24507 DEFVAR_LISP ("display-pixels-per-inch", &Vdisplay_pixels_per_inch,
24508 doc: /* Pixels per inch value for non-window system displays.
24509 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
24510 Vdisplay_pixels_per_inch = make_float (72.0);
24511
24512 #if GLYPH_DEBUG
24513 DEFVAR_INT ("debug-end-pos", &debug_end_pos, doc: /* Don't ask. */);
24514 #endif
24515
24516 DEFVAR_BOOL ("truncate-partial-width-windows",
24517 &truncate_partial_width_windows,
24518 doc: /* *Non-nil means truncate lines in all windows less than full frame wide. */);
24519 truncate_partial_width_windows = 1;
24520
24521 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
24522 doc: /* When nil, display the mode-line/header-line/menu-bar in the default face.
24523 Any other value means to use the appropriate face, `mode-line',
24524 `header-line', or `menu' respectively. */);
24525 mode_line_inverse_video = 1;
24526
24527 DEFVAR_LISP ("line-number-display-limit", &Vline_number_display_limit,
24528 doc: /* *Maximum buffer size for which line number should be displayed.
24529 If the buffer is bigger than this, the line number does not appear
24530 in the mode line. A value of nil means no limit. */);
24531 Vline_number_display_limit = Qnil;
24532
24533 DEFVAR_INT ("line-number-display-limit-width",
24534 &line_number_display_limit_width,
24535 doc: /* *Maximum line width (in characters) for line number display.
24536 If the average length of the lines near point is bigger than this, then the
24537 line number may be omitted from the mode line. */);
24538 line_number_display_limit_width = 200;
24539
24540 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
24541 doc: /* *Non-nil means highlight region even in nonselected windows. */);
24542 highlight_nonselected_windows = 0;
24543
24544 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
24545 doc: /* Non-nil if more than one frame is visible on this display.
24546 Minibuffer-only frames don't count, but iconified frames do.
24547 This variable is not guaranteed to be accurate except while processing
24548 `frame-title-format' and `icon-title-format'. */);
24549
24550 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
24551 doc: /* Template for displaying the title bar of visible frames.
24552 \(Assuming the window manager supports this feature.)
24553
24554 This variable has the same structure as `mode-line-format', except that
24555 the %c and %l constructs are ignored. It is used only on frames for
24556 which no explicit name has been set \(see `modify-frame-parameters'). */);
24557
24558 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
24559 doc: /* Template for displaying the title bar of an iconified frame.
24560 \(Assuming the window manager supports this feature.)
24561 This variable has the same structure as `mode-line-format' (which see),
24562 and is used only on frames for which no explicit name has been set
24563 \(see `modify-frame-parameters'). */);
24564 Vicon_title_format
24565 = Vframe_title_format
24566 = Fcons (intern ("multiple-frames"),
24567 Fcons (build_string ("%b"),
24568 Fcons (Fcons (empty_unibyte_string,
24569 Fcons (intern ("invocation-name"),
24570 Fcons (build_string ("@"),
24571 Fcons (intern ("system-name"),
24572 Qnil)))),
24573 Qnil)));
24574
24575 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
24576 doc: /* Maximum number of lines to keep in the message log buffer.
24577 If nil, disable message logging. If t, log messages but don't truncate
24578 the buffer when it becomes large. */);
24579 Vmessage_log_max = make_number (100);
24580
24581 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
24582 doc: /* Functions called before redisplay, if window sizes have changed.
24583 The value should be a list of functions that take one argument.
24584 Just before redisplay, for each frame, if any of its windows have changed
24585 size since the last redisplay, or have been split or deleted,
24586 all the functions in the list are called, with the frame as argument. */);
24587 Vwindow_size_change_functions = Qnil;
24588
24589 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
24590 doc: /* List of functions to call before redisplaying a window with scrolling.
24591 Each function is called with two arguments, the window
24592 and its new display-start position. Note that the value of `window-end'
24593 is not valid when these functions are called. */);
24594 Vwindow_scroll_functions = Qnil;
24595
24596 DEFVAR_LISP ("redisplay-end-trigger-functions", &Vredisplay_end_trigger_functions,
24597 doc: /* Functions called when redisplay of a window reaches the end trigger.
24598 Each function is called with two arguments, the window and the end trigger value.
24599 See `set-window-redisplay-end-trigger'. */);
24600 Vredisplay_end_trigger_functions = Qnil;
24601
24602 DEFVAR_LISP ("mouse-autoselect-window", &Vmouse_autoselect_window,
24603 doc: /* *Non-nil means autoselect window with mouse pointer.
24604 If nil, do not autoselect windows.
24605 A positive number means delay autoselection by that many seconds: a
24606 window is autoselected only after the mouse has remained in that
24607 window for the duration of the delay.
24608 A negative number has a similar effect, but causes windows to be
24609 autoselected only after the mouse has stopped moving. \(Because of
24610 the way Emacs compares mouse events, you will occasionally wait twice
24611 that time before the window gets selected.\)
24612 Any other value means to autoselect window instantaneously when the
24613 mouse pointer enters it.
24614
24615 Autoselection selects the minibuffer only if it is active, and never
24616 unselects the minibuffer if it is active.
24617
24618 When customizing this variable make sure that the actual value of
24619 `focus-follows-mouse' matches the behavior of your window manager. */);
24620 Vmouse_autoselect_window = Qnil;
24621
24622 DEFVAR_LISP ("auto-resize-tool-bars", &Vauto_resize_tool_bars,
24623 doc: /* *Non-nil means automatically resize tool-bars.
24624 This dynamically changes the tool-bar's height to the minimum height
24625 that is needed to make all tool-bar items visible.
24626 If value is `grow-only', the tool-bar's height is only increased
24627 automatically; to decrease the tool-bar height, use \\[recenter]. */);
24628 Vauto_resize_tool_bars = Qt;
24629
24630 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
24631 doc: /* *Non-nil means raise tool-bar buttons when the mouse moves over them. */);
24632 auto_raise_tool_bar_buttons_p = 1;
24633
24634 DEFVAR_BOOL ("make-cursor-line-fully-visible", &make_cursor_line_fully_visible_p,
24635 doc: /* *Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
24636 make_cursor_line_fully_visible_p = 1;
24637
24638 DEFVAR_LISP ("tool-bar-border", &Vtool_bar_border,
24639 doc: /* *Border below tool-bar in pixels.
24640 If an integer, use it as the height of the border.
24641 If it is one of `internal-border-width' or `border-width', use the
24642 value of the corresponding frame parameter.
24643 Otherwise, no border is added below the tool-bar. */);
24644 Vtool_bar_border = Qinternal_border_width;
24645
24646 DEFVAR_LISP ("tool-bar-button-margin", &Vtool_bar_button_margin,
24647 doc: /* *Margin around tool-bar buttons in pixels.
24648 If an integer, use that for both horizontal and vertical margins.
24649 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
24650 HORZ specifying the horizontal margin, and VERT specifying the
24651 vertical margin. */);
24652 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
24653
24654 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
24655 doc: /* *Relief thickness of tool-bar buttons. */);
24656 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
24657
24658 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
24659 doc: /* List of functions to call to fontify regions of text.
24660 Each function is called with one argument POS. Functions must
24661 fontify a region starting at POS in the current buffer, and give
24662 fontified regions the property `fontified'. */);
24663 Vfontification_functions = Qnil;
24664 Fmake_variable_buffer_local (Qfontification_functions);
24665
24666 DEFVAR_BOOL ("unibyte-display-via-language-environment",
24667 &unibyte_display_via_language_environment,
24668 doc: /* *Non-nil means display unibyte text according to language environment.
24669 Specifically this means that unibyte non-ASCII characters
24670 are displayed by converting them to the equivalent multibyte characters
24671 according to the current language environment. As a result, they are
24672 displayed according to the current fontset. */);
24673 unibyte_display_via_language_environment = 0;
24674
24675 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
24676 doc: /* *Maximum height for resizing mini-windows.
24677 If a float, it specifies a fraction of the mini-window frame's height.
24678 If an integer, it specifies a number of lines. */);
24679 Vmax_mini_window_height = make_float (0.25);
24680
24681 DEFVAR_LISP ("resize-mini-windows", &Vresize_mini_windows,
24682 doc: /* *How to resize mini-windows.
24683 A value of nil means don't automatically resize mini-windows.
24684 A value of t means resize them to fit the text displayed in them.
24685 A value of `grow-only', the default, means let mini-windows grow
24686 only, until their display becomes empty, at which point the windows
24687 go back to their normal size. */);
24688 Vresize_mini_windows = Qgrow_only;
24689
24690 DEFVAR_LISP ("blink-cursor-alist", &Vblink_cursor_alist,
24691 doc: /* Alist specifying how to blink the cursor off.
24692 Each element has the form (ON-STATE . OFF-STATE). Whenever the
24693 `cursor-type' frame-parameter or variable equals ON-STATE,
24694 comparing using `equal', Emacs uses OFF-STATE to specify
24695 how to blink it off. ON-STATE and OFF-STATE are values for
24696 the `cursor-type' frame parameter.
24697
24698 If a frame's ON-STATE has no entry in this list,
24699 the frame's other specifications determine how to blink the cursor off. */);
24700 Vblink_cursor_alist = Qnil;
24701
24702 DEFVAR_BOOL ("auto-hscroll-mode", &automatic_hscrolling_p,
24703 doc: /* *Non-nil means scroll the display automatically to make point visible. */);
24704 automatic_hscrolling_p = 1;
24705
24706 DEFVAR_INT ("hscroll-margin", &hscroll_margin,
24707 doc: /* *How many columns away from the window edge point is allowed to get
24708 before automatic hscrolling will horizontally scroll the window. */);
24709 hscroll_margin = 5;
24710
24711 DEFVAR_LISP ("hscroll-step", &Vhscroll_step,
24712 doc: /* *How many columns to scroll the window when point gets too close to the edge.
24713 When point is less than `hscroll-margin' columns from the window
24714 edge, automatic hscrolling will scroll the window by the amount of columns
24715 determined by this variable. If its value is a positive integer, scroll that
24716 many columns. If it's a positive floating-point number, it specifies the
24717 fraction of the window's width to scroll. If it's nil or zero, point will be
24718 centered horizontally after the scroll. Any other value, including negative
24719 numbers, are treated as if the value were zero.
24720
24721 Automatic hscrolling always moves point outside the scroll margin, so if
24722 point was more than scroll step columns inside the margin, the window will
24723 scroll more than the value given by the scroll step.
24724
24725 Note that the lower bound for automatic hscrolling specified by `scroll-left'
24726 and `scroll-right' overrides this variable's effect. */);
24727 Vhscroll_step = make_number (0);
24728
24729 DEFVAR_BOOL ("message-truncate-lines", &message_truncate_lines,
24730 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
24731 Bind this around calls to `message' to let it take effect. */);
24732 message_truncate_lines = 0;
24733
24734 DEFVAR_LISP ("menu-bar-update-hook", &Vmenu_bar_update_hook,
24735 doc: /* Normal hook run to update the menu bar definitions.
24736 Redisplay runs this hook before it redisplays the menu bar.
24737 This is used to update submenus such as Buffers,
24738 whose contents depend on various data. */);
24739 Vmenu_bar_update_hook = Qnil;
24740
24741 DEFVAR_LISP ("menu-updating-frame", &Vmenu_updating_frame,
24742 doc: /* Frame for which we are updating a menu.
24743 The enable predicate for a menu binding should check this variable. */);
24744 Vmenu_updating_frame = Qnil;
24745
24746 DEFVAR_BOOL ("inhibit-menubar-update", &inhibit_menubar_update,
24747 doc: /* Non-nil means don't update menu bars. Internal use only. */);
24748 inhibit_menubar_update = 0;
24749
24750 DEFVAR_BOOL ("inhibit-eval-during-redisplay", &inhibit_eval_during_redisplay,
24751 doc: /* Non-nil means don't eval Lisp during redisplay. */);
24752 inhibit_eval_during_redisplay = 0;
24753
24754 DEFVAR_BOOL ("inhibit-free-realized-faces", &inhibit_free_realized_faces,
24755 doc: /* Non-nil means don't free realized faces. Internal use only. */);
24756 inhibit_free_realized_faces = 0;
24757
24758 #if GLYPH_DEBUG
24759 DEFVAR_BOOL ("inhibit-try-window-id", &inhibit_try_window_id,
24760 doc: /* Inhibit try_window_id display optimization. */);
24761 inhibit_try_window_id = 0;
24762
24763 DEFVAR_BOOL ("inhibit-try-window-reusing", &inhibit_try_window_reusing,
24764 doc: /* Inhibit try_window_reusing display optimization. */);
24765 inhibit_try_window_reusing = 0;
24766
24767 DEFVAR_BOOL ("inhibit-try-cursor-movement", &inhibit_try_cursor_movement,
24768 doc: /* Inhibit try_cursor_movement display optimization. */);
24769 inhibit_try_cursor_movement = 0;
24770 #endif /* GLYPH_DEBUG */
24771
24772 DEFVAR_INT ("overline-margin", &overline_margin,
24773 doc: /* *Space between overline and text, in pixels.
24774 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
24775 margin to the caracter height. */);
24776 overline_margin = 2;
24777 }
24778
24779
24780 /* Initialize this module when Emacs starts. */
24781
24782 void
24783 init_xdisp ()
24784 {
24785 Lisp_Object root_window;
24786 struct window *mini_w;
24787
24788 current_header_line_height = current_mode_line_height = -1;
24789
24790 CHARPOS (this_line_start_pos) = 0;
24791
24792 mini_w = XWINDOW (minibuf_window);
24793 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
24794
24795 if (!noninteractive)
24796 {
24797 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
24798 int i;
24799
24800 XWINDOW (root_window)->top_line = make_number (FRAME_TOP_MARGIN (f));
24801 set_window_height (root_window,
24802 FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f),
24803 0);
24804 mini_w->top_line = make_number (FRAME_LINES (f) - 1);
24805 set_window_height (minibuf_window, 1, 0);
24806
24807 XWINDOW (root_window)->total_cols = make_number (FRAME_COLS (f));
24808 mini_w->total_cols = make_number (FRAME_COLS (f));
24809
24810 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
24811 scratch_glyph_row.glyphs[TEXT_AREA + 1]
24812 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
24813
24814 /* The default ellipsis glyphs `...'. */
24815 for (i = 0; i < 3; ++i)
24816 default_invis_vector[i] = make_number ('.');
24817 }
24818
24819 {
24820 /* Allocate the buffer for frame titles.
24821 Also used for `format-mode-line'. */
24822 int size = 100;
24823 mode_line_noprop_buf = (char *) xmalloc (size);
24824 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
24825 mode_line_noprop_ptr = mode_line_noprop_buf;
24826 mode_line_target = MODE_LINE_DISPLAY;
24827 }
24828
24829 help_echo_showing_p = 0;
24830 }
24831
24832
24833 /* arch-tag: eacc864d-bb6a-4b74-894a-1a4399a1358b
24834 (do not change this comment) */